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);
        }
Beispiel #2
0
        public void DTO2DB(DTO.LoadingPlan dtoItem, ref LoadingPlan dbItem, string _tempFolder, bool mapDetail)
        {
            // map fields
            AutoMapper.Mapper.Map <DTO.LoadingPlan, LoadingPlan>(dtoItem, dbItem);

            // insert image
            Module.Framework.DAL.DataFactory fwFactory = new Module.Framework.DAL.DataFactory();
            if (dtoItem.ProductPicture1_HasChange)
            {
                dbItem.ProductPicture1 = fwFactory.CreateFilePointer(_tempFolder, dtoItem.ProductPicture1_NewFile, dtoItem.ProductPicture1);
            }
            if (dtoItem.ProductPicture2_HasChange)
            {
                dbItem.ProductPicture2 = fwFactory.CreateFilePointer(_tempFolder, dtoItem.ProductPicture2_NewFile, dtoItem.ProductPicture2);
            }
            if (dtoItem.ContainerPicture1_HasChange)
            {
                dbItem.ContainerPicture1 = fwFactory.CreateFilePointer(_tempFolder, dtoItem.ContainerPicture1_NewFile, dtoItem.ContainerPicture1);
            }
            if (dtoItem.ContainerPicture2_HasChange)
            {
                dbItem.ContainerPicture2 = fwFactory.CreateFilePointer(_tempFolder, dtoItem.ContainerPicture2_NewFile, dtoItem.ContainerPicture2);
            }
            if (!string.IsNullOrEmpty(dtoItem.ShipmentDate))
            {
                if (DateTime.TryParse(dtoItem.ShipmentDate, nl, System.Globalization.DateTimeStyles.None, out tmpDate))
                {
                    dbItem.ShipmentDate = tmpDate;
                }
            }
            if (!string.IsNullOrEmpty(dtoItem.LoadingDate))
            {
                if (DateTime.TryParse(dtoItem.LoadingDate, nl, System.Globalization.DateTimeStyles.None, out tmpDate))
                {
                    dbItem.LoadingDate = tmpDate;
                }
            }

            if (mapDetail)
            {
                // map detail
                if (dtoItem.LoadingPlanDetails != null)
                {
                    // check for child rows deleted
                    foreach (LoadingPlanDetail dbDetail in dbItem.LoadingPlanDetail.ToArray())
                    {
                        if (!dtoItem.LoadingPlanDetails.Select(o => o.LoadingPlanDetailID).Contains(dbDetail.LoadingPlanDetailID))
                        {
                            dbItem.LoadingPlanDetail.Remove(dbDetail);
                        }
                    }

                    // map child rows
                    foreach (DTO.LoadingPlanDetail dtoDetail in dtoItem.LoadingPlanDetails)
                    {
                        LoadingPlanDetail dbDetail;
                        if (dtoDetail.LoadingPlanDetailID <= 0)
                        {
                            dbDetail = new LoadingPlanDetail();
                            dbItem.LoadingPlanDetail.Add(dbDetail);
                        }
                        else
                        {
                            dbDetail = dbItem.LoadingPlanDetail.FirstOrDefault(o => o.LoadingPlanDetailID == dtoDetail.LoadingPlanDetailID);
                        }

                        if (dbDetail != null)
                        {
                            AutoMapper.Mapper.Map <DTO.LoadingPlanDetail, LoadingPlanDetail>(dtoDetail, dbDetail);
                        }
                    }
                }

                // map sparepart detail
                if (dtoItem.LoadingPlanSparepartDetails != null)
                {
                    // check for child rows deleted
                    foreach (LoadingPlanSparepartDetail dbSparepartDetail in dbItem.LoadingPlanSparepartDetail.ToArray())
                    {
                        if (!dtoItem.LoadingPlanSparepartDetails.Select(o => o.LoadingPlanSparepartDetailID).Contains(dbSparepartDetail.LoadingPlanSparepartDetailID))
                        {
                            dbItem.LoadingPlanSparepartDetail.Remove(dbSparepartDetail);
                        }
                    }

                    // map child rows
                    foreach (DTO.LoadingPlanSparepartDetail dtoSparepartDetail in dtoItem.LoadingPlanSparepartDetails)
                    {
                        LoadingPlanSparepartDetail dbSparepartDetail;
                        if (dtoSparepartDetail.LoadingPlanSparepartDetailID <= 0)
                        {
                            dbSparepartDetail = new LoadingPlanSparepartDetail();
                            dbItem.LoadingPlanSparepartDetail.Add(dbSparepartDetail);
                        }
                        else
                        {
                            dbSparepartDetail = dbItem.LoadingPlanSparepartDetail.FirstOrDefault(o => o.LoadingPlanSparepartDetailID == dtoSparepartDetail.LoadingPlanSparepartDetailID);
                        }

                        if (dbSparepartDetail != null)
                        {
                            AutoMapper.Mapper.Map <DTO.LoadingPlanSparepartDetail, LoadingPlanSparepartDetail>(dtoSparepartDetail, dbSparepartDetail);
                        }
                    }
                }
            }
        }
        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);
            }
        }