Bug [0000231] Wrong On Order for Advanced Manufacturing.before final production.
[fa-stable.git] / reporting / rep303.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_ITEMSVALREP';
13 // ----------------------------------------------------------------
14 // $ Revision:  2.0 $
15 // Creator:     Joe Hunt
16 // date_:       2005-05-19
17 // Title:       Stock Check Sheet
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 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
26 include_once($path_to_root . "/includes/db/manufacturing_db.inc");
27
28 //----------------------------------------------------------------------------------------------------
29
30 print_stock_check();
31
32 function getTransactions($category, $location)
33 {
34         $sql = "SELECT ".TB_PREF."stock_master.category_id,
35                         ".TB_PREF."stock_category.description AS cat_description,
36                         ".TB_PREF."stock_master.stock_id,
37                         ".TB_PREF."stock_master.description, ".TB_PREF."stock_master.inactive,
38                         IF(".TB_PREF."stock_moves.stock_id IS NULL, '', ".TB_PREF."stock_moves.loc_code) AS loc_code,
39                         SUM(IF(".TB_PREF."stock_moves.stock_id IS NULL,0,".TB_PREF."stock_moves.qty)) AS QtyOnHand
40                 FROM (".TB_PREF."stock_master,
41                         ".TB_PREF."stock_category)
42                 LEFT JOIN ".TB_PREF."stock_moves ON
43                         (".TB_PREF."stock_master.stock_id=".TB_PREF."stock_moves.stock_id OR ".TB_PREF."stock_master.stock_id IS NULL)
44                 WHERE ".TB_PREF."stock_master.category_id=".TB_PREF."stock_category.category_id
45                 AND (".TB_PREF."stock_master.mb_flag='B' OR ".TB_PREF."stock_master.mb_flag='M')";
46         if ($category != 0)
47                 $sql .= " AND ".TB_PREF."stock_master.category_id = ".db_escape($category);
48         if ($location != 'all')
49                 $sql .= " AND IF(".TB_PREF."stock_moves.stock_id IS NULL, '1=1',".TB_PREF."stock_moves.loc_code = ".db_escape($location).")";
50         $sql .= " GROUP BY ".TB_PREF."stock_master.category_id,
51                 ".TB_PREF."stock_category.description,
52                 ".TB_PREF."stock_master.stock_id,
53                 ".TB_PREF."stock_master.description
54                 ORDER BY ".TB_PREF."stock_master.category_id,
55                 ".TB_PREF."stock_master.stock_id";
56
57     return db_query($sql,"No transactions were returned");
58 }
59
60 //----------------------------------------------------------------------------------------------------
61
62 function print_stock_check()
63 {
64         global $comp_path, $path_to_root, $pic_height;
65
66         $category = $_POST['PARAM_0'];
67         $location = $_POST['PARAM_1'];
68         $pictures = $_POST['PARAM_2'];
69         $check    = $_POST['PARAM_3'];
70         $shortage = $_POST['PARAM_4'];
71         $no_zeros = $_POST['PARAM_5'];
72         $comments = $_POST['PARAM_6'];
73         $destination = $_POST['PARAM_7'];
74         if ($destination)
75                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
76         else
77                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
78
79         if ($category == ALL_NUMERIC)
80                 $category = 0;
81         if ($category == 0)
82                 $cat = _('All');
83         else
84                 $cat = get_category_name($category);
85
86         if ($location == ALL_TEXT)
87                 $location = 'all';
88         if ($location == 'all')
89                 $loc = _('All');
90         else
91                 $loc = get_location_name($location);
92         if ($shortage)
93         {
94                 $short = _('Yes');
95                 $available = _('Shortage');
96         }       
97         else
98         {
99                 $short = _('No');
100                 $available = _('Available');
101         }
102         if ($no_zeros) $nozeros = _('Yes');
103         else $nozeros = _('No');
104         if ($check)
105         {
106                 $cols = array(0, 100, 250, 295, 345, 390, 445,  515);
107                 $headers = array(_('Stock ID'), _('Description'), _('Quantity'), _('Check'), _('Demand'), $available, _('On Order'));
108                 $aligns = array('left', 'left', 'right', 'right', 'right', 'right', 'right');
109         }
110         else
111         {
112                 $cols = array(0, 100, 250, 315, 380, 445,       515);
113                 $headers = array(_('Stock ID'), _('Description'), _('Quantity'), _('Demand'), $available, _('On Order'));
114                 $aligns = array('left', 'left', 'right', 'right', 'right', 'right');
115         }
116
117
118         $params =   array(      0 => $comments,
119                                 1 => array('text' => _('Category'), 'from' => $cat, 'to' => ''),
120                                 2 => array('text' => _('Location'), 'from' => $loc, 'to' => ''),
121                                 3 => array('text' => _('Only Shortage'), 'from' => $short, 'to' => ''),
122                                 4 => array('text' => _('Suppress Zeros'), 'from' => $nozeros, 'to' => ''));
123
124         if ($pictures)
125                 $user_comp = user_company();
126         else
127                 $user_comp = "";
128
129         $rep = new FrontReport(_('Stock Check Sheets'), "StockCheckSheet", user_pagesize());
130
131         $rep->Font();
132         $rep->Info($params, $cols, $headers, $aligns);
133         $rep->Header();
134
135         $res = getTransactions($category, $location);
136         $catt = '';
137         while ($trans=db_fetch($res))
138         {
139                 if ($location == 'all')
140                         $loc_code = "";
141                 else
142                         $loc_code = $location;
143                 $demandqty = get_demand_qty($trans['stock_id'], $loc_code);
144                 $demandqty += get_demand_asm_qty($trans['stock_id'], $loc_code);
145                 $onorder = get_on_porder_qty($trans['stock_id'], $loc_code);
146                 $flag = get_mb_flag($trans['stock_id']);
147                 if ($flag == 'M')
148                         $onorder += get_on_worder_qty($trans['stock_id'], $loc_code);
149                 if ($no_zeros && $trans['QtyOnHand'] == 0 && $demandqty == 0 && $onorder == 0)
150                         continue;
151                 if ($shortage && $trans['QtyOnHand'] - $demandqty >= 0)
152                         continue;
153                 if ($catt != $trans['cat_description'])
154                 {
155                         if ($catt != '')
156                         {
157                                 $rep->Line($rep->row - 2);
158                                 $rep->NewLine(2, 3);
159                         }
160                         $rep->TextCol(0, 1, $trans['category_id']);
161                         $rep->TextCol(1, 2, $trans['cat_description']);
162                         $catt = $trans['cat_description'];
163                         $rep->NewLine();
164                 }
165                 $rep->NewLine();
166                 $dec = get_qty_dec($trans['stock_id']);
167                 $rep->TextCol(0, 1, $trans['stock_id']);
168                 $rep->TextCol(1, 2, $trans['description'].($trans['inactive']==1 ? " ("._("Inactive").")" : ""), -1);
169                 $rep->AmountCol(2, 3, $trans['QtyOnHand'], $dec);
170                 if ($check)
171                 {
172                         $rep->TextCol(3, 4, "_________");
173                         $rep->AmountCol(4, 5, $demandqty, $dec);
174                         $rep->AmountCol(5, 6, $trans['QtyOnHand'] - $demandqty, $dec);
175                         $rep->AmountCol(6, 7, $onorder, $dec);
176                 }
177                 else
178                 {
179                         $rep->AmountCol(3, 4, $demandqty, $dec);
180                         $rep->AmountCol(4, 5, $trans['QtyOnHand'] - $demandqty, $dec);
181                         $rep->AmountCol(5, 6, $onorder, $dec);
182                 }
183                 if ($pictures)
184                 {
185                         $image = $comp_path .'/'. $user_comp . '/images/' 
186                                 . item_img_name($trans['stock_id']) . '.jpg';
187                         if (file_exists($image))
188                         {
189                                 $rep->NewLine();
190                                 if ($rep->row - $pic_height < $rep->bottomMargin)
191                                         $rep->Header();
192                                 $rep->AddImage($image, $rep->cols[1], $rep->row - $pic_height, 0, $pic_height);
193                                 $rep->row -= $pic_height;
194                                 $rep->NewLine();
195                         }
196                 }
197         }
198         $rep->Line($rep->row - 4);
199         $rep->NewLine();
200     $rep->End();
201 }
202
203 ?>