<?php
 include "log.php";
 class ContHistory{
    
    private $db;
    private $contract_id ;
    private $father;
    private $log;
    public function __construct($db,$contract_id,$father)
    {
       
        $this->db = $db;
        $this->contract_id = $contract_id;
        $this->father = $father;
        $branch = 0;
        $log = new Log($db,$contract_id,$father,$branch);
        $this->log = $log ;
    }

    function checkHistory($day_cost,$kilo_cost,$addition_cost,$violations,$ins_claim,$deduction)
    {
        $log = $this->log;
        $row = ContHistory::last();
        $first = ContHistory::firstDay();
        $new = false;
        if(!empty($row)){
            if($day_cost != $row["day_cost"]) {
                 $new = true;
                    $log->upCont($first['day_cost'],$row['day_cost'],$day_cost,1);
                }
            if($kilo_cost != $row["kilo_cost"]) { $new = true;}
            if($addition_cost != $row["addition_cost"]) {
                 $new = true;
                 $log->upCont($first['addition_cost'],$row['addition_cost'],$addition_cost,2);
                }
            if($violations != $row["violations"]) { $new  = true;}
            if($ins_claim != $row["ins_claim"]) { $new = true;}
            if($deduction != $row["deduction"]) { 
                $new = true;
                if($deduction > 0){
                    $log->upCont($first['deduction'],$row['deduction'],$deduction,3);
                }   
            }
        }else{$new = true ;}
       
        return $new ;
    }
    public function last(){
        $db          = $this->db;
        $contract_id = $this->contract_id;

        $last = $db->prepare("SELECT * FROM contractsback where contract_id = $contract_id ORDER BY id DESC LIMIT 1");
        $last->execute();
        if($last->rowCount() > 0){
            $row = $last->fetch(PDO::FETCH_ASSOC);        
        }
        return $row ;
    }
    function create($day_cost,$kilo_cost,$addition_cost,$violations,$ins_claim,$deduction,$father){
        $db          = $this->db;
        $contract_id = $this->contract_id;

        $back = $db->prepare("INSERT INTO contractsback(father, contract_id, day_cost, kilo_cost, addition_cost, violations, ins_claim, deduction)
        VALUES  (?,?,?,?,?,?,?,?)");
        $back->bindParam(1,$father);
        $back->bindParam(2,$contract_id);
        $back->bindParam(3,$day_cost);
        $back->bindParam(4,$kilo_cost);
        $back->bindParam(5,$addition_cost);
        $back->bindParam(6,$violations);
        $back->bindParam(7,$ins_claim);
        $back->bindParam(8,$deduction);
        $back->execute(); 
    }
    function firstDay(){
        $db          = $this->db;
        $contract_id = $this->contract_id;
       
        $sql = $db->prepare("SELECT * from contractsback where contract_id = $contract_id
                ORDER BY id LIMIT 1");
        $sql->execute();
        if($sql->rowCount()> 0){
            $row = $sql->fetch(PDO::FETCH_ASSOC);
        }
        return $row;
    }
    function firstAddition(){
        $db          = $this->db;
        $contract_id = $this->contract_id;

        $sql = $db->prepare("SELECT * from contractsback where contract_id = $contract_id
                AND addition_cost > 0 ORDER BY id LIMIT 1");
        $sql->execute();
        if($sql->rowCount()> 0){
            $row = $sql->fetch(PDO::FETCH_ASSOC);
        }
        return $row;
    }

 }
?>