public ActionResult <string> ReverseEntry([FromForm] ReverseEntryDataTE data)
        {
            string result                    = "Not Done";
            int    ProfitDeduction           = 0;
            int    BillAmountDeduction       = 0;
            List <ProdAddHistoryTE> products = _context.prodAddHistoryData
                                               .Where(x => x.ProductId == data.Productid && x.Size == data.Size).ToList();

            /*if (products != null)
             * {
             *  int Total = 0;
             * foreach (var prod in products)
             * {
             *  Total += prod.Cost;
             * }*/
            //UnitPrice = Total / products.Count;
            ProductProfitService profitService = new ProductProfitService(_context);

            ProfitDeduction = profitService
                              .CalculatingProductProfitForReversalProduct(data.Productid, data.Size, data.Billnumber, data.Quantity, data.Saleprice);
            BillAmountDeduction = data.Quantity * data.Saleprice;
            var stock = _context.stocks
                        .SingleOrDefault(x => x.ProductId == data.Productid && x.Size.ToUpper() == data.Size.ToUpper());

            if (stock != null)
            {
                stock.Quantity += data.Quantity;
                _context.Entry(stock).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                _context.SaveChanges();



                //Modifying Bill Table
                var BillData = _context.billscollections.Single(x => x.Billnumber == Convert.ToInt32(data.Billnumber));
                if (BillData != null)
                {
                    BillData.Billamount           -= BillAmountDeduction;
                    BillData.Payableamount        -= BillAmountDeduction;
                    BillData.Billprofit           -= ProfitDeduction;
                    BillData.Isbillmodified        = true;
                    _context.Entry(BillData).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                    _context.SaveChanges();

                    // Modifiying - Pending BillData Table
                    var PendingBillData            = _context.billspending.Single(x => x.Billnumber == Convert.ToInt32(data.Billnumber));
                    int CustPreviousPendingBalance = 0;
                    if (PendingBillData.Pendingamount <= BillAmountDeduction)
                    {
                        CustPreviousPendingBalance            = PendingBillData.Pendingamount;
                        PendingBillData.Pendingamount         = 0;
                        PendingBillData.Iscompleted           = true;
                        _context.Entry(PendingBillData).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                        _context.SaveChanges();
                    }
                    else
                    {
                        PendingBillData.Pendingamount -= BillAmountDeduction;

                        if (PendingBillData.Pendingamount <= 0)
                        {
                            PendingBillData.Iscompleted = true;
                        }

                        _context.Entry(PendingBillData).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                        _context.SaveChanges();
                    }

                    //Modifying SaleHistoryTable
                    var SaleTableData = _context.saleswithCustomerIds
                                        .Where(x => x.Custid == BillData.Customerid.ToString() &&
                                               x.Productid == data.Productid &&
                                               x.Prodsize == data.Size &&
                                               x.Purchasedate <= BillData.Billdate);
                    int MaxId = SaleTableData.Max(x => x.Id);
                    SalewithCustIdTE SaleTableObj = SaleTableData.Single(x => x.Id == MaxId);
                    SaleTableObj.Quantity             -= data.Quantity;
                    SaleTableObj.TotalCost            -= data.Quantity * data.Saleprice;
                    _context.Entry(SaleTableObj).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                    _context.SaveChanges();

                    //Adding Reverse History in DB
                    data.Date = DateTime.Now;
                    _context.Add(data);
                    _context.SaveChanges();

                    //Modifying Global Cash
                    bool isDataAvailable = _context.cashposition.Any();
                    if (isDataAvailable && PendingBillData.Pendingamount < BillAmountDeduction)
                    {
                        int Id = _context.cashposition.Max(x => x.Id);
                        var CashPositionData = _context.cashposition.Single(x => x.Id == Id);
                        int TotalDeduction   = BillAmountDeduction - CustPreviousPendingBalance;
                        CashPositionData.Globalcash            = CashPositionData.Globalcash - TotalDeduction;
                        _context.Entry(CashPositionData).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                        _context.SaveChanges();
                    }

                    result = "Reverse Done!";
                }
                else
                {
                    result = "Bill Not Found";
                }
            }
            else
            {
                result = "Stock Not Found";
            }

            return(Ok(result));
        }
Esempio n. 2
0
        public ActionResult <List <ProdSaleHistoryReportModel> > GetProductSaleHistory([FromForm] ViewTemplateModelOne data, string groupvalue)
        {
            bool     IsFromDateValid = false;
            bool     IsEndDateValid  = false;
            DateTime FromDate;
            string   CompleteFromDateString = "";
            DateTime EndDate;
            string   CompleteEndDateString = "";
            DateTime dt;
            bool     SelectOne   = false;
            bool     SelectTwo   = false;
            bool     SelectThree = false;
            List <SalewithCustIdTE>           products        = new List <SalewithCustIdTE>();
            List <ProdSaleHistoryReportModel> ReportModelList = new List <ProdSaleHistoryReportModel>();

            string Result   = "";
            bool   NotFound = false;
            bool   IsGroup  = groupvalue == "GROUP" ? true : false;

            if (data.FromDate != null && data.EndDate != null)
            {
                DateService DateService1 = new DateService();
                CompleteFromDateString = DateService1.GetCompleteFromDate(data.FromDate);
                IsFromDateValid        = DateTime.TryParse(CompleteFromDateString, out dt);

                DateService DateService2 = new DateService();
                CompleteEndDateString = DateService2.GetCompleteEndDate(data.EndDate);
                IsEndDateValid        = DateTime.TryParse(CompleteEndDateString, out dt);
            }
            if (IsFromDateValid && IsEndDateValid)
            {
                FromDate = Convert.ToDateTime(CompleteFromDateString);
                EndDate  = Convert.ToDateTime(CompleteEndDateString);
                if (data.CategoryValue != null && data.ProductValue == null && data.SizeValue == null)
                {
                    SelectOne = true;
                    var ProductsByCategoryId = context.products.Where(x => x.CategoryId.ToString() == data.CategoryValue).ToList();
                    if (ProductsByCategoryId.Count() != 0)
                    {
                        foreach (var ProductsById in ProductsByCategoryId)
                        {
                            var collection = context.saleswithCustomerIds.Where(x => x.Productid == ProductsById.Id && x.Purchasedate >= FromDate && x.Purchasedate <= EndDate).OrderBy(x => x.Prodsize).ToList();
                            foreach (var prod in collection)
                            {
                                products.Add(prod);
                            }
                        }
                        // return Ok(products);
                    }
                    else
                    {
                        Result   = "NotFound";
                        NotFound = true;
                    }
                }
                else if (data.ProductValue != null && data.SizeValue == null)
                {
                    SelectTwo = true;
                    var DataCollection = context
                                         .saleswithCustomerIds
                                         .Where(x => x.Productid.ToString() == data.ProductValue && x.Purchasedate >= FromDate && x.Purchasedate <= EndDate).OrderBy(x => x.Prodsize).ToList();
                    if (DataCollection.Count() != 0)
                    {
                        products = DataCollection;
                    }
                    else
                    {
                        Result   = "NotFound";
                        NotFound = true;
                    }
                }
                else if (data.ProductValue != null && data.SizeValue != null)
                {
                    SelectThree = true;
                    var DataBySize = context.saleswithCustomerIds
                                     .Where(x => x.Productid.ToString() == data.ProductValue && x.Prodsize == data.SizeValue && x.Purchasedate >= FromDate && x.Purchasedate <= EndDate).ToList();
                    if (DataBySize.Count() > 0)
                    {
                        products = DataBySize;
                    }
                    else
                    {
                        Result   = "NotFound";
                        NotFound = true;
                    }
                }

                if (products.Count() > 0 && SelectOne && IsGroup)
                {
                    var GroupBySizeSelectOne = products.GroupBy(x => new { x.Productid, x.Prodsize }).Select(z => new SalewithCustIdTE()
                    {
                        Productid = z.Key.Productid,
                        Prodsize  = z.Key.Prodsize,
                        Quantity  = z.Sum(x => x.Quantity),
                        Unitprice = z.Sum(x => x.Unitprice) / z.Count(),
                    });
                    products = GroupBySizeSelectOne.ToList();
                }

                else if (products.Count() > 0 && SelectTwo && IsGroup)
                {
                    var GroupBySelectTwo = products.GroupBy(x => x.Prodsize).Select(z => new SalewithCustIdTE()
                    {
                        Productid = products[0].Productid,
                        Prodsize  = z.Key,
                        Quantity  = z.Sum(x => x.Quantity),
                        Unitprice = z.Sum(x => x.Unitprice) / z.Count(),
                    });
                    products = GroupBySelectTwo.ToList();
                }

                else if (products.Count() > 0 && SelectThree && IsGroup)
                {
                    SalewithCustIdTE SelectThreeData = new SalewithCustIdTE()
                    {
                        Productid = products[0].Productid,
                        Prodsize  = products[0].Prodsize,
                        Quantity  = products.Sum(x => x.Quantity),
                        Unitprice = products.Sum(x => x.Unitprice) / products.Count()
                    };
                    products.Clear();
                    products.Add(SelectThreeData);
                }

                //Produt Name not avilable in products thats why converting into new Model with productName!!
                if (products.Count() > 0)
                {
                    foreach (var prod in products)
                    {
                        ProductNameService         NameService = new ProductNameService(context);
                        ProdSaleHistoryReportModel model       = new ProdSaleHistoryReportModel()
                        {
                            Productname = NameService.GetProductName(prod.Productid),
                            Size        = prod.Prodsize,
                            Quantity    = prod.Quantity,
                            Cost        = prod.Unitprice,
                            Date        = prod.Purchasedate,
                            Productid   = prod.Productid
                        };
                        ReportModelList.Add(model);
                    }
                }

                else
                {
                    NotFound = true;
                }
            }
            if (NotFound)
            {
                return(Ok(Result));
            }

            return(Ok(ReportModelList));
        }
Esempio n. 3
0
        public ActionResult <List <ProdProfitReportModel> > GetProductSaleProfit([FromForm] ViewTemplateModelOne data)
        {
            bool     IsFromDateValid = false;
            bool     IsEndDateValid  = false;
            DateTime FromDate;
            string   CompleteFromDateString = "";
            DateTime EndDate;
            string   CompleteEndDateString = "";
            DateTime dt;
            bool     SelectOne   = false;
            bool     SelectTwo   = false;
            bool     SelectThree = false;
            List <SalewithCustIdTE>      products    = new List <SalewithCustIdTE>();
            List <ProdProfitReportModel> ReportModel = new List <ProdProfitReportModel>();
            string Result   = "";
            bool   NotFound = false;

            if (data.FromDate != null && data.EndDate != null)
            {
                DateService DateService1 = new DateService();
                CompleteFromDateString = DateService1.GetCompleteFromDate(data.FromDate);
                IsFromDateValid        = DateTime.TryParse(CompleteFromDateString, out dt);

                DateService DateService2 = new DateService();
                CompleteEndDateString = DateService2.GetCompleteEndDate(data.EndDate);
                IsEndDateValid        = DateTime.TryParse(CompleteEndDateString, out dt);
            }
            if (IsFromDateValid && IsEndDateValid)
            {
                FromDate = Convert.ToDateTime(CompleteFromDateString);
                EndDate  = Convert.ToDateTime(CompleteEndDateString);

                if (data.CategoryValue != null && data.ProductValue == null && data.SizeValue == null)
                {
                    SelectOne = true;
                    var ProductsByCategoryId = context.products.Where(x => x.CategoryId.ToString() == data.CategoryValue).ToList();
                    if (ProductsByCategoryId.Count() != 0)
                    {
                        foreach (var ProductsById in ProductsByCategoryId)
                        {
                            var collection = context.saleswithCustomerIds.Where(x => x.Productid == ProductsById.Id && x.Purchasedate >= FromDate && x.Purchasedate <= EndDate).OrderBy(x => x.Prodsize).ToList();
                            foreach (var prod in collection)
                            {
                                products.Add(prod);
                            }
                        }
                        // return Ok(products);
                    }
                    else
                    {
                        Result   = "NotFound";
                        NotFound = true;
                    }
                }
                else if (data.ProductValue != null && data.SizeValue == null)
                {
                    SelectTwo = true;
                    var DataCollection = context
                                         .saleswithCustomerIds
                                         .Where(x => x.Productid.ToString() == data.ProductValue && x.Purchasedate >= FromDate && x.Purchasedate <= EndDate).OrderBy(x => x.Prodsize).ToList();
                    if (DataCollection.Count() != 0)
                    {
                        products = DataCollection;
                    }
                    else
                    {
                        Result   = "NotFound";
                        NotFound = true;
                    }
                }
                else if (data.ProductValue != null && data.SizeValue != null)
                {
                    SelectThree = true;
                    var DataBySize = context.saleswithCustomerIds
                                     .Where(x => x.Productid.ToString() == data.ProductValue && x.Prodsize == data.SizeValue && x.Purchasedate >= FromDate && x.Purchasedate <= EndDate).ToList();
                    if (DataBySize.Count() > 0)
                    {
                        products = DataBySize;
                    }
                    else
                    {
                        Result   = "NotFound";
                        NotFound = true;
                    }
                }
                List <SalewithCustIdTE> ProdSaleGropued = new List <SalewithCustIdTE>();
                if (products.Count() > 0 && SelectOne)
                {
                    var GroupBySizeSelectOne = products.GroupBy(x => new { x.Productid, x.Prodsize }).Select(z => new SalewithCustIdTE()
                    {
                        Productid = z.Key.Productid,
                        Prodsize  = z.Key.Prodsize,
                        Quantity  = z.Sum(x => x.Quantity),
                        Unitprice = z.Sum(x => x.Unitprice) / z.Count(),
                    });
                    ProdSaleGropued = GroupBySizeSelectOne.ToList();
                }

                else if (products.Count() > 0 && SelectTwo)
                {
                    var GroupBySelectTwo = products.GroupBy(x => x.Prodsize).Select(z => new SalewithCustIdTE()
                    {
                        Productid = products[0].Productid,
                        Prodsize  = z.Key,
                        Quantity  = z.Sum(x => x.Quantity),
                        Unitprice = z.Sum(x => x.Unitprice) / z.Count(),
                    });
                    ProdSaleGropued = GroupBySelectTwo.ToList();
                }

                else if (products.Count() > 0 && SelectThree)
                {
                    SalewithCustIdTE SelectThreeData = new SalewithCustIdTE()
                    {
                        Productid = products[0].Productid,
                        Prodsize  = products[0].Prodsize,
                        Quantity  = products.Sum(x => x.Quantity),
                        Unitprice = products.Sum(x => x.Unitprice) / products.Count()
                    };
                    ProdSaleGropued.Clear();
                    ProdSaleGropued.Add(SelectThreeData);
                }

                // Creating Profit Model for Result
                if (ProdSaleGropued.Count() > 0)
                {
                    List <ProdAddHistoryTE> ProductsAll = new List <ProdAddHistoryTE>();
                    foreach (SalewithCustIdTE prod in ProdSaleGropued)
                    {
                        List <ProdAddHistoryTE> ProdRegistered = context
                                                                 .prodAddHistoryData
                                                                 .Where(x => x.ProductId == prod.Productid && x.Size == prod.Prodsize).ToList();
                        foreach (var prodSingle in ProdRegistered)
                        {
                            ProductsAll.Add(prodSingle);
                        }
                    }

                    if (ProductsAll.Count() > 0)
                    {
                        List <ProdAddHistoryTE> ProdRegGrouped = ProductsAll.GroupBy(x => new { x.ProductId, x.Size }).Select(z => new ProdAddHistoryTE()
                        {
                            ProductId = z.Key.ProductId,
                            Size      = z.Key.Size,
                            Cost      = z.Sum(x => x.Cost) / z.Count(),
                            Quantity  = z.Sum(x => x.Quantity),
                        }).ToList <ProdAddHistoryTE>();
                        foreach (var ProductSold in ProdSaleGropued)
                        {
                            var ProductRegistered               = ProdRegGrouped.Single(x => x.ProductId == ProductSold.Productid && x.Size == ProductSold.Prodsize);
                            ProductNameService    NameService   = new ProductNameService(context);
                            ProductProfitService  profitService = new ProductProfitService(context);
                            ProdProfitReportModel model         = new ProdProfitReportModel();
                            model.Productname         = NameService.GetProductName(ProductSold.Productid);
                            model.Size                = ProductSold.Prodsize;
                            model.Purchasecostaverage = ProductRegistered.Cost;
                            model.Salecostaverage     = ProductSold.Unitprice;
                            model.Quantitysold        = ProductSold.Quantity;
                            model.Profit              = profitService.CalcuatingProductProfitBasedDateValue(ProductSold.Productid, ProductSold.Prodsize, FromDate, EndDate);

                            ReportModel.Add(model);
                        }
                    }
                }
            }

            //
            if (NotFound)
            {
                return(Ok(Result));
            }
            return(Ok(ReportModel));
        }
        public ActionResult <string> PurchaseStock(CustwithOrder custwithOrder)
        {
            foreach (CartItems item in custwithOrder.cartItems)
            {
                SalewithCustIdTE salewithCustId = new SalewithCustIdTE
                {
                    Productname  = item.productName,
                    Prodsize     = item.size,
                    Quantity     = item.Quantity,
                    Unitprice    = item.cost,
                    TotalCost    = item.Quantity * item.cost,
                    Purchasedate = DateTime.Now,
                    Productid    = item.id,
                    Custid       = custwithOrder.customer.CustomerId
                };
                _context.Add(salewithCustId);
                _context.SaveChanges();
            }

            foreach (CartItems item in custwithOrder.cartItems)
            {
                StockTE stockTE = _context.stocks.Single(x => x.ProductId == item.id && x.Size == item.size);
                stockTE.Quantity = stockTE.Quantity - item.Quantity;
                _context.Entry(stockTE).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                _context.SaveChanges();
            }

            //To Calculate Bill profit
            List <Profitmodel> Datas = new List <Profitmodel>();
            int Totalprofit          = 0;

            foreach (CartItems item in custwithOrder.cartItems)
            {
                ProductProfitService profitService = new ProductProfitService(_context);
                Profitmodel          model         = new Profitmodel();
                model.Profit = profitService
                               .CalculatingProductProfitFoCurrentPruchase(item.id, item.size, item.Quantity, item.cost);
                Datas.Add(model);
            }
            foreach (var data in Datas)
            {
                Totalprofit += data.Profit;
            }



            //Generate Bill No and Passing Complete Purchased Object back to FrontEnd for PDF generation
            int  Billno;
            bool Notempty = _context.billscollections.Any();

            if (Notempty)
            {
                int MaxIdValue = _context.billscollections.Max(x => x.Id);
                int LastBillNo = _context.billscollections.Single(x => x.Id == MaxIdValue).Billnumber;
                Billno = LastBillNo + 1;
            }
            else
            {
                Billno = 801000;
            }
            CustObjwithBillNo custObjwithBillNo = new CustObjwithBillNo
            {
                Custwithorder = custwithOrder,
                Billnumber    = Billno,
                Billprofit    = Totalprofit
            };



            return(Ok(custObjwithBillNo));
        }