public ActionResult Sale(ProductSaleAdd request)
        {
            string name         = HttpContext.User.Identity.Name;
            string operatorName = AgroExpressDBAccess.GetFullNamebyUserID(name);

            bool smsStatus = false;

            if (ModelState.IsValid)
            {
                List <Sale> SaleRequest = new List <Sale>();

                double TotalBill = 0;
                double TottalPai = 0;
                foreach (var sale in request.ProductSaleInfo)
                {
                    if (sale.ProductId != null && sale.ProductId != 0 && sale.Amount != null && sale.Amount != 0 && sale.Rate != null && sale.Servicecharge != null)
                    {
                        SaleRequest.Add(new Sale
                        {
                            ProductId     = (int)sale.ProductId,
                            Amount        = (double)sale.Amount,
                            Rate          = (double)sale.Rate,
                            ServiceCharge = (double)sale.Servicecharge,
                            CustomerId    = request.CustomerID,
                            OperatorName  = operatorName,
                            DateTime      = request.SaleDate,
                            SMSSent       = smsStatus
                        });
                        TotalBill += (double)sale.Amount * ((double)sale.Rate + (double)sale.Servicecharge);
                    }
                }
                if (request.BillPaid != null)
                {
                    TottalPai = (double)request.BillPaid;
                }
                AgroExpressDBAccess.AddProductSaleList(SaleRequest, request.CustomerID, request.SalePointId, TotalBill, TottalPai, operatorName);
            }
            ModelState.Clear();
            ProductSaleAdd ProductSaleView = ProductSaleInitial();

            return(View(ProductSaleView));
        }
        private ProductSaleAdd ProductSaleInitial()
        {
            string name = HttpContext.User.Identity.Name;

            ProductSaleAdd ProductSale = new ProductSaleAdd();

            ProductSale.SaleDate = System.DateTime.UtcNow.AddHours(6);

            List <SalePoint> salePointList = new List <SalePoint>();

            salePointList = AgroExpressDBAccess.GetSalePointListForUSer(name);
            if (salePointList != null)
            {
                ProductSale.salepointlist = salePointList.Select(x => new SelectListItem
                {
                    Value = x.PKSalePointID.ToString(),
                    Text  = x.SalePointName
                });
            }

            List <Area> arealist = AgroExpressDBAccess.GetallEnabledArea();

            if (arealist != null)
            {
                arealist         = arealist.Where(a => salePointList.Any(b => b.PKSalePointID == a.SalePointId)).ToList();
                ProductSale.Area = arealist.Select(x => new SelectListItem
                {
                    Value = x.PKAreaId.ToString(),
                    Text  = x.AreaName
                });
            }

            List <SubArea> sarealist = AgroExpressDBAccess.GetallEnabledSubArea();

            if (sarealist != null)
            {
                sarealist           = sarealist.Where(a => arealist.Any(b => b.PKAreaId == a.AreaId)).ToList();
                ProductSale.SubArea = sarealist.Select(x => new SelectListItem
                {
                    Value = x.PKSubAreaId.ToString(),
                    Text  = x.SubAreaName
                });
            }

            List <Product>         productList = AgroExpressDBAccess.GetAllEnabledProduct();
            List <ProductSaleInfo> PSI         = new List <ProductSaleInfo>();
            var MilkInfo = AgroExpressDBAccess.IsProductExist("Milk");

            if (MilkInfo != null)
            {
                PSI.Add(new ProductSaleInfo {
                    ProductId = MilkInfo.PKProductId, Amount = null, Rate = null, Servicecharge = null
                });
            }
            else
            {
                Product MilkInfoAdd = new Product
                {
                    ProductName = "Milk",
                    SellingUnit = "Ltr",
                    Stock       = 0
                };
                AgroExpressDBAccess.AddProduct(MilkInfoAdd);
                MilkInfo = AgroExpressDBAccess.IsProductExist("Milk");
                PSI.Add(new ProductSaleInfo {
                    ProductId = MilkInfo.PKProductId, Amount = null, Rate = null, Servicecharge = null
                });
            }
            // Product dropdown list

            productList         = productList.Where(a => a.ProductName.ToLower() != "milk").ToList();
            ProductSale.product = productList.Select(x => new SelectListItem
            {
                Value = x.PKProductId.ToString(),
                Text  = x.ProductName
            });

            //
            int len = productList.Count;

            for (int i = 0; i < len; i++)
            {
                PSI.Add(new ProductSaleInfo {
                    ProductId = null, Amount = null, Rate = null, Servicecharge = 0
                });
            }
            ProductSale.ProductSaleInfo = PSI;

            List <Customer> customer = AgroExpressDBAccess.GetallEnabledCustomer();

            customer             = customer.Where(a => sarealist.Any(b => b.PKSubAreaId == a.SubAreaId)).ToList();
            ProductSale.Customer = customer.Select(x => new SelectListItem
            {
                Value = x.PKCustomerId.ToString(),
                Text  = x.FullName
            });
            return(ProductSale);
        }
        public ActionResult Sale()
        {
            ProductSaleAdd ProductSaleView = ProductSaleInitial();

            return(View(ProductSaleView));
        }