public async Task CreateOrEdit(CreateOrEditSalesOrderDto input) { if (input.Id == null) { await Create(input); } else { await Update(input); } }
protected virtual async Task Update(CreateOrEditSalesOrderDto input) { var salesOrder = _salesOrderRepository.Get((int)input.Id); //Iterate Sales Order Lines var salesOrderLineList = new List <SalesOrderLine>(); List <string> salesOrderLineNames = new List <string>(); foreach (var item in input.SalesOrderLines.ToList()) { var id = (int)item.Id; var markForDelete = item.MarkForDelete; var name = item.Name; if (markForDelete) { //Existing item if (id != 0) { //Delete from database var deleteItem = _salesOrderLineRepository.Get(id); await _salesOrderLineRepository.DeleteAsync(id); } //Remove from collection input.SalesOrderLines.Remove(item); } else { var salesOrderLine = new SalesOrderLine { Id = (int)item.Id, Name = item.Name, Description = item.Description, Quantity = decimal.Parse(item.Quantity.Replace(".", "")), Price = decimal.Parse(item.UnitPrice.Replace(".", "")), Amount = decimal.Parse(item.LineAmount.Replace(".", "")), SalesOrderId = salesOrder.Id }; //ADD SUB TO COLLECTION salesOrderLineNames.Add(item.Name); salesOrderLineList.Add(salesOrderLine); } } //ADD SUBS TO PARENT salesOrder.SalesOrderLineNames = string.Join(", ", salesOrderLineNames); salesOrder.SalesOrderLines = salesOrderLineList; salesOrder.Id = (int)input.Id; salesOrder.Number = input.Number; salesOrder.ProcessedBySurabaya = input.ProcessedBySurabaya; salesOrder.CustomerId = input.CustomerId; salesOrder.Date = input.Date; salesOrder.Deadline = input.Deadline; salesOrder.Amount = decimal.Parse(input.Amount.Replace(".", "")); salesOrder.ProductionStatusId = 10; // 10 - New salesOrder.MarkForDelete = false; ////Update //_salesOrderRepository.Update(salesOrder); }
protected virtual async Task Create(CreateOrEditSalesOrderDto input) { var salesOrder = new SalesOrder { //Id = 0, Number = CreateSalesOrderNumber(input.Date, input.CustomerId), ProcessedBySurabaya = input.ProcessedBySurabaya, CustomerId = input.CustomerId, Date = input.Date, Deadline = input.Deadline, Amount = decimal.Parse(input.Amount.Replace(".", "")), ProductionStatusId = 10, // 10 - New MarkForDelete = false }; //Iterate Sales Order Lines var salesOrderLineList = new List <SalesOrderLine>(); List <string> salesOrderLineNames = new List <string>(); foreach (var item in input.SalesOrderLines.ToList()) { //Create Sales Order Line var markForDelete = item.MarkForDelete; var name = item.Name; if (markForDelete) { //Remove from collection input.SalesOrderLines.Remove(item); } else { var salesOrderLine = new SalesOrderLine { Id = 0, Name = item.Name, Description = item.Description, Quantity = decimal.Parse(item.Quantity.Replace(".", "")), Price = decimal.Parse(item.UnitPrice.Replace(".", "")), Amount = decimal.Parse(item.LineAmount.Replace(".", "")) }; //Add item to collection salesOrderLineList.Add(salesOrderLine); salesOrderLineNames.Add(item.Name); } } //Add collection to parent salesOrder.SalesOrderLineNames = string.Join(", ", salesOrderLineNames); salesOrder.SalesOrderLines = salesOrderLineList; //Create Sales Order int newSalesOrderId = _salesOrderRepository.InsertAndGetId(salesOrder); //Create Sales Invoice var salesInvoice = new SalesInvoice { Number = salesOrder.Number.Substring(1), Date = salesOrder.Date, DueDate = salesOrder.Deadline, SalesOrderId = newSalesOrderId, Amount = salesOrder.Amount, Paid = 0, Outstanding = salesOrder.Amount, SalesInvoiceLineNames = salesOrder.SalesOrderLineNames, MarkForDelete = false }; int newSalesInvoiceId = await _salesInvoiceRepository.InsertAndGetIdAsync(salesInvoice); }