diff --git a/htdocs/install/repair.php b/htdocs/install/repair.php index d4679c9ac0c..e354b2a30da 100644 --- a/htdocs/install/repair.php +++ b/htdocs/install/repair.php @@ -76,6 +76,7 @@ print '

'.$langs->trans("Repair").'

'; print 'Option restore_thirdparties_logos is '.(GETPOST('restore_thirdparties_logos')?GETPOST('restore_thirdparties_logos'):'0').'
'."\n"; print 'Option clean_linked_elements is '.(GETPOST('clean_linked_elements')?GETPOST('clean_linked_elements'):'0').'
'."\n"; print 'Option clean_orphelin_dir (1 or confirmed) is '.(GETPOST('clean_orphelin_dir')?GETPOST('clean_orphelin_dir'):'0').'
'."\n"; +print 'Option clean_product_stock_batch (1 or confirmed) is '.(GETPOST('clean_product_stock_batch')?GETPOST('clean_product_stock_batch'):'0').'
'."\n"; print '
'; print ''; @@ -519,6 +520,107 @@ if ($ok && GETPOST('clean_orphelin_dir')) } } +// clean_linked_elements: Check and clean linked elements +if ($ok && GETPOST('clean_product_stock_batch')) +{ + print ''; + + $sql ="SELECT p.rowid, p.ref, p.tobatch, ps.rowid as psrowid, ps.fk_entrepot, ps.reel, SUM(pb.qty) as reelbatch"; + $sql.=" FROM ".MAIN_DB_PREFIX."product as p, ".MAIN_DB_PREFIX."product_stock as ps, ".MAIN_DB_PREFIX."product_batch as pb"; + $sql.=" WHERE p.rowid = ps.fk_product AND ps.rowid = pb.fk_product_stock"; + $sql.=" AND p.tobatch = 1"; + $sql.=" GROUP BY p.rowid, p.ref, p.tobatch, ps.rowid, ps.fk_entrepot, ps.reel"; + $sql.=" HAVING reel != SUM(pb.qty)"; + $resql = $db->query($sql); + if ($resql) + { + $num = $db->num_rows($resql); + + if ($num) + { + $i = 0; + while ($i < $num) + { + $obj=$db->fetch_object($resql); + print ''; + + $i++; + } + } + } + else + { + dol_print_error($db); + } + + +} + + + + + print '

Clean table product_batch
'.$obj->rowid.'-'.$obj->ref.'-'.$obj->fk_entrepot.' -> '.$obj->psrowid.': '.$obj->reel.' != '.$obj->reelbatch; + + // Fix + if ($obj->reel > $obj->reelbatch) + { + $methodtofix='updatestock'; + + if ($methodtofix == 'updatebatch') + { + // Method 1 + print ' -> Insert qty '.($obj->reel - $obj->reelbatch).' with lot 000000 linked to fk_product_stock='.$obj->psrowid; + if (GETPOST('clean_product_stock_batch') == 'confirmed') + { + $sql2 ="INSERT INTO ".MAIN_DB_PREFIX."product_batch(fk_product_stock, batch, qty)"; + $sql2.="VALUES(".$obj->psrowid.", '000000', ".($obj->reel - $obj->reelbatch).")"; + $resql2=$db->query($sql2); + if (! $resql2) + { + // TODO If it fails, we must make update + //$sql2 ="UPDATE ".MAIN_DB_PREFIX."product_batch"; + //$sql2.=" SET ".$obj->psrowid.", '000000', ".($obj->reel - $obj->reelbatch).")"; + //$sql2.=" WHERE fk_product_stock = ".$obj->psrowid" + } + } + } + if ($methodtofix == 'updatestock') + { + // Method 2 + print ' -> Update qty of stock with qty = '.$obj->reelbatch.' for ps.rowid = '.$obj->psrowid; + if (GETPOST('clean_product_stock_batch') == 'confirmed') + { + $error=0; + + $db->begin(); + + $sql2 ="UPDATE ".MAIN_DB_PREFIX."product_stock"; + $sql2.=" SET reel = ".$obj->reelbatch." WHERE rowid = ".$obj->psrowid; + $resql2=$db->query($sql2); + if ($resql2) + { + // We update product stock, so we must update product.stock too. + $sql3='UPDATE llx_product p SET p.stock= (SELECT SUM(ps.reel) FROM llx_product_stock ps WHERE ps.fk_product = p.rowid)'; + $resql3=$db->query($sql3); + if (! $resql3) + { + $error++; + dol_print_error($db); + } + } + else + { + $error++; + dol_print_error($db); + } + + if (!$error) $db->commit(); + else $db->rollback(); + } + } + } + + print'
';