[0004212] Work Order Entry: fixed error when voided WO refence is reused.
[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 = 'SA_SUPPLIERANALYTIC';
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 grn.id,
33                         order_no,
34                         grn.supplier_id,
35                         supplier.supp_name,
36                         item.item_code,
37                         item.description,
38                         qty_recd,
39                         quantity_inv,
40                         std_cost_unit,
41                         act_price,
42                         unit_price
43                 FROM ".TB_PREF."grn_items item,
44                         ".TB_PREF."grn_batch grn,
45                         ".TB_PREF."purch_order_details poline,
46                         ".TB_PREF."suppliers supplier
47                 WHERE grn.supplier_id=supplier.supplier_id
48                 AND grn.id = item.grn_batch_id
49                 AND item.po_detail_item = poline.po_detail_item
50                 AND qty_recd-quantity_inv!=0";
51
52         if ($fromsupp != ALL_TEXT)
53                 $sql .= " AND grn.supplier_id =".db_escape($fromsupp);
54
55         $sql .= " ORDER BY grn.supplier_id,     grn.id";
56
57     return db_query($sql, "No transactions were returned");
58 }
59
60 //----------------------------------------------------------------------------------------------------
61
62 function print_outstanding_GRN()
63 {
64     global $path_to_root;
65
66     $fromsupp = $_POST['PARAM_0'];
67     $comments = $_POST['PARAM_1'];
68         $orientation = $_POST['PARAM_2'];
69         $destination = $_POST['PARAM_3'];
70         if ($destination)
71                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
72         else
73                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
74
75         $orientation = ($orientation ? 'L' : 'P');
76         if ($fromsupp == ALL_TEXT)
77                 $from = _('All');
78         else
79                 $from = get_supplier_name($fromsupp);
80     $dec = user_price_dec();
81
82         $cols = array(0, 40, 80, 190,   250, 320, 385, 450,     515);
83
84         $headers = array(_('GRN'), _('Order'), _('Item') . '/' . _('Description'), _('Qty Recd'), _('qty Inv'), _('Balance'),
85                 _('Act Price'), _('Value'));
86
87         $aligns = array('left', 'left', 'left', 'right', 'right', 'right', 'right', 'right');
88
89     $params =   array(  0 => $comments,
90                                     1 => array('text' => _('Supplier'), 'from' => $from, 'to' => ''));
91
92     $rep = new FrontReport(_('Outstanding GRNs Report'), "OutstandingGRN", user_pagesize(), 9, $orientation);
93     if ($orientation == 'L')
94         recalculate_cols($cols);
95
96     $rep->Font();
97     $rep->Info($params, $cols, $headers, $aligns);
98     $rep->NewPage();
99
100         $Tot_Val=0;
101         $Supplier = '';
102         $SuppTot_Val=0;
103         $res = getTransactions($fromsupp);
104
105         While ($GRNs = db_fetch($res))
106         {
107                 $dec2 = get_qty_dec($GRNs['item_code']);
108                 if ($Supplier != $GRNs['supplier_id'])
109                 {
110                         if ($Supplier != '')
111                         {
112                                 $rep->NewLine(2);
113                                 $rep->TextCol(0, 7, _('Total'));
114                                 $rep->AmountCol(7, 8, $SuppTot_Val, $dec);
115                                 $rep->Line($rep->row - 2);
116                                 $rep->NewLine(3);
117                                 $SuppTot_Val = 0;
118                         }
119                         $rep->TextCol(0, 6, $GRNs['supp_name']);
120                         $Supplier = $GRNs['supplier_id'];
121                 }
122                 $rep->NewLine();
123                 $rep->TextCol(0, 1, $GRNs['id']);
124                 $rep->TextCol(1, 2, $GRNs['order_no']);
125                 $rep->TextCol(2, 3, $GRNs['item_code'] . '-' . $GRNs['description']);
126                 $rep->AmountCol(3, 4, $GRNs['qty_recd'], $dec2);
127                 $rep->AmountCol(4, 5, $GRNs['quantity_inv'], $dec2);
128                 $QtyOstg = $GRNs['qty_recd'] - $GRNs['quantity_inv'];
129                 $Value = ($GRNs['qty_recd'] - $GRNs['quantity_inv']) * $GRNs['act_price'];
130                 $rep->AmountCol(5, 6, $QtyOstg, $dec2);
131                 $rep->AmountCol(6, 7, $GRNs['act_price'], $dec);
132                 $rep->AmountCol(7, 8, $Value, $dec);
133                 $Tot_Val += $Value;
134                 $SuppTot_Val += $Value;
135
136                 $rep->NewLine(0, 1);
137         }
138         if ($Supplier != '')
139         {
140                 $rep->NewLine();
141                 $rep->TextCol(0, 7, _('Total'));
142                 $rep->AmountCol(7, 8, $SuppTot_Val, $dec);
143                 $rep->Line($rep->row - 2);
144                 $rep->NewLine(3);
145                 $SuppTot_Val = 0;
146         }
147         $rep->NewLine(2);
148         $rep->TextCol(0, 7, _('Grand Total'));
149         $rep->AmountCol(7, 8, $Tot_Val, $dec);
150         $rep->Line($rep->row - 2);
151         $rep->NewLine();
152     $rep->End();
153 }
154