﻿// Created by ML on 28/10/2021 for update WMSPRODLINE base on the prod line assigned to the resource of the first operation
// Created by ML on 28/10/2021 for update the Production Line Output Bin based on the Beas Resource Item Master Data UDF1
// Updated on 14/03/2022 using the new CreateWorkOrderPositionEnd event triggered on each WO Pos inestead of CreateWorkOrderPositionEnd
//Tested on 17/03/2022 with multiple WO Pos on Beas 2022.02

function CreateWorkOrderPositionEnd
sql=SELECT "U_PMX_IMBP" into iswhspdmx from "OWHS" a WHERE a."WhsCode" IN (SELECT TOP 1 "WhsCode" FROM "BEAS_FTPOS" c WHERE c."BELNR_ID" = <e_WoDocEntry> AND c."BELPOS_ID" = <e_WoLineNumber>)
if <iswhspdmx> = 'Y' then
 select "BEAS_APLATZ"."WMS_PRODLINE" into wmsprodline from "BEAS_APLATZ"  &
        INNER JOIN "BEAS_FTPOS" c ON c."BELNR_ID" = <e_WoDocEntry> AND c."BELPOS_ID" = <e_WoLineNumber> &
        INNER JOIN "BEAS_FTAPL" a ON a."BELNR_ID" = <e_WoDocEntry>  AND a."BELPOS_ID" = <e_WoLineNumber>  and a."POS_ID"=10 &
      where "BEAS_APLATZ"."APLATZ_ID"=a."APLATZ_ID"
     
 sql=UPDATE "BEAS_FTPOS" SET "WMS_PRODLINE" = <wmsprodline,dbstring> where "BELNR_ID"=<e_WoDocEntry> and "BELPOS_ID"=<e_WoLineNumber>

SELECT "UDF1" into checkoutp from "BEAS_APLATZ" d WHERE d."UDF1" IN (SELECT TOP 1 "Code" FROM "PMX_OSBI" e WHERE e."Code"=d."UDF1")
if <iswhspdmx> = 'Y' and then
select "BEAS_APLATZ"."UDF1" into outputbin from "BEAS_APLATZ"  &
        INNER JOIN "BEAS_FTPOS" c ON c."BELNR_ID" = <e_WoDocEntry> AND c."BELPOS_ID" = <e_WoLineNumber> &
        INNER JOIN "BEAS_FTAPL" a ON a."BELNR_ID" = <e_WoDocEntry>  AND a."BELPOS_ID" = <e_WoLineNumber>  and a."POS_ID"=10 &
      where "BEAS_APLATZ"."APLATZ_ID"=a."APLATZ_ID"

sql=UPDATE "BEAS_FTPOS" SET "BINCODE" = <outputbin,dbstring> where "BELNR_ID"=<e_WoDocEntry> and "BELPOS_ID"=<e_WoLineNumber>

end if

end function