*** empty log message ***
[fa-stable.git] / reporting / rep201.php
1 <?php
2
3 $page_security = 2;
4 // ----------------------------------------------------------------
5 // $ Revision:  2.0 $
6 // Creator:     Joe Hunt
7 // date_:       2005-05-19
8 // Title:       Supplier Balances
9 // ----------------------------------------------------------------
10 $path_to_root="../";
11
12 include_once($path_to_root . "includes/session.inc");
13 include_once($path_to_root . "includes/date_functions.inc");
14 include_once($path_to_root . "includes/data_checks.inc");
15 include_once($path_to_root . "gl/includes/gl_db.inc");
16
17 //----------------------------------------------------------------------------------------------------
18
19 // trial_inquiry_controls();
20 print_supplier_balances();
21
22 function getTransactions($supplier_id, $date)
23 {
24         $date = date2sql($date);
25         
26     $sql = "SELECT ".TB_PREF."supp_trans.*, ".TB_PREF."sys_types.type_name, 
27                                 (".TB_PREF."supp_trans.ov_amount + ".TB_PREF."supp_trans.ov_gst + ".TB_PREF."supp_trans.ov_discount)
28                                 AS TotalAmount, ".TB_PREF."supp_trans.alloc AS Allocated,
29                                 ((".TB_PREF."supp_trans.type = 20)
30                                         AND ".TB_PREF."supp_trans.due_date < '$date') AS OverDue
31                         FROM ".TB_PREF."supp_trans, ".TB_PREF."sys_types
32                         WHERE ".TB_PREF."supp_trans.tran_date <= '$date' AND ".TB_PREF."supp_trans.supplier_id = '$supplier_id'
33                                 AND ".TB_PREF."supp_trans.type = ".TB_PREF."sys_types.type_id
34                                 ORDER BY ".TB_PREF."supp_trans.tran_date";
35
36     $TransResult = db_query($sql,"No transactions were returned");
37
38     return $TransResult;
39 }
40
41 //----------------------------------------------------------------------------------------------------
42
43 function print_supplier_balances()
44 {
45     global $path_to_root;
46
47     include_once($path_to_root . "reporting/includes/pdf_report.inc");
48
49     $to = $_POST['PARAM_0'];
50     $fromsupp = $_POST['PARAM_1'];
51     $tosupp = $_POST['PARAM_2'];
52     $currency = $_POST['PARAM_3'];
53     $comments = $_POST['PARAM_4'];
54     
55     if ($fromsupp == null)
56         $fromsupp = 0;
57     if ($tosupp == null)
58         $tosupp = 0;
59     $dec = user_price_dec();
60
61         if ($currency == reserved_words::get_all())
62         {
63                 $convert = true;
64                 $currency = _('Balances in Home currency');
65         }
66         else
67                 $convert = false;
68
69         $cols = array(0, 100, 130, 190, 250, 320, 385, 450,     515);
70
71         $headers = array(_('Trans Type'), _('#'), _('Date'), _('Due Date'), _('Charges'),
72                 _('Credits'), _('Allocated'), _('Outstanding'));
73
74         $aligns = array('left', 'left', 'left', 'left', 'right', 'right', 'right', 'right');
75
76     $params =   array(  0 => $comments,
77                                     1 => array('text' => _('End Date'), 'from' => $to, 'to' => ''),
78                                     2 => array('text' => _('Supplier'), 'from' => get_supplier_name($fromsupp),
79                             'to' => get_supplier_name($tosupp)),
80                                     3 => array(  'text' => _('Currency'),'from' => $currency, 'to' => ''));
81
82     $rep = new FrontReport(_('Supplier Balances'), "SupplierBalances.pdf", user_pagesize());
83
84     $rep->Font();
85     $rep->Info($params, $cols, $headers, $aligns);
86     $rep->Header();
87
88         $total = array();
89         $grandtotal = array();
90
91         $sql = "SELECT supplier_id, supp_name AS name, curr_code FROM ".TB_PREF."suppliers
92                 WHERE supplier_id>=$fromsupp AND supplier_id<=$tosupp ORDER BY supp_name";
93         $result = db_query($sql, "The customers could not be retrieved");
94         
95         while ($myrow=db_fetch($result)) 
96         {
97                 if (!$convert && $currency != $myrow['curr_code'])
98                         continue;
99                 $rep->fontSize += 2;
100                 $rep->TextCol(0, 3, $myrow['name']);
101                 if ($convert)
102                 {
103                         $rate = get_exchange_rate_from_home_currency($myrow['curr_code'], $to);
104                         $rep->TextCol(3, 4,     $myrow['curr_code']);
105                 }
106                 else
107                         $rate = 1.0;
108                 $rep->fontSize -= 2;
109                 $rep->NewLine(1, 2);
110                 $res = getTransactions($myrow['supplier_id'], $to);
111                 if (db_num_rows($res)==0)
112                         continue;
113                 $rep->Line($rep->row + 4);
114                 $total[0] = $total[1] = $total[2] = $total[3] = 0.0; 
115                 while ($trans=db_fetch($res))
116                 {
117                         $rep->NewLine(1, 2);
118                         $rep->TextCol(0, 1,     $trans['type_name']);
119                         $rep->TextCol(1, 2,     $trans['reference']);
120                         $rep->TextCol(2, 3,     sql2date($trans['tran_date']));
121                         if ($trans['type'] == 20)       
122                                 $rep->TextCol(3, 4,     sql2date($trans['due_date']));
123                         $item[0] = $item[1] = 0.0;              
124                         if ($trans['TotalAmount'] > 0.0)        
125                         {
126                                 $item[0] = Abs($trans['TotalAmount']) * $rate;          
127                                 $rep->TextCol(4, 5,     number_format2($item[0], $dec));
128                         }               
129                         else    
130                         {
131                                 $item[1] = Abs($trans['TotalAmount']) * $rate;          
132                                 $rep->TextCol(5, 6,     number_format2($item[1], $dec));
133                         }               
134                         $item[2] = $trans['Allocated'] * $rate;
135                         $rep->TextCol(6, 7,     number_format2($item[2], $dec));
136                         if ($trans['type'] == 20)
137                                 $item[3] = ($trans['TotalAmount'] - $trans['Allocated']) * $rate;               
138                         else    
139                                 $item[3] = ($trans['TotalAmount'] + $trans['Allocated']) * $rate;               
140                         $rep->TextCol(7, 8,     number_format2($item[3], $dec));
141                         for ($i = 0; $i < 4; $i++)
142                         {
143                                 $total[$i] += $item[$i];
144                                 $grandtotal[$i] += $item[$i];
145                         }       
146                 }
147                 $rep->Line($rep->row - 8);
148                 $rep->NewLine(2);
149                 $rep->TextCol(0, 3,     _('Total'));
150                 for ($i = 0; $i < 4; $i++)
151                 {
152                         $rep->TextCol($i + 4, $i + 5, number_format2($total[$i], $dec));
153                         $total[$i] = 0.0;
154                 }       
155         $rep->Line($rep->row  - 4);
156         $rep->NewLine(2);
157         }
158         $rep->fontSize += 2;
159         $rep->TextCol(0, 3,     _('Grand Total'));
160         $rep->fontSize -= 2;
161         for ($i = 0; $i < 4; $i++)
162                 $rep->TextCol($i + 4, $i + 5,number_format2($grandtotal[$i], $dec));
163         $rep->Line($rep->row  - 4);
164     $rep->End();
165 }
166
167 ?>