<?
// Level 9 이하만 사용 가능
$func->checkLevelModal(9);

// 기능 제한 (Admin : 1, Staff : 5, Accounting : 6, Driver : 9 만 가능)
$permit = array("1", "3", "5", "6", "7", "9");
if (in_array($_SESSION['ss_LEVEL'], $permit)) {
    $setTag = "ENABLED";
}
else {
		$setTag = "DISABLED";	
		$msg = "Sorry, You don't have permission. Please contact Administrator.";
		$func -> modalMsg ($msg, "/index_intranet.php");
		exit();
}

//echo "<br><br><br><br><br><br>[$startYear][$tmp]";

$startYear = str_replace("-", "", trim($startYear));

if ($startYear =="") {
	$startYear = date("Y");
	//$date = new DateTime('PREVIOUS MONTH');
	//$startYear = $date->format('Y');
}



for($ii=2022; $ii<= date("Y"); $ii++) {
	if ($ii == $startYear) $selectedTag = "selected";
	else $selectedTag = "";
	
	$yearSTR .= "<option value='$ii' $selectedTag >$ii</option>";

}

$beforeYear = $startYear -1;

//echo "<br><br><br><br><br><br>[$startYear][$beforeYear]";

$rowSTR = "";
$rowYSTR = "";
$rowAccSTR = "";

$monthArray = array("","January","February","March","April","May","June","July","August","September","October","November","December");

//$graphSTR = "['Month', '".$beforeYear."년', '".$startYear."년'],\n";
//$graphAccSTR = "['Type', '전체계정', '새계정', '삭제계정', { role: 'annotation' } ],\n";
$graphSTR = "['Month', '".$beforeYear."', '".$startYear."'],\n";
$graphAccSTR = "['Type', 'Total', 'New', 'Terminate', { role: 'annotation' } ],\n";

$graphQSTR = "['Year', 'Volumes'],\n";

for ($mmonth = 1; $mmonth <= 12; $mmonth ++) {
			
			$month_quantity_total = 0;
			$month_visit_total = 0;
			$month_call_total = 0;
			
			$mmonthtmp = sprintf("%02d", $mmonth);	
			
      $graphSTR .= "['".$monthArray[$mmonth]."', ";
      
      // 이전 연도부터 선택된 연도까지 오일양 
			for ($yyear = $beforeYear; $yyear <= $startYear; $yyear ++) {	
						$qry_m = "SELECT SUM(rd_quantity) AS quantity
											FROM tbl_report_daily 
											WHERE rd_orderdate LIKE '".$yyear.$mmonthtmp."%' ";

						$rt_m = $jdb->fQuery($qry_m, "list error");
						
						//echo "[".$yyear.$mmonthtmp."][".$rt_m['quantity']."[$qry_m]<br>";

						if ($rt_m[0] != "") $month_quantity = $rt_m['quantity'];
						else $month_quantity = "";
						
						if ($yyear%2 == 0) $bgcolorSTR = "background-color: #f6f0f0;";
						else $bgcolorSTR = "";
						
						$rowYSTR .= "<td style='text-align:center;font-weight:bold; $bgcolorSTR'>".$yyear."</td>";
						
						// 현재월의 데이터는 보이지 않음
						/*if ($yyear == date('Y') && $mmonth == date('m')) {
							$rowSTR .= "<td style='text-align:center; $bgcolorSTR'>N/A</td>";
							$graphSTR .= "0,";
						}
						else {
							$rowSTR .= "<td style='text-align:center; $bgcolorSTR'>".number_format(floatval($month_quantity))."</td>";
							$graphSTR .= (int)$month_quantity.",";
						}*/
						
						// 현재월에 데이터 보임
						$rowSTR .= "<td style='text-align:center; $bgcolorSTR'>".number_format(floatval($month_quantity))."</td>";
						$graphSTR .= (int)$month_quantity.",";
						
			}
			$graphSTR = substr($graphSTR, 0, -1);
			$graphSTR .= "],\n";


			// 선택된 연도의 계정정보 조회
			
			$qry_acc = "select sum(ra_account) AS accountno, sum(ra_accountnew) AS newaccount, sum(ra_accountia) AS iaaccount
									FROM tbl_report_account WHERE ra_month LIKE '".$startYear.$mmonthtmp."%' ";
			$rt_acc = $jdb->fQuery($qry_acc, "list error");
			
			// 2023년 11월까지의 계정 정보는 보이지 않기로 함 (2023.12.27)
			if ($startYear.$mmonthtmp <= "202311") {
					$rt_acc['accountno'] = 0;
					$rt_acc['iaaccount'] = 0;
					$rt_acc['newaccount'] = 0;	
			}
			
			$rowAccSTR .= "<tr><td><strong>".$monthArray[$mmonth]."</strong></td><td style='text-align:right;'>".(int)$rt_acc['accountno']."</td><td style='text-align:right;'>".(int)$rt_acc['newaccount']."</td><td style='text-align:right;'>".(int)$rt_acc['iaaccount']."</td></tr>\n";
			$graphAccSTR .= "['".$monthArray[$mmonth]."', ".(int)$rt_acc['accountno'].", ".(int)$rt_acc['newaccount'].", ".(int)$rt_acc['iaaccount'].",''],\n";
			//echo "[$qry_acc]<br>";

}


// 연도별 전체 오일양
for ($yyear = ($startYear-5); $yyear <= $startYear; $yyear ++) {	

		$qry_q = "SELECT SUM(rd_quantity) AS quantity
							FROM tbl_report_daily 
							WHERE rd_orderdate LIKE '".$yyear."%' ";

		if ($yyear >= 2021) $rt_q = $jdb->fQuery($qry_q, "list error");
		
		if ($rt_q[0] != "") $year_quantity = $rt_q['quantity'];
		else $year_quantity = "";

		$rowQSTR .= "<tr><td><strong>".$yyear."</strong></td><td style='text-align:right;'>".number_format(floatval($year_quantity))."</td></tr>\n";
		$graphQSTR .= "['".$yyear."', ".(int)$year_quantity." ],\n";
		//echo "[$qry_acc]<br>";

}




?>



<SCRIPT LANGUAGE=JAVASCRIPT>
<!--
function goSearch(f){
    var f = document.form3;

			//f.searchtype.value = document.getElementById('searchtype').value;
			//f.searchDriver.value = document.getElementById('searchDriver').value;
			//f.key_word.value = document.getElementById('key_word').value;
			f.submit();
}
//-->
</SCRIPT>



<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
  google.charts.load('current', {'packages':['corechart']});
  google.charts.setOnLoadCallback(drawChart);

  function drawChart() {
    var data = google.visualization.arrayToDataTable([

<?=$graphSTR?>

/*      ['Month', '2022', '2023'],
      ['January', 1000, 400],
      ['2015', 1170, 460],
      ['2016', 660, 1120],
      ['2017', 1030, 540]
*/
    ]);

    var options = {
      /*title : 'Monthly Coffee Production by Country',*/
      vAxis: {title: 'Volumes', titleTextStyle: {color: '#FF0000'}},
      hAxis: {title: 'Month', titleTextStyle: {color: '#FF0000'}},
      seriesType: 'bars',
      series: {5: {type: 'line'}}
    };

    var chart = new google.visualization.ComboChart(document.getElementById('chart_div'));
		chart.draw(data, options);
  }
  
  google.charts.load('current', {'packages':['corechart']});
  google.charts.setOnLoadCallback(drawChartAccount);

  function drawChartAccount() {
    var data = google.visualization.arrayToDataTable([

<?=$graphAccSTR?>

/*
      ['Genre', 'Fantasy & Sci Fi', 'Romance', 'Mystery/Crime', 'General',
       'Western', 'Literature', { role: 'annotation' } ],
      ['2010', 10, 24, 20, 32, 18, 5, ''],
      ['2020', 16, 22, 23, 30, 16, 9, ''],
      ['2030', 28, 19, 29, 30, 12, 13, '']
*/
    ]);

    var options = {
      vAxis: {title: 'Accounts', titleTextStyle: {color: '#FF0000'}},
      hAxis: {title: 'Month', titleTextStyle: {color: '#FF0000'}},
      legend: { position: 'top', maxLines: 3 },
      bar: { groupWidth: '60%' },
      isStacked: true,
    };  
    
		var chart = new google.visualization.ColumnChart(document.getElementById('chart_account_div'));
    chart.draw(data, options);

  }


  google.charts.load('current', {'packages':['corechart']});
  google.charts.setOnLoadCallback(drawChartYear);

  function drawChartYear() {
    var data = google.visualization.arrayToDataTable([

<?=$graphQSTR?>

/*
   		['Year', 'Volume'],
       ['2012',  900],
       ['2013',  1000],
       ['2014',  1170],
       ['2015',  1250],
       ['2016',  1530]
*/

    ]);

    var options = {
			vAxis: {title: 'Volumes', titleTextStyle: {color: '#FF0000'}},
      hAxis: {title: 'Year', titleTextStyle: {color: '#FF0000'}},
    	/*title: 'Population (in millions)',*/
    	bar: { groupWidth: '60%' },
    	
    	}; 

    // Instantiate and draw the chart.
    var chart = new google.visualization.ColumnChart(document.getElementById('chart_year_div'));
    chart.draw(data, options);
 }
         
</script>



<main id="main" class="main">

  <!-- ======= Breadcrumbs ======= -->
  <div class="breadcrumbs">
    <div class="container">

      <div class="d-flex justify-content-between align-items-center">
        <h2>YEARLY REPORT</h2>
        <ol>
          <li><a href="index.html">HOME</a></li>
          <li>REPORT</li>
          <li>YEARLY REPORT</li>
        </ol>
      </div>

    </div>
  </div><!-- End Breadcrumbs -->


  <section class="page">


    <div class="container" data-aos="fade-up">

        <div class="wrap-border">
						<FORM CLASS="form-report" METHOD=POST NAME=form3 ACTION='<?=$_SERVER["PHP_SELF"]?>'>
							<INPUT TYPE=HIDDEN NAME=view VALUE='report_yearly'>
							<INPUT TYPE=HIDDEN NAME=key_word VALUE=''>

            <table class="table-search-report col-float-right">
              <tr>
                <!--td>
                  <input type="date" placeholder="2022-12-31" class="custom-select">     

                </td-->
                <td> 
	                <select name="startYear" id="startYear" class="custom-select" required='required' style="width:200px" onChange="goSearch();">
	                  <?=$yearSTR?>
	                </select>
	              </td>
              
                <td>
                    <button class="btn btn-search" type="submit"><i class="bi-search"></i></button>
                </td>

              </tr>

            </table>
          </FORM>
        </div><!--wrap-border-->

    </div><!--container Ends-->

    


    <div class="container" data-aos="fade-up"  style="clear: both;">
      
      <div class="wrap-overflow forecast-info report-wrap margin-top-25">
          <table class="tb-list tb-report tb-list-months">
           <tr class="tr-list-months" >
            <th colspan="2" style='text-align:center;'>January</th>
            <th colspan="2" style='text-align:center;'>February</th>
            <th colspan="2" style='text-align:center;'>March</th>
            <th colspan="2" style='text-align:center;'>April</th>
            <th colspan="2" style='text-align:center;'>May</th>
            <th colspan="2" style='text-align:center;'>June</th>
            <th colspan="2" style='text-align:center;'>July</th>
            <th colspan="2" style='text-align:center;'>August</th>
            <th colspan="2" style='text-align:center;'>September</th>
            <th colspan="2" style='text-align:center;'>October</th>
            <th colspan="2" style='text-align:center;'>November</th>
            <th colspan="2" style='text-align:center;'>December</th>
           </tr>
           <tr class="tr-list-months yearly">
<?=$rowYSTR?>
           </tr>
           <tr class="tr-list-months yearly">
<?=$rowSTR?>
           </tr>

          </table>
      </div>
  </div><!--container Ends-->

  <div class="container margin-top-25" data-aos="fade-up">
    <br>
    <div class="grid-layout-graph-col-2">
      <div class="graph-box">
        <h4 class="text-center">Monthly Comparison of Oil Collection with Last Year (L)</h4>
        <div class="border-graph">
        	
        	<div id="chart_div" ></div>
        	
        </div>
      </div>


    </div>
  </div>


  <div class="container margin-top-50">
      <div class="box-table">
        <table class="tb-list-yearly tb-report">
          <tr class="tr-list-months">
            <th rowspan="2"><?=$startYear?></th>
            <th colspan="3">Change in Total Accounts</th>
          </tr>
          <tr>
            <th>Total</th>
            <th>New</th>
            <th>Terminate</th>
          </tr>
          
<?=$rowAccSTR?>
          
<!--
          <tr>
            <td><strong>January</strong></td>
            <td>4179</td>
            <td>39</td>
            <td>19</td>
          </tr>
-->

        </table>
      </div>
      
      <div class="col-float-right width-graph-65">
        <h4 class="text-center"> Monthly Change in Total Accounts (<?=$startYear?>)</h4>
        <div class="border-graph-wrap">
        	
        	<div id="chart_account_div" style="width: 95%; height: 400px; margin:0 auto;"></div>
        
        </div>
      </div>
  </div>

  <div class="container margin-top-25">
   <hr>
  </div>


  <div class="container margin-top-50">
    <div class="box-table">
      <table class="tb-list-yearly tb-report">
        <tr>
          <th colspan="2">Yearly Oil Pickup Volumes (L)</th>
        </tr>

<?=$rowQSTR?>

<!--
        <tr>
          <td>2016년</td>
          <td>4179</td>
        </tr>
-->
      </table>
    </div>

    <div class="col-float-right width-graph-65">
      <h4 class="text-center"> Yearly Oil Pickup Volumes (L)</h4>
      <div class="border-graph-wrap-2">
      	
      	<div id="chart_year_div" style="width: 95%; height: 400px; margin:0 auto;"></div>
      	
      </div>
    </div>
  </div>


<!--
  <div class="container margin-top-25" style="clear: both;">
      <a class="btn-sub text-center width-200 col-float-right" href="#">LIST</a>
  </div>
-->      

  </section>

</main><!-- End #main -->