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