Merged all main trunk bugfixes up to release 2.0.5
[fa-stable.git] / reporting / rep203.php
1 <?php
2
3 $page_security = 2;
4 // ----------------------------------------------------------------
5 // $ Revision:  2.0 $
6 // Creator:     Joe Hunt
7 // date_:       2005-05-19
8 // Title:       Payment Report
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_payment_report();
21
22 function getTransactions($supplier, $date)
23 {
24         $date = date2sql($date);
25
26         $sql = "SELECT ".TB_PREF."sys_types.type_name,
27                         ".TB_PREF."supp_trans.supp_reference,
28                         ".TB_PREF."supp_trans.tran_date,
29                         ".TB_PREF."supp_trans.due_date,
30                         ".TB_PREF."supp_trans.trans_no,
31                         ".TB_PREF."supp_trans.type,
32                         ".TB_PREF."supp_trans.rate,
33                         (ABS(".TB_PREF."supp_trans.ov_amount) + ABS(".TB_PREF."supp_trans.ov_gst) - ".TB_PREF."supp_trans.alloc) AS Balance,
34                         (ABS(".TB_PREF."supp_trans.ov_amount) + ABS(".TB_PREF."supp_trans.ov_gst) ) AS TranTotal
35                 FROM ".TB_PREF."supp_trans,
36                         ".TB_PREF."sys_types
37                 WHERE ".TB_PREF."sys_types.type_id = ".TB_PREF."supp_trans.type
38                 AND ".TB_PREF."supp_trans.supplier_id = '" . $supplier . "'
39                 AND ABS(".TB_PREF."supp_trans.ov_amount) + ABS(".TB_PREF."supp_trans.ov_gst) - ".TB_PREF."supp_trans.alloc != 0
40                 AND ".TB_PREF."supp_trans.tran_date <='" . $date . "'
41                 ORDER BY ".TB_PREF."supp_trans.type,
42                         ".TB_PREF."supp_trans.trans_no";
43
44     return db_query($sql, "No transactions were returned");
45 }
46
47 //----------------------------------------------------------------------------------------------------
48
49 function print_payment_report()
50 {
51     global $path_to_root;
52
53     include_once($path_to_root . "reporting/includes/pdf_report.inc");
54
55     $to = $_POST['PARAM_0'];
56     $fromsupp = $_POST['PARAM_1'];
57     $currency = $_POST['PARAM_2'];
58     $comments = $_POST['PARAM_3'];
59
60         if ($fromsupp == reserved_words::get_all_numeric())
61                 $from = _('All');
62         else
63                 $from = get_supplier_name($fromsupp);
64
65     $dec = user_price_dec();
66
67         if ($currency == reserved_words::get_all())
68         {
69                 $convert = true;
70                 $currency = _('Balances in Home Currency');
71         }
72         else
73                 $convert = false;
74
75         $cols = array(0, 100, 130, 190, 250, 320, 385, 450,     515);
76
77         $headers = array(_('Trans Type'), _('#'), _('Due Date'), '', '',
78                 '', _('Total'), _('Balance'));
79
80         $aligns = array('left', 'left', 'left', 'left', 'right', 'right', 'right', 'right');
81
82     $params =   array(  0 => $comments,
83                                     1 => array('text' => _('End Date'), 'from' => $to, 'to' => ''),
84                                     2 => array('text' => _('Supplier'), 'from' => $from, 'to' => ''),
85                                     3 => array(  'text' => _('Currency'),'from' => $currency, 'to' => ''));
86
87     $rep = new FrontReport(_('Payment Report'), "PaymentReport.pdf", user_pagesize());
88
89     $rep->Font();
90     $rep->Info($params, $cols, $headers, $aligns);
91     $rep->Header();
92
93         $total = array();
94         $grandtotal = array(0,0);
95
96         $sql = "SELECT supplier_id, supp_name AS name, curr_code, ".TB_PREF."payment_terms.terms FROM ".TB_PREF."suppliers, ".TB_PREF."payment_terms
97                 WHERE ";
98         if ($fromsupp != reserved_words::get_all_numeric())
99                 $sql .= "supplier_id=$fromsupp AND ";
100         $sql .= "".TB_PREF."suppliers.payment_terms = ".TB_PREF."payment_terms.terms_indicator
101                 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, 6, $myrow['name'] . " - " . $myrow['terms']);
110                 if ($convert)
111                         $rep->TextCol(6, 7,     $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] = 0.0;
119                 while ($trans=db_fetch($res))
120                 {
121                         if ($convert)
122                                 $rate = $trans['rate'];
123                         else
124                                 $rate = 1.0;
125                         $rep->NewLine(1, 2);
126                         $rep->TextCol(0, 1,     $trans['type_name']);
127                         $rep->TextCol(1, 2,     $trans['supp_reference']);
128                         if ($trans['type'] == 20)
129                                 $rep->TextCol(2, 3,     sql2date($trans['due_date']));
130                         else    
131                                 $rep->TextCol(2, 3,     sql2date($trans['tran_date']));
132                         if ($trans['type'] != 20)
133                         {
134                                 $trans['TranTotal'] = -$trans['TranTotal'];
135                                 $trans['Balance'] = -$trans['Balance'];
136                         }
137                         $item[0] = $trans['TranTotal'] * $rate;
138                         $rep->TextCol(6, 7,     number_format2($item[0], $dec));
139                         $item[1] = $trans['Balance'] * $rate;
140                         $rep->TextCol(7, 8,     number_format2($item[1], $dec));
141                         for ($i = 0; $i < 2; $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 < 2; $i++)
151                 {
152                         $rep->TextCol($i + 6, $i + 7, 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 < 2; $i++)
162                 $rep->TextCol($i + 6, $i + 7,number_format2($grandtotal[$i], $dec));
163         $rep->Line($rep->row  - 4);
164     $rep->End();
165 }
166
167 ?>