Exemple #1
0
        public override bool UpdateData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification)
        {
            notification      = new Library.DTO.Notification();
            notification.Type = Library.DTO.NotificationType.Success;

            DTO.WarehouseTransferDTO dtoWarehouseTransfer = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.WarehouseTransferDTO>();

            try
            {
                // Get company
                Framework.DAL.DataFactory frameworkFactory = new Framework.DAL.DataFactory();
                int?companyID = frameworkFactory.GetCompanyID(userId);

                using (WarehouseTransferMngEntities context = CreateContext())
                {
                    WarehouseTransfer dbWarehouseTransfer;

                    if (id == 0)
                    {
                        dbWarehouseTransfer = new WarehouseTransfer();
                        context.WarehouseTransfer.Add(dbWarehouseTransfer);
                    }
                    else
                    {
                        dbWarehouseTransfer = context.WarehouseTransfer.FirstOrDefault(o => o.WarehouseTransferID == id);
                    }

                    if (dbWarehouseTransfer == null)
                    {
                        notification.Type    = Library.DTO.NotificationType.Error;
                        notification.Message = "Can not find WarehouseTransfer!";

                        return(false);
                    }

                    converter.DTO2DB_WarehouseTransfer(userId, companyID, dtoWarehouseTransfer, ref dbWarehouseTransfer);

                    // Remove local WarehouseTransferDetail
                    context.WarehouseTransferDetail.Local.Where(o => o.WarehouseTransfer == null).ToList().ForEach(o => context.WarehouseTransferDetail.Remove(o));
                    context.SaveChanges();

                    // Generate code WarehouseTransfer
                    if (id == 0)
                    {
                        context.WarehouseTransferMng_function_GenerateReceiptCode(dbWarehouseTransfer.WarehouseTransferID, dbWarehouseTransfer.CompanyID, dbWarehouseTransfer.ReceiptDate.Value.Year, dbWarehouseTransfer.ReceiptDate.Value.Month);
                    }

                    // Get data WarehouseTransfer to reload
                    dtoItem = GetData(userId, dbWarehouseTransfer.WarehouseTransferID, out notification).Data;
                }

                return(true);
            }
            catch (Exception ex)
            {
                notification.Type    = Library.DTO.NotificationType.Error;
                notification.Message = Library.Helper.GetInnerException(ex).Message;

                return(false);
            }
        }
        public void DTO2DB_WarehouseTransfer(int userID, int?companyID, DTO.WarehouseTransferDTO dtoWarehouseTransfer, ref WarehouseTransfer dbWarehouseTransfer)
        {
            AutoMapper.Mapper.Map <DTO.WarehouseTransferDTO, WarehouseTransfer>(dtoWarehouseTransfer, dbWarehouseTransfer);
            dbWarehouseTransfer.CompanyID   = companyID;
            dbWarehouseTransfer.UpdatedBy   = userID;
            dbWarehouseTransfer.UpdatedDate = DateTime.Now;
            dbWarehouseTransfer.ReceiptDate = dtoWarehouseTransfer.ReceiptDate.ConvertStringToDateTime();

            if (dtoWarehouseTransfer.WarehouseTransferID == 0)
            {
                dbWarehouseTransfer.CreatedBy   = userID;
                dbWarehouseTransfer.CreatedDate = DateTime.Now;
            }

            if (dtoWarehouseTransfer.WarehouseTransferDetails != null)
            {
                foreach (WarehouseTransferDetail dbWarehouseTransferDetail in dbWarehouseTransfer.WarehouseTransferDetail.ToList())
                {
                    if (!dtoWarehouseTransfer.WarehouseTransferDetails.Select(s => s.WarehouseTransferDetailID).Contains(dbWarehouseTransferDetail.WarehouseTransferDetailID))
                    {
                        dbWarehouseTransfer.WarehouseTransferDetail.Remove(dbWarehouseTransferDetail);
                    }
                }

                foreach (DTO.WarehouseTransferDetailDTO dtoWarehouseTransferDetail in dtoWarehouseTransfer.WarehouseTransferDetails.ToList())
                {
                    WarehouseTransferDetail dbWarehouseTransferDetail;

                    if (dtoWarehouseTransferDetail.WarehouseTransferDetailID <= 0)
                    {
                        dbWarehouseTransferDetail = new WarehouseTransferDetail();
                        dbWarehouseTransfer.WarehouseTransferDetail.Add(dbWarehouseTransferDetail);
                    }
                    else
                    {
                        dbWarehouseTransferDetail = dbWarehouseTransfer.WarehouseTransferDetail.FirstOrDefault(o => o.WarehouseTransferDetailID == dtoWarehouseTransferDetail.WarehouseTransferDetailID);
                    }

                    if (dbWarehouseTransferDetail != null)
                    {
                        AutoMapper.Mapper.Map <DTO.WarehouseTransferDetailDTO, WarehouseTransferDetail>(dtoWarehouseTransferDetail, dbWarehouseTransferDetail);
                    }
                }
            }
            ////set base info
            //dbItem.CompanyID = companyID;
            //dbItem.ReceiptDate = dtoItem.ReceiptDate.ConvertStringToDateTime();
            //if (dtoItem.WarehouseTransferID>0)
            //{
            //    dbItem.UpdatedDate = DateTime.Now;
            //    dbItem.UpdatedBy = userId;
            //}
            //else {
            //    dbItem.CreatedDate = DateTime.Now;
            //    dbItem.CreatedBy = userId;
            //}
            ////create product
            //if (dbItem.WarehouseTransferID > 0)
            //{
            //    //delete delivery note item
            //    var deliveryNote = dbItem.DeliveryNote.FirstOrDefault();
            //    foreach (var item in deliveryNote.DeliveryNoteDetail.ToArray())
            //    {
            //        deliveryNote.DeliveryNoteDetail.Remove(item);
            //    }

            //    //delete receiving note item
            //    var receivingNote = dbItem.ReceivingNote.FirstOrDefault();
            //    foreach (var item in receivingNote.ReceivingNoteDetail.ToArray())
            //    {
            //        receivingNote.ReceivingNoteDetail.Remove(item);
            //    }

            //    //create transfer item
            //    DeliveryNoteDetail dbDeliveryNoteDetail;
            //    ReceivingNoteDetail dbReceivingNoteDetail;
            //    foreach (var item in dtoItem.WarehouseTransferProductDTOs)
            //    {
            //        //add deliverynote detail
            //        dbDeliveryNoteDetail = new DeliveryNoteDetail();
            //        dbDeliveryNoteDetail.ProductionItemID = item.ProductionItemID;
            //        dbDeliveryNoteDetail.Qty = item.Quantity;
            //        dbDeliveryNoteDetail.QNTBarCode = item.QNTBarCode;
            //        dbDeliveryNoteDetail.FromFactoryWarehouseID = dtoItem.FromFactoryWarehouseID;
            //        deliveryNote.DeliveryNoteDetail.Add(dbDeliveryNoteDetail);

            //        //add receivingNote detail
            //        dbReceivingNoteDetail = new ReceivingNoteDetail();
            //        dbReceivingNoteDetail.ProductionItemID = item.ProductionItemID;
            //        dbReceivingNoteDetail.Quantity = item.Quantity;
            //        dbReceivingNoteDetail.QNTBarCode = item.QNTBarCode;
            //        dbReceivingNoteDetail.ToFactoryWarehouseID = dtoItem.ToFactoryWarehouseID;
            //        receivingNote.ReceivingNoteDetail.Add(dbReceivingNoteDetail);
            //    }
            //}
            //else {
            //    //create delivery note
            //    DeliveryNote deliveryNote = new DeliveryNote();
            //    deliveryNote.DeliveryNoteTypeID = 3;
            //    deliveryNote.DeliveryNoteDate = DateTime.Now;
            //    deliveryNote.CompanyID = companyID;
            //    deliveryNote.UpdatedBy = userId;
            //    deliveryNote.UpdatedDate = DateTime.Now;
            //    dbItem.DeliveryNote.Add(deliveryNote);

            //    //create receiving note
            //    ReceivingNote receivingNote = new ReceivingNote();
            //    receivingNote.ReceivingNoteTypeID = 3;
            //    receivingNote.ReceivingNoteDate = DateTime.Now;
            //    receivingNote.CompanyID = companyID;
            //    receivingNote.UpdatedBy = userId;
            //    receivingNote.UpdatedDate = DateTime.Now;
            //    dbItem.ReceivingNote.Add(receivingNote);

            //    //create transfer item
            //    DeliveryNoteDetail dbDeliveryNoteDetail;
            //    ReceivingNoteDetail dbReceivingNoteDetail;
            //    foreach (var item in dtoItem.WarehouseTransferProductDTOs)
            //    {
            //        //add deliverynote detail
            //        dbDeliveryNoteDetail = new DeliveryNoteDetail();
            //        dbDeliveryNoteDetail.ProductionItemID = item.ProductionItemID;
            //        dbDeliveryNoteDetail.Qty = item.Quantity;
            //        dbDeliveryNoteDetail.QNTBarCode = item.QNTBarCode;
            //        dbDeliveryNoteDetail.FromFactoryWarehouseID = dtoItem.FromFactoryWarehouseID;
            //        deliveryNote.DeliveryNoteDetail.Add(dbDeliveryNoteDetail);

            //        //add receivingNote detail
            //        dbReceivingNoteDetail = new ReceivingNoteDetail();
            //        dbReceivingNoteDetail.ProductionItemID = item.ProductionItemID;
            //        dbReceivingNoteDetail.Quantity = item.Quantity;
            //        dbReceivingNoteDetail.QNTBarCode = item.QNTBarCode;
            //        dbReceivingNoteDetail.ToFactoryWarehouseID = dtoItem.ToFactoryWarehouseID;
            //        receivingNote.ReceivingNoteDetail.Add(dbReceivingNoteDetail);
            //    }
            //}
        }