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(items); model.Vendors = Models.ModelViewHelper.Vendors(vendors); model.UnitOfMeasurements = Models.ModelViewHelper.Measurements(measurements); try { var po = new PurchaseOrderHeader() { CreatedBy = User.Identity.Name, CreatedOn = DateTime.Now, ModifiedBy = User.Identity.Name, ModifiedOn = DateTime.Now, 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, CreatedBy = User.Identity.Name, CreatedOn = DateTime.Now, ModifiedBy = User.Identity.Name, ModifiedOn = DateTime.Now, }); } _purchasingService.AddPurchaseOrder(po, true); return(RedirectToAction("PurchaseOrders")); } catch { return(View(model)); } }
public IActionResult SavePurchaseOrder([FromBody] Dto.Purchasing.PurchaseOrder purchaseOrderDto) { string[] errors = null; 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)); } try { bool isNew = purchaseOrderDto.Id == 0; Core.Domain.Purchases.PurchaseOrderHeader purchaseOrder = null; if (isNew) { purchaseOrder = new Core.Domain.Purchases.PurchaseOrderHeader(); } else { purchaseOrder = _purchasingService.GetPurchaseOrderById(purchaseOrderDto.Id); } purchaseOrder.ReferenceNo = purchaseOrderDto.ReferenceNo; purchaseOrder.PaymentTermId = purchaseOrderDto.PaymentTermId; purchaseOrder.VendorId = purchaseOrderDto.VendorId; purchaseOrder.Date = purchaseOrderDto.OrderDate; foreach (var line in purchaseOrderDto.PurchaseOrderLines) { if (!isNew) { var existingLine = purchaseOrder.PurchaseOrderLines.Where(id => id.Id == line.Id).FirstOrDefault(); if (purchaseOrder.PurchaseOrderLines.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 { var purchaseOrderLine = new Core.Domain.Purchases.PurchaseOrderLine(); purchaseOrderLine.Amount = line.Amount.GetValueOrDefault(); purchaseOrderLine.Discount = line.Discount.GetValueOrDefault(); purchaseOrderLine.Quantity = line.Quantity.GetValueOrDefault(); purchaseOrderLine.ItemId = line.ItemId.GetValueOrDefault(); purchaseOrderLine.MeasurementId = line.MeasurementId.GetValueOrDefault(); purchaseOrder.PurchaseOrderLines.Add(purchaseOrderLine); } } else { var purchaseOrderLine = new Core.Domain.Purchases.PurchaseOrderLine(); purchaseOrderLine.Amount = line.Amount.GetValueOrDefault(); purchaseOrderLine.Discount = line.Discount.GetValueOrDefault(); purchaseOrderLine.Quantity = line.Quantity.GetValueOrDefault(); purchaseOrderLine.ItemId = line.ItemId.GetValueOrDefault(); purchaseOrderLine.MeasurementId = line.MeasurementId.GetValueOrDefault(); purchaseOrder.PurchaseOrderLines.Add(purchaseOrderLine); } } if (isNew) { _purchasingService.AddPurchaseOrder(purchaseOrder, true); } else { var deleted = (from line in purchaseOrder.PurchaseOrderLines where !purchaseOrderDto.PurchaseOrderLines.Any(x => x.Id == line.Id) select line).ToList(); foreach (var line in deleted) { if (line.PurchaseInvoiceLines.Count() > 0) { throw new Exception("The line cannot be deleted. An invoice line is created from the item."); } } foreach (var line in deleted) { purchaseOrder.PurchaseOrderLines.Remove(line); } _purchasingService.UpdatePurchaseOrder(purchaseOrder); } return(new OkObjectResult(Ok())); } catch (Exception ex) { errors = new string[1] { ex.InnerException != null ? ex.InnerException.Message : ex.Message }; return(new BadRequestObjectResult(errors)); } }