diff --git a/htdocs/core/class/coreobject.class.php b/htdocs/core/class/coreobject.class.php index b7e4b2770e3..f5e5b0e79a1 100644 --- a/htdocs/core/class/coreobject.class.php +++ b/htdocs/core/class/coreobject.class.php @@ -78,14 +78,20 @@ class CoreObject extends CommonObject { } + private function checkFieldType($field, $type) { + + if( isset($this->__fields[$field]) && method_exists($this, 'is_'.$type)) { + return $this->{'is_'.$type}($this->__fields[$field]); + } + else return false; + + } private function is_date(Array &$info){ - if(is_array($info)) { - if(isset($info['type']) && $info['type']=='date') return true; - else return false; - } + if(isset($info['type']) && $info['type']=='date') return true; else return false; + } private function is_array($info) { @@ -239,7 +245,25 @@ class CoreObject extends CommonObject { } } + public function addChild($tabName, $id='', $key='id', $try_to_load = false) { + if(!empty($id)) { + foreach($this->{$tabName} as $k=>&$object) { + if($object->{$key} === $id) return $k; + } + } + + $k = count($this->{$tabName}); + + $className = ucfirst($tabName); + $this->{$tabName}[$k] = new $className($this->db); + if($id>0 && $key==='id' && $try_to_load) { + $this->{$tabName}[$k]->fetch($id); + } + + + return $k; + } public function fetchChild() { if($this->withChild && !empty($this->childtables) && !empty($this->fk_element)) { @@ -272,13 +296,13 @@ class CoreObject extends CommonObject { } - public function update() { - + public function update(User &$user) { + if(empty($this->id )) return $this->create($user); } - public function create() { - + public function create(User &$user) { + if($this->id>0) return $this->update($user); } @@ -287,10 +311,46 @@ class CoreObject extends CommonObject { if(empty($this->{$field})) return ''; elseif($this->{$field}<=strtotime('1000-01-01 00:00:00')) return ''; else { + return dol_print_date($this->{$field}, $format); } } + public function set_date($field,$date){ + + if(empty($date)) { + $this->{$field} = 0; + } + else { + require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php'; + + $this->{$field} = dol_stringtotime($date); + } + + return $this->{$field}; + } + + public function set_values(&$Tab) { + foreach ($Tab as $key=>$value) { + + if($this->checkFieldType($key,'date')) { + $this->set_date($key, $value); + } + else if( $this->checkFieldType($key,'array')){ + $this->{$key} = $value; + } + else if( $this->checkFieldType($key,'float') ) { + $this->{$key} = (double)price2num($value); + } + else if( $this->checkFieldType($key,'int') ) { + $this->{$key} = (int)price2num($value); + } + else { + $this->{$key} = @stripslashes($value); + } + + } + } } diff --git a/htdocs/inventory/class/inventory.class.php b/htdocs/inventory/class/inventory.class.php index 2a485d24a0a..f2ff6484c64 100644 --- a/htdocs/inventory/class/inventory.class.php +++ b/htdocs/inventory/class/inventory.class.php @@ -147,15 +147,16 @@ class Inventory extends CoreObject } - function update() + function update(User &$user) { + //si on valide l'inventaire on sauvegarde le stock à cette instant if ($this->status) { $this->regulate(); } - parent::update(); + parent::update($user); } function set_values($Tab) @@ -209,7 +210,7 @@ class Inventory extends CoreObject $det->load_product(); $date = $this->get_date('date_inventory', 'Y-m-d'); - if(empty($date))$date = $this->get_date('date_cre', 'Y-m-d'); + if(empty($date))$date = $this->get_date('datec', 'Y-m-d'); $det->setStockDate( $date , $fk_entrepot); } @@ -416,9 +417,9 @@ class Inventorydet extends CoreObject return true; } - function setStockDate(&$PDOdb, $date, $fk_warehouse) { + function setStockDate($date, $fk_warehouse) { - list($pmp,$stock) = $this->getPmpStockFromDate($PDOdb, $date, $fk_warehouse); + list($pmp,$stock) = $this->getPmpStockFromDate($date, $fk_warehouse); $this->qty_stock = $stock; $this->pmp = $pmp; @@ -432,18 +433,17 @@ class Inventorydet extends CoreObject AND datem<='".$date." 23:59:59' ORDER BY datem DESC LIMIT 1"; - $res = $db->query($sql); + $res = $this->db->query($sql); - if($obj = $db->fetch_object($res)) { + if($res && $obj = $this->db->fetch_object($res)) { $last_pa = $obj->price; } $this->pa = $last_pa; - /* var_dump($fk_warehouse,$this->product->stock_warehouse,$this->pmp, $this->pa, $this->qty_stock); - exit;*/ + } - function getPmpStockFromDate(&$PDOdb, $date, $fk_warehouse){ + function getPmpStockFromDate( $date, $fk_warehouse){ $res = $this->product->load_stock(); @@ -459,7 +459,7 @@ class Inventorydet extends CoreObject } - //On récupère tous les mouvements de stocks du produit entre aujourd'hui et la date de l'inventaire + //All Stock mouvement between now and inventory date $sql = "SELECT value, price FROM ".MAIN_DB_PREFIX."stock_mouvement WHERE fk_product = ".$this->product->id." @@ -467,31 +467,25 @@ class Inventorydet extends CoreObject AND datem > '".date('Y-m-d 23:59:59',strtotime($date))."' ORDER BY datem DESC"; - //echo $sql.'
'; - $db->query($sql); - $TMouvementStock = $PDOdb->Get_All(); + + $res = $this->db->query($sql); + $laststock = $stock; $lastpmp = $pmp; - //Pour chacun des mouvements on recalcule le PMP et le stock physique - foreach($TMouvementStock as $mouvement){ - - //150 - //if($this->product->id==394) echo 'laststock = '.$stock.'
'; - - //9.33 - //if($this->product->id==394) echo 'lastpmp = '.$pmp.'
'; - $price = ($mouvement->price>0 && $mouvement->value>0) ? $mouvement->price : $lastpmp; + + if($res) { + while($mouvement = $this->db->fetch_object($res)) { + $price = ($mouvement->price>0 && $mouvement->value>0) ? $mouvement->price : $lastpmp; - $stock_value = $laststock * $lastpmp; + $stock_value = $laststock * $lastpmp; + + $laststock -= $mouvement->value; + + $last_stock_value = $stock_value - ($mouvement->value * $price); + + $lastpmp = ($laststock != 0) ? $last_stock_value / $laststock : $lastpmp; + } - $laststock -= $mouvement->value; - - $last_stock_value = $stock_value - ($mouvement->value * $price); - - $lastpmp = ($laststock != 0) ? $last_stock_value / $laststock : $lastpmp; - - - } return array($lastpmp,$laststock); diff --git a/htdocs/inventory/inventory.php b/htdocs/inventory/inventory.php index d46f05059b4..4e70ee1b4cc 100644 --- a/htdocs/inventory/inventory.php +++ b/htdocs/inventory/inventory.php @@ -66,18 +66,17 @@ function _action() $inventory = new Inventory($db); - _fiche_warehouse($user, $db, $conf, $langs, $inventory); + _card_warehouse( $inventory); break; case 'confirmCreate': if (!$user->rights->inventory->create) accessforbidden(); - - + $inventory = new Inventory($db); - $inventory->set_values($_REQUEST); + $inventory->set_values($_POST); - $fk_inventory = $inventory->save(); + $fk_inventory = $inventory->create($user); $fk_category = (int)GETPOST('fk_category'); $fk_supplier = (int)GETPOST('fk_supplier'); $fk_warehouse = (int)GETPOST('fk_warehouse'); @@ -86,7 +85,7 @@ function _action() $e = new Entrepot($db); $e->fetch($fk_warehouse); $TChildWarehouses = array($fk_warehouse); - if(method_exists($e, 'get_children_warehouses')) $e->get_children_warehouses($fk_warehouse, $TChildWarehouses); + $e->get_children_warehouses($fk_warehouse, $TChildWarehouses); $sql = 'SELECT ps.fk_product, ps.fk_entrepot FROM '.MAIN_DB_PREFIX.'product_stock ps @@ -103,27 +102,27 @@ function _action() ORDER BY p.ref ASC,p.label ASC'; - $Tab = $PDOdb->ExecuteAsArray($sql); - - foreach($Tab as &$row) { - - $inventory->add_product($PDOdb, $row->fk_product, $row->fk_entrepot); + $res = $db->query($sql); + if($res) { + while($obj = $db->fetch_object($res)){ + + $inventory->add_product($obj->fk_product, $obj->fk_entrepot); + } } - $inventory->save($PDOdb); + $inventory->update($user); + + header('Location: '.dol_buildpath('inventory/inventory.php?id='.$inventory->id.'&action=edit', 1)); + break; - header('Location: '.dol_buildpath('inventory/inventory.php?id='.$inventory->getId().'&action=edit', 1)); - case 'edit': if (!$user->rights->inventory->write) accessforbidden(); - $id = __get('id', 0, 'int'); - $inventory = new Inventory($db); - $inventory->load($PDOdb, $id); + $inventory->fetch(GETPOST('id')); - _fiche($PDOdb, $user, $db, $conf, $langs, $inventory, __get('action', 'edit', 'string')); + _card($inventory, GETPOST('action')); break; @@ -389,29 +388,29 @@ function _list() llxFooter(''); } -function _fiche_warehouse(&$PDOdb, &$user, &$db, &$conf, $langs, $inventory) +function _card_warehouse(&$inventory) { + global $langs,$conf,$db, $user, $form; + dol_include_once('/categories/class/categorie.class.php'); llxHeader('',$langs->trans('inventorySelectWarehouse'),'',''); print dol_get_fiche_head(inventoryPrepareHead($inventory)); - $form=new TFormCore('inventory.php', 'confirmCreate'); - print $form->hidden('action', 'confirmCreate'); - $form->Set_typeaff('edit'); + echo '
'; + echo ''; $formproduct = new FormProduct($db); - $formDoli = new Form($db); ?> - + - + @@ -421,11 +420,11 @@ function _fiche_warehouse(&$PDOdb, &$user, &$db, &$conf, $langs, $inventory) - + - + @@ -439,7 +438,8 @@ function _fiche_warehouse(&$PDOdb, &$user, &$db, &$conf, $langs, $inventory) print ''; print ''; - print $form->end_form(); + echo ''; + llxFooter(''); } @@ -455,7 +455,7 @@ function _card(&$inventory, $mode='edit') print dol_get_fiche_head(inventoryPrepareHead($inventory, $langs->trans('inventoryOfWarehouse', $warehouse->libelle), '&action='.$mode)); $lines = array(); - _card_line($inventory, $lines, $form); + _card_line($inventory, $lines, $mode); print ''.$langs->trans('inventoryOnDate')." ".$inventory->get_date('date_inventory').'

'; @@ -488,7 +488,7 @@ function _card(&$inventory, $mode='edit') } -function _card_line(&$inventory, &$lines, &$form) +function _card_line(&$inventory, &$lines, $mode) { global $db; $inventory->amount_actual = 0; @@ -512,21 +512,23 @@ function _card_line(&$inventory, &$lines, &$form) if(!empty($TCacheEntrepot[$Inventorydet->fk_warehouse])) $e = $TCacheEntrepot[$Inventorydet->fk_warehouse]; elseif($e->fetch($Inventorydet->fk_warehouse) > 0) $TCacheEntrepot[$e->id] = $e; + $qty = (float)GETPOST('qty_to_add')[$k]; + $lines[]=array( 'produit' => $product->getNomUrl(1).' - '.$product->label ,'entrepot'=>$e->getNomUrl(1) ,'barcode' => $product->barcode - /*,'qty' => $form->texte('', 'qty_to_add['.$k.']', (isset($_REQUEST['qty_to_add'][$k]) ? $_REQUEST['qty_to_add'][$k] : 0), 8, 0, "style='text-align:center;'") - .($form->type_aff!='view' ? ''.img_picto('Ajouter', 'plus16@inventory').'' : '') + ,'qty' =>($mode == 'edit' ? '' : $qty ) + .($mode =='edit' ? ''.img_picto($langs->trans('Add'), 'plus16@inventory').'' : '') ,'qty_view' => $Inventorydet->qty_view ? $Inventorydet->qty_view : 0 ,'qty_stock' => $stock ,'qty_regulated' => $Inventorydet->qty_regulated ? $Inventorydet->qty_regulated : 0 - ,'action' => $user->rights->inventory->write ? ''.img_picto($langs->trans('inventoryDeleteLine'), 'delete').'' : '' + ,'action' => ($user->rights->inventory->write ? ''.img_picto($langs->trans('inventoryDeleteLine'), 'delete').'' : '') ,'pmp_stock'=>round($pmp_actual,2) ,'pmp_actual'=> round($pmp * $Inventorydet->qty_view,2) - ,'pmp_new'=>(!empty($user->rights->inventory->changePMP) ? $form->texte('', 'new_pmp['.$k.']',$Inventorydet->new_pmp, 8, 0, "style='text-align:right;'") - .($form->type_aff!='view' ? ''.img_picto($langs->trans('Save'), 'bt-save.png@inventory').'' : '') : '' ) - */,'pa_stock'=>round($last_pa * $stock,2) + ,'pmp_new'=>(!empty($user->rights->inventory->changePMP) ? '' + .($mode =='edit' ? ''.img_picto($langs->trans('Save'), 'bt-save.png@inventory').'' : '') : '') + ,'pa_stock'=>round($last_pa * $stock,2) ,'pa_actual'=>round($last_pa * $Inventorydet->qty_view,2) ,'current_pa_stock'=>round($current_pa * $stock,2) ,'current_pa_actual'=>round($current_pa * $Inventorydet->qty_view,2) @@ -610,80 +612,9 @@ function exportCSV(&$inventory) { exit; } -function generateODT(&$PDOdb, &$db, &$conf, &$langs, &$inventory) -{ - $TBS=new TTemplateTBS(); - - $InventoryPrint = array(); // Tableau envoyé à la fonction render contenant les informations concernant l'inventaire - - foreach($inventory->Inventorydet as $k => $v) - { - $prod = new Product($db); - $prod->fetch($v->fk_product); - //$prod->fetch_optionals($prod->id); - - $InventoryPrint[] = array( - 'product' => $prod->ref.' - '.$prod->label - , 'qty_view' => $v->qty_view - ); - } - - $warehouse = new Entrepot($db); - $warehouse->fetch($inventory->fk_warehouse); - - $dirName = 'INVENTORY'.$inventory->getId().'('.date("d_m_Y").')'; - $dir = DOL_DATA_ROOT.'/inventory/'.$dirName.'/'; - - @mkdir($dir, 0777, true); - - $template = "templateINVENTORY.odt"; - //$template = "templateOF.doc"; - - $file_gen = $TBS->render(dol_buildpath('inventory/exempleTemplate/'.$template) - ,array( - 'InventoryPrint'=>$InventoryPrint - ) - ,array( - 'date_cre'=>$inventory->get_date('date_cre', 'd/m/Y') - ,'date_maj'=>$inventory->get_date('date_maj', 'd/m/Y H:i') - ,'date_inv'=>$inventory->get_date('date_inventory', 'd/m/Y') - ,'numero'=>empty($inventory->title) ? 'Inventaire n°'.$inventory->getId() : $inventory->title - ,'warehouse'=>$warehouse->libelle - ,'status'=>($inventory->status ? $langs->transnoentitiesnoconv('inventoryValidate') : $langs->transnoentitiesnoconv('inventoryDraft')) - ,'logo'=>DOL_DATA_ROOT."/mycompany/logos/".MAIN_INFO_SOCIETE_LOGO - ) - ,array() - ,array( - 'outFile'=>$dir.$inventory->getId().".odt" - ,"convertToPDF"=>(!empty($conf->global->INVENTORY_GEN_PDF) ? true : false) - ,'charset'=>OPENTBS_ALREADY_UTF8 - - ) - - ); - - header("Location: ".DOL_URL_ROOT."/document.php?modulepart=inventory&entity=".$conf->entity."&file=".$dirName."/".$inventory->getId(). (!empty($conf->global->INVENTORY_GEN_PDF) ? '.pdf' : '.odt') ); - - /* - $size = filesize("./" . basename($file_gen)); - header("Content-Type: application/force-download; name=\"" . basename($file_gen) . "\""); - header("Content-Transfer-Encoding: binary"); - header("Content-Length: $size"); - header("Content-Disposition: attachment; filename=\"" . basename($file_gen) . "\""); - header("Expires: 0"); - header("Cache-Control: no-cache, must-revalidate"); - header("Pragma: no-cache"); - - readfile($file_gen); - */ - - //header("Location: ".DOL_URL_ROOT."/document.php?modulepart=asset&entity=1&file=".$dirName."/".$assetOf->numero.".doc"); - -} function _footerList($view,$total_pmp,$total_pmp_actual,$total_pa,$total_pa_actual, $total_current_pa,$total_current_pa_actual) { global $conf,$user,$langs; - if ($view['can_validate'] == 1) { ?> diff --git a/htdocs/inventory/tpl/inventory.tpl.php b/htdocs/inventory/tpl/inventory.tpl.php index d7022245f5b..8a3bfd0f2a6 100644 --- a/htdocs/inventory/tpl/inventory.tpl.php +++ b/htdocs/inventory/tpl/inventory.tpl.php @@ -154,9 +154,9 @@ - - - @@ -168,18 +168,17 @@
- trans('Print') ?> trans('ExportCSV') ?> trans('Modify') ?> rights->inventory->changePMP)) { - echo ''.$langs->trans('ApplyPMP').''; } if ($can_validate == 1) { ?> - Réguler le stock + Réguler le stock @@ -196,7 +195,6 @@
trans('Title') ?>texte('', 'title', '',50,255) ?>
trans('Date') ?>calendrier('', 'date_inventory',time()) ?>select_date(time(),'date_inventory'); ?>
trans('SelectCategory') ?>select_all_categories(0,'', 'fk_category') ?>select_all_categories(0,'', 'fk_category') ?>
trans('SelectFournisseur') ?>select_thirdparty('','fk_supplier','s.fournisseur = 1') ?>select_thirdparty('','fk_supplier','s.fournisseur = 1') ?>
trans('OnlyProdsInStock') ?>