<?php

/* * ********************************************************************
  Copyright (C) FrontAccounting, LLC.
  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>.
 * ********************************************************************* */
$page_security = 'SA_SALESANALYTIC';
// ----------------------------------------------------------------
// $ Revision:	2.0 $
// Creator:	Joe Hunt
// date_:	2005-05-19
// Title:	Inventory Sales Report
// ----------------------------------------------------------------
$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 . "/includes/banking.inc");
include_once($path_to_root . "/gl/includes/gl_db.inc");
include_once($path_to_root . "/inventory/includes/db/items_category_db.inc");

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

print_inventory_sales();

function getTransactions($from, $to, $trans_nos) {
    $from = date2sql($from);
    $to = date2sql($to);

    $sql = "SELECT item.category_id,
			category.description AS cat_description,
			item.stock_id,
			item.description, item.inactive,
			item.mb_flag,
			move.loc_code,
			trans.debtor_no,
			debtor.name AS debtor_name,
			move.tran_date,
                        SUM(item.weight*move.qty) AS weight,
			SUM(-move.qty) AS qty,
			SUM(-move.qty*move.price) AS amt,
			SUM(-IF(move.standard_cost <> 0, move.qty * move.standard_cost, move.qty *item.material_cost)) AS cost
		FROM " . TB_PREF . "stock_master item,
			" . TB_PREF . "stock_category category,
			" . TB_PREF . "debtor_trans trans,
			" . TB_PREF . "debtors_master debtor,
			" . TB_PREF . "stock_moves move
		WHERE item.stock_id=move.stock_id
		AND item.category_id=category.category_id
		AND trans.debtor_no=debtor.debtor_no
		AND move.type=trans.type
		AND move.trans_no=trans.trans_no
		AND move.tran_date>='$from'
		AND move.tran_date<='$to'
		AND (trans.type=" . ST_CUSTDELIVERY . ") 
                AND trans.trans_no in (" . $trans_nos . ")";

    $sql .= " AND item.mb_flag<>'F'";

    $sql .= " GROUP BY item.stock_id ORDER BY item.stock_id,
		item.category_id";
    //display_warning($sql);
    return db_query($sql, "No transactions were returned");
}

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

function print_inventory_sales() {
    global $path_to_root;

    $from = $_POST['PARAM_0'];
    $to = $_POST['PARAM_1'];
    $trans_nos = $_POST['PARAM_2'];
    $orientation = $_POST['PARAM_3'];
    $destination = $_POST['PARAM_4'];
    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();



    $cols = array(0, 90, 400, 450, 515);

    $headers = array(_('Category'), _('Description'), _('Weight'), _('QTY'));

    $aligns = array('left', 'left', 'rigt', 'right');

    $params = array(0=>"",
        1 => array('text' => _('Period'), 'from' => $from, 'to' => $to),
        2 => array('text' => _('Delivery Notes'), 'from' => $trans_nos, 'to' => ''));

    $rep = new FrontReport(_('Combined Delivery Notes'), "CombibeDeliveryNotes", user_pagesize(), 9, $orientation);
    if ($orientation == 'L')
        recalculate_cols($cols);

    $rep->Font();
    $rep->Info($params, $cols, $headers, $aligns);
    $rep->NewPage();

    $res = getTransactions($from, $to, $trans_nos);
    $total = $grandtotal = 0.0;
    $total1 = $grandtotal1 = 0.0;
    $catt = '';
    while ($trans = db_fetch($res)) {
        if ($catt != $trans['cat_description']) {
            if ($catt != '') {
                $rep->NewLine(2, 3);
                $rep->TextCol(0, 2, _('Total'));
                $rep->AmountCol(2, 3, $total, $dec);
                $rep->AmountCol(3, 4, $total1, $dec);
                $rep->Line($rep->row - 2);
                $rep->NewLine();
                $rep->NewLine();
                $total = $total1 = $total2 = 0.0;
            }
            $rep->TextCol(0, 1, $trans['category_id']);
            $rep->TextCol(1, 4, $trans['cat_description']);
            $catt = $trans['cat_description'];
            $rep->NewLine();
        }


        $rep->NewLine();
        $rep->fontSize -= 2;
        $rep->TextCol(0, 1, $trans['stock_id']);
        $rep->TextCol(1, 2, $trans['description'] . ($trans['inactive'] == 1 ? " (" . _("Inactive") . ")" : ""), -1);
        $rep->AmountCol(2, 3, is_numeric(abs($trans['weight'])) ? abs($trans['weight']) : 0, $dec);
        $rep->AmountCol(3, 4, $trans['qty'], get_qty_dec($trans['stock_id']));

        $rep->fontSize += 2;
        $total += is_numeric(abs($trans['weight'])) ? abs($trans['weight']) : 0;
        $total1 += $trans['qty'];
        $grandtotal += is_numeric(abs($trans['weight'])) ? abs($trans['weight']) : 0;
        $grandtotal1 += $trans['qty'];
    }
    $rep->NewLine(2, 3);
    $rep->TextCol(0, 2, _('Total'));
    $rep->AmountCol(2, 3, $total, $dec);
    $rep->AmountCol(3, 4, $total1, $dec);
    $rep->Line($rep->row - 2);
    $rep->NewLine();
    $rep->NewLine(2, 1);
    $rep->TextCol(0, 2, _('Grand Total'));
    $rep->AmountCol(2, 3, $grandtotal, $dec);
    $rep->AmountCol(3, 4, $grandtotal1, $dec);

    $rep->Line($rep->row - 4);
    $rep->NewLine();
    $rep->End();
}


function count_array($array) {
    return (is_array($array)) ? count($array) : (($array === NULL) ? 0 : 1);
}