Delivery notes removed from Customer Balnces, warning fix.
[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' 
34         AND ".TB_PREF."debtor_trans.debtor_no = '$debtorno'
35         AND ".TB_PREF."debtor_trans.type != 13
36         AND ".TB_PREF."debtor_trans.type = ".TB_PREF."sys_types.type_id
37         ORDER BY ".TB_PREF."debtor_trans.tran_date";
38
39     return db_query($sql,"No transactions were returned");
40 }
41
42 //----------------------------------------------------------------------------------------------------
43
44 function print_customer_balances()
45 {
46     global $path_to_root;
47
48         include_once($path_to_root . "reporting/includes/pdf_report.inc");
49
50     $to = $_POST['PARAM_0'];
51     $fromcust = $_POST['PARAM_1'];
52     $currency = $_POST['PARAM_2'];
53     $comments = $_POST['PARAM_3'];
54
55         if ($fromcust == reserved_words::get_all_numeric())
56                 $from = _('All');
57         else
58                 $from = get_customer_name($fromcust);
59     $dec = user_price_dec();
60
61         if ($currency == reserved_words::get_all())
62         {
63                 $convert = true;
64                 $currency = _('Balances in Home Currency');
65         }
66         else
67                 $convert = false;
68
69         $cols = array(0, 100, 130, 190, 250, 320, 385, 450,     515);
70
71         $headers = array(_('Trans Type'), _('#'), _('Date'), _('Due Date'), _('Charges'), _('Credits'),
72                 _('Allocated'),         _('Outstanding'));
73
74         $aligns = array('left', 'left', 'left', 'left', 'right', 'right', 'right', 'right');
75
76     $params =   array(  0 => $comments,
77                                     1 => array('text' => _('End Date'), 'from' => $to,          'to' => ''),
78                                     2 => array('text' => _('Customer'), 'from' => $from,        'to' => ''),
79                                     3 => array('text' => _('Currency'), 'from' => $currency, 'to' => ''));
80
81     $rep = new FrontReport(_('Customer Balances'), "CustomerBalances.pdf", user_pagesize());
82
83     $rep->Font();
84     $rep->Info($params, $cols, $headers, $aligns);
85     $rep->Header();
86
87         $total = array(0,0,0,0);
88         $grandtotal = array(0,0,0,0);
89
90         $sql = "SELECT debtor_no, name, curr_code FROM ".TB_PREF."debtors_master ";
91         if ($fromcust != reserved_words::get_all_numeric())
92                 $sql .= "WHERE debtor_no=$fromcust ";
93         $sql .= "ORDER BY name";
94         $result = db_query($sql, "The customers could not be retrieved");
95
96         while ($myrow = db_fetch($result))
97         {
98                 if (!$convert && $currency != $myrow['curr_code'])
99                         continue;
100                 $rep->fontSize += 2;
101                 $rep->TextCol(0, 3, $myrow['name']);
102                 if ($convert)
103                         $rep->TextCol(3, 4,     $myrow['curr_code']);
104                 $rep->fontSize -= 2;
105                 $rep->NewLine(1, 2);
106                 $res = get_transactions($myrow['debtor_no'], $to);
107                 if (db_num_rows($res)==0)
108                         continue;
109                 $rep->Line($rep->row + 4);
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'] == 10)
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'] == 10)
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                         $rep->TextCol($i + 4, $i + 5, number_format2($total[$i], $dec));
152         $rep->Line($rep->row  - 4);
153         $rep->NewLine(2);
154         }
155         $rep->fontSize += 2;
156         $rep->TextCol(0, 3, _('Grand Total'));
157         $rep->fontSize -= 2;
158         for ($i = 0; $i < 4; $i++)
159                 $rep->TextCol($i + 4, $i + 5, number_format2($grandtotal[$i], $dec));
160         $rep->Line($rep->row  - 4);
161     $rep->End();
162 }
163
164 ?>