Esempio 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");
 }
Esempio 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");
        }
Esempio 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(TransactionTypes.Dr, account.Key, account.Value));
            }
            foreach (var account in groupedCreditAccounts)
            {
                glHeader.GeneralLedgerLines.Add(_financialService.CreateGeneralLedgerLine(TransactionTypes.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(),
                        CreatedBy = salesDelivery.CreatedBy,
                        CreatedOn = DateTime.Now,
                        ModifiedBy = salesDelivery.ModifiedBy,
                        ModifiedOn = DateTime.Now,
                    };

                    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,
                            CreatedBy = salesDelivery.CreatedBy,
                            CreatedOn = DateTime.Now,
                            ModifiedBy = salesDelivery.ModifiedBy,
                            ModifiedOn = DateTime.Now,
                        });
                    }
                    AddSalesOrder(salesOrder, false);
                    salesDelivery.SalesOrderHeader = salesOrder;
                }

                if (toSave)
                    _salesDeliveryRepo.Insert(salesDelivery);
            }
        }
Esempio n. 4
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);
            }
        }