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.