root/experimental_v2/Cronjobs/a2billing_invoice2_cront.php

Revision 546, 4.0 kB (checked in by areski, 1 year ago)

UPDATE : lib for Crontjobs

Line 
1 #!/usr/bin/php -q
2 <?php
3 /***************************************************************************
4  *            a2billing_invoice_cront.php
5  *
6  *  13 April 2007
7  *  Purpose: To greate invoices for Each User.
8  *  Copyright  2007  User : Belaid Arezqui
9  *  ADD THIS SCRIPT IN A CRONTAB JOB
10  *
11  *  The sample above will run the script every day of each month at 6AM
12     crontab -e
13     0 6 1 * * php /var/lib/asterisk/agi-bin/libs_a2billing/crontjob/a2billing_invoice_cront.php
14     
15     
16     field     allowed values
17     -----     --------------
18     minute     0-59
19     hour         0-23
20     day of month     1-31
21     month     1-12 (or names, see below)
22     day of week     0-7 (0 or 7 is Sun, or use names)
23     
24 ****************************************************************************/
25
26 set_time_limit(0);
27 error_reporting(E_ALL ^ (E_NOTICE | E_WARNING));
28 //dl("pgsql.so"); // remove "extension= pgsql.so !
29
30 include_once (dirname(__FILE__)."/lib/Class.Table.php");
31 include (dirname(__FILE__)."/lib/Class.A2Billing.php");
32 include (dirname(__FILE__)."/lib/Misc.php");
33 include (dirname(__FILE__)."/lib/A2B_invoice.php");
34
35 //Flag to show the debuging information
36 $verbose_level=2;
37
38 $groupcard = 5000;
39
40 // User's choice
41 //$sendemail = '"No';             // ('Yes'/'No'). disabled because smarty is needed
42 $enableminimalamount = 'off';    // ('on'/'off') if total <  $minimalamount, won't bill invoice
43 $minimalamount = 0;                // if $enableminimalamount == 'on'. in invoice currency. 
44 $customtemplate = '';             // '' = customer default
45 $choose_currency == '';         // '' = customer default
46 $billcalls = 'on';                // ('on'/'off')
47 $billcharges = 'on';            // ('on'/'off')
48 $nowdate = date('Y-m-d H:i:s');
49 $billday = date("j");        //
50
51 $A2B = new A2Billing();
52 $A2B -> load_conf($agi, NULL, 0, $idconfig);
53
54 write_log(LOGFILE_CRONT_INVOICE, basename(__FILE__).' line:'.__LINE__."[#### CRONT INVOICE BEGIN ####]");
55
56 if (!$A2B -> DbConnect()){               
57     echo "[Cannot connect to the database]\n";
58     write_log(LOGFILE_CRONT_INVOICE, basename(__FILE__).' line:'.__LINE__."[Cannot connect to the database]");
59     exit;
60 }
61
62 $instance_table = new Table();
63 $currencies_list = get_currencies($A2B -> DBHandle);
64
65
66 // CHECK COUNT OF CARD ON WHICH APPLY THE SERVICE
67 $QUERY = "SELECT count(*) FROM cc_card ";
68 $result = $instance_table -> SQLExec ($A2B -> DBHandle, $QUERY);
69
70 $nb_card = $result[0][0];
71
72 $nbpagemax = (intval($nb_card/$groupcard));
73
74 if ($verbose_level>=1) echo "===> NB_CARD : $nb_card - NBPAGEMAX:$nbpagemax\n";
75
76 if (!($nb_card>0)){
77     if ($verbose_level>=1) echo "[No card to create the Invoice]\n";       
78     exit();
79 }       
80
81 $invoice = new A2B_Invoice($A2B -> DBHandle, $verbose_level);
82
83 for ($page = 0; $page <= $nbpagemax; $page++)
84 {
85     if ($verbose_level >= 1)  echo "$page <= $nbpagemax \n";
86     
87     $Query_Customers = "SELECT id, invoiceday FROM cc_card";
88     
89     if ($A2B->config["database"]['dbtype'] == "postgres")
90     {
91         $Query_Customers .= " LIMIT $groupcard OFFSET ".$page*$groupcard;
92     }
93     else
94     {
95         $Query_Customers .= " LIMIT ".$page*$groupcard.", $groupcard";
96     }
97     $resmax = $instance_table -> SQLExec ($A2B ->DBHandle, $Query_Customers);
98     
99     if (is_array($resmax)){
100         $numrow = count($resmax);
101         if($verbose_level >= 2) print_r($resmax[0]);
102     }else{
103         $numrow = 0;
104     }
105     
106     if($verbose_level >= 1) echo "\n Total Customers Found: ".$numrow;
107     
108     if ($numrow == 0) {
109         if ($verbose_level>=1) echo "\n[No card to create the Invoice]\n";           
110         exit();           
111     }else{
112         foreach($resmax as $Customer){
113             
114             $invoiceday = (is_numeric ($Customer[1]) && $Customer[1]>=1) ? $Customer[1] : 1;
115             
116             if ($billday != $invoiceday) {
117                 if ($verbose_level>=1)    echo "\nIt is not Customer ".$Customer[0]."    billday";
118                 continue;
119             }
120             
121             $invoice->RetrieveInformation($Customer[0], $billcalls == "on", $billcharges == "on", $nowdate);
122             $invoice->CreateInvoice($choose_currency);           
123             $invoice->BillInvoice(($enableminimalamount == 'on'), $minimalamount, $customtemplate);
124
125             // disabled because smarty is needed
126             //if ($sendemail == 'Yes')
127             //    $invoice->SendEMail($smarty);
128         }
129     }
130 }
131
132 ?>
133
Note: See TracBrowser for help on using the browser.


Google