1 (edited by boxygen 11/11/2019 08:15:44 am)

Topic: Creating Invoice through REST API

Using the Latest API module (modified by @ApMuthu) and guidance given in Wiki I modified the following file

facurlrest.php

<?php
// FrontAccounting Bridge REST Test Script
// Author: Ap.Muthu
// Website: www.apmuthu.com
// Release Date: 2012-11-28

include_once "fabridge.php";

$method = isset($_GET['m']) ? $_GET['m'] : 'g'; // g, p, t, d => GET, POST, PUT, DELETE
$action = isset($_GET['a']) ? $_GET['a'] : '';
$record = isset($_GET['r']) ? $_GET['r'] : '';
$filter = isset($_GET['f']) ? $_GET['f'] : false;

$data =  json_encode(array(
      'trans_type' => '10',
      'ref'=> 'NoGuia0001',
      'customer_id'=> '78',
      'branch_id'=> '78',
      'location'=> 'DEF',
      'deliver_to'=> 'ABC, S.A. DE C.V.',
      'delivery_date'=> '2019/11/09',
      'delivery_address'=> 'Karachi',
      'order_date'=> '09/11/2019',
      'phone'=> '',
      'cust_ref'=> '',
      'comments'=> '',
      'ship_via'=> '1',
      'payment'=> '1',
      'sales_type'=> '1',
      'items'=> array
        (
          0 => array
          (
            'stock_id'=> '2801',
            'description'=> 'iPhone',
            'qty'=> '1',
            'price'=> '100',
            'discount'=> '0'
          ),
          0 => array
          (
            'stock_id'=> '2802',
            'description'=> 'Samsung',
            'qty'=> '1',
            'price'=> '100',
            'discount'=> '0'
          ),
        ),

      ));


$output = fa_bridge($method, $action, $record, $filter, $data);

?>

The $data is being read at modules/api/sales.inc at Line: 256 as below

Array
(
    [{"trans_type":"10","ref":"NoGuia0001","customer_id":"78","branch_id":"78","location":"DEF","deliver_to":"ABC,_S_A__DE_C_V_","delivery_date":"2019\/11\/09","delivery_address":"Karachi","order_date":"09\/11\/2019","phone":"","cust_ref":"","comments":"","ship_via":"1","payment":"1","sales_type":"1","items":] => Array
        (
            [{"stock_id":"2802","description":"Samsung","qty":"1","price":"100","discount":"0"}] => 
        )

)

While the Correct JSON Presentation of this Data shall be

{"trans_type":"10","ref":"NoGuia0001","customer_id":"78","branch_id":"78","location":"DEF","deliver_to":"ABC, S.A. DE C.V.","delivery_date":"2019\/11\/09","delivery_address":"Karachi","order_date":"09\/11\/2019","phone":"","cust_ref":"","comments":"","ship_via":"1","payment":"1","sales_type":"1",
"items":[{"stock_id":"2802","description":"Samsung","qty":"1","price":"100","discount":"0"}]}

How to handle this?

Due to wrong presentation of JSON Data I am not able to apply json_decode($data, true).

However, I have tested the api successfully by manually putting the correct presentation of this data on modules/api/sales.inc

Regards.

www.boxygen.pk

Re: Creating Invoice through REST API

You have used the same subscript for both items. Try the following:

$data =  json_encode(array(
      'trans_type' => '10',
      'ref'=> 'NoGuia0001',
      'customer_id'=> '78',
      'branch_id'=> '78',
      'location'=> 'DEF',
      'deliver_to'=> 'ABC, S.A. DE C.V.',
      'delivery_date'=> '2019/11/09',
      'delivery_address'=> 'Karachi',
      'order_date'=> '09/11/2019',
      'phone'=> '',
      'cust_ref'=> '',
      'comments'=> '',
      'ship_via'=> '1',
      'payment'=> '1',
      'sales_type'=> '1',
      'items'=> array
        (
          array
          (
            'stock_id'=> '2801',
            'description'=> 'iPhone',
            'qty'=> '1',
            'price'=> '100',
            'discount'=> '0'
          ),
          array
          (
            'stock_id'=> '2802',
            'description'=> 'Samsung',
            'qty'=> '1',
            'price'=> '100',
            'discount'=> '0'
          ),
        ),

      ));

3 (edited by boxygen 11/11/2019 12:38:54 pm)

Re: Creating Invoice through REST API

Ok Thanks for pointing this out. But problem doesn't lie here.

Even If I pass following simplest data as given in wiki

$data = json_encode(array(
'firstName'=> 'John',
'lastName'=> 'Doe'
));

$output = fa_bridge($method, $action, $record, $filter, $data);

It is parsed in api/sales.inc as below

function sales_add()
{
    $app    = \Slim\Slim::getInstance('SASYS');
    $req    = $app->request();
    $info    = $req->post();

The variable $info is read as below

Array
(
    [{"firstName":"John","lastName":"Doe"}] => 
)

While it shall be parsed as below

{"firstName":"John","lastName":"Doe"}

Please Check this https://prnt.sc/pvdi0b

Post's attachments

raw_json_output_in_php.png 8 kb, file has never been downloaded. 

You don't have the permssions to download the attachments of this post.
www.boxygen.pk

Re: Creating Invoice through REST API

This is possibly a PHP version problem - which version are you using?

In PHP v5.3.1 on XAMPP 1.7.3 on WInXP SP3, the following was output for: echo json_encode($data, true);:

"{\"trans_type\":\"10\",\"ref\":\"NoGuia0001\",\"customer_id\":\"78\",\"branch_id\":\"78\",\"location\":\"DEF\",\"deliver_to\":\"ABC, S.A. DE C.V.\",\"delivery_date\":\"2019\\\/11\\\/09\",\"delivery_address\":\"Karachi\",\"order_date\":\"09\\\/11\\\/2019\",\"phone\":\"\",\"cust_ref\":\"\",\"comments\":\"\",\"ship_via\":\"1\",\"payment\":\"1\",\"sales_type\":\"1\",\"items\":[{\"stock_id\":\"2801\",\"description\":\"iPhone\",\"qty\":\"1\",\"price\":\"100\",\"discount\":\"0\"},{\"stock_id\":\"2802\",\"description\":\"Samsung\",\"qty\":\"1\",\"price\":\"100\",\"discount\":\"0\"}]}"

Re: Creating Invoice through REST API

I am using PHP Version 5.6.40 on CentOs Linux Cloud.

But I think this is not related to PHP version. Have you given the above output after sending the $data through curl request in API?

In my first post I have shown two outputs of same data.

First one is read at the receiving server of API call. (Not JSON optimized)

Second one is at the Send Server before API call. (JSON optimized)

So If the PHP version is causing the issue then both outputs shall be same.

In my case sending and receiving servers are same server with two different directories.

Can you please retest it with the API Call?

www.boxygen.pk

Re: Creating Invoice through REST API

My test was on browser only. Check your PHP.ini settings in CLI and in webserver for any differences.

The CLI output for php check.php is:

"{\"trans_type\":\"10\",\"ref\":\"NoGuia0001\",\"customer_id\":\"78\",\"branch_id\":\"78\",\"location\":\"DEF\",\"deliver_to\":\"ABC, S.A. DE C.V.\",\"delivery_date\":\"2019\\\/11\\\/09\",\"delivery_address\":\"Karachi\",\"order_date\":\"09\\\/11\\\/2019\",\"phone\":\"\",\"cust_ref\":\"\",\"comments\":\"\",\"ship_via\":\"1\",\"payment\":\"1\",\"sales_type\":\"1\",\"items\":[{\"stock_id\":\"2801\",\"description\":\"iPhone\",\"qty\":\"1\",\"price\":\"100\",\"discount\":\"0\"},{\"stock_id\":\"2802\",\"description\":\"Samsung\",\"qty\":\"1\",\"price\":\"100\",\"discount\":\"0\"}]}"

Re: Creating Invoice through REST API

Ok, one more thing.

The current core api module doesn't have the line of code to decode as below.

json_decode($info, true);

So is it assumed that the POST data being sent through api is already decoded by the time it reaches at this point

function sales_add()
{
    $app    = \Slim\Slim::getInstance('SASYS');
    $req    = $app->request();
    $info    = $req->post();

Or something is missed here?

Because without converting the JSON data into PHP Array we can't add it to cart.

www.boxygen.pk

Re: Creating Invoice through REST API

@apmuthu, I was right. The problem was not of PHP.

This Post helped me. STACK OVERFLOW

now facurlrest.php looks like below

<?php
// FrontAccounting Bridge REST Test Script
// Author: Ap.Muthu
// Website: www.apmuthu.com
// Release Date: 2012-11-28

include_once "fabridge.php";

$method = isset($_GET['m']) ? $_GET['m'] : 'g'; // g, p, t, d => GET, POST, PUT, DELETE
$action = isset($_GET['a']) ? $_GET['a'] : '';
$record = isset($_GET['r']) ? $_GET['r'] : '';
$filter = isset($_GET['f']) ? $_GET['f'] : false;

$data =  array(
      'trans_type' => '10',
      'ref'=> 'NoGuia0001',
      'customer_id'=> '78',
      'branch_id'=> '78',
      'location'=> 'DEF',
      'deliver_to'=> 'ABC, S.A. DE C.V.',
      'delivery_date'=> '09/11/2019',
      'delivery_address'=> 'Karachi',
      'order_date'=> '09/11/2019',
      'phone'=> '',
      'cust_ref'=> '',
      'comments'=> '',
      'ship_via'=> '1',
      'payment'=> '1',
      'sales_type'=> '1',
      'items'=> array
        (
          0 => array
          (
            'stock_id'=> '2801',
            'description'=> 'iPhone',
            'qty'=> '1',
            'price'=> '100',
            'discount'=> '0'
          ),
          1 => array
          (
            'stock_id'=> '2802',
            'description'=> 'Samsung',
            'qty'=> '1',
            'price'=> '100',
            'discount'=> '0'
          ),
        ),

      );

$data = http_build_query($data);

$output = fa_bridge($method, $action, $record, $filter, $data);

?>
www.boxygen.pk

Re: Creating Invoice through REST API

You probably missed the json_decode line in fabridge.php file at near the end:

$content = ($code == "200") ? json_decode($content, true) : false;

10 (edited by boxygen 11/12/2019 02:12:06 am)

Re: Creating Invoice through REST API

No I have that code and it works fine for GET commands.

I didn't had any problem in reading the $content returned from the curl request, at all.

I had problem in sending the POST request using curl.

That is addressed by http_build_query($data); line.

In Wiki I suggest change the code as below for facurlrest.php

Contents of facurlrest.php file:

<?php
// FrontAccounting Bridge REST Test Script
// Author: Ap.Muthu
// Website: www.apmuthu.com
// Release Date: 2012-11-28

include_once "fabridge.php";

$method = isset($_GET['m']) ? $_GET['m'] : 'g'; // g, p, t, d => GET, POST, PUT, DELETE
$action = isset($_GET['a']) ? $_GET['a'] : '';
$record = isset($_GET['r']) ? $_GET['r'] : '';
$filter = isset($_GET['f']) ? $_GET['f'] : false;

// Sample Data for POST
$data = array(
'firstName'=> 'John',
'lastName'=> 'Doe'
);

$data = http_build_query($data);

$output = fa_bridge($method, $action, $record, $filter, $data);
echo print_r($output, true);

?>
www.boxygen.pk

Re: Creating Invoice through REST API

Done. Thanks.

12 (edited by anoopmb 11/12/2019 02:31:51 pm)

Re: Creating Invoice through REST API

Hello guys ,

I have updated api to Slim 3

We can easily implement Eloquent ORM too (Laravel One)

index.php looks like this

if (!isset($path_to_root))
{
    $path_to_root = "../../..";
}

ini_set('html_errors', false);
ini_set('xdebug.show_exception_trace', 0);
ini_set('xdebug.auto_trace', 2);
error_reporting(E_ALL);
ini_set("display_errors", 1);

include_once('config_api.php');

global $security_areas, $security_groups, $security_headings, $path_to_root, $db, $db_connections;

$page_security = 'SA_API';




include_once(FA_ROOT . "/sales/includes/cart_class.inc");
include_once(FA_ROOT . "/includes/session.inc");

include_once(FA_ROOT . "/includes/ui.inc");
include_once(FA_ROOT . "/sales/includes/sales_ui.inc");
include_once(FA_ROOT . "/sales/includes/ui/sales_order_ui.inc");
include_once(FA_ROOT . "/sales/includes/db/sales_order_db.inc");
include_once(FA_ROOT . "/sales/includes/db/sales_invoice_db.inc");
include_once(FA_ROOT . "/sales/includes/db/sales_delivery_db.inc");

include_once (API_ROOT . "/vendor/autoload.php");

$app = new \Slim\App([
    'debug' => false,
    'settings' => [
        'displayErrorDetails' => true
    ]
]);

require __DIR__ . "/app/Helpers/dependencies.php";
require __DIR__ . "/app/Helpers/handlers.php";
require __DIR__ . "/app/Helpers/middleware.php";


require __DIR__ . '/app/Sales/routes.php';



$app->run();
ANOOP
Experience is the name everyone gives to their mistakes!

Re: Creating Invoice through REST API

It is composer based. How can I install composer for this API?

www.boxygen.pk

Re: Creating Invoice through REST API

get it from here

https://getcomposer.org/download/

install into your server or to local(then copy all to server)  then edit your composer.json to update dependency versions

then run composer install in command line

ANOOP
Experience is the name everyone gives to their mistakes!

Re: Creating Invoice through REST API

If you manually include the necessary files, you will not need composer. Also the Slim3 version of index.php will need to have the array syntax reverted for use in PHP < 5.4