<?php
include "../actions/dbPDO.php";
include "../op/co_wanted.php";
$counter = 1 ;
$cont = $db->prepare("SELECT contract_id , date_in  from contracts
          where date_in between '2019-01-1' AND '2019-01-2' order by date_in , time_in ");
$cont->execute();
if($cont->rowCount()>0){
 while($boz = $cont->fetch())
        {$end         = $boz['date_in'];
        $contract_id  = $boz['contract_id'];
        $sam 					=   vat_invoice($contract_id , $end);
        $cont_status		= $sam["cont_status"] ;

        $deduction     = $sam["deduction"] ;
        $days_cost 	  = $sam["days_cost"] ;
        $start   			= $sam["date_out"] ;
        $days_qty   		= $sam["days_qty"] ;
        $day_cost			= $sam["day_cost"];
        $kilos_cost    = $sam["kilos_cost"];
        $kilo_cost			= $sam["kilo_cost"];
        $kilo_qty		  = $sam["kilo_qty"];
        $hours_cost    = $sam["hours_cost"] ;
        $hour_qty		  = $sam["hour_qty"] ;
        $hour_cost			= $sam["hour_cost"];
        $addtion_cost  = $sam["addtion_cost"];
        $violations    = $sam["violations"] ;
        $net_cost      = $sam["net_cost"] ;
        $vat_req       = $sam["vat_req"]	 ;
        $after_vat     = $sam["after_vat"]	;
        $addtion_desc  = $sam['addtion_desc'];
        $total_rent = $days_cost + $hours_cost + $kilos_cost ;
        echo $start." ".$days_qty;

        $maxsql= $db->prepare("SELECT max(vat_id) as maxd FROM vat");
        $maxsql->execute();
        if($maxsql->rowCount()>0){$row = $maxsql->fetch(PDO::FETCH_ASSOC);
        $maxD = $row['maxd']+1;} else {$maxD = 19000 ; }

        $new_vat = $db->prepare("INSERT INTO `vat` (`vat_id`, `contract_id`, `cont_status`, `start`, `end`, `days_qty`, `day_cost`, `days_cost`,
                    `kilo_qty`, `kilos_cost`, `hour_qty`, `hours_cost`, `total_rent`, `addtion_cost`, `violations`, `deduction`, `net_cost`, `vat_req`, `after_vat`,  `father`,addtion_desc)
                      VALUES (?, ?, ?, ?, ?,?, ?, ?, ?, ?,?, ?, ?, ?, ?,?, ?, ?, ?, ?,?)");
        $new_vat->bindParam(1,$maxD);
        $new_vat->bindParam(2,$contract_id);
        $new_vat->bindParam(3,$cont_status);
        $new_vat->bindParam(4,$start);
        $new_vat->bindParam(5,$end);
        $new_vat->bindParam(6,$days_qty);

        $new_vat->bindParam(7,$day_cost);
        $new_vat->bindParam(8,$days_cost);
        $new_vat->bindParam(9,$kilo_qty);
        $new_vat->bindParam(10,$kilos_cost);
        $new_vat->bindParam(11,$hour_qty);
        $new_vat->bindParam(12,$hours_cost);

        $new_vat->bindParam(13,$total_rent);
        $new_vat->bindParam(14,$addtion_cost);
        $new_vat->bindParam(15,$violations);
        $new_vat->bindParam(16,$deduction);
        $new_vat->bindParam(17,$net_cost);
        $new_vat->bindParam(18,$vat_req);

        $new_vat->bindParam(19,$after_vat);
        $new_vat->bindParam(20,$father);
        $new_vat->bindParam(21,$addtion_desc);
        if($new_vat->execute()){
        echo "<div class='alert alert-success'>
         <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
         <b> تم إصدار فاتورة ضريبية بنجاح </b>
        </div>";
        }else{
        echo "<div class='alert alert-danger'>
             <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
             <b>يوجد خطأ في تسجيل البيانات ... راجع  من فضلك</b>
         </div>";
        }
       }
       echo $counter;}
?>
