Exemplo n.º 1
0
        public void DTO2DB_QCReport(DTO.QCReportDTO dtoItem, ref QCReport dbItem)
        {
            //QCReportImage
            if (dtoItem.QCReportImageDTOs != null)
            {
                foreach (QCReportImage item in dbItem.QCReportImage.ToList())
                {
                    if (!dtoItem.QCReportImageDTOs.Select(s => s.QCReportImageID).Contains(item.QCReportImageID))
                    {
                        dbItem.QCReportImage.Remove(item);
                    }
                }

                foreach (DTO.QCReportImageDTO dto in dtoItem.QCReportImageDTOs)
                {
                    QCReportImage item;

                    if (dto.QCReportImageID < 0)
                    {
                        item = new QCReportImage();

                        dbItem.QCReportImage.Add(item);
                    }
                    else
                    {
                        item = dbItem.QCReportImage.FirstOrDefault(s => s.QCReportImageID == dto.QCReportImageID);
                    }

                    if (item != null)
                    {
                        AutoMapper.Mapper.Map <DTO.QCReportImageDTO, QCReportImage>(dto, item);
                    }
                }
            }

            //QCReportSummary
            if (dtoItem.QCReportSummaryDTOs != null)
            {
                foreach (QCReportSummary item in dbItem.QCReportSummary.ToList())
                {
                    if (!dtoItem.QCReportSummaryDTOs.Select(s => s.QCReportSummaryID).Contains(item.QCReportSummaryID))
                    {
                        dbItem.QCReportSummary.Remove(item);
                    }
                }

                foreach (DTO.QCReportSummaryDTO dto in dtoItem.QCReportSummaryDTOs)
                {
                    QCReportSummary item;

                    if (dto.QCReportSummaryID < 0)
                    {
                        item = new QCReportSummary();

                        dbItem.QCReportSummary.Add(item);
                    }
                    else
                    {
                        item = dbItem.QCReportSummary.FirstOrDefault(s => s.QCReportSummaryID == dto.QCReportSummaryID);
                    }

                    if (item != null)
                    {
                        AutoMapper.Mapper.Map <DTO.QCReportSummaryDTO, QCReportSummary>(dto, item);
                    }
                }
            }

            //QCReportDetail
            if (dtoItem.QCReportDetailDTOs != null)
            {
                foreach (QCReportDetail item in dbItem.QCReportDetail.ToList())
                {
                    if (!dtoItem.QCReportDetailDTOs.Select(s => s.QCReportDetailID).Contains(item.QCReportDetailID))
                    {
                        dbItem.QCReportDetail.Remove(item);
                    }
                }

                foreach (DTO.QCReportDetailDTO dto in dtoItem.QCReportDetailDTOs)
                {
                    QCReportDetail item;

                    if (dto.QCReportDetailID < 0)
                    {
                        item = new QCReportDetail();

                        dbItem.QCReportDetail.Add(item);
                    }
                    else
                    {
                        item = dbItem.QCReportDetail.FirstOrDefault(s => s.QCReportDetailID == dto.QCReportDetailID);
                    }

                    if (item != null)
                    {
                        AutoMapper.Mapper.Map <DTO.QCReportDetailDTO, QCReportDetail>(dto, item);
                    }
                }
            }

            //QCReportDefect
            if (dtoItem.QCReportDefectDTOs != null)
            {
                foreach (QCReportDefect item in dbItem.QCReportDefect.ToList())
                {
                    if (!dtoItem.QCReportDefectDTOs.Select(s => s.QCReportDefectID).Contains(item.QCReportDefectID))
                    {
                        dbItem.QCReportDefect.Remove(item);
                    }
                }

                foreach (DTO.QCReportDefectDTO dto in dtoItem.QCReportDefectDTOs)
                {
                    QCReportDefect item;

                    if (dto.QCReportDefectID < 0)
                    {
                        item = new QCReportDefect();

                        dbItem.QCReportDefect.Add(item);
                    }
                    else
                    {
                        item = dbItem.QCReportDefect.FirstOrDefault(s => s.QCReportDefectID == dto.QCReportDefectID);
                    }

                    if (item != null)
                    {
                        AutoMapper.Mapper.Map <DTO.QCReportDefectDTO, QCReportDefect>(dto, item);
                    }
                }
            }

            //QCReportTestEnvironment
            if (dtoItem.QCReportTestEnvironmentDTOs != null)
            {
                foreach (QCReportTestEnvironment item in dbItem.QCReportTestEnvironment.ToList())
                {
                    if (!dtoItem.QCReportTestEnvironmentDTOs.Select(s => s.QCReportTestEnvironmentID).Contains(item.QCReportTestEnvironmentID))
                    {
                        dbItem.QCReportTestEnvironment.Remove(item);
                    }
                }

                foreach (DTO.QCReportTestEnvironmentDTO dto in dtoItem.QCReportTestEnvironmentDTOs)
                {
                    QCReportTestEnvironment item;

                    if (dto.QCReportTestEnvironmentID < 0)
                    {
                        item = new QCReportTestEnvironment();

                        dbItem.QCReportTestEnvironment.Add(item);
                    }
                    else
                    {
                        item = dbItem.QCReportTestEnvironment.FirstOrDefault(s => s.QCReportTestEnvironmentID == dto.QCReportTestEnvironmentID);
                    }

                    if (item != null)
                    {
                        AutoMapper.Mapper.Map <DTO.QCReportTestEnvironmentDTO, QCReportTestEnvironment>(dto, item);
                    }
                }
            }

            //QCReportFactoryOrderDetail
            if (dtoItem.QCReportFactoryOrderDetailDTOs != null)
            {
                foreach (var sItem in dtoItem.QCReportFactoryOrderDetailDTOs.ToList())
                {
                    if (sItem.IsSelected == false)
                    {
                        dtoItem.QCReportFactoryOrderDetailDTOs.Remove(sItem);
                    }
                }

                foreach (QCReportFactoryOrderDetail item in dbItem.QCReportFactoryOrderDetail.ToList())
                {
                    if (!dtoItem.QCReportFactoryOrderDetailDTOs.Select(s => s.QCReportFactoryOrderDetailID).Contains(item.QCReportFactoryOrderDetailID))
                    {
                        dbItem.QCReportFactoryOrderDetail.Remove(item);
                    }
                }

                foreach (DTO.QCReportFactoryOrderDetailDTO dto in dtoItem.QCReportFactoryOrderDetailDTOs)
                {
                    QCReportFactoryOrderDetail item;

                    if (dto.QCReportFactoryOrderDetailID < 0)
                    {
                        item = new QCReportFactoryOrderDetail();

                        dbItem.QCReportFactoryOrderDetail.Add(item);
                    }
                    else
                    {
                        item = dbItem.QCReportFactoryOrderDetail.FirstOrDefault(s => s.QCReportFactoryOrderDetailID == dto.QCReportFactoryOrderDetailID);
                    }

                    if (item != null)
                    {
                        //select is save
                        if (dto.IsSelected == true)
                        {
                            AutoMapper.Mapper.Map <DTO.QCReportFactoryOrderDetailDTO, QCReportFactoryOrderDetail>(dto, item);
                        }
                    }
                }
            }

            AutoMapper.Mapper.Map <DTO.QCReportDTO, QCReport>(dtoItem, dbItem);
            dbItem.InspectedDate = dtoItem.InspectedDate.ConvertStringToDateTime();
            dbItem.UpdatedDate   = dtoItem.UpdatedDate.ConvertStringToDateTime();
        }
Exemplo n.º 2
0
        public bool Update(int userId, int id, ref object dto, out Notification notification)
        {
            DTO.QCReportDTO dtoItem = ((Newtonsoft.Json.Linq.JObject)dto).ToObject <DTO.QCReportDTO>();
            notification = new Notification()
            {
                Type = NotificationType.Success
            };
            try
            {
                using (QCReportEntities context = CreateContext())
                {
                    QCReport dbItem = null;
                    if (id > 0)
                    {
                        dbItem = context.QCReport.FirstOrDefault(s => s.QCReportID == id);

                        if (dbItem == null)
                        {
                            notification = new Notification()
                            {
                                Type = NotificationType.Error, Message = "Can not find data"
                            };
                            return(false);
                        }
                    }
                    else
                    {
                        dbItem = new QCReport();
                        context.QCReport.Add(dbItem);
                    }

                    // add QCReportImage
                    foreach (var item in dtoItem.QCReportImageDTOs)
                    {
                        if (item.ScanHasChange)
                        {
                            if (string.IsNullOrEmpty(item.ScanNewFile))
                            {
                                fwFactory.RemoveFile(item.FileUD);
                            }
                            else
                            {
                                item.FileUD = fwFactory.CreateFilePointer(FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\", item.ScanNewFile, item.FileUD);
                            }
                        }
                    }

                    converter.DTO2DB_QCReport(dtoItem, ref dbItem);
                    // product image
                    if (dtoItem.ScanHasChange)
                    {
                        dbItem.ProductImage = fwFactory.CreateFilePointer(FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\", dtoItem.ScanNewFile, dtoItem.ProductImage);
                    }

                    if (dtoItem.ReportScanHasChange)
                    {
                        dbItem.ReportFile = fwFactory.CreateFilePointer(FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\", dtoItem.ReportScanNewFile, dtoItem.ReportFile);
                    }

                    dbItem.UpdatedBy   = userId;
                    dbItem.UpdatedDate = DateTime.Now;
                    context.QCReportSummary.Local.Where(o => o.QCReport == null).ToList().ForEach(s => context.QCReportSummary.Remove(s));
                    context.QCReportDetail.Local.Where(o => o.QCReport == null).ToList().ForEach(s => context.QCReportDetail.Remove(s));
                    context.QCReportDefect.Local.Where(o => o.QCReport == null).ToList().ForEach(s => context.QCReportDefect.Remove(s));
                    context.QCReportImage.Local.Where(o => o.QCReport == null).ToList().ForEach(s => context.QCReportImage.Remove(s));
                    context.QCReportTestEnvironment.Local.Where(o => o.QCReport == null).ToList().ForEach(s => context.QCReportTestEnvironment.Remove(s));
                    context.QCReportFactoryOrderDetail.Local.Where(o => o.QCReport == null).ToList().ForEach(s => context.QCReportFactoryOrderDetail.Remove(s));
                    context.SaveChanges();

                    //Create code of id
                    dbItem.QCReportUD = "QC" + dbItem.QCReportID.ToString("D8");
                    context.SaveChanges();

                    if (id == 0)
                    {
                        string emailSubject = "";
                        string emailBody    = "";
                        string url          = "";
                        url           = this.frontendURL + "QCReportMng/Edit/" + dbItem.QCReportID.ToString();
                        emailSubject += "New QC report - " + "[" + dtoItem.ClientUD + "]" + " / " + "[" + dtoItem.FactoryUD + "]" + " - " + dtoItem.ArticleCode + " - " + dtoItem.Description;
                        emailBody    += "New QC Report:";
                        emailBody    += Environment.NewLine + "Click <a href='" + url + "'>here</a> to link to QC Report:" + url;

                        var QCReportMenber = context.QCReportMng_QCReportGroupMember_View.ToList();
                        var ClientManager  = context.QCReportMng_ClientManager_View.Where(o => o.ClientID == dtoItem.ClientID).FirstOrDefault();

                        List <int?> UserIDs = new List <int?>();
                        UserIDs.Add(ClientManager.AccManagerID);
                        UserIDs.Add(ClientManager.AccManagerAssitantID);
                        UserIDs.Add(ClientManager.VNMerchandiserID);
                        UserIDs.Add(ClientManager.VNMerchandiserAssitantID);

                        string sendToEmail = "";
                        if (QCReportMenber.Count() > 0)
                        {
                            foreach (var item in QCReportMenber)
                            {
                                if (sendToEmail != "")
                                {
                                    sendToEmail += ";" + item.Email;
                                }
                                else
                                {
                                    sendToEmail = item.Email;
                                }

                                UserIDs.Add(item.UserID);
                            }
                        }
                        if (ClientManager.AccManagerEmail != "")
                        {
                            if (sendToEmail != "")
                            {
                                sendToEmail += ";" + ClientManager.AccManagerEmail;
                            }
                            else
                            {
                                sendToEmail = ClientManager.AccManagerEmail;
                            }
                        }
                        if (ClientManager.AccManagerAssitantEmail != "")
                        {
                            if (sendToEmail != "")
                            {
                                sendToEmail += ";" + ClientManager.AccManagerAssitantEmail;
                            }
                            else
                            {
                                sendToEmail = ClientManager.AccManagerAssitantEmail;
                            }
                        }
                        if (ClientManager.VNMerchandiserEmail != "")
                        {
                            if (sendToEmail != "")
                            {
                                sendToEmail += ";" + ClientManager.VNMerchandiserEmail;
                            }
                            else
                            {
                                sendToEmail = ClientManager.VNMerchandiserEmail;
                            }
                        }
                        if (ClientManager.VNMerchandiserAssitantEmail != "")
                        {
                            if (sendToEmail != "")
                            {
                                sendToEmail += ";" + ClientManager.VNMerchandiserAssitantEmail;
                            }
                            else
                            {
                                sendToEmail = ClientManager.VNMerchandiserAssitantEmail;
                            }
                        }

                        SendNotification(emailSubject, emailBody, sendToEmail, UserIDs);
                    }


                    dto = GetData(dbItem.QCReportID, dbItem.SaleOrderDetailID, dbItem.FactoryID, null, null, null, out notification).Data;
                }
                return(true);
            }
            catch (Exception ex)
            {
                notification = new Notification()
                {
                    Type = NotificationType.Error, Message = ex.Message
                };
                return(false);
            }
        }