public ActionResult SaveOrder(Models.ViewModels.Purchases.PurchaseHeaderViewModel model) { PurchaseOrderHeader order = null; if (model.Id.HasValue == false || model.Id == 0) { order = new PurchaseOrderHeader(); } else { order = _purchasingService.GetPurchaseOrderById(model.Id.Value); } order.VendorId = model.VendorId.Value; order.Date = model.Date; order.Description = string.Empty; foreach (var line in model.PurchaseLine.PurchaseLineItems) { var item = _inventoryService.GetItemById(line.ItemId); order.PurchaseOrderLines.Add(new PurchaseOrderLine() { Amount = line.Price, ItemId = item.Id, Quantity = line.Quantity, MeasurementId = item.PurchaseMeasurementId.Value, Cost = item.Cost.Value, }); } _purchasingService.AddPurchaseOrder(order, true); return RedirectToAction("PurchaseOrders"); }
public void AddPurchaseInvoice(PurchaseInvoiceHeader purchaseIvoice, int? purchaseOrderId) { #region Auto create purchase order if (!purchaseOrderId.HasValue) { var po = new PurchaseOrderHeader() { Date = purchaseIvoice.Date, No = GetNextNumber(SequenceNumberTypes.PurchaseOrder).ToString(), Vendor = purchaseIvoice.Vendor, VendorId = purchaseIvoice.VendorId.Value, Description = purchaseIvoice.Description, }; foreach (var line in purchaseIvoice.PurchaseInvoiceLines) { var item = _itemRepo.GetById(line.ItemId); po.PurchaseOrderLines.Add(new PurchaseOrderLine() { ItemId = item.Id, MeasurementId = line.MeasurementId, Quantity = line.Quantity, Cost = item.Cost.Value, Discount = line.Discount.HasValue ? line.Discount.Value : 0, Amount = item.Cost.Value * line.Quantity, }); } purchaseIvoice.PurchaseOrders.Add(po); var poReceipt = new PurchaseReceiptHeader() { Date = DateTime.Now, Vendor = po.Vendor, VendorId = po.VendorId.Value, PurchaseOrderHeaderId = po.Id, }; foreach (var line in purchaseIvoice.PurchaseInvoiceLines) { poReceipt.PurchaseReceiptLines.Add(new PurchaseReceiptLine() { ItemId = line.ItemId, MeasurementId = line.MeasurementId, Quantity = line.Quantity, ReceivedQuantity = (line.ReceivedQuantity.HasValue ? line.ReceivedQuantity.Value : 0), Cost = line.Cost.Value, Amount = line.Cost.Value * (line.ReceivedQuantity.HasValue ? line.ReceivedQuantity.Value : 0), }); } po.PurchaseReceipts.Add(poReceipt); AddPurchaseOrderReceipt(poReceipt); } #endregion var glHeader = _financialService.CreateGeneralLedgerHeader(DocumentTypes.PurchaseInvoice, purchaseIvoice.Date, purchaseIvoice.Description); decimal totalTaxAmount = 0, totalAmount = 0, totalDiscount = 0; var taxes = new List<KeyValuePair<int, decimal>>(); foreach (var line in purchaseIvoice.PurchaseInvoiceLines) { var lineTaxes = _financialService.ComputeInputTax(purchaseIvoice.VendorId.Value, line.ItemId, line.Quantity, line.Cost.Value, decimal.Zero); var lineAmount = line.Quantity * line.Cost; var totalLineAmount = lineAmount + lineTaxes.Sum(t => t.Value); totalAmount += (decimal)totalLineAmount; foreach (var t in lineTaxes) taxes.Add(t); } 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) }; totalTaxAmount = taxes.Sum(t => t.Value); foreach (var tax in groupedTaxes) { var tx = _financialService.GetTaxes().Where(t => t.Id == tax.Key).FirstOrDefault(); var debitPurchaseTaxAccount = _financialService.CreateGeneralLedgerLine(DrOrCrSide.Dr, tx.PurchasingAccountId.Value, tax.Value); glHeader.GeneralLedgerLines.Add(debitPurchaseTaxAccount); } } if (totalDiscount > 0) { } Vendor vendor = _vendorRepo.GetById(purchaseIvoice.VendorId.Value); var creditVendorAccount = _financialService.CreateGeneralLedgerLine(DrOrCrSide.Cr, vendor.AccountsPayableAccountId.Value, totalAmount); glHeader.GeneralLedgerLines.Add(creditVendorAccount); var debitGRNClearingAccount = _financialService.CreateGeneralLedgerLine(DrOrCrSide.Dr, GetGeneralLedgerSetting().GoodsReceiptNoteClearingAccountId.Value, totalAmount - totalTaxAmount); glHeader.GeneralLedgerLines.Add(debitGRNClearingAccount); if (_financialService.ValidateGeneralLedgerEntry(glHeader)) { purchaseIvoice.GeneralLedgerHeader = glHeader; purchaseIvoice.No = GetNextNumber(SequenceNumberTypes.PurchaseInvoice).ToString(); _purchaseInvoiceRepo.Insert(purchaseIvoice); // TODO: Look for other way to update the purchase order's invoice header id field so that it shall be in a single transaction along with purchase invoice saving var purchOrder = _purchaseOrderRepo.GetById(purchaseOrderId.Value); purchOrder.PurchaseInvoiceHeaderId = purchaseIvoice.Id; _purchaseOrderRepo.Update(purchOrder); } }
public ActionResult SavePurchaseOrder(Models.ViewModels.Purchases.AddPurchaseOrder model) { var items = _inventoryService.GetAllItems(); var accounts = _financialService.GetAccounts(); var measurements = _inventoryService.GetMeasurements(); var taxes = _financialService.GetTaxes(); var itemCategories = _inventoryService.GetItemCategories(); var vendors = _purchasingService.GetVendors(); model.Items = Models.ModelViewHelper.Items(); model.Vendors = Models.ModelViewHelper.Vendors(); model.UnitOfMeasurements = Models.ModelViewHelper.Measurements(); try { var po = new PurchaseOrderHeader() { VendorId = model.VendorId, Date = model.Date, //No = _settingService.GetNextNumber(Core.Module.Common.Data.SequenceNumberTypes.PurchaseOrder).ToString(), //DocumentTypeId = (int)DocumentTypes.PurchaseOrder }; foreach (var item in model.PurchaseOrderLines) { var persistedItem = _inventoryService.GetItemById(item.ItemId); po.PurchaseOrderLines.Add(new PurchaseOrderLine() { Amount = item.TotalLineCost, ItemId = item.ItemId, Quantity = item.Quantity, MeasurementId = item.UnitOfMeasurementId, Cost = persistedItem.Cost.Value, }); } _purchasingService.AddPurchaseOrder(po, true); return RedirectToAction("PurchaseOrders"); } catch { return View(model); } }
public void AddPurchaseOrder(PurchaseOrderHeader purchaseOrder, bool toSave) { purchaseOrder.No = GetNextNumber(SequenceNumberTypes.PurchaseOrder).ToString(); if(toSave) _purchaseOrderRepo.Insert(purchaseOrder); }