Removed redundant form_types functions and the file /reporting/includes/form_types...
[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     $currency = $_POST['PARAM_2'];
52     $comments = $_POST['PARAM_3'];
53
54         if ($fromsupp == reserved_words::get_all_numeric())
55                 $from = _('All');
56         else
57                 $from = get_supplier_name($fromsupp);
58     $dec = user_price_dec();
59
60         if ($currency == reserved_words::get_all())
61         {
62                 $convert = true;
63                 $currency = _('Balances in Home currency');
64         }
65         else
66                 $convert = false;
67
68         $cols = array(0, 100, 130, 190, 250, 320, 385, 450,     515);
69
70         $headers = array(_('Trans Type'), _('#'), _('Date'), _('Due Date'), _('Charges'),
71                 _('Credits'), _('Allocated'), _('Outstanding'));
72
73         $aligns = array('left', 'left', 'left', 'left', 'right', 'right', 'right', 'right');
74
75     $params =   array(  0 => $comments,
76                                     1 => array('text' => _('End Date'), 'from' => $to, 'to' => ''),
77                                     2 => array('text' => _('Supplier'), 'from' => $from, 'to' => ''),
78                                     3 => array(  'text' => _('Currency'),'from' => $currency, 'to' => ''));
79
80     $rep = new FrontReport(_('Supplier Balances'), "SupplierBalances.pdf", user_pagesize());
81
82     $rep->Font();
83     $rep->Info($params, $cols, $headers, $aligns);
84     $rep->Header();
85
86         $total = array();
87         $grandtotal = array();
88
89         $sql = "SELECT supplier_id, supp_name AS name, curr_code FROM ".TB_PREF."suppliers ";
90         if ($fromsupp != reserved_words::get_all_numeric())
91                 $sql .= "WHERE supplier_id=$fromsupp ";
92         $sql .= "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                         $rep->TextCol(3, 4,     $myrow['curr_code']);
103                 $rep->fontSize -= 2;
104                 $rep->NewLine(1, 2);
105                 $res = getTransactions($myrow['supplier_id'], $to);
106                 if (db_num_rows($res)==0)
107                         continue;
108                 $rep->Line($rep->row + 4);
109                 $total[0] = $total[1] = $total[2] = $total[3] = 0.0;
110                 while ($trans=db_fetch($res))
111                 {
112                         $rep->NewLine(1, 2);
113                         $rep->TextCol(0, 1,     $trans['type_name']);
114                         $rep->TextCol(1, 2,     $trans['reference']);
115                         $date = sql2date($trans['tran_date']);
116                         $rep->TextCol(2, 3,     $date);
117                         if ($trans['type'] == 20)
118                                 $rep->TextCol(3, 4,     sql2date($trans['due_date']));
119                         $item[0] = $item[1] = 0.0;
120                         if ($convert)
121                                 $rate = get_exchange_rate_from_home_currency($myrow['curr_code'], $date);
122                         else
123                                 $rate = 1.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 ?>