<?php
$basePath = "./";
// $basePath = "../../";
require_once ($basePath.'Classes/dbManage.php');
require_once ($basePath.'Classes/constant/libelle.class.php');
require_once ($basePath.'Classes/booking/Sale.class.php');
require_once ($basePath.'Classes/person/Person.class.php');
require_once ($basePath.'Classes/booking/Manifest.class.php');
require_once ($basePath.'Classes/booking/Finance.class.php');
require_once ($basePath.'Classes/booking/Booking.class.php');
require_once ($basePath.'Classes/mobile/Control.class.php');
require_once ($basePath.'Classes/mobile/Promotion.class.php');
require_once ($basePath.'Classes/mobile/ManifestServerResponse.class.php');

Class Manifest
{
	public $person;
	public $sale;
	public $finance;
	public $control;
	
	//constructeur par défaut
    // public function __construct() { }
	
	
	public function __construct($person=null, $sale=null, $finance=null, $control=null, $promotion=null)
	{
		$this->person = $person;
		$this->sale = $sale;
		$this->finance = $finance;
		$this->control = $control;
		$this->promotion = $promotion;
	}
	
	public function getPromotion() {
        return $this->promotion;
    }
	
    public function  setPromotion($promotion) {
        $this->promotion = $promotion;
    }
	
	public function getPerson() {
        return $this->person;
    }
	
    public function  setPerson($person) {
        $this->person = $person;
    }
	
	public function getSale() {
        return $this->sale;
    }
    public function  setSale($sale) {
        $this->sale = $sale;
    }
	
	public function getControl() {
        return $this->control;
    }
    public function  setControl($control) {
        $this->control = $control;
    }
	
	/* public function getPseudoUser() {
        return $this->pseudoUser;
    }
    public function  setPseudoUser($pseudoUser) {
        $this->pseudoUser = $pseudoUser;
    } */
	
	
	
	public function getManifest($trainNumber, $pseudoUser, $saleDate='')
	{
		// $response = array();
		$sale = null;
		$person = null;
		$finance = null;
		$control = null;
		$manifest = null;
		$returnArray = array();
		$forCommitAction = false;
		try{
			
			$sql="INSERT INTO kzcmit (cmuser, cmetat, cmddeb, cmactn, cmucre, cmdcre) 
				VALUE(:tempPseudoUser, 'E', now(), 'MANIFESTEXPORTATION', :pseudoUser, now());";			
			$tempPseudoUser = date("mdHms").$pseudoUser;
			
			$this->db = new dbManage();
			$req = $this->db->__dbh->prepare($sql);
			$req->bindParam(':tempPseudoUser',$tempPseudoUser);
			$req->bindParam(':pseudoUser',$pseudoUser);
			$req->execute();
			$this->db->close();
			
			
			
			$this->db = new dbManage();
			$sql   = "SELECT kzvnteid, vnpers, vntrai,  vncveh, vnsieg, vngdep, vngfin ,vnddep, vnhdep, coalesce(vncode,' ') AS macode, coalesce(vnqrco,' ') AS maqrco, peivip, pefide, vnivip, vnetat, vnclas,vnucre,
			pemile, pecivi, peccni, penomf, penomp, pectel, petele, pemail, peacti, pepayo, pestat, 
			kzfinaid,fivnte,fimpai, COALESCE(fimttc,0) AS fimttc, COALESCE(fitamo,0) AS fitamo, COALESCE(fimore,'') AS fimore , COALESCE(firedu,0) AS firedu, ficurr, COALESCE(fiprom,0) AS fiprom,fiacti, COALESCE(firttc,0) AS firttc, COALESCE(firctv,'') AS firctv, COALESCE(firtva,0) AS firtva, COALESCE(fisttc,0) AS fisttc, COALESCE(fisctv,'') AS fisctv, COALESCE(fistva,0) AS fistva,
			vnacti, pednai, ctctle, ctchke, cttype, ctlong, ctlati, ctpklm, ctctld, COALESCE(ctctlu,'') AS ctctlu, ctchkd, COALESCE(ctchku,'') AS ctchku, ctumaj, kzctrlid, ctacti
			FROM kzvnte 
			INNER JOIN kzpers ON kzpersid = vnpers   
			INNER JOIN kzfina ON fivnte = kzvnteid and fiacti = 1
			LEFT join kzctrl  on ctvnte = kzvnteid and ctacti = 1
			WHERE  vnhist = 0  
			AND vnacti = 1
			AND vntrai = :trainNumber 
			";
			
			if($saleDate != '' && $pseudoUser != ''){
				$sql = $sql." AND vnucre != '".$pseudoUser."' 
				 AND vndcre >= '".$saleDate."' ";
				 $forCommitAction = true;
			}
			$sql = $sql." ORDER BY vntrai DESC, vncveh DESC, vnsieg ASC;";
			
			$req = $this->db->__dbh->prepare($sql);
			$req->bindParam(':trainNumber', $trainNumber);
			$req->execute();
			$result = $req->fetchAll(PDO::FETCH_ASSOC);			
			$this->db->close();
			
			
			for ($i=0; $i<count($result); $i++)
			{ 
				$sale = new Sale($result[$i]['kzvnteid'], $result[$i]['vnpers'], $result[$i]['vntrai'],$result[$i]['vncveh'], $result[$i]['vnsieg'],$result[$i]['vngdep'], $result[$i]['vnddep'],$result[$i]['vnhdep'], $result[$i]['vngfin'],$result[$i]['macode'], $result[$i]['maqrco'],$result[$i]['vnivip'],$result[$i]['vnetat'], $result[$i]['vnclas'], 'N',$result[$i]['vnacti']);
				
				$person = new Person($result[$i]['vnpers'],$result[$i]['penomf'],$result[$i]['penomp'],$result[$i]['pecivi'],$result[$i]['pefide'],$result[$i]['pectel'],$result[$i]['petele'], $result[$i]['peccni'],$result[$i]['pemail'],$result[$i]['pednai'],$result[$i]['peivip'],$result[$i]['peacti'], $result[$i]['pepayo'],$result[$i]['pestat'] );
				
				$finance = new Finance($result[$i]['kzfinaid'],$result[$i]['kzvnteid'],$result[$i]['ficurr'], $result[$i]['fimpai'],$result[$i]['fimttc'], $result[$i]['fitamo'],$result[$i]['fimore'], $result[$i]['firedu'],$result[$i]['fiprom'],
				$result[$i]['firttc'],$result[$i]['firctv'],$result[$i]['firtva'],$result[$i]['fisttc'],$result[$i]['fisctv'],$result[$i]['fistva'], $result[$i]['fiacti']);
				
				if($forCommitAction === true){
					$control = new Control($result[$i]['kzctrlid'],$result[$i]['kzvnteid'],$result[$i]['ctctle'],$result[$i]['ctchke'],$result[$i]['cttype'],$result[$i]['ctlong'],$result[$i]['ctlati'],$result[$i]['ctpklm'],$result[$i]['ctctld'],$result[$i]['ctctlu'],$result[$i]['ctchkd'],$result[$i]['ctchku'],$result[$i]['ctacti']);
				}else{
					$control = null;
				}
				
				
				// $manifest = new Manifest($person,$sale,$finance, $result[$i]['vnucre']);
				$manifest = new Manifest($person,$sale,$finance, $control);
				
				$returnArray[$i] = $manifest;
			}
			
			$sql=" UPDATE kzcmit set cmetat = 'T', cmtfin = 'S', cmuser = :pseudoUser, cmdfin = now(), cmumaj = 'KZADMIN'
			WHERE cmuser = :tempPseudoUser; ";
			$this->db = new dbManage();
			$req = $this->db->__dbh->prepare($sql);
			$req->bindParam(':tempPseudoUser', $tempPseudoUser);
			$req->bindParam(':pseudoUser', $pseudoUser);
			$req->execute();
			$this->db->close();
			
			return $returnArray;
		}catch(Exception $e){
			echo $e->getMessage();
			echo $e->getTraceAsString();  
			return "error";
		}
	}
	
	
	public function startedImport($trainNumber, $pseudoUser, $macAdress, $saleDate='')
	{
		
		try{
			
			$sql = "SELECT kzeqmtid AS equipmentId FROM kzeqmt WHERE eqamac = :macAdress AND eqacti = 1;";
			$this->db = new dbManage();
			$req = $this->db->__dbh->prepare($sql);
			$req->bindParam(':macAdress',$macAdress);
			$req->execute();
			$result = $req->fetchAll(PDO::FETCH_ASSOC);		
			$this->db->close();
			$equipmentId = $result[0]['equipmentId'];
			
			
			$sql="INSERT INTO kzcmit (cmeqmt, cmuser, cmetat, cmddeb, cmdfin, cmactn, cmucre, cmdcre) 
				VALUE(:equipmentId, :pseudoUser, 'T', NOW(), NOW(), 'MANIFESTEXPORTATION', :createdUser, NOW());";			
			$tempPseudoUser = date("mdHms").$pseudoUser;
			
			$this->db = new dbManage();
			$req = $this->db->__dbh->prepare($sql);
			$req->bindParam(':pseudoUser',$pseudoUser);
			$req->bindParam(':equipmentId',$equipmentId);
			$req->bindParam(':createdUser',$createdUser);
			$req->execute();
			$this->db->close();
			
			return "OK";
		}catch(Exception $e){
			echo $e->getMessage();
			echo $e->getTraceAsString();  
			return "KO";
		}
	}
	
	
	public function getAllSale($listId)
	{
		// $response = array();
		$sale = null;
		$returnArray = array();
		try{
			
			// $sql="INSERT INTO kzcmit (cmuser, cmetat, cmddeb, cmactn, cmucre, cmdcre) 
				// VALUE(:tempPseudoUser, 'E', now(), 'MANIFESTEXPORTATION', :pseudoUser, now());";			
			// $tempPseudoUser = date("mdHms").$pseudoUser;
			
			// $this->db = new dbManage();
			// $req = $this->db->__dbh->prepare($sql);
			// $req->bindParam(':tempPseudoUser',$tempPseudoUser);
			// $req->bindParam(':pseudoUser',$pseudoUser);
			// $req->execute();
			// $this->db->close();
			
			
			
			$this->db = new dbManage();
			$sql   = "SELECT kzvnteid , vnpers , vntrai ,  vncveh , vnsieg , vngdep , vngfin  , vnddep , vnhdep , coalesce(vncode ,' ') AS vncode  , coalesce(vnqrco ,' ') AS vnqrco  , vnivip  , vnetat  , vnclas  , vnucre  , vnacti
			FROM kzvnte 
			WHERE  kzvnteid  IN (".$listId."); ";
			
			$req = $this->db->__dbh->prepare($sql);
			$req->execute();
			$result = $req->fetchAll(PDO::FETCH_ASSOC);			
			$this->db->close();
			
			for ($i=0; $i<count($result); $i++)
			{ 
				$sale = new Sale($result[$i]['kzvnteid'], $result[$i]['vnpers'], $result[$i]['vntrai'],$result[$i]['vncveh'], $result[$i]['vnsieg'],$result[$i]['vngdep'], $result[$i]['vnddep'],$result[$i]['vnhdep'], $result[$i]['vngfin'],$result[$i]['vncode'], $result[$i]['vnqrco'],$result[$i]['vnivip'],$result[$i]['vnetat'], $result[$i]['vnclas'], 'N',$result[$i]['vnacti']);
				
				// $manifest = new Manifest($person,$sale,$finance, $result[$i]['vnucre']);
				// $manifest = new Manifest($person,$sale,$finance, $control);
				
				$returnArray[$i] = $sale;
			}
			
			// $sql=" UPDATE kzcmit set cmetat = 'T', cmtfin = 'S', cmuser = :pseudoUser, cmdfin = now(), cmumaj = 'KZADMIN'
			// WHERE cmuser = :tempPseudoUser; ";
			// $this->db = new dbManage();
			// $req = $this->db->__dbh->prepare($sql);
			// $req->bindParam(':tempPseudoUser', $tempPseudoUser);
			// $req->bindParam(':pseudoUser', $pseudoUser);
			// $req->execute();
			// $this->db->close();
			
			return $returnArray;
		}catch(Exception $e){
			echo $e->getMessage();
			echo $e->getTraceAsString();  
			return "error";
		}
	}
	
	
	public function getAllSaleId($trainNumber, $pseudoUser, $saleDate='')
	{
		// $response = array();
		$sale = null;
		$returnArray = array();
		try{
			
			
			$this->db = new dbManage();
			$sql   = "SELECT kzvnteid AS id
			FROM kzvnte 
			INNER JOIN kzpers ON kzpersid = vnpers   
			INNER JOIN kzfina ON fivnte = kzvnteid and fiacti = 1
			LEFT join kzctrl  on ctvnte = kzvnteid and ctacti = 1
			WHERE  vnhist = 0  
			AND vnacti = 1
			AND vntrai = :trainNumber
			";
			
			$req = $this->db->__dbh->prepare($sql);
			$req->bindParam(':trainNumber', $trainNumber);
			$req->execute();
			$this->db->close();
			return  $req->fetchAll(PDO::FETCH_ASSOC);	
			
		}catch(Exception $e){
			echo $e->getMessage();
			echo $e->getTraceAsString();  
			return "error";
		}
	}
	
	public function getAllFinance($listId)
	{
		
		$finance = null;
		$returnArray = array();
		try{
			
			
			
			$this->db = new dbManage();
			$sql   = "SELECT kzfinaid , fivnte , fimpai , COALESCE(fimttc ,0) AS fimttc , COALESCE(fitamo ,0) AS fitamo , COALESCE(fimore ,'') AS fimore  , COALESCE(firedu ,0) AS firedu , ficurr , COALESCE(fiprom ,0) AS fiprom ,fiacti , COALESCE(firttc ,0) AS firttc , COALESCE(firctv ,'') AS firctv , COALESCE(firtva ,0) AS firtva , COALESCE(fisttc ,0) AS fisttc , COALESCE(fisctv ,'') AS fisctv , COALESCE(fistva ,0) AS fistva
			FROM kzfina 
			WHERE kzfinaid IN (".$listId."); ";
			
			$req = $this->db->__dbh->prepare($sql);
			$req->execute();
			$result = $req->fetchAll(PDO::FETCH_ASSOC);			
			$this->db->close();
			
			for ($i=0; $i<count($result); $i++)
			{ 
				$finance = new Finance($result[$i]['kzfinaid'],$result[$i]['fivnte'],$result[$i]['ficurr'], $result[$i]['fimpai'],$result[$i]['fimttc'], $result[$i]['fitamo'],$result[$i]['fimore'], $result[$i]['firedu'],$result[$i]['fiprom'],
				$result[$i]['firttc'],$result[$i]['firctv'],$result[$i]['firtva'],$result[$i]['fisttc'],$result[$i]['fisctv'],$result[$i]['fistva'], $result[$i]['fiacti']);
				
				$returnArray[$i] = $finance;
			}
		
			return $returnArray;
		}catch(Exception $e){
			echo $e->getMessage();
			echo $e->getTraceAsString();  
			return "error";
		}
	}
	
	public function getAllFinanceId($trainNumber, $pseudoUser, $saleDate='')
	{
		
		$finance = null;
		$returnArray = array();
		try{
			
			
			
			$this->db = new dbManage();
			$sql   = " SELECT kzfinaid AS id
			FROM kzvnte 
			INNER JOIN kzpers ON kzpersid = vnpers   
			INNER JOIN kzfina ON fivnte = kzvnteid and fiacti = 1
			LEFT join kzctrl  on ctvnte = kzvnteid and ctacti = 1
			WHERE  vnhist = 0  
			AND vnacti = 1
			AND vntrai = :trainNumber 
			";
			
			$req = $this->db->__dbh->prepare($sql);
			$req->bindParam(':trainNumber', $trainNumber);
			$req->execute();
			$this->db->close();
			return $req->fetchAll(PDO::FETCH_ASSOC);		
			
		}catch(Exception $e){
			echo $e->getMessage();
			echo $e->getTraceAsString();  
			return "error";
		}
	}
	
	
	public function getAllPerson($listId)
	{
		
		$person = null;
		$returnArray = array();
		try{
			
			$this->db = new dbManage();
			$sql   = " SELECT kzpersid , pecivi , peccni , penomf , penomp , pectel , petele , pemail , peacti , pepayo , pestat , pefide , pednai , peivip  
			FROM kzpers
			WHERE  kzpersid IN (".$listId."); ";
			
			$req = $this->db->__dbh->prepare($sql);
			$req->bindParam(':trainNumber', $trainNumber);
			$req->execute();
			$result = $req->fetchAll(PDO::FETCH_ASSOC);			
			$this->db->close();
			
			for ($i=0; $i<count($result); $i++)
			{ 
				
				$person = new Person($result[$i]['kzpersid'],$result[$i]['penomf'],$result[$i]['penomp'],$result[$i]['pecivi'],$result[$i]['pefide'],$result[$i]['pectel'],$result[$i]['petele'], $result[$i]['peccni'],$result[$i]['pemail'],$result[$i]['pednai'],$result[$i]['peivip'],$result[$i]['peacti'], $result[$i]['pepayo'],$result[$i]['pestat'] );
				
				$returnArray[$i] = $person;
			}
			
			return $returnArray;
		}catch(Exception $e){
			echo $e->getMessage();
			echo $e->getTraceAsString();  
			return "error";
		}
	}
	
	public function getAllPersonId($trainNumber, $pseudoUser, $saleDate='')
	{
		
		$person = null;
		$returnArray = array();
		try{
			
			$this->db = new dbManage();
			$sql   = " SELECT DISTINCT kzpersid AS id
			FROM kzvnte 
			INNER JOIN kzpers ON kzpersid = vnpers   
			INNER JOIN kzfina ON fivnte = kzvnteid and fiacti = 1
			LEFT join kzctrl  on ctvnte = kzvnteid and ctacti = 1
			WHERE  vnhist = 0  
			AND vnacti = 1
			AND vntrai = :trainNumber 
			";
			
			$req = $this->db->__dbh->prepare($sql);
			$req->bindParam(':trainNumber', $trainNumber);
			$req->execute();
			$this->db->close();
			return $req->fetchAll(PDO::FETCH_ASSOC);		
			
		}catch(Exception $e){
			echo $e->getMessage();
			echo $e->getTraceAsString();  
			return "error";
		}
	}
	
	
	public function importManifest($result, $internCall=false)
	{
		$sale = null;
		$person = null;
		$control = null;
		$manifest = null;
		$returnArray = array();
		$answerArray = array();
		$manifestServerArray = array();
		$updatedLineNumber = 0;
		$insertedLineNumber = 0;
		$notInsertedLineNumber = 0;
		$j = 0;
		
		if($internCall === false)
		{
			$result = json_decode($result,true);
		}
		
		try{
			
			for ($i=0; $i<count($result); $i++)
			{
				
				$sale = new  Sale($result[$i]['sale']['saleId'], $result[$i]['sale']['personId'], $result[$i]['sale']['trainNumber'],$result[$i]['sale']['coachName'], $result[$i]['sale']['seatNumber'],$result[$i]['sale']['departureCode'], $result[$i]['sale']['departureDate'],$result[$i]['sale']['departureHour'], $result[$i]['sale']['arrivalCode'],$result[$i]['sale']['ticketCode'], $result[$i]['sale']['cipherCode'],$result[$i]['sale']['coachVipStatus'],$result[$i]['sale']['saleStatus'], $result[$i]['sale']['rank'],$result[$i]['sale']['isNew'],$result[$i]['sale']['activationStatus'],$result[$i]['sale']['serverSaleId'] );
				
				$person = new Person($result[$i]['person']['personId'],$result[$i]['person']['name'],$result[$i]['person']['firstName'],$result[$i]['person']['civility'],$result[$i]['person']['fidelityCode'],$result[$i]['person']['phoneCode'],$result[$i]['person']['phoneNumber'], $result[$i]['person']['cniCode'],$result[$i]['person']['email'],$result[$i]['person']['vipStatus'],$result[$i]['person']['activationStatus']);
				
				$finance = new Finance($result[$i]['finance']['financeId'],$result[$i]['finance']['saleId'],$result[$i]['finance']['currency'],$result[$i]['finance']['paymentMode'],$result[$i]['finance']['paymentAmount'], $result[$i]['finance']['fullRateAmount'],$result[$i]['finance']['discountMotive'], $result[$i]['finance']['discountMotiveAmount'],$result[$i]['finance']['promotionAmount'],$result[$i]['finance']['adjustmentAmount'],$result[$i]['finance']['adjustmentTvaCode'],$result[$i]['finance']['adjustmentTvaAmount'],$result[$i]['finance']['outClassingAmount'],$result[$i]['finance']['outClassingTvaCode'],$result[$i]['finance']['outClassingTvaAmount'],$result[$i]['finance']['activationStatus']); 
				
								
				$control = new Control($result[$i]['control']['controlId'],$result[$i]['control']['saleId'],$result[$i]['control']['controlStatus'],$result[$i]['control']['checkingStatus'],$result[$i]['control']['controlType'],$result[$i]['control']['longitude'],$result[$i]['control']['latitude'],$result[$i]['control']['kilometricPoint'],$result[$i]['control']['controlDateTime'],$result[$i]['control']['checkingDateTime']);
				
				$manifest = new Manifest($person,$sale,$finance,$control);
				$mobileSaleId = $sale->getSaleId();
				// $returnArray[$i] = $manifest;
				if(!empty($result[$i]['control']['saleId']))
				{
					try
					{
						
						$controlType = $control->getControlType();
						$longitude = $control->getLongitude();
						$latitude = $control->getLatitude();
						$kilometricPoint = $control->getKilometricPoint();
						$controlDateTime = $control->getControlDateTime();
						$checkingDateTime = $control->getCheckingDateTime();
						$pseudoUser = $result[$i]['pseudoUser'];
						
						
						if($sale->getIsNew()=='O')
						{
							
							$firstName = $person->getFirstName();
							$name = $person->getName();
							$displayName = $name." ".$firstName;
							$countryCode = $person->getPhoneCode();
							$phoneCode = $person->getPhoneCode();
							$phoneNumber = $person->getPhoneNumber();
							$birthDate = "0001-01-01";
							// $birthDate = $person->getBirthDate();
							$email = $person->getMail();
							$status = $person->getSearchStatus();
							$vipStatut = $person->getVipStatus();
							$fidelityCode = $person->getFidelityCode();
							$cniCode = $person->getCniCode();
							$number = 1;
							$matricule = ' ';
								
							
							$sql="CALL insertPerson(:firstName, :name, :displayName, :countryCode , :phoneNumber ,:phoneCode, :birthDate, :civility, :email , :statut , :cniCode , :vipStatut , :fidelityCode , :pseudoUser , :number, :matricule);";
							
							$this->db = new dbManage();
							$req = $this->db->__dbh->prepare($sql);
							$req->bindParam(':firstName', $firstName);
							$req->bindParam(':name', $name);
							$req->bindParam(':displayName', $displayName);
							$req->bindParam(':countryCode', $countryCode);
							$req->bindParam(':phoneNumber', $phoneNumber);
							$req->bindParam(':phoneCode', $phoneCode);
							$req->bindParam(':birthDate', $birthDate);
							$req->bindParam(':civility', $civility);
							$req->bindParam(':email', $email);
							$req->bindParam(':statut', $statut);
							$req->bindParam(':cniCode', $cniCode);
							$req->bindParam(':vipStatut', $vipStatut);
							$req->bindParam(':fidelityCode', $fidelityCode);
							$req->bindParam(':pseudoUser', $pseudoUser);
							$req->bindParam(':number', $number);
							$req->bindParam(':matricule', $matricule);
							$req->execute();
							$returnArray = $req->fetchAll(PDO::FETCH_ASSOC);
							$this->db->close();
							
							$personId = $returnArray[0]['result'];
							$splitTable = explode('|',$personId);
							$personId = $splitTable[0];
							$splitTable = explode('-',$personId);
							$personId = $splitTable[0];
							$tvaCode = 'TAX19.25';
							
							$ticketCode = Booking::ticketCodeGeneration(rand(),$sale->getSeatNumber());
							
							$cipherCode = Booking::ticketCodeEncryption($ticketCode);
							$departureDate = $sale->getDepartureDate();
							$departureHour = $sale ->getDepartureHour();
							$departureCode = $sale->getDepartureCode();
							$arrivalCode = $sale->getArrivalCode();
							$trainNumber = $sale->getTrainNumber();
							$coachName = $sale->getCoachName();
							$rank = $sale->getRank();
							$seatNumber = $sale->getSeatNumber();
							$saleVipStatus = $sale->getCoachVipStatus();
							$travelType = 0;
							$saleStatus = 'O';
							$civility = $person->getCivility();
							$provenance = 'C';
							
							$paymentMode = $finance->getPaymentMode();
							$currency = $finance->getCurrency();
							$fullRateAmount = $finance->getFullRateAmount();
							$discountMotive = $finance->getDiscountMotive();
							$discountMotiveAmount = $finance->getDiscountMotiveAmount();
							$userPaymentId = -1;
							$promotionAmount = 0;
							$paymentAmount = $finance->getPaymentAmount();
							
							$adjustmentAmount = $finance->getAdjustmentAmount();
							$adjustmentTvaCode = $finance->getAdjustmentTvaCode();
							$adjustmentTvaAmount = $finance->getAdjustmentTvaAmount();
							$outClassingAmount = $finance->getOutClassingAmount();
							$outClassingTvaCode = $finance->getOutClassingTvaCode();
							$outClassingTvaAmount = $finance->getOutClassingTvaAmount();
							
							$controlStatus = $control->getControlStatus();
							$checkingStatus = $control->getCheckingStatus();
								
							$civility = $person->getCivility();
	
							$sql = "CALL insertSale (:personId , :ticketCode, :cipherCode , :departureDate, :departureHour, :departureCode, :arrivalCode, :trainNumber, :coachName, :rank, :seatNumber, :saleVipStatus, :travelType, :saleStatus, :pseudoUser, :provenance, :civility, :paymentMode, :currency, :fullRateAmount, :discountMotive, :discountMotiveAmount, :userPaymentId, :promotionAmount, :paymentAmount, :adjustmentAmount, :adjustmentTvaCode, :adjustmentTvaAmount, :outClassingAmount, :outClassingTvaCode, :outClassingTvaAmount, :tvaCode,  :controlStatus, :checkingStatus, :controlType, :longitude, :latitude, :kilometricPoint, :controlDateTime, :checkingDateTime)";
						
							$this->db = new dbManage();
							$req = $this->db->__dbh->prepare($sql);
							$req->bindParam(':personId', $personId);
							$req->bindParam(':ticketCode', $ticketCode);
							$req->bindParam(':cipherCode', $cipherCode);
							$req->bindParam(':departureDate', $departureDate);
							$req->bindParam(':departureHour', $departureHour);
							$req->bindParam(':departureCode', $departureCode);
							$req->bindParam(':arrivalCode', $arrivalCode);
							$req->bindParam(':trainNumber', $trainNumber);
							$req->bindParam(':coachName', $coachName);
							$req->bindParam(':rank', $rank);
							$req->bindParam(':seatNumber', $seatNumber);
							$req->bindParam(':saleVipStatus', $saleVipStatus);
							$req->bindParam(':travelType', $travelType);
							$req->bindParam(':saleStatus', $saleStatus);
							$req->bindParam(':pseudoUser', $pseudoUser);
							$req->bindParam(':provenance', $provenance);
							$req->bindParam(':civility', $civility);
							$req->bindParam(':paymentMode', $paymentMode);
							$req->bindParam(':currency', $currency);
							$req->bindParam(':fullRateAmount', $fullRateAmount);
							$req->bindParam(':discountMotive', $discountMotive);
							$req->bindParam(':discountMotiveAmount', $discountMotiveAmount);
							$req->bindParam(':userPaymentId', $userPaymentId);
							$req->bindParam(':promotionAmount', $promotionAmount);
							
							
							$req->bindParam(':paymentAmount', $paymentAmount);
							$req->bindParam(':adjustmentAmount', $adjustmentAmount);
							$req->bindParam(':adjustmentTvaCode', $adjustmentTvaCode);
							$req->bindParam(':adjustmentTvaAmount', $adjustmentTvaAmount);
							$req->bindParam(':outClassingAmount', $outClassingAmount);
							$req->bindParam(':outClassingTvaCode', $outClassingTvaCode);
							$req->bindParam(':outClassingTvaAmount', $outClassingTvaAmount);
							$req->bindParam(':controlStatus', $controlStatus);
							$req->bindParam(':checkingStatus', $checkingStatus);
							$req->bindParam(':controlType', $controlType);
							$req->bindParam(':longitude', $longitude);
							$req->bindParam(':latitude', $latitude);
							$req->bindParam(':kilometricPoint', $kilometricPoint);
							$req->bindParam(':controlDateTime', $controlDateTime);
							$req->bindParam(':checkingDateTime', $checkingDateTime);
							$req->bindParam(':tvaCode', $tvaCode);
							$req->execute();
							$returnArray = $req->fetchAll(PDO::FETCH_ASSOC);
							$this->db->close();
							
														
							$returnArray = $returnArray[0];
							$saleId = $returnArray['saleId'];
							
							if($returnArray['saleId']!=-1){
								
								
								$ticketCode = Booking::ticketCodeGeneration($saleId,$sale->getSeatNumber());
								$cipherCode = Booking::ticketCodeEncryption($ticketCode); 
								
								
								$sql = "UPDATE kzvnte set 
										vncode = :ticketCode,
										vnqrco = :cipherCode,
										vnumaj = :pseudoUser,
										vndmaj = now()
										WHERE kzvnteid = :saleId; ";
								$this->db = new dbManage();
								$req = $this->db->__dbh->prepare($sql);
								$req->bindParam(':ticketCode', $ticketCode);
								$req->bindParam(':cipherCode', $cipherCode);
								$req->bindParam(':saleId', $saleId);
								$req->bindParam(':pseudoUser', $pseudoUser);$req->execute();
								$this->db->close();
								
								//Envoie du SMS et Mail pour cette personne	
								
								$manifestServerResponse = new ManifestServerResponse($mobileSaleId,$saleId,'O',$ticketCode,$cipherCode);
								$manifestServerArray[$j] = $manifestServerResponse;
								$j++;
								$insertedLineNumber++;
								
							}
							else
							{
								
								$splitTab = explode('|',$returnArray['seatStatus']);
								$manifestServerResponse = new ManifestServerResponse($mobileSaleId,$splitTab[4],'N',$splitTab[2],$splitTab[3]);
								$manifestServerArray[$j] = $manifestServerResponse;
								$j++;
								$notInsertedLineNumber++;
							}
							
						}
						else
						{
							$serverSaleId = $sale->getServerSaleId();
							
							$currentDate = date('Y-m-d H:i:s');				
							$sql="CALL updateSaleInformation(:serverSaleId, :controlStatus, :checkingStatus, :controlType , :longitude , :latitude, :kilometricPoint, :controlDateTime, :checkingDateTime, :pseudoUser, :adjustmentAmount, :adjustmentTvaCode , :adjustmentTvaAmount , :outClassingAmount , :outClassingTvaCode , :outClassingTvaAmount);";
							$this->db = new dbManage();
							$req = $this->db->__dbh->prepare($sql);
							$req->bindParam(':serverSaleId', $serverSaleId);
							$req->bindParam(':controlStatus', $controlStatus);
							$req->bindParam(':checkingStatus', $checkingStatus);
							$req->bindParam(':controlType', $controlType);
							$req->bindParam(':longitude', $longitude);
							$req->bindParam(':latitude', $latitude);
							$req->bindParam(':kilometricPoint', $kilometricPoint);
							$req->bindParam(':controlDateTime', $controlDateTime);
							$req->bindParam(':checkingDateTime', $checkingDateTime);
							$req->bindParam(':pseudoUser', $pseudoUser);
							$req->bindParam(':adjustmentAmount', $adjustmentAmount);
							$req->bindParam(':adjustmentTvaCode', $adjustmentTvaCode);
							$req->bindParam(':adjustmentTvaAmount', $adjustmentTvaAmount);
							$req->bindParam(':outClassingAmount', $outClassingAmount);
							$req->bindParam(':outClassingTvaCode', $outClassingTvaCode);
							$req->bindParam(':outClassingTvaAmount', $outClassingTvaAmount);
							$req->execute();
							$req->fetchAll(PDO::FETCH_ASSOC);
							$this->db->close();
							
							$ticketCode = "";
							$cipherCode = "";
							$manifestServerResponse = new ManifestServerResponse($mobileSaleId,$serverSaleId,'C',$ticketCode,$cipherCode);
							$manifestServerArray[$j] = $manifestServerResponse;
							$j++;
							
							$updatedLineNumber++;
						}	
							
					}catch(Exception $e){
						
						// echo $e->getMessage();
						// echo $e->getTraceAsString();
						// print_r($returnArray);
						return $sale;
						
					}
				} 
				
			}
			
			$answerArray['insertedLineNumber'] = $insertedLineNumber;
			$answerArray['notInsertedLineNumber'] = $notInsertedLineNumber;
			$answerArray['updatedLineNumber'] = $updatedLineNumber;
			$answerArray['manifestServerArray'] = $manifestServerArray;
			return $answerArray ;
		}catch(Exception $e){
			echo $e->getMessage();
			echo $e->getTraceAsString();
			// return $answerArray;
		}
	}
	
	
	public function createVersement($pseudoUser)
	{
		try{
				$sql="CALL createNewVersement(:saleUser, :connectedUser);";
				$this->db = new dbManage();
				$req = $this->db->__dbh->prepare($sql);
				$req->bindParam(':saleUser', $pseudoUser);
				$req->bindParam(':connectedUser', $pseudoUser);
				$req->execute();
				$returnArray = $req->fetchAll(PDO::FETCH_ASSOC);
				$this->db->close();
				
				$answerArray['versement'] = $returnArray[0];
				return $answerArray;
		}catch(Exception $e){
			// echo $e->getMessage();
			// echo $e->getTraceAsString();
			// return $answerArray;
		}
	}
/* 	 
	public function importPartialManifest($result)
	{
		$sale = null;
		$person = null;
		$control = null;
		$manifest = null;
		$returnArray = array();
		$answerArray = array();
		$manifestServerArray = array();
		$updatedLineNumber = 0;
		$insertedLineNumber = 0;
		$notInsertedLineNumber = 0;
		$j = 0;
		
		$result = json_decode($result,true);
		
		try{
			
			for ($i=0; $i<count($result); $i++)
			{
				
				$sale = new Sale($result[$i]['sale']['saleId'], $result[$i]['sale']['personId'], $result[$i]['sale']['trainNumber'],$result[$i]['sale']['coachName'], $result[$i]['sale']['seatNumber'],$result[$i]['sale']['departureCode'], $result[$i]['sale']['departureDate'],$result[$i]['sale']['departureHour'], $result[$i]['sale']['arrivalCode'],$result[$i]['sale']['ticketCode'], $result[$i]['sale']['cipherCode'],$result[$i]['sale']['coachVipStatus'],$result[$i]['sale']['saleStatus'], $result[$i]['sale']['rank'],$result[$i]['sale']['isNew'],$result[$i]['sale']['activationStatus'], $result[$i]['sale']['serverSaleId']);
				
				$person = new Person($result[$i]['person']['personId'],$result[$i]['person']['name'],$result[$i]['person']['firstName'],$result[$i]['person']['civility'],$result[$i]['person']['fidelityCode'],$result[$i]['person']['phoneCode'],$result[$i]['person']['phoneNumber'], $result[$i]['person']['cniCode'],$result[$i]['person']['email'],$result[$i]['person']['vipStatus'],$result[$i]['person']['activationStatus']);
				
				$finance = new Finance($result[$i]['finance']['financeId'],$result[$i]['finance']['saleId'],$result[$i]['finance']['currency'],$result[$i]['finance']['paymentMode'],$result[$i]['finance']['paymentAmount'], $result[$i]['finance']['fullRateAmount'],$result[$i]['finance']['discountMotive'], $result[$i]['finance']['discountMotiveAmount'],$result[$i]['finance']['promotionAmount'],$result[$i]['finance']['adjustmentAmount'],$result[$i]['finance']['adjustmentTvaCode'],$result[$i]['finance']['adjustmentTvaAmount'],$result[$i]['finance']['outClassingAmount'],$result[$i]['finance']['outClassingTvaCode'],$result[$i]['finance']['outClassingTvaAmount'],$result[$i]['finance']['activationStatus']); 
				
								
				$control = new Control($result[$i]['control']['controlId'],$result[$i]['control']['saleId'],$result[$i]['control']['controlStatus'],$result[$i]['control']['checkingStatus'],$result[$i]['control']['controlType'],$result[$i]['control']['longitude'],$result[$i]['control']['latitude'],$result[$i]['control']['kilometricPoint'],$result[$i]['control']['controlDateTime'],$result[$i]['control']['controlUserCode'],$result[$i]['control']['checkingDateTime'],$result[$i]['control']['checkingUserCode'],$result[$i]['control']['activationStatus']);
				
				$manifest = new Manifest($person,$sale,$finance,$control);
				$mobileSaleId = $sale->getSaleId();
				// $returnArray[$i] = $manifest;
				
				if(!empty($result[$i]['control']['saleId']))
				{
					try
					{
						
						$controlType = $control->getControlType();
						$controlStatus = $control->getControlStatus();
						$checkingStatus = $control->getCheckingStatus();
						$longitude = $control->getLongitude();
						$latitude = $control->getLatitude();
						$kilometricPoint = $control->getKilometricPoint();
						$controlDateTime = $control->getControlDateTime();
						$checkingDateTime = $control->getCheckingDateTime();
						$controlUserCode = $control->getControlUserCode();
						$checkingUserCode = $control->getCheckingUserCode();
						$pseudoUser = $result[$i]['pseudoUser'];
						$historisationStatus = 0;
						
						if($sale->getActivationStatus() != 1){
							$historisationStatus = $sale->getActivationStatus() ;
						}
						
						$this->db = new dbManage();
						if($sale->getIsNew()=='O')
						{
							$firstName = $person->getFirstName();
							$name = $person->getName();
							$displayName = $name." ".$firstName;
							$countryCode = $person->getPhoneCode();
							$phoneCode = $person->getPhoneCode();
							$phoneNumber = $person->getPhoneNumber();
							$birthDate = "0001-01-01";
							// $birthDate = $person->getBirthDate();
							$email = $person->getMail();
							$status = $person->getSearchStatus();
							$vipStatut = $person->getVipStatus();
							$fidelityCode = $person->getFidelityCode();
							$cniCode = $person->getCniCode();
							$number = 1;
							$matricule = ' ';
								
							
							$sql="CALL insertPerson(:firstName, :name, :displayName, :countryCode , :phoneNumber ,:phoneCode, :birthDate, :civility, :email , :statut , :cniCode , :vipStatut , :fidelityCode , :pseudoUser , :number, :matricule);";
							
							
							$req = $this->db->__dbh->prepare($sql);
							$req->bindParam(':firstName', $firstName);
							$req->bindParam(':name', $name);
							$req->bindParam(':displayName', $displayName);
							$req->bindParam(':countryCode', $countryCode);
							$req->bindParam(':phoneNumber', $phoneNumber);
							$req->bindParam(':phoneCode', $phoneCode);
							$req->bindParam(':birthDate', $birthDate);
							$req->bindParam(':civility', $civility);
							$req->bindParam(':email', $email);
							$req->bindParam(':statut', $statut);
							$req->bindParam(':cniCode', $cniCode);
							$req->bindParam(':vipStatut', $vipStatut);
							$req->bindParam(':fidelityCode', $fidelityCode);
							$req->bindParam(':pseudoUser', $pseudoUser);
							$req->bindParam(':number', $number);
							$req->bindParam(':matricule', $matricule);
							$req->execute();
							$returnArray = $req->fetchAll(PDO::FETCH_ASSOC);
							// $req->closeCursor();
							$req = NULL;
							// $this->db->close();
							 
							$personId = $returnArray[0]['result'];
							$splitTable = explode('|',$personId);
							$personId = $splitTable[0];
							$splitTable = explode('-',$personId);
							$personId = $splitTable[0];
							$tvaCode = 'TAX19.25';
							
							$ticketCode = Booking::ticketCodeGeneration(rand(),$sale->getSeatNumber());
							
							$cipherCode = Booking::ticketCodeEncryption($ticketCode);
							$departureDate = $sale->getDepartureDate();
							$departureHour = $sale ->getDepartureHour();
							$departureCode = $sale->getDepartureCode();
							$arrivalCode = $sale->getArrivalCode();
							$trainNumber = $sale->getTrainNumber();
							$coachName = $sale->getCoachName();
							$rank = $sale->getRank();
							$seatNumber = $sale->getSeatNumber();
							$saleVipStatus = $sale->getCoachVipStatus();
							$travelType = 0;
							$saleStatus = 'O';
							$civility = $person->getCivility();
							$provenance = 'C';
							
							$paymentMode = $finance->getPaymentMode();
							$currency = $finance->getCurrency();
							$fullRateAmount = $finance->getFullRateAmount();
							$discountMotive = $finance->getDiscountMotive();
							$discountMotiveAmount = $finance->getDiscountMotiveAmount();
							$serverFootPrint = NULL;
							$promotionAmount = $finance->getPromotionAmount();;
							$paymentAmount = $finance->getPaymentAmount();
							
							$adjustmentAmount = $finance->getAdjustmentAmount();
							$adjustmentTvaCode = $finance->getAdjustmentTvaCode();
							$adjustmentTvaAmount = $finance->getAdjustmentTvaAmount();
							$outClassingAmount = $finance->getOutClassingAmount();
							$outClassingTvaCode = $finance->getOutClassingTvaCode();
							$outClassingTvaAmount = $finance->getOutClassingTvaAmount();
							
							
								
							$civility = $person->getCivility();
							
							$oldSaleId = $sale->getSaleId(); 
	
							$sql = "CALL insertSale (:personId , :ticketCode, :cipherCode , :departureDate, :departureHour, :departureCode, :arrivalCode, :trainNumber, :coachName, :rank, :seatNumber, :saleVipStatus, :travelType, :saleStatus, :pseudoUser, :provenance, :civility, :paymentMode, :currency, :fullRateAmount, :discountMotive, :discountMotiveAmount, :serverFootPrint, :promotionAmount, :paymentAmount, :adjustmentAmount, :adjustmentTvaCode, :adjustmentTvaAmount, :outClassingAmount, :outClassingTvaCode, :outClassingTvaAmount, :tvaCode,  :controlStatus, :checkingStatus, :controlType, :longitude, :latitude, :kilometricPoint, :controlDateTime, :checkingDateTime, :controlUserCode, :checkingUserCode, :oldSaleId, :historisationStatus)";
						
							// $this->db = new dbManage();
							$req = $this->db->__dbh->prepare($sql);
							$req->bindParam(':personId', $personId);
							$req->bindParam(':ticketCode', $ticketCode);
							$req->bindParam(':cipherCode', $cipherCode);
							$req->bindParam(':departureDate', $departureDate);
							$req->bindParam(':departureHour', $departureHour);
							$req->bindParam(':departureCode', $departureCode);
							$req->bindParam(':arrivalCode', $arrivalCode);
							$req->bindParam(':trainNumber', $trainNumber);
							$req->bindParam(':coachName', $coachName);
							$req->bindParam(':rank', $rank);
							$req->bindParam(':seatNumber', $seatNumber);
							$req->bindParam(':saleVipStatus', $saleVipStatus);
							$req->bindParam(':travelType', $travelType);
							$req->bindParam(':saleStatus', $saleStatus);
							$req->bindParam(':pseudoUser', $pseudoUser);
							$req->bindParam(':provenance', $provenance);
							$req->bindParam(':civility', $civility);
							$req->bindParam(':paymentMode', $paymentMode);
							$req->bindParam(':currency', $currency);
							$req->bindParam(':fullRateAmount', $fullRateAmount);
							$req->bindParam(':discountMotive', $discountMotive);
							$req->bindParam(':discountMotiveAmount', $discountMotiveAmount);
							$req->bindParam(':serverFootPrint', $serverFootPrint);
							$req->bindParam(':promotionAmount', $promotionAmount);
							$req->bindParam(':paymentAmount', $paymentAmount);
							$req->bindParam(':adjustmentAmount', $adjustmentAmount);
							$req->bindParam(':adjustmentTvaCode', $adjustmentTvaCode);
							$req->bindParam(':adjustmentTvaAmount', $adjustmentTvaAmount);
							$req->bindParam(':outClassingAmount', $outClassingAmount);
							$req->bindParam(':outClassingTvaCode', $outClassingTvaCode);
							$req->bindParam(':outClassingTvaAmount', $outClassingTvaAmount);
							$req->bindParam(':controlStatus', $controlStatus);
							$req->bindParam(':checkingStatus', $checkingStatus);
							$req->bindParam(':controlType', $controlType);
							$req->bindParam(':longitude', $longitude);
							$req->bindParam(':latitude', $latitude);
							$req->bindParam(':kilometricPoint', $kilometricPoint);
							$req->bindParam(':controlDateTime', $controlDateTime);
							$req->bindParam(':checkingDateTime', $checkingDateTime);
							$req->bindParam(':tvaCode', $tvaCode);
							$req->bindParam(':controlUserCode', $controlUserCode);
							$req->bindParam(':checkingUserCode', $checkingUserCode);
							$req->bindParam(':oldSaleId', $oldSaleId);
							$req->bindParam(':historisationStatus', $historisationStatus);
							$req->execute();
							$returnArray = $req->fetchAll(PDO::FETCH_ASSOC);
							// $this->db->close(); 
							
							$req = NULL;
							
							$returnArray = $returnArray[0];
							$saleId = $returnArray['saleId'];
							
							if($returnArray['saleId']!=-1){
								
								
								$ticketCode = Booking::ticketCodeGeneration($saleId,$sale->getSeatNumber());
								$cipherCode = Booking::ticketCodeEncryption($ticketCode); 
								
								
								$sql = "UPDATE kzvnte set 
										vncode = :ticketCode,
										vnqrco = :cipherCode,
										vnumaj = :pseudoUser,
										vndmaj = now()
										WHERE kzvnteid = :saleId; ";
								// $this->db = new dbManage();
								$req = $this->db->__dbh->prepare($sql);
								$req->bindParam(':ticketCode', $ticketCode);
								$req->bindParam(':cipherCode', $cipherCode);
								$req->bindParam(':saleId', $saleId);
								$req->bindParam(':pseudoUser', $pseudoUser);
								$req->execute();
								// $this->db->close();
								$req = NULL;
								//Envoie du SMS et Mail pour cette personne	
								
								$manifestServerResponse = new ManifestServerResponse($mobileSaleId,$saleId,'O',$ticketCode,$cipherCode);
								$manifestServerArray[$j] = $manifestServerResponse;
								$j++;
								$insertedLineNumber++;
								
							}
							else
							{
								
								$splitTab = explode('|',$returnArray['seatStatus']);
								
								
								// $manifestServerResponse = new ManifestServerResponse($mobileSaleId,$splitTab[4],'N',$splitTab[2],$splitTab[3]);
								$manifestServerResponse = new ManifestServerResponse($mobileSaleId,-1,'N','','');
								$manifestServerArray[$j] = $manifestServerResponse;
								$j++;
								$notInsertedLineNumber++;
							}
							
						}
						else
						{
							
							// echo $controlDateTime."controlDateTime <br />";
							// echo $checkingDateTime."checkingDateTime <br />";
							
							$serverSaleId = $sale->getServerSaleId();			
							$sql="CALL updateSaleInformation(:serverSaleId, :controlStatus, :checkingStatus, :controlType , :longitude , :latitude, :kilometricPoint, :controlDateTime, :checkingDateTime, :pseudoUser, :adjustmentAmount, :adjustmentTvaCode , :adjustmentTvaAmount , :outClassingAmount , :outClassingTvaCode , :outClassingTvaAmount, :controlUserCode, :checkingUserCode, :historisationStatus);";
							// $this->db = new dbManage();
							$req = $this->db->__dbh->prepare($sql);
							
							$req->bindParam(':serverSaleId', $serverSaleId);
							$req->bindParam(':controlStatus', $controlStatus);
							$req->bindParam(':checkingStatus', $checkingStatus);
							$req->bindParam(':controlType', $controlType);
							$req->bindParam(':longitude', $longitude);
							$req->bindParam(':latitude', $latitude);
							$req->bindParam(':kilometricPoint', $kilometricPoint);
							$req->bindParam(':controlDateTime', $controlDateTime);
							$req->bindParam(':checkingDateTime', $checkingDateTime);
							$req->bindParam(':pseudoUser', $pseudoUser);
							$req->bindParam(':adjustmentAmount', $adjustmentAmount);
							$req->bindParam(':adjustmentTvaCode', $adjustmentTvaCode);
							$req->bindParam(':adjustmentTvaAmount', $adjustmentTvaAmount);
							$req->bindParam(':outClassingAmount', $outClassingAmount);
							$req->bindParam(':outClassingTvaCode', $outClassingTvaCode);
							$req->bindParam(':outClassingTvaAmount', $outClassingTvaAmount);
							$req->bindParam(':controlUserCode', $controlUserCode);
							$req->bindParam(':checkingUserCode', $checkingUserCode);
							$req->bindParam(':historisationStatus', $historisationStatus); 
							$req->execute();
							$req->fetchAll(PDO::FETCH_ASSOC);
							$req = NULL;
							
							// $ticketCode = "";
							// $cipherCode = "";
							// $manifestServerResponse = new ManifestServerResponse($mobileSaleId,$saleId,'C',$ticketCode,$cipherCode);
							// $manifestServerArray[$j] = $manifestServerResponse;
							// $j++;
							$updatedLineNumber++;
						}	
							
					} catch(Exception $e){ 
						if($this->db != NULL) $this->db->close();
						echo $e->getMessage();
						echo $e->getTraceAsString();
						
					}
				} 
				
			}
			if($this->db != NULL)$this->db->close();
			$answerArray['insertedLineNumber'] = $insertedLineNumber;
			$answerArray['notInsertedLineNumber'] = $notInsertedLineNumber;
			$answerArray['updatedLineNumber'] = $updatedLineNumber;
			$answerArray['manifestServerArray'] = $manifestServerArray;
			return $answerArray;
		}catch(Exception $e){
			if($this->db != NULL)$this->db->close();
			echo $e->getMessage();
			echo $e->getTraceAsString();
			
			// return $answerArray;
		}
	}
	 */
	
	
	
	/* public function importPartialManifest($result)
	{
		$sale = null;
		$person = null;
		$control = null;
		$manifest = null;
		$returnArray = array();
		$answerArray = array();
		$manifestServerArray = array();
		$updatedLineNumber = 0;
		$insertedLineNumber = 0;
		$notInsertedLineNumber = 0;
		$j = 0;
		
		$result = json_decode($result,true);
		
		try{
			
			for ($i=0; $i<count($result); $i++)
			{
				
				$sale = new Sale($result[$i]['sale']['saleId'], $result[$i]['sale']['personId'], $result[$i]['sale']['trainNumber'],$result[$i]['sale']['coachName'], $result[$i]['sale']['seatNumber'],$result[$i]['sale']['departureCode'], $result[$i]['sale']['departureDate'],$result[$i]['sale']['departureHour'], $result[$i]['sale']['arrivalCode'],$result[$i]['sale']['ticketCode'], $result[$i]['sale']['cipherCode'],$result[$i]['sale']['coachVipStatus'],$result[$i]['sale']['saleStatus'], $result[$i]['sale']['rank'],$result[$i]['sale']['isNew'],$result[$i]['sale']['activationStatus'], $result[$i]['sale']['serverSaleId']);
				
				$person = new Person($result[$i]['person']['personId'],$result[$i]['person']['name'],$result[$i]['person']['firstName'],$result[$i]['person']['civility'],$result[$i]['person']['fidelityCode'],$result[$i]['person']['phoneCode'],$result[$i]['person']['phoneNumber'], $result[$i]['person']['cniCode'],$result[$i]['person']['email'],$result[$i]['person']['vipStatus'],$result[$i]['person']['activationStatus']);
				
				$finance = new Finance($result[$i]['finance']['financeId'],$result[$i]['finance']['saleId'],$result[$i]['finance']['currency'],$result[$i]['finance']['paymentMode'],$result[$i]['finance']['paymentAmount'], $result[$i]['finance']['fullRateAmount'],$result[$i]['finance']['discountMotive'], $result[$i]['finance']['discountMotiveAmount'],$result[$i]['finance']['promotionAmount'],$result[$i]['finance']['adjustmentAmount'],$result[$i]['finance']['adjustmentTvaCode'],$result[$i]['finance']['adjustmentTvaAmount'],$result[$i]['finance']['outClassingAmount'],$result[$i]['finance']['outClassingTvaCode'],$result[$i]['finance']['outClassingTvaAmount'],$result[$i]['finance']['activationStatus']); 
				
								
				$control = new Control($result[$i]['control']['controlId'],$result[$i]['control']['saleId'],$result[$i]['control']['controlStatus'],$result[$i]['control']['checkingStatus'],$result[$i]['control']['controlType'],$result[$i]['control']['longitude'],$result[$i]['control']['latitude'],$result[$i]['control']['kilometricPoint'],$result[$i]['control']['controlDateTime'],$result[$i]['control']['controlUserCode'],$result[$i]['control']['checkingDateTime'],$result[$i]['control']['checkingUserCode'],$result[$i]['control']['activationStatus']);
				
				$manifest = new Manifest($person,$sale,$finance,$control);
				$mobileSaleId = $sale->getSaleId();
				// $returnArray[$i] = $manifest;
				
				if(!empty($result[$i]['control']['saleId']))
				{
					try
					{
						
						$controlType = $control->getControlType();
						$controlStatus = $control->getControlStatus();
						$checkingStatus = $control->getCheckingStatus();
						$longitude = $control->getLongitude();
						$latitude = $control->getLatitude();
						$kilometricPoint = $control->getKilometricPoint();
						$controlDateTime = $control->getControlDateTime();
						$checkingDateTime = $control->getCheckingDateTime();
						$controlUserCode = $control->getControlUserCode();
						$checkingUserCode = $control->getCheckingUserCode();
						$pseudoUser = $result[$i]['pseudoUser'];
						$historisationStatus = 0;
						
						if($sale->getActivationStatus() != 1){
							$historisationStatus = $sale->getActivationStatus() ;
						}
						
						$this->db = new dbManage();
						if($sale->getIsNew()=='O')
						{
							$firstName = $person->getFirstName();
							$name = $person->getName();
							$displayName = $name." ".$firstName;
							$countryCode = $person->getPhoneCode();
							$phoneCode = $person->getPhoneCode();
							$phoneNumber = $person->getPhoneNumber();
							$birthDate = "0001-01-01";
							// $birthDate = $person->getBirthDate();
							$email = $person->getMail();
							$status = $person->getSearchStatus();
							$vipStatut = $person->getVipStatus();
							$fidelityCode = $person->getFidelityCode();
							$cniCode = $person->getCniCode();
							$number = 1;
							$matricule = ' ';
								
							
							$sql="CALL insertPerson(:firstName, :name, :displayName, :countryCode , :phoneNumber ,:phoneCode, :birthDate, :civility, :email , :statut , :cniCode , :vipStatut , :fidelityCode , :pseudoUser , :number, :matricule);";
							
							
							$req = $this->db->__dbh->prepare($sql);
							$req->bindParam(':firstName', $firstName);
							$req->bindParam(':name', $name);
							$req->bindParam(':displayName', $displayName);
							$req->bindParam(':countryCode', $countryCode);
							$req->bindParam(':phoneNumber', $phoneNumber);
							$req->bindParam(':phoneCode', $phoneCode);
							$req->bindParam(':birthDate', $birthDate);
							$req->bindParam(':civility', $civility);
							$req->bindParam(':email', $email);
							$req->bindParam(':statut', $statut);
							$req->bindParam(':cniCode', $cniCode);
							$req->bindParam(':vipStatut', $vipStatut);
							$req->bindParam(':fidelityCode', $fidelityCode);
							$req->bindParam(':pseudoUser', $pseudoUser);
							$req->bindParam(':number', $number);
							$req->bindParam(':matricule', $matricule);
							$req->execute();
							$returnArray = $req->fetchAll(PDO::FETCH_ASSOC);
							// $req->closeCursor();
							$req = NULL;
							// $this->db->close();
							 
							$personId = $returnArray[0]['result'];
							$splitTable = explode('|',$personId);
							$personId = $splitTable[0];
							$splitTable = explode('-',$personId);
							$personId = $splitTable[0];
							$tvaCode = 'TAX19.25';
							
							$ticketCode = Booking::ticketCodeGeneration(rand(),$sale->getSeatNumber());
							
							$cipherCode = Booking::ticketCodeEncryption($ticketCode);
							$departureDate = $sale->getDepartureDate();
							$departureHour = $sale ->getDepartureHour();
							$departureCode = $sale->getDepartureCode();
							$arrivalCode = $sale->getArrivalCode();
							$trainNumber = $sale->getTrainNumber();
							$coachName = $sale->getCoachName();
							$rank = $sale->getRank();
							$seatNumber = $sale->getSeatNumber();
							$saleVipStatus = $sale->getCoachVipStatus();
							$travelType = 0;
							$saleStatus = 'O';
							$civility = $person->getCivility();
							$provenance = 'C';
							
							$paymentMode = $finance->getPaymentMode();
							$currency = $finance->getCurrency();
							$fullRateAmount = $finance->getFullRateAmount();
							$discountMotive = $finance->getDiscountMotive();
							$discountMotiveAmount = $finance->getDiscountMotiveAmount();
							$serverFootPrint = NULL;
							$promotionAmount = $finance->getPromotionAmount();;
							$paymentAmount = $finance->getPaymentAmount();
							
							$adjustmentAmount = $finance->getAdjustmentAmount();
							$adjustmentTvaCode = $finance->getAdjustmentTvaCode();
							$adjustmentTvaAmount = $finance->getAdjustmentTvaAmount();
							$outClassingAmount = $finance->getOutClassingAmount();
							$outClassingTvaCode = $finance->getOutClassingTvaCode();
							$outClassingTvaAmount = $finance->getOutClassingTvaAmount();
							
							
								
							$civility = $person->getCivility();
							
							$oldSaleId = $sale->getSaleId(); 
	
						 	 
							$sql = "CALL insertSale (:personId , :ticketCode, :cipherCode , :departureDate, :departureHour, :departureCode, :arrivalCode, :trainNumber, :coachName, :rank, :seatNumber, :saleVipStatus, :travelType, :saleStatus, :pseudoUser, :provenance, :civility, :paymentMode, :currency, :fullRateAmount, :discountMotive, :discountMotiveAmount, :serverFootPrint, :promotionAmount, :paymentAmount, :adjustmentAmount, :adjustmentTvaCode, :adjustmentTvaAmount, :outClassingAmount, :outClassingTvaCode, :outClassingTvaAmount, :tvaCode,  :controlStatus, :checkingStatus, :controlType, :longitude, :latitude, :kilometricPoint, :controlDateTime, :checkingDateTime, :controlUserCode, :checkingUserCode, :oldSaleId, :historisationStatus)";
						
							// $this->db = new dbManage();
							$req = $this->db->__dbh->prepare($sql);
							$req->bindParam(':personId', $personId);
							$req->bindParam(':ticketCode', $ticketCode);
							$req->bindParam(':cipherCode', $cipherCode);
							$req->bindParam(':departureDate', $departureDate);
							$req->bindParam(':departureHour', $departureHour);
							$req->bindParam(':departureCode', $departureCode);
							$req->bindParam(':arrivalCode', $arrivalCode);
							$req->bindParam(':trainNumber', $trainNumber);
							$req->bindParam(':coachName', $coachName);
							$req->bindParam(':rank', $rank);
							$req->bindParam(':seatNumber', $seatNumber);
							$req->bindParam(':saleVipStatus', $saleVipStatus);
							$req->bindParam(':travelType', $travelType);
							$req->bindParam(':saleStatus', $saleStatus);
							$req->bindParam(':pseudoUser', $pseudoUser);
							$req->bindParam(':provenance', $provenance);
							$req->bindParam(':civility', $civility);
							$req->bindParam(':paymentMode', $paymentMode);
							$req->bindParam(':currency', $currency);
							$req->bindParam(':fullRateAmount', $fullRateAmount);
							$req->bindParam(':discountMotive', $discountMotive);
							$req->bindParam(':discountMotiveAmount', $discountMotiveAmount);
							$req->bindParam(':serverFootPrint', $serverFootPrint);
							$req->bindParam(':promotionAmount', $promotionAmount);
							$req->bindParam(':paymentAmount', $paymentAmount);
							$req->bindParam(':adjustmentAmount', $adjustmentAmount);
							$req->bindParam(':adjustmentTvaCode', $adjustmentTvaCode);
							$req->bindParam(':adjustmentTvaAmount', $adjustmentTvaAmount);
							$req->bindParam(':outClassingAmount', $outClassingAmount);
							$req->bindParam(':outClassingTvaCode', $outClassingTvaCode);
							$req->bindParam(':outClassingTvaAmount', $outClassingTvaAmount);
							$req->bindParam(':controlStatus', $controlStatus);
							$req->bindParam(':checkingStatus', $checkingStatus);
							$req->bindParam(':controlType', $controlType);
							$req->bindParam(':longitude', $longitude);
							$req->bindParam(':latitude', $latitude);
							$req->bindParam(':kilometricPoint', $kilometricPoint);
							$req->bindParam(':controlDateTime', $controlDateTime);
							$req->bindParam(':checkingDateTime', $checkingDateTime);
							$req->bindParam(':tvaCode', $tvaCode);
							$req->bindParam(':controlUserCode', $controlUserCode);
							$req->bindParam(':checkingUserCode', $checkingUserCode);
							$req->bindParam(':oldSaleId', $oldSaleId);
							$req->bindParam(':historisationStatus', $historisationStatus);
							$req->execute();
							$returnArray = $req->fetchAll(PDO::FETCH_ASSOC);
							 
							$req = NULL;
							
							$returnArray = $returnArray[0];
							$saleId = $returnArray['saleId'];
							$ticketCode = Booking::ticketCodeGeneration($saleId,$sale->getSeatNumber());
							$cipherCode = Booking::ticketCodeEncryption($ticketCode); 
							
							
							$sql = "UPDATE kzvnte set 
									vncode = :ticketCode,
									vnqrco = :cipherCode,
									vnumaj = :pseudoUser,
									vndmaj = now()
									WHERE kzvnteid = :saleId; ";
							// $this->db = new dbManage();
							$req = $this->db->__dbh->prepare($sql);
							$req->bindParam(':ticketCode', $ticketCode);
							$req->bindParam(':cipherCode', $cipherCode);
							$req->bindParam(':saleId', $saleId);
							$req->bindParam(':pseudoUser', $pseudoUser);
							$req->execute();
							$req = NULL;
							
							if($returnArray['seatStatus']!='A'){
								 
								//Envoie du SMS et Mail pour cette personne	
								
								$manifestServerResponse = new ManifestServerResponse($mobileSaleId,$saleId,'O',$ticketCode,$cipherCode);
								$manifestServerArray[$j] = $manifestServerResponse;
								$j++;
								$insertedLineNumber++;
								
							}
							else
							{
								 
								$manifestServerResponse = new ManifestServerResponse($mobileSaleId,$saleId,$returnArray['seatStatus'],$ticketCode,$cipherCode);
								$manifestServerArray[$j] = $manifestServerResponse;
								$j++;
								$notInsertedLineNumber++;
							}
							
						}
						else
						{
							
							// echo $controlDateTime."controlDateTime <br />";
							// echo $checkingDateTime."checkingDateTime <br />";
							
							$serverSaleId = $sale->getServerSaleId();			
							$sql="CALL updateSaleInformation(:serverSaleId, :controlStatus, :checkingStatus, :controlType , :longitude , :latitude, :kilometricPoint, :controlDateTime, :checkingDateTime, :pseudoUser, :adjustmentAmount, :adjustmentTvaCode , :adjustmentTvaAmount , :outClassingAmount , :outClassingTvaCode , :outClassingTvaAmount, :controlUserCode, :checkingUserCode, :historisationStatus);";
							// $this->db = new dbManage();
							$req = $this->db->__dbh->prepare($sql);
							
							$req->bindParam(':serverSaleId', $serverSaleId);
							$req->bindParam(':controlStatus', $controlStatus);
							$req->bindParam(':checkingStatus', $checkingStatus);
							$req->bindParam(':controlType', $controlType);
							$req->bindParam(':longitude', $longitude);
							$req->bindParam(':latitude', $latitude);
							$req->bindParam(':kilometricPoint', $kilometricPoint);
							$req->bindParam(':controlDateTime', $controlDateTime);
							$req->bindParam(':checkingDateTime', $checkingDateTime);
							$req->bindParam(':pseudoUser', $pseudoUser);
							$req->bindParam(':adjustmentAmount', $adjustmentAmount);
							$req->bindParam(':adjustmentTvaCode', $adjustmentTvaCode);
							$req->bindParam(':adjustmentTvaAmount', $adjustmentTvaAmount);
							$req->bindParam(':outClassingAmount', $outClassingAmount);
							$req->bindParam(':outClassingTvaCode', $outClassingTvaCode);
							$req->bindParam(':outClassingTvaAmount', $outClassingTvaAmount);
							$req->bindParam(':controlUserCode', $controlUserCode);
							$req->bindParam(':checkingUserCode', $checkingUserCode);
							$req->bindParam(':historisationStatus', $historisationStatus); 
							$req->execute();
							$req->fetchAll(PDO::FETCH_ASSOC);
							$req = NULL;
							
							 
							$updatedLineNumber++;
						}	
							
					} catch(Exception $e){  
						 
						if(isset($this->db) && $this->db != NULL)$this->db->close(); 
						writeLog($e->getMessage(), '','errorLog.txt'); 
						writeLog($e->getTraceAsString(), '','errorLog.txt');
						
					}
				} 
				
			}
			if(isset($this->db) && $this->db != NULL)$this->db->close();
			$answerArray['insertedLineNumber'] = $insertedLineNumber;
			$answerArray['notInsertedLineNumber'] = $notInsertedLineNumber;
			$answerArray['updatedLineNumber'] = $updatedLineNumber;
			$answerArray['manifestServerArray'] = $manifestServerArray;
			return $answerArray;
		}catch(Exception $e){
			
			if(isset($this->db) && $this->db != NULL)$this->db->close(); 
			writeLog($e->getMessage(), '','errorLog.txt'); 
			writeLog($e->getTraceAsString(), '','errorLog.txt');
			
			return "error";
		}
	} */
	
	
	public function importPartialManifest($result)
	{
		$sale = null;
		$person = null;
		$control = null;
		$manifest = null;
		$returnArray = array();
		$answerArray = array();
		$manifestServerArray = array();
		$updatedLineNumber = 0;
		$insertedLineNumber = 0;
		$notInsertedLineNumber = 0;
		$j = 0;
		
		$result = json_decode($result,true);
		
		// return $result; 
		
		try{
			
			for ($i=0; $i<count($result); $i++) 
			{
				
				$sale = new Sale($result[$i]['sale']['saleId'], $result[$i]['sale']['personId'], $result[$i]['sale']['trainNumber'],$result[$i]['sale']['coachName'], $result[$i]['sale']['seatNumber'],$result[$i]['sale']['departureCode'], $result[$i]['sale']['departureDate'],$result[$i]['sale']['departureHour'], $result[$i]['sale']['arrivalCode'],$result[$i]['sale']['ticketCode'], $result[$i]['sale']['cipherCode'],$result[$i]['sale']['coachVipStatus'],$result[$i]['sale']['saleStatus'], $result[$i]['sale']['rank'],$result[$i]['sale']['isNew'],$result[$i]['sale']['activationStatus'], $result[$i]['sale']['serverSaleId']);
				// $sale = new Sale($result[$i]['sale']['saleId'], $result[$i]['sale']['personId'], "TESTTEST",$result[$i]['sale']['coachName'], $result[$i]['sale']['seatNumber'],$result[$i]['sale']['departureCode'], $result[$i]['sale']['departureDate'],$result[$i]['sale']['departureHour'], $result[$i]['sale']['arrivalCode'],$result[$i]['sale']['ticketCode'], $result[$i]['sale']['cipherCode'],$result[$i]['sale']['coachVipStatus'],$result[$i]['sale']['saleStatus'], $result[$i]['sale']['rank'],$result[$i]['sale']['isNew'],$result[$i]['sale']['activationStatus'], $result[$i]['sale']['serverSaleId']);
				
				$person = new Person($result[$i]['person']['personId'],$result[$i]['person']['name'],$result[$i]['person']['firstName'],$result[$i]['person']['civility'],$result[$i]['person']['fidelityCode'],$result[$i]['person']['phoneCode'],$result[$i]['person']['phoneNumber'], $result[$i]['person']['cniCode'],$result[$i]['person']['email'],$result[$i]['person']['vipStatus'],$result[$i]['person']['activationStatus']);
				
				$finance = new Finance($result[$i]['finance']['financeId'],$result[$i]['finance']['saleId'],$result[$i]['finance']['currency'],$result[$i]['finance']['paymentMode'],$result[$i]['finance']['paymentAmount'], $result[$i]['finance']['fullRateAmount'],$result[$i]['finance']['discountMotive'], $result[$i]['finance']['discountMotiveAmount'],$result[$i]['finance']['promotionAmount'],$result[$i]['finance']['adjustmentAmount'],$result[$i]['finance']['adjustmentTvaCode'],$result[$i]['finance']['adjustmentTvaAmount'],$result[$i]['finance']['outClassingAmount'],$result[$i]['finance']['outClassingTvaCode'],$result[$i]['finance']['outClassingTvaAmount'],$result[$i]['finance']['activationStatus']); 
				
								
				$control = new Control($result[$i]['control']['controlId'],$result[$i]['control']['saleId'],$result[$i]['control']['controlStatus'],$result[$i]['control']['checkingStatus'],$result[$i]['control']['controlType'],$result[$i]['control']['longitude'],$result[$i]['control']['latitude'],$result[$i]['control']['kilometricPoint'],$result[$i]['control']['controlDateTime'],$result[$i]['control']['controlUserCode'],$result[$i]['control']['checkingDateTime'],$result[$i]['control']['checkingUserCode'],$result[$i]['control']['activationStatus']);
				
				$manifest = new Manifest($person,$sale,$finance,$control);
				$mobileSaleId = $sale->getSaleId();
				// $returnArray[$i] = $manifest;
				$this->db = new dbManage();
				if(!empty($result[$i]['control']['saleId']))
				{
					try
					{
						
						$controlType = $control->getControlType();
						$controlStatus = $control->getControlStatus();
						$checkingStatus = $control->getCheckingStatus();
						$longitude = $control->getLongitude();
						$latitude = $control->getLatitude();
						$kilometricPoint = $control->getKilometricPoint();
						$controlDateTime = $control->getControlDateTime();
						$checkingDateTime = $control->getCheckingDateTime();
						$controlUserCode = $control->getControlUserCode();
						$checkingUserCode = $control->getCheckingUserCode();
						$pseudoUser = $result[$i]['pseudoUser'];
						$historisationStatus = 0;
						
						if($sale->getActivationStatus() != 1){
							$historisationStatus = $sale->getActivationStatus() ;
						}
						
						
						if($sale->getIsNew()=='O')
						{
							$firstName = $person->getFirstName();
							$name = $person->getName();
							$displayName = $name." ".$firstName;
							$countryCode = $person->getPhoneCode();
							$phoneCode = $person->getPhoneCode();
							$phoneNumber = $person->getPhoneNumber();
							$birthDate = "0001-01-01";
							// $birthDate = $person->getBirthDate();
							$email = $person->getMail();
							$status = $person->getSearchStatus();
							$vipStatut = $person->getVipStatus();
							$fidelityCode = $person->getFidelityCode();
							$cniCode = $person->getCniCode();
							$number = 1;
							$matricule = ' ';
								
							
							$sql="CALL insertPerson(:firstName, :name, :displayName, :countryCode , :phoneNumber ,:phoneCode, :birthDate, :civility, :email , :statut , :cniCode , :vipStatut , :fidelityCode , :pseudoUser , :number, :matricule);";
							
							
							$req = $this->db->__dbh->prepare($sql);
							$req->bindParam(':firstName', $firstName);
							$req->bindParam(':name', $name);
							$req->bindParam(':displayName', $displayName);
							$req->bindParam(':countryCode', $countryCode);
							$req->bindParam(':phoneNumber', $phoneNumber);
							$req->bindParam(':phoneCode', $phoneCode);
							$req->bindParam(':birthDate', $birthDate);
							$req->bindParam(':civility', $civility);
							$req->bindParam(':email', $email);
							$req->bindParam(':statut', $statut);
							$req->bindParam(':cniCode', $cniCode);
							$req->bindParam(':vipStatut', $vipStatut);
							$req->bindParam(':fidelityCode', $fidelityCode);
							$req->bindParam(':pseudoUser', $pseudoUser);
							$req->bindParam(':number', $number);
							$req->bindParam(':matricule', $matricule);
							$req->execute();
							$returnArray = $req->fetchAll(PDO::FETCH_ASSOC);
							// $req->closeCursor();
							$req = NULL;
							// $this->db->close();
							 
							$personId = $returnArray[0]['result'];
							$splitTable = explode('|',$personId);
							$personId = $splitTable[0];
							$splitTable = explode('-',$personId);
							$personId = $splitTable[0];
							$tvaCode = 'TAX19.25';
							
							$ticketCode = Booking::ticketCodeGeneration(rand(),$sale->getSeatNumber());
							
							$cipherCode = Booking::ticketCodeEncryption($ticketCode);
							$departureDate = $sale->getDepartureDate();
							$departureHour = $sale ->getDepartureHour();
							$departureCode = $sale->getDepartureCode();
							$arrivalCode = $sale->getArrivalCode();
							$trainNumber = $sale->getTrainNumber();
							$coachName = $sale->getCoachName();
							$rank = $sale->getRank();
							$seatNumber = $sale->getSeatNumber();
							$saleVipStatus = $sale->getCoachVipStatus();
							$travelType = 0;
							$saleStatus = 'O';
							$civility = $person->getCivility();
							$provenance = 'C';
							
							$paymentMode = $finance->getPaymentMode();
							$currency = $finance->getCurrency();
							$fullRateAmount = $finance->getFullRateAmount();
							$discountMotive = $finance->getDiscountMotive();
							$discountMotiveAmount = $finance->getDiscountMotiveAmount();
							$serverFootPrint = NULL;
							$promotionAmount = $finance->getPromotionAmount();;
							$paymentAmount = $finance->getPaymentAmount();
							
							$adjustmentAmount = $finance->getAdjustmentAmount();
							$adjustmentTvaCode = $finance->getAdjustmentTvaCode();
							$adjustmentTvaAmount = $finance->getAdjustmentTvaAmount();
							$outClassingAmount = $finance->getOutClassingAmount();
							$outClassingTvaCode = $finance->getOutClassingTvaCode();
							$outClassingTvaAmount = $finance->getOutClassingTvaAmount();
							
							
								
							$civility = $person->getCivility();
							
							$oldSaleId = $sale->getSaleId(); 
	
						 	/* echo procedureParameter($personId , $ticketCode, $cipherCode , $departureDate, $departureHour, $departureCode, $arrivalCode, $trainNumber, $coachName, $rank, $seatNumber, $saleVipStatus, $travelType, $saleStatus, $pseudoUser, $provenance, $civility, $paymentMode, $currency, $fullRateAmount, $discountMotive, $discountMotiveAmount, $serverFootPrint, $promotionAmount, $paymentAmount, $adjustmentAmount, $adjustmentTvaCode, $adjustmentTvaAmount, $outClassingAmount, $outClassingTvaCode, $outClassingTvaAmount, $tvaCode,  $controlStatus, $checkingStatus, $controlType, $longitude, $latitude, $kilometricPoint, $controlDateTime, $checkingDateTime, $controlUserCode, $checkingUserCode, $oldSaleId, $historisationStatus);
							return "";   */
							$sql = "CALL insertSale (:personId , :ticketCode, :cipherCode , :departureDate, :departureHour, :departureCode, :arrivalCode, :trainNumber, :coachName, :rank, :seatNumber, :saleVipStatus, :travelType, :saleStatus, :pseudoUser, :provenance, :civility, :paymentMode, :currency, :fullRateAmount, :discountMotive, :discountMotiveAmount, :serverFootPrint, :promotionAmount, :paymentAmount, :adjustmentAmount, :adjustmentTvaCode, :adjustmentTvaAmount, :outClassingAmount, :outClassingTvaCode, :outClassingTvaAmount, :tvaCode,  :controlStatus, :checkingStatus, :controlType, :longitude, :latitude, :kilometricPoint, :controlDateTime, :checkingDateTime, :controlUserCode, :checkingUserCode, :oldSaleId, :historisationStatus)";
						
							// $this->db = new dbManage();
							$req = $this->db->__dbh->prepare($sql);
							$req->bindParam(':personId', $personId);
							$req->bindParam(':ticketCode', $ticketCode);
							$req->bindParam(':cipherCode', $cipherCode);
							$req->bindParam(':departureDate', $departureDate);
							$req->bindParam(':departureHour', $departureHour);
							$req->bindParam(':departureCode', $departureCode);
							$req->bindParam(':arrivalCode', $arrivalCode);
							$req->bindParam(':trainNumber', $trainNumber);
							$req->bindParam(':coachName', $coachName);
							$req->bindParam(':rank', $rank);
							$req->bindParam(':seatNumber', $seatNumber);
							$req->bindParam(':saleVipStatus', $saleVipStatus);
							$req->bindParam(':travelType', $travelType);
							$req->bindParam(':saleStatus', $saleStatus);
							$req->bindParam(':pseudoUser', $pseudoUser);
							$req->bindParam(':provenance', $provenance);
							$req->bindParam(':civility', $civility);
							$req->bindParam(':paymentMode', $paymentMode);
							$req->bindParam(':currency', $currency);
							$req->bindParam(':fullRateAmount', $fullRateAmount);
							$req->bindParam(':discountMotive', $discountMotive);
							$req->bindParam(':discountMotiveAmount', $discountMotiveAmount);
							$req->bindParam(':serverFootPrint', $serverFootPrint);
							$req->bindParam(':promotionAmount', $promotionAmount);
							$req->bindParam(':paymentAmount', $paymentAmount);
							$req->bindParam(':adjustmentAmount', $adjustmentAmount);
							$req->bindParam(':adjustmentTvaCode', $adjustmentTvaCode);
							$req->bindParam(':adjustmentTvaAmount', $adjustmentTvaAmount);
							$req->bindParam(':outClassingAmount', $outClassingAmount);
							$req->bindParam(':outClassingTvaCode', $outClassingTvaCode);
							$req->bindParam(':outClassingTvaAmount', $outClassingTvaAmount);
							$req->bindParam(':controlStatus', $controlStatus);
							$req->bindParam(':checkingStatus', $checkingStatus);
							$req->bindParam(':controlType', $controlType);
							$req->bindParam(':longitude', $longitude);
							$req->bindParam(':latitude', $latitude);
							$req->bindParam(':kilometricPoint', $kilometricPoint);
							$req->bindParam(':controlDateTime', $controlDateTime);
							$req->bindParam(':checkingDateTime', $checkingDateTime);
							$req->bindParam(':tvaCode', $tvaCode);
							$req->bindParam(':controlUserCode', $controlUserCode);
							$req->bindParam(':checkingUserCode', $checkingUserCode);
							$req->bindParam(':oldSaleId', $oldSaleId);
							$req->bindParam(':historisationStatus', $historisationStatus);
							$req->execute();
							$returnArray = $req->fetchAll(PDO::FETCH_ASSOC);
							// $this->db->close(); 
							
							$req = NULL;
							
							$returnArray = $returnArray[0];
							$saleId = $returnArray['saleId'];
							$ticketCode = Booking::ticketCodeGeneration($saleId,$sale->getSeatNumber());
							$cipherCode = Booking::ticketCodeEncryption($ticketCode); 
							
							
							$sql = "UPDATE kzvnte set 
									vncode = :ticketCode,
									vnqrco = :cipherCode,
									vnumaj = :pseudoUser,
									vndmaj = now()
									WHERE kzvnteid = :saleId; ";
							// $this->db = new dbManage();
							$req = $this->db->__dbh->prepare($sql);
							$req->bindParam(':ticketCode', $ticketCode);
							$req->bindParam(':cipherCode', $cipherCode);
							$req->bindParam(':saleId', $saleId);
							$req->bindParam(':pseudoUser', $pseudoUser);
							$req->execute();
							$req = NULL;
							
							if($returnArray['seatStatus']!='A'){
								 
								//Envoie du SMS et Mail pour cette personne	
								
								$manifestServerResponse = new ManifestServerResponse($mobileSaleId,$saleId,'O',$ticketCode,$cipherCode);
								$manifestServerArray[$j] = $manifestServerResponse;
								$j++;
								$insertedLineNumber++;
								
							}
							else
							{
								
								// $manifestServerResponse = new ManifestServerResponse($mobileSaleId,$splitTab[4],'N',$splitTab[2],$splitTab[3]);
								$manifestServerResponse = new ManifestServerResponse($mobileSaleId,$saleId,$returnArray['seatStatus'],$ticketCode,$cipherCode);
								$manifestServerArray[$j] = $manifestServerResponse;
								$j++;
								$notInsertedLineNumber++;
							}
							
						}
						else
						{
							
							// echo $controlDateTime."controlDateTime <br />";
							// echo $checkingDateTime."checkingDateTime <br />";
							
							$serverSaleId = $sale->getServerSaleId();			
							$sql="CALL updateSaleInformation(:serverSaleId, :controlStatus, :checkingStatus, :controlType , :longitude , :latitude, :kilometricPoint, :controlDateTime, :checkingDateTime, :pseudoUser, :adjustmentAmount, :adjustmentTvaCode , :adjustmentTvaAmount , :outClassingAmount , :outClassingTvaCode , :outClassingTvaAmount, :controlUserCode, :checkingUserCode, :historisationStatus);";
							// $this->db = new dbManage();
							$req = $this->db->__dbh->prepare($sql);
							
							$req->bindParam(':serverSaleId', $serverSaleId);
							$req->bindParam(':controlStatus', $controlStatus);
							$req->bindParam(':checkingStatus', $checkingStatus);
							$req->bindParam(':controlType', $controlType);
							$req->bindParam(':longitude', $longitude);
							$req->bindParam(':latitude', $latitude);
							$req->bindParam(':kilometricPoint', $kilometricPoint);
							$req->bindParam(':controlDateTime', $controlDateTime);
							$req->bindParam(':checkingDateTime', $checkingDateTime);
							$req->bindParam(':pseudoUser', $pseudoUser);
							$req->bindParam(':adjustmentAmount', $adjustmentAmount);
							$req->bindParam(':adjustmentTvaCode', $adjustmentTvaCode);
							$req->bindParam(':adjustmentTvaAmount', $adjustmentTvaAmount);
							$req->bindParam(':outClassingAmount', $outClassingAmount);
							$req->bindParam(':outClassingTvaCode', $outClassingTvaCode);
							$req->bindParam(':outClassingTvaAmount', $outClassingTvaAmount);
							$req->bindParam(':controlUserCode', $controlUserCode);
							$req->bindParam(':checkingUserCode', $checkingUserCode);
							$req->bindParam(':historisationStatus', $historisationStatus); 
							$req->execute();
							$req->fetchAll(PDO::FETCH_ASSOC);
							$req = NULL;
							
							// $ticketCode = "";
							// $cipherCode = "";
							// $manifestServerResponse = new ManifestServerResponse($mobileSaleId,$saleId,'C',$ticketCode,$cipherCode);
							// $manifestServerArray[$j] = $manifestServerResponse;
							// $j++;
							$updatedLineNumber++;
						}	
							
					} catch(Exception $e){ 
						if(isset($this->db) && $this->db != NULL)$this->db->close();
						// echo $e->getMessage();
						writeLog($e->getMessage(), '','errorLog.txt');
						// echo $e->getTraceAsString();
						writeLog($e->getTraceAsString(), '','errorLog.txt');
						
					}
				} 
				
			}
			if(isset($this->db) && $this->db != NULL)$this->db->close();
			$answerArray['insertedLineNumber'] = $insertedLineNumber;
			$answerArray['notInsertedLineNumber'] = $notInsertedLineNumber;
			$answerArray['updatedLineNumber'] = $updatedLineNumber;
			$answerArray['manifestServerArray'] = $manifestServerArray;
			return $answerArray;
		}catch(Exception $e){
			/* if($this->db != NULL)$this->db->close();
			echo $e->getMessage();
			echo $e->getTraceAsString(); */
			if(isset($this->db) && $this->db != NULL)$this->db->close();
			// echo $e->getMessage();
			writeLog($e->getMessage(), '','errorLog.txt');
			// echo $e->getTraceAsString();
			writeLog($e->getTraceAsString(), '','errorLog.txt');
			
			return "error";
		}
	}
	
	
	public function importPromotion($result)
	{
		
		$promotion = null;
		$result = json_decode($result,true);
		
		try{
			$pseudoUser =   $result['pseudoUser'];
			$result = $result['promotion'];
			$motive = getListValue('ANNULATION','A');
			for ($i=0; $i<count($result); $i++)
			{
				$promotion = new Promotion($result[$i]['id'], $result[$i]['code'], $result[$i]['generatedSaleId'],$result[$i]['usedSaleId'], $result[$i]['amount'],$result[$i]['activationStatus']);
				$code = $promotion->getCode();
				$generatedSaleId = $promotion->getGeneratedSaleId();
				$usedSaleId = $promotion->getUsedSaleId();
				$amount = $promotion->getAmount();
				
				$sql = "INSERT INTO kzprom (pocode, pomoti, poveni, povent, poutil, pomont, poucre, podcre, poddep, podfin) 
				VALUES (:code, :motive, :generatedSaleId, :usedSaleId, 'U',:amount, :pseudoUser, NOW(), NOW(), NOW())";
				$this->db = new dbManage();
				$req = $this->db->__dbh->prepare($sql);
				$req->bindParam(':code', $code );
				$req->bindParam(':motive', $motive);
				$req->bindParam(':generat edSaleId', $generatedSaleId);
				$req->bindParam(':usedSaleId', $usedSaleId);
				$req->bindParam(':amount', $amount);
				$req->bindParam(':pseudoUser', $pseudoUser);
				$req->execute();
				$returnArray = $req->fetchAll(PDO::FETCH_ASSOC);
				$this->db->close();
			}	
				// $returnArray[$i] = $manifest;
				
				
			return "OK"; 
		}catch(Exception $e){
			echo $e->getMessage();
			echo $e->getTraceAsString();
			return "KO"; 
		}
	}
	
	public function commit($parameter){
		
		try{
			
			$sql="INSERT INTO kzcmit (cmuser, cmetat, cmddeb, cmactn, cmucre, cmdcre) 
				VALUE(:tempPseudoUser, 'E', now(), 'CTLCOMMIT', :pseudoUser, now());";
			$result = json_decode($parameter,true);
			$pseudoUser = $result['pseudoUser'];
			$trainNumber = $result['trainNumber'];
			$tempPseudoUser = date("mdhms").$pseudoUser;
			$this->db = new dbManage();
			$req = $this->db->__dbh->prepare($sql);
			$req->bindParam(':tempPseudoUser',$tempPseudoUser);
			$req->bindParam(':pseudoUser',$pseudoUser);
			$req->execute();
			$this->db->close();
				
			$answerArray['importManifest'] = $this->importManifest($result['saleInformation'],false, true);
			
			$sql = "SELECT cmdfin AS cmdfin, COUNT(*) as nbre
					FROM kzcmit 
					WHERE cmuser = :pseudoUser 
					and CMACTN = 'MANIFESTEXPORTATION'
					ORDER BY cmdfin DESC 
					LIMIT 1; ";
			$this->db = new dbManage();
			$req = $this->db->__dbh->prepare($sql);
			$req->bindParam(':pseudoUser', $result['pseudoUser']);
			$returnArray = $req->execute();
			$this->db->close();
			
			if($returnArray[0]['nbre'] == 1){
				$date = $returnArray[0]['cmdfin'];
			}else{
				$date = date('Y-d-m h:m:s');
			}
			
			$answerArray['getManifest'] = $this->getManifest($trainNumber,$pseudoUser,$date);
			
			
			$sql=" UPDATE kzcmit set cmetat = 'T', cmtfin = 'S', cmucre = :pseudoUser, cmdfin = now(), cmumaj = 'KZADMIN'
			WHERE cmucre = :tempPseudoUser; ";
			$this->db = new dbManage();
			$req = $this->db->__dbh->prepare($sql);
			$req->bindParam(':tempPseudoUser', $tempPseudoUser);
			$req->bindParam(':pseudoUser', $pseudoUser);
			$req->execute();
			$this->db->close();
			
			return $answerArray;
		}catch(Exception $e){
			echo $e->getMessage();
			echo $e->getTraceAsString();
			return -1;
		}
	}
	
	
}

?>