1 (edited by justapeddler 04/17/2018 10:04:37 am)

Topic: refs table

I am getting headache trying to figure out the relationships for the `refs` table.

References code does not query this table at all. From sql_trail I am only able to see one REPLACE syntax that I'm still searching coming from which part of code.

references.inc says that refs table is to be deprecated.

I am trying to insert records (for inventory moves/adjustment) from other application and is it safe for me to simply ignore inserting records to the `refs` table?

Using 2.4RC1

Re: refs table

Are you using API to insert and directly accessing the database and insert from another application ?

Subscription service based on FA
HRM CRM POS batch Themes

3 (edited by justapeddler 04/17/2018 05:13:45 pm)

Re: refs table

I am directly accessing the database and inserting from another application as I could not find any documentation of API that supports inventory moves. The API that I found only supports inventory adjustments. It is not possible to use adjustments API to do stock moves as they use different types.

Re: refs table

Have a look at the API24 in the extensions repo.

5 (edited by justapeddler 04/18/2018 04:41:02 am)

Re: refs table

Thank you for pointing me out on new information, apmuthu.

After looking at the inventory API, it seems that it is modified so we can pass 'type' argument. I believe this is done using the /stock/ API.

I am getting more confused tho:
This line in inventory.inc API

    $trans_no = add_stock_adjustment($_SESSION['adj_items']->line_items, $info['location'], $info['date'], $info['type'], $info['increase'], $info['reference'], $info['memo']);

Have extra arguments on type (this is the one needed to differentiate on move/adjust) and increase (I don't know what is this used for).

While the method in /inventory/includes/db/items_adjust_db.inc is

add_stock_adjustment($items, $location, $date_, $reference, $memo_)

Shouldn't this return an error? I haven't tested it live yet. I tried to look at the items_adjust_db.inc in newest 2.4.4 release.

Re: refs table

Okay I figured out what went wrong.

The API is still using a method from frontaccounting 2.3.x

This is it from items_adjust_db.inc 2.3.26:

function add_stock_adjustment($items, $location, $date_, $type, $increase, $reference, $memo_)

And the one which decided the type is from inside add_stock_adjustment_item (items_adjust_db.inc):

    add_stock_move(ST_INVADJUST, $stock_id, $adj_id, $location,
        $date_, $reference, $quantity, $standard_cost);

It is ST_LOCTRANSFER / ST_INVADJUST that I am looking for. Stock moves need to be passed as ST_LOCTRANSFER.

The $type arguments in 2.3.26 is never resolved.

Do have any idea for a fix? Or should I make a new API that implements add_stock_transfer (from items_transfer_db.inc) maybe naming it /stockmove/ ?

Re: refs table

The API extension for FA 2.3.x is here.

The API24 version is attached herewith. Looks like you are using the one from the official / source repos that may not be updated.

The functions you alluded to have since been updated in the FA24extensions repo and attached here.

Re: refs table

I did access the link you provided for, apmuthu. It seems that the inventory.inc hasnt updated yet for 2.4 usage.

Take a look here: https://github.com/apmuthu/FA24extensions/blob/master/Extensions/api24/inventory.inc

Line 88:

    $trans_no = add_stock_adjustment($_SESSION['adj_items']->line_items, $info['location'], $info['date'], $info['type'], $info['increase'], $info['reference'], $info['memo']);

It is still using add_stock_adjustment method for 2.3

The attachment you provided is also the same.

Re: refs table

You are right. We need to remove the type and increase parameters. It is documented at the end of api_changes.txt.

Committed.

Verify if any more endpoints are needed to satisfy: ST_LOCTRANSFER / ST_INVADJUST / ST_LOCTRANSFER

Re: refs table

Does the field validation also needs to be removed?
Line 37-39 and 49-51

    if (! isset($info['type'])) {
        api_error(412, 'Movement Type is required');
    }
    if (! isset($info['increase'])) {
        api_error(412, 'Increase is required. (1 = true, 0 = false)');
    }

I don't quite understand how the api $req and $info parses the arguments.

Re: refs table

Also:

add_stock_transfer method is not implemented yet in the API.

Is is possible if I just duplicate and modify the inventory.inc file for it to accept another argument (location to be transferred to) so it can handle inventory transfers ?

Re: refs table

The $info variable is populated when the api24/src/Inventory.php file got included (see line 65) and the class Inventory was instantiated in api24/index.php in line 73. The latter file calls the function stock_adjustment_add() without any arguments in line 129 where the $info gets populated from $_POST of the Ajax Request. In all of these cases, we see that the $info['type'] and $info['increase'] do not appear in any validation and hence the validations in the api24/inventory.inc file for these elements can be removed as well.

Committed.

Does this satisfy your stock transfer?

Post's attachments

api24.zip 195.4 kb, 2 downloads since 2018-04-19 

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

Re: refs table

My api returned this error:

Slim Application Error

The application could not run because of the following error:
Details
Type: ErrorException
Code: 2
Message: Missing argument 1 for stock_adjustment_add(), called in C:\xampp\htdocs\fa\modules\api24\index.php on line 129 and defined
File: C:\xampp\htdocs\fa\modules\api24\inventory.inc
Line: 15
Trace

#0 C:\xampp\htdocs\fa\modules\api24\inventory.inc(15): Slim\Slim::handleErrors(2, 'Missing argumen...', 'C:\\xampp\\htdocs...', 15, Array)
#1 C:\xampp\htdocs\fa\modules\api24\index.php(129): stock_adjustment_add()
#2 [internal function]: {closure}()
#3 C:\xampp\htdocs\fa\modules\api24\Slim\Route.php(468): call_user_func_array(Object(Closure), Array)
#4 C:\xampp\htdocs\fa\modules\api24\Slim\Slim.php(1355): Slim\Route->dispatch()
#5 C:\xampp\htdocs\fa\modules\api24\Slim\Middleware\Flash.php(85): Slim\Slim->call()
#6 C:\xampp\htdocs\fa\modules\api24\Slim\Middleware\MethodOverride.php(92): Slim\Middleware\Flash->call()
#7 C:\xampp\htdocs\fa\modules\api24\index.php(59): Slim\Middleware\MethodOverride->call()
#8 C:\xampp\htdocs\fa\modules\api24\Slim\Middleware\ContentTypes.php(81): JsonToFormData->call()
#9 C:\xampp\htdocs\fa\modules\api24\Slim\Middleware\PrettyExceptions.php(67): Slim\Middleware\ContentTypes->call()
#10 C:\xampp\htdocs\fa\modules\api24\Slim\Slim.php(1300): Slim\Middleware\PrettyExceptions->call()
#11 C:\xampp\htdocs\fa\modules\api24\index.php(447): Slim\Slim->run()
#12 {main}

Do you have any idea what went wrong?

Re: refs table

Since stock transfer needs another argument, the prototypes too must be modified.

Re: refs table

I am still trying the functionality of the /stock/ or stock adjustment api to ensure that it has no problem. Apparently it has.

I think i also found the problem.

Inventory.inc line 15:

  
function stock_adjustment_add($info) 

index.php line 129:

stock_adjustment_add();

The inventory adjustment api code part in the index.php has different syntax from the rest (it does not use the FAAPI php path). I am currently clueless about this.

I dont know if omitting the $info argument from the function inside inventory.inc would work, I can only test it tomorrow.

Re: refs table

There is no need for the$info argument in inventory.inc as it gets assigned inside the function stock_adjustment_add() on line 19.

Committed.

Post's attachments

api24.zip 195.4 kb, file has never been downloaded. 

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

17 (edited by justapeddler 04/26/2018 01:12:36 pm)

Re: refs table

I finally get the stock transfer API to work.

For anyone interested,

create a new transfer.inc file at the main api directory:

<?php
/**********************************************
Author: Andres Amaya
Name: Inventory REST API
Free software under GNU GPL
***********************************************/
$path_to_root = "../..";
include_once ($path_to_root . "/inventory/includes/inventory_db.inc");
include_once ($path_to_root . "/inventory/includes/db/items_codes_db.inc");
include_once ($path_to_root . "/inventory/includes/db/items_locations_db.inc");
include_once ($path_to_root . "/inventory/includes/db/items_transfer_db.inc");
include_once ($path_to_root . "/gl/includes/gl_db.inc");
include_once ($path_to_root . "/includes/ui/items_cart.inc");
function stock_transfer_add()
{
    $app = \Slim\Slim::getInstance('SASYS');
    $req = $app->request();
    $info = $req->post();
    // Example
    // $ex = array('stock_id' => 'PLUMA', 'location_from' => 'DEF', 'location_to' => 'XXX', 'date' => today(), 'reference' =>
    // '123qwe', 'quantity' => 555, 'standard_cost' => 10, 'memo' => 'Proof of API 2');
    // echo base64_encode(json_encode($ex));
    // print_r($info);
    // Validate Required Fields
    if (! isset($info['stock_id'])) {
        api_error(412, 'Stock Id is required aieu');
    }
    if (! isset($info['location_from'])) {
        api_error(412, 'Location from is required');
    }
    if (! isset($info['location_to'])) {
        api_error(412, 'Location to is required');
    }
    if (! isset($info['date'])) {
        api_error(412, 'Date is required');
    }
    if (! isset($info['reference'])) {
        api_error(412, 'Reference is required');
    }
    if (! isset($info['quantity'])) {
        api_error(412, 'Quantity is required');
    }
    if (! isset($info['standard_cost'])) {
        $info['standard_cost'] = 0;
    }
    if (! isset($info['memo'])) {
        api_error(412, 'Memo is required');
    }
    // Create Adjustment Order
    unset($_SESSION['adj_items']);
    $_SESSION['adj_items'] = new items_cart(ST_LOCTRANSFER);
    $_SESSION['gl_items'] = new items_cart(0);
    $info['date'] = today();
    if (! is_date_in_fiscalyear($info['date']))
        $info['date'] = end_fiscalyear();
    $_SESSION['adj_items']->tran_date = $info['date'];
    $_SESSION['gl_items']->tran_date = $info['date'];
    // This should never happen
    if ($_SESSION['adj_items']->find_cart_item($info['stock_id']))
        api_error(500, 'Item Already Exists In Cart');
    else {
        $_SESSION['adj_items']->add_to_cart(count($_SESSION['adj_items']->line_items), $info['stock_id'], $info['quantity'], $info['standard_cost']);
        // $code_id, $dimension_id, $dimension2_id, $amount, $reference, $description=null
        // TODO Obtain accounts against which to make seats
        // Product Inventory
        $_SESSION['gl_items']->add_gl_item('1510', 0, 0, ($info['quantity'] * $info['standard_cost'] * - 1), $info['memo']);
        // Configured for Absolute, Own
        $_SESSION['gl_items']->add_gl_item('4010', 0, 0, ($info['quantity'] * $info['standard_cost']), $info['memo']);
        $_SESSION['gl_items']->order_id = 0;
        $_SESSION['gl_items']->reference = 666;
    }
    /*
     * print_r($_SESSION['adj_items']); echo "----------- GL -------"; print_r($_SESSION['gl_items']); echo "------
     * DEBIT: " . $_SESSION['gl_items']->gl_items_total_debit(); echo "------ CREDIT: " .
     * $_SESSION['gl_items']->gl_items_total_credit();
     */
    // Process Order
    $trans_no = add_stock_transfer($_SESSION['adj_items']->line_items, $info['location_from'], $info['location_to'], $info['date'], $info['reference'], $info['memo']);
    $gl_trans_no = write_journal_entries($_SESSION['gl_items'], false);
    new_doc_date($info['date']);
    $_SESSION['adj_items']->clear_items();
    unset($_SESSION['adj_items']);
    $_SESSION['gl_items']->clear_items();
    unset($_SESSION['gl_items']);
    api_create_response("Stock Transfer has been added");
}
?>

Add this to index.php just below line 131:

// ------------------------------- Stock Transfers -------------------------------
// Add Stock Adjustment
$rest->post('/stocktransfer/', function () use($rest)
{
    include_once (API_ROOT . "/transfer.inc");
    stock_transfer_add();
});
// ------------------------------- Stock Transfers -------------------------------

The API post path is /stocktransfer/

I tested it and so far it works as I needed it and no problems found so far.

Thank you apmuthu for all the assistance!

Re: refs table

@justapeddler: Thanks for your submissions and feedback. This is the real spirit of FA that keeps it ticking!
As stock transfers are part of inventory, it might make sense to extend inventory.inc, but keeping it in a separate transfer.inc  will make for independent development. Please go thru it thoroughly for different scenarios and then I can add it in to the FA24extensions repo.

Re: refs table

I bring good news. I managed to modify it so it can handle multiple items entry.
POST the stock_id, qty, as arrays ex:

stock_id[0]
stock_id[1]
stock_id[2]
quantity[0]
quantity[1]
quantity[2]

Here is the new transfer.inc

<?php
/**********************************************
Author: Andres Amaya
Name: Inventory REST API
Free software under GNU GPL
***********************************************/
$path_to_root = "../..";
include_once ($path_to_root . "/inventory/includes/inventory_db.inc");
include_once ($path_to_root . "/inventory/includes/db/items_codes_db.inc");
include_once ($path_to_root . "/inventory/includes/db/items_locations_db.inc");
include_once ($path_to_root . "/inventory/includes/db/items_transfer_db.inc");
include_once ($path_to_root . "/gl/includes/gl_db.inc");
include_once ($path_to_root . "/includes/ui/items_cart.inc");
function stock_transfer_add()
{
    $app = \Slim\Slim::getInstance('SASYS');
    $req = $app->request();
    $info = $req->post();
    // Example
    // $ex = array('stock_id' => 'PLUMA', 'location_from' => 'DEF', 'location_to' => 'XXX', 'date' => today(), 'reference' =>
    // '123qwe', 'quantity' => 555, 'standard_cost' => 10, 'memo' => 'Proof of API 2');
    // echo base64_encode(json_encode($ex));
    // print_r($info);
    // Validate Required Fields
    if (! isset($info['stock_id'])) {
        api_error(412, 'Stock Id is required aieu');
    }
    if (! isset($info['location_from'])) {
        api_error(412, 'Location from is required');
    }
    if (! isset($info['location_to'])) {
        api_error(412, 'Location to is required');
    }
    if (! isset($info['date'])) {
        api_error(412, 'Date is required');
    }
    if (! isset($info['reference'])) {
        api_error(412, 'Reference is required');
    }
    if (! isset($info['quantity'])) {
        api_error(412, 'Quantity is required');
    }
    if (! isset($info['standard_cost'])) {
        $info['standard_cost'] = 0;
    }
    if (! isset($info['memo'])) {
        api_error(412, 'Memo is required');
    }
    // Create Adjustment Order
    unset($_SESSION['transfer_items']);
    $_SESSION['transfer_items'] = new items_cart(ST_LOCTRANSFER);
    $_SESSION['gl_items'] = new items_cart(0);
    $info['date'] = today();
    if (! is_date_in_fiscalyear($info['date']))
        $info['date'] = end_fiscalyear();
    $_SESSION['transfer_items']->tran_date = $info['date'];
    $_SESSION['gl_items']->tran_date = $info['date'];
    // This should never happen
    if ($_SESSION['transfer_items']->find_cart_item($info['stock_id']))
        api_error(500, 'Item Already Exists In Cart');
    else {
        for($i = 0; $i < count($info['stock_id']); $i++) {
        $_SESSION['transfer_items']->add_to_cart($i, $info['stock_id'][$i], $info['quantity'][$i], 0);
        // $code_id, $dimension_id, $dimension2_id, $amount, $reference, $description=null
        // TODO Obtain accounts against which to make seats
        // Product Inventory
        $_SESSION['gl_items']->add_gl_item('1510', 0, 0, ($info['quantity'][$i] * $info['standard_cost'][$i] * - 1), $info['memo']);
        // Configured for Absolute, Own
        $_SESSION['gl_items']->add_gl_item('4010', 0, 0, ($info['quantity'][$i] * $info['standard_cost'][$i]), $info['memo']);
        $_SESSION['gl_items']->order_id = 0;
        $_SESSION['gl_items']->reference = 666;
        }
    }
    /*
     * print_r($_SESSION['transfer_items']); echo "----------- GL -------"; print_r($_SESSION['gl_items']); echo "------
     * DEBIT: " . $_SESSION['gl_items']->gl_items_total_debit(); echo "------ CREDIT: " .
     * $_SESSION['gl_items']->gl_items_total_credit();
     */
    // Process Order
    $trans_no = add_stock_transfer($_SESSION['transfer_items']->line_items, $info['location_from'], $info['location_to'], $info['date'], $info['reference'], $info['memo']);
    $gl_trans_no = write_journal_entries($_SESSION['gl_items'], false);
    new_doc_date($info['date']);
    $_SESSION['transfer_items']->clear_items();
    unset($_SESSION['transfer_items']);
    $_SESSION['gl_items']->clear_items();
    unset($_SESSION['gl_items']);
    api_create_response("Stock Transfer has been added");
}
?>

I didn't merge the code with inventory.inc out of fear messing the original entry.

Here is where some changes is made:

if ($_SESSION['transfer_items']->find_cart_item($info['stock_id']))
        api_error(500, 'Item Already Exists In Cart');
    else {
        for($i = 0; $i < count($info['stock_id']); $i++) {
        $_SESSION['transfer_items']->add_to_cart($i, $info['stock_id'][$i], $info['quantity'][$i], 0);
        // $code_id, $dimension_id, $dimension2_id, $amount, $reference, $description=null
        // TODO Obtain accounts against which to make seats
        // Product Inventory
        $_SESSION['gl_items']->add_gl_item('1510', 0, 0, ($info['quantity'][$i] * $info['standard_cost'][$i] * - 1), $info['memo']);
        // Configured for Absolute, Own
        $_SESSION['gl_items']->add_gl_item('4010', 0, 0, ($info['quantity'][$i] * $info['standard_cost'][$i]), $info['memo']);
        $_SESSION['gl_items']->order_id = 0;
        $_SESSION['gl_items']->reference = 666;
        }
    }

Now, I MacGyvered (figured out) most of my way into the code to make it work. I have zero clue in some areas of the code, example is the 'gl_items' entries. I have zero idea what it does in the system and my code may mess something up. I hope someone knowledgeable on it could help me on this.

Re: refs table

You might also want to modify the "//Example" in transfer.inc to match your multiple {stock_id, quantity} line entries. In which file did you make the changes listed in your second code snippet above?

21 (edited by justapeddler 04/27/2018 04:17:07 pm)

Re: refs table

The second snippet is from the above transfer.inc , line 59.

I think I broke functionality for single addition, I will try to fix it and update the example later on.