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