public ActionResult AddAccount(int?AcHeadID, decimal?Amount, string Remarks, string TransType = "CN")
        {
            int      fyearid  = Convert.ToInt32(Session["fyearid"].ToString());
            DateTime fromdate = Convert.ToDateTime(Session["FyearFrom"].ToString());
            DateTime todate   = Convert.ToDateTime(Session["FyearTo"].ToString());
            List <CreditNoteDetailVM> list = (List <CreditNoteDetailVM>)Session["CreditNoteDetail"];
            CreditNoteDetailVM        item = new CreditNoteDetailVM();

            if (list != null)
            {
                item = list.Where(cc => cc.AcHeadID == AcHeadID).FirstOrDefault();
            }
            else
            {
                list = new List <CreditNoteDetailVM>();
            }
            if (item == null)
            {
                item            = new CreditNoteDetailVM();
                item.AcHeadID   = AcHeadID;
                item.AcHeadName = db.AcHeads.Find(item.AcHeadID).AcHead1;
                item.Amount     = Amount;
                item.Remarks    = Remarks;
                list.Add(item);
            }

            Session["CreditNoteDetail"] = list;
            CreditNoteVM vm = new CreditNoteVM();

            vm.Details = list;
            return(PartialView("CreditNoteDetail", vm));
        }
        public ActionResult ServiceIndex()
        {
            var data = db.CreditNotes.Where(d => d.IsShipping == false).ToList();

            List <CreditNoteVM> lst = new List <CreditNoteVM>();

            foreach (var item in data)
            {
                //var job = (from c in db.JInvoices where c.InvoiceID == item.InvoiceID select c).FirstOrDefault();
                string jobcode = "";
                //if (job != null)
                //{
                //    var jobid = job.JobID;
                //     jobcode = (from j in db.JobGenerations where j.JobID == jobid select j.JobCode).FirstOrDefault();

                //}
                //else
                //{
                var purchaseinvoice = (from d in db.SalesInvoices where d.SalesInvoiceID == item.InvoiceID && d.IsShipping == false select d).FirstOrDefault();
                jobcode = purchaseinvoice.SalesInvoiceNo;

                //jobcode = item.InvoiceID.ToString();
                //}
                string customer = (from c in db.CUSTOMERs where c.CustomerID == item.CustomerID && c.CustomerType == 2 select c.Customer1).FirstOrDefault();

                CreditNoteVM v = new CreditNoteVM();
                v.JobNO        = jobcode;
                v.Date         = item.CreditNoteDate.Value;
                v.CustomerName = customer;
                v.Amount       = item.Amount.Value;
                lst.Add(v);
            }

            return(View(lst));
        }
        public ActionResult DeleteAccount(int index)
        {
            int      fyearid  = Convert.ToInt32(Session["fyearid"].ToString());
            DateTime fromdate = Convert.ToDateTime(Session["FyearFrom"].ToString());
            DateTime todate   = Convert.ToDateTime(Session["FyearTo"].ToString());
            List <CreditNoteDetailVM> list  = (List <CreditNoteDetailVM>)Session["CreditNoteDetail"];
            List <CreditNoteDetailVM> list1 = new List <CreditNoteDetailVM>();
            CreditNoteDetailVM        item  = new CreditNoteDetailVM();

            list.RemoveAt(index);

            Session["CreditNoteDetail"] = list;
            CreditNoteVM vm = new CreditNoteVM();

            vm.Details = list;
            return(PartialView("CreditNoteDetail", vm));
        }
        public ActionResult Index()
        {
            var data = db.CreditNotes.ToList().OrderByDescending(c => c.CreditNoteDate).ToList();

            List <CreditNoteVM> lst = new List <CreditNoteVM>();

            foreach (var item in data)
            {
                ////var job = (from c in db.JInvoices where c.InvoiceID == item.InvoiceID select c).FirstOrDefault();
                //string jobcode = "";

                //if (item.InvoiceType == "TR")
                //{
                //    var purchaseinvoice = (from d in db.CustomerInvoices where d.CustomerInvoiceID == item.InvoiceID select d).FirstOrDefault();
                //    if (purchaseinvoice!=null)
                //        jobcode = purchaseinvoice.CustomerInvoiceNo;
                //}
                //else if (item.InvoiceType == "OP")
                //{
                //    var purchaseinvoice = (from d in db.AcOPInvoiceDetails where d.AcOPInvoiceDetailID == item.InvoiceID select d).FirstOrDefault();
                //    if (purchaseinvoice!=null)
                //        jobcode = purchaseinvoice.InvoiceNo;

                //}

                string customer = (from c in db.CustomerMasters where c.CustomerID == item.CustomerID && (c.CustomerType == "CR" || c.CustomerType == "CL") select c.CustomerName).FirstOrDefault();

                CreditNoteVM v = new CreditNoteVM();
                //v.JobNO = jobcode;

                v.CreditNoteNo = item.CreditNoteNo;
                v.CreditNoteID = item.CreditNoteID;
                v.Date         = item.CreditNoteDate.Value;
                v.CustomerName = customer;
                v.Description  = item.Description;
                v.Amount       = item.Amount.Value;
                lst.Add(v);
            }

            return(View(lst));
        }
        public ActionResult Create(int id = 0)
        {
            int fyearid = Convert.ToInt32(Session["fyearid"].ToString());

            //ViewBag.customer = db.CustomerMasters.Where(d => d.CustomerType == "CR").OrderBy(x => x.CustomerName).ToList();
            ViewBag.achead = db.AcHeads.ToList();

            if (id == 0)
            {
                ViewBag.Title = "CREDIT NOTE/CUSTOMER JOURNAL";
                CreditNoteVM vm = new CreditNoteVM();
                vm.CreditNoteNo       = AccountsDAO.GetMaxCreditNoteNo(fyearid);
                vm.Date               = CommanFunctions.GetLastDayofMonth().Date;
                vm.TransType          = "";
                vm.AcHeadID           = 52; //Customer control account
                vm.AmountType         = "0";
                vm.AcDetailAmountType = "1";
                List <CreditNoteDetailVM> list = new List <CreditNoteDetailVM>();
                vm.Details = list;

                Session["CreditNoteDetail"] = list;

                return(View(vm));
            }
            else
            {
                ViewBag.Title = "CREDIT NOTE/CUSTOMER JOURNAL - Modify";
                CreditNoteVM vm = new CreditNoteVM();
                var          v  = db.CreditNotes.Find(id);
                vm.CreditNoteID = v.CreditNoteID;
                vm.CreditNoteNo = v.CreditNoteNo;
                vm.Date         = Convert.ToDateTime(v.CreditNoteDate);
                vm.AcJournalID  = Convert.ToInt32(v.AcJournalID);
                vm.CustomerID   = Convert.ToInt32(v.CustomerID);
                var customer = db.CustomerMasters.Find(v.CustomerID).CustomerName;
                if (customer != null)
                {
                    vm.CustomerName = customer;
                }
                vm.AcHeadID    = Convert.ToInt32(v.AcHeadID);
                vm.Amount      = Convert.ToDecimal(v.Amount);
                vm.Description = v.Description;
                vm.TransType   = v.TransType;
                if (v.RecPayID != null && v.RecPayID != 0)
                {
                    vm.RecPayID = Convert.ToInt32(v.RecPayID);
                }
                else
                {
                    vm.RecPayID = 0;
                }

                var detaillist = (from c in db.CreditNoteDetails join d in db.AcHeads on c.AcHeadID equals d.AcHeadID where c.CreditNoteID == v.CreditNoteID select new CreditNoteDetailVM {
                    AcHeadID = c.AcHeadID, AcHeadName = d.AcHead1, Amount = c.Amount, Remarks = c.Remarks
                }).ToList();
                vm.Details = detaillist;
                Session["CreditNoteDetail"] = detaillist;
                if (v.InvoiceID != null && v.InvoiceID != 0 && v.TransType == "CN")
                {
                    vm.InvoiceID   = Convert.ToInt32(v.InvoiceID);
                    vm.InvoiceType = v.InvoiceType;
                    vm.ForInvoice  = true;


                    SetTradeInvoiceOfCustomer(vm.CustomerID, 0, vm.CreditNoteID, vm.TransType);
                    List <CustomerTradeReceiptVM> lst = (List <CustomerTradeReceiptVM>)Session["CustomerInvoice"];

                    if (v.InvoiceType == "TR")
                    {
                        var invoice = lst.Where(cc => cc.SalesInvoiceID == vm.InvoiceID && cc.InvoiceType == "TR").FirstOrDefault();
                        if (invoice != null)
                        {
                            vm.InvoiceNo      = invoice.InvoiceNo;
                            vm.InvoiceDate    = invoice.DateTime;
                            vm.InvoiceAmount  = Convert.ToDecimal(invoice.InvoiceAmount);
                            vm.ReceivedAmount = Convert.ToDecimal(invoice.AmountReceived);
                        }
                    }
                    else if (v.InvoiceType == "OP")
                    {
                        //var invoice1 = db.AcOPInvoiceDetails.Where(cc=>cc.AcOPInvoiceDetailID ==vm.InvoiceID).FirstOrDefault();
                        //if (invoice1 != null)
                        //{
                        //    vm.InvoiceNo = invoice1.InvoiceNo;
                        //    vm.InvoiceDate = Convert.ToDateTime(invoice1.InvoiceDate).ToString("dd/MM/yyyy");
                        //    vm.InvoiceAmount = Convert.ToDecimal(invoice1.Amount);
                        //}
                        var invoice = lst.Where(cc => cc.SalesInvoiceID == vm.InvoiceID && cc.InvoiceType == "OP").FirstOrDefault();
                        vm.InvoiceNo      = invoice.InvoiceNo;
                        vm.InvoiceDate    = invoice.DateTime;
                        vm.InvoiceAmount  = Convert.ToDecimal(invoice.InvoiceAmount);
                        vm.ReceivedAmount = Convert.ToDecimal(invoice.AmountReceived);
                    }
                }
                else if (v.RecPayID != null && v.RecPayID != 0 && v.TransType == "CJ")
                {
                    vm.InvoiceID   = Convert.ToInt32(v.RecPayID);
                    vm.InvoiceType = v.InvoiceType;
                    vm.ForInvoice  = true;

                    SetTradeInvoiceOfCustomer(vm.CustomerID, 0, vm.CreditNoteID, vm.TransType);
                    List <CustomerTradeReceiptVM> lst = (List <CustomerTradeReceiptVM>)Session["CustomerInvoice"];

                    if (v.InvoiceType == "TR")
                    {
                        var invoice = lst.Where(cc => cc.SalesInvoiceID == vm.InvoiceID && cc.InvoiceType == "TR").FirstOrDefault();
                        if (invoice != null)
                        {
                            vm.InvoiceNo      = invoice.InvoiceNo;
                            vm.InvoiceDate    = invoice.DateTime;
                            vm.InvoiceAmount  = Convert.ToDecimal(invoice.InvoiceAmount);
                            vm.ReceivedAmount = Convert.ToDecimal(invoice.AmountReceived);
                        }
                    }
                    else if (v.InvoiceType == "OP")
                    {
                        var invoice = lst.Where(cc => cc.SalesInvoiceID == vm.InvoiceID && cc.InvoiceType == "OP").FirstOrDefault();
                        vm.InvoiceNo      = invoice.InvoiceNo;
                        vm.InvoiceDate    = invoice.DateTime;
                        vm.InvoiceAmount  = Convert.ToDecimal(invoice.InvoiceAmount);
                        vm.ReceivedAmount = Convert.ToDecimal(invoice.AmountReceived);
                    }
                }
                else
                {
                    vm.ForInvoice = false;
                }


                vm.Date = Convert.ToDateTime(v.CreditNoteDate);
                //vm.
                return(View(vm));
            }
        }
        public ActionResult Create(CreditNoteVM v)
        {
            var             userid   = Convert.ToInt32(Session["UserID"]);
            int             BranchID = Convert.ToInt32(Session["CurrentBranchID"].ToString());
            AcJournalMaster ajm      = new AcJournalMaster();
            int             fyearid  = Convert.ToInt32(Session["fyearid"].ToString());

            if (v.AcJournalID > 0)
            {
                ajm = db.AcJournalMasters.Find(v.AcJournalID);
                var ajmd = db.AcJournalDetails.Where(cc => cc.AcJournalID == v.AcJournalID).ToList();
                db.AcJournalDetails.RemoveRange(ajmd);
                db.AcJournalMasters.Remove(ajm);
                db.SaveChanges();
                v.AcJournalID = 0;
            }
            if (v.AcJournalID == 0)
            {
                int acjm = 0;
                acjm = (from c in db.AcJournalMasters orderby c.AcJournalID descending select c.AcJournalID).FirstOrDefault();

                ajm.AcJournalID       = acjm + 1;
                ajm.AcCompanyID       = Convert.ToInt32(Session["CurrentCompanyID"].ToString());
                ajm.BranchID          = Convert.ToInt32(Session["CurrentBranchID"].ToString());
                ajm.AcFinancialYearID = fyearid;
                ajm.PaymentType       = 1;
                ajm.Remarks           = v.Description; //  "Credit Note for " + v.CustomerName + " invoice : " + v.InvoiceNo;
                ajm.StatusDelete      = false;

                ajm.VoucherNo = AccountsDAO.GetMaxVoucherNo(v.TransType, fyearid);
                ajm.TransDate = v.Date;

                ajm.TransType   = 2;
                ajm.VoucherType = v.TransType;


                db.AcJournalMasters.Add(ajm);
                db.SaveChanges();
            }


            AcJournalDetail a = new AcJournalDetail();

            a = db.AcJournalDetails.Where(cc => cc.AcJournalID == ajm.AcJournalID && cc.Amount < 0).FirstOrDefault();
            if (a == null)
            {
                a = new AcJournalDetail();
                a.AcJournalDetailID = 0;
            }
            if (a.AcJournalDetailID == 0)
            {
                int maxacj = (from c in db.AcJournalDetails orderby c.AcJournalDetailID descending select c.AcJournalDetailID).FirstOrDefault();
                a.AcJournalDetailID = maxacj + 1;
                a.AcJournalID       = ajm.AcJournalID;
                a.AcHeadID          = v.AcHeadID;// customercon.AcHeadID; ;
            }
            if (v.TransType == "CN")
            {
                a.Amount = -1 * v.Amount;
            }
            else
            {
                a.Amount = v.Amount;
            }
            a.BranchID = Convert.ToInt32(Session["CurrentBranchID"].ToString());
            a.Remarks  = v.Description;

            if (a.ID == 0)
            {
                db.AcJournalDetails.Add(a);
                db.SaveChanges();
            }
            else
            {
                db.Entry(a).State = System.Data.Entity.EntityState.Modified;
                db.SaveChanges();
            }
            foreach (var detail in v.Details)
            {
                AcJournalDetail b = new AcJournalDetail();
                b.AcJournalDetailID = 0;
                if (b.AcJournalDetailID == 0)
                {
                    int maxacj = 0;
                    maxacj = (from c in db.AcJournalDetails orderby c.AcJournalDetailID descending select c.AcJournalDetailID).FirstOrDefault();

                    b.AcJournalDetailID = maxacj + 1;
                    b.AcJournalID       = ajm.AcJournalID;
                }
                b.AcHeadID = detail.AcHeadID;
                if (v.TransType == "CN")
                {
                    b.Amount = detail.Amount;
                }
                else
                {
                    b.Amount = -1 * detail.Amount;
                }
                b.BranchID = Convert.ToInt32(Session["CurrentBranchID"].ToString());
                b.Remarks  = detail.Remarks;
                if (b.ID == 0)
                {
                    db.AcJournalDetails.Add(b);
                    db.SaveChanges();
                }
                else
                {
                    db.Entry(b).State = System.Data.Entity.EntityState.Modified;
                    db.SaveChanges();
                }
            }

            var invid    = 0;
            int?recpayid = 0;

            CreditNote d = new CreditNote();

            if (v.CreditNoteID == 0)
            {
                int maxid = 0;

                var data = (from c in db.CreditNotes orderby c.CreditNoteID descending select c).FirstOrDefault();

                if (data == null)
                {
                    maxid = 1;
                }
                else
                {
                    maxid = data.CreditNoteID + 1;
                }

                d.CreditNoteID = maxid;
                d.CreditNoteNo = AccountsDAO.GetMaxCreditNoteNo(fyearid);
                d.FYearID      = Convert.ToInt32(Session["fyearid"].ToString());
                d.AcCompanyID  = Convert.ToInt32(Session["CurrentCompanyID"].ToString());
                d.BranchID     = Convert.ToInt32(Session["CurrentBranchID"].ToString());
                d.statusclose  = false;
                d.CreatedBy    = userid;
                d.CreatedDate  = CommanFunctions.GetCurrentDateTime();
                d.ModifiedBy   = userid;
                d.ModifiedDate = CommanFunctions.GetCurrentDateTime();
            }
            else
            {
                d = db.CreditNotes.Find(v.CreditNoteID);
                var det = db.CreditNoteDetails.Where(cc => cc.CreditNoteID == v.CreditNoteID).ToList();
                if (det != null)
                {
                    db.CreditNoteDetails.RemoveRange(det);
                    db.SaveChanges();
                }

                d.ModifiedBy   = userid;
                d.ModifiedDate = CommanFunctions.GetCurrentDateTime();
            }

            d.InvoiceType    = v.InvoiceType;
            d.CreditNoteDate = v.Date;
            if (v.TransType == "CN")
            {
                d.Amount = v.Amount;
            }
            else
            {
                d.Amount = -1 * v.Amount;
            }
            d.AcJournalID = ajm.AcJournalID;

            d.AcHeadID    = v.AcHeadID;
            d.CustomerID  = v.CustomerID;
            d.Description = v.Description;
            d.TransType   = v.TransType;
            if (v.InvoiceID != 0)
            {
                if (v.TransType == "CN")
                {
                    d.InvoiceID = v.InvoiceID;
                }
                else
                {
                    d.RecPayID = v.InvoiceID;
                }
            }
            else
            {
                d.InvoiceID = 0;
            }
            //d.IsShipping = true;
            if (v.CreditNoteID == 0)
            {
                db.CreditNotes.Add(d);
                db.SaveChanges();
                TempData["SuccessMsg"] = "Successfully Added Credit Note";
            }
            else
            {
                db.Entry(d).State = System.Data.Entity.EntityState.Modified;
                db.SaveChanges();
                TempData["SuccessMsg"] = "Successfully Updated Credit Note";
            }

            foreach (var detail in v.Details)
            {
                CreditNoteDetail det = new CreditNoteDetail();
                det.AcHeadID     = detail.AcHeadID;
                det.Amount       = detail.Amount;
                det.Remarks      = detail.Remarks;
                det.CreditNoteID = d.CreditNoteID;
                db.CreditNoteDetails.Add(det);
                db.SaveChanges();
            }

            return(RedirectToAction("Index", "CreditNote"));
        }
        public ActionResult Create(CreditNoteVM v)
        {
            AcJournalMaster ajm = new AcJournalMaster();

            int acjm = 0;

            acjm = (from c in db.AcJournalMasters orderby c.AcJournalID descending select c.AcJournalID).FirstOrDefault();

            ajm.AcJournalID       = acjm + 1;
            ajm.AcCompanyID       = Convert.ToInt32(Session["AcCompanyID"].ToString());
            ajm.AcFinancialYearID = Convert.ToInt32(Session["fyearid"].ToString());
            ajm.PaymentType       = 1;
            ajm.Remarks           = "Credit Note";
            ajm.StatusDelete      = false;
            ajm.TransDate         = v.Date;
            ajm.VoucherNo         = "C-" + ajm.AcJournalID;
            ajm.TransType         = 2;
            ajm.VoucherType       = "";

            db.AcJournalMasters.Add(ajm);
            db.SaveChanges();


            AcJournalDetail b = new AcJournalDetail();

            int maxacj = 0;

            maxacj = (from c in db.AcJournalDetails orderby c.AcJournalDetailID descending select c.AcJournalDetailID).FirstOrDefault();

            b.AcJournalDetailID = maxacj + 1;
            b.AcJournalID       = ajm.AcJournalID;
            b.AcHeadID          = v.AcHeadID;
            b.Amount            = -v.Amount;
            b.BranchID          = Convert.ToInt32(Session["AcCompanyID"].ToString());
            b.Remarks           = "";

            db.AcJournalDetails.Add(b);
            db.SaveChanges();


            AcJournalDetail a = new AcJournalDetail();

            maxacj = (from c in db.AcJournalDetails orderby c.AcJournalDetailID descending select c.AcJournalDetailID).FirstOrDefault();
            a.AcJournalDetailID = maxacj + 1;
            a.AcJournalID       = ajm.AcJournalID;
            a.AcHeadID          = v.AcHeadID;
            a.Amount            = v.Amount;
            a.BranchID          = Convert.ToInt32(Session["AcCompanyID"].ToString());
            a.Remarks           = "";



            db.AcJournalDetails.Add(a);
            db.SaveChanges();

            var invid    = 0;
            int?recpayid = 0;

            //if (v.TradingInvoice == false)
            //{
            //    int jobid = (from j in db.JobGenerations where j.JobCode == v.JobNO select j.JobID).FirstOrDefault();

            //     invid = (from c in db.JInvoices where c.JobID == jobid select c.InvoiceID).FirstOrDefault();


            //    var recpay = (from c in db.RecPayDetails where c.InvoiceID == invid select c).FirstOrDefault();
            //    if (recpay != null)
            //    {
            //        recpayid = recpay.RecPayID;
            //    }
            //}
            //else
            //{
            //    invid =Convert.ToInt32(v.JobNO);

            //  var  recpay = (from c in db.RecPayDetails where c.InvoiceID == invid select c).FirstOrDefault();
            //    if(recpay != null)
            //    {
            //        recpayid = recpay.RecPayID;
            //    }
            //}
            invid = Convert.ToInt32(v.JobNO);
            var ids = (from x in db.SalesInvoiceDetails where x.SalesInvoiceID == invid select(int?) x.SalesInvoiceDetailID).ToList();

            recpayid = (from c in db.RecPayDetails where ids.Contains(c.InvoiceID) select c.RecPayID).FirstOrDefault().Value;

            CreditNote d = new CreditNote();

            //int max = (from c in db.CreditNotes orderby c.CreditNoteNo descending select c.CreditNoteNo).FirstOrDefault().Value;
            int maxid = 0;

            var data = (from c in db.CreditNotes orderby c.CreditNoteID descending select c).FirstOrDefault();

            if (data == null)
            {
                maxid = 1;
            }
            else
            {
                maxid = data.CreditNoteID + 1;
            }

            d.CreditNoteID   = maxid;
            d.CreditNoteNo   = maxid;
            d.InvoiceID      = invid;
            d.CreditNoteDate = v.Date;
            d.Amount         = v.Amount;
            d.AcJournalID    = ajm.AcJournalID;
            d.FYearID        = Convert.ToInt32(Session["fyearid"].ToString());
            d.AcCompanyID    = Convert.ToInt32(Session["AcCompanyID"].ToString());
            d.RecPayID       = recpayid;
            d.AcHeadID       = v.AcHeadID;
            d.CustomerID     = v.CustomerID;
            d.statusclose    = false;
            d.InvoiceType    = "C";
            d.IsShipping     = true;

            db.CreditNotes.Add(d);
            db.SaveChanges();

            TempData["SuccessMsg"] = "Successfully Added Credit Note";
            return(RedirectToAction("Index", "CreditNote"));
        }