<meta charset="utf-8">
<?php
    require('../conn/conn_mssql.php');
    require('../conn/conn_mysql.php');
            $name=$_GET['name'];
            $bday=$_GET['bday'];
            $car_series=$_GET['car_series'];

            $day = date("d");

            $month=date("m",strtotime($bday));

            $year=date("Y",strtotime($bday));
            $day=$year.'-'.$month.'-1';
            $end=$year."-".$month;
            $year = $year;
            $model="";

            if($_GET["name"]==40230100){
            $isuzu="บริษัท อีซูซุนครหลวง จำกัด";   
                }
            else{
            $isuzu="บริษัท อีซูซุนครหลวงมอเตอร์ จำกัด";
            }

$header='
<p style="text-align: right;">'.$end.' / '.$car_series.' ( {PAGENO}/{nb} )</p>
        <h3 style="text-align: center;">บัญชีพิเศษและบัญชีคุมสินค้า</h3>

<table>
        
    <tr>
        <td width="400">ชื่อผู้ปรกอบการค้า : '.$isuzu.'</td>
        <td width="100"></td>
        <td width="200">ปริมาณนับเป็น : คัน </td>
        
    </tr>
<tr>
    <td width="400">ชื่อสินค้า : '.$car_series.'</td>
    <td width="400"></td>
    <td width="400"></td>
</tr>

';
                $sql_code="SELECT DISTINCT Car_Model FROM View_Buy_Car WHERE Car_Series='".$car_series."' AND MONTH(DATE)='$month' AND YEAR(DATE)='$year'";
                $query_code=$conn->prepare($sql_code);
                $query_code->execute();
                while($result_code = $query_code->fetch( PDO::FETCH_ASSOC)){
                    $model .=$result_code['Car_Model'].", ";
                }
                $lett="$model";
$header .='
<tr>
    <td COLSPAN="3">Model Code : '.substr($lett,0,150).'...</td>
</tr>
</table>
';

$htmlele ='
<meta charset="utf-8">

<!DOCTYPE html>
<html> 
<head>
<title>ใบเสนอราคา</title>
</head>   
<body>
<style>
.text-right {
text-align: right;
font-size:15px;
}
.text-left {
text-align: left;
font-size:12px;
}
.text-center {
text-align: center;
font-size:12px;
}
body{
    font-family: "Garuda";
    font-size: 8pt;
}
.thai2{
    font-family: "Garuda";
}
p{
	padding-top :none;
    text-align: justify;
}
.border{
  border :1;
  border-collapse: collapse;
  padding:5px;
}
.redus{
  border-top-left-radius: 5;
  border-top-right-radius: 5;
}
h3 {
	padding :0px;
}
	

</style>
<table class="border redus text-center">
<thead>
<tr>
    <td COLSPAN="8" class="border">รายรับ</td>
    <td COLSPAN="6" style="color:red; class="border"">รายการจ่าย</td>
    <td COLSPAN="3" class="border">คงเหลือ</td>
</tr>
<tr>
    <td class="border" width="4.75%">วันที่ขาย</td>
    <td class="border" width="4.75%">วัน/เดือน/ปี</td>
    <td class="border" width="6.45%">ใบสำคัญ</td>
    <td class="border" width="4.54%">เลขเครื่อง</td>
    <td class="border" width="11.15%">เลขตัวถัง</td>
    <td class="border" width="3.2%">สี</td>
    <td class="border" width="6.7%">ราคาต้นทุนต่อหน่วย</td>
    <td class="border" width="7.2%">ราคาต้นทุนทั้งสิ้น</td>
    <td style="color:red;" class="border" width="4.75%">วัน/เดือน/ปี</td>
    <td style="color:red;" class="border" width="4.3%">ใบสำคัญจ่าย</td>
    <td style="color:red;" class="border" width="4.5%">เลขเครื่อง</td>
    <td style="color:red;" class="border" width="11.15%">เลขตัวถัง</td>
    <td style="color:red;" class="border" width="3.2%">สี</td>
    <td style="color:red;" class="border" width="6.6%">ราคาต้นทุนต่อหน่วย</td>
    <td class="border"  width="7.3%">ราคาต้นทุนทั้งสิ้น</td>
    <td class="border" width="2.23%">ปริมาณ</td>
    <td class="border">ราคาต้นทุนทั้งสิ้น</td>
</tr>
</thead>
<tbody>
';
$total1=0;
$total2=0;
$num_car=0;
                $result_table=$mysqli->query("SELECT * FROM day WHERE ref!='' AND car_series='".$car_series."' AND `year_month`='$end' AND  branch_name='$name'  ORDER BY day_buy,`statud`,ref ASC");
                while($data_table=$result_table->FETCH_ASSOC()){
                $htmlele .='<tr>';
                    if($data_table['statud']=="buy" || $data_table['statud']=="stock_out"){

                        $htmlele .='<td style="color:red;" class="border">';
                    
                        if($data_table['day_sell']=='0000-00-00'){
                            $htmlele .='';
                            $num_car=$num_car+1;
                        }
                        else{
                        $htmlele .=date("d/m/y",strtotime($data_table['day_sell']));
                        }
                           
                        if($data_table['statud']=="stock_out"){
                            $color="color:#000;";
                        }
                        else{
                            $color="color:#007bff;";
                        }
                    
                    $htmlele .='</td>
                    <td style="'.$color.'" class="border">'.date("d/m/y",strtotime($data_table['day_buy'])).'</td>
                    <td style="'.$color.'" class="border">'.$data_table['ref'].'</td>
                    <td style="'.$color.'" class="border">';
                    $sql_ref="SELECT Machine_Code,Chassis_No,Vehicle_Color,PRICE FROM View_Buy_Car WHERE Inv_No='".$data_table['ref']."'";
                    $query_ref=$conn->prepare($sql_ref);
                    $query_ref->execute();
                    $result_ref = $query_ref->fetch( PDO::FETCH_ASSOC);
                    $htmlele .=$result_ref['Machine_Code'];
                    
                    $htmlele .='
                    </td>
                    <td style="'.$color.'" class="border">'.$result_ref['Chassis_No'].'</td>
                    <td style="'.$color.'" class="border">'.substr($result_ref['Vehicle_Color'],0,3).'</td>
                    <td style="'.$color.'" class="border">'.number_format($result_ref['PRICE'],2).'</td>
                    <td style="'.$color.'" class="border"></td>
                    <td style="color:red;" class="border"></td>
                    <td style="color:red;" class="border"></td>
                    <td style="color:red;" class="border"></td>
                    <td style="color:red;" class="border"></td>
                    <td style="color:red;" class="border"></td>
                    <td style="color:red;" class="border"></td>
                    <td class="border"></td>
                    <td class="border"></td>
                    <td class="border"></td>';
                    number_format($total1=$total1+$result_ref['PRICE'],2);
                    }
                    else{
                        $htmlele .='    
                            <td style="color:red;" class="border"></td>
                            <td class="border"></td>
                            <td class="border"></td>
                            <td class="border"></td>
                            <td class="border"></td>
                            <td class="border"></td>
                            <td class="border"></td>
                            <td class="border"></td>
                            <td style="color:red; height: 25px;" class="border">'.date("d/m/y",strtotime($data_table['day_buy'])).'</td>
                            <td style="color:red; height: 25px;" class="border">'.$data_table['ref'].'</td>
                            <td style="color:red; height: 25px;" class="border">';
                             $sql_eng="SELECT Machine_Code,Chassis_Number FROM View_All_Bill WHERE Ref_Number='".$data_table['ref']."'";
                             $query_eng=$conn->prepare($sql_eng);
                             $query_eng->execute();
                             $result_eng = $query_eng->fetch( PDO::FETCH_ASSOC);
                            
                            
                            $htmlele .=$result_eng['Machine_Code'].'</td>
                            <td style="color:red; height: 25px;" class="border">'.$result_eng['Chassis_Number'].'</td>
                            <td style="color:red; height: 25px;" class="border">';
                             
                            $sql_color="SELECT Vehicle_Color,PRICE FROM View_Buy_Car WHERE Machine_Code='".$result_eng['Machine_Code']."'";
                             $query_color=$conn->prepare($sql_color);
                             $query_color->execute();
                             $result_color = $query_color->fetch( PDO::FETCH_ASSOC);
                            
                            
                            
                            $htmlele .=substr($result_color['Vehicle_Color'],0,3).'</td>
                            <td style="color:red; height: 25px;" class="border">'; 
                            $sql_ref2="SELECT Amount FROM View_All_Bill WHERE Ref_Number='".$data_table['ref']."'";
                            $query_ref2=$conn->prepare($sql_ref2);
                            $query_ref2->execute();
                            $result_ref2 = $query_ref2->fetch( PDO::FETCH_ASSOC);
        
                            $htmlele .=number_format($result_color['PRICE'],2).'</td>
                            <td height: 25px;" class="border"></td>
                            <td class="border"></td>
                            <td class="border"></td>';
                            number_format($total2=$total2+$result_color['PRICE'],2);
                    }    
                    $htmlele .='</tr>
                    
                    ';    
                            }
$htmlele .='
                <tr>
                    <td style="color:red;" class="border"></td>
                        <td class="border"></td>
                        <td class="border"></td>
                        <td class="border"></td>
                        <td class="border"></td>
                        <td class="border"></td>
                        <td class="border"></td>
                        <td class="border"></td>
                        <td style="color:red;" class="border"></td>
                        <td style="color:red;" class="border"></td>
                        <td style="color:red;" class="border"></td>
                        <td style="color:red;" class="border"></td>
                        <td style="color:red;" class="border"></td>
                        <td style="color:red;" class="border"></td>
                        <td class="border"></td>
                        <td class="border"></td>
                        <td class="border"></td>
                </tr>    
                <tr>
                    <td style="color:red;" class="border"></td>
                        <td class="border"></td>
                        <td class="border"></td>
                        <td class="border"></td>
                        <td class="border"></td>
                        <td class="border"></td>
                        <td class="border"></td>
                        <td style="color:#007bff;" class="border">'.number_format($total1,2).'</td>
                        <td style="color:red;" class="border"></td>
                        <td style="color:red;" class="border"></td>
                        <td style="color:red;" class="border"></td>
                        <td style="color:red;" class="border"></td>
                        <td style="color:red;" class="border"></td>
                        <td style="color:red;" class="border"></td>
                        <td style="color:red;" class="border">'.number_format($total2,2).'</td>
                        <td style="" class="border">'.$num_car.' คัน</td>
                        <td style="" class="border">'.number_format($total3=$total1-$total2,2).'</td>
                </tr>
                <tr>
                    <td style="color:red;" class="border"></td>
                        <td class="border"></td>
                        <td class="border"></td>
                        <td class="border"></td>
                        <td class="border"></td>
                        <td class="border"></td>
                        <td class="border"></td>
                        <td class="border"></td>
                        <td style="color:red;" class="border"></td>
                        <td style="color:red;" class="border"></td>
                        <td style="color:red;" class="border"></td>
                        <td style="color:red;" class="border"></td>
                        <td style="color:red;" class="border"></td>
                        <td style="color:red;" class="border"></td>
                        <td class="border"></td>
                        <td class="border"></td>
                        <td class="border"></td>
                </tr> 
                </tbody>                    
</table>
</body>
</html>';
$data1='1234';
?>

<?php
		include("mpdf/mpdf.php");
		/*
		 $mpdf = new mPDF(   '',    // mode - default ''
                '',    // format - A4, for example, default ''
                0,     // font size - default 0
                '',    // default font family
                15,    // margin_left
                15,    // margin right
                58,     // margin top
                60,    // margin bottom
                6,     // margin header
                0,     // margin footer
                'L' );  // L - landscape, P - portrait
		*/
		$mpdf=new mPDF('','A4-L','','' , 5 , 5 , 35 , 10 , 0 , 0 ,'L'); 
		//$stylesheet = file_get_contents('vendor/bootstrap/css/bootstrap.css');
		$mpdf->SetDisplayMode('fullpage');
		$mpdf->list_indent_first_level = 0;  // 1 or 0 - whether to indent the first level of a list
		$mpdf->SetHTMLHeader($header);
        $mpdf->SetHTMLFooter();
		$mpdf->allow_charset_conversion=true;
		$mpdf->charset_in='UTF-8';
		//$mpdf->WriteHTML($stylesheet,1);
		$mpdf->WriteHTML($htmlele);  
		$mpdf->Output(); 
		/* $mpdf->Output('doc/PDF/Quotation_'.$order_id.'_'.$tele.'.pdf','F'); */
?>