Beispiel #1
0
        public void SaveCustomerAllocation(CustomerAllocation allocation)
        {
            //Revenue recognition. Debit the customer advances (liability) account and credit the revenue account.
            //In case of allocation, credit the accounts receivable since sales account is already credited from invoice.
            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)
            {
                Account accountToDebit = invoice.Customer.CustomerAdvancesAccount;
                var     debit          = _financialService.CreateGeneralLedgerLine(Core.Domain.DrOrCrSide.Dr, accountToDebit.Id, allocation.Amount);
                glHeader.GeneralLedgerLines.Add(debit);
            }

            Account accountToCredit = invoice.Customer.AccountsReceivableAccount;
            var     credit          = _financialService.CreateGeneralLedgerLine(Core.Domain.DrOrCrSide.Cr, accountToCredit.Id, allocation.Amount);

            glHeader.GeneralLedgerLines.Add(credit);

            if (_financialService.ValidateGeneralLedgerEntry(glHeader))
            {
                invoice.GeneralLedgerHeader = glHeader;
                invoice.CustomerAllocations.Add(allocation);
                _salesInvoiceRepo.Update(invoice);
            }
        }
Beispiel #2
0
        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 }));
        }