public SupplierSubscriptionServiceTests()
        {
            _mockRepo = new Mock <IRepository>();
            _mockRepo.SetupSequence(x => x.GetOne(It.IsAny <Expression <Func <SupplierSubscription, bool> > >()))
            .Returns(this.GetTestSupplierSubscription())
            .Returns(null)
            .Returns(null)
            .Returns(null);
            _mockRepo.Setup(x => x.GetById(1, It.IsAny <Expression <Func <Company, Object> > >())).Returns(this.GetTestCompany);
            _mockRepo.Setup(x => x.GetById <Supplier>(1)).Returns(this.GetTestSupplier);
            var ledgerAccountService = new LedgerAccountService(_mockRepo.Object);

            _supplierSubscriptionService = new SupplierSubscriptionService(_mockRepo.Object, ledgerAccountService);
        }
        public ActionResult SingleMergingPost(int?MergingLedgerAccountId, int?MainLedgerAccountId)
        {
            if (MergingLedgerAccountId == 0 || MergingLedgerAccountId == null)
            {
                ModelState.AddModelError("", "Please select Merging LedgerAccount.");
                return(View("SingleMerging"));
            }

            if (MainLedgerAccountId == 0 || MainLedgerAccountId == null)
            {
                ModelState.AddModelError("", "Please select Main LedgerAccount.");
                return(View("SingleMerging"));
            }

            if (MergingLedgerAccountId == MainLedgerAccountId)
            {
                ModelState.AddModelError("", "Merging LedgerAccount and Main LedgerAccount should not be same.");
                return(View("SingleMerging"));
            }


            LedgerAccount MergingLedgerAccount = new LedgerAccountService(_unitOfWork).Find((int)MergingLedgerAccountId);
            LedgerAccount MainLedgerAccount    = new LedgerAccountService(_unitOfWork).Find((int)MainLedgerAccountId);

            if (MergingLedgerAccount.PersonId != null && MainLedgerAccount.PersonId == null)
            {
                ModelState.AddModelError("", "Merging Ledger Account is a Person Ledger Account and Main Ledger Account is an individual Ledger Account, Can not be merge.");
                return(View("SingleMerging"));
            }

            //if (MergingLedgerAccount.PersonId == null && MainLedgerAccount.PersonId != null)
            //{
            //    ModelState.AddModelError("", "Main Ledger Account is a Person Ledger Account and Merging Ledger Account is an individual Ledger Account, Can not be merge.");
            //    return View("SingleMerging");
            //}


            MergingExcel M = new MergingExcel();

            M.MergingItem = MergingLedgerAccount.LedgerAccountName + MergingLedgerAccount.LedgerAccountSuffix;
            M.MainItem    = MainLedgerAccount.LedgerAccountName + MainLedgerAccount.LedgerAccountSuffix;
            //M.MergingItem = new LedgerAccountService(_unitOfWork).Find((int)MergingLedgerAccountId).Name;
            //M.MainItem = new LedgerAccountService(_unitOfWork).Find((int)MainLedgerAccountId).Name;

            IList <MergingExcel> MergingRecordList = new List <MergingExcel>();

            MergingRecordList.Add(M);

            return(DatabaseOperation(MergingRecordList));
        }
        public int LedgerPost(SaleOrderHeader pd)
        {
            int LedgerHeaderId = 0;

            if (pd.LedgerHeaderId == 0 || pd.LedgerHeaderId == null)
            {
                LedgerHeader LedgerHeader = new LedgerHeader();

                LedgerHeader.DocTypeId    = pd.DocTypeId;
                LedgerHeader.DocDate      = pd.DocDate;
                LedgerHeader.DocNo        = pd.DocNo;
                LedgerHeader.DivisionId   = pd.DivisionId;
                LedgerHeader.SiteId       = pd.SiteId;
                LedgerHeader.Remark       = pd.Remark;
                LedgerHeader.CreatedBy    = pd.CreatedBy;
                LedgerHeader.CreatedDate  = DateTime.Now.Date;
                LedgerHeader.ModifiedBy   = pd.ModifiedBy;
                LedgerHeader.ModifiedDate = DateTime.Now.Date;

                new LedgerHeaderService(_unitOfWork).Create(LedgerHeader);
            }
            else
            {
                LedgerHeader LedgerHeader = new LedgerHeaderService(_unitOfWork).Find((int)pd.LedgerHeaderId);

                LedgerHeader.DocTypeId    = pd.DocTypeId;
                LedgerHeader.DocDate      = pd.DocDate;
                LedgerHeader.DocNo        = pd.DocNo;
                LedgerHeader.DivisionId   = pd.DivisionId;
                LedgerHeader.SiteId       = pd.SiteId;
                LedgerHeader.Remark       = pd.Remark;
                LedgerHeader.ModifiedBy   = pd.ModifiedBy;
                LedgerHeader.ModifiedDate = DateTime.Now.Date;

                new LedgerHeaderService(_unitOfWork).Update(LedgerHeader);

                IEnumerable <Ledger> LedgerList = new LedgerService(_unitOfWork).FindForLedgerHeader(LedgerHeader.LedgerHeaderId);

                foreach (Ledger item in LedgerList)
                {
                    new LedgerService(_unitOfWork).Delete(item);
                }

                LedgerHeaderId = LedgerHeader.LedgerHeaderId;
            }


            int SalesAc = new LedgerAccountService(_unitOfWork).Find(LedgerAccountConstants.Sale).LedgerAccountId;

            if (pd.Advance > 0)
            {
                Ledger LedgerDr = new Ledger();
                if (LedgerHeaderId != 0)
                {
                    LedgerDr.LedgerHeaderId = LedgerHeaderId;
                }
                LedgerDr.LedgerAccountId       = SalesAc;
                LedgerDr.ContraLedgerAccountId = pd.BillToBuyerId;
                LedgerDr.AmtDr     = pd.Advance ?? 0;
                LedgerDr.AmtCr     = 0;
                LedgerDr.Narration = "";
                new LedgerService(_unitOfWork).Create(LedgerDr);

                Ledger LedgerCr = new Ledger();
                if (LedgerHeaderId != 0)
                {
                    LedgerCr.LedgerHeaderId = LedgerHeaderId;
                }
                LedgerCr.LedgerAccountId       = pd.BillToBuyerId;
                LedgerCr.ContraLedgerAccountId = SalesAc;
                LedgerCr.AmtDr     = 0;
                LedgerCr.AmtCr     = pd.Advance ?? 0;
                LedgerCr.Narration = "";
                new LedgerService(_unitOfWork).Create(LedgerCr);
            }

            return(LedgerHeaderId);
        }
예제 #4
0
        public ActionResult DeleteConfirmed(ReasonViewModel vm)
        {
            if (ModelState.IsValid)
            {
                Person         person         = new PersonService(_unitOfWork).Find(vm.id);
                BusinessEntity businessentiry = _BusinessEntityService.Find(vm.id);
                Courier        Courier        = _CourierService.Find(vm.id);

                ActivityLog al = new ActivityLog()
                {
                    ActivityType = (int)ActivityTypeContants.Deleted,
                    CreatedBy    = User.Identity.Name,
                    CreatedDate  = DateTime.Now,
                    DocId        = vm.id,
                    UserRemark   = vm.Reason,
                    Narration    = "Courier is deleted with Name:" + person.Name,
                    DocTypeId    = new DocumentTypeService(_unitOfWork).FindByName(TransactionDocCategoryConstants.SaleOrder).DocumentTypeId,
                    UploadDate   = DateTime.Now,
                };
                new ActivityLogService(_unitOfWork).Create(al);

                //Then find all the Person Address associated with the above Person.
                PersonAddress personaddress = _PersonAddressService.GetShipAddressByPersonId(vm.id);
                _PersonAddressService.Delete(personaddress.PersonAddressID);


                IEnumerable <PersonContact> personcontact = new PersonContactService(_unitOfWork).GetPersonContactIdListByPersonId(vm.id);
                //Mark ObjectState.Delete to all the Person Contact For Above Person.
                foreach (PersonContact item in personcontact)
                {
                    new PersonContactService(_unitOfWork).Delete(item.PersonContactID);
                }

                IEnumerable <PersonBankAccount> personbankaccount = new PersonBankAccountService(_unitOfWork).GetPersonBankAccountIdListByPersonId(vm.id);
                //Mark ObjectState.Delete to all the Person Contact For Above Person.
                foreach (PersonBankAccount item in personbankaccount)
                {
                    new PersonBankAccountService(_unitOfWork).Delete(item.PersonBankAccountID);
                }

                LedgerAccount LedgerAccount = new LedgerAccountService(_unitOfWork).GetLedgerAccountByPersondId(vm.id);
                new LedgerAccountService(_unitOfWork).Delete(LedgerAccount);


                IEnumerable <PersonRegistration> personregistration = new PersonRegistrationService(_unitOfWork).GetPersonRegistrationIdListByPersonId(vm.id);
                //Mark ObjectState.Delete to all the Person Registration For Above Person.
                foreach (PersonRegistration item in personregistration)
                {
                    new PersonRegistrationService(_unitOfWork).Delete(item.PersonRegistrationID);
                }


                // Now delete the Parent Courier
                _CourierService.Delete(Courier);
                _BusinessEntityService.Delete(businessentiry);
                new PersonService(_unitOfWork).Delete(person);


                try
                {
                    _unitOfWork.Save();
                }

                catch (Exception ex)
                {
                    string message = _exception.HandleException(ex);
                    ModelState.AddModelError("", message);
                    return(PartialView("_Reason", vm));
                }
                return(Json(new { success = true }));
            }
            return(PartialView("_Reason", vm));
        }
        //[ValidateAntiForgeryToken]
        public ActionResult _CreatePost(PersonViewModel PersonVm)
        {
            if (ModelState.IsValid)
            {
                if (PersonVm.PersonID == 0)
                {
                    Person         person         = Mapper.Map <PersonViewModel, Person>(PersonVm);
                    BusinessEntity businessentity = Mapper.Map <PersonViewModel, BusinessEntity>(PersonVm);
                    PersonAddress  personaddress  = Mapper.Map <PersonViewModel, PersonAddress>(PersonVm);
                    LedgerAccount  account        = Mapper.Map <PersonViewModel, LedgerAccount>(PersonVm);

                    person.IsActive     = true;
                    person.CreatedDate  = DateTime.Now;
                    person.ModifiedDate = DateTime.Now;
                    person.CreatedBy    = User.Identity.Name;
                    person.ModifiedBy   = User.Identity.Name;
                    person.ObjectState  = Model.ObjectState.Added;
                    new PersonService(_unitOfWork).Create(person);


                    int CurrentDivisionId = (int)System.Web.HttpContext.Current.Session["DivisionId"];
                    int CurrentSiteId     = (int)System.Web.HttpContext.Current.Session["SiteId"];

                    string Divisions = PersonVm.DivisionIds;
                    if (Divisions != null)
                    {
                        Divisions = "|" + Divisions.Replace(",", "|,|") + "|";
                    }
                    else
                    {
                        Divisions = "|" + CurrentDivisionId.ToString() + "|";
                    }

                    businessentity.DivisionIds = Divisions;

                    string Sites = PersonVm.SiteIds;
                    if (Sites != null)
                    {
                        Sites = "|" + Sites.Replace(",", "|,|") + "|";
                    }
                    else
                    {
                        Sites = "|" + CurrentSiteId.ToString() + "|";
                    }

                    businessentity.SiteIds = Sites;


                    new  BusinessEntityService(_unitOfWork).Create(businessentity);

                    personaddress.AddressType  = null;
                    personaddress.CreatedDate  = DateTime.Now;
                    personaddress.ModifiedDate = DateTime.Now;
                    personaddress.CreatedBy    = User.Identity.Name;
                    personaddress.ModifiedBy   = User.Identity.Name;
                    personaddress.ObjectState  = Model.ObjectState.Added;
                    new PersonAddressService(_unitOfWork).Create(personaddress);


                    account.LedgerAccountId      = db.LedgerAccount.Max(i => i.LedgerAccountId) + 1;
                    account.LedgerAccountName    = person.Name;
                    account.LedgerAccountSuffix  = person.Suffix;
                    account.LedgerAccountGroupId = PersonVm.LedgerAccountGroupId;
                    account.IsActive             = true;
                    account.CreatedDate          = DateTime.Now;
                    account.ModifiedDate         = DateTime.Now;
                    account.CreatedBy            = User.Identity.Name;
                    account.ModifiedBy           = User.Identity.Name;
                    account.ObjectState          = Model.ObjectState.Added;
                    new LedgerAccountService(_unitOfWork).Create(account);


                    if (PersonVm.CstNo != "" && PersonVm.CstNo != null)
                    {
                        PersonRegistration personregistration = new PersonRegistration();
                        personregistration.RegistrationType = PersonRegistrationType.CstNo;
                        personregistration.RegistrationNo   = PersonVm.CstNo;
                        personregistration.CreatedDate      = DateTime.Now;
                        personregistration.ModifiedDate     = DateTime.Now;
                        personregistration.CreatedBy        = User.Identity.Name;
                        personregistration.ModifiedBy       = User.Identity.Name;
                        personregistration.ObjectState      = Model.ObjectState.Added;
                        new PersonRegistrationService(_unitOfWork).Create(personregistration);
                    }

                    if (PersonVm.TinNo != "" && PersonVm.TinNo != null)
                    {
                        PersonRegistration personregistration = new PersonRegistration();
                        personregistration.RegistrationType = PersonRegistrationType.TinNo;
                        personregistration.RegistrationNo   = PersonVm.TinNo;
                        personregistration.CreatedDate      = DateTime.Now;
                        personregistration.ModifiedDate     = DateTime.Now;
                        personregistration.CreatedBy        = User.Identity.Name;
                        personregistration.ModifiedBy       = User.Identity.Name;
                        personregistration.ObjectState      = Model.ObjectState.Added;
                        new PersonRegistrationService(_unitOfWork).Create(personregistration);
                    }

                    if (PersonVm.PanNo != "" && PersonVm.PanNo != null)
                    {
                        PersonRegistration personregistration = new PersonRegistration();
                        personregistration.RegistrationType = PersonRegistrationType.PANNo;
                        personregistration.RegistrationNo   = PersonVm.PanNo;
                        personregistration.CreatedDate      = DateTime.Now;
                        personregistration.ModifiedDate     = DateTime.Now;
                        personregistration.CreatedBy        = User.Identity.Name;
                        personregistration.ModifiedBy       = User.Identity.Name;
                        personregistration.ObjectState      = Model.ObjectState.Added;
                        new PersonRegistrationService(_unitOfWork).Create(personregistration);
                    }


                    if (PersonVm.GstNo != "" && PersonVm.GstNo != null)
                    {
                        PersonRegistration personregistration = new PersonRegistration();
                        personregistration.RegistrationType = PersonRegistrationType.GstNo;
                        personregistration.RegistrationNo   = PersonVm.GstNo;
                        personregistration.CreatedDate      = DateTime.Now;
                        personregistration.ModifiedDate     = DateTime.Now;
                        personregistration.CreatedBy        = User.Identity.Name;
                        personregistration.ModifiedBy       = User.Identity.Name;
                        personregistration.ObjectState      = Model.ObjectState.Added;
                        new PersonRegistrationService(_unitOfWork).Create(personregistration);
                    }


                    if (PersonVm.AadharNo != "" && PersonVm.AadharNo != null)
                    {
                        PersonRegistration personregistration = new PersonRegistration();
                        personregistration.RegistrationType = PersonRegistrationType.AadharNo;
                        personregistration.RegistrationNo   = PersonVm.AadharNo;
                        personregistration.CreatedDate      = DateTime.Now;
                        personregistration.ModifiedDate     = DateTime.Now;
                        personregistration.CreatedBy        = User.Identity.Name;
                        personregistration.ModifiedBy       = User.Identity.Name;
                        personregistration.ObjectState      = Model.ObjectState.Added;
                        new PersonRegistrationService(_unitOfWork).Create(personregistration);
                    }


                    PersonRole personrole = new PersonRole();
                    personrole.PersonRoleId  = -1;
                    personrole.PersonId      = person.PersonID;
                    personrole.RoleDocTypeId = person.DocTypeId;
                    personrole.CreatedDate   = DateTime.Now;
                    personrole.ModifiedDate  = DateTime.Now;
                    personrole.CreatedBy     = User.Identity.Name;
                    personrole.ModifiedBy    = User.Identity.Name;
                    personrole.ObjectState   = Model.ObjectState.Added;
                    new PersonRoleService(_unitOfWork).Create(personrole);

                    int ProspectDocTypeId = new DocumentTypeService(_unitOfWork).Find(MasterDocTypeConstants.Prospect).DocumentTypeId;
                    if (person.DocTypeId == ProspectDocTypeId)
                    {
                        int CustomerDocTypeId = new DocumentTypeService(_unitOfWork).Find(MasterDocTypeConstants.Customer).DocumentTypeId;

                        PersonRole personrole1 = new PersonRole();
                        personrole.PersonRoleId   = -2;
                        personrole1.PersonId      = person.PersonID;
                        personrole1.RoleDocTypeId = CustomerDocTypeId;
                        personrole1.CreatedDate   = DateTime.Now;
                        personrole1.ModifiedDate  = DateTime.Now;
                        personrole1.CreatedBy     = User.Identity.Name;
                        personrole1.ModifiedBy    = User.Identity.Name;
                        personrole1.ObjectState   = Model.ObjectState.Added;
                        new PersonRoleService(_unitOfWork).Create(personrole1);
                    }


                    int ProcessId = new ProcessService(_unitOfWork).Find(ProcessConstants.Sales).ProcessId;

                    PersonProcess personprocess = new PersonProcess();
                    personprocess.PersonId     = person.PersonID;
                    personprocess.ProcessId    = ProcessId;
                    personprocess.CreatedDate  = DateTime.Now;
                    personprocess.ModifiedDate = DateTime.Now;
                    personprocess.CreatedBy    = User.Identity.Name;
                    personprocess.ModifiedBy   = User.Identity.Name;
                    personprocess.ObjectState  = Model.ObjectState.Added;
                    new PersonProcessService(_unitOfWork).Create(personprocess);


                    try
                    {
                        _unitOfWork.Save();
                    }

                    catch (Exception ex)
                    {
                        string message = _exception.HandleException(ex);
                        ModelState.AddModelError("", message);
                        return(View(PersonVm));
                    }

                    return(Json(new { success = true, PersonId = person.PersonID, Name = person.Name + ", " + person.Suffix + " [" + person.Code + "]" }));
                }
                else
                {
                    //string tempredirect = (Request["Redirect"].ToString());
                    Person             person         = Mapper.Map <PersonViewModel, Person>(PersonVm);
                    BusinessEntity     businessentity = Mapper.Map <PersonViewModel, BusinessEntity>(PersonVm);
                    PersonAddress      personaddress  = new PersonAddressService(_unitOfWork).Find(PersonVm.PersonAddressID);
                    LedgerAccount      account        = new LedgerAccountService(_unitOfWork).Find(PersonVm.AccountId);
                    PersonRegistration PersonCst      = new PersonRegistrationService(_unitOfWork).Find(PersonVm.PersonRegistrationCstNoID ?? 0);
                    PersonRegistration PersonTin      = new PersonRegistrationService(_unitOfWork).Find(PersonVm.PersonRegistrationTinNoID ?? 0);
                    PersonRegistration PersonPAN      = new PersonRegistrationService(_unitOfWork).Find(PersonVm.PersonRegistrationPanNoID ?? 0);
                    PersonRegistration PersonGst      = new PersonRegistrationService(_unitOfWork).Find(PersonVm.PersonRegistrationGstNoID ?? 0);
                    PersonRegistration PersonAadhar   = new PersonRegistrationService(_unitOfWork).Find(PersonVm.PersonRegistrationAadharNoID ?? 0);



                    person.IsActive     = true;
                    person.ModifiedDate = DateTime.Now;
                    person.ModifiedBy   = User.Identity.Name;
                    new PersonService(_unitOfWork).Update(person);


                    new BusinessEntityService(_unitOfWork).Update(businessentity);

                    personaddress.Address      = PersonVm.Address;
                    personaddress.CityId       = PersonVm.CityId;
                    personaddress.Zipcode      = PersonVm.Zipcode;
                    personaddress.ModifiedDate = DateTime.Now;
                    personaddress.ModifiedBy   = User.Identity.Name;
                    personaddress.ObjectState  = Model.ObjectState.Modified;
                    new PersonAddressService(_unitOfWork).Update(personaddress);


                    account.LedgerAccountName   = person.Name;
                    account.LedgerAccountSuffix = person.Suffix;
                    account.ModifiedDate        = DateTime.Now;
                    account.ModifiedBy          = User.Identity.Name;
                    new LedgerAccountService(_unitOfWork).Update(account);

                    if (PersonVm.CstNo != null && PersonVm.CstNo != "")
                    {
                        if (PersonCst != null)
                        {
                            PersonCst.RegistrationNo = PersonVm.CstNo;
                            new PersonRegistrationService(_unitOfWork).Update(PersonCst);
                        }
                        else
                        {
                            PersonRegistration personregistration = new PersonRegistration();
                            personregistration.PersonId         = PersonVm.PersonID;
                            personregistration.RegistrationType = PersonRegistrationType.CstNo;
                            personregistration.RegistrationNo   = PersonVm.CstNo;
                            personregistration.CreatedDate      = DateTime.Now;
                            personregistration.ModifiedDate     = DateTime.Now;
                            personregistration.CreatedBy        = User.Identity.Name;
                            personregistration.ModifiedBy       = User.Identity.Name;
                            personregistration.ObjectState      = Model.ObjectState.Added;
                            new PersonRegistrationService(_unitOfWork).Create(personregistration);
                        }
                    }

                    if (PersonVm.TinNo != null && PersonVm.TinNo != "")
                    {
                        if (PersonTin != null)
                        {
                            PersonTin.RegistrationNo = PersonVm.TinNo;
                            new PersonRegistrationService(_unitOfWork).Update(PersonTin);
                        }
                        else
                        {
                            PersonRegistration personregistration = new PersonRegistration();
                            personregistration.PersonId         = PersonVm.PersonID;
                            personregistration.RegistrationType = PersonRegistrationType.TinNo;
                            personregistration.RegistrationNo   = PersonVm.TinNo;
                            personregistration.CreatedDate      = DateTime.Now;
                            personregistration.ModifiedDate     = DateTime.Now;
                            personregistration.CreatedBy        = User.Identity.Name;
                            personregistration.ModifiedBy       = User.Identity.Name;
                            personregistration.ObjectState      = Model.ObjectState.Added;
                            new PersonRegistrationService(_unitOfWork).Create(personregistration);
                        }
                    }

                    if (PersonVm.PanNo != null && PersonVm.PanNo != "")
                    {
                        if (PersonPAN != null)
                        {
                            PersonPAN.RegistrationNo = PersonVm.PanNo;
                            new PersonRegistrationService(_unitOfWork).Update(PersonPAN);
                        }
                        else
                        {
                            PersonRegistration personregistration = new PersonRegistration();
                            personregistration.PersonId         = PersonVm.PersonID;
                            personregistration.RegistrationType = PersonRegistrationType.PANNo;
                            personregistration.RegistrationNo   = PersonVm.PanNo;
                            personregistration.CreatedDate      = DateTime.Now;
                            personregistration.ModifiedDate     = DateTime.Now;
                            personregistration.CreatedBy        = User.Identity.Name;
                            personregistration.ModifiedBy       = User.Identity.Name;
                            personregistration.ObjectState      = Model.ObjectState.Added;
                            new PersonRegistrationService(_unitOfWork).Create(personregistration);
                        }
                    }

                    if (PersonVm.GstNo != null && PersonVm.GstNo != "")
                    {
                        if (PersonGst != null)
                        {
                            PersonGst.RegistrationNo = PersonVm.GstNo;
                            new PersonRegistrationService(_unitOfWork).Update(PersonGst);
                        }
                        else
                        {
                            PersonRegistration personregistration = new PersonRegistration();
                            personregistration.PersonId         = PersonVm.PersonID;
                            personregistration.RegistrationType = PersonRegistrationType.GstNo;
                            personregistration.RegistrationNo   = PersonVm.GstNo;
                            personregistration.CreatedDate      = DateTime.Now;
                            personregistration.ModifiedDate     = DateTime.Now;
                            personregistration.CreatedBy        = User.Identity.Name;
                            personregistration.ModifiedBy       = User.Identity.Name;
                            personregistration.ObjectState      = Model.ObjectState.Added;
                            new PersonRegistrationService(_unitOfWork).Create(personregistration);
                        }
                    }

                    if (PersonVm.AadharNo != null && PersonVm.AadharNo != "")
                    {
                        if (PersonAadhar != null)
                        {
                            PersonAadhar.RegistrationNo = PersonVm.AadharNo;
                            new PersonRegistrationService(_unitOfWork).Update(PersonAadhar);
                        }
                        else
                        {
                            PersonRegistration personregistration = new PersonRegistration();
                            personregistration.PersonId         = PersonVm.PersonID;
                            personregistration.RegistrationType = PersonRegistrationType.AadharNo;
                            personregistration.RegistrationNo   = PersonVm.AadharNo;
                            personregistration.CreatedDate      = DateTime.Now;
                            personregistration.ModifiedDate     = DateTime.Now;
                            personregistration.CreatedBy        = User.Identity.Name;
                            personregistration.ModifiedBy       = User.Identity.Name;
                            personregistration.ObjectState      = Model.ObjectState.Added;
                            new PersonRegistrationService(_unitOfWork).Create(personregistration);
                        }
                    }


                    try
                    {
                        _unitOfWork.Save();
                    }

                    catch (Exception ex)
                    {
                        string message = _exception.HandleException(ex);
                        ModelState.AddModelError("", message);
                        return(View("Create", PersonVm));
                    }

                    return(Json(new { success = true, PersonId = person.PersonID, Name = person.Name + ", " + person.Suffix + " [" + person.Code + "]" }));
                }
            }
            return(View(PersonVm));
        }
        public ActionResult PostSummary(JobInvoiceSummaryDetailViewModel vm)
        {
            //TempData["CSEXC"] = "Customize Test Exception";


            String ErrorMsg = "";

            foreach (var item in vm.JobInvoiceSummaryViewModel)
            {
                if (item.AdvanceAdjusted > item.InvoiceAmount + item.TanaAmount)
                {
                    string ProductUidName = "";
                    if (item.ProductUidId != null)
                    {
                        ProductUidName = new ProductUidService(_unitOfWork).Find((int)item.ProductUidId).ProductUidName;
                    }

                    ErrorMsg = "Total adjusted advance for barcode " + ProductUidName + " is exceeding invoice amount.";
                }
            }


            var AdvanceData = (from H in vm.JobInvoiceSummaryViewModel
                               group new { H } by new { H.CostCenterId } into Result
                               select new
            {
                CostCenterId = Result.Key.CostCenterId,
                TotalAdvanceAmount = Result.Max(m => m.H.AdvanceAmount),
                TotalAdvanceAdjusted = Result.Sum(m => m.H.AdvanceAdjusted)
            }).ToList();

            foreach (var item in AdvanceData)
            {
                if (item.TotalAdvanceAdjusted > item.TotalAdvanceAmount)
                {
                    string CostCenterName = new CostCenterService(_unitOfWork).Find(item.CostCenterId).CostCenterName;
                    ErrorMsg = "Total adjusted advance for purja " + CostCenterName + " is exceeding total advance amount.";
                }
            }



            var TdsData = (from H in vm.JobInvoiceSummaryViewModel
                           group new { H } by new { H.PersonId } into Result
                           select new
            {
                PersonId = Result.Key.PersonId,
                TotalTdsAmount = Result.Max(m => m.H.TdsAmount),
                TotalTdsAdjusted = Result.Sum(m => m.H.TdsAdjusted)
            }).ToList();

            foreach (var item in TdsData)
            {
                if (item.TotalTdsAdjusted > item.TotalTdsAmount)
                {
                    string PersonName = new PersonService(_unitOfWork).Find(item.PersonId).Name;
                    ErrorMsg = "Total adjusted tds for person " + PersonName + " is exceeding total tds amount.";
                }
            }

            if (ErrorMsg != "")
            {
                TempData["CSEXC"] = ErrorMsg;
                return(View("Summary", vm));
            }



            List <LogTypeViewModel> LogList = new List <LogTypeViewModel>();
            bool Modified = false;
            int  Id       = vm.JobInvoiceHeaderId;

            var Header = _JobInvoiceHeaderService.Find(Id);


            int LedgerHeaderId                   = 0;
            int LedgerLineId                     = 0;
            int LedgerId                         = 0;
            int LedgerAdjId                      = 0;
            int TanaPaymentDueCnt                = 0;
            int TdsCollectionCnt                 = 0;
            int SavedTanaLedgerHeaderId          = 0;
            int SavedTdsCollectionLedgerHeaderId = 0;
            int TanaPaymentDueAcId               = new LedgerAccountService(_unitOfWork).Find(TanaPaymentDueAccount).LedgerAccountId;


            DeleteLedgerTransaction(Header.JobInvoiceHeaderId);


            DataTable AdvanceAdjustedDataTable = new DataTable();

            AdvanceAdjustedDataTable.Columns.Add("Id");
            AdvanceAdjustedDataTable.Columns.Add("Qty");

            foreach (var item in vm.JobInvoiceSummaryViewModel)
            {
                if (item.TanaAmount > 0)
                {
                    #region "Tana Payment Due"
                    if (TanaPaymentDueCnt == 0)
                    {
                        LedgerHeader TanaLedgerHeader = new LedgerHeader();
                        TanaLedgerHeader.LedgerHeaderId  = LedgerHeaderId;
                        TanaLedgerHeader.DocTypeId       = new DocumentTypeService(_unitOfWork).Find(TanaPaymentDueDocType).DocumentTypeId;
                        TanaLedgerHeader.DocDate         = DateTime.Now.Date;
                        TanaLedgerHeader.DivisionId      = Header.DivisionId;
                        TanaLedgerHeader.SiteId          = Header.SiteId;
                        TanaLedgerHeader.DocNo           = new DocumentTypeService(_unitOfWork).FGetNewDocNo("DocNo", ConfigurationManager.AppSettings["DataBaseSchema"] + ".LedgerHeaders", TanaLedgerHeader.DocTypeId, TanaLedgerHeader.DocDate, TanaLedgerHeader.DivisionId, TanaLedgerHeader.SiteId);
                        TanaLedgerHeader.LedgerAccountId = TanaPaymentDueAcId;
                        TanaLedgerHeader.Narration       = "Tana Payment Due";
                        TanaLedgerHeader.Status          = (int)StatusConstants.Submitted;
                        TanaLedgerHeader.ProcessId       = new ProcessService(_unitOfWork).Find(ProcessConstants.Weaving).ProcessId;
                        TanaLedgerHeader.CostCenterId    = null;
                        TanaLedgerHeader.AdjustmentType  = "Due";
                        TanaLedgerHeader.PaymentFor      = DateTime.Now.Date;

                        TanaLedgerHeader.ReferenceDocId     = Header.JobInvoiceHeaderId;
                        TanaLedgerHeader.ReferenceDocTypeId = Header.DocTypeId;

                        TanaLedgerHeader.CreatedDate  = DateTime.Now;
                        TanaLedgerHeader.ModifiedDate = DateTime.Now;
                        TanaLedgerHeader.CreatedBy    = User.Identity.Name;
                        TanaLedgerHeader.ModifiedBy   = User.Identity.Name;
                        TanaLedgerHeader.ObjectState  = Model.ObjectState.Added;
                        new LedgerHeaderService(_unitOfWork).Create(TanaLedgerHeader);
                        LedgerHeaderId          = LedgerHeaderId - 1;
                        TanaPaymentDueCnt       = TanaPaymentDueCnt + 1;
                        SavedTanaLedgerHeaderId = TanaLedgerHeader.LedgerHeaderId;
                    }


                    LedgerLine TanaLedgerLine = new LedgerLine();
                    TanaLedgerLine.LedgerLineId       = LedgerLineId;
                    TanaLedgerLine.LedgerHeaderId     = SavedTanaLedgerHeaderId;
                    TanaLedgerLine.LedgerAccountId    = new LedgerAccountService(_unitOfWork).GetLedgerAccountByPersondId(item.PersonId).LedgerAccountId;
                    TanaLedgerLine.CostCenterId       = item.CostCenterId;
                    TanaLedgerLine.ProductUidId       = item.ProductUidId;
                    TanaLedgerLine.Amount             = item.TanaAmount;
                    TanaLedgerLine.ReferenceDocLineId = item.JobInvoiceLineId;
                    TanaLedgerLine.ReferenceDocTypeId = Header.DocTypeId;
                    TanaLedgerLine.CreatedDate        = DateTime.Now;
                    TanaLedgerLine.ModifiedDate       = DateTime.Now;
                    TanaLedgerLine.CreatedBy          = User.Identity.Name;
                    TanaLedgerLine.ModifiedBy         = User.Identity.Name;
                    TanaLedgerLine.ObjectState        = Model.ObjectState.Added;
                    new LedgerLineService(_unitOfWork).Create(TanaLedgerLine);
                    LedgerLineId = LedgerLineId - 1;


                    Ledger TanaLedgerDr = new Ledger();
                    TanaLedgerDr.LedgerId              = LedgerId;
                    TanaLedgerDr.LedgerHeaderId        = SavedTanaLedgerHeaderId;
                    TanaLedgerDr.LedgerAccountId       = TanaPaymentDueAcId;
                    TanaLedgerDr.ContraLedgerAccountId = TanaLedgerLine.LedgerAccountId;
                    TanaLedgerDr.CostCenterId          = null;
                    TanaLedgerDr.ProductUidId          = item.ProductUidId;
                    TanaLedgerDr.AmtDr        = TanaLedgerLine.Amount;
                    TanaLedgerDr.AmtCr        = 0;
                    TanaLedgerDr.Narration    = "Tana Payment Due";
                    TanaLedgerDr.LedgerLineId = TanaLedgerLine.LedgerLineId;
                    TanaLedgerDr.ObjectState  = Model.ObjectState.Added;
                    new LedgerService(_unitOfWork).Create(TanaLedgerDr);
                    LedgerId = LedgerId - 1;


                    Ledger TanaLedgerCr = new Ledger();
                    TanaLedgerDr.LedgerId              = LedgerId;
                    TanaLedgerCr.LedgerHeaderId        = SavedTanaLedgerHeaderId;
                    TanaLedgerCr.LedgerAccountId       = TanaLedgerLine.LedgerAccountId;
                    TanaLedgerCr.ContraLedgerAccountId = TanaPaymentDueAcId;
                    TanaLedgerCr.CostCenterId          = item.CostCenterId;
                    TanaLedgerDr.ProductUidId          = item.ProductUidId;
                    TanaLedgerCr.AmtDr        = 0;
                    TanaLedgerCr.AmtCr        = TanaLedgerLine.Amount;
                    TanaLedgerCr.Narration    = null;
                    TanaLedgerCr.LedgerLineId = TanaLedgerLine.LedgerLineId;
                    TanaLedgerCr.ObjectState  = Model.ObjectState.Added;
                    new LedgerService(_unitOfWork).Create(TanaLedgerCr);
                    LedgerId = LedgerId - 1;

                    #endregion
                }

                if (item.TdsAdjusted > 0)
                {
                    #region "Tds Collection"


                    var TdsRate = (from H in db.BusinessEntity
                                   join Tr in db.TdsRate on new { X1 = H.TdsCategoryId ?? 0, X2 = H.TdsGroupId ?? 0 } equals new { X1 = Tr.TdsCategoryId, X2 = Tr.TdsGroupId } into TdsRateTable
                                   from TdsRateTab in TdsRateTable.DefaultIfEmpty()
                                   where H.PersonID == item.PersonId
                                   select TdsRateTab).FirstOrDefault();


                    if (TdsCollectionCnt == 0)
                    {
                        LedgerHeader TdsLedgerHeader = new LedgerHeader();
                        TdsLedgerHeader.LedgerHeaderId  = LedgerHeaderId;
                        TdsLedgerHeader.DocTypeId       = new DocumentTypeService(_unitOfWork).Find(TdsCollectionDocType).DocumentTypeId;
                        TdsLedgerHeader.DocDate         = DateTime.Now.Date;
                        TdsLedgerHeader.DivisionId      = Header.DivisionId;
                        TdsLedgerHeader.SiteId          = Header.SiteId;
                        TdsLedgerHeader.DocNo           = new DocumentTypeService(_unitOfWork).FGetNewDocNo("DocNo", ConfigurationManager.AppSettings["DataBaseSchema"] + ".LedgerHeaders", TdsLedgerHeader.DocTypeId, TdsLedgerHeader.DocDate, TdsLedgerHeader.DivisionId, TdsLedgerHeader.SiteId);
                        TdsLedgerHeader.LedgerAccountId = TdsRate.LedgerAccountId;
                        TdsLedgerHeader.Narration       = "Tds Adjustment";
                        TdsLedgerHeader.Status          = (int)StatusConstants.Submitted;
                        TdsLedgerHeader.ProcessId       = new ProcessService(_unitOfWork).Find(ProcessConstants.Weaving).ProcessId;
                        TdsLedgerHeader.CostCenterId    = null;
                        TdsLedgerHeader.AdjustmentType  = "Due";
                        TdsLedgerHeader.PaymentFor      = DateTime.Now.Date;

                        TdsLedgerHeader.ReferenceDocId     = Header.JobInvoiceHeaderId;
                        TdsLedgerHeader.ReferenceDocTypeId = Header.DocTypeId;

                        TdsLedgerHeader.CreatedDate  = DateTime.Now;
                        TdsLedgerHeader.ModifiedDate = DateTime.Now;
                        TdsLedgerHeader.CreatedBy    = User.Identity.Name;
                        TdsLedgerHeader.ModifiedBy   = User.Identity.Name;
                        TdsLedgerHeader.ObjectState  = Model.ObjectState.Added;
                        new LedgerHeaderService(_unitOfWork).Create(TdsLedgerHeader);
                        LedgerHeaderId   = LedgerHeaderId - 1;
                        TdsCollectionCnt = TdsCollectionCnt + 1;
                        SavedTdsCollectionLedgerHeaderId = TdsLedgerHeader.LedgerHeaderId;
                    }


                    LedgerLine TdsLedgerLine = new LedgerLine();
                    TdsLedgerLine.LedgerLineId    = LedgerLineId;
                    TdsLedgerLine.LedgerHeaderId  = SavedTdsCollectionLedgerHeaderId;
                    TdsLedgerLine.LedgerAccountId = new LedgerAccountService(_unitOfWork).GetLedgerAccountByPersondId(item.PersonId).LedgerAccountId;
                    if (item.CostCenterId != 0)
                    {
                        TdsLedgerLine.CostCenterId = item.CostCenterId;
                    }
                    TdsLedgerLine.ProductUidId = item.ProductUidId;
                    TdsLedgerLine.BaseValue    = item.TdsAdjusted * 100 / TdsRate.Percentage;
                    TdsLedgerLine.Amount       = item.TdsAdjusted;
                    TdsLedgerLine.CreatedDate  = DateTime.Now;
                    TdsLedgerLine.ModifiedDate = DateTime.Now;
                    TdsLedgerLine.CreatedBy    = User.Identity.Name;
                    TdsLedgerLine.ModifiedBy   = User.Identity.Name;
                    TdsLedgerLine.ObjectState  = Model.ObjectState.Added;
                    new LedgerLineService(_unitOfWork).Create(TdsLedgerLine);
                    LedgerLineId = LedgerLineId - 1;


                    Ledger TdsLedgerDr = new Ledger();
                    TdsLedgerDr.LedgerId              = LedgerId;
                    TdsLedgerDr.LedgerHeaderId        = SavedTdsCollectionLedgerHeaderId;
                    TdsLedgerDr.LedgerAccountId       = TdsLedgerLine.LedgerAccountId;
                    TdsLedgerDr.ContraLedgerAccountId = TdsRate.LedgerAccountId;
                    if (item.CostCenterId != 0)
                    {
                        TdsLedgerDr.CostCenterId = item.CostCenterId;
                    }
                    TdsLedgerDr.ProductUidId = item.ProductUidId;
                    TdsLedgerDr.AmtDr        = TdsLedgerLine.Amount;
                    TdsLedgerDr.AmtCr        = 0;
                    TdsLedgerDr.Narration    = "Tds Adjusted.";
                    TdsLedgerDr.LedgerLineId = TdsLedgerLine.LedgerLineId;
                    TdsLedgerDr.ObjectState  = Model.ObjectState.Added;
                    new LedgerService(_unitOfWork).Create(TdsLedgerDr);
                    LedgerId = LedgerId - 1;


                    Ledger TdsLedgerCr = new Ledger();
                    TdsLedgerCr.LedgerId              = LedgerId;
                    TdsLedgerCr.LedgerHeaderId        = SavedTdsCollectionLedgerHeaderId;
                    TdsLedgerCr.LedgerAccountId       = (int)TdsRate.LedgerAccountId;
                    TdsLedgerCr.ContraLedgerAccountId = TdsLedgerLine.LedgerAccountId;
                    TdsLedgerCr.CostCenterId          = null;
                    TdsLedgerCr.ProductUidId          = item.ProductUidId;
                    TdsLedgerCr.AmtDr        = 0;
                    TdsLedgerCr.AmtCr        = TdsLedgerLine.Amount;
                    TdsLedgerCr.Narration    = null;
                    TdsLedgerCr.LedgerLineId = TdsLedgerLine.LedgerLineId;
                    TdsLedgerCr.ObjectState  = Model.ObjectState.Added;
                    new LedgerService(_unitOfWork).Create(TdsLedgerCr);
                    LedgerId = LedgerId - 1;


                    var TempLedger = (from L in db.Ledger
                                      where L.ReferenceDocLineId == item.JobInvoiceLineId && L.ReferenceDocTypeId == Header.DocTypeId
                                      select new
                    {
                        LedgerCrId = L.LedgerId
                    }).FirstOrDefault();

                    LedgerAdj LedgerAdj = new LedgerAdj();
                    LedgerAdj.LedgerAdjId  = LedgerAdjId;
                    LedgerAdj.DrLedgerId   = TdsLedgerDr.LedgerId;
                    LedgerAdj.CrLedgerId   = TempLedger == null ? 0 : TempLedger.LedgerCrId;
                    LedgerAdj.SiteId       = Header.SiteId;
                    LedgerAdj.Adj_Type     = "Tds Adjustment";
                    LedgerAdj.Amount       = TdsLedgerDr.AmtDr;
                    LedgerAdj.CreatedBy    = Header.CreatedBy;
                    LedgerAdj.CreatedDate  = DateTime.Now;
                    LedgerAdj.ModifiedBy   = Header.ModifiedBy;
                    LedgerAdj.ModifiedDate = DateTime.Now;
                    LedgerAdj.ObjectState  = Model.ObjectState.Added;
                    new LedgerAdjService(_unitOfWork).Create(LedgerAdj);
                    LedgerAdjId = LedgerAdjId - 1;

                    #endregion
                }

                if (item.AdvanceAdjusted > 0)
                {
                    var AdvanceAdjustedDataRow = AdvanceAdjustedDataTable.NewRow();
                    AdvanceAdjustedDataRow["Id"]  = item.JobInvoiceLineId;
                    AdvanceAdjustedDataRow["Qty"] = item.AdvanceAdjusted;
                    AdvanceAdjustedDataTable.Rows.Add(AdvanceAdjustedDataRow);
                }
            }


            if (AdvanceAdjustedDataTable.Rows.Count > 0)
            {
                string  ConnectionString = (string)System.Web.HttpContext.Current.Session["DefaultConnectionString"];
                DataSet ds = new DataSet();
                using (SqlConnection sqlConnection = new SqlConnection(ConnectionString))
                {
                    sqlConnection.Open();
                    using (SqlCommand cmd = new SqlCommand("" + ConfigurationManager.AppSettings["DataBaseSchema"] + ".spGetLedgerAdj"))
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Connection  = sqlConnection;
                        cmd.Parameters.AddWithValue("@T", AdvanceAdjustedDataTable);
                        cmd.CommandTimeout = 1000;
                        using (SqlDataAdapter adp = new SqlDataAdapter(cmd))
                        {
                            adp.Fill(ds);
                        }
                    }
                }

                for (int j = 0; j <= ds.Tables[0].Rows.Count - 1; j++)
                {
                    LedgerAdj LedgerAdj = new LedgerAdj();
                    LedgerAdj.LedgerAdjId  = LedgerAdjId;
                    LedgerAdj.DrLedgerId   = (int)ds.Tables[0].Rows[j]["LedgerDrId"];
                    LedgerAdj.CrLedgerId   = (int)ds.Tables[0].Rows[j]["LedgerCrId"];
                    LedgerAdj.SiteId       = Header.SiteId;
                    LedgerAdj.Adj_Type     = "Advance Adjustment";
                    LedgerAdj.Amount       = (decimal)ds.Tables[0].Rows[j]["BalanceDr"];
                    LedgerAdj.CreatedBy    = Header.CreatedBy;
                    LedgerAdj.CreatedDate  = DateTime.Now;
                    LedgerAdj.ModifiedBy   = Header.ModifiedBy;
                    LedgerAdj.ModifiedDate = DateTime.Now;
                    LedgerAdj.ObjectState  = Model.ObjectState.Added;
                    new LedgerAdjService(_unitOfWork).Create(LedgerAdj);
                    LedgerAdjId = LedgerAdjId - 1;
                }
            }

            try
            {
                _unitOfWork.Save();
            }

            catch (Exception ex)
            {
                string message = _exception.HandleException(ex);
                ModelState.AddModelError("", message);
                PrepareViewBag(vm.JobInvoiceHeaderId);
                return(Json(new { Success = false }));
            }

            LogActivity.LogActivityDetail(LogVm.Map(new ActiivtyLogViewModel
            {
                DocTypeId    = Header.DocTypeId,
                DocId        = Header.JobInvoiceHeaderId,
                ActivityType = (int)ActivityTypeContants.Modified,
                DocNo        = Header.DocNo,
                //xEModifications = Modifications,
                DocDate   = Header.DocDate,
                DocStatus = Header.Status,
            }));

            string RetUrl = "";


            if (Header.Status == (int)StatusConstants.Drafted || Header.Status == (int)StatusConstants.Import)
            {
                RetUrl = System.Configuration.ConfigurationManager.AppSettings["JobsDomain"] + "/JobInvoiceHeader/Modify/" + Header.JobInvoiceHeaderId;
            }
            else if (Header.Status == (int)StatusConstants.Submitted || Header.Status == (int)StatusConstants.ModificationSubmitted || Header.Status == (int)StatusConstants.Modified)
            {
                RetUrl = System.Configuration.ConfigurationManager.AppSettings["JobsDomain"] + "/JobInvoiceHeader/ModifyAfter_Submit/" + Header.JobInvoiceHeaderId;
            }
            else if (Header.Status == (int)StatusConstants.Approved || Header.Status == (int)StatusConstants.Closed)
            {
                RetUrl = System.Configuration.ConfigurationManager.AppSettings["JobsDomain"] + "/JobInvoiceHeader/ModifyAfter_Approve/" + Header.JobInvoiceHeaderId;
            }
            else
            {
                RetUrl = System.Configuration.ConfigurationManager.AppSettings["JobsDomain"] + "/JobInvoiceHeader/Index/" + Header.DocTypeId;
            }

            return(Json(new { Success = true, Url = RetUrl }));
        }
        public ActionResult _CreatePost(PersonOpeningViewModel svm)
        {
            if (ModelState.IsValid)
            {
                LedgerAccount LedgerAccount = new LedgerAccountService(_unitOfWork).GetLedgerAccountByPersondId(svm.PersonId);

                if (svm.LedgerHeaderId <= 0)
                {
                    var temp = (from H in db.LedgerHeader
                                where H.DocTypeId == svm.DocTypeId && H.DocNo == svm.DocNo && H.SiteId == svm.SiteId && H.DivisionId == svm.DivisionId
                                select H).FirstOrDefault();

                    if (temp != null)
                    {
                        if (svm.LedgerSetting.IsAutoDocNo == true)
                        {
                            svm.DocNo = new DocumentTypeService(_unitOfWork).FGetNewDocNo("DocNo", ConfigurationManager.AppSettings["DataBaseSchema"] + ".LedgerHeaders", svm.DocTypeId, svm.DocDate, svm.DivisionId, svm.SiteId);
                        }
                    }
                }


                if (svm.LedgerHeaderId == 0)
                {
                    LedgerHeader Header = new LedgerHeader();

                    Header.DocHeaderId     = svm.PersonId;
                    Header.DocNo           = svm.DocNo;
                    Header.DocTypeId       = svm.DocTypeId;
                    Header.DocDate         = svm.DocDate;
                    Header.SiteId          = svm.SiteId;
                    Header.DivisionId      = svm.DivisionId;
                    Header.PaymentFor      = null;
                    Header.AdjustmentType  = null;
                    Header.ProcessId       = null;
                    Header.GodownId        = null;
                    Header.LedgerAccountId = null;
                    Header.DrCr            = null;
                    Header.PartyDocNo      = svm.PartyDocNo;
                    Header.PartyDocDate    = svm.PartyDocDate;
                    Header.Narration       = svm.Narration;
                    Header.CreatedDate     = DateTime.Now;
                    Header.CreatedBy       = User.Identity.Name;
                    Header.ModifiedDate    = DateTime.Now;
                    Header.ModifiedBy      = User.Identity.Name;
                    Header.ObjectState     = Model.ObjectState.Added;
                    db.LedgerHeader.Add(Header);


                    LedgerLine Line = new LedgerLine();

                    Line.LedgerHeaderId     = Header.LedgerHeaderId;
                    Line.LedgerAccountId    = LedgerAccount.LedgerAccountId;
                    Line.Amount             = svm.Amount;
                    Line.ChqNo              = null;
                    Line.ChqDate            = null;
                    Line.CostCenterId       = null;
                    Line.BaseRate           = 0;
                    Line.BaseValue          = 0;
                    Line.ReferenceId        = null;
                    Line.ProductUidId       = null;
                    Line.ReferenceDocTypeId = null;
                    Line.ReferenceDocId     = null;
                    Line.CreatedDate        = DateTime.Now;
                    Line.ModifiedDate       = DateTime.Now;
                    Line.CreatedBy          = User.Identity.Name;
                    Line.Remark             = svm.Narration;
                    Line.ModifiedBy         = User.Identity.Name;
                    Line.ObjectState        = Model.ObjectState.Added;
                    db.LedgerLine.Add(Line);


                    Ledger Ledger = new Ledger();

                    if (svm.DrCr == NatureConstants.Debit)
                    {
                        Ledger.AmtDr = svm.Amount;
                    }
                    else if (svm.DrCr == NatureConstants.Credit)
                    {
                        Ledger.AmtCr = svm.Amount;
                    }

                    Ledger.ChqNo = Line.ChqNo;
                    Ledger.ContraLedgerAccountId = null;
                    Ledger.CostCenterId          = Line.CostCenterId;
                    Ledger.DueDate         = null;
                    Ledger.LedgerAccountId = Line.LedgerAccountId;
                    Ledger.LedgerHeaderId  = Line.LedgerHeaderId;
                    Ledger.LedgerLineId    = Line.LedgerLineId;
                    Ledger.ProductUidId    = Line.ProductUidId;
                    Ledger.Narration       = Header.Narration;
                    Ledger.ObjectState     = Model.ObjectState.Added;
                    Ledger.LedgerId        = 1;
                    db.Ledger.Add(Ledger);



                    try
                    {
                        db.SaveChanges();
                    }

                    catch (Exception ex)
                    {
                        string message = _exception.HandleException(ex);
                        PrepareViewBag();
                        ModelState.AddModelError("", message);
                        return(PartialView("_Create", svm));
                    }
                    return(RedirectToAction("_Create", new { id = svm.PersonId }));
                }
                else
                {
                    LedgerHeader Header = new LedgerHeaderService(_unitOfWork).Find(svm.LedgerHeaderId);
                    LedgerLine   Line   = new LedgerLineService(_unitOfWork).FindByLedgerHeader(svm.LedgerHeaderId).FirstOrDefault();
                    Ledger       Ledger = new LedgerService(_unitOfWork).FindForLedgerHeader(svm.LedgerHeaderId).FirstOrDefault();


                    Header.SiteId       = svm.SiteId;
                    Header.DivisionId   = svm.DivisionId;
                    Header.DocNo        = svm.DocNo;
                    Header.DocTypeId    = svm.DocTypeId;
                    Header.DocDate      = svm.DocDate;
                    Header.PartyDocNo   = svm.PartyDocNo;
                    Header.PartyDocDate = svm.PartyDocDate;
                    Header.Narration    = svm.Narration;
                    Header.ModifiedDate = DateTime.Now;
                    Header.ModifiedBy   = User.Identity.Name;
                    Header.ObjectState  = Model.ObjectState.Modified;
                    db.LedgerHeader.Add(Header);


                    Line.Amount       = svm.Amount;
                    Line.ModifiedDate = DateTime.Now;
                    Line.ModifiedBy   = User.Identity.Name;
                    Line.ObjectState  = Model.ObjectState.Modified;
                    db.LedgerLine.Add(Line);


                    if (svm.DrCr == NatureConstants.Debit)
                    {
                        Ledger.AmtDr = svm.Amount;
                        Ledger.AmtCr = 0;
                    }
                    else if (svm.DrCr == NatureConstants.Credit)
                    {
                        Ledger.AmtCr = svm.Amount;
                        Ledger.AmtDr = 0;
                    }

                    Ledger.ChqNo           = Line.ChqNo;
                    Ledger.CostCenterId    = Line.CostCenterId;
                    Ledger.LedgerAccountId = Line.LedgerAccountId;
                    Ledger.LedgerHeaderId  = Line.LedgerHeaderId;
                    Ledger.LedgerLineId    = Line.LedgerLineId;
                    Ledger.ProductUidId    = Line.ProductUidId;
                    Ledger.Narration       = Header.Narration;
                    Ledger.ObjectState     = Model.ObjectState.Modified;
                    db.Ledger.Add(Ledger);

                    try
                    {
                        db.SaveChanges();
                    }

                    catch (Exception ex)
                    {
                        string message = _exception.HandleException(ex);
                        PrepareViewBag();
                        ModelState.AddModelError("", message);
                        return(PartialView("_Create", svm));
                    }


                    return(Json(new { success = true }));
                }
            }

            PrepareViewBag();
            return(PartialView("_Create", svm));
        }