2 /**********************************************************************
3 Copyright (C) FrontAccounting, LLC.
4 Released under the terms of the GNU General Public License, GPL,
5 as published by the Free Software Foundation, either version 3
6 of the License, or (at your option) any later version.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 $page_security = 'SA_SALESANALYTIC';
13 // ----------------------------------------------------------------
17 // Title: Inventory Sales Report
18 // ----------------------------------------------------------------
21 include_once($path_to_root . "/includes/session.inc");
22 include_once($path_to_root . "/includes/date_functions.inc");
23 include_once($path_to_root . "/includes/data_checks.inc");
24 include_once($path_to_root . "/includes/banking.inc");
25 include_once($path_to_root . "/gl/includes/gl_db.inc");
26 include_once($path_to_root . "/inventory/includes/db/items_category_db.inc");
28 //----------------------------------------------------------------------------------------------------
30 print_inventory_sales();
32 function getTransactions($category, $location, $fromcust, $from, $to, $show_service)
34 $from = date2sql($from);
37 $sql = "SELECT item.category_id,
38 category.description AS cat_description,
40 item.description, item.inactive,
44 debtor.name AS debtor_name,
46 SUM(-move.qty) AS qty,
47 SUM(-move.qty*move.price) AS amt,
48 SUM(-IF(move.unit_cost <> 0, move.qty * move.unit_cost, move.qty *item.material_cost)) AS cost
49 FROM ".TB_PREF."stock_master item,
50 ".TB_PREF."stock_category category,
51 ".TB_PREF."debtor_trans trans,
52 ".TB_PREF."debtors_master debtor,
53 ".TB_PREF."stock_moves move
54 WHERE item.stock_id=move.stock_id
55 AND item.category_id=category.category_id
56 AND trans.debtor_no=debtor.debtor_no
57 AND move.type=trans.type
58 AND move.trans_no=trans.trans_no
59 AND move.tran_date>='$from'
60 AND move.tran_date<='$to'
61 AND (trans.type=".ST_CUSTDELIVERY." OR move.type=".ST_CUSTCREDIT.")";
64 $sql .= " AND (item.mb_flag='B' OR item.mb_flag='M')";
66 $sql .= " AND item.mb_flag<>'F'";
68 $sql .= " AND item.category_id = ".db_escape($category);
71 $sql .= " AND move.loc_code = ".db_escape($location);
74 $sql .= " AND debtor.debtor_no = ".db_escape($fromcust);
76 $sql .= " GROUP BY item.stock_id, debtor.name ORDER BY item.category_id,
77 item.stock_id, debtor.name";
79 return db_query($sql,"No transactions were returned");
83 //----------------------------------------------------------------------------------------------------
85 function print_inventory_sales()
89 $from = $_POST['PARAM_0'];
90 $to = $_POST['PARAM_1'];
91 $category = $_POST['PARAM_2'];
92 $location = $_POST['PARAM_3'];
93 $fromcust = $_POST['PARAM_4'];
94 $show_service = $_POST['PARAM_5'];
95 $comments = $_POST['PARAM_6'];
96 $orientation = $_POST['PARAM_7'];
97 $destination = $_POST['PARAM_8'];
99 include_once($path_to_root . "/reporting/includes/excel_report.inc");
101 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
103 $orientation = ($orientation ? 'L' : 'P');
104 $dec = user_price_dec();
106 if ($category == ALL_NUMERIC)
111 $cat = get_category_name($category);
116 $loc = get_location_name($location);
121 $fromc = get_customer_name($fromcust);
122 if ($show_service) $show_service_items = _('Yes');
123 else $show_service_items = _('No');
125 $cols = array(0, 75, 175, 250, 300, 375, 450, 515);
127 $headers = array(_('Category'), _('Description'), _('Customer'), _('Qty'), _('Sales'), _('Cost'), _('Contribution'));
131 $aligns = array('left', 'left', 'left', 'right', 'right', 'right', 'right');
133 $params = array( 0 => $comments,
134 1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
135 2 => array('text' => _('Category'), 'from' => $cat, 'to' => ''),
136 3 => array('text' => _('Location'), 'from' => $loc, 'to' => ''),
137 4 => array('text' => _('Customer'), 'from' => $fromc, 'to' => ''),
138 5 => array('text' => _('Show Service Items'), 'from' => $show_service_items, 'to' => ''));
140 $rep = new FrontReport(_('Inventory Sales Report'), "InventorySalesReport", user_pagesize(), 9, $orientation);
141 if ($orientation == 'L')
142 recalculate_cols($cols);
145 $rep->Info($params, $cols, $headers, $aligns);
148 $res = getTransactions($category, $location, $fromcust, $from, $to, $show_service);
149 $total = $grandtotal = 0.0;
150 $total1 = $grandtotal1 = 0.0;
151 $total2 = $grandtotal2 = 0.0;
153 while ($trans=db_fetch($res))
155 if ($catt != $trans['cat_description'])
160 $rep->TextCol(0, 4, _('Total'));
161 $rep->AmountCol(4, 5, $total, $dec);
162 $rep->AmountCol(5, 6, $total1, $dec);
163 $rep->AmountCol(6, 7, $total2, $dec);
164 $rep->Line($rep->row - 2);
167 $total = $total1 = $total2 = 0.0;
169 $rep->TextCol(0, 1, $trans['category_id']);
170 $rep->TextCol(1, 6, $trans['cat_description']);
171 $catt = $trans['cat_description'];
175 $curr = get_customer_currency($trans['debtor_no']);
176 $rate = get_exchange_rate_from_home_currency($curr, sql2date($trans['tran_date']));
177 $trans['amt'] *= $rate;
178 $cb = $trans['amt'] - $trans['cost'];
181 $rep->TextCol(0, 1, $trans['stock_id']);
182 if ($fromcust == ALL_TEXT)
184 $rep->TextCol(1, 2, $trans['description'].($trans['inactive']==1 ? " ("._("Inactive").")" : ""), -1);
185 $rep->TextCol(2, 3, $trans['debtor_name']);
188 $rep->TextCol(1, 3, $trans['description'].($trans['inactive']==1 ? " ("._("Inactive").")" : ""), -1);
189 $rep->AmountCol(3, 4, $trans['qty'], get_qty_dec($trans['stock_id']));
190 $rep->AmountCol(4, 5, $trans['amt'], $dec);
191 if (is_service($trans['mb_flag']))
192 $rep->TextCol(5, 6, "---");
194 $rep->AmountCol(5, 6, $trans['cost'], $dec);
195 $rep->AmountCol(6, 7, $cb, $dec);
197 $total += $trans['amt'];
198 $total1 += $trans['cost'];
200 $grandtotal += $trans['amt'];
201 $grandtotal1 += $trans['cost'];
205 $rep->TextCol(0, 4, _('Total'));
206 $rep->AmountCol(4, 5, $total, $dec);
207 $rep->AmountCol(5, 6, $total1, $dec);
208 $rep->AmountCol(6, 7, $total2, $dec);
209 $rep->Line($rep->row - 2);
212 $rep->TextCol(0, 4, _('Grand Total'));
213 $rep->AmountCol(4, 5, $grandtotal, $dec);
214 $rep->AmountCol(5, 6, $grandtotal1, $dec);
215 $rep->AmountCol(6, 7, $grandtotal2, $dec);
217 $rep->Line($rep->row - 4);