Thursday, August 24, 2017

Payment Document Management Link to General Ledger

For customers who are using Payment Document Management module, they will be in a big need to reconcile their post dated checks against general ledger, and this is almost requested by all our customers.

The below view works to get this resolved! It brings the journal entry number and GL details for each Post Date Document and for both Receivables and Payables checks.

 

/****** Object:  View [dbo].[DI_PDC_GL_Link]    Script Date: 8/24/2017 10:13:57 AM ******/
CREATE VIEW [dbo].[DI_PDC_GL_Link] AS -- Sales Checks (Checks Under Collections)

SELECT GL20000.JRNENTRY AS [Journal Number],
       GL20000.SEQNUMBR AS [GL Line Sequence Number],
       dbo.CM20600.CMXFRNUM AS [Transfer Number],
       dbo.RVLPD011.CMRecordNumber AS [Transfer Record Number],
       dbo.RVLPD011.REMITID AS [Remitance ID],
       dbo.RVLPD011.PMTDOCID AS [Payment Document],
       dbo.RVLPD011.ORICBOOK AS [From Checkbook],
       dbo.RVLPD011.DESCBOOK AS [To Checkbook],
       dbo.RVLPD011.CURNCYID AS Currency,
       dbo.RVLPD011.TRXDATE AS [Transaction Date],
       dbo.RVLPD011.NUMOFTRX AS [Number of Checks],
       dbo.RVLPD011.FUNCTAMT AS [Functional Total Amount],
       dbo.RVLPD011.ORIGAMT AS [Originating Total Amount],
       RVLPD013Sorted.RMDTYPAL AS [Document Type],
       RVLPD013Sorted.DOCNUMBR AS [Document Number],
       dbo.RVLPD009.CUSTNMBR AS [Customer Number],
       dbo.RVLPD009.CUSTNAME AS [Customer Name],
       dbo.RVLPD009.STMTNAME AS [Statement Name],
       dbo.RVLPD009.CHEKNMBR AS [Check Number],
       dbo.RVLPD009.CHEKBKID AS [Checkbook ID],
       dbo.RVLPD009.DOCDATE AS [Check Date],
       dbo.RVLPD009.DUEDATE AS [Due Date],
       RVLPD009.DOCAMNT AS [Check Amount],
       RVLPD013Sorted.Sequence
FROM dbo.CM20600
LEFT OUTER JOIN dbo.RVLPD011 ON dbo.CM20600.Xfr_Record_Number = dbo.RVLPD011.CMRecordNumber
LEFT OUTER JOIN
  (SELECT *,
          CASE
              WHEN ROW_NUMBER() OVER (Partition BY REMITID
                                      ORDER BY Dex_Row_ID) = 1 THEN 1
              ELSE (ROW_NUMBER() OVER (Partition BY REMITID
                                       ORDER BY Dex_Row_ID)*2+1)-2
          END * 16384 AS SEQUENCE
   FROM RVLPD013) RVLPD013Sorted ON dbo.RVLPD011.REMITID = RVLPD013Sorted.REMITID
LEFT OUTER JOIN dbo.RVLPD009 ON RVLPD013Sorted.DOCNUMBR = dbo.RVLPD009.DOCNUMBR
LEFT OUTER JOIN GL20000 ON ORCTRNUM = dbo.RVLPD011.REMITID
AND SOURCDOC = 'RMPDC'
AND RVLPD013Sorted.Sequence = SEQNUMBR
WHERE GL20000.JRNENTRY IS NOT NULL
UNION ALL --Purchasing Checks (Deferred Checks)

SELECT GL20000.JRNENTRY AS [Journal Number],
       GL20000.SEQNUMBR AS [GL Line Sequence Number],
       dbo.CM20600.CMXFRNUM AS [Transfer Number],
       dbo.RVLPD027.CMRecordNumber AS [Transfer Record Number],
       dbo.RVLPD027.REMITID AS [Remitance ID],
       dbo.RVLPD027.PMTDOCID AS [Payment Document],
       dbo.RVLPD027.ORICBOOK AS [From Checkbook],
       dbo.RVLPD027.DESCBOOK AS [To Checkbook],
       dbo.RVLPD027.CURNCYID AS Currency,
       dbo.RVLPD027.TRXDATE AS [Transaction Date],
       dbo.RVLPD027.NUMOFTRX AS [Number of Checks],
       dbo.RVLPD027.FUNCTAMT AS [Functional Total Amount],
       dbo.RVLPD027.ORIGAMT AS [Originating Total Amount],
       RVLPD029Sorted.DOCTYPE AS [Document Type],
       RVLPD029Sorted.DOCNUMBR AS [Document Number],
       RVLPD025.VENDORID AS [Vendor Number],
       RVLPD025.VENDNAME AS [Vendor Name],
       RVLPD025.VNDCHKNM AS [Statement Name],
       RVLPD025.CHEKNMBR AS [Check Number],
       RVLPD025.CHEKBKID AS [Checkbook ID],
       RVLPD025.DOCDATE AS [Check Date],
       RVLPD025.DUEDATE AS [Due Date],
       RVLPD025.DOCAMNT AS [Check Amount],
       RVLPD029Sorted.Sequence
FROM dbo.CM20600
LEFT OUTER JOIN dbo.RVLPD027 ON dbo.CM20600.Xfr_Record_Number = dbo.RVLPD027.CMRecordNumber
LEFT OUTER JOIN
  (SELECT *,
          CASE
              WHEN ROW_NUMBER() OVER (Partition BY REMITID
                                      ORDER BY Dex_Row_ID) = 1 THEN 1
              ELSE (ROW_NUMBER() OVER (Partition BY REMITID
                                       ORDER BY Dex_Row_ID)*2+1)-2
          END * 16384 AS SEQUENCE
   FROM RVLPD029) RVLPD029Sorted ON dbo.RVLPD027.REMITID = RVLPD029Sorted.REMITID
LEFT OUTER JOIN
  (SELECT *
   FROM RVLPD025
   UNION ALL SELECT *
   FROM RVLPD026) RVLPD025 ON RVLPD029Sorted.DOCNUMBR = RVLPD025.DOCNUMBR
LEFT OUTER JOIN GL20000 ON ORCTRNUM = dbo.RVLPD027.REMITID
AND SOURCDOC = 'PMPDC'
AND RVLPD029Sorted.Sequence = SEQNUMBR
WHERE GL20000.JRNENTRY IS NOT NULL

Hope that this helps.

Regards,

--
Mohammad R. Daoud MVP - MCT
MCP, MCBMSP, MCTS, MCBMSS
+962 - 79 - 999 65 85
me@mohdaoud.com
http://www.di.jo

Monday, March 13, 2017

taAnalyticsDistribution Procedure Not working!

Today I been working on an interesting case with eConnect, I been trying to call taAnalyticsDistribution using normal SQL Query but I didn’t find the impact of the run on Dynamics GP and it was always returning the value “0” in ErrorState which should mean that the command was executed properly:

EXEC    @return_value = [dbo].[taAnalyticsDistribution]
        @I_vDOCNMBR = N'3464',
        @I_vDOCTYPE = 0,
        @I_vAMOUNT = 50.35,
        @I_vACTNUMST = N'000-1100-00',
        @I_vaaTrxDim = N'DEPARTMENT',
        @I_vaaTrxDimCode = N'1',
        @I_vaaSubLedgerAssignID=1,
        @I_vUpdateIfExists=1,
        @O_iErrorState = @O_iErrorState OUTPUT,
        @oErrString = @oErrString OUTPUT

I tried to break the code to see what’s going on and therefore installed a decryption application to decrypt the SP and ran an SQL trace, where I realized  that the procedure is indeed not working as expected and not retuning anything due to a misplaced “RETURN” Command below:

image

Removing this command fixed my case! I don’t want to publish the full code of the procedure as might be having differences between every version of the eConnect and therefore I am providing a way to get this resolved on your environment.

Hope that this helps.

Regards,

--
Mohammad R. Daoud MVP - MCT
MCP, MCBMSP, MCTS, MCBMSS
+962 - 79 - 999 65 85
me@mohdaoud.com
http://www.di.jo

Wednesday, August 31, 2016

Dynamics GP Users: the creation of login failed for unknown reason. Contact your SQL Server Administrator for assistance.

 

One of my community fellows has reported a challenging case this evening, he been trying to add a user to Dynamics GP and unable to succeed with this operation, and the obvious cause for this message was that the user could be already added to your database and not added to your GP nor your SQL level logins but wasn’t the case, the message is showing on any name you selects and while running under the “sa” user.

When you create the user at the SQL level it creates with no issues, you only have a problem while creating the user from Dynamics GP, therefore I tried a fresh copy of Dynamics GP and tried to create users with no luck, been sure that the “sa” is a system administrator on the SQL level and the “sa” is the owner of the DYNAMICS and other companies databases.

Digging deeply into this I ran SQL profiler trace to see what’s going on and realized that everything is working as desired, I tried to create the user using the CREATE LOGIN command and it  creates the user perfectly! But noticed upon creating the user by using the command that the returned message wasn’t “Command Completed Successfully” it was “Mail Queued”, where I looked further into this and realized that the customer was having a DDL Trigger on the server level that tracks the changes on the Logins and sends an email to the Administrators informing them about this change following the best practices! And this was the real cause behind the issue, it seems that GP wasn’t receiving the expected result from adding this user and been generating unknown error, disabling the trigger got this issue to be fixed!

Hope that this helps.

Regards,

--
Mohammad R. Daoud MVP - MCT
MCP, MCBMSP, MCTS, MCBMSS
+962 - 79 - 999 65 85
me@mohdaoud.com
http://www.di.jo

Tuesday, June 21, 2016

Msg 515, Level 16, State 2, Procedure aagCreateGLWorkSLRealTimePostForApply, Line 82 Cannot insert the value NULL into column 'GLPOSTDT', table 'AFDCO.dbo.AAG10000'; column does not allow nulls. INSERT fails.

This morning I an issue reported from one of my customers, they are getting the below message when they try to apply multi-currency transactions:

image

I went through a long investigation process, after few hours it turned to be orphaned records in GL100001 with JRNENTRY = 0!

image

Deleting these record fixed the problem! Hope that this helps.

Regards,

--
Mohammad R. Daoud MVP - MCT
MCP, MCBMSP, MCTS, MCBMSS
+962 - 79 - 999 65 85
me@mohdaoud.com
http://www.di.jo

Monday, June 6, 2016

Inventory not changing account to the correct site account for all transactions

A wired case was reported this morning, our customer is having several inventory accounts one for each warehouse, they wanted the system to change the account upon changing the site, which is covered in the default Dynamics GP functionality, the setup for this is explained in the following article:

https://community.dynamics.com/gp/f/32/t/157821

The customer was having the correct setup, but the system wasn’t changing the account, which is normally occurs if the target account doesn’t exist, after checking I realized that accounts were there, but the system is also not flipping the accounts.

After further investigations it turned to be missing accounts in GL00105, accounts were exist in GL00100 but not in GL00105, I ran check links for financials and everything worked as expected!

Hope that this helps.

Regards,

--
Mohammad R. Daoud MVP - MCT
MCP, MCBMSP, MCTS, MCBMSS
+962 - 79 - 999 65 85
me@mohdaoud.com
http://www.di.jo

Monday, May 30, 2016

Dynamics GP Notes for Arabic Characters

I been away from community for the last few months, been engaged in several projects and in learning Dynamics AX following the treand!

Back to track, I installed back the “Open Live Writer” as the “Windows Live Writer” is not supporting blogger! And blogging now about issues I seen worth to be posted.

In this article I am covering an old issue that comes with Dynamics GP, whenever you type Arabic to Dynamics GP notes you realize the paid behind this which leaves users using NOTEPAD to type and then paste it back to GP.

image

I have created an small AddOn that fixes this for users and I am sharing this over the community, you just email me to send you the package, the tool is just allowing you to write your text inside a user-friendly textbox and whenever you are done it copies back your text to the original note:

image

Hope that this helps!

Regards,

--
Mohammad R. Daoud MVP - MCT
MCP, MCBMSP, MCTS, MCBMSS
+962 - 79 - 999 65 85
me@mohdaoud.com
http://www.di.jo

Wednesday, May 20, 2015

Dynamics GP 2015 eConnect error: There is an active Purchase Order Approval workflow. Use Dynamics GP to enter this data

  This morning I got an issue with one of my customers, they were getting the following error upon integrating Receiving Transactions

Sql procedure error codes returned:

Error Number = 12013  Stored Procedure= taPopRcptLineInsert  Error Description = There is an active Purchase Order Approval workflow. Use Dynamics GP to enter this data
Node Identifier Parameters: taPopRcptLineInsert
PONUMBER = 150686

<taPopRcptLineInsert>
  <POPTYPE>1</POPTYPE>
  <POPRCTNM>PMRR000000004316</POPRCTNM>
  <PONUMBER>150686</PONUMBER>
  <ITEMNMBR>P-XXXXX</ITEMNMBR>
  <VENDORID>2042           </VENDORID>
  <VNDITNUM>P-XXXX</VNDITNUM>
  <UOFM>UNIT     </UOFM>
  <UNITCOST>30.500</UNITCOST>
  <EXTDCOST>7320.000</EXTDCOST>
  <QTYSHPPD>240.000</QTYSHPPD>
  <LOCNCODE>MAIN       </LOCNCODE>
  <POLNENUM>32768</POLNENUM>
</taPopRcptLineInsert>

This doesn’t make sense, the workflow is activated for the workflow and the Purchase Order used for this receiving is approved!

Nothing on the internet is available explaining this issue, and no place else to look as eConnect Stored Procedures are encrypted.

To get this invistigated, I installed dbFrog SQL Decryptor to decrypt the eConnect procedure and to see whats going on, and found that the procedure is checking if the workflow is activated and if it was activated it checks if the purchase order workflow_status is not equal to “9”, while the purchase order “Completed” status is “6” not “9”:

image

I modified the procedure to reflect my customer change and it worked like a charm, below is the updated procedure after the change if your system database name is DYNAMICS, otherwise you need to change it:

ALTER PROCEDURE dbo.taPopRcptLineInsert @I_vPOPTYPE smallint, @I_vPOPRCTNM char(17), @I_vPONUMBER char(17) = '', @I_vITEMNMBR char(30) = '', @I_vITEMDESC char(100) = NULL, @I_vVENDORID char(15), @I_vRCPTLNNM int = 0, @I_vVNDITNUM char(30) = '', @I_vVNDITDSC char(100) = '', @I_vACTLSHIP datetime = '', @I_vINVINDX int = 0, @I_vInventoryAccount varchar(75) = '', @I_vUOFM char(8) = '', @I_vUNITCOST numeric(19, 5) = NULL, @I_vEXTDCOST numeric(19, 5) = NULL, @I_vNONINVEN smallint = 0, @I_vJOBNUMBR char(17) = '', @I_vBOLPRONUMBER char(30) = '', @I_vQTYSHPPD numeric(19, 5), @I_vQTYINVCD numeric(19, 5) = 0, @I_vAUTOCOST int = 0, @I_vPurchase_IV_Item_Taxable smallint = 2, @I_vPurchase_Item_Tax_Schedu char(15) = '', @I_vPurchase_Site_Tax_Schedu char(15) = '', @I_vTAXAMNT numeric(19, 5) = 0, @I_vLanded_Cost_Group_ID char(15) = NULL, @I_vLOCNCODE char(10) = '', @I_vPOLNENUM int = 0, @I_vreceiptdate datetime = '', @I_vCURNCYID char(15) = '', @I_vProjNum char(15) = NULL, @I_vCostCatID char(15) = NULL, @I_vAutoAssignBin smallint = 1, @I_vCMMTTEXT varchar(500) = NULL, @I_vRequesterTrx smallint = 0, @I_vUSRDEFND1 char(50) = '', @I_vUSRDEFND2 char(50) = '', @I_vUSRDEFND3 char(50) = '', @I_vUSRDEFND4 varchar(8000) = '', @I_vUSRDEFND5 varchar(8000) = '', @O_iErrorState int OUTPUT, @oErrString varchar(255) OUTPUT WITH ENCRYPTION
AS
  SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
  SET NOCOUNT ON
  DECLARE @PACONTNUMBER char(11),
          @LCCOST numeric(19, 5),
          @Total_Landed_Cost_Amount numeric(19, 5),
          @LCLINENUMBER int,
          @Landed_Cost_ID char(15),
          @LOFSGMNT int,
          @LOFSGMNTALL int,
          @LOFSGMNTEND int,
          @ACCNT_STRING char(100),
          @NEW_ACCNT_STRING varchar(100),
          @MAXSEG int,
          @ACTINDX int,
          @ACSGFLOC smallint,
          @Location_Segment char(67),
          @RcptLineNoteIDArray_1 numeric(19, 5),
          @RcptLineNoteIDArray_2 numeric(19, 5),
          @RcptLineNoteIDArray_3 numeric(19, 5),
          @RcptLineNoteIDArray_4 numeric(19, 5),
          @RcptLineNoteIDArray_5 numeric(19, 5),
          @RcptLineNoteIDArray_6 numeric(19, 5),
          @RcptLineNoteIDArray_7 numeric(19, 5),
          @RcptLineNoteIDArray_8 numeric(19, 5),
          @DECPLCUR smallint,
          @DECPLQTY smallint,
          @ODECPLCU smallint,
          @JOBNUMBR char(17),
          @INVINDX int,
          @VCTNMTHD smallint,
          @ITMTRKOP smallint,
          @UOFM char(8),
          @PRCHSUOM char(8),
          @UMQTYINB numeric(19, 5),
          @ITEMNMBR char(30),
          @ITEMDESC char(100),
          @VNDITNUM char(30),
          @VNDITDSC char(100),
          @Capital_Item tinyint,
          @LOCNCODE char(10),
          @LOCNCODEREG char(10),
          @POLNENUM int,
          @POSTATUS smallint,
          @POLNESTA smallint,
          @POTYPE smallint,
          @CURNCYID char(15),
          @PO_CURNCYID char(15),
          @CURRNIDX int,
          @PO_CURRNIDX int,
          @iStatus int,
          @iError int,
          @iCustomState int,
          @iCustomErrString varchar(255),
          @iCursorError int,
          @O_oErrorState int,
          @O_iUpdCrtItemErrState int,
          @O_iPOLineInsrErrState int,
          @O_iPOHdrErrState int,
          @O_iCommentMstErrState int,
          @O_iLineIvcInsrtErrState int,
          @UNITCOST numeric(19, 5),
          @ORUNTCST numeric(19, 5),
          @iGetNextNoteIdxErrState int,
          @sCompanyID smallint,
          @RCPTLNNM smallint,
          @SHIPMTHD char(15),
          @VADCDPAD char(15),
          @PURPVIDX int,
          @Revalue_Inventory int,
          @Tolerance_Percentage int,
          @POPALWOP_2 tinyint,
          @SERLTQTY numeric(19, 5),
          @VENDORID char(15),
          @STNDCOST numeric(19, 5),
          @UPPVIDX int,
          @RUPPVAMT numeric(19, 5),
          @OLDCUCST numeric(19, 5),
          @ACPURIDX int,
          @ISMCTRX tinyint,
          @FUNLCURR char(15),
          @DECPLCURItem smallint,
          @EDITDECPLCUR smallint,
          @FUNDECPLCUR smallint,
          @MCINSTALLED smallint,
          @LandedCostErrState int,
          @LandedCostErrString varchar(255),
          @projectexists smallint,
          @PASTAT smallint,
          @PAcloseProjcosts smallint,
          @costcatexists smallint,
          @PATU smallint,
          @PAinactive smallint,
          @CUSTNMBR char(15),
          @ENABLEMULTIBIN smallint,
          @ITEMTYPE tinyint,
          @count int,
          @PassedExtCost tinyint,
          @PAProjectType smallint,
          @PAAcctgMethod smallint,
          @PACOSTCATID char(15),
          @PATMProfitType int,
          @l_ProjNum char(15),
          @PAbllngtype int,
          @PAOverhead_Amount numeric(19, 5),
          @PAOverheaPercentage numeric(19, 5),
          @PABILRATE numeric(19, 5),
          @PAProfitType smallint,
          @PABaselinePTaxOptions smallint,
          @PAPurchase_Tax_Options smallint,
          @PAProfitAmount numeric(19, 5),
          @PAProfitPercent numeric(19, 5),
          @PA_Variance_QTY_Accrued_ numeric(19, 5),
          @PA_Variance_Accrued_Reve numeric(19, 5),
          @PO_UOMSCHDL char(11),
          @PO_UOFM char(9),
          @PO_ITMTSHID char(15),
          @PO_TRDISAMT numeric(19, 5),
          @PO_PALineItemSeq int,
          @PO_PAIV_Item_Checkbox smallint,
          @PO_PATU smallint,
          @PO_PAPurchase_Tax_Options smallint,
          @PO_PRICELVL char(15),
          @PO_PACHGORDNO char(17),
          @PACOSTCATNME char(31),
          @PA_Variance_Cost tinyint,
          @Status smallint,
          @PABilling_StatusN smallint,
          @PAMARKPERCENT numeric(19, 5),
          @PAviprofittypefrom smallint,
          @use_billing_rate smallint,
          @use_markup_percentage smallint,
          @PAINCPRCHTXPRJCST smallint,
          @PA_Base_Billing_Rate numeric(19, 5),
          @PABaseOvhdCost numeric(19, 5),
          @PAShipmentExtCost numeric(19, 5),
          @tax_amount numeric(19, 5),
          @PAOverhead_IDX int,
          @PATotalProfit numeric(19, 5),
          @PATOTALOVERH numeric(19, 5),
          @PAACREV numeric(19, 5),
          @PA00901_PATMProfitType int,
          @PA01001_PATMProfitType smallint,
          @PAsetupkey smallint,
          @PO_PACogs_Idx integer,
          @PO_PACGBWIPIDX integer,
          @PABase_Unit_Cost numeric(19, 5),
          @PABase_Qty numeric(19, 5),
          @PAPostedBillingsN numeric(19, 5),
          @PACogs_Idx integer,
          @PACGBWIPIDX integer,
          @PAUnbilled_AR_Idx integer,
          @PAUnbilled_Proj_Rev_Idx integer,
          @PAContra_Account_IDX integer,
          @PO_INVINDX integer,
          @PA_MC_Accrued_Revenue numeric(19, 5),
          @PROJECTLOADED integer,
          @PA_MC_Base_Billing_Rate numeric(19, 5),
          @PA_MC_Billing_Rate numeric(19, 5),
          @PA_MC_VarianceAccruedRev numeric(19, 5),
          @UNITPRCE numeric(19, 5),
          @PRICMTHD smallint,
          @LISTPRCE numeric(19, 5),
          @QTYBSUOM numeric(19, 5),
          @UOMSCHDL char(10),
          @UOFMBASE char(8),
          @CURRCOST numeric(19, 5),
          @ROUNDTO smallint,
          @ROUNDHOW smallint,
          @RNDGAMNT numeric(19, 5),
          @PADECPLQTY smallint,
          @NOTEINDX numeric(19, 5),
          @POLNEARY_3 numeric(19, 5),
          @DATE1 datetime,
          @TIME1 datetime,
          @TOOKACCTOFFPOORPASSED tinyint,
          @UserDate datetime,
          @PeriodID int,
          @Closed int,
          @Year int,
          @iCustomGLPeriodErr int,
          @iCustomGLPeriodState int,
          @O_glCalculateGLPeriodErrorState1 int,
          @O_glCalculateGLPeriodErrorState2 int,
          @PA_Create_Periodic_Budget_RecordsErrorState int,
          @PA_IVCreate_Periodic_Budget_RecordsErrorState int,
          @PABBeginDate_Period int,
          @PABEndDate_YEAR int,
          @PAFBeginDate_Period int,
          @PAFEndDate_YEAR int,
          @PABBeginDate_YEAR int,
          @PAFBeginDate_YEAR int,
          @NUMOFPER int,
          @l_fore_ctr int,
          @l_base_ctr int,
          @PABBeginDate datetime,
          @PABEndDate datetime,
          @PAFBeginDate datetime,
          @PAFEndDate datetime,
          @PAFEndDate_Period int,
          @PABEndDate_Period int,
          @i_startyearupdate int,
          @i_endyearupdate int,
          @i_baselinestartyearupdate int,
          @i_forecaststartyearupdate int,
          @ACTIVE tinyint,
          @NOACCTONITEM tinyint,
          @UseQtyOverageTolerance tinyint,
          @QtyOverTolerancePercent int,
          @CurrentQtyShipped numeric(19, 5),
          @QtyOnPO numeric(19, 5),
          @MaxToleranceAmount numeric(19, 5),
          @RemainingQtyOnPO numeric(19, 5),
          @SITEINACTIVE tinyint,
          @ITEMSITEINACTIVE tinyint
  SELECT
    @PACONTNUMBER = '',
    @LCCOST = 0,
    @Total_Landed_Cost_Amount = 0,
    @LCLINENUMBER = 0,
    @Landed_Cost_ID = '',
    @LOFSGMNT = 0,
    @LOFSGMNTALL = 0,
    @LOFSGMNTEND = 0,
    @ACCNT_STRING = '',
    @NEW_ACCNT_STRING = '',
    @MAXSEG = 0,
    @ACTINDX = 0,
    @ACSGFLOC = 0,
    @Location_Segment = '',
    @UNITCOST = 0,
    @ORUNTCST = 0,
    @ITMTRKOP = 1,
    @LOCNCODE = '',
    @DECPLCUR = 0,
    @DECPLQTY = 0,
    @iGetNextNoteIdxErrState = 0,
    @sCompanyID = '',
    @RcptLineNoteIDArray_1 = 0,
    @RcptLineNoteIDArray_2 = 0,
    @RcptLineNoteIDArray_3 = 0,
    @RcptLineNoteIDArray_4 = 0,
    @RcptLineNoteIDArray_5 = 0,
    @RcptLineNoteIDArray_6 = 0,
    @RcptLineNoteIDArray_7 = 0,
    @RcptLineNoteIDArray_8 = 0,
    @UMQTYINB = 0,
    @UOFM = '',
    @POLNENUM = 0,
    @POSTATUS = 0,
    @POLNESTA = 0,
    @POTYPE = 0,
    @DECPLQTY = 0,
    @VCTNMTHD = 0,
    @DECPLCUR = 0,
    @JOBNUMBR = '',
    @INVINDX = 0,
    @VNDITNUM = '',
    @VNDITDSC = '',
    @Capital_Item = 0,
    @O_oErrorState = 0,
    @PO_CURNCYID = '',
    @CURRNIDX = 0,
    @PO_CURRNIDX = 0,
    @O_iErrorState = 0,
    @iStatus = 0,
    @O_iUpdCrtItemErrState = 0,
    @O_iPOLineInsrErrState = 0,
    @O_iPOHdrErrState = 0,
    @O_iCommentMstErrState = 0,
    @O_iLineIvcInsrtErrState = 0,
    @iGetNextNoteIdxErrState = 0,
    @RCPTLNNM = 0,
    @SHIPMTHD = '',
    @VADCDPAD = '',
    @PURPVIDX = 0,
    @Revalue_Inventory = 0,
    @Tolerance_Percentage = 0,
    @POPALWOP_2 = 0,
    @SERLTQTY = 0,
    @VENDORID = '',
    @ITEMNMBR = '',
    @PRCHSUOM = '',
    @LOCNCODEREG = '',
    @ITEMDESC = '',
    @STNDCOST = 0,
    @UPPVIDX = 0,
    @RUPPVAMT = 0,
    @OLDCUCST = 0,
    @ACPURIDX = 0,
    @ISMCTRX = 0,
    @FUNLCURR = '',
    @DECPLCURItem = 0,
    @EDITDECPLCUR = 0,
    @FUNDECPLCUR = 0,
    @MCINSTALLED = 1,
    @LandedCostErrState = 0,
    @LandedCostErrString = '',
    @projectexists = 0,
    @PASTAT = 0,
    @PAcloseProjcosts = 0,
    @costcatexists = 0,
    @PATU = 0,
    @PAinactive = 0,
    @CUSTNMBR = '',
    @ENABLEMULTIBIN = 0,
    @ITEMTYPE = 0,
    @count = 0,
    @PassedExtCost = 0,
    @PAProjectType = 0,
    @PAAcctgMethod = 0,
    @PACOSTCATID = '',
    @PATMProfitType = 0,
    @l_ProjNum = '',
    @PAbllngtype = 0,
    @PAOverhead_Amount = 0,
    @PAOverheaPercentage = 0,
    @PABILRATE = 0,
    @PAProfitType = 0,
    @PABaselinePTaxOptions = 0,
    @PAPurchase_Tax_Options = 0,
    @PAProfitAmount = 0,
    @PAProfitPercent = 0,
    @PA_Variance_QTY_Accrued_ = 0,
    @PA_Variance_Accrued_Reve = 0,
    @PO_UOMSCHDL = '',
    @PO_UOFM = '',
    @PO_ITMTSHID = '',
    @PO_TRDISAMT = 0,
    @PO_PALineItemSeq = 0,
    @PO_PAIV_Item_Checkbox = 0,
    @PO_PATU = 4,
    @PO_PAPurchase_Tax_Options = 0,
    @PO_PRICELVL = '',
    @PO_PACHGORDNO = '',
    @PACOSTCATNME = '',
    @PA_Variance_Cost = 0,
    @Status = 0,
    @PABilling_StatusN = 0,
    @PAMARKPERCENT = 0,
    @PAviprofittypefrom = 0,
    @use_billing_rate = 0,
    @use_markup_percentage = 0,
    @PAINCPRCHTXPRJCST = 0,
    @PA_Base_Billing_Rate = 0,
    @PABaseOvhdCost = 0,
    @PAShipmentExtCost = 0,
    @tax_amount = 0,
    @PAOverhead_IDX = 0,
    @PATotalProfit = 0,
    @PATOTALOVERH = 0,
    @PAACREV = 0,
    @PA00901_PATMProfitType = 0,
    @PA01001_PATMProfitType = 0,
    @PAsetupkey = 0,
    @PO_PACogs_Idx = 0,
    @PO_PACGBWIPIDX = 0,
    @PABase_Unit_Cost = 0,
    @PABase_Qty = 0,
    @PAPostedBillingsN = 0,
    @PACogs_Idx = 0,
    @PACGBWIPIDX = 0,
    @PAUnbilled_AR_Idx = 0,
    @PAUnbilled_Proj_Rev_Idx = 0,
    @PAContra_Account_IDX = 0,
    @PO_INVINDX = 0,
    @PA_MC_Accrued_Revenue = 0,
    @PROJECTLOADED = 0,
    @PA_MC_Base_Billing_Rate = 0,
    @PA_MC_Billing_Rate = 0,
    @PA_MC_VarianceAccruedRev = 0,
    @UNITPRCE = 0,
    @PRICMTHD = 0,
    @LISTPRCE = 0,
    @QTYBSUOM = 1,
    @UOMSCHDL = '',
    @UOFMBASE = '',
    @CURRCOST = 0,
    @ROUNDTO = 0,
    @ROUNDHOW = 0,
    @RNDGAMNT = 0,
    @PADECPLQTY = 0,
    @NOTEINDX = 0,
    @POLNEARY_3 = 0,
    @DATE1 = '',
    @TIME1 = '',
    @TOOKACCTOFFPOORPASSED = 0,
    @UserDate = CAST(GETDATE() AS varchar(12)),
    @PeriodID = 0,
    @Closed = 0,
    @Year = 0,
    @iCustomGLPeriodErr = 0,
    @iCustomGLPeriodState = 0,
    @O_glCalculateGLPeriodErrorState1 = 0,
    @O_glCalculateGLPeriodErrorState2 = 0,
    @PA_Create_Periodic_Budget_RecordsErrorState = 0,
    @PA_IVCreate_Periodic_Budget_RecordsErrorState = 0,
    @PABBeginDate = '',
    @PABEndDate = '',
    @PAFBeginDate = '',
    @PAFEndDate = '',
    @PABBeginDate_Period = 0,
    @PABEndDate_YEAR = 0,
    @PAFBeginDate_Period = 0,
    @PAFEndDate_YEAR = 0,
    @PABBeginDate_YEAR = 0,
    @PAFBeginDate_YEAR = 0,
    @NUMOFPER = 0,
    @l_fore_ctr = 0,
    @l_base_ctr = 0,
    @PAFEndDate_Period = 0,
    @PABEndDate_Period = 0,
    @i_startyearupdate = 0,
    @i_endyearupdate = 0,
    @i_baselinestartyearupdate = 0,
    @i_forecaststartyearupdate = 0,
    @ACTIVE = 0,
    @NOACCTONITEM = 0,
    @UseQtyOverageTolerance = 0,
    @QtyOverTolerancePercent = 0,
    @CurrentQtyShipped = 0,
    @SITEINACTIVE = 0,
    @ITEMSITEINACTIVE = 0
  IF (@oErrString IS NULL)
  BEGIN
    SELECT
      @oErrString = ''
  END
  EXEC @iStatus = taPopRcptLineInsertPre @I_vPOPTYPE OUTPUT,
                                         @I_vPOPRCTNM OUTPUT,
                                         @I_vPONUMBER OUTPUT,
                                         @I_vITEMNMBR OUTPUT,
                                         @I_vITEMDESC OUTPUT,
                                         @I_vVENDORID OUTPUT,
                                         @I_vRCPTLNNM OUTPUT,
                                         @I_vVNDITNUM OUTPUT,
                                         @I_vVNDITDSC OUTPUT,
                                         @I_vACTLSHIP OUTPUT,
                                         @I_vINVINDX OUTPUT,
                                         @I_vInventoryAccount OUTPUT,
                                         @I_vUOFM OUTPUT,
                                         @I_vUNITCOST OUTPUT,
                                         @I_vEXTDCOST OUTPUT,
                                         @I_vNONINVEN OUTPUT,
                                         @I_vJOBNUMBR OUTPUT,
                                         @I_vBOLPRONUMBER OUTPUT,
                                         @I_vQTYSHPPD OUTPUT,
                                         @I_vQTYINVCD OUTPUT,
                                         @I_vAUTOCOST OUTPUT,
                                         @I_vPurchase_IV_Item_Taxable OUTPUT,
                                         @I_vPurchase_Item_Tax_Schedu OUTPUT,
                                         @I_vPurchase_Site_Tax_Schedu OUTPUT,
                                         @I_vTAXAMNT OUTPUT,
                                         @I_vLanded_Cost_Group_ID OUTPUT,
                                         @I_vLOCNCODE OUTPUT,
                                         @I_vPOLNENUM OUTPUT,
                                         @I_vreceiptdate OUTPUT,
                                         @I_vCURNCYID OUTPUT,
                                         @I_vProjNum OUTPUT,
                                         @I_vCostCatID OUTPUT,
                                         @I_vAutoAssignBin OUTPUT,
                                         @I_vCMMTTEXT OUTPUT,
                                         @I_vRequesterTrx OUTPUT,
                                         @I_vUSRDEFND1 OUTPUT,
                                         @I_vUSRDEFND2 OUTPUT,
                                         @I_vUSRDEFND3 OUTPUT,
                                         @I_vUSRDEFND4 OUTPUT,
                                         @I_vUSRDEFND5 OUTPUT,
                                         @O_iErrorState = @iCustomState OUTPUT,
                                         @oErrString = @iCustomErrString OUTPUT
  SELECT
    @iError = @@error
  IF ((@iStatus = 0)
    AND (@iError <> 0))
  BEGIN
    SELECT
      @iStatus = @iError
  END
  IF ((@iStatus <> 0)
    OR (@iCustomState <> 0))
  BEGIN
    SELECT
      @oErrString = RTRIM(@oErrString) + ' ' + LTRIM(RTRIM(@iCustomErrString))
    SELECT
      @O_iErrorState = 149
    EXEC @iStatus = taUpdateString @O_iErrorState,
                                   @oErrString,
                                   @oErrString OUTPUT,
                                   @O_oErrorState OUTPUT
    RETURN (@O_iErrorState)
  END
  IF (@I_vACTLSHIP IS NULL
    OR @I_vBOLPRONUMBER IS NULL
    OR @I_vINVINDX IS NULL
    OR @I_vInventoryAccount IS NULL
    OR @I_vITEMNMBR IS NULL
    OR @I_vJOBNUMBR IS NULL
    OR @I_vNONINVEN IS NULL
    OR @I_vPONUMBER IS NULL
    OR @I_vPOPRCTNM IS NULL
    OR @I_vRCPTLNNM IS NULL
    OR @I_vUOFM IS NULL
    OR @I_vVNDITDSC IS NULL
    OR @I_vVNDITNUM IS NULL
    OR @I_vPOPTYPE IS NULL
    OR @I_vCURNCYID IS NULL
    OR @I_vAutoAssignBin IS NULL)
  BEGIN
    SELECT
      @O_iErrorState = 2050
    EXEC @iStatus = taUpdateString @O_iErrorState,
                                   @oErrString,
                                   @oErrString OUTPUT,
                                   @O_oErrorState OUTPUT
    RETURN (@O_iErrorState)
  END
  IF (@I_vPOPRCTNM = ''
    OR (@I_vITEMNMBR = ''
    AND @I_vVNDITNUM = ''))
  BEGIN
    SELECT
      @O_iErrorState = 2051
    EXEC @iStatus = taUpdateString @O_iErrorState,
                                   @oErrString,
                                   @oErrString OUTPUT,
                                   @O_oErrorState OUTPUT
    RETURN (@O_iErrorState)
  END
  SELECT
    @I_vPONUMBER = UPPER(@I_vPONUMBER),
    @I_vITEMNMBR = UPPER(@I_vITEMNMBR),
    @I_vVENDORID = UPPER(@I_vVENDORID),
    @I_vPOPRCTNM = UPPER(@I_vPOPRCTNM),
    @I_vCURNCYID = UPPER(@I_vCURNCYID)
  SELECT
    @POPALWOP_2 = POPALWOP_2
  FROM POP40100(nolock)
  WHERE INDEX1 = 1
  IF (@I_vCostCatID IS NOT NULL)
  BEGIN
  BEGIN
    SELECT
      @PAINCPRCHTXPRJCST = PAINCPRCHTXPRJCST
    FROM PA41701(nolock)
    WHERE PAsetupkey = 1
    SELECT
      @PROJECTLOADED = 1
    SELECT
      @PAsetupkey = PAsetupkey,
      @PAviprofittypefrom = PAviprofittypefrom
    FROM PA42201(nolock)
  END
    IF (@PAsetupkey <> 1)
    BEGIN
      SELECT
        @O_iErrorState = 8181
      EXEC @iStatus = taUpdateString @O_iErrorState,
                                     @oErrString,
                                     @oErrString OUTPUT,
                                     @O_oErrorState OUTPUT
    END
  END
  IF EXISTS (SELECT
      1
    FROM POP10300(nolock)
    WHERE POPRCTNM = @I_vPOPRCTNM)
    OR EXISTS (SELECT
      1
    FROM POP30300(nolock)
    WHERE POPRCTNM = @I_vPOPRCTNM)
  BEGIN
    SELECT
      @O_iErrorState = 8053
    EXEC @iStatus = taUpdateString @O_iErrorState,
                                   @oErrString,
                                   @oErrString OUTPUT,
                                   @O_oErrorState OUTPUT
  END
  IF @I_vINVINDX <> 0
  BEGIN
    SELECT
      @TOOKACCTOFFPOORPASSED = 1
  END
  IF ((@I_vAUTOCOST = 1)
    AND ((@I_vUNITCOST > 0)
    OR (@I_vEXTDCOST > 0)))
  BEGIN
    SELECT
      @O_iErrorState = 594
    EXEC @iStatus = taUpdateString @O_iErrorState,
                                   @oErrString,
                                   @oErrString OUTPUT,
                                   @O_oErrorState OUTPUT
  END
  IF (@I_vPOPTYPE NOT IN (1, 3))
  BEGIN
    SELECT
      @O_iErrorState = 5455
    EXEC @iStatus = taUpdateString @O_iErrorState,
                                   @oErrString,
                                   @oErrString OUTPUT,
                                   @O_oErrorState OUTPUT
  END
  IF (@I_vPONUMBER <> '')
  BEGIN
    IF NOT EXISTS (SELECT
        1
      FROM POP10100(nolock)
      WHERE PONUMBER = @I_vPONUMBER)
    BEGIN
      SELECT
        @O_iErrorState = 2052
      EXEC @iStatus = taUpdateString @O_iErrorState,
                                     @oErrString,
                                     @oErrString OUTPUT,
                                     @O_oErrorState OUTPUT
    END
  END
  IF EXISTS (SELECT
      1
    FROM POP10170(nolock)
    WHERE PONUMBER = @I_vPONUMBER)
  BEGIN
    SELECT
      @O_iErrorState = 11971
    EXEC @iStatus = taUpdateString @O_iErrorState,
                                   @oErrString,
                                   @oErrString OUTPUT,
                                   @O_oErrorState OUTPUT
    RETURN (@O_iErrorState)
  END
  SELECT
    @POSTATUS = POSTATUS,
    @CUSTNMBR = CUSTNMBR
  FROM POP10100(nolock)
  WHERE PONUMBER = @I_vPONUMBER
  IF (@POSTATUS IN (4, 5, 6))
  BEGIN
    SELECT
      @O_iErrorState = 589
    EXEC @iStatus = taUpdateString @O_iErrorState,
                                   @oErrString,
                                   @oErrString OUTPUT,
                                   @O_oErrorState OUTPUT
  END
  IF (@I_vPOLNENUM < 0)
  BEGIN
    SELECT
      @O_iErrorState = 3810
    EXEC @iStatus = taUpdateString @O_iErrorState,
                                   @oErrString,
                                   @oErrString OUTPUT,
                                   @O_oErrorState OUTPUT
  END
  SELECT
    @FUNLCURR = FUNLCURR
  FROM MC40000(nolock)
  IF (@O_iErrorState <> 0)
  BEGIN
    RETURN (@O_iErrorState)
  END
  IF (@I_vNONINVEN = 0)
  BEGIN
    IF (@I_vVNDITNUM = '')
    BEGIN
      SELECT
        @I_vVNDITNUM = VNDITNUM
      FROM IV00103(nolock)
      WHERE ITEMNMBR = @I_vITEMNMBR
      AND VENDORID = @I_vVENDORID
    END
    IF (@I_vITEMNMBR = '')
    BEGIN
      SELECT
        @count = COUNT(ITEMNMBR)
      FROM IV00103(nolock)
      WHERE VNDITNUM = @I_vVNDITNUM
      AND VENDORID = @I_vVENDORID
      IF (@count > 1)
      BEGIN
        SELECT
          @O_iErrorState = 9008
        EXEC @iStatus = taUpdateString @O_iErrorState,
                                       @oErrString,
                                       @oErrString OUTPUT,
                                       @O_oErrorState OUTPUT
        RETURN (@O_iErrorState)
      END
      ELSE
      BEGIN
        SELECT
          @I_vITEMNMBR = ITEMNMBR
        FROM IV00103(nolock)
        WHERE VNDITNUM = @I_vVNDITNUM
        AND VENDORID = @I_vVENDORID
      END
    END
  END
  IF ((@I_vNONINVEN = 1)
    AND (@I_vVNDITNUM = ''))
  BEGIN
    SELECT
      @I_vVNDITNUM = @I_vITEMNMBR
  END
  IF (@I_vPONUMBER <> '')
  BEGIN
    IF (@I_vPOLNENUM <> 0)
    BEGIN
      IF NOT EXISTS (SELECT
          1
        FROM POP10110(nolock)
        WHERE PONUMBER = @I_vPONUMBER
        AND ORD = @I_vPOLNENUM)
      BEGIN
        SELECT
          @O_iErrorState = 3808
        EXEC @iStatus = taUpdateString @O_iErrorState,
                                       @oErrString,
                                       @oErrString OUTPUT,
                                       @O_oErrorState OUTPUT
      END
      SELECT
        @ITEMDESC = ITEMDESC,
        @VNDITDSC = VNDITDSC,
        @VNDITNUM = VNDITNUM,
        @UOFM = UOFM,
        @INVINDX = INVINDX,
        @JOBNUMBR = JOBNUMBR,
        @Capital_Item = Capital_Item,
        @DECPLCUR = DECPLCUR,
        @DECPLQTY = DECPLQTY,
        @ODECPLCU = ODECPLCU,
        @LOCNCODE = LOCNCODE,
        @UMQTYINB = UMQTYINB,
        @UNITCOST = UNITCOST,
        @ORUNTCST = ORUNTCST,
        @POLNESTA = POLNESTA,
        @POTYPE = POTYPE,
        @VENDORID = VENDORID,
        @ITEMNMBR = ITEMNMBR,
        @I_vLanded_Cost_Group_ID =
                                  CASE
                                    WHEN @I_vLanded_Cost_Group_ID IS NULL THEN Landed_Cost_Group_ID
                                    ELSE @I_vLanded_Cost_Group_ID
                                  END,
        @I_vProjNum =
                     CASE
                       WHEN @I_vProjNum IS NULL THEN ProjNum
                       ELSE @I_vProjNum
                     END,
        @I_vCostCatID =
                       CASE
                         WHEN @I_vCostCatID IS NULL THEN CostCatID
                         ELSE @I_vCostCatID
                       END,
        @POLNEARY_3 = POLNEARY_3
      FROM POP10110(nolock)
      WHERE PONUMBER = @I_vPONUMBER
      AND ORD = @I_vPOLNENUM
      IF @I_vCostCatID <> ''
      BEGIN
        SELECT
          @PROJECTLOADED = 1
      END
      IF (@I_vITEMNMBR <> @ITEMNMBR)
      BEGIN
        SELECT
          @O_iErrorState = 3809
        EXEC @iStatus = taUpdateString @O_iErrorState,
                                       @oErrString,
                                       @oErrString OUTPUT,
                                       @O_oErrorState OUTPUT
      END
      IF (@I_vUOFM <> '')
      BEGIN
        IF (@I_vUOFM <> @UOFM)
        BEGIN
          SELECT
            @O_iErrorState = 6663
          EXEC @iStatus = taUpdateString @O_iErrorState,
                                         @oErrString,
                                         @oErrString OUTPUT,
                                         @O_oErrorState OUTPUT
        END
      END
      IF (@I_vVENDORID <> @VENDORID)
      BEGIN
        SELECT
          @O_iErrorState = 6662
        EXEC @iStatus = taUpdateString @O_iErrorState,
                                       @oErrString,
                                       @oErrString OUTPUT,
                                       @O_oErrorState OUTPUT
      END
    END
    ELSE
    BEGIN
      IF (@I_vLOCNCODE <> '')
        AND (@POPALWOP_2 = 0)
      BEGIN
        SELECT
          @count = COUNT(ORD)
        FROM POP10110(nolock)
        WHERE PONUMBER = @I_vPONUMBER
        AND ITEMNMBR = @I_vITEMNMBR
        AND VNDITNUM = @I_vVNDITNUM
        AND VENDORID = @I_vVENDORID
        AND LOCNCODE = @I_vLOCNCODE
        SELECT
          @ITEMDESC = ITEMDESC,
          @VNDITDSC = VNDITDSC,
          @VNDITNUM = VNDITNUM,
          @UOFM = UOFM,
          @INVINDX = INVINDX,
          @JOBNUMBR = JOBNUMBR,
          @Capital_Item = Capital_Item,
          @DECPLCUR = DECPLCUR,
          @DECPLQTY = DECPLQTY,
          @ODECPLCU = ODECPLCU,
          @POLNENUM = ORD,
          @LOCNCODE = LOCNCODE,
          @UMQTYINB = UMQTYINB,
          @UNITCOST = UNITCOST,
          @ORUNTCST = ORUNTCST,
          @POLNESTA = POLNESTA,
          @POTYPE = POTYPE,
          @VENDORID = VENDORID,
          @I_vLanded_Cost_Group_ID =
                                    CASE
                                      WHEN @I_vLanded_Cost_Group_ID IS NULL THEN Landed_Cost_Group_ID
                                      ELSE @I_vLanded_Cost_Group_ID
                                    END,
          @I_vProjNum =
                       CASE
                         WHEN @I_vProjNum IS NULL THEN ProjNum
                         ELSE @I_vProjNum
                       END,
          @I_vCostCatID =
                         CASE
                           WHEN @I_vCostCatID IS NULL THEN CostCatID
                           ELSE @I_vCostCatID
                         END,
          @POLNEARY_3 = POLNEARY_3
        FROM POP10110(nolock)
        WHERE PONUMBER = @I_vPONUMBER
        AND ITEMNMBR = @I_vITEMNMBR
        AND VNDITNUM = @I_vVNDITNUM
        AND VENDORID = @I_vVENDORID
        AND LOCNCODE = @I_vLOCNCODE
        IF @I_vCostCatID <> ''
        BEGIN
          SELECT
            @PROJECTLOADED = 1
        END
      END
      ELSE
      BEGIN
        SELECT
          @count = COUNT(ORD)
        FROM POP10110(nolock)
        WHERE PONUMBER = @I_vPONUMBER
        AND ITEMNMBR = @I_vITEMNMBR
        AND VNDITNUM = @I_vVNDITNUM
        AND VENDORID = @I_vVENDORID
        SELECT
          @ITEMDESC = ITEMDESC,
          @VNDITDSC = VNDITDSC,
          @VNDITNUM = VNDITNUM,
          @UOFM = UOFM,
          @INVINDX = INVINDX,
          @JOBNUMBR = JOBNUMBR,
          @Capital_Item = Capital_Item,
          @DECPLCUR = DECPLCUR,
          @DECPLQTY = DECPLQTY,
          @ODECPLCU = ODECPLCU,
          @POLNENUM = ORD,
          @LOCNCODE = LOCNCODE,
          @UMQTYINB = UMQTYINB,
          @UNITCOST = UNITCOST,
          @ORUNTCST = ORUNTCST,
          @POLNESTA = POLNESTA,
          @POTYPE = POTYPE,
          @VENDORID = VENDORID,
          @I_vLanded_Cost_Group_ID =
                                    CASE
                                      WHEN @I_vLanded_Cost_Group_ID IS NULL THEN Landed_Cost_Group_ID
                                      ELSE @I_vLanded_Cost_Group_ID
                                    END,
          @I_vProjNum =
                       CASE
                         WHEN @I_vProjNum IS NULL THEN ProjNum
                         ELSE @I_vProjNum
                       END,
          @I_vCostCatID =
                         CASE
                           WHEN @I_vCostCatID IS NULL THEN CostCatID
                           ELSE @I_vCostCatID
                         END,
          @POLNEARY_3 = POLNEARY_3
        FROM POP10110(nolock)
        WHERE PONUMBER = @I_vPONUMBER
        AND ITEMNMBR = @I_vITEMNMBR
        AND VNDITNUM = @I_vVNDITNUM
        AND VENDORID = @I_vVENDORID
        IF @I_vCostCatID <> ''
        BEGIN
          SELECT
            @PROJECTLOADED = 1
        END
      END
      IF (@count = 1)
      BEGIN
        SELECT
          @I_vPOLNENUM = @POLNENUM
      END
      ELSE
      BEGIN
        IF (@count > 1)
        BEGIN
          SELECT
            @O_iErrorState = 9344
          EXEC @iStatus = taUpdateString @O_iErrorState,
                                         @oErrString,
                                         @oErrString OUTPUT,
                                         @O_oErrorState OUTPUT
        END
        IF (@count = 0)
        BEGIN
          SELECT
            @O_iErrorState = 9343
          EXEC @iStatus = taUpdateString @O_iErrorState,
                                         @oErrString,
                                         @oErrString OUTPUT,
                                         @O_oErrorState OUTPUT
        END
      END
      IF (@PROJECTLOADED = 1)
      BEGIN
        SELECT
          @INVINDX = PACGBWIPIDX
        FROM PA10601(nolock)
        WHERE PApurordnum = @I_vPONUMBER
        AND ORD = @I_vPOLNENUM
      END
    END
  END
  ELSE
  BEGIN
    SELECT
      @POTYPE =
               CASE
                 WHEN (@PROJECTLOADED = 1) AND
                   (@I_vPOPTYPE = 3) THEN 1
                 WHEN (@PROJECTLOADED = 1) AND
                   (@I_vPOPTYPE = 3) AND
                   (@I_vNONINVEN = 1) THEN 0
                 ELSE 0
               END,
      @I_vUOFM =
                CASE
                  WHEN (@I_vNONINVEN = 1) AND
                    (@I_vUOFM = '') THEN 'Each'
                  ELSE @I_vUOFM
                END,
      @UMQTYINB =
                 CASE
                   WHEN @I_vNONINVEN = 1 THEN 1
                   ELSE 0
                 END,
      @I_vLanded_Cost_Group_ID =
                                CASE
                                  WHEN @I_vLanded_Cost_Group_ID IS NULL THEN ''
                                  ELSE @I_vLanded_Cost_Group_ID
                                END,
      @I_vProjNum =
                   CASE
                     WHEN @I_vProjNum IS NULL THEN ''
                     ELSE @I_vProjNum
                   END,
      @I_vCostCatID =
                     CASE
                       WHEN @I_vCostCatID IS NULL THEN ''
                       ELSE @I_vCostCatID
                     END
  END
  IF (@POLNEARY_3 = 0)
  BEGIN
    SELECT
      @I_vCMMTTEXT = ''
  END
  IF (@I_vProjNum <> '')
    AND (@I_vCostCatID = '')
  BEGIN
    SELECT
      @O_iErrorState = 8173
    EXEC @iStatus = taUpdateString @O_iErrorState,
                                   @oErrString,
                                   @oErrString OUTPUT,
                                   @O_oErrorState OUTPUT
  END
  IF (@I_vProjNum = '')
    AND (@I_vCostCatID <> '')
  BEGIN
    SELECT
      @O_iErrorState = 8174
    EXEC @iStatus = taUpdateString @O_iErrorState,
                                   @oErrString,
                                   @oErrString OUTPUT,
                                   @O_oErrorState OUTPUT
  END
  SELECT
    @sCompanyID = CMPANYID
  FROM DYNAMICS..SY01500(nolock)
  WHERE INTERID = DB_NAME()
  IF (@I_vProjNum <> '')
    AND (@I_vCostCatID <> '')
  BEGIN
    EXEC @iStatus = DYNAMICS..tasmGetNextNoteIndex @I_sCompanyID = @sCompanyID,
                                                @I_iSQLSessionID = 0,
                                                @I_noteincrement = 1,
                                                @O_mNoteIndex = @NOTEINDX OUTPUT,
                                                @O_iErrorState = @iGetNextNoteIdxErrState OUTPUT
    SELECT
      @iError = @@error
    IF ((@iStatus <> 0)
      OR (@iGetNextNoteIdxErrState <> 0)
      OR (@iError <> 0))
    BEGIN
      SELECT
        @oErrString = RTRIM(@oErrString) + ' ' + @iGetNextNoteIdxErrState
      SELECT
        @O_iErrorState = 8239
      EXEC @iStatus = taUpdateString @O_iErrorState,
                                     @oErrString,
                                     @oErrString OUTPUT,
                                     @O_oErrorState OUTPUT
      RETURN (@O_iErrorState)
    END
  END
  IF (@I_vProjNum <> '')
    AND (@I_vProjNum <> '<NONE>')
  BEGIN
    IF (@I_vPOPTYPE IN (1, 3))
    BEGIN
      SELECT
        @projectexists = 1,
        @PASTAT = PASTAT,
        @PAcloseProjcosts = PAcloseProjcosts,
        @PACONTNUMBER = PACONTNUMBER,
        @PAProjectType = PAProjectType,
        @PAAcctgMethod = PAAcctgMethod,
        @CUSTNMBR =
                   CASE
                     WHEN @CUSTNMBR = '' AND
                       @I_vPONUMBER = '' THEN CUSTNMBR
                     ELSE ''
                   END
      FROM PA01201(nolock)
      WHERE PAPROJNUMBER = @I_vProjNum
    END
    IF (@projectexists <> 1)
    BEGIN
      SELECT
        @O_iErrorState = 8095
      EXEC @iStatus = taUpdateString @O_iErrorState,
                                     @oErrString,
                                     @oErrString OUTPUT,
                                     @O_oErrorState OUTPUT
    END
    IF (@PASTAT > 1
      AND @PASTAT < 5)
    BEGIN
      SELECT
        @O_iErrorState = 8096
      EXEC @iStatus = taUpdateString @O_iErrorState,
                                     @oErrString,
                                     @oErrString OUTPUT,
                                     @O_oErrorState OUTPUT
    END
    IF (@PAcloseProjcosts = 1)
    BEGIN
      SELECT
        @O_iErrorState = 8097
      EXEC @iStatus = taUpdateString @O_iErrorState,
                                     @oErrString,
                                     @oErrString OUTPUT,
                                     @O_oErrorState OUTPUT
    END
    SELECT
      @PASTAT = PASTAT,
      @PAcloseProjcosts = PAcloseProjcosts,
      @PACONTNUMBER = PACONTNUMBER
    FROM PA01101(nolock)
    WHERE PACONTNUMBER = @PACONTNUMBER
    IF (@PASTAT > 1
      AND @PASTAT < 5)
    BEGIN
      SELECT
        @O_iErrorState = 8098
      EXEC @iStatus = taUpdateString @O_iErrorState,
                                     @oErrString,
                                     @oErrString OUTPUT,
                                     @O_oErrorState OUTPUT
    END
    IF (@PAcloseProjcosts = 1)
    BEGIN
      SELECT
        @O_iErrorState = 8099
      EXEC @iStatus = taUpdateString @O_iErrorState,
                                     @oErrString,
                                     @oErrString OUTPUT,
                                     @O_oErrorState OUTPUT
    END
    IF EXISTS (SELECT
        1
      FROM PA00501(nolock)
      WHERE CUSTNMBR = @CUSTNMBR
      AND PAcloseProjcosts = 1)
    BEGIN
      SELECT
        @O_iErrorState = 8100
      EXEC @iStatus = taUpdateString @O_iErrorState,
                                     @oErrString,
                                     @oErrString OUTPUT,
                                     @O_oErrorState OUTPUT
    END
  END
  IF (@I_vCostCatID <> '')
    AND (@I_vCostCatID <> '<NONE>')
  BEGIN
    SELECT
      @costcatexists = 1,
      @PATU = PATU,
      @PAinactive = PAinactive,
      @PACOSTCATID = PACOSTCATID,
      @PATMProfitType = PATMProfitType,
      @PAPurchase_Tax_Options = PAPurchase_Tax_Options,
      @PACOSTCATNME = PACOSTCATNME
    FROM PA01001(nolock)
    WHERE PACOSTCATID = @I_vCostCatID
    IF (@costcatexists <> 1)
    BEGIN
      SELECT
        @O_iErrorState = 8101
      EXEC @iStatus = taUpdateString @O_iErrorState,
                                     @oErrString,
                                     @oErrString OUTPUT,
                                     @O_oErrorState OUTPUT
    END
    IF (@PATU <> 4)
    BEGIN
      SELECT
        @O_iErrorState = 8102
      EXEC @iStatus = taUpdateString @O_iErrorState,
                                     @oErrString,
                                     @oErrString OUTPUT,
                                     @O_oErrorState OUTPUT
    END
    IF (@PAinactive = 1)
    BEGIN
      SELECT
        @O_iErrorState = 8103
      EXEC @iStatus = taUpdateString @O_iErrorState,
                                     @oErrString,
                                     @oErrString OUTPUT,
                                     @O_oErrorState OUTPUT
    END
  END
  IF (@I_vProjNum <> '<NONE>')
    AND (@I_vProjNum <> '')
    AND (@I_vPONUMBER <> '')
  BEGIN
    IF (EXISTS (SELECT
        1
      FROM POP10110(nolock)
      WHERE PONUMBER = @I_vPONUMBER
      AND ORD = @I_vPOLNENUM
      AND ProjNum = '')
      )
    BEGIN
      SELECT
        @O_iErrorState = 8193
      EXEC @iStatus = taUpdateString @O_iErrorState,
                                     @oErrString,
                                     @oErrString OUTPUT,
                                     @O_oErrorState OUTPUT
    END
  END
  IF (@I_vProjNum <> '<NONE>')
    AND (@I_vProjNum <> '')
    AND (@I_vPONUMBER <> '')
  BEGIN
    IF (NOT EXISTS (SELECT
        1
      FROM POP10110(nolock)
      WHERE PONUMBER = @I_vPONUMBER
      AND ORD = @I_vPOLNENUM
      AND ProjNum = @I_vProjNum)
      )
    BEGIN
      SELECT
        @O_iErrorState = 8696
      EXEC @iStatus = taUpdateString @O_iErrorState,
                                     @oErrString,
                                     @oErrString OUTPUT,
                                     @O_oErrorState OUTPUT
    END
  END
  IF (@PROJECTLOADED = 1)
  BEGIN
    IF (@I_vProjNum <> '<NONE>')
      AND (@I_vProjNum <> '')
    BEGIN
      IF (@I_vCostCatID <> '<NONE>')
      BEGIN
        SELECT
          @l_ProjNum = ISNULL(PAPROJNUMBER, ''),
          @PASTAT = ISNULL(PASTAT, 0),
          @PAbllngtype = ISNULL(PAbllngtype, 0),
          @PAOverhead_Amount = ISNULL(PAForecastOvrhdAmtPerUnt, 0),
          @PAOverheaPercentage = ISNULL(PAForecastOvrhdPct, 0),
          @PABILRATE = ISNULL(PAForecastBaseProfitAmt, 0),
          @PAProfitType = ISNULL(PAProfitType, 0),
          @PABaselinePTaxOptions = ISNULL(PABaselinePTaxOptions, 0),
          @PO_CURNCYID = PAMCCURNCYID
        FROM PA01301(nolock)
        WHERE PAPROJNUMBER = @I_vProjNum
        AND PACOSTCATID = @I_vCostCatID
        AND PATU = @PATU
        SELECT
          @PO_CURRNIDX = CURRNIDX
        FROM DYNAMICS..MC40200(nolock)
        WHERE CURNCYID = @PO_CURNCYID
        IF (@l_ProjNum = '')
        BEGIN
          SELECT
            @O_iErrorState = 8177
          EXEC @iStatus = taUpdateString @O_iErrorState,
                                         @oErrString,
                                         @oErrString OUTPUT,
                                         @O_oErrorState OUTPUT
        END
        ELSE
        BEGIN
          IF (@PASTAT IN (2, 3, 4, 5))
          BEGIN
            SELECT
              @O_iErrorState = 8178
            EXEC @iStatus = taUpdateString @O_iErrorState,
                                           @oErrString,
                                           @oErrString OUTPUT,
                                           @O_oErrorState OUTPUT
          END
        END
      END
    END
  END
  SELECT
    @FUNDECPLCUR = DECPLCUR - 1
  FROM MC40000 a (NOLOCK),
       DYNAMICS..MC40200 b (NOLOCK)
  WHERE a.FUNCRIDX = b.CURRNIDX
  AND a.FUNLCURR = b.CURNCYID
  IF (@PROJECTLOADED = 1)
  BEGIN
    IF (@I_vPONUMBER <> '')
      AND (@I_vProjNum <> '')
      AND (@I_vCostCatID <> '')
      AND (@I_vVNDITNUM <> '')
    BEGIN
      IF (@I_vPOLNENUM <> '')
      BEGIN
        SELECT
          @PO_UOMSCHDL = UOMSCHDL,
          @PO_PATU = PATU,
          @PO_PAPurchase_Tax_Options = PAPurchase_Tax_Options,
          @PO_ITMTSHID = ITMTSHID,
          @PO_TRDISAMT = TRDISAMT,
          @PO_PAIV_Item_Checkbox = PAIV_Item_Checkbox,
          @PO_PALineItemSeq = PALineItemSeq,
          @PO_PRICELVL = PRICELVL,
          @PO_PACHGORDNO = PACHGORDNO
        FROM PA10601(nolock)
        WHERE PApurordnum = @I_vPONUMBER
        AND ORD = @I_vPOLNENUM
      END
      ELSE
      BEGIN
        SELECT
          @PO_UOMSCHDL = UOMSCHDL,
          @PO_PATU = PATU,
          @PO_PAPurchase_Tax_Options = PAPurchase_Tax_Options,
          @PO_ITMTSHID = ITMTSHID,
          @PO_TRDISAMT = TRDISAMT,
          @PO_PAIV_Item_Checkbox = PAIV_Item_Checkbox,
          @PO_PALineItemSeq = PALineItemSeq,
          @PO_PRICELVL = PRICELVL,
          @PO_PACHGORDNO = PACHGORDNO
        FROM PA10601(nolock)
        WHERE PApurordnum = @I_vPONUMBER
        AND PAPROJNUMBER = @I_vProjNum
        AND VNDITNUM = @I_vVNDITNUM
        AND PACOSTCATID = @I_vCostCatID
      END
      IF @PAPurchase_Tax_Options = 0
      BEGIN
        SELECT
          @PAPurchase_Tax_Options = @PO_PAPurchase_Tax_Options
      END
      SELECT TOP 1
        @PO_PRICELVL = PRICELVL
      FROM PA01303(nolock)
      WHERE PAPROJNUMBER = @I_vProjNum
      AND PACOSTCATID = @I_vCostCatID
      AND ITEMNMBR = @I_vITEMNMBR
    END
    ELSE
    BEGIN
      SELECT
        @PO_PAIV_Item_Checkbox = ISNULL(PAIV_Item_Checkbox, 0)
      FROM PA01301(nolock)
      WHERE PAPROJNUMBER = @I_vProjNum
      AND PACOSTCATID = @I_vCostCatID
      AND PATU = @PO_PATU
      SELECT TOP 1
        @PO_PRICELVL = ISNULL(PRICELVL, ''),
        @PO_PALineItemSeq = ISNULL(PALineItemSeq, 0)
      FROM PA01303(nolock)
      WHERE PAPROJNUMBER = @I_vProjNum
      AND PACOSTCATID = @I_vCostCatID
      AND ITEMNMBR = @I_vITEMNMBR
    END
    IF (@I_vreceiptdate = '')
    BEGIN
      SELECT
        @O_iErrorState = 21042
      EXEC @iStatus = taUpdateString @O_iErrorState,
                                     @oErrString,
                                     @oErrString OUTPUT,
                                     @O_oErrorState OUTPUT
    END
    EXEC @iStatus = glCalculateGLPeriod @I_sSeries = 7,
                                        @I_cOrigin = 'PA Receivings Trx Entry',
                                        @I_dDate = @I_vreceiptdate,
                                        @I_dUserDate = @UserDate,
                                        @O_sPeriodID = @PABBeginDate_Period OUTPUT,
                                        @O_tClosed = @Closed OUTPUT,
                                        @O_sYear = @PABBeginDate_YEAR OUTPUT,
                                        @O_iOUTErr = @iCustomGLPeriodErr OUTPUT,
                                        @O_iErrorState = @O_glCalculateGLPeriodErrorState1 OUTPUT
    SELECT
      @iError = @@error
    IF ((@iStatus <> 0)
      OR (@O_glCalculateGLPeriodErrorState1 <> 0)
      OR (@iError <> 0))
    BEGIN
      SELECT
        @oErrString = RTRIM(@oErrString) + ' ' + LTRIM(RTRIM(@O_glCalculateGLPeriodErrorState1))
      SELECT
        @O_iErrorState = 21043
      EXEC @iStatus = taUpdateString @O_iErrorState,
                                     @oErrString,
                                     @oErrString OUTPUT,
                                     @O_oErrorState OUTPUT
      RETURN (@O_iErrorState)
    END
    EXEC @iStatus = glCalculateGLPeriod @I_sSeries = 7,
                                        @I_cOrigin = 'PA Receivings Trx Entry',
                                        @I_dDate = @I_vreceiptdate,
                                        @I_dUserDate = @UserDate,
                                        @O_sPeriodID = @PABEndDate_Period OUTPUT,
                                        @O_tClosed = @Closed OUTPUT,
                                        @O_sYear = @PABEndDate_YEAR OUTPUT,
                                        @O_iOUTErr = @iCustomGLPeriodErr OUTPUT,
                                        @O_iErrorState = @O_glCalculateGLPeriodErrorState2 OUTPUT
    SELECT
      @iError = @@error
    IF ((@iStatus <> 0)
      OR (@O_glCalculateGLPeriodErrorState2 <> 0)
      OR (@iError <> 0))
    BEGIN
      SELECT
        @oErrString = RTRIM(@oErrString) + ' ' + LTRIM(RTRIM(@O_glCalculateGLPeriodErrorState2))
      SELECT
        @O_iErrorState = 20144
      EXEC @iStatus = taUpdateString @O_iErrorState,
                                     @oErrString,
                                     @oErrString OUTPUT,
                                     @O_oErrorState OUTPUT
      RETURN (@O_iErrorState)
    END
    SELECT
      @PAFBeginDate_Period = @PABBeginDate_Period,
      @PAFBeginDate_YEAR = @PABBeginDate_YEAR,
      @PAFEndDate_Period = @PABEndDate_Period,
      @PAFEndDate_YEAR = @PABEndDate_YEAR,
      @i_startyearupdate = @PABBeginDate_YEAR,
      @i_endyearupdate = @PABEndDate_YEAR,
      @i_baselinestartyearupdate = @PABEndDate_YEAR,
      @i_forecaststartyearupdate = @PABEndDate_YEAR
    SELECT
      @NUMOFPER = NUMOFPER
    FROM SY40101(nolock)
    WHERE YEAR1 = @PABEndDate_YEAR
    IF (@PABEndDate_YEAR - @PABBeginDate_YEAR) = 1
    BEGIN
      SET @l_base_ctr = (@NUMOFPER - @PABBeginDate_Period) + (@PABEndDate_Period) + 1
    END
    ELSE
    IF (@PABEndDate_YEAR = @PABBeginDate_YEAR)
    BEGIN
      SET @l_base_ctr = (@PABEndDate_Period - @PABBeginDate_Period) + 1
    END
    ELSE
    BEGIN
      SET @l_base_ctr = (((@PABEndDate_YEAR - @PABBeginDate_YEAR) * @NUMOFPER) + (@NUMOFPER - @PABBeginDate_Period) + (@NUMOFPER - @PABBeginDate_Period))
    END
    IF (@PABEndDate_YEAR <> @PABBeginDate_YEAR)
    BEGIN
      SET @l_fore_ctr = (@NUMOFPER - @PAFBeginDate_Period) + (@PAFEndDate_Period) + 1
    END
    ELSE
    IF (@PABEndDate_YEAR = @PABBeginDate_YEAR)
    BEGIN
      SET @l_fore_ctr = (@PAFEndDate_Period - @PAFBeginDate_Period) + 1
    END
    ELSE
    BEGIN
      SET @l_fore_ctr = (((@PAFEndDate_YEAR - @PAFBeginDate_YEAR - 1) * @NUMOFPER) + (@NUMOFPER - @PAFBeginDate_Period) + (@NUMOFPER - @PABBeginDate_Period))
    END
    IF (@costcatexists = 1
      AND @CUSTNMBR <> '')
    BEGIN
      IF (@PO_PAIV_Item_Checkbox = 1)
      BEGIN
        EXEC @iStatus = PA_IVCreate_Periodic_Budget_Records @I_projectnumber = @I_vProjNum,
                                                            @I_transactionusage = @PATU,
                                                            @I_costcategory = @I_vCostCatID,
                                                            @I_contractnumber = @PACONTNUMBER,
                                                            @I_customernumber = @CUSTNMBR,
                                                            @I_sourcefileID = 0,
                                                            @I_basestartdate = @PABBeginDate,
                                                            @I_baseenddate = @PABEndDate,
                                                            @I_forestartdate = @PAFBeginDate,
                                                            @I_foreenddate = @PAFEndDate,
                                                            @I_actualstartdate = @I_vreceiptdate,
                                                            @I_actualenddate = '',
                                                            @I_userdate = @UserDate,
                                                            @I_deciqty = @DECPLQTY,
                                                            @I_functionaldeciglobals = @FUNDECPLCUR,
                                                            @I_create = 0,
                                                            @I_change_baseline = 1,
                                                            @I_change_forecast = 1,
                                                            @i_firstdate = '',
                                                            @i_lastdate = '',
                                                            @i_year_start = 0,
                                                            @i_year_end = 0,
                                                            @i_startyearupdate = @i_startyearupdate,
                                                            @i_endyearupdate = @i_endyearupdate,
                                                            @i_baselinestartyearupdate = @i_baselinestartyearupdate,
                                                            @i_baseline_period = @PABEndDate_Period,
                                                            @i_forecaststartyearupdate = @i_forecaststartyearupdate,
                                                            @i_forecast_period = @PAFEndDate_Period,
                                                            @l_base_ctr = @l_base_ctr,
                                                            @l_fore_ctr = @l_base_ctr,
                                                            @I_err = @PA_IVCreate_Periodic_Budget_RecordsErrorState OUTPUT
        SELECT
          @iError = @@error
        IF ((@iStatus <> 0)
          OR (@PA_IVCreate_Periodic_Budget_RecordsErrorState <> 0)
          OR (@iError <> 0))
        BEGIN
          SELECT
            @oErrString = RTRIM(@oErrString) + ' ' + LTRIM(RTRIM(@PA_IVCreate_Periodic_Budget_RecordsErrorState))
          SELECT
            @O_iErrorState = 20145
          EXEC @iStatus = taUpdateString @O_iErrorState,
                                         @oErrString,
                                         @oErrString OUTPUT,
                                         @O_oErrorState OUTPUT
          RETURN (@O_iErrorState)
        END
      END
      ELSE
      BEGIN
        EXECUTE @iStatus = PA_Create_Periodic_Budget_Records @I_projectnumber = @I_vProjNum,
                                                             @I_transactionusage = @PATU,
                                                             @I_costcategory = @I_vCostCatID,
                                                             @I_contractnumber = @PACONTNUMBER,
                                                             @I_customernumber = @CUSTNMBR,
                                                             @I_sourcefileID = 0,
                                                             @I_basestartdate = @PABBeginDate,
                                                             @I_baseenddate = @PABEndDate,
                                                             @I_forestartdate = @PAFBeginDate,
                                                             @I_foreenddate = @PAFEndDate,
                                                             @I_actualstartdate = @I_vreceiptdate,
                                                             @I_actualenddate = '',
                                                             @I_userdate = @UserDate,
                                                             @I_deciqty = @DECPLQTY,
                                                             @I_functionaldeciglobals = @FUNDECPLCUR,
                                                             @I_create = 0,
                                                             @I_change_baseline = 1,
                                                             @I_change_forecast = 1,
                                                             @i_firstdate = '',
                                                             @i_lastdate = '',
                                                             @i_year_start = 0,
                                                             @i_year_end = 0,
                                                             @i_startyearupdate = @i_startyearupdate,
                                                             @i_endyearupdate = @i_endyearupdate,
                                                             @i_baselinestartyearupdate = @i_baselinestartyearupdate,
                                                             @i_baseline_period = @PABEndDate_Period,
                                                             @i_forecaststartyearupdate = @i_forecaststartyearupdate,
                                                             @i_forecast_period = @PAFEndDate_Period,
                                                             @l_base_ctr = @l_base_ctr,
                                                             @l_fore_ctr = @l_base_ctr,
                                                             @I_err = @PA_Create_Periodic_Budget_RecordsErrorState OUTPUT
        SELECT
          @iError = @@error
        IF ((@iStatus <> 0)
          OR (@PA_Create_Periodic_Budget_RecordsErrorState <> 0)
          OR (@iError <> 0))
        BEGIN
          SELECT
            @oErrString = RTRIM(@oErrString) + ' ' + LTRIM(RTRIM(@PA_Create_Periodic_Budget_RecordsErrorState))
          SELECT
            @O_iErrorState = 20146
          EXEC @iStatus = taUpdateString @O_iErrorState,
                                         @oErrString,
                                         @oErrString OUTPUT,
                                         @O_oErrorState OUTPUT
          RETURN (@O_iErrorState)
        END
      END
    END
  END
  SELECT TOP 1
    @LOCNCODEREG = LOCNCODE
  FROM IV40700(nolock)
  WHERE LOCNCODE <> ''
  IF ((@LOCNCODEREG = '')
    AND (@I_vLOCNCODE <> ''))
  BEGIN
    SELECT
      @O_iErrorState = 4605
    EXEC @iStatus = taUpdateString @O_iErrorState,
                                   @oErrString,
                                   @oErrString OUTPUT,
                                   @O_oErrorState OUTPUT
  END
  IF ((@LOCNCODEREG <> '')
    AND (@I_vLOCNCODE <> ''))
  BEGIN
    IF ((@I_vPONUMBER <> '')
      AND (NOT EXISTS (SELECT
        1
      FROM IV40700(nolock)
      WHERE LOCNCODE = @I_vLOCNCODE)
      ))
    BEGIN
      SELECT
        @O_iErrorState = 1277
      EXEC @iStatus = taUpdateString @O_iErrorState,
                                     @oErrString,
                                     @oErrString OUTPUT,
                                     @O_oErrorState OUTPUT
    END
    IF ((@I_vNONINVEN = 0)
      AND (NOT EXISTS (SELECT
        1
      FROM IV00102(nolock)
      WHERE ITEMNMBR = @I_vITEMNMBR
      AND LOCNCODE = @I_vLOCNCODE)
      ))
    BEGIN
      SELECT
        @O_iErrorState = 4604
      EXEC @iStatus = taUpdateString @O_iErrorState,
                                     @oErrString,
                                     @oErrString OUTPUT,
                                     @O_oErrorState OUTPUT
    END
  END
  IF (@I_vNONINVEN = 0)
  BEGIN
    IF (@I_vPONUMBER = '')
    BEGIN
      IF (NOT EXISTS (SELECT
          1
        FROM IV00103(nolock)
        WHERE ITEMNMBR = @I_vITEMNMBR
        AND VNDITNUM = @I_vVNDITNUM
        AND VENDORID = @I_vVENDORID)
        )
      BEGIN
        SELECT
          @O_iErrorState = 1779
        EXEC @iStatus = taUpdateString @O_iErrorState,
                                       @oErrString,
                                       @oErrString OUTPUT,
                                       @O_oErrorState OUTPUT
      END
    END
    ELSE
    BEGIN
      IF (@I_vPOLNENUM <> 0)
        AND (NOT EXISTS (SELECT
          1
        FROM POP10110(nolock)
        WHERE ITEMNMBR = @I_vITEMNMBR
        AND VNDITNUM = @I_vVNDITNUM
        AND VENDORID = @I_vVENDORID
        AND PONUMBER = @I_vPONUMBER
        AND ORD = @I_vPOLNENUM)
        )
      BEGIN
        SELECT
          @O_iErrorState = 7922
        EXEC @iStatus = taUpdateString @O_iErrorState,
                                       @oErrString,
                                       @oErrString OUTPUT,
                                       @O_oErrorState OUTPUT
      END
    END
  END
  IF (@I_vPONUMBER = '')
  BEGIN
    IF (@I_vNONINVEN = 0)
    BEGIN
      SELECT
        @DECPLCUR = ((DECPLCUR - 1) + 7),
        @ODECPLCU = (DECPLCUR - 1),
        @DECPLQTY = ((DECPLQTY - 1) + 1)
      FROM IV00101(nolock)
      WHERE ITEMNMBR = @I_vITEMNMBR
      IF (@I_vUOFM <> '')
      BEGIN
        IF NOT EXISTS (SELECT
            1
          FROM IV00106(nolock)
          WHERE ITEMNMBR = @I_vITEMNMBR
          AND UOFM = @I_vUOFM)
        BEGIN
          SELECT
            @O_iErrorState = 9373
          EXEC @iStatus = taUpdateString @O_iErrorState,
                                         @oErrString,
                                         @oErrString OUTPUT,
                                         @O_oErrorState OUTPUT
        END
        SELECT
          @UOFM = @I_vUOFM
      END
      ELSE
      BEGIN
        SELECT
          @PRCHSUOM = PRCHSUOM
        FROM IV00103(nolock)
        WHERE VENDORID = @I_vVENDORID
        AND ITEMNMBR = @I_vITEMNMBR
        AND VNDITNUM = @I_vVNDITNUM
        IF (@PRCHSUOM = '')
        BEGIN
          SELECT
            @PRCHSUOM = PRCHSUOM
          FROM IV00101(nolock)
          WHERE ITEMNMBR = @I_vITEMNMBR
        END
        IF (@PRCHSUOM = '')
        BEGIN
          SELECT
            @O_iErrorState = 9374
          EXEC @iStatus = taUpdateString @O_iErrorState,
                                         @oErrString,
                                         @oErrString OUTPUT,
                                         @O_oErrorState OUTPUT
        END
        SELECT
          @UOFM = @PRCHSUOM
      END
      SELECT
        @UMQTYINB = QTYBSUOM
      FROM IV00106(nolock)
      WHERE ITEMNMBR = @I_vITEMNMBR
      AND UOFM = @UOFM
      IF (@I_vVNDITDSC = '')
      BEGIN
        SELECT
          @I_vVNDITDSC = VNDITDSC
        FROM IV00103(nolock)
        WHERE VENDORID = @I_vVENDORID
        AND ITEMNMBR = @I_vITEMNMBR
      END
    END
    ELSE
    BEGIN
      SELECT
        @DECPLQTY = DECPLQTY,
        @DECPLCUR = ((DECPLCUR - 1) + 7),
        @ODECPLCU = (DECPLCUR - 1)
      FROM POP40100(nolock)
      WHERE INDEX1 = 1
      SELECT
        @UOFM = @I_vUOFM
    END
    IF (@I_vCURNCYID = '')
    BEGIN
      SELECT
        @I_vCURNCYID = ISNULL(CURNCYID, '')
      FROM PM00200(nolock)
      WHERE VENDORID = @I_vVENDORID
    END
    IF (@I_vCURNCYID <> '')
    BEGIN
      SELECT
        @CURRNIDX = ISNULL(CURRNIDX, 0)
      FROM DYNAMICS..MC40200(nolock)
      WHERE CURNCYID = @I_vCURNCYID
    END
    ELSE
    BEGIN
      SELECT
        @I_vCURNCYID = ISNULL(FUNLCURR, ''),
        @CURRNIDX = ISNULL(FUNCRIDX, 0)
      FROM MC40000(nolock)
    END
    IF ((@I_vLOCNCODE = '')
      AND (@LOCNCODEREG <> ''))
    BEGIN
      SELECT
        @O_iErrorState = 4601
      EXEC @iStatus = taUpdateString @O_iErrorState,
                                     @oErrString,
                                     @oErrString OUTPUT,
                                     @O_oErrorState OUTPUT
    END
    IF (@I_vAUTOCOST = 1)
    BEGIN
      SELECT
        @ORUNTCST = Last_Originating_Cost
      FROM IV00103(nolock)
      WHERE VENDORID = @I_vVENDORID
      AND ITEMNMBR = @I_vITEMNMBR
      AND Last_Currency_ID = @I_vCURNCYID
    END
  END
  IF (@POLNESTA IN (4, 5, 6))
  BEGIN
    SELECT
      @O_iErrorState = 591
    EXEC @iStatus = taUpdateString @O_iErrorState,
                                   @oErrString,
                                   @oErrString OUTPUT,
                                   @O_oErrorState OUTPUT
  END
  IF (@POTYPE = 2)
  BEGIN
    SELECT
      @O_iErrorState = 590
    EXEC @iStatus = taUpdateString @O_iErrorState,
                                   @oErrString,
                                   @oErrString OUTPUT,
                                   @O_oErrorState OUTPUT
  END
  IF ((@VENDORID <> @I_vVENDORID)
    AND (@I_vPONUMBER <> ''))
  BEGIN
    SELECT
      @O_iErrorState = 3799
    EXEC @iStatus = taUpdateString @O_iErrorState,
                                   @oErrString,
                                   @oErrString OUTPUT,
                                   @O_oErrorState OUTPUT
  END
  IF (@I_vJOBNUMBR = '')
  BEGIN
    SELECT
      @I_vJOBNUMBR = @JOBNUMBR
  END
  IF (@I_vUOFM = '')
  BEGIN
    SELECT
      @I_vUOFM = @UOFM
  END
  IF (@I_vPOLNENUM = 0)
  BEGIN
    SELECT
      @I_vPOLNENUM = @POLNENUM
  END
  IF (@I_vLOCNCODE = '')
  BEGIN
    SELECT
      @I_vLOCNCODE = @LOCNCODE
  END
  ELSE
  BEGIN
    IF ((@I_vLOCNCODE <> @LOCNCODE)
      AND (@I_vPONUMBER <> ''))
    BEGIN
      IF (@POPALWOP_2 = 0)
      BEGIN
        SELECT
          @O_iErrorState = 1276
        EXEC @iStatus = taUpdateString @O_iErrorState,
                                       @oErrString,
                                       @oErrString OUTPUT,
                                       @O_oErrorState OUTPUT
      END
    END
  END
  IF ((@I_vPONUMBER <> '')
    AND (@I_vUOFM <> ''))
  BEGIN
    IF (@I_vUOFM <> @UOFM)
    BEGIN
      SELECT
        @O_iErrorState = 4606
      EXEC @iStatus = taUpdateString @O_iErrorState,
                                     @oErrString,
                                     @oErrString OUTPUT,
                                     @O_oErrorState OUTPUT
    END
  END
  IF (((@VNDITNUM = '')
    OR (@VNDITNUM IS NULL))
    AND (@I_vPONUMBER <> ''))
  BEGIN
    SELECT
      @O_iErrorState = 2053
    EXEC @iStatus = taUpdateString @O_iErrorState,
                                   @oErrString,
                                   @oErrString OUTPUT,
                                   @O_oErrorState OUTPUT
  END
  IF ((@I_vNONINVEN = 0)
    AND (NOT EXISTS (SELECT
      1
    FROM IV00101(nolock)
    WHERE ITEMNMBR = @I_vITEMNMBR)
    ))
  BEGIN
    SELECT
      @O_iErrorState = 2054
    EXEC @iStatus = taUpdateString @O_iErrorState,
                                   @oErrString,
                                   @oErrString OUTPUT,
                                   @O_oErrorState OUTPUT
  END
  IF (@I_vPONUMBER <> '')
  BEGIN
    SELECT
      @CURNCYID = ISNULL(CURNCYID, 0)
    FROM POP10100(nolock)
    WHERE PONUMBER = @I_vPONUMBER
    SELECT
      @CURRNIDX = ISNULL(CURRNIDX, 0)
    FROM DYNAMICS..MC40200(nolock)
    WHERE CURNCYID = @CURNCYID
    IF ((@I_vCURNCYID <> '')
      AND (@I_vCURNCYID <> @CURNCYID))
    BEGIN
      SELECT
        @O_iErrorState = 7320
      EXEC @iStatus = taUpdateString @O_iErrorState,
                                     @oErrString,
                                     @oErrString OUTPUT,
                                     @O_oErrorState OUTPUT
    END
    IF (@I_vCURNCYID = '')
    BEGIN
      SELECT
        @I_vCURNCYID = @CURNCYID
    END
  END

  IF (EXISTS (SELECT
      1
    FROM WF100001
    WHERE FormID = 829)
    )
    AND (EXISTS (SELECT
      1
    FROM WF100002
    WHERE WF100002.ACTIVE = 1)
    )
  BEGIN
    IF (EXISTS (SELECT
        1
      FROM POP10100
      WHERE Workflow_Status <> 6
      AND PONUMBER = @I_vPONUMBER)
      )
    BEGIN
      SELECT
        @O_iErrorState = 12013
      EXEC @iStatus = taUpdateString @O_iErrorState,
                                     @oErrString,
                                     @oErrString OUTPUT,
                                     @O_oErrorState OUTPUT
    END
  END
  IF ((@I_vCURNCYID <> '')
    AND (@I_vCURNCYID <> @FUNLCURR))
  BEGIN
    SELECT
      @ISMCTRX = 1
  END
  IF @I_vNONINVEN = 0
  BEGIN
    SELECT
      @I_vITEMDESC =
                    CASE
                      WHEN @I_vITEMDESC IS NULL THEN ITEMDESC
                      ELSE @I_vITEMDESC
                    END,
      @ITMTRKOP = ITMTRKOP,
      @ITEMTYPE = ITEMTYPE,
      @VCTNMTHD = VCTNMTHD,
      @INVINDX = IVIVINDX,
      @STNDCOST = STNDCOST,
      @PURPVIDX = PURPVIDX,
      @UPPVIDX = UPPVIDX,
      @Revalue_Inventory = Revalue_Inventory,
      @Tolerance_Percentage = Tolerance_Percentage,
      @PO_UOMSCHDL = UOMSCHDL,
      @PRICMTHD = PRICMTHD,
      @UOMSCHDL = UOMSCHDL,
      @CURRCOST = CURRCOST
    FROM IV00101(nolock)
    WHERE ITEMNMBR = @I_vITEMNMBR
    IF (@VCTNMTHD NOT IN (4, 5))
    BEGIN
      SELECT
        @UPPVIDX = 0
    END
    ELSE
    BEGIN
      IF (@UPPVIDX = 0)
      BEGIN
        SELECT
          @UPPVIDX = ACTINDX
        FROM SY01100(nolock)
        WHERE SERIES = 5
        AND SEQNUMBR = 1250
      END
    END
    IF @I_vVNDITDSC = ''
    BEGIN
      SELECT
        @I_vVNDITDSC = @VNDITDSC
    END
    IF @I_vINVINDX = 0
    BEGIN
      IF @I_vINVINDX = 0
      BEGIN
        SELECT
          @I_vINVINDX = INVINDX
        FROM POP10110(nolock)
        WHERE PONUMBER = @I_vPONUMBER
        AND ORD = @I_vPOLNENUM
        IF @I_vINVINDX <> 0
        BEGIN
          SELECT
            @TOOKACCTOFFPOORPASSED = 1
        END
      END
      IF @I_vINVINDX = 0
      BEGIN
        SELECT
          @I_vINVINDX = @INVINDX
      END
      IF @I_vINVINDX = 0
      BEGIN
        SELECT
          @I_vINVINDX = ACTINDX
        FROM SY01100(nolock)
        WHERE SERIES = 5
        AND SEQNUMBR = 100
      END
    END
  END
  ELSE
  BEGIN
    IF @I_vITEMDESC IS NULL
    BEGIN
      SELECT
        @I_vITEMDESC = @ITEMDESC
    END
    IF @I_vITEMDESC = ''
    BEGIN
      SELECT
        @I_vITEMDESC = @I_vVNDITDSC
    END
    IF @I_vVNDITDSC = ''
    BEGIN
      SELECT
        @I_vVNDITDSC = @VNDITDSC
    END
    IF @I_vINVINDX = 0
    BEGIN
      SELECT
        @I_vINVINDX = @INVINDX
      IF (@I_vINVINDX = 0)
        AND (@I_vCostCatID = '')
      BEGIN
        SELECT
          @I_vINVINDX = PMPRCHIX
        FROM PM00200(nolock)
        WHERE VENDORID = @I_vVENDORID
      END
      ELSE
      BEGIN
        IF (@PROJECTLOADED = 1)
        BEGIN
          IF (@I_vPOLNENUM <> '')
          BEGIN
            SELECT
              @I_vINVINDX = INVINDX
            FROM POP10110(nolock)
            WHERE PONUMBER = @I_vPONUMBER
            AND ORD = @I_vPOLNENUM
          END
          ELSE
          BEGIN
            IF (@PAProjectType = 3)
            BEGIN
              SELECT
                @I_vINVINDX = PAACTINDX
              FROM PA43001(nolock)
              WHERE PAsfid = 26
              AND PArecordid = @I_vCostCatID
              AND PAaccttype = 30
            END
            ELSE
            BEGIN
              SELECT
                @I_vINVINDX = PAACTINDX
              FROM PA43001(nolock)
              WHERE PAsfid = 26
              AND PArecordid = @I_vCostCatID
              AND PAaccttype = 1
            END
          END
        END
      END
    END
  END
  IF (@ITEMTYPE = 3)
  BEGIN
    SELECT
      @O_iErrorState = 9337
    EXEC @iStatus = taUpdateString @O_iErrorState,
                                   @oErrString,
                                   @oErrString OUTPUT,
                                   @O_oErrorState OUTPUT
  END
  SELECT
    @SITEINACTIVE = INACTIVE
  FROM IV40700(nolock)
  WHERE LOCNCODE = @I_vLOCNCODE
  SELECT
    @ITEMSITEINACTIVE = INACTIVE
  FROM IV00102(nolock)
  WHERE (ITEMNMBR = @I_vITEMNMBR
  AND LOCNCODE = @I_vLOCNCODE)
  IF (@I_vLOCNCODE <> ''
    AND @SITEINACTIVE = 1)
  BEGIN
    SELECT
      @O_iErrorState = 11819
    EXEC @iStatus = taUpdateString @O_iErrorState,
                                   @oErrString,
                                   @oErrString OUTPUT,
                                   @O_oErrorState OUTPUT
  END
  IF (@I_vLOCNCODE <> ''
    AND @I_vITEMNMBR <> ''
    AND @ITEMSITEINACTIVE = 1)
  BEGIN
    SELECT
      @O_iErrorState = 11820
    EXEC @iStatus = taUpdateString @O_iErrorState,
                                   @oErrString,
                                   @oErrString OUTPUT,
                                   @O_oErrorState OUTPUT
  END
  IF (@I_vInventoryAccount <> '')
  BEGIN
    SELECT
      @I_vINVINDX = 0
    SELECT
      @I_vINVINDX = ACTINDX
    FROM GL00105(nolock)
    WHERE ACTNUMST = @I_vInventoryAccount
    IF (@I_vINVINDX = 0)
    BEGIN
      SELECT
        @O_iErrorState = 446
      EXEC @iStatus = taUpdateString @O_iErrorState,
                                     @oErrString,
                                     @oErrString OUTPUT,
                                     @O_oErrorState OUTPUT
    END
  END
  IF NOT EXISTS (SELECT
      1
    FROM GL00105(nolock)
    WHERE ACTINDX = @I_vINVINDX)
  BEGIN
    SELECT
      @O_iErrorState = 4612
    EXEC @iStatus = taUpdateString @O_iErrorState,
                                   @oErrString,
                                   @oErrString OUTPUT,
                                   @O_oErrorState OUTPUT
  END
  IF (@I_vUNITCOST < 0)
    OR (@I_vUNITCOST IS NULL
    AND @I_vAUTOCOST = 0)
  BEGIN
    SELECT
      @O_iErrorState = 8052
    EXEC @iStatus = taUpdateString @O_iErrorState,
                                   @oErrString,
                                   @oErrString OUTPUT,
                                   @O_oErrorState OUTPUT
  END
  IF (@ISMCTRX = 0)
  BEGIN
    IF (EXISTS (SELECT DISTINCT
        1
      FROM IV00105(nolock)
      WHERE ITEMNMBR = @I_vITEMNMBR)
      )
    BEGIN
      IF (NOT EXISTS (SELECT
          1
        FROM IV00105(nolock)
        WHERE ITEMNMBR = @I_vITEMNMBR
        AND CURNCYID <> '')
        )
      BEGIN
        SELECT
          @MCINSTALLED = 0
      END
    END
    ELSE
    BEGIN
      IF ((NOT EXISTS (SELECT TOP 1
          CURNCYID
        FROM IV00105(nolock)
        WHERE CURNCYID <> '')
        )
        AND (NOT EXISTS (SELECT TOP 1
          CURNCYID
        FROM CM00100(nolock)
        WHERE CURNCYID <> '')
        ))
      BEGIN
        SELECT
          @MCINSTALLED = 0
      END
    END
  END
  IF (@I_vNONINVEN = 0)
  BEGIN
    IF (@ISMCTRX = 1)
    BEGIN
      SELECT
        @DECPLCURItem = DECPLCUR - 1,
        @ODECPLCU = DECPLCUR - 1,
        @LISTPRCE = LISTPRCE
      FROM IV00105(nolock)
      WHERE ITEMNMBR = @I_vITEMNMBR
      AND CURNCYID = @I_vCURNCYID
    END
    ELSE
    BEGIN
      SELECT
        @DECPLCURItem = DECPLCUR - 1,
        @ODECPLCU = DECPLCUR - 1,
        @LISTPRCE = LISTPRCE
      FROM IV00105(nolock)
      WHERE ITEMNMBR = @I_vITEMNMBR
      AND (CURNCYID = ''
      OR CURNCYID = @I_vCURNCYID)
    END
  END
  ELSE
  BEGIN
    IF (@MCINSTALLED = 0)
    BEGIN
      SELECT
        @DECPLCURItem = DECPLCUR - 1
      FROM POP40100(nolock)
      WHERE INDEX1 = 1
    END
    ELSE
    BEGIN
      SELECT
        @DECPLCURItem = DECPLCUR - 1
      FROM POP40600(nolock)
      WHERE CURNCYID = @I_vCURNCYID
    END
  END
  IF (@ISMCTRX = 1)
  BEGIN
    SELECT
      @EDITDECPLCUR = DECPLCUR - 1
    FROM DYNAMICS..MC40200(nolock)
    WHERE CURNCYID = @I_vCURNCYID
  END
  ELSE
  BEGIN
    SELECT
      @EDITDECPLCUR = @FUNDECPLCUR
  END
  IF (@I_vPONUMBER = '')
  BEGIN
    SELECT
      @ORUNTCST = ROUND((@ORUNTCST * @UMQTYINB), @DECPLCURItem)
  END
  IF (@I_vUNITCOST IS NULL
    OR @I_vUNITCOST = 0)
    AND (@I_vAUTOCOST = 1)
  BEGIN
    SELECT
      @I_vUNITCOST = @ORUNTCST
  END
  IF (@I_vAUTOCOST = 0)
  BEGIN
    IF @I_vEXTDCOST IS NULL
    BEGIN
      SELECT
        @I_vEXTDCOST =
                      CASE
                        WHEN @I_vPOPTYPE = 1 THEN ROUND((@I_vUNITCOST) * @I_vQTYSHPPD, @EDITDECPLCUR)
                        ELSE ROUND((@I_vUNITCOST) * @I_vQTYINVCD, @EDITDECPLCUR)
                      END
    END
    ELSE
    BEGIN
      SELECT
        @PassedExtCost = 1
    END
    IF ((@I_vQTYSHPPD = 0)
      AND (@I_vPOPTYPE = 1))
    BEGIN
      IF (@I_vEXTDCOST > 0)
      BEGIN
        SELECT
          @O_iErrorState = 5446
        EXEC @iStatus = taUpdateString @O_iErrorState,
                                       @oErrString,
                                       @oErrString OUTPUT,
                                       @O_oErrorState OUTPUT
      END
    END
    IF ((@I_vQTYINVCD = 0)
      AND (@I_vPOPTYPE = 3))
    BEGIN
      IF (@I_vEXTDCOST > 0)
      BEGIN
        SELECT
          @O_iErrorState = 7993
        EXEC @iStatus = taUpdateString @O_iErrorState,
                                       @oErrString,
                                       @oErrString OUTPUT,
                                       @O_oErrorState OUTPUT
      END
    END
    IF ((@I_vPOPTYPE = 1)
      AND (@I_vQTYSHPPD <> 0))
    BEGIN
      IF ((ROUND(ABS((@I_vUNITCOST) - ROUND((@I_vEXTDCOST / @I_vQTYSHPPD), @DECPLCURItem)) * @I_vQTYSHPPD, @EDITDECPLCUR)) <> 0)
      BEGIN
        IF ((ROUND(ABS(ROUND((@I_vUNITCOST) * @I_vQTYSHPPD, @EDITDECPLCUR) - @I_vEXTDCOST) * @I_vQTYSHPPD, @EDITDECPLCUR)) <> 0)
        BEGIN
          SELECT
            @O_iErrorState = 2057
          EXEC @iStatus = taUpdateString @O_iErrorState,
                                         @oErrString,
                                         @oErrString OUTPUT,
                                         @O_oErrorState OUTPUT
        END
      END
    END
    IF ((@I_vPOPTYPE = 3)
      AND (@I_vQTYINVCD <> 0))
    BEGIN
      IF ((ROUND(ABS((@I_vUNITCOST) - ROUND((@I_vEXTDCOST / @I_vQTYINVCD), @DECPLCURItem)) * @I_vQTYINVCD, @EDITDECPLCUR)) <> 0)
      BEGIN
        IF ((ROUND(ABS(ROUND((@I_vUNITCOST) * @I_vQTYINVCD, @EDITDECPLCUR) - @I_vEXTDCOST) * @I_vQTYINVCD, @EDITDECPLCUR)) <> 0)
        BEGIN
          SELECT
            @O_iErrorState = 8051
          EXEC @iStatus = taUpdateString @O_iErrorState,
                                         @oErrString,
                                         @oErrString OUTPUT,
                                         @O_oErrorState OUTPUT
        END
      END
    END
  END
  IF (@I_vRCPTLNNM = 0)
  BEGIN
    SELECT
      @I_vRCPTLNNM = ISNULL(MAX(RCPTLNNM), 0) + 16384
    FROM POP10310(nolock)
    WHERE POPRCTNM = @I_vPOPRCTNM
  END
  IF EXISTS (SELECT
      1
    FROM POP10310(nolock)
    WHERE POPRCTNM = @I_vPOPRCTNM
    AND RCPTLNNM = @I_vRCPTLNNM)
  BEGIN
    SELECT
      @O_iErrorState = 2061
    EXEC @iStatus = taUpdateString @O_iErrorState,
                                   @oErrString,
                                   @oErrString OUTPUT,
                                   @O_oErrorState OUTPUT
  END
  IF ((@I_vQTYINVCD > @I_vQTYSHPPD)
    AND (@I_vPOPTYPE = 3))
  BEGIN
    SELECT
      @O_iErrorState = 2062
    EXEC @iStatus = taUpdateString @O_iErrorState,
                                   @oErrString,
                                   @oErrString OUTPUT,
                                   @O_oErrorState OUTPUT
  END
  IF ((@I_vQTYINVCD > 0)
    AND (@I_vPOPTYPE = 1))
  BEGIN
    SELECT
      @O_iErrorState = 734
    EXEC @iStatus = taUpdateString @O_iErrorState,
                                   @oErrString,
                                   @oErrString OUTPUT,
                                   @O_oErrorState OUTPUT
  END
  IF ((@I_vPurchase_IV_Item_Taxable < 1)
    OR (@I_vPurchase_IV_Item_Taxable > 3))
  BEGIN
    SELECT
      @O_iErrorState = 2045
    EXEC @iStatus = taUpdateString @O_iErrorState,
                                   @oErrString,
                                   @oErrString OUTPUT,
                                   @O_oErrorState OUTPUT
  END
  IF EXISTS (SELECT
      1
    FROM POP10360(nolock)
    WHERE POPRCTNM = @I_vPOPRCTNM
    AND RCPTLNNM = @I_vRCPTLNNM)
  BEGIN
    SELECT
      @I_vPurchase_IV_Item_Taxable = 1
  END
  IF (@I_vPurchase_IV_Item_Taxable <> 1)
  BEGIN
    SELECT
      @I_vPurchase_Item_Tax_Schedu = ''
  END
  IF (@I_vPurchase_Item_Tax_Schedu <> '')
  BEGIN
    IF (NOT EXISTS (SELECT
        1
      FROM TX00101(nolock)
      WHERE TAXSCHID = @I_vPurchase_Item_Tax_Schedu)
      )
    BEGIN
      SELECT
        @O_iErrorState = 833
      EXEC @iStatus = taUpdateString @O_iErrorState,
                                     @oErrString,
                                     @oErrString OUTPUT,
                                     @O_oErrorState OUTPUT
    END
  END
  IF (@I_vPurchase_Site_Tax_Schedu <> '')
  BEGIN
    IF (NOT EXISTS (SELECT
        1
      FROM TX00101(nolock)
      WHERE TAXSCHID = @I_vPurchase_Site_Tax_Schedu)
      )
    BEGIN
      SELECT
        @O_iErrorState = 834
      EXEC @iStatus = taUpdateString @O_iErrorState,
                                     @oErrString,
                                     @oErrString OUTPUT,
                                     @O_oErrorState OUTPUT
    END
  END
  IF (@I_vLanded_Cost_Group_ID <> '')
  BEGIN
    IF (NOT EXISTS (SELECT
        1
      FROM IV41101(nolock)
      WHERE Landed_Cost_Group_ID = @I_vLanded_Cost_Group_ID)
      )
    BEGIN
      SELECT
        @O_iErrorState = 837
      EXEC @iStatus = taUpdateString @O_iErrorState,
                                     @oErrString,
                                     @oErrString OUTPUT,
                                     @O_oErrorState OUTPUT
    END
  END
  IF (@I_vPONUMBER <> '')
  BEGIN
    SELECT
      @SHIPMTHD = ISNULL(SHIPMTHD, '')
    FROM POP10100(nolock)
    WHERE PONUMBER = @I_vPONUMBER
  END
  ELSE
  BEGIN
    SELECT
      @VADCDPAD = ISNULL(VADCDPAD, '')
    FROM PM00200(nolock)
    WHERE VENDORID = @I_vVENDORID
    SELECT
      @SHIPMTHD = ISNULL(SHIPMTHD, '')
    FROM PM00300(nolock)
    WHERE VENDORID = @I_vVENDORID
    AND ADRSCODE = @VADCDPAD
  END
  IF (@I_vNONINVEN = 1)
  BEGIN
    SELECT
      @PURPVIDX = ISNULL(PURPVIDX, 0)
    FROM PM00200(nolock)
    WHERE VENDORID = @I_vVENDORID
    IF (@PURPVIDX = 0)
    BEGIN
      SELECT
        @PURPVIDX = ACTINDX
      FROM SY01100(nolock)
      WHERE SERIES = 4
      AND SEQNUMBR = 1400
    END
  END
  ELSE
  BEGIN
    SELECT
      @PURPVIDX = ISNULL(PURPVIDX, 0)
    FROM IV00101(nolock)
    WHERE ITEMNMBR = @I_vITEMNMBR
    IF (@PURPVIDX = 0)
    BEGIN
      SELECT
        @PURPVIDX = ACTINDX,
        @NOACCTONITEM = 1
      FROM SY01100(nolock)
      WHERE SERIES = 5
      AND SEQNUMBR = 1200
    END
  END
  IF (@I_vNONINVEN = 1)
  BEGIN
    IF (EXISTS (SELECT
        ITEMNMBR
      FROM IV00101(nolock)
      WHERE ITEMNMBR = @I_vITEMNMBR)
      )
    BEGIN
      SELECT
        @O_iErrorState = 8162
      EXEC @iStatus = taUpdateString @O_iErrorState,
                                     @oErrString,
                                     @oErrString OUTPUT,
                                     @O_oErrorState OUTPUT
      RETURN (@O_iErrorState)
    END
  END
  IF (ROUND(@I_vUNITCOST, @DECPLCURItem) <> (@I_vUNITCOST))
  BEGIN
    SELECT
      @O_iErrorState = 7321
    EXEC @iStatus = taUpdateString @O_iErrorState,
                                   @oErrString,
                                   @oErrString OUTPUT,
                                   @O_oErrorState OUTPUT
  END
  IF (ROUND(@I_vEXTDCOST, @EDITDECPLCUR) <> (@I_vEXTDCOST))
  BEGIN
    SELECT
      @O_iErrorState = 7322
    EXEC @iStatus = taUpdateString @O_iErrorState,
                                   @oErrString,
                                   @oErrString OUTPUT,
                                   @O_oErrorState OUTPUT
  END
  IF (@POTYPE IN (1, 3))
  BEGIN
    IF (@I_vNONINVEN = 1)
    BEGIN
      SELECT
        @UseQtyOverageTolerance = UseQtyOverageTolerance,
        @QtyOverTolerancePercent = QtyOverTolerancePercent
      FROM POP40100(nolock)
      IF (@UseQtyOverageTolerance = 1
        AND @QtyOverTolerancePercent > 0)
      BEGIN
        SELECT
          @CurrentQtyShipped = ISNULL(SUM(QTYSHPPD), 0)
        FROM POP10500(nolock)
        WHERE PONUMBER = @I_vPONUMBER
        AND POLNENUM = @I_vPOLNENUM
        SELECT
          @QtyOnPO = QTYORDER - QTYCANCE
        FROM POP10110(nolock)
        WHERE PONUMBER = @I_vPONUMBER
        AND ORD = @I_vPOLNENUM
        SELECT
          @MaxToleranceAmount = CEILING((CAST(@QtyOverTolerancePercent AS decimal(8, 3)) / 100000 * @QtyOnPO) * POWER(10, @DECPLQTY - 1)) / POWER(10, @DECPLQTY - 1) + @QtyOnPO
        SELECT
          @RemainingQtyOnPO = @QtyOnPO - @CurrentQtyShipped
        IF (@I_vQTYSHPPD + (@QtyOnPO - @RemainingQtyOnPO) >= @MaxToleranceAmount)
        BEGIN
          SELECT
            @O_iErrorState = 11780
          EXEC @iStatus = taUpdateString @O_iErrorState,
                                         @oErrString,
                                         @oErrString OUTPUT,
                                         @O_oErrorState OUTPUT
        END
      END
    END
    ELSE
    IF (@I_vNONINVEN = 0)
    BEGIN
      SELECT
        @UseQtyOverageTolerance = UseQtyOverageTolerance,
        @QtyOverTolerancePercent = QtyOverTolerancePercent
      FROM IV00101(nolock)
      WHERE ITEMNMBR = @I_vITEMNMBR
      IF (@UseQtyOverageTolerance = 1
        AND @QtyOverTolerancePercent > 0)
      BEGIN
        SELECT
          @CurrentQtyShipped = ISNULL(SUM(QTYSHPPD), 0)
        FROM POP10500(nolock)
        WHERE PONUMBER = @I_vPONUMBER
        AND POLNENUM = @I_vPOLNENUM
        SELECT
          @QtyOnPO = QTYORDER - QTYCANCE
        FROM POP10110(nolock)
        WHERE PONUMBER = @I_vPONUMBER
        AND ORD = @I_vPOLNENUM
        SELECT
          @MaxToleranceAmount = CEILING((CAST(@QtyOverTolerancePercent AS decimal(8, 3)) / 100000 * @QtyOnPO) * POWER(10, @DECPLQTY - 1)) / POWER(10, @DECPLQTY - 1) + @QtyOnPO
        SELECT
          @RemainingQtyOnPO = @QtyOnPO - @CurrentQtyShipped
        IF (@I_vQTYSHPPD + (@QtyOnPO - @RemainingQtyOnPO) >= @MaxToleranceAmount)
        BEGIN
          SELECT
            @O_iErrorState = 11781
          EXEC @iStatus = taUpdateString @O_iErrorState,
                                         @oErrString,
                                         @oErrString OUTPUT,
                                         @O_oErrorState OUTPUT
        END
      END
    END
  END
  IF (@POTYPE = 1)
    AND (@PROJECTLOADED = 1)
    AND (@I_vNONINVEN = 0)
  BEGIN
    SELECT
      @PAbllngtype = 3,
      @PABilling_StatusN = 4
  END
  ELSE
  BEGIN
    IF (@POTYPE = 2)
    BEGIN
      IF (@I_vProjNum <> '<NONE>')
      BEGIN
        SELECT
          @PAbllngtype = PAbllngtype
        FROM PA01303(nolock)
        WHERE PAPROJNUMBER = @I_vProjNum
        AND PACOSTCATID = @I_vCostCatID
        AND ITEMNMBR = @I_vITEMNMBR
        AND PALineItemSeq = @PO_PALineItemSeq
        SELECT
          @PABilling_StatusN =
                              CASE
                                WHEN @PAbllngtype = 3 THEN 4
                                ELSE 1
                              END
        IF (@PAProjectType <> 1)
        BEGIN
          SELECT
            @PAbllngtype = 1,
            @PABilling_StatusN = 5
        END
      END
      ELSE
      BEGIN
        SELECT
          @PASTAT = 4,
          @PAbllngtype = 3,
          @PABILRATE = 0,
          @PAMARKPERCENT = 0,
          @PAACREV = 0
      END
    END
  END
  IF (@I_vPOPTYPE <> 1)
  BEGIN
    SELECT
      @PATOTALOVERH = ROUND((@PAOverhead_Amount + (@PABase_Unit_Cost * (@PAOverheaPercentage / 100))) * @PABase_Qty, 2)
  END
  IF (@POTYPE IN (0, 1))
    AND (@PROJECTLOADED = 1)
    AND (@I_vNONINVEN = 1)
  BEGIN
    IF (@PAProjectType <> 1)
    BEGIN
      SELECT
        @PAbllngtype = 1,
        @PABilling_StatusN = 5
    END
    ELSE
    BEGIN
      SELECT
        @PAbllngtype = 1,
        @PABilling_StatusN = 1
    END
  END
  IF (@I_vNONINVEN = 1)
    AND (@PROJECTLOADED = 1)
  BEGIN
    IF NOT EXISTS (SELECT
        1
      FROM PA10702(nolock)
      WHERE POPTYPE = @I_vPOPTYPE
      AND PAVIDN = @I_vPOPRCTNM
      AND RCPTLNNM = @I_vRCPTLNNM)
    BEGIN
      IF (@PAviprofittypefrom = 1)
        AND (@I_vProjNum <> '<NONE>')
      BEGIN
        IF @PAProjectType = 1
        BEGIN
          IF @PAProfitType = 1
          BEGIN
            SELECT
              @use_billing_rate = 1
            SELECT
              @PABILRATE = PAFProfitAmt,
              @PAbllngtype = PAbllngtype
            FROM PA01301(nolock)
            WHERE PAPROJNUMBER = @I_vProjNum
            AND PACOSTCATID = @I_vCostCatID
            AND PATU = @PATU
          END
          ELSE
          IF @PAProfitType = 2
          BEGIN
            SELECT
              @use_markup_percentage = 1
            SELECT
              @PAMARKPERCENT = PAFProfitPcnt,
              @PAbllngtype = PAbllngtype
            FROM PA01301(nolock)
            WHERE PAPROJNUMBER = @I_vProjNum
            AND PACOSTCATID = @I_vCostCatID
            AND PATU = @PATU
          END
          ELSE
          IF @PAProfitType = 8
          BEGIN
            SELECT
              @use_markup_percentage = 0
            SELECT
              @PAbllngtype = PAbllngtype
            FROM PA01301(nolock)
            WHERE PAPROJNUMBER = @I_vProjNum
            AND PACOSTCATID = @I_vCostCatID
            AND PATU = @PATU
          END
        END
      END
      ELSE
      IF (@PAviprofittypefrom = 2)
      BEGIN
        SELECT
          @PA00901_PATMProfitType = PATMProfitType,
          @PABILRATE = PATMProfitAmount,
          @PAMARKPERCENT = PATMProfitPercent
        FROM PA00901(nolock)
        WHERE VENDORID = @I_vVENDORID
        IF (@PA00901_PATMProfitType <> 0)
        BEGIN
          IF @PAProjectType = 1
          BEGIN
            IF (@PA00901_PATMProfitType = 1)
            BEGIN
              SELECT
                @use_billing_rate = 1,
                @PAMARKPERCENT = 0
              SELECT
                @PAbllngtype = PAbllngtype
              FROM PA01301(nolock)
              WHERE PAPROJNUMBER = @I_vProjNum
              AND PACOSTCATID = @I_vCostCatID
              AND PATU = @PATU
              IF (@PAProfitType = 8
                AND @PA00901_PATMProfitType = 1
                AND @PAAcctgMethod = 2)
              BEGIN
                SELECT
                  @PABILRATE = 0,
                  @PAProfitType = 1
              END
              ELSE
              BEGIN
                IF (@PAProfitType = 8
                  AND @PA00901_PATMProfitType = 1
                  AND @PAAcctgMethod = 1)
                BEGIN
                  SELECT
                    @PAProfitType = 1
                END
              END
            END
            ELSE
            IF (@PA00901_PATMProfitType = 2)
            BEGIN
              SELECT
                @use_markup_percentage = 1,
                @PABILRATE = 0
            END
            ELSE
            IF (@PA00901_PATMProfitType = 8)
            BEGIN
              SELECT
                @use_markup_percentage = 0
              SELECT
                @PAbllngtype = PAbllngtype
              FROM PA01301(nolock)
            END
          END
        END
      END
      ELSE
      BEGIN
        SELECT
          @PA01001_PATMProfitType = PATMProfitType,
          @PABILRATE = PATMProfitAmount,
          @PAMARKPERCENT = PATMProfitPercent
        FROM PA01001(nolock)
        WHERE PACOSTCATID = @I_vCostCatID
        IF @PAProjectType = 1
        BEGIN
          SELECT
            @PAbllngtype = PAbllngtype
          FROM PA01301(nolock)
          WHERE PAPROJNUMBER = @I_vProjNum
          AND PACOSTCATID = @I_vCostCatID
          AND PATU = @PATU
          IF @PA01001_PATMProfitType = 1
          BEGIN
            SELECT
              @use_billing_rate = 1,
              @PAMARKPERCENT = 0
          END
          ELSE
          IF @PA01001_PATMProfitType = 2
          BEGIN
            SELECT
              @use_markup_percentage = 1,
              @PABILRATE = 0
          END
          ELSE
          IF @PA01001_PATMProfitType = 8
          BEGIN
            SELECT
              @use_markup_percentage = 0
          END
        END
      END
    END
  END
  IF (@PAbllngtype = 1)
    AND (@PROJECTLOADED = 1)
  BEGIN
    IF (@use_billing_rate = 1)
      AND (@use_markup_percentage = 0)
    BEGIN
      IF (@I_vNONINVEN = 1)
        OR (@POTYPE = 2)
      BEGIN
        SELECT
          @PAMARKPERCENT = 0
        SELECT
          @PAACREV = (@PABILRATE * @I_vQTYSHPPD)
      END
    END
    ELSE
    IF (@use_billing_rate = 0)
      AND (@use_markup_percentage = 1)
    BEGIN
      IF (@I_vNONINVEN = 1)
        OR (@POTYPE = 2)
      BEGIN
        SELECT
          @PABILRATE = 0
        IF (@I_vEXTDCOST IS NOT NULL)
        BEGIN
          SELECT
            @PAACREV = ROUND(@I_vEXTDCOST * (1 + (@PAMARKPERCENT / 100)), @DECPLCUR)
        END
        ELSE
        BEGIN
          IF (@I_vPOPTYPE = 1)
          BEGIN
            SELECT
              @I_vEXTDCOST = ROUND(@ORUNTCST * @I_vQTYSHPPD, @EDITDECPLCUR)
            SELECT
              @PAACREV = ROUND(@I_vEXTDCOST * (1 + (@PAMARKPERCENT / 100)), @DECPLCUR)
          END
          ELSE
          BEGIN
            SELECT
              @I_vEXTDCOST = ROUND(@ORUNTCST * @I_vQTYINVCD, @EDITDECPLCUR)
            SELECT
              @PAACREV = ROUND(@I_vEXTDCOST * (1 + (@PAMARKPERCENT / 100)), @DECPLCUR)
          END
        END
      END
    END
  END
  ELSE
  BEGIN
    SELECT
      @PAACREV = 0
  END
  IF (@PAACREV <> 0)
    AND (@PROJECTLOADED = 1)
  BEGIN
    IF (@PAINCPRCHTXPRJCST = 1)
    BEGIN
      SELECT
        @tax_amount = @I_vTAXAMNT
    END
    SELECT
      @PATotalProfit = @PAPostedBillingsN - (@PAShipmentExtCost + @tax_amount)
  END
  IF (@PROJECTLOADED = 1)
  BEGIN
    SELECT
      @PA_Base_Billing_Rate =
                             CASE
                               WHEN @I_vUOFM <> '' AND
                                 (@PABILRATE = 0 OR
                                 @UMQTYINB = 0) THEN 0
                               WHEN @I_vUOFM <> '' AND
                                 @PABILRATE <> 0 AND
                                 @UMQTYINB <> 0 THEN (@PABILRATE / @UMQTYINB)
                               ELSE @PABILRATE
                             END,
      @PABaseOvhdCost =
                       CASE
                         WHEN @I_vUOFM <> '' AND
                           (@PAOverhead_Amount = 0 OR
                           @UMQTYINB = 0) THEN 0
                         WHEN @I_vUOFM <> '' AND
                           @PAOverhead_Amount <> 0 AND
                           @UMQTYINB <> 0 THEN (@PAOverhead_Amount / @UMQTYINB)
                         ELSE @PAOverhead_Amount
                       END,
      @PAShipmentExtCost =
                          CASE
                            WHEN (@I_vPOPTYPE = 1 OR
                              @I_vPOPTYPE = 3) THEN (@I_vQTYSHPPD * @I_vUNITCOST)
                            ELSE 0
                          END
  END
  IF (@I_vCostCatID <> '<NONE>')
    AND (@PROJECTLOADED = 1)
  BEGIN
    IF (@PAProjectType = 1)
      OR (@I_vProjNum = '<NONE>')
    BEGIN
      IF ((@I_vPOPTYPE = 1)
        AND (@PO_PAIV_Item_Checkbox = 0))
        OR ((@I_vPOPTYPE <> 1)
        AND (@PO_PAIV_Item_Checkbox = 0))
        OR ((@I_vPOPTYPE <> 1)
        AND (@PO_PAIV_Item_Checkbox = 1)
        AND (@POTYPE = 2))
      BEGIN
        IF (@PAAcctgMethod = 1)
        BEGIN
          EXEC @iStatus = taPAAuxAcctsGetIdx @I_vPAPROJNUMBER = @I_vProjNum,
                                             @I_vPACOSTCATID = @I_vCostCatID,
                                             @I_vEMPLOYID = @I_vVENDORID,
                                             @I_vPATU = 4,
                                             @I_vPAcosttrxid = 'VI',
                                             @I_vTYPE = 2,
                                             @I_vCUSTNMBR = @CUSTNMBR,
                                             @I_vPACONTNUMBER = @PACONTNUMBER,
                                             @O_iINDEX = @PO_PACogs_Idx OUTPUT,
                                             @O_iErrorState = @O_oErrorState OUTPUT
          SELECT
            @iError = @@error
          IF @iStatus = 0
            AND @iError <> 0
            SELECT
              @iStatus = @iError
          IF (@iStatus <> 0)
            OR (@O_oErrorState <> 0)
          BEGIN
            SELECT
              @O_iErrorState = 8184
            EXEC @iStatus = taUpdateString @O_iErrorState,
                                           @oErrString,
                                           @oErrString OUTPUT,
                                           @O_oErrorState OUTPUT
          END
        END
        ELSE
        IF (@PAAcctgMethod = 2)
        BEGIN
          EXEC @iStatus = taPAAuxAcctsGetIdx @I_vPAPROJNUMBER = @I_vProjNum,
                                             @I_vPACOSTCATID = @I_vCostCatID,
                                             @I_vEMPLOYID = @I_vVENDORID,
                                             @I_vPATU = 4,
                                             @I_vPAcosttrxid = 'VI',
                                             @I_vTYPE = 1,
                                             @I_vCUSTNMBR = @CUSTNMBR,
                                             @I_vPACONTNUMBER = @PACONTNUMBER,
                                             @O_iINDEX = @PACGBWIPIDX OUTPUT,
                                             @O_iErrorState = @O_oErrorState OUTPUT
          SELECT
            @iError = @@error
          IF @iStatus = 0
            AND @iError <> 0
            SELECT
              @iStatus = @iError
          IF (@iStatus <> 0)
            OR (@O_oErrorState <> 0)
          BEGIN
            SELECT
              @O_iErrorState = 8185
            EXEC @iStatus = taUpdateString @O_iErrorState,
                                           @oErrString,
                                           @oErrString OUTPUT,
                                           @O_oErrorState OUTPUT
          END
          IF (@PACGBWIPIDX = 0)
          BEGIN
            EXEC @iStatus = taPAAuxAcctsGetIdx @I_vPAPROJNUMBER = @I_vProjNum,
                                               @I_vPACOSTCATID = @I_vCostCatID,
                                               @I_vEMPLOYID = @I_vVENDORID,
                                               @I_vPATU = 4,
                                               @I_vPAcosttrxid = 'VI',
                                               @I_vTYPE = 2,
                                               @I_vCUSTNMBR = @CUSTNMBR,
                                               @I_vPACONTNUMBER = @PACONTNUMBER,
                                               @O_iINDEX = @PO_PACogs_Idx OUTPUT,
                                               @O_iErrorState = @O_oErrorState OUTPUT
            SELECT
              @iError = @@error
            IF @iStatus = 0
              AND @iError <> 0
              SELECT
                @iStatus = @iError
            IF (@iStatus <> 0)
              OR (@O_oErrorState <> 0)
            BEGIN
              SELECT
                @O_iErrorState = 8186
              EXEC @iStatus = taUpdateString @O_iErrorState,
                                             @oErrString,
                                             @oErrString OUTPUT,
                                             @O_oErrorState OUTPUT
            END
          END
        END
      END
      IF (@PO_PACogs_Idx = 0)
      BEGIN
        EXEC @iStatus = taPAAuxAcctsGetIdx @I_vPAPROJNUMBER = @I_vProjNum,
                                           @I_vPACOSTCATID = @I_vCostCatID,
                                           @I_vEMPLOYID = @I_vVENDORID,
                                           @I_vPATU = 4,
                                           @I_vPAcosttrxid = 'VI',
                                           @I_vTYPE = 2,
                                           @I_vCUSTNMBR = @CUSTNMBR,
                                           @I_vPACONTNUMBER = @PACONTNUMBER,
                                           @O_iINDEX = @PO_PACogs_Idx OUTPUT,
                                           @O_iErrorState = @O_oErrorState OUTPUT
        SELECT
          @iError = @@error
        IF @iStatus = 0
          AND @iError <> 0
          SELECT
            @iStatus = @iError
        IF (@iStatus <> 0)
          OR (@O_oErrorState <> 0)
        BEGIN
          SELECT
            @O_iErrorState = 8187
          EXEC @iStatus = taUpdateString @O_iErrorState,
                                         @oErrString,
                                         @oErrString OUTPUT,
                                         @O_oErrorState OUTPUT
        END
      END
      IF (@PAAcctgMethod <> 2)
      BEGIN
        EXEC @iStatus = taPAAuxAcctsGetIdx @I_vPAPROJNUMBER = @I_vProjNum,
                                           @I_vPACOSTCATID = @I_vCostCatID,
                                           @I_vEMPLOYID = @I_vVENDORID,
                                           @I_vPATU = 4,
                                           @I_vPAcosttrxid = 'VI',
                                           @I_vTYPE = 4,
                                           @I_vCUSTNMBR = @CUSTNMBR,
                                           @I_vPACONTNUMBER = @PACONTNUMBER,
                                           @O_iINDEX = @PAUnbilled_AR_Idx OUTPUT,
                                           @O_iErrorState = @O_oErrorState OUTPUT
        SELECT
          @iError = @@error
        IF @iStatus = 0
          AND @iError <> 0
          SELECT
            @iStatus = @iError
        IF (@iStatus <> 0)
          OR (@O_oErrorState <> 0)
        BEGIN
          SELECT
            @O_iErrorState = 8188
          EXEC @iStatus = taUpdateString @O_iErrorState,
                                         @oErrString,
                                         @oErrString OUTPUT,
                                         @O_oErrorState OUTPUT
        END
        IF (@PAUnbilled_AR_Idx <> 0)
        BEGIN
          EXEC @iStatus = taPAAuxAcctsGetIdx @I_vPAPROJNUMBER = @I_vProjNum,
                                             @I_vPACOSTCATID = @I_vCostCatID,
                                             @I_vEMPLOYID = @I_vVENDORID,
                                             @I_vPATU = 4,
                                             @I_vPAcosttrxid = 'VI',
                                             @I_vTYPE = 5,
                                             @I_vCUSTNMBR = @CUSTNMBR,
                                             @I_vPACONTNUMBER = @PACONTNUMBER,
                                             @O_iINDEX = @PAUnbilled_Proj_Rev_Idx OUTPUT,
                                             @O_iErrorState = @O_oErrorState OUTPUT
          SELECT
            @iError = @@error
          IF @iStatus = 0
            AND @iError <> 0
            SELECT
              @iStatus = @iError
          IF (@iStatus <> 0)
            OR (@O_oErrorState <> 0)
          BEGIN
            SELECT
              @O_iErrorState = 8189
            EXEC @iStatus = taUpdateString @O_iErrorState,
                                           @oErrString,
                                           @oErrString OUTPUT,
                                           @O_oErrorState OUTPUT
          END
          IF (@PAUnbilled_Proj_Rev_Idx = 0)
            SELECT
              @PAUnbilled_AR_Idx = 0
        END
      END
      IF (@PO_PAIV_Item_Checkbox <> 1)
      BEGIN
        IF (@I_vPOPTYPE <> 1)
        BEGIN
          EXEC @iStatus = taPAAuxAcctsGetIdx @I_vPAPROJNUMBER = @I_vProjNum,
                                             @I_vPACOSTCATID = @I_vCostCatID,
                                             @I_vEMPLOYID = @I_vVENDORID,
                                             @I_vPATU = 4,
                                             @I_vPAcosttrxid = 'VI',
                                             @I_vTYPE = 3,
                                             @I_vCUSTNMBR = @CUSTNMBR,
                                             @I_vPACONTNUMBER = @PACONTNUMBER,
                                             @O_iINDEX = @PAContra_Account_IDX OUTPUT,
                                             @O_iErrorState = @O_oErrorState OUTPUT
          SELECT
            @iError = @@error
          IF @iStatus = 0
            AND @iError <> 0
            SELECT
              @iStatus = @iError
          IF (@iStatus <> 0)
            OR (@O_oErrorState <> 0)
          BEGIN
            SELECT
              @O_iErrorState = 8190
            EXEC @iStatus = taUpdateString @O_iErrorState,
                                           @oErrString,
                                           @oErrString OUTPUT,
                                           @O_oErrorState OUTPUT
          END
        END
      END
    END
    ELSE
    BEGIN
      IF (@PACGBWIPIDX = 0)
      BEGIN
        EXEC @iStatus = taPAAuxAcctsGetIdx @I_vPAPROJNUMBER = @I_vProjNum,
                                           @I_vPACOSTCATID = @I_vCostCatID,
                                           @I_vEMPLOYID = @I_vVENDORID,
                                           @I_vPATU = 4,
                                           @I_vPAcosttrxid = 'VI',
                                           @I_vTYPE = 30,
                                           @I_vCUSTNMBR = @CUSTNMBR,
                                           @I_vPACONTNUMBER = @PACONTNUMBER,
                                           @O_iINDEX = @PACGBWIPIDX OUTPUT,
                                           @O_iErrorState = @O_oErrorState OUTPUT
        SELECT
          @iError = @@error
        IF @iStatus = 0
          AND @iError <> 0
          SELECT
            @iStatus = @iError
        IF ((@O_oErrorState = 2055)
          OR (@iStatus = 2055))
          AND (@PO_PAIV_Item_Checkbox = 1)
          SELECT
            @O_oErrorState = 0,
            @iStatus = 0
        IF (@iStatus <> 0)
          OR (@O_oErrorState <> 0)
        BEGIN
          SELECT
            @O_iErrorState = 8191
          EXEC @iStatus = taUpdateString @O_iErrorState,
                                         @oErrString,
                                         @oErrString OUTPUT,
                                         @O_oErrorState OUTPUT
        END
      END
      IF ((@I_vPOPTYPE <> 1)
        AND (@PO_PAIV_Item_Checkbox = 0))
      BEGIN
        IF (@PACGBWIPIDX <> 0)
        BEGIN
          IF (@PAContra_Account_IDX = 0)
          BEGIN
            EXEC @iStatus = taPAAuxAcctsGetIdx @I_vPAPROJNUMBER = @I_vProjNum,
                                               @I_vPACOSTCATID = @I_vCostCatID,
                                               @I_vEMPLOYID = @I_vVENDORID,
                                               @I_vPATU = 4,
                                               @I_vPAcosttrxid = 'VI',
                                               @I_vTYPE = 31,
                                               @I_vCUSTNMBR = @CUSTNMBR,
                                               @I_vPACONTNUMBER = @PACONTNUMBER,
                                               @O_iINDEX = @PAContra_Account_IDX OUTPUT,
                                               @O_iErrorState = @O_oErrorState OUTPUT
            SELECT
              @iError = @@error
            IF @iStatus = 0
              AND @iError <> 0
              SELECT
                @iStatus = @iError
            IF (@iStatus <> 0)
              OR (@O_oErrorState <> 0)
            BEGIN
              SELECT
                @O_iErrorState = 8192
              EXEC @iStatus = taUpdateString @O_iErrorState,
                                             @oErrString,
                                             @oErrString OUTPUT,
                                             @O_oErrorState OUTPUT
            END
          END
        END
      END
    END
  END
  IF (@PO_PACGBWIPIDX = 0)
    AND (@PROJECTLOADED = 1)
  BEGIN
    SELECT
      @PO_PACGBWIPIDX = @PACGBWIPIDX
  END
  IF (@I_vNONINVEN = 1)
    AND (@PROJECTLOADED = 1)
  BEGIN
    IF (@I_vProjNum <> '<NONE>')
      AND (@I_vCostCatID <> '<NONE>')
    BEGIN
      IF (@PAAcctgMethod = 1)
      BEGIN
        SELECT
          @PO_INVINDX = @PO_PACogs_Idx
      END
      ELSE
      BEGIN
        IF (@PACGBWIPIDX = 0)
        BEGIN
          SELECT
            @PO_INVINDX = @PACGBWIPIDX
        END
        ELSE
        BEGIN
          SELECT
            @PO_INVINDX = @PO_PACogs_Idx
        END
      END
    END
    ELSE
    BEGIN
      SELECT
        @PO_INVINDX = PMPRCHIX
      FROM PM00200(nolock)
      WHERE VENDORID = @I_vVENDORID
    END
  END
  IF (@PROJECTLOADED = 1)
    AND (@I_vCostCatID <> '')
    AND (@I_vNONINVEN = 1)
  BEGIN
    SELECT
      @PADECPLQTY = DECPLQTY
    FROM PA01001(nolock)
    WHERE PACOSTCATID = @I_vCostCatID
  END
  IF (@PROJECTLOADED = 1)
    AND (@I_vNONINVEN = 0)
  BEGIN
    IF ((@PAContra_Account_IDX = 0)
      AND (@I_vPOPTYPE = 3))
    BEGIN
      SELECT
        @PAContra_Account_IDX = PMAPINDX
      FROM PM00200(nolock)
      WHERE VENDORID = @I_vVENDORID
    END
    IF ((@PAContra_Account_IDX = 0)
      AND (@I_vPOPTYPE = 3))
    BEGIN
      SELECT
        @PAContra_Account_IDX = ACTINDX
      FROM SY01100(nolock)
      WHERE SERIES = 5
      AND SEQNUMBR = 200
    END
  END
  IF (@PROJECTLOADED = 1)
  BEGIN
    SELECT
      @UOFMBASE = BASEUOFM
    FROM IV40201(nolock)
    WHERE UOMSCHDL = @UOMSCHDL
    SELECT
      @QTYBSUOM = QTYBSUOM
    FROM IV40202(nolock)
    WHERE UOMSCHDL = @UOMSCHDL
    AND EQUIVUOM = @UOFMBASE
    AND UOFM = @I_vUOFM
    SELECT
      @PABILRATE =
                  CASE
                    WHEN @PRICMTHD = 1 THEN ISNULL(UOMPRICE, 0)
                    WHEN @PRICMTHD = 2 THEN ISNULL(ROUND((@LISTPRCE * @QTYBSUOM) * (UOMPRICE / 100), @DECPLCURItem), 0)
                    WHEN @PRICMTHD = 3 THEN ISNULL(ROUND((@CURRCOST * @QTYBSUOM) * (1 + (UOMPRICE / 100)), @DECPLCURItem), 0)
                    WHEN @PRICMTHD = 4 THEN ISNULL(ROUND((@STNDCOST * @QTYBSUOM) * (1 + (UOMPRICE / 100)), @DECPLCURItem), 0)
                    WHEN @PRICMTHD = 5 THEN ISNULL(ROUND((@CURRCOST * @QTYBSUOM) / (1 - (UOMPRICE / 100)), @DECPLCURItem), 0)
                    WHEN @PRICMTHD = 6 THEN ISNULL(ROUND((@STNDCOST * @QTYBSUOM) / (1 - (UOMPRICE / 100)), @DECPLCURItem), 0)
                    ELSE 0
                  END
    FROM IV00108(nolock)
    WHERE ITEMNMBR = @I_vITEMNMBR
    AND CURNCYID = @CURNCYID
    AND PRCLEVEL = @PO_PRICELVL
    AND UOFM = @I_vUOFM
    AND QTYBSUOM = @QTYBSUOM
    AND FROMQTY <= @I_vQTYSHPPD
    AND TOQTY >= @I_vQTYSHPPD
  END
  SELECT
    @RNDGAMNT = ISNULL(RNDGAMNT, 0),
    @ROUNDHOW = ISNULL(ROUNDHOW, 0),
    @ROUNDTO = ISNULL(ROUNDTO, 0)
  FROM IV00107(nolock)
  WHERE ITEMNMBR = @I_vITEMNMBR
  AND CURNCYID = @CURNCYID
  AND PRCLEVEL = @PO_PRICELVL
  AND UOFM = @I_vUOFM
  AND QTYBSUOM = @QTYBSUOM
  IF (@ROUNDTO > 1
    AND @ROUNDTO < 5)
  BEGIN
    EXEC @iStatus = taPricingGetRoundPrice @ROUNDTO,
                                           @ROUNDHOW,
                                           @RNDGAMNT,
                                           @PABILRATE OUTPUT
    SELECT
      @iError = @@error
    IF (@iError <> 0)
      OR (@iStatus <> 0)
    BEGIN
      IF (@iStatus <> 0)
      BEGIN
        SELECT
          @oErrString = RTRIM(@oErrString) + ' ' + @iStatus
      END
      SELECT
        @O_iErrorState = 8182
      EXEC @iStatus = taUpdateString @O_iErrorState,
                                     @oErrString,
                                     @oErrString OUTPUT,
                                     @O_oErrorState OUTPUT
    END
  END
  IF (@I_vRequesterTrx = 0)
  BEGIN
    EXEC @iStatus = eConnectOutVerify @I_vDOCTYPE = 'PO_Receiving_Transaction',
                                      @I_vINDEX1 = @I_vPOPRCTNM,
                                      @I_vINDEX2 = '',
                                      @I_vINDEX3 = '',
                                      @I_vINDEX4 = '',
                                      @I_vINDEX5 = '',
                                      @I_vINDEX6 = '',
                                      @I_vINDEX7 = '',
                                      @I_vINDEX8 = '',
                                      @I_vINDEX9 = '',
                                      @I_vINDEX10 = '',
                                      @I_vINDEX11 = '',
                                      @I_vINDEX12 = '',
                                      @I_vINDEX13 = '',
                                      @I_vINDEX14 = '',
                                      @I_vINDEX15 = '',
                                      @I_vDelete = 0,
                                      @O_iErrorState = @iCustomState OUTPUT
    SELECT
      @iError = @@error
    IF ((@iStatus = 0)
      AND (@iError <> 0))
    BEGIN
      SELECT
        @iStatus = @iError
    END
    IF ((@iStatus <> 0)
      OR (@iCustomState <> 0))
    BEGIN
      SELECT
        @O_iErrorState = 1289
      EXEC @iStatus = taUpdateString @O_iErrorState,
                                     @oErrString,
                                     @oErrString OUTPUT,
                                     @O_oErrorState OUTPUT
    END
  END
  SELECT
    @MAXSEG = MAX(SGMTNUMB)
  FROM SY00300(nolock)
  SELECT
    @Location_Segment = ISNULL(Location_Segment, '')
  FROM IV40700(nolock)
  WHERE LOCNCODE = @I_vLOCNCODE
  SELECT
    @ACSGFLOC = ACSGFLOC,
    @ENABLEMULTIBIN = ENABLEMULTIBIN
  FROM IV40100(nolock)
  WHERE SETUPKEY = 1
  IF ((@I_vINVINDX <> 0)
    AND (@Location_Segment <> '')
    AND (@ACSGFLOC <> 0)
    AND (@TOOKACCTOFFPOORPASSED = 0))
  BEGIN
    SELECT
      @ACCNT_STRING = ''
    SELECT
      @ACCNT_STRING = ACTNUMST
    FROM GL00105(nolock)
    WHERE ACTINDX = @I_vINVINDX
    IF (@ACSGFLOC < @MAXSEG)
    BEGIN
      SELECT
        @LOFSGMNT = SUM(LOFSGMNT) + (@MAXSEG - @ACSGFLOC)
      FROM SY00300(nolock)
      WHERE SGMTNUMB >= @ACSGFLOC
      SELECT
        @LOFSGMNTEND = SUM(LOFSGMNT) + (@MAXSEG - @ACSGFLOC)
      FROM SY00300(nolock)
      WHERE SGMTNUMB > @ACSGFLOC
    END
    ELSE
    BEGIN
      SELECT
        @LOFSGMNT = SUM(LOFSGMNT)
      FROM SY00300(nolock)
      WHERE SGMTNUMB = @ACSGFLOC
    END
    SELECT
      @LOFSGMNTALL = SUM(LOFSGMNT) + (@MAXSEG - 1)
    FROM SY00300(nolock)
    IF (@ACSGFLOC = @MAXSEG)
    BEGIN
      SELECT
        @NEW_ACCNT_STRING = SUBSTRING(@ACCNT_STRING, 1, @LOFSGMNTALL - @LOFSGMNT) + RTRIM(LTRIM(@Location_Segment))
    END
    ELSE
    BEGIN
      SELECT
        @NEW_ACCNT_STRING = SUBSTRING(@ACCNT_STRING, 1, @LOFSGMNTALL - @LOFSGMNT) + RTRIM(LTRIM(@Location_Segment)) + SUBSTRING(@ACCNT_STRING, @LOFSGMNTALL - @LOFSGMNTEND + 1, @LOFSGMNTALL)
    END
    SELECT
      @ACTINDX = ISNULL(ACTINDX, 0)
    FROM GL00105(nolock)
    WHERE ACTNUMST = @NEW_ACCNT_STRING
    IF (@ACTINDX <> 0)
    BEGIN
      SELECT
        @ACTIVE = ACTIVE
      FROM GL00100(nolock)
      WHERE ACTINDX = @ACTINDX
    END
    IF (@ACTINDX <> 0)
      AND (@ACTIVE = 1)
    BEGIN
      SELECT
        @I_vINVINDX = @ACTINDX
    END
  END
  IF ((@PURPVIDX <> 0)
    AND (@Location_Segment <> '')
    AND (@ACSGFLOC <> 0)
    AND (@TOOKACCTOFFPOORPASSED = 1)
    AND (@NOACCTONITEM = 0))
  BEGIN
    SELECT
      @ACCNT_STRING = ''
    SELECT
      @ACTINDX = 0
    SELECT
      @ACCNT_STRING = ACTNUMST
    FROM GL00105(nolock)
    WHERE ACTINDX = @PURPVIDX
    IF (@ACSGFLOC < @MAXSEG)
    BEGIN
      SELECT
        @LOFSGMNT = SUM(LOFSGMNT) + (@MAXSEG - @ACSGFLOC)
      FROM SY00300(nolock)
      WHERE SGMTNUMB >= @ACSGFLOC
      SELECT
        @LOFSGMNTEND = SUM(LOFSGMNT) + (@MAXSEG - @ACSGFLOC)
      FROM SY00300(nolock)
      WHERE SGMTNUMB > @ACSGFLOC
    END
    ELSE
    BEGIN
      SELECT
        @LOFSGMNT = SUM(LOFSGMNT)
      FROM SY00300(nolock)
      WHERE SGMTNUMB = @ACSGFLOC
    END
    SELECT
      @LOFSGMNTALL = SUM(LOFSGMNT) + (@MAXSEG - 1)
    FROM SY00300(nolock)
    IF (@ACSGFLOC = @MAXSEG)
    BEGIN
      SELECT
        @NEW_ACCNT_STRING = SUBSTRING(@ACCNT_STRING, 1, @LOFSGMNTALL - @LOFSGMNT) + RTRIM(LTRIM(@Location_Segment))
    END
    ELSE
    BEGIN
      SELECT
        @NEW_ACCNT_STRING = SUBSTRING(@ACCNT_STRING, 1, @LOFSGMNTALL - @LOFSGMNT) + RTRIM(LTRIM(@Location_Segment)) + SUBSTRING(@ACCNT_STRING, @LOFSGMNTALL - @LOFSGMNTEND + 1, @LOFSGMNTALL)
    END
    SELECT
      @ACTINDX = ISNULL(ACTINDX, 0)
    FROM GL00105(nolock)
    WHERE ACTNUMST = @NEW_ACCNT_STRING
    IF (@ACTINDX <> 0)
    BEGIN
      SELECT
        @PURPVIDX = @ACTINDX
    END
  END
  IF ((@UPPVIDX <> 0)
    AND (@Location_Segment <> '')
    AND (@ACSGFLOC <> 0)
    AND (@TOOKACCTOFFPOORPASSED = 1)
    AND (@NOACCTONITEM = 0))
  BEGIN
    SELECT
      @ACCNT_STRING = ''
    SELECT
      @ACTINDX = 0
    SELECT
      @ACCNT_STRING = ACTNUMST
    FROM GL00105(nolock)
    WHERE ACTINDX = @UPPVIDX
    IF (@ACSGFLOC < @MAXSEG)
    BEGIN
      SELECT
        @LOFSGMNT = SUM(LOFSGMNT) + (@MAXSEG - @ACSGFLOC)
      FROM SY00300(nolock)
      WHERE SGMTNUMB >= @ACSGFLOC
      SELECT
        @LOFSGMNTEND = SUM(LOFSGMNT) + (@MAXSEG - @ACSGFLOC)
      FROM SY00300(nolock)
      WHERE SGMTNUMB > @ACSGFLOC
    END
    ELSE
    BEGIN
      SELECT
        @LOFSGMNT = SUM(LOFSGMNT)
      FROM SY00300(nolock)
      WHERE SGMTNUMB = @ACSGFLOC
    END
    SELECT
      @LOFSGMNTALL = SUM(LOFSGMNT) + (@MAXSEG - 1)
    FROM SY00300(nolock)
    IF (@ACSGFLOC = @MAXSEG)
    BEGIN
      SELECT
        @NEW_ACCNT_STRING = SUBSTRING(@ACCNT_STRING, 1, @LOFSGMNTALL - @LOFSGMNT) + RTRIM(LTRIM(@Location_Segment))
    END
    ELSE
    BEGIN
      SELECT
        @NEW_ACCNT_STRING = SUBSTRING(@ACCNT_STRING, 1, @LOFSGMNTALL - @LOFSGMNT) + RTRIM(LTRIM(@Location_Segment)) + SUBSTRING(@ACCNT_STRING, @LOFSGMNTALL - @LOFSGMNTEND + 1, @LOFSGMNTALL)
    END
    SELECT
      @ACTINDX = ISNULL(ACTINDX, 0)
    FROM GL00105(nolock)
    WHERE ACTNUMST = @NEW_ACCNT_STRING
    IF (@ACTINDX <> 0)
    BEGIN
      SELECT
        @UPPVIDX = @ACTINDX
    END
  END
  SELECT
    @RcptLineNoteIDArray_1 = NOTEINDX
  FROM IV00101(nolock)
  WHERE ITEMNMBR = @I_vITEMNMBR
  SELECT
    @RcptLineNoteIDArray_2 = NOTEINDX
  FROM IV40700(nolock)
  WHERE LOCNCODE = @I_vLOCNCODE
  SELECT
    @RcptLineNoteIDArray_3 = ISNULL(NOTEINDX, 0)
  FROM GL00100(nolock)
  WHERE ACTINDX = @I_vINVINDX
  SELECT
    @RcptLineNoteIDArray_4 = 0
  SELECT
    @RcptLineNoteIDArray_5 = 0
  SELECT
    @RcptLineNoteIDArray_6 = NOTEINDX
  FROM TX00101(nolock)
  WHERE TAXSCHID = @I_vPurchase_Item_Tax_Schedu
  SELECT
    @RcptLineNoteIDArray_7 = NOTEINDX
  FROM TX00101(nolock)
  WHERE TAXSCHID = @I_vPurchase_Site_Tax_Schedu
  SELECT
    @RcptLineNoteIDArray_8 = NOTEINDX
  FROM IV41101(nolock)
  WHERE Landed_Cost_Group_ID = @I_vLanded_Cost_Group_ID
  IF (@O_iErrorState <> 0)
  BEGIN
    RETURN (@O_iErrorState)
  END
  WHILE (1 = 1)
  BEGIN
    IF (@I_vPONUMBER <> '')
    BEGIN
      UPDATE POP10100
      SET REMSUBTO = ROUND(OREMSUBT - ROUND((@ORUNTCST * @I_vQTYINVCD), @EDITDECPLCUR), @EDITDECPLCUR),
          OREMSUBT = ROUND(OREMSUBT - ROUND((@ORUNTCST * @I_vQTYINVCD), @EDITDECPLCUR), @EDITDECPLCUR)
      WHERE PONUMBER = @I_vPONUMBER
      IF (@@error <> 0)
      BEGIN
        SELECT
          @O_iErrorState = 9379
        EXEC @iStatus = taUpdateString @O_iErrorState,
                                       @oErrString,
                                       @oErrString OUTPUT,
                                       @O_oErrorState OUTPUT
        RETURN (@O_iErrorState)
      END
    END
    INSERT POP10310 (POPRCTNM, RCPTLNNM, PONUMBER, ITEMNMBR, ITEMDESC, VNDITNUM, VNDITDSC, UMQTYINB, ACTLSHIP, COMMNTID, INVINDX, UOFM, UNITCOST, EXTDCOST, LOCNCODE, RcptLineNoteIDArray_1, RcptLineNoteIDArray_2, RcptLineNoteIDArray_3, RcptLineNoteIDArray_4, RcptLineNoteIDArray_5, RcptLineNoteIDArray_6, RcptLineNoteIDArray_7, RcptLineNoteIDArray_8, NONINVEN, DECPLCUR, DECPLQTY, ITMTRKOP, VCTNMTHD, TRXSORCE, JOBNUMBR, COSTCODE, COSTTYPE, CURNCYID, CURRNIDX, RATETPID, XCHGRATE, RATECALC, DENXRATE, ORUNTCST, OREXTCST, ODECPLCU, BOLPRONUMBER, Capital_Item, Product_Indicator, Purchase_IV_Item_Taxable, Purchase_Item_Tax_Schedu, Purchase_Site_Tax_Schedu, BSIVCTTL, TAXAMNT, ORTAXAMT, BCKTXAMT, OBTAXAMT, Revalue_Inventory, Tolerance_Percentage, PURPVIDX, Remaining_AP_Amount, SHIPMTHD, Landed_Cost_Group_ID, Landed_Cost_Warnings, BackoutTradeDiscTax, OrigBackoutTradeDiscTax, Landed_Cost, Invoice_Match, RCPTRETNUM, RCPTRETLNNUM, INVRETNUM, INVRETLNNUM, ISLINEINTRA, ProjNum, CostCatID)
      SELECT
        @I_vPOPRCTNM,
        @I_vRCPTLNNM,
        @I_vPONUMBER,
        @I_vITEMNMBR,
        @I_vITEMDESC,
        @I_vVNDITNUM,
        @I_vVNDITDSC,
        @UMQTYINB,
        @I_vACTLSHIP,
        '',
        @I_vINVINDX,
        @I_vUOFM,
        CASE
          WHEN (@I_vAUTOCOST = 0) THEN @I_vUNITCOST
          ELSE @ORUNTCST
        END,
        CASE
          WHEN (@I_vAUTOCOST = 0 AND
            @I_vPOPTYPE = 1) OR
            (@PassedExtCost = 1) THEN @I_vEXTDCOST
          WHEN (@I_vAUTOCOST = 1 AND
            @I_vPOPTYPE = 1) THEN ROUND(@ORUNTCST * @I_vQTYSHPPD, @EDITDECPLCUR)
          WHEN (@I_vAUTOCOST = 0 AND
            @I_vPOPTYPE = 3) THEN ROUND(@I_vUNITCOST * @I_vQTYINVCD, @EDITDECPLCUR)
          WHEN (@I_vAUTOCOST = 1 AND
            @I_vPOPTYPE = 3) THEN ROUND(@ORUNTCST * @I_vQTYINVCD, @EDITDECPLCUR)
          ELSE 0
        END,
        @I_vLOCNCODE,
        @RcptLineNoteIDArray_1,
        @RcptLineNoteIDArray_2,
        @RcptLineNoteIDArray_3,
        @RcptLineNoteIDArray_4,
        @RcptLineNoteIDArray_5,
        @RcptLineNoteIDArray_6,
        @RcptLineNoteIDArray_7,
        @RcptLineNoteIDArray_8,
        @I_vNONINVEN,
        @DECPLCUR,
        CASE
          WHEN (@I_vNONINVEN = 1) AND
            (@PROJECTLOADED = 1) THEN @PADECPLQTY
          ELSE @DECPLQTY
        END,
        @ITMTRKOP,
        @VCTNMTHD,
        '',
        @I_vJOBNUMBR,
        '',
        0,
        CASE
          WHEN @MCINSTALLED = 1 THEN @I_vCURNCYID
          ELSE ''
        END,
        CASE
          WHEN @MCINSTALLED = 1 THEN @CURRNIDX
          ELSE 0
        END,
        '',
        0,
        0,
        0,
        CASE
          WHEN (@I_vAUTOCOST = 0) THEN @I_vUNITCOST
          ELSE @ORUNTCST
        END,
        CASE
          WHEN (@I_vAUTOCOST = 0 AND
            @I_vPOPTYPE = 1) OR
            (@PassedExtCost = 1) THEN @I_vEXTDCOST
          WHEN (@I_vAUTOCOST = 1 AND
            @I_vPOPTYPE = 1) THEN ROUND(@ORUNTCST * @I_vQTYSHPPD, @EDITDECPLCUR)
          WHEN (@I_vAUTOCOST = 0 AND
            @I_vPOPTYPE = 3) THEN ROUND(@I_vUNITCOST * @I_vQTYINVCD, @EDITDECPLCUR)
          WHEN (@I_vAUTOCOST = 1 AND
            @I_vPOPTYPE = 3) THEN ROUND(@ORUNTCST * @I_vQTYINVCD, @EDITDECPLCUR)
          ELSE 0
        END,
        @ODECPLCU,
        @I_vBOLPRONUMBER,
        @Capital_Item,
        0,
        @I_vPurchase_IV_Item_Taxable,
        @I_vPurchase_Item_Tax_Schedu,
        @I_vPurchase_Site_Tax_Schedu,
        0,
        @I_vTAXAMNT,
        @I_vTAXAMNT,
        0,
        0,
        @Revalue_Inventory,
        @Tolerance_Percentage,
        @PURPVIDX,
        CASE
          WHEN (@I_vAUTOCOST = 0) THEN @I_vEXTDCOST
          WHEN (@I_vAUTOCOST = 1 AND
            @I_vPOPTYPE = 1) THEN ROUND(@ORUNTCST * @I_vQTYSHPPD, @EDITDECPLCUR)
          WHEN (@I_vAUTOCOST = 1 AND
            @I_vPOPTYPE = 3) THEN ROUND(@ORUNTCST * @I_vQTYINVCD, @EDITDECPLCUR)
          ELSE 0
        END,
        @SHIPMTHD,
        @I_vLanded_Cost_Group_ID,
        0,
        0,
        0,
        0,
        0,
        '',
        0,
        '',
        0,
        0,
        @I_vProjNum,
        @I_vCostCatID
    IF (@@error <> 0)
    BEGIN
      SELECT
        @O_iErrorState = 2075
      EXEC @iStatus = taUpdateString @O_iErrorState,
                                     @oErrString,
                                     @oErrString OUTPUT,
                                     @O_oErrorState OUTPUT
      BREAK
    END
    INSERT POP10500 (PONUMBER, POLNENUM, POPRCTNM, RCPTLNNM, QTYSHPPD, QTYINVCD, QTYREJ, QTYMATCH, QTYRESERVED, QTYINVRESERVE, Status, UMQTYINB, OLDCUCST, JOBNUMBR, COSTCODE, COSTTYPE, ORCPTCOST, OSTDCOST, APPYTYPE, POPTYPE, VENDORID, ITEMNMBR, UOFM, TRXLOCTN, DATERECD, RCTSEQNM, SPRCTSEQ, PCHRPTCT, SPRCPTCT, OREXTCST, RUPPVAMT, ACPURIDX, INVINDX, UPPVIDX, NOTEINDX, CURNCYID, CURRNIDX, XCHGRATE, RATECALC, DENXRATE, RATETPID, EXGTBLID, Capital_Item, Product_Indicator, Total_Landed_Cost_Amount, QTYTYPE, Posted_LC_PPV_Amount, QTYREPLACED, QTYINVADJ)
      SELECT
        @I_vPONUMBER,
        CASE
          WHEN (@I_vPONUMBER = '') THEN 0
          ELSE @I_vPOLNENUM
        END,
        @I_vPOPRCTNM,
        @I_vRCPTLNNM,
        @I_vQTYSHPPD,
        @I_vQTYINVCD,
        0,
        0,
        0,
        0,
        0,
        @UMQTYINB,
        @OLDCUCST,
        @JOBNUMBR,
        '',
        0,
        CASE
          WHEN (@I_vAUTOCOST = 0) THEN @I_vUNITCOST
          ELSE @ORUNTCST
        END,
        @STNDCOST,
        1,
        @I_vPOPTYPE,
        @I_vVENDORID,
        @I_vITEMNMBR,
        @UOFM,
        @I_vLOCNCODE,
        '',
        0,
        0,
        0,
        0,
        CASE
          WHEN (@I_vAUTOCOST = 0) THEN ROUND(@I_vUNITCOST * (@I_vQTYSHPPD - @I_vQTYINVCD), @EDITDECPLCUR)
          WHEN (@I_vAUTOCOST = 1) THEN ROUND(@ORUNTCST * (@I_vQTYSHPPD - @I_vQTYINVCD), @EDITDECPLCUR)
          ELSE 0
        END,
        @RUPPVAMT,
        @ACPURIDX,
        @I_vINVINDX,
        @UPPVIDX,
        0,
        CASE
          WHEN @MCINSTALLED = 1 THEN @I_vCURNCYID
          ELSE ''
        END,
        CASE
          WHEN @MCINSTALLED = 1 THEN @CURRNIDX
          ELSE 0
        END,
        0,
        0,
        0,
        '',
        '',
        @Capital_Item,
        0,
        0,
        1,
        0,
        0,
        0
    IF (@@error <> 0)
    BEGIN
      SELECT
        @O_iErrorState = 2076
      EXEC @iStatus = taUpdateString @O_iErrorState,
                                     @oErrString,
                                     @oErrString OUTPUT,
                                     @O_oErrorState OUTPUT
      BREAK
    END
    IF (@I_vProjNum <> '')
    BEGIN
      INSERT PA10702 (PAVIDN, RCPTLNNM, PApurordnum, ITEMNMBR, CURRNIDX, DATEVAL, DENXRATE, EXCHDATE, XCHGRATE, EXGTBLID, ITMTSHID, MCTRXSTT, ORD, ORTDISAM, PAAcctgMethod, PAACREV, PA_Base_Billing_Rate, PABaseOvhdCost, PABase_Qty, PABase_Unit_Cost, PAbillnoteidx, PABILRATE, PABilling_StatusN, PAbllngtype, PACBADDFRMFLY, PACHGORDNO, PACogs_Idx, PAContra_Account_IDX, PACONTNUMBER, PACOSTCATID, PACOSTCATNME, PAIV_Item_Checkbox, PALineItemSeq, PAMARKPERCENT, PA_MC_Accrued_Revenue, PA_MC_Base_Billing_Rate, PA_MC_Billing_Rate, PAMCCURNCYID, PA_MC_VarianceAccruedRev, PAORIACCRREV, PAORIGBASEOVRHCST, PAORIGBILLRATE, PAORIGOVHDAMT, PAORIGPROFAMT, PAOrigSEC, PAORIGTOTOVRHD, PAORIGTOTPROF, PAOrigVarExt, PAOverhead_Amount, PAOverhead_IDX, PAOverheaPercentage, PAPO_Line_Errors, PAProfitAmount, PAProfitPercent, PAProfitType, PAPROJNUMBER, PAProjectType, PAPurchase_Tax_Options, PAShipmentExtCost, PATOTALOVERH, PATotalProfit, PATU, PAUnbilled_AR_Idx, PAUnbilled_Proj_Rev_Idx, PAUD1, PAUD2, PA_Variance_Cost, PA_Variance_Accrued_Reve, PA_Variance_QTY_Accrued_, PACGBWIPIDX, POTYPE, POPTYPE, PRICELVL, RATECALC, RATETPID, receiptdate, TIME1, TRDISAMT, UOMSCHDL, VENDORID)
        SELECT
          @I_vPOPRCTNM,
          @I_vRCPTLNNM,
          @I_vPONUMBER,
          @I_vITEMNMBR,
          @PO_CURRNIDX,
          CASE
            WHEN @I_vreceiptdate = '' THEN CONVERT(varchar(12), GETDATE())
            ELSE @I_vreceiptdate
          END,
          0,
          '',
          0,
          '',
          CASE
            WHEN @PAPurchase_Tax_Options <> 1 THEN ''
            ELSE @PO_ITMTSHID
          END,
          0,
          @I_vPOLNENUM,
          @PO_TRDISAMT,
          @PAAcctgMethod,
          @PAACREV,
          @PA_Base_Billing_Rate,
          @PABaseOvhdCost,
          @I_vQTYSHPPD,
          CASE
            WHEN (@I_vAUTOCOST = 0) THEN @I_vUNITCOST
            ELSE @ORUNTCST
          END,
          @NOTEINDX,
          @PABILRATE,
          CASE
            WHEN @PAbllngtype = 3 THEN 4
            ELSE @PABilling_StatusN
          END,
          @PAbllngtype,
          0,
          @PO_PACHGORDNO,
          CASE
            WHEN @I_vNONINVEN = 1 THEN @PO_PACogs_Idx
            ELSE @PACogs_Idx
          END,
          @PAContra_Account_IDX,
          @PACONTNUMBER,
          @I_vCostCatID,
          @PACOSTCATNME,
          @PO_PAIV_Item_Checkbox,
          @PO_PALineItemSeq,
          @PAMARKPERCENT,
          @PA_MC_Accrued_Revenue,
          @PA_MC_Base_Billing_Rate,
          @PA_MC_Billing_Rate,
          @PO_CURNCYID,
          @PA_MC_VarianceAccruedRev,
          @PAACREV,
          @PABaseOvhdCost,
          @PABILRATE,
          @PAOverhead_Amount,
          @PAProfitAmount,
          CASE
            WHEN (@I_vAUTOCOST = 0) THEN ROUND(@I_vUNITCOST * @I_vQTYSHPPD, @EDITDECPLCUR)
            WHEN (@I_vAUTOCOST = 1) THEN ROUND(@ORUNTCST * @I_vQTYSHPPD, @EDITDECPLCUR)
            ELSE 0
          END,
          @PATOTALOVERH,
          @PATotalProfit,
          @PA_Variance_Accrued_Reve,
          @PAOverhead_Amount,
          @PAOverhead_IDX,
          @PAOverheaPercentage,
          0,
          @PAProfitAmount,
          @PAProfitPercent,
          @PAProfitType,
          @I_vProjNum,
          @PAProjectType,
          @PAPurchase_Tax_Options,
          CASE
            WHEN (@I_vAUTOCOST = 0) THEN ROUND(@I_vUNITCOST * @I_vQTYSHPPD, @EDITDECPLCUR)
            WHEN (@I_vAUTOCOST = 1) THEN ROUND(@ORUNTCST * @I_vQTYSHPPD, @EDITDECPLCUR)
            ELSE 0
          END,
          @PATOTALOVERH,
          @PATotalProfit,
          @PO_PATU,
          @PAUnbilled_AR_Idx,
          @PAUnbilled_Proj_Rev_Idx,
          '',
          '',
          @PA_Variance_Cost,
          @PA_Variance_Accrued_Reve,
          @PA_Variance_QTY_Accrued_,
          @PO_PACGBWIPIDX,
          @POTYPE,
          @I_vPOPTYPE,
          @PO_PRICELVL,
          0,
          '',
          CASE
            WHEN @I_vreceiptdate = '' THEN CONVERT(varchar(12), GETDATE())
            ELSE @I_vreceiptdate
          END,
          '',
          @PO_TRDISAMT,
          @PO_UOMSCHDL,
          @I_vVENDORID
      IF @@error <> 0
      BEGIN
        SELECT
          @O_iErrorState = 8175
        EXEC @iStatus = taUpdateString @O_iErrorState,
                                       @oErrString,
                                       @oErrString OUTPUT,
                                       @O_oErrorState OUTPUT
        BREAK
      END
      INSERT PA10721 (PApurordnum, POLNENUM, PAVIDN, RCPTLNNM, VENDORID, Status, ITEMNMBR, UOFM, APPYTYPE, EXTDCOST, ORUNTCST, PACOSTCATID, PAPROJNUMBER, RCPTCOST, UNITCOST)
        SELECT
          @I_vPONUMBER,
          CASE
            WHEN (@I_vPONUMBER = '') THEN 0
            ELSE @I_vPOLNENUM
          END,
          @I_vPOPRCTNM,
          @I_vRCPTLNNM,
          @I_vVENDORID,
          @Status,
          @I_vITEMNMBR,
          @I_vUOFM,
          1,
          CASE
            WHEN (@I_vAUTOCOST = 0) THEN @I_vEXTDCOST
            WHEN (@I_vAUTOCOST = 1 AND
              @I_vPOPTYPE = 1) THEN ROUND(@ORUNTCST * @I_vQTYSHPPD, @EDITDECPLCUR)
            WHEN (@I_vAUTOCOST = 1 AND
              @I_vPOPTYPE = 3) THEN ROUND(@ORUNTCST * @I_vQTYINVCD, @EDITDECPLCUR)
            ELSE 0
          END,
          CASE
            WHEN (@I_vAUTOCOST = 0) THEN @I_vUNITCOST
            ELSE @ORUNTCST
          END,
          @I_vCostCatID,
          @I_vProjNum,
          0,
          CASE
            WHEN (@I_vAUTOCOST = 0) THEN @I_vUNITCOST
            ELSE @ORUNTCST
          END
      IF @@error <> 0
      BEGIN
        SELECT
          @O_iErrorState = 8176
        EXEC @iStatus = taUpdateString @O_iErrorState,
                                       @oErrString,
                                       @oErrString OUTPUT,
                                       @O_oErrorState OUTPUT
        BREAK
      END
    END
    IF (@I_vProjNum <> '')
      AND (@I_vCostCatID <> '')
      AND (@I_vNONINVEN = 1)
    BEGIN
      UPDATE PA01301
      SET PAUnpostedQty = PAUnpostedQty + @I_vQTYSHPPD,
          PAUnpostedTotalCostN = PAUnpostedTotalCostN + ROUND(@I_vUNITCOST * @I_vQTYSHPPD, @EDITDECPLCUR),
          PAUnpostAccrRevN = PAUnpostAccrRevN + @PAACREV
      WHERE PAPROJNUMBER = @I_vProjNum
      AND PACOSTCATID = @I_vCostCatID
      AND PATU = @PO_PATU
      IF @@error <> 0
      BEGIN
        SELECT
          @O_iErrorState = 10408
        EXEC @iStatus = taUpdateString @O_iErrorState,
                                       @oErrString,
                                       @oErrString OUTPUT,
                                       @O_oErrorState OUTPUT
        BREAK
      END
      UPDATE PA01201
      SET PAUnpostedQty = PAUnpostedQty + @I_vQTYSHPPD,
          PAUnpostedTotalCostN = PAUnpostedTotalCostN + ROUND(@I_vUNITCOST * @I_vQTYSHPPD, @EDITDECPLCUR),
          PAUnpostAccrRevN = PAUnpostAccrRevN + @PAACREV
      WHERE PAPROJNUMBER = @I_vProjNum
      IF @@error <> 0
      BEGIN
        SELECT
          @O_iErrorState = 10417
        EXEC @iStatus = taUpdateString @O_iErrorState,
                                       @oErrString,
                                       @oErrString OUTPUT,
                                       @O_oErrorState OUTPUT
        BREAK
      END
      UPDATE PA01101
      SET PAUnpostedQty = PAUnpostedQty + @I_vQTYSHPPD,
          PAUnpostedTotalCostN = PAUnpostedTotalCostN + ROUND(@I_vUNITCOST * @I_vQTYSHPPD, @EDITDECPLCUR),
          PAUnpostAccrRevN = PAUnpostAccrRevN + @PAACREV
      WHERE PACONTNUMBER = @PACONTNUMBER
      IF @@error <> 0
      BEGIN
        SELECT
          @O_iErrorState = 10418
        EXEC @iStatus = taUpdateString @O_iErrorState,
                                       @oErrString,
                                       @oErrString OUTPUT,
                                       @O_oErrorState OUTPUT
        BREAK
      END
      UPDATE PA00501
      SET PAUnpostedQty = PAUnpostedQty + @I_vQTYSHPPD,
          PAUnpostedTotalCostN = PAUnpostedTotalCostN + ROUND(@I_vUNITCOST * @I_vQTYSHPPD, @EDITDECPLCUR)
      WHERE CUSTNMBR = @CUSTNMBR
      IF @@error <> 0
      BEGIN
        SELECT
          @O_iErrorState = 10419
        EXEC @iStatus = taUpdateString @O_iErrorState,
                                       @oErrString,
                                       @oErrString OUTPUT,
                                       @O_oErrorState OUTPUT
        BREAK
      END
    END
    IF ((@ENABLEMULTIBIN = 1)
      AND (@ITMTRKOP = 1)
      AND (@I_vAutoAssignBin = 1)
      AND (@I_vNONINVEN = 0))
    BEGIN
      IF EXISTS (SELECT
          1
        FROM POP10340(nolock)
        WHERE POPRCTNM = @I_vPOPRCTNM
        AND ITEMNMBR = @I_vITEMNMBR
        AND RCPTLNNM = @I_vRCPTLNNM)
      BEGIN
        SELECT
          @O_iErrorState = 9335
        EXEC @iStatus = taUpdateString @O_iErrorState,
                                       @oErrString,
                                       @oErrString OUTPUT,
                                       @O_oErrorState OUTPUT
      END
      IF (@I_vQTYSHPPD > 0)
      BEGIN
        EXEC @iStatus = taPopRcptMultiBin @I_vPOPRCTNM = @I_vPOPRCTNM,
                                          @I_vRCPTLNNM = @I_vRCPTLNNM,
                                          @I_vITEMNMBR = @I_vITEMNMBR,
                                          @I_vBIN = '',
                                          @I_vQUANTITY = @I_vQTYSHPPD,
                                          @I_vUOFM = @I_vUOFM,
                                          @I_vCreateBin = 0,
                                          @I_vRequesterTrx = 1,
                                          @O_iErrorState = @iCustomState OUTPUT,
                                          @oErrString = @iCustomErrString OUTPUT
        SELECT
          @iError = @@error
        IF ((@iStatus <> 0)
          OR (@iCustomState <> 0)
          OR (@iError <> 0))
        BEGIN
          SELECT
            @oErrString = RTRIM(@oErrString) + ' ' + @iCustomErrString
          SELECT
            @O_iErrorState = 9336
          EXEC @iStatus = taUpdateString @O_iErrorState,
                                         @oErrString,
                                         @oErrString OUTPUT,
                                         @O_oErrorState OUTPUT
        END
      END
    END
    IF (@ITMTRKOP IN (2, 3))
    BEGIN
      SELECT
        @SERLTQTY = ISNULL(SUM(SERLTQTY), 0)
      FROM POP10330(nolock)
      WHERE POPRCTNM = @I_vPOPRCTNM
      AND RCPTLNNM = @I_vRCPTLNNM
      IF (ROUND((@I_vQTYSHPPD * @UMQTYINB), (@DECPLQTY - 1)) <> @SERLTQTY)
      BEGIN
        SELECT
          @O_iErrorState = 3389
        EXEC @iStatus = taUpdateString @O_iErrorState,
                                       @oErrString,
                                       @oErrString OUTPUT,
                                       @O_oErrorState OUTPUT
        BREAK
      END
    END
    SELECT
      @I_vEXTDCOST =
                    CASE
                      WHEN (@I_vAUTOCOST = 0 AND
                        @I_vPOPTYPE = 1) THEN @I_vEXTDCOST
                      WHEN (@I_vAUTOCOST = 1 AND
                        @I_vPOPTYPE = 1) THEN ROUND(@ORUNTCST * @I_vQTYSHPPD, @EDITDECPLCUR)
                      WHEN (@I_vAUTOCOST = 0 AND
                        @I_vPOPTYPE = 3) THEN ROUND(@I_vUNITCOST * @I_vQTYINVCD, @EDITDECPLCUR)
                      WHEN (@I_vAUTOCOST = 1 AND
                        @I_vPOPTYPE = 3) THEN ROUND(@ORUNTCST * @I_vQTYINVCD, @EDITDECPLCUR)
                      ELSE 0
                    END
    IF (@I_vLanded_Cost_Group_ID <> '')
    BEGIN
      IF (@I_vreceiptdate = '')
      BEGIN
        SELECT
          @O_iErrorState = 9339
        EXEC @iStatus = taUpdateString @O_iErrorState,
                                       @oErrString,
                                       @oErrString OUTPUT,
                                       @O_oErrorState OUTPUT
        BREAK
      END
      DECLARE LCIDS INSENSITIVE CURSOR FOR
      SELECT
        Landed_Cost_ID
      FROM IV41102(nolock)
      WHERE Landed_Cost_Group_ID = @I_vLanded_Cost_Group_ID
      ORDER BY Landed_Cost_ID
      SET NOCOUNT ON
      OPEN LCIDS
      SELECT
        @iCursorError = @@cursor_rows
      IF @iCursorError = 0
      BEGIN
        DEALLOCATE LCIDS
      END
      ELSE
      BEGIN
        FETCH NEXT FROM LCIDS INTO @Landed_Cost_ID
        WHILE (@@FETCH_STATUS <> -1)
        BEGIN
          IF (@@fetch_status = -2)
          BEGIN
            SELECT
              @O_iErrorState = 9235
            EXEC @iStatus = taUpdateString @O_iErrorState,
                                           @oErrString,
                                           @oErrString OUTPUT,
                                           @O_oErrorState OUTPUT
            BREAK
          END
          SELECT
            @LCCOST =
                     CASE
                       WHEN (@I_vAUTOCOST = 0) THEN ROUND(@I_vUNITCOST * @I_vQTYSHPPD, @EDITDECPLCUR)
                       WHEN (@I_vAUTOCOST = 1) THEN ROUND(@ORUNTCST * @I_vQTYSHPPD, @EDITDECPLCUR)
                       ELSE 0
                     END
          SELECT
            @LCLINENUMBER = @LCLINENUMBER + 16384
          EXEC @iStatus = taPopRcptLandedCost @I_vPOPRCTNM = @I_vPOPRCTNM,
                                              @I_vLanded_Cost_ID = @Landed_Cost_ID,
                                              @I_vRCPTLNNM = @I_vRCPTLNNM,
                                              @I_vLCLINENUMBER = @LCLINENUMBER,
                                              @I_vQTYSHPPD = @I_vQTYSHPPD,
                                              @I_vEXTDCOST = @LCCOST,
                                              @I_vreceiptdate = @I_vreceiptdate,
                                              @I_vRequesterTrx = 1,
                                              @O_iErrorState = @LandedCostErrState OUTPUT,
                                              @oErrString = @LandedCostErrString OUTPUT
          SELECT
            @iError = @@error
          IF ((@iStatus <> 0)
            OR (@LandedCostErrState <> 0)
            OR (@iError <> 0))
          BEGIN
            SELECT
              @oErrString = RTRIM(@oErrString) + ' ' + @LandedCostErrString
            SELECT
              @O_iErrorState = 8050
            EXEC @iStatus = taUpdateString @O_iErrorState,
                                           @oErrString,
                                           @oErrString OUTPUT,
                                           @O_oErrorState OUTPUT
            BREAK
          END
          FETCH NEXT FROM LCIDS INTO @Landed_Cost_ID
        END
        DEALLOCATE LCIDS
      END
    END
    IF (@I_vPONUMBER <> '')
    BEGIN
      IF (@I_vCMMTTEXT <> '')
      BEGIN
        IF (EXISTS (SELECT
            NOTEINDX
          FROM SY03900(nolock)
          WHERE NOTEINDX = @POLNEARY_3)
          )
        BEGIN
          UPDATE SY03900
          SET TXTFIELD = @I_vCMMTTEXT,
              DATE1 = CONVERT(varchar(12), GETDATE()),
              TIME1 = SUBSTRING(CONVERT(varchar(25), GETDATE()), 12, 12)
          WHERE NOTEINDX = @POLNEARY_3
          IF (@@error <> 0)
          BEGIN
            SELECT
              @O_iErrorState = 8272
            EXEC @iStatus = taUpdateString @O_iErrorState,
                                           @oErrString,
                                           @oErrString OUTPUT,
                                           @O_oErrorState OUTPUT
            BREAK
          END
        END
        ELSE
        BEGIN
          INSERT SY03900 (NOTEINDX, DATE1, TIME1, TXTFIELD)
            SELECT
              @POLNEARY_3,
              CONVERT(varchar(12), GETDATE()),
              SUBSTRING(CONVERT(varchar(25), GETDATE()), 12, 12),
              @I_vCMMTTEXT
          IF (@@error <> 0)
          BEGIN
            SELECT
              @O_iErrorState = 8271
            EXEC @iStatus = taUpdateString @O_iErrorState,
                                           @oErrString,
                                           @oErrString OUTPUT,
                                           @O_oErrorState OUTPUT
            BREAK
          END
        END
      END
      ELSE
      BEGIN
        IF (@I_vCMMTTEXT = '')
        BEGIN
          DELETE SY03900
          WHERE NOTEINDX = @POLNEARY_3
          IF (@@error <> 0)
          BEGIN
            SELECT
              @O_iErrorState = 8273
            EXEC @iStatus = taUpdateString @O_iErrorState,
                                           @oErrString,
                                           @oErrString OUTPUT,
                                           @O_oErrorState OUTPUT
            BREAK
          END
        END
      END
    END
    BREAK
  END
  EXEC @iStatus = taPopRcptLineInsertPost @I_vPOPTYPE,
                                          @I_vPOPRCTNM,
                                          @I_vPONUMBER,
                                          @I_vITEMNMBR,
                                          @I_vITEMDESC,
                                          @I_vVENDORID,
                                          @I_vRCPTLNNM,
                                          @I_vVNDITNUM,
                                          @I_vVNDITDSC,
                                          @I_vACTLSHIP,
                                          @I_vINVINDX,
                                          @I_vInventoryAccount,
                                          @I_vUOFM,
                                          @I_vUNITCOST,
                                          @I_vEXTDCOST,
                                          @I_vNONINVEN,
                                          @I_vJOBNUMBR,
                                          @I_vBOLPRONUMBER,
                                          @I_vQTYSHPPD,
                                          @I_vQTYINVCD,
                                          @I_vAUTOCOST,
                                          @I_vPurchase_IV_Item_Taxable,
                                          @I_vPurchase_Item_Tax_Schedu,
                                          @I_vPurchase_Site_Tax_Schedu,
                                          @I_vTAXAMNT,
                                          @I_vLanded_Cost_Group_ID,
                                          @I_vLOCNCODE,
                                          @I_vPOLNENUM,
                                          @I_vreceiptdate,
                                          @I_vCURNCYID,
                                          @I_vProjNum,
                                          @I_vCostCatID,
                                          @I_vAutoAssignBin,
                                          @I_vCMMTTEXT,
                                          @I_vRequesterTrx,
                                          @I_vUSRDEFND1,
                                          @I_vUSRDEFND2,
                                          @I_vUSRDEFND3,
                                          @I_vUSRDEFND4,
                                          @I_vUSRDEFND5,
                                          @O_iErrorState = @iCustomState OUTPUT,
                                          @oErrString = @iCustomErrString OUTPUT
  SELECT
    @iError = @@error
  IF ((@iStatus = 0)
    AND (@iError <> 0))
  BEGIN
    SELECT
      @iStatus = @iError
  END
  IF ((@iStatus <> 0)
    OR (@iCustomState <> 0))
  BEGIN
    SELECT
      @oErrString = RTRIM(@oErrString) + ' ' + LTRIM(RTRIM(@iCustomErrString))
    SELECT
      @O_iErrorState = 150
    EXEC @iStatus = taUpdateString @O_iErrorState,
                                   @oErrString,
                                   @oErrString OUTPUT,
                                   @O_oErrorState OUTPUT
  END
  IF (@I_vRequesterTrx = 0)
  BEGIN
    EXEC @iStatus = eConnectOutVerify @I_vDOCTYPE = 'PO_Receiving_Transaction',
                                      @I_vINDEX1 = @I_vPOPRCTNM,
                                      @I_vINDEX2 = '',
                                      @I_vINDEX3 = '',
                                      @I_vINDEX4 = '',
                                      @I_vINDEX5 = '',
                                      @I_vINDEX6 = '',
                                      @I_vINDEX7 = '',
                                      @I_vINDEX8 = '',
                                      @I_vINDEX9 = '',
                                      @I_vINDEX10 = '',
                                      @I_vINDEX11 = '',
                                      @I_vINDEX12 = '',
                                      @I_vINDEX13 = '',
                                      @I_vINDEX14 = '',
                                      @I_vINDEX15 = '',
                                      @I_vDelete = 1,
                                      @O_iErrorState = @iCustomState OUTPUT
    SELECT
      @iError = @@error
    IF ((@iStatus = 0)
      AND (@iError <> 0))
    BEGIN
      SELECT
        @iStatus = @iError
    END
    IF ((@iStatus <> 0)
      OR (@iCustomState <> 0))
    BEGIN
      SELECT
        @O_iErrorState = 1290
      EXEC @iStatus = taUpdateString @O_iErrorState,
                                     @oErrString,
                                     @oErrString OUTPUT,
                                     @O_oErrorState OUTPUT
      RETURN (@O_iErrorState)
    END
  END
  RETURN (@O_iErrorState)
GO

Hope that this helps.


Regards,

--
Mohammad R. Daoud MVP - MCT
MCP, MCBMSP, MCTS, MCBMSS
+962 - 79 - 999 65 85
me@mohdaoud.com
http://www.di.jo

Related Posts:

Related Posts with Thumbnails