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 INLocation_IsCosted_FieldVerifying(PXCache sender, PXFieldVerifyingEventArgs e) { if (e.Row == null) { return; } bool enable; if ((bool?)e.NewValue == true) { INLocationStatus status = PXSelect <INLocationStatus, Where <INLocationStatus.siteID, Equal <Current <INLocation.siteID> >, And <INLocationStatus.locationID, Equal <Current <INLocation.locationID> >, And <INLocationStatus.qtyOnHand, NotEqual <decimal0> > > > > .SelectSingleBound(this, new[] { e.Row }); enable = status == null; } else { INCostStatus status = PXSelect <INCostStatus, Where <INCostStatus.costSiteID, Equal <Current <INLocation.locationID> >, And <INCostStatus.qtyOnHand, Greater <decimal0> > > > .SelectSingleBound(this, new[] { e.Row }); enable = status == null; } if (!enable) { throw new PXSetPropertyException(Messages.LocationCostedWarning, PXErrorLevel.Error); } if ((bool?)e.NewValue == true) { sender.RaiseExceptionHandling <INLocation.isCosted>(e.Row, true, new PXSetPropertyException(Messages.LocationCostedSetWarning, PXErrorLevel.RowWarning)); } }
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); }
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); }
public virtual void UpdateStdCost(INUpdateStdCostRecord itemsite) { using (new PXConnectionScope()) { using (PXTransactionScope ts = new PXTransactionScope()) { //will be true for non-stock items as well if (itemsite.StdCostOverride == false) { PXDatabase.Update <INItemSite>( new PXDataFieldAssign("StdCost", PXDbType.DirectExpression, "PendingStdCost"), new PXDataFieldAssign("StdCostDate", PXDbType.DateTime, itemsite.PendingStdCostDate), new PXDataFieldAssign("PendingStdCost", PXDbType.Decimal, 0m), new PXDataFieldAssign("PendingStdCostDate", PXDbType.DateTime, null), new PXDataFieldAssign("LastStdCost", PXDbType.DirectExpression, "StdCost"), new PXDataFieldRestrict("InventoryID", PXDbType.Int, itemsite.InventoryID), new PXDataFieldRestrict("StdCostOverride", PXDbType.Bit, false), new PXDataFieldRestrict("PendingStdCostDate", PXDbType.DateTime, 4, itemsite.PendingStdCostDate, PXComp.LE)); PXDatabase.Update <InventoryItem>( new PXDataFieldAssign("StdCost", PXDbType.DirectExpression, "PendingStdCost"), new PXDataFieldAssign("StdCostDate", PXDbType.DateTime, itemsite.PendingStdCostDate), new PXDataFieldAssign("PendingStdCost", PXDbType.Decimal, 0m), new PXDataFieldAssign("PendingStdCostDate", PXDbType.DateTime, null), new PXDataFieldAssign("LastStdCost", PXDbType.DirectExpression, "StdCost"), new PXDataFieldRestrict("InventoryID", PXDbType.Int, itemsite.InventoryID), new PXDataFieldRestrict("PendingStdCostDate", PXDbType.DateTime, 4, itemsite.PendingStdCostDate, PXComp.LE)); } else { PXDatabase.Update <INItemSite>( new PXDataFieldAssign("StdCost", PXDbType.DirectExpression, "PendingStdCost"), new PXDataFieldAssign("StdCostDate", PXDbType.DateTime, itemsite.PendingStdCostDate), new PXDataFieldAssign("PendingStdCost", PXDbType.Decimal, 0m), new PXDataFieldAssign("PendingStdCostDate", PXDbType.DateTime, null), new PXDataFieldAssign("LastStdCost", PXDbType.DirectExpression, "StdCost"), new PXDataFieldRestrict("InventoryID", PXDbType.Int, itemsite.InventoryID), new PXDataFieldRestrict("SiteID", PXDbType.Int, itemsite.SiteID), new PXDataFieldRestrict("PendingStdCostDate", PXDbType.DateTime, 4, itemsite.PendingStdCostDate, PXComp.LE)); } if (je.adjustment.Current == null) { je.adjustment.Cache.Insert(); } foreach ( PXResult <INCostStatus, InventoryItem, INSite, INPostClass, INItemSite> res in incoststatus.View.SelectMultiBound(new object[] { je.adjustment.Current, itemsite })) { INCostStatus layer = res; INSite site = res; INTran tran = new INTran(); try { ValidateProjectLocation(site, res); } catch (PXException ex) { PXProcessing <INUpdateStdCostRecord> .SetError(ex.Message); continue; } 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.PendingStdCost)) - layer.TotalCost; je.transactions.Insert(tran); } if (je.transactions.Select().Count == 0) { je.Clear(); } else { je.Save.Press(); PXProcessing <INUpdateStdCostRecord> .SetInfo(PXMessages.LocalizeFormatNoPrefixNLA(Messages.AdjstmentCreated, je.adjustment.Current.RefNbr)); } ts.Complete(); } } }
protected virtual void DefaultUnitCost(PXCache sender, PXFieldUpdatedEventArgs e) { if (adjustment.Current != null && adjustment.Current.OrigModule == INRegister.origModule.PI) { return; } object UnitCost = null; InventoryItem item = (InventoryItem)PXSelectorAttribute.Select <INTran.inventoryID>(sender, e.Row); if (item.ValMethod == INValMethod.Specific && string.IsNullOrEmpty(((INTran)e.Row).LotSerialNbr) == false) { INCostStatus status = PXSelectJoin <INCostStatus, LeftJoin <INLocation, On <INLocation.locationID, Equal <Current <INTran.locationID> > >, InnerJoin <INCostSubItemXRef, On <INCostSubItemXRef.costSubItemID, Equal <INCostStatus.costSubItemID> > > >, Where <INCostStatus.inventoryID, Equal <Current <INTran.inventoryID> >, And2 <Where <INLocation.isCosted, Equal <boolFalse>, And <INCostStatus.costSiteID, Equal <Current <INTran.siteID> >, Or <INCostStatus.costSiteID, Equal <Current <INTran.locationID> > > > >, And <INCostSubItemXRef.subItemID, Equal <Current <INTran.subItemID> >, And <INCostStatus.lotSerialNbr, Equal <Current <INTran.lotSerialNbr> > > > > > > .SelectSingleBound(this, new object[] { e.Row }); if (status != null && status.QtyOnHand != 0m) { UnitCost = PXDBPriceCostAttribute.Round((decimal)(status.TotalCost / status.QtyOnHand)); } } else if (item.ValMethod == INValMethod.FIFO && string.IsNullOrEmpty(((INTran)e.Row).OrigRefNbr) == false) { INCostStatus status = PXSelectJoin <INCostStatus, LeftJoin <INLocation, On <INLocation.locationID, Equal <Current <INTran.locationID> > >, InnerJoin <INCostSubItemXRef, On <INCostSubItemXRef.costSubItemID, Equal <INCostStatus.costSubItemID> > > >, Where <INCostStatus.inventoryID, Equal <Current <INTran.inventoryID> >, And2 <Where <INLocation.isCosted, Equal <boolFalse>, And <INCostStatus.costSiteID, Equal <Current <INTran.siteID> >, Or <INCostStatus.costSiteID, Equal <Current <INTran.locationID> > > > >, And <INCostSubItemXRef.subItemID, Equal <Current <INTran.subItemID> >, And <INCostStatus.receiptNbr, Equal <Current <INTran.origRefNbr> > > > > > > .SelectSingleBound(this, new object[] { e.Row }); if (status != null && status.QtyOnHand != 0m) { UnitCost = PXDBPriceCostAttribute.Round((decimal)(status.TotalCost / status.QtyOnHand)); } } else { if (item.ValMethod == INValMethod.Average) { sender.RaiseFieldDefaulting <INTran.avgCost>(e.Row, out UnitCost); } if (UnitCost == null || (decimal)UnitCost == 0m) { sender.RaiseFieldDefaulting <INTran.unitCost>(e.Row, out UnitCost); } } decimal?qty = (decimal?)sender.GetValue <INTran.qty>(e.Row); if (UnitCost != null && ((decimal)UnitCost != 0m || qty < 0m)) { if ((decimal)UnitCost < 0m) { sender.RaiseFieldDefaulting <INTran.unitCost>(e.Row, out UnitCost); } decimal?unitcost = INUnitAttribute.ConvertToBase <INTran.inventoryID>(sender, e.Row, ((INTran)e.Row).UOM, (decimal)UnitCost, INPrecision.UNITCOST); //suppress trancost recalculation for cost only adjustments if (qty == 0m) { sender.SetValue <INTran.unitCost>(e.Row, unitcost); } else { sender.SetValueExt <INTran.unitCost>(e.Row, unitcost); } } }
protected virtual INRegister AddToAdjustment(INCostStatus layer, decimal?tranCost) { if (tranCost == 0m) { return(null); } var site = INSite.PK.Find(this, layer.CostSiteID); var inventoryItem = InventoryItem.PK.Find(this, layer.InventoryID); bool newAdjustment = true; var adjustment = Adjustments.Find <INRegister.branchID>(site.BranchID); if (adjustment != null) { newAdjustment = false; INTran existTran = SelectFrom <INTran> .Where <INTran.docType.IsEqual <@P.AsString> .And <INTran.refNbr.IsEqual <@P.AsString> .And <INTran.inventoryID.IsEqual <@P.AsInt> .And <INTran.siteID.IsEqual <@P.AsInt> > > > > .View.ReadOnly.SelectWindowed(je, 0, 1, adjustment.DocType, adjustment.RefNbr, layer.InventoryID, layer.CostSiteID); if (existTran != null) { return(null); } if (je.adjustment.Current != adjustment) { SaveAdjustment(); je.adjustment.Current = adjustment; } } var tran = PrepareTransaction(layer, site, inventoryItem, tranCost); if (tran == null) { return(null); } if (newAdjustment) { SaveAdjustment(); adjustment = (INRegister)je.adjustment.Cache.CreateInstance(); adjustment.BranchID = site.BranchID; adjustment = (INRegister)je.adjustment.Cache.Insert(adjustment); } tran = je.transactions.Insert(tran); if (tran == null) { if (newAdjustment) { je.Clear(); } return(null); } if (newAdjustment) { Adjustments.Add(adjustment); } return(adjustment); }