private void btnAllocate_Click(object sender, EventArgs e) { List <Models.OrderItem> openOrderItems = dbContext.OrderItems.Where(o => o.IsComplete == false) .Where(o => o.Part.ID == part.ID).ToList(); int.TryParse(textEditQty.Text, out int qtyToAllocate); int qtyAllocatedRunningTotal = 0; Models.OperationPerformed opPerformed = new Models.OperationPerformed(); opPerformed.qtyDone = qtyToAllocate; opPerformed.timePerformed = DateTime.Now; opPerformed.usr = currentUser; opPerformed.Notes = textEditNotes.Text; opPerformed.OrderItemOperationPerformeds = new List <Models.OrderItemOperationPerformed>(); foreach (Models.OrderItem orderItem in openOrderItems.ToList()) { if (orderItem.QtyRequired <= qtyToAllocate - qtyAllocatedRunningTotal) { orderItem.QtyNested = orderItem.QtyRequired; qtyAllocatedRunningTotal += orderItem.QtyRequired; Models.OrderItemOperationPerformed orderItemOpPerformed = new Models.OrderItemOperationPerformed(); orderItemOpPerformed.qtyDone = orderItem.QtyRequired; orderItemOpPerformed.orderItemOperation = orderItemOperation; orderItemOpPerformed.operationPerformed = opPerformed; dbContext.OrderItemOperationPerformeds.Add(orderItemOpPerformed); opPerformed.OrderItemOperationPerformeds.Add(orderItemOpPerformed); } } dbContext.OperationPerformeds.Add(opPerformed); dbContext.SaveChanges(); }
/// <summary> /// record an operation completed /// </summary> /// <param name="qty"></param> /// <returns></returns> public void RecordOperationCompleted(int qty) { Models.OrderItemOperation orderItemOp = Globals.dbContext.OrderItemOperations.Where(o => o.ID == OrderItemOp.ID).FirstOrDefault(); int qtyDone = qty; Models.OrderItemOperationPerformed itemOpPerformed = new Models.OrderItemOperationPerformed(); Models.OperationPerformed opPerformed = new Models.OperationPerformed(); opPerformed.qtyDone = qtyDone; opPerformed.timePerformed = DateTime.Now; opPerformed.usr = currentUser; opPerformed.OrderItemOperationsPerformed = new List <Models.OrderItemOperationPerformed>(); int totalQtyLeftToDo = qtyDone; // assign operations to order items foreach (Models.OrderItemOperation associatedOrderItemOp in associatedOrderItemOps) { if (associatedOrderItemOp.qtyDone < associatedOrderItemOp.qtyRequired) { int itemQtyLeftToDo = associatedOrderItemOp.qtyRequired - associatedOrderItemOp.qtyDone; Models.OrderItemOperationPerformed orderItemOpPerformed = new Models.OrderItemOperationPerformed(); if (totalQtyLeftToDo <= itemQtyLeftToDo) { orderItemOpPerformed = new Models.OrderItemOperationPerformed(); orderItemOpPerformed.qtyDone = totalQtyLeftToDo; associatedOrderItemOp.qtyDone += totalQtyLeftToDo; associatedOrderItemOp.OrderItemOperationPerformeds.Add(orderItemOpPerformed); opPerformed.OrderItemOperationsPerformed.Add(orderItemOpPerformed); totalQtyLeftToDo -= totalQtyLeftToDo; break; } else { orderItemOpPerformed = new Models.OrderItemOperationPerformed(); orderItemOpPerformed.qtyDone = itemQtyLeftToDo; associatedOrderItemOp.qtyDone = associatedOrderItemOp.qtyRequired; associatedOrderItemOp.OrderItemOperationPerformeds.Add(orderItemOpPerformed); opPerformed.OrderItemOperationsPerformed.Add(orderItemOpPerformed); totalQtyLeftToDo -= itemQtyLeftToDo; } } } // if we've filled all the order items, we need to do something with the rest... if (totalQtyLeftToDo > 0) { Models.Operation overBatchOperation = new Models.Operation(); overBatchOperation.Location = orderItemOp.operation.Location; overBatchOperation.Name = orderItemOp.operation.Name; overBatchOperation.Part = orderItemOp.operation.Part; Globals.dbContext.Operations.Add(overBatchOperation); Models.OrderItemOperation overBatchItemOperation = new Models.OrderItemOperation(); overBatchItemOperation.operation = overBatchOperation; overBatchItemOperation.orderItem = null; overBatchItemOperation.qtyDone = totalQtyLeftToDo; overBatchItemOperation.qtyRequired = 0; Globals.dbContext.OrderItemOperations.Add(overBatchItemOperation); Models.OrderItemOperationPerformed overBatchItemOperatonPerformed = new Models.OrderItemOperationPerformed(); overBatchItemOperatonPerformed.orderItemOperation = overBatchItemOperation; overBatchItemOperatonPerformed.qtyDone = totalQtyLeftToDo; overBatchItemOperatonPerformed.opPerformed = opPerformed; Globals.dbContext.OrderItemOperationPerformeds.Add(overBatchItemOperatonPerformed); opPerformed.OrderItemOperationsPerformed.Add(overBatchItemOperatonPerformed); overBatchOperation.OrderItemOperations.Add(overBatchItemOperation); } Globals.dbContext.OperationPerformeds.Add(opPerformed); Globals.dbContext.SaveChanges(); }