public ActionResult Create(int id = 0)
        {
            ViewBag.Title    = "Cost Update - Create";
            ViewBag.employee = db.EmployeeMasters.ToList();
            List <VoucherTypeVM> lsttype = new List <VoucherTypeVM>();

            lsttype.Add(new VoucherTypeVM {
                TypeName = "Pickup Cash"
            });
            lsttype.Add(new VoucherTypeVM {
                TypeName = "Shipper"
            });
            lsttype.Add(new VoucherTypeVM {
                TypeName = "Consignee"
            });
            int userId = Convert.ToInt32(Session["UserID"].ToString());

            ViewBag.PaymentType = lsttype;
            ViewBag.Currency    = db.CurrencyMasters.ToList();
            ViewBag.Trips       = db.TruckDetails.ToList();
            CostUpdateMasterVM vm = new CostUpdateMasterVM();

            if (id == 0)
            {
                vm.ID         = 0;
                vm.DetailVM   = new List <CostUpdateDetailVM>();
                vm.EntryDate  = DateTime.Now;
                vm.CurrencyId = Convert.ToInt32(Session["CurrencyId"].ToString());
                var emp = db.EmployeeMasters.Where(cc => cc.UserID == userId).FirstOrDefault();
                if (emp != null)
                {
                    vm.EmployeeID = emp.EmployeeID;
                }
                ViewBag.EditMode             = "false";
                Session["CostAWBAllocation"] = null;
            }
            else
            {
                ViewBag.Title = "Cost Update - Modify";
                CostUpdateMaster v = db.CostUpdateMasters.Find(id);
                vm.ID            = v.ID;
                vm.CurrencyId    = Convert.ToInt32(Session["CurrencyId"].ToString());
                vm.EntryDate     = v.EntryDate;
                vm.EmployeeID    = v.EmployeeID;
                vm.TruckDetailID = v.TruckDetailID;
                var truck = db.TruckDetails.Find(v.TruckDetailID);
                if (truck != null)
                {
                    vm.TDNo = truck.ReceiptNo;
                }
                else
                {
                    vm.TDNo = "";
                }
                vm.BranchID          = v.BranchID;
                vm.AcFinancialYearID = v.AcFinancialYearID;
                ViewBag.EditMode     = "true";
                List <CostUpdateConsignmentVM> AWBAllocationall = new List <CostUpdateConsignmentVM>();

                AWBAllocationall = (from c in db.CostUpdateConsignments join d in db.InScanMasters on c.InScanID equals d.InScanID where c.CostUpdateMasterId == id select new CostUpdateConsignmentVM {
                    ID = c.ID, CostUpdateMasterId = c.CostUpdateMasterId, RevenueCostMasterID = c.RevenueCostMasterID, CostUpdateDetailId = c.CostUpdateDetailId, Amount = c.Amount, InScanID = c.InScanID, ConsignmentNo = d.ConsignmentNo, ConsignmentDate = d.TransactionDate
                }).ToList();

                Session["CostAWBAllocation"] = AWBAllocationall;
            }

            return(View(vm));
        }
        public ActionResult Create(CostUpdateMasterVM vm)
        {
            int branchid       = Convert.ToInt32(Session["CurrentBranchID"].ToString());
            int fyearid        = Convert.ToInt32(Session["fyearid"].ToString());
            CostUpdateMaster v = new CostUpdateMaster();
            List <CostUpdateConsignmentVM> AWBAllocationall = (List <CostUpdateConsignmentVM>)Session["CostAWBAllocation"];

            if (vm.ID == 0)
            {
                v.EntryDate         = vm.EntryDate;
                v.EmployeeID        = vm.EmployeeID;
                v.TruckDetailID     = vm.TruckDetailID;
                v.BranchID          = branchid;
                v.AcFinancialYearID = fyearid;
                db.CostUpdateMasters.Add(v);
                db.SaveChanges();
                //update inscan revenue update status
                var td = db.TruckDetails.Find(vm.TruckDetailID);
                //td.TDRemarks = vm.Remarks;
                td.CostUpdated     = true;
                db.Entry(td).State = System.Data.Entity.EntityState.Modified;
                db.SaveChanges();
            }
            else
            {
                v                   = db.CostUpdateMasters.Find(vm.ID);
                v.EntryDate         = vm.EntryDate;
                v.EmployeeID        = vm.EmployeeID;
                v.TruckDetailID     = vm.TruckDetailID;
                v.BranchID          = branchid;
                v.AcFinancialYearID = fyearid;
                db.Entry(v).State   = System.Data.Entity.EntityState.Modified;
                db.SaveChanges();
                //update inscan revenue update status
                var td = db.TruckDetails.Find(vm.TruckDetailID);
                //td.TDRemarks = vm.Remarks;
                td.CostUpdated     = true;
                db.Entry(td).State = System.Data.Entity.EntityState.Modified;
                db.SaveChanges();
            }

            for (int i = 0; i < vm.DetailVM.Count; i++)
            {
                if (vm.DetailVM[i].IsDeleted != true)
                {
                    if (vm.DetailVM[i].ID == 0)
                    {
                        CostUpdateDetail detail = new CostUpdateDetail();
                        detail.MasterID            = v.ID;
                        detail.RevenueCostMasterID = vm.DetailVM[i].RevenueCostMasterID;
                        detail.AcHeadCreditId      = vm.DetailVM[i].AcHeadCreditId;
                        detail.AcHeadDebitId       = vm.DetailVM[i].AcHeadDebitId;
                        detail.Amount       = vm.DetailVM[i].Amount;
                        detail.CurrencyId   = vm.DetailVM[i].CurrencyId;
                        detail.SupplierId   = vm.DetailVM[i].SupplierId;
                        detail.ExchangeRate = vm.DetailVM[i].ExchangeRate;


                        db.CostUpdateDetails.Add(detail);
                        db.SaveChanges();
                        //adding consignment referece to this entry
                        int acheadid = Convert.ToInt32(vm.DetailVM[i].RevenueCostMasterID);

                        var oldlist = db.CostUpdateConsignments.Where(cc => cc.CostUpdateDetailId == detail.ID && cc.CostUpdateMasterId == detail.MasterID).ToList();
                        if (oldlist != null)
                        {
                            foreach (var olditem in oldlist)
                            {
                                db.CostUpdateConsignments.Remove(olditem);
                                db.SaveChanges();
                            }
                        }
                        if (AWBAllocationall != null)
                        {
                            var list = AWBAllocationall.Where(cc => cc.RevenueCostMasterID == acheadid).ToList();
                            if (list != null && list.Count > 0)
                            {
                                foreach (var item2 in list)
                                {
                                    CostUpdateConsignment accons = new CostUpdateConsignment();
                                    accons.CostUpdateMasterId  = detail.MasterID;
                                    accons.CostUpdateDetailId  = detail.ID;
                                    accons.RevenueCostMasterID = acheadid;
                                    accons.InScanID            = Convert.ToInt32(item2.InScanID);
                                    accons.Amount = item2.Amount;
                                    db.CostUpdateConsignments.Add(accons);
                                    db.SaveChanges();
                                }
                            }
                            else
                            {
                                var     listnew           = (from c in db.InScanMasters where c.IsDeleted == false && (c.TruckDetailId == vm.TruckDetailID) orderby c.ConsignmentNo select new { InScanID = c.InScanID, TransactionDate = c.TransactionDate, ConsignmentNo = c.ConsignmentNo, TruckDetailID = c.TruckDetailId }).ToList();
                                decimal consignmentamount = 0;
                                consignmentamount = vm.DetailVM[i].Amount / listnew.Count;
                                foreach (var item2 in listnew)
                                {
                                    CostUpdateConsignment accons = new CostUpdateConsignment();
                                    accons.CostUpdateMasterId  = detail.MasterID;
                                    accons.CostUpdateDetailId  = detail.ID;
                                    accons.RevenueCostMasterID = acheadid;
                                    accons.InScanID            = Convert.ToInt32(item2.InScanID);
                                    accons.Amount = consignmentamount;
                                    db.CostUpdateConsignments.Add(accons);
                                    db.SaveChanges();
                                }
                            }
                        }
                        else
                        {
                            var     listnew           = (from c in db.InScanMasters where c.IsDeleted == false && (c.TruckDetailId == vm.TruckDetailID) orderby c.ConsignmentNo select new { InScanID = c.InScanID, TransactionDate = c.TransactionDate, ConsignmentNo = c.ConsignmentNo, TruckDetailID = c.TruckDetailId }).ToList();
                            decimal consignmentamount = 0;
                            consignmentamount = vm.DetailVM[i].Amount / listnew.Count;
                            if (listnew != null && listnew.Count > 0)
                            {
                                foreach (var item2 in listnew)
                                {
                                    CostUpdateConsignment accons = new CostUpdateConsignment();
                                    accons.CostUpdateMasterId  = detail.MasterID;
                                    accons.CostUpdateDetailId  = detail.ID;
                                    accons.RevenueCostMasterID = acheadid;
                                    accons.InScanID            = Convert.ToInt32(item2.InScanID);
                                    accons.Amount = consignmentamount;
                                    db.CostUpdateConsignments.Add(accons);
                                    db.SaveChanges();
                                }
                            }
                        }
                    }
                    else
                    {
                        CostUpdateDetail detail = db.CostUpdateDetails.Find(vm.DetailVM[i].ID);
                        if (detail != null)
                        {
                            detail.MasterID            = v.ID;
                            detail.RevenueCostMasterID = vm.DetailVM[i].RevenueCostMasterID;
                            detail.AcHeadCreditId      = vm.DetailVM[i].AcHeadCreditId;
                            detail.AcHeadDebitId       = vm.DetailVM[i].AcHeadDebitId;
                            detail.Amount       = vm.DetailVM[i].Amount;
                            detail.CurrencyId   = vm.DetailVM[i].CurrencyId;
                            detail.SupplierId   = vm.DetailVM[i].SupplierId;
                            detail.ExchangeRate = vm.DetailVM[i].ExchangeRate;


                            db.Entry(detail).State = System.Data.Entity.EntityState.Modified;
                            db.SaveChanges();

                            //adding consignment referece to this entry
                            int acheadid = Convert.ToInt32(vm.DetailVM[i].RevenueCostMasterID);

                            var oldlist = db.CostUpdateConsignments.Where(cc => cc.CostUpdateDetailId == detail.ID && cc.CostUpdateMasterId == detail.MasterID && cc.RevenueCostMasterID == acheadid).ToList();
                            if (oldlist != null)
                            {
                                foreach (var olditem in oldlist)
                                {
                                    db.CostUpdateConsignments.Remove(olditem);
                                    db.SaveChanges();
                                }
                            }
                            if (AWBAllocationall != null && AWBAllocationall.Count > 0)
                            {
                                var list = AWBAllocationall.Where(cc => cc.RevenueCostMasterID == acheadid).ToList();
                                if (list != null && list.Count > 0)
                                {
                                    foreach (var item2 in list)
                                    {
                                        CostUpdateConsignment accons = new CostUpdateConsignment();
                                        accons.CostUpdateMasterId  = detail.MasterID;
                                        accons.CostUpdateDetailId  = detail.ID;
                                        accons.RevenueCostMasterID = acheadid;
                                        accons.InScanID            = Convert.ToInt32(item2.InScanID);
                                        accons.Amount = item2.Amount;
                                        db.CostUpdateConsignments.Add(accons);
                                        db.SaveChanges();
                                    }
                                }
                                else
                                {
                                    var listnew = (from c in db.InScanMasters where c.IsDeleted == false && (c.TruckDetailId == vm.TruckDetailID) orderby c.ConsignmentNo select new { InScanID = c.InScanID, TransactionDate = c.TransactionDate, ConsignmentNo = c.ConsignmentNo, TruckDetailID = c.TruckDetailId }).ToList();
                                    if (listnew != null && listnew.Count > 0)
                                    {
                                        decimal consignmentamount = 0;
                                        consignmentamount = vm.DetailVM[i].Amount / listnew.Count;
                                        foreach (var item2 in listnew)
                                        {
                                            CostUpdateConsignment accons = new CostUpdateConsignment();
                                            accons.CostUpdateMasterId  = detail.MasterID;
                                            accons.CostUpdateDetailId  = detail.ID;
                                            accons.RevenueCostMasterID = acheadid;
                                            accons.InScanID            = Convert.ToInt32(item2.InScanID);
                                            accons.Amount = consignmentamount;
                                            db.CostUpdateConsignments.Add(accons);
                                            db.SaveChanges();
                                        }
                                    }
                                }
                            }
                            else
                            {
                                var listnew = (from c in db.InScanMasters where c.IsDeleted == false && (c.TruckDetailId == vm.TruckDetailID) orderby c.ConsignmentNo select new { InScanID = c.InScanID, TransactionDate = c.TransactionDate, ConsignmentNo = c.ConsignmentNo, TruckDetailID = c.TruckDetailId }).ToList();
                                if (listnew != null)
                                {
                                    decimal consignmentamount = 0;
                                    consignmentamount = vm.DetailVM[i].Amount / listnew.Count;
                                    foreach (var item2 in listnew)
                                    {
                                        CostUpdateConsignment accons = new CostUpdateConsignment();
                                        accons.CostUpdateMasterId  = detail.MasterID;
                                        accons.CostUpdateDetailId  = detail.ID;
                                        accons.RevenueCostMasterID = acheadid;
                                        accons.InScanID            = Convert.ToInt32(item2.InScanID);
                                        accons.Amount = consignmentamount;
                                        db.CostUpdateConsignments.Add(accons);
                                        db.SaveChanges();
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    if (vm.DetailVM[i].ID > 0)
                    {
                        int detailid        = vm.DetailVM[i].ID;
                        var consignmentlist = db.CostUpdateConsignments.Where(cc => cc.CostUpdateDetailId == detailid).ToList();
                        foreach (var item2 in consignmentlist)
                        {
                            db.CostUpdateConsignments.Remove(item2);
                            db.SaveChanges();
                        }
                        CostUpdateDetail detail = db.CostUpdateDetails.Find(vm.DetailVM[i].ID);
                        db.CostUpdateDetails.Remove(detail);
                        db.SaveChanges();
                    }
                }
            }
            TempData["SuccessMsg"] = "Trips Cost Updated Successfully!";
            return(RedirectToAction("Index"));
        }