root/experimental_v2/A2BCustomer_UI/A2B_entity_invoice_detail_pdf.php

Revision 566, 26.8 kB (checked in by areski, 1 year ago)

UPDATE : update patch lib

  • Property svn:executable set to *
Line 
1 <?php
2 include ("lib/customer.defines.php");
3 include ("lib/customer.module.access.php");
4 include ("lib/customer.smarty.php");
5
6 if (!$A2B->config["webcustomerui"]['invoice']) exit();
7
8 if (! has_rights (ACX_ACCESS)){
9     Header ("HTTP/1.0 401 Unauthorized");
10     Header ("Location: PP_error.php?c=accessdenied");       
11     die();
12 }
13
14 getpost_ifset(array('customer', 'posted', 'Period', 'choose_currency','exporttype', 'invoice_type', 'id','cardid'));
15
16 $customer = $_SESSION["pr_login"];
17 $vat = $_SESSION["vat"];
18 if ($invoice_type == "")
19 {   
20     $invoice_type = 1;
21 }
22 if ($invoice_type == 1)
23 {
24     $invoice_heading = gettext("Unbilled Details");   
25     $invocie_top_heading = gettext("Unbilled Invoice Details for Card Number");   
26 }
27 else
28 {
29     $invoice_heading = gettext("Billed Details");
30     $invocie_top_heading = gettext("Billed Invoice Details for Card Number");   
31 }
32
33 if ($exporttype=="pdf")
34 {   
35     header("Content-Type: application/octet-stream");
36     header("Content-Disposition: attachment; filename=UnBilledDetails_".date("d/m/Y-H:i").'.pdf');
37     header("Accept-Ranges: bytes");
38     header("Pragma: no-cache");
39     header("Expires: 0");
40     header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
41     header("Content-transfer-encoding: binary");       
42 }
43
44 // this variable specifie the debug type (0 => nothing, 1 => sql result, 2 => boucle checking, 3 other value checking)
45 $FG_DEBUG = 0;
46
47 // The variable FG_TABLE_NAME define the table name to use
48 $FG_TABLE_NAME="cc_call t1";
49
50 $DBHandle  = DbConnect();
51
52 // The variable Var_col would define the col that we want show in your table
53 // First Name of the column in the html page, second name of the field
54 $FG_TABLE_COL = array();
55
56 $FG_TABLE_COL[]=array (gettext("Calldate"), "starttime", "18%", "center", "SORT", "19", "", "", "", "", "", "display_dateformat");
57 $FG_TABLE_COL[]=array (gettext("Source"), "src", "10%", "center", "SORT", "30");
58 $FG_TABLE_COL[]=array (gettext("Callednumber"), "calledstation", "18%", "right", "SORT", "30", "", "", "", "", "", "");
59 $FG_TABLE_COL[]=array (gettext("Destination"), "destination", "18%", "center", "SORT", "30", "", "", "", "", "", "remove_prefix");
60 $FG_TABLE_COL[]=array (gettext("Duration"), "sessiontime", "8%", "center", "SORT", "30", "", "", "", "", "", "display_minute");
61
62 if (!(isset($customer)  &&  ($customer>0)) && !(isset($entercustomer)  &&  ($entercustomer>0))){
63     $FG_TABLE_COL[]=array (gettext("Cardused"), "username", "11%", "center", "SORT", "30");
64 }
65
66 $FG_TABLE_COL[]=array (gettext("Cost"), "sessionbill", "9%", "center", "SORT", "30", "", "", "", "", "", "display_2bill");
67 // ??? cardID
68 $FG_TABLE_DEFAULT_ORDER = "t1.starttime";
69 $FG_TABLE_DEFAULT_SENS = "DESC";
70     
71 // This Variable store the argument for the SQL query
72
73 $FG_COL_QUERY='t1.starttime, t1.src, t1.calledstation, t1.destination, t1.sessiontime  ';
74 if (!(isset($customer)  &&  ($customer>0)) && !(isset($entercustomer)  &&  ($entercustomer>0))){
75     $FG_COL_QUERY.=', t1.username';
76 }
77 $FG_COL_QUERY.=', t1.sessionbill';
78 if (LINK_AUDIO_FILE == 'YES')
79     $FG_COL_QUERY .= ', t1.uniqueid';
80
81 $FG_COL_QUERY_GRAPH='t1.callstart, t1.duration';
82
83 // The variable LIMITE_DISPLAY define the limit of record to display by page
84 $FG_LIMITE_DISPLAY=500;
85
86 // Number of column in the html table
87 $FG_NB_TABLE_COL=count($FG_TABLE_COL);
88
89 //This variable will store the total number of column
90 $FG_TOTAL_TABLE_COL = $FG_NB_TABLE_COL;
91 if ($FG_DELETION || $FG_EDITION) $FG_TOTAL_TABLE_COL++;
92
93 //This variable define the Title of the HTML table
94 $FG_HTML_TABLE_TITLE=" - Call Logs - ";
95
96 //This variable define the width of the HTML table
97 $FG_HTML_TABLE_WIDTH="70%";
98
99     if ($FG_DEBUG == 3) echo "<br>Table : $FG_TABLE_NAME      -     Col_query : $FG_COL_QUERY";
100     $instance_table = new Table($FG_TABLE_NAME, $FG_COL_QUERY);
101     $instance_table_graph = new Table($FG_TABLE_NAME, $FG_COL_QUERY_GRAPH);
102
103
104 if ( is_null ($order) || is_null($sens) ){
105     $order = $FG_TABLE_DEFAULT_ORDER;
106     $sens  = $FG_TABLE_DEFAULT_SENS;
107 }
108
109 if ($posted==1){
110  
111   $SQLcmd = '';
112  
113   $SQLcmd = do_field($SQLcmd, 'src', 'src');
114   $SQLcmd = do_field($SQLcmd, 'dst', 'calledstation'); 
115 }
116
117 $date_clause='';
118 // Period (Month-Day)
119 if (DB_TYPE == "postgres"){       
120          $UNIX_TIMESTAMP = "";
121 }else{
122         $UNIX_TIMESTAMP = "UNIX_TIMESTAMP";
123 }
124
125
126 $lastdayofmonth = date("t", strtotime($tostatsmonth.'-01'));
127  
128 if (strpos($SQLcmd, 'WHERE') > 0) {
129     $FG_TABLE_CLAUSE = substr($SQLcmd,6).$date_clause;
130 }elseif (strpos($date_clause, 'AND') > 0){
131     $FG_TABLE_CLAUSE = substr($date_clause,5);
132 }
133
134
135 if (strlen($FG_TABLE_CLAUSE)>0) $FG_TABLE_CLAUSE.=" AND ";
136 $FG_TABLE_CLAUSE.="t1.username='$customer'";
137
138 if (strlen($FG_TABLE_CLAUSE)>0)
139 {
140     $FG_TABLE_CLAUSE.=" AND ";
141 }
142 if ($invoice_type == 1)
143 {
144     $FG_TABLE_CLAUSE.="t1.starttime >(Select CASE  WHEN max(cover_enddate) IS NULL THEN '0001-01-01 01:00:00' ELSE max(cover_enddate) END from cc_invoices WHERE cardid = ".$_SESSION["card_id"].")";
145 }
146 else
147 {
148     $FG_TABLE_CLAUSE.="t1.starttime >(Select cover_startdate  from cc_invoices where id ='$id') AND t1.stoptime <(Select cover_enddate from cc_invoices where id ='$id') ";   
149 }
150
151 $_SESSION["pr_sql_export"]="SELECT $FG_COL_QUERY FROM $FG_TABLE_NAME WHERE $FG_TABLE_CLAUSE";
152
153 $QUERY = "SELECT substring(t1.starttime,1,10) AS day, sum(t1.sessiontime) AS calltime, sum(t1.sessionbill) AS cost, count(*) as nbcall FROM $FG_TABLE_NAME WHERE ".$FG_TABLE_CLAUSE."   GROUP BY substring(t1.starttime,1,10) ORDER BY day"; //extract(DAY from calldate)
154 //echo "$QUERY";
155
156 if (!$nodisplay)
157 {       
158     $list_total_day = $instance_table->SQLExec ($DBHandle, $QUERY);
159 }//end IF nodisplay
160 // GROUP BY DESTINATION FOR THE INVOICE
161
162 $QUERY = "SELECT destination, sum(t1.sessiontime) AS calltime,
163 sum(t1.sessionbill) AS cost, count(*) as nbcall FROM $FG_TABLE_NAME WHERE ".$FG_TABLE_CLAUSE."  GROUP BY destination";
164
165 if (!$nodisplay)
166 {
167     $list_total_destination  = $instance_table->SQLExec ($DBHandle, $QUERY);
168 }//end IF nodisplay
169
170
171 /************************************************ DID Billing Section *********************************************/
172 // Fixed + Dial = 0
173 // Fixed = 1
174 // Dail = 2
175 // Free = 3
176
177
178 // 1. Billing Type:: All DID Calls that have DID Type 0 and 2
179
180 if ($invoice_type == 1)
181 {
182     $QUERY = "SELECT t1.amount, t1.creationdate, t1.description, t3.countryname, t2.did ".
183     " FROM cc_charge t1 LEFT JOIN (cc_did t2, cc_country t3 ) ON ( t1.id_cc_did = t2.id AND t2.id_cc_country = t3.id ) ".
184     " WHERE (t1.chargetype = 1 OR  t1.chargetype = 2) AND t1.id_cc_card = ".$_SESSION["card_id"].
185     " AND t1.creationdate >(Select CASE  WHEN max(cover_enddate) IS NULL THEN '0001-01-01 01:00:00' ELSE max(cover_enddate) END from cc_invoices)";
186 }
187 else
188 {
189     $QUERY = "SELECT t1.amount, t1.creationdate, t1.description, t3.countryname, t2.did ".
190     " FROM cc_charge t1 LEFT JOIN (cc_did t2, cc_country t3 ) ON ( t1.id_cc_did = t2.id AND t2.id_cc_country = t3.id ) ".
191     " WHERE (t1.chargetype = 1 OR t1.chargetype = 2) AND t1.id_cc_card = ".$_SESSION["card_id"].
192     " AND t1.creationdate > (Select cover_startdate  from cc_invoices where id = '$id') AND t1.creationdate < (Select cover_enddate from cc_invoices where id = '$id')";
193 }
194
195  
196 if (!$nodisplay)
197 {
198     $list_total_did = $instance_table->SQLExec ($DBHandle, $QUERY);
199 }//end IF nodisplay
200
201 /************************************************ END DID Billing Section *********************************************/
202
203 /*************************************************CHARGES SECTION START ************************************************/
204
205 // Charge Types
206
207 // Connection charge for DID setup = 1
208 // Monthly Charge for DID use = 2
209 // Subscription fee = 3
210 // Extra charge =  4
211
212 if ($invoice_type == 1)
213 {
214     $QUERY = "SELECT t1.id_cc_card, t1.iduser, t1.creationdate, t1.amount, t1.chargetype, t1.id_cc_did, t1.currency, t1.description" .
215     " FROM cc_charge t1, cc_card t2 WHERE (t1.chargetype <> 1 AND t1.chargetype <> 2) " .
216     " AND t2.username = '$customer' AND t1.id_cc_card = t2.id AND t1.creationdate >= (Select CASE WHEN max(cover_enddate) is NULL " .
217     " THEN '0001-01-01 01:00:00' ELSE max(cover_enddate) END from cc_invoices) Order by t1.creationdate";
218 }
219 else
220 {
221     $QUERY = "SELECT t1.id_cc_card, t1.iduser, t1.creationdate, t1.amount, t1.chargetype, t1.id_cc_did, t1.currency" .
222     " FROM cc_charge t1, cc_card t2 WHERE (t1.chargetype <> 1 AND t1.chargetype <> 2) AND" .
223     " t2.username = '$customer' AND t1.id_cc_card = t2.id AND " .
224     " t1.creationdate >(Select cover_startdate  from cc_invoices where id ='$id') " .
225     " AND t1.creationdate <(Select cover_enddate from cc_invoices where id ='$id')";   
226 }
227 //echo "<br>".$QUERY."<br>";
228
229 if (!$nodisplay)
230 {
231     $list_total_charges = $instance_table->SQLExec ($DBHandle, $QUERY);
232 }//end IF nodisplay
233
234
235 /*************************************************CHARGES SECTION END ************************************************/
236
237 if ($nb_record<=$FG_LIMITE_DISPLAY){
238     $nb_record_max=1;
239 }else{
240     if ($nb_record % $FG_LIMITE_DISPLAY == 0){
241         $nb_record_max=(intval($nb_record/$FG_LIMITE_DISPLAY));
242     }else{
243         $nb_record_max=(intval($nb_record/$FG_LIMITE_DISPLAY)+1);
244     }   
245 }
246
247 if ($FG_DEBUG == 3) echo "<br>Nb_record : $nb_record";
248 if ($FG_DEBUG == 3) echo "<br>Nb_record_max : $nb_record_max";
249
250 /*************************************************************/
251
252 if ((isset($customer)  &&  ($customer>0)) || (isset($entercustomer)  &&  ($entercustomer>0))){
253
254     $FG_TABLE_CLAUSE = "";
255     if (isset($customer)  &&  ($customer>0)){       
256         $FG_TABLE_CLAUSE =" username='$customer' ";
257     }elseif (isset($entercustomer)  &&  ($entercustomer>0)){
258         $FG_TABLE_CLAUSE =" username='$entercustomer' ";
259     }
260     $instance_table_customer = new Table("cc_card", "id,  username, lastname, firstname, address, city, state, country, zipcode, phone, email, fax, activated, creationdate");
261     $info_customer = $instance_table_customer -> Get_list ($DBHandle, $FG_TABLE_CLAUSE, "id", "ASC", null, null, null, null);
262 }
263
264 if($invoice_type == 1)
265 {
266     $QUERY = "Select CASE WHEN max(cover_enddate) is NULL THEN '0001-01-01 01:00:00' ELSE max(cover_enddate) END from cc_invoices WHERE cardid = ".$cardid;
267 }
268 else
269 {
270     $QUERY = "Select cover_enddate, cover_startdate  from cc_invoices where id ='$id'";
271 }
272 if (!$nodisplay){
273     $invoice_dates = $instance_table->SQLExec ($DBHandle, $QUERY);           
274     if ($invoice_dates[0][0] == '0001-01-01 01:00:00')
275     {
276         $invoice_dates[0][0] = $info_customer[0][13];
277     }
278 }//end IF nodisplay
279
280 ?>
281
282 <?php
283 //$smarty->display( 'main.tpl');
284 if($exporttype == "pdf")
285 {
286     require('pdf-invoices/html2pdf/html2fpdf.php');
287        ob_start();
288 }
289
290 ?>
291 <?php
292 $currencies_list = get_currencies();
293
294 //For DID DIAL & Fixed + Dial
295 $totalcost = 0;
296 if (is_array($list_total_destination) && count($list_total_destination)>0){
297     $mmax=0;
298     $totalcall=0;
299     $totalminutes=0;
300     
301     foreach ($list_total_destination as $data){   
302         if ($mmax < $data[1]) $mmax=$data[1];
303         $totalcall+=$data[3];
304         $totalminutes+=$data[1];
305         $totalcost+=$data[2];
306     
307     }
308 }
309 ?>
310 <?php if (INVOICE_IMAGE != ""){ ?>
311 <table cellpadding="0"  align="center">
312 <tr>
313 <td align="center">
314 <img src="<?php echo Images_Path;?>/asterisk01.jpg" align="middle">
315 </td>
316 </tr>
317 </table>
318 <?php } ?>
319 <br>
320 <center><h4><font color="#FF0000"><?php echo $invocie_top_heading; ?>&nbsp;<?php echo $info_customer[0][1] ?> </font></h4></center>
321 <br>
322 <br>
323    
324     <table cellspacing="0" cellpadding="2" align="center" width="80%" >
325      
326       <tr>
327         <td colspan="2" bgcolor="#FFFFCC"><font size="5" color="#FF0000"><?php echo $invoice_heading; ?></font></td>
328       </tr>
329       <tr>
330         <td valign="top" colspan="2"></td>
331       </tr>     
332     <tr>
333       <td width="35%">&nbsp; </td>
334       <td >&nbsp; </td>
335     </tr>
336     <tr>
337       <td width="35%" ><font color="#003399"><?php echo gettext("Name")?>&nbsp; :</font> </td>
338       <td  ><font color="#003399"><?php echo $info_customer[0][3] ." ".$info_customer[0][2] ?></font></td>
339     </tr>
340     <tr>
341       <td width="35%" ><font color="#003399"><?php echo gettext("Card Number")?>&nbsp; :</font></td>
342       <td  ><font color="#003399"><?php echo $info_customer[0][1] ?></font> </td>
343     </tr>           
344     <?php
345             if ($invoice_type == 1){
346     ?>
347     <tr>
348       <td width="35%" ><font color="#003399"><?php echo gettext("From Date")?>&nbsp; :</font></td>
349       <td  ><font color="#003399"><?php echo display_dateonly($invoice_dates[0][0]);?> </font></td>
350     </tr>
351     <?php }else{ ?>
352     <tr>
353       <td width="35%" ><font color="#003399"><?php echo gettext("From Date")?>&nbsp; :</font></td>
354       <td  ><font color="#003399"><?php echo display_dateonly($invoice_dates[0][1]);?> </font></td>
355     </tr>
356     <tr>
357       <td width="35%" ><font color="#003399"><?php echo gettext("To Date")?>&nbsp; :</font></td>
358       <td  ><font color="#003399"><?php echo display_dateonly($invoice_dates[0][0]);?> </font></td>
359     </tr>
360     <?php } ?>
361     </table> 
362   <table align="center" width="80%">
363     <tr>
364         <td colspan="4" align="center"><font> <b><?php echo gettext("By Destination")?></b></font> </td>
365     </tr>
366
367             <tr bgcolor="#CCCCCC">
368               <td  width="29%"><font color="#003399"><b><?php echo gettext("Destination")?> </b></font></td>
369               <td width="38%" ><font color="#003399"><b><?php echo gettext("Duration")?></b></font> </td>
370              
371               <td width="12%" align="center" ><font color="#003399"><b><?php echo gettext("Calls")?> </b></font></td>
372               <td   align="right"><font color="#003399"><b><?php echo gettext("Amount")." (".BASE_CURRENCY.")"; ?> </b></font></td>
373             </tr>
374             <?php         
375                 $i=0;
376                 if (is_array($list_total_destination) && count($list_total_destination)>0){
377                 foreach ($list_total_destination as $data){   
378                 $i=($i+1)%2;       
379                 $tmc = $data[1]/$data[3];
380                 
381                 if ((!isset($resulttype)) || ($resulttype=="min")){ 
382                     $tmc = sprintf("%02d",intval($tmc/60)).":".sprintf("%02d",intval($tmc%60));       
383                 }else{
384                 
385                     $tmc =intval($tmc);
386                 }
387                 
388                 if ((!isset($resulttype)) || ($resulttype=="min")){ 
389                         $minutes = sprintf("%02d",intval($data[1]/60)).":".sprintf("%02d",intval($data[1]%60));
390                 }else{
391                         $minutes = $data[1];
392                 }
393                 if ($mmax>0)     $widthbar= intval(($data[1]/$mmax)*200);
394         
395             ?>
396             <tr class="invoice_rows">
397               <td width="29%" ><font color="#003399"><?php echo $data[0]?></font></td>
398               <td width="38%" ><font color="#003399"><?php echo $minutes?> </font></td>
399              
400               <td width="12%" align="right" ><font color="#003399"><?php echo $data[3]?></font> </td>
401               <td  align="right" ><font color="#003399"><?php  display_2bill($data[2]) ?></font></td>
402             </tr>
403             <?php      }             
404         
405             if ((!isset($resulttype)) || ($resulttype=="min")){ 
406                 $total_tmc = sprintf("%02d",intval(($totalminutes/$totalcall)/60)).":".sprintf("%02d",intval(($totalminutes/$totalcall)%60));               
407                 $totalminutes = sprintf("%02d",intval($totalminutes/60)).":".sprintf("%02d",intval($totalminutes%60));
408             }else{
409                 $total_tmc = intval($totalminutes/$totalcall);           
410             }
411              ?>   
412              <tr >
413               <td width="29%" >&nbsp;</td>
414               <td width="38%" >&nbsp;</td>             
415               <td width="12%" >&nbsp; </td>
416               <td  >&nbsp; </td>
417              
418             </tr>
419             <tr bgcolor="#CCCCCC">
420               <td width="29%" ><font color="#003399"><?php echo gettext("TOTAL");?> </font></td>
421               <td width="38%" ><font color="#003399"><?php echo $totalminutes?></font></td>             
422               <td width="12%"  align="right"><font color="#003399"><?php echo $totalcall?> </font></td>
423               <td  align="right" ><font color="#003399"><?php  display_2bill($totalcost - $totalcost_did) ?></font> </td>
424             </tr>   
425             <?php }else{?>         
426             <tr >
427               <td width="29%"><?php echo gettext("None!!!");?></td>
428               <td width="38%">&nbsp;</td>
429              
430               <td width="12%">&nbsp; </td>
431               <td >&nbsp; </td>
432              
433             </tr>           
434             <?php }?>         
435             <tr >
436               <td width="29%">&nbsp;</td>
437               <td width="38%">&nbsp;</td>
438              
439               <td width="12%">&nbsp; </td>
440               <td >&nbsp; </td>
441              
442             </tr>           
443             </table>
444             <table align="center" width="80%">
445             <!-- Start Here ****************************************-->
446             <?php
447                 if (is_array($list_total_day) && count($list_total_day)>0){
448                 
449                 $mmax=0;
450                 $totalcall=0;
451                 $totalminutes=0;
452                 $totalcost_day=0;
453                 foreach ($list_to