示例#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.FactoryProductionStatusDTO dtoFactoryProductionStatus = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.FactoryProductionStatusDTO>();
            try
            {
                using (FactoryProductionStatusEntities context = CreateContext())
                {
                    FactoryProductionStatus dbItem = null;
                    if (id > 0)
                    {
                        dbItem = context.FactoryProductionStatus.Where(o => o.FactoryProductionStatusID == id).FirstOrDefault();
                        if (dbItem.WeekNo != dtoFactoryProductionStatus.WeekNo)
                        {
                            throw new Exception("You can not change week");
                        }
                    }
                    else
                    {
                        dbItem = new FactoryProductionStatus();
                        context.FactoryProductionStatus.Add(dbItem);
                    }
                    if (dbItem == null)
                    {
                        notification.Message = "data not found!";
                        return(false);
                    }
                    else
                    {
                        //calculate Produced Cont for every order
                        decimal totalCont = 0;
                        foreach (var item in dtoFactoryProductionStatus.FactoryProductionStatusDetailDTOs)
                        {
                            totalCont = 0;
                            foreach (var sItem in item.FactoryProductionStatusOrderDetailDTOs)
                            {
                                if (sItem.Qnt40HC.HasValue && sItem.Qnt40HC != 0 && sItem.ProducedQnt.HasValue)
                                {
                                    sItem.ProducedCont = Math.Round(Convert.ToDecimal(sItem.ProducedQnt.Value) / Convert.ToDecimal(sItem.Qnt40HC), 2);
                                    totalCont         += Math.Round(Convert.ToDecimal(sItem.ProducedQnt.Value) / Convert.ToDecimal(sItem.Qnt40HC), 2);
                                }
                            }
                            item.ProducedContainerQnt = totalCont;
                        }
                        //convert dto to db
                        converter.DTO2DB_FactoryProductionStatus(dtoFactoryProductionStatus, ref dbItem);
                        dbItem.UpdatedBy = userId;
                        //check permission on factory
                        if (fwFactory.CheckFactoryPermission(userId, dbItem.FactoryID.Value) == 0)
                        {
                            throw new Exception("You do not have access permission on this factory");
                        }
                        //check week
                        //int curentWeek = Library.Helper.GetCurrentWeek();
                        //if (curentWeek - dbItem.WeekNo.Value >= 2)
                        //{
                        //    throw new Exception("You can not update because this week is over 2 weeks compare with current week (" + curentWeek.ToString() + " )");
                        //}
                        //remove orphan
                        context.FactoryProductionStatusDetail.Local.Where(o => o.FactoryProductionStatus == null).ToList().ForEach(o => context.FactoryProductionStatusDetail.Remove(o));
                        //save data
                        context.SaveChanges();
                        //get return data
                        dtoItem = GetData(dbItem.FactoryProductionStatusID, out notification).Data;

                        //auto create production item
                        context.FactoryStockReceiptMng_function_CreateProductionItem();

                        //get total output procduce all week
                        //List<int> factoryOrderDetailIDs = new List<int>();
                        //foreach (var item in dbItem.FactoryProductionStatusDetail.ToList())
                        //{
                        //    foreach (var sItem in item.FactoryProductionStatusOrderDetail.Where(o =>o.FactoryOrderDetailID.HasValue).ToList())
                        //    {
                        //        factoryOrderDetailIDs.Add(sItem.FactoryOrderDetailID.Value);
                        //    }
                        //}
                        //var factoryOrderDetails = from item in context.FactoryProductionStatusOrderDetail.Where(o =>factoryOrderDetailIDs.Contains(o.FactoryOrderDetailID.Value)).ToList()
                        //                         group item by new { item.FactoryOrderDetailID } into g
                        //                         select new { g.Key.FactoryOrderDetailID, TotalOutputProduced = g.Sum(o => o.OutputProducedQnt) };
                        ////var factoryStock = context.FactoryStockReceiptDetail.Where(o => o.FactoryOrderDetailID.HasValue && factoryOrderDetails.Select(s => s.FactoryOrderDetailID).Contains(o.FactoryOrderDetailID));
                        //foreach (var item in context.FactoryStockReceiptDetail.Where(o => o.FactoryOrderDetailID.HasValue && factoryOrderDetailIDs.Contains(o.FactoryOrderDetailID.Value)))
                        //{
                        //    var x = factoryOrderDetails.Where(o => o.FactoryOrderDetailID == item.FactoryOrderDetailID).FirstOrDefault();
                        //    if (x != null)
                        //    {
                        //        item.ProducedQnt = x.TotalOutputProduced;
                        //    }
                        //}
                        //context.SaveChanges();
                        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);
            }
        }
示例#2
0
        public void DTO2DB_FactoryProductionStatus(DTO.FactoryProductionStatusDTO dtoItem, ref FactoryProductionStatus dbItem)
        {
            if (dtoItem.FactoryProductionStatusDetailDTOs != null)
            {
                //delete
                foreach (var item in dbItem.FactoryProductionStatusDetail.ToArray())
                {
                    if (!dtoItem.FactoryProductionStatusDetailDTOs.Select(s => s.FactoryProductionStatusDetailID).Contains(item.FactoryProductionStatusDetailID))
                    {
                        foreach (var sItem in item.FactoryProductionStatusOrderDetail.ToArray())
                        {
                            item.FactoryProductionStatusOrderDetail.Remove(sItem);
                        }
                        dbItem.FactoryProductionStatusDetail.Remove(item);
                    }
                    else
                    {
                        foreach (var sItem in item.FactoryProductionStatusOrderDetail.ToArray())
                        {
                            var x = dtoItem.FactoryProductionStatusDetailDTOs.Where(o => o.FactoryProductionStatusDetailID == item.FactoryProductionStatusDetailID).FirstOrDefault().FactoryProductionStatusOrderDetailDTOs;
                            if (!x.Select(s => s.FactoryProductionStatusOrderDetailID).Contains(sItem.FactoryProductionStatusOrderDetailID))
                            {
                                item.FactoryProductionStatusOrderDetail.Remove(sItem);
                            }
                        }
                    }
                }

                //update
                FactoryProductionStatusDetail      dbDetail;
                FactoryProductionStatusOrderDetail dbOrderDetail;
                foreach (var item in dtoItem.FactoryProductionStatusDetailDTOs)
                {
                    if (item.FactoryProductionStatusDetailID < 0)
                    {
                        dbDetail = new FactoryProductionStatusDetail();
                        dbItem.FactoryProductionStatusDetail.Add(dbDetail);
                        if (item.FactoryProductionStatusOrderDetailDTOs != null)
                        {
                            foreach (var sItem in item.FactoryProductionStatusOrderDetailDTOs)
                            {
                                dbOrderDetail = new FactoryProductionStatusOrderDetail();
                                dbDetail.FactoryProductionStatusOrderDetail.Add(dbOrderDetail);
                                AutoMapper.Mapper.Map <DTO.FactoryProductionStatusOrderDetailDTO, FactoryProductionStatusOrderDetail>(sItem, dbOrderDetail);
                            }
                        }
                    }
                    else
                    {
                        dbDetail = dbItem.FactoryProductionStatusDetail.Where(o => o.FactoryProductionStatusDetailID == item.FactoryProductionStatusDetailID).FirstOrDefault();
                        if (dbDetail != null && item.FactoryProductionStatusOrderDetailDTOs != null)
                        {
                            foreach (var sItem in item.FactoryProductionStatusOrderDetailDTOs)
                            {
                                if (sItem.FactoryProductionStatusOrderDetailID > 0)
                                {
                                    dbOrderDetail = dbDetail.FactoryProductionStatusOrderDetail.Where(o => o.FactoryProductionStatusOrderDetailID == sItem.FactoryProductionStatusOrderDetailID).FirstOrDefault();
                                }
                                else
                                {
                                    dbOrderDetail = new FactoryProductionStatusOrderDetail();
                                    dbDetail.FactoryProductionStatusOrderDetail.Add(dbOrderDetail);
                                }
                                if (dbOrderDetail != null)
                                {
                                    AutoMapper.Mapper.Map <DTO.FactoryProductionStatusOrderDetailDTO, FactoryProductionStatusOrderDetail>(sItem, dbOrderDetail);
                                }
                            }
                        }
                    }
                    if (dbDetail != null)
                    {
                        AutoMapper.Mapper.Map <DTO.FactoryProductionStatusDetailDTO, FactoryProductionStatusDetail>(item, dbDetail);
                    }
                }
            }
            AutoMapper.Mapper.Map <DTO.FactoryProductionStatusDTO, FactoryProductionStatus>(dtoItem, dbItem);
            dbItem.UpdatedDate = DateTime.Now;
        }