public bool UpdatePurchaseOrder(PurchaseOrder po, int senderId = -1) { try { bool allItemsProcessed = true; bool allItemsPending = true; foreach (Item item in po.Items) { if (item.Status == ItemStatus.Pending) { allItemsProcessed = false; } if (item.Status != ItemStatus.Pending && item.Status != ItemStatus.NoLongerRequired && item.Description != "No longer needed") { allItemsPending = false; } } if (allItemsProcessed && po.ClosePurchaseOrder) { po.Status = POStatus.Closed; } else if (allItemsPending) { po.Status = POStatus.Pending; } else if (allItemsProcessed && po.ClosePurchaseOrder == false && po.Status != POStatus.Closed) { po.Status = POStatus.UnderReview; } else if (!allItemsProcessed && !allItemsPending) { po.Status = POStatus.UnderReview; } if (IsValid(po) && po.Items.Count > 0 ? repo.UpdatePurchaseOrder(po.IsModifyItem ? po : MergeDuplicates(po)) : false) { if (po.Status == POStatus.Closed) { EmployeeService empService = new EmployeeService(); EmployeeDTO emp = empService.GetEmployeeInformation(senderId); if (SendPOProcessedEmailNotification(po: po, sender: emp)) { return(true); } po.AddError(new Error(120, "The process confirmation email was not sent", "Email not sent")); return(false); } return(true); } po.AddError(new Error(10, "The Purchase Order was not updated", "Business")); return(false); } catch (Exception ex) { po.AddError(new Error(2, ex.Message, ex.GetType().ToString())); return(false); } }