public IActionResult SaveAllocation([FromBody] dynamic allocationDto) { string[] errors = null; try { if (!ModelState.IsValid) { errors = new string[ModelState.ErrorCount]; foreach (var val in ModelState.Values) { for (int i = 0; i < ModelState.ErrorCount; i++) { errors[i] = val.Errors[i].ErrorMessage; } } return(new BadRequestObjectResult(errors)); } foreach (var line in allocationDto.AllocationLines) { decimal?amount = (decimal?)line.AmountToAllocate; if (amount.HasValue) { var allocation = new Core.Domain.Sales.CustomerAllocation(); allocation.CustomerId = allocationDto.CustomerId; allocation.Date = allocationDto.Date; allocation.SalesInvoiceHeaderId = line.InvoiceId; allocation.SalesReceiptHeaderId = allocationDto.ReceiptId; allocation.Amount = amount.GetValueOrDefault(); _salesService.SaveCustomerAllocation(allocation); } } return(new ObjectResult(Ok())); } catch (Exception ex) { errors = new string[1] { ex.InnerException != null ? ex.InnerException.Message : ex.Message }; return(new BadRequestObjectResult(errors)); } }
public ActionResult SaveAllocation(Models.ViewModels.Sales.Allocate model) { var receipt = _salesService.GetSalesReceiptById(model.ReceiptId); var customer = _salesService.GetCustomerById(receipt.CustomerId); var invoice = _salesService.GetSalesInvoiceById(model.InvoiceId); var allocations = _salesService.GetCustomerReceiptsForAllocation(customer.Id); model.InvoiceId = model.InvoiceId; model.TotalAmountAvailableToAllocate = allocations.Sum(a => a.AvailableAmountToAllocate); model.LeftToAllocateFromReceipt = receipt.AvailableAmountToAllocate; if (invoice == null) { return View(model); } else { var invoiceTotalAmount = invoice.ComputeTotalAmount(); if (model.AmountToAllocate > invoiceTotalAmount || model.AmountToAllocate > receipt.AvailableAmountToAllocate || invoice.Status == Core.Domain.SalesInvoiceStatus.Closed) { return View(model); } var allocation = new CustomerAllocation() { CustomerId = customer.Id, SalesReceiptHeaderId = receipt.Id, SalesInvoiceHeaderId = invoice.Id, Amount = model.AmountToAllocate, Date = DateTime.Now }; _salesService.SaveCustomerAllocation(allocation); } return RedirectToAction("CustomerDetail", new { id = customer.Id }); }
public void SaveCustomerAllocation(CustomerAllocation allocation) { //Revenue recognition. Debit the customer advances (liability) account and credit the revenue account. var invoice = _salesInvoiceRepo.GetById(allocation.SalesInvoiceHeaderId); var receipt = _salesReceiptRepo.GetById(allocation.SalesReceiptHeaderId); var glHeader = _financialService.CreateGeneralLedgerHeader(Core.Domain.DocumentTypes.CustomerAllocation, allocation.Date, string.Empty); foreach (var line in receipt.SalesReceiptLines) { var debit = _financialService.CreateGeneralLedgerLine(Core.Domain.TransactionTypes.Dr, line.AccountToCreditId.Value, allocation.Amount); glHeader.GeneralLedgerLines.Add(debit); } Account accountToCredit = invoice.Customer.AccountsReceivableAccount; var credit = _financialService.CreateGeneralLedgerLine(Core.Domain.TransactionTypes.Cr, accountToCredit.Id, allocation.Amount); glHeader.GeneralLedgerLines.Add(credit); if (_financialService.ValidateGeneralLedgerEntry(glHeader)) { invoice.GeneralLedgerHeader = glHeader; invoice.CustomerAllocations.Add(allocation); _salesInvoiceRepo.Update(invoice); } }