<?php

    $db = $GLOBALS ['db'];
    $date_from = $_POST["date_from"];
    $date_to = $_POST["date_to"];
    $searchByType = $_POST['type'];
    $productId = $_POST['productid'];
    $date_from = new DateTime($date_from);
    $date_to = new DateTime($date_to);
	

	$query="SELECT
	        acco.name AS 'name',
		    acco.id AS 'id' ,
		    acco.parent_id AS 'parent_id' ,
		    produkt.code ,
	        sum(
	        CASE WHEN faktura.type!='correct'
	            THEN
	                    CASE WHEN faktura.currency_value is null or faktura.currency_value='' or faktura.currency_value=0
	                            THEN
	                                    pozycja.total_netto
	                            ELSE
	                                    pozycja.total_netto*faktura.currency_value
	                    END
	            ELSE
						CASE WHEN pozycja.old_ecminvoiceoutitem_id IS null OR pozycja.old_ecminvoiceoutitem_id='' THEN
	                            0
	                    ELSE
	                            CASE WHEN faktura.currency_value is null or faktura.currency_value='' or faktura.currency_value=0
	                                    THEN
	                                            pozycja.total_netto_corrected
	                                    ELSE
	                                            pozycja.total_netto_corrected*faktura.currency_value
	                            END
	           			END
				END
	        ) as netto,
	        sum(
	                CASE WHEN faktura.type!='correct' 
	                        THEN 
	                                pozycja.quantity
	                        ELSE 
	                                pozycja.quantity-pozycja.old_quantity 
	                END
	        ) as ilosc,
	        sum(

	                                pozycja.price_purchase*pozycja.quantity 

	        ) as koszt
	            FROM
	                ecminvoiceoutitems pozycja
	                    JOIN
	                ecminvoiceouts faktura ON pozycja.ecminvoiceout_id = faktura.id
	                    JOIN
	                ecmproducts produkt ON pozycja.ecmproduct_id = produkt.id
	                	JOIN
	                accounts acco ON acco.id = faktura.parent_id
	            WHERE
				faktura.register_date BETWEEN 
	                '".$date_from->format('Y-m-d')."' AND
	                   '".$date_to->format('Y-m-d')."'
	                        and faktura.type like '".$searchByType."' 
	            ";

	/*
	            if( $contrator == 1249 )
	            	$query .= "and acco.parent_id='".$contrator."'";
	            else
	            	$query .= "and faktura.parent_id='".$contrator."'";
	*/


	            $query .= "
	            and produkt.id = '".$productId."'
					and faktura.canceled = 0
						and faktura.deleted= 0
							and pozycja.deleted= 0
	            GROUP BY acco.id
	            COLLATE utf8_polish_ci;";

    $data = array();
    $rows = $db->query ($query);

	// prepare data for Smarty
	while($r = $db->fetchByAssoc ( $rows ))
	{
		$row = array();
		$row["id"] = $r["id"]; 
		$row["name"] = $r["name"]; 
		$row["code"] = $r["code"];
		$row["ilosc"] = $r["ilosc"];
		$row["netto"] = $r["netto"];
		$row["srednia"] = $row["netto"]/$row["ilosc"];
		$row["koszt"] = $r["koszt"];

		//if($row["netto"]>$row["koszt"])
		//{
			 $row["marza"] = ($row["netto"]-$row["koszt"])/($row["netto"])*100;
		//}else{
		//	$row["marza"] = 0;
		//}
		
		$data [] = $row;
					
	}
        $smart = new Sugar_Smarty ();
        $smart -> assign("data", $data);


	
	echo $smart->display ( 'modules/EcmReports/tpls/detailContractors.tpl' );
?>