avatar
Untitled

Guest 541 7th May, 2024

CSHARP 24.09 KB
                                           
                         public static void transferItem(string _fromWMSLocationId, string _toWMSLocationId,
                        string _ItemId, string _ConfigId, string _InventSizeId, string _InventColorId, decimal _InventQty, string _emplId
                        , string _inventLocationId)
        {
            WMSLocation fromWMSLocation = null;
            WMSLocation toWMSLocation = null;
            WMSPallet toWMSPallet = null;
            string documentTypeId = InventParameters.Find().DefaultMMDocumentTypeId;
            InventDim inventDim = null;
            TransferTable transferTable = null;
            List<Tuple<string, string, decimal>> pickedInventDims = new List<Tuple<string, string, decimal>>();
            string externalJournalId = "";
            bool fastTransfer = false;

            using (NemesisDataBase db = new NemesisDataBase())
            {
                fromWMSLocation = db.WMSLocation.Where(wmsl => wmsl.WMSLocationId == _fromWMSLocationId).FirstOrDefault();
                toWMSLocation = db.WMSLocation.Where(wmsl => wmsl.WMSLocationId == _toWMSLocationId).FirstOrDefault();
            }

            if (fromWMSLocation == null || fromWMSLocation.RecordId == 0)
            {
                InfoLog.Error(string.Format(Labels.Error.WMSLocationNotFound, _fromWMSLocationId), false, true);
                return;
            }

            if (toWMSLocation == null || toWMSLocation.RecordId == 0)
            {
                InfoLog.Error(string.Format(Labels.Error.WMSLocationNotFound, _toWMSLocationId), false, true);
                return;
            }
            if (String.IsNullOrEmpty(toWMSLocation.InputBlockingCauseId) == false)
            {
                InfoLog.Error(string.Format(Labels.Info.LocationIsBlockedForInput, _toWMSLocationId), false, true);
                return;
            }

            /*
            if (string.IsNullOrEmpty(wmsJournalId) == false)
            {
                wmsJournalName = WMSJournalName.Find(wmsJournalId);
            }
            
            if (wmsJournalName == null || wmsJournalName.RecordId == 0)
            {
                InfoLog.Error(string.Format(Labels.Error.WMSJournalOfTransferNotSpecified), false, true);
                return;
            }
            */

            using (NemesisDataBase db = new NemesisDataBase())
            {
                db.ttsBegin();
                toWMSPallet = db.WMSPallet.Where(wp => wp.InventLocationId == toWMSLocation.InventLocationId
                                                    && wp.WMSLocationId == toWMSLocation.WMSLocationId)
                                                    .FirstOrDefault();

                if (toWMSPallet == null || toWMSPallet.RecordId == 0)
                {
                    toWMSPallet = new WMSPallet();
                    toWMSPallet = WMSPallet.InitFromLocation(toWMSLocation, WMSParameters.Find().DefaultWMSPalletId);

                    db.WMSPallet.Add(toWMSPallet);

                    db.SaveChanges();
                }
                string itemId = _ItemId;
                decimal inventQty = _InventQty;

                if (string.IsNullOrEmpty(itemId) || inventQty == 0.0m)
                {
                    return; // continue;
                }

                inventQty = Nemesis.DataBase.BusinessLayer.WMS.TemporaryBufferHelper.moveReservedFromTempBuff(fromWMSLocation
                                                                                                            , toWMSLocation
                                                                                                            , itemId
                                                                                                            , inventQty
                                                                                                            , ""
                                                                                                            );
                if (inventQty <= 0)
                {
                    db.ttsCommit();

                    return; // continue;
                }

                fastTransfer = true;
                if (fromWMSLocation.WMSLocationWithoutPool != toWMSLocation.WMSLocationWithoutPool || fromWMSLocation.InventLocationId != toWMSLocation.InventLocationId)
                {
                    fastTransfer = false;
                }

                if (!fastTransfer)
                {
                    if (!fromWMSLocation.WMSLocationWithoutPool || (fromWMSLocation.WMSLocationWithoutPool && fromWMSLocation.InventLocationId != toWMSLocation.InventLocationId))
                    {
                        var dataArea = SysExternalDatabase.Find("AX").DataAreaId;
                        //czy ne zaburzymy puli?
                        decimal? availPool;
                        using (TransactionScope suppressedScope = new TransactionScope(TransactionScopeOption.Suppress))
                        using (AXDataBase axdb = new AXDataBase())
                        {
                            const string commandText = "SELECT SUM(A.AVAILPOOL)FROM INVENTSUMPOOL_ITP A "
                                                     + "JOIN INVENTDIM B ON B.INVENTDIMID = A.INVENTDIMID "
                                                     + "WHERE A.DATAAREAID = @DATAAREAID "
                                                     + "AND A.ITEMID = @ITEMID "
                                                     + "AND B.DATAAREAID = @DATAAREAID "
                                                     + "AND B.INVENTLOCATIONID = @INVENTLOCATIONID ";

                            SqlParameter[] conParameters = {new SqlParameter("@ITEMID", _ItemId),
                                                    new SqlParameter("@DATAAREAID", dataArea),
                                                    new SqlParameter("@INVENTLOCATIONID", _inventLocationId)
                                                    };
                            availPool = axdb.Database.SqlQuery<decimal?>(commandText, conParameters).FirstOrDefault();
                        }
                        if (availPool < inventQty)
                            InfoLog.Error("Nie można przenieść: " + inventQty + ". Ilość dostępna w puli to: " + availPool);
                    }
                }

                if (!fastTransfer)
                {
                    pickedInventDims.Add(new Tuple<string, string, decimal>(_inventLocationId, "", _InventQty));

                    decimal qtyRemain = _InventQty;
                }

                if (transferTable == null)
                {
                    transferTable = new TransferTable();
                    transferTable.InitValue();
                    transferTable.InitFromDocumentTypeAndLocations(documentTypeId, fromWMSLocation.InventLocationId, toWMSLocation.InventLocationId);
                    db.TransferTable.Add(transferTable);
                    db.SaveChanges();
                }

                #region create line
                inventDim = new InventDim();
                inventDim.InitValue();
                inventDim.ConfigId = "";
                inventDim.InventSizeId = "";
                inventDim.InventColorId = "";
                inventDim.InventSiteId = fromWMSLocation.InventSiteId;
                inventDim.InventLocationId = fromWMSLocation.InventLocationId;
                inventDim.WMSLocationId = fromWMSLocation.WMSLocationId;

                TransferLine transferLine = new TransferLine();
                transferLine.InitValue();
                transferLine.InitFromEntity(transferTable);
                transferLine.ItemId = itemId;
                transferLine.ModifiedField("ItemId");
                transferLine.TransferQty = inventQty;
                transferLine.InventDimId = InventDim.FindOrCreate(inventDim).InventDimId;

                inventDim.InventSiteId = toWMSPallet.InventSiteId;
                inventDim.InventLocationId = toWMSPallet.InventLocationId;
                inventDim.WMSLocationId = toWMSPallet.WMSLocationId;
                inventDim.WMSPalletId = toWMSPallet.WMSPalletId;
                transferLine.ToInventDimId = InventDim.FindOrCreate(inventDim).InventDimId;
                db.TransferLine.Add(transferLine);
                db.SaveChanges();
                #endregion

                #region pick line
                Decimal qtyToPick;
                qtyToPick = Math.Abs(db.InventTrans.Where(it => it.InventTransId == transferLine.InventTransId 
                                                        && it.StatusIssue == StatusIssue.OnOrder
                                                        ).Select(x => x.Qty)
                                                        .DefaultIfEmpty(0.0m)
                                                        .Sum()
                                                        );

                var query = from inventSum in db.InventSum.AsNoTracking()
                            join invDim in db.InventDim on inventSum.InventDimId equals invDim.InventDimId
                            where !inventSum.Closed
                                && inventSum.PhysicalInvent > 0
                                && inventSum.ItemId == transferLine.ItemId
                                 && (invDim.InventSiteId == fromWMSLocation.InventSiteId)
                                 && (invDim.InventLocationId == fromWMSLocation.InventLocationId)
                                 && (invDim.WMSLocationId == fromWMSLocation.WMSLocationId)
                            select new { inventSum, invDim };

                List<string[]> tmpRegisterMap = new List<string[]>();

                foreach (var q in query)
                {
                    var updateQty = qtyToPick > q.inventSum.AvailPhysical ? q.inventSum.AvailPhysical : qtyToPick;

                    if (updateQty > 0)
                    {
                        InventMovement inventMovement = InventMovement.Construct(transferLine);

                        var inventUpdate_Picked1 = new InventUpdate_Picked(inventMovement, q.invDim, updateQty * -1);
                        inventUpdate_Picked1.updateNow();

                        qtyToPick -= updateQty;
                        q.inventSum.PhysicalInvent -= updateQty;

                        if (qtyToPick == 0)
                            break;
                    }
                    else
                    {
                        updateQty = qtyToPick > q.inventSum.PhysicalInvent ? q.inventSum.PhysicalInvent : qtyToPick;

                        if (updateQty > 0)
                        {
                            var unRegisterQuery = from inventTrans in db.InventTrans
                                                  join issueLine in db.IssueLine on inventTrans.InventTransId equals issueLine.InventTransId
                                                  join issueTable in db.IssueTable on issueLine.IssueId equals issueTable.IssueId
                                                  join invDim in db.InventDim on q.invDim.InventDimId equals invDim.InventDimId
                                                  where inventTrans.StatusReceipt == StatusReceipt.None
                                                  && inventTrans.StatusIssue == StatusIssue.ReservPhysical
                                                  && inventTrans.ItemId == itemId
                                                  && issueTable.IssueType == IssueType.RW
                                                  select new { inventTrans };
                            foreach (var urq in unRegisterQuery)
                            {
                                if (updateQty <= 0)
                                {
                                    break;
                                }

                                if (updateQty < -urq.inventTrans.Qty)
                                {
                                    urq.inventTrans.Split(-updateQty, db);
                                }

                                var inventMovement = urq.inventTrans.InventMovement();

                                var inventDimParmFrom = new InventDimParm();
                                inventDimParmFrom.setAllInventDim();

                                var inventUpdate_Reservation = InventUpdate_Reservation.newParameters(inventMovement, q.invDim, inventDimParmFrom, -urq.inventTrans.Qty);
                                inventUpdate_Reservation.updateNow();

                                updateQty -= Math.Abs(urq.inventTrans.Qty);
                                qtyToPick -= Math.Abs(urq.inventTrans.Qty);

                                tmpRegisterMap.Add(new string[] { $"{urq.inventTrans.InventTransId}", $"{urq.inventTrans.Qty}", $"{q.invDim.InventDimId}" });
                            }
                        }
                    }

                }

                if (qtyToPick > 0)
                {
                    db.ttsAbort();
                    InfoLog.Error(Nemesis.Labels.Error.NoSufficientQuantityOfItems, true, true);
                    return;
                }
                #endregion

                if (tmpRegisterMap.Count > 0)
                {
                    bool isAnyNoReservationExists = (from inventSum in db.InventSum.AsNoTracking()
                                                     join invDim in db.InventDim on inventSum.InventDimId equals invDim.InventDimId
                                                     where !inventSum.Closed
                                                         && inventSum.AvailPhysical > 0
                                                         && inventSum.ItemId == itemId
                                                         && (invDim.InventSiteId == toWMSLocation.InventSiteId)
                                                         && (invDim.InventLocationId == toWMSLocation.InventLocationId)
                                                         && (invDim.WMSLocationId == toWMSLocation.WMSLocationId)
                                                     select inventSum).Any();

                    if (isAnyNoReservationExists)
                    {
                        db.ttsAbort();
                        InfoLog.Error(Nemesis.Labels.Error.CannotCombineReservedItemsOnALocation, true, true);
                        return;
                    }
                }
                else
                {
                    bool isAnyReservationExists = (from inventSum in db.InventSum.AsNoTracking()
                                                   join invDim in db.InventDim on inventSum.InventDimId equals invDim.InventDimId
                                                   where !inventSum.Closed
                                                       && inventSum.ReservPhysical > 0
                                                       && inventSum.ItemId == itemId
                                                       && (invDim.InventSiteId == toWMSLocation.InventSiteId)
                                                       && (invDim.InventLocationId == toWMSLocation.InventLocationId)
                                                       && (invDim.WMSLocationId == toWMSLocation.WMSLocationId)
                                                   select inventSum).Any();

                    if (isAnyReservationExists)
                    {
                        db.ttsAbort();
                        InfoLog.Error(Nemesis.Labels.Error.CannotCombineReservedItemsOnALocation, true, true);
                        return;
                    }
                }

                #region register line
                Decimal qtyToRegister;
                qtyToRegister = db.InventTrans.Where(it => it.InventTransId == transferLine.ToInventTransId
                                                        && it.StatusIssue == StatusIssue.None
                                                        && it.StatusReceipt == StatusReceipt.Ordered
                                                        ).Select(x => x.Qty)
                                                        .DefaultIfEmpty(0.0m)
                                                        .Sum()
                                                        ;

                var queryPickedTrans = from inventTransPicked in db.InventTrans
                                       join invDim in db.InventDim on inventTransPicked.InventDimId equals invDim.InventDimId
                            where inventTransPicked.InventTransId == transferLine.InventTransId
                                && inventTransPicked.StatusIssue == StatusIssue.Picked
                            select new { inventTransPicked, invDim };

                foreach (var q in queryPickedTrans)
                {
                    InventMovement inventMovementTo = InventMovement.Construct(transferLine, InventMovSubType.TransferReceipt);
                    InventDim inventDimTo = InventDim.Find(transferLine.ToInventDimId);

                    inventDimTo.InventBatchId = q.invDim.InventBatchId;
                    inventDimTo = InventDim.FindDim(inventDimTo);

                    InventUpdate_Registered inventUpdate_Registered = new InventUpdate_Registered(inventMovementTo
                                                                                                , inventDimTo
                                                                                                // , inventDim
                                                                                                // , inventDimParm
                                                                                                , qtyToRegister
                                                                                                );

                    inventUpdate_Registered.updateNow();
                }

                foreach (var map in tmpRegisterMap)
                {
                    var inventTrans = InventTrans.findByInventTransId(map[0]);
                    var unRegisterQty = Decimal.Parse(map[1]);
                    var inventDimFrom = InventDim.Find(map[2]);

                    var toInventDim = inventDimFrom;
                    toInventDim.WMSLocationId = toWMSLocation.WMSLocationId;
                    toInventDim.InventLocationId = toWMSLocation.InventLocationId;
                    toInventDim.WMSPalletId = toWMSPallet.WMSPalletId;

                    var inventDimParmTo = InventDimParm.active(inventTrans.InventMovement().DimGroupId());

                    var inventUpdate_Reservation = InventUpdate_Reservation.newParameters(inventTrans.InventMovement(), toInventDim, inventDimParmTo, unRegisterQty);
                    inventUpdate_Reservation.updateNow();
                }
                #endregion

                #region post document
                List<TransferTable> transferTableList = new List<TransferTable>();

                transferTableList.Add(transferTable);

                TmpFormLetterParameters postingParameters = new TmpFormLetterParameters();
                postingParameters.InitValue();
                postingParameters.UpdateType = (int)WMSIssueUpdateType.Picked;
                postingParameters.PostingType = (int)WMSPostingType.OrdinaryPosting;

                FormLetter.postDocument(DocumentType.TransferShipJour,
                                        transferTableList.ConvertAll(entity => (DataBase.API.EntityBase)entity),
                                        postingParameters);

                postingParameters = new TmpFormLetterParameters();
                postingParameters.InitValue();
                postingParameters.UpdateType = (int)WMSReceiptUpdateType.Registered;
                postingParameters.PostingType = (int)WMSPostingType.OrdinaryPosting;

                FormLetter.postDocument(DocumentType.TransferReceiveJour,
                                        transferTableList.ConvertAll(entity => (DataBase.API.EntityBase)entity),
                                        postingParameters);

                #endregion

                string toWMSLocationId = toWMSLocation.WMSLocationId;
                string fromWMSLocationId = fromWMSLocation.WMSLocationId;

                updateAllocatedLocationMaxQty(_ItemId, _inventLocationId, toWMSLocationId, _InventQty);

                //InventRepackingDateTable.CreateEntry(fromWMSPallet.WMSPalletId, toWMSPallet.WMSPalletId, _inventLocationId, _ItemId, _InventQty, DateTime.Now, true);

                if (!fastTransfer && pickedInventDims.Count > 0 && tmpRegisterMap.Count == 0)
                {
                    using (WSHelper wsHelper = new WSHelper())
                    {
                        Response response = null;
                        var transferInputData = new FastTransferPoolInputData();

                        var transferInventBatchList = new List<RemoveInventBatch>();

                        foreach (var picked in pickedInventDims)
                        {
                            var transfer = new RemoveInventBatch();
                            transfer.InventLocationId = picked.Item1;
                            transfer.InventBatchId = picked.Item2;
                            transfer.ItemId = _ItemId;
                            transfer.Qty = picked.Item3;
                            transferInventBatchList.Add(transfer);
                        }

                        transferInputData.inventLocationId = _inventLocationId;
                        if (fromWMSLocation.WMSLocationWithoutPool)
                            transferInputData.fromDisabledPool = 1;
                        else
                            transferInputData.fromDisabledPool = 0;

                        if (fromWMSLocation.InventLocationId != toWMSLocation.InventLocationId)
                        {
                            transferInputData.toInventLocationId = toWMSLocation.InventLocationId;
                            if (toWMSLocation.WMSLocationWithoutPool)
                                transferInputData.toDisabledPool = 1;
                            else
                                transferInputData.toDisabledPool = 0;
                        }

                        transferInputData.inventBatchCon = transferInventBatchList.ToArray();


                        response = wsHelper.WSClient.HandleFastTransferPool(transferInputData);

                        if (response.status.status != StatusCode.OK || response.data == null || ((string)response.data == ""))
                        {
                            InfoLog.Error(response.status.message, false);
                            InfoLog.Error(Labels.Error.ErrorInAx);
                        }

                        externalJournalId = (string)response.data;
                    }
                }

                db.ttsCommit();
            }
        }
                      
                                       
To share this paste please copy this url and send to your friends
RAW Paste Data
Recent Pastes
Ta strona używa plików cookie w celu usprawnienia i ułatwienia dostępu do serwisu oraz prowadzenia danych statystycznych. Dalsze korzystanie z tej witryny oznacza akceptację tego stanu rzeczy.
Wykorzystywanie plików Cookie
Jak wyłączyć cookies?
ROZUMIEM