Exemplo n.º 1
0
        public ActionResult SaveSalesDelivery(Models.ViewModels.Sales.SalesDeliveryViewModel model)
        {
            var salesDelivery = new SalesDeliveryHeader()
            {
                CustomerId    = model.CustomerId,
                PaymentTermId = model.PaymentTermId,
                Date          = model.Date,
                CreatedBy     = User.Identity.Name,
                CreatedOn     = DateTime.Now,
                ModifiedBy    = User.Identity.Name,
                ModifiedOn    = DateTime.Now,
            };

            foreach (var line in model.SalesDeliveryLines)
            {
                salesDelivery.SalesDeliveryLines.Add(new SalesDeliveryLine()
                {
                    ItemId        = line.ItemId,
                    MeasurementId = line.MeasurementId,
                    Quantity      = line.Quantity,
                    Discount      = line.Discount,
                    Price         = line.Quantity * line.Price,
                    CreatedBy     = User.Identity.Name,
                    CreatedOn     = DateTime.Now,
                    ModifiedBy    = User.Identity.Name,
                    ModifiedOn    = DateTime.Now,
                });
            }
            _salesService.AddSalesDelivery(salesDelivery, true);
            return(RedirectToAction("SalesDeliveries"));
        }
Exemplo n.º 2
0
        public ActionResult SaveDelivery(Models.ViewModels.Sales.SalesHeaderViewModel model)
        {
            SalesDeliveryHeader delivery = null;

            if (model.Id == 0)
            {
                delivery           = new SalesDeliveryHeader();
                delivery.CreatedBy = User.Identity.Name;
                delivery.CreatedOn = DateTime.Now;
            }
            else
            {
                delivery = _salesService.GetSalesDeliveryById(model.Id);
            }

            delivery.ModifiedBy    = User.Identity.Name;
            delivery.ModifiedOn    = DateTime.Now;
            delivery.CustomerId    = model.CustomerId.Value;
            delivery.PaymentTermId = model.PaymentTermId;
            delivery.Date          = model.Date;

            foreach (var line in model.SalesLine.SalesLineItems)
            {
                SalesDeliveryLine lineItem = null;
                var item = _inventoryService.GetItemByNo(line.ItemNo);
                if (!line.Id.HasValue)
                {
                    lineItem           = new SalesDeliveryLine();
                    lineItem.CreatedBy = User.Identity.Name;
                    lineItem.CreatedOn = DateTime.Now;
                    delivery.SalesDeliveryLines.Add(lineItem);
                }
                else
                {
                    lineItem = delivery.SalesDeliveryLines.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.Price         = line.Price;
            }

            if (model.Id == 0)
            {
                _salesService.AddSalesDelivery(delivery, true);
            }
            else
            {
            }

            return(RedirectToAction("SalesDeliveries"));
        }
Exemplo n.º 3
0
        public void AddSalesDelivery(SalesDeliveryHeader salesDelivery, bool toSave)
        {
            var glHeader = _financialService.CreateGeneralLedgerHeader(DocumentTypes.SalesDelivery, salesDelivery.Date, string.Empty);
            // Debit = COGS, Credit = Inventory
            var debitAccounts  = new List <KeyValuePair <int, decimal> >();
            var creditAccounts = new List <KeyValuePair <int, decimal> >();

            foreach (var line in salesDelivery.SalesDeliveryLines)
            {
                var item = _inventoryService.GetItemById(line.ItemId.Value);
                debitAccounts.Add(new KeyValuePair <int, decimal>(item.CostOfGoodsSoldAccountId.Value, item.Cost.Value * line.Quantity));
                creditAccounts.Add(new KeyValuePair <int, decimal>(item.InventoryAccountId.Value, item.Cost.Value * line.Quantity));
            }
            var groupedDebitAccounts = (from kvp in debitAccounts
                                        group kvp by kvp.Key into g
                                        select new KeyValuePair <int, decimal>(g.Key, g.Sum(e => e.Value)));
            var groupedCreditAccounts = (from kvp in creditAccounts
                                         group kvp by kvp.Key into g
                                         select new KeyValuePair <int, decimal>(g.Key, g.Sum(e => e.Value)));

            foreach (var account in groupedDebitAccounts)
            {
                glHeader.GeneralLedgerLines.Add(_financialService.CreateGeneralLedgerLine(DrOrCrSide.Dr, account.Key, account.Value));
            }
            foreach (var account in groupedCreditAccounts)
            {
                glHeader.GeneralLedgerLines.Add(_financialService.CreateGeneralLedgerLine(DrOrCrSide.Cr, account.Key, account.Value));
            }

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

                salesDelivery.No = GetNextNumber(SequenceNumberTypes.SalesDelivery).ToString();

                if (!salesDelivery.SalesOrderHeaderId.HasValue)
                {
                    var salesOrder = new SalesOrderHeader()
                    {
                        CustomerId    = salesDelivery.CustomerId,
                        PaymentTermId = salesDelivery.PaymentTermId,
                        Date          = salesDelivery.Date,
                        No            = GetNextNumber(SequenceNumberTypes.SalesOrder).ToString(),
                    };

                    foreach (var line in salesDelivery.SalesDeliveryLines)
                    {
                        var item = _inventoryService.GetItemById(line.ItemId.Value);
                        salesOrder.SalesOrderLines.Add(new SalesOrderLine()
                        {
                            ItemId        = item.Id,
                            MeasurementId = line.MeasurementId.Value,
                            Quantity      = line.Quantity,
                            Amount        = item.Price.Value,
                        });
                    }
                    AddSalesOrder(salesOrder, false);
                    salesDelivery.SalesOrderHeader = salesOrder;
                }

                if (toSave)
                {
                    _salesDeliveryRepo.Insert(salesDelivery);
                }
            }
        }
Exemplo n.º 4
0
        public void AddSalesInvoice(SalesInvoiceHeader salesInvoice, int?salesDeliveryId)
        {
            decimal totalAmount = 0, totalDiscount = 0;

            var taxes = new List <KeyValuePair <int, decimal> >();
            var sales = new List <KeyValuePair <int, decimal> >();

            var glHeader = _financialService.CreateGeneralLedgerHeader(DocumentTypes.SalesInvoice, salesInvoice.Date, string.Empty);
            var customer = _customerRepo.GetById(salesInvoice.CustomerId);

            foreach (var lineItem in salesInvoice.SalesInvoiceLines)
            {
                var item = _itemRepo.GetById(lineItem.ItemId);

                var lineAmount = lineItem.Quantity * lineItem.Amount;

                if (!item.GLAccountsValidated())
                {
                    throw new Exception("Item is not correctly setup for financial transaction. Please check if GL accounts are all set.");
                }

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

                var totalLineAmount = lineAmount - lineDiscountAmount;

                totalAmount += totalLineAmount;

                var lineTaxes = _financialService.ComputeOutputTax(salesInvoice.CustomerId, item.Id, lineItem.Quantity, lineItem.Amount, lineItem.Discount);

                foreach (var t in lineTaxes)
                {
                    taxes.Add(t);
                }

                var lineTaxAmount = lineTaxes != null && lineTaxes.Count > 0 ? lineTaxes.Sum(t => t.Value) : 0;
                totalLineAmount = totalLineAmount - lineTaxAmount;

                sales.Add(new KeyValuePair <int, decimal>(item.SalesAccountId.Value, totalLineAmount));

                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);
                }
            }

            totalAmount += salesInvoice.ShippingHandlingCharge;
            var debitCustomerAR = _financialService.CreateGeneralLedgerLine(DrOrCrSide.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;
                var creditSalesAccount = _financialService.CreateGeneralLedgerLine(DrOrCrSide.Cr, salesAccount.Key, Math.Round(salesAmount, 2, MidpointRounding.ToEven));
                glHeader.GeneralLedgerLines.Add(creditSalesAccount);
            }

            if (taxes != null && taxes.Count > 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(DrOrCrSide.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(DrOrCrSide.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(DrOrCrSide.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,
                    };
                    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,
                        });
                    }
                    AddSalesDelivery(salesDelivery, false);
                    salesInvoice.SalesDeliveryHeader = salesDelivery;
                }
                _salesInvoiceRepo.Insert(salesInvoice);
            }
        }