public ActionResult SaveCashMR(MoneyReceiptVM MrVM, List <MoneyReceiptExt> AdjBills)
        {
            using (var transaction = new TransactionScope())
            {
                try
                {
                    RBACUser rUser = new RBACUser(Session["UserName"].ToString());
                    if (!rUser.HasPermission("MoneyReceiptCash_Insert"))
                    {
                        return(Json("X", JsonRequestBehavior.AllowGet));
                    }
                    //----Edit Nasir---//
                    //var cabkop = "";
                    //var sysSet = _sysSetService.All().FirstOrDefault();
                    //if (sysSet.ActualBasis == true)
                    //{
                    //var cabkop = _defACService.All().ToList().FirstOrDefault().CashAc;
                    //}
                    //else
                    //{
                    //    cabkop = _defACService.All().ToList().FirstOrDefault().CashAc;
                    //}
                    //---  ---//
                    string CRNo   = "";
                    var    IfExit = _moneyReceiptService.All().Where(x => x.MRNo == MrVM.MRNo).FirstOrDefault();
                    if (IfExit == null)
                    {
                        //var todayDate = DateTime.Now;
                        CashReceipt           CR   = new CashReceipt();
                        CashReceiptSubDetails CRSD = new CashReceiptSubDetails();
                        MoneyReceipt          MR   = new MoneyReceipt();
                        MR.MRSL        = MrVM.MRSL;
                        MR.BranchCode  = (MrVM.BranchCode == null) ? Session["BranchCode"].ToString() : MrVM.BranchCode;
                        MR.ProjCode    = (MrVM.ProjCode == null) ? Session["ProjCode"].ToString() : MrVM.ProjCode;
                        MR.MRNo        = MrVM.MRNo;
                        MR.MRDate      = MrVM.MRDate; //.AddHours(todayDate.Hour).AddMinutes(todayDate.Minute).AddSeconds(todayDate.Second).AddMilliseconds(todayDate.Millisecond);
                        MR.MRAgainst   = MrVM.MRAgainst;
                        MR.VchrNo      = MrVM.VchrNo;
                        MR.PayMode     = "Ca";
                        MR.Posted      = MrVM.Posted;
                        MR.AdjWithBill = MrVM.AdjWithBill;
                        MR.MRAmount    = MrVM.Amount;

                        // Accode ????
                        MR.Accode = MrVM.MRAgainst; // "1.2.001";

                        MR.CustCode = MrVM.CustCode;
                        MR.FinYear  = Session["FinYear"].ToString();
                        MR.Remarks  = MrVM.Remarks;
                        MR.JobNo    = MrVM.JobNo;
                        //MR.Ca_Bk = cabkop;

                        // CR.ReceiptNo = _CashReceiptController.GenerateReceiptNo(MR.BranchCode).Substring(6, 0);//MrVM.MRNo;
                        CR.ReceiptNo = _CashReceiptController.GenerateReceiptNo(MR.BranchCode);
                        // CR.ReceiptNo = MrVM.MRNo;
                        CRNo           = CR.ReceiptNo;
                        CR.ReceiptDate = (DateTime)MrVM.MRDate;
                        CR.purAccode   = MrVM.MRAgainst;// "1.2.004";
                        CR.RefNo       = MrVM.MRSL;
                        CR.Amount      = (Double)MrVM.Amount;
                        CR.Advance     = false;
                        CR.Remarks     = MrVM.Remarks;
                        CR.GLPost      = false;
                        CR.BranchCode  = MrVM.BranchCode;
                        CR.VoucherNo   = MrVM.VchrNo;
                        CR.BranchCode  = MR.BranchCode;
                        CR.FinYear     = Session["FinYear"].ToString();

                        CRSD.SubCode       = MrVM.CustCode;
                        CRSD.Accode        = MrVM.MRAgainst;
                        CRSD.Amount        = (Double)MrVM.Amount;
                        CRSD.CashReceiptNo = CRNo;

                        //CR.purAccode = cabkop; // "1.2.004";
                        _moneyReceiptService.Add(MR);
                        _CashReceiptService.Add(CR);
                        _CashReceiptSubDetailsAppService.Add(CRSD);
                        _CashReceiptService.Save();
                        _moneyReceiptService.Save();
                        _CashReceiptSubDetailsAppService.Save();

                        //List<MoneyReceiptExt> MRExtList = new List<MoneyReceiptExt>();

                        if (AdjBills != null)
                        {
                            decimal TotAmt = MrVM.Amount;
                            foreach (var bill in AdjBills)
                            {
                                MoneyReceiptExt MRExt    = new MoneyReceiptExt();
                                var             saleMain = _salesMainService.All().Where(s => s.SaleNo == bill.SaleNo).FirstOrDefault();
                                MRExt.MRId        = _moneyReceiptService.All().OrderBy(s => s.MRId).LastOrDefault().MRId;
                                MRExt.SaleNo      = bill.SaleNo;
                                MRExt.InstallNo   = _moneyReceiptExtService.All().OrderBy(s => s.InstallNo).Where(x => x.SaleNo == bill.SaleNo).Select(s => s.InstallNo).LastOrDefault();
                                MRExt.InstallNo   = MRExt.InstallNo + 1;
                                MRExt.InstallDate = DateTime.Now;
                                if (TotAmt >= bill.Amount)
                                {
                                    MRExt.Amount = bill.Amount;
                                    TotAmt       = TotAmt - bill.Amount;

                                    saleMain.ReceiptNo  = MrVM.MRNo;
                                    saleMain.ReceiptAmt = bill.Amount;
                                    saleMain.IsPaid     = true;
                                    _salesMainService.Update(saleMain);
                                    _salesMainService.Save();

                                    _moneyReceiptExtService.Add(MRExt);
                                    _moneyReceiptExtService.Save();
                                }
                                else
                                {
                                    if (bill.IsPaid_mre)
                                    {
                                        saleMain.IsPaid = true;
                                        MRExt.Amount    = TotAmt;

                                        //TotAmt = TotAmt - bill.Amount;
                                    }
                                    else
                                    {
                                        saleMain.IsPaid = false;
                                        MRExt.Amount    = TotAmt;
                                        // MRExt.Amount = bill.Amount;
                                        //TotAmt = TotAmt - bill.Amount;
                                    }


                                    saleMain.ReceiptNo  = MrVM.MRNo;
                                    saleMain.ReceiptAmt = TotAmt;

                                    _moneyReceiptExtService.Add(MRExt);
                                    _moneyReceiptExtService.Save();

                                    string sql = string.Format("select sm.ReceiptAmt,sm.SaleNo, sm.SaleDate, (sm.NetAmount - isnull(sum(mre.Amount),0)) as Billamount from SalesMain sm left join MoneyReceiptExt mre on mre.SaleNo = sm.SaleNo where sm.CustCode = " + MrVM.CustCode + " and sm.IsPaid = 'false'  group by sm.ReceiptAmt, mre.SaleNo, sm.NetAmount, sm.SaleNo, sm.SaleDate, mre.Amount order by sm.SaleNo");
                                    IEnumerable <billShow> BillLst;
                                    using (AcclineERPContext dbContext = new AcclineERPContext())
                                    {
                                        BillLst = dbContext.Database.SqlQuery <billShow>(sql).ToList();
                                    }
                                    foreach (var Salebill in BillLst)
                                    {
                                        if (Salebill.BillAmount == 0)
                                        {
                                            saleMain.ReceiptNo  = MrVM.MRNo;
                                            saleMain.ReceiptAmt = ((Salebill.ReceiptAmt == null) ? 0 : Salebill.ReceiptAmt) + TotAmt;
                                            saleMain.IsPaid     = true;
                                            _salesMainService.Update(saleMain);
                                            _salesMainService.Save();
                                        }
                                    }
                                }
                            } //end of  foreach
                        }
                        // New Add By Nasir
                        var GCa = _AcBrService.All().Where(s => s.BranchCode == MR.BranchCode && s.Ca_Ba == "Ca").Select(x => x.Accode).FirstOrDefault();
                        if (GCa == null)
                        {
                            var gset = _gsetService.All().LastOrDefault();
                            GCa = gset.GCa;
                        }

                        //------------------
                        TransactionLogService.SaveTransactionLog(_transactionLogService, "Cash MR", "Save", MR.MRSL, Session["UserName"].ToString());
                        LoadDropDown.journalVoucherSave("CR", "I", CRNo, MrVM.VchrNo, Session["FinYear"].ToString(), Session["ProjCode"].ToString(), Session["BranchCode"].ToString(), Convert.ToDateTime(MrVM.MRDate), GCa, Session["UserName"].ToString());

                        transaction.Complete();
                        return(Json("1", JsonRequestBehavior.AllowGet));
                    }
                    else
                    {
                        transaction.Dispose();
                        return(Json("3", JsonRequestBehavior.AllowGet));
                    }
                }

                catch (Exception)
                {
                    transaction.Dispose();
                    return(Json("0", JsonRequestBehavior.AllowGet));
                }
            }
        }
        public ActionResult SaveCheRec(ChequeReceipt CheRec, List <ChequeReceiptExt> AdjBills)
        {
            using (var transaction = new TransactionScope())
            {
                try
                {
                    RBACUser rUser = new RBACUser(Session["UserName"].ToString());
                    if (!rUser.HasPermission("ChequeReceipt_Insert"))
                    {
                        return(Json("X", JsonRequestBehavior.AllowGet));
                    }

                    string BrNo   = "";
                    var    items  = _chequeReceiptService.All().ToList().FirstOrDefault(x => x.ChqReceiptNo == CheRec.ChqReceiptNo);
                    var    chqArc = _chequeArchiveService.All().ToList().Where(x => x.ChqNo == CheRec.ChqNo).Select(s => s.ChqStatus).ToList();
                    if (chqArc.Count == 0 || chqArc.Contains("Deposit") && CheRec.ChqStatus != "Receive" || chqArc.Contains("Encash") && CheRec.ChqStatus != "Deposit" ||
                        chqArc.Contains("Dishonour") && CheRec.ChqStatus != "Encash" || chqArc.Contains("Return") && CheRec.ChqStatus != "Dishonour")
                    {
                        if (items == null)
                        {
                            ChequeReceipt cr = new ChequeReceipt();
                            cr            = CheRec;
                            cr.MRTing     = 0;
                            cr.MRTingTime = null;
                            cr.ProjCode   = Session["ProjCode"].ToString();
                            cr.FinYear    = Session["FinYear"].ToString();
                            _chequeReceiptService.Add(cr);
                            _chequeReceiptService.Save();

                            ChequeArchive CA = new ChequeArchive();
                            CA.ChqNo      = CheRec.ChqNo;
                            CA.ChqStatus  = CheRec.ChqStatus;
                            CA.UpdateBy   = CheRec.UpdateBy;
                            CA.UpdateDate = CheRec.UpdateDate;
                            CA.Reason     = CheRec.Reason;
                            CA.Remarks    = CheRec.Remarks;
                            _chequeArchiveService.Add(CA);
                            _chequeArchiveService.Save();

                            BankReceipt br = new BankReceipt();
                            br.BReceiptNo   = GenerateBankReceiptNo(CheRec.BranchCode);
                            BrNo            = br.BReceiptNo;
                            br.BReceiptDate = CheRec.ChqReceiptDate;
                            br.purAccode    = CheRec.MRAgainst.ToString();
                            br.RefNo        = CheRec.ChqReceiptNo;
                            br.bankAccode   = CheRec.DepositBank;
                            br.ChequeNo     = CheRec.ChqNo;
                            br.ChequeDate   = CheRec.ChqDate;
                            br.Amount       = (double)CheRec.Amount;
                            br.Advance      = false;
                            br.Remarks      = CheRec.Remarks;
                            br.FinYear      = Session["FinYear"].ToString();
                            br.GLPost       = CheRec.GLPost;
                            br.BranchCode   = CheRec.BranchCode;
                            br.VoucherNo    = CheRec.VchrNo;
                            br.SubCode      = CheRec.SubCode;
                            br.bankCode     = CheRec.BankCode;
                            _bankReceiptService.Add(br);
                            _bankReceiptService.Save();

                            if (AdjBills != null)
                            {
                                decimal TotAmt = CheRec.Amount;
                                foreach (var bill in AdjBills)
                                {
                                    ChequeReceiptExt chqExt = new ChequeReceiptExt();
                                    var saleMain            = _salesMainService.All().Where(s => s.SaleNo == bill.BillNo).FirstOrDefault();
                                    chqExt.ChqReceiptID = _chequeReceiptService.All().LastOrDefault().ChqReceiptId;
                                    chqExt.BillNo       = bill.BillNo;
                                    chqExt.BillDate     = saleMain.SaleDate;
                                    if (TotAmt >= bill.BillAmount)
                                    {
                                        chqExt.BillAmount = bill.BillAmount;
                                        TotAmt            = TotAmt - bill.BillAmount;

                                        saleMain.ReceiptNo  = CheRec.ChqReceiptNo;
                                        saleMain.ReceiptAmt = bill.BillAmount;
                                        saleMain.IsPaid     = true;
                                        _salesMainService.Update(saleMain);
                                        _salesMainService.Save();

                                        _chqRecExtService.Add(chqExt);
                                        _chqRecExtService.Save();
                                    }
                                    else
                                    {
                                        if (bill.IsPaid_mre)
                                        {
                                            saleMain.IsPaid  = true;
                                            chqExt.AdjAmount = TotAmt;
                                        }
                                        else
                                        {
                                            saleMain.IsPaid  = false;
                                            chqExt.AdjAmount = TotAmt;
                                        }

                                        chqExt.BillAmount = TotAmt;
                                        _chqRecExtService.Add(chqExt);
                                        _chqRecExtService.Save();

                                        string sql = string.Format("select sm.ReceiptAmt, sm.SaleNo as BillNo, sm.SaleDate as BillDate, (sm.NetAmount - isnull(sum(cre.BillAmount),0)) as Billamount from SalesMain sm left join ChequeReceiptExt cre on cre.BillNo = sm.SaleNo where sm.CustCode = " + CheRec.SubCode + " and sm.IsPaid = 'false'  group by sm.ReceiptAmt, cre.BillNo, sm.NetAmount, sm.SaleNo, sm.SaleDate, cre.BillAmount order by sm.SaleNo");
                                        IEnumerable <billShow> BillLst;
                                        using (AcclineERPContext dbContext = new AcclineERPContext())
                                        {
                                            BillLst = dbContext.Database.SqlQuery <billShow>(sql).ToList();
                                        }
                                        foreach (var Salebill in BillLst)
                                        {
                                            if (Salebill.BillAmount == 0)
                                            {
                                                saleMain.ReceiptNo  = CheRec.ChqReceiptNo;
                                                saleMain.ReceiptAmt = ((Salebill.ReceiptAmt == null) ? 0 : Salebill.ReceiptAmt) + TotAmt;
                                                saleMain.IsPaid     = true;
                                                _salesMainService.Update(saleMain);
                                                _salesMainService.Save();
                                            }
                                        }
                                    }
                                }
                            }

                            TransactionLogService.SaveTransactionLog(_transactionLogService, "ChequeReceipt", "Save", CheRec.ChqReceiptNo, Session["UserName"].ToString());
                            transaction.Complete();
                            return(Json("1", JsonRequestBehavior.AllowGet));
                        }
                        else
                        {
                            return(Json("2", JsonRequestBehavior.AllowGet));
                        }
                    }
                    else
                    {
                        return(Json("3", JsonRequestBehavior.AllowGet));
                    }
                }
                catch (Exception)
                {
                    transaction.Dispose();
                    return(Json("0", JsonRequestBehavior.AllowGet));
                }
            }
        }
Ejemplo n.º 3
0
        public ActionResult SaveUpdateCustAdj(CustAdjustment CustAdj, List <CustAdjExt> CustAdjExt, string IsSave)
        {
            using (var transaction = new TransactionScope())
            {
                try
                {
                    RBACUser rUser = new RBACUser(Session["UserName"].ToString());

                    #region for Cust Adjustment transection
                    string trnType = "Save";
                    string Msg     = "1";

                    //Firstly deleted the saved For Update
                    if (IsSave == "1")
                    {
                        if (!rUser.HasPermission("CustAdjustment_Update"))
                        {
                            return(Json("U", JsonRequestBehavior.AllowGet));
                        }
                        trnType = "Update";
                        Msg     = "2";
                        _custAdjExtService.All().ToList().Where(y => y.AdjNo == CustAdj.AdjNo).ToList().ForEach(x => _custAdjExtService.Delete(x));
                        _custAdjExtService.Save();
                        var cAdjDel = _custAdjustmentService.All().ToList().Where(y => y.AdjNo == CustAdj.AdjNo).FirstOrDefault();
                        _custAdjustmentService.Delete(cAdjDel);
                        _custAdjustmentService.Save();
                    }

                    if (!rUser.HasPermission("CustAdjustment_Insert"))
                    {
                        return(Json("X", JsonRequestBehavior.AllowGet));
                    }

                    var IfExit = _custAdjustmentService.All().Where(x => x.AdjNo == CustAdj.AdjNo).FirstOrDefault();
                    if (IfExit == null)
                    {
                        if (CustAdjExt != null)
                        {
                            foreach (var CAdjExtItem in CustAdjExt)
                            {
                                CustAdjExt CAdjExt = new CustAdjExt();
                                CAdjExt.AdjNo     = CAdjExtItem.AdjNo;
                                CAdjExt.AdjAmt    = CAdjExtItem.AdjAmt;
                                CAdjExt.AdjOn     = CAdjExtItem.AdjOn;
                                CAdjExt.AdjReason = CAdjExtItem.AdjReason;
                                CAdjExt.CustCode  = CAdjExtItem.CustCode;
                                _custAdjExtService.Add(CAdjExt);
                                _custAdjExtService.Save();
                            }
                        }

                        CustAdj.FinYear       = Session["FinYear"].ToString();
                        CustAdj.BranchCode    = Session["BranchCode"].ToString();
                        CustAdj.ProjCode      = Session["ProjCode"].ToString();
                        CustAdj.EntryDateTime = DateTime.Now;
                        if (CustAdj.AdjType == "Invoice")
                        {
                            if (CustAdj.DrCr == "Debit")
                            {
                                foreach (var item in CustAdjExt)
                                {
                                    var salesMain = _SalesMainService.All().Where(x => x.SaleNo == item.AdjOn).FirstOrDefault();

                                    salesMain.AdjAmt = (salesMain.AdjAmt == null ? 0 : salesMain.AdjAmt) + item.AdjAmt;
                                    if ((salesMain.ReceiptAmt == null ? 0 : salesMain.ReceiptAmt) + salesMain.AdjAmt == salesMain.NetAmount)
                                    {
                                        salesMain.IsPaid = true;
                                    }
                                    _SalesMainService.Update(salesMain);
                                    _SalesMainService.Save();
                                }
                            }
                            else
                            {
                                foreach (var item in CustAdjExt)
                                {
                                    var salesMain = _SalesMainService.All().Where(x => x.SaleNo == item.AdjOn).FirstOrDefault();

                                    salesMain.AdjAmt = (salesMain.AdjAmt == null ? 0 : salesMain.AdjAmt) + item.AdjAmt * (-1);
                                    if ((salesMain.ReceiptAmt == null ? 0 : salesMain.ReceiptAmt) + salesMain.AdjAmt == salesMain.NetAmount)
                                    {
                                        salesMain.IsPaid = true;
                                    }
                                    _SalesMainService.Update(salesMain);
                                    _SalesMainService.Save();
                                }
                            }
                        }
                        //else if (CustAdj.AdjType == "Money Receipt")
                        //{
                        //    if (CustAdj.DrCr == "Debit")
                        //    {
                        //        foreach (var item in CustAdjExt)
                        //        {
                        //            var salesMain = _SalesMainService.All().Where(x => x.SaleNo == item.AdjOn).FirstOrDefault();

                        //            salesMain.AdjAmt += item.AdjAmt;
                        //            if ((salesMain.ReceiptAmt == null ? 0 : salesMain.ReceiptAmt) + salesMain.AdjAmt == salesMain.NetAmount)
                        //            {
                        //                salesMain.IsPaid = true;
                        //            }
                        //            _SalesMainService.Update(salesMain);
                        //            _SalesMainService.Save();
                        //        }
                        //    }
                        //    else
                        //    {
                        //        foreach (var item in CustAdjExt)
                        //        {
                        //            var salesMain = _SalesMainService.All().Where(x => x.SaleNo == item.AdjOn).FirstOrDefault();

                        //            salesMain.AdjAmt += item.AdjAmt * (-1);
                        //            if ((salesMain.ReceiptAmt == null ? 0 : salesMain.ReceiptAmt) + salesMain.AdjAmt == salesMain.NetAmount)
                        //            {
                        //                salesMain.IsPaid = true;
                        //            }
                        //            _SalesMainService.Update(salesMain);
                        //            _SalesMainService.Save();
                        //        }
                        //    }

                        //}
                        _custAdjustmentService.Add(CustAdj);
                        _custAdjustmentService.Save();
                        TransactionLogService.SaveTransactionLog(_transactionLogService, "Customer Adjustment", trnType, CustAdj.AdjNo, Session["UserName"].ToString());
                        //LoadDropDown.ProvitionInvRSave("IR", "I", recvInfo.RcvNo, recvInfo.VchrNo, Session["FinYear"].ToString(), Session["ProjCode"].ToString(), Session["BranchCode"].ToString(), recvMain.RcvDate.ToString("yyyy-MM-dd"), Session["UserName"].ToString());
                        transaction.Complete();
                        return(Json(Msg, JsonRequestBehavior.AllowGet));
                    }
                    else
                    {
                        transaction.Dispose();
                        return(Json("3", JsonRequestBehavior.AllowGet));
                    }
                    #endregion
                }
                catch (Exception ex)
                {
                    transaction.Dispose();
                    return(Json(ex.Message.ToString(), JsonRequestBehavior.AllowGet));
                }
            }
        }
        public ActionResult SaveMROnline(MROnlineVM MrVM, List <MoneyReceiptExt> AdjBills)
        {
            using (var transaction = new TransactionScope())
            {
                try
                {
                    RBACUser rUser = new RBACUser(Session["UserName"].ToString());
                    if (!rUser.HasPermission("MoneyReceiptOnline_Insert"))
                    {
                        return(Json("X", JsonRequestBehavior.AllowGet));
                    }
                    var IfExit = _moneyReceiptService.All().Where(x => x.MRSL == MrVM.MRSL && x.MRNo == MrVM.MRNo).FirstOrDefault();
                    if (IfExit == null)
                    {
                        //var todayDate = DateTime.Now;
                        //CashReceipt CR = new CashReceipt();
                        BankReceipt  br = new BankReceipt();
                        MoneyReceipt MR = new MoneyReceipt();
                        MR.MRSL       = MrVM.MRSL;
                        MR.BranchCode = (MrVM.BranchCode == null) ? Session["BranchCode"].ToString() : MrVM.BranchCode;
                        MR.ProjCode   = (MrVM.ProjCode == null) ? Session["ProjCode"].ToString() : MrVM.ProjCode;
                        MR.MRNo       = MrVM.MRNo;
                        MR.MRDate     = MrVM.MRDate; //.AddHours(todayDate.Hour).AddMinutes(todayDate.Minute).AddSeconds(todayDate.Second).AddMilliseconds(todayDate.Millisecond);
                        MR.MRAgainst  = MrVM.MRAgainst;
                        MR.VchrNo     = MrVM.VchrNo;
                        MR.PayMode    = "Ca";
                        //MR.Posted = MrVM.Posted;
                        //MR.AdjWithBill = MrVM.AdjWithBill;
                        MR.MRAmount = MrVM.MRAmount;
                        MR.Accode   = MrVM.MRAgainst;
                        //if (MrVM.Accode == "0")
                        //{
                        //    MR.Accode = _defACService.All().ToList().FirstOrDefault().CashAc;
                        //}
                        MR.CustCode    = MrVM.CustCode;
                        MR.FinYear     = Session["FinYear"].ToString();
                        MR.Remarks     = MrVM.Remarks;
                        MR.JobNo       = MrVM.JobNo;
                        MR.CollectedBy = MrVM.CollectedBy;
                        MR.GetwayId    = MrVM.GetwayId;
                        MR.DepositBank = MrVM.DepositBank;
                        MR.EncashDate  = MrVM.DepositDate;

                        br.BReceiptNo   = MrVM.MRSL;
                        br.BReceiptDate = (DateTime)MrVM.MRDate;
                        br.purAccode    = MrVM.MRAgainst;
                        br.RefNo        = MrVM.MRNo;
                        br.bankAccode   = MrVM.DepositBank;
                        br.ChequeNo     = "Online";
                        br.ChequeDate   = DateTime.Now;
                        br.Amount       = (double)MrVM.MRAmount;
                        br.Advance      = false;
                        br.Remarks      = MrVM.Remarks;
                        br.FinYear      = Session["FinYear"].ToString();
                        br.GLPost       = false;
                        br.BranchCode   = (MrVM.BranchCode == null) ? Session["BranchCode"].ToString() : MrVM.BranchCode;
                        br.VoucherNo    = MrVM.VchrNo;
                        br.SubCode      = MrVM.CustCode;
                        br.bankCode     = MrVM.DepositBank;


                        //CR.ReceiptNo = MrVM.MRNo;
                        //CR.ReceiptDate = (DateTime)MrVM.MRDate;
                        //CR.purAccode = MrVM.MRAgainst;
                        ////if (MrVM.Accode == "0")
                        ////{
                        ////    CR.purAccode = _defACService.All().ToList().FirstOrDefault().CashAc;
                        ////}
                        //CR.RefNo = MrVM.MRSL;
                        //CR.Amount = (Double)MrVM.MRAmount;
                        //CR.Advance = false;
                        //CR.Remarks = MrVM.Remarks;
                        //CR.GLPost = false;
                        //CR.BranchCode = MrVM.BranchCode;
                        //CR.VoucherNo = MrVM.VchrNo;
                        //CR.JobNo = MrVM.JobNo;
                        //CR.FinYear = Session["FinYear"].ToString();
                        _moneyReceiptService.Add(MR);
                        //_CashReceiptService.Add(CR);
                        //_CashReceiptService.Save();
                        _bankReceiptService.Add(br);

                        _moneyReceiptService.Save();

                        _bankReceiptService.Save();


                        //List<MoneyReceiptExt> MRExtList = new List<MoneyReceiptExt>();

                        if (AdjBills != null)
                        {
                            decimal TotAmt = MrVM.MRAmount;
                            foreach (var bill in AdjBills)
                            {
                                MoneyReceiptExt MRExt    = new MoneyReceiptExt();
                                var             saleMain = _salesMainService.All().Where(s => s.SaleNo == bill.SaleNo).FirstOrDefault();
                                MRExt.MRId        = _moneyReceiptService.All().OrderBy(x => x.MRId).LastOrDefault().MRId;
                                MRExt.SaleNo      = bill.SaleNo;
                                MRExt.InstallNo   = _moneyReceiptExtService.All().OrderBy(s => s.InstallNo).Where(x => x.SaleNo == bill.SaleNo).Select(s => s.InstallNo).LastOrDefault();
                                MRExt.InstallNo   = MRExt.InstallNo + 1;
                                MRExt.InstallDate = DateTime.Now;
                                if (TotAmt >= bill.Amount)
                                {
                                    MRExt.Amount = bill.Amount;
                                    TotAmt       = TotAmt - bill.Amount;

                                    saleMain.ReceiptNo  = MrVM.MRNo;
                                    saleMain.ReceiptAmt = bill.Amount;
                                    saleMain.IsPaid     = true;
                                    _salesMainService.Update(saleMain);
                                    _salesMainService.Save();

                                    _moneyReceiptExtService.Add(MRExt);
                                    _moneyReceiptExtService.Save();
                                }
                                else
                                {
                                    if (bill.IsPaid_mre)
                                    {
                                        saleMain.IsPaid = true;
                                        MRExt.Amount    = TotAmt;
                                    }
                                    else
                                    {
                                        saleMain.IsPaid = false;
                                        MRExt.Amount    = TotAmt;
                                    }

                                    saleMain.ReceiptNo  = MrVM.MRNo;
                                    saleMain.ReceiptAmt = bill.Amount;
                                    //MRExt.Amount = TotAmt;
                                    _moneyReceiptExtService.Add(MRExt);
                                    _moneyReceiptExtService.Save();

                                    string sql = string.Format("select sm.ReceiptAmt,sm.SaleNo, sm.SaleDate, (sm.NetAmount - isnull(sum(mre.Amount),0)) as Billamount from SalesMain sm left join MoneyReceiptExt mre on mre.SaleNo = sm.SaleNo where sm.CustCode = " + MrVM.CustCode + " and sm.IsPaid = 'false'  group by sm.ReceiptAmt, mre.SaleNo, sm.NetAmount, sm.SaleNo, sm.SaleDate, mre.Amount order by sm.SaleNo");
                                    IEnumerable <billShow> BillLst;
                                    using (AcclineERPContext dbContext = new AcclineERPContext())
                                    {
                                        BillLst = dbContext.Database.SqlQuery <billShow>(sql).ToList();
                                    }
                                    foreach (var Salebill in BillLst)
                                    {
                                        if (Salebill.BillAmount == 0)
                                        {
                                            saleMain.ReceiptNo  = MrVM.MRNo;
                                            saleMain.ReceiptAmt = ((Salebill.ReceiptAmt == null) ? 0 : Salebill.ReceiptAmt) + TotAmt;
                                            saleMain.IsPaid     = true;
                                            _salesMainService.Update(saleMain);
                                            _salesMainService.Save();
                                        }
                                    }
                                }
                            }
                        }

                        //var GCa = _AcBrService.All().Where(s => s.BranchCode == MR.BranchCode && s.Ca_Ba == "Ba").Select(x => x.Accode).FirstOrDefault();
                        //if (GCa == null)
                        //{
                        //    var gset = _gsetService.All().LastOrDefault();
                        //    GCa = gset.GCa;
                        //}

                        TransactionLogService.SaveTransactionLog(_transactionLogService, "Online MR", "Save", MR.MRSL, Session["UserName"].ToString());

                        LoadDropDown.journalVoucherSave("BR", "I", MrVM.MRSL, MrVM.VchrNo, Session["FinYear"].ToString(), Session["ProjCode"].ToString(), Session["BranchCode"].ToString(), Convert.ToDateTime(MrVM.MRDate), MrVM.DepositBank, Session["UserName"].ToString());

                        transaction.Complete();
                        return(Json("1", JsonRequestBehavior.AllowGet));
                    }
                    else
                    {
                        transaction.Dispose();
                        return(Json("3", JsonRequestBehavior.AllowGet));
                    }
                }

                catch (Exception)
                {
                    transaction.Dispose();
                    return(Json("0", JsonRequestBehavior.AllowGet));
                }
            }
        }