public ActionResult GetMoneyReceiptPdf(string ReceiptNo)
        {
            RBACUser rUser = new RBACUser(Session["UserName"].ToString());

            if (!rUser.HasPermission("MoneyReceiptCash_Print"))
            {
                string errMsg = "P";
                return(RedirectToAction("CashMoneyReceipt", "CashMoneyReceipt", new { errMsg }));
            }
            MoneyReceiptVM MRVM     = new MoneyReceiptVM();
            var            cashRPfd = _moneyReceiptService.All().FirstOrDefault(x => x.MRNo == ReceiptNo && x.PayMode == "Ca");

            if (cashRPfd != null)
            {
                MRVM.MRNo     = cashRPfd.MRNo;
                MRVM.CustCode = cashRPfd.CustCode.Trim();
                MRVM.CustName = (from s in _subsidiaryInfoService.All().ToList() where s.SubCode == cashRPfd.CustCode.Trim() select s.SubName).FirstOrDefault();
                MRVM.Address  = (from se in _SubsidiaryExtService.All().ToList() where se.SubCode.Trim() == cashRPfd.CustCode.Trim() select se.SubAddress).FirstOrDefault();
                MRVM.InWord   = InWord.ConvertToWords(Convert.ToString(cashRPfd.MRAmount));
                MRVM.AcType   = _newChartService.All().FirstOrDefault(s => s.Accode == cashRPfd.Accode).AcName;
                MRVM.MRDate   = cashRPfd.MRDate;

                #region //For us Culture Ex: 0.00
                const string culture = "en-US";
                CultureInfo  ci      = CultureInfo.GetCultureInfo(culture);
                Thread.CurrentThread.CurrentCulture   = ci;
                Thread.CurrentThread.CurrentUICulture = ci;
                #endregion

                MRVM.Amount  = Convert.ToDecimal(cashRPfd.MRAmount);
                MRVM.PayMode = "Ca";
                return(new Rotativa.ViewAsPdf("~/Views/CashReceipt/MoneyReceiptPdf.cshtml", MRVM)
                {
                    PageSize = Rotativa.Options.Size.A4
                });
            }
            else
            {
                //string errMsg = "E";
                //return RedirectToAction("CashOperation", "CashOperation", new { errMsg });
                MRVM.PayMode = "Ca";
                return(new Rotativa.ViewAsPdf("~/Views/CashReceipt/MoneyReceiptPdf.cshtml", MRVM)
                {
                    PageSize = Rotativa.Options.Size.A4
                });
            }
        }
        public ActionResult GetMoneyReceiptPdf(string ReceiptNo)
        {
            MoneyReceiptVM MRVM  = new MoneyReceiptVM();
            var            crPfd = _chequeReceiptService.All().FirstOrDefault(x => x.ChqReceiptNo == ReceiptNo);

            if (crPfd != null)
            {
                var todayDate = DateTime.Now;
                var TingCount = _chequeReceiptService.All().FirstOrDefault(x => x.ChqReceiptNo == ReceiptNo);
                TingCount.MRTing     = TingCount.MRTing + 1;
                TingCount.MRTingTime = todayDate.AddHours(todayDate.Hour).AddMinutes(todayDate.Minute).AddSeconds(todayDate.Second).AddMilliseconds(todayDate.Millisecond);

                MRVM.MRNo       = crPfd.ChqReceiptNo;
                MRVM.CustCode   = crPfd.SubCode.Trim();
                MRVM.ChequeDate = (DateTime)crPfd.ChqDate;
                MRVM.CustName   = (from s in _subsidiaryService.All().ToList() where s.SubCode == crPfd.SubCode.Trim() select s.SubName).FirstOrDefault();
                MRVM.Address    = (from se in _SubsidiaryExtService.All().ToList() where se.SubCode.Trim() == crPfd.SubCode.Trim() select se.SubAddress).FirstOrDefault();
                MRVM.InWord     = InWord.ConvertToWords(crPfd.Amount.ToString());
                MRVM.AcType     = LoadDropDown.GetMrAgainst(crPfd.MRAgainst);
                MRVM.MRDate     = crPfd.ChqReceiptDate;
                MRVM.Amount     = crPfd.Amount;
                MRVM.PayMode    = "ChequeReceipt";
                MRVM.ChequeNo   = crPfd.ChqNo;
                MRVM.BankName   = _bankInfoService.All().FirstOrDefault(s => s.BankCode == crPfd.BankCode).BankName;;
                MRVM.Branch     = crPfd.BranchName;
                _chequeReceiptService.Update(TingCount);
                _chequeReceiptService.Save();
                return(new Rotativa.ViewAsPdf("~/Views/CashReceipt/MoneyReceiptPdf.cshtml", MRVM)
                {
                    PageSize = Rotativa.Options.Size.A4
                });
            }
            else
            {
                string errMsg = "NF";
                return(RedirectToAction("ChequeReceipts", "ChequeReceipts", new { errMsg }));
            }
        }
        public ActionResult UpdateCashMR(MoneyReceiptVM MrVM)
        {
            using (var transaction = new TransactionScope())
            {
                try
                {
                    RBACUser rUser = new RBACUser(Session["UserName"].ToString());
                    if (!rUser.HasPermission("MoneyReceiptCash_Update"))
                    {
                        return(Json("U", JsonRequestBehavior.AllowGet));
                    }

                    var VchrExist = _pVchrmainService.All().FirstOrDefault(s => s.VchrNo == MrVM.VchrNo);
                    if (VchrExist == null)
                    {
                        return(Json("3", JsonRequestBehavior.AllowGet));
                    }

                    var MR   = _moneyReceiptService.All().Where(x => x.MRSL == MrVM.MRSL && x.MRNo == MrVM.MRNo && MrVM.Posted == false).FirstOrDefault();
                    var CR   = _CashReceiptService.All().Where(s => s.RefNo == MrVM.MRSL).FirstOrDefault();
                    var CRSD = _CashReceiptSubDetailsAppService.All().Where(s => s.CashReceiptNo == CR.ReceiptNo).FirstOrDefault();

                    if (MR != null && CR != null && CRSD != null)
                    {
                        //var cabkop = _defACService.All().ToList().FirstOrDefault().CashAc;
                        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;
                        }

                        //For MR
                        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;
                        MR.MRAgainst   = MrVM.MRAgainst;
                        MR.VchrNo      = MrVM.VchrNo;
                        MR.Posted      = MrVM.Posted;
                        MR.AdjWithBill = MrVM.AdjWithBill;
                        MR.MRAmount    = MrVM.Amount;
                        MR.Accode      = MrVM.Accode;
                        MR.CustCode    = MrVM.CustCode;
                        MR.FinYear     = Session["FinYear"].ToString();
                        MR.JobNo       = MrVM.JobNo;
                        MR.Remarks     = MrVM.Remarks;
                        MR.Ca_Bk       = GCa;


                        CR.ReceiptDate = (DateTime)MrVM.MRDate;
                        CR.purAccode   = MrVM.MRAgainst;// "1.2.004";
                        CR.RefNo       = MrVM.MRSL;
                        CR.Amount      = (Double)MrVM.Amount;
                        CR.Remarks     = MrVM.Remarks;
                        CR.VoucherNo   = MrVM.VchrNo;

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


                        _moneyReceiptService.Update(MR);
                        _CashReceiptService.Update(CR);
                        _CashReceiptSubDetailsAppService.Update(CRSD);
                        _CashReceiptService.Save();
                        _CashReceiptSubDetailsAppService.Save();
                        _moneyReceiptService.Save();
                        TransactionLogService.SaveTransactionLog(_transactionLogService, "Cash MR", "Update", MR.MRSL, Session["UserName"].ToString());
                        LoadDropDown.journalVoucherRemoval("CR", MrVM.MRNo, MrVM.VchrNo, Session["FinYear"].ToString());
                        LoadDropDown.journalVoucherSave("CR", "I", MrVM.MRNo, 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 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));
                }
            }
        }