Esempio n. 1
0
        public override bool UpdateData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification)
        {
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            DTO.FactoryOrderNorm dtoFactoryOrderNorm = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.FactoryOrderNorm>();
            try
            {
                using (FactoryOrderNormEntities context = CreateContext())
                {
                    FactoryOrderNorm dbItem = null;
                    if (id > 0)
                    {
                        dbItem = context.FactoryOrderNorm.Where(o => o.FactoryOrderNormID == id).FirstOrDefault();
                    }
                    else
                    {
                        dbItem = new FactoryOrderNorm();
                        context.FactoryOrderNorm.Add(dbItem);
                    }

                    if (dbItem == null)
                    {
                        notification.Message = "data not found!";
                        return(false);
                    }
                    else
                    {
                        //convert dto to db
                        converter.DTO2DB_FactoryOrderNorm(dtoFactoryOrderNorm, ref dbItem);
                        //remove orphan item
                        context.FactoryMaterialOrderNorm.Local.Where(o => o.FactoryFinishedProductOrderNorm == null).ToList().ForEach(o => context.FactoryMaterialOrderNorm.Remove(o));
                        context.FactoryFinishedProductOrderNorm.Local.Where(o => o.FactoryOrderNorm == null).ToList().ForEach(o => context.FactoryFinishedProductOrderNorm.Remove(o));
                        //save data
                        context.SaveChanges();
                        //get return data
                        dtoItem = GetData(dbItem.FactoryOrderNormID, out notification).Data;
                        return(true);
                    }
                }
            }
            catch (Exception ex)
            {
                notification.Type    = Library.DTO.NotificationType.Error;
                notification.Message = ex.Message;
                notification.DetailMessage.Add(ex.Message);
                if (ex.GetBaseException() != null)
                {
                    notification.DetailMessage.Add(ex.GetBaseException().Message);
                }
                return(false);
            }
        }
Esempio n. 2
0
        //public DTO.EditFormData GetEditFormData(int id, int? clientID, int? productID, out Library.DTO.Notification notification)
        //{
        //    DTO.EditFormData editFormData = new DTO.EditFormData();
        //    notification = new Library.DTO.Notification() { Type = Library.DTO.NotificationType.Success };
        //    Module.Support.DAL.DataFactory support_factory = new Support.DAL.DataFactory();
        //    try
        //    {
        //        using (FactoryOrderNormEntities context = CreateContext())
        //        {
        //            if (id > 0)
        //            {
        //                FactoryOrderNormMng_FactoryOrderNorm_View dbItem;
        //                dbItem = context.FactoryOrderNormMng_FactoryOrderNorm_View.FirstOrDefault(o => o.FactoryOrderNormID == id);
        //                editFormData.Data = converter.DB2DTO_FactoryOrderNorm(dbItem);
        //            }
        //            else
        //            {
        //                //get info client and product
        //                var client = context.FactoryOrderNormMng_Client_View.Where(o => o.ClientID == clientID).FirstOrDefault();
        //                var clientProduct = context.FactoryOrderNormMng_ClientProduct_View.Where(o => o.ClientID == clientID && o.ProductID == productID).FirstOrDefault();

        //                var product = context.FactoryOrderNormMng_Product_View.Where(o => o.ProductID == productID).FirstOrDefault();
        //                int? modelID = product.ModelID;
        //                var defaultNorm = context.FactoryOrderNormMng_FactoryFinishedProductNorm_View.Where(o => o.ModelID == modelID).ToList();

        //                //init edit form info
        //                editFormData.Data = new DTO.FactoryOrderNorm();
        //                editFormData.Data.Season = Library.Helper.GetCurrentSeason();
        //                editFormData.Data.ClientID = clientID;
        //                editFormData.Data.ProductID = productID;
        //                editFormData.Data.ClientUD = client.ClientUD;
        //                editFormData.Data.ArticleCode = clientProduct.ArticleCode;
        //                editFormData.Data.Description = clientProduct.Description;
        //                editFormData.Data.ModelUD = product.ModelUD;
        //                editFormData.Data.ModelNM = product.ModelNM;

        //                //init default material norm
        //                editFormData.Data.FactoryFinishedProductOrderNorms = new List<DTO.FactoryFinishedProductOrderNorm>();
        //                DTO.FactoryFinishedProductOrderNorm finishedProductNorm;
        //                DTO.FactoryMaterialOrderNorm materialNorm;

        //                int i = -1;
        //                foreach (var item in defaultNorm)
        //                {
        //                    finishedProductNorm = new DTO.FactoryFinishedProductOrderNorm();
        //                    finishedProductNorm.FactoryMaterialOrderNorms = new List<DTO.FactoryMaterialOrderNorm>();

        //                    finishedProductNorm.FactoryFinishedProductID = item.FactoryFinishedProductID;
        //                    finishedProductNorm.NormValue = item.NormValue;
        //                    finishedProductNorm.UnitID = item.UnitID;
        //                    finishedProductNorm.FactoryFinishedProductUD = item.FactoryFinishedProductUD;
        //                    finishedProductNorm.FactoryFinishedProductNM = item.FactoryFinishedProductNM;
        //                    finishedProductNorm.IsEditing = false;

        //                    foreach (var mItem in item.FactoryOrderNormMng_FactoryMaterialNorm_View)
        //                    {
        //                        materialNorm = new DTO.FactoryMaterialOrderNorm();
        //                        materialNorm.FactoryMaterialID = mItem.FactoryMaterialID;
        //                        materialNorm.NormValue = mItem.NormValue;
        //                        materialNorm.UnitID = mItem.UnitID;
        //                        materialNorm.FactoryMaterialUD = mItem.FactoryMaterialUD;
        //                        materialNorm.FactoryMaterialNM = mItem.FactoryMaterialNM;
        //                        materialNorm.UnitNM = mItem.UnitNM;
        //                        materialNorm.IsEditing = true;
        //                        finishedProductNorm.FactoryMaterialOrderNorms.Add(materialNorm);
        //                    }
        //                    editFormData.Data.FactoryFinishedProductOrderNorms.Add(finishedProductNorm);
        //                    i--;
        //                }
        //            }
        //            //get support list
        //            editFormData.Units = support_factory.GetUnit(1);
        //            editFormData.Seasons = support_factory.GetSeason();
        //            //editFormData.FactoryGoodsProcedures = support_factory.GetFactoryGoodsProcedure();
        //            return editFormData;
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        notification.Type = Library.DTO.NotificationType.Error;
        //        notification.Message = ex.Message;
        //        notification.DetailMessage.Add(ex.Message);
        //        if (ex.GetBaseException() != null)
        //        {
        //            notification.DetailMessage.Add(ex.GetBaseException().Message);
        //        }
        //        return editFormData;
        //    }
        //}

        public int CreateOrderNorm(string season, int?clientID, int?productID, out Library.DTO.Notification notification)
        {
            DTO.EditFormData editFormData = new DTO.EditFormData();
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success, Message = "Create BOM Of Order success"
            };
            try
            {
                if (string.IsNullOrEmpty(season) || season == "null")
                {
                    throw new Exception("Season is empty. You have to select season");
                }
                if (!clientID.HasValue)
                {
                    throw new Exception("Client is empty. You have to select client");
                }
                if (!productID.HasValue)
                {
                    throw new Exception("Product is empty. You have to select product");
                }
                using (FactoryOrderNormEntities context = CreateContext())
                {
                    var product     = context.FactoryOrderNormMng_Product_View.Where(o => o.ProductID == productID).FirstOrDefault();
                    int?modelID     = product.ModelID;
                    var defaultNorm = context.FactoryOrderNormMng_FactoryFinishedProductNorm_View.Where(o => o.ModelID == modelID).ToList();

                    using (var dbContextTransaction = context.Database.BeginTransaction())
                    {
                        try
                        {
                            //db
                            FactoryOrderNorm orderNorm = new FactoryOrderNorm();
                            context.FactoryOrderNorm.Add(orderNorm);

                            //init edit form info
                            orderNorm.Season    = Library.Helper.GetCurrentSeason();
                            orderNorm.ClientID  = clientID;
                            orderNorm.ProductID = productID;

                            //init default material norm
                            FactoryFinishedProductOrderNorm finishedProductNorm;
                            FactoryMaterialOrderNorm        materialNorm;
                            FactoryFinishedProductOrderNormFactoryGoodsProcedure goodsProcedure;

                            List <DTO.TempComponentNorm> tempNormIds = new List <DTO.TempComponentNorm>();
                            foreach (var item in defaultNorm.Where(o => o.ParentNormID == null))
                            {
                                finishedProductNorm = new FactoryFinishedProductOrderNorm();
                                orderNorm.FactoryFinishedProductOrderNorm.Add(finishedProductNorm);

                                finishedProductNorm.FactoryFinishedProductID = item.FactoryFinishedProductID;
                                finishedProductNorm.NormValue           = item.NormValue;
                                finishedProductNorm.UnitID              = item.UnitID;
                                finishedProductNorm.MaterialGroupTypeID = item.MaterialGroupTypeID;

                                foreach (var mItem in item.FactoryOrderNormMng_FactoryMaterialNorm_View)
                                {
                                    materialNorm = new FactoryMaterialOrderNorm();
                                    finishedProductNorm.FactoryMaterialOrderNorm.Add(materialNorm);

                                    materialNorm.FactoryMaterialID = mItem.FactoryMaterialID;
                                    materialNorm.NormValue         = mItem.NormValue;
                                    materialNorm.UnitID            = mItem.UnitID;
                                }

                                foreach (var pItem in item.FactoryOrderNormMng_FactoryFinishedProductNormFactoryGoodsProcedure_View)
                                {
                                    goodsProcedure = new FactoryFinishedProductOrderNormFactoryGoodsProcedure();
                                    finishedProductNorm.FactoryFinishedProductOrderNormFactoryGoodsProcedure.Add(goodsProcedure);

                                    goodsProcedure.FactoryGoodsProcedureID = pItem.FactoryGoodsProcedureID;
                                    goodsProcedure.IsDefault = pItem.IsDefault;
                                }

                                context.SaveChanges();
                                tempNormIds.Add(new DTO.TempComponentNorm {
                                    FinishedProductNormID = item.FactoryFinishedProductNormID, FinishedProductOrderNormID = finishedProductNorm.FactoryFinishedProductOrderNormID
                                });
                            }

                            foreach (var item in defaultNorm.Where(o => o.ParentNormID != null))
                            {
                                finishedProductNorm = new FactoryFinishedProductOrderNorm();
                                orderNorm.FactoryFinishedProductOrderNorm.Add(finishedProductNorm);

                                finishedProductNorm.FactoryFinishedProductID = item.FactoryFinishedProductID;
                                finishedProductNorm.NormValue           = item.NormValue;
                                finishedProductNorm.UnitID              = item.UnitID;
                                finishedProductNorm.ParentNormID        = tempNormIds.Where(o => o.FinishedProductNormID == item.ParentNormID).FirstOrDefault().FinishedProductOrderNormID;
                                finishedProductNorm.MaterialGroupTypeID = item.MaterialGroupTypeID;

                                foreach (var mItem in item.FactoryOrderNormMng_FactoryMaterialNorm_View)
                                {
                                    materialNorm = new FactoryMaterialOrderNorm();
                                    finishedProductNorm.FactoryMaterialOrderNorm.Add(materialNorm);

                                    materialNorm.FactoryMaterialID = mItem.FactoryMaterialID;
                                    materialNorm.NormValue         = mItem.NormValue;
                                    materialNorm.UnitID            = mItem.UnitID;
                                }

                                foreach (var pItem in item.FactoryOrderNormMng_FactoryFinishedProductNormFactoryGoodsProcedure_View)
                                {
                                    goodsProcedure = new FactoryFinishedProductOrderNormFactoryGoodsProcedure();
                                    finishedProductNorm.FactoryFinishedProductOrderNormFactoryGoodsProcedure.Add(goodsProcedure);

                                    goodsProcedure.FactoryGoodsProcedureID = pItem.FactoryGoodsProcedureID;
                                    goodsProcedure.IsDefault = pItem.IsDefault;
                                }
                            }
                            context.SaveChanges();
                            dbContextTransaction.Commit();
                            return(orderNorm.FactoryOrderNormID);
                        }
                        catch (Exception ex)
                        {
                            dbContextTransaction.Rollback();
                            throw new Exception(ex.ToString());
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                notification.Type    = Library.DTO.NotificationType.Error;
                notification.Message = ex.Message;
                notification.DetailMessage.Add(ex.Message);
                if (ex.GetBaseException() != null)
                {
                    notification.DetailMessage.Add(ex.GetBaseException().Message);
                }
                return(-1);
            }
        }
Esempio n. 3
0
 public void DTO2DB_FactoryOrderNorm(DTO.FactoryOrderNorm dtoItem, ref FactoryOrderNorm dbItem)
 {
     //if (dtoItem.FactoryFinishedProductOrderNorms != null)
     //{
     //    //user remove factory finished product in dto => check to remove in db
     //    foreach (var item in dbItem.FactoryFinishedProductOrderNorm.ToArray())
     //    {
     //        if (!dtoItem.FactoryFinishedProductOrderNorms.Select(s => s.FactoryFinishedProductOrderNormID).Contains(item.FactoryFinishedProductOrderNormID))
     //        {
     //            foreach (var mItem in item.FactoryMaterialOrderNorm.ToArray())
     //            {
     //                item.FactoryMaterialOrderNorm.Remove(mItem);
     //            }
     //            dbItem.FactoryFinishedProductOrderNorm.Remove(item);
     //        }
     //        else
     //        {
     //            //user remove factory material in dto => check to remove in db
     //            foreach (var mItem in item.FactoryMaterialOrderNorm.ToArray())
     //            {
     //                var x = dtoItem.FactoryFinishedProductOrderNorms.Where(o => o.FactoryFinishedProductOrderNormID == item.FactoryFinishedProductOrderNormID).FirstOrDefault().FactoryMaterialOrderNorms;
     //                if (!x.Select(s => s.FactoryMaterialOrderNormID).Contains(mItem.FactoryMaterialOrderNormID))
     //                {
     //                    item.FactoryMaterialOrderNorm.Remove(mItem);
     //                }
     //            }
     //        }
     //    }
     //    //read factory finished product
     //    FactoryFinishedProductOrderNorm dbFinishedProductNorm;
     //    FactoryMaterialOrderNorm dbMaterialNorm;
     //    foreach (var item in dtoItem.FactoryFinishedProductOrderNorms)
     //    {
     //        if (item.FactoryFinishedProductOrderNormID > 0)
     //        {
     //            dbFinishedProductNorm = dbItem.FactoryFinishedProductOrderNorm.Where(o => o.FactoryFinishedProductOrderNormID == item.FactoryFinishedProductOrderNormID).FirstOrDefault();
     //            if (dbFinishedProductNorm != null && item.FactoryMaterialOrderNorms != null)
     //            {
     //                foreach (var mItem in item.FactoryMaterialOrderNorms)
     //                {
     //                    if (mItem.FactoryMaterialOrderNormID > 0)
     //                    {
     //                        dbMaterialNorm = dbFinishedProductNorm.FactoryMaterialOrderNorm.Where(o => o.FactoryMaterialOrderNormID == mItem.FactoryMaterialOrderNormID).FirstOrDefault();
     //                    }
     //                    else
     //                    {
     //                        dbMaterialNorm = new FactoryMaterialOrderNorm();
     //                        dbFinishedProductNorm.FactoryMaterialOrderNorm.Add(dbMaterialNorm);
     //                    }
     //                    if (dbMaterialNorm != null)
     //                    {
     //                        AutoMapper.Mapper.Map<DTO.FactoryMaterialOrderNorm, FactoryMaterialOrderNorm>(mItem, dbMaterialNorm);
     //                    }
     //                }
     //            }
     //        }
     //        else
     //        {
     //            dbFinishedProductNorm = new FactoryFinishedProductOrderNorm();
     //            dbItem.FactoryFinishedProductOrderNorm.Add(dbFinishedProductNorm);
     //            if (item.FactoryMaterialOrderNorms != null)
     //            {
     //                foreach (var mItem in item.FactoryMaterialOrderNorms)
     //                {
     //                    dbMaterialNorm = new FactoryMaterialOrderNorm();
     //                    dbFinishedProductNorm.FactoryMaterialOrderNorm.Add(dbMaterialNorm);
     //                    AutoMapper.Mapper.Map<DTO.FactoryMaterialOrderNorm, FactoryMaterialOrderNorm>(mItem, dbMaterialNorm);
     //                }
     //            }
     //        }
     //        //mapping
     //        if (dbFinishedProductNorm != null)
     //        {
     //            AutoMapper.Mapper.Map<DTO.FactoryFinishedProductOrderNorm, FactoryFinishedProductOrderNorm>(item, dbFinishedProductNorm);
     //        }
     //    }
     //}
     AutoMapper.Mapper.Map <DTO.FactoryOrderNorm, FactoryOrderNorm>(dtoItem, dbItem);
 }