public virtual IEnumerable AddInvSelBySite(PXAdapter adapter)
        {
            foreach (INSiteStatusSelected line in sitestatus.Cache.Cached)
            {
                if (line.Selected == true && line.QtySelected > 0)
                {
                    INTran newline = PXCache <INTran> .CreateCopy(this.transactions.Insert(new INTran()));

                    newline.SiteID      = line.SiteID;
                    newline.InventoryID = line.InventoryID;
                    newline.ToSiteID    = this.transfer.Current.ToSiteID;
                    newline.SubItemID   = line.SubItemID;
                    newline.UOM         = line.BaseUnit;
                    newline             = PXCache <INTran> .CreateCopy(transactions.Update(newline));

                    newline.LocationID = line.LocationID;
                    newline            = PXCache <INTran> .CreateCopy(transactions.Update(newline));

                    newline.Qty = line.QtySelected;
                    transactions.Update(newline);
                }
            }
            sitestatus.Cache.Clear();
            return(adapter.Get());
        }
 protected virtual void CheckSplitsForSameTask(PXCache sender, INTran row)
 {
     if (row.HasMixedProjectTasks == true)
     {
         sender.RaiseExceptionHandling <INTran.locationID>(row, null, new PXSetPropertyException(Messages.MixedProjectsInSplits));
     }
 }
        protected virtual void INTran_RowPersisting(PXCache sender, PXRowPersistingEventArgs e)
        {
            INTran row = (INTran)e.Row;

            if ((e.Operation & PXDBOperation.Command) == PXDBOperation.Update)
            {
                if (!string.IsNullOrEmpty(row.SOShipmentNbr))
                {
                    if (PXDBQuantityAttribute.Round((decimal)(row.Qty + row.OrigQty)) > 0m)
                    {
                        sender.RaiseExceptionHandling <INTran.qty>(row, row.Qty, new PXSetPropertyException(CS.Messages.Entry_LE, -row.OrigQty));
                    }
                    else if (PXDBQuantityAttribute.Round((decimal)(row.Qty + row.OrigQty)) < 0m)
                    {
                        sender.RaiseExceptionHandling <INTran.qty>(row, row.Qty, new PXSetPropertyException(CS.Messages.Entry_GE, -row.OrigQty));
                    }
                }
            }

            if (((e.Operation & PXDBOperation.Command) == PXDBOperation.Insert || (e.Operation & PXDBOperation.Command) == PXDBOperation.Update))
            {
                CheckForSingleLocation(sender, row);
                CheckSplitsForSameTask(sender, row);
                CheckLocationTaskRule(sender, row);
            }
        }
        protected virtual void INTran_TaskID_FieldDefaulting(PXCache sender, PXFieldDefaultingEventArgs e)
        {
            INTran row = e.Row as INTran;

            if (row == null)
            {
                return;
            }

            if (PM.ProjectAttribute.IsPMVisible(this, BatchModule.IN))
            {
                if (row.LocationID != null)
                {
                    PXResultset <INLocation> result = PXSelectJoin <INLocation,
                                                                    LeftJoin <PMTask, On <PMTask.projectID, Equal <INLocation.projectID>, And <PMTask.taskID, Equal <INLocation.taskID> > > >,
                                                                    Where <INLocation.siteID, Equal <Required <INLocation.siteID> >,
                                                                           And <INLocation.locationID, Equal <Required <INLocation.locationID> > > > > .Select(sender.Graph, row.SiteID, row.LocationID);

                    foreach (PXResult <INLocation, PMTask> res in result)
                    {
                        PMTask task = (PMTask)res;
                        if (task != null && task.TaskCD != null && task.VisibleInIN == true && task.IsActive == true)
                        {
                            e.NewValue = task.TaskCD;
                            return;
                        }
                    }
                }
            }
        }
Beispiel #5
0
        protected virtual void INTran_ToLocationID_FieldDefaulting(PXCache sender, PXFieldDefaultingEventArgs e)
        {
            /// Needed for the posting of Transfer Orders via
            /// <see cref="SO.SOShipmentEntry.PostShipment(INIssueEntry, PXResult{SO.SOOrderShipment, SO.SOOrder}, DocumentList{INRegister}, AR.ARInvoice)"/>

            INTran row = (INTran)e.Row;

            if (row == null || row.DocType != INDocType.Transfer)
            {
                return;
            }

            INItemSite itemSite = PXSelect <INItemSite,
                                            Where <INItemSite.siteID, Equal <Required <INItemSite.siteID> >,
                                                   And <INItemSite.inventoryID, Equal <Required <INItemSite.inventoryID> > > > >
                                  .SelectWindowed(this, 0, 1, row.ToSiteID, row.InventoryID);

            if (itemSite != null)
            {
                e.NewValue = row.SiteID == row.ToSiteID ? itemSite.DfltShipLocationID : itemSite.DfltReceiptLocationID;
                e.Cancel   = true;
            }
            else
            {
                INSite site = (INSite)PXSelectorAttribute.Select <INTran.toSiteID>(sender, row);
                if (site != null)
                {
                    e.NewValue = row.SiteID == row.ToSiteID ? site.ShipLocationID : site.ReceiptLocationID;
                    e.Cancel   = true;
                }
            }
        }
        protected virtual void INTran_RowInserted(PXCache sender, PXRowInsertedEventArgs e)
        {
            INTran row = e.Row as INTran;

            if (row != null && (row.OrigModule == BatchModule.SO || row.OrigModule == BatchModule.PO))
            {
                INRegister doc = (INRegister)PXParentAttribute.SelectParent(sender, e.Row, typeof(INRegister));
                if (doc != null)
                {
                    PXCache cache = issue.Cache;
                    object  copy  = cache.CreateCopy(doc);

                    doc.SOShipmentType = row.SOShipmentType;
                    doc.SOShipmentNbr  = row.SOShipmentNbr;
                    doc.SOOrderType    = row.SOOrderType;
                    doc.SOOrderNbr     = row.SOOrderNbr;
                    doc.POReceiptType  = row.POReceiptType;
                    doc.POReceiptNbr   = row.POReceiptNbr;

                    if (!object.Equals(doc, cache.Current))
                    {
                        cache.Update(doc);
                    }
                    else
                    {
                        if (cache.GetStatus(doc) == PXEntryStatus.Notchanged || cache.GetStatus(doc) == PXEntryStatus.Held)
                        {
                            cache.SetStatus(doc, PXEntryStatus.Updated);
                        }
                        cache.RaiseRowUpdated(doc, copy);
                    }
                }
            }
        }
        protected virtual void INTran_LocationID_FieldDefaulting(PXCache sender, PXFieldDefaultingEventArgs e)
        {
            INTran row = (INTran)e.Row;

            if (row == null)
            {
                return;
            }

            INItemSite itemSite = itemsite.SelectWindowed(0, 1, row.SiteID, row.InventoryID);

            if (itemSite != null)
            {
                e.NewValue = itemSite.DfltReceiptLocationID;
                e.Cancel   = true;
            }
            else
            {
                INSite site = (INSite)PXSelectorAttribute.Select <INTran.siteID>(sender, row);
                if (site != null)
                {
                    e.NewValue = site.ReceiptLocationID;
                    e.Cancel   = true;
                }
            }
        }
        protected virtual void INTran_ProjectID_FieldDefaulting(PXCache sender, PXFieldDefaultingEventArgs e)
        {
            INTran row = e.Row as INTran;

            if (row == null)
            {
                return;
            }

            if (PM.ProjectAttribute.IsPMVisible(this, BatchModule.IN))
            {
                if (row.LocationID != null)
                {
                    PXResultset <INLocation> result = PXSelectJoin <INLocation,
                                                                    LeftJoin <PMProject, On <PMProject.contractID, Equal <INLocation.projectID> > >,
                                                                    Where <INLocation.siteID, Equal <Required <INLocation.siteID> >,
                                                                           And <INLocation.locationID, Equal <Required <INLocation.locationID> > > > > .Select(sender.Graph, row.SiteID, row.LocationID);

                    foreach (PXResult <INLocation, PMProject> res in result)
                    {
                        PMProject project = (PMProject)res;
                        if (project != null && project.ContractCD != null && project.VisibleInIN == true)
                        {
                            e.NewValue = project.ContractCD;
                            return;
                        }
                    }
                }
            }
        }
        public virtual void RevalueInventory(INUpdateStdCostRecord itemsite)
        {
            using (new PXConnectionScope())
            {
                using (PXTransactionScope ts = new PXTransactionScope())
                {
                    if (je.adjustment.Current == null)
                    {
                        je.adjustment.Cache.Insert();
                    }

                    foreach (
                        PXResult <INCostStatus, InventoryItem, INSite, INPostClass, INItemSite> res in
                        incoststatus.View.SelectMultiBound(new object[] { itemsite }))
                    {
                        INCostStatus layer = res;
                        INSite       site  = res;
                        INTran       tran  = new INTran();

                        if (((INCostStatus)res).LayerType == INLayerType.Oversold)
                        {
                            tran.TranType = INTranType.NegativeCostAdjustment;
                        }
                        else
                        {
                            tran.TranType = INTranType.StandardCostAdjustment;
                        }
                        tran.BranchID   = site.BranchID;
                        tran.InvtAcctID = layer.AccountID;
                        tran.InvtSubID  = layer.SubID;
                        tran.AcctID     = INReleaseProcess.GetAccountDefaults <INPostClass.stdCstRevAcctID>
                                              (je, (InventoryItem)res, (INSite)res, (INPostClass)res);
                        tran.SubID = INReleaseProcess.GetAccountDefaults <INPostClass.stdCstRevSubID>
                                         (je, (InventoryItem)res, (INSite)res, (INPostClass)res);

                        tran.InventoryID = layer.InventoryID;
                        tran.SubItemID   = layer.CostSubItemID;
                        tran.SiteID      = layer.CostSiteID;
                        tran.Qty         = 0m;
                        tran.TranCost    = PXDBCurrencyAttribute.BaseRound(this, (decimal)(layer.QtyOnHand * itemsite.StdCost)) -
                                           layer.TotalCost;

                        je.transactions.Insert(tran);
                    }

                    if (je.transactions.Select().Count == 0)
                    {
                        je.Clear();
                    }
                    else
                    {
                        je.Save.Press();
                        PXProcessing <INUpdateStdCostRecord> .SetInfo(string.Format(Messages.AdjstmentCreated, je.adjustment.Current.RefNbr));
                    }

                    ts.Complete();
                }
            }
        }
        protected virtual void CheckForSingleLocation(PXCache sender, INTran row)
        {
            InventoryItem item = (InventoryItem)PXSelectorAttribute.Select(sender, row, sender.GetField(typeof(INTran.inventoryID)));

            if (item != null && item.StkItem == true && row.TaskID != null && row.LocationID == null)
            {
                sender.RaiseExceptionHandling <INTran.locationID>(row, null, new PXSetPropertyException(Messages.RequireSingleLocation));
            }
        }
        protected virtual void INRegister_TransferNbr_FieldVerifying(PXCache sender, PXFieldVerifyingEventArgs e)
        {
            INTran tran = transactions.SelectWindowed(0, 1);

            if (tran != null)
            {
                e.Cancel = true;
            }
        }
Beispiel #12
0
        public virtual void INTran_ReceiptedQty_FieldSelecting(PXCache sender, PXFieldSelectingEventArgs e)
        {
            INTran row = (INTran)e.Row;

            if (row == null)
            {
                return;
            }
            e.ReturnValue = INUnitAttribute.ConvertFromBase <INTran.inventoryID, INTran.uOM>(sender, e.Row, row.ReceiptedBaseQty.GetValueOrDefault(), INPrecision.QUANTITY);;
        }
        protected virtual void INRegister_RowSelected(PXCache sender, PXRowSelectedEventArgs e)
        {
            if (e.Row == null)
            {
                return;
            }

            release.SetEnabled(e.Row != null && ((INRegister)e.Row).Hold == false && ((INRegister)e.Row).Released == false);
            iNEdit.SetEnabled(e.Row != null && ((INRegister)e.Row).Hold == false && ((INRegister)e.Row).Released == false);
            iNRegisterDetails.SetEnabled(e.Row != null && ((INRegister)e.Row).Released == true);
            iNItemLabels.SetEnabled(e.Row != null && ((INRegister)e.Row).Released == true);
            addInvBySite.SetEnabled(e.Row != null && ((INRegister)e.Row).Released == false);

            PXUIFieldAttribute.SetEnabled(sender, e.Row, ((INRegister)e.Row).Released == false && ((INRegister)e.Row).OrigModule == GL.BatchModule.IN);
            PXUIFieldAttribute.SetEnabled <INRegister.refNbr>(sender, e.Row, true);
            PXUIFieldAttribute.SetEnabled <INRegister.totalQty>(sender, e.Row, false);
            PXUIFieldAttribute.SetEnabled <INRegister.totalCost>(sender, e.Row, false);
            PXUIFieldAttribute.SetEnabled <INRegister.status>(sender, e.Row, false);

            sender.AllowInsert = true;
            sender.AllowUpdate = (((INRegister)e.Row).Released == false);
            sender.AllowDelete = (((INRegister)e.Row).Released == false && ((INRegister)e.Row).OrigModule == GL.BatchModule.IN);

            lsselect.AllowInsert = (((INRegister)e.Row).Released == false && ((INRegister)e.Row).OrigModule == GL.BatchModule.IN);
            lsselect.AllowUpdate = (((INRegister)e.Row).Released == false);
            lsselect.AllowDelete = (((INRegister)e.Row).Released == false && ((INRegister)e.Row).OrigModule == GL.BatchModule.IN);

            PXUIFieldAttribute.SetVisible <INRegister.controlQty>(sender, e.Row, (bool)insetup.Current.RequireControlTotal);
            PXUIFieldAttribute.SetVisible <INRegister.controlCost>(sender, e.Row, (bool)insetup.Current.RequireControlTotal);

            INTran tran = transactions.Current ?? transactions.SelectWindowed(0, 1);

            if (tran != null)
            {
                ((INRegister)e.Row).TransferNbr = tran.OrigRefNbr;
            }
            PXUIFieldAttribute.SetEnabled <INRegister.transferNbr>(sender, e.Row, sender.AllowUpdate && tran == null);
            PXUIFieldAttribute.SetEnabled <INRegister.branchID>(sender, e.Row, sender.AllowUpdate && ((INRegister)e.Row).TransferNbr == null);

            if (sender.Graph.IsImport == true && copy != null && sender.ObjectsEqual <INRegister.transferNbr, INRegister.released>(e.Row, copy))
            {
                return;
            }

            PXUIFieldAttribute.SetEnabled <INTran.branchID>(transactions.Cache, null, sender.AllowUpdate && ((INRegister)e.Row).TransferNbr == null);
            PXUIFieldAttribute.SetEnabled <INTran.inventoryID>(transactions.Cache, null, sender.AllowUpdate && ((INRegister)e.Row).TransferNbr == null);
            PXUIFieldAttribute.SetEnabled <INTran.subItemID>(transactions.Cache, null, sender.AllowUpdate && ((INRegister)e.Row).TransferNbr == null);
            PXUIFieldAttribute.SetEnabled <INTran.siteID>(transactions.Cache, null, sender.AllowUpdate && ((INRegister)e.Row).TransferNbr == null);

            PXUIFieldAttribute.SetVisible <INTran.projectID>(transactions.Cache, null, IsPMVisible);
            PXUIFieldAttribute.SetVisible <INTran.taskID>(transactions.Cache, null, IsPMVisible);

            copy = PXCache <INRegister> .CreateCopy((INRegister)e.Row);
        }
Beispiel #14
0
 protected virtual void CheckForSingleLocation(PXCache sender, INTran row)
 {
     if (row.TaskID != null && row.LocationID == null)
     {
         InventoryItem item = InventoryItem.PK.Find(this, row.InventoryID);
         if (item != null && item.StkItem == true && row.LocationID == null)
         {
             sender.RaiseExceptionHandling <INTran.locationID>(row, null, new PXSetPropertyException(Messages.RequireSingleLocation));
         }
     }
 }
        public virtual bool PrepareImportRow(string viewName, IDictionary keys, IDictionary values)
        {
            if (string.Compare(viewName, "transactions", StringComparison.OrdinalIgnoreCase) == 0)
            {
                CorrectKey("DocType", CurrentDocument.Current.DocType, keys, values);
                CorrectKey("RefNbr", CurrentDocument.Current.RefNbr, keys, values);
                INTran tran = PXSelect <INTran, Where <INTran.docType, Equal <Current <INRegister.docType> >, And <INTran.refNbr, Equal <Current <INRegister.refNbr> > > >, OrderBy <Desc <INTran.lineNbr> > > .SelectSingleBound(this, new object[] { CurrentDocument.Current });

                CorrectKey("LineNbr", tran == null? 1 : tran.LineNbr + 1, keys, values);
            }
            return(true);
        }
        protected virtual void CheckLocationTaskRule(PXCache sender, INTran row)
        {
            if (row.TaskID != null)
            {
                INLocation selectedLocation = (INLocation)PXSelectorAttribute.Select(sender, row, sender.GetField(typeof(INTran.locationID)));

                if (selectedLocation != null && selectedLocation.TaskID != row.TaskID)
                {
                    sender.RaiseExceptionHandling <INTran.locationID>(row, selectedLocation.LocationCD,
                                                                      new PXSetPropertyException(Messages.LocationIsMappedToAnotherTask, PXErrorLevel.Warning));
                }
            }
        }
Beispiel #17
0
        protected virtual void CheckLocationTaskRule(PXCache sender, INTran row)
        {
            if (row.TaskID != null)
            {
                INLocation selectedLocation = INLocation.PK.Find(sender.Graph, row.LocationID);

                if (selectedLocation != null && selectedLocation.TaskID != row.TaskID && (selectedLocation.TaskID != null || selectedLocation.ProjectID != null))
                {
                    sender.RaiseExceptionHandling <INTran.locationID>(row, selectedLocation.LocationCD,
                                                                      new PXSetPropertyException(IN.Messages.LocationIsMappedToAnotherTask, PXErrorLevel.Warning));
                }
            }
        }
 protected virtual void INRegister_ToSiteID_FieldUpdated(PXCache sender, PXFieldUpdatedEventArgs e)
 {
     if (e.Row != null)
     {
         foreach (INTran item in this.transactions.Select())
         {
             INTran updated = (INTran)this.transactions.Cache.CreateCopy(item);
             updated.ToSiteID = ((INRegister)e.Row).ToSiteID;
             this.transactions.Cache.Update(updated);
         }
     }
     Set1Step((INRegister)e.Row);
 }
Beispiel #19
0
        protected virtual void INTran_RowPersisting(PXCache sender, PXRowPersistingEventArgs e)
        {
            if (e.Operation.Command() == PXDBOperation.Delete)
            {
                return;
            }

            INTran row = (INTran)e.Row;

            InventoryItem item      = (InventoryItem)PXSelectorAttribute.Select <INTran.inventoryID>(sender, row);
            INLotSerClass itemclass =
                (INLotSerClass)PXSelectorAttribute.Select <InventoryItem.lotSerClassID>(this.Caches[typeof(InventoryItem)], item);

            PXPersistingCheck check =
                ((INTran)e.Row).InvtMult != 0 && (
                    (item != null && item.ValMethod == INValMethod.Specific) ||
                    (itemclass != null &&
                     itemclass.LotSerTrack != INLotSerTrack.NotNumbered &&
                     itemclass.LotSerAssign == INLotSerAssign.WhenReceived &&
                     ((INTran)e.Row).Qty != 0m))
                                 ? PXPersistingCheck.NullOrBlank
                                 : PXPersistingCheck.Nothing;


            PXDefaultAttribute.SetPersistingCheck <INTran.subID>(sender, e.Row, PXPersistingCheck.Null);
            PXDefaultAttribute.SetPersistingCheck <INTran.locationID>(sender, e.Row, PXPersistingCheck.Null);
            PXDefaultAttribute.SetPersistingCheck <INTran.lotSerialNbr>(sender, e.Row, check);

            if (adjustment.Current != null && adjustment.Current.OrigModule != INRegister.origModule.PI && item != null && item.ValMethod == INValMethod.FIFO && ((INTran)e.Row).OrigRefNbr == null)
            {
                bool dropShipPO = false;
                if (row != null && row.POReceiptNbr != null && row.POReceiptLineNbr != null)
                {
                    PO.POReceiptLine pOReceiptLine = PXSelect <PO.POReceiptLine, Where <PO.POReceiptLine.receiptNbr, Equal <Required <PO.POReceiptLine.receiptNbr> >, And <PO.POReceiptLine.lineNbr, Equal <Required <PO.POReceiptLine.lineNbr> > > > > .Select(this, row.POReceiptNbr, row.POReceiptLineNbr);

                    dropShipPO = pOReceiptLine != null && (pOReceiptLine.LineType == PO.POLineType.GoodsForDropShip || pOReceiptLine.LineType == PO.POLineType.NonStockForDropShip);
                }
                if (!dropShipPO && sender.RaiseExceptionHandling <INTran.origRefNbr>(e.Row, null, new PXSetPropertyException(ErrorMessages.FieldIsEmpty, typeof(INTran.origRefNbr).Name)))
                {
                    throw new PXRowPersistingException(typeof(INTran.origRefNbr).Name, null, ErrorMessages.FieldIsEmpty, typeof(INTran.origRefNbr).Name);
                }
            }

            if (item != null && item.ValMethod == INValMethod.Standard && row.TranType == INTranType.Adjustment && row.InvtMult != 0 && row.BaseQty == 0m && row.TranCost != 0m)
            {
                if (sender.RaiseExceptionHandling <INTran.tranCost>(e.Row, row.TranCost, new PXSetPropertyException(Messages.StandardCostNoCostOnlyAdjust)))
                {
                    throw new PXRowPersistingException(typeof(INTran.tranCost).Name, row.TranCost, Messages.StandardCostNoCostOnlyAdjust);
                }
            }
        }
Beispiel #20
0
        protected virtual void SetupManualCostFlag(PXCache inTranCache, INTran tran, bool externalCall)
        {
            if (string.IsNullOrEmpty(adjustment.Current?.PIID) || !externalCall)
            {
                return;
            }

            if (tran == null || tran.Qty <= 0 || tran.ManualCost == true)
            {
                return;
            }

            inTranCache.SetValueExt <INTran.manualCost>(tran, true);
        }
        protected virtual void INTran_LocationID_FieldUpdated(PXCache sender, PXFieldUpdatedEventArgs e)
        {
            INTran row = e.Row as INTran;

            if (row == null)
            {
                return;
            }

            sender.SetDefaultExt <INTran.projectID>(e.Row);            //will set pending value for TaskID to null if project is changed. This is the desired behavior for all other screens.
            if (sender.GetValuePending <INTran.taskID>(e.Row) == null) //To redefault the TaskID in currecnt screen - set the Pending value from NULL to NOTSET
            {
                sender.SetValuePending <INTran.taskID>(e.Row, PXCache.NotSetValue);
            }
            sender.SetDefaultExt <INTran.taskID>(e.Row);
        }
        protected virtual void INTran_RowPersisting(PXCache sender, PXRowPersistingEventArgs e)
        {
            INTran row = (INTran)e.Row;

            if ((e.Operation & PXDBOperation.Command) == PXDBOperation.Update)
            {
                if (!string.IsNullOrEmpty(row.POReceiptNbr))
                {
                    if (PXDBQuantityAttribute.Round(sender, (decimal)(row.Qty + row.OrigQty)) > 0m)
                    {
                        sender.RaiseExceptionHandling <INTran.qty>(row, row.Qty, new PXSetPropertyException(CS.Messages.Entry_LE, -row.OrigQty));
                    }
                    else if (PXDBQuantityAttribute.Round(sender, (decimal)(row.Qty + row.OrigQty)) < 0m)
                    {
                        sender.RaiseExceptionHandling <INTran.qty>(row, row.Qty, new PXSetPropertyException(CS.Messages.Entry_GE, -row.OrigQty));
                    }
                }
            }
        }
        public override void Persist()
        {
            foreach (INTran tran in transactions.Select())
            {
                INTran trtran = PXSelect <INTran, Where <INTran.tranType, Equal <Current <INTran.origTranType> >,
                                                         And <INTran.refNbr, Equal <Current <INTran.origRefNbr> >,
                                                              And <INTran.lineNbr, Equal <Current <INTran.origLineNbr> >,
                                                                   And <INTran.tranType, Equal <INTranType.transfer> > > > > > .SelectSingleBound(this, new object[] { tran });

                if (receipt.Current.Hold != true && trtran != null && trtran.BaseQty != tran.BaseQty)
                {
                    decimal qty  = INUnitAttribute.ConvertFromBase(transactions.Cache, tran.InventoryID, tran.UOM, trtran.BaseQty ?? 0m, INPrecision.QUANTITY);
                    INTran  copy = (INTran)transactions.Cache.CreateCopy(tran);
                    transactions.Update(copy);
                    transactions.Cache.RaiseExceptionHandling <INTran.qty>(tran, tran.Qty, new PXSetPropertyException(CS.Messages.Entry_EQ, qty));
                }
            }

            base.Persist();
        }
        protected virtual void INTran_RowPersisting(PXCache sender, PXRowPersistingEventArgs e)
        {
            INTran row = (INTran)e.Row;

            if ((e.Operation & PXDBOperation.Command) == PXDBOperation.Update)
            {
                if (!string.IsNullOrEmpty(row.POReceiptNbr))
                {
                    if (PXDBQuantityAttribute.Round((decimal)(row.Qty + row.OrigQty)) > 0m)
                    {
                        sender.RaiseExceptionHandling <INTran.qty>(row, row.Qty, new PXSetPropertyException(CS.Messages.Entry_LE, -row.OrigQty));
                    }
                    else if (PXDBQuantityAttribute.Round((decimal)(row.Qty + row.OrigQty)) < 0m)
                    {
                        sender.RaiseExceptionHandling <INTran.qty>(row, row.Qty, new PXSetPropertyException(CS.Messages.Entry_GE, -row.OrigQty));
                    }
                }
            }

            if (((e.Operation & PXDBOperation.Command) == PXDBOperation.Insert || (e.Operation & PXDBOperation.Command) == PXDBOperation.Update))
            {
                if (row.Qty == 0 && row.TranCost > 0)
                {
                    if (row.POReceiptNbr != null && row.POReceiptLineNbr != null && row.POReceiptType != null)
                    {
                        PO.POReceiptLine poreceiptline = new PO.POReceiptLine();
                        poreceiptline.ReceiptType = row.POReceiptType;
                        poreceiptline.LineNbr     = row.POReceiptLineNbr;
                        poreceiptline.ReceiptNbr  = row.POReceiptNbr;
                        throw
                            new PXErrorContextProcessingException(this, poreceiptline, new PXSetPropertyException(Messages.ZeroQtyWhenNonZeroCost));
                    }
                    sender.RaiseExceptionHandling <INTran.qty>(row, row.Qty,
                                                               new PXSetPropertyException(Messages.ZeroQtyWhenNonZeroCost));
                }
                CheckForSingleLocation(sender, row);
                CheckSplitsForSameTask(sender, row);
                CheckLocationTaskRule(sender, row);
            }
        }
Beispiel #25
0
        protected virtual INTran PrepareTransaction(INCostStatus layer, INSite site, InventoryItem inventoryItem, decimal?tranCost)
        {
            try
            {
                ValidateProjectLocation(site, inventoryItem);
            }
            catch (PXException ex)
            {
                PXProcessing <INUpdateStdCostRecord> .SetError(ex.Message);

                return(null);
            }

            INTran tran = new INTran();

            if (layer.LayerType == INLayerType.Oversold)
            {
                tran.TranType = INTranType.NegativeCostAdjustment;
            }
            else
            {
                tran.TranType = INTranType.StandardCostAdjustment;
            }
            tran.BranchID   = site.BranchID;
            tran.InvtAcctID = layer.AccountID;
            tran.InvtSubID  = layer.SubID;
            var postClass = INPostClass.PK.Find(this, inventoryItem.PostClassID);

            tran.AcctID = INReleaseProcess.GetAccountDefaults <INPostClass.stdCstRevAcctID>
                              (je, inventoryItem, site, postClass);
            tran.SubID = INReleaseProcess.GetAccountDefaults <INPostClass.stdCstRevSubID>
                             (je, inventoryItem, site, postClass);

            tran.InventoryID = layer.InventoryID;
            tran.SubItemID   = layer.CostSubItemID;
            tran.SiteID      = layer.CostSiteID;
            tran.Qty         = 0m;
            tran.TranCost    = tranCost;
            return(tran);
        }
Beispiel #26
0
        protected virtual void INTran_ReasonCode_FieldVerifying(PXCache sender, PXFieldVerifyingEventArgs e)
        {
            INTran row = e.Row as INTran;

            if (row != null)
            {
                ReasonCode reasoncd = PXSelect <ReasonCode, Where <ReasonCode.reasonCodeID, Equal <Optional <ReasonCode.reasonCodeID> > > > .Select(this, e.NewValue);

                if (reasoncd != null && row.ProjectID != null && !ProjectDefaultAttribute.IsNonProject(this, row.ProjectID))
                {
                    PX.Objects.GL.Account account = PXSelect <PX.Objects.GL.Account, Where <PX.Objects.GL.Account.accountID, Equal <Required <PX.Objects.GL.Account.accountID> > > > .Select(this, reasoncd.AccountID);

                    if (account != null && account.AccountGroupID == null)
                    {
                        sender.RaiseExceptionHandling <INTran.reasonCode>(e.Row, account.AccountCD, new PXSetPropertyException(PM.Messages.NoAccountGroup, PXErrorLevel.Warning, account.AccountCD));
                    }
                }

                e.Cancel = (reasoncd != null) &&
                           (row.TranType != INTranType.Issue && row.TranType != INTranType.Receipt && reasoncd.Usage == ReasonCodeUsages.Sales || reasoncd.Usage == row.DocType);
            }
        }
Beispiel #27
0
        public virtual IEnumerable InventorySummary(PXAdapter adapter)
        {
            PXCache tCache = transactions.Cache;
            INTran  line   = transactions.Current;

            if (line == null)
            {
                return(adapter.Get());
            }

            InventoryItem item = (InventoryItem)PXSelectorAttribute.Select <INTran.inventoryID>(tCache, line);

            if (item != null && item.StkItem == true)
            {
                INSubItem sbitem = (INSubItem)PXSelectorAttribute.Select <INTran.subItemID>(tCache, line);
                InventorySummaryEnq.Redirect(item.InventoryID,
                                             ((sbitem != null) ? sbitem.SubItemCD : null),
                                             line.SiteID,
                                             line.LocationID);
            }
            return(adapter.Get());
        }
Beispiel #28
0
        protected virtual IEnumerable internalResultRecords()
        {
            InventoryTranHistEnqFilter filter = Filter.Current;

            bool summaryByDay      = filter.SummaryByDay ?? false;
            bool includeUnreleased = filter.IncludeUnreleased ?? false;

            var resultList = new List <PXResult <InventoryTranHistEnqResult, INTran, INTranSplit> >();

            if (filter.InventoryID == null)
            {
                PXDelegateResult emptyResult = new PXDelegateResult();
                emptyResult.IsResultFiltered  = true;
                emptyResult.IsResultSorted    = true;
                emptyResult.IsResultTruncated = true;

                return(emptyResult);                 //empty
            }

            PXSelectBase <INTranSplit> cmd = new PXSelectReadonly2 <INTranSplit,
                                                                    InnerJoin <INTran, On <INTranSplit.FK.Tran>,
                                                                               InnerJoin <INSubItem, On <INTranSplit.FK.SubItem>,
                                                                                          InnerJoin <INSite, On <INTran.FK.Site> > > >,
                                                                    Where <INTranSplit.inventoryID, Equal <Current <InventoryTranHistEnqFilter.inventoryID> >, And <Match <INSite, Current <AccessInfo.userName> > > >,
                                                                    OrderBy <Asc <INTranSplit.docType, Asc <INTranSplit.refNbr, Asc <INTranSplit.lineNbr, Asc <INTranSplit.splitLineNbr> > > > > >(this);

            PXSelectBase <INItemSiteHistByDay> cmdBegBalanceNew = new PXSelectReadonly2 <INItemSiteHistByDay,
                                                                                         InnerJoin <INItemSiteHistDay,
                                                                                                    On <INItemSiteHistDay.inventoryID, Equal <INItemSiteHistByDay.inventoryID>,
                                                                                                        And <INItemSiteHistDay.siteID, Equal <INItemSiteHistByDay.siteID>,
                                                                                                             And <INItemSiteHistDay.subItemID, Equal <INItemSiteHistByDay.subItemID>,
                                                                                                                  And <INItemSiteHistDay.locationID, Equal <INItemSiteHistByDay.locationID>,
                                                                                                                       And <INItemSiteHistDay.sDate, Equal <INItemSiteHistByDay.lastActivityDate> > > > > >,
                                                                                                    InnerJoin <INSubItem,
                                                                                                               On <INSubItem.subItemID, Equal <INItemSiteHistByDay.subItemID> >,
                                                                                                               InnerJoin <INSite, On <INSite.siteID, Equal <INItemSiteHistByDay.siteID> > > > >,
                                                                                         Where <INItemSiteHistByDay.inventoryID, Equal <Current <InventoryTranHistEnqFilter.inventoryID> >,
                                                                                                And <INItemSiteHistByDay.date, Equal <Required <INItemSiteHistByDay.date> >,
                                                                                                     And <Match <INSite, Current <AccessInfo.userName> > > > > >(this);

            if (!SubCDUtils.IsSubCDEmpty(filter.SubItemCD) && PXAccess.FeatureInstalled <FeaturesSet.subItem>())
            {
                cmd.WhereAnd <Where <INSubItem.subItemCD, Like <Current <InventoryTranHistEnqFilter.subItemCDWildcard> > > >();
                cmdBegBalanceNew.WhereAnd <Where <INSubItem.subItemCD, Like <Current <InventoryTranHistEnqFilter.subItemCDWildcard> > > >();
            }

            if (filter.SiteID != null && PXAccess.FeatureInstalled <FeaturesSet.warehouse>())
            {
                cmd.WhereAnd <Where <INTranSplit.siteID, Equal <Current <InventoryTranHistEnqFilter.siteID> > > >();
                cmdBegBalanceNew.WhereAnd <Where <INItemSiteHistByDay.siteID, Equal <Current <InventoryTranHistEnqFilter.siteID> > > >();
            }

            if ((filter.LocationID ?? -1) != -1 && PXAccess.FeatureInstalled <FeaturesSet.warehouseLocation>())        // there are cases when filter.LocationID = -1
            {
                cmd.WhereAnd <Where <INTranSplit.locationID, Equal <Current <InventoryTranHistEnqFilter.locationID> > > >();
                cmdBegBalanceNew.WhereAnd <Where <INItemSiteHistByDay.locationID, Equal <Current <InventoryTranHistEnqFilter.locationID> > > >();
            }

            if ((filter.LotSerialNbr ?? "") != "" && PXAccess.FeatureInstalled <FeaturesSet.lotSerialTracking>())
            {
                cmd.WhereAnd <Where <INTranSplit.lotSerialNbr, Like <Current <InventoryTranHistEnqFilter.lotSerialNbrWildcard> > > >();
            }

            if (!includeUnreleased)
            {
                cmd.WhereAnd <Where <INTranSplit.released, Equal <True> > >();
            }

            decimal cumulativeQty = 0m;

            if (filter.StartDate != null)
            {
                foreach (PXResult <INItemSiteHistByDay, INItemSiteHistDay> res in cmdBegBalanceNew.Select(filter.StartDate))
                {
                    INItemSiteHistByDay byday = res;
                    INItemSiteHistDay   hist  = res;

                    cumulativeQty +=
                        ((byday.LastActivityDate != null && byday.Date != null &&
                          byday.Date.Value.Date == byday.LastActivityDate.Value.Date)
                                            ? hist.BegQty
                                            : hist.EndQty) ?? 0m;
                }

                if (includeUnreleased)
                {
                    INSite site = INSite.PK.Find(this, filter.SiteID);

                    int calendarOrganizationID = PXAccess.GetParentOrganizationID(site?.BranchID) ?? FinPeriod.organizationID.MasterValue;

                    string   TranPeriodID;
                    DateTime?PeriodStartDate;

                    try
                    {
                        TranPeriodID    = FinPeriodRepository.GetPeriodIDFromDate(filter.StartDate, calendarOrganizationID);
                        PeriodStartDate = FinPeriodRepository.PeriodStartDate(TranPeriodID, calendarOrganizationID);
                    }
                    catch (PXFinPeriodException)
                    {
                        TranPeriodID    = null;
                        PeriodStartDate = filter.StartDate;
                    }

                    PXSelectBase <OrganizationFinPeriod> periodCmd =
                        new PXSelectGroupBy <OrganizationFinPeriod,
                                             Where <OrganizationFinPeriod.finPeriodID, LessEqual <Required <OrganizationFinPeriod.finPeriodID> >,
                                                    And <OrganizationFinPeriod.iNClosed, Equal <False>,
                                                         Or <OrganizationFinPeriod.finPeriodID, Equal <Required <OrganizationFinPeriod.finPeriodID> > > > >,
                                             Aggregate <GroupBy <OrganizationFinPeriod.finPeriodID> >,
                                             OrderBy <Asc <OrganizationFinPeriod.finPeriodID> > >(this);

                    List <object> periodCmdParams = new List <object>()
                    {
                        TranPeriodID, TranPeriodID
                    };


                    if (calendarOrganizationID != FinPeriod.organizationID.MasterValue)
                    {
                        periodCmd.WhereAnd <Where <OrganizationFinPeriod.organizationID, Equal <Required <OrganizationFinPeriod.organizationID> > > >();

                        periodCmdParams.Add(calendarOrganizationID);
                    }

                    OrganizationFinPeriod firstOpenOrCurrentClosedPeriod = periodCmd.SelectWindowed(0, 1, periodCmdParams.ToArray());

                    if (firstOpenOrCurrentClosedPeriod != null)
                    {
                        TranPeriodID    = firstOpenOrCurrentClosedPeriod.FinPeriodID;
                        PeriodStartDate = FinPeriodRepository.PeriodStartDate(firstOpenOrCurrentClosedPeriod.FinPeriodID, calendarOrganizationID);

                        PXView v2 = new PXView(this, true, cmd.View.BqlSelect
                                               .WhereAnd <Where <INTranSplit.tranDate, GreaterEqual <Required <INTranSplit.tranDate> > > >()
                                               .WhereAnd <Where <INTranSplit.tranDate, Less <Required <INTranSplit.tranDate> > > >()
                                               .WhereAnd <Where <INTranSplit.released, Equal <False> > >()
                                               .AggregateNew <Aggregate <
                                                                  GroupBy <INTranSplit.inventoryID, GroupBy <INTranSplit.invtMult, Sum <INTranSplit.baseQty> > > > >());

                        int splitStartRow  = 0;
                        int splitTotalRows = 0;

                        foreach (PXResult <INTranSplit> res in v2.Select(new object[0], new object[] { PeriodStartDate, filter.StartDate.Value }, new object[0],
                                                                         new string[0], new bool[0], new PXFilterRow[0], ref splitStartRow, 0, ref splitTotalRows))
                        {
                            INTranSplit tsRec = res;
                            cumulativeQty += (tsRec.InvtMult * tsRec.BaseQty) ?? 0m;
                        }
                    }
                }
            }

            if (filter.StartDate != null)
            {
                cmd.WhereAnd <Where <INTranSplit.tranDate, GreaterEqual <Current <InventoryTranHistEnqFilter.startDate> > > >();
            }

            if (filter.EndDate != null)
            {
                cmd.WhereAnd <Where <INTranSplit.tranDate, LessEqual <Current <InventoryTranHistEnqFilter.endDate> > > >();
            }

            AlterSortsAndFilters(out string[] newSortColumns, out bool[] newDescendings, out bool sortsChanged, out PXFilterRow[] newFilters, out bool filtersChanged);

            //if user clicks last, sorts will be inverted
            //as it is not possible to calculate beginning balance from the end
            //we will select without top from the start and then apply reverse order and select top n records
            //for next page we will ommit startrow to set beginning balance correctly
            //top (n, m) will be set in the outer search results since we do not reset PXView.StartRow to 0
            //Also, user can set a filter or sort by a column from result DAC (sortsChanged = true, filtersChanged = true) and need to get full result set.
            int  startRow           = 0;
            bool allowSelectWithTop = !sortsChanged && !filtersChanged && !PXView.ReverseOrder;
            int  maximumRows        = allowSelectWithTop ? PXView.StartRow + PXView.MaximumRows : 0;
            int  totalRows          = 0;

            PXView selectView = !summaryByDay
                            ? cmd.View
                            : new PXView(this, true,
                                         cmd.View.BqlSelect
                                         .AggregateNew <Aggregate <GroupBy <INTranSplit.tranDate, Sum <INTranSplit.qtyIn, Sum <INTranSplit.qtyOut> > > > >());

            List <object> intermediateResult = selectView.Select(PXView.Currents, new object[] { filter.StartDate },
                                                                 new string[newSortColumns.Length], newSortColumns, newDescendings, newFilters, ref startRow, maximumRows,
                                                                 ref totalRows);

            int gridLineNbr = 0;

            foreach (PXResult <INTranSplit, INTran, INSubItem> it in intermediateResult)
            {
                INTranSplit ts_rec = (INTranSplit)it;
                INTran      t_rec  = (INTran)it;

                if (summaryByDay)
                {
                    InventoryTranHistEnqResult item = new InventoryTranHistEnqResult();
                    item.BegQty      = cumulativeQty;
                    item.TranDate    = ts_rec.TranDate;
                    item.QtyIn       = ts_rec.QtyIn;
                    item.QtyOut      = ts_rec.QtyOut;
                    item.EndQty      = item.BegQty + ts_rec.QtyIn - ts_rec.QtyOut;
                    item.GridLineNbr = ++gridLineNbr;
                    resultList.Add(new PXResult <InventoryTranHistEnqResult, INTran, INTranSplit>(item, null, null));
                    cumulativeQty += (ts_rec.QtyIn - ts_rec.QtyOut) ?? 0m;
                }
                else
                {
                    InventoryTranHistEnqResult item = new InventoryTranHistEnqResult();
                    item.BegQty   = cumulativeQty;
                    item.TranDate = ts_rec.TranDate;
                    item.QtyIn    = ts_rec.QtyIn;
                    item.QtyOut   = ts_rec.QtyOut;
                    item.EndQty   = item.BegQty + ts_rec.QtyIn - ts_rec.QtyOut;

                    item.DocType      = ts_rec.DocType;
                    item.RefNbr       = ts_rec.RefNbr;
                    item.LineNbr      = ts_rec.LineNbr;
                    item.SplitLineNbr = ts_rec.SplitLineNbr;
                    item.GridLineNbr  = ++gridLineNbr;

                    decimal?unitcost;
                    if (filter.ShowAdjUnitCost ?? false)
                    {
                        unitcost = ts_rec.TotalQty != null && ts_rec.TotalQty != 0m ? (ts_rec.TotalCost + ts_rec.AdditionalCost) / ts_rec.TotalQty : 0m;
                    }
                    else
                    {
                        unitcost = ts_rec.TotalQty != null && ts_rec.TotalQty != 0m ? ts_rec.TotalCost / ts_rec.TotalQty : 0m;
                    }

                    item.UnitCost = unitcost;
                    resultList.Add(new PXResult <InventoryTranHistEnqResult, INTran, INTranSplit>(item, t_rec, ts_rec));
                    cumulativeQty += (ts_rec.InvtMult * ts_rec.BaseQty) ?? 0m;
                }
            }


            PXDelegateResult delegateResult = new PXDelegateResult();

            delegateResult.IsResultFiltered  = !filtersChanged;
            delegateResult.IsResultSorted    = !sortsChanged;
            delegateResult.IsResultTruncated = totalRows > resultList.Count;

            if (!PXView.ReverseOrder)
            {
                delegateResult.AddRange(resultList);
            }
            else
            {
                var sortedList = PXView.Sort(resultList);
                delegateResult.AddRange(sortedList.Cast <PXResult <InventoryTranHistEnqResult, INTran, INTranSplit> >());
                delegateResult.IsResultSorted = true;
            }

            return(delegateResult);
        }
        protected virtual IEnumerable internalResultRecords()
        {
            const bool useItemCostHistoryToOptimizeBalanceCalculation = true;             // false to debug and ensure that INItemCostHistory corresponds to INCostTran

            InventoryTranByAcctEnqFilter filter = Filter.Current;

            bool summaryByDay = filter.SummaryByDay ?? false;

            PXUIFieldAttribute.SetVisible <InventoryTranByAcctEnqResult.tranType>(ResultRecords.Cache, null, !summaryByDay);
            PXUIFieldAttribute.SetVisible <InventoryTranByAcctEnqResult.docRefNbr>(ResultRecords.Cache, null, !summaryByDay);
            PXUIFieldAttribute.SetVisible <InventoryTranByAcctEnqResult.subItemCD>(ResultRecords.Cache, null, !summaryByDay);
            PXUIFieldAttribute.SetVisible <InventoryTranByAcctEnqResult.siteID>(ResultRecords.Cache, null, !summaryByDay);
            PXUIFieldAttribute.SetVisible <InventoryTranByAcctEnqResult.locationID>(ResultRecords.Cache, null, !summaryByDay);

            PXUIFieldAttribute.SetVisible <InventoryTranByAcctEnqResult.accountID>(ResultRecords.Cache, null, !summaryByDay);
            PXUIFieldAttribute.SetVisible <InventoryTranByAcctEnqResult.subID>(ResultRecords.Cache, null, !summaryByDay);
            PXUIFieldAttribute.SetVisible <InventoryTranByAcctEnqResult.inventoryID>(ResultRecords.Cache, null, !summaryByDay);

            PXUIFieldAttribute.SetVisible <InventoryTranByAcctEnqResult.costAdj>(ResultRecords.Cache, null, !summaryByDay);
            PXUIFieldAttribute.SetVisible <InventoryTranByAcctEnqResult.finPerNbr>(ResultRecords.Cache, null, !summaryByDay);
            PXUIFieldAttribute.SetVisible <InventoryTranByAcctEnqResult.tranPerNbr>(ResultRecords.Cache, null, !summaryByDay);
            PXUIFieldAttribute.SetVisible <InventoryTranByAcctEnqResult.qty>(ResultRecords.Cache, null, !summaryByDay);
            PXUIFieldAttribute.SetVisible <InventoryTranByAcctEnqResult.unitCost>(ResultRecords.Cache, null, !summaryByDay);

            PXUIFieldAttribute.SetVisible(Tran.Cache, null, !summaryByDay);

            var resultList = new List <PXResult <InventoryTranByAcctEnqResult, INTran> >();

            decimal cumulativeBalance = 0m;

            if (filter.AccountID == null)
            {
                return(resultList);                 //empty
            }

            if (filter.FinPeriodID == null)
            {
                return(resultList);                 //empty
            }

            if (useItemCostHistoryToOptimizeBalanceCalculation)
            {
                PXSelectBase <INItemCostHist> cmd_CostHist = new PXSelectJoinGroupBy <INItemCostHist,

                                                                                      InnerJoin <Sub,
                                                                                                 On <Sub.subID, Equal <INItemCostHist.subID> > >,

                                                                                      Where <INItemCostHist.finPeriodID, Less <Current <InventoryTranByAcctEnqFilter.finPeriodID> > >,

                                                                                      Aggregate <
                                                                                          Sum <INItemCostHist.tranYtdCost,
                                                                                               Sum <INItemCostHist.tranBegCost,
                                                                                                    Sum <INItemCostHist.finYtdCost,
                                                                                                         Sum <INItemCostHist.finBegCost> > > > > >(this);

                //if (filter.AccountID != null) // checked above
                {
                    cmd_CostHist.WhereAnd <Where <INItemCostHist.accountID, Equal <Current <InventoryTranByAcctEnqFilter.accountID> > > >();
                }

                if (!SubCDUtils.IsSubCDEmpty(filter.SubCD))
                {
                    cmd_CostHist.WhereAnd <Where <Sub.subCD, Like <Current <InventoryTranByAcctEnqFilter.subCDWildcard> > > >();
                }

                PXResultset <INItemCostHist> costHistResult = (PXResultset <INItemCostHist>)cmd_CostHist.Select();
                if (costHistResult.Count == 1)                 // 0 is possible too
                {
                    if (filter.ByFinancialPeriod == true)
                    {
                        cumulativeBalance += (((INItemCostHist)costHistResult[0]).FinYtdCost ?? 0m) - (((INItemCostHist)costHistResult[0]).FinBegCost ?? 0m);
                    }
                    else
                    {
                        cumulativeBalance += (((INItemCostHist)costHistResult[0]).TranYtdCost ?? 0m) - (((INItemCostHist)costHistResult[0]).TranBegCost ?? 0m);
                    }
                }
            }

            PXSelectBase <INTranCost> cmd = new PXSelectReadonly2 <INTranCost,
                                                                   InnerJoin <INTran,
                                                                              On <INTran.tranType, Equal <INTranCost.tranType>,
                                                                                  And <INTran.refNbr, Equal <INTranCost.refNbr>,
                                                                                       And <INTran.lineNbr, Equal <INTranCost.lineNbr> > > >,
                                                                              InnerJoin <InventoryItem, On <InventoryItem.inventoryID, Equal <INTranCost.inventoryID>,
                                                                                                            And <Match <InventoryItem, Current <AccessInfo.userName> > > >,
                                                                                         InnerJoin <Sub, On <Sub.subID, Equal <INTranCost.invtSubID> >,
                                                                                                    InnerJoin <INSubItem, On <INSubItem.subItemID, Equal <INTranCost.costSubItemID> >,
                                                                                                               LeftJoin <INSite, On <INSite.siteID, Equal <INTranCost.costSiteID> >,
                                                                                                                         LeftJoin <INLocation, On <INLocation.locationID, Equal <INTranCost.costSiteID> >,
                                                                                                                                   LeftJoin <INCostStatus, On <INTranCost.costID, Equal <INCostStatus.costID> > > > > > > > >,
                                                                   Where <INSite.siteID, IsNull,
                                                                          Or <Match <INSite, Current <AccessInfo.userName> > > >,
                                                                   OrderBy <Asc <INTranCost.tranDate,
                                                                                 Asc <INTranCost.createdDateTime> > > >(this);

            //if (filter.FinPeriodID != null) // checked above
            if (filter.ByFinancialPeriod == true)
            {
                cmd.WhereAnd <Where <INTranCost.finPeriodID, Equal <Current <InventoryTranByAcctEnqFilter.finPeriodID> > > >();
            }
            else
            {
                cmd.WhereAnd <Where <INTranCost.tranPeriodID, Equal <Current <InventoryTranByAcctEnqFilter.finPeriodID> > > >();
            }

            //if (filter.AccountID != null) // checked above
            {
                cmd.WhereAnd <Where <INTranCost.invtAcctID, Equal <Current <InventoryTranByAcctEnqFilter.accountID> > > >();
            }

            if (!SubCDUtils.IsSubCDEmpty(filter.SubCD))
            {
                cmd.WhereAnd <Where <Sub.subCD, Like <Current <InventoryTranByAcctEnqFilter.subCDWildcard> > > >();
            }

            if (filter.EndDate != null)
            {
                cmd.WhereAnd <Where <INTranCost.tranDate, LessEqual <Current <InventoryTranByAcctEnqFilter.endDate> > > >();
            }
            if (filter.InventoryID != null)
            {
                cmd.WhereAnd <Where <INTranCost.inventoryID, Equal <Current <InventoryTranByAcctEnqFilter.inventoryID> > > >();
            }

            if (filter.SiteID != null)
            {
                cmd.WhereAnd <Where <INTranCost.costSiteID, Equal <Current <InventoryTranByAcctEnqFilter.siteID> > > >();
            }

            int gridLineNbr = 0;

            foreach (PXResult <INTranCost, INTran, InventoryItem, Sub, INSubItem, INSite, INLocation, INCostStatus> it in cmd.Select())
            {
                INTranCost   tc_rec = (INTranCost)it;
                INTran       t_rec  = (INTran)it;
                INSite       s_rec  = (INSite)it;
                INLocation   l_rec  = (INLocation)it;
                INSubItem    si_rec = (INSubItem)it;
                INCostStatus cs_rec = (INCostStatus)it;

                decimal rowCost = (tc_rec.InvtMult * tc_rec.TranCost) ?? 0m;

                if (tc_rec.TranDate < filter.StartDate)
                {
                    cumulativeBalance += rowCost;
                }
                else
                {
                    if (summaryByDay)
                    {
                        if ((resultList.Count > 0) && (((InventoryTranByAcctEnqResult)resultList[resultList.Count - 1]).TranDate == tc_rec.TranDate))
                        {
                            InventoryTranByAcctEnqResult lastItem = resultList[resultList.Count - 1];
                            if (rowCost >= 0)
                            {
                                lastItem.Debit += rowCost;
                            }
                            else
                            {
                                lastItem.Credit -= rowCost;
                            }
                            lastItem.EndBalance += rowCost;
                            resultList[resultList.Count - 1] = new PXResult <InventoryTranByAcctEnqResult, INTran>(lastItem, null);
                        }
                        else
                        {
                            InventoryTranByAcctEnqResult item = new InventoryTranByAcctEnqResult();
                            item.BegBalance = cumulativeBalance;
                            item.TranDate   = tc_rec.TranDate;
                            if (rowCost >= 0)
                            {
                                item.Debit  = rowCost;
                                item.Credit = 0m;
                            }
                            else
                            {
                                item.Debit  = 0m;
                                item.Credit = -rowCost;
                            }
                            item.EndBalance      = item.BegBalance + rowCost;
                            item.GridLineNbr     = ++gridLineNbr;
                            item.CreatedDateTime = tc_rec.CreatedDateTime;
                            resultList.Add(new PXResult <InventoryTranByAcctEnqResult, INTran>(item, null));
                        }
                        cumulativeBalance += rowCost;
                    }
                    else
                    {
                        InventoryTranByAcctEnqResult item = new InventoryTranByAcctEnqResult();
                        item.BegBalance = cumulativeBalance;
                        item.TranDate   = tc_rec.TranDate;
                        if (rowCost >= 0)
                        {
                            item.Debit  = rowCost;
                            item.Credit = 0m;
                        }
                        else
                        {
                            item.Debit  = 0m;
                            item.Credit = -rowCost;
                        }
                        item.EndBalance = item.BegBalance + rowCost;

                        item.AccountID   = tc_rec.InvtAcctID;
                        item.SubID       = tc_rec.InvtSubID;
                        item.TranType    = tc_rec.TranType;
                        item.DocType     = t_rec.DocType;
                        item.DocRefNbr   = tc_rec.RefNbr;
                        item.ReceiptNbr  = cs_rec.ReceiptNbr;
                        item.InventoryID = tc_rec.InventoryID;
                        item.SubItemCD   = si_rec.SubItemCD;
                        if (s_rec.SiteID != null)
                        {
                            item.SiteID     = s_rec.SiteID;
                            item.LocationID = null;
                        }
                        else
                        if (l_rec.LocationID != null)                                 //though it's more or less guaranteed
                        {
                            item.SiteID     = l_rec.SiteID;
                            item.LocationID = l_rec.LocationID;
                        }
                        item.TranDate        = tc_rec.TranDate;
                        item.FinPerNbr       = tc_rec.FinPeriodID;
                        item.TranPerNbr      = tc_rec.TranPeriodID;
                        item.Qty             = tc_rec.Qty * tc_rec.InvtMult;
                        item.UnitCost        = (tc_rec.Qty ?? 0m) == 0m ? null : ((tc_rec.TranCost ?? 0m) + (tc_rec.VarCost ?? 0m)) / tc_rec.Qty;
                        item.CostAdj         = tc_rec.CostRefNbr != tc_rec.RefNbr;
                        item.GridLineNbr     = ++gridLineNbr;
                        item.CreatedDateTime = tc_rec.CreatedDateTime;
                        resultList.Add(new PXResult <InventoryTranByAcctEnqResult, INTran>(item, t_rec));
                        cumulativeBalance += rowCost;
                    }
                }
            }
            return(resultList);
        }
        protected virtual IEnumerable internalResultRecords()
        {
            InventoryTranHistEnqFilter filter = Filter.Current;

            bool summaryByDay      = filter.SummaryByDay ?? false;
            bool includeUnreleased = filter.IncludeUnreleased ?? false;

            PXUIFieldAttribute.SetVisible <InventoryTranHistEnqResult.inventoryID>(ResultRecords.Cache, null, false);
            PXUIFieldAttribute.SetVisible <InventoryTranHistEnqResult.finPerNbr>(ResultRecords.Cache, null, false);  //???
            PXUIFieldAttribute.SetVisible <InventoryTranHistEnqResult.tranPerNbr>(ResultRecords.Cache, null, false); //???


            PXUIFieldAttribute.SetVisible <InventoryTranHistEnqResult.tranType>(ResultRecords.Cache, null, !summaryByDay);
            PXUIFieldAttribute.SetVisible <InventoryTranHistEnqResult.docRefNbr>(ResultRecords.Cache, null, !summaryByDay);
            PXUIFieldAttribute.SetVisible <InventoryTranHistEnqResult.subItemID>(ResultRecords.Cache, null, !summaryByDay);
            PXUIFieldAttribute.SetVisible <InventoryTranHistEnqResult.siteID>(ResultRecords.Cache, null, !summaryByDay);
            PXUIFieldAttribute.SetVisible <InventoryTranHistEnqResult.locationID>(ResultRecords.Cache, null, !summaryByDay);
            PXUIFieldAttribute.SetVisible <InventoryTranHistEnqResult.lotSerialNbr>(ResultRecords.Cache, null, !summaryByDay);
            PXUIFieldAttribute.SetVisible(Tran.Cache, null, !summaryByDay);

            var resultList = new List <PXResult <InventoryTranHistEnqResult, INTran> >();

            if (filter.InventoryID == null)
            {
                return(resultList);  //empty
            }

            PXSelectBase <INTranSplit> cmd = new PXSelectReadonly2 <INTranSplit,
                                                                    InnerJoin <INTran,
                                                                               On <INTran.docType, Equal <INTranSplit.docType>,
                                                                                   And <INTran.refNbr, Equal <INTranSplit.refNbr>,
                                                                                        And <INTran.lineNbr, Equal <INTranSplit.lineNbr> > > >,
                                                                               InnerJoin <INSubItem,
                                                                                          On <INSubItem.subItemID, Equal <INTranSplit.subItemID> >,
                                                                                          InnerJoin <INSite, On <INSite.siteID, Equal <INTran.siteID> > > > >,
                                                                    Where <INTranSplit.inventoryID, Equal <Current <InventoryTranHistEnqFilter.inventoryID> >, And <Match <INSite, Current <AccessInfo.userName> > > >,
                                                                    OrderBy <Asc <INTranSplit.docType, Asc <INTranSplit.refNbr, Asc <INTranSplit.lineNbr, Asc <INTranSplit.splitLineNbr> > > > > >(this);

            PXSelectBase <INItemSiteHistByPeriod> cmdBegBalance = new PXSelectReadonly2 <INItemSiteHistByPeriod,
                                                                                         InnerJoin <INItemSiteHist,
                                                                                                    On <INItemSiteHist.inventoryID, Equal <INItemSiteHistByPeriod.inventoryID>,
                                                                                                        And <INItemSiteHist.siteID, Equal <INItemSiteHistByPeriod.siteID>,
                                                                                                             And <INItemSiteHist.subItemID, Equal <INItemSiteHistByPeriod.subItemID>,
                                                                                                                  And <INItemSiteHist.locationID, Equal <INItemSiteHistByPeriod.locationID>,
                                                                                                                       And <INItemSiteHist.finPeriodID, Equal <INItemSiteHistByPeriod.lastActivityPeriod> > > > > >,
                                                                                                    InnerJoin <INSubItem,
                                                                                                               On <INSubItem.subItemID, Equal <INItemSiteHistByPeriod.subItemID> >,
                                                                                                               InnerJoin <INSite, On <INSite.siteID, Equal <INItemSiteHistByPeriod.siteID> > > > >,
                                                                                         Where <INItemSiteHistByPeriod.inventoryID, Equal <Current <InventoryTranHistEnqFilter.inventoryID> >,
                                                                                                And <INItemSiteHistByPeriod.finPeriodID, Equal <Required <INItemSiteHistByPeriod.finPeriodID> >,
                                                                                                     And <Match <INSite, Current <AccessInfo.userName> > > > > >(this);

            if (!SubCDUtils.IsSubCDEmpty(filter.SubItemCD) && PXAccess.FeatureInstalled <FeaturesSet.subItem>())
            {
                cmdBegBalance.WhereAnd <Where <INSubItem.subItemCD, Like <Current <InventoryTranHistEnqFilter.subItemCDWildcard> > > >();
                cmd.WhereAnd <Where <INSubItem.subItemCD, Like <Current <InventoryTranHistEnqFilter.subItemCDWildcard> > > >();
            }

            if (filter.SiteID != null && PXAccess.FeatureInstalled <FeaturesSet.warehouse>())
            {
                cmdBegBalance.WhereAnd <Where <INItemSiteHistByPeriod.siteID, Equal <Current <InventoryTranHistEnqFilter.siteID> > > >();
                cmd.WhereAnd <Where <INTranSplit.siteID, Equal <Current <InventoryTranHistEnqFilter.siteID> > > >();
            }

            if ((filter.LocationID ?? -1) != -1 && PXAccess.FeatureInstalled <FeaturesSet.warehouseLocation>()) // there are cases when filter.LocationID = -1
            {
                cmdBegBalance.WhereAnd <Where <INItemSiteHistByPeriod.locationID, Equal <Current <InventoryTranHistEnqFilter.locationID> > > >();
                cmd.WhereAnd <Where <INTranSplit.locationID, Equal <Current <InventoryTranHistEnqFilter.locationID> > > >();
            }

            if ((filter.LotSerialNbr ?? "") != "" && PXAccess.FeatureInstalled <FeaturesSet.lotSerialTracking>())
            {
                cmd.WhereAnd <Where <INTranSplit.lotSerialNbr, Like <Current <InventoryTranHistEnqFilter.lotSerialNbrWildcard> > > >();
            }

            if (!includeUnreleased)
            {
                cmd.WhereAnd <Where <INTranSplit.released, Equal <True> > >();
            }

            decimal  cumulativeQty = 0m;
            string   TranPeriodID;
            DateTime?PeriodStartDate;
            bool     AnyPeriod = false;

            INSite site = PXSelect <INSite, Where <INSite.siteID, Equal <Required <INSite.siteID> > > > .Select(this, filter.SiteID);

            int calendarOrganizationID = PXAccess.GetParentOrganizationID(site?.BranchID) ?? FinPeriod.organizationID.MasterValue;

            try
            {
                TranPeriodID    = FinPeriodRepository.GetPeriodIDFromDate(filter.StartDate, calendarOrganizationID);
                PeriodStartDate = FinPeriodRepository.PeriodStartDate(TranPeriodID, calendarOrganizationID);
            }
            catch (PXFinPeriodException)
            {
                TranPeriodID    = null;
                PeriodStartDate = filter.StartDate;
            }

            int startRow    = 0;
            int totalRows   = 0;
            int maximumRows = 0;

            if (includeUnreleased)
            {
                PXSelectBase <OrganizationFinPeriod> periodCmd =
                    new PXSelectGroupBy <OrganizationFinPeriod,
                                         Where <OrganizationFinPeriod.finPeriodID, LessEqual <Required <OrganizationFinPeriod.finPeriodID> >,
                                                And <OrganizationFinPeriod.iNClosed, Equal <False>,
                                                     Or <OrganizationFinPeriod.finPeriodID, Equal <Required <OrganizationFinPeriod.finPeriodID> > > > >,
                                         Aggregate <GroupBy <OrganizationFinPeriod.finPeriodID> >,
                                         OrderBy <Asc <OrganizationFinPeriod.finPeriodID> > >(this);

                List <object> periodCmdParams = new List <object>()
                {
                    TranPeriodID, TranPeriodID
                };


                if (calendarOrganizationID != FinPeriod.organizationID.MasterValue)
                {
                    periodCmd.WhereAnd <Where <OrganizationFinPeriod.organizationID, Equal <Required <OrganizationFinPeriod.organizationID> > > >();

                    periodCmdParams.Add(calendarOrganizationID);
                }

                OrganizationFinPeriod firstOpenOrCurrentClosedPeriod = periodCmd.SelectWindowed(0, 1, periodCmdParams.ToArray());

                if (firstOpenOrCurrentClosedPeriod != null)
                {
                    TranPeriodID    = firstOpenOrCurrentClosedPeriod.FinPeriodID;
                    PeriodStartDate = FinPeriodRepository.PeriodStartDate(firstOpenOrCurrentClosedPeriod.FinPeriodID, calendarOrganizationID);
                }

                foreach (PXResult <INItemSiteHistByPeriod, INItemSiteHist> res in cmdBegBalance.Select(TranPeriodID))
                {
                    INItemSiteHistByPeriod byperiod = res;
                    INItemSiteHist         hist     = res;

                    cumulativeQty += string.Equals(byperiod.FinPeriodID, byperiod.LastActivityPeriod) ? (decimal)hist.TranBegQty : (decimal)hist.TranYtdQty;
                    AnyPeriod      = true;
                }

                if (AnyPeriod)
                {
                    //if StartDate is not on the Period border, make additional select with grouping
                    if (PeriodStartDate != filter.StartDate)
                    {
                        PXView v2 = new PXView(this, true, cmd.View.BqlSelect
                                               .WhereAnd <Where <INTranSplit.tranDate, GreaterEqual <Required <INTranSplit.tranDate> > > >()
                                               .WhereAnd <Where <INTranSplit.tranDate, Less <Required <INTranSplit.tranDate> > > >()
                                               .AggregateNew <Aggregate <GroupBy <INTranSplit.inventoryID, GroupBy <INTranSplit.invtMult, Sum <INTranSplit.baseQty> > > > >());

                        foreach (PXResult <INTranSplit> res in v2.Select(new object[0], new object[] { PeriodStartDate, filter.StartDate }, new object[0], new string[0], new bool[0], new PXFilterRow[0], ref startRow, 0, ref totalRows))
                        {
                            INTranSplit ts_rec = res;
                            cumulativeQty += (ts_rec.InvtMult * ts_rec.BaseQty) ?? 0m;
                        }
                    }
                }
                else
                {
                    PXView v2 = new PXView(this, true, cmd.View.BqlSelect
                                           .WhereAnd <Where <INTranSplit.tranDate, Less <Required <INTranSplit.tranDate> > > >()
                                           .AggregateNew <Aggregate <GroupBy <INTranSplit.inventoryID, GroupBy <INTranSplit.invtMult, Sum <INTranSplit.baseQty> > > > >());

                    foreach (PXResult <INTranSplit> res in v2.Select(new object[0], new object[] { filter.StartDate }, new object[0], new string[0], new bool[0], new PXFilterRow[0], ref startRow, 0, ref totalRows))
                    {
                        INTranSplit ts_rec = res;
                        cumulativeQty += (ts_rec.InvtMult * ts_rec.BaseQty) ?? 0m;
                    }
                }
            }
            else
            {
                foreach (PXResult <INItemSiteHistByPeriod, INItemSiteHist> res in cmdBegBalance.Select(TranPeriodID))
                {
                    INItemSiteHistByPeriod byperiod = res;
                    INItemSiteHist         hist     = res;

                    cumulativeQty += string.Equals(byperiod.FinPeriodID, byperiod.LastActivityPeriod) ? (decimal)hist.TranBegQty : (decimal)hist.TranYtdQty;
                    AnyPeriod      = true;
                }

                if (AnyPeriod)
                {
                    //if StartDate is not on the Period border, make additional select with grouping
                    if (PeriodStartDate != filter.StartDate)
                    {
                        PXView v2 = new PXView(this, true, cmd.View.BqlSelect
                                               .WhereAnd <Where <INTranSplit.tranDate, GreaterEqual <Required <INTranSplit.tranDate> > > >()
                                               .WhereAnd <Where <INTranSplit.tranDate, Less <Required <INTranSplit.tranDate> > > >()
                                               .AggregateNew <Aggregate <GroupBy <INTranSplit.inventoryID, GroupBy <INTranSplit.invtMult, Sum <INTranSplit.baseQty> > > > >());

                        foreach (PXResult <INTranSplit> res in v2.Select(new object[0], new object[] { PeriodStartDate, filter.StartDate }, new object[0], new string[0], new bool[0], new PXFilterRow[0], ref startRow, 0, ref totalRows))
                        {
                            INTranSplit ts_rec = res;
                            cumulativeQty += (ts_rec.InvtMult * ts_rec.BaseQty) ?? 0m;
                        }
                    }
                }
            }

            if (filter.StartDate != null)
            {
                cmd.WhereAnd <Where <INTranSplit.tranDate, GreaterEqual <Current <InventoryTranHistEnqFilter.startDate> > > >();
            }

            if (filter.EndDate != null)
            {
                cmd.WhereAnd <Where <INTranSplit.tranDate, LessEqual <Current <InventoryTranHistEnqFilter.endDate> > > >();
            }

            string[] newSortColumns;
            bool[]   newDescendings;
            AlterSorts(out newSortColumns, out newDescendings);
            PXFilterRow[] newFilters = AlterFilters();

            //if user clicks last, sorts will be inverted
            //as it is not possible to calculate beginning balance from the end
            //we will select without top from the start and then apply reverse order and select top n records
            //for next page we will ommit startrow to set beginning balance correctly
            //top (n, m) will be set in the outer search results since we do not reset PXView.StartRow to 0
            startRow    = 0;
            maximumRows = !PXView.ReverseOrder ? PXView.StartRow + PXView.MaximumRows : 0;
            totalRows   = 0;

            PXView selectView = !summaryByDay ? cmd.View
                                : new PXView(this, true, cmd.View.BqlSelect.AggregateNew <Aggregate <GroupBy <INTranSplit.tranDate, Sum <INTranSplit.qtyIn, Sum <INTranSplit.qtyOut> > > > >());

            List <object> intermediateResult = selectView.Select(PXView.Currents, new object[] { filter.StartDate }, new string[newSortColumns.Length], newSortColumns, PXView.Descendings, newFilters, ref startRow, maximumRows, ref totalRows);

            int gridLineNbr = 0;

            foreach (PXResult <INTranSplit, INTran, INSubItem> it in intermediateResult)
            {
                INTranSplit ts_rec = (INTranSplit)it;
                INTran      t_rec  = (INTran)it;

                if (ts_rec.TranDate < filter.StartDate)
                {
                    cumulativeQty += (ts_rec.InvtMult * ts_rec.BaseQty) ?? 0m;
                }
                else
                {
                    if (summaryByDay)
                    {
                        InventoryTranHistEnqResult item = new InventoryTranHistEnqResult();
                        item.BegQty      = cumulativeQty;
                        item.TranDate    = ts_rec.TranDate;
                        item.QtyIn       = ts_rec.QtyIn;
                        item.QtyOut      = ts_rec.QtyOut;
                        item.EndQty      = item.BegQty + ts_rec.QtyIn - ts_rec.QtyOut;
                        item.GridLineNbr = ++gridLineNbr;
                        resultList.Add(new PXResult <InventoryTranHistEnqResult, INTran>(item, null));
                        cumulativeQty += (ts_rec.QtyIn - ts_rec.QtyOut) ?? 0m;
                    }
                    else
                    {
                        InventoryTranHistEnqResult item = new InventoryTranHistEnqResult();
                        item.BegQty   = cumulativeQty;
                        item.TranDate = ts_rec.TranDate;
                        item.QtyIn    = ts_rec.QtyIn;
                        item.QtyOut   = ts_rec.QtyOut;
                        item.EndQty   = item.BegQty + ts_rec.QtyIn - ts_rec.QtyOut;

                        item.InventoryID  = ts_rec.InventoryID;
                        item.TranType     = ts_rec.TranType;
                        item.DocType      = ts_rec.DocType;
                        item.DocRefNbr    = ts_rec.RefNbr;
                        item.SubItemID    = ts_rec.SubItemID;
                        item.SiteID       = ts_rec.SiteID;
                        item.LocationID   = ts_rec.LocationID;
                        item.LotSerialNbr = ts_rec.LotSerialNbr;
                        item.FinPerNbr    = t_rec.FinPeriodID;
                        item.TranPerNbr   = t_rec.TranPeriodID;
                        item.Released     = t_rec.Released;
                        item.GridLineNbr  = ++gridLineNbr;

                        decimal?unitcost;
                        if (filter.ShowAdjUnitCost ?? false)
                        {
                            unitcost = ts_rec.TotalQty != null && ts_rec.TotalQty != 0m ? (ts_rec.TotalCost + ts_rec.AdditionalCost) / ts_rec.TotalQty : 0m;
                        }
                        else
                        {
                            unitcost = ts_rec.TotalQty != null && ts_rec.TotalQty != 0m ? ts_rec.TotalCost / ts_rec.TotalQty : 0m;
                        }

                        item.UnitCost = unitcost;
                        resultList.Add(new PXResult <InventoryTranHistEnqResult, INTran>(item, t_rec));
                        cumulativeQty += (ts_rec.InvtMult * ts_rec.BaseQty) ?? 0m;
                    }
                }
            }
            if (!PXView.ReverseOrder)
            {
                return(resultList);
            }
            return(PXView.Sort(resultList));
        }