Re: REST API

cambell wrote:
s4m3shms wrote:

I was testing the sales APIs from your latest repository, none of the methods worked for me.

For example:

http://url/modules/api/sales/
or
http://url/modules/api/sales/5

returns nothing.

@s4m3shms The endpoint for the sales url needs to have a transaction type on the end, and its not optional.  So:

http://url/modules/api/sales/10

Have a look at the Sales_Test file to see an example of how to use the api using php.

https://github.com/cambell-prince/FrontAccountingSimpleAPI/blob/master-upstream/tests/Sales_Test.php


would give you all the sales invoices for example.  To get a particular invoice it would be:

http://url/modules/api/sales/5/10

assuming there was an invoice number 5.

Have a look at the Sales_Test.php file to see an example of using the api in php

https://github.com/cambell-prince/FrontAccountingSimpleAPI/blob/master-upstream/tests/Sales_Test.php

Thanks Cambell
In fact I was not aware of the API format, its working now.
I am trying to run your Sales_Test.php page, its not going through due to PHPUnit dependency issue.
Am trying to sort it out and update here.

Thanks again

Re: REST API

Hello Cambell
The error is

Fatal error: Class 'PHPUnit_Framework_TestCase' not found in /var/www/fa/fa243/frontaccounting/modules/apitest/tests/Sales_Test.php on line 13

I  have composer.json in the "tests" folder as below and ran composer update

  "require-dev": {
        "phpunit/phpunit": "3.7.*"
     
    }

Is this the correct path, or any specific version should I specify?

Regards

Re: REST API

Dear Cambell

Can you please look at this screenshot?

https://ibb.co/cmmDJ7

I have followed the JSON structure from Sales_Test.php to POST a sale,  but I couldn't add.


{
"trans_type":"10",
"ref":"005/2016",
"comments":"REST POST",
"order_date":"12/31/2016",
"delivery_date":"12/31/2016",
"cust_ref":"TPS",
"deliver_to":"QQ-1",
"delivery_address":"QQ-1",
"phone":"36390908",
"ship_via":"1",
"location":"DEF",
"freight_cost":"0",
"customer_id":"1",
"branch_id":"1",
"sales_type":"1",
"dimension_id":"0",
"dimension2_id":"0",
"items":[{"stock_id":"102","qty":"2","price":"250","discount":"0","description":"iPhone 6 64GB"}]

Message as I got as

{
    "success": 0,
    "msg": "Could Not Save to Database"
}

Regards

Post's attachments

api.jpg 48.3 kb, file has never been downloaded. 

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

Re: REST API

Hello Cambell

Finally I got it working

Sorry to say it was because of duplicate "ref"  element in the entry.

Another question is : when I GET the following  I expected to get all the sales transactions I have done including the sales I have entered through API.

http://URL/modules/api/sales/10/

But I always get two transactions only:
[{"trans_no":"1","type":"10",.................................

{"trans_no":"2","type":"10",..................................
]

Any hints on this?

Regards
Sume

Re: REST API

Hello @apmuthu

Does your version have the same result on returning "sales"?

Regards
Sume

Re: REST API

Look at overwritten Lines 109,110 in api24/sales.inc:

$info[$sale_index]['ov_discount'] = $trans['ov_discount'];
$info[$sale_index]['ov_discount'] = $trans['Total'];

The above has been corrected and committed in my repo.

Also the VARLIB commit in the core has not been updated as yet in this module but committed in my repo.

The URLs for sales (TRANS_TYPE = 10 for Sales Invoices) are like:

http://www.example.com/frontac24/modules/api24/sales/<TRANS_TYPE>
http:// www.example.com/frontac24/modules/api24/sales/<TRANS_NO>/<TRANS_TYPE>
http:// www.example.com/frontac24/modules/api24/sales/<TRANS_NO>/<TRANS_TYPE>?page=<PAGE_NO>

Note the order of the attributes in the last 2 URLs above.
The api24/config_api.inc has the constant RESULTS_PER_PAGE defined in it.

Both work okay in my version. For Transaction Number 2 in Transaction Type Sales Invoice (10) the output is:

{
 "ref":"002\/2017"
,"comments":""
,"order_date":"05\/07\/2017"
,"payment":"4"
,"payment_terms":{
    "0":"4"
   ,"terms_indicator":"4"
   ,"1":"Cash Only"
   ,"terms":"Cash Only"
   ,"2":"0"
   ,"days_before_due":"0"
   ,"3":"0"
   ,"day_in_following_month":"0"
   ,"4":"0"
   ,"inactive":"0"
   ,"5":"1"
   ,"cash_sale":"1"
 }
,"due_date":"05\/07\/2017"
,"phone":""
,"cust_ref":""
,"delivery_address":"N\/A"
,"ship_via":"1"
,"deliver_to":"Donald Easter LLC"
,"delivery_date":"05\/07\/2017"
,"location":"DEF"
,"freight_cost":"0"
,"email":""
,"customer_id":"1"
,"branch_id":"1"
,"sales_type":"1"
,"dimension_id":"0"
,"dimension2_id":"0"
,"line_items":
 [
  {
     "id":"6"
    ,"stock_id":"101"
    ,"qty":1
    ,"units":"each"
    ,"price":"300"
    ,"discount":"0"
    ,"description":"iPad Air 2 16GB"
  }
 ]
,"sub_total":300
,"display_total":300
}

Re: REST API

Hello @apmuthu

Excellent reply, it is clear now.

Thanks very much.

Regards
Sume

58 (edited by s4m3shms 02/19/2018 08:23:53 am)

Re: REST API

Hello Apmuthu

I have retrieved one inventory item using API

{"stock_id":"101","category_id":"1","tax_type_id":"1","description":"iPad Air 2 16GB","long_description":"","units":"each","mb_flag":"B","sales_account":"4010","cogs_account":"5010","inventory_account":"1510","adjustment_account":"5040","wip_account":"1530","purchase_cost":"0","last_cost":null,"material_cost":"200","labour_cost":"0","overhead_cost":"0","inactive":"0","no_sale":"0","no_purchase":"0"}

I need to calculate the "Price after Tax " same as in the  Direct invoice( that is 300 for stock_id 101) in my custom program for POSTing the sales using API. How can I calculate the "Price after Tax" value?

I need to POST something like this

{"stock_id":"101","qty":1,"price":"300","discount":"0","description":"iPad Air 2 16GB"}

Help much appreciated

Regards
Sume

Re: REST API

You cannot get the price and price after tax from the inventory item call.
You need to make a tax call and some other calls - check out the various API constructs as to what will get you what you want. If there is a value in some table's field that is not there in the standard API calls, let us know.

Re: REST API

apmuthu wrote:

You cannot get the price and price after tax from the inventory item call.
You need to make a tax call and some other calls - check out the various API constructs as to what will get you what you want. If there is a value in some table's field that is not there in the standard API calls, let us know.

I have found some API which returns unit cost ,tax types and tax  groups with the below results

{"stock_id":"102","unit_cost":"150"}
[{"id":"1","name":"Regular","exempt":"0"}]
[{"id":"1","name":"Tax","inactive":"0"},{"id":"2","name":"Tax Exempt","inactive":"0"}]

I could not relate these to the sales price.
Also in FA sales pricing, I see the manual entry for sales price!! Is sales price auto calculated from unit_price with taxes??

Sorry for the basic questions. I am trying to do a POS demo for the FA and stuck with this.

Thanks

Re: REST API

Use the normal Web UI for FA and navigate to the screen that provides for the sales price and taxes you want. Study the code behind it and see what functions provide them and use those functions by extending the API to do what you need. Also study the ERDs in the Wiki at:
http://frontaccounting.com/fawiki/index.php?n=Devel.ERDiagram23
http://frontaccounting.com/fawiki/index.php?n=Devel.ERDiagram24

Re: REST API

Thanks @apmuthu. I will go through the code and try my best.

Regards
Sume

63 (edited by cambell 06/22/2018 08:45:41 am)

Re: REST API

Hi,

An update to the REST API is available here v2.4-1.6:

https://github.com/andresamayadiaz/FrontAccountingSimpleAPI/releases

Changes since v2.4-1.5

22 June 2018

Add full CRUD for Journal entry, update, and delete (void).

21 June 2018

- Add full CRUD for Bank Accounts under bankaccounts endpoint.
- Fix formatting of code under src/ and test/ to conform to PSR-2

20 June 2018

- Add full CRUD for GLAccounts under glaccounts endpoint.
- Add CRUD for Dimensions under dimensions endpoint.
- Add static api generator and sample documentation.

19 June 2018

- Core VARLIB_PATH and VARLOG_PATH inclusions added
- TaxType: Added getById (Apmuthu 24 Nov 2017).
- Sales: Fix #32 Sales transactions Total not returned.
- Stock Adjust: Added unit test.
- Stock Adjust: Return now encoded as json msg.
- Stock Adjust: Missing argument $info fixed (Apmuthu 23 Apr 2018).
- Stock Adjust: add_stock_adjustment parameters fixed for FA 2.4 changes in API (Apmuthu, justapeddler 19 Apr 2018).
- Translated Spanish comments to English (Apmuthu 18 Nov 2017).

Cambell https://github.com/cambell-prince

Re: REST API

Hi,
In purchase API module, post API inserts cart with trans_type=sales_order and inserts into sales_order table.
I dont understand why it does not create cart with trans_type=purchase_order to insert into purch_orders table.
Is it a mistake?

Would u please help me.

Re: REST API

In the existing version, the Purchases.php is not included and it is a templated copy of Sales.php awaiting editing and still includes sales.inc instead of a new purchases.inc which needs to be hammered out.

Try the experimental Purchases in the API GitMaster as it is now updated with the upstream 2.4-1.6 without need for composer.

Re: REST API

I want to Add New Customer and then Record its Sales Order using API from an external website. On my installation multiple companies are running but this feature is needed only for one Company. How can I do this?

www.boxygen.pk

Re: REST API

1. Create a separate table / array list to match credentials for an SA_API permitted user with some additional API code linked to a specific company and rights associated with allowed API routes.
2. When that user logs in with the API code, it is matched to the appropriate company and authorisations allowed.
3. All interactions using the API done by this user will then pertain to only that company.

This also be made use of by app designers with other means like htaccess and htpasswd to choose the company and functionality for specific linux users.

Re: REST API

@boxygen You would need to install this feature (module) that would work for all companies.  I'm assuming that you want to force even valid users to be denied on certain companies.  Currently this would require you to modify util.php in the api_login() function.

Suppose that you only want to allow company number 3, then you would change util.php (Line 25 - 27) from something like this:

        if (!$succeed) {
            $app->halt(403, 'Bad Login For Company: ' . $company . ' With User: ' . $user);
        }

to something like this:

        if (!$succeed || $company != '3') {
            $app->halt(403, 'Bad Login For Company: ' . $company . ' With User: ' . $user);
        }

It would be a nice future feature to add proper access control that can be configured from Setup  | User Access Roles.

Cambell https://github.com/cambell-prince

Re: REST API

This wiki page is probably still relevant and stands updated.
FA Bridge is what you need to execute from a remote server.

Post's attachments

SimpleAPI module v2.4-1.6.pdf 289.4 kb, 4 downloads since 2018-06-25 

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

Re: REST API

hi
i think there is problem in ADD-Sales API when returning last ref-no in _parse_next method in references.inc.
It can not return the last reference. (I think there is a problem when joining journal and voided tables because it does not return any data row because the type field in journal table is always 0)

Can you please help me how to resolve the problem.

Re: REST API

Are you using the new method of pattern based document numbering or is it based on the old v2.3 method?

File: admin/db/transactions_db.inc line 183+:

function get_systype_db_info($type)
{
    switch ($type)
    {
        case     ST_JOURNAL      : return array(TB_PREF."journal", "type", "trans_no", "reference", "tran_date");
        case     ST_BANKPAYMENT  : return array(TB_PREF."bank_trans", "type", "trans_no", "ref", "trans_date");
..
..

File: includes/references.inc Line 183:

$db_info = get_systype_db_info($type);
## For a Journal entry, it will be:
## $_db_info = array(TB_PREF."journal", "type", "trans_no", "reference", "tran_date");

Line 170+ in the same file:

        $sql = "SELECT MAX(CAST(SUBSTR($ref_fld, ".(strlen($prefix)+1).",LENGTH($ref_fld)-".(strlen($postfix)+strlen($prefix)).") AS UNSIGNED))"
                ." FROM `$trans_table` tbl
                    LEFT JOIN ".TB_PREF."voided v ON tbl.`$tno_fld`=v.id AND v.type=$type"
                ." WHERE ISNULL(v.id)"
                .($type_fld ? " AND tbl.`$type_fld`=$type" : '')
                ." AND `$ref_fld` REGEXP ".db_escape('^'.preg_quote($prefix).'[0-9]*'.preg_quote($postfix).'$');

@joe: anything wrong here?

Re: REST API

Thanks for your reply.
I use v2.4.
But in File: includes/references.inc 170+
the code line $result = db_query($sql, 'cannot retrieve last reference'); does not return any data or error and the subsequence codes are not executed.

Re: REST API

Because there is no record with required trans_type in journal table.

Re: REST API

Try to display the sql after the sql. The where clause addresses the issue of absence of voided entry.

Re: REST API

would you please explain more.
I want to post json object like below to insert an invoice:
{
        "customer_id":2,
        "order_date":"06/27/2018",
        "ref":"001/2018",
        "trans_type":"ST_SALESINVOICE",
        "items":[
            {"stock_id":103, "qty":5, "price":5555, "discount":0,"description":"no desc"}
            ]
}
I want to know how _parse_next function handle the next reference if there is no record in journal table with trans_type=ST_SALESINVOICE
When I post this object dbquery in references.inc 170+ does not respond:
$sql = "SELECT MAX(CAST(SUBSTR($ref_fld, ".(strlen($prefix)+1).",LENGTH($ref_fld)-".(strlen($postfix)+strlen($prefix)).") AS UNSIGNED))"
                ." FROM `$trans_table` tbl
                    LEFT JOIN ".TB_PREF."voided v ON tbl.`$tno_fld`=v.id AND v.type=$type"
                ." WHERE ISNULL(v.id)"
                .($type_fld ? " AND tbl.`$type_fld`=$type" : '')
                ." AND `$ref_fld` REGEXP ".db_escape('^'.preg_quote($prefix).'[0-9]*'.preg_quote($postfix).'$');
        $result = db_query($sql, 'cannot retrieve last reference');