public async Task <HttpResponseMessage> DeleteReceiveAndPay([FromBody] int id) { var organId = Convert.ToInt32(SecurityManager.CurrentUserContext.OrganizationId); FinanYearRule finanYearRule = new FinanYearRule(); var finanYerar = await finanYearRule.GetCurrentFinanYear(organId); var res = await Rule.FindAsync(id); PayRecevieDeleteValidate validator = new PayRecevieDeleteValidate(); FluentValidation.Results.ValidationResult results = validator.Validate(res); string failurs = ""; if (!results.IsValid) { foreach (var error in results.Errors) { failurs += "<br/>" + error.ErrorMessage; } return(Request.CreateResponse(HttpStatusCode.OK, new { resultCode = ZhivarEnums.ResultCode.ValidationError, data = failurs })); } await Rule.DeleteAsync(id); await Rule.SaveChangesAsync(); //var res = await payRecevieRule.GetAllByOrganIdAsync(organId); return(Request.CreateResponse(HttpStatusCode.OK, new { resultCode = (int)ZhivarEnums.ResultCode.Successful, data = res })); }
private async Task <Document> createDocument(TransferMoney transferMoney, string from, string to, int organId) { DocumentRule documentRule = new DocumentRule(); FinanYearRule finanYearRule = new FinanYearRule(); var finanYears = await finanYearRule.GetAllByOrganIdAsync(organId); var finanYear = finanYears.Where(x => x.Closed == false && x.OrganId == organId).SingleOrDefault(); Document document = new Document(); document.Credit = transferMoney.Amount; document.DateTime = DateTime.Now; document.Debit = transferMoney.Amount; document.Description = " انتقال وجه از " + from + " به " + to; document.DisplayDate = PersianDateUtils.ToPersianDate(DateTime.Now); document.IsManual = false; document.Number = await documentRule.createNumberDocumentAsync(organId); document.Number2 = await documentRule.createNumberDocumentAsync(organId); document.Status = ZhivarEnums.DocumentStatus.TaeedShode; document.StatusString = "تایید شده"; document.OrganId = organId; document.FinanYear = finanYear; document.FinanYearId = finanYear.ID; document.Type = ZhivarEnums.NoeDoc.Transfer; List <Transaction> transactions = new List <Transaction>(); transactions = await RegisterTransaction(document, transferMoney, organId); document.Transactions = transactions; return(document); }
public virtual async Task <HttpResponseMessage> SaveFinanYear([FromBody] RequestFinanYear requestFinanYear) { var organId = Convert.ToInt32(SecurityManager.CurrentUserContext.OrganizationId); FinanYearRule finanYearRule = new FinanYearRule(); var finanQuery = await finanYearRule.GetAllByOrganIdAsync(organId); var finanYear = finanQuery.Where(x => x.FirstYear == true).SingleOrDefault(); return(Request.CreateResponse(HttpStatusCode.OK, new { resultCode = ZhivarEnums.ResultCode.Successful, data = requestFinanYear.finanYear })); }
private async Task <Document> createDocument(PayRecevie payRecevie, int organId, int numberDocument) { FinanYearRule finanYearRule = new FinanYearRule(); var finanYears = await finanYearRule.GetAllByOrganIdAsync(organId); var finanYear = finanYears.Where(x => x.Closed == false).SingleOrDefault(); Document document = new Document(); document.Credit = payRecevie.Amount; document.DateTime = PersianDateUtils.ToDateTime(payRecevie.DisplayDate); document.Debit = payRecevie.Amount; document.Description = payRecevie.Description; document.DisplayDate = payRecevie.DisplayDate; document.IsManual = false; document.Status = ZhivarEnums.DocumentStatus.TaeedShode; document.ObjectState = OMF.Common.Enums.ObjectState.Added; if (numberDocument != 0) { document.Number = numberDocument; document.Number2 = numberDocument; } else { DocumentRule documentRule = new DocumentRule(); document.Number = await documentRule.createNumberDocumentAsync(organId); document.Number2 = await documentRule.createNumberDocumentAsync(organId); } document.Status = ZhivarEnums.DocumentStatus.TaeedShode; document.StatusString = "تایید شده"; document.OrganId = organId; document.FinanYear = finanYear; document.FinanYearId = finanYear.ID; List <Transaction> transactions = new List <Transaction>(); if (payRecevie.IsReceive) { document.Type = ZhivarEnums.NoeDoc.Recive; transactions = await RegisterTransactionRecevie(document, payRecevie, organId); } else { document.Type = ZhivarEnums.NoeDoc.Pay; transactions = await RegisterTransactionPay(document, payRecevie, organId); } document.Transactions = transactions; return(document); }
public virtual async Task <HttpResponseMessage> GetCurrentUserAndBusinesses() { var userId = SecurityManager.CurrentUserContext.UserId; PersonRule personRule = new PersonRule(); var person = personRule.GetPersonByUserId(Convert.ToInt32(userId)); FinanYearRule finanYearRule = new FinanYearRule(); var finanYears = await finanYearRule.GetAllByOrganIdAsync(person.ID); BussinessSiteRule bussinessSiteRule = new BussinessSiteRule(); var bussinesses = await bussinessSiteRule.GetAllByOrganIdAsync(person.ID); var responseCurrentUserAndBusinesses = new ResponseCurrentUserAndBusinesses(); responseCurrentUserAndBusinesses.appVersion = "3.1"; responseCurrentUserAndBusinesses.businesses = new List <Bussiness>(); responseCurrentUserAndBusinesses.businesses = bussinesses.ToList(); responseCurrentUserAndBusinesses.businessInfo = new BusinessInfo(); responseCurrentUserAndBusinesses.businessInfo.business = bussinesses.SingleOrDefault(); responseCurrentUserAndBusinesses.businessInfo.finanYear = finanYears.Where(x => x.Closed == false).SingleOrDefault(); responseCurrentUserAndBusinesses.businessInfo.finanYears = finanYears.ToList(); responseCurrentUserAndBusinesses.showCloseFinanYearAlert = false; responseCurrentUserAndBusinesses.todayDate = Utilities.PersianDateUtils.ToPersianDate(DateTime.Now); responseCurrentUserAndBusinesses.user = new Common.User() { FirstName = person.Nam, Id = person.ID, IsConfirmed = true, LastName = person.NamKhanvadegi, Phone = person.Tel, Name = person.Nam + " " + person.NamKhanvadegi, }; return(Request.CreateResponse(HttpStatusCode.OK, new { resultCode = (int)Enums.ResultCode.Successful, data = responseCurrentUserAndBusinesses })); }
private async Task <Document> CreateDocumentChangeChequeStatus(ChequesAndStatsVM chequesAndStatsVM, int organId) { FinanYearRule finanYearRule = new FinanYearRule(); var finanYears = await finanYearRule.GetAllByOrganIdAsync(organId); var finanYear = finanYears.Where(x => x.Closed == false).SingleOrDefault(); ChequeRule chequeRule = new ChequeRule(); var cheque = await chequeRule.FindAsync(chequesAndStatsVM.chequeId); DocumentRule documentRule = new DocumentRule(); Document document = new Document(); document.Credit = cheque.Amount; document.DateTime = PersianDateUtils.ToDateTime(chequesAndStatsVM.date); document.Debit = cheque.Amount; document.Description = chequesAndStatsVM.description; document.DisplayDate = chequesAndStatsVM.date; document.IsManual = false; document.Status = ZhivarEnums.DocumentStatus.TaeedShode; document.Number = await documentRule.createNumberDocumentAsync(organId); document.Number2 = await documentRule.createNumberDocumentAsync(organId); document.StatusString = "تایید شده"; document.OrganId = organId; document.FinanYear = finanYear; document.FinanYearId = finanYear.ID; document.Type = ZhivarEnums.NoeDoc.Recive; List <Transaction> transactions = new List <Transaction>(); AccountRule accountRule = new AccountRule(); var accounts = await accountRule.GetAllByOrganIdAsync(organId); switch (chequesAndStatsVM.receiveType) { case "cash": { var accountCash = accounts.Where(x => x.ComplteCoding == "1101" + chequesAndStatsVM.detailAccount.Code).SingleOrDefault(); transactions.Add(new Transaction() { //AccDocument = document, AccountId = accountCash.ID, Amount = cheque.Amount, Description = chequesAndStatsVM.description, Credit = 0, Debit = cheque.Amount, IsCredit = false, IsDebit = true, DocumentId = document.ID, Date = PersianDateUtils.ToDateTime(chequesAndStatsVM.date), DisplayDate = chequesAndStatsVM.date }); break; } case "bank": { var accountBank = accounts.Where(x => x.ComplteCoding == "1103" + chequesAndStatsVM.detailAccount.Code).SingleOrDefault(); transactions.Add(new Transaction() { //AccDocument = document, AccountId = accountBank.ID, Amount = cheque.Amount, Description = chequesAndStatsVM.description, Credit = 0, Debit = cheque.Amount, IsCredit = false, IsDebit = true, DocumentId = document.ID, Date = PersianDateUtils.ToDateTime(chequesAndStatsVM.date), DisplayDate = chequesAndStatsVM.date }); break; } } var asnadDreaftaniAccount = accounts.Where(x => x.ComplteCoding == "1105" + cheque.Contact.Code).SingleOrDefault(); transactions.Add(new Transaction() { //AccDocument = document, AccountId = asnadDreaftaniAccount.ID, Amount = cheque.Amount, Credit = cheque.Amount, Debit = 0, Description = chequesAndStatsVM.description, DocumentId = document.ID, IsCredit = true, IsDebit = false, Date = PersianDateUtils.ToDateTime(chequesAndStatsVM.date), DisplayDate = chequesAndStatsVM.date }); document.Transactions = transactions; return(document); }
private async Task <Document> CreateDocumentPaidChequeReceipt(ChequesAndStatsVM chequesAndStatsVM, int organId) { FinanYearRule finanYearRule = new FinanYearRule(); var finanYears = await finanYearRule.GetAllByOrganIdAsync(organId); var finanYear = finanYears.Where(x => x.Closed == false).SingleOrDefault(); ChequeRule chequeRule = new ChequeRule(); var cheque = await chequeRule.FindAsync(chequesAndStatsVM.chequeId); //var chequeBanks = await _chequeBankService.GetAllByOrganIdAsync(organId); var chequeBanks = this.BusinessRule.UnitOfWork.RepositoryAsync <ChequeBank>().Queryable().Where(x => x.OrganId == organId); var chequeBank = chequeBanks.Where(x => x.ChequeId == cheque.ID).SingleOrDefault(); BankRule bankRule = new BankRule(); var bank = await bankRule.FindAsync(chequeBank.BankId); DocumentRule documentRule = new DocumentRule(); Document document = new Document(); document.Credit = cheque.Amount; document.DateTime = PersianDateUtils.ToDateTime(chequesAndStatsVM.date); document.Debit = cheque.Amount; document.Description = chequesAndStatsVM.description; document.DisplayDate = chequesAndStatsVM.date; document.IsManual = false; document.Status = ZhivarEnums.DocumentStatus.TaeedShode; document.Number = await documentRule.createNumberDocumentAsync(organId); document.Number2 = await documentRule.createNumberDocumentAsync(organId); document.StatusString = "تایید شده"; document.OrganId = organId; document.FinanYear = finanYear; document.FinanYearId = finanYear.ID; document.Type = ZhivarEnums.NoeDoc.Recive; List <Transaction> transactions = new List <Transaction>(); AccountRule accountRule = new AccountRule(); var accounts = await accountRule.GetAllByOrganIdAsync(organId); var accountPay = accounts.Where(x => x.ComplteCoding == "2102" + bank.Code).SingleOrDefault(); transactions.Add(new Transaction() { //AccDocument = document, AccountId = accountPay.ID, Amount = cheque.Amount, Description = chequesAndStatsVM.description, Credit = 0, Debit = cheque.Amount, IsCredit = false, IsDebit = true, DocumentId = document.ID, Date = PersianDateUtils.ToDateTime(chequesAndStatsVM.date), DisplayDate = chequesAndStatsVM.date }); var accountBank = accounts.Where(x => x.ComplteCoding == "1103" + bank.Code).SingleOrDefault(); transactions.Add(new Transaction() { //AccDocument = document, AccountId = accountBank.ID, Amount = cheque.Amount, Credit = cheque.Amount, Debit = 0, Description = chequesAndStatsVM.description, DocumentId = document.ID, IsCredit = true, IsDebit = false, Date = PersianDateUtils.ToDateTime(chequesAndStatsVM.date), DisplayDate = chequesAndStatsVM.date }); document.Transactions = transactions; return(document); }
public async Task <HttpResponseMessage> GetRequiredDataToClosingFinanYear() { var organId = Convert.ToInt32(SecurityManager.CurrentUserContext.OrganizationId); TransactionRule transactionRule = new TransactionRule(); var transactions = await transactionRule.GetAllByOrganIdAsync(organId); AccountRule accountRule = new AccountRule(); var accounts = await accountRule.GetAllByOrganIdAsync(organId); var incomeAccount = accounts.Where(x => x.ComplteCoding == "71" || x.ComplteCoding == "72").Select(x => x.ID).ToList(); var costAccount = accounts.Where(x => x.ComplteCoding == "81" || x.ComplteCoding == "82" || x.ComplteCoding == "83").Select(x => x.ID).ToList(); decimal incomeAmount = 0; decimal costAmount = 0; var accountsMoienQuery = accounts.AsQueryable().Where(x => incomeAccount.Contains(x.ParentId)); var allAccountQuery = accounts.AsQueryable(); List <int> childIds = (from account in accountsMoienQuery select account.ID).ToList(); List <int> childChildIds = (from account in allAccountQuery join accountsMoien in accountsMoienQuery on account.ParentId equals accountsMoien.ID select account.ID).ToList(); var selected = transactions.Where(a => incomeAccount.Contains(a.AccountId) || childIds.Contains(a.AccountId) || childChildIds.Contains(a.AccountId)).ToList(); if (selected.Any()) { var credit = selected.Sum(x => x.Credit); var debit = selected.Sum(x => x.Debit); incomeAmount = credit - debit; } var accountsMoienCostQuery = accounts.AsQueryable().Where(x => costAccount.Contains(x.ParentId)); List <int> childIdsCost = (from account in accountsMoienCostQuery select account.ID).ToList(); List <int> childChildIdsCost = (from account in allAccountQuery join accountsMoien in accountsMoienCostQuery on account.ParentId equals accountsMoien.ID select account.ID).ToList(); var selectedCost = transactions.Where(a => costAccount.Contains(a.AccountId) || childIdsCost.Contains(a.AccountId) || childChildIdsCost.Contains(a.AccountId)).ToList(); if (selectedCost.Any()) { var credit = selectedCost.Sum(x => x.Credit); var debit = selectedCost.Sum(x => x.Debit); costAmount = credit - debit; } FinanYearRule finanYearRule = new FinanYearRule(); var finanQuery = await finanYearRule.GetAllByOrganIdAsync(organId); var finanYear = finanQuery.Where(x => x.FirstYear == true).SingleOrDefault(); ShareholderRule shareholderRule = new ShareholderRule(); var shareHolders = await shareholderRule.GetAllByOrganIdAsync(organId); var startDate = PersianDateUtils.ToDateTime(finanYear.DisplayStartDate); var endDate = PersianDateUtils.ToDateTime(finanYear.DisplayEndDate); double diff2 = (endDate - startDate).TotalDays; var startDateNew = endDate.AddDays(1); var endDateNew = endDate.AddDays(diff2); var displayEndDateNew = PersianDateUtils.ToPersianDateTime(endDateNew); var displayStartDateNew = PersianDateUtils.ToPersianDateTime(startDateNew); var startYear = Convert.ToInt32(displayStartDateNew.Substring(0, 4)); var startMonth = Convert.ToInt32(displayStartDateNew.Substring(5, 2)); var startDay = Convert.ToInt32(displayStartDateNew.Substring(8, 2)); var endYear = Convert.ToInt32(displayEndDateNew.Substring(0, 4)); var endMonth = Convert.ToInt32(displayEndDateNew.Substring(5, 2)); var endDay = Convert.ToInt32(displayEndDateNew.Substring(8, 2)); var isMoreThanYear = false; if (diff2 > 365) { isMoreThanYear = true; } var resualt = new DataToClosingFinanYear(); resualt.closingDate = finanYear.DisplayEndDate; resualt.netIncome = incomeAmount - costAmount; resualt.newFinanYear = new FinanYearVM() { Closed = false, DisplayEndDate = displayEndDateNew, DisplayStartDate = displayStartDateNew, EndDate = endDateNew, EndDateDay = endDay, EndDateMonth = endMonth, EndDateYear = endYear, FirstYear = false, Id = 0, IsMoreThanOneYear = isMoreThanYear, Name = endYear + " سال مال منتهی به", Note = "", StartDate = startDate, StartDateDay = startDay, StartDateMonth = startMonth, StartDateYear = startYear, }; resualt.shareholders = new List <ShareholderVM>(); foreach (var shareHolder in shareHolders) { resualt.shareholders.Add(new ShareholderVM() { Address = shareHolder.Address, City = shareHolder.City, Code = shareHolder.Code, ContactEmail = "", ContactType = shareHolder.ContactType, Credits = shareHolder.Credits, //DetailAccount = new DetailAccount() //{ // Accounts = null, // Balance = 0, // BalanceType = 0, // Code = "000001", // Id = 1, // Name = "مونا ابراهیمی", // Node = new Node() // { // FamilyTree = "اشخاص", // Id = 1, // Name = "اشخاص", // Parent = null, // Parents = ",1,", // SystemAccount = 1 // }, // RelatedAccounts = ",6,22,7,32,33,34,35,", // credit = 0, // debit = 0, //}, Email = shareHolder.Email, Fax = shareHolder.Fax, FirstName = shareHolder.FirstName, Id = (int)shareHolder.ID, IsCustomer = shareHolder.IsCustomer, IsEmployee = shareHolder.IsEmployee, IsShareHolder = shareHolder.IsShareHolder, IsVendor = shareHolder.IsVendor, LastName = shareHolder.LastName, Liability = shareHolder.Liability, Mobile = shareHolder.Mobile, Name = shareHolder.Name, NationalCode = shareHolder.NationalCode, Note = shareHolder.Note, People = shareHolder.People, Phone = shareHolder.Phone, PostalCode = shareHolder.PostalCode, Rating = shareHolder.Rating, RegistrationDate = shareHolder.RegistrationDate, RegistrationNumber = shareHolder.RegistrationNumber, SharePercent = shareHolder.SharePercent, State = shareHolder.State, Website = shareHolder.Website, }); } return(Request.CreateResponse(HttpStatusCode.OK, new { resualtCode = (int)ZhivarEnums.ResultCode.Successful, data = resualt })); }
public async Task <HttpResponseMessage> SaveReceiveAndPay([FromBody] PayRecevie payRecevie) { try { var organId = Convert.ToInt32(SecurityManager.CurrentUserContext.OrganizationId); FinanYearRule finanYearRule = new FinanYearRule(); var finanYerar = await finanYearRule.GetCurrentFinanYear(organId); var documentNumber = 0; int payRecevieNumber = 0; if (payRecevie.ID > 0) { PayRecevie temp = new PayRecevie(); temp = payRecevie; PayRecevieRule payRecevieRule = new PayRecevieRule(); payRecevieRule.Delete(payRecevie.ID); documentNumber = payRecevie.Document.Number; payRecevieNumber = payRecevie.Number; await payRecevieRule.SaveChangesAsync(); } payRecevie.ID = -1; if (payRecevie.Contact != null) { payRecevie.ContactId = payRecevie.Contact.ID; payRecevie.Contact.ObjectState = OMF.Common.Enums.ObjectState.Unchanged; } if (payRecevie.Invoice != null) { payRecevie.InvoiceId = payRecevie.Invoice.ID; payRecevie.Invoice.ObjectState = OMF.Common.Enums.ObjectState.Unchanged; } if (payRecevie.Cost != null) { payRecevie.CostId = payRecevie.Cost.ID; payRecevie.Cost.ObjectState = OMF.Common.Enums.ObjectState.Unchanged; } payRecevie.OrganId = organId; payRecevie.Type = payRecevie.Type; payRecevie.Date = PersianDateUtils.ToDateTime(payRecevie.DisplayDate); payRecevie.DisplayDate = payRecevie.DisplayDate; // payRecevie.Date = DateTime.Now; //payRecevie.DisplayDate = Utilities.PersianDateUtils.ToPersianDate(DateTime.Now); decimal amount = 0; foreach (var item in payRecevie.Items) { amount += item.Amount; if (item.Cash != null) { item.CashId = item.Cash.ID; } else if (item.Bank != null) { item.BankId = item.Bank.ID; } else if (item.Cheque != null && item.Type == ZhivarEnums.DetailPayReceiveType.Cheque) { Cheque cheque = new Cheque() { Amount = item.Amount, BankBranch = item.Cheque.BankBranch, BankName = item.Cheque.BankName, ChequeNumber = item.Cheque.ChequeNumber, Contact = payRecevie.Contact, ContactId = payRecevie.Contact.ID, Date = PersianDateUtils.ToDateTime(item.Cheque.DisplayDate), OrganId = organId, Status = ZhivarEnums.ChequeStatus.Normal, DisplayDate = item.Cheque.DisplayDate, }; if (payRecevie.IsReceive) { cheque.Type = ZhivarEnums.ChequeType.Dareaftani; } else { cheque.Type = ZhivarEnums.ChequeType.Pardakhtani; } if (item.Cheque.ReceiptDate != null) { cheque.ReceiptDate = item.Cheque.ReceiptDate; } item.Cheque = cheque; item.ChequeId = item.Cheque.ID; item.Cheque.ObjectState = OMF.Common.Enums.ObjectState.Added; if (!payRecevie.IsReceive) { var temp = item.ChequeBank.ID; item.ChequeBank = new ChequeBank() { BankId = temp, ChequeId = item.Cheque.ID, OrganId = organId }; item.ChequeBank.ObjectState = OMF.Common.Enums.ObjectState.Added; item.ChequeBankId = item.ChequeBank.ID; } item.ObjectState = OMF.Common.Enums.ObjectState.Added; } if (item.Type == ZhivarEnums.DetailPayReceiveType.KharjCheque) { ChequeRule chequeRule = new ChequeRule(); var cheque = await chequeRule.FindAsync(item.Cheque.ID); cheque.Status = ZhivarEnums.ChequeStatus.Sold; chequeRule.Update(cheque); await chequeRule.SaveChangesAsync(); item.Cheque.ContactId = item.Cheque.Contact.ID; } item.ObjectState = OMF.Common.Enums.ObjectState.Added; } payRecevie.Amount = amount; if (payRecevie.Invoice != null) { //if (payRecevie.Invoice.Contact != null) payRecevie.Invoice.ContactId = payRecevie.Invoice.ContactId; //payRecevie.Invoice.FinanYear = finanYerar; //payRecevie.Invoice.FinanYearId = finanYerar.ID; var invoice = payRecevie.Invoice; invoice.Rest -= amount; invoice.Paid += amount; if (invoice.Rest <= 0) { invoice.Status = ZhivarEnums.NoeInsertFactor.Received; } this.BusinessRule.UnitOfWork.RepositoryAsync <Invoice>().Update(invoice); } if (payRecevie.Cost != null) { //if (payRecevie.Cost.Contact != null) payRecevie.Cost.ContactId = payRecevie.Cost.ContactId; var cost = payRecevie.Cost; cost.Rest -= amount; cost.Paid += amount; if (cost.Rest <= 0) { cost.Status = ZhivarEnums.CostStatus.Paid; } this.BusinessRule.UnitOfWork.RepositoryAsync <Cost>().Update(cost); } payRecevie.Document = await createDocument(payRecevie, organId, documentNumber); if (payRecevieNumber > 0) { payRecevie.Number = payRecevieNumber; } else { var payRecevies = await Rule.GetAllByOrganIdAsync(organId); var lastPayRecevies = payRecevies.OrderByDescending(x => x.ID).FirstOrDefault(); if (lastPayRecevies != null) { payRecevie.Number = lastPayRecevies.Number + 1; } else { payRecevie.Number = 1; } } foreach (var transaction in payRecevie.Document.Transactions) { transaction.ObjectState = OMF.Common.Enums.ObjectState.Added; //if (transaction.AccDocument != null) // transaction.AccDocument.ObjectState = OMF.Common.Enums.ObjectState.Unchanged; if (transaction.Account != null) { transaction.Account.ObjectState = OMF.Common.Enums.ObjectState.Unchanged; } // transaction.Transactions = null; } payRecevie.Document.ObjectState = OMF.Common.Enums.ObjectState.Added; payRecevie.ObjectState = OMF.Common.Enums.ObjectState.Added; this.BusinessRule.UnitOfWork.RepositoryAsync <PayRecevie>().InsertOrUpdateGraph(payRecevie); await this.BusinessRule.UnitOfWork.SaveChangesAsync(); return(Request.CreateResponse(HttpStatusCode.OK, new { resultCode = (int)ZhivarEnums.ResultCode.Successful, data = payRecevie })); } catch (Exception ex) { throw; } }
public async Task <HttpResponseMessage> LoadReceiveAndPay([FromBody] int id) { var organId = Convert.ToInt32(SecurityManager.CurrentUserContext.OrganizationId); FinanYearRule finanYearRule = new FinanYearRule(); var finanYerar = await finanYearRule.GetCurrentFinanYear(organId); var payRecevie = await Rule.FindAsync(id); ReceiveAndPayVM receiveAndPayVM = new ReceiveAndPayVM(); //var account = await accountRule.FindAsync((int)payRecevie.AccountId); //receiveAndPayVM.Account = account; receiveAndPayVM.Amount = payRecevie.Amount; receiveAndPayVM.Contact = payRecevie.Contact; receiveAndPayVM.Description = payRecevie.Description; //receiveAndPayVM.DetailAccount = payRecevie.de receiveAndPayVM.DisplayDate = payRecevie.DisplayDate; receiveAndPayVM.Id = payRecevie.ID; receiveAndPayVM.Invoice = payRecevie.Invoice; receiveAndPayVM.Items = payRecevie.Items.ToList(); receiveAndPayVM.Number = payRecevie.Document.Number; receiveAndPayVM.Number2 = payRecevie.Document.Number2; //receiveAndPayVM.PayItems = payRecevie.pa receiveAndPayVM.Type = payRecevie.Type; if (payRecevie.Contact != null) { payRecevie.ContactId = payRecevie.Contact.ID; } if (payRecevie.Invoice != null) { payRecevie.InvoiceId = payRecevie.Invoice.ID; } decimal amount = 0; foreach (var item in payRecevie.Items) { amount += item.Amount; if (item.CashId > 0) { CashRule cashRule = new CashRule(); item.Cash = await cashRule.FindAsync((int)item.CashId); } else if (item.Bank != null) { item.BankId = item.Bank.ID; } else if (item.Cheque != null) { Cheque cheque = new Cheque() { Amount = item.Amount, BankBranch = item.Cheque.BankBranch, BankName = item.Cheque.BankName, ChequeNumber = item.Cheque.ChequeNumber, Contact = payRecevie.Contact, ContactId = payRecevie.Contact.ID, Date = DateTime.Now,// item.Cheque.Date, OrganId = organId, Status = ZhivarEnums.ChequeStatus.Normal, DisplayDate = item.Cheque.DisplayDate, Type = ZhivarEnums.ChequeType.Pardakhtani, ReceiptDate = DateTime.Now, }; item.Cheque = cheque; item.ChequeId = item.Cheque.ID; } } payRecevie.Amount = amount; if (payRecevie.Contact != null) { var account1104 = await CalcAccountByCodeTafziliAsync(organId, "1104" + payRecevie.Contact.Code); var account1105 = await CalcAccountByCodeTafziliAsync(organId, "1105" + payRecevie.Contact.Code); var account2101 = await CalcAccountByCodeTafziliAsync(organId, "2101" + payRecevie.Contact.Code); payRecevie.Contact.Balance = account1104.sumTotal + account1105.sumTotal + account2101.sumTotal - amount; payRecevie.Contact.Credits = account1104.sumCredit + account1105.sumCredit + account2101.sumCredit - amount; payRecevie.Contact.Liability = account1104.sumDebit + account1105.sumDebit + account2101.sumDebit; } //if (payRecevie.Invoice != null) //{ // if (payRecevie.Invoice.Contact != null) // payRecevie.Invoice.ContactId = payRecevie.Invoice.Contact.ID; // //payRecevie.Invoice.FinanYear = finanYerar; // //payRecevie.Invoice.FinanYearId = finanYerar.ID; // var invoice = payRecevie.Invoice; // invoice.Rest -= amount; // invoice.Paid += amount; // invoiceRule.Update(invoice); //} return(Request.CreateResponse(HttpStatusCode.OK, new { resultCode = (int)ZhivarEnums.ResultCode.Successful, data = payRecevie })); }
public async Task <List <string> > SaveContract(ContractVM contractVM) { try { var organId = Convert.ToInt32(SecurityManager.CurrentUserContext.OrganizationId); FinanYearRule finanYearRule = new FinanYearRule(); var finanYears = await finanYearRule.GetAllByOrganIdAsync(organId); var finanYear = finanYears.Where(x => x.Closed == false && x.OrganId == organId); contractVM.Contract_Sazes = contractVM.Contract_Sazes.Where(x => x.Saze != null).ToList(); foreach (var contract_Saze in contractVM.Contract_Sazes) { if (contract_Saze.Saze != null) { contract_Saze.SazeId = contract_Saze.Saze.ID; } if (contract_Saze.NoeEjare != null) { contract_Saze.NoeEjareId = contract_Saze.NoeEjare.ID; } if (contract_Saze.DisplayTarikhShorou != null) { contract_Saze.TarikhShorou = PersianDateUtils.ToDateTime(contract_Saze.DisplayTarikhShorou); if (contract_Saze.NoeEjareId == 1) { contract_Saze.TarikhPayan = contract_Saze.TarikhShorou.AddDays((double)contract_Saze.Quantity); } else if (contract_Saze.NoeEjareId == 2) { PersianCalendar pc = new PersianCalendar(); contract_Saze.TarikhPayan = pc.AddMonths(contract_Saze.TarikhShorou, (int)contract_Saze.Quantity); contract_Saze.DisplayTarikhPayan = PersianDateUtils.ToPersianDate(contract_Saze.TarikhPayan); } } } if (contractVM.Contact != null) { contractVM.ContactId = contractVM.Contact.ID; } DomainClasses.Contract.Contract contract = new DomainClasses.Contract.Contract(); Mapper.Map(contractVM, contract); contract.OrganId = organId; contract.DateTime = PersianDateUtils.ToDateTime(contract.DisplayDate); ContractValidate validator = new ContractValidate(); FluentValidation.Results.ValidationResult results = validator.Validate(contract); List <string> failurs = new List <string>();; if (!results.IsValid) { foreach (var error in results.Errors) { failurs.Add(error.ErrorMessage); } return(failurs); } // contract = savePaymentForContract(contract, contractVM); if (SecurityManager.CurrentUserContext.Roles.Any(x => x.RoleCode == "Manager") && contractVM.ContractType == ContractType.RentFrom) { contract.Status = Status.ConfirmationContract; } else if (SecurityManager.CurrentUserContext.Roles.Any(x => x.RoleCode == "Manager") && contractVM.ContractType == ContractType.PreContract) { contract.Status = Status.ConfirmationPreContract; contract.ContractType = ContractType.RentTo; // contract.Contract_Sazes = uow.Repository<Contract_Saze>().Queryable().Where(x => x.ContractID == contract.ID).ToList(); foreach (var contract_Saze in contract.Contract_Sazes ?? new List <Contract_Saze>()) { contract_Saze.Status = Status.ConfirmationPreContract; //contract_Saze.ObjectState = OMF.Common.Enums.ObjectState.Modified; } } else if (contractVM.ContractType == ContractType.PreContract && contractVM.Status == Status.SendPreContract) { //nothing } else { contract.Status = Status.Temporary; } if (contract.ID > 0) { foreach (var contractSaze in contract.Contract_Sazes) { contractSaze.TarikhShorou = PersianDateUtils.ToDateTime(contractSaze.DisplayTarikhShorou); contractSaze.ContractID = contract.ID; if (contractSaze.ID > 0) { contractSaze.ContractID = contract.ID; contractSaze.ObjectState = OMF.Common.Enums.ObjectState.Modified; } else { contractSaze.ContractID = contract.ID; contractSaze.ObjectState = OMF.Common.Enums.ObjectState.Added; } foreach (var contarct_Saze_Bazareab in contractSaze.Contarct_Saze_Bazareabs) { contractSaze.HasBazareab = true; if (contarct_Saze_Bazareab.ID > 0) { contarct_Saze_Bazareab.ContarctSazeID = contractSaze.ID; contarct_Saze_Bazareab.ObjectState = ObjectState.Modified; } else { contarct_Saze_Bazareab.ContarctSazeID = contractSaze.ID; contarct_Saze_Bazareab.ObjectState = ObjectState.Added; } } foreach (var contract_Saze_Tarah in contractSaze.Contract_Saze_Tarahs) { contractSaze.HasTarah = true; if (contract_Saze_Tarah.ID > 0) { contract_Saze_Tarah.ContarctSazeID = contractSaze.ID; contract_Saze_Tarah.ObjectState = ObjectState.Modified; } else { contract_Saze_Tarah.ContarctSazeID = contractSaze.ID; contract_Saze_Tarah.ObjectState = ObjectState.Added; } } foreach (var contract_Saze_Chapkhane in contractSaze.Contract_Saze_Chapkhanes) { contractSaze.HasChap = true; if (contract_Saze_Chapkhane.ID > 0) { contract_Saze_Chapkhane.ContarctSazeID = contractSaze.ID; contract_Saze_Chapkhane.ObjectState = ObjectState.Modified; } else { contract_Saze_Chapkhane.ContarctSazeID = contractSaze.ID; contract_Saze_Chapkhane.ObjectState = ObjectState.Added; } } foreach (var contract_Saze_Nasab in contractSaze.Contract_Saze_Nasabs) { contractSaze.HasNasab = true; if (contract_Saze_Nasab.ID > 0) { contract_Saze_Nasab.ContarctSazeID = contractSaze.ID; contract_Saze_Nasab.ObjectState = ObjectState.Modified; } else { contract_Saze_Nasab.ContarctSazeID = contractSaze.ID; contract_Saze_Nasab.ObjectState = ObjectState.Added; } } } foreach (var contract_PayRecevie in contract.Contract_PayRecevies) { if (contract_PayRecevie.ID > 0) { contract_PayRecevie.ContractId = contract.ID; contract_PayRecevie.ObjectState = OMF.Common.Enums.ObjectState.Modified; } else { contract_PayRecevie.ContractId = contract.ID; contract_PayRecevie.IsReceive = true; contract_PayRecevie.OrganId = organId; contract_PayRecevie.Status = Status.Temporary; contract_PayRecevie.Type = PayRecevieType.Sir; contract_PayRecevie.Date = DateTime.Now; contract_PayRecevie.ContactId = contract.ID; contract_PayRecevie.ObjectState = OMF.Common.Enums.ObjectState.Added; } foreach (var contract_DetailPayRecevie in contract_PayRecevie.Contract_DetailPayRecevies) { if (contract_DetailPayRecevie.ID > 0) { contract_DetailPayRecevie.Contract_PayRecevieId = contract_PayRecevie.ID; contract_DetailPayRecevie.ObjectState = ObjectState.Modified; } else { contract_DetailPayRecevie.Contract_PayRecevieId = contract_PayRecevie.ID; contract_DetailPayRecevie.ObjectState = ObjectState.Added; } } } contract.ObjectState = OMF.Common.Enums.ObjectState.Modified; } else { foreach (var contractSaze in contract.Contract_Sazes) { contractSaze.ContractID = contract.ID; contractSaze.TarikhShorou = PersianDateUtils.ToDateTime(contractSaze.DisplayTarikhShorou); if (contractSaze.ID > 0) { contractSaze.ContractID = contract.ID; contractSaze.ObjectState = OMF.Common.Enums.ObjectState.Modified; } else { contractSaze.ContractID = contract.ID; contractSaze.ObjectState = OMF.Common.Enums.ObjectState.Added; } foreach (var contarct_Saze_Bazareab in contractSaze.Contarct_Saze_Bazareabs) { contractSaze.HasBazareab = true; if (contarct_Saze_Bazareab.NoeMozdBazryab == NoeMozd.Month) { contarct_Saze_Bazareab.Hazine = 0; } if (contarct_Saze_Bazareab.ID > 0) { contarct_Saze_Bazareab.ContarctSazeID = contractSaze.ID; contarct_Saze_Bazareab.ObjectState = ObjectState.Modified; } else { contarct_Saze_Bazareab.ContarctSazeID = contractSaze.ID; contarct_Saze_Bazareab.ObjectState = ObjectState.Added; } } foreach (var contract_Saze_Tarah in contractSaze.Contract_Saze_Tarahs) { contractSaze.HasTarah = true; if (contract_Saze_Tarah.NoeMozdTarah == NoeMozd.Month) { contract_Saze_Tarah.Hazine = 0; } if (contract_Saze_Tarah.ID > 0) { contract_Saze_Tarah.ContarctSazeID = contractSaze.ID; contract_Saze_Tarah.ObjectState = ObjectState.Modified; } else { contract_Saze_Tarah.ContarctSazeID = contractSaze.ID; contract_Saze_Tarah.ObjectState = ObjectState.Added; } } foreach (var contract_Saze_Chapkhane in contractSaze.Contract_Saze_Chapkhanes) { contractSaze.HasChap = true; if (contract_Saze_Chapkhane.ID > 0) { contract_Saze_Chapkhane.ContarctSazeID = contractSaze.ID; contract_Saze_Chapkhane.ObjectState = ObjectState.Modified; } else { contract_Saze_Chapkhane.ContarctSazeID = contractSaze.ID; contract_Saze_Chapkhane.ObjectState = ObjectState.Added; } } foreach (var contract_Saze_Nasab in contractSaze.Contract_Saze_Nasabs) { contractSaze.HasNasab = true; if (contract_Saze_Nasab.NoeMozdNasab == NoeMozd.Month) { contract_Saze_Nasab.Hazine = 0; } if (contract_Saze_Nasab.ID > 0) { contract_Saze_Nasab.ContarctSazeID = contractSaze.ID; contract_Saze_Nasab.ObjectState = ObjectState.Modified; } else { contract_Saze_Nasab.ContarctSazeID = contractSaze.ID; contract_Saze_Nasab.ObjectState = ObjectState.Added; } } } if (contract.Contract_PayRecevies != null && contract.Contract_PayRecevies.Count() > 0 && contract.Contract_PayRecevies[0].Contract_DetailPayRecevies != null && contract.Contract_PayRecevies[0].Contract_DetailPayRecevies.Count() > 0) { foreach (var contract_PayRecevie in contract.Contract_PayRecevies) { contract_PayRecevie.ContractId = contract.ID; contract_PayRecevie.Date = DateTime.Now; contract_PayRecevie.IsReceive = true; if (contract_PayRecevie.ID > 0) { contract_PayRecevie.ContractId = contract.ID; contract_PayRecevie.ObjectState = OMF.Common.Enums.ObjectState.Modified; } else { contract_PayRecevie.ContractId = contract.ID; contract_PayRecevie.ObjectState = OMF.Common.Enums.ObjectState.Added; } foreach (var contract_DetailPayRecevie in contract_PayRecevie.Contract_DetailPayRecevies) { if (contract_DetailPayRecevie.ID > 0) { contract_DetailPayRecevie.Contract_PayRecevieId = contract_PayRecevie.ID; contract_DetailPayRecevie.ObjectState = ObjectState.Modified; } else { contract_DetailPayRecevie.Contract_PayRecevieId = contract_PayRecevie.ID; contract_DetailPayRecevie.ObjectState = ObjectState.Added; } } } } else { contract.Contract_PayRecevies = null; } contract.ObjectState = OMF.Common.Enums.ObjectState.Added; } this.UnitOfWork.RepositoryAsync <DomainClasses.Contract.Contract>().InsertOrUpdateGraph(contract); // ContactRule contactRule = new ContactRule(); // await contactRule.UpdateContact(invoice.InvoiceType, invoice.ContactId); await this.UnitOfWork.SaveChangesAsync(); if (contractVM.ID <= 0) { if ((SecurityManager.CurrentUserContext.Roles.Any(x => x.RoleCode == "Manager") && contractVM.ContractType == ContractType.PreContract) || (SecurityManager.CurrentUserContext.Roles.Any(x => x.RoleCode == "Manager") && contractVM.Status == Status.ConfirmationContract)) { InvoiceRule invoiceRule = new InvoiceRule(); var invoice = invoiceRule.ConvertContractToInvoice(contract.ID, NoeFactor.RentTo); var document = invoiceRule.RegisterDocument(invoice, invoice.OrganId); DocumentRule documentRule = new DocumentRule(); await documentRule.InsertAsync(document, invoice.OrganId); await documentRule.SaveChangesAsync(); invoice.DocumentID = document.ID; invoiceRule.Update(invoice); await invoiceRule.SaveChangesAsync(); } else if (SecurityManager.CurrentUserContext.Roles.Any(x => x.RoleCode == "Manager") && contractVM.ContractType == ContractType.RentTo) { InvoiceRule invoiceRule = new InvoiceRule(); invoiceRule.ConvertContractToInvoice(contract.ID, NoeFactor.RentFrom); } } return(failurs); } catch (Exception ex) { var p = ex; throw; } }
public async Task <HttpResponseMessage> SaveCost([FromBody] CostVM costVM) { try { bool ShouldSend = false; var organId = Convert.ToInt32(SecurityManager.CurrentUserContext.OrganizationId); FinanYearRule finanYearRule = new FinanYearRule(); var finanYears = await finanYearRule.GetAllByOrganIdAsync(organId); var finanYear = finanYears.Where(x => x.Closed == false && x.OrganId == organId); costVM.CostItems = costVM.CostItems.Where(x => x.Item != null && x.Sum > 0).ToList(); foreach (var costItem in costVM.CostItems) { if (costItem.Item != null) { costItem.ItemId = costItem.Item.ID; } } Cost cost = new Cost(); Mapper.Map(costVM, cost); cost.OrganId = organId; if (costVM.Contact != null) { cost.ContactId = costVM.Contact.ID;//.DetailAccount.Node.Id; } cost.CostItems = cost.CostItems.Where(x => x.ItemId != 0 && x.Sum > 0).ToList(); CostValidate validator = new CostValidate(); FluentValidation.Results.ValidationResult results = validator.Validate(cost); string failurs = ""; if (!results.IsValid) { foreach (var error in results.Errors) { failurs += "<br/>" + error.ErrorMessage; } return(Request.CreateResponse(HttpStatusCode.OK, new { resultCode = ZhivarEnums.ResultCode.ValidationError, data = failurs })); } if (SecurityManager.CurrentUserContext.Roles.Any(x => x.RoleCode == "Manager")) { cost.Status = ZhivarEnums.CostStatus.WaitingToPay; } else if (costVM.Status == ZhivarEnums.CostStatus.WaitingToPay) { ShouldSend = true; } else { cost.Status = ZhivarEnums.CostStatus.Temporary; } //if (cost.Status == ZhivarEnums.CostStatus.WaitingToPay) //{ // var document = await Rule.RegisterDocument(costVM, organId); // DocumentRule documentRule = new DocumentRule(); // await documentRule.InsertAsync(document, organId); //} if (cost.ID > 0) { foreach (var costItem in cost.CostItems) { costItem.ObjectState = OMF.Common.Enums.ObjectState.Modified; costItem.CostId = cost.ID; } cost.ObjectState = OMF.Common.Enums.ObjectState.Modified; } else { foreach (var costItem in cost.CostItems) { costItem.ObjectState = OMF.Common.Enums.ObjectState.Added; } cost.ObjectState = OMF.Common.Enums.ObjectState.Added; } Rule.InsertOrUpdateGraph(cost); if (costVM.ID <= 0) { if (SecurityManager.CurrentUserContext.Roles.Any(x => x.RoleCode == "Manager")) { var document = await Rule.RegisterDocument(costVM, organId); DocumentRule documentRule = new DocumentRule(); await documentRule.InsertAsync(document, organId); await documentRule.SaveChangesAsync(); cost.DocumentId = document.ID; } } await this.BusinessRule.UnitOfWork.SaveChangesAsync(); if (ShouldSend) { await StartWorkFlow(new WorkFlowBusiClass() { Code = "Cost", ID = cost.ID, OrganizationID = 22, InstanceTitle = cost.Explain }, true); } return(Request.CreateResponse(HttpStatusCode.OK, new { resultCode = ZhivarEnums.ResultCode.Successful, data = cost })); } catch (Exception ex) { throw; } }
public async Task <HttpResponseMessage> SaveInvoice([FromBody] InvoiceVM invoiceVM) { try { var organId = Convert.ToInt32(SecurityManager.CurrentUserContext.OrganizationId); FinanYearRule finanYearRule = new FinanYearRule(); var finanYears = await finanYearRule.GetAllByOrganIdAsync(organId); var finanYear = finanYears.Where(x => x.Closed == false && x.OrganId == organId); invoiceVM.InvoiceItems = invoiceVM.InvoiceItems.Where(x => x.Item != null).ToList(); foreach (var invoiceItem in invoiceVM.InvoiceItems) { if (invoiceItem.Item != null) { invoiceItem.ItemId = invoiceItem.Item.ID; } } if (invoiceVM.Contact != null) { invoiceVM.ContactId = invoiceVM.Contact.ID; } Invoice invoice = new Invoice(); Mapper.Map(invoiceVM, invoice); invoice.OrganId = organId; //if (invoice.Contact != null) //{ // invoice.ContactId = invoice.Contact.ID; //} // invoice.InvoiceItems = invoice.InvoiceItems.Where(x => x.ItemId != 0).ToList(); InvoiceValidate validator = new InvoiceValidate(); FluentValidation.Results.ValidationResult results = validator.Validate(invoice); string failurs = ""; if (!results.IsValid) { foreach (var error in results.Errors) { failurs += "<br/>" + error.ErrorMessage; } return(Request.CreateResponse(HttpStatusCode.OK, new { resultCode = ZhivarEnums.ResultCode.ValidationError, data = failurs })); } if (invoice.ID > 0) { foreach (var invoiceItem in invoice.InvoiceItems) { invoiceItem.InvoiceId = invoice.ID; if (invoiceItem.ID > 0) { invoiceItem.ObjectState = OMF.Common.Enums.ObjectState.Modified; } else { invoiceItem.ObjectState = OMF.Common.Enums.ObjectState.Added; } //if (invoiceItem.Item != null) // invoiceItem.Item = null;//.ObjectState = OMF.Common.Enums.ObjectState.Unchanged; } invoice.ObjectState = OMF.Common.Enums.ObjectState.Modified; } else { foreach (var invoiceItem in invoice.InvoiceItems) { invoiceItem.ObjectState = OMF.Common.Enums.ObjectState.Added; //if (invoiceItem.Item != null) // invoiceItem.Item = null;//.ObjectState = OMF.Common.Enums.ObjectState.Unchanged; } invoice.ObjectState = OMF.Common.Enums.ObjectState.Added; } this.BusinessRule.UnitOfWork.RepositoryAsync <Invoice>().InsertOrUpdateGraph(invoice); ContactRule contactRule = new ContactRule(); await contactRule.UpdateContact(invoice.InvoiceType, invoice.ContactId); //await this.BusinessRule.UnitOfWork.SaveChangesAsync(); if (invoice.Status == ZhivarEnums.NoeInsertFactor.WaitingToReceive) { var document = await Rule.RegisterDocumentAsync(invoiceVM, organId); DocumentRule documentRule = new DocumentRule(); await documentRule.InsertAsync(document, organId); await documentRule.SaveChangesAsync(); invoice.DocumentID = document.ID; this.BusinessRule.UnitOfWork.RepositoryAsync <Invoice>().Update(invoice); //await this.BusinessRule.UnitOfWork.SaveChangesAsync(); } await this.BusinessRule.UnitOfWork.SaveChangesAsync(); // await RegisterDocument(invoice, organId); return(Request.CreateResponse(HttpStatusCode.OK, new { resultCode = ZhivarEnums.ResultCode.Successful, data = invoice })); } catch (Exception ex) { throw; } }