PageRenderTime 110ms CodeModel.GetById 91ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 1ms

/htdocs/product/inventory/inventory.php

http://github.com/Dolibarr/dolibarr
PHP | 433 lines | 243 code | 79 blank | 111 comment | 42 complexity | 4f2891a5c8b8f65b5748f77468539552 MD5 | raw file
  1<?php
  2/* Copyright (C) 2019 Laurent Destailleur  <eldy@users.sourceforge.net>
  3 *
  4 * This program is free software; you can redistribute it and/or modify
  5 * it under the terms of the GNU General Public License as published by
  6 * the Free Software Foundation; either version 3 of the License, or
  7 * (at your option) any later version.
  8 *
  9 * This program is distributed in the hope that it will be useful,
 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12 * GNU General Public License for more details.
 13 *
 14 * You should have received a copy of the GNU General Public License
 15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
 16 */
 17
 18/**
 19 *   	\file       htdocs/product/inventory/inventory.php
 20 *		\ingroup    inventory
 21 *		\brief      Tabe to enter counting
 22 */
 23
 24require '../../main.inc.php';
 25include_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
 26include_once DOL_DOCUMENT_ROOT.'/product/inventory/class/inventory.class.php';
 27include_once DOL_DOCUMENT_ROOT.'/product/inventory/lib/inventory.lib.php';
 28
 29// Load translation files required by the page
 30$langs->loadLangs(array("stocks", "other"));
 31
 32// Get parameters
 33$id = GETPOST('id', 'int');
 34$ref = GETPOST('ref', 'alpha');
 35$action = GETPOST('action', 'aZ09');
 36$confirm = GETPOST('confirm', 'alpha');
 37$cancel = GETPOST('cancel', 'aZ09');
 38$contextpage = GETPOST('contextpage', 'aZ') ?GETPOST('contextpage', 'aZ') : 'myobjectcard'; // To manage different context of search
 39$backtopage = GETPOST('backtopage', 'alpha');
 40
 41if (empty($conf->global->MAIN_USE_ADVANCED_PERMS))
 42{
 43	$result = restrictedArea($user, 'stock', $id);
 44}
 45else
 46{
 47	$result = restrictedArea($user, 'stock', $id, '', 'inventory_advance');
 48}
 49
 50// Initialize technical objects
 51$object = new Inventory($db);
 52$extrafields = new ExtraFields($db);
 53$diroutputmassaction = $conf->stock->dir_output.'/temp/massgeneration/'.$user->id;
 54$hookmanager->initHooks(array('inventorycard')); // Note that conf->hooks_modules contains array
 55
 56// Fetch optionals attributes and labels
 57$extrafields->fetch_name_optionals_label($object->table_element);
 58
 59$search_array_options = $extrafields->getOptionalsFromPost($object->table_element, '', 'search_');
 60
 61// Initialize array of search criterias
 62$search_all = trim(GETPOST("search_all", 'alpha'));
 63$search = array();
 64foreach ($object->fields as $key => $val)
 65{
 66    if (GETPOST('search_'.$key, 'alpha')) $search[$key] = GETPOST('search_'.$key, 'alpha');
 67}
 68
 69if (empty($action) && empty($id) && empty($ref)) $action = 'view';
 70
 71// Load object
 72include DOL_DOCUMENT_ROOT.'/core/actions_fetchobject.inc.php'; // Must be include, not include_once.
 73
 74// Security check - Protection if external user
 75//if ($user->socid > 0) accessforbidden();
 76//if ($user->socid > 0) $socid = $user->socid;
 77//$result = restrictedArea($user, 'mymodule', $id);
 78
 79if (empty($conf->global->MAIN_USE_ADVANCED_PERMS))
 80{
 81	$permissiontoadd = $user->rights->stock->creer;
 82	$permissiontodelete = $user->rights->stock->supprimer;
 83}
 84else
 85{
 86	$permissiontoadd = $user->rights->stock->inventory_advance->write;
 87	$permissiontodelete = $user->rights->stock->inventory_advance->write;
 88}
 89
 90
 91/*
 92 * Actions
 93 */
 94
 95$parameters = array();
 96$reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
 97if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
 98
 99if (empty($reshook))
100{
101	$error = 0;
102
103	$backurlforlist = DOL_URL_ROOT.'/product/inventory/list.php';
104	$backtopage = DOL_URL_ROOT.'/product/inventory/inventory.php?id='.$object->id;
105
106	// Actions cancel, add, update, delete or clone
107	include DOL_DOCUMENT_ROOT.'/core/actions_addupdatedelete.inc.php';
108
109	// Actions when linking object each other
110	include DOL_DOCUMENT_ROOT.'/core/actions_dellink.inc.php';
111
112	// Actions when printing a doc from card
113	include DOL_DOCUMENT_ROOT.'/core/actions_printing.inc.php';
114
115	// Actions to send emails
116	/*$triggersendname = 'MYOBJECT_SENTBYMAIL';
117	$autocopy='MAIN_MAIL_AUTOCOPY_MYOBJECT_TO';
118	$trackid='myobject'.$object->id;
119	include DOL_DOCUMENT_ROOT.'/core/actions_sendmails.inc.php';*/
120}
121
122
123
124
125/*
126 * View
127 */
128
129$form = new Form($db);
130
131llxHeader('', $langs->trans('Inventory'), '');
132
133// Example : Adding jquery code
134print '<script type="text/javascript" language="javascript">
135jQuery(document).ready(function() {
136	function init_myfunc()
137	{
138		jQuery("#myid").removeAttr(\'disabled\');
139		jQuery("#myid").attr(\'disabled\',\'disabled\');
140	}
141	init_myfunc();
142	jQuery("#mybutton").click(function() {
143		init_myfunc();
144	});
145});
146</script>';
147
148
149// Part to show record
150if ($object->id > 0)
151{
152    $res = $object->fetch_optionals();
153
154    $head = inventoryPrepareHead($object);
155	dol_fiche_head($head, 'inventory', $langs->trans("Inventory"), -1, 'stock');
156
157	$formconfirm = '';
158
159	// Confirmation to delete
160	if ($action == 'delete') {
161	    $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('DeleteInventory'), $langs->trans('ConfirmDeleteOrder'), 'confirm_delete', '', 0, 1);
162	}
163	// Confirmation to delete line
164	if ($action == 'deleteline')
165	{
166		$formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id.'&lineid='.$lineid, $langs->trans('DeleteLine'), $langs->trans('ConfirmDeleteLine'), 'confirm_deleteline', '', 0, 1);
167	}
168
169	// Clone confirmation
170	if ($action == 'clone') {
171		// Create an array for form
172		$formquestion = array();
173		$formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('ToClone'), $langs->trans('ConfirmCloneMyObject', $object->ref), 'confirm_clone', $formquestion, 'yes', 1);
174	}
175
176	// Call Hook formConfirm
177	$parameters = array('formConfirm' => $formconfirm, 'lineid' => $lineid);
178	$reshook = $hookmanager->executeHooks('formConfirm', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
179	if (empty($reshook)) $formconfirm .= $hookmanager->resPrint;
180	elseif ($reshook > 0) $formconfirm = $hookmanager->resPrint;
181
182	// Print form confirm
183	print $formconfirm;
184
185
186	// Object card
187	// ------------------------------------------------------------
188	$linkback = '<a href="'.DOL_URL_ROOT.'/product/inventory/list.php'.(!empty($socid) ? '?socid='.$socid : '').'">'.$langs->trans("BackToList").'</a>';
189
190	$morehtmlref = '<div class="refidno">';
191	/*
192	// Ref bis
193	$morehtmlref.=$form->editfieldkey("RefBis", 'ref_client', $object->ref_client, $object, $user->rights->inventory->creer, 'string', '', 0, 1);
194	$morehtmlref.=$form->editfieldval("RefBis", 'ref_client', $object->ref_client, $object, $user->rights->inventory->creer, 'string', '', null, null, '', 1);
195	// Thirdparty
196	$morehtmlref.='<br>'.$langs->trans('ThirdParty') . ' : ' . $soc->getNomUrl(1);
197	// Project
198	if (! empty($conf->projet->enabled))
199	{
200	    $langs->load("projects");
201	    $morehtmlref.='<br>'.$langs->trans('Project') . ' ';
202	    if ($user->rights->inventory->creer)
203	    {
204	        if ($action != 'classify')
205	        {
206	            $morehtmlref.='<a class="editfielda" href="' . $_SERVER['PHP_SELF'] . '?action=classify&amp;id=' . $object->id . '">' . img_edit($langs->transnoentitiesnoconv('SetProject')) . '</a> : ';
207	            if ($action == 'classify') {
208	                //$morehtmlref.=$form->form_project($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->socid, $object->fk_project, 'projectid', 0, 0, 1, 1);
209	                $morehtmlref.='<form method="post" action="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'">';
210	                $morehtmlref.='<input type="hidden" name="action" value="classin">';
211	                $morehtmlref.='<input type="hidden" name="token" value="'.newToken().'">';
212	                $morehtmlref.=$formproject->select_projects($object->socid, $object->fk_project, 'projectid', $maxlength, 0, 1, 0, 1, 0, 0, '', 1);
213	                $morehtmlref.='<input type="submit" class="button valignmiddle" value="'.$langs->trans("Modify").'">';
214	                $morehtmlref.='</form>';
215	            } else {
216	                $morehtmlref.=$form->form_project($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->socid, $object->fk_project, 'none', 0, 0, 0, 1);
217	            }
218	        }
219	    } else {
220	        if (! empty($object->fk_project)) {
221	            $proj = new Project($db);
222	            $proj->fetch($object->fk_project);
223	            $morehtmlref.=$proj->getNomUrl();
224	        } else {
225	            $morehtmlref.='';
226	        }
227	    }
228	}
229	*/
230	$morehtmlref .= '</div>';
231
232
233	dol_banner_tab($object, 'ref', $linkback, 1, 'ref', 'ref', $morehtmlref);
234
235
236	print '<div class="fichecenter">';
237	print '<div class="fichehalfleft">';
238	print '<div class="underbanner clearboth"></div>';
239	print '<table class="border centpercent tableforfield">'."\n";
240
241	// Common attributes
242	include DOL_DOCUMENT_ROOT.'/core/tpl/commonfields_view.tpl.php';
243
244	// Other attributes. Fields from hook formObjectOptions and Extrafields.
245	include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_view.tpl.php';
246
247	print '</table>';
248	print '</div>';
249	print '</div>';
250
251	print '<div class="clearboth"></div>';
252
253	dol_fiche_end();
254
255
256	// Buttons for actions
257	if ($action == 'edit') {
258		print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
259		print '<input type="hidden" name="token" value="'.newToken().'">';
260		print '<input type="hidden" name="action" value="update">';
261		print '<input type="hidden" name="id" value="'.$object->id.'">';
262		if ($backtopage) print '<input type="hidden" name="backtopage" value="'.$backtopage.'">';
263
264		print '<div class="center">';
265		print '<span class="opacitymedium">'.$langs->trans("InventoryDesc").'</span><br>';
266		print '<input type="submit" class="button" name="save" value="'.$langs->trans("Save").'">';
267		print ' &nbsp; ';
268		print '<input type="submit" class="button" name="cancel" value="'.$langs->trans("Cancel").'">';
269		print '</div>';
270		print '<br>';
271	}
272	else {
273    	print '<div class="tabsAction">'."\n";
274    	$parameters = array();
275    	$reshook = $hookmanager->executeHooks('addMoreActionsButtons', $parameters, $object, $action); // Note that $action and $object may have been modified by hook
276    	if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
277
278    	if (empty($reshook))
279    	{
280    		if ($object->status == Inventory::STATUS_DRAFT)
281    		{
282    			if ($permissiontoadd)
283    			{
284    				print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=edit">'.$langs->trans("Edit").'</a>'."\n";
285    			}
286    			else
287    			{
288    				print '<a class="butActionRefused classfortooltip" href="#" title="'.dol_escape_htmltag($langs->trans("NotEnoughPermissions")).'">'.$langs->trans('Edit').'</a>'."\n";
289    			}
290    		}
291
292    		if ($object->status == Inventory::STATUS_DRAFT)
293    		{
294	        	if ($permissiontoadd)
295	    		{
296	    			print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=validate">'.$langs->trans("Validate").'</a>'."\n";
297	    		}
298	    		else
299	    		{
300	    			print '<a class="butActionRefused classfortooltip" href="#" title="'.dol_escape_htmltag($langs->trans("NotEnoughPermissions")).'">'.$langs->trans('Validate').'</a>'."\n";
301	    		}
302    		}
303
304    		/*if ($object->status == Inventory::STATUS_VALIDATED)
305    		{
306	    		if ($permissiontoadd)
307	    		{
308	    			print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=edit">'.$langs->trans("RecordVerb").'</a>'."\n";
309	    		}
310	    		else
311	    		{
312	    			print '<a class="butActionRefused classfortooltip" href="#" title="'.dol_escape_htmltag($langs->trans("NotEnoughPermissions")).'">'.$langs->trans('RecordVerb').'</a>'."\n";
313	    		}
314    		}*/
315    	}
316    	print '</div>'."\n";
317	}
318
319
320	print '<div class="fichecenter">';
321	//print '<div class="fichehalfleft">';
322	print '<div class="clearboth"></div>';
323
324	//print load_fiche_titre($langs->trans('Consumption'), '', '');
325
326	print '<div class="div-table-responsive-no-min">';
327	print '<table id="tablelines" class="noborder noshadow centpercent">';
328
329	print '<tr class="liste_titre">';
330	print '<td>'.$langs->trans("Warehouse").'</td>';
331	print '<td>'.$langs->trans("Product").'</td>';
332	if ($conf->productbatch->enabled) {
333		print '<td>';
334		print $langs->trans("Batch");
335		print '</td>';
336	}
337	print '<td class="right">'.$langs->trans("RecordedQty").'</td>';
338	print '<td class="right">'.$langs->trans("RealQty").'</td>';
339	print '<td>';
340	print '</td>';
341	print '</tr>';
342
343
344	$sql = 'SELECT ps.rowid, ps.fk_entrepot as fk_warehouse, ps.fk_product';
345	$sql .= ' FROM '.MAIN_DB_PREFIX.'product_stock as ps, '.MAIN_DB_PREFIX.'product as p, '.MAIN_DB_PREFIX.'entrepot as e';
346	$sql .= ' WHERE p.entity IN ('.getEntity('product').')';
347	$sql .= ' AND ps.fk_product = p.rowid AND ps.fk_entrepot = e.rowid';
348	if (empty($conf->global->STOCK_SUPPORTS_SERVICES)) $sql .= " AND p.fk_product_type = 0";
349	if ($object->fk_product > 0) $sql .= ' AND ps.fk_product = '.$object->fk_product;
350	if ($object->fk_warehouse > 0) $sql .= ' AND ps.fk_entrepot = '.$object->fk_warehouse;
351
352	$cacheOfProducts = array();
353	$cacheOfWarehouses = array();
354
355	$resql = $db->query($sql);
356	if ($resql)
357	{
358		$num = $db->num_rows($resql);
359
360		$i = 0;
361		$totalarray = array();
362		while ($i < $num)
363		{
364			$obj = $db->fetch_object($resql);
365
366			if (is_object($cacheOfWarehouses[$obj->fk_warehouse])) {
367				$warehouse_static = $cacheOfWarehouses[$obj->fk_warehouse];
368			} else {
369				$warehouse_static = new Entrepot($db);
370				$warehouse_static->fetch($obj->fk_warehouse);
371
372				$cacheOfWarehouses[$warehouse_static->id] = $warehouse_static;
373			}
374
375			if (is_object($cacheOfProducts[$obj->fk_product])) {
376				$product_static = $cacheOfProducts[$obj->fk_product];
377			} else {
378				$product_static = new Product($db);
379				$product_static->fetch($obj->fk_product);
380
381				$option = 'nobatch';
382				$option .= ',novirtual';
383				$product_static->load_stock($option); // Load stock_reel + stock_warehouse. This can also call load_virtual_stock()
384
385				$cacheOfProducts[$product_static->id] = $product_static;
386			}
387
388			print '<tr class="oddeven">';
389			print '<td>';
390			print $warehouse_static->getNomUrl(1);
391			print '</td>';
392			print '<td>';
393			print $product_static->getNomUrl(1);
394			print '</td>';
395
396			if ($conf->productbatch->enabled) {
397				print '<td>';
398				print '';
399				print '</td>';
400			}
401
402			print '<td>';
403			print '';
404			print '</td>';
405			print '<td>';
406			print '';
407			print '</td>';
408			print '<td>';
409			print '';
410			print '</td>';
411
412			print '</tr>';
413
414			$i++;
415		}
416	} else {
417		dol_print_error($db);
418	}
419
420	print '</table>';
421	print '</div>';
422
423	//print '</div>';
424	print '</div>';
425
426	if ($action == 'edit') {
427		print '</form>';
428	}
429}
430
431// End of page
432llxFooter();
433$db->close();