<?php
ini_set('display_errors',1);
 //   $db=$GLOBALS['superGlobalDb'];
    $db=$GLOBALS['db']; 
   $query="select * from operating_values where value0='smsApi'";
    $res=$db->query($query);
    
    $dane= $db->fetchByAssoc($res);

    if($dane['value8']=='' && $dane['value9']==''){
      
    } else {
        if($dane['value4']!=''){
        	echo 'e';
            // pierwsze przypomnienie o platności
            $date = new DateTime(date('Y-m-d'));
            $date->modify('+'.$dane['value4'].' day');
            
            $query2="select * from ecmtransactions where payment_date >='".$date->format('Y-m-d')."' and payment_date <= '".$date->format('Y-m-d')."' and settled=0 and type=0 and deleted=0";
          
            $res2=$db->query($query2); 
            $to_send=array();
            echo 'e'.$res2->num_rows;
            while($dane2=$db->fetchByAssoc($res2)){
              var_dump($dane2);
                $a = new Account();
                $a->retrieve($dane2['parent_id']);
           
               
                if($a->allow_prevent=='1'){
           
             global $app_list_strings;
	$result['unit_name'] = $lists['ecmproducts_unit_dom'][$unit_id];

    $q="select id,type,name,value,type as type_id from ecmtransactions where id='".$dane2['id']."'";
    
    $res = $db->query($q);
   $row = $db->fetchByAssoc($res);
   $total_settled=0;
$rel = $db->query("SELECT * FROM ecmtransactions_rel WHERE ecmtransaction_a_id='".$row['id']."' OR ecmtransaction_b_id='".$row['id']."'");
	while ($rr = $db->fetchByAssoc($rel)) {
            
		if ($rr['ecmtransaction_a_id'] == $r['id'])
			$rel_id = $rr['ecmtransaction_b_id'];
		else 
			$rel_id = $rr['ecmtransaction_a_id'];
		$t = $db->fetchByAssoc($db->query("SELECT * FROM ecmtransactions WHERE id='$rel_id'"));
			$tmp2 = array();
			$tmp2['name'] = $t['name'];
			$tmp2['trans_id'] = $t['id'];
			$tmp2['value']=$rr['value'];
			if($t['type']==0 && $rel->num_rows==1){
			   
			    if(abs($rr['value'])==abs($r['value'])){
			     if($r['value']>0){
			        $rr['value']=abs($rr['value']);
			     } else {
			         $v = -1 * abs( $rr['value']);
			         $rr['value']=$v;
			     }
			    }
			} else {

			    if(abs($rr['value'])==abs($r['value'])){
			        if($r['value']>0){
			            $rr['value']=abs($rr['value']);
			        } else {
			            $v = -1 * abs( $rr['value']);
			            $rr['value']=$v;
			        }
			    } else {
			        if($r['value']>0){
			            $rr['value']=abs($rr['value']);
			        }
			    }
			}
		
		$total_settled+=floatval($rr['value']);
	}
   $row['value']=$row['value']-$total_settled;
   if($row['value']<0){
   	continue;
   }
   if($dane['value8']!='' && $a->phone_office!='') {
       // sms
       $miesiac = date('n',strtotime($dane2['payment_date']));
       $miesiac_PL = array(1 => 'stycznia', 2 => 'lutego', 3 => 'marca', 
4 => 'kwietnia', 5 => 'maja', 6 => 'czerwca', 7 => 'lipca', 
8 => 'sierpnia', 9 => 'wrzesnia', 10=> 'pazdziernika', 
11 => 'listopada', 12 => 'grudnia');
       $text=date('d',strtotime($dane2['payment_date'])).' '.$miesiac_PL[$miesiac].' mija termin platnosci za '.preg_replace("/[^A-Za-z0-9\-\/]/",' ',preg_replace("/\r\n|\r|\n/",' ',$dane2['name'])).'. Prosimy o wplate '.$row['value'].' zl na konto: 53 1020 5011 0000 9002 0270 6323. Saas Systems';
       	      $params_sms = array(
                'username' => $dane['value1'],
                'password' => md5($dane['value2']),
        	      		'encoding'=>'utf-8',
                'to' => $a->phone_office,
                'from' => $dane['value3'],
                'eco' => 1,
                'message' => $text,
        	      		
        );
              sms_send($params_sms);
              echo ' sle!';
   }
   
    if($dane['value9']!='') {
       // emailsendEma
        $miesiac = date('n',strtotime($dane2['payment_date']));
       $miesiac_PL = array(1 => 'stycznia', 2 => 'lutego', 3 => 'marca', 
4 => 'kwietnia', 5 => 'maja', 6 => 'czerwca', 7 => 'lipca', 
8 => 'sierpnia', 9 => 'wrzesnia', 10=> 'pazdziernika', 
11 => 'listopada', 12 => 'grudnia');
       $text=date('d',strtotime($dane2['payment_date'])).' '.$miesiac_PL[$miesiac].' mija termin platnosci za '.preg_replace("/[^A-Za-z0-9\-\/]/",' ',preg_replace("/\r\n|\r|\n/",' ',$dane2['name'])).'. Prosimy o wplate '.$row['value'].' zl na konto: 53 1020 5011 0000 9002 0270 6323. SaaS SystemS';
        sendEmail($dane2['parent_id'],$text);

   }
                } else {
                    continue;
                }
                    
            }
        }
        
                if($dane['value5']!=''){
            // drugiew przypomnienie o platności
            $date = new DateTime(date('Y-m-d'));
            $date->modify('+'.$dane['value5'].' day');
            
            $query2="select * from ecmtransactions where payment_date >='".$date->format('Y-m-d')."' and payment_date <= '".$date->format('Y-m-d')."' and settled=0 and type=0 and deleted=0";
          
            $res2=$db->query($query2); 
            echo 'e'.$res2->num_rows;
            $to_send=array();
            while($dane2=$db->fetchByAssoc($res2)){
              
                $a = new Account();
                $a->retrieve($dane2['parent_id']);
           
               
                if($a->allow_prevent=='1'){
           
             global $app_list_strings;
	$result['unit_name'] = $lists['ecmproducts_unit_dom'][$unit_id];

    $q="select id,type,name,value,type as type_id from ecmtransactions where id='".$dane2['id']."'";
    
    $res = $db->query($q);
   $row = $db->fetchByAssoc($res);
   $total_settled=0;
$rel = $db->query("SELECT * FROM ecmtransactions_rel WHERE ecmtransaction_a_id='".$row['id']."' OR ecmtransaction_b_id='".$row['id']."'");
	while ($rr = $db->fetchByAssoc($rel)) {
            
		if ($rr['ecmtransaction_a_id'] == $r['id'])
			$rel_id = $rr['ecmtransaction_b_id'];
		else 
			$rel_id = $rr['ecmtransaction_a_id'];
		$t = $db->fetchByAssoc($db->query("SELECT * FROM ecmtransactions WHERE id='$rel_id'"));
			$tmp2 = array();
			$tmp2['name'] = $t['name'];
			$tmp2['trans_id'] = $t['id'];
			$tmp2['value']=$rr['value'];
			if($t['type']==0 && $rel->num_rows==1){
			   
			    if(abs($rr['value'])==abs($r['value'])){
			     if($r['value']>0){
			        $rr['value']=abs($rr['value']);
			     } else {
			         $v = -1 * abs( $rr['value']);
			         $rr['value']=$v;
			     }
			    }
			} else {

			    if(abs($rr['value'])==abs($r['value'])){
			        if($r['value']>0){
			            $rr['value']=abs($rr['value']);
			        } else {
			            $v = -1 * abs( $rr['value']);
			            $rr['value']=$v;
			        }
			    } else {
			        if($r['value']>0){
			            $rr['value']=abs($rr['value']);
			        }
			    }
			}
		
		$total_settled+=floatval($rr['value']);
	}
   $row['value']=$row['value']-$total_settled;
   if($row['value']<0){
   	continue;
   }
   if($dane['value8']!='' && $a->phone_office!='') {
       // sms
       $miesiac = date('n',strtotime($dane2['payment_date']));
       $miesiac_PL = array(1 => 'stycznia', 2 => 'lutego', 3 => 'marca', 
4 => 'kwietnia', 5 => 'maja', 6 => 'czerwca', 7 => 'lipca', 
8 => 'sierpnia', 9 => 'wrzesnia', 10=> 'pazdziernika', 
11 => 'listopada', 12 => 'grudnia');
       $text=date('d',strtotime($dane2['payment_date'])).' '.$miesiac_PL[$miesiac].' mija termin platnosci za '.preg_replace("/[^A-Za-z0-9\-\/]/",' ',preg_replace("/\r\n|\r|\n/",' ',$dane2['name'])).'. Prosimy o wplate '.$row['value'].' zl na konto: 53 1020 5011 0000 9002 0270 6323. Saas Systems';
       	      $params_sms = array(
                'username' => $dane['value1'],
                'password' => md5($dane['value2']),
        	      		'encoding'=>'utf-8',
                'to' => $a->phone_office,
                'from' => $dane['value3'],
                'eco' => 1,
                'message' => $text,
        	      		
        );
              sms_send($params_sms);
              echo ' sle!';
   }
   
    if($dane['value9']!='') {
       // emailsendEma
        $miesiac = date('n',strtotime($dane2['payment_date']));
       $miesiac_PL = array(1 => 'stycznia', 2 => 'lutego', 3 => 'marca', 
4 => 'kwietnia', 5 => 'maja', 6 => 'czerwca', 7 => 'lipca', 
8 => 'sierpnia', 9 => 'wrzesnia', 10=> 'pazdziernika', 
11 => 'listopada', 12 => 'grudnia');
       $text=date('d',strtotime($dane2['payment_date'])).' '.$miesiac_PL[$miesiac].' mija termin platnosci za '.preg_replace("/[^A-Za-z0-9\-\/]/",' ',preg_replace("/\r\n|\r|\n/",' ',$dane2['name'])).'. Prosimy o wplate '.$row['value'].' zl na konto: 53 1020 5011 0000 9002 0270 6323. SaaS SystemS';
        sendEmail($dane2['parent_id'],$text);

   }
                } else {
                    continue;
                }
                    
            }
        }
         
        
                        if($dane['value7']!=''){
            //windykacja
            $date = new DateTime(date('Y-m-d'));
            $date->modify('-'.$dane['value6'].' day');
             $date->modify('-'.$dane['value7'].' day');
            $query2="select * from ecmtransactions where payment_date<='".$date->format('Y-m-d')."' and settled=0 and type=0 and deleted=0";
          
            $res2=$db->query($query2); 
            echo 'e'.$res2->num_rows;
            $to_send=array();
            while($dane2=$db->fetchByAssoc($res2)){
            	echo "<br>";
            	echo round(($date->format('d') - date('d',strtotime($dane2['payment_date']))),2);
            	echo "       ".$date->format('d')."          ".date('d',strtotime($dane2['payment_date'])).' '. $dane2['name'];
            if(round(($date->format('d') - date('d',strtotime($dane2['payment_date']))),2) % 5 ==0){
                echo "sle!";
            } else {
            	echo 'czemu';
                continue;
            }
            echo ' no c';
            echo "<pre>";
            var_dump($dane2);
                $a = new Account();
                $a->retrieve($dane2['parent_id']);
           
               echo $a->name. '   '.$dane2['parent_id'];
                if($a->allow_prevent=='1'){
           echo "jade dali";
             global $app_list_strings;
	$result['unit_name'] = $lists['ecmproducts_unit_dom'][$unit_id];

    $q="select id,type,name,value,type as type_id from ecmtransactions where id='".$dane2['id']."'";
    
    $res = $db->query($q);
   $row = $db->fetchByAssoc($res);
   $total_settled=0;
$rel = $db->query("SELECT * FROM ecmtransactions_rel WHERE ecmtransaction_a_id='".$row['id']."' OR ecmtransaction_b_id='".$row['id']."'");
	while ($rr = $db->fetchByAssoc($rel)) {
            
		if ($rr['ecmtransaction_a_id'] == $r['id'])
			$rel_id = $rr['ecmtransaction_b_id'];
		else 
			$rel_id = $rr['ecmtransaction_a_id'];
		$t = $db->fetchByAssoc($db->query("SELECT * FROM ecmtransactions WHERE id='$rel_id'"));
			$tmp2 = array();
			$tmp2['name'] = $t['name'];
			$tmp2['trans_id'] = $t['id'];
			$tmp2['value']=$rr['value'];
			if($t['type']==0 && $rel->num_rows==1){
			   
			    if(abs($rr['value'])==abs($r['value'])){
			     if($r['value']>0){
			        $rr['value']=abs($rr['value']);
			     } else {
			         $v = -1 * abs( $rr['value']);
			         $rr['value']=$v;
			     }
			    }
			} else {

			    if(abs($rr['value'])==abs($r['value'])){
			        if($r['value']>0){
			            $rr['value']=abs($rr['value']);
			        } else {
			            $v = -1 * abs( $rr['value']);
			            $rr['value']=$v;
			        }
			    } else {
			        if($r['value']>0){
			            $rr['value']=abs($rr['value']);
			        }
			    }
			}
		
		$total_settled+=floatval($rr['value']);
	}
   $row['value']=$row['value']-$total_settled;
   if($row['value']<0){
   	continue;
   }
   if($dane['value8']!='' && $a->phone_office!='') {
       // sms
       $miesiac = date('n',strtotime($dane2['payment_date']));
       $miesiac_PL = array(1 => 'stycznia', 2 => 'lutego', 3 => 'marca', 
4 => 'kwietnia', 5 => 'maja', 6 => 'czerwca', 7 => 'lipca', 
8 => 'sierpnia', 9 => 'wrzesnia', 10=> 'pazdziernika', 
11 => 'listopada', 12 => 'grudnia');
       $text=date('d',strtotime($dane2['payment_date'])).' '.$miesiac_PL[$miesiac].' minal termin platnosci za '.preg_replace("/[^A-Za-z0-9\-\/]/",' ',preg_replace("/\r\n|\r|\n/",' ',$dane2['name'])).'. Prosimy o wplate '.$row['value'].' zl na konto: 53 1020 5011 0000 9002 0270 6323. Saas Systems';
       	      $params_sms = array(
                'username' => $dane['value1'],
                'password' => md5($dane['value2']),
        	      		'encoding'=>'utf-8',
                'to' => $a->phone_office,
                'from' => $dane['value3'],
                'eco' => 1,
                'message' => $text,
        	      		
        );
              sms_send($params_sms);
            
   }
   
    if($dane['value9']!='') {
       // emailsendEma
        $miesiac = date('n',strtotime($dane2['payment_date']));
       $miesiac_PL = array(1 => 'stycznia', 2 => 'lutego', 3 => 'marca', 
4 => 'kwietnia', 5 => 'maja', 6 => 'czerwca', 7 => 'lipca', 
8 => 'sierpnia', 9 => 'wrzesnia', 10=> 'pazdziernika', 
11 => 'listopada', 12 => 'grudnia');
       $text=date('d',strtotime($dane2['payment_date'])).' '.$miesiac_PL[$miesiac].' minal termin platnosci za '.preg_replace("/[^A-Za-z0-9\-\/]/",' ',preg_replace("/\r\n|\r|\n/",' ',$dane2['name'])).'. Prosimy o wplate '.$row['value'].' zl na konto: 53 1020 5011 0000 9002 0270 6323. SaaS SystemS';
        sendEmail($dane2['parent_id'],$text);

   }
                } else {
                    continue;
                }
                    
            }
        }
                                if($dane['value6']!=''){
            //windykacja dwa
            $date = new DateTime(date('Y-m-d'));
            $date->modify('-'.$dane['value6'].' day');
            
            $query2="select * from ecmtransactions where payment_date <= '".$date->format('Y-m-d')."' and payment_date >= '".$date->format('Y-m-d')."' and settled=0 and type=0 and deleted=0";
          
            $res2=$db->query($query2); 
            echo 'e'.$res2->num_rows;
            $to_send=array();
            while($dane2=$db->fetchByAssoc($res2)){
              
                $a = new Account();
                $a->retrieve($dane2['parent_id']);
           
               
                if($a->allow_prevent=='1'){
           
             global $app_list_strings;
	$result['unit_name'] = $lists['ecmproducts_unit_dom'][$unit_id];

    $q="select id,type,name,value,type as type_id from ecmtransactions where id='".$dane2['id']."'";
    
    $res = $db->query($q);
   $row = $db->fetchByAssoc($res);
   $total_settled=0;
$rel = $db->query("SELECT * FROM ecmtransactions_rel WHERE ecmtransaction_a_id='".$row['id']."' OR ecmtransaction_b_id='".$row['id']."'");
	while ($rr = $db->fetchByAssoc($rel)) {
            
		if ($rr['ecmtransaction_a_id'] == $r['id'])
			$rel_id = $rr['ecmtransaction_b_id'];
		else 
			$rel_id = $rr['ecmtransaction_a_id'];
		$t = $db->fetchByAssoc($db->query("SELECT * FROM ecmtransactions WHERE id='$rel_id'"));
			$tmp2 = array();
			$tmp2['name'] = $t['name'];
			$tmp2['trans_id'] = $t['id'];
			$tmp2['value']=$rr['value'];
			if($t['type']==0 && $rel->num_rows==1){
			   
			    if(abs($rr['value'])==abs($r['value'])){
			     if($r['value']>0){
			        $rr['value']=abs($rr['value']);
			     } else {
			         $v = -1 * abs( $rr['value']);
			         $rr['value']=$v;
			     }
			    }
			} else {

			    if(abs($rr['value'])==abs($r['value'])){
			        if($r['value']>0){
			            $rr['value']=abs($rr['value']);
			        } else {
			            $v = -1 * abs( $rr['value']);
			            $rr['value']=$v;
			        }
			    } else {
			        if($r['value']>0){
			            $rr['value']=abs($rr['value']);
			        }
			    }
			}
		
		$total_settled+=floatval($rr['value']);
	}
   $row['value']=$row['value']-$total_settled;
     if($row['value']<0){
     	continue;
     }
   if($dane['value8']!='' && $a->phone_office!='') {
       // sms
       $miesiac = date('n',strtotime($dane2['payment_date']));
       $miesiac_PL = array(1 => 'stycznia', 2 => 'lutego', 3 => 'marca', 
4 => 'kwietnia', 5 => 'maja', 6 => 'czerwca', 7 => 'lipca', 
8 => 'sierpnia', 9 => 'wrzesnia', 10=> 'pazdziernika', 
11 => 'listopada', 12 => 'grudnia');
       $text=date('d',strtotime($dane2['payment_date'])).' '.$miesiac_PL[$miesiac].' minal termin platnosci za '.preg_replace("/[^A-Za-z0-9\-\/]/",' ',preg_replace("/\r\n|\r|\n/",' ',$dane2['name'])).'. Prosimy o wplate '.$row['value'].' zl na konto: 53 1020 5011 0000 9002 0270 6323. Saas Systems';
       	      $params_sms = array(
                'username' => $dane['value1'],
                'password' => md5($dane['value2']),
        	      		'encoding'=>'utf-8',
                'to' => $a->phone_office,
                'from' => $dane['value3'],
                'eco' => 1,
                'message' => $text,
        	      		
        );
              sms_send($params_sms);
            
   }
   
    if($dane['value9']!='') {
       // emailsendEma
        $miesiac = date('n',strtotime($dane2['payment_date']));
       $miesiac_PL = array(1 => 'stycznia', 2 => 'lutego', 3 => 'marca', 
4 => 'kwietnia', 5 => 'maja', 6 => 'czerwca', 7 => 'lipca', 
8 => 'sierpnia', 9 => 'wrzesnia', 10=> 'pazdziernika', 
11 => 'listopada', 12 => 'grudnia');
       $text=date('d',strtotime($dane2['payment_date'])).' '.$miesiac_PL[$miesiac].' minal termin platnosci za '.preg_replace("/[^A-Za-z0-9\-\/]/",' ',preg_replace("/\r\n|\r|\n/",' ',$dane2['name'])).'. Prosimy o wplate '.$row['value'].' zl na konto: 53 1020 5011 0000 9002 0270 6323. SaaS SystemS';
        sendEmail($dane2['parent_id'],$text);

   }
                } else {
                    continue;
                }
                    
            }
        } 
    }
   
     function sendEmail($id,$text,$title){
        require_once 'include/ECM/EcmSendPdfButton/EcmSendPdfButton.inc';
        $t = new EcmSendPdfButton('Accounts', $id,'','');
    
      
$sea = new SugarEmailAddress; 
// Grab the array of addresses
$addresses = $sea->getAddressesByGUID($id, 'Accounts');  

foreach ( $addresses as $address ) {

     $t->setAdress($address['email_address']);


}
     
        $t->setSubject('Przypominamy o platności');
        $t->setBody($text);
      
        $success= $t->sendEmail();
        var_dump($success);
    }
        function sms_send($params, $backup = false ) {
    
        static $content;
    echo "<pre>";
    
    var_dump($params);
       
    }