public void DTO2DB_FactorySaleInvoice(DTO.FactorySaleInvoiceDTO dtoItem, ref FactorySaleInvoice dbItem, int userId)
        {
            if (dtoItem.FactorySaleInvoiceDetailDTOs != null)
            {
                foreach (var item in dbItem.FactorySaleInvoiceDetail.ToArray())
                {
                    if (!dtoItem.FactorySaleInvoiceDetailDTOs.Select(s => s.FactorySaleInvoiceDetailID).Contains(item.FactorySaleInvoiceDetailID))
                    {
                        dbItem.FactorySaleInvoiceDetail.Remove(item);
                    }
                }
                foreach (var item in dtoItem.FactorySaleInvoiceDetailDTOs)
                {
                    FactorySaleInvoiceDetail dbDetail = new FactorySaleInvoiceDetail();

                    if (item.FactorySaleInvoiceDetailID < 0)
                    {
                        dbItem.FactorySaleInvoiceDetail.Add(dbDetail);
                    }
                    else
                    {
                        dbDetail = dbItem.FactorySaleInvoiceDetail.Where(s => s.FactorySaleInvoiceDetailID == item.FactorySaleInvoiceDetailID).FirstOrDefault();
                    }

                    if (dbDetail != null)
                    {
                        AutoMapper.Mapper.Map <DTO.FactorySaleInvoiceDetailDTO, FactorySaleInvoiceDetail>(item, dbDetail);
                    }
                }
            }
            AutoMapper.Mapper.Map <DTO.FactorySaleInvoiceDTO, FactorySaleInvoice>(dtoItem, dbItem);
            if (!string.IsNullOrEmpty(dtoItem.InvoiceDate))
            {
                dbItem.InvoiceDate = dtoItem.InvoiceDate.ConvertStringToDateTime();
            }

            if (!string.IsNullOrEmpty(dtoItem.PostingDate))
            {
                dbItem.PostingDate = dtoItem.PostingDate.ConvertStringToDateTime();
            }
            if (dbItem.FactorySaleInvoiceID > 0)
            {
                dbItem.UpdatedBy   = userId;
                dbItem.UpdatedDate = DateTime.Now;
            }
            else
            {
                dbItem.CreatedBy   = userId;
                dbItem.CreatedDate = DateTime.Now;
            }
        }
Exemple #2
0
        public override bool UpdateData(int userId, int id, ref object dtoItem, out Notification notification)
        {
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            DTO.FactorySaleInvoiceDTO dtoFactorySaleInvoice = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.FactorySaleInvoiceDTO>();
            try
            {
                using (FactorySaleInvoiceEntities context = CreateContext())
                {
                    FactorySaleInvoice dbItem = null;
                    if (id == 0)
                    {
                        dbItem = new FactorySaleInvoice();
                        context.FactorySaleInvoice.Add(dbItem);
                    }
                    else
                    {
                        dbItem = context.FactorySaleInvoice.Where(o => o.FactorySaleInvoiceID == id).FirstOrDefault();
                    }
                    if (dbItem == null)
                    {
                        notification.Message = "data not found!";
                        return(false);
                    }
                    else
                    {
                        //upload file
                        Module.Framework.DAL.DataFactory fwFactory = new Module.Framework.DAL.DataFactory();
                        string tempFolder = FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\";
                        if (dtoFactorySaleInvoice.File_HasChange.HasValue && dtoFactorySaleInvoice.File_HasChange.Value)
                        {
                            dtoFactorySaleInvoice.AttachedFile = fwFactory.CreateFilePointer(tempFolder, dtoFactorySaleInvoice.File_NewFile, dtoFactorySaleInvoice.AttachedFile, dtoFactorySaleInvoice.FriendlyName);
                        }

                        //convert dto to db
                        converter.DTO2DB_FactorySaleInvoice(dtoFactorySaleInvoice, ref dbItem, userId);

                        if (id == 0)
                        {
                            dbItem.FactorySaleInvoiceStatusID = 1;
                            //dbItem.SetStatusBy = userId;
                            //dbItem.SetStatusDate = DateTime.Now;
                        }
                        //remove orphan
                        context.FactorySaleInvoiceDetail.Local.Where(o => o.FactorySaleInvoice == null).ToList().ForEach(o => context.FactorySaleInvoiceDetail.Remove(o));

                        //save data
                        context.SaveChanges();

                        //generate purchase invoice code
                        if (string.IsNullOrEmpty(dbItem.DocCode))
                        {
                            context.FactorySaleInvoice_function_GenerateFactorySaleInvoiceUD(dbItem.FactorySaleInvoiceID, dbItem.InvoiceDate.Value.Year, dbItem.InvoiceDate.Value.Month);
                        }

                        //get return data
                        dtoItem = GetData(userId, dbItem.FactorySaleInvoiceID, null, out notification).Data;
                        return(true);
                    }
                }
            }
            catch (Exception ex)
            {
                notification.Type    = Library.DTO.NotificationType.Error;
                notification.Message = Library.Helper.GetInnerException(ex).Message;
                return(false);
            }
        }