Preparation for Excel Writer continued
[fa-stable.git] / reporting / rep101.php
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         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/gpl-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     $to = $_POST['PARAM_0'];
58     $fromcust = $_POST['PARAM_1'];
59     $currency = $_POST['PARAM_2'];
60     $comments = $_POST['PARAM_3'];
61         $destination = $_POST['PARAM_4'];
62         if ($destination)
63         {
64                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
65                 $filename = "CustomerBalances.xml";
66         }       
67         else
68         {
69                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
70                 $filename = "CustomerBalances.pdf";
71         }
72
73         if ($fromcust == reserved_words::get_all_numeric())
74                 $from = _('All');
75         else
76                 $from = get_customer_name($fromcust);
77     $dec = user_price_dec();
78
79         if ($currency == reserved_words::get_all())
80         {
81                 $convert = true;
82                 $currency = _('Balances in Home Currency');
83         }
84         else
85                 $convert = false;
86
87         $cols = array(0, 100, 130, 190, 250, 320, 385, 450,     515);
88
89         $headers = array(_('Trans Type'), _('#'), _('Date'), _('Due Date'), _('Charges'), _('Credits'),
90                 _('Allocated'),         _('Outstanding'));
91
92         $aligns = array('left', 'left', 'left', 'left', 'right', 'right', 'right', 'right');
93
94     $params =   array(  0 => $comments,
95                                     1 => array('text' => _('End Date'), 'from' => $to,          'to' => ''),
96                                     2 => array('text' => _('Customer'), 'from' => $from,        'to' => ''),
97                                     3 => array('text' => _('Currency'), 'from' => $currency, 'to' => ''));
98
99     $rep = new FrontReport(_('Customer Balances'), $filename, user_pagesize());
100
101     $rep->Font();
102     $rep->Info($params, $cols, $headers, $aligns);
103     $rep->Header();
104
105         $grandtotal = array(0,0,0,0);
106
107         $sql = "SELECT debtor_no, name, curr_code FROM ".TB_PREF."debtors_master ";
108         if ($fromcust != reserved_words::get_all_numeric())
109                 $sql .= "WHERE debtor_no=$fromcust ";
110         $sql .= "ORDER BY name";
111         $result = db_query($sql, "The customers could not be retrieved");
112
113         while ($myrow = db_fetch($result))
114         {
115                 if (!$convert && $currency != $myrow['curr_code'])
116                         continue;
117                 $rep->fontSize += 2;
118                 $rep->TextCol(0, 3, $myrow['name']);
119                 if ($convert)
120                         $rep->TextCol(3, 4,     $myrow['curr_code']);
121                 $rep->fontSize -= 2;
122                 $rep->NewLine(1, 2);
123                 $res = get_transactions($myrow['debtor_no'], $to);
124                 if (db_num_rows($res)==0)
125                         continue;
126                 $rep->Line($rep->row + 4);
127                 $total = array(0,0,0,0);
128                 while ($trans = db_fetch($res))
129                 {
130                         $rep->NewLine(1, 2);
131                         $rep->TextCol(0, 1, $trans['type_name']);
132                         $rep->TextCol(1, 2,     $trans['reference']);
133                         $rep->DateCol(2, 3,     $trans['tran_date'], true);
134                         if ($trans['type'] == 10)
135                                 $rep->DateCol(3, 4,     $trans['due_date'], true);
136                         $item[0] = $item[1] = 0.0;
137                         if ($convert)
138                                 $rate = $trans['rate'];
139                         else
140                                 $rate = 1.0;
141                         if ($trans['type'] == 11 || $trans['type'] == 12 || $trans['type'] == 2)
142                                 $trans['TotalAmount'] *= -1;
143                         if ($trans['TotalAmount'] > 0.0)
144                         {
145                                 $item[0] = round2(abs($trans['TotalAmount']) * $rate, $dec);
146                                 $rep->AmountCol(4, 5, $item[0], $dec);
147                         }
148                         else
149                         {
150                                 $item[1] = round2(Abs($trans['TotalAmount']) * $rate, $dec);
151                                 $rep->AmountCol(5, 6, $item[1], $dec);
152                         }
153                         $item[2] = round2($trans['Allocated'] * $rate, $dec);
154                         $rep->AmountCol(6, 7, $item[2], $dec);
155                         /*
156                         if ($trans['type'] == 10)
157                                 $item[3] = ($trans['TotalAmount'] - $trans['Allocated']) * $rate;
158                         else
159                                 $item[3] = ($trans['TotalAmount'] + $trans['Allocated']) * $rate;
160                         */
161                         if ($trans['type'] == 10)
162                                 $item[3] = $item[0] + $item[1] - $item[2];
163                         else    
164                                 $item[3] = $item[0] - $item[1] + $item[2];
165                         $rep->AmountCol(7, 8, $item[3], $dec);
166                         for ($i = 0; $i < 4; $i++)
167                         {
168                                 $total[$i] += $item[$i];
169                                 $grandtotal[$i] += $item[$i];
170                         }
171                 }
172                 $rep->Line($rep->row - 8);
173                 $rep->NewLine(2);
174                 $rep->TextCol(0, 3, _('Total'));
175                 for ($i = 0; $i < 4; $i++)
176                         $rep->AmountCol($i + 4, $i + 5, $total[$i], $dec);
177         $rep->Line($rep->row  - 4);
178         $rep->NewLine(2);
179         }
180         $rep->fontSize += 2;
181         $rep->TextCol(0, 3, _('Grand Total'));
182         $rep->fontSize -= 2;
183         for ($i = 0; $i < 4; $i++)
184                 $rep->AmountCol($i + 4, $i + 5, $grandtotal[$i], $dec);
185         $rep->Line($rep->row  - 4);
186         $rep->NewLine();
187     $rep->End();
188 }
189
190 ?>