<?php
 class Log{
    private $db;
    private $contract_id ;
    private $father ;
    private $branch ;
    public function __construct($db,$contract_id,$father,$branch)
    {
        $this->db = $db;
        $this->contract_id = $contract_id;
        $this->father      = $father    ;
        $this->branch      = $branch    ;
    }
    public function fatherName(){
        $db          = $this->db;
        $father      = $this->father;
        $f = $db->prepare("SELECT realName FROM users where user_id = $father LIMIT 1");
        $f->execute();
        if($f->rowCount() > 0){
            $row = $f->fetch(PDO::FETCH_ASSOC);
            return $row['realName']        ;
        }else{
            return "no body" ;
        }
    }
    public function branchName($branch){
        $db          = $this->db;
        $b = $db->prepare("SELECT b_name FROM branches where branchID = $branch LIMIT 1");
        $b->execute();
        if($b->rowCount() > 0){
            $row = $b->fetch(PDO::FETCH_ASSOC);
            return $row['b_name']        ;
        }else{
            return "no branch" ;
        }
    }
    public function contract(){
        $db          = $this->db;
        $contract_id = $this->contract_id;
        $b = $db->prepare("SELECT * FROM contracts where contract_id = $contract_id LIMIT 1");
        $b->execute();
        if($b->rowCount() > 0){
            $row = $b->fetch(PDO::FETCH_ASSOC);
            return $row ;
        }else{
            return "no branch" ;
        }
    }
    public function car($carid){
        $db          = $this->db;
        $carinfo = "لا توجد سيارة";
        $ca = $db->prepare("SELECT * FROM cars where carid = $carid LIMIT 1");
        $ca->execute();
        if($ca->rowCount() > 0){
            $row = $ca->fetch(PDO::FETCH_ASSOC);
            $kind = $row['kind'] ;
            $model = $row['model'];
            $PL = $row['letters']." ".$row['numbers'];
            $carinfo = $kind." ".$PL." موديل ".$model ;
        }
        return $carinfo ;
        
    }
   function newCont($car){
        $db = $this->db ;
        $contract_id = $this->contract_id ;
        $father      = $this->father ;
        $branch      = $this->branch   ;
        $fatherName = $this->fatherName();
        $branchName = $this->branchName($branch);
        $contract   = $this->contract();
        $carinfo    = $this->car($car);
        $smart_comm = "تم فتح عقد ".$carinfo." إيجار يومي ".$contract["day_cost"]." الكيلو اليومي " .$contract["free_kilo"] ." فرع ".$branchName." موظف ".$fatherName;
        $new = $db->prepare("INSERT INTO `log` (`logid`, `father`, `contract_id`, `branch`, `smart_com`, `audate`) 
                    VALUES (NULL, ?, ?, ?, ?, current_timestamp())");
        $new->bindParam(1,$father);
        $new->bindParam(2,$contract_id);
        $new->bindParam(3,$branch);
        $new->bindParam(4,$smart_comm);
        $new->execute(); 
   }
   function upCont($first,$next,$last,$kind){
    $db = $this->db ;
    $contract_id = $this->contract_id ;
    $father      = $this->father ;
    $branch      = $this->fatherBranch();
    $fatherName = $this->fatherName();
    $branchName = $this->branchName($branch);
    $contract   = $this->contract();
    $car        = $contract['carid'];
    $carinfo    = $this->car($car);

    switch ( $kind) {
        case '1':      $smart_comm  = " تم تعديل عقد ".$contract_id." إيجار يومي ".$first." ثم " .$next ." أخير " .$last ." ".$carinfo." فرع ".$branchName." موظف ".$fatherName;      
                break;
        case '2':      $smart_comm  = " تم تعديل عقد ".$contract_id." تعويضات ".$first." ثم " .$next ." أخير " .$last ." ".$carinfo." فرع ".$branchName." موظف ".$fatherName;
             break;
        case '3':      $smart_comm  = " تم خصم علي عقد ".$contract_id."  ".$first." ثم " .$next ." أخير " .$last ." ".$carinfo." فرع ".$branchName." موظف ".$fatherName; 
              break;
        
    }
    $new = $db->prepare("INSERT INTO `log` (`logid`, `father`, `contract_id`, `branch`, `smart_com`, `audate`) 
                VALUES (NULL, ?, ?, ?, ?, current_timestamp())");
    $new->bindParam(1,$father);
    $new->bindParam(2,$contract_id);
    $new->bindParam(3,$branch);
    $new->bindParam(4,$smart_comm);
    $new->execute(); 
}
   function sanadPOS($in_id,$money_in,$pay){
    $db = $this->db ;
    $contract_id = $this->contract_id ;
    $father      = $this->father ;
    $branch      = $this->branch   ;
    $fatherName = $this->fatherName();
    $branchName = $this->branchName($branch);
    switch ( $pay) {
        case '1':      $payMethod  = "كاش";             break;
        case '2':      $payMethod  = "تحويل الراجحي";  break;
        case '3':      $payMethod  = "نقاط بيع";       break;
        case '4':      $payMethod  = "فواتير";         break;
        case '5':      $payMethod  = "تحويل الأهلي";    break;
    }
    $smart_comm = $in_id." تم سداد ".$money_in."  ".$payMethod." عقد " .$contract_id ." فرع ".$branchName." موظف ".$fatherName;
    $new = $db->prepare("INSERT INTO `log` (`logid`, `father`, `contract_id`, `branch`, `smart_com`, `audate`) 
                VALUES (NULL, ?, ?, ?, ?, current_timestamp())");
    $new->bindParam(1,$father);
    $new->bindParam(2,$contract_id);
    $new->bindParam(3,$branch);
    $new->bindParam(4,$smart_comm);
    $new->execute(); 
}
function sanadUpdate($in_id,$money_in,$pay,$last){
    $db = $this->db ;
    $contract_id = $this->contract_id ;
    $father      = $this->father ;
    $branch      = $this->branch   ;
    $fatherName = $this->fatherName();
    $branchName = $this->branchName($branch);
    switch ( $pay) {
        case '1':      $payMethod  = "كاش";             break;
        case '2':      $payMethod  = "تحويل الراجحي";  break;
        case '3':      $payMethod  = "نقاط بيع";       break;
        case '4':      $payMethod  = "فواتير";         break;
        case '5':      $payMethod  = "تحويل الأهلي";    break;
    }
    $smart_comm = $in_id." تم تعديل سند من  ".$last." إلي ".$money_in."  ".$payMethod." عقد " .$contract_id ." فرع ".$branchName." موظف ".$fatherName;
    $new = $db->prepare("INSERT INTO `log` (`logid`, `father`, `contract_id`, `branch`, `smart_com`, `audate`) 
                VALUES (NULL, ?, ?, ?, ?, current_timestamp())");
    $new->bindParam(1,$father);
    $new->bindParam(2,$contract_id);
    $new->bindParam(3,$branch);
    $new->bindParam(4,$smart_comm);
    $new->execute(); 
}
function outUpdate($out_id,$money_out,$last){
    $db = $this->db ;
    $contract_id = $this->contract_id ;
    $father      = $this->father ;
    $branch      = $this->branch   ;
    $fatherName = $this->fatherName();
    $branchName = $this->branchName($branch);
    $smart_comm = $out_id." تم تعديل سند صرف من  ".$last." إلي ".$money_out." عقد " .$contract_id ." فرع ".$branchName." موظف ".$fatherName;
    $new = $db->prepare("INSERT INTO `log` (`logid`, `father`, `contract_id`, `branch`, `smart_com`, `audate`) 
                VALUES (NULL, ?, ?, ?, ?, current_timestamp())");
    $new->bindParam(1,$father);
    $new->bindParam(2,$contract_id);
    $new->bindParam(3,$branch);
    $new->bindParam(4,$smart_comm);
    $new->execute(); 
}
function fatherBranch(){
        $db          = $this->db;
        $father      = $this->father;
        $b = $db->prepare("SELECT branch FROM users where user_id = $father LIMIT 1");
        $b->execute();
        if($b->rowCount() > 0){
            $row = $b->fetch(PDO::FETCH_ASSOC);
            return $row['branch']   ;
        }else{
            return 0 ;
        }
}
    
 }
?>