--- /dev/null
+<?php
+
+class PdoContractManager extends AbstractPdoManager {
+
+ public function getNbContrat(){
+ $sql = "SELECT count(*) FROM contracts";
+ return $this->queryOne($sql);
+ }
+
+ public function getContractsForOutput($offset,$limit){
+ $sql = "SELECT id,number,state,start_date,end_date FROM contracts " .
+ " ORDER BY number " .
+ " LIMIT $offset,$limit ";
+ return $this->query($sql);
+ }
+
+ public function getBySugarId($sugar_id){
+ $sql = "SELECT * FROM contracts WHERE sugar_id=?";
+ $result = $this->queryOne($sql,$sugar_id);
+ if (! $result){
+ return false;
+ }
+ $customer = new Contract();
+ $customer->importFromArray($result);
+ return $customer;
+ }
+
+ public function edit(Contract $contract){
+ if ($contract->id){
+ $this->updateContract($contract);
+ return $contract->id;
+ } else {
+ return $this->createContract($contract);
+ }
+ }
+
+ public function createContract(Contract $contract) {
+ $query = $this -> pdo -> prepare(
+ 'INSERT INTO contracts (
+ number,
+ state,
+ start_date,
+ end_date,
+ sugar_id,
+ customer_id
+ ) VALUES (
+ :number,
+ :state,
+ :start_date,
+ :end_date,
+ :sugar_id,
+ :customer_id
+ );'
+ );
+ $query -> bindValue(':number', $contract -> number);
+ $query -> bindValue(':state', $contract -> state);
+ $query -> bindValue(':start_date', $contract -> start_date);
+ $query -> bindValue(':end_date', $contract -> end_date);
+ $query -> bindValue(':sugar_id', $contract -> sugar_id);
+ $query -> bindValue(':customer_id', $contract -> customer_id);
+ $query -> execute();
+ return $this->lastInsertId();
+ }
+
+ public function updateContract(Contract $contract) {
+
+ $query = $this -> pdo -> prepare(
+ 'UPDATE contracts
+ SET number = :number,
+ state = :state,
+ start_date = :start_date,
+ end_date = :end_date,
+ sugar_id = :sugar_id,
+ customer_id = :customer_id
+ WHERE id = :id'
+ );
+ $query -> bindValue(':id', $contract -> id);
+ $query -> bindValue(':number', $contract -> number);
+ $query -> bindValue(':state', $contract -> state);
+ $query -> bindValue(':start_date', $contract -> start_date);
+ $query -> bindValue(':end_date', $contract -> end_date);
+ $query -> bindValue(':sugar_id', $contract -> sugar_id);
+ $query -> bindValue(':customer_id', $contract -> customer_id);
+ $query -> execute();
+ $query->closeCursor();
+ }
+
+ private function getContractByResult($result){
+ $contract = new Contract();
+ if (! $result){
+ return $contract;
+ }
+ $contract->importFromArray($result);
+ return $contract;
+
+ }
+
+ public function getContract($id) {
+ $query = $this -> pdo -> prepare('SELECT * FROM contracts WHERE id = :id');
+ $query -> bindValue(':id', $id);
+ $query -> execute();
+
+ $result = $query -> fetch(PDO::FETCH_ASSOC);
+ $contract = $this->getContractByResult($result);
+
+ $query->closeCursor();
+
+ return $contract;
+ }
+
+ public function getContractByNumber($number) {
+
+ $query = $this -> pdo -> prepare('SELECT * FROM contracts WHERE number = :number');
+ $query -> bindValue(':number', $number);
+ $query -> execute();
+
+ $result = $query -> fetch(PDO::FETCH_ASSOC);
+ $contract = $this->getContractByResult($result);
+
+ $query->closeCursor();
+
+ return $contract;
+ }
+
+ // TODO : Il faut que client_adullact_id et product soit des colonnes de contracts !
+ // pour le moment, il semble qu'on les retrouve avec une partie du "number"
+ public function getValidContractByNumberAndVersionDate($number, $versionDate) {
+
+
+ $query = $this -> pdo -> prepare('SELECT * FROM contracts WHERE number LIKE :number');
+ $query -> bindValue(':number', $number."%");
+ $query -> execute();
+
+ $results = $query -> fetchAll(PDO::FETCH_ASSOC);
+
+ /** @var Contract $lastContract */
+
+ // FOR EACH CONTRACTS FOUND WITH A SIMILAR NUMBER
+ foreach ($results as $result) {
+
+ // IF THE DATE OF THE VERSION IS IN THE RANGE OF THIS CONTRACT (END DATE CAN BE EMPTY)
+ if($versionDate >= $result['start_date'] && ($versionDate <= $result['end_date'] || !isset($result['end_date']) || $result['end_date'] == "")
+
+ // CHECKING CONTRACT VALIDITY STATE
+ && ($result['state'] == "^encours^" || $result['state'] == "^signe^" || $result['state'] == "^signe_maj_version^")) {
+
+ // WE GET THE MOST RECENT CONTRACT AMONG THOSE
+ if(!isset($lastContract) || $result['start_date'] > $lastContract -> start_date) {
+ $lastContract = $this->getContractByResult($result);
+
+ }
+ }
+ }
+
+ $query->closeCursor();
+
+ if(isset($lastContract)) {
+ return $lastContract;
+ }
+
+ return null;
+ }
+
+ public function getContracts() {
+
+ $query = $this -> pdo -> prepare('SELECT * FROM contracts');
+ $query -> execute();
+
+ $results = $query -> fetchAll(PDO::FETCH_ASSOC);
+ $contracts = array();
+ foreach ($results as $result) {
+ $contracts[] = $this->getContractByResult($result);
+ }
+
+ $query->closeCursor();
+
+ return $contracts;
+ }
+
+ public function removeContract(Contract $contract) {
+ $query = $this -> pdo -> prepare('DELETE FROM contracts WHERE id = :id');
+ $query -> bindValue('id', $contract -> id);
+ $query -> execute();
+ }
+
+ public function getByCustomerId($customer_id){
+ $sql = "SELECT number,state,start_date,end_date,sugar_id FROM contracts WHERE customer_id=?";
+ return $this->query($sql,$customer_id);
+ }
+
+}
+