<?php
class Monitoring {
	private $db;
	private $transactions;
	private $days;
	public function __construct() {
		$this->db = $GLOBALS ['superGlobalDb'];
		$this->days = 14;
		$this->getTransactions ();
		$this->sendMessage ();
	}
	public function getTransactions() {
		$tr = new EcmTransaction (); // moduł cennik
		$transactions = $tr->get_full_list ('', " settled!=1 and type=0 and payment_date <= DATE_SUB(SYSDATE(), INTERVAL " . $this->days . " DAY) " );
		$this->transactions = array ();
		foreach ( $transactions as $transaction ) {
			$a= new Account();
			$a->retrieve($transaction->parent_id);
	
			$this->transactions [$a->index_dbf] ['total'] += $transaction->value;
			$this->transactions [$a->index_dbf] ['records'] [] = $transaction;
			$this->transactions [$a->index_dbf] ['name'] [] = $transaction->name;
		}
	}
	public function logIt($parent_id, $number, $msg, $total, $transactions) {
		
		$id=create_guid();

		$this->db->query("insert into send_report values ('".$id."','".$parent_id."','".$number."','".$msg."','".$total."','".date('Y-m-d H:i:s')."')");
	
		foreach ($transactions as $transaction){
	
			$id2=create_guid();

			$this->db->query("insert into send_report_items values('".$id2."','".$id."','".$transaction->name."','".$transaction->value."','".$transaction->payment_date."')");
		}
	}
	function sms_send($params, $backup = false ) {
	
		static $content;
	
		if($backup == true){
			$url = 'https://api2.smsapi.pl/sms.do';
		}else{
			$url = 'https://api.smsapi.pl/sms.do';
		}
	
		$c = curl_init();
		curl_setopt( $c, CURLOPT_URL, $url );
		curl_setopt( $c, CURLOPT_POST, true );
		curl_setopt( $c, CURLOPT_POSTFIELDS, $params );
		curl_setopt( $c, CURLOPT_RETURNTRANSFER, true );
	
		$content = curl_exec( $c );
		$http_status = curl_getinfo($c, CURLINFO_HTTP_CODE);
	
		if($http_status != 200 && $backup == false){
			$backup = true;
			sms_send($params, $backup);
		}
	
		curl_close( $c );
		return $content;
	}
	
	public function sendSMS($parent_id, $number, $msg, $total, $transactions) {
	if($number!=''){
		$params = array(
				'username' => 's.szostakowska@przemyslowka.pl',
				'password' => 'e8df7a8edc038ab5dd24f280ef7c659f',
				'encoding'=>'utf-8',
				'to' => $number,
				'from' => '',
				'eco' => 1,
				'message' => $msg,
		
		);
			
		echo $this->sms_send($params,false);
	}
		
		$this->logIt ( $parent_id, $number, $msg, $total, $transactions );
	}
	public function sendMessage() {
	
	
		foreach ( $this->transactions as $key => $val ) {
		
			$a = new Account ();
			$a->retrieve_by_string_fields (array('index_dbf'=>$key)); 
		
			$numbers = $a->getTelephonesList ( true );
			
			if (count ( $this->transactions [$key] ['records'] ) > 1) {
				$msg = "Prosze o uregulowanie zaleglych platnosci: " . implode ( ",", $this->transactions [$key] ['name'] ) . ",razem na kwote: " . format_number ( $this->transactions [$key] ['total'] ) . ". Przemyslowka tel: 795492715";
			} else {
				$msg = "Prosze o uregulowanie zaleglej platnosci: " . implode ( ",", $this->transactions [$key] ['name'] ) . " na kwote: " . format_number ( $this->transactions [$key] ['total'] ) . ". Przemyslowka tel: 795492715";
			}
			if(round($this->transactions [$key] ['total'],0 )==0){continue;}
			if (count ( $numbers ) > 0) {
				foreach ( $numbers as $number ) {
					$this->sendSMS ( $key, $number['telephone'], $msg, $this->transactions [$key] ['total'], $this->transactions [$key] ['records'] );
				}
			} else {
				$this->logIt( $key, null, $msg, $this->transactions [$key] ['total'], $this->transactions [$key] ['records'] );
			}
		}
	}
}



?>