示例#1
0
        public ActionResult SaveInvoice(Models.ViewModels.Sales.SalesHeaderViewModel model)
        {
            SalesInvoiceHeader invoice = null;
            if (model.Id == 0)
            {
                invoice = new SalesInvoiceHeader();
                invoice.CreatedBy = User.Identity.Name;
                invoice.CreatedOn = DateTime.Now;
            }
            else
            {
                invoice = _salesService.GetSalesInvoiceById(model.Id);
            }

            invoice.ModifiedBy = User.Identity.Name;
            invoice.ModifiedOn = DateTime.Now;
            invoice.CustomerId = model.CustomerId.Value;
            invoice.Date = model.Date;
            invoice.ShippingHandlingCharge = model.ShippingHandlingCharges;
            
            foreach (var line in model.SalesLine.SalesLineItems)
            {
                SalesInvoiceLine lineItem = null;
                var item = _inventoryService.GetItemByNo(line.ItemNo);
                if (!line.Id.HasValue)
                {
                    lineItem = new SalesInvoiceLine();
                    lineItem.CreatedBy = User.Identity.Name;
                    lineItem.CreatedOn = DateTime.Now;
                    invoice.SalesInvoiceLines.Add(lineItem);
                }
                else
                {
                    lineItem = invoice.SalesInvoiceLines.Where(i => i.Id == line.Id).FirstOrDefault();
                }

                lineItem.ModifiedBy = User.Identity.Name;
                lineItem.ModifiedOn = DateTime.Now;
                lineItem.ItemId = line.ItemId;
                lineItem.MeasurementId = item.SellMeasurementId.Value;
                lineItem.Quantity = line.Quantity;
                lineItem.Discount = line.Discount;
                lineItem.Amount = line.Price;
            }

            if (model.Id == 0)
            {
                _salesService.AddSalesInvoice(invoice, null);
            }
            else
            {
                _salesService.UpdateSalesInvoice(invoice);
            }

            return RedirectToAction("SalesInvoices");
        }
示例#2
0
        public ActionResult SaveSalesInvoice(Models.ViewModels.Sales.AddSalesInvoice model)
        {
            if (model.AddSalesInvoiceLines.Sum(i => i.Amount) == 0 || model.AddSalesInvoiceLines.Count < 1)
            {
                model.Customers = Models.ModelViewHelper.Customers();
                model.Items = Models.ModelViewHelper.Items();
                model.Measurements = Models.ModelViewHelper.Measurements();
                ModelState.AddModelError("Amount", "No invoice line");
                return View(model);
            }
            var invoiceHeader = new SalesInvoiceHeader();
            var invoiceLines = new List<SalesInvoiceLine>();
            foreach (var item in model.AddSalesInvoiceLines)
            {
                var Item = _inventoryService.GetItemById(item.ItemId);
                var invoiceDetail = new SalesInvoiceLine();
                invoiceDetail.TaxId = Item.ItemTaxGroupId;
                invoiceDetail.ItemId = item.ItemId;
                invoiceDetail.MeasurementId = item.MeasurementId;
                invoiceDetail.Quantity = item.Quantity;
                invoiceDetail.Discount = item.Discount;
                invoiceDetail.Amount = Convert.ToDecimal(item.Quantity * Item.Price);
                invoiceDetail.CreatedBy = User.Identity.Name;
                invoiceDetail.CreatedOn = DateTime.Now;
                invoiceDetail.ModifiedBy = User.Identity.Name;
                invoiceDetail.ModifiedOn = DateTime.Now;
                invoiceLines.Add(invoiceDetail);
            }
            invoiceHeader.SalesInvoiceLines = invoiceLines;
            invoiceHeader.CustomerId = model.CustomerId;
            invoiceHeader.Date = model.Date;
            invoiceHeader.ShippingHandlingCharge = 4;// model.ShippingHandlingCharge;
            invoiceHeader.CreatedBy = User.Identity.Name;
            invoiceHeader.CreatedOn = DateTime.Now;
            invoiceHeader.ModifiedBy = User.Identity.Name;
            invoiceHeader.ModifiedOn = DateTime.Now;

            _salesService.AddSalesInvoice(invoiceHeader, model.SalesOrderId);
            return RedirectToAction("SalesInvoices");
        }
        public ActionResult GenerateBulkInvoice(Models.ViewModels.Administration.CreateBulkInvoice model)
        {
            var day = 15;
            var month = DateTime.Now.Month == 12 ? 1 : DateTime.Now.Month + 1;
            var year = DateTime.Now.Month == 12 ? DateTime.Now.Year + 1 : DateTime.Now.Year;
            DateTime invoiceDate = new DateTime(year, month, day);

            var query = from c in _salesService.GetCustomers()
                        select c;
            var customers = query.ToList();

            var itemAssociationDues = (from i in _inventoryService.GetAllItems().ToList()
                                       where (i != null
                                       && i.SellDescription.ToLower().Contains("dues"))
                                       select i).FirstOrDefault();

            var invoices = new List<SalesInvoiceHeader>();

            foreach (var customer in customers)
            {
                var current = (from si in _salesService.GetSalesInvoices()
                               where si.CustomerId == customer.Id
                               && si.Date.Year == invoiceDate.Year
                               && si.Date.Month == invoiceDate.Month
                               && si.Date.Day == invoiceDate.Day
                               select si).FirstOrDefault();
                if (current != null)
                    return RedirectToAction("BulkInvoice");

                var invoiceLine = new SalesInvoiceLine();
                invoiceLine.ItemId = itemAssociationDues.Id;
                invoiceLine.Quantity = 1;
                invoiceLine.Amount = Convert.ToDecimal(itemAssociationDues.Price * invoiceLine.Quantity);
                invoiceLine.MeasurementId = itemAssociationDues.SmallestMeasurementId.Value;
                invoiceLine.CreatedBy = User.Identity.Name;
                invoiceLine.CreatedOn = DateTime.Now;
                invoiceLine.ModifiedBy = User.Identity.Name;
                invoiceLine.ModifiedOn = DateTime.Now;

                var invoice = new SalesInvoiceHeader();
                invoice.Date = invoiceDate;
                invoice.CustomerId = customer.Id;
                invoice.CreatedBy = User.Identity.Name;
                invoice.CreatedOn = DateTime.Now;
                invoice.ModifiedBy = User.Identity.Name;
                invoice.ModifiedOn = DateTime.Now;
                invoice.SalesInvoiceLines.Add(invoiceLine);

                invoices.Add(invoice);
            }

            foreach (var invoice in invoices)
                _salesService.AddSalesInvoice(invoice, null);

            return RedirectToAction("SalesInvoices", "Sales");
        }
示例#4
0
 public void UpdateSalesInvoice(SalesInvoiceHeader salesInvoice)
 {
     _salesInvoiceRepo.Update(salesInvoice);
 }
示例#5
0
        public void AddSalesInvoice(SalesInvoiceHeader salesInvoice, int? salesDeliveryId)
        {   
            decimal taxAmount = 0, totalAmount = 0, totalDiscount = 0;

            var taxes = new List<KeyValuePair<int, decimal>>();
            var sales = new List<KeyValuePair<int, decimal>>();
            
            foreach (var lineItem in salesInvoice.SalesInvoiceLines)
            {
                var item = _itemRepo.GetById(lineItem.ItemId);

                var lineDiscountAmount = (lineItem.Discount / 100) * lineItem.Amount;
                totalDiscount += lineDiscountAmount;

                var lineAmount = lineItem.Amount - lineDiscountAmount;
                
                totalAmount += lineAmount;
                
                var lineTaxes = _financialService.ComputeOutputTax(item.Id, lineItem.Quantity, lineItem.Amount, lineItem.Discount);
                
                foreach (var t in lineTaxes)
                    taxes.Add(t);
                
                sales.Add(new KeyValuePair<int, decimal>(item.SalesAccountId.Value, lineAmount));

                if (item.ItemCategory.ItemType == ItemTypes.Purchased)
                {
                    lineItem.InventoryControlJournal = _inventoryService.CreateInventoryControlJournal(lineItem.ItemId,
                        lineItem.MeasurementId,
                        DocumentTypes.SalesInvoice,
                        null,
                        lineItem.Quantity,
                        lineItem.Quantity * item.Cost,
                        lineItem.Quantity * item.Price);
                }
            }

            var glHeader = _financialService.CreateGeneralLedgerHeader(DocumentTypes.SalesInvoice, salesInvoice.Date, string.Empty);
            var customer = _customerRepo.GetById(salesInvoice.CustomerId);
            totalAmount += salesInvoice.ShippingHandlingCharge;
            var debitCustomerAR = _financialService.CreateGeneralLedgerLine(TransactionTypes.Dr, customer.AccountsReceivableAccount.Id, Math.Round(totalAmount, 2, MidpointRounding.ToEven));
            glHeader.GeneralLedgerLines.Add(debitCustomerAR);

            var groupedSalesAccount = from s in sales
                                      group s by s.Key into grouped
                                      select new
                                      {
                                          Key = grouped.Key,
                                          Value = grouped.Sum(s => s.Value)
                                      };

            foreach (var salesAccount in groupedSalesAccount)
            {
                var salesAmount = (salesAccount.Value + totalDiscount) - taxAmount;
                var creditSalesAccount = _financialService.CreateGeneralLedgerLine(TransactionTypes.Cr, salesAccount.Key, Math.Round(salesAmount, 2, MidpointRounding.ToEven));
                glHeader.GeneralLedgerLines.Add(creditSalesAccount);
            }

            if (taxAmount > 0)
            {
                var groupedTaxes = from t in taxes
                                   group t by t.Key into grouped
                                   select new
                                   {
                                       Key = grouped.Key,
                                       Value = grouped.Sum(t => t.Value)
                                   };

                foreach (var tax in groupedTaxes)
                {
                    var tx = _financialService.GetTaxes().Where(t => t.Id == tax.Key).FirstOrDefault();
                    var creditSalesTaxAccount = _financialService.CreateGeneralLedgerLine(TransactionTypes.Cr, tx.SalesAccountId.Value, Math.Round(tax.Value, 2, MidpointRounding.ToEven));
                    glHeader.GeneralLedgerLines.Add(creditSalesTaxAccount);
                }
            }

            if (totalDiscount > 0)
            {
                var salesDiscountAccount = base.GetGeneralLedgerSetting().SalesDiscountAccount;
                var creditSalesDiscountAccount = _financialService.CreateGeneralLedgerLine(TransactionTypes.Dr, salesDiscountAccount.Id, Math.Round(totalDiscount, 2, MidpointRounding.ToEven));
                glHeader.GeneralLedgerLines.Add(creditSalesDiscountAccount);
            }

            if (salesInvoice.ShippingHandlingCharge > 0)
            {
                var shippingHandlingAccount = base.GetGeneralLedgerSetting().ShippingChargeAccount;
                var creditShippingHandlingAccount = _financialService.CreateGeneralLedgerLine(TransactionTypes.Cr, shippingHandlingAccount.Id, Math.Round(salesInvoice.ShippingHandlingCharge, 2, MidpointRounding.ToEven));
                glHeader.GeneralLedgerLines.Add(creditShippingHandlingAccount);
            }

            if (_financialService.ValidateGeneralLedgerEntry(glHeader))
            {
                salesInvoice.GeneralLedgerHeader = glHeader;

                salesInvoice.No = GetNextNumber(SequenceNumberTypes.SalesInvoice).ToString();

                if (!salesDeliveryId.HasValue)
                {
                    var salesDelivery = new SalesDeliveryHeader()
                    {
                        CustomerId = salesInvoice.CustomerId,
                        Date = salesInvoice.Date,
                        CreatedBy = salesInvoice.CreatedBy,
                        CreatedOn = DateTime.Now,
                        ModifiedBy = salesInvoice.ModifiedBy,
                        ModifiedOn = DateTime.Now,
                    };
                    foreach(var line in salesInvoice.SalesInvoiceLines)
                    {
                        var item = _itemRepo.GetById(line.ItemId);
                        salesDelivery.SalesDeliveryLines.Add(new SalesDeliveryLine()
                        {
                            ItemId = line.ItemId,
                            MeasurementId = line.MeasurementId,
                            Quantity = line.Quantity,
                            Discount = line.Discount,
                            Price = item.Cost.Value,
                            CreatedBy = salesInvoice.CreatedBy,
                            CreatedOn = DateTime.Now,
                            ModifiedBy = salesInvoice.ModifiedBy,
                            ModifiedOn = DateTime.Now,
                        });
                    }
                    AddSalesDelivery(salesDelivery, false);
                    salesInvoice.SalesDeliveryHeader = salesDelivery;
                }
                _salesInvoiceRepo.Insert(salesInvoice);
            }
        }
示例#6
0
        public IActionResult SaveSalesInvoice([FromBody] Dto.Sales.SalesInvoice salesInvoiceDto)
        {
            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));
                }

                bool isNew = salesInvoiceDto.Id == 0;
                Core.Domain.Sales.SalesInvoiceHeader salesInvoice = null;
                Core.Domain.Sales.SalesOrderHeader   salesOrder   = null;

                // Creating a new invoice
                if (isNew)
                {
                    // if fromsalesorderid has NO value, then create automatically a new sales order.
                    if (!salesInvoiceDto.FromSalesOrderId.HasValue)
                    {
                        salesOrder               = new Core.Domain.Sales.SalesOrderHeader();
                        salesOrder.Date          = salesInvoiceDto.InvoiceDate;
                        salesOrder.PaymentTermId = salesInvoiceDto.PaymentTermId;
                        salesOrder.CustomerId    = salesInvoiceDto.CustomerId;
                        salesOrder.ReferenceNo   = salesInvoiceDto.ReferenceNo;
                        salesOrder.Status        = SalesOrderStatus.FullyInvoiced;
                    }
                    else
                    {
                        // else,  your invoice is created from existing (open) sales order.
                        salesOrder = _salesService.GetSalesOrderById(salesInvoiceDto.FromSalesOrderId.GetValueOrDefault());
                    }

                    // populate invoice header
                    salesInvoice               = new Core.Domain.Sales.SalesInvoiceHeader();
                    salesInvoice.CustomerId    = salesInvoiceDto.CustomerId.GetValueOrDefault();
                    salesInvoice.Date          = salesInvoiceDto.InvoiceDate;
                    salesInvoice.PaymentTermId = salesInvoiceDto.PaymentTermId;
                    salesInvoice.ReferenceNo   = salesInvoiceDto.ReferenceNo;

                    foreach (var line in salesInvoiceDto.SalesInvoiceLines)
                    {
                        var salesInvoiceLine = new Core.Domain.Sales.SalesInvoiceLine();

                        salesInvoiceLine.Amount        = line.Amount.GetValueOrDefault();
                        salesInvoiceLine.Discount      = line.Discount.GetValueOrDefault();
                        salesInvoiceLine.Quantity      = line.Quantity.GetValueOrDefault();
                        salesInvoiceLine.ItemId        = line.ItemId.GetValueOrDefault();
                        salesInvoiceLine.MeasurementId = line.MeasurementId.GetValueOrDefault();
                        salesInvoice.SalesInvoiceLines.Add(salesInvoiceLine);

                        // line.Id here is referring to SalesOrderLineId. It is pre-populated when you create a new sales invoice from sales order.
                        if (line.Id != 0)
                        {
                            salesInvoiceLine.SalesOrderLineId = line.Id;
                        }
                        else
                        {
                            // if you reach here, this line item is newly added to invoice which is not originally in sales order. create correspondin orderline and add to sales order.
                            var salesOrderLine = new Core.Domain.Sales.SalesOrderLine();
                            salesOrderLine.Amount           = line.Amount.GetValueOrDefault();
                            salesOrderLine.Discount         = line.Discount.GetValueOrDefault();
                            salesOrderLine.Quantity         = line.Quantity.GetValueOrDefault();
                            salesOrderLine.ItemId           = line.ItemId.GetValueOrDefault();
                            salesOrderLine.MeasurementId    = line.MeasurementId.GetValueOrDefault();
                            salesInvoiceLine.SalesOrderLine = salesOrderLine;
                            salesOrder.SalesOrderLines.Add(salesOrderLine);

                            salesInvoiceLine.SalesOrderLine = salesOrderLine; // map invoice line to newly added orderline
                        }
                    }
                }
                else
                {
                    // if you reach here, you are updating existing invoice.
                    salesInvoice = _salesService.GetSalesInvoiceById(salesInvoiceDto.Id);

                    if (salesInvoice.GeneralLedgerHeaderId.HasValue)
                    {
                        throw new Exception("Invoice is already posted. Update is not allowed.");
                    }

                    salesInvoice.Date          = salesInvoiceDto.InvoiceDate;
                    salesInvoice.PaymentTermId = salesInvoiceDto.PaymentTermId;
                    salesInvoice.ReferenceNo   = salesInvoiceDto.ReferenceNo;

                    foreach (var line in salesInvoiceDto.SalesInvoiceLines)
                    {
                        var existingLine = salesInvoice.SalesInvoiceLines.Where(id => id.Id == line.Id).FirstOrDefault();
                        if (salesInvoice.SalesInvoiceLines.Where(id => id.Id == line.Id).FirstOrDefault() != null)
                        {
                            existingLine.Amount        = line.Amount.GetValueOrDefault();
                            existingLine.Discount      = line.Discount.GetValueOrDefault();
                            existingLine.Quantity      = line.Quantity.GetValueOrDefault();
                            existingLine.ItemId        = line.ItemId.GetValueOrDefault();
                            existingLine.MeasurementId = line.MeasurementId.GetValueOrDefault();
                        }
                        else
                        {
                            //if you reach here, this line item is newly added to invoice. also, it has no SalesOrderLineId.
                            var salesInvoiceLine = new Core.Domain.Sales.SalesInvoiceLine();
                            salesInvoiceLine.Amount        = line.Amount.GetValueOrDefault();
                            salesInvoiceLine.Discount      = line.Discount.GetValueOrDefault();
                            salesInvoiceLine.Quantity      = line.Quantity.GetValueOrDefault();
                            salesInvoiceLine.ItemId        = line.ItemId.GetValueOrDefault();
                            salesInvoiceLine.MeasurementId = line.MeasurementId.GetValueOrDefault();
                            salesInvoice.SalesInvoiceLines.Add(salesInvoiceLine);

                            // add a new order line.
                            var salesOrderLine = new Core.Domain.Sales.SalesOrderLine();
                            salesOrderLine.Amount        = line.Amount.GetValueOrDefault();
                            salesOrderLine.Discount      = line.Discount.GetValueOrDefault();
                            salesOrderLine.Quantity      = line.Quantity.GetValueOrDefault();
                            salesOrderLine.ItemId        = line.ItemId.GetValueOrDefault();
                            salesOrderLine.MeasurementId = line.MeasurementId.GetValueOrDefault();

                            // but on what order should the new orderline be added?
                            // note: each invoice is map to one and only one sales order. it can't be done that invoice lines came from multiple sales orders.
                            // with this rule, we are sure that all invoice lines are contained in the same sales order.
                            // therefore, we could just pick the first line, get the salesorderlineid, then get the salesorderheader.

                            // you will retrieve salesorder one time.
                            if (salesOrder == null)
                            {
                                // use the last value of existingLine
                                salesOrder = _salesService.GetSalesOrderLineById(existingLine.SalesOrderLine.SalesOrderHeaderId).SalesOrderHeader;
                                salesOrder.SalesOrderLines.Add(salesOrderLine);
                            }

                            salesInvoiceLine.SalesOrderLine = salesOrderLine; // map invoice line to newly added orderline
                        }
                    }
                }

                if (!isNew)
                {
                    var deleted = (from line in salesInvoice.SalesInvoiceLines
                                   where !salesInvoiceDto.SalesInvoiceLines.Any(x => x.Id == line.Id)
                                   select line).ToList();

                    foreach (var line in deleted)
                    {
                        salesInvoice.SalesInvoiceLines.Remove(line);
                    }
                }

                _salesService.SaveSalesInvoice(salesInvoice, salesOrder);

                return(new OkObjectResult(Ok()));
            }
            catch (Exception ex)
            {
                errors = new string[1] {
                    ex.InnerException != null ? ex.InnerException.Message : ex.Message
                };
                return(new BadRequestObjectResult(errors));
            }
        }