public bool Generate()
        {
            try
            {
                if (ViewModel.JO.Validate() == false)
                {
                    Approval approval = new Approval();
                    approval.Order = ViewModel.JO.Order;
                    approval.IsApproved = null;
                    approval.ApprovalEntityTypeID = 3;

                    StringBuilder comments = new StringBuilder();
                    comments.Append("Generated on " + DBResources.Instance.GetServerTime() + " by " + DBResources.Instance.CurrentUser.UserName);

                    approval.Comments = comments.ToString();
                    ViewModel.JO.Approval = approval;
                    
                    ViewModel.Order.DyeingJOes.Add(ViewModel.JO);

                    DBResources.Instance.Save();
                    
                    StringBuilder sb = new StringBuilder();
                    sb.AppendLine("Job Order Successfully Created");
                    sb.AppendLine("Submitted For Approval !.");
                    InformUser(sb.ToString());

                    ViewModel.JO.RefreshInfoJobOrderInfo();
                }
                else
                {
                    InformUser("Fix the Highlighted Errors and try again");
                    return false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }

            return true;
        }
        private void positiveBtn_Click_1(object sender, RoutedEventArgs e)
        {
            if (positiveBtn.Content.ToString() == "Generate" || positiveBtn.Content.ToString() == "Submit")
            {
                ViewModel.PurchaseOrder.Validate();
                if (!ViewModel.PurchaseOrder.HasErrors)
                {
                    if (positiveBtn.Content.ToString() == "Generate")
                    {
                        ViewModel.PurchaseOrder = DBResources.Instance.Context.PurchaseOrders.Add(ViewModel.PurchaseOrder);
                    }

                    foreach (IPurchaseOrderItem item in ViewModel.SelectedItems)
                    {
                        item.PurchaseOrder = ViewModel.PurchaseOrder;
                    }

                    if (ViewModel.PurchaseOrder.Approval == null)
                    {
                        Order.PurchaseOrders.Add(ViewModel.PurchaseOrder);
                        Approval approval = new Approval();
                        approval.ApprovalEntityTypeID = 2;
                        approval.Order = ViewModel.Order;
                        StringBuilder sb = new StringBuilder();
                        sb.AppendLine("Generated by " + DBResources.Instance.CurrentUser.UserName + " at " + DBResources.Instance.GetServerTime().ToString());
                        sb.AppendLine();
                        approval.Comments = sb.ToString();
                        ViewModel.PurchaseOrder.Approval = approval;
                        approval.PurchaseOrders.Add(ViewModel.PurchaseOrder);
                    }
                    else
                    {
                        StringBuilder sb = new StringBuilder();
                        sb.AppendLine("Submitted By " + DBResources.Instance.CurrentUser.UserName + " at " + DBResources.Instance.GetServerTime().ToString());
                        sb.AppendLine();
                        ViewModel.PurchaseOrder.Approval.Comments = sb.ToString() + ViewModel.PurchaseOrder.Approval.Comments;
                        ViewModel.PurchaseOrder.Approval.IsApproved = null;
                    }

                    DBResources.Instance.Save();
                    

                    if (positiveBtn.Content.ToString() == "Generate")
                    {
                        SetUIAccesibility(PurchaseOrderState.Generated);
                        StringBuilder sb = new StringBuilder();
                        sb.AppendLine("Purchase Order Successfully Created");
                        sb.AppendLine("Submitted For Approval !.");
                        InformUser(sb.ToString());
                        Reset();
                        if (PurchaseOrderStatusChanged != null)
                        {
                            PurchaseOrderStatusChanged(PurchaseOrderState.Generated);
                        }
                    }
                    else
                    {
                        SetUIAccesibility(PurchaseOrderState.Submitted);
                        InformUser("Purchase Order Successfully Submitted");
                    }
                }
                else
                {
                    InformUser("Fix the Highlighted Errors and try again");
                }
            }
            else if(positiveBtn.Content.ToString() == "Approve")
            {
                
                foreach (IPurchaseOrderItem item in ViewModel.SelectedItems)
                {
                    if (item is ProductMaterialItem)
                    {
                        ProductMaterialItem pmItem = item as ProductMaterialItem;
                        OrderedItem itemToOrder = new OrderedItem { ProductMaterialItem = pmItem, OrderedQuantity = pmItem.Quantity };
                        itemToOrder.CostWrapper = item.CostWrapper;
                        itemToOrder.TaxPerUnitWrapper = item.TaxPerUnitWrapper;
                        pmItem.SupplierWrapper = ViewModel.PurchaseOrder.Company;
                        pmItem.LastPOGeneratedOn = DBResources.Instance.GetServerTime();
                        ViewModel.PurchaseOrder.OrderedItems.Add(itemToOrder);
                        item.PurchaseOrder = ViewModel.PurchaseOrder;
                    }
                }


                StringBuilder sb = new StringBuilder();
                sb.AppendLine("Approved By " + DBResources.Instance.CurrentUser.UserName + " at " + DBResources.Instance.GetServerTime().ToString());
                sb.AppendLine();
                ViewModel.PurchaseOrder.Approval.Comments = sb.ToString() + ViewModel.PurchaseOrder.Approval.Comments;
                ViewModel.PurchaseOrder.Approval.IsApproved = true;

                DBResources.Instance.Save();

                SetUIAccesibility(PurchaseOrderState.Approved);
            }
            else if (positiveBtn.Content.ToString() == "PDF")
            {
                PurchaseOrderPDFGenerator pdfGenerator = new PurchaseOrderPDFGenerator(ViewModel.PurchaseOrder);
                string generatedFile = pdfGenerator.GeneratePurchaseOrder();
                if (string.IsNullOrEmpty(generatedFile))
                {
                    InformUser("Failed to Generate !");
                }
                else
                {
                    //Initiate PDF Viewer
                    System.Diagnostics.Process.Start(generatedFile);

                    //Initiate New Outlook Message
                    try
                    {
                        InitiateNewOutlookMessage(generatedFile);
                    }
                    catch
                    {

                    }
                }
            }

            ViewModel.PurchaseOrder.RefreshUIProperties();
        }
        private bool? Save(bool isSubmit)
        {
            if (m_ViewModel != null)
            {
                if (m_ViewModel.Validate())
                {
                        foreach (OrderProduct product in m_ViewModel.Order.OrderProducts)
                        {
                            foreach (ProductMaterial material in product.ProductMaterials)
                            {
                                if (material.Approval != null && material.Approval.IsApproved != null && material.Approval.IsApproved.Value == false)
                                {
                                    material.Approval.IsApproved = null; 
                                }

                                if (material.Approval == null && material.IsBOMCostExceedsBudget())
                                {
                                    Approval approval = new Approval();
                                    approval.ApprovalEntityTypeID = 1;
                                    approval.Comments = "Submitted for Approval by " + DBResources.Instance.CurrentUser.UserName + " at " + DateTime.Now.ToString();
                                    approval.Order = m_ViewModel.Order;
                                    material.Approval = approval;
                                }
                            }

                            bool result = m_ViewModel.Save(isSubmit, "Save");
                            if (result)
                            {
                                poMaterialsDetails.SetUIAccesibility();
                                foreach (OrderProduct op in m_ViewModel.Order.OrderProducts)
                                {
                                    foreach (ProductMaterial pm in op.ProductMaterials)
                                    {
                                        pm.RefreshUIProperties();
                                        foreach (ProductMaterialItem item in pm.ProductMaterialItems)
                                        {
                                            item.RefreshUIProperties();
                                        }
                                    }
                                }
                            }

                            return result;
                        }
                   return false;
                }
                return false;
            }
            return false;
        }