<?php
//insert.php
require_once "../magic/session_config.php";
bdr_start_session();
ob_start();
  $branch = $_SESSION["branch"];
  $father = $_SESSION["user_id"];
  $sanad_date = date("Y-m-d");

if($_POST['id2'] !=""){
    $contract_id   = $_POST['id2'];
    $money_in      = $_POST['money_in'];
    $printed_no    = $_POST['printed_no'];
    $payMethod     = $_POST['payMethod'];
    $comm          = $_POST['comm2'];
    $current_km    = $_POST['current_km'];

$data = array();
// $error = array();
include "../actions/dbPDO.php";

$name ='/^[-a-zA-Z0-9_\p{Arabic}-+ـ\s]*$/u';
// $Number ="/^[1-9]\d*(\.\d+)?$/";
$Number ="/^[0-9]+$/";

if(empty($money_in))
    {
      $error["money_in"] = "من فضلك سجل المبلغ ليتم حفظ السند ";
    }
      if(!preg_match($Number,$money_in)){ $error["money_in"] = "من فضلك تأكد من رقم المبلغ" ;}
    if(!empty($printed_no))
      {
        if(!preg_match($name,$printed_no)){ $error["printed_no"] = "من فضلك رقم السند أرقام و حروف فقط" ;}
        }
if(!empty($current_km))
{
  if(!preg_match($Number,$current_km)){ $error["current_km"] = "قراءة العداد الحالي أرقام فقط .. راجع من فض " ;}
}
if(!empty($comm))
{
  if(!preg_match($name,$comm)){ $error["comm"] = "الملاحظات حروف و أرقام فقط ... راجع من فضلك";}
}

if(!empty($error)){
  $data["error"] = $error ;
}else {
  // $contract_id =" 100001" ;
        $payFor = "3";
        $maxsql= $db->prepare("SELECT max(in_id) as maxd FROM invoice");
        $maxsql->execute();
        if($maxsql->rowCount()>0){$row = $maxsql->fetch(PDO::FETCH_ASSOC);
          $maxD = $row['maxd']+1;} else {$maxD = 1 ; }

  $sanad=$db->prepare(" INSERT INTO invoice(in_id , contract_id , printed_no , sanad_date , money_in , payMethod ,
          payFor , comm , father , branch , current_km )
     VALUES( ? , ? , ? , ? , ? , ? , ? ,?, ?, ? ,?)");
   $sanad->bindParam(1,$maxD);
   $sanad->bindParam(2,$contract_id);
   $sanad->bindParam(3,$printed_no);
   $sanad->bindParam(4,$sanad_date);
   $sanad->bindParam(5,$money_in);
   $sanad->bindParam(6,$payMethod);
   $sanad->bindParam(7,$payFor);
   $sanad->bindParam(8,$comm);
   $sanad->bindParam(9,$father);
   $sanad->bindParam(10,$branch);
   $sanad->bindParam(11,$current_km);
   if($sanad->execute()){
  $data["in_id"] = $maxD ;

  $data["msg"]= "<div class='alert alert-success'>
        <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
      <b>تم تسجيل سند القبض بنجاح. </b>
      </div>" ;

      $tot_in = $db->prepare("SELECT sum(money_in) as inn from invoice WHERE contract_id = ? ");
	    $tot_in->bindParam(1,$contract_id);
	     $tot_in->execute();
	      if($tot_in->rowCount()>0){$toty_in = $tot_in->fetch(PDO::FETCH_ASSOC);
	         $data["total_in"] = $toty_in["inn"];
           $total_in         = $toty_in["inn"];}

  $tot_out = $db->prepare("SELECT sum(money_out) as outt from outvoice WHERE contract_id = ? ");
	$tot_out->bindParam(1,$contract_id);
	$tot_out->execute();
	 if($tot_out->rowCount()>0){$toty_out = $tot_out->fetch(PDO::FETCH_ASSOC);
	     $data["total_out"] = $toty_out["outt"];
       $total_out         = $toty_out["outt"];
      } else { $data["total_out"] = "0" ;}

$data["payed"] = $total_in - $total_out ;
  }
    else  { $data["msg"] = "<div class='alert alert-danger'>
            <a href='#' class='close' data-dismiss='alert' aria-label='close'style='float:left;'>&times;</a>
            <b>يوجد خطأ في تسجيل البيانات ... راجع  من فضلك</b>
            </div> " ; }

    //check if the contract is closed or not for updating its status

    include "../op/close_up.php";
    // auto_close($contract_id) ;
}
echo json_encode($data);
}


?>
