<?php
/*=======================================================\
|                 Point of Sales Module                  |
|--------------------------------------------------------|
|   Creator: Phương                                      |
|   Date :   18-Dec-2017                                 |
|   Description: ERP Point of Sales Extension            |
|   Website: http://notrinos.com                         |
|                                                        |
\=======================================================*/
$page_security = 'SA_SALESMANREP';
$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 . "/sales/includes/sales_db.inc");
include_once($path_to_root . "/inventory/includes/db/items_category_db.inc");

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

print_salesman_list();

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

function GetSalesmanTrans($person_from, $person_to, $customer, $from, $to)
{
	$fromdate = date2sql($from);
	$todate = date2sql($to);

	$sql = "SELECT DISTINCT trans.*,
			ov_amount+ov_discount AS InvoiceTotal,
			cust.name AS DebtorName,
			cust.curr_code,
			salesman.*,
			strans.*,
			stock.description AS ItemName
		FROM ".TB_PREF."debtor_trans trans,
			 ".TB_PREF."debtors_master cust,
			 ".TB_PREF."pos_salesman_trans strans,
			 ".TB_PREF."stock_master stock,
			".TB_PREF."pos_salesman salesman
		WHERE strans.debtor_trans_no=trans.trans_no
		    AND trans.debtor_no=cust.debtor_no 
		    AND salesman.id = strans.salesman_id 
		    AND strans.stock_id = stock.stock_id 
		    AND (trans.type=".ST_SALESINVOICE." OR trans.type=".ST_CUSTCREDIT.")
		    AND trans.tran_date BETWEEN '$fromdate' AND '$todate' ";
		if(!empty($person_from))
			$sql .= " AND salesman.id >= ".db_escape($person_from);
		if(!empty($person_to))
			$sql .= " AND salesman.id <= ".db_escape($person_to);
		if(!empty($customer))
			$sql .= " AND cust.debtor_no = ".db_escape($customer);
		
		$sql .= " ORDER BY salesman.id, trans.tran_date";

	return db_query($sql, "Error getting order details");
}

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

function print_salesman_list() {
	global $path_to_root;

	$person_from = $_POST['PARAM_0'];
	$person_to = $_POST['PARAM_1'];
	$customer = $_POST['PARAM_2'];
	$from = $_POST['PARAM_3'];
	$to = $_POST['PARAM_4'];
	$comments = $_POST['PARAM_5'];
	$orientation = $_POST['PARAM_6'];

	include_once($path_to_root . "/reporting/includes/pdf_report.inc");

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

	$cus = empty($customer) ? _("No Customer Filter") : get_customer_name($customer);

	$dec = user_price_dec();

	$cols = array(0, 40, 100, 210, 340,	360, 410, 460, 515);

	$headers = array(_('Inv #'), _('Inv Date'), _('Customer'), _('Item'), _('Qty'), _('Price'), _('Commission'), _('Total'));

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

	$headers2 = array(_('Salesman'), " ", ' ', _('Phone'), _('Email'));

    $params =   array( 	0 => $comments,
	    				1 => array(  'text' => _('Period'), 'from' => $from, 'to' => $to),
	    				2 => array(  'text' => _('Customer'),'from' => $cus,'to' => ''));

	$aligns2 = $aligns;

	$rep = new FrontReport(_('POS Salesman Listing'), "SalesmanListing", user_pagesize(), 9, $orientation);
    if ($orientation == 'L')
    	recalculate_cols($cols);
	$cols2 = $cols;
	$rep->Font();
	$rep->Info($params, $cols, $headers, $aligns, $cols2, $headers2, $aligns2);

	$rep->NewPage();

	$salesman = 0;
	$subtotal = 0;
	$total = 0;
	$sub_commission = 0;
	$total_commission = 0;

	$result = GetSalesmanTrans($person_from, $person_to, $customer, $from, $to);

	while ($myrow=db_fetch($result)) {

		$rep->NewLine(0, 2, false, $salesman);

		if ($salesman != $myrow['id']){
			if ($salesman != 0){
				$rep->Line($rep->row - 8);
				$rep->NewLine(2);
				$rep->TextCol(0, 3, _('Total'));
				$rep->AmountCol(6, 7, $sub_commission, $dec);
				$rep->AmountCol(7, 8, $subtotal, $dec);
    			$rep->Line($rep->row  - 4);
    			$rep->NewLine(2);
			}
			$rep->Font('bold');
			$rep->TextCol(0, 3,	$myrow['id']." ".$myrow['name']);
			$rep->TextCol(3, 4,	$myrow['phone']);
			$rep->TextCol(4, 6,	$myrow['email']);
			$rep->NewLine(2);
			$salesman = $myrow['id'];
			$total += $subtotal;
			$subtotal = 0;
			$sub_commission = 0;
		}
		$amt = $myrow['quantity'] * $myrow['unit_price'];
		$comm = empty($myrow['commission']) ? '' : $myrow['commission'].'%';

		$rep->Font();

		$rep->TextCol(0, 1,	$myrow['trans_no']);
		$rep->DateCol(1, 2,	$myrow['tran_date'], true);
		$rep->TextCol(2, 3,	$myrow['DebtorName']);
		$rep->TextCol(3, 4,	$myrow['ItemName']);
		$rep->TextCol(4, 5,	$myrow['quantity']);
		$rep->TextCol(5, 6,	$myrow['unit_price']);
		$rep->TextCol(6, 7, $comm);
		$rep->AmountCol(7, 8, $amt, $dec);

		$rep->NewLine();

		$subtotal += $amt;
		$sub_commission += ($amt * $myrow['commission']) / 100;
		$total_commission += $sub_commission;
	}
	if ($salesman != 0) {
		$rep->Line($rep->row - 4);
		$rep->NewLine(2);
		$rep->TextCol(0, 3, _('Total'));
		$rep->AmountCol(6, 7, $sub_commission, $dec);
		$rep->AmountCol(7, 8, $subtotal, $dec);
		$rep->Line($rep->row  - 4);
		$rep->NewLine(2);
		$total += $subtotal;
	}
	$rep->fontSize += 2;
	$rep->TextCol(0, 3, _('Grand Total'));
	$rep->fontSize -= 2;
	$rep->AmountCol(6, 7, $total_commission, $dec);
	$rep->AmountCol(7, 8, $total, $dec);
	$rep->Line($rep->row  - 4);
	$rep->NewLine();
	$rep->End();
}

