Option to hide Manufacturing and Fixed Assets tabs in reorganized company settings...
[fa-stable.git] / reporting / rep305.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:       GRN Valuation 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 . "/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");
27
28 //----------------------------------------------------------------------------------------------------
29
30 print_grn_valuation();
31
32 function getTransactions($from, $to)
33 {
34         $from = date2sql($from);
35         $to = date2sql($to);
36
37         $sql = "SELECT grn.id batch_no,
38                         grn.supplier_id, 
39             poline.*,
40             item.description,
41                         grn_line.qty_recd,
42                         grn_line.quantity_inv,
43                         grn_line.id grn_item_id
44         FROM "
45                 .TB_PREF."stock_master item,"
46                 .TB_PREF."purch_order_details poline,"
47                 .TB_PREF."grn_batch grn,"
48                 .TB_PREF."grn_items grn_line
49         WHERE item.stock_id=poline.item_code
50         AND grn.purch_order_no=poline.order_no
51                 AND grn.id = grn_line.grn_batch_id 
52                 AND grn_line.po_detail_item = poline.po_detail_item
53         AND grn_line.qty_recd>0
54         AND grn.delivery_date>='$from'
55         AND grn.delivery_date<='$to'
56         ORDER BY item.stock_id, grn.delivery_date";
57
58     return db_query($sql,"No transactions were returned");
59
60 }
61
62 function getSuppInvDetails($grn_item_id)
63 {
64         $sql = "SELECT
65                         inv_line.supp_trans_no inv_no,
66                         inv_line.quantity inv_qty,
67                         inv.rate,
68                         IF (inv.tax_included = 1, inv_line.unit_price - inv_line.unit_tax, inv_line.unit_price) inv_price
69                         FROM "
70                                 .TB_PREF."grn_items grn_line,"
71                                 .TB_PREF."supp_trans inv,"
72                                 .TB_PREF."supp_invoice_items inv_line
73                         WHERE grn_line.id = inv_line.grn_item_id
74                         AND grn_line.po_detail_item = inv_line.po_detail_item_id
75                         AND grn_line.item_code = inv_line.stock_id
76                         AND inv.type = inv_line.supp_trans_type
77                         AND inv.trans_no = inv_line.supp_trans_no
78                         AND inv_line.supp_trans_type = 20
79                         AND inv_line.grn_item_id = ".$grn_item_id."
80                         ORDER BY inv_line.id asc";
81
82         return db_query($sql,"No transactions were returned");
83 }
84
85 //----------------------------------------------------------------------------------------------------
86
87 function print_grn_valuation()
88 {
89     global $path_to_root;
90
91         $from = $_POST['PARAM_0'];
92         $to = $_POST['PARAM_1'];
93         $comments = $_POST['PARAM_2'];
94         $orientation = $_POST['PARAM_3'];
95         $destination = $_POST['PARAM_4'];
96         if ($destination)
97                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
98         else
99                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
100
101         $orientation = ($orientation ? 'L' : 'P');
102         $dec = user_price_dec();
103
104         $cols = array(0, 75, 225, 260, 295, 330, 370, 410, 455, 515);
105         $headers = array(_('Stock ID'), _('Description'), _('PO No'), _('GRN')."#", _('Inv')."#", _('Qty'), _('Inv Price'), _('PO Price'), _('Total'));
106
107         $aligns = array('left', 'left', 'left', 'left', 'left', 'right', 'right', 'right', 'right');
108
109     $params =   array(  0 => $comments,
110                                     1 => array('text' => _('Period'),'from' => $from, 'to' => $to));
111
112     $rep = new FrontReport(_('GRN Valuation Report'), "GRNValuationReport", user_pagesize(), 9, $orientation);
113     if ($orientation == 'L')
114         recalculate_cols($cols);
115
116     $rep->Font();
117     $rep->Info($params, $cols, $headers, $aligns);
118     $rep->NewPage();
119
120         $res = getTransactions($from, $to);
121         $total = $qtotal = $grandtotal = 0.0;
122         $stock_id = '';
123         while ($trans=db_fetch($res))
124         {
125                 if ($stock_id != $trans['item_code'])
126                 {
127                         if ($stock_id != '')
128                         {
129                                 $rep->Line($rep->row  - 4);
130                                 $rep->NewLine(2);
131                                 $rep->TextCol(0, 3, _('Total'));
132                                 $rep->AmountCol(5, 6, $qtotal, $qdec);
133                                 $rep->AmountCol(8, 9, $total, $dec);
134                                 $rep->NewLine();
135                                 $total = $qtotal = 0;
136                         }
137                         $stock_id = $trans['item_code'];
138                 }
139
140                 $rep->NewLine();
141                 $rep->TextCol(0, 1, $trans['item_code']);
142                 $rep->TextCol(1, 2, $trans['description']);
143                 $rep->TextCol(2, 3, $trans['order_no']);
144                 $qdec = get_qty_dec($trans['item_code']);
145                 $rep->TextCol(3, 4, $trans['batch_no']);
146
147                 if ($trans['quantity_inv'])
148                 {
149                         $suppinv = getSuppInvDetails($trans['grn_item_id']);
150                         while ($inv=db_fetch($suppinv))
151                         {       
152                                 $inv['inv_price'] *= $inv['rate'];
153                                 $rep->TextCol(4, 5, $inv['inv_no']);
154                                 $rep->AmountCol(5, 6, $inv['inv_qty'], $qdec);
155                                 $rep->AmountCol(6, 7, $inv['inv_price'], $dec);
156                                 $rep->AmountCol(7, 8, $trans['std_cost_unit'], $dec);
157                                 $amt = round2($inv['inv_qty'] * $inv['inv_price'], $dec);
158                                 $rep->AmountCol(8, 9, $amt, $dec);
159                                 $rep->NewLine();
160                                 $total += $amt;
161                                 $qtotal += $inv['inv_qty'];
162                                 $grandtotal += $amt;
163                         }
164                 }
165                 
166                 if ($trans['qty_recd'] - $trans['quantity_inv'] !=0 )
167                 {
168                         $curr = get_supplier_currency($trans['supplier_id']);
169                         $rate = get_exchange_rate_from_home_currency($curr, sql2date($trans['delivery_date']));
170                         $trans['unit_price'] *= $rate;
171                         $rep->TextCol(4, 5, "--");
172                         $rep->AmountCol(5, 6, $trans['qty_recd'] - $trans['quantity_inv'], $qdec);
173                         $rep->AmountCol(7, 8, $trans['unit_price'], $dec);
174                         $amt = round2(($trans['qty_recd'] - $trans['quantity_inv']) * $trans['unit_price'], $dec);
175                         $rep->AmountCol(8, 9, $amt, $dec);
176                         $total += $amt;
177                         $qtotal += $trans['qty_recd'] - $trans['quantity_inv'];
178                         $grandtotal += $amt;
179                 }
180                 else
181                         $rep->NewLine(-1);
182         }
183         if ($stock_id != '')
184         {
185                 $rep->Line($rep->row  - 4);
186                 $rep->NewLine(2);
187                 $rep->TextCol(0, 3, _('Total'));
188                 $rep->AmountCol(5, 6, $qtotal, $qdec);
189                 $rep->AmountCol(8, 9, $total, $dec);
190                 $rep->Line($rep->row  - 4);
191                 $rep->NewLine(2);
192                 $rep->TextCol(0, 7, _('Grand Total'));
193                 $rep->AmountCol(8, 9, $grandtotal, $dec);
194         }
195
196         $rep->Line($rep->row  - 4);
197         $rep->NewLine();
198     $rep->End();
199 }
200