<?php 
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
include('../include/auth.php');
include('../include/lskfjwfe.php');
include('../include/header.php');
$user=$userid;
if (isset($_POST['contract_num'])) {
        //variables to hold our submitted data with post     
       
        $fid=$_POST['fid'];
        $dateTimeZone = new DateTimeZone("Europe/Athens");
        $dateTime = new DateTime("now", $dateTimeZone);
        $timeOffset = $dateTimeZone->getOffset($dateTime);
        $date = time() + $timeOffset;
        $contract_num = $_POST['contract_num'];
        $station_id=$_POST['station_id'];
        $employee_id = $_POST['employee_id'];
        $Res_ref =$_POST['Res_ref'];
        $Res_value =$_POST['Res_value'];
        $Contract_value = $_POST['Contract_value'];
        $extras_sold = $_POST['extras_sold'];
        $cidpayment = $_POST['cidpayment'];					 
        $deposit = $_POST['deposit'];
        $ctotal_paid = $_POST['ctotal_paid'];
        $vehfleet_id=$_POST['vehfleet_id'];
        $cont_fuel_out = $_POST['cont_fuel_out'];
        $cont_fuel_in = $_POST['cont_fuel_in'];					 
        $cont_km_out = $_POST['cont_km_out'];
        $cont_km_in = $_POST['cont_km_in'];
        $cont_damage_out = $_POST['cont_damage_out'];
        $cont_damage_in = $_POST['cont_damage_in'];      
        $dateadded = $_POST['dateadded'];
        $cont_codriver = $_POST['cont_codriver'];
        $cont_copport= $_POST['cont_copport'];
        $cont_colicense  = $_POST['cont_colicense'];
        $cont_notes = $_POST['cont_notes'];
        $cont_return = $_POST['cont_return'];
        $cont_return= \DateTime::createFromFormat('d-m-Y H:i', $cont_return)->getTimestamp(); 
        $action="Added";
        $item="Contract";
        $details="New Contract Added Number: ".$contract_num;  
        $status="contract";
        $conttype=$_POST['conttype'];
        if($conttype!='0'){
       
        $stprefix=$_POST['stprefix'];
        $cnum=$_POST['cnum'];
        }
          $db3->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
         
        try {
            $db3->beginTransaction();          
        
            $sh = $db3->prepare("INSERT INTO contracts (contract_id, contract_num, station_id, employee_id, Res_ref, Res_value, Contract_value, extras_sold, cidpayment, deposit, ctotal_paid, vehfleet_id, cont_fuel_out, cont_fuel_in, cont_km_out, cont_km_in, cont_damage_out, cont_damage_in, dateadded, cont_codriver, cont_copport, cont_colicense, cont_notes, cont_return) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )");
            $sh->execute([NULL, $contract_num, $station_id, $employee_id, $Res_ref, $Res_value, $Contract_value, $extras_sold, $cidpayment, $deposit, $ctotal_paid, $vehfleet_id, $cont_fuel_out, $cont_fuel_in, $cont_km_out, $cont_km_in, $cont_damage_out, $cont_damage_in, $dateadded, $cont_codriver, $cont_copport, $cont_colicense, $cont_notes, $cont_return]);

            $sh = $db3->prepare("UPDATE jostj_vikrentcar_orders SET status=? WHERE id=?");
            $sh->execute([$status, $fid]);

            $sh = $db3->prepare("INSERT INTO reservation_log (reservation_log_id, reservation_log_res, reservation_log_user, reservation_log_date, reservation_log_action, reservation_log_item, reservation_log_details) VALUES (?, ?, ?,?, ?, ?, ? )");
            $sh->execute([NULL, $fid, $user, $date, $action, $item, $details]);

            if($conttype!='0'){
                $sh = $db3->prepare("INSERT INTO econtract (econtract_id, econtract_num, econtract_statid, econtract_prefix) VALUES (?, ?, ?, ?)");
                $sh->execute([NULL, $cnum, $station_id, $stprefix]);
            }

            $db3->commit();
            echo "<div class='alert alert-success'>Record Updated Succesfully</div><script>"; echo "window.location.replace('../reservation.php?id=$fid');";
            echo "</script>";   
        exit;
        } catch ( PDOException  $e ) {
            $db3->rollBack();
            echo "<div class='alert alert-danger'>Error updating record: </div>" . $e->getMessage();
        }
        $db3= null;
    }
    ?>      