<?php
if (!defined('sugarEntry') || !sugarEntry)
	die('Not A Valid Entry Point');

global $current_user;
if ($current_user->is_admin || $current_user->id == '49584975-a4a4-6c28-3b31-54bcb71a8f2b' ||
		$current_user->id == 'cc468949-70d1-18d4-36e7-52e785735f91' ||
		$current_user->id == 'e3a315cd-5752-4e83-aa73-5236e2a2d39c' ||
		$current_user->id=='54eb00ac-d71a-1749-1111-524ad6b2886a') {

		} else {
			ACLController::displayNoAccess(true);
			sugar_die();
		}

/**
 * ********************* PREPARE ********************
 */
global $app_list_strings;
global $mod_strings;
$db = $GLOBALS ['db'];
$date_from = date("Y-01-01");
$date_to = date("Y-m-01");
$return = array();
$suma['netto'] = 0;
$suma['brutto'] = 0;
$query = "";

// Pobieranie danych z formularza
if (!$_GET['date_from']) {
	$date_from = date("Y-01-01");
} else {
	$date_from = $_GET['date_from'];
	$tmp = date_parse_from_format("d.m.Y", $_GET['date_from']);
	$date_from = date($tmp['year'] . "-" . $tmp['month'] . "-01");
}
if (!$_GET['date_to']) {
	$date_to = date("Y-m-d");
} else {
	$date_to = $_GET['date_to'];
}
//formatowanie daty
$Calendar_daFormat = str_replace("d", "%d", str_replace("m", "%m", str_replace("Y", "%Y", $GLOBALS['timedate']->get_date_format())));
$date_from = $GLOBALS['timedate']->to_display_date($date_from);
$date_to = $GLOBALS['timedate']->to_display_date($date_to);

/**
 * ************* GET DATA FROM DB********************
 */
$wcquery = "
 SELECT 
	wca.ecmproduct ecmproductid,
	wca.quantity quantity,
	ROUND(wca.quantity*wca.ecmactioncost,2) netto,
	ROUND(wca.quantity*wca.costbrutto,2) brutto,
	wc.worker_name workername,
	a.name actionname,
	a.indeks actioncode
FROM
	ecmworkcards_ecmaction wca,
	ecmworkcards wc,
	ecmactions a
WHERE
	wc.deleted = 0
	AND wc.id = wca.ecmworkcards
	AND wca.ecmaction = a.id
	AND wc.date >= STR_TO_DATE('" . $date_from . "','%d.%m.%Y')
	AND wc.date <= STR_TO_DATE('" . $date_to . "','%d.%m.%Y')
ORDER BY wc.worker_name, a.indeks";
$rows = $db->query($wcquery);
$wcarray = array();
if ($rows->num_rows != 0) {
	while ($r = $db->fetchByAssoc($rows)) {
		if($wcarray[$r['ecmproductid']] == NULL){
			$wcarray[$r['ecmproductid']] = array();
			$wcarray[$r['ecmproductid']][$r['workername']] = array();
			$row = array();
			$row['actioncode'] = $r['actioncode'];
			$row['actionname'] = $r['actionname'];
			$row['quantity'] = $r['quantity'];
			$row['netto'] = $r['netto'];
			$row['brutto'] = $r['brutto'];
			$suma['netto'] += $r['netto'];
			$suma['brutto'] += $r['brutto'];
			$wcarray[$r['ecmproductid']][$r['workername']][$r['actioncode']] = $row;
		}else{
			if($wcarray[$r['ecmproductid']][$r['workername']] == NULL){
				$wcarray[$r['ecmproductid']][$r['workername']] = array();
				$row = array();
				$row['actioncode'] = $r['actioncode'];
				$row['actionname'] = $r['actionname'];
				$row['quantity'] = $r['quantity'];
				$row['netto'] = $r['netto'];
				$row['brutto'] = $r['brutto'];
				$suma['netto'] += $r['netto'];
				$suma['brutto'] += $r['brutto'];
				$wcarray[$r['ecmproductid']][$r['workername']][$r['actioncode']] = $row;
			}else{
				if($wcarray[$r['ecmproductid']][$r['workername']][$r['actioncode']] == NULL){
					$row = array();
					$row['actioncode'] = $r['actioncode'];
					$row['actionname'] = $r['actionname'];
					$row['quantity'] = $r['quantity'];
					$row['netto'] = $r['netto'];
					$row['brutto'] = $r['brutto'];
					$suma['netto'] += $r['netto'];
					$suma['brutto'] += $r['brutto'];
					$wcarray[$r['ecmproductid']][$r['workername']][$r['actioncode']] = $row;
				}else{
					$wcarray[$r['ecmproductid']][$r['workername']][$r['actioncode']]['quantity'] += $r['quantity'];
					$wcarray[$r['ecmproductid']][$r['workername']][$r['actioncode']]['netto'] += $r['netto'];
					$wcarray[$r['ecmproductid']][$r['workername']][$r['actioncode']]['brutto'] += $r['brutto'];
					$suma['netto'] += $r['netto'];
					$suma['brutto'] += $r['brutto'];
				}
			}
		}
	}
}

$query = "
SELECT 
	wca.ecmproduct ecmproduct,
	p.name name,
	p.code code,
	sum(wca.ecmactioncost*wca.quantity) netto,
	sum(wca.costbrutto*wca.quantity) brutto
FROM
	ecmworkcards_ecmaction wca,
	ecmproducts p,
	ecmworkcards wc
WHERE
	wc.deleted = 0
		AND wc.id = wca.ecmworkcards
		AND wca.ecmproduct = p.id
		AND wc.date >= STR_TO_DATE('" . $date_from . "','%d.%m.%Y')
		AND wc.date <= STR_TO_DATE('" . $date_to . "','%d.%m.%Y')
GROUP BY wca.ecmproduct
ORDER BY p.code";
$rows = $db->query($query);
if ($rows->num_rows != 0) {
	$indeks=0;
	while ($r = $db->fetchByAssoc($rows)) {
		$row = array();
		$row['code'] = $r['code'];
		$row['name'] = $r['name'];
		$row['netto'] = $r['netto'];
		$row['brutto'] = $r['brutto'];
		$row['indeks'] = $indeks++;
		$row['actions'] = $wcarray[$r['ecmproduct']];
		$query = "
		SELECT 
			a.quantity
		FROM
			ecmstockdocinsideinitems a,
			ecmstockdocinsideins b
		WHERE
			a.ecmstockdocinsidein_id = b.id
			AND a.ecmproduct_id = '". $r['ecmproduct'] ."'
			AND b.deleted = 0
			AND b.register_date >= STR_TO_DATE('" . $date_from . "','%d.%m.%Y')
			AND b.register_date <= STR_TO_DATE('" . $date_to . "','%d.%m.%Y')";
		$tmp = $db->fetchByAssoc($db->query($query));
		$row['pw'] = round($tmp['quantity']);
		$return[$r['ecmproduct']] = $row;
	}
}
/**
 * ******************** SMARTY **********************
 */
$smarty = new Sugar_Smarty ();
$smarty->assign('MOD', $mod_strings);
$smarty->assign('data', $return);
$smarty->assign('suma', $suma);
$smarty->assign('date_from', $date_from);
$smarty->assign('date_to', $date_to);
$smarty->assign('dateFormat', $Calendar_daFormat);
if( $_GET['toPDF'] == '1' ) {
	$output = $smarty->fetch( 'modules/EcmReports/tpls/PDF/ReportEcmWorkCardsEcmProduct.tpl' );
	include_once ("include/MPDF57/mpdf.php");
	$p = new mPDF ( '', 'A4', null, 'helvetica', 10, 10, 10, 10, 5, 5 );
	$p->setTitle($mod_strings["LBL_REPORT_ECMWORKCARDS_PRODUCTS"]);
	$p->setFooter('{PAGENO}');
	$p->writeHTML($output);
	$p->Output ('RaportKartPracy_Produkty.pdf', 'I');
} else {
	echo $smarty->display('modules/EcmReports/tpls/ReportEcmWorkCardsEcmProduct.tpl');
}
?>
