Moving 2.0 development version to main trunk.
[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.due_date,
29                         ".TB_PREF."supp_trans.trans_no,
30                         ".TB_PREF."supp_trans.type,
31                         (".TB_PREF."supp_trans.ov_amount + ".TB_PREF."supp_trans.ov_gst - ".TB_PREF."supp_trans.alloc) AS Balance,
32                         (".TB_PREF."supp_trans.ov_amount + ".TB_PREF."supp_trans.ov_gst ) AS TranTotal
33                 FROM ".TB_PREF."supp_trans,
34                         ".TB_PREF."sys_types
35                 WHERE ".TB_PREF."sys_types.type_id = ".TB_PREF."supp_trans.type
36                 AND ".TB_PREF."supp_trans.supplier_id = '" . $supplier . "'
37                 AND ".TB_PREF."supp_trans.ov_amount + ".TB_PREF."supp_trans.ov_gst - ".TB_PREF."supp_trans.alloc != 0
38                 AND ".TB_PREF."supp_trans.due_date <='" . $date . "'
39                 ORDER BY ".TB_PREF."supp_trans.type,
40                         ".TB_PREF."supp_trans.trans_no";
41
42     return db_query($sql, "No transactions were returned");
43 }
44
45 //----------------------------------------------------------------------------------------------------
46
47 function print_payment_report()
48 {
49     global $path_to_root;
50
51     include_once($path_to_root . "reporting/includes/pdf_report.inc");
52
53     $to = $_POST['PARAM_0'];
54     $fromsupp = $_POST['PARAM_1'];
55     $currency = $_POST['PARAM_2'];
56     $comments = $_POST['PARAM_3'];
57
58         if ($fromsupp == reserved_words::get_all_numeric())
59                 $from = _('All');
60         else
61                 $from = get_supplier_name($fromsupp);
62
63     $dec = user_price_dec();
64
65         if ($currency == reserved_words::get_all())
66         {
67                 $convert = true;
68                 $currency = _('Balances in Home Currency');
69         }
70         else
71                 $convert = false;
72
73         $cols = array(0, 100, 130, 190, 250, 320, 385, 450,     515);
74
75         $headers = array(_('Trans Type'), _('#'), _('Due Date'), '', '',
76                 '', _('Total'), _('Balance'));
77
78         $aligns = array('left', 'left', 'left', 'left', 'right', 'right', 'right', 'right');
79
80     $params =   array(  0 => $comments,
81                                     1 => array('text' => _('End Date'), 'from' => $to, 'to' => ''),
82                                     2 => array('text' => _('Supplier'), 'from' => $from, 'to' => ''),
83                                     3 => array(  'text' => _('Currency'),'from' => $currency, 'to' => ''));
84
85     $rep = new FrontReport(_('Payment Report'), "PaymentReport.pdf", user_pagesize());
86
87     $rep->Font();
88     $rep->Info($params, $cols, $headers, $aligns);
89     $rep->Header();
90
91         $total = array();
92         $grandtotal = array(0,0);
93
94         $sql = "SELECT supplier_id, supp_name AS name, curr_code, ".TB_PREF."payment_terms.terms FROM ".TB_PREF."suppliers, ".TB_PREF."payment_terms
95                 WHERE ";
96         if ($fromsupp != reserved_words::get_all_numeric())
97                 $sql .= "supplier_id=$fromsupp AND ";
98         $sql .= "".TB_PREF."suppliers.payment_terms = ".TB_PREF."payment_terms.terms_indicator
99                 ORDER BY supp_name";
100         $result = db_query($sql, "The customers could not be retrieved");
101
102         while ($myrow=db_fetch($result))
103         {
104                 if (!$convert && $currency != $myrow['curr_code'])
105                         continue;
106                 $rep->fontSize += 2;
107                 $rep->TextCol(0, 6, $myrow['name'] . " - " . $myrow['terms']);
108                 if ($convert)
109                 {
110                         $rate = get_exchange_rate_from_home_currency($myrow['curr_code'], $to);
111                         $rep->TextCol(6, 7,     $myrow['curr_code']);
112                 }
113                 else
114                         $rate = 1.0;
115                 $rep->fontSize -= 2;
116                 $rep->NewLine(1, 2);
117                 $res = getTransactions($myrow['supplier_id'], $to);
118                 if (db_num_rows($res)==0)
119                         continue;
120                 $rep->Line($rep->row + 4);
121                 $total[0] = $total[1] = 0.0;
122                 while ($trans=db_fetch($res))
123                 {
124                         $rep->NewLine(1, 2);
125                         $rep->TextCol(0, 1,     $trans['type_name']);
126                         $rep->TextCol(1, 2,     $trans['supp_reference']);
127                         $rep->TextCol(2, 3,     sql2date($trans['due_date']));
128                         $item[0] = Abs($trans['TranTotal']) * $rate;
129                         $rep->TextCol(6, 7,     number_format2($item[0], $dec));
130                         $item[1] = $trans['Balance'] * $rate;
131                         $rep->TextCol(7, 8,     number_format2($item[1], $dec));
132                         for ($i = 0; $i < 2; $i++)
133                         {
134                                 $total[$i] += $item[$i];
135                                 $grandtotal[$i] += $item[$i];
136                         }
137                 }
138                 $rep->Line($rep->row - 8);
139                 $rep->NewLine(2);
140                 $rep->TextCol(0, 3,     _('Total'));
141                 for ($i = 0; $i < 2; $i++)
142                 {
143                         $rep->TextCol($i + 6, $i + 7, number_format2($total[$i], $dec));
144                         $total[$i] = 0.0;
145                 }
146         $rep->Line($rep->row  - 4);
147         $rep->NewLine(2);
148         }
149         $rep->fontSize += 2;
150         $rep->TextCol(0, 3,     _('Grand Total'));
151         $rep->fontSize -= 2;
152         for ($i = 0; $i < 2; $i++)
153                 $rep->TextCol($i + 6, $i + 7,number_format2($grandtotal[$i], $dec));
154         $rep->Line($rep->row  - 4);
155     $rep->End();
156 }
157
158 ?>