transfer_pre_process.php
<?php
// +-----------------------------------------------------------------+
// | PhreeBooks Open Source ERP |
// +-----------------------------------------------------------------+
// | Copyright (c) 2008 PhreeSoft, LLC |
// | http://www.PhreeSoft.com |
// +-----------------------------------------------------------------+
// | This program is free software: you can redistribute it and/or |
// | modify it under the terms of the GNU General Public License as |
// | published by the Free Software Foundation, either version 3 of |
// | the License, or any later version. |
// | |
// | This program is distributed in the hope that it will be useful, |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
// | GNU General Public License for more details. |
// | |
// | The license that is bundled with this package is located in the |
// | file: /doc/manual/ch01-Introduction/license.html. |
// | If not, see http://www.gnu.org/licenses/ |
// +-----------------------------------------------------------------+
// Path: /modules/inventory/pages/transfer/pre_process.php
//
$cog_item_types = array('si', 'sr', 'ms', 'mi', 'as'); // item types that are tracked in cost of goods sold
/************** Check user security *****************************/
$security_level = $_SESSION['admin_security'][SECURITY_ID_TRANSFER_INVENTORY];
if ($security_level == 0) { // no permission to enter page, error and redirect to home page
$messageStack->add_session(ERROR_NO_PERMISSION, 'error');
gen_redirect(html_href_link(FILENAME_DEFAULT, '', 'SSL'));
}
/************** include page specific files *********************/
require(DIR_FS_WORKING . 'language/' . $_SESSION['language'] . '/language.php');
require(DIR_FS_WORKING . 'functions/inventory.php');
/************** page specific initialization *************************/
$error = false;
$post_date = ($_POST['post_date']) ? gen_db_date_short($_POST['post_date']) : date('Y-m-d', time());
$action = (isset($_GET['action']) ? $_GET['action'] : $_POST['todo']);
/*************** hook for custom actions ***************************/
$custom_path = DIR_FS_MY_FILES . 'custom/inventory/transfer/extra_actions.php';
if (file_exists($custom_path)) { include($custom_path); }
/*************** Act on the action request *************************/
switch ($action) {
case 'save':
// security check
if ($security_level < 2) {
$messageStack->add_session(ERROR_NO_PERMISSION,'error');
gen_redirect(html_href_link(FILENAME_DEFAULT, gen_get_all_get_params(array('action')), 'SSL'));
break;
}
// retrieve and clean input values
$sku = db_prepare_input($_POST['sku_1']);
$qty = db_prepare_input($_POST['adj_qty']);
$source_store_id = $_POST['source_store_id'];
$dest_store_id = $_POST['dest_store_id'];
$result = $db->Execute("select inventory_type, cost_method from " . TABLE_INVENTORY . " where sku = '" . $sku . "'");
$cost_method = $result->fields['cost_method'];
$disp_order = ($cost_method <> 'l') ? 'post_date' : 'post_date DESC'; // else LIFO
// test for errors
// make sure this is a gl trackable (stock, assy, etc.) item
if (!in_array($result->fields['inventory_type'], $cog_item_types)) {
$messageStack->add(INV_XFER_ERROR_NO_COGS_REQD, 'error');
$error = true;
}
// quantity needs to be greater than zero
if ($qty < 0) {
$messageStack->add(INV_XFER_ERROR_QTY_ZERO, 'error');
$error = true;
}
// source and dest need to be different
if ($source_store_id == $dest_store_id) {
$messageStack->add(INV_XFER_ERROR_SAME_STORE_ID, 'error');
$error = true;
}
$sql = "select id, ref_id, remaining, sum(remaining) as total_remain, unit_cost, sum(unit_cost) as total_cost
from " . TABLE_INVENTORY_HISTORY . "
where sku = '" . $sku . "' and remaining > 0 and store_id = " . $source_store_id . "
group by id order by $disp_order";
$result = $db->Execute($sql);
if ($result->fields['total_remain'] < $qty) {
$messageStack->add(INV_XFER_ERROR_NOT_ENOUGH_SKU, 'error');
$error = true;
}
if (!$error) {
$working_qty = $qty;
while (!$result->EOF) { // loops until either qty is zero and/or inventory history is exhausted
if ($cost_method == 'a') { // Average cost
$cost = $result->fields['total_cost'] / $result->fields['total_remain'];
} else { // FIFO, LIFO
$cost = $result->fields['unit_cost']; // for the specific history record
}
$cost_qty = ($working_qty <= $result->fields['remaining']) ? $working_qty : $result->fields['remaining'];
$working_qty -= $result->fields['remaining'];
$sql = "update " . TABLE_INVENTORY_HISTORY . " set remaining = remaining - " . $cost_qty . " where id = " . $result->fields['id'];
$db->Execute($sql);
$sql_data_array = array(
'ref_id' => $result->fields['ref_id'],
'store_id' => $dest_store_id,
'sku' => $sku,
'qty' => $cost_qty,
'remaining' => $cost_qty,
'unit_cost' => $cost,
'post_date' => $post_date,
);
db_perform(TABLE_INVENTORY_HISTORY, $sql_data_array, 'insert');
if ($working_qty <= 0) break;
$result->MoveNext();
}
gen_add_audit_log(sprintf(INV_LOG_TRANSFER, $source_store_id, $dest_store_id), $sku, $qty);
$messageStack->add_session(sprintf(INV_XFER_SUCCESS, $qty),'success');
gen_redirect(html_href_link(FILENAME_DEFAULT, gen_get_all_get_params(array('action')), 'SSL'));
}
$cInfo = new objectInfo($_POST);
break;
case 'delete': // TBD not allowed
case 'edit': // TBD not allowed
default:
$cInfo = new objectInfo(array());
}
/***************** prepare to display templates *************************/
$include_header = true; // include header flag
$include_footer = true; // include footer flag
$include_calendar = true;
$include_template = 'template_main.php'; // include display template (required)
define('PAGE_TITLE', BOX_INV_TRANSFER);
?>