<?php
/**********************************************************************
	Copyright (C) NotrinosERP.
	Released under the terms of the GNU General Public License, GPL,
	as published by the Free Software Foundation, either version 3
	of the License, or (at your option) 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 License here <http://www.gnu.org/licenses/gpl-3.0.html>.
***********************************************************************/

/**
 * Report 356 — Inventory Turn Rate
 *
 * Inventory turnover ratio by item with COGS and average inventory.
 *
 * Parameters:
 *   PARAM_0: Start Date
 *   PARAM_1: End Date
 *   PARAM_2: Location
 *   PARAM_3: Item Category
 *   PARAM_4: Comments
 *   PARAM_5: Orientation
 *   PARAM_6: Destination (0=PDF, 1=Excel)
 */
$page_security = 'SA_ITEMSVALREP';
$path_to_root = '..';

include_once($path_to_root . '/includes/session.inc');
include_once($path_to_root . '/includes/date_functions.inc');
include_once($path_to_root . '/includes/data_checks.inc');
include_once($path_to_root . '/inventory/warehouse/includes/db/warehouse_reports_db.inc');

//----------------------------------------------------------------------------------------------------

print_turn_rate_report();

function print_turn_rate_report()
{
	global $path_to_root;

	$from_date = $_POST['PARAM_0'];
	$to_date   = $_POST['PARAM_1'];
	$location  = $_POST['PARAM_2'];
	$category  = $_POST['PARAM_3'];
	$comments  = $_POST['PARAM_4'];
	$orientation = $_POST['PARAM_5'];
	$destination = $_POST['PARAM_6'];

	if ($destination)
		include_once($path_to_root . '/reporting/includes/excel_report.inc');
	else
		include_once($path_to_root . '/reporting/includes/pdf_report.inc');

	$orientation = ($orientation ? 'L' : 'P');
	$dec = user_price_dec();

	if ($category == ALL_NUMERIC) $category = 0;
	if ($location == ALL_TEXT) $location = null;

	$loc_name = $location ? get_location_name($location) : _('All');
	$cat_name = $category ? get_category_name($category) : _('All');

	$cols = array(0, 80, 200, 280, 360, 430, 515);
	$headers = array(_('Item'), _('Description'), _('COGS'), _('Avg Inventory'), _('Turn Ratio'), _('Days Supply'));
	$aligns = array('left', 'left', 'right', 'right', 'right', 'right');

	$params = array(
		0 => $comments,
		1 => array('text' => _('Period'), 'from' => $from_date, 'to' => $to_date),
		2 => array('text' => _('Location'), 'from' => $loc_name, 'to' => ''),
		3 => array('text' => _('Category'), 'from' => $cat_name, 'to' => ''),
	);

	$rep = new FrontReport(_('Inventory Turn Rate'), 'TurnRate', user_pagesize(), 9, $orientation);
	if ($orientation == 'L')
		recalculate_cols($cols);
	$rep->Font();
	$rep->Info($params, $cols, $headers, $aligns);
	$rep->NewPage();

	$result = get_inventory_turn_rate_data($from_date, $to_date, $location, $category);

	$total_cogs = 0;
	$total_inventory = 0;

	while ($row = db_fetch($result)) {
		$rep->TextCol(0, 1, $row['stock_id']);
		$rep->TextCol(1, 2, $row['item_description']);
		$rep->AmountCol(2, 3, $row['cogs_value'], $dec);
		$rep->AmountCol(3, 4, $row['avg_inventory_value'], $dec);
		$rep->AmountCol(4, 5, $row['turnover_ratio'], 2);
		$rep->AmountCol(5, 6, $row['days_of_supply'], 0);
		$rep->NewLine();

		$total_cogs += $row['cogs_value'];
		$total_inventory += $row['avg_inventory_value'];
	}

	$rep->Line($rep->row - 2);
	$rep->NewLine();
	$rep->Font('bold');
	$rep->TextCol(0, 2, _('Totals'));
	$rep->AmountCol(2, 3, $total_cogs, $dec);
	$rep->AmountCol(3, 4, $total_inventory, $dec);
	$overall_ratio = $total_inventory > 0 ? round($total_cogs / $total_inventory, 2) : 0;
	$rep->AmountCol(4, 5, $overall_ratio, 2);
	$rep->Font();

	$rep->End();
}
