Example #1
0
        public override bool UpdateData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification)
        {
            DTO.ProductionItem dtoProductionItem = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.ProductionItem>();
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            string countryCode = null;
            int    number;
            string indexName;
            bool   permissionApprove = false;

            if (fwBll.CanPerformAction(userId, moduleCode, Library.DTO.ModuleAction.CanApprove))
            {
                permissionApprove = true;
            }
            try
            {
                using (ProductionItemEntities context = CreateContext())
                {
                    ProductionItem dbItem = null;
                    if (id == 0)
                    {
                        dbItem = new ProductionItem();
                        context.ProductionItem.Add(dbItem);
                        // Auto generate code
                        switch (dtoProductionItem.CountryID)
                        {
                        case 1:
                            countryCode = "VN";
                            break;

                        case 2:
                            countryCode = "IN";
                            break;

                        case 3:
                            countryCode = "HL";
                            break;
                        }

                        if (dtoProductionItem.ProductionItemUD == null)
                        {
                            dtoProductionItem.ProductionItemUD = context.ProductionItemMng_function_GenerateItemCode(dtoProductionItem.CountryID, countryCode, dtoProductionItem.ProductionItemGroupID, dtoProductionItem.ProductionItemMaterialTypeID, dtoProductionItem.ProductionItemTypeID).FirstOrDefault();

                            // Check length of production item code enough 12 characters
                            if (dtoProductionItem.ProductionItemUD.Length < 12)
                            {
                                notification.Type    = Library.DTO.NotificationType.Error;
                                notification.Message = "Create Production Item is error. Please connect to Mr. Hanh to help!";

                                return(false);
                            }
                        }
                    }
                    else
                    {
                        dbItem = context.ProductionItem.FirstOrDefault(o => o.ProductionItemID == id);
                    }

                    if (dbItem == null)
                    {
                        notification.Message = "Production Item not found!";
                        return(false);
                    }
                    else
                    {
                        // processing image
                        if (dtoProductionItem.FileLocation_HasChange)
                        {
                            dtoProductionItem.ProductImage = fwFactory.CreateFilePointer(FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\", dtoProductionItem.FileLocation_NewFile, dtoProductionItem.ProductImage);
                        }

                        //only one Defaut warehouse accepted
                        int c = dtoProductionItem.ProductionItemWarehouses.Where(o => o.IsDefault.Equals(true)).Count();
                        if (c == 0 && dtoProductionItem.ProductionItemTypeID != 7)
                        {
                            throw new System.Data.DataException("Please select at leat one default warehouse!");
                        }
                        //if (c > 1)
                        //{
                        //    throw new System.Data.DataException("Only one warehouse can be a default!");
                        //}

                        //only one Defaut vender accepted
                        int v = dtoProductionItem.ProductionItemVenders.Where(o => o.IsDefault.Equals(true)).Count();
                        if (v == 0)
                        {
                            //throw new System.Data.DataException("Please select at leat one default vender!");
                        }
                        if (v > 1)
                        {
                            throw new System.Data.DataException("Only one vender can be default option");
                        }

                        foreach (var item in dtoProductionItem.productionItemSubUnitDTOs)
                        {
                            if (!item.ConversionFactor.HasValue)
                            {
                                throw new Exception("You need fill-in conversion factory for unit");
                            }
                            if (item.ValidFrom == null || item.ValidFrom.Equals(""))
                            {
                                throw new Exception("You need fill-in validDate for sub unit");
                            }

                            if (item.ProductionItemUnitID > 0)
                            {
                                if (permissionApprove)
                                {
                                    var    getInforItem = context.ProductionItemUnit.FirstOrDefault(o => o.ProductionItemUnitID == item.ProductionItemUnitID);
                                    string validDateStr = getInforItem.ValidFrom != null?getInforItem.ValidFrom.Value.ToString("dd/MM/yyyy") : "";

                                    if (item.ConversionFactor != getInforItem.ConversionFactor || item.ValidFrom != validDateStr)
                                    {
                                        ProductionItemUnitHistory productionItemUnitHistory;
                                        productionItemUnitHistory = new ProductionItemUnitHistory();
                                        //Get data;
                                        ProductionItemUnit xxx = dbItem.ProductionItemUnit.FirstOrDefault(o => o.ProductionItemUnitID == item.ProductionItemUnitID);
                                        xxx.ProductionItemUnitHistory.Add(productionItemUnitHistory);
                                        //Mapping Data
                                        productionItemUnitHistory.Remark           = getInforItem.Remark != null ?getInforItem.Remark : item.Remark;
                                        productionItemUnitHistory.UpdateDate       = DateTime.Now;
                                        productionItemUnitHistory.UpdatedBy        = userId;
                                        productionItemUnitHistory.HsqdHistory      = getInforItem.ConversionFactor != null ? getInforItem.ConversionFactor : item.ConversionFactor;
                                        productionItemUnitHistory.ValidFromHistory = getInforItem.ValidFrom != null ? getInforItem.ValidFrom : item.ValidFrom.ConvertStringToDateTime();
                                    }
                                }
                            }
                        }

                        converter.DTO2BD(dtoProductionItem, ref dbItem, permissionApprove, userId);

                        //update hidden data
                        int?companyID = fwFactory.GetCompanyID(userId);
                        dbItem.CompanyID = companyID;

                        // ProductionItemWarehouse
                        context.ProductionItemWarehouse.Local.Where(o => o.ProductionItem == null).ToList().ForEach(o => context.ProductionItemWarehouse.Remove(o));
                        // ProductionItemWarehouse
                        context.ProductionItemVender.Local.Where(o => o.ProductionItem == null).ToList().ForEach(o => context.ProductionItemVender.Remove(o));
                        //ProductionItemUnit
                        context.ProductionItemUnit.Local.Where(o => o.ProductionItem == null).ToList().ForEach(o => context.ProductionItemUnit.Remove(o));

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

                        // Update by hand. (unneccessary)
                        dbItem.CountryID = dtoProductionItem.CountryID;
                        dbItem.ProductionItemMaterialTypeID = dtoProductionItem.ProductionItemMaterialTypeID;
                        dbItem.ProductionItemTypeID         = dtoProductionItem.ProductionItemTypeID;

                        context.SaveChanges();

                        dtoItem = GetData(userId, dbItem.ProductionItemID, out notification).Data;

                        return(true);
                    }
                }
            }

            catch (System.Data.DataException dEx)
            {
                notification.Type = Library.DTO.NotificationType.Error;
                Library.ErrorHelper.DataExceptionParser(dEx, out number, out indexName);
                if (number == 2601 && !string.IsNullOrEmpty(indexName))
                {
                    switch (indexName)
                    {
                    case "ProductItemFactoryWarehouseUnique":
                        notification.Message = "Duplicate Factory Warehouse ";
                        break;

                    case "IX_ProductionItemVender_FactoryRawMaterial":
                        notification.Message = "Duplicate Vender";
                        break;
                    }
                }
                else
                {
                    notification.Message = dEx.Message;
                }
                return(false);
            }
        }
        public void DTO2BD(DTO.ProductionItem dtoItem, ref ProductionItem dbItem, bool permissionApprove, int userId)
        {
            // ProductionItemWarehouse
            if (dtoItem.ProductionItemWarehouses != null)
            {
                foreach (var item in dbItem.ProductionItemWarehouse.ToArray())
                {
                    if (!dtoItem.ProductionItemWarehouses.Select(o => o.ProductionItemWarehouseID).Contains(item.ProductionItemWarehouseID))
                    {
                        dbItem.ProductionItemWarehouse.Remove(item);
                    }
                }

                //map child row
                foreach (var item in dtoItem.ProductionItemWarehouses)
                {
                    ProductionItemWarehouse dbProductionItemWarehouse;
                    if (item.ProductionItemWarehouseID <= 0)
                    {
                        dbProductionItemWarehouse = new ProductionItemWarehouse();
                        dbItem.ProductionItemWarehouse.Add(dbProductionItemWarehouse);
                    }
                    else
                    {
                        dbProductionItemWarehouse = dbItem.ProductionItemWarehouse.FirstOrDefault(o => o.ProductionItemWarehouseID == item.ProductionItemWarehouseID);
                    }
                    if (dbProductionItemWarehouse != null)
                    {
                        AutoMapper.Mapper.Map <DTO.ProductionItemWarehouse, ProductionItemWarehouse>(item, dbProductionItemWarehouse);
                    }
                }
            }

            // ProductionItemVender
            if (dtoItem.ProductionItemVenders != null)
            {
                foreach (var item in dbItem.ProductionItemVender.ToArray())
                {
                    if (!dtoItem.ProductionItemVenders.Select(o => o.ProductionItemVenderID).Contains(item.ProductionItemVenderID))
                    {
                        dbItem.ProductionItemVender.Remove(item);
                    }
                }

                //map child row
                foreach (var item in dtoItem.ProductionItemVenders)
                {
                    ProductionItemVender dbProductionItemVender;
                    if (item.ProductionItemVenderID <= 0)
                    {
                        dbProductionItemVender = new ProductionItemVender();
                        dbItem.ProductionItemVender.Add(dbProductionItemVender);
                    }
                    else
                    {
                        dbProductionItemVender = dbItem.ProductionItemVender.FirstOrDefault(o => o.ProductionItemVenderID == item.ProductionItemVenderID);
                    }
                    if (dbProductionItemVender != null)
                    {
                        AutoMapper.Mapper.Map <DTO.ProductionItemVender, ProductionItemVender>(item, dbProductionItemVender);
                    }
                }
            }

            //ProductionItemSubUnit
            if (dtoItem.productionItemSubUnitDTOs != null)
            {
                foreach (var item in dbItem.ProductionItemUnit.ToArray())
                {
                    if (!dtoItem.productionItemSubUnitDTOs.Select(o => o.ProductionItemUnitID).Contains(item.ProductionItemUnitID))
                    {
                        dbItem.ProductionItemUnit.Remove(item);
                    }
                }

                //map child row
                foreach (var item in dtoItem.productionItemSubUnitDTOs)
                {
                    ProductionItemUnit dbproductionItemUnit;
                    if (item.ProductionItemUnitID <= 0)
                    {
                        dbproductionItemUnit = new ProductionItemUnit();
                        dbItem.ProductionItemUnit.Add(dbproductionItemUnit);
                    }
                    else
                    {
                        dbproductionItemUnit = dbItem.ProductionItemUnit.FirstOrDefault(o => o.ProductionItemUnitID == item.ProductionItemUnitID);
                    }
                    if (dbproductionItemUnit != null)
                    {
                        if (item.ProductionItemUnitID < 0)
                        {
                            AutoMapper.Mapper.Map <DTO.ProductionItemSubUnitDTO, ProductionItemUnit>(item, dbproductionItemUnit);
                            dbproductionItemUnit.ValidFrom   = item.ValidFrom.ConvertStringToDateTime();
                            dbproductionItemUnit.UpdatedBy   = userId;
                            dbproductionItemUnit.UpdatedDate = DateTime.Now;
                        }
                        else
                        {
                            if (permissionApprove)
                            {
                                var    checkItem    = dbItem.ProductionItemUnit.FirstOrDefault(o => o.ProductionItemUnitID == item.ProductionItemUnitID);
                                string ValidFromStr = checkItem.ValidFrom != null?checkItem.ValidFrom.Value.ToString("dd/MM/yyyy") : "";

                                if (checkItem.ConversionFactor != item.ConversionFactor || ValidFromStr != item.ValidFrom)
                                {
                                    AutoMapper.Mapper.Map <DTO.ProductionItemSubUnitDTO, ProductionItemUnit>(item, dbproductionItemUnit);
                                    dbproductionItemUnit.ValidFrom   = item.ValidFrom.ConvertStringToDateTime();
                                    dbproductionItemUnit.UpdatedBy   = userId;
                                    dbproductionItemUnit.UpdatedDate = DateTime.Now;
                                }
                            }
                            else
                            {
                                dbproductionItemUnit.Remark = item.Remark;
                            }
                        }
                    }
                }
            }
            //int? currentUnitID = dbItem.UnitID;
            AutoMapper.Mapper.Map <DTO.ProductionItem, ProductionItem>(dtoItem, dbItem);
            //if(dbItem.ProductionItemID > 0)
            //{
            //    dbItem.UnitID = currentUnitID;
            //}

            dbItem.UpdatedDate = dtoItem.UpdatedDate.ConvertStringToDateTime();
            dbItem.StartDate   = dtoItem.StartDate.ConvertStringToDateTime();
            dbItem.EndDate     = dtoItem.EndDate.ConvertStringToDateTime();
        }