<?php
if (!isset($_SESSION)) {session_start();  }
ob_start();
// if(!isset($_SESSION["user_id"])){header("location:../logout.php");}
// $father = $_SESSION["user_id"];
$father = 3 ;
  require_once "../magic/DataBase.php";
  require_once "../magic/VatBase.php";
	$dataBase = new DataBase();
	$conn = $dataBase->getConnection();

    $vatBase = new VatBase();
	$vatconn = $vatBase->getConnection();

    function clientInfo(int $contract_id, PDO $conn) : array {
        $info =array();

        $sql = $conn->prepare("SELECT customers.StreetName , customers.BuildingNumber , customers.PlotIdentification , customers.PostalNumber ,
                 customers.SubDivisionName,customers.CityName , customers.cust_name as ClientName ,customers.cust_vat as VatNumber
                 from customers , contracts 
                 where contracts.cust_id = customers.cust_id 
                 AND contracts.contract_id = ?");
        $sql->bindParam(1, $contract_id);
        $sql->execute();
        $info = $sql->fetch();

        return $info;
    }

    // if(isset($_POST['myday'])){
        // $myday      = $_POST['myday'] ; 
        $myday ="2023-01-05";
        $first_day  = date('Y-m-01', strtotime($myday));
        $last_day 	= date('Y-m-t', strtotime($myday));

        $output = array();

        $oldVAT = array();
        $pre = $vatconn->prepare("SELECT IRN from prevat");
        $pre->execute();
        while ($row = $pre->fetch()) {
            $oldVAT[] = $row['IRN'];
        }
        $oldPreVAT ="1,2";
        if(!empty($oldVAT)){
            $oldPreVAT = implode(',', $oldVAT);
        }       
        
        $sql = $conn->prepare("SELECT * from vat where    vat_id NOT IN ($oldPreVAT) ORDER BY vat_id DESC    LIMIT 3");
        // $sql->bindParam(1,$first_day);
        // $sql->bindParam(2,$last_day);
        $sql->execute();

        if ($sql->rowCount() > 0) {
            while ($row = $sql->fetch()) {
                $contract_id = $row['contract_id'];
                $output = clientInfo($contract_id, $conn);
                $output['contract_id'] = $contract_id ;
                $output['IRN'] = $row['vat_id'] ;
                $output['DeliveryDateTime'] = $row['end'] ;
                $output['paymentType'] = 1 ;
                $output['LineExtensionAmount'] = $row['net_cost'] ;
                $output['TaxExclusiveAmount'] = $row['net_cost'] ;
                $output['TaxInclusiveAmount'] = $row['after_vat'] ;
                $output['AllowanceTotalAmount'] = $row['deduction'] ;
                $output['PrepaidAmount'] = 0 ;
                $output['PayableAmount'] = $row['after_vat'] ;
                $output['TaxTotal'] = $row['vat_req'] ;
                $output['TaxableAmount'] = $row['net_cost'] ;
                $output['TaxAmount'] = $row['vat_req'] ;
                $output['TaxPercentage'] = 15.0 ;
                $output['TaxAmount'] = $row['vat_req'] ;
                $output['AllowanceChargeAmount'] = $row['deduction'] ;
                $output['AllowanceChargeTaxPercentage'] = 15.0 ;

                $ins = $vatconn->prepare("INSERT INTO `prevat`(`IRN`, `contract_id`, `VatNumber`, `StreetName`, `BuildingNumber`,
                 `PlotIdentification`, `SubDivisionName`, `CityName`, `PostalNumber`, `ClientName`, `DeliveryDateTime`, `paymentType`,  
                `InvoiceID`, `LineExtensionAmount`, `TaxInclusiveAmount`, `TaxExclusiveAmount`, 
                `AllowanceTotalAmount`, `PrepaidAmount`, `PayableAmount`, `TaxTotal`, `TaxableAmount`, `TaxAmount`, 
                `TaxPercentage`,  `AllowanceChargeAmount`, `AllowanceChargeTaxPercentage`, `father`) 
                VALUES (?,? , ? ,? ,? , ? ,? , ?  ,? , ? ,?,? , ? ,? ,? , ? ,?,? , ? ,? ,? , ? ,?,? , ? ,? ) ");
                $ins->bindParam(1, $output['IRN']);
                $ins->bindParam(2, $output['contract_id']);
                $ins->bindParam(3, $output['VatNumber']);
                $ins->bindParam(4, $output['StreetName']);
                $ins->bindParam(5, $output['BuildingNumber']);
                $ins->bindParam(6, $output['PlotIdentification']);
                $ins->bindParam(7, $output['SubDivisionName']);
                $ins->bindParam(8, $output['CityName']);
                $ins->bindParam(9, $output['PostalNumber']);
                $ins->bindParam(10, $output['ClientName']);
                $ins->bindParam(11, $output['DeliveryDateTime']);
                $ins->bindParam(12, $output['paymentType']);
                $ins->bindParam(13, $output['IRN']);
                $ins->bindParam(14, $output['LineExtensionAmount']);
                $ins->bindParam(15, $output['TaxInclusiveAmount']);
                $ins->bindParam(16, $output['TaxExclusiveAmount']);
                $ins->bindParam(17, $output['AllowanceTotalAmount']);
                $ins->bindParam(18, $output['PrepaidAmount']);
                $ins->bindParam(19, $output['PayableAmount']);
                $ins->bindParam(20, $output['TaxTotal']);
                $ins->bindParam(21, $output['TaxableAmount']);
                $ins->bindParam(22, $output['TaxAmount']);
                $ins->bindParam(23, $output['TaxPercentage']);
                $ins->bindParam(24, $output['AllowanceChargeAmount']);
                $ins->bindParam(25, $output['AllowanceChargeTaxPercentage']);
                $ins->bindParam(26, $father);
                if($ins->execute()){
                    $line = $vatconn->prepare("INSERT INTO `invoiceline`( `LineName`, `LinePrice`, `LineQuantity`, `LineSubTotal`, `LineTaxTotal`, `LineNetTotal`,`invoiceID`) 
                                        VALUES (?,?,?,?,?,?,?)");
                    $days_cost = $row['days_cost'];
                    $kilos_cost = $row['kilos_cost'];
                    $hours_cost = $row['hours_cost'];
                    $addtion_cost = $row['addtion_cost'];
                    if($days_cost > 0){
                        $LineName = "قمية إيجار";
                        $LineTaxTotal = round(($days_cost * 0.15),2);
                        $LineNetTotal =  $LineTaxTotal+$days_cost ;
                        $line->bindParam(1,$LineName );
                        $line->bindParam(2, $row['day_cost']);
                        $line->bindParam(3, $row['days_qty']);
                        $line->bindParam(4, $days_cost);
                        $line->bindParam(5, $LineTaxTotal);
                        $line->bindParam(6, $LineNetTotal);
                        $line->bindParam(7,$output['IRN']);
                        $line->execute();
                    }
                    if($kilos_cost > 0){
                        $LineName = "كيلومتر زائد";
                        $LineTaxTotal = round(($kilos_cost * 0.15),2);
                        $LineNetTotal =  $LineTaxTotal+$kilos_cost ;
                        $kilo_qty = $row['kilo_qty'];
                        $kilo_cost = round(($kilos_cost / $kilo_qty),2);
                        $line->bindParam(1,$LineName );
                        $line->bindParam(2, $kilo_cost);
                        $line->bindParam(3, $kilo_qty);
                        $line->bindParam(4, $kilos_cost);
                        $line->bindParam(5, $LineTaxTotal);
                        $line->bindParam(6, $LineNetTotal);
                        $line->bindParam(7,$output['IRN']);
                        $line->execute();
                    }
                    if($hours_cost > 0){
                        $LineName = "ساعات زائدة";
                        $LineTaxTotal = round(($hours_cost * 0.15),2);
                        $LineNetTotal =  $LineTaxTotal+$hours_cost ;
                        $hour_qty = $row['hour_qty'];
                        $hour_cost = round(($hours_cost / $hour_qty),2);
                        $line->bindParam(1,$LineName );
                        $line->bindParam(2, $hour_cost);
                        $line->bindParam(3, $hour_qty);
                        $line->bindParam(4, $hours_cost);
                        $line->bindParam(5, $LineTaxTotal);
                        $line->bindParam(6, $LineNetTotal);
                        $line->bindParam(7,$output['IRN']);
                        $line->execute();
                    }
                    if($addtion_cost > 0){
                        $LineName = "تعويض إضافي";
                        $LineTaxTotal = round(($addtion_cost * 0.15),2);
                        $LineNetTotal =  $LineTaxTotal+$addtion_cost ;
                        $qty = "1";

                        $line->bindParam(1,$LineName );
                        $line->bindParam(2, $addtion_cost);
                        $line->bindParam(3, $qty);
                        $line->bindParam(4, $addtion_cost);
                        $line->bindParam(5, $LineTaxTotal);
                        $line->bindParam(6, $LineNetTotal);
                        $line->bindParam(7,$output['IRN']);
                        $line->execute();
                    }
                }
            }
        }
    // }