Skip to content

Instantly share code, notes, and snippets.

@ranafaraz
Created March 7, 2021 13:35
Show Gist options
  • Save ranafaraz/560f2d7ded93f0a72fe71b99fe6141cc to your computer and use it in GitHub Desktop.
Save ranafaraz/560f2d7ded93f0a72fe71b99fe6141cc to your computer and use it in GitHub Desktop.

Revisions

  1. ranafaraz created this gist Mar 7, 2021.
    1,033 changes: 1,033 additions & 0 deletions Import Legacy Data.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,1033 @@
    <?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "idempiere";

    // Creating connection with MySQL - Local DB
    $conn = mysqli_connect($servername, $username, $password, $dbname);
    // Check connection
    if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
    }

    // Connecting to Postgre SQL - Live iDempiere's DB
    $idbconn = pg_connect("host=000.00.0.000 port=0000 dbname=xyz user=abc password=abc");

    // Fetching Data from Local DB
    $sql = "SELECT
    *
    FROM vouchers_7_march
    ORDER BY id ASC
    LIMIT 2
    ";
    $result = mysqli_query($conn, $sql);

    if (mysqli_num_rows($result) > 0) {

    // Variables to be inserted in Payment Service
    $date_acct = date("Y-m-d h:i:s"); // Format: 2021-01-07 11:59:21.87
    $date_received = date("Y-m-d h:i:s");
    $date_trx = date("Y-m-d h:i:s");


    // Date Time will be stored when the script started for debugging purpose.
    echo "The Data & Time is " . date("Y-m-d h:i:s") . ", when the script started.<br>";


    // output data of each row

    ?>
    <table border='1'>
    <tr>
    <td>Sr #</td>
    <td>ID</td>
    <td>Org code</td>
    <td>Budget Code</td>
    <td>Bill Type</td>
    <td>Narration</td>
    <td>Org ID</td>
    <td>Org Code</td>
    <td>Org Name</td>
    <td>Charge ID</td>
    <td>Charge Code</td>
    <td>Head Name</td>
    </tr>
    <?php
    $count = 1;
    while($row = mysqli_fetch_assoc($result)) {


    // Variable Declaration
    $id = $row["id"];
    $invoice_no = $row["invno"];
    $date = $row["date"];
    $org = $row["org_code"]; // Organization Code from old budget software.
    $b_code = $row["budget_code"]; // Budget Code from old budget software.
    $party_id = $row["party_id"];
    $bill_amt = $row["bill_amt"];
    $gst_base_amt = $row["gst1"];
    $gst_withheld = $row["gst2"];
    $it_percent = $row["it_percent"];
    $it_amt = $row["it_amt"];
    $bill_net_amt = $row["bill_net"]; // After deduction.
    $budget_year = $row["budget_year"];
    $bill_type = $row["bill_type"];
    $narration = $row["narration"];
    $recno = $row["recno"];
    $other_deduction = $row["other_deduction"];
    $created_by = $row["created_by"];
    $other = $row["other"];
    $tempnarration = $row["tempNarration"];
    $flood_tax = $row["flood_tax"]; //PRA Tax
    $security = $row["security"];
    $water_charges = $row["water_charges"];



    // Query iDempiere

    $org = pg_query($idbconn, "SELECT ad_org_id, value, name FROM ad_org WHERE value = '$org' AND ad_client_id = 1000000 "); // Fetching matched Org from iDempiere.
    $charge = pg_query($idbconn, "SELECT c_charge_id, value, name FROM c_charge WHERE value = '$b_code' AND ad_client_id = 1000000 "); // Fetching matched Budget Code from iDempiere.
    //$budget = pg_query($idbconn, "SELECT name FROM gl_budget WHERE isprimary = 'Y' && budgetstatus = 'A' && isactive = 'Y' ");

    $o = pg_fetch_assoc($org);
    $c = pg_fetch_assoc($charge);





    ?>

    <tr>

    <td style="color:red;">
    <?php echo $count; $count++; ?>
    </td>
    <td style="color:red;">
    <?php echo $row["id"]; ?>
    </td>
    <td style="color:red;">
    <?php echo $row["org_code"]; ?>
    </td>
    <td style="color:red;">
    <?php echo $row["budget_code"]; ?>
    </td>
    <td style="color:red;">
    <?php // echo $row["bill_type"]; ?>
    </td>
    <td style="color:red;">
    <?php // echo $row["narration"]; ?>
    </td>

    <?php



    //Organization (ad_org)
    if (pg_num_rows($org) == 0) {
    echo "<td colspan='3'><h1>ORG Not Found!</h1></td>";
    } else {
    echo "<td style=\"color:blue;\"> $o[ad_org_id] </td>";
    echo "<td style=\"color:blue;\"> $o[value] </td>";
    echo "<td style=\"color:blue;\"> $o[name] </td>";
    }
    // while ($o = pg_fetch_assoc($org)) { // Printing Data from AD_Org_ID
    // echo "<td style=\"color:blue;\"> $o[ad_org_id] </td>";
    // echo "<td style=\"color:blue;\"> $o[value] </td>";
    // echo "<td style=\"color:blue;\"> $o[name] </td>";
    // }


    // Charge Head (c_charge)
    if (pg_num_rows($charge) == 0) {
    echo "<td colspan='3'><h1>Charge Not Found!</h1></td>";
    } else {
    echo "<td style=\"color:green;\"> $c[c_charge_id] </td>";
    echo "<td style=\"color:green;\"> $c[value] </td>";
    echo "<td style=\"color:green;\"> $c[name] </td>";
    }
    // while ($c = pg_fetch_assoc($charge)) { // Print Data from C_Charge_ID
    //
    // echo "<td style=\"color:green;\"> $c[c_charge_id] </td>";
    // echo "<td style=\"color:green;\"> $c[value] </td>";
    // echo "<td style=\"color:green;\"> $c[name] </td>";
    // }
    ?>
    </tr>
    <tr>
    <?php

    // echo "Org ID: " . $o["ad_org_id"];
    // echo "Charge ID: " . $c["c_charge_id"];
    // echo "<br/>Bill Net Amt: " .$bill_net_amt;
    // die; /////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    if (isset($o) && isset($c)) { // If Organization & Charge are selected from iDempiere Database.

    // Import of Record Started Successfully
    $import_started = "UPDATE vouchers_7_march SET import_started = 1 WHERE id=$id";
    $import_started_result = mysqli_query($conn, $import_started);

    $raw_org_name = $o["name"];
    $escaped_org = str_replace(array(':', '-', '/', '*', '!', '@', '#', '%', '&', '$', '(', ')'), '', $raw_org_name);

    $raw_narration = $narration;
    $escaped_narration = str_replace(array(':', '-', '/', '*', '!', '@', '#', '&', '$', '(', ')'), '', $raw_narration);


    //Creating Invoice using SOAP API of iDempiere ////////////////////////////////////////////////////////////
    $wsdl = '
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:_0="http://idempiere.org/ADInterface/1_0">
    <soapenv:Header/>
    <soapenv:Body>
    <_0:createData>
    <_0:ModelCRUDRequest>
    <_0:ModelCRUD>
    <_0:serviceType>invoice</_0:serviceType>
    <_0:TableName>C_Invoice</_0:TableName>
    <_0:DataRow>
    <_0:field column="AD_Client_ID">
    <_0:val>1000000</_0:val>
    </_0:field>
    <_0:field column="AD_Org_ID">
    <_0:val>'.$o["ad_org_id"].'</_0:val>
    </_0:field>
    <_0:field column="IsSOTrx">
    <_0:val>N</_0:val>
    </_0:field>
    <_0:field column="M_PriceList_ID">
    <_0:val>1000002</_0:val>
    </_0:field>
    <_0:field column="GL_Budget_ID">
    <_0:val>1000000</_0:val>
    </_0:field>
    <_0:field column="C_DocTypeTarget_ID">
    <_0:val>1000008</_0:val>
    </_0:field>
    <_0:field column="Description">
    <_0:val>Legacy Data: (Narration: '.$escaped_narration.')
    (Invoice ID: '.$invoice_no.') (Date: '.$date.')
    (Org: '.$escaped_org.') (Budget Code: '.$b_code.')
    (Party ID: '.$party_id.') (Bill Amount: '.$bill_amt.')
    (GST: '.$gst_base_amt.') (GST Withheld: '.$gst_withheld.')
    (IT Percent: '.$it_percent.') (IT Amount: '.$it_amt.')
    (Bill Net Amount: '.$bill_net_amt.') (Budget Year: '.$budget_year.')
    (Bill Type: '.$bill_type.') (Rec No: '.$recno.')
    (Other Deduction: '.$other_deduction.') (Created By: '.$created_by.')
    (Other: '.$other.') (Temp Narration: '.$tempnarration.')
    (Flood Tax: '.$flood_tax.') (Security: '.$security.')
    (Water Charges: '.$water_charges.')</_0:val>
    </_0:field>
    <_0:field column="F_Invoice_Type_ID">
    <_0:val>'.$bill_type.'</_0:val>
    </_0:field>
    <_0:field column="C_BPartner_ID">
    <_0:val>1007680</_0:val>
    </_0:field>
    <_0:field column="C_DocType_ID">
    <_0:val>1000008</_0:val>
    </_0:field>
    <_0:field column="GenerateTo">
    <_0:val>N</_0:val>
    </_0:field>
    <_0:field column="AD_User_ID">
    <_0:val>1000001</_0:val>
    </_0:field>
    <_0:field column="C_ConversionType_ID">
    <_0:val>114</_0:val>
    </_0:field>
    <_0:field column="legacy_voc_id">
    <_0:val>'.$id.'</_0:val>
    </_0:field>
    <_0:field column="legacy_party_id">
    <_0:val>'.$party_id.'</_0:val>
    </_0:field>
    <_0:field column="TotalLines">
    <_0:val>'.$bill_amt.'</_0:val>
    </_0:field>
    <_0:field column="GrandTotal">
    <_0:val>'.$bill_amt.'</_0:val>
    </_0:field>
    </_0:DataRow>
    </_0:ModelCRUD>
    <_0:ADLoginRequest>
    <_0:user></_0:user>
    <_0:pass></_0:pass>
    <_0:lang>en_US</_0:lang>
    <_0:ClientID>1000000</_0:ClientID>
    <_0:RoleID>1000000</_0:RoleID>
    <_0:OrgID>0</_0:OrgID>
    <_0:WarehouseID>0</_0:WarehouseID>
    <_0:stage>0</_0:stage>
    </_0:ADLoginRequest>
    </_0:ModelCRUDRequest>
    </_0:createData>
    </soapenv:Body>
    </soapenv:Envelope>';

    $url = "http://localhost/ADInterface/services/ModelADService?wsdl";

    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $wsdl);

    $resp = curl_exec($curl);
    curl_close($curl);
    $clean_xml = str_replace(['ns1:', 'soap:', 'xmlns:'], '', $resp);
    $soap = simplexml_load_string($clean_xml);
    $invoice_id = $soap->Body->createDataResponse->StandardResponse["RecordID"];
    echo "<th>Invoice ID: </th>";
    echo "<th>".$invoice_id."</th>";

    if (isset($invoice_id)) {
    // Invoice Created Successfully
    $invoice_created = "UPDATE vouchers_7_march SET invoice_created = 1 WHERE id=$id";
    $invoice_created_result = mysqli_query($conn, $invoice_created);




    // Loop will stop for (x) seconds.
    //sleep(1);


    // Invoice Line //////////////////////////////////////////////////////////////////////////////



    $wsdl = '
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:_0="http://idempiere.org/ADInterface/1_0">
    <soapenv:Header/>
    <soapenv:Body>
    <_0:createData>
    <_0:ModelCRUDRequest>
    <_0:ModelCRUD>
    <_0:serviceType>invoiceline</_0:serviceType>
    <_0:TableName>C_InvoiceLine</_0:TableName>
    <_0:DataRow>
    <_0:field column="AD_Client_ID">
    <_0:val>1000000</_0:val>
    </_0:field>
    <_0:field column="AD_Org_ID">
    <_0:val>'.$o["ad_org_id"].'</_0:val>
    </_0:field>
    <_0:field column="C_Invoice_ID">
    <_0:val>'.$invoice_id.'</_0:val>
    </_0:field>
    <_0:field column="QtyInvoiced">
    <_0:val>1</_0:val>
    </_0:field>
    <_0:field column="PriceActual">
    <_0:val>'.$bill_amt.'</_0:val>
    </_0:field>
    <_0:field column="LineNetAmt">
    <_0:val>'.$bill_amt.'</_0:val>
    </_0:field>
    <_0:field column="C_Charge_ID">
    <_0:val>'.$c["c_charge_id"].'</_0:val>
    </_0:field>
    <_0:field column="LineTotalAmt">
    <_0:val>'.$bill_amt.'</_0:val>
    </_0:field>
    <_0:field column="QtyEntered">
    <_0:val>1</_0:val>
    </_0:field>
    <_0:field column="PriceEntered">
    <_0:val>'.$bill_amt.'</_0:val>
    </_0:field>
    <_0:field column="RRAmt">
    <_0:val>0</_0:val>
    </_0:field>
    <_0:field column="C_BPartner_ID">
    <_0:val>1007680</_0:val>
    </_0:field>
    </_0:DataRow>
    </_0:ModelCRUD>
    <_0:ADLoginRequest>
    <_0:user></_0:user>
    <_0:pass></_0:pass>
    <_0:lang>en_US</_0:lang>
    <_0:ClientID>1000000</_0:ClientID>
    <_0:RoleID>1000000</_0:RoleID>
    <_0:OrgID>0</_0:OrgID>
    <_0:WarehouseID>0</_0:WarehouseID>
    <_0:stage>0</_0:stage>
    </_0:ADLoginRequest>
    </_0:ModelCRUDRequest>
    </_0:createData>
    </soapenv:Body>
    </soapenv:Envelope>
    ';

    //echo "<pre>" . $wsdl; die;

    $url = "http://localhost/ADInterface/services/ModelADService?wsdl";

    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $wsdl);

    $resp = curl_exec($curl);
    curl_close($curl);
    $clean_xml = str_replace(['ns1:', 'soap:', 'xmlns:'], '', $resp);
    $soap = simplexml_load_string($clean_xml);
    $invoiceline_id = $soap->Body->createDataResponse->StandardResponse["RecordID"];
    $invoiceline_error = $soap->Body->createDataResponse->StandardResponse->Error;
    echo "<th>Invoice Line</th><th>".$invoiceline_id."</th>";

    if(isset($invoiceline_id)) {
    // Invoice Line Created Successfully
    $invoiceline_created = "UPDATE vouchers_7_march SET invoiceline_created = 1 WHERE id=$id";
    $invoiceline_created_result = mysqli_query($conn, $invoiceline_created);


    // Loop will stop for (x) seconds.
    // sleep(1);
    // Sum up Taxes
    $total_deduction = $gst_withheld + $it_amt + $other_deduction + $other + $flood_tax + $security;

    // GST Withholdings - Started //////////////////////////////////////////////////////
    if($gst_withheld > 0){
    $wsdl = '
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:_0="http://idempiere.org/ADInterface/1_0">
    <soapenv:Header/>
    <soapenv:Body>
    <_0:createData>
    <_0:ModelCRUDRequest>
    <_0:ModelCRUD>
    <_0:serviceType>create_withholdings</_0:serviceType>
    <_0:TableName>F_Withholding</_0:TableName>
    <_0:DataRow>
    <_0:field column="AD_Client_ID">
    <_0:val>1000000</_0:val>
    </_0:field>
    <_0:field column="AD_Org_ID">
    <_0:val>0</_0:val>
    </_0:field>
    <_0:field column="C_Invoice_ID">
    <_0:val>'.$invoice_id.'</_0:val>
    </_0:field>
    <_0:field column="C_Tax_ID">
    <_0:val>1000017</_0:val>
    </_0:field>
    <_0:field column="LCO_WithholdingType_ID">
    <_0:val>1000001</_0:val>
    </_0:field>
    <_0:field column="WH_Other_BaseAmt">
    <_0:val>'.$gst_base_amt.'</_0:val>
    </_0:field>
    <_0:field column="Other_Charge">
    <_0:val>'.$gst_withheld.'</_0:val>
    </_0:field>
    <_0:field column="TotalDeduction">
    <_0:val>'.$total_deduction.'</_0:val>
    </_0:field>
    </_0:DataRow>
    </_0:ModelCRUD>
    <_0:ADLoginRequest>
    <_0:user></_0:user>
    <_0:pass></_0:pass>
    <_0:lang>en_US</_0:lang>
    <_0:ClientID>1000000</_0:ClientID>
    <_0:RoleID>1000000</_0:RoleID>
    <_0:OrgID>0</_0:OrgID>
    <_0:WarehouseID>0</_0:WarehouseID>
    <_0:stage>0</_0:stage>
    </_0:ADLoginRequest>
    </_0:ModelCRUDRequest>
    </_0:createData>
    </soapenv:Body>
    </soapenv:Envelope>
    ';

    //echo "<pre>" . $wsdl; die;

    $url = "http://localhost/ADInterface/services/ModelADService?wsdl";

    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $wsdl);

    $resp = curl_exec($curl);
    curl_close($curl);
    $clean_xml = str_replace(['ns1:', 'soap:', 'xmlns:'], '', $resp);
    $soap = simplexml_load_string($clean_xml);
    $wh_gst_id = $soap->Body->createDataResponse->StandardResponse["RecordID"];
    $wh_gst_error = $soap->Body->createDataResponse->StandardResponse->Error;
    echo "<th>WH GST</th><th>".$wh_gst_id."</th>";

    // GST Withholdings - Ended ///////////////////////////////////////////////////////

    }

    if($it_amt > 0) {

    $wsdl = '
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:_0="http://idempiere.org/ADInterface/1_0">
    <soapenv:Header/>
    <soapenv:Body>
    <_0:createData>
    <_0:ModelCRUDRequest>
    <_0:ModelCRUD>
    <_0:serviceType>create_withholdings</_0:serviceType>
    <_0:TableName>F_Withholding</_0:TableName>
    <_0:DataRow>
    <_0:field column="AD_Client_ID">
    <_0:val>1000000</_0:val>
    </_0:field>
    <_0:field column="AD_Org_ID">
    <_0:val>0</_0:val>
    </_0:field>
    <_0:field column="C_Invoice_ID">
    <_0:val>'.$invoice_id.'</_0:val>
    </_0:field>
    <_0:field column="C_Tax_ID">
    <_0:val>1000010</_0:val>
    </_0:field>
    <_0:field column="LCO_WithholdingType_ID">
    <_0:val>1000004</_0:val>
    </_0:field>
    <_0:field column="WH_Other_BaseAmt">
    <_0:val>'.$bill_amt.'</_0:val>
    </_0:field>
    <_0:field column="WH_Other_Percent">
    <_0:val>'.$it_percent.'</_0:val>
    </_0:field>
    <_0:field column="Other_Charge">
    <_0:val>'.$it_amt.'</_0:val>
    </_0:field>
    <_0:field column="TotalDeduction">
    <_0:val>'.$total_deduction.'</_0:val>
    </_0:field>
    </_0:DataRow>
    </_0:ModelCRUD>
    <_0:ADLoginRequest>
    <_0:user></_0:user>
    <_0:pass></_0:pass>
    <_0:lang>en_US</_0:lang>
    <_0:ClientID>1000000</_0:ClientID>
    <_0:RoleID>1000000</_0:RoleID>
    <_0:OrgID>0</_0:OrgID>
    <_0:WarehouseID>0</_0:WarehouseID>
    <_0:stage>0</_0:stage>
    </_0:ADLoginRequest>
    </_0:ModelCRUDRequest>
    </_0:createData>
    </soapenv:Body>
    </soapenv:Envelope>
    ';

    //echo "<pre>" . $wsdl; die;

    $url = "http://localhost/ADInterface/services/ModelADService?wsdl";

    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $wsdl);

    $resp = curl_exec($curl);
    curl_close($curl);
    $clean_xml = str_replace(['ns1:', 'soap:', 'xmlns:'], '', $resp);
    $soap = simplexml_load_string($clean_xml);
    $wh_it_id = $soap->Body->createDataResponse->StandardResponse["RecordID"];
    $wh_it_error = $soap->Body->createDataResponse->StandardResponse->Error;
    echo "<th>WH IT</th><th>".$wh_it_id."</th>";

    // IT - Withholdings - Ended ///////////////////////////////////////////////////////

    // Other - Withholdings - Started

    }

    if ($other > 0) {
    $wsdl = '
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:_0="http://idempiere.org/ADInterface/1_0">
    <soapenv:Header/>
    <soapenv:Body>
    <_0:createData>
    <_0:ModelCRUDRequest>
    <_0:ModelCRUD>
    <_0:serviceType>create_withholdings</_0:serviceType>
    <_0:TableName>F_Withholding</_0:TableName>
    <_0:DataRow>
    <_0:field column="AD_Client_ID">
    <_0:val>1000000</_0:val>
    </_0:field>
    <_0:field column="AD_Org_ID">
    <_0:val>0</_0:val>
    </_0:field>
    <_0:field column="C_Invoice_ID">
    <_0:val>'.$invoice_id.'</_0:val>
    </_0:field>
    <_0:field column="LCO_WithholdingType_ID">
    <_0:val>1000009</_0:val>
    </_0:field>
    <_0:field column="Other_Charge">
    <_0:val>'.$other.'</_0:val>
    </_0:field>
    <_0:field column="TotalDeduction">
    <_0:val>'.$total_deduction.'</_0:val>
    </_0:field>
    </_0:DataRow>
    </_0:ModelCRUD>
    <_0:ADLoginRequest>
    <_0:user></_0:user>
    <_0:pass></_0:pass>
    <_0:lang>en_US</_0:lang>
    <_0:ClientID>1000000</_0:ClientID>
    <_0:RoleID>1000000</_0:RoleID>
    <_0:OrgID>0</_0:OrgID>
    <_0:WarehouseID>0</_0:WarehouseID>
    <_0:stage>0</_0:stage>
    </_0:ADLoginRequest>
    </_0:ModelCRUDRequest>
    </_0:createData>
    </soapenv:Body>
    </soapenv:Envelope>
    ';

    //echo "<pre>" . $wsdl; die;

    $url = "http://localhost/ADInterface/services/ModelADService?wsdl";

    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $wsdl);

    $resp = curl_exec($curl);
    curl_close($curl);
    $clean_xml = str_replace(['ns1:', 'soap:', 'xmlns:'], '', $resp);
    $soap = simplexml_load_string($clean_xml);
    $wh_other_id = $soap->Body->createDataResponse->StandardResponse["RecordID"];
    $wh_other_error = $soap->Body->createDataResponse->StandardResponse->Error;
    echo "<th>Other WH</th><th>".$wh_other_id."</th>";

    // Other - Withholdings - Ended ///////////////////////////////////////////////////////

    // PST Witholdings - Started
    }

    if ($flood_tax > 0) {
    $wsdl = '
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:_0="http://idempiere.org/ADInterface/1_0">
    <soapenv:Header/>
    <soapenv:Body>
    <_0:createData>
    <_0:ModelCRUDRequest>
    <_0:ModelCRUD>
    <_0:serviceType>create_withholdings</_0:serviceType>
    <_0:TableName>F_Withholding</_0:TableName>
    <_0:DataRow>
    <_0:field column="AD_Client_ID">
    <_0:val>1000000</_0:val>
    </_0:field>
    <_0:field column="AD_Org_ID">
    <_0:val>0</_0:val>
    </_0:field>
    <_0:field column="C_Invoice_ID">
    <_0:val>'.$invoice_id.'</_0:val>
    </_0:field>
    <_0:field column="LCO_WithholdingType_ID">
    <_0:val>1000003</_0:val>
    </_0:field>
    <_0:field column="Other_Charge">
    <_0:val>'.$flood_tax.'</_0:val>
    </_0:field>
    <_0:field column="TotalDeduction">
    <_0:val>'.$total_deduction.'</_0:val>
    </_0:field>
    </_0:DataRow>
    </_0:ModelCRUD>
    <_0:ADLoginRequest>
    <_0:user></_0:user>
    <_0:pass></_0:pass>
    <_0:lang>en_US</_0:lang>
    <_0:ClientID>1000000</_0:ClientID>
    <_0:RoleID>1000000</_0:RoleID>
    <_0:OrgID>0</_0:OrgID>
    <_0:WarehouseID>0</_0:WarehouseID>
    <_0:stage>0</_0:stage>
    </_0:ADLoginRequest>
    </_0:ModelCRUDRequest>
    </_0:createData>
    </soapenv:Body>
    </soapenv:Envelope>
    ';

    //echo "<pre>" . $wsdl; die;

    $url = "http://localhost/ADInterface/services/ModelADService?wsdl";

    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $wsdl);

    $resp = curl_exec($curl);
    curl_close($curl);
    $clean_xml = str_replace(['ns1:', 'soap:', 'xmlns:'], '', $resp);
    $soap = simplexml_load_string($clean_xml);
    $wh_pst_id = $soap->Body->createDataResponse->StandardResponse["RecordID"];
    $wh_pst_id_error = $soap->Body->createDataResponse->StandardResponse->Error;
    echo "<th>PST WH</th><th>".$wh_pst_id."</th>";

    // PST - Withholdings - Ended ///////////////////////////////////////////////////////

    }

    // Complete Invoice using SOAP DoAction Method

    $wsdl = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:_0="http://idempiere.org/ADInterface/1_0">
    <soapenv:Header/>
    <soapenv:Body>
    <_0:setDocAction>
    <_0:ModelSetDocActionRequest>
    <_0:ModelSetDocAction>
    <_0:serviceType>complete_invoice</_0:serviceType>
    <_0:tableName>C_Invoice</_0:tableName>
    <_0:recordID>'.$invoice_id.'</_0:recordID>
    <_0:docAction>CO</_0:docAction>
    </_0:ModelSetDocAction>
    <_0:ADLoginRequest>
    <_0:user></_0:user>
    <_0:pass></_0:pass>
    <_0:lang>en_US</_0:lang>
    <_0:ClientID>1000000</_0:ClientID>
    <_0:RoleID>1000000</_0:RoleID>
    <_0:OrgID>0</_0:OrgID>
    <_0:WarehouseID>0</_0:WarehouseID>
    <_0:stage>0</_0:stage>
    </_0:ADLoginRequest>
    </_0:ModelSetDocActionRequest>
    </_0:setDocAction>
    </soapenv:Body>
    </soapenv:Envelope>';


    $url = "http://localhost/ADInterface/services/ModelADService?wsdl";

    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $wsdl);

    $resp = curl_exec($curl);
    curl_close($curl);
    $clean_xml = str_replace(['ns1:', 'soap:', 'xmlns:'], '', $resp);
    $soap = simplexml_load_string($clean_xml);
    $doc_complete_resp = $soap->Body->setDocActionResponse->StandardResponse["RecordID"];
    echo "<th>Invoice Completed</th><th>".$doc_complete_resp."</th>";

    if (isset($doc_complete_resp)) {
    // Invoice Completed Successfully
    $invoice_completed = "UPDATE vouchers_7_march SET invoice_completed = 1 WHERE id=$id";
    $invoice_completed_result = mysqli_query($conn, $invoice_completed);



    // Create Payment against Invoice ID using SOAP CreatePayment Method

    $wsdl = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:_0="http://idempiere.org/ADInterface/1_0">
    <soapenv:Header/>
    <soapenv:Body>
    <_0:createData>
    <_0:ModelCRUDRequest>
    <_0:ModelCRUD>
    <_0:serviceType>create_payment</_0:serviceType>
    <_0:TableName>C_Payment</_0:TableName>
    <_0:DataRow>
    <_0:field column="AD_Client_ID">
    <_0:val>1000000</_0:val>
    </_0:field>
    <_0:field column="AD_Org_ID">
    <_0:val>'.$o["ad_org_id"].'</_0:val>
    </_0:field>
    <_0:field column="C_BankAccount_ID">
    <_0:val>1000105</_0:val>
    </_0:field>
    <_0:field column="C_BPartner_ID">
    <_0:val>1007680</_0:val>
    </_0:field>
    <_0:field column="C_ConversionType_ID">
    <_0:val>114</_0:val>
    </_0:field>
    <_0:field column="C_Currency_ID">
    <_0:val>306</_0:val>
    </_0:field>
    <_0:field column="C_DocType_ID">
    <_0:val>1000012</_0:val>
    </_0:field>
    <_0:field column="C_Invoice_ID">
    <_0:val>'.$invoice_id.'</_0:val>
    </_0:field>
    <_0:field column="DateAcct">
    <_0:val>'.$date_acct.'</_0:val>
    </_0:field>
    <_0:field column="DateReceived">
    <_0:val>'.$date_received.'</_0:val>
    </_0:field>
    <_0:field column="DateTrx">
    <_0:val>'.$date_trx.'</_0:val>
    </_0:field>
    <_0:field column="PayAmt">
    <_0:val>'.$bill_net_amt.'</_0:val>
    </_0:field>
    <_0:field column="Description">
    <_0:val>'.$total_deduction.'/- PKR have been deducted from '.$bill_amt.'</_0:val>
    </_0:field>
    <_0:field column="TaxAmt">
    <_0:val>0</_0:val>
    </_0:field>
    <_0:field column="WriteOffAmt">
    <_0:val>'.$total_deduction.'</_0:val>
    </_0:field>
    </_0:DataRow>
    </_0:ModelCRUD>
    <_0:ADLoginRequest>
    <_0:user></_0:user>
    <_0:pass></_0:pass>
    <_0:ClientID>1000000</_0:ClientID>
    <_0:RoleID>1000000</_0:RoleID>
    <_0:OrgID>0</_0:OrgID>
    <_0:WarehouseID>0</_0:WarehouseID>
    <_0:stage>0</_0:stage>
    </_0:ADLoginRequest>
    </_0:ModelCRUDRequest>
    </_0:createData>
    </soapenv:Body>
    </soapenv:Envelope>';

    $url = "http://localhost/ADInterface/services/ModelADService?wsdl";

    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $wsdl);

    $resp = curl_exec($curl);
    curl_close($curl);
    $clean_xml = str_replace(['ns1:', 'soap:', 'xmlns:'], '', $resp);
    $soap = simplexml_load_string($clean_xml);
    $payment_id = $soap->Body->createDataResponse->StandardResponse["RecordID"];
    echo "<th>Payment ID</th><th>".$payment_id."</th>";

    // Create Payment Ended
    if (isset($payment_id)) {
    // Payment Created Successfully
    $payment_created = "UPDATE vouchers_7_march SET payment_created = 1 WHERE id=$id";
    $payment_created_result = mysqli_query($conn, $payment_created);




    // Complete Payment
    $wsdl = '
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:_0="http://idempiere.org/ADInterface/1_0">
    <soapenv:Header/>
    <soapenv:Body>
    <_0:setDocAction>
    <_0:ModelSetDocActionRequest>
    <_0:ModelSetDocAction>
    <_0:serviceType>complete_payment</_0:serviceType>
    <_0:tableName>C_Payment</_0:tableName>
    <_0:recordID>'.$payment_id.'</_0:recordID>
    <_0:docAction>CO</_0:docAction>
    </_0:ModelSetDocAction>
    <_0:ADLoginRequest>
    <_0:user></_0:user>
    <_0:pass></_0:pass>
    <_0:lang>en_US</_0:lang>
    <_0:ClientID>1000000</_0:ClientID>
    <_0:RoleID>1000000</_0:RoleID>
    <_0:OrgID>0</_0:OrgID>
    <_0:WarehouseID>0</_0:WarehouseID>
    <_0:stage>0</_0:stage>
    </_0:ADLoginRequest>
    </_0:ModelSetDocActionRequest>
    </_0:setDocAction>
    </soapenv:Body>
    </soapenv:Envelope>
    ';

    $url = "http://localhost/ADInterface/services/ModelADService?wsdl";

    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $wsdl);

    $resp = curl_exec($curl);
    curl_close($curl);
    $clean_xml = str_replace(['ns1:', 'soap:', 'xmlns:'], '', $resp);
    $soap = simplexml_load_string($clean_xml);
    $co_payment_id = $soap->Body->setDocActionResponse->StandardResponse["RecordID"];
    echo "<th>Payment Complete ID</th><th>".$co_payment_id."</th>";

    if (isset($co_payment_id)) {
    // Payment Completed Successfully
    $payment_completed = "UPDATE vouchers_7_march SET payment_completed = 1 WHERE id=$id";
    $payment_completed_result = mysqli_query($conn, $payment_completed);

    // Updating isPaid Status of Invoice
    $ispaid = pg_query($idbconn, "UPDATE c_invoice SET ispaid = 'Y' WHERE c_invoice_id =$invoice_id");
    if(isset($ispaid)) {
    echo "<th>Invoice Ispaid</th>";
    }
    }


    // Complete Payment Ended


    ?>
    </tr>

    <?php
    // Record Imported Successfully
    $is_imported = "UPDATE vouchers_7_march SET is_imported = 1 WHERE id=$id";
    $is_imported_result = mysqli_query($conn, $is_imported);

    } // If Payment is created - closed
    } // If invoice is completed - closed
    } // If Invoice Line ID is Set - Closed
    } // If Invoice ID is Set - Closed
    } // If $o and $c are set - Closed
    // Loop will stop for (x) seconds.
    // sleep(1);
    }


    ?>
    </table>
    <?php

    } else {
    echo "0 results";
    }

    mysqli_close($conn);

    // Date Time will be stored when the script started for debugging purpose.
    echo "The Data & Time is " . date("Y-m-d h:i:s") . ", when the script ended.<br>";

    ?>

    <hr />

    <?php // Checking Connection to iDempiere Postgre SQL DB

    if (!$idbconn){
    echo "<center><h1>Doesn't work =(</h1></center>";
    }else
    echo "<center><h1>iDempiere Connected!</h1></center>";



    pg_close($idbconn);
    ?>