<?php
require_once('Database.php');
class Request extends Database{
  public $Id;
  public $UserAccount;
  public $Delivery;
  public $FullAddress;
  public $Certification;
  public $Urgent;
  public $FromLang;
  public $ToLang;
  public $Path;
  public $Status;
  public $EstimatedPrice;
  public $EstimatedDelivery;
  public $PathTraslated;
  public $Words;
  public $InteriorAddress;
  public $City;
  public $State;
  public $Zip;
  public $PaymentToken;

  public function InsertRecord(){
    $Query = "INSERT INTO Request (UserAccount, Delivery, FullAddress, Certification, Urgent, FromLang, ToLang, Path, Status, EstimatedPrice, SubmissionDate, Words, InteriorAddress, City, State, Zip)
                            VALUES('$this->UserAccount','$this->Delivery','$this->FullAddress',$this->Certification,$this->Urgent,$this->FromLang,$this->ToLang,'$this->Path',1,0, NOW(), $this->Words, '$this->InteriorAddress', '$this->City', '$this->State', '$this->Zip')";
                            
                            //echo $Query;
    return $this->ExecuteQueryReturnId($Query);
  }

  public function UpdateEstimation(){
    $Query = "UPDATE Request SET EstimatedPrice = $this->EstimatedPrice WHERE Id = $this->Id";
    //echo $Query;
    return $this->ExecuteQuery($Query);
  }

  public function UpdateToken($token){
    $Query = "UPDATE Request SET PaymentToken = '$token' WHERE Id = $this->Id";
    //echo $Query;
    return $this->ExecuteQuery($Query);
  }

  public function UpdateFilePath(){
    $Query = "UPDATE Request SET PathTraslated = '$this->PathTraslated', DeliveryDate = NOW() WHERE Id = $this->Id";
    //echo $Query;
    return $this->ExecuteQuery($Query);
  }

  public function UpdateStatus(){
    $Query = "UPDATE Request SET Status = $this->Status WHERE Id = $this->Id";
    //echo $Query;
    return $this->ExecuteQuery($Query);
  }

  public function UpdateEstimatedDelivery(){
    $Query = "UPDATE Request SET EstimatedDelivery = \"$this->EstimatedDelivery\" WHERE Id = $this->Id";
    //echo $Query;
    return $this->ExecuteQuery($Query);
  }

    public function GetRecords(){
        $Query = "SELECT *  FROM Request WHERE Status = 1";
        return $this->ExecuteReader($Query);
    }

  public function GetRecord(){
    $Query = "SELECT *  FROM Request WHERE Id = $this->Id";
    return $this->ExecuteUniqueReader($Query);
  }

    public function GetRecordsByUserAccount($status=""){
    $added = "";
    if($status!=""){
      $added = " AND Status = $status";
    }
      $Query = "SELECT *  FROM Request WHERE UserAccount = $this->UserAccount $added";
      //echo $Query;
      return $this->ExecuteReader($Query);
    }

  public function GetRecordsGlobal($status=""){
    $added = "";
    if($status!=""){
      $added = " WHERE Status = $status";
    }
    if($status=="*"){
      $added = " WHERE Status <> 2";
    }
    $Query = "SELECT *  FROM Request $added";
    //echo $Query;
    return $this->ExecuteReader($Query);
  }

  public function GetHooks(){
    $Query = "SELECT *  FROM Request WHERE Status <> 0";
    //echo $Query;
    return $this->ExecuteReader($Query);
  }

    public function GetCountByDates(){
      $Query = "SELECT DATE_FORMAT(SubmissionDate, '%Y-%m') AS Submission, COUNT(1) AS Total  FROM Request 
                    WHERE UserAccount = $this->UserAccount AND SubmissionDate IS NOT NULL
                    GROUP BY Submission ";
      return $this->ExecuteReader($Query);
    }

  public function GetCountAccountByDates(){
    $Query = "SELECT DATE_FORMAT(SubmissionDate, '%Y-%m') AS Submission, COUNT(1) AS Total  FROM Request 
                    WHERE SubmissionDate IS NOT NULL
                    GROUP BY Submission ";
    return $this->ExecuteReader($Query);
  }
}
