4 // ----------------------------------------------------------------
8 // Title: Supplier Balances
9 // ----------------------------------------------------------------
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");
17 //----------------------------------------------------------------------------------------------------
19 // trial_inquiry_controls();
20 print_supplier_balances();
22 function getTransactions($supplier_id, $date)
24 $date = date2sql($date);
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";
36 $TransResult = db_query($sql,"No transactions were returned");
41 //----------------------------------------------------------------------------------------------------
43 function print_supplier_balances()
47 include_once($path_to_root . "reporting/includes/pdf_report.inc");
49 $to = $_POST['PARAM_0'];
50 $fromsupp = $_POST['PARAM_1'];
51 $currency = $_POST['PARAM_2'];
52 $comments = $_POST['PARAM_3'];
54 if ($fromsupp == reserved_words::get_all_numeric())
57 $from = get_supplier_name($fromsupp);
58 $dec = user_price_dec();
60 if ($currency == reserved_words::get_all())
63 $currency = _('Balances in Home currency');
68 $cols = array(0, 100, 130, 190, 250, 320, 385, 450, 515);
70 $headers = array(_('Trans Type'), _('#'), _('Date'), _('Due Date'), _('Charges'),
71 _('Credits'), _('Allocated'), _('Outstanding'));
73 $aligns = array('left', 'left', 'left', 'left', 'right', 'right', 'right', 'right');
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' => ''));
80 $rep = new FrontReport(_('Supplier Balances'), "SupplierBalances.pdf", user_pagesize());
83 $rep->Info($params, $cols, $headers, $aligns);
87 $grandtotal = array(0,0,0,0);
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");
95 while ($myrow=db_fetch($result))
97 if (!$convert && $currency != $myrow['curr_code'])
100 $rep->TextCol(0, 3, $myrow['name']);
102 $rep->TextCol(3, 4, $myrow['curr_code']);
105 $res = getTransactions($myrow['supplier_id'], $to);
106 if (db_num_rows($res)==0)
108 $rep->Line($rep->row + 4);
109 $total[0] = $total[1] = $total[2] = $total[3] = 0.0;
110 while ($trans=db_fetch($res))
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;
121 $rate = get_exchange_rate_from_home_currency($myrow['curr_code'], $date);
124 if ($trans['TotalAmount'] > 0.0)
126 $item[0] = Abs($trans['TotalAmount']) * $rate;
127 $rep->TextCol(4, 5, number_format2($item[0], $dec));
131 $item[1] = Abs($trans['TotalAmount']) * $rate;
132 $rep->TextCol(5, 6, number_format2($item[1], $dec));
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;
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++)
143 $total[$i] += $item[$i];
144 $grandtotal[$i] += $item[$i];
147 $rep->Line($rep->row - 8);
149 $rep->TextCol(0, 3, _('Total'));
150 for ($i = 0; $i < 4; $i++)
152 $rep->TextCol($i + 4, $i + 5, number_format2($total[$i], $dec));
155 $rep->Line($rep->row - 4);
159 $rep->TextCol(0, 3, _('Grand Total'));
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);