public bool DeleteData(int userId, int id, out Library.DTO.Notification notification)
        {
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            try
            {
                // check permission
                if (id > 0 && fwFactory.CheckLoadingPlanPermission(userId, id) == 0)
                {
                    throw new Exception("Current user don't have access permission for the selected loading plan data");
                }

                using (FactoryLoadingPlanMngEntities context = CreateContext())
                {
                    LoadingPlan dbItem = context.LoadingPlan.FirstOrDefault(o => o.LoadingPlanID == id);
                    if (dbItem == null)
                    {
                        throw new Exception("Loading plan not found!");
                    }

                    // check if loading plan already confirmed
                    if (dbItem.IsConfirmed.HasValue && dbItem.IsConfirmed.Value)
                    {
                        throw new Exception("Can not delete the confirmed loading plan!");
                    }

                    // check if loading plan already has invoice
                    PurchasingInvoice dbInvoice = context.PurchasingInvoice.FirstOrDefault(o => o.BookingID == dbItem.BookingID);
                    if (dbInvoice != null)
                    {
                        throw new Exception("Can not delete the loading plan which already has invoice issued!");
                    }

                    // everything ok, delete the loading plan
                    if (dbItem.ProductPicture1 != string.Empty)
                    {
                        fwFactory.RemoveImageFile(dbItem.ProductPicture1);
                    }
                    if (dbItem.ProductPicture2 != string.Empty)
                    {
                        fwFactory.RemoveImageFile(dbItem.ProductPicture2);
                    }
                    if (dbItem.ContainerPicture1 != string.Empty)
                    {
                        fwFactory.RemoveImageFile(dbItem.ContainerPicture1);
                    }
                    if (dbItem.ContainerPicture2 != string.Empty)
                    {
                        fwFactory.RemoveImageFile(dbItem.ContainerPicture2);
                    }
                    context.LoadingPlan.Remove(dbItem);
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                notification.Type    = Library.DTO.NotificationType.Error;
                notification.Message = ex.Message;
                return(false);
            }

            return(true);
        }
        public override bool UpdateData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification)
        {
            DTO.LoadingPlan dtoLoadingPlan = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.LoadingPlan>();
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            try
            {
                // check permission
                if (fwFactory.CheckBookingPermission(userId, dtoLoadingPlan.BookingID.Value) == 0)
                {
                    throw new Exception("Current user don't have access permission for the selected booking data");
                }
                if (id > 0 && fwFactory.CheckLoadingPlanPermission(userId, id) == 0)
                {
                    throw new Exception("Current user don't have access permission for the selected loading plan data");
                }

                using (FactoryLoadingPlanMngEntities context = CreateContext())
                {
                    LoadingPlan dbItem = null;
                    using (DbContextTransaction scope = context.Database.BeginTransaction())
                    {
                        context.Database.ExecuteSqlCommand("SELECT * FROM LoadingPlan WITH (TABLOCKX, HOLDLOCK); SELECT * FROM LoadingPlanDetail WITH (TABLOCKX, HOLDLOCK); SELECT * FROM LoadingPlanSparepartDetail WITH (TABLOCKX, HOLDLOCK);");

                        try
                        {
                            if (id == 0)
                            {
                                dbItem = new LoadingPlan();
                                dbItem.LoadingPlanUD = context.FactoryLoadingPlanMng_function_GenerateLoadingPlanCode(dtoLoadingPlan.FactoryID).FirstOrDefault();
                                context.LoadingPlan.Add(dbItem);
                            }
                            else
                            {
                                dbItem = context.LoadingPlan.FirstOrDefault(o => o.LoadingPlanID == id);
                            }

                            if (dbItem == null)
                            {
                                notification.Message = "Loading plan not found!";
                                return(false);
                            }
                            else
                            {
                                // check concurrency
                                if (dbItem.ConcurrencyFlag != null && !dbItem.ConcurrencyFlag.SequenceEqual(Convert.FromBase64String(dtoLoadingPlan.ConcurrencyFlag)))
                                {
                                    throw new Exception(Library.Helper.TEXT_CONCURRENCY_CONFLICT);
                                }

                                // check if invoice already issued
                                PurchasingInvoice dbInvoice = context.PurchasingInvoice.FirstOrDefault(o => o.BookingID == dtoLoadingPlan.BookingID);
                                if (dbInvoice == null)
                                {
                                    // check remain quantity
                                    foreach (DTO.LoadingPlanDetail dtoDetail in dtoLoadingPlan.LoadingPlanDetails)
                                    {
                                        List <FactoryLoadingPlanMng_FactoryOrderDetailLoaded_View> dbQnts = context.FactoryLoadingPlanMng_FactoryOrderDetailLoaded_View.Where(o => o.FactoryOrderDetailID == dtoDetail.FactoryOrderDetailID && o.LoadingPlanID != dtoLoadingPlan.LoadingPlanID).ToList();
                                        if (dtoDetail.Quantity.HasValue)
                                        {
                                            if (dtoDetail.OrderQnt.Value < dbQnts.Sum(o => o.Quantity) + dtoDetail.Quantity.Value)
                                            {
                                                string OldLoadingPlan = string.Empty;
                                                dbQnts.ForEach(o => OldLoadingPlan += o.LoadingPlanUD + ";");
                                                throw new Exception("Quantity exceed ordered quantity<br/><u>Product</u>: " + dtoDetail.Description + "<br/><u>Total ordered</u>: " + dtoDetail.OrderQnt.Value.ToString() + "<br/><u>Total loaded</u>: " + dbQnts.Sum(o => o.Quantity).ToString() + "<br/><u>Previous loading plan</u>: " + OldLoadingPlan + "<br/><u>To be loaded in this loading plan</u>: " + dtoDetail.Quantity);
                                            }
                                        }
                                    }

                                    foreach (DTO.LoadingPlanSparepartDetail dtoSparepartDetail in dtoLoadingPlan.LoadingPlanSparepartDetails)
                                    {
                                        List <FactoryLoadingPlanMng_FactoryOrderSparepartDetailLoaded_View> dbQnts = context.FactoryLoadingPlanMng_FactoryOrderSparepartDetailLoaded_View.Where(o => o.FactoryOrderSparepartDetailID == dtoSparepartDetail.FactoryOrderSparepartDetailID && o.LoadingPlanID != dtoLoadingPlan.LoadingPlanID).ToList();
                                        if (dtoSparepartDetail.Quantity.HasValue)
                                        {
                                            if (dtoSparepartDetail.OrderQnt < dbQnts.Sum(o => o.Quantity) + dtoSparepartDetail.Quantity.Value)
                                            {
                                                string OldLoadingPlan = string.Empty;
                                                dbQnts.ForEach(o => OldLoadingPlan += o.LoadingPlanUD + ";");
                                                throw new Exception("Quantity exceed ordered quantity<br/><u>Sparepart</u>: " + dtoSparepartDetail.Description + "<br/><u>Total ordered</u>: " + dtoSparepartDetail.OrderQnt.Value.ToString() + "<br/><u>Total loaded</u>: " + dbQnts.Sum(o => o.Quantity).ToString() + "<br/><u>Previous loading plan</u>: " + OldLoadingPlan + "<br/><u>To be loaded in this loading plan</u>: " + dtoSparepartDetail.Quantity);
                                            }
                                        }
                                    }

                                    converter.DTO2DB(dtoLoadingPlan, ref dbItem, FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\", true);
                                    context.LoadingPlanDetail.Local.Where(o => o.LoadingPlan == null).ToList().ForEach(o => context.LoadingPlanDetail.Remove(o));
                                    context.LoadingPlanSparepartDetail.Local.Where(o => o.LoadingPlan == null).ToList().ForEach(o => context.LoadingPlanSparepartDetail.Remove(o));
                                }
                                else
                                {
                                    converter.DTO2DB(dtoLoadingPlan, ref dbItem, FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\", false);
                                }

                                dbItem.UpdatedBy   = userId;
                                dbItem.UpdatedDate = DateTime.Now;

                                // remove orphan
                                context.LoadingPlanDetail.Local.Where(o => o.LoadingPlan == null).ToList().ForEach(o => context.LoadingPlanDetail.Remove(o));
                                context.LoadingPlanSparepartDetail.Local.Where(o => o.LoadingPlan == null).ToList().ForEach(o => context.LoadingPlanSparepartDetail.Remove(o));

                                context.SaveChanges();
                            }
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }
                        finally
                        {
                            scope.Commit();
                        }
                    }

                    dtoItem = GetData(userId, dbItem.LoadingPlanID, -1, -1, -1, out notification).Data;
                    return(true);
                }
            }
            catch (Exception ex)
            {
                notification = new Library.DTO.Notification()
                {
                    Message = ex.Message, Type = Library.DTO.NotificationType.Error
                };
                return(false);
            }
        }