Feature 5388: Print Invoices (documents) list gets too long. Fixed by default 180...
[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 /**
33  * Bar codes checker - Checks if a barcode can be valid and returns type of barcode
34  * 
35  * @link                http://www.phpclasses.org/package/8560-PHP-Detect-type-and-check-EAN-and-UPC-barcodes.html
36  * @type tests  EAN, EAN-8, EAN-13, GTIN-8, GTIN-12, GTIN-14, UPC, UPC-12 coupon code, JAN 
37  * @author              Ferry Bouwhuis
38  * @version             1.0.1
39  * @LastChange  2014-04-13
40  */
41
42 function barcode_check($code, $return_value = false, $get_type = false)
43 {
44         //Setting return value
45         /*
46          * If true it will retun al barcodes as 14 digit strings
47          * If false it will return only what is needed UPC -> 12 / EAN -> 13 / 
48          */
49
50         //Filter UPC coupon codes
51         /*
52          * If true it will return false on UPC coupon codes
53          * Type will always return UPC coupon code
54          */
55         $skip_coupon_codes = true;
56
57         //Trims parsed string to remove unwanted whitespace or characters
58         $code = (string)trim($code); 
59         if (preg_match('/[^0-9]/', $code))
60                 return false;
61
62         if (!is_string($code))
63                 $code = strval($code);
64         $code = trim($code);    
65         $length = strlen($code);
66         if(($length > 11 && $length <= 14) || $length == 8)
67         {       
68                 $zeroes = 18 - $length;
69                 $fill = "";
70                 for ($i = 0; $i < $zeroes; $i++)
71                         $fill .= "0";
72                 $code = $fill . $code;
73                 
74                 $calc = 0;
75                 for ($i = 0; $i < (strlen($code) - 1); $i++)
76                         $calc += ($i % 2 ? $code[$i] * 1 :  $code[$i] * 3);
77
78                 if (substr(10 - (substr($calc, -1)), -1) != substr($code, -1))
79                         return false;
80                 elseif (substr($code, 5, 1) > 2)
81                 {
82                         //EAN / JAN / EAN-13 code
83                         if ($get_type)
84                                 return 'EAN';
85                         else
86                                 return (string)substr($code, ($return_value ? -14 : -13));
87                 }
88                 elseif (substr($code, 6, 1) == 0 && substr($code, 0, 10) == 0)
89                 {
90                         //EAN-8 / GTIN-8 code
91                         if ($get_type)
92                                 return 'EAN-8';
93                         else
94                                 return (string)substr($code, ($return_value ? -14 : -8));
95                 }
96                 elseif (substr($code, 5, 1) <= 0)
97                 {
98                         //UPC / UCC-12 GTIN-12 code
99                         if ($get_type)
100                         {
101                                 if (substr($code, 6, 1) == 5)
102                                         return 'UPC coupon code';
103                                 else
104                                         return 'UPC';
105                         }
106                         else
107                         {
108                                 if ($skip_coupon_codes && substr($code, 6, 1) == 5)
109                                         return false;
110                                 return (string)substr($code, ($return_value ? -14 : -12));
111                         }
112                 }
113                 elseif (substr($code, 0, 6) == 0)
114                 {
115                         //GTIN-14
116                         if ($get_type)
117                                 return 'GTIN-14';
118                         else
119                                 return (string)substr($code, -14);
120                 }
121                 else
122                 {
123                         //EAN code
124                         if ($get_type)
125                                 return 'EAN';
126                         else
127                                 return (string)substr($code,($return_value ? -14 : -13));
128                 }
129         }
130         else
131                 return false;
132 }
133         
134 function getTransactions($category, $location, $item_like)
135 {
136         $sql = "SELECT item.category_id,
137                         category.description AS cat_description,
138                         item.stock_id, item.units,
139                         item.description, item.inactive,
140                         IF(move.stock_id IS NULL, '', move.loc_code) AS loc_code,
141                         SUM(IF(move.stock_id IS NULL,0,move.qty)) AS QtyOnHand
142                 FROM ("
143                         .TB_PREF."stock_master item,"
144                         .TB_PREF."stock_category category)
145                         LEFT JOIN ".TB_PREF."stock_moves move ON item.stock_id=move.stock_id
146                 WHERE item.category_id=category.category_id
147                 AND (item.mb_flag='B' OR item.mb_flag='M')";
148         if ($category != 0)
149                 $sql .= " AND item.category_id = ".db_escape($category);
150         if ($location != 'all')
151                 $sql .= " AND IF(move.stock_id IS NULL, '1=1',move.loc_code = ".db_escape($location).")";
152   if($item_like)
153   {
154     $regexp = null;
155
156     if(sscanf($item_like, "/%s", $regexp)==1)
157       $sql .= " AND item.stock_id RLIKE ".db_escape($regexp);
158     else
159       $sql .= " AND item.stock_id LIKE ".db_escape($item_like);
160   }
161         $sql .= " GROUP BY item.category_id,
162                 category.description,
163                 item.stock_id,
164                 item.description
165                 ORDER BY item.category_id,
166                 item.stock_id";
167
168     return db_query($sql,"No transactions were returned");
169 }
170
171 //----------------------------------------------------------------------------------------------------
172
173 function print_stock_check()
174 {
175     global $path_to_root, $SysPrefs;
176
177         $category = $_POST['PARAM_0'];
178         $location = $_POST['PARAM_1'];
179         $pictures = $_POST['PARAM_2'];
180         $check    = $_POST['PARAM_3'];
181         $shortage = $_POST['PARAM_4'];
182         $no_zeros = $_POST['PARAM_5'];
183         $like     = $_POST['PARAM_6']; 
184         $comments = $_POST['PARAM_7'];
185         $orientation = $_POST['PARAM_8'];
186         $destination = $_POST['PARAM_9'];
187
188         if ($destination)
189                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
190         else
191                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
192
193         $orientation = ($orientation ? 'L' : 'P');
194         if ($category == ALL_NUMERIC)
195                 $category = 0;
196         if ($category == 0)
197                 $cat = _('All');
198         else
199                 $cat = get_category_name($category);
200
201         if ($location == ALL_TEXT)
202                 $location = 'all';
203         if ($location == 'all')
204                 $loc = _('All');
205         else
206                 $loc = get_location_name($location);
207         if ($shortage)
208         {
209                 $short = _('Yes');
210                 $available = _('Shortage');
211         }
212         else
213         {
214                 $short = _('No');
215                 $available = _('Available');
216         }
217         $barcodes = !empty($SysPrefs->prefs['barcodes_on_stock']);
218         if ($no_zeros) $nozeros = _('Yes');
219         else $nozeros = _('No');
220         if ($check)
221         {
222                 $cols = array(0, 75, 225, 250, 295, 345, 390, 445,      515);
223                 $headers = array(_('Stock ID'), _('Description'), _('UOM'), _('Quantity'), _('Check'), _('Demand'), $available, _('On Order'));
224                 $aligns = array('left', 'left', 'left', 'right', 'right', 'right', 'right', 'right');
225         }
226         else
227         {
228                 $cols = array(0, 75, 225, 250, 315, 380, 445,   515);
229                 $headers = array(_('Stock ID'), _('Description'), _('UOM'), _('Quantity'), _('Demand'), $available, _('On Order'));
230                 $aligns = array('left', 'left', 'left', 'right', 'right', 'right', 'right');
231         }
232
233     $params =   array(
234                 0 => $comments,
235         1 => array('text' => _('Category'), 'from' => $cat, 'to' => ''),
236         2 => array('text' => _('Location'), 'from' => $loc, 'to' => ''),
237         3 => array('text' => _('Only Shortage'), 'from' => $short, 'to' => ''),
238                 4 => array('text' => _('Suppress Zeros'), 'from' => $nozeros, 'to' => '')
239         );
240
241         if ($barcodes)
242         {
243         // define barcode style
244         $style = array(
245                 'position' => 'L', // If blank string, barcode starts on left edge of page
246                 'stretch' => false,
247                 'fitwidth' => true,
248                 'cellfitalign' => '',
249                 'border' => false,
250                 'padding' => 3,
251                 'fgcolor' => array(0,0,0),
252                 'bgcolor' => false, //array(255,255,255),
253                 'text' => true,
254                 'font' => 'helvetica',
255                 'fontsize' => 8,
256                 'stretchtext' => 4
257         );
258         // write1DBarcode($code, $type, $x='', $y='', $w='', $h='', $xres=0.4, $style='', $align='')
259     }   
260
261         $rep = new FrontReport(_('Stock Check Sheets'), "StockCheckSheet", user_pagesize(), 9, $orientation);
262     if ($orientation == 'L')
263         recalculate_cols($cols);
264
265     $rep->Font();
266     $rep->Info($params, $cols, $headers, $aligns);
267     $rep->NewPage();
268
269         $res = getTransactions($category, $location, $like);
270         $catt = '';
271         while ($trans=db_fetch($res))
272         {
273                 if ($location == 'all')
274                         $loc_code = "";
275                 else
276                         $loc_code = $location;
277                 $demandqty = get_demand_qty($trans['stock_id'], $loc_code);
278                 $demandqty += get_demand_asm_qty($trans['stock_id'], $loc_code);
279                 $onorder = get_on_porder_qty($trans['stock_id'], $loc_code);
280                 $onorder += get_on_worder_qty($trans['stock_id'], $loc_code);
281                 if ($no_zeros && $trans['QtyOnHand'] == 0 && $demandqty == 0 && $onorder == 0)
282                         continue;
283                 if ($shortage && $trans['QtyOnHand'] - $demandqty >= 0)
284                         continue;
285                 if ($catt != $trans['cat_description'])
286                 {
287                         if ($catt != '')
288                         {
289                                 $rep->Line($rep->row - 2);
290                                 $rep->NewLine(2, 3);
291                         }
292                         $rep->TextCol(0, 1, $trans['category_id']);
293                         $rep->TextCol(1, 2, $trans['cat_description']);
294                         $catt = $trans['cat_description'];
295                         $rep->NewLine();
296                 }
297                 $rep->NewLine();
298                 $dec = get_qty_dec($trans['stock_id']);
299                 $rep->TextCol(0, 1, $trans['stock_id']);
300                 $rep->TextCol(1, 2, $trans['description'].($trans['inactive']==1 ? " ("._("Inactive").")" : ""), -1);
301                 $rep->TextCol(2, 3, $trans['units']);
302                 $rep->AmountCol(3, 4, $trans['QtyOnHand'], $dec);
303                 if ($check)
304                 {
305                         $rep->TextCol(4, 5, "_________");
306                         $rep->AmountCol(5, 6, $demandqty, $dec);
307                         $rep->AmountCol(6, 7, $trans['QtyOnHand'] - $demandqty, $dec);
308                         $rep->AmountCol(7, 8, $onorder, $dec);
309                 }
310                 else
311                 {
312                         $rep->AmountCol(4, 5, $demandqty, $dec);
313                         $rep->AmountCol(5, 6, $trans['QtyOnHand'] - $demandqty, $dec);
314                         $rep->AmountCol(6, 7, $onorder, $dec);
315                 }
316                 if ($pictures || $barcodes)
317                 {
318                         $rep->NewLine();
319                         if ($rep->row - $SysPrefs->pic_height < $rep->bottomMargin)
320                                 $rep->NewPage();
321                         $firstcol = 1;  
322                         $adjust = false;
323                         if ($barcodes && barcode_check($trans['stock_id']))
324                         {
325                                 $adjust = true;
326                                 $bar_y = $rep->GetY();
327                                 $barcode = str_pad($trans['stock_id'], 7, '0', STR_PAD_LEFT);
328                                 $barcode = substr($barcode, 0, 8); // EAN 8 Check digit is auto computed and barcode printed
329                                 $rep->write1DBarcode($barcode, 'EAN8', $rep->cols[$firstcol++], $bar_y + 22, 22, $SysPrefs->pic_height, 1.2, $style, 'N');
330                         }       
331                         if ($pictures)
332                         {
333                                 $adjust = true;
334                                 $image = company_path() . '/images/' . item_img_name($trans['stock_id']) . '.jpg';
335                                 if (file_exists($image))
336                                 {
337                                         $rep->AddImage($image, $rep->cols[$firstcol], $rep->row - $SysPrefs->pic_height, 0, $SysPrefs->pic_height);
338                                 }
339                         }
340                         if ($adjust)
341                                 $rep->row -= $SysPrefs->pic_height;
342                 }
343         }
344         $rep->Line($rep->row - 4);
345         $rep->NewLine();
346     $rep->End();
347 }
348