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 '
| 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') ?> | @@ -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').'|||
| 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 @@ | |||
|---|---|---|---|