* Copyright (C) 2004-2011 Laurent Destailleur * Copyright (C) 2004 Eric Seigne * Copyright (C) 2005 Simon TOSSER * Copyright (C) 2005-2009 Regis Houssin * Copyright (C) 2013 Cédric Salvador * Copyright (C) 2013-2015 Juanjo Menent * Copyright (C) 2014-2015 Cédric Gross * Copyright (C) 2015 Marcos García * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ /** * \file htdocs/product/stock/product.php * \ingroup product stock * \brief Page to list detailed stock of a product */ require '../../main.inc.php'; require_once DOL_DOCUMENT_ROOT.'/product/stock/class/entrepot.class.php'; require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php'; require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php'; require_once DOL_DOCUMENT_ROOT.'/product/class/html.formproduct.class.php'; if (! empty($conf->productbatch->enabled)) require_once DOL_DOCUMENT_ROOT.'/product/class/productbatch.class.php'; $langs->load("products"); $langs->load("orders"); $langs->load("bills"); $langs->load("stocks"); $langs->load("sendings"); if (! empty($conf->productbatch->enabled)) $langs->load("productbatch"); $backtopage=GETPOST('backtopage'); $action=GETPOST("action"); $cancel=GETPOST('cancel'); $id=GETPOST('id', 'int'); $ref=GETPOST('ref', 'alpha'); $stocklimit = GETPOST('stocklimit'); $desiredstock = GETPOST('desiredstock'); $cancel = GETPOST('cancel'); $fieldid = isset($_GET["ref"])?'ref':'rowid'; $d_eatby=dol_mktime(12, 0, 0, $_POST['eatbymonth'], $_POST['eatbyday'], $_POST['eatbyyear']); $d_sellby=dol_mktime(12, 0, 0, $_POST['sellbymonth'], $_POST['sellbyday'], $_POST['sellbyyear']); $pdluoid=GETPOST('pdluoid','int'); // Security check if ($user->societe_id) $socid=$user->societe_id; $result=restrictedArea($user,'produit&stock',$id,'product&product','','',$fieldid); /* * Actions */ if ($cancel) $action=''; // Set stock limit if ($action == 'setstocklimit') { $product = new Product($db); $result=$product->fetch($id); $product->seuil_stock_alerte=$stocklimit; $result=$product->update($product->id,$user,0,'update'); if ($result < 0) setEventMessage($product->error, 'errors'); $action=''; } // Set desired stock if ($action == 'setdesiredstock') { $product = new Product($db); $result=$product->fetch($id); $product->desiredstock=$desiredstock; $result=$product->update($product->id,$user,0,'update'); if ($result < 0) setEventMessage($product->error, 'errors'); $action=''; } // Correct stock if ($action == "correct_stock" && ! $cancel) { if (! (GETPOST("id_entrepot") > 0)) { setEventMessage($langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv("Warehouse")), 'errors'); $error++; $action='correction'; } if (! GETPOST("nbpiece")) { setEventMessage($langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv("NumberOfUnit")), 'errors'); $error++; $action='correction'; } if (! empty($conf->productbatch->enabled)) { $product = new Product($db); $result=$product->fetch($id); if ($product->hasbatch() && (! GETPOST("sellby")) && (! GETPOST("eatby")) && (! GETPOST("batch_number"))) { setEventMessage($langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv("atleast1batchfield")), 'errors'); $error++; $action='correction'; } } if (! $error) { $priceunit=price2num(GETPOST("unitprice")); if (is_numeric(GETPOST("nbpiece")) && $id) { if (empty($product)) { $product = new Product($db); $result=$product->fetch($id); } if ($product->hasbatch()) { $result=$product->correct_stock_batch( $user, GETPOST("id_entrepot"), GETPOST("nbpiece"), GETPOST("mouvement"), GETPOST("label"), // label movement $priceunit, $d_eatby, $d_sellby, GETPOST('batch_number'), GETPOST('inventorycode') ); // We do not change value of stock for a correction } else { $result=$product->correct_stock( $user, GETPOST("id_entrepot"), GETPOST("nbpiece"), GETPOST("mouvement"), GETPOST("label"), $priceunit, GETPOST('inventorycode') ); // We do not change value of stock for a correction } if ($result > 0) { if ($backtopage) { header("Location: ".$backtopage); exit; } else { header("Location: ".$_SERVER["PHP_SELF"]."?id=".$product->id); exit; } } else { setEventMessage($product->error,'errors'); $action='correction'; } } } } // Transfer stock from a warehouse to another warehouse if ($action == "transfert_stock" && ! $cancel) { if (! (GETPOST("id_entrepot_source",'int') > 0) || ! (GETPOST("id_entrepot_destination",'int') > 0)) { setEventMessage($langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv("Warehouse")), 'errors'); $error++; $action='transfert'; } if (! GETPOST("nbpiece",'int')) { setEventMessage($langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv("NumberOfUnit")), 'errors'); $error++; $action='transfert'; } if (GETPOST("id_entrepot_source",'int') == GETPOST("id_entrepot_destination",'int')) { setEventMessage($langs->trans("ErrorSrcAndTargetWarehouseMustDiffers"), 'errors'); $error++; $action='transfert'; } if (! $error) { if ($id) { $product = new Product($db); $result=$product->fetch($id); $db->begin(); $product->load_stock(); // Load array product->stock_warehouse // Define value of products moved $pricesrc=0; //if (isset($product->stock_warehouse[GETPOST("id_entrepot_source")]->pmp)) $pricesrc=$product->stock_warehouse[GETPOST("id_entrepot_source")]->pmp; if (isset($product->pmp)) $pricesrc=$product->pmp; $pricedest=$pricesrc; if ($product->hasbatch()) { $pdluo = new Productbatch($db); if ($pdluoid > 0) { $result=$pdluo->fetch($pdluoid); if ($result) { $srcwarehouseid=$pdluo->warehouseid; $batch=$pdluo->batch; $eatby=$pdluo->eatby; $sellby=$pdluo->sellby; } else { setEventMessages($pdluo->error, $pdluo->errors, 'errors'); $error++; } } else { $srcwarehouseid=GETPOST('id_entrepot_source','int'); $batch=GETPOST('batch_number'); $eatby=$d_eatby; $sellby=$d_sellby; } if (! $error) { // Remove stock $result1=$product->correct_stock_batch( $user, $srcwarehouseid, GETPOST("nbpiece",'int'), 1, GETPOST("label",'san_alpha'), $pricesrc, $eatby,$sellby,$batch, GETPOST('inventorycode') ); // Add stock $result2=$product->correct_stock_batch( $user, GETPOST("id_entrepot_destination",'int'), GETPOST("nbpiece",'int'), 0, GETPOST("label",'san_alpha'), $pricedest, $eatby,$sellby,$batch, GETPOST('inventorycode') ); } } else { // Remove stock $result1=$product->correct_stock( $user, GETPOST("id_entrepot_source"), GETPOST("nbpiece"), 1, GETPOST("label"), $pricesrc, GETPOST('inventorycode') ); // Add stock $result2=$product->correct_stock( $user, GETPOST("id_entrepot_destination"), GETPOST("nbpiece"), 0, GETPOST("label"), $pricedest, GETPOST('inventorycode') ); } if (! $error && $result1 >= 0 && $result2 >= 0) { $db->commit(); if ($backtopage) { header("Location: ".$backtopage); exit; } else { header("Location: product.php?id=".$product->id); exit; } } else { setEventMessages($product->error, $product->errors, 'errors'); $db->rollback(); $action='transfert'; } } } } // Update batch information if ($action == 'updateline' && GETPOST('save') == $langs->trans('Save')) { $pdluo = new Productbatch($db); $result=$pdluo->fetch(GETPOST('pdluoid','int')); if ($result>0) { if ($pdluo->id) { if ((! GETPOST("sellby")) && (! GETPOST("eatby")) && (! GETPOST("batch_number"))) { setEventMessage($langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv("atleast1batchfield")), 'errors'); } else { $d_eatby=dol_mktime(12, 0, 0, $_POST['eatbymonth'], $_POST['eatbyday'], $_POST['eatbyyear']); $d_sellby=dol_mktime(12, 0, 0, $_POST['sellbymonth'], $_POST['sellbyday'], $_POST['sellbyyear']); $pdluo->batch=GETPOST("batch_number",'san_alpha'); $pdluo->eatby=$d_eatby; $pdluo->sellby=$d_sellby; $result=$pdluo->update($user); if ($result<0) { setEventMessages($pdluo->error,$pdluo->errors, 'errors'); } } } else { setEventMessages($langs->trans('BatchInformationNotfound'),null, 'errors'); } } else { setEventMessages($pdluo->error,null, 'errors'); } header("Location: product.php?id=".$id); exit; } /* * View */ $formproduct=new FormProduct($db); if ($id > 0 || $ref) { $product = new Product($db); $result = $product->fetch($id,$ref); $product->load_stock(); $help_url='EN:Module_Stocks_En|FR:Module_Stock|ES:Módulo_Stocks'; llxHeader("",$langs->trans("CardProduct".$product->type),$help_url); if ($result > 0) { $head=product_prepare_head($product); $titre=$langs->trans("CardProduct".$product->type); $picto=($product->type==Product::TYPE_SERVICE?'service':'product'); dol_fiche_head($head, 'stock', $titre, 0, $picto); dol_htmloutput_events(); $form = new Form($db); print ''; // Ref print ''; print ''; print ''; // Label print ''; print ''; // Status (to sell) print ''; // Status (to buy) print ''; if ($conf->productbatch->enabled) { print ''; } // PMP print ''; print ''; print ''; // Minimum Price print ''; print ''; $object = $product; if (empty($conf->global->PRODUIT_MULTIPRICES)) { // Price print ''; // Price minimum print ''; } else { // Price print ''; // Price minimum print ''; } // Stock alert threshold print ''; // Desired stock print ''; // Real stock $product->load_stock(); $text_stock_options = ''; $text_stock_options.= (! empty($conf->global->STOCK_CALCULATE_ON_SHIPMENT)?$langs->trans("DeStockOnShipment").'
':''); $text_stock_options.= (! empty($conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER)?$langs->trans("DeStockOnValidateOrder").'
':''); $text_stock_options.= (! empty($conf->global->STOCK_CALCULATE_ON_BILL)?$langs->trans("DeStockOnBill").'
':''); $text_stock_options.= (! empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_BILL)?$langs->trans("ReStockOnBill").'
':''); $text_stock_options.= (! empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_VALIDATE_ORDER)?$langs->trans("ReStockOnValidateOrder").'
':''); $text_stock_options.= (! empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_DISPATCH_ORDER)?$langs->trans("ReStockOnDispatchOrder").'
':''); print ''; print ''; print ''; // Calculating a theorical value print ''; print "'; print ''; print ''; print ''; // Last movement $sql = "SELECT max(m.datem) as datem"; $sql.= " FROM ".MAIN_DB_PREFIX."stock_mouvement as m"; $sql.= " WHERE m.fk_product = '".$product->id."'"; $resqlbis = $db->query($sql); if ($resqlbis) { $obj = $db->fetch_object($resqlbis); $lastmovementdate=$db->jdate($obj->datem); } else { dol_print_error($db); } print '"; print "
'.$langs->trans("Ref").''; print $form->showrefnav($product,'ref','',1,'ref'); print '
'.$langs->trans("Label").''.$product->label.'
'.$langs->trans("Status").' ('.$langs->trans("Sell").')'; if (! empty($conf->use_javascript_ajax) && $user->rights->produit->creer && ! empty($conf->global->MAIN_DIRECT_STATUS_UPDATE)) { print ajax_object_onoff($product, 'status', 'tosell', 'ProductStatusOnSell', 'ProductStatusNotOnSell'); } else { print $product->getLibStatut(2,0); } print '
'.$langs->trans("Status").' ('.$langs->trans("Buy").')'; if (! empty($conf->use_javascript_ajax) && $user->rights->produit->creer && ! empty($conf->global->MAIN_DIRECT_STATUS_UPDATE)) { print ajax_object_onoff($product, 'status_buy', 'tobuy', 'ProductStatusOnBuy', 'ProductStatusNotOnBuy'); } else { print $product->getLibStatut(2,1); } print '
'.$langs->trans("ManageLotSerial").''; print $product->getLibStatut(0,2); print '
'.$langs->trans("AverageUnitPricePMP").''; if ($product->pmp > 0) print price($product->pmp).' '.$langs->trans("HT"); print '
'.$langs->trans("BuyingPriceMin").''; $product_fourn = new ProductFournisseur($db); if ($product_fourn->find_min_price_product_fournisseur($product->id) > 0) { if ($product_fourn->product_fourn_price_id > 0) print $product_fourn->display_price_product_fournisseur(); else print $langs->trans("NotDefined"); } print '
' . $langs->trans("SellingPrice") . ''; if ($object->price_base_type == 'TTC') { print price($object->price_ttc) . ' ' . $langs->trans($object->price_base_type); } else { print price($object->price) . ' ' . $langs->trans($object->price_base_type); } print '
' . $langs->trans("MinPrice") . ''; if ($object->price_base_type == 'TTC') { print price($object->price_min_ttc) . ' ' . $langs->trans($object->price_base_type); } else { print price($object->price_min) . ' ' . $langs->trans($object->price_base_type); } print '
' . $langs->trans("SellingPrice") . ''; print $langs->trans("Variable"); print '
' . $langs->trans("MinPrice") . ''; print $langs->trans("Variable"); print '
'.$form->editfieldkey("StockLimit",'stocklimit',$product->seuil_stock_alerte,$product,$user->rights->produit->creer).''; print $form->editfieldval("StockLimit",'stocklimit',$product->seuil_stock_alerte,$product,$user->rights->produit->creer); print '
'.$form->editfieldkey("DesiredStock",'desiredstock',$product->desiredstock,$product,$user->rights->produit->creer).''; print $form->editfieldval("DesiredStock",'desiredstock',$product->desiredstock,$product,$user->rights->produit->creer); print '
'; print $form->textwithtooltip($langs->trans("PhysicalStock"), $text_stock_options, 2, 1, img_picto('', 'info'), '', 2); print ''.$product->stock_reel; if ($product->seuil_stock_alerte && ($product->stock_reel < $product->seuil_stock_alerte)) print ' '.img_warning($langs->trans("StockLowerThanLimit")); print '
'.$langs->trans("VirtualStock").'".(empty($product->stock_theorique)?0:$product->stock_theorique); if ($product->stock_theorique < $product->seuil_stock_alerte) { print ' '.img_warning($langs->trans("StockLowerThanLimit")); } print '
'; print $langs->trans("StockDiffPhysicTeoric"); print ''; $found=0; // Number of customer orders running if (! empty($conf->commande->enabled)) { if ($found) print '
'; else $found=1; print $langs->trans("ProductQtyInCustomersOrdersRunning").': '.$product->stats_commande['qty']; $result=$product->load_stats_commande(0,'0'); if ($result < 0) dol_print_error($db,$product->error); print ' ('.$langs->trans("ProductQtyInDraft").': '.$product->stats_commande['qty'].')'; } // Number of product from customer order already sent (partial shipping) if (! empty($conf->expedition->enabled)) { if ($found) print '
'; else $found=1; $result=$product->load_stats_sending(0,'2'); print $langs->trans("ProductQtyInShipmentAlreadySent").': '.$product->stats_expedition['qty']; } // Number of supplier order running if (! empty($conf->fournisseur->enabled)) { if ($found) print '
'; else $found=1; $result=$product->load_stats_commande_fournisseur(0,'3,4'); print $langs->trans("ProductQtyInSuppliersOrdersRunning").': '.$product->stats_commande_fournisseur['qty']; $result=$product->load_stats_commande_fournisseur(0,'0,1,2'); if ($result < 0) dol_print_error($db,$product->error); print ' ('.$langs->trans("ProductQtyInDraftOrWaitingApproved").': '.$product->stats_commande_fournisseur['qty'].')'; } // Number of product from supplier order already received (partial receipt) if (! empty($conf->fournisseur->enabled)) { if ($found) print '
'; else $found=1; print $langs->trans("ProductQtyInSuppliersShipmentAlreadyRecevied").': '.$product->stats_reception['qty']; } print '
'.$langs->trans("LastMovement").''; if ($lastmovementdate) { print dol_print_date($lastmovementdate,'dayhour').' '; print '('.$langs->trans("FullList").')'; } else { print ''.$langs->trans("None").''; } print "
"; } print ''; /* * Correct stock */ if ($action == "correction") { include DOL_DOCUMENT_ROOT.'/product/stock/tpl/stockcorrection.tpl.php'; print '
'; } /* * Transfer of units */ if ($action == "transfert") { include DOL_DOCUMENT_ROOT.'/product/stock/tpl/stocktransfer.tpl.php'; print '
'; } /* * Set initial stock */ /* if ($_GET["action"] == "definir") { print_titre($langs->trans("SetStock")); print "
id\" method=\"post\">\n"; print ''; print ''; print ''; print ''; print ''; print '
'.$langs->trans("Warehouse").''; print $formproduct->selectWarehouses('','id_entrepot','',1); print ''.$langs->trans("NumberOfUnit").'
 '; print '
'; print '
'; } */ } else { dol_print_error(); } /* ************************************************************************** */ /* */ /* Barre d'action */ /* */ /* ************************************************************************** */ if (empty($action) && $product->id) { print "
\n"; if ($user->rights->stock->mouvement->creer) { print ''.$langs->trans("StockCorrection").''; } //if (($user->rights->stock->mouvement->creer) && ! $product->hasbatch()) if (($user->rights->stock->mouvement->creer)) { print ''.$langs->trans("StockMovement").''; } print '
'; } /* * Stock detail (by warehouse). Do not go down into batch. */ print '
'; print ''; print ''; print ''; print ''; print ''; print ''; print ''; if ((! empty($conf->productbatch->enabled)) && $product->hasbatch()) { print ''; print ''; print ''; print ''; print ''; print ''; } $sql = "SELECT e.rowid, e.label, e.lieu, ps.reel, ps.pmp, ps.rowid as product_stock_id"; $sql.= " FROM ".MAIN_DB_PREFIX."entrepot as e,"; $sql.= " ".MAIN_DB_PREFIX."product_stock as ps"; $sql.= " WHERE ps.reel != 0"; $sql.= " AND ps.fk_entrepot = e.rowid"; $sql.= " AND e.entity IN (".getEntity('stock', 1).")"; $sql.= " AND ps.fk_product = ".$product->id; $sql.= " ORDER BY e.label"; $entrepotstatic=new Entrepot($db); $total=0; $totalvalue=$totalvaluesell=0; $resql=$db->query($sql); if ($resql) { $num = $db->num_rows($resql); $total=$totalwithpmp; $i=0; $var=false; while ($i < $num) { $obj = $db->fetch_object($resql); $entrepotstatic->id=$obj->rowid; $entrepotstatic->libelle=$obj->label; $entrepotstatic->lieu=$obj->lieu; print ''; print ''; print ''; // PMP print ''; // Value purchase print ''; // Sell price print ''; // Value sell print ''; else print $langs->trans("Variable"); print ''; ; $total += $obj->reel; if (price2num($product->pmp)) $totalwithpmp += $obj->reel; $totalvalue = $totalvalue + ($product->pmp*$obj->reel); $totalvaluesell = $totalvaluesell + ($product->price*$obj->reel); //Batch Detail if ((! empty($conf->productbatch->enabled)) && $product->hasbatch()) { $details=Productbatch::findAll($db,$obj->product_stock_id); if ($details<0) dol_print_error($db); foreach ($details as $pdluo) { if ( $action == 'editline' && GETPOST('lineid','int')==$pdluo->id ) { //Current line edit print "\n".''; print ''; print ''; print ''; print ''; print ''; } } } $i++; $var=!$var; } } else dol_print_error($db); print ''; print ''; print ''; // Value purchase print ''; print ''; // Value to sell print ''; print ""; print "
'.$langs->trans("Warehouse").''.$langs->trans("NumberOfUnit").''.$langs->trans("AverageUnitPricePMPShort").''.$langs->trans("EstimatedStockValueShort").''.$langs->trans("SellPriceMin").''.$langs->trans("EstimatedStockValueSellShort").'
'.$langs->trans("batch_number").''.$langs->trans("l_eatby").''.$langs->trans("l_sellby").'
'.$entrepotstatic->getNomUrl(1).''.$obj->reel.($obj->reel<0?' '.img_warning():'').''.(price2num($product->pmp)?price2num($product->pmp,'MU'):'').''.(price2num($product->pmp)?price(price2num($product->pmp*$obj->reel,'MT')):'').''; if (empty($conf->global->PRODUIT_MULTI_PRICES)) print price(price2num($product->price,'MU'),1); else print $langs->trans("Variable"); print ''; if (empty($conf->global->PRODUIT_MULTI_PRICES)) print price(price2num($product->price*$obj->reel,'MT'),1).'
'; print '
'; print ''; print ''; print ''; print ''; print ''; print '
'; $form->select_date($pdluo->eatby,'eatby','','',1,'',1,0,1); print ''; $form->select_date($pdluo->sellby,'sellby','','',1,'',1,0,1); print ''.$pdluo->qty.($pdluo->qty<0?' '.img_warning():'').''; print '
'; } else { print "\n".'
'; print img_picto($langs->trans("Tranfer"),'uparrow','class="hideonsmartphone"').' '; print 'id.'">'.$langs->trans("StockMovement").''; // Disabled, because edition of stock content must use the "Correct stock menu". // Do not use this, or data will be wrong (bad tracking of movement label, inventory code, ... //print 'id.'#'.$pdluo->id.'">'; //print img_edit().''.$pdluo->batch.''. dol_print_date($pdluo->eatby,'day') .''. dol_print_date($pdluo->sellby,'day') .''.$pdluo->qty.($pdluo->qty<0?' '.img_warning():'').'
'.$langs->trans("Total").':'.$total.''; print ($totalwithpmp?price(price2num($totalvalue/$totalwithpmp,'MU')):' '); // This value may have rounding errors print ''; print $totalvalue?price(price2num($totalvalue,'MT'),1):' '; print ''; if (empty($conf->global->PRODUIT_MULTI_PRICES)) print ($total?price($totalvaluesell/$total,1):' '); else print $langs->trans("Variable"); print ''; if (empty($conf->global->PRODUIT_MULTI_PRICES)) print price(price2num($totalvaluesell,'MT'),1); else print $langs->trans("Variable"); print '
"; llxFooter(); $db->close();