Merged bugfixes since 2.0.6
[fa-stable.git] / reporting / rep201.php
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU Affero General Public License,
5         AGPL, as published by the Free Software Foundation, either version 
6         3 of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/agpl-3.0.html>.
11 ***********************************************************************/
12 $page_security = 2;
13 // ----------------------------------------------------------------
14 // $ Revision:  2.0 $
15 // Creator:     Joe Hunt
16 // date_:       2005-05-19
17 // Title:       Supplier Balances
18 // ----------------------------------------------------------------
19 $path_to_root="..";
20
21 include_once($path_to_root . "/includes/session.inc");
22 include_once($path_to_root . "/includes/date_functions.inc");
23 include_once($path_to_root . "/includes/data_checks.inc");
24 include_once($path_to_root . "/gl/includes/gl_db.inc");
25
26 //----------------------------------------------------------------------------------------------------
27
28 // trial_inquiry_controls();
29 print_supplier_balances();
30
31 function getTransactions($supplier_id, $date)
32 {
33         $date = date2sql($date);
34
35     $sql = "SELECT ".TB_PREF."supp_trans.*, ".TB_PREF."sys_types.type_name,
36                                 (".TB_PREF."supp_trans.ov_amount + ".TB_PREF."supp_trans.ov_gst + ".TB_PREF."supp_trans.ov_discount)
37                                 AS TotalAmount, ".TB_PREF."supp_trans.alloc AS Allocated,
38                                 ((".TB_PREF."supp_trans.type = 20)
39                                         AND ".TB_PREF."supp_trans.due_date < '$date') AS OverDue
40                         FROM ".TB_PREF."supp_trans, ".TB_PREF."sys_types
41                         WHERE ".TB_PREF."supp_trans.tran_date <= '$date' AND ".TB_PREF."supp_trans.supplier_id = '$supplier_id'
42                                 AND ".TB_PREF."supp_trans.type = ".TB_PREF."sys_types.type_id
43                                 ORDER BY ".TB_PREF."supp_trans.tran_date";
44
45     $TransResult = db_query($sql,"No transactions were returned");
46
47     return $TransResult;
48 }
49
50 //----------------------------------------------------------------------------------------------------
51
52 function print_supplier_balances()
53 {
54     global $path_to_root;
55
56     include_once($path_to_root . "/reporting/includes/pdf_report.inc");
57
58     $to = $_POST['PARAM_0'];
59     $fromsupp = $_POST['PARAM_1'];
60     $currency = $_POST['PARAM_2'];
61     $comments = $_POST['PARAM_3'];
62
63         if ($fromsupp == reserved_words::get_all_numeric())
64                 $from = _('All');
65         else
66                 $from = get_supplier_name($fromsupp);
67     $dec = user_price_dec();
68
69         if ($currency == reserved_words::get_all())
70         {
71                 $convert = true;
72                 $currency = _('Balances in Home currency');
73         }
74         else
75                 $convert = false;
76
77         $cols = array(0, 100, 130, 190, 250, 320, 385, 450,     515);
78
79         $headers = array(_('Trans Type'), _('#'), _('Date'), _('Due Date'), _('Charges'),
80                 _('Credits'), _('Allocated'), _('Outstanding'));
81
82         $aligns = array('left', 'left', 'left', 'left', 'right', 'right', 'right', 'right');
83
84     $params =   array(  0 => $comments,
85                                     1 => array('text' => _('End Date'), 'from' => $to, 'to' => ''),
86                                     2 => array('text' => _('Supplier'), 'from' => $from, 'to' => ''),
87                                     3 => array(  'text' => _('Currency'),'from' => $currency, 'to' => ''));
88
89     $rep = new FrontReport(_('Supplier Balances'), "SupplierBalances.pdf", user_pagesize());
90
91     $rep->Font();
92     $rep->Info($params, $cols, $headers, $aligns);
93     $rep->Header();
94
95         $total = array();
96         $grandtotal = array(0,0,0,0);
97
98         $sql = "SELECT supplier_id, supp_name AS name, curr_code FROM ".TB_PREF."suppliers ";
99         if ($fromsupp != reserved_words::get_all_numeric())
100                 $sql .= "WHERE supplier_id=$fromsupp ";
101         $sql .= "ORDER BY supp_name";
102         $result = db_query($sql, "The customers could not be retrieved");
103
104         while ($myrow=db_fetch($result))
105         {
106                 if (!$convert && $currency != $myrow['curr_code'])
107                         continue;
108                 $rep->fontSize += 2;
109                 $rep->TextCol(0, 3, $myrow['name']);
110                 if ($convert)
111                         $rep->TextCol(3, 4,     $myrow['curr_code']);
112                 $rep->fontSize -= 2;
113                 $rep->NewLine(1, 2);
114                 $res = getTransactions($myrow['supplier_id'], $to);
115                 if (db_num_rows($res)==0)
116                         continue;
117                 $rep->Line($rep->row + 4);
118                 $total[0] = $total[1] = $total[2] = $total[3] = 0.0;
119                 while ($trans=db_fetch($res))
120                 {
121                         $rep->NewLine(1, 2);
122                         $rep->TextCol(0, 1,     $trans['type_name']);
123                         $rep->TextCol(1, 2,     $trans['reference']);
124                         $date = sql2date($trans['tran_date']);
125                         $rep->TextCol(2, 3,     $date);
126                         if ($trans['type'] == 20)
127                                 $rep->TextCol(3, 4,     sql2date($trans['due_date']));
128                         $item[0] = $item[1] = 0.0;
129                         if ($convert)
130                                 //$rate = get_exchange_rate_from_home_currency($myrow['curr_code'], $date);
131                                 $rate = $trans['rate'];
132                         else
133                                 $rate = 1.0;
134                         if ($trans['TotalAmount'] > 0.0)
135                         {
136                                 $item[0] = round2(abs($trans['TotalAmount']) * $rate, $dec);
137                                 $rep->TextCol(4, 5,     number_format2($item[0], $dec));
138                         }
139                         else
140                         {
141                                 $item[1] = round2(abs($trans['TotalAmount']) * $rate, $dec);
142                                 $rep->TextCol(5, 6,     number_format2($item[1], $dec));
143                         }
144                         $item[2] = round2($trans['Allocated'] * $rate, $dec);
145                         $rep->TextCol(6, 7,     number_format2($item[2], $dec));
146                         /*
147                         if ($trans['type'] == 20)
148                                 $item[3] = ($trans['TotalAmount'] - $trans['Allocated']) * $rate;
149                         else
150                                 $item[3] = ($trans['TotalAmount'] + $trans['Allocated']) * $rate;
151                         */      
152                         if ($trans['type'] == 20)
153                                 $item[3] = $item[0] + $item[1] - $item[2];
154                         else    
155                                 $item[3] = $item[0] - $item[1] + $item[2];
156                         $rep->TextCol(7, 8,     number_format2($item[3], $dec));
157                         for ($i = 0; $i < 4; $i++)
158                         {
159                                 $total[$i] += $item[$i];
160                                 $grandtotal[$i] += $item[$i];
161                         }
162                 }
163                 $rep->Line($rep->row - 8);
164                 $rep->NewLine(2);
165                 $rep->TextCol(0, 3,     _('Total'));
166                 for ($i = 0; $i < 4; $i++)
167                 {
168                         $rep->TextCol($i + 4, $i + 5, number_format2($total[$i], $dec));
169                         $total[$i] = 0.0;
170                 }
171         $rep->Line($rep->row  - 4);
172         $rep->NewLine(2);
173         }
174         $rep->fontSize += 2;
175         $rep->TextCol(0, 3,     _('Grand Total'));
176         $rep->fontSize -= 2;
177         for ($i = 0; $i < 4; $i++)
178                 $rep->TextCol($i + 4, $i + 5,number_format2($grandtotal[$i], $dec));
179         $rep->Line($rep->row  - 4);
180     $rep->End();
181 }
182
183 ?>