Security sql statements update against sql injection attacks.
[fa-stable.git] / reporting / rep204.php
1 <?php
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 = 2;
13 // ----------------------------------------------------------------
14 // $ Revision:  2.0 $
15 // Creator:     Joe Hunt
16 // date_:       2005-05-19
17 // Title:       Outstanding GRNs Report
18 // ----------------------------------------------------------------
19 $path_to_root="..";
20
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 . "/gl/includes/gl_db.inc");
25
26 //----------------------------------------------------------------------------------------------------
27
28 print_outstanding_GRN();
29
30 function getTransactions($fromsupp)
31 {
32         $sql = "SELECT ".TB_PREF."grn_batch.id,
33                         order_no,
34                         ".TB_PREF."grn_batch.supplier_id,
35                         ".TB_PREF."suppliers.supp_name,
36                         ".TB_PREF."grn_items.item_code,
37                         ".TB_PREF."grn_items.description,
38                         qty_recd,
39                         quantity_inv,
40                         std_cost_unit,
41                         act_price,
42                         unit_price
43                 FROM ".TB_PREF."grn_items,
44                         ".TB_PREF."grn_batch,
45                         ".TB_PREF."purch_order_details,
46                         ".TB_PREF."suppliers
47                 WHERE ".TB_PREF."grn_batch.supplier_id=".TB_PREF."suppliers.supplier_id
48                 AND ".TB_PREF."grn_batch.id = ".TB_PREF."grn_items.grn_batch_id
49                 AND ".TB_PREF."grn_items.po_detail_item = ".TB_PREF."purch_order_details.po_detail_item
50                 AND qty_recd-quantity_inv <>0 ";
51         if ($fromsupp != reserved_words::get_all_numeric())
52                 $sql .= "AND ".TB_PREF."grn_batch.supplier_id =".db_escape($fromsupp)." ";
53         $sql .= "ORDER BY ".TB_PREF."grn_batch.supplier_id,
54                         ".TB_PREF."grn_batch.id";
55
56     return db_query($sql, "No transactions were returned");
57 }
58
59 //----------------------------------------------------------------------------------------------------
60
61 function print_outstanding_GRN()
62 {
63     global $path_to_root;
64
65     $fromsupp = $_POST['PARAM_0'];
66     $comments = $_POST['PARAM_1'];
67         $destination = $_POST['PARAM_2'];
68         if ($destination)
69                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
70         else
71                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
72
73         if ($fromsupp == reserved_words::get_all_numeric())
74                 $from = _('All');
75         else
76                 $from = get_supplier_name($fromsupp);
77     $dec = user_price_dec();
78
79         $cols = array(0, 40, 80, 190,   250, 320, 385, 450,     515);
80
81         $headers = array(_('GRN'), _('Order'), _('Item') . '/' . _('Description'), _('Qty Recd'), _('qty Inv'), _('Balance'),
82                 _('Std Cost'), _('Value'));
83
84         $aligns = array('left', 'left', 'left', 'right', 'right', 'right', 'right', 'right');
85
86     $params =   array(  0 => $comments,
87                                     1 => array('text' => _('Supplier'), 'from' => $from, 'to' => ''));
88
89     $rep = new FrontReport(_('Outstanding GRNs Report'), "OutstandingGRN", user_pagesize());
90
91     $rep->Font();
92     $rep->Info($params, $cols, $headers, $aligns);
93     $rep->Header();
94
95         $Tot_Val=0;
96         $Supplier = '';
97         $SuppTot_Val=0;
98         $res = getTransactions($fromsupp);
99
100         While ($GRNs = db_fetch($res))
101         {
102                 $dec2 = get_qty_dec($GRNs['item_code']);
103                 if ($Supplier != $GRNs['supplier_id'])
104                 {
105                         if ($Supplier != '')
106                         {
107                                 $rep->NewLine(2);
108                                 $rep->TextCol(0, 7, _('Total'));
109                                 $rep->AmountCol(7, 8, $SuppTot_Val, $dec);
110                                 $rep->Line($rep->row - 2);
111                                 $rep->NewLine(3);
112                                 $SuppTot_Val = 0;
113                         }
114                         $rep->TextCol(0, 6, $GRNs['supp_name']);
115                         $Supplier = $GRNs['supplier_id'];
116                 }
117                 $rep->NewLine();
118                 $rep->TextCol(0, 1, $GRNs['id']);
119                 $rep->TextCol(1, 2, $GRNs['order_no']);
120                 $rep->TextCol(2, 3, $GRNs['item_code'] . '-' . $GRNs['description']);
121                 $rep->AmountCol(3, 4, $GRNs['qty_recd'], $dec2);
122                 $rep->AmountCol(4, 5, $GRNs['quantity_inv'], $dec2);
123                 $QtyOstg = $GRNs['qty_recd'] - $GRNs['quantity_inv'];
124                 $Value = ($GRNs['qty_recd'] - $GRNs['quantity_inv']) * $GRNs['std_cost_unit'];
125                 $rep->AmountCol(5, 6, $QtyOstg, $dec2);
126                 $rep->AmountCol(6, 7, $GRNs['std_cost_unit'], $dec);
127                 $rep->AmountCol(7, 8, $Value, $dec);
128                 $Tot_Val += $Value;
129                 $SuppTot_Val += $Value;
130
131                 $rep->NewLine(0, 1);
132         }
133         if ($Supplier != '')
134         {
135                 $rep->NewLine();
136                 $rep->TextCol(0, 7, _('Total'));
137                 $rep->AmountCol(7, 8, $SuppTot_Val, $dec);
138                 $rep->Line($rep->row - 2);
139                 $rep->NewLine(3);
140                 $SuppTot_Val = 0;
141         }
142         $rep->NewLine(2);
143         $rep->TextCol(0, 7, _('Grand Total'));
144         $rep->AmountCol(7, 8, $Tot_Val, $dec);
145         $rep->Line($rep->row - 2);
146         $rep->NewLine();
147     $rep->End();
148 }
149
150 ?>