public int EditSupplierRecieptDetails(List <RecPayDetail> rpayDetails, int recpayID)
        {
            //code for edit
            try
            {
                foreach (var CU in rpayDetails)
                {
                    if (recpayID > 0)
                    {
                        RecPayDetail objrpayDetails = Context1.RecPayDetails.Where(item => item.RecPayDetailID == CU.RecPayDetailID).FirstOrDefault();
                        objrpayDetails.Amount     = CU.Amount;
                        objrpayDetails.CurrencyID = CU.CurrencyID;
                        objrpayDetails.InvDate    = CU.InvDate;

                        objrpayDetails.InvNo = CU.InvNo;

                        objrpayDetails.InvoiceID = CU.InvoiceID;

                        objrpayDetails.Remarks = CU.Remarks;
                        Context1.Entry(objrpayDetails).State = EntityState.Modified;
                    }
                    Context1.SaveChanges();
                }
                return(1);
            }
            catch (Exception)
            {
                return(0);
            }
        }
 public int EditRecpayDetailsCustR(int recpayid, decimal amount)
 {
     using (SHIPPING_FinalEntities Context1 = new SHIPPING_FinalEntities())
     {
         var obj = (from t in Context1.RecPayDetails where t.RecPayID == recpayid && t.InvoiceID == 0 select t).FirstOrDefault();
         //var obj = Context1.RecPayDetails.Find(recpayid,0);
         if (obj != null)
         {
             RecPayDetail recpdetail = new RecPayDetail();
             recpdetail.RecPayDetailID = obj.RecPayDetailID;
             recpdetail.Amount         = -(amount);
             // recpdetail.CurrencyID = cueencyid;
             //recpd.InvDate = item.InvoiceDate.Value;
             recpdetail.RecPayID = obj.RecPayID;
             // recpdetail.Remarks = remark;
             recpdetail.InvoiceID     = 0;
             recpdetail.StatusInvoice = obj.StatusInvoice;
             recpdetail.InvDate       = obj.InvDate;
             recpdetail.InvNo         = obj.InvNo;
             recpdetail.Lock          = false;
             Context1.Entry(obj).CurrentValues.SetValues(recpdetail);
             // Context1.Entry(recpdetail).State = EntityState.Modified;
             Context1.SaveChanges();
             return(1);
         }
         else
         {
             return(0);
         }
     }
 }
        public ActionResult Create(CustomerRcieptVM RecP, string Command, string Currency)
        {
            int RPID    = 0;
            int fyearid = Convert.ToInt32(Session["fyearid"].ToString());
            int i       = 0;

            RecP.FYearID = Convert.ToInt32(Session["fyearid"]);
            RecP.UserID  = Convert.ToInt32(Session["UserID"]);
            var StaffNotes = (from d in db.StaffNotes where d.RecPayID == RecP.RecPayID && d.PageTypeId == 2 orderby d.NotesId descending select d).ToList();
            var branchid   = Convert.ToInt32(Session["CurrentBranchID"]);
            var users      = (from d in db.UserRegistrations select d).ToList();
            List <ReceiptAllocationDetailVM> AWBAllocationall = (List <ReceiptAllocationDetailVM>)Session["AWBAllocation"];
            var staffnotemodel = new List <StaffNoteModel>();

            foreach (var item in StaffNotes)
            {
                var model = new StaffNoteModel();
                model.id         = item.NotesId;
                model.employeeid = item.EmployeeId;
                //model.jobid = item.JobId;
                model.TaskDetails = item.Notes;
                model.Datetime    = item.EntryDate;
                model.EmpName     = users.Where(d => d.UserID == item.EmployeeId).FirstOrDefault().UserName;
                staffnotemodel.Add(model);
            }
            ViewBag.StaffNoteModel = staffnotemodel;
            //if (RecP.RecPayID > 0)
            //{
            //    RP.EditCustomerRecPay(RecP, Session["UserID"].ToString());
            //    RP.EditCustomerRecieptDetails(RecP.recPayDetail, RecP.RecPayID);
            //}
            if (RecP.CashBank != null)
            {
                RecP.StatusEntry = "CS";
                int acheadid = Convert.ToInt32(RecP.CashBank);
                var achead   = (from t in db.AcHeads where t.AcHeadID == acheadid select t.AcHead1).FirstOrDefault();
                RecP.BankName = achead;
            }
            else
            {
                RecP.StatusEntry = "BK";
                int acheadid = Convert.ToInt32(RecP.ChequeBank);
                var achead   = (from t in db.AcHeads where t.AcHeadID == acheadid select t.AcHead1).FirstOrDefault();
                RecP.BankName = achead;
            }
            if (RecP.CustomerRcieptChildVM == null)
            {
                RecP.CustomerRcieptChildVM = new List <CustomerRcieptChildVM>();
            }
            //Adding Entry in Rec PAY

            ///Insert Entry For RecPay Details
            ///
            if (RecP.RecPayID <= 0)
            {
                decimal Fmoney = 0;
                //for (int j = 0; j < RecP.CustomerRcieptChildVM.Count; j++)
                //{
                //    Fmoney = Fmoney + Convert.ToDecimal(RecP.CustomerRcieptChildVM[j].Amount);
                //}
                //if (Fmoney > 0)
                //{
                //    RecP.FMoney = Fmoney;
                //}
                RecP.AcCompanyID = branchid;

                RPID = ReceiptDAO.AddCustomerRecieptPayment(RecP, Session["UserID"].ToString()); //.AddCustomerRecieptPayment(RecP, Session["UserID"].ToString());

                RecP.RecPayID = (from c in db.RecPays orderby c.RecPayID descending select c.RecPayID).FirstOrDefault();
                decimal TotalAmount = 0;

                foreach (var item in RecP.CustomerRcieptChildVM)
                {
                    if (item.Amount > 0)
                    {
                        RecPayDetail recpd             = new RecPayDetail();
                        int?         maxrecpaydetailid = (from c in db.RecPayDetails orderby c.RecPayDetailID descending select c.RecPayDetailID).FirstOrDefault();
                        if (maxrecpaydetailid == null)
                        {
                            maxrecpaydetailid = 1;
                        }
                        else
                        {
                            maxrecpaydetailid = maxrecpaydetailid + 1;
                        }

                        recpd.RecPayDetailID      = Convert.ToInt32(maxrecpaydetailid);
                        recpd.Amount              = -(item.Amount);
                        recpd.CurrencyID          = item.CurrencyId;
                        recpd.AdjustmentAmount    = 0;
                        recpd.InvDate             = item.InvoiceDate.Value;
                        recpd.RecPayID            = RecP.RecPayID;
                        recpd.Remarks             = item.Remarks;
                        recpd.InvoiceID           = 0;
                        recpd.InScanID            = item.InScanID;
                        recpd.AcOPInvoiceDetailID = 0;
                        recpd.StatusInvoice       = "D";
                        db.RecPayDetails.Add(recpd);
                        db.SaveChanges();
                    }
                }


                //To Balance Invoice AMount
                if (RecP.FMoney > 0)
                {
                    //int l = ReceiptDAO.InsertRecpayDetailsForCust(RecP.RecPayID, 0, 0, Convert.ToDecimal(RecP.FMoney), null, "D", false, null, null, null, Convert.ToInt32(RecP.CurrencyId), 4, 0);
                    int fyaerId = Convert.ToInt32(Session["fyearid"].ToString());
                    ReceiptDAO.InsertJournalOfCustomer(RecP.RecPayID, fyaerId);
                }
                // var Recpaydata = (from d in db.RecPays where d.RecPayID == RecP.RecPayID select d).FirstOrDefault();

                //Recpaydata.RecPayID = RecP.RecPayID;
                //Recpaydata.IsTradingReceipt = true;
                //db.Entry(Recpaydata).State = EntityState.Modified;
                //db.SaveChanges();
            }
            else //edit mode
            {
                var details = (from d in db.RecPayDetails where d.RecPayID == RecP.RecPayID select d).ToList();
                if (details != null)
                {
                    db.RecPayDetails.RemoveRange(details);
                    db.SaveChanges();
                }

                RecPay recpay = new RecPay();
                recpay                  = db.RecPays.Find(RecP.RecPayID);
                recpay.RecPayDate       = RecP.RecPayDate;
                recpay.RecPayID         = RecP.RecPayID;
                recpay.AcJournalID      = RecP.AcJournalID;
                recpay.BankName         = RecP.BankName;
                recpay.ChequeDate       = RecP.ChequeDate;
                recpay.ChequeNo         = RecP.ChequeNo;
                recpay.CustomerID       = RecP.CustomerID;
                recpay.DocumentNo       = RecP.DocumentNo;
                recpay.EXRate           = RecP.EXRate;
                recpay.FYearID          = RecP.FYearID;
                recpay.FMoney           = RecP.FMoney;
                recpay.StatusEntry      = RecP.StatusEntry;
                recpay.IsTradingReceipt = true;
                recpay.FMoney           = RecP.FMoney;
                recpay.Remarks          = RecP.Remarks;
                recpay.ModifiedBy       = RecP.UserID;
                recpay.ModifiedDate     = DateTime.Now;
                db.Entry(recpay).State  = EntityState.Modified;
                db.SaveChanges();

                foreach (var item in RecP.CustomerRcieptChildVM)
                {
                    if (item.Amount > 0)
                    {
                        RecPayDetail recpd = new RecPayDetail();

                        int?maxrecpaydetailid = (from c in db.RecPayDetails orderby c.RecPayDetailID descending select c.RecPayDetailID).FirstOrDefault();
                        if (maxrecpaydetailid == null)
                        {
                            maxrecpaydetailid = 1;
                        }
                        else
                        {
                            maxrecpaydetailid = maxrecpaydetailid + 1;
                        }

                        recpd.RecPayDetailID   = Convert.ToInt32(maxrecpaydetailid);
                        recpd.Amount           = -(item.Amount);
                        recpd.CurrencyID       = item.CurrencyId;
                        recpd.AdjustmentAmount = 0;
                        if (item.InvoiceDate != null)
                        {
                            recpd.InvDate = item.InvoiceDate.Value;
                        }
                        recpd.RecPayID            = RecP.RecPayID;
                        recpd.Remarks             = item.Remarks;
                        recpd.InvoiceID           = 0;
                        recpd.InScanID            = item.InScanID;
                        recpd.AcOPInvoiceDetailID = 0;
                        recpd.StatusInvoice       = "D";
                        db.RecPayDetails.Add(recpd);
                        db.SaveChanges();
                    }
                }
                //int editrecPay = 0;
                //var sumOfAmount = db.RecPayDetails.Where(m => m.RecPayID == RecP.RecPayID && m.InvoiceID != 0).Sum(c => c.Amount);
                //editrecPay = editfu.EditRecpayDetailsCustR(RecP.RecPayID, Convert.ToInt32(sumOfAmount));
                if (RecP.AcJournalID == null)
                {
                    ReceiptDAO.InsertJournalOfCustomer(RecP.RecPayID, fyearid);
                }
                else
                {
                    int editAcJdetails = editfu.EditAcJDetails(RecP.AcJournalID.Value, Convert.ToDecimal(RecP.FMoney));
                }
            }


            return(RedirectToAction("Index", "CODReceipt"));
        }