Merged bugfixes since 2.0.6
[fa-stable.git] / reporting / rep101.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 // ----------------------------------------------------------------
15 // $ Revision:  2.0 $
16 // Creator:     Joe Hunt
17 // date_:       2005-05-19
18 // Title:       Customer Balances
19 // ----------------------------------------------------------------
20 $path_to_root="..";
21
22 include_once($path_to_root . "/includes/session.inc");
23 include_once($path_to_root . "/includes/date_functions.inc");
24 include_once($path_to_root . "/includes/data_checks.inc");
25 include_once($path_to_root . "/gl/includes/gl_db.inc");
26
27 //----------------------------------------------------------------------------------------------------
28
29 // trial_inquiry_controls();
30 print_customer_balances();
31
32 function get_transactions($debtorno, $date)
33 {
34         $date = date2sql($date);
35
36     $sql = "SELECT ".TB_PREF."debtor_trans.*, ".TB_PREF."sys_types.type_name,
37                 (".TB_PREF."debtor_trans.ov_amount + ".TB_PREF."debtor_trans.ov_gst + ".TB_PREF."debtor_trans.ov_freight + ".TB_PREF."debtor_trans.ov_discount)
38                 AS TotalAmount, ".TB_PREF."debtor_trans.alloc AS Allocated,
39                 ((".TB_PREF."debtor_trans.type = 10)
40                 AND ".TB_PREF."debtor_trans.due_date < '$date') AS OverDue
41         FROM ".TB_PREF."debtor_trans, ".TB_PREF."sys_types
42         WHERE ".TB_PREF."debtor_trans.tran_date <= '$date'
43         AND ".TB_PREF."debtor_trans.debtor_no = '$debtorno'
44         AND ".TB_PREF."debtor_trans.type != 13
45         AND ".TB_PREF."debtor_trans.type = ".TB_PREF."sys_types.type_id
46         ORDER BY ".TB_PREF."debtor_trans.tran_date";
47
48     return db_query($sql,"No transactions were returned");
49 }
50
51 //----------------------------------------------------------------------------------------------------
52
53 function print_customer_balances()
54 {
55     global $path_to_root;
56
57         include_once($path_to_root . "/reporting/includes/pdf_report.inc");
58
59     $to = $_POST['PARAM_0'];
60     $fromcust = $_POST['PARAM_1'];
61     $currency = $_POST['PARAM_2'];
62     $comments = $_POST['PARAM_3'];
63
64         if ($fromcust == reserved_words::get_all_numeric())
65                 $from = _('All');
66         else
67                 $from = get_customer_name($fromcust);
68     $dec = user_price_dec();
69
70         if ($currency == reserved_words::get_all())
71         {
72                 $convert = true;
73                 $currency = _('Balances in Home Currency');
74         }
75         else
76                 $convert = false;
77
78         $cols = array(0, 100, 130, 190, 250, 320, 385, 450,     515);
79
80         $headers = array(_('Trans Type'), _('#'), _('Date'), _('Due Date'), _('Charges'), _('Credits'),
81                 _('Allocated'),         _('Outstanding'));
82
83         $aligns = array('left', 'left', 'left', 'left', 'right', 'right', 'right', 'right');
84
85     $params =   array(  0 => $comments,
86                                     1 => array('text' => _('End Date'), 'from' => $to,          'to' => ''),
87                                     2 => array('text' => _('Customer'), 'from' => $from,        'to' => ''),
88                                     3 => array('text' => _('Currency'), 'from' => $currency, 'to' => ''));
89
90     $rep = new FrontReport(_('Customer Balances'), "CustomerBalances.pdf", user_pagesize());
91
92     $rep->Font();
93     $rep->Info($params, $cols, $headers, $aligns);
94     $rep->Header();
95
96         $grandtotal = array(0,0,0,0);
97
98         $sql = "SELECT debtor_no, name, curr_code FROM ".TB_PREF."debtors_master ";
99         if ($fromcust != reserved_words::get_all_numeric())
100                 $sql .= "WHERE debtor_no=$fromcust ";
101         $sql .= "ORDER BY 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 = get_transactions($myrow['debtor_no'], $to);
115                 if (db_num_rows($res)==0)
116                         continue;
117                 $rep->Line($rep->row + 4);
118                 $total = array(0,0,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'] == 10)
127                                 $rep->TextCol(3, 4,     sql2date($trans['due_date']));
128                         $item[0] = $item[1] = 0.0;
129                         if ($convert)
130                                 $rate = $trans['rate'];
131                         else
132                                 $rate = 1.0;
133                         if ($trans['type'] == 11 || $trans['type'] == 12 || $trans['type'] == 2)
134                                 $trans['TotalAmount'] *= -1;
135                         if ($trans['TotalAmount'] > 0.0)
136                         {
137                                 $item[0] = round2(abs($trans['TotalAmount']) * $rate, $dec);
138                                 $rep->TextCol(4, 5,     number_format2($item[0], $dec));
139                         }
140                         else
141                         {
142                                 $item[1] = round2(Abs($trans['TotalAmount']) * $rate, $dec);
143                                 $rep->TextCol(5, 6,     number_format2($item[1], $dec));
144                         }
145                         $item[2] = round2($trans['Allocated'] * $rate, $dec);
146                         $rep->TextCol(6, 7,     number_format2($item[2], $dec));
147                         /*
148                         if ($trans['type'] == 10)
149                                 $item[3] = ($trans['TotalAmount'] - $trans['Allocated']) * $rate;
150                         else
151                                 $item[3] = ($trans['TotalAmount'] + $trans['Allocated']) * $rate;
152                         */
153                         if ($trans['type'] == 10)
154                                 $item[3] = $item[0] + $item[1] - $item[2];
155                         else    
156                                 $item[3] = $item[0] - $item[1] + $item[2];
157                         $rep->TextCol(7, 8, number_format2($item[3], $dec));
158                         for ($i = 0; $i < 4; $i++)
159                         {
160                                 $total[$i] += $item[$i];
161                                 $grandtotal[$i] += $item[$i];
162                         }
163                 }
164                 $rep->Line($rep->row - 8);
165                 $rep->NewLine(2);
166                 $rep->TextCol(0, 3, _('Total'));
167                 for ($i = 0; $i < 4; $i++)
168                         $rep->TextCol($i + 4, $i + 5, number_format2($total[$i], $dec));
169         $rep->Line($rep->row  - 4);
170         $rep->NewLine(2);
171         }
172         $rep->fontSize += 2;
173         $rep->TextCol(0, 3, _('Grand Total'));
174         $rep->fontSize -= 2;
175         for ($i = 0; $i < 4; $i++)
176                 $rep->TextCol($i + 4, $i + 5, number_format2($grandtotal[$i], $dec));
177         $rep->Line($rep->row  - 4);
178     $rep->End();
179 }
180
181 ?>