<?php
if (! defined ( 'sugarEntry' ) || ! sugarEntry)
	die ( 'Not A Valid Entry Point' );
	
	// require_once('modules/EcmGroupSales/HeaderMenu.php');

global $sugar_version, $sugar_config, $current_user, $app_strings, $mod_strings;

require_once ('modules/EcmInsideOrders/EcmInsideOrder.php');
require_once ('include/time.php');
require_once ('include/json_config.php');

// add jquery
echo '<link rel="stylesheet" type="text/css" href="modules/EcmPrepaymentInvoices/css/style.css"/>';
echo '<script type="text/javascript"
src="include/jQuery/jquery-lastes.js"></script>';
echo '<script type="text/javascript"
src="include/jQuery/jquery.tablesorter.pager.js"></script>';
echo '<script type="text/javascript"
src="modules/EcmPrepaymentInvoices/javascript/ProductSummary.js"></script>';

$json_config = new json_config ();

		$focus = new EcmPrepaymentInvoice ();
		$db = $GLOBALS ['db'];
		$data = array();
		$categoryArray=array();
		$id_array= explode(",", $_GET['ids']) ;
		
		//var_dump($id_array);
		$where="and doc.id IN (";
		$count=count($id_array);
		$i=0;
		
		foreach($id_array as $value){
			$i++;
			if($value=='1' || $value=='on')continue;
			if($count!=$i)$where.="'".$value."',";
			else
				$where.="'".$value."'";
		
			
		}$where.=")";
		if($_GET['product_group']=='1')$where.=" and pr.group_ks=1";
		if($_GET['product_group']=='2')$where.=" and pr.group_ks=2";
		if($_GET['product_group']=='3')$where.=" and pr.group_ks=3";
	
			$query= "SELECT 
						pr.id,
						item.code,
						item.name, 
						ROUND(sum(item.quantity),0) AS quantity,
						ROUND(ifnull(x.stock, 0),0) AS stock,
						pr.group_ks,
						pr.product_category_name,
						pr.product_category_id 
					FROM
						ecmprepaymentinvoiceitems as item
							INNER JOIN
						ecmprepaymentinvoices as doc ON doc.id=item.ecmprepaymentinvoice_id
							INNER JOIN
						ecmproducts as pr ON pr.id=item.ecmproduct_id
							INNER JOIN 
						(SELECT sum(quantity) as stock ,product_id
							FROM 
								ecmstockstates
							GROUP BY product_id) x ON x.product_id=item.ecmproduct_id 
					WHERE doc.deleted='0' and doc.status!='s10' " . $where;
			$query.=" AND NOT EXISTS
				        (
				        SELECT  NULL
				        FROM    ecmstockdocouts t1
				        WHERE   t1.so_id = doc.id
				        ) GROUP by item.ecmproduct_id 
					ORDER BY pr.group_ks DESC";
	
	$rows = $db->query ($query);
	
	while($r = $db->fetchByAssoc ( $rows ))
	{
                        $row = array();
			$row["id"] = $r["id"]; 
			$row["code"] = $r["code"];
			$row["name"] = $r["name"];
			$row["quantity"] = $r["quantity"];
			$row["stock"] = $r["stock"];
			$row["product_category_name"]=$r["product_category_name"];
			$row["group_ks"] = $r["group_ks"];
			if($r["group_ks"]=='1')$daj='TH';
			if($r["group_ks"]=='2')$daj='WG';
			if($r["group_ks"]=='3')$daj='S';
			$row["group_ks_name"] = $daj;
	
                        array_push($row, $r);
			$data [] = $row;	
			$total+=$r['quantity'];
			$stock+=$r['stock'];
	}
	$data['quantity']=$total;
	$data['stock']=$stock;

	//var_dump($data);
	
	/// sumowanie

//var_dump($data);

$edit->ss = new Sugar_Smarty ();
$edit->module = 'EcmPrepaymentInvoices';

$edit->ss->assign ( "CREATED_BY_NAME", $focus->created_by_name );
$edit->ss->assign ( "MODIFIED_BY_NAME", $focus->modified_by_name );
$edit->ss->assign ( 'FOCUS', $focus);
$edit->ss->assign ( 'POST', $_POST);
$edit->ss->assign ( 'LINK', $link);
$edit->ss->assign ( 'IDS', $_GET['ids']);
$edit->ss->assign('POSITION_LIST',$data);
$edit->ss->assign ( 'MOD', $mod_strings);
$edit->ss-> display('modules/EcmPrepaymentInvoices/tpls/ProductSummary.tpl'); //4


// loading view
//echo '<link rel="stylesheet" type="text/css" href="modules/EcmInsideOrders/javascript/helper.css" media="screen" /><div class="loading_panel"></div>';
?>