<?php
$home = $_SERVER['DOCUMENT_ROOT'];


include_once($_SERVER['DOCUMENT_ROOT'] . "/apps/billing/config/autoload.php");

Master_Protect();

$page_title="Billing Report";
include_once($_SERVER['DOCUMENT_ROOT'] . "/core/classes/main/header.php");

Master_render_menu('', $hidemenu, '');
echo '
<div id="containerSpe ">
	<div id="wrapper">
       <div class="d-flex flex-column ml-4 mr-4" id="content-wrapper">
            <div id="content" class="container1">
				<div class="page-header-presentation">
					<div class="row  mb-4 mt-2 ">      
						<div class="col-sm-12 col-md-4">
							<h2 data-toggle="tooltip" data-placement="top" title="" data-original-title="Create, Read, Update, Delete, Upload, Download">            Monthly Report</h2>                    
						</div>
                    </div>
                </div>
                
           ';

function GetMonthsSales()
{
    
    $sql = "   select count(1) as bills,
                       DATE_FORMAT(STR_TO_DATE(JSON_UNQUOTE(JSON_EXTRACT(attributes,'$.issue_date')), '%Y-%m-%d'),'%M %Y') as vshow ,
                       DATE_FORMAT(STR_TO_DATE(JSON_UNQUOTE(JSON_EXTRACT(attributes,'$.issue_date')), '%Y-%m-%d'),'%Y%m') as value 
                 from billing_invoice
                group by DATE_FORMAT(STR_TO_DATE(JSON_UNQUOTE(JSON_EXTRACT(attributes,'$.issue_date')),'%Y-%m-%d'),'%M %Y')
					    ,DATE_FORMAT(STR_TO_DATE(JSON_UNQUOTE(JSON_EXTRACT(attributes,'$.issue_date')),'%Y-%m-%d'),'%Y%m')
                order by DATE_FORMAT(STR_TO_DATE(JSON_UNQUOTE(JSON_EXTRACT(attributes,'$.issue_date')),'%Y-%m-%d'),'%Y%m') desc;
				";
    $db=Master_DBController::getInstance();
	
    $Amount = $db->runBaseQuery($sql);    
    return (json_encode($Amount));
}



$Months = GetMonthsSales();
$m = json_decode($Months);

echo ' <script>
       function RefreshPdf (e){

        window.location.href = "/apps/billing/cp/billing_reportsales?dat="+e.value;
        
       };
       </script>
';
echo '
<div id ="content1" class="con1tainer ml-4 mb-2">
	<div class="row p-0">
		<div class="col-12 p-0">
			<select id ="MonthsSelectSales" class="day-select form-control" Onchange="RefreshPdf(this);">';
				$x = 0;

				if (isset($_GET['dat']))
					$dat = $_GET['dat'];
				else 
					$dat = '';

				foreach ($m as $month) {
				  if ($x == 0 && $dat == '') {
					$dat = $month->value;
				  }

				  if ($dat == $month->value)
					echo '<option value="' . $month->value . '" selected="selected" >' . $month->vshow . '</option  >';
				  else
					echo '<option value="' . $month->value . '">' . $month->vshow . '</option  >';

				  $x++;
				}

			echo '</select><br>
		</div>
	</div>';


$temp_name_pdf = '/apps/billing/tmp/sales_' . $_SESSION['user_id'] . '.pdf';
if (file_exists($_SERVER['DOCUMENT_ROOT'] .$temp_name_pdf)) 
unlink($_SERVER['DOCUMENT_ROOT'] .$temp_name_pdf);

createPDFSales($dat, $_SERVER['DOCUMENT_ROOT'] . $temp_name_pdf);

?>
			
				<div class="row " style="height:70vh">		
					  <iframe src="<?php echo $temp_name_pdf; ?>" width="100%" height="100%" name="iframe_a"></iframe>
				</div>				
			</div>
		</div>
    </div>
  </div>
</div>

<?php
    include_once($_SERVER['DOCUMENT_ROOT'] . "/core/classes/main/footer.php");
?>