*** empty log message ***
[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     $tosupp = $_POST['PARAM_2'];
56     $currency = $_POST['PARAM_3'];
57     $comments = $_POST['PARAM_4'];
58
59         if ($fromsupp == null)
60                 $fromsupp = 0;
61         if ($tosupp == null)
62                 $tosupp = 0;
63             
64     $dec = user_price_dec();
65
66         if ($currency == reserved_words::get_all())
67         {
68                 $convert = true;
69                 $currency = _('Balances in Home Currency');
70         }
71         else
72                 $convert = false;
73
74         $cols = array(0, 100, 130, 190, 250, 320, 385, 450,     515);
75
76         $headers = array(_('Trans Type'), _('#'), _('Due Date'), '', '',
77                 '', _('Total'), _('Balance'));
78
79         $aligns = array('left', 'left', 'left', 'left', 'right', 'right', 'right', 'right');
80
81     $params =   array(  0 => $comments,
82                                     1 => array('text' => _('End Date'), 'from' => $to, 'to' => ''),
83                                     2 => array('text' => _('Supplier'), 'from' => get_supplier_name($fromsupp),
84                             'to' => get_supplier_name($tosupp)),
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();
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 supplier_id>=$fromsupp AND supplier_id<=$tosupp AND ".TB_PREF."suppliers.payment_terms = ".TB_PREF."payment_terms.terms_indicator 
98                 ORDER BY supp_name";
99         $result = db_query($sql, "The customers could not be retrieved");
100         
101         while ($myrow=db_fetch($result)) 
102         {
103                 if (!$convert && $currency != $myrow['curr_code'])
104                         continue;
105                 $rep->fontSize += 2;
106                 $rep->TextCol(0, 6, $myrow['name'] . " - " . $myrow['terms']);
107                 if ($convert)
108                 {
109                         $rate = get_exchange_rate_from_home_currency($myrow['curr_code'], $to);
110                         $rep->TextCol(6, 7,     $myrow['curr_code']);
111                 }
112                 else
113                         $rate = 1.0;
114                 $rep->fontSize -= 2;
115                 $rep->NewLine(1, 2);
116                 $res = getTransactions($myrow['supplier_id'], $to);
117                 if (db_num_rows($res)==0)
118                         continue;
119                 $rep->Line($rep->row + 4);
120                 $total[0] = $total[1] = $total[2] = $total[3] = 0.0; 
121                 while ($trans=db_fetch($res))
122                 {
123                         $rep->NewLine(1, 2);
124                         $rep->TextCol(0, 1,     $trans['type_name']);
125                         $rep->TextCol(1, 2,     $trans['supp_reference']);
126                         $rep->TextCol(2, 3,     sql2date($trans['due_date']));
127                         $item[0] = Abs($trans['TranTotal']) * $rate;            
128                         $rep->TextCol(6, 7,     number_format2($item[0], $dec));
129                         $item[1] = $trans['Balance'] * $rate;
130                         $rep->TextCol(7, 8,     number_format2($item[1], $dec));
131                         for ($i = 0; $i < 2; $i++)
132                         {
133                                 $total[$i] += $item[$i];
134                                 $grandtotal[$i] += $item[$i];
135                         }       
136                 }
137                 $rep->Line($rep->row - 8);
138                 $rep->NewLine(2);
139                 $rep->TextCol(0, 3,     _('Total'));
140                 for ($i = 0; $i < 2; $i++)
141                 {
142                         $rep->TextCol($i + 6, $i + 7, number_format2($total[$i], $dec));
143                         $total[$i] = 0.0;
144                 }       
145         $rep->Line($rep->row  - 4);
146         $rep->NewLine(2);
147         }
148         $rep->fontSize += 2;
149         $rep->TextCol(0, 3,     _('Grand Total'));
150         $rep->fontSize -= 2;
151         for ($i = 0; $i < 2; $i++)
152                 $rep->TextCol($i + 6, $i + 7,number_format2($grandtotal[$i], $dec));
153         $rep->Line($rep->row  - 4);
154     $rep->End();
155 }
156
157 ?>