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: Sales Summary 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, $from, $to)
34 $from = date2sql($from);
36 $sql = "SELECT item.category_id,
37 category.description AS cat_description,
40 line.unit_price * trans.rate AS unit_price,
41 SUM(IF(line.debtor_trans_type = ".ST_CUSTCREDIT.", -line.quantity, line.quantity)) AS quantity
42 FROM ".TB_PREF."stock_master item,
43 ".TB_PREF."stock_category category,
44 ".TB_PREF."debtor_trans trans,
45 ".TB_PREF."debtor_trans_details line
46 WHERE line.stock_id = item.stock_id
47 AND item.category_id=category.category_id
48 AND line.debtor_trans_type=trans.type
49 AND line.debtor_trans_no=trans.trans_no
50 AND trans.tran_date>='$from'
51 AND trans.tran_date<='$to'
53 AND item.mb_flag <>'F'
54 AND (line.debtor_trans_type = ".ST_SALESINVOICE." OR line.debtor_trans_type = ".ST_CUSTCREDIT.")";
56 $sql .= " AND item.category_id = ".db_escape($category);
57 $sql .= " GROUP BY item.category_id,
62 ORDER BY item.category_id, item.stock_id, line.unit_price";
64 //display_notification($sql);
66 return db_query($sql,"No transactions were returned");
70 //----------------------------------------------------------------------------------------------------
72 function print_inventory_sales()
76 $from = $_POST['PARAM_0'];
77 $to = $_POST['PARAM_1'];
78 $category = $_POST['PARAM_2'];
79 $comments = $_POST['PARAM_3'];
80 $orientation = $_POST['PARAM_4'];
81 $destination = $_POST['PARAM_5'];
83 include_once($path_to_root . "/reporting/includes/excel_report.inc");
85 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
87 $orientation = ($orientation ? 'L' : 'P');
88 $dec = user_price_dec();
90 if ($category == ALL_NUMERIC)
95 $cat = get_category_name($category);
97 $cols = array(0, 100, 260, 300, 350, 425, 430, 515);
99 $headers = array(_('Item/Category'), _('Description'), _('Qty'), _('Unit Price'), _('Sales'), '', _('Remark'));
101 $aligns = array('left', 'left', 'right', 'right', 'right', 'right', 'left');
103 $params = array( 0 => $comments,
104 1 => array('text' => _('Period'),'from' => $from, 'to' => $to),
105 2 => array('text' => _('Category'), 'from' => $cat, 'to' => ''));
107 $rep = new FrontReport(_('Item Sales Summary Report'), "ItemSalesSummaryReport", user_pagesize(), 9, $orientation);
108 if ($orientation == 'L')
109 recalculate_cols($cols);
112 $rep->Info($params, $cols, $headers, $aligns);
115 $res = getTransactions($category, $from, $to);
116 $total = $grandtotal = 0.0;
118 while ($trans=db_fetch($res))
120 if ($catt != $trans['cat_description'])
125 $rep->TextCol(0, 4, _('Total'));
126 $rep->AmountCol(4, 5, $total, $dec);
127 $rep->Line($rep->row - 2);
132 $rep->TextCol(0, 1, $trans['category_id']);
133 $rep->TextCol(1, 7, $trans['cat_description']);
134 $catt = $trans['cat_description'];
140 $rep->TextCol(0, 1, $trans['stock_id']);
141 $rep->TextCol(1, 2, $trans['description']);
142 $rep->AmountCol(2, 3, $trans['quantity'], get_qty_dec($trans['stock_id']));
143 $rep->AmountCol(3, 4, $trans['unit_price'], $dec);
144 $rep->AmountCol(4, 5, $trans['quantity']*$trans['unit_price'], $dec);
145 if ($trans['unit_price'] == 0)
146 $rep->TextCol(6, 7, _('Gift'));
148 $total += $trans['quantity']*$trans['unit_price'];
149 $grandtotal += $trans['quantity']*$trans['unit_price'];
152 $rep->TextCol(0, 4, _('Total'));
153 $rep->AmountCol(4, 5, $total, $dec);
154 $rep->Line($rep->row - 2);
157 $rep->TextCol(0, 4, _('Grand Total'));
158 $rep->AmountCol(4, 5, $grandtotal, $dec);
160 $rep->Line($rep->row - 4);