<?php
//ini_set('display_errors',1);
require_once ('modules/EcmInvoiceOuts/EcmInvoiceOut.php');
global $mod_strings, $app_list_strings;

$accountName = null;
$accountId = null;
if(isSet($_REQUEST["accountName"])){
	$accountName = $_REQUEST["accountName"];
}
if(isSet($_REQUEST["accountId"])){
	$accountId = $_REQUEST["accountId"];
}
$to_pdf = false;
if(isSet($_GET ['to_pdf']) && $_GET ['to_pdf'] =='1'){
	$to_pdf = true;
}

if (isset($accountId) && $accountId!='') {
    $where['accountId'] = $accountId;
} else if (!isset($accountId) && isset($accountName) && strlen($accountName) > 0) {
    $where['accountName'] = $accountName;
}

try {
    $tmp = new DateTime(date('Y-m-31'));
    $tmp2 = new DateTime(date('Y-m-01'));
    $tmp2->sub(new DateInterval('P1Y'));
	$where['register_date_to'] = $tmp->format('Y-m-d');
    $where['register_date_from'] = $tmp2->format('Y-m-d');

} catch (Exception $e) {
    echo $e->getMessage();
    exit(1);
}

$data = AnalysisEcmQuote($where);
$rows = array();
if(count($data)>0){
	foreach ($data as $key => $value) {
		$rows['created'][] = $value['created'];
		$rows['accepted'][] = $value['accepted'];
		$rows['not_accepted'][] = $value['not_accepted'];
	}
}

$smarty = new Sugar_Smarty ();
$smarty->assign("MOD", $mod_strings);
$smarty->assign("DATA", $data);
$smarty->assign("ROWS", $rows);

$smarty->assign("accountName", $accountName);
$smarty->assign("accountId", $accountId);

if ($to_pdf) {
    $output = $smarty->fetch('modules/EcmReports/tpls/PDF/AnalysisProductSale.tpl');

    include_once ("include/MPDF57/mpdf.php");
    $p = new mPDF('', 'A4', NULL, 'helvetica', 10, 10, 10, 10, 5, 5);
    $p->writeHTML($output);
    $p->Output('RaportSprzedazy.pdf', 'I');
} else {
    echo $smarty->display('modules/EcmReports/tpls/AnalysisEcmQuote.tpl');
}

function AnalysisEcmQuote($where) {
    global $db;
    $query = "SELECT status, register_date FROM ecmquotes  WHERE deleted=0 ";
    //die(1);
    if (isset($where) && is_array($where) && count($where) > 0) {

        $wherereturn = array();
        foreach ($where as $key => $value) {
            switch ($key) {
                case 'register_date_from':
                    $wherereturn[] = "register_date>='" . $value . "'";
                    break;
                case 'register_date_to':
                    $wherereturn[] = "register_date<='" . $value . "'";
                    break;
                case 'accountName':
                    $wherereturn[] = "parent_name LIKE '%" . trim($value) . "%'";
                    $wherereturn[] = "parent_type ='Accounts'";
                    break;
				case 'accountId':
					$wherereturn[] = "parent_id = '" . trim($value) . "'";
					break;
            }
        }
        if (count($wherereturn) > 0) {
            $query .= " AND " . implode(" AND ", $wherereturn);
        }
    }
    $query .= " ORDER BY register_date";
    $result = $db->query($query);
	$return = array();
    if ($result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {
			$return[substr($row['register_date'], 0, 7)][$row['status']] += 1;
        }
    } else {
        $return = NULL;
    }
	
   	if(count($return)<12){
		try {
			$currentDate = new DateTime(date('Y-m-01'));
			$date = new DateTime(date('Y-m-01'));
			$date->sub(new DateInterval('P1Y'));
			$register_date_fill = $date->format('Y-m');
		} catch (Exception $e) {
			echo $e->getMessage();
			exit(1);
		}
		while(count($return)<12){
			if(!isset($return[$register_date_fill])){
				$return[$register_date_fill]['total_purchase'] = 0;
				$return[$register_date_fill]['total_netto'] = 0;
			}
			$date->add(new DateInterval('P1M'));
			$register_date_fill = $date->format('Y-m');
		}
	}
	
	ksort($return);
	$formated_return;
	foreach ($return as $data => $row){
		$split = split("-",$data);
		$formated_return[$split[1] .".".$split[0]] = $row;
	}
	
    return $formated_return;
}
?>