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