private void DoSaveOrDeleteGoodsReceipt(int nmvnTaskID, bool doSaveOrDelete, IGoodsReceiptDTO goodsReceiptDTO, int?goodsReceiptID)
        {
            //***VERY IMPORTANT***: THE BaseService.UserID IS AUTOMATICALLY SET BY CustomControllerAttribute OF CONTROLLER, ONLY WHEN BaseService IS INITIALIZED BY CONTROLLER. BUT HERE, THE this.goodsReceiptBaseService IS INITIALIZED BY VehiclesInvoiceService => SO SHOULD SET goodsReceiptBaseService.UserID = this.UserID
            if (this.IsMaterial(nmvnTaskID))
            {
                IMaterialReceiptBaseService goodsReceiptBaseService = new MaterialReceiptBaseService(new GoodsReceiptRepository(this.GenericWithDetailRepository.TotalSmartPortalEntities));
                goodsReceiptBaseService.UserID = this.UserID;

                if (doSaveOrDelete)
                {
                    goodsReceiptBaseService.Save((GoodsReceiptDTO <GROptionMaterial>)goodsReceiptDTO, true);
                }
                else
                {
                    goodsReceiptBaseService.Delete((int)goodsReceiptID, true);
                }
            }
            else
            if (this.IsItem(nmvnTaskID))
            {
                IItemReceiptBaseService goodsReceiptBaseService = new ItemReceiptBaseService(new GoodsReceiptRepository(this.GenericWithDetailRepository.TotalSmartPortalEntities));
                goodsReceiptBaseService.UserID = this.UserID;

                if (doSaveOrDelete)
                {
                    goodsReceiptBaseService.Save((GoodsReceiptDTO <GROptionItem>)goodsReceiptDTO, true);
                }
                else
                {
                    goodsReceiptBaseService.Delete((int)goodsReceiptID, true);
                }
            }
            else
            if (this.IsProduct(nmvnTaskID))
            {
                IProductReceiptBaseService goodsReceiptBaseService = new ProductReceiptBaseService(new GoodsReceiptRepository(this.GenericWithDetailRepository.TotalSmartPortalEntities));
                goodsReceiptBaseService.UserID = this.UserID;
                if (doSaveOrDelete)
                {
                    goodsReceiptBaseService.Save((GoodsReceiptDTO <GROptionProduct>)goodsReceiptDTO, true);
                }
                else
                {
                    goodsReceiptBaseService.Delete((int)goodsReceiptID, true);
                }
            }
        }
 public void Save(IGoodsReceiptDTO goodsReceiptDTO)
 {
     if (this.grOption == GlobalEnums.GROption.IsMaterial)
     {
         materialReceiptBaseService.Save((GoodsReceiptDTO <GROptionMaterial>)goodsReceiptDTO, true);
     }
     else
     if (this.grOption == GlobalEnums.GROption.IsItem)
     {
         itemReceiptBaseService.Save((GoodsReceiptDTO <GROptionItem>)goodsReceiptDTO, true);
     }
     else
     if (this.grOption == GlobalEnums.GROption.IsProduct)
     {
         productReceiptBaseService.Save((GoodsReceiptDTO <GROptionProduct>)goodsReceiptDTO, true);
     }
 }
        protected override void SaveRelative(WarehouseTransfer warehouseTransfer, SaveRelativeOption saveRelativeOption)
        {
            base.SaveRelative(warehouseTransfer, saveRelativeOption);

            if (warehouseTransfer.OneStep)
            {
                GRHelperService grHelperService = new GRHelperService(this.GetGROption(warehouseTransfer.NMVNTaskID), this.GenericWithDetailRepository.TotalSmartPortalEntities, this.UserID);

                IGoodsReceiptAPIRepository goodsReceiptAPIRepository = new GoodsReceiptAPIRepository(this.GenericWithDetailRepository.TotalSmartPortalEntities);
                if (saveRelativeOption == SaveRelativeOption.Update)
                {
                    IGoodsReceiptDTO goodsReceiptDTO = grHelperService.NewGoodsReceiptDTO();

                    goodsReceiptDTO.EntryDate = warehouseTransfer.EntryDate;

                    goodsReceiptDTO.GoodsReceiptTypeID  = (int)GlobalEnums.GoodsReceiptTypeID.WarehouseTransfer;
                    goodsReceiptDTO.WarehouseTransferID = warehouseTransfer.WarehouseTransferID;

                    goodsReceiptDTO.ShiftID = warehouseTransfer.ShiftID;
                    goodsReceiptDTO.OneStep = warehouseTransfer.OneStep;

                    goodsReceiptDTO.Warehouse = new TotalDTO.Commons.WarehouseBaseDTO()
                    {
                        WarehouseID = warehouseTransfer.WarehouseReceiptID
                    };
                    goodsReceiptDTO.WarehouseIssue = new TotalDTO.Commons.WarehouseBaseDTO()
                    {
                        WarehouseID = warehouseTransfer.WarehouseID
                    };

                    goodsReceiptDTO.StorekeeperID    = warehouseTransfer.StorekeeperID;
                    goodsReceiptDTO.PreparedPersonID = warehouseTransfer.PreparedPersonID;
                    goodsReceiptDTO.ApproverID       = warehouseTransfer.PreparedPersonID;

                    goodsReceiptDTO.Purposes    = warehouseTransfer.Caption;
                    goodsReceiptDTO.Description = warehouseTransfer.Description;
                    goodsReceiptDTO.Remarks     = warehouseTransfer.Remarks;

                    goodsReceiptDTO.Approved     = warehouseTransfer.Approved;
                    goodsReceiptDTO.ApprovedDate = warehouseTransfer.ApprovedDate;

                    List <GoodsReceiptPendingWarehouseTransferDetail> pendingWarehouseTransferDetails = goodsReceiptAPIRepository.GetPendingWarehouseTransferDetails((int)goodsReceiptDTO.NMVNTaskID, null, goodsReceiptDTO.WarehouseTransferID, goodsReceiptDTO.WarehouseID, goodsReceiptDTO.WarehouseIssueID, null, goodsReceiptDTO.OneStep);
                    foreach (GoodsReceiptPendingWarehouseTransferDetail pendingWarehouseTransferDetail in pendingWarehouseTransferDetails)
                    {
                        GoodsReceiptDetailDTO goodsReceiptDetailDTO = new GoodsReceiptDetailDTO()
                        {
                            GoodsReceiptID = goodsReceiptDTO.GoodsReceiptID,

                            WarehouseTransferID        = pendingWarehouseTransferDetail.WarehouseTransferID,
                            WarehouseTransferDetailID  = pendingWarehouseTransferDetail.WarehouseTransferDetailID,
                            WarehouseTransferReference = pendingWarehouseTransferDetail.WarehouseTransferReference,
                            WarehouseTransferEntryDate = pendingWarehouseTransferDetail.WarehouseTransferEntryDate,
                            GoodsReceiptReference      = pendingWarehouseTransferDetail.GoodsReceiptReference,
                            GoodsReceiptEntryDate      = pendingWarehouseTransferDetail.GoodsReceiptEntryDate,

                            BatchID        = pendingWarehouseTransferDetail.BatchID,
                            BatchEntryDate = pendingWarehouseTransferDetail.BatchEntryDate,


                            CommodityID     = pendingWarehouseTransferDetail.CommodityID,
                            CommodityCode   = pendingWarehouseTransferDetail.CommodityCode,
                            CommodityName   = pendingWarehouseTransferDetail.CommodityName,
                            CommodityTypeID = pendingWarehouseTransferDetail.CommodityTypeID,

                            LabID = pendingWarehouseTransferDetail.LabID,

                            Barcode   = pendingWarehouseTransferDetail.Barcode,
                            BatchCode = pendingWarehouseTransferDetail.BatchCode,
                            SealCode  = pendingWarehouseTransferDetail.SealCode,
                            LabCode   = pendingWarehouseTransferDetail.LabCode,

                            ProductionDate = pendingWarehouseTransferDetail.ProductionDate,
                            ExpiryDate     = pendingWarehouseTransferDetail.ExpiryDate,

                            BinLocationID   = pendingWarehouseTransferDetail.BinLocationID,
                            BinLocationCode = pendingWarehouseTransferDetail.BinLocationCode,

                            UnitWeight = pendingWarehouseTransferDetail.UnitWeight,
                            TareWeight = pendingWarehouseTransferDetail.TareWeight,

                            QuantityRemains = (decimal)pendingWarehouseTransferDetail.QuantityRemains,
                            Quantity        = (decimal)pendingWarehouseTransferDetail.QuantityRemains,
                        };
                        goodsReceiptDTO.ViewDetails.Add(goodsReceiptDetailDTO);
                    }

                    goodsReceiptDTO.TotalQuantity = goodsReceiptDTO.GetTotalQuantity();

                    grHelperService.Save(goodsReceiptDTO);
                }

                if (saveRelativeOption == SaveRelativeOption.Undo)
                {//NOTES: THIS UNDO REQUIRE: JUST SAVE ONLY ONE GoodsReceipt FOR AN WarehouseTransfer
                    int?goodsReceiptID = goodsReceiptAPIRepository.GetGoodsReceiptID(null, null, warehouseTransfer.WarehouseTransferID, null);
                    grHelperService.Delete(goodsReceiptID);
                }
            }
        }
        protected override void SaveRelative(WarehouseAdjustment warehouseAdjustment, SaveRelativeOption saveRelativeOption)
        {
            base.SaveRelative(warehouseAdjustment, saveRelativeOption);

            if (warehouseAdjustment.HasPositiveLine)
            {
                GRHelperService grHelperService = new GRHelperService(this.GetGROption(warehouseAdjustment.NMVNTaskID), this.GenericWithDetailRepository.TotalSmartPortalEntities, this.UserID);

                IGoodsReceiptAPIRepository goodsReceiptAPIRepository = new GoodsReceiptAPIRepository(this.GenericWithDetailRepository.TotalSmartPortalEntities);
                if (saveRelativeOption == SaveRelativeOption.Update)
                {
                    IGoodsReceiptDTO goodsReceiptDTO = grHelperService.NewGoodsReceiptDTO();

                    goodsReceiptDTO.EntryDate = warehouseAdjustment.EntryDate;
                    goodsReceiptDTO.ShiftID   = 1; // warehouseAdjustment.ShiftID;

                    goodsReceiptDTO.GoodsReceiptTypeID    = (int)GlobalEnums.GoodsReceiptTypeID.WarehouseAdjustments;
                    goodsReceiptDTO.WarehouseAdjustmentID = warehouseAdjustment.WarehouseAdjustmentID;

                    goodsReceiptDTO.Warehouse = new TotalDTO.Commons.WarehouseBaseDTO()
                    {
                        WarehouseID = warehouseAdjustment.WarehouseReceiptID
                    };
                    goodsReceiptDTO.Customer = new TotalDTO.Commons.CustomerBaseDTO()
                    {
                        CustomerID = warehouseAdjustment.CustomerID
                    };

                    goodsReceiptDTO.StorekeeperID    = warehouseAdjustment.StorekeeperID;
                    goodsReceiptDTO.PreparedPersonID = warehouseAdjustment.PreparedPersonID;
                    goodsReceiptDTO.ApproverID       = warehouseAdjustment.PreparedPersonID;

                    goodsReceiptDTO.Purposes    = warehouseAdjustment.AdjustmentJobs;
                    goodsReceiptDTO.Description = warehouseAdjustment.Description;
                    goodsReceiptDTO.Remarks     = warehouseAdjustment.Remarks;

                    goodsReceiptDTO.Approved     = warehouseAdjustment.Approved;
                    goodsReceiptDTO.ApprovedDate = warehouseAdjustment.ApprovedDate;

                    List <PendingWarehouseAdjustmentDetail> pendingWarehouseAdjustmentDetails = goodsReceiptAPIRepository.GetPendingWarehouseAdjustmentDetails(warehouseAdjustment.LocationID, null, goodsReceiptDTO.WarehouseAdjustmentID, goodsReceiptDTO.WarehouseID, null, false);
                    foreach (PendingWarehouseAdjustmentDetail pendingWarehouseAdjustmentDetail in pendingWarehouseAdjustmentDetails)
                    {
                        GoodsReceiptDetailDTO goodsReceiptDetailDTO = new GoodsReceiptDetailDTO()
                        {
                            GoodsReceiptID = goodsReceiptDTO.GoodsReceiptID,

                            WarehouseAdjustmentID        = pendingWarehouseAdjustmentDetail.WarehouseAdjustmentID,
                            WarehouseAdjustmentDetailID  = pendingWarehouseAdjustmentDetail.WarehouseAdjustmentDetailID,
                            WarehouseAdjustmentReference = pendingWarehouseAdjustmentDetail.PrimaryReference,
                            WarehouseAdjustmentEntryDate = pendingWarehouseAdjustmentDetail.PrimaryEntryDate,

                            WarehouseAdjustmentTypeID = pendingWarehouseAdjustmentDetail.WarehouseAdjustmentTypeID,

                            BatchID        = pendingWarehouseAdjustmentDetail.BatchID,
                            BatchCode      = pendingWarehouseAdjustmentDetail.BatchCode,
                            BatchEntryDate = pendingWarehouseAdjustmentDetail.BatchEntryDate,

                            CommodityID   = pendingWarehouseAdjustmentDetail.CommodityID,
                            CommodityCode = pendingWarehouseAdjustmentDetail.CommodityCode,
                            CommodityName = pendingWarehouseAdjustmentDetail.CommodityName,

                            LabID = -1,

                            QuantityRemains = (decimal)pendingWarehouseAdjustmentDetail.QuantityRemains,
                            Quantity        = (decimal)pendingWarehouseAdjustmentDetail.QuantityRemains,
                        };
                        goodsReceiptDTO.ViewDetails.Add(goodsReceiptDetailDTO);
                    }

                    goodsReceiptDTO.TotalQuantity = goodsReceiptDTO.GetTotalQuantity();

                    grHelperService.Save(goodsReceiptDTO);
                }

                if (saveRelativeOption == SaveRelativeOption.Undo)
                {//NOTES: THIS UNDO REQUIRE: JUST SAVE ONLY ONE GoodsReceipt FOR AN WarehouseAdjustment
                    int?goodsReceiptID = goodsReceiptAPIRepository.GetGoodsReceiptID(null, null, null, warehouseAdjustment.WarehouseAdjustmentID);
                    grHelperService.Delete(goodsReceiptID);
                }
            }
        }
        protected override void SaveRelative(WarehouseAdjustment warehouseAdjustment, SaveRelativeOption saveRelativeOption)
        {
            base.SaveRelative(warehouseAdjustment, saveRelativeOption);

            if (warehouseAdjustment.HasPositiveLine)
            {
                IGoodsReceiptAPIRepository goodsReceiptAPIRepository = new GoodsReceiptAPIRepository(this.GenericWithDetailRepository.TotalSmartPortalEntities);
                if (saveRelativeOption == SaveRelativeOption.Update)
                {
                    IGoodsReceiptDTO goodsReceiptDTO = this.NewGoodsReceiptDTO(warehouseAdjustment.NMVNTaskID);

                    goodsReceiptDTO.EntryDate = warehouseAdjustment.EntryDate;
                    goodsReceiptDTO.Warehouse = new TotalDTO.Commons.WarehouseBaseDTO()
                    {
                        WarehouseID = warehouseAdjustment.WarehouseReceiptID
                    };

                    goodsReceiptDTO.WarehouseAdjustmentID = warehouseAdjustment.WarehouseAdjustmentID;

                    goodsReceiptDTO.GoodsReceiptTypeID = (int)GlobalEnums.GoodsReceiptTypeID.WarehouseAdjustments;

                    goodsReceiptDTO.StorekeeperID    = warehouseAdjustment.StorekeeperID;
                    goodsReceiptDTO.PreparedPersonID = warehouseAdjustment.PreparedPersonID;
                    goodsReceiptDTO.ApproverID       = warehouseAdjustment.PreparedPersonID;

                    goodsReceiptDTO.Customer = new TotalDTO.Commons.CustomerBaseDTO()
                    {
                        CustomerID = warehouseAdjustment.CustomerID
                    };

                    goodsReceiptDTO.Purposes    = warehouseAdjustment.AdjustmentJobs;
                    goodsReceiptDTO.Description = warehouseAdjustment.Description;
                    goodsReceiptDTO.Remarks     = warehouseAdjustment.Remarks;

                    goodsReceiptDTO.Approved     = warehouseAdjustment.Approved;
                    goodsReceiptDTO.ApprovedDate = warehouseAdjustment.ApprovedDate;

                    List <PendingWarehouseAdjustmentDetail> pendingWarehouseAdjustmentDetails = goodsReceiptAPIRepository.GetPendingWarehouseAdjustmentDetails(warehouseAdjustment.LocationID, null, warehouseAdjustment.WarehouseAdjustmentID, warehouseAdjustment.WarehouseReceiptID, null, false);
                    foreach (PendingWarehouseAdjustmentDetail pendingWarehouseAdjustmentDetail in pendingWarehouseAdjustmentDetails)
                    {
                        GoodsReceiptDetailDTO goodsReceiptDetailDTO = new GoodsReceiptDetailDTO()
                        {
                            GoodsReceiptID = goodsReceiptDTO.GoodsReceiptID,

                            WarehouseAdjustmentID        = pendingWarehouseAdjustmentDetail.WarehouseAdjustmentID,
                            WarehouseAdjustmentDetailID  = pendingWarehouseAdjustmentDetail.WarehouseAdjustmentDetailID,
                            WarehouseAdjustmentReference = pendingWarehouseAdjustmentDetail.PrimaryReference,
                            WarehouseAdjustmentEntryDate = pendingWarehouseAdjustmentDetail.PrimaryEntryDate,

                            WarehouseAdjustmentTypeID = pendingWarehouseAdjustmentDetail.WarehouseAdjustmentTypeID,

                            BatchID        = pendingWarehouseAdjustmentDetail.BatchID,
                            BatchEntryDate = pendingWarehouseAdjustmentDetail.BatchEntryDate,

                            CommodityID   = pendingWarehouseAdjustmentDetail.CommodityID,
                            CommodityCode = pendingWarehouseAdjustmentDetail.CommodityCode,
                            CommodityName = pendingWarehouseAdjustmentDetail.CommodityName,

                            QuantityRemains = (decimal)pendingWarehouseAdjustmentDetail.QuantityRemains,
                            Quantity        = (decimal)pendingWarehouseAdjustmentDetail.QuantityRemains,
                        };
                        goodsReceiptDTO.ViewDetails.Add(goodsReceiptDetailDTO);
                    }

                    goodsReceiptDTO.TotalQuantity = goodsReceiptDTO.GetTotalQuantity();

                    this.DoSaveOrDeleteGoodsReceipt(warehouseAdjustment.NMVNTaskID, true, goodsReceiptDTO, null);
                }

                if (saveRelativeOption == SaveRelativeOption.Undo)
                {//NOTES: THIS UNDO REQUIRE: JUST SAVE ONLY ONE GoodsReceipt FOR AN WarehouseAdjustment
                    int?goodsReceiptID = goodsReceiptAPIRepository.GetGoodsReceiptIDofWarehouseAdjustment(warehouseAdjustment.WarehouseAdjustmentID);
                    if (goodsReceiptID != null)
                    {
                        this.DoSaveOrDeleteGoodsReceipt(warehouseAdjustment.NMVNTaskID, false, null, goodsReceiptID);
                    }
                    else
                    {
                        throw new Exception("Lỗi không tìm thấy phiếu nhập kho cũ của phiếu điều chỉnh kho này!" + "\r\n" + "\r\n" + "Vui lòng kiểm tra lại dữ liệu trước khi tiếp tục.");
                    }
                }
            }
        }