[0004212] Work Order Entry: fixed error when voided WO refence is reused.
[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         $barcodes = !empty($SysPrefs->prefs['barcodes_on_stock']);
116         if ($no_zeros) $nozeros = _('Yes');
117         else $nozeros = _('No');
118         if ($check)
119         {
120                 $cols = array(0, 75, 225, 250, 295, 345, 390, 445,      515);
121                 $headers = array(_('Stock ID'), _('Description'), _('UOM'), _('Quantity'), _('Check'), _('Demand'), $available, _('On Order'));
122                 $aligns = array('left', 'left', 'left', 'right', 'right', 'right', 'right', 'right');
123         }
124         else
125         {
126                 $cols = array(0, 75, 225, 250, 315, 380, 445,   515);
127                 $headers = array(_('Stock ID'), _('Description'), _('UOM'), _('Quantity'), _('Demand'), $available, _('On Order'));
128                 $aligns = array('left', 'left', 'left', 'right', 'right', 'right', 'right');
129         }
130
131     $params =   array(
132                 0 => $comments,
133         1 => array('text' => _('Category'), 'from' => $cat, 'to' => ''),
134         2 => array('text' => _('Location'), 'from' => $loc, 'to' => ''),
135         3 => array('text' => _('Only Shortage'), 'from' => $short, 'to' => ''),
136                 4 => array('text' => _('Suppress Zeros'), 'from' => $nozeros, 'to' => '')
137         );
138
139         if ($barcodes)
140         {
141         // define barcode style
142         $style = array(
143                 'position' => 'L', // If blank string, barcode starts on left edge of page
144                 'stretch' => false,
145                 'fitwidth' => true,
146                 'cellfitalign' => '',
147                 'border' => false,
148                 'padding' => 3,
149                 'fgcolor' => array(0,0,0),
150                 'bgcolor' => false, //array(255,255,255),
151                 'text' => true,
152                 'font' => 'helvetica',
153                 'fontsize' => 8,
154                 'stretchtext' => 4
155         );
156         // write1DBarcode($code, $type, $x='', $y='', $w='', $h='', $xres=0.4, $style='', $align='')
157     }   
158
159         $rep = new FrontReport(_('Stock Check Sheets'), "StockCheckSheet", user_pagesize(), 9, $orientation);
160     if ($orientation == 'L')
161         recalculate_cols($cols);
162
163     $rep->Font();
164     $rep->Info($params, $cols, $headers, $aligns);
165     $rep->NewPage();
166
167         $res = getTransactions($category, $location, $like);
168         $catt = '';
169         while ($trans=db_fetch($res))
170         {
171                 if ($location == 'all')
172                         $loc_code = "";
173                 else
174                         $loc_code = $location;
175                 $demandqty = get_demand_qty($trans['stock_id'], $loc_code);
176                 $demandqty += get_demand_asm_qty($trans['stock_id'], $loc_code);
177                 $onorder = get_on_porder_qty($trans['stock_id'], $loc_code);
178                 $onorder += get_on_worder_qty($trans['stock_id'], $loc_code);
179                 if ($no_zeros && $trans['QtyOnHand'] == 0 && $demandqty == 0 && $onorder == 0)
180                         continue;
181                 if ($shortage && $trans['QtyOnHand'] - $demandqty >= 0)
182                         continue;
183                 if ($catt != $trans['cat_description'])
184                 {
185                         if ($catt != '')
186                         {
187                                 $rep->Line($rep->row - 2);
188                                 $rep->NewLine(2, 3);
189                         }
190                         $rep->TextCol(0, 1, $trans['category_id']);
191                         $rep->TextCol(1, 2, $trans['cat_description']);
192                         $catt = $trans['cat_description'];
193                         $rep->NewLine();
194                 }
195                 $rep->NewLine();
196                 $dec = get_qty_dec($trans['stock_id']);
197                 $rep->TextCol(0, 1, $trans['stock_id']);
198                 $rep->TextCol(1, 2, $trans['description'].($trans['inactive']==1 ? " ("._("Inactive").")" : ""), -1);
199                 $rep->TextCol(2, 3, $trans['units']);
200                 $rep->AmountCol(3, 4, $trans['QtyOnHand'], $dec);
201                 if ($check)
202                 {
203                         $rep->TextCol(4, 5, "_________");
204                         $rep->AmountCol(5, 6, $demandqty, $dec);
205                         $rep->AmountCol(6, 7, $trans['QtyOnHand'] - $demandqty, $dec);
206                         $rep->AmountCol(7, 8, $onorder, $dec);
207                 }
208                 else
209                 {
210                         $rep->AmountCol(4, 5, $demandqty, $dec);
211                         $rep->AmountCol(5, 6, $trans['QtyOnHand'] - $demandqty, $dec);
212                         $rep->AmountCol(6, 7, $onorder, $dec);
213                 }
214                 if ($pictures || $barcodes)
215                 {
216                         $rep->NewLine();
217                         if ($rep->row - $SysPrefs->pic_height < $rep->bottomMargin)
218                                 $rep->NewPage();
219                         $firstcol = 1;  
220                         if ($barcodes)
221                         {
222                                 $bar_y = $rep->GetY();
223                                 $barcode = str_pad($trans['stock_id'], 7, '0', STR_PAD_LEFT);
224                                 $barcode = substr($barcode, 0, 8); // EAN 8 Check digit is auto computed and barcode printed
225                                 $rep->write1DBarcode($barcode, 'EAN8', $rep->cols[$firstcol++], $bar_y + 22, 22, $SysPrefs->pic_height, 1.2, $style, 'N');
226                         }       
227                         if ($pictures)
228                         {
229                                 $image = company_path() . '/images/' . item_img_name($trans['stock_id']) . '.jpg';
230                                 if (file_exists($image))
231                                 {
232                                         $rep->AddImage($image, $rep->cols[$firstcol], $rep->row - $SysPrefs->pic_height, 0, $SysPrefs->pic_height);
233                                 }
234                         }       
235                         $rep->row -= $SysPrefs->pic_height;
236                         $rep->NewLine();
237                 }
238         }
239         $rep->Line($rep->row - 4);
240         $rep->NewLine();
241     $rep->End();
242 }
243