<?
// 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>[$startYearMonth][$tmp]";

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

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

//total record
$query = "SELECT COUNT(m_uid) FROM tbl_member WHERE m_level = 9 AND m_status = 'A' ";
$driver_cnt=$jdb->rQuery($query, "record query error");

// Get Driver Info
$qry_driver = "SELECT * FROM tbl_member WHERE m_level = 9 AND m_status = 'A' ORDER by m_initial ASC ";
$rt_driver = $jdb->nQuery($qry_driver, "list error"); 

$driverArray = array();
$driverCnt = 0;

$weekly_data = array();

$getLastday = date('t', strtotime($startYearMonth."10")); 

//echo "[$startYearMonth][$getLastday]<br>";

// Get Driver with Title
$titleSTR = "<tr>
                <th class='td-140'></th>
                <th class='td-50'></th>";

while($lt_driver=mysqli_fetch_array($rt_driver, MYSQLI_ASSOC)) {
	$driverArrayTmp = array($lt_driver['m_uid'] => $lt_driver['m_initial']);	
	$driverArray += $driverArrayTmp;
	
	$titleSTR .= "<th class='td-80' style='text-align:center;'>".$lt_driver['m_initial']."</th>";
}	

$titleSTR .= "</tr>\n";


// Get Oil Quantity
$rowOilSTR = "<tr>
				          <td rowspan='2'>Oil Pickup Volumes</td><td>(L)</td>";
$rowOilTonSTR = "<tr>
				          <td>(Mton)</td>";				          
$rowVisitSTR = "<tr>
				          <td># of Account Visits</td><td></td>";
$rowAccountSTR = "<tr>
				          <td>Total Accounts</td><td></td>";
$EUDataSTR = "<tr>
				          <td>ISCC Oil Volumes</td><td></td>";				          
$rowNewSTR = "<tr>
				          <td>Total Contracts</td><td></td>";


$colorArray = array("#3366CC","#DC3912");

// Account Status 
// cstatus => A : Active, I : Inactive, T : Total
$cstatus = 'T';
if ($cstatus == 'T') $status_query = " AND (c.c_status = 'A' OR c.c_status = 'I') ";
else if ($cstatus == 'I') $status_query = " AND (c.c_status = 'I') ";
else {
	$cstatus = 'A';
	$status_query = " AND (c.c_status = 'A') ";
}

foreach ($driverArray as $kkey => $vvalue) {						

// ISCC Oil Volumes => 새로운 메뉴로 이동 (2025.06.12)
/*
		$qry_eu = "SELECT 
									SUM(CASE WHEN d_visitdate like '".$startYearMonth."%' THEN d_quantity ELSE 0 END) AS EUDATA
								FROM  tbl_daily 
								WHERE d_customeruid != '' AND d_status = 'F' 
									AND (d_customeruid IN (SELECT c.c_uid FROM tbl_customer c WHERE c.c_driveruid = '".$kkey."' ". $status_query. "))
									AND d_form_eu != '' AND d_form_eu <= d_orderdate ";
//echo "<br><br><br><br><br>EU[$qry_eu]<br>";			
		$rt_eu = $jdb->fQuery($qry_eu, "fetch query error");
		$EUDataSTR .= "<td style='text-align:right;'>".number_format(floatval($rt_eu['EUDATA']))."</td>";
*/
			
		$qry_sum = "select sum(rd_quantity) AS quantity, sum(rd_visit) AS visitno
								FROM tbl_report_daily WHERE rd_driveruid = '".$kkey."' AND rd_orderdate LIKE '".$startYearMonth."%' ";
		$rt_sum = $jdb->fQuery($qry_sum, "list error");
		//echo "[$qry_sum]<br>";
		$rowOilSTR .= "<td style='text-align:right;'>".number_format(floatval($rt_sum['quantity']))."</td>";
		$rowOilTonSTR .= "<td style='text-align:right;'>".number_format(floatval($rt_sum['quantity'])*0.00091068,1)."</td>";
		$rowVisitSTR .= "<td style='text-align:right;'>".(int)$rt_sum['visitno']."</td>";
		
		$colorSTR = $colorArray[$driverCnt%2];
	
		$graphOilTonSTR .= "['".$vvalue."' ,".number_format(floatval($rt_sum['quantity'])*0.00091068,1).", '".$colorSTR."'],";		
		$graphVisitSTR .= "['".$vvalue."' ,".(int)$rt_sum['visitno'].", '".$colorSTR."'],";		

		$qry_acc = "select sum(ra_account) AS accountno, sum(ra_accountnew) AS newaccount
								FROM tbl_report_account WHERE ra_driveruid = '".$kkey."' AND ra_month ='".$startYearMonth."' ";
		$rt_acc = $jdb->fQuery($qry_acc, "list error");
		//echo "[$qry_acc]<br>";
		$rowAccountSTR .= "<td style='text-align:right;'>".(int)$rt_acc['accountno']."</td>";
		$rowNewSTR .= "<td style='text-align:right;'>".(int)$rt_acc['newaccount']."</td>";
		
		$driverCnt++;	
		
}

$rowOilSTR .= "</tr>\n";
$rowOilTonSTR .= "</tr>\n";
$EUDataSTR .= "</tr>\n";
$rowVisitSTR .= "</tr>\n";
$rowAccountSTR .= "</tr>\n";	
$rowNewSTR .= "</tr>\n";	


$startYearMonthTmp = substr($startYearMonth,0,4)."-".substr($startYearMonth,4,2);
	
?>
	
	
<script type="text/javascript">
$(document).ready(function(){
	
		$(function() {
		     $('.date-picker').datepicker(
		                    {
		                        dateFormat: "yy-mm",
		                        changeMonth: true,
		                        changeYear: true,
		                        showButtonPanel: true,
		                        onClose: function(dateText, inst) {


		                            function isDonePressed(){
		                                return ($('#ui-datepicker-div').html().indexOf('ui-datepicker-close ui-state-default ui-priority-primary ui-corner-all ui-state-hover') > -1);
		                            }

		                            if (isDonePressed()){
		                                var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
		                                var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
		                                $(this).datepicker('setDate', new Date(year, month, 1)).trigger('change');
		                                
		                                 $('.date-picker').focusout()//Added to remove focus from datepicker input box on selecting date
		                            }
		                        },
		                        beforeShow : function(input, inst) {

		                            inst.dpDiv.addClass('month_year_datepicker')

		                            if ((datestr = $(this).val()).length > 0) {
		                                year = datestr.substring(datestr.length-4, datestr.length);
		                                month = datestr.substring(0, 2);
		                                $(this).datepicker('option', 'defaultDate', new Date(year, month-1, 1));
		                                $(this).datepicker('setDate', new Date(year, month-1, 1));
		                                $(".ui-datepicker-calendar").hide();
		                            }
		                        }
		                    })
		});
	
});
</script>

<style>
.ui-datepicker-calendar {
    display: none;
    }
</style>


<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>




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

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

        <div class="d-flex justify-content-between align-items-center">
          <h2>MONTHLY DRIVERS REPORT</h2>
          <ol>
            <li><a href="index.html">HOME</a></li>
            <li>REPORT</li>
            <li>MONTHLY DRIVERS 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_monthly'>
							<INPUT TYPE=HIDDEN NAME=key_word VALUE=''>
													            	
              <table class="table-search-report col-float-right">
                <tr>
                  <td>
                  	 <input name="startYearMonth" id="startYearMonth" class="custom-select date-picker" value="<?=$startYearMonthTmp?>" onChange="goSearch();"/>
                    <!--input type="date" placeholder="2022-12-31" class="custom-select"-->     

                  </td>
                  <!--td> 
                      <input class="custom-select" type="text" placeholder="Search.." name="search">
                  </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;">
        <hr>
        <h4 class="text-center">Monthly Drivers Report (<?=$startYearMonthTmp?>)</h2>
        <hr>

      </div>


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

        <div class="wrap-overflow forecast-info report-wrap">
            <table class="tb-list tb-report">

<?=$titleSTR?>

<?=$rowOilSTR?>
<?=$rowOilTonSTR?>
<!--<?=$EUDataSTR?>-->
<?=$rowVisitSTR?>
<?=$rowAccountSTR?>



<!--
              <tr>
                <td rowspan="2">오일 픽업 양</td>
                <td>(L)</td>
                <td>81982</td>
                <td>67097</td>
                <td>81982</td>
                <td>67097</td>
                <td>81982</td>
                <td>67097</td>
                <td>81982</td>
                <td>67097</td>
                <td>67097</td>
              </tr>
              <tr>
                <td>(Mton)</td>
                <td>81982</td>
                <td>67097</td>
                <td>81982</td>
                <td>67097</td>
                <td>81982</td>
                <td>67097</td>
                <td>81982</td>
                <td>67097</td>
                <td>67097</td>
              </tr>
              <tr>
                <td>계정 방문 횟수</td>
                <td></td>
                <td>81982</td>
                <td>67097</td>
                <td>81982</td>
                <td>67097</td>
                <td>81982</td>
                <td>67097</td>
                <td>81982</td>
                <td>67097</td>
                <td>67097</td>
              </tr>
              <tr>
                <td>총 계정 수</td>
                <td></td>
                <td>81982</td>
                <td>67097</td>
                <td>81982</td>
                <td>67097</td>
                <td>81982</td>
                <td>67097</td>
                <td>81982</td>
                <td>67097</td>
                <td>67097</td>
              </tr>

              <tr>
                <td>상위 20% 계정 수</td>
                <td></td>
                <td>81982</td>
                <td>67097</td>
                <td>81982</td>
                <td>67097</td>
                <td>81982</td>
                <td>67097</td>
                <td>81982</td>
                <td>67097</td>
                <td>67097</td>
              </tr>
              <tr>
                <td>총 계약건 수</td>
                <td></td>
                <td>81982</td>
                <td>67097</td>
                <td>81982</td>
                <td>67097</td>
                <td>81982</td>
                <td>67097</td>
                <td>81982</td>
                <td>67097</td>
                <td>67097</td>
              </tr>
-->
              
              <!--tr>
                <td rowspan="2">총 근무 시간</td>
                <td>(hr)</td>
                <td>81982</td>
                <td>67097</td>
                <td>81982</td>
                <td>67097</td>
                <td>81982</td>
                <td>67097</td>
                <td>81982</td>
                <td>67097</td>
                <td>67097</td>
              </tr>
              <tr>
                <td>(days)</td>
                <td>81982</td>
                <td>67097</td>
                <td>81982</td>
                <td>67097</td>
                <td>81982</td>
                <td>67097</td>
                <td>81982</td>
                <td>67097</td>
                <td>67097</td>
              </tr-->
            </table>
        </div>
    </div><!--container Ends-->



<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([
        ["Element", "Volume", { role: "style" } ],

<?=$graphOilTonSTR?>

/*
        ["Copper", 8.94, "orange"],
        ["Silver", 10.49, "gold"],
        ["Gold", 19.30, "green"],
        ["Platinum", 21.45, "blue"],
        ["Copper", 8.94, "red"],
        ["Silver", 10.49, "orange"],
        ["Gold", 19.30, "gold"],
        ["Platinum", 21.45, "green"],
        ["Platinum", 21.45, "blue"],
        ["Silver", 10.49, "red"],
        ["Gold", 19.30, "orange"],
        ["Platinum", 21.45, "gold"]                  
*/
      ]);

      var view = new google.visualization.DataView(data);
      view.setColumns([0, 1,
                       { calc: "stringify",
                         sourceColumn: 1,
                         type: "string",
                         role: "annotation" },
                       2]);

      var options = {
        //title: "Density of Precious Metals, in g/cm^3",
        //width: 600,
        //height: 400,
        bar: {groupWidth: "60%"},
        legend: { position: "none" },
	      hAxis: {title: 'Driver', titleTextStyle: {color: 'black'}},
	      vAxis: {title: 'Volume (Mton)', titleTextStyle: {color: 'black'}}        
      };
      var chart = new google.visualization.ColumnChart(document.getElementById("columnchart_values"));
      chart.draw(view, options);
  }




    google.charts.setOnLoadCallback(drawChartVisit);
    function drawChartVisit() {
      var data = google.visualization.arrayToDataTable([
        ["Element", "Visits", { role: "style" } ],

<?=$graphVisitSTR?>

/*
        ["Copper", 8.94, "orange"],
        ["Silver", 10.49, "gold"],
        ["Gold", 19.30, "green"],
        ["Platinum", 21.45, "blue"],
        ["Copper", 8.94, "red"],
        ["Silver", 10.49, "orange"],
        ["Gold", 19.30, "gold"],
        ["Platinum", 21.45, "green"],
        ["Platinum", 21.45, "blue"],
        ["Silver", 10.49, "red"],
        ["Gold", 19.30, "orange"],
        ["Platinum", 21.45, "gold"]                  
*/
      ]);

      var view = new google.visualization.DataView(data);
      view.setColumns([0, 1,
                       { calc: "stringify",
                         sourceColumn: 1,
                         type: "string",
                         role: "annotation" },
                       2]);

      var options = {
        //title: "Density of Precious Metals, in g/cm^3",
        //width: 600,
        //height: 400,
        bar: {groupWidth: "60%"},
        legend: { position: "none" },
	      hAxis: {title: 'Driver', titleTextStyle: {color: 'black'}},
	      vAxis: {title: 'Total Visits', titleTextStyle: {color: 'black'}}        
      };
      var chart = new google.visualization.ColumnChart(document.getElementById("columnchart_visit_values"));
      chart.draw(view, options);
  }
			$(window).resize(function(){
			       // drawChart();
			        //drawRegionsMap();
			});
			  
  </script>



    <div class="container margin-top-25" data-aos="fade-up">
      <br>
      <div class="grid-layout-graph-col-2">
        <div class="graph-box">
          <br>
          <h4 class="text-center">Oil Pickup Volume (Mton)</h4>
          <div class="border-graph">
          	
          	<div id="columnchart_values" style="width: 100%; height: 500px;"></div>
          	
          </div>
        </div>
        <div class="graph-box">
          <br>
          <h4 class="text-center">Total Visits</h4>
          <div class="border-graph">
          	
          	<div id="columnchart_visit_values" style="width: 100%; height: 500px;"></div>
          	
          </div>
        </div>

      </div>
    </div>

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

    </section>

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