Skip to Content
0

How to use an Addon SAP B1 with a web script php

Aug 01, 2017 at 09:51 PM

72

avatar image
Former Member

Hello, I want to use a php script which makes Order Sales but I have to use an Addon to consult the "price" of the products in the order because the products are divided in batches and their prices varying.

I want to know how to access to Addon from an php script, I leave the code here:

<?php


require_once('../connections/sap.php');
require_once('sanitize_string.php');




$iRetVal = 0; //Variable de control de errores
$sError = ""; //String del error de $iRetVal
//Base de datos de acuerdo al Id de la empresa [1 -> Droguería Proconsumo, 2 -> Proconsumo]


if (isset($_GET['action'])) {
    switch ($_GET['action']) {
        case 'addOrder':
            require_once('../../../phpframework/headers/headers.php');
            setHeadersTextFile();
            echo addOrder();
            break;
        case 'getJSON':
            getJSON();
            break;
    }
}




function getAdditionalId($cardCode) {
    return getClienteRTN(sprintf("SELECT OCRD.\"AddID\" FROM OCRD WHERE OCRD.\"CardCode\" = '%s'", $cardCode));
}


function executeScalarSap($query) {
    $oCompany = Connect($_GET['ocompany']);


    $oRecordSet = $oCompany->GetBusinessObject(300); // 300 = BoRecordset
    $oRecordSet->DoQuery($query);
    $arr = array();


    while (!$oRecordSet->EoF) {
        $arrRow = array();


        foreach ($oRecordSet->Fields as $key) {
            $field = $key->Name;
            
            if (sizeof($arr) >= 0) {
                $arrRow[$field] = (string) utf8_decode(sanitize_string($key->Value));
            } else {
                $arrRow[] = (string) utf8_decode($field);
            }
        }
        $oRecordSet->MoveNext();
        array_push($arr, $arrRow);
    }
    return $arr;
}


function getClienteRTN($query) {
    $oCompany = Connect($_GET['ocompany']);


    $oRecordSet = $oCompany->GetBusinessObject(300); // 300 = BoRecordset
    $oRecordSet->DoQuery($query);
    $oRecordSet->MoveFirst();
    if ($oRecordSet->EoF) {
        return;
    } else {
        return $oRecordSet->Fields->Item(0)->Value;
    }
}

function addOrder() {
    $comp = $_GET["ocompany"]; //Id de la compañía que llevará la URL
    $oCompany = Connect($comp); // Nos conectamos a la base de datos correspondiente a la compañía
    $now = date("d-m-Y");
    $docDueDate = date("d-m-Y", strtotime($now . "+ 30 days"));
    $cardCode = $_GET['cardcode'];
    $userFields = getAdditionalId($_GET['cardcode']);
    $detalle = $_GET['detalle'];
        
    if ($oCompany) {


        if ($oCompany->CompanyDb != getCompanyDBById($comp)) {
            $oCompany->Disconnect();
            Connect($comp);
        }



        $oOrder = $oCompany->GetBusinessObject(17); //Objeto de negocios de SAP para efectuar "Órdenes de Compra" [17]
        
        $oOrder->DocDate = $now;
        $oOrder->DocDueDate = $docDueDate;
        $oOrder->CardCode = $cardCode;
        $oOrder->UserFields->Fields->Item("U_RTN")->Value = $userFields;


        $firstLine = true;
        for ($i = 1; $i <= $detalle; $i++) {
            if ($firstLine) {
                $firstLine = false;
            } else {
                $oOrder->Lines->Add();
            }


            $oOrder->Lines->ItemCode = isset($_GET["p$i"]) ? $_GET["p$i"] : ''; // Codigo Producto
            $oOrder->Lines->Quantity = isset($_GET["c$i"]) ? $_GET["c$i"] : ''; // Cantidad
            //$oOrder->Lines->Price = isset($_GET["r$i"]) ? $_GET["r$i"] : '0'; // Precio    //<-- I NEED TO GET THE PRODUCT'S PRICE HERE FROM ADDON [MADE WITH .NET SDK]
            $oOrder->Lines->TaxCode = '';
            //$oOrder->Lines->DiscountPercent = isset($_GET["d$i"]) ? $_GET["d$i"] : '0'; // Descuento
            $oOrder->Lines->WarehouseCode = isset($_GET["s$i"]) ? $_GET["s$i"] : ''; // Serie de lote
            //$oOrder->Lines->WarehouseCode = isset($_GET["u$i"]) ? $_GET["u$i"] : ''; // Ubicación
        }


        $iResponse = $oOrder->Add();


        $iRetVal = "";
        $sError = "";


        if ($iResponse <> 0) {
            $oCompany->GetLastError($iResponse, $sError);
            echo $sError;
            //$message = sprintf("Error al agregar orden de venta: %s", $sError);
            //throw new Exception($message);
        } else {
            echo " Order completed successfully!";
        }
    }
}


function getCompanyDBById($companyID) {
    switch ($companyID) {
        case 1:
            return 'SBO_DROGUERIATEST';
            break;
        case 2:
            return 'SBO_PROCONSUMOTEST';
            break;
    }
}
10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

0 Answers