#! /usr/bin/php -q
<?php
	    // $conn_user  = 'root';
    	// $conn_pass  = 'root';
    	// $conn_server  = "localhost";
    	// $conn_db  = "mydb";
    	// $link=mysql_connect($conn_server,$conn_user,$conn_pass) ;
    	// if (!$link) {
      	//	die('Could not connect: ' . mysql_error());
    	// }
    	// mysql_select_db($conn_db);
    	require "db_connect.inc.php";
	     $query="SELECT CR.idCALL_RECORDS,CDR.billsec,CDR.dst,CDR.dstchannel
                FROM call_records CR
                JOIN cdr CDR ON CDR.idCDR=CR.idCDR
                WHERE CR.checked=0
                AND validity=1";
	     $result = mysql_query($query);
	     if (!$result) {
      		  die('Chyba 1:' . mysql_error());
	     }
	     $numrows=mysql_num_rows($result);
	    // print '\n numrows:'.$numrows;
	     if ($numrows!=0) {
    		    while (list($idCALL_RECORDS,$billsec,$dst,$dstchannel)=mysql_fetch_row($result)){
    		        $polepom=explode("/",$dstchannel);
    		        echo "\n destchannel: ".$dstchannel;
    		        echo "\n Billsec: ".$billsec;
    		        $pomsubpole=explode("-",$polepom[1]);
    		        $polepom[1]=$pomsubpole[0];
    		        if ($polepom[1]==$dst){
                    $trunk="Local";
                }else{
                    $trunk=$polepom[1];
                }
                echo "\n TRUNK: ".$trunk;                 		        
    		        $delkadst=strlen($dst);
		            $price=0;
		            
      	        do {
         	          $destnum = substr($dst,0,$delkadst); //postupne orezavam zprava predvolbu, dokud se nedostanu k nejake shode    
                    $subquery="SELECT uptotime,unionnum,cost,idPAYTYPE,idBILL,Tarif
                                FROM call_records_view
                                WHERE trunkname=\"".$trunk."\"
                                AND destnum LIKE \"".$destnum."\"
			                          AND idCALL_RECORDS=".$idCALL_RECORDS."
			                          
			                          AND StartDate = (SELECT StartDate
                                                  FROM call_records_view
                                                  WHERE StartDate < now()
                                                  AND trunkname=\"".$trunk."\"
                                                  AND destnum LIKE \"".$destnum."\"
                                                  AND idCALL_RECORDS=".$idCALL_RECORDS."
                                                  GROUP BY StartDate
                                                  ORDER BY StartDate DESC
                                                  LIMIT 1)
			                          
                                GROUP BY uptotime
                                ORDER BY uptotime DESC
                              ";
                    $subresult = mysql_query($subquery);
                    if (mysql_num_rows($subresult)==0){
                        $delkadst--;
                    }else{
                        $price=0;
                        $idPAYTYPE='';
                        $id_BILL='';
                        $id_TARIF='';
                        while(list($_uptotime,$_unionnum,$cost,$paytype,$idBILL,$idTARIF)=mysql_fetch_row($subresult)){
                                if($billsec>$_uptotime){ 
                                    $_billsec=$billsec-$_uptotime;
                                    echo "\n idCDR".$idCALL_RECORDS."billsec".$_billsec;
     		                            $price=$price+($_billsec*$_unionnum*$cost);
                                    echo "price=".$price;
                                    $billsec=$_uptotime;
                                    $idPAYTYPE=$paytype;
                                    $id_BILL= $idBILL; 
                                    $id_TARIF=$idTARIF;      
                                } 
                        }
                        $subquery="UPDATE call_records SET price=".$price.",checked=1,idTARIF=".$id_TARIF.",idPAYTYPE=".$idPAYTYPE."
                                   WHERE idCALL_RECORDS=".$idCALL_RECORDS."";
                        $subresult = mysql_query($subquery);
                        echo "\n update:".$idCALL_RECORDS;
                        echo "\n trunk:".$trunk;
                        if (!$subresult) {
                            die('Chyba 1:' . mysql_error());
                        }
                        #Pokud se jedna o predplacenoun skupinu, odecti credit
                        if ($idPAYTYPE == 2){
                            echo "\n KONAM PAYTYPE ";
                            $subquery2="UPDATE bill_credit SET value=value - ".$price."
                                        WHERE idBILL=".$id_BILL."";
                            $subresult2 = mysql_query($subquery2);
                            if (!$subresult2) {
                                die('Chyba updatu creditu :' . mysql_error());
                            }        
                        }
                    
                    }
                    
                    
                    
                    
                    
                    
                }while ($delkadst!=0 && $billsec!=0)  ;
            }
       }


?>
