<?php
if (!isset($_SESSION)) {
    session_start();
}
ob_start();
if (!isset($_SESSION["user_id"])) {
    header("location:../logout.php");
}
$father = $_SESSION["user_id"];
if (!empty($_POST['contract_id'])) {
    include "../actions/dbPDO.php";
    include "../op/wanted.php";
    $contract_id = $_POST['contract_id'];
    $end         = $_POST['end'];
    if (empty($end)) {
        $end = date("Y-m-d");
    }
    $vat_id;
    $maxID = $db->prepare("SELECT max(vat_id) as vatMax from vat where contract_id = ?");
    $maxID->bindParam(1, $contract_id);
    $maxID->execute();
    if ($maxID->rowCount() > 0) {
        $dow = $maxID->fetch(PDO::FETCH_ASSOC);
        $vat_id = $dow['vatMax'];
    } 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>";
        exit();
    }
    $del = $db->prepare("DELETE FROM vat where vat_id = $vat_id");
    $del->execute();

    $last_sql = $db->prepare("SELECT max(end) as last from vat where contract_id = ?");
    $last_sql->bindParam(1, $contract_id);
    $last_sql->execute();
    if ($last_sql->rowCount() > 0) {
        $segl = $last_sql->fetch(PDO::FETCH_ASSOC);
        $last = $segl['last'];
    }
    $cont_close = $db->prepare("SELECT date_in from contracts where contract_id = ?");
    $cont_close->bindParam(1, $contract_id);
    $cont_close->execute();
    if ($cont_close->rowCount() > 0) {
        $bozz = $cont_close->fetch(PDO::FETCH_ASSOC);
        $closed = $bozz['date_in'];
    }
    $last = date('Y-m-d', strtotime($last));
    $end2 = date('Y-m-d', strtotime($end));
    if (!empty($closed)) {
        $closed = date('Y-m-d', strtotime($closed));
        if ($end2 > $closed) {
            echo "<div class='alert alert-danger'>
             <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
             <b>العقد مغلق قبل نهاية الفترة المحددة ... انتبه من فضلك</b>
         </div>";
            exit();
        }
    }
    if ($end2 <= $last) {
        echo "<div class='alert alert-danger'>
           <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
           <b> تم عمل فاتورة ضريبة لهذة الفترة من قبل  ... راجع من فضلك </b>
       </div>";
        exit();
    }
    $wan           = new Wanted($db, $contract_id);
    $sam           = $wan->vatInvoice($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'];
    if (empty($deduction)) {
        $deduction = 0;
    }
    $total_rent = $days_cost + $hours_cost + $kilos_cost;

    $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, $vat_id);
    $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>";
    }
}
