Пример #1
0
        public void DTO2DB_PurchaseRequest(DTO.PurchaseRequestDTO dtoItem, ref PurchaseRequest dbItem)
        {
            if (dtoItem.PurchaseRequestDetailDTOs != null)
            {
                //delete item in db that exist in dto but not exist in db
                foreach (var item in dbItem.PurchaseRequestDetail.ToArray())
                {
                    if (!dtoItem.PurchaseRequestDetailDTOs.Select(s => s.PurchaseRequestDetailID).Contains(item.PurchaseRequestDetailID))
                    {
                        //delete purchase request workorder item
                        foreach (var wItem in item.PurchaseRequestWorkOrderDetail.ToArray())
                        {
                            item.PurchaseRequestWorkOrderDetail.Remove(wItem);
                        }

                        //delete purchase request detail
                        dbItem.PurchaseRequestDetail.Remove(item);
                    }
                    else
                    {
                        var x = dtoItem.PurchaseRequestDetailDTOs.Where(o => o.PurchaseRequestDetailID == item.PurchaseRequestDetailID).FirstOrDefault().PurchaseRequestWorkOrderDetailDTOs;

                        //delete purchase request detail
                        foreach (var wItem in item.PurchaseRequestWorkOrderDetail.ToArray())
                        {
                            if (!x.Select(s => s.PurchaseRequestWorkOrderDetailID).Contains(wItem.PurchaseRequestWorkOrderDetailID))
                            {
                                item.PurchaseRequestWorkOrderDetail.Remove(wItem);
                            }
                        }
                    }
                }
                //read from dto to db
                PurchaseRequestDetail          dbPurchaseDetail;
                PurchaseRequestWorkOrderDetail dbPurchaseWorkOrderDetail;
                foreach (var item in dtoItem.PurchaseRequestDetailDTOs)
                {
                    if (item.PurchaseRequestDetailID > 0)
                    {
                        dbPurchaseDetail = dbItem.PurchaseRequestDetail.Where(o => o.PurchaseRequestDetailID == item.PurchaseRequestDetailID).FirstOrDefault();

                        //read from purchase request workorder detail dto to db
                        if (dbPurchaseDetail != null && item.PurchaseRequestWorkOrderDetailDTOs != null)
                        {
                            foreach (var wItem in item.PurchaseRequestWorkOrderDetailDTOs)
                            {
                                if (wItem.PurchaseRequestWorkOrderDetailID > 0)
                                {
                                    dbPurchaseWorkOrderDetail = dbPurchaseDetail.PurchaseRequestWorkOrderDetail.Where(o => o.PurchaseRequestWorkOrderDetailID == wItem.PurchaseRequestWorkOrderDetailID).FirstOrDefault();
                                }
                                else
                                {
                                    dbPurchaseWorkOrderDetail = new PurchaseRequestWorkOrderDetail();
                                    dbPurchaseDetail.PurchaseRequestWorkOrderDetail.Add(dbPurchaseWorkOrderDetail);
                                }
                                if (dbPurchaseWorkOrderDetail != null)
                                {
                                    AutoMapper.Mapper.Map <DTO.PurchaseRequestWorkOrderDetailDTO, PurchaseRequestWorkOrderDetail>(wItem, dbPurchaseWorkOrderDetail);
                                }
                            }
                        }
                    }
                    else
                    {
                        if (item.ProductionItemID == null || item.RequestQnt == null)
                        {
                            continue;
                        }

                        dbPurchaseDetail = new PurchaseRequestDetail();
                        dbItem.PurchaseRequestDetail.Add(dbPurchaseDetail);

                        //read from purchase request workorder detail dto to db
                        if (item.PurchaseRequestWorkOrderDetailDTOs != null)
                        {
                            foreach (var mItem in item.PurchaseRequestWorkOrderDetailDTOs)
                            {
                                dbPurchaseWorkOrderDetail = new PurchaseRequestWorkOrderDetail();
                                dbPurchaseDetail.PurchaseRequestWorkOrderDetail.Add(dbPurchaseWorkOrderDetail);
                                AutoMapper.Mapper.Map <DTO.PurchaseRequestWorkOrderDetailDTO, PurchaseRequestWorkOrderDetail>(mItem, dbPurchaseWorkOrderDetail);
                            }
                        }
                    }

                    //read purchase request detail dto to db
                    if (dbPurchaseDetail != null)
                    {
                        AutoMapper.Mapper.Map <DTO.PurchaseRequestDetailDTO, PurchaseRequestDetail>(item, dbPurchaseDetail);
                        dbPurchaseDetail.ETA = item.ETA.ConvertStringToDateTime();
                    }
                }
            }
            AutoMapper.Mapper.Map <DTO.PurchaseRequestDTO, PurchaseRequest>(dtoItem, dbItem);
            dbItem.PurchaseRequestDate = dtoItem.PurchaseRequestDate.ConvertStringToDateTime();
            dbItem.ETA = dtoItem.ETA.ConvertStringToDateTime();
        }
Пример #2
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.PurchaseRequestDTO dtoPurchaseRequest = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.PurchaseRequestDTO>();
            try
            {
                //get companyID
                Module.Framework.DAL.DataFactory fw_factory = new Framework.DAL.DataFactory();
                int?companyID = fw_factory.GetCompanyID(userId);
                using (PurchaseRequestMngEntities context = CreateContext())
                {
                    PurchaseRequest dbItem = null;
                    if (id == 0)
                    {
                        dbItem = new PurchaseRequest();
                        context.PurchaseRequest.Add(dbItem);
                    }
                    else
                    {
                        dbItem = context.PurchaseRequest.Where(o => o.PurchaseRequestID == id).FirstOrDefault();
                    }
                    if (dbItem == null)
                    {
                        notification.Message = "data not found!";
                        return(false);
                    }
                    else
                    {
                        //convert dto to db
                        converter.DTO2DB_PurchaseRequest(dtoPurchaseRequest, ref dbItem);
                        dbItem.CompanyID   = companyID;
                        dbItem.UpdatedBy   = userId;
                        dbItem.UpdatedDate = DateTime.Now;

                        if (id == 0)
                        {
                            dbItem.PurchaseRequestUD = context.PurchaseRequestMng_function_GeneratePurchaseRequestUD().FirstOrDefault();
                            dbItem.CreatedBy         = userId;
                            dbItem.CreatedDate       = DateTime.Now;
                        }

                        //remove orphan
                        context.PurchaseRequestWorkOrderDetail.Local.Where(o => o.PurchaseRequestDetail == null).ToList().ForEach(o => context.PurchaseRequestWorkOrderDetail.Remove(o));
                        context.PurchaseRequestDetail.Local.Where(o => o.PurchaseRequest == null).ToList().ForEach(o => context.PurchaseRequestDetail.Remove(o));
                        //save data
                        context.SaveChanges();
                        //get return data
                        dtoItem = GetData(dbItem.PurchaseRequestID, 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);
            }
        }