public ActionResult DailyChalan(int CustomerId, int UserId, int ChalanId)
        {
            //HomeController.<>c__DisplayClass24 <>c__DisplayClass = new HomeController.<>c__DisplayClass24();
            //<>c__DisplayClass.CustomerId = CustomerId;
            //<>c__DisplayClass.UserId = UserId;
            //<>c__DisplayClass.ChalanId = ChalanId;
            if (Session["UserId"] != null)
            {
                //HomeController.<>c__DisplayClass26 <>c__DisplayClass2 = new HomeController.<>c__DisplayClass26();
                //<>c__DisplayClass2.CS$<>8__locals25 = <>c__DisplayClass;
                int num = 0;
                PragatiEntities pragatiEntities = new PragatiEntities();
                ChalanViewModel chalanViewModel = new ChalanViewModel();
                var invcdata = (
                    from a in pragatiEntities.Invoices
                    where a.CustomerId == (int?)CustomerId && a.UserId == (int?)UserId && a.InvoiceId == ChalanId
                    select a).FirstOrDefault<Invoice>();

                Customer customer = (
                    from a in pragatiEntities.Customers
                    where a.CustomerId == CustomerId
                    select a).FirstOrDefault<Customer>();
                chalanViewModel.ChalanId = invcdata.InvoiceId;
                chalanViewModel.CustomerName = customer.CustomerName + " " + customer.CustomerSurname;
                chalanViewModel.CustomerFirmName = customer.CustomerFirmname;
                chalanViewModel.Date = DateTime.Parse(invcdata.DateTime).ToString("dd/MM/yyyy");
                chalanViewModel.TotalAmount = invcdata.TotalAmount.Value;
                List<Sale> list = (
                    from a in pragatiEntities.Sales
                    where a.CustomerId == (int?)CustomerId && a.UserId == (int?)UserId && a.CreatedDateTime == invcdata.DateTime
                    select a).ToList<Sale>();
                List<ItemsViewModel> list2 = new List<ItemsViewModel>();
                bool flag = false;
                bool flag2 = false;
                bool flag3 = false;
                bool flag4 = false;
                string text = "";
                string text2 = "";
                string text3 = "";
                float num2 = 0f;
                float num3 = 0f;
                //using (List<Sale>.Enumerator enumerator = list.GetEnumerator())
                //{
                //while (enumerator.MoveNext())
                //{
                foreach (var item in list)
                {
                    //HomeController.<>c__DisplayClass28 <>c__DisplayClass3 = new HomeController.<>c__DisplayClass28();
                    //<>c__DisplayClass3.CS$<>8__locals27 = <>c__DisplayClass2;
                    //<>c__DisplayClass3.CS$<>8__locals25 = <>c__DisplayClass;
                    //<>c__DisplayClass3.item = enumerator.Current;

                    num++;
                    ItemsViewModel itemsViewModel = new ItemsViewModel();
                    flag4 = false;
                    Stock stockdata = (
                        from a in pragatiEntities.Stocks
                        where (int?)a.StockId == item.StockId && a.MaterialId == item.MaterialId && a.UserId == item.UserId
                        select a).FirstOrDefault<Stock>();
                    AllMaterial allMaterial = (
                        from a in pragatiEntities.AllMaterials
                        where (int?)a.MaterialId == item.MaterialId
                        select a).FirstOrDefault<AllMaterial>();
                    MaterialType materialType = (
                        from a in pragatiEntities.MaterialTypes
                        where (int?)a.MaterialTypeId == stockdata.MaterialTypeId
                        select a).FirstOrDefault<MaterialType>();
                    if (stockdata.MaterialTypeId == 1 || stockdata.MaterialTypeId == 4 || stockdata.MaterialTypeId == 2)
                    {
                        SubMaterialType subMaterialType = (
                            from a in pragatiEntities.SubMaterialTypes
                            where (int?)a.SubMaterialTypeId == stockdata.SubMaterialTypeId && a.MaterialTypeId == stockdata.MaterialTypeId
                            select a).FirstOrDefault<SubMaterialType>();
                        foreach (ItemsViewModel current in list2)
                        {
                            if (current.ItemName == materialType.MaterialName.Substring(0, 1) + "-" + subMaterialType.Name.Substring(0, 3))
                            {
                                itemsViewModel = current;
                                flag = true;
                                break;
                            }
                            flag = false;
                            text = "";
                        }
                        if (!flag)
                        {
                            flag4 = true;
                            itemsViewModel.ItemName = materialType.MaterialName.Substring(0, 1) + "-" + subMaterialType.Name.Substring(0, 3);
                            itemsViewModel.Rate = subMaterialType.Price.Value;
                            itemsViewModel.Amount = item.TotalAmount.Value;
                            itemsViewModel.SerailNo = num;
                        }
                        float num4 = subMaterialType.Price.Value * (float)item.Nos_Of_Piece.Value;
                        itemsViewModel.totAmount += num4;
                        string text4 = text;
                        text = string.Concat(new string[]
                            {
                                text4,
                                allMaterial.MaterialCode,
                                "(",
                                item.Nos_Of_Piece.ToString(),
                                "),"
                            });
                        float num5 = (float)item.Nos_Of_Piece.Value;
                        itemsViewModel.totpiece += num5;
                        ItemsViewModel expr_980 = itemsViewModel;
                        expr_980.lstsubitems += text;
                        flag = false;
                        text = "";
                    }
                    else
                    {
                        if (stockdata.MaterialTypeId == 3)
                        {
                            Sub_Kasab sub_Kasab = (
                                from a in pragatiEntities.Sub_Kasab
                                where (int?)a.Sub_KasabId == stockdata.Sub_KasabId && a.MaterialTypeId == stockdata.MaterialTypeId
                                select a).FirstOrDefault<Sub_Kasab>();
                            foreach (ItemsViewModel current in list2)
                            {
                                if (current.ItemName == materialType.MaterialName.Substring(0, 1) + "-" + sub_Kasab.Name.Substring(0, 3))
                                {
                                    itemsViewModel = current;
                                    flag2 = true;
                                    break;
                                }
                                flag2 = false;
                                text = "";
                            }
                            if (!flag2)
                            {
                                flag4 = true;
                                itemsViewModel.ItemName = materialType.MaterialName.Substring(0, 1) + "-" + sub_Kasab.Name.Substring(0, 3);
                                itemsViewModel.Rate = sub_Kasab.Price.Value;
                                itemsViewModel.Amount = item.TotalAmount.Value;
                                itemsViewModel.SerailNo = num;
                            }
                            float num6 = sub_Kasab.Price.Value * (float)item.Nos_Of_Piece.Value;
                            itemsViewModel.totAmount += num6;
                            string text4 = text2;
                            text2 = string.Concat(new string[]
                                {
                                    text4,
                                    allMaterial.MaterialCode,
                                    "(",
                                    item.Nos_Of_Piece.ToString(),
                                    "),"
                                });
                            float num7 = (float)item.Nos_Of_Piece.Value;
                            itemsViewModel.totpiece += num7;
                            ItemsViewModel expr_C7D = itemsViewModel;
                            expr_C7D.lstsubitems += text2;
                            flag2 = false;
                            text2 = "";
                        }
                        else
                        {
                            if (stockdata.MaterialTypeId == 5 || stockdata.MaterialTypeId == 6 || stockdata.MaterialTypeId == 7)
                            {
                                foreach (ItemsViewModel current in list2)
                                {
                                    if (current.ItemName == materialType.MaterialName.Substring(0, 1))
                                    {
                                        itemsViewModel = current;
                                        flag3 = true;
                                        break;
                                    }
                                    flag3 = false;
                                }
                                float? tot_Weight_in_Gram_;
                                if (!flag3)
                                {
                                    flag4 = true;
                                    itemsViewModel.ItemName = materialType.MaterialName.Substring(0, 1);
                                    itemsViewModel.Rate = (float)allMaterial.Price.Value;
                                    itemsViewModel.Amount = item.TotalAmount.Value;
                                    ItemsViewModel arg_E18_0 = itemsViewModel;
                                    tot_Weight_in_Gram_ = item.Tot_Weight_in_Gram_;
                                    arg_E18_0.Piece = ((tot_Weight_in_Gram_.HasValue ? new float?(tot_Weight_in_Gram_.GetValueOrDefault() / 1000f) : null) + " Kg ").ToString();
                                    itemsViewModel.SerailNo = num;
                                }
                                object obj = text3;
                                object[] array = new object[5];
                                array[0] = obj;
                                array[1] = allMaterial.MaterialCode;
                                array[2] = "(";
                                object[] arg_E8D_0 = array;
                                int arg_E8D_1 = 3;
                                tot_Weight_in_Gram_ = item.Tot_Weight_in_Gram_;
                                arg_E8D_0[arg_E8D_1] = (tot_Weight_in_Gram_.HasValue ? new float?(tot_Weight_in_Gram_.GetValueOrDefault() / 1000f) : null);
                                array[4] = "),";
                                text3 = string.Concat(array);
                                itemsViewModel.lstsubitems = text3;
                                num2 += item.Tot_Weight_in_Gram_.Value / 1000f;
                                itemsViewModel.totpiece = num2;
                                num3 += item.TotalAmount.Value;
                                itemsViewModel.totAmount = num3;
                                flag3 = false;
                                text3 = "";
                                num2 = 0f;
                                num3 = 0f;
                            }
                        }
                    }
                    if (flag4)
                    {
                        list2.Add(itemsViewModel);
                        chalanViewModel.Items = list2;
                    }
                }
                //}
                //}
                return View(chalanViewModel);
            }
            else
            {
                return RedirectToAction("LogIn");
            }
        }
        public ActionResult AddCustomerCredit(AddCreditAmountViewModel acavm)
        {
            if (Session["UserId"] != null)
            {
                int UserId = int.Parse(Session["UserId"].ToString());
                acavm.UserId = UserId;
                if (acavm.Amount != 0.0)
                {
                    if (acavm.customerId != 0)
                    {
                        PragatiEntities db = new PragatiEntities();
                        CustomerPayableAmount cpa = db.CustomerPayableAmounts.Where(a => a.CustomerId == acavm.customerId && a.UserId == acavm.UserId).FirstOrDefault();
                        if (cpa == null)
                        {
                            CustomerPayableAmount cpab = new CustomerPayableAmount();
                            cpab.CustomerId = acavm.customerId;
                            cpab.UserId = acavm.UserId;
                            cpab.Amount = (float)acavm.Amount;
                            db.CustomerPayableAmounts.Add(cpab);
                            try
                            {
                                db.SaveChanges();
                            }
                            catch
                            {
                                @ViewBag.Message = "Error Occured While adding credit into Customer Account...Please try again";
                                return View();
                            }
                            return RedirectToAction("CustomerPayment");

                        }
                        else
                        {
                            cpa.Amount = cpa.Amount + (float)acavm.Amount;
                            cpa.CustomerId = acavm.customerId;
                            cpa.UserId = acavm.UserId;
                            try
                            {
                                db.SaveChanges();
                            }
                            catch
                            {
                                @ViewBag.Message = "Error Occured While adding credit into Customer Account...Please try again";
                                return View();
                            }
                            return RedirectToAction("CustomerPayment");
                        }
                        //if (num == 1)
                        //{

                        //    return RedirectToAction("CustomerPayment");
                        //}
                        //else
                        //{
                        //    @ViewBag.Message = "Error Occured While adding credit into Customer Account...Please try again";
                        //    return View();
                        //}
                    }
                    else
                    {
                        @ViewBag.Message = "Please Select Customer";
                        return View();
                    }
                }
                else
                {
                    @ViewBag.Message = "Please Enter Valid Amount";
                    return View();
                }

            }
            else
            {
                return RedirectToAction("LogIn");
            }
        }
        public ActionResult MonthlyChalan(int CustomerId, int UserId, string Fromdate, string ToDate)
        {
            if (Session["UserId"] != null)
            {
                PragatiEntities pragatiEntities = new PragatiEntities();
                MonthlyInvoiceViewModel monthlyInvoiceViewModel = new MonthlyInvoiceViewModel();
                List<ChalansViewModel> list = new List<ChalansViewModel>();
                List<MonthlyInvoice_Down_ViewModel> lst_sub_down = new List<MonthlyInvoice_Down_ViewModel>();
                List<MonthlyInvoice_Down_ReturnViewModel> lst_sub_return_down = new List<MonthlyInvoice_Down_ReturnViewModel>();

                Customer customer = (
                    from a in pragatiEntities.Customers
                    where a.CustomerId == CustomerId
                    select a).FirstOrDefault<Customer>();
                DateTime t = DateTime.Parse(Fromdate);
                DateTime t2 = DateTime.Parse(ToDate);
                monthlyInvoiceViewModel.CustomerName = customer.CustomerName + " " + customer.CustomerSurname;
                monthlyInvoiceViewModel.CustomerFirmName = customer.CustomerFirmname;
                monthlyInvoiceViewModel.FromDate = t.ToString("dd/MM/yyyy");
                monthlyInvoiceViewModel.ToDate = t2.ToString("dd/MM/yyyy");

                List<Invoice> list2 = (
                    from a in pragatiEntities.Invoices
                    where a.CustomerId == (int?)CustomerId && a.UserId == (int?)UserId
                    select a).ToList<Invoice>();

                float num = 0f;
                foreach (Invoice current in list2)
                {
                    DateTime dateTime = DateTime.Parse(current.DateTime);
                    if (t <= dateTime && dateTime <= t2)
                    {
                        ChalansViewModel chalansViewModel = new ChalansViewModel();
                        chalansViewModel.ChalanId = current.InvoiceId;
                        chalansViewModel.Date = DateTime.Parse(current.DateTime).ToString("dd/MM/yyyy");
                        chalansViewModel.Amount = current.TotalAmount.Value;
                        num += current.TotalAmount.Value;

                        foreach (var current_sub in current.Sales)
                        {
                            var submatid = (from m in pragatiEntities.Stocks where m.StockId == (int)current_sub.StockId select m).FirstOrDefault();

                            if (current_sub.ReturnPiece == "No")
                            {
                                // foreach (var item in lst_sub_down)
                                // {
                                MonthlyInvoice_Down_ViewModel sub_down = new MonthlyInvoice_Down_ViewModel();

                                var subname = (from m in pragatiEntities.SubMaterialTypes where m.SubMaterialTypeId == submatid.SubMaterialTypeId select m).FirstOrDefault();
                                var mainname = (from m in pragatiEntities.MaterialTypes where m.MaterialTypeId == submatid.MaterialTypeId select m).FirstOrDefault();
                                if (subname != null)
                                {
                                    // subname.Name = "";
                                    //total number of thread based on item
                                    if (lst_sub_down.Count == 0)
                                    {
                                        sub_down.Matname = mainname.MaterialName + "-" + subname.Name;
                                        if (current_sub.Nos_Of_Piece != 0)
                                        {
                                            sub_down.Piece = (int)current_sub.Nos_Of_Piece;

                                            lst_sub_down.Add(sub_down);
                                        }
                                    }
                                    else
                                    {
                                        foreach (var itemss in lst_sub_down)
                                        {
                                            if (itemss.Matname == mainname.MaterialName + "-" + subname.Name)
                                            {
                                                if (current_sub.Nos_Of_Piece != 0)
                                                {

                                                    itemss.Piece = itemss.Piece + (int)current_sub.Nos_Of_Piece;

                                                    //sub_down.Piece += (int)current_sub.Nos_Of_Piece;
                                                }
                                            }
                                            else
                                            {
                                                sub_down.Matname = mainname.MaterialName + "-" + subname.Name;
                                                if (current_sub.Nos_Of_Piece != 0)
                                                {
                                                    sub_down.Piece = (int)current_sub.Nos_Of_Piece;

                                                    lst_sub_down.Add(sub_down);
                                                }
                                            }
                                        }
                                    }

                                }
                                // }
                            }
                            else if (current_sub.ReturnPiece == "Yes")
                            {
                                MonthlyInvoice_Down_ReturnViewModel sub_returndown = new MonthlyInvoice_Down_ReturnViewModel();

                                var subname = (from m in pragatiEntities.SubMaterialTypes where m.SubMaterialTypeId == submatid.SubMaterialTypeId select m).FirstOrDefault();
                                var mainname = (from m in pragatiEntities.MaterialTypes where m.MaterialTypeId == submatid.MaterialTypeId select m).FirstOrDefault();
                                if (subname != null)
                                {
                                    // subname.Name = "";
                                    //total number of thread based on item
                                    if (lst_sub_return_down.Count == 0)
                                    {
                                        sub_returndown.ReturnMatname = mainname.MaterialName + "-" + subname.Name;
                                        if (current_sub.Nos_Of_Piece != 0)
                                        {
                                            sub_returndown.ReturnPiece = (int)current_sub.Nos_Of_Piece;

                                            lst_sub_return_down.Add(sub_returndown);
                                        }
                                    }
                                    else
                                    {
                                        foreach (var itemss in lst_sub_return_down)
                                        {
                                            if (itemss.ReturnMatname == mainname.MaterialName + "-" + subname.Name)
                                            {
                                                if (current_sub.Nos_Of_Piece != 0)
                                                {

                                                    itemss.ReturnPiece = itemss.ReturnPiece + (int)current_sub.Nos_Of_Piece;

                                                    //sub_down.Piece += (int)current_sub.Nos_Of_Piece;
                                                }
                                            }
                                            else
                                            {
                                                sub_returndown.ReturnMatname = mainname.MaterialName + "-" + subname.Name;
                                                if (current_sub.Nos_Of_Piece != 0)
                                                {
                                                    sub_returndown.ReturnPiece = (int)current_sub.Nos_Of_Piece;

                                                    lst_sub_return_down.Add(sub_returndown);
                                                }
                                            }
                                        }
                                    }

                                }
                            }

                        }

                        list.Add(chalansViewModel);
                    }
                }
                monthlyInvoiceViewModel.TotalAmount = num;
                monthlyInvoiceViewModel.lstChalans = list;
                monthlyInvoiceViewModel.lst_totaldownItems = lst_sub_down;
                monthlyInvoiceViewModel.lst_totalReturndownItems = lst_sub_return_down;
                MonthlyInvoice monthlyInvoice = (
                    from a in pragatiEntities.MonthlyInvoices
                    where a.CustomerId == (int?)CustomerId && a.UserId == (int?)UserId && a.FromDateTime == Fromdate && a.ToDateTime == ToDate
                    select a).FirstOrDefault<MonthlyInvoice>();
                if (monthlyInvoice == null)
                {
                    MonthlyInvoice monthlyInvoice2 = new MonthlyInvoice();
                    monthlyInvoice2.CustomerId = new int?(CustomerId);
                    monthlyInvoice2.FromDateTime = Fromdate;
                    monthlyInvoice2.ToDateTime = ToDate;
                    monthlyInvoice2.TotalAmount = new float?(num);
                    monthlyInvoice2.UserId = new int?(UserId);
                    pragatiEntities.MonthlyInvoices.Add(monthlyInvoice2);
                    pragatiEntities.SaveChanges();
                }
                MonthlyInvoice monthlyInvoice3 = (
                    from a in pragatiEntities.MonthlyInvoices
                    where a.CustomerId == (int?)CustomerId && a.UserId == (int?)UserId && a.FromDateTime == Fromdate && a.ToDateTime == ToDate
                    select a).FirstOrDefault<MonthlyInvoice>();
                monthlyInvoiceViewModel.BillId = monthlyInvoice3.MonthlyInvoiceId;
                return View(monthlyInvoiceViewModel);
            }
            else
            {
                return RedirectToAction("LogIn");
            }
        }
        public ActionResult EditMaterial(EditMaterialViewModel emvm, HttpPostedFileBase MaterialImage)
        {
            if (Session["UserId"] != null)
            {
                PragatiEntities db = new PragatiEntities();

                string text = MaterialImage.FileName;
                text = text.Substring(text.LastIndexOf("."));
                if (text == ".jpg" || text == ".jpeg" || text == ".png" || text == ".JPG" || text == ".JPEG" || text == ".PNG")
                {
                    string text2 = Path.GetFileName(MaterialImage.FileName);
                    string fileName = MaterialImage.FileName;
                    text2 = text2.Substring(0, text2.LastIndexOf(".") - 1);
                    text2 = text2 + DateTime.Now.Ticks.ToString() + text;
                    string filename = Path.Combine(Server.MapPath("~/MaterialImage/"), text2);
                    MaterialImage.SaveAs(filename);
                    string materialImagePath = "~/MaterialImage/" + text2;
                    emvm.MaterialImagePath = materialImagePath;

                    AllMaterial allMaterial = db.AllMaterials.Where(a => a.MaterialId == emvm.MaterialId).FirstOrDefault();
                    allMaterial.MaterialCode = emvm.MaterialCode;
                    allMaterial.Datetime = new DateTime?(DateTime.Now);
                    allMaterial.MaterialId = emvm.MaterialId;
                    if (emvm.MaterialImage != null)
                    {

                        allMaterial.MaterialImagePath = emvm.MaterialImagePath;
                    }
                    try
                    {
                        db.SaveChanges();
                        ViewBag.success = "Material Updated Successfully";
                        return View(emvm);
                    }
                    catch
                    {
                        ViewBag.Message = "Error occured Please try Again";
                        return View(emvm);
                    }

                }
                else
                {

                    ViewBag.Message = "Invalid Image.";
                    return View(emvm);
                }

            }
            else
            {
                return RedirectToAction("LogIn");
            }
        }
        public ActionResult EditMaterial(int id)
        {
            if (Session["UserId"] != null)
            {
                PragatiEntities db = new PragatiEntities();
                var data = db.AllMaterials.Where(a => a.MaterialId == id).FirstOrDefault();
                EditMaterialViewModel edtmvm = new EditMaterialViewModel();
                edtmvm.MaterialId = id;
                edtmvm.MaterialCode = data.MaterialCode;
                edtmvm.MaterialImagePath = data.MaterialImagePath;
                var materialtype = db.MaterialTypes.Where(a => a.MaterialTypeId == data.MaterialTypeId).FirstOrDefault();
                edtmvm.MaterialName = materialtype.MaterialName;
                if (materialtype.MaterialTypeId == 3)
                {
                    var submaterialtype = db.Sub_Kasab.Where(a => a.MaterialTypeId == data.MaterialTypeId).FirstOrDefault();
                    edtmvm.SubMaterialName = submaterialtype.Name;
                    edtmvm.SubMaterialId = submaterialtype.Sub_KasabId;
                }
                return View(edtmvm);

            }
            else
            {
                return RedirectToAction("LogIn");
            }
        }
        public ActionResult DeleteMaterial(int id)
        {
            if (Session["UserId"] != null)
            {
                PragatiEntities pragatiEntities = new PragatiEntities();
                Stock stock = (
                    from a in pragatiEntities.Stocks
                    where a.MaterialId == (int?)id
                    select a).FirstOrDefault<Stock>();
                if (stock == null)
                {
                    AllMaterial allMaterial = (
                        from a in pragatiEntities.AllMaterials
                        where a.MaterialId == id
                        select a).FirstOrDefault<AllMaterial>();
                    string path = Server.MapPath(allMaterial.MaterialImagePath);
                    // File.Delete(path);
                    //try
                    //{
                    //    FileInfo fifo = new FileInfo(path);
                    //    fifo.Delete();
                    //}
                    pragatiEntities.AllMaterials.Remove(allMaterial);
                    try
                    {
                        pragatiEntities.SaveChanges();
                        return RedirectToAction("ViewMaterials", new
                        {
                            id = 1
                        });

                    }
                    catch
                    {
                        return RedirectToAction("ViewMaterials", new
                        {
                            id = -1
                        });

                    }
                }
                return RedirectToAction("ViewMaterials", new
                {
                    id = -2
                });
            }
            else
            {
                return RedirectToAction("LogIn");
            }
        }