public virtual void UpdateCostAndPrice(List <DocLineExt> docLines) { if (docLines.Count == 0) { return; } FSServiceOrder fsServiceOrderRow = docLines[0].fsServiceOrder; FSSrvOrdType fsSrvOrdTypeRow = docLines[0].fsSrvOrdType; FSPostDoc fsPostDocRow = docLines[0].fsPostDoc; FSAppointment fsAppointmentRow = docLines[0].fsAppointment; if (fsSrvOrdTypeRow.BillingType != ID.SrvOrdType_BillingType.COST_AS_COST) { return; } PMTran pmTranRow; INTran inTranRow, inTranRowUpd; decimal curyUnitCost = 0m; List <PXResult <INTran> > inTranRowsList = null; DocLineExt docLineExtRow = docLines.Where(x => x.fsPostDoc.INDocLineRef != null).FirstOrDefault(); if (docLineExtRow != null && docLineExtRow.fsPostDoc != null && docLineExtRow.fsPostDoc.INDocLineRef != null) { inTranRow = (INTran)docLineExtRow.fsPostDoc.INDocLineRef; inTranRowsList = PXSelect <INTran, Where < INTran.docType, Equal <Required <INTran.docType> >, And < INTran.refNbr, Equal <Required <INTran.refNbr> > > > > .Select(new PXGraph(), inTranRow.DocType, inTranRow.RefNbr) .ToList(); } if (inTranRowsList.Count() > 0) { foreach (DocLineExt docLineExt in docLines) { pmTranRow = (PMTran)docLineExt.fsPostDoc.DocLineRef; inTranRow = (INTran)docLineExt.fsPostDoc.INDocLineRef; if (pmTranRow != null && inTranRow != null) { inTranRowUpd = inTranRowsList.RowCast <INTran>() .Where(x => x.DocType == inTranRow.DocType && x.RefNbr == inTranRow.RefNbr && x.LineNbr == inTranRow.LineNbr) .FirstOrDefault(); if (inTranRowUpd != null) { IPXCurrencyHelper currencyHelper = Base.FindImplementation <IPXCurrencyHelper>(); curyUnitCost = INUnitAttribute.ConvertToBase <PMTran.inventoryID, PMTran.uOM>(Base.Transactions.Cache, pmTranRow, (decimal)inTranRowUpd.UnitCost, INPrecision.NOROUND); if (currencyHelper != null) { currencyHelper.CuryConvCury((decimal)(inTranRowUpd.UnitCost), out curyUnitCost); } else { CM.PXDBCurrencyAttribute.CuryConvCury(Base.Transactions.Cache, Base.BaseCuryInfo.Current, curyUnitCost, out curyUnitCost, true); } pmTranRow.TranCuryUnitRate = Math.Round(curyUnitCost, CommonSetupDecPl.PrcCst, MidpointRounding.AwayFromZero); Base.Transactions.Update(pmTranRow); } } } } }
public override List <DocLineExt> GetInvoiceLines(Guid currentProcessID, int billingCycleID, string groupKey, bool getOnlyTotal, out decimal?invoiceTotal, string postTo) { PXGraph tempGraph = new PXGraph(); if (getOnlyTotal == true) { /* Always keep both BQLs with the same Joins and Where conditions */ FSAppointmentDet fsAppointmentDetRow = PXSelectJoinGroupBy <FSAppointmentDet, InnerJoin <FSAppointment, On <FSAppointment.appointmentID, Equal <FSAppointmentDet.appointmentID> >, InnerJoin <FSServiceOrder, On <FSServiceOrder.sOID, Equal <FSAppointment.sOID> >, InnerJoin <FSSrvOrdType, On <FSSrvOrdType.srvOrdType, Equal <FSServiceOrder.srvOrdType> >, InnerJoin <FSPostDoc, On < FSPostDoc.appointmentID, Equal <FSAppointment.appointmentID>, And <FSPostDoc.entityType, Equal <ListField_PostDoc_EntityType.Appointment> > >, LeftJoin <FSPostInfo, On < FSPostInfo.postID, Equal <FSAppointmentDet.postID> > > > > > >, Where < FSPostDoc.processID, Equal <Required <FSPostDoc.processID> >, And <FSPostDoc.billingCycleID, Equal <Required <FSPostDoc.billingCycleID> >, And <FSPostDoc.groupKey, Equal <Required <FSPostDoc.groupKey> >, And <FSAppointmentDet.lineType, NotEqual <FSLineType.Comment>, And <FSAppointmentDet.lineType, NotEqual <FSLineType.Instruction>, And <FSAppointmentDet.isCanceledNotPerformed, NotEqual <True>, And <FSAppointmentDet.lineType, NotEqual <ListField_LineType_Pickup_Delivery.Pickup_Delivery>, And <FSAppointmentDet.isPrepaid, Equal <False>, And <FSAppointmentDet.isBillable, Equal <True>, And < Where2 < Where < FSAppointmentDet.postID, IsNull>, Or < Where < FSPostInfo.aRPosted, Equal <False>, And <FSPostInfo.aPPosted, Equal <False>, And <FSPostInfo.sOPosted, Equal <False>, And <FSPostInfo.sOInvPosted, Equal <False>, And < Where2 < Where < Required <FSPostBatch.postTo>, NotEqual <FSPostBatch.postTo.SO> >, Or < Where < Required <FSPostBatch.postTo>, Equal <FSPostBatch.postTo.SO>, And <FSPostInfo.iNPosted, Equal <False> > > > > > > > > > > > > > > > > > > > > >, Aggregate < Sum <FSAppointmentDet.billableTranAmt> > > .Select(tempGraph, currentProcessID, billingCycleID, groupKey, postTo, postTo); invoiceTotal = fsAppointmentDetRow.BillableTranAmt; FSAppointmentDet fsAppointmentInventoryItem = PXSelectJoinGroupBy <FSAppointmentDet, InnerJoin <FSAppointment, On <FSAppointment.appointmentID, Equal <FSAppointmentDet.appointmentID> >, InnerJoin <FSServiceOrder, On <FSServiceOrder.sOID, Equal <FSAppointment.sOID> >, InnerJoin <FSSrvOrdType, On <FSSrvOrdType.srvOrdType, Equal <FSServiceOrder.srvOrdType> >, InnerJoin <FSPostDoc, On < FSPostDoc.appointmentID, Equal <FSAppointment.appointmentID>, And <FSPostDoc.entityType, Equal <ListField_PostDoc_EntityType.Appointment> > >, LeftJoin <FSPostInfo, On < FSPostInfo.postID, Equal <FSAppointmentDet.postID> > > > > > >, Where < FSAppointmentDet.lineType, Equal <ListField_LineType_Pickup_Delivery.Pickup_Delivery>, And <FSPostDoc.processID, Equal <Required <FSPostDoc.processID> >, And <FSPostDoc.billingCycleID, Equal <Required <FSPostDoc.billingCycleID> >, And <FSPostDoc.groupKey, Equal <Required <FSPostDoc.groupKey> >, And < Where2 < Where < FSAppointmentDet.postID, IsNull>, Or < Where < FSPostInfo.aRPosted, Equal <False>, And <FSPostInfo.aPPosted, Equal <False>, And <FSPostInfo.sOPosted, Equal <False>, And <FSPostInfo.sOInvPosted, Equal <False>, And < Where2 < Where < Required <FSPostBatch.postTo>, NotEqual <FSPostBatch.postTo.SO> >, Or < Where < Required <FSPostBatch.postTo>, Equal <FSPostBatch.postTo.SO>, And <FSPostInfo.iNPosted, Equal <False> > > > > > > > > > > > > > > > >, Aggregate < Sum <FSAppointmentDet.billableTranAmt> > > .Select(tempGraph, currentProcessID, billingCycleID, groupKey, postTo, postTo); invoiceTotal += fsAppointmentInventoryItem.BillableTranAmt ?? 0; return(null); } else { invoiceTotal = null; /* Always keep both BQLs with the same Joins and Where conditions */ var resultSet1 = PXSelectJoin <FSAppointmentDet, InnerJoin <FSAppointment, On <FSAppointment.appointmentID, Equal <FSAppointmentDet.appointmentID> >, InnerJoin <FSServiceOrder, On <FSServiceOrder.sOID, Equal <FSAppointment.sOID> >, InnerJoin <FSSrvOrdType, On <FSSrvOrdType.srvOrdType, Equal <FSServiceOrder.srvOrdType> >, InnerJoin <FSPostDoc, On < FSPostDoc.appointmentID, Equal <FSAppointment.appointmentID>, And <FSPostDoc.entityType, Equal <ListField_PostDoc_EntityType.Appointment> > >, LeftJoin <FSPostInfo, On < FSPostInfo.postID, Equal <FSAppointmentDet.postID> >, LeftJoin <FSSODet, On <FSSODet.srvOrdType, Equal <FSServiceOrder.srvOrdType>, And <FSSODet.refNbr, Equal <FSServiceOrder.refNbr>, And <FSSODet.sODetID, Equal <FSAppointmentDet.sODetID> > > >, LeftJoin <FSSODetSplit, On <FSSODetSplit.srvOrdType, Equal <FSSODet.srvOrdType>, And <FSSODetSplit.refNbr, Equal <FSSODet.refNbr>, And <FSSODetSplit.lineNbr, Equal <FSSODet.lineNbr>, And <FSSODetSplit.completed, Equal <False> > > > >, LeftJoin <INItemPlan, On <INItemPlan.planID, Equal <FSSODetSplit.planID> >, LeftJoin <PMTask, On <PMTask.taskID, Equal <FSAppointmentDet.projectTaskID> > > > > > > > > > >, Where < FSPostDoc.processID, Equal <Required <FSPostDoc.processID> >, And <FSPostDoc.billingCycleID, Equal <Required <FSPostDoc.billingCycleID> >, And <FSPostDoc.groupKey, Equal <Required <FSPostDoc.groupKey> >, And <FSAppointmentDet.lineType, NotEqual <FSLineType.Comment>, And <FSAppointmentDet.lineType, NotEqual <FSLineType.Instruction>, And <FSAppointmentDet.isCanceledNotPerformed, NotEqual <True>, And <FSAppointmentDet.lineType, NotEqual <ListField_LineType_Pickup_Delivery.Pickup_Delivery>, And <FSAppointmentDet.isPrepaid, Equal <False>, And < Where2 < Where < FSAppointmentDet.postID, IsNull>, Or < Where < FSPostInfo.aRPosted, Equal <False>, And <FSPostInfo.aPPosted, Equal <False>, And <FSPostInfo.sOPosted, Equal <False>, And <FSPostInfo.sOInvPosted, Equal <False>, And < Where2 < Where < Required <FSPostBatch.postTo>, NotEqual <FSPostBatch.postTo.SO> >, Or < Where < Required <FSPostBatch.postTo>, Equal <FSPostBatch.postTo.SO>, And <FSPostInfo.iNPosted, Equal <False> > > > > > > > > > > > > > > > > > > > >, OrderBy < Asc <FSAppointment.executionDate, Asc <FSAppointmentDet.appointmentID, Asc <FSAppointmentDet.appDetID> > > > > .Select(tempGraph, currentProcessID, billingCycleID, groupKey, postTo, postTo); var docLines = new List <DocLineExt>(); foreach (PXResult <FSAppointmentDet, FSAppointment, FSServiceOrder, FSSrvOrdType, FSPostDoc, FSPostInfo, FSSODet, FSSODetSplit, INItemPlan, PMTask> row in resultSet1) { docLines.Add(new DocLineExt(row)); } var resultSet2 = PXSelectJoin <FSAppointmentDet, InnerJoin <FSAppointment, On <FSAppointment.appointmentID, Equal <FSAppointmentDet.appointmentID> >, InnerJoin <FSServiceOrder, On <FSServiceOrder.sOID, Equal <FSAppointment.sOID> >, InnerJoin <FSSrvOrdType, On <FSSrvOrdType.srvOrdType, Equal <FSServiceOrder.srvOrdType> >, InnerJoin <FSPostDoc, On < FSPostDoc.appointmentID, Equal <FSAppointment.appointmentID>, And <FSPostDoc.entityType, Equal <ListField_PostDoc_EntityType.Appointment> > >, LeftJoin <FSPostInfo, On < FSPostInfo.postID, Equal <FSAppointmentDet.postID> >, LeftJoin <PMTask, On <PMTask.taskID, Equal <FSAppointmentDet.projectTaskID> > > > > > > >, Where < FSAppointmentDet.lineType, Equal <ListField_LineType_Pickup_Delivery.Pickup_Delivery>, And <FSPostDoc.processID, Equal <Required <FSPostDoc.processID> >, And <FSPostDoc.billingCycleID, Equal <Required <FSPostDoc.billingCycleID> >, And <FSPostDoc.groupKey, Equal <Required <FSPostDoc.groupKey> >, And < Where2 < Where < FSAppointmentDet.postID, IsNull>, Or < Where < FSPostInfo.aRPosted, Equal <False>, And <FSPostInfo.aPPosted, Equal <False>, And <FSPostInfo.sOPosted, Equal <False>, And <FSPostInfo.sOInvPosted, Equal <False>, And < Where2 < Where < Required <FSPostBatch.postTo>, NotEqual <FSPostBatch.postTo.SO> >, Or < Where < Required <FSPostBatch.postTo>, Equal <FSPostBatch.postTo.SO>, And <FSPostInfo.iNPosted, Equal <False> > > > > > > > > > > > > > > > >, OrderBy < Asc <FSAppointment.executionDate, Asc <FSAppointmentDet.appointmentID, Asc <FSAppointmentDet.appDetID> > > > > .Select(tempGraph, currentProcessID, billingCycleID, groupKey, postTo, postTo); DocLineExt docLineExtRow; foreach (PXResult <FSAppointmentDet, FSAppointment, FSServiceOrder, FSSrvOrdType, FSPostDoc, FSPostInfo, PMTask> row in resultSet2) { docLineExtRow = new DocLineExt(row); docLineExtRow.docLine.AcctID = ServiceOrderCore.Get_TranAcctID_DefaultValue(this, docLineExtRow.fsSrvOrdType.SalesAcctSource, docLineExtRow.docLine.InventoryID, docLineExtRow.docLine.SiteID, docLineExtRow.fsServiceOrder); docLines.Add(docLineExtRow); } return(docLines); } }
public virtual void CreateInvoice(PXGraph graphProcess, List <DocLineExt> docLines, List <DocLineExt> docLinesGrouped, short invtMult, DateTime?invoiceDate, string invoiceFinPeriodID, OnDocumentHeaderInsertedDelegate onDocumentHeaderInserted, OnTransactionInsertedDelegate onTransactionInserted, PXQuickProcess.ActionFlow quickProcessFlow) { if (docLinesGrouped.Count == 0) { return; } bool?initialHold = false; FSServiceOrder fsServiceOrderRow = docLinesGrouped[0].fsServiceOrder; FSSrvOrdType fsSrvOrdTypeRow = docLinesGrouped[0].fsSrvOrdType; FSPostDoc fsPostDocRow = docLinesGrouped[0].fsPostDoc; FSAppointment fsAppointmentRow = docLinesGrouped[0].fsAppointment; Base.FieldDefaulting.AddHandler <INRegister.branchID>((sender, e) => { e.NewValue = fsServiceOrderRow.BranchID; e.Cancel = true; }); INRegister inRegisterRow = new INRegister(); inRegisterRow.DocType = INDocType.Issue; AutoNumberHelper.CheckAutoNumbering(Base, Base.insetup.SelectSingle().IssueNumberingID); inRegisterRow.TranDate = invoiceDate; inRegisterRow.FinPeriodID = invoiceFinPeriodID; inRegisterRow.TranDesc = fsAppointmentRow != null ? fsAppointmentRow.DocDesc : fsServiceOrderRow.DocDesc; inRegisterRow = PXCache <INRegister> .CreateCopy(Base.issue.Insert(inRegisterRow)); initialHold = inRegisterRow.Hold; inRegisterRow.NoteID = null; PXNoteAttribute.GetNoteIDNow(Base.issue.Cache, inRegisterRow); Base.issue.Cache.SetValueExtIfDifferent <INRegister.hold>(inRegisterRow, true); inRegisterRow = Base.issue.Update(inRegisterRow); if (onDocumentHeaderInserted != null) { onDocumentHeaderInserted(Base, inRegisterRow); } IDocLine docLine = null; INTran inTranRow = null; FSxINTran fsxINTranRow = null; PMTask pmTaskRow = null; List <GroupDocLineExt> singleLines = docLines.Where(x => x.docLine.LineType == ID.LineType_ALL.INVENTORY_ITEM).GroupBy( x => new { x.docLine.DocID, x.docLine.LineID }, (key, group) => new GroupDocLineExt(key.DocID, key.LineID, group.ToList())).ToList(); foreach (GroupDocLineExt singleLine in singleLines) { DocLineExt docLineExt = singleLine.Group.First(); docLine = docLineExt.docLine; fsPostDocRow = docLineExt.fsPostDoc; fsServiceOrderRow = docLineExt.fsServiceOrder; fsSrvOrdTypeRow = docLineExt.fsSrvOrdType; fsAppointmentRow = docLineExt.fsAppointment; inTranRow = new INTran(); inTranRow.BranchID = docLine.BranchID; inTranRow.TranType = INTranType.Issue; inTranRow = PXCache <INTran> .CreateCopy(Base.transactions.Insert(inTranRow)); inTranRow.InventoryID = docLine.InventoryID; inTranRow.UOM = docLine.UOM; pmTaskRow = docLineExt.pmTask; if (pmTaskRow != null && pmTaskRow.Status == ProjectTaskStatus.Completed) { throw new PXException(TX.Error.POSTING_PMTASK_ALREADY_COMPLETED, fsServiceOrderRow.RefNbr, docLine.LineRef, pmTaskRow.TaskCD); } if (docLine.ProjectID != null && docLine.ProjectTaskID != null) { inTranRow.ProjectID = docLine.ProjectID; inTranRow.TaskID = docLine.ProjectTaskID; } inTranRow.SiteID = docLine.SiteID; inTranRow.LocationID = docLine.SiteLocationID; inTranRow.TranDesc = docLine.TranDesc; inTranRow.CostCodeID = docLine.CostCodeID; inTranRow.ReasonCode = fsSrvOrdTypeRow.ReasonCode; inTranRow = PXCache <INTran> .CreateCopy(Base.transactions.Update(inTranRow)); INTranSplit currentSplit = Base.splits.Select(); if (fsAppointmentRow == null) { bool qtyAssigned = false; if (currentSplit != null && singleLine.Group != null && singleLine.Group.Count > 0) { Base.splits.Delete(currentSplit); } foreach (DocLineExt splitLine in singleLine.Group) { if (splitLine.fsSODetSplit.SplitLineNbr != null && splitLine.fsSODetSplit.Completed == false) { INTranSplit split = new INTranSplit(); split = Base.splits.Insert(split); INTranSplit copySplit = (INTranSplit)Base.splits.Cache.CreateCopy(split); copySplit.SiteID = splitLine.fsSODetSplit.SiteID != null ? splitLine.fsSODetSplit.SiteID : copySplit.SiteID; copySplit.LocationID = splitLine.fsSODetSplit.LocationID != null ? splitLine.fsSODetSplit.LocationID : copySplit.LocationID; copySplit.LotSerialNbr = splitLine.fsSODetSplit.LotSerialNbr; copySplit.Qty = splitLine.fsSODetSplit.Qty; split = Base.splits.Update(copySplit); qtyAssigned = true; } } inTranRow = (INTran)Base.transactions.Cache.CreateCopy(Base.transactions.Current); if (qtyAssigned == false) { inTranRow.Qty = docLine.GetQty(FieldType.BillableField); } else if (inTranRow.Qty != docLine.GetQty(FieldType.BillableField)) { throw new PXException(TX.Error.QTY_POSTED_ERROR); } } else { bool qtyAssigned = false; if (string.IsNullOrEmpty(docLine.LotSerialNbr) == false) { if (currentSplit != null) { Base.splits.Delete(currentSplit); } INTranSplit split = new INTranSplit(); split = Base.splits.Insert(split); INTranSplit copySplit = (INTranSplit)Base.splits.Cache.CreateCopy(split); copySplit.SiteID = docLine.SiteID; copySplit.LocationID = docLine.SiteLocationID != null ? docLine.SiteLocationID : copySplit.LocationID; copySplit.LotSerialNbr = docLine.LotSerialNbr != null ? docLine.LotSerialNbr : copySplit.LotSerialNbr; copySplit.Qty = docLine.GetQty(FieldType.BillableField); split = Base.splits.Update(copySplit); qtyAssigned = true; } inTranRow = (INTran)Base.transactions.Cache.CreateCopy(Base.transactions.Current); if (qtyAssigned == false) { inTranRow.Qty = docLine.GetQty(FieldType.BillableField); } else if (inTranRow.Qty != docLine.GetQty(FieldType.BillableField)) { throw new PXException(TX.Error.QTY_POSTED_ERROR); } } inTranRow.UnitPrice = docLine.CuryUnitPrice * invtMult; inTranRow.TranAmt = docLine.GetTranAmt(FieldType.BillableField) * invtMult; fsxINTranRow = Base.transactions.Cache.GetExtension <FSxINTran>(inTranRow); fsxINTranRow.Source = docLine.BillingBy; fsxINTranRow.SOID = fsServiceOrderRow.SOID; fsxINTranRow.BillCustomerID = fsServiceOrderRow.CustomerID; fsxINTranRow.CustomerLocationID = fsServiceOrderRow.LocationID; fsxINTranRow.SODetID = docLine.PostSODetID; fsxINTranRow.AppointmentID = docLine.PostAppointmentID; fsxINTranRow.AppointmentDate = fsAppointmentRow?.ExecutionDate; fsxINTranRow.AppDetID = docLine.PostAppDetID; SharedFunctions.CopyNotesAndFiles(Base.transactions.Cache, inTranRow, docLine, fsSrvOrdTypeRow); fsPostDocRow.INDocLineRef = inTranRow = Base.transactions.Update(inTranRow); if (onTransactionInserted != null) { onTransactionInserted(Base, inTranRow); } } inRegisterRow = Base.issue.Update(inRegisterRow); if (Base.insetup.Current?.RequireControlTotal == true) { Base.issue.Cache.SetValueExtIfDifferent <INRegister.controlQty>(inRegisterRow, inRegisterRow.TotalQty); Base.issue.Cache.SetValueExtIfDifferent <INRegister.controlAmount>(inRegisterRow, inRegisterRow.TotalAmount); } if (initialHold != true) { Base.issue.Cache.SetValueExtIfDifferent <INRegister.hold>(inRegisterRow, false); } inRegisterRow = Base.issue.Update(inRegisterRow); }