Beispiel #1
0
        public void DTO2DB_FactoryStockReceipt(DTO.FactoryStockReceipt dtoItem, ref FactoryStockReceipt dbItem)
        {
            if (dtoItem.FactoryStockReceiptDetails != null) // allow user delete item in case receipt is export
            {
                foreach (var item in dbItem.FactoryStockReceiptDetail.ToArray())
                {
                    if (!dtoItem.FactoryStockReceiptDetails.Select(s => s.FactoryStockReceiptDetailID).Contains(item.FactoryStockReceiptDetailID))
                    {
                        dbItem.FactoryStockReceiptDetail.Remove(item);
                    }
                }

                foreach (var item in dtoItem.FactoryStockReceiptDetails)
                {
                    FactoryStockReceiptDetail dbDetail;
                    if (item.FactoryStockReceiptDetailID < 0)
                    {
                        dbDetail = new FactoryStockReceiptDetail();
                        dbItem.FactoryStockReceiptDetail.Add(dbDetail);
                    }
                    else
                    {
                        dbDetail = dbItem.FactoryStockReceiptDetail.Where(o => o.FactoryStockReceiptDetailID == item.FactoryStockReceiptDetailID).FirstOrDefault();
                    }

                    if (dbDetail != null)
                    {
                        AutoMapper.Mapper.Map <DTO.FactoryStockReceiptDetail, FactoryStockReceiptDetail>(item, dbDetail);
                    }
                }
            }
            AutoMapper.Mapper.Map <DTO.FactoryStockReceipt, FactoryStockReceipt>(dtoItem, dbItem);
            dbItem.UpdatedDate = DateTime.Now;
            dbItem.UpdatedBy   = dtoItem.UpdatedBy;
            dbItem.ReceiptDate = dtoItem.ReceiptDate.ConvertStringToDateTime();
            if (dtoItem.FactoryStockReceiptID == null)
            {
                dbItem.CreatedDate = DateTime.Now;
                dbItem.CreatedBy   = dtoItem.UpdatedBy;
            }
        }
Beispiel #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.FactoryStockReceipt dtoFactoryStockReceipt = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.FactoryStockReceipt>();
            dtoFactoryStockReceipt.UpdatedBy = userId;
            Module.Framework.DAL.DataFactory fwFactory = new Module.Framework.DAL.DataFactory();
            try
            {
                using (FactoryStockReceiptEntities context = CreateContext())
                {
                    FactoryStockReceipt dbItem = null;
                    if (id == 0)
                    {
                        dbItem = new FactoryStockReceipt();
                        context.FactoryStockReceipt.Add(dbItem);
                    }
                    else
                    {
                        dbItem = context.FactoryStockReceipt.Where(o => o.FactoryStockReceiptID == id).FirstOrDefault();
                    }

                    if (dbItem == null)
                    {
                        notification.Message = "data not found!";
                        return(false);
                    }
                    else
                    {
                        //
                        if (!dtoFactoryStockReceipt.FactoryID.HasValue) // 2 : Export
                        {
                            throw new Exception("Factory is empty. You should fill-in factory");
                        }
                        //check permission on factory
                        if (fwFactory.CheckFactoryPermission(userId, dtoFactoryStockReceipt.FactoryID.Value) == 0)
                        {
                            throw new Exception("You do not have access permission on this factory");
                        }

                        //validate quantity
                        if (dtoFactoryStockReceipt.ReceiptTypeID == 1) //import
                        {
                            ValidateQntImport(dtoFactoryStockReceipt);
                        }
                        else if (dtoFactoryStockReceipt.ReceiptTypeID == 2)  //export
                        {
                            ValidateQntExport(dtoFactoryStockReceipt);
                        }

                        //convert dto to db
                        converter.DTO2DB_FactoryStockReceipt(dtoFactoryStockReceipt, ref dbItem);
                        //remove orphan item
                        context.FactoryStockReceiptDetail.Local.Where(o => o.FactoryStockReceipt == null).ToList().ForEach(o => context.FactoryStockReceiptDetail.Remove(o));
                        //save data
                        context.SaveChanges();
                        //update receipt no
                        context.FactoryStockReceiptMng_function_GenerateReceipNo(dbItem.FactoryStockReceiptID, dbItem.ReceiptTypeID);
                        //get return data
                        dtoItem = GetData(userId, dbItem.FactoryStockReceiptID, 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);
            }
        }