Fix: Mass update of vat must work also for supplier prices

Conflicts:
	htdocs/product/admin/product_tools.php
This commit is contained in:
Laurent Destailleur 2013-12-07 12:06:41 +01:00
parent d4c007a909
commit 73e60e0629

View File

@ -1,5 +1,6 @@
<?php <?php
/* Copyright (C) 2012 Regis Houssin <regis.houssin@capnetworks.com> /* Copyright (C) 2012 Regis Houssin <regis.houssin@capnetworks.com>
* Copyright (C) 2013 Laurent Destailleur <eldy@users.sourceforge.net>
* *
* This program is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
@ -28,6 +29,7 @@ require '../../main.inc.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php';
require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php'; require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php';
$langs->load("admin"); $langs->load("admin");
$langs->load("products"); $langs->load("products");
@ -41,6 +43,7 @@ $newvatrate=GETPOST('newvatrate');
//$price_base_type=GETPOST('price_base_type'); //$price_base_type=GETPOST('price_base_type');
$objectstatic = new Product($db); $objectstatic = new Product($db);
$objectstatic2 = new ProductFournisseur($db);
/* /*
@ -60,21 +63,26 @@ if ($action == 'convert')
if (! $error) if (! $error)
{ {
$country_id=$mysoc->country_id; // TODO Allow to choose country into form
$nbrecordsmodified=0;
$db->begin(); $db->begin();
// If country to edit is my country, so we change customer prices
if ($country_id == $mysoc->country_id)
{
$sql = 'SELECT rowid'; $sql = 'SELECT rowid';
$sql.= ' FROM '.MAIN_DB_PREFIX.'product'; $sql.= ' FROM '.MAIN_DB_PREFIX.'product';
$sql.= ' WHERE entity IN ('.getEntity('product',1).')'; $sql.= ' WHERE entity IN ('.getEntity('product',1).')';
$sql.= " AND tva_tx = '".$db->escape($oldvatrate)."'"; $sql.= " AND tva_tx = '".$db->escape($oldvatrate)."'";
//$sql.= ' AND price_base_type = "'..'"';
//print $sql;
$resql=$db->query($sql); $resql=$db->query($sql);
if ($resql) if ($resql)
{ {
$num = $db->num_rows($resql); $num = $db->num_rows($resql);
$i = 0; $nbrecordsmodified=0; $i = 0;
while ($i < $num) while ($i < $num)
{ {
$obj = $db->fetch_object($resql); $obj = $db->fetch_object($resql);
@ -145,19 +153,86 @@ if ($action == 'convert')
$i++; $i++;
} }
}
else dol_print_error($db);
}
$fourn = new Fournisseur($db);
// Change supplier prices
$sql = 'SELECT pfp.rowid, pfp.fk_soc, pfp.price as price, pfp.quantity as qty, pfp.fk_availability, pfp.ref_fourn';
$sql.= ' FROM '.MAIN_DB_PREFIX.'product_fournisseur_price as pfp, '.MAIN_DB_PREFIX.'societe as s';
$sql.= ' WHERE pfp.fk_soc = s.rowid AND pfp.entity IN ('.getEntity('product',1).')';
$sql.= " AND tva_tx = '".$db->escape($oldvatrate)."'";
$sql.= " AND s.fk_pays = '".$country_id."'";
//print $sql;
$resql=$db->query($sql);
if ($resql)
{
$num = $db->num_rows($resql);
$i = 0;
while ($i < $num)
{
$obj = $db->fetch_object($resql);
$ret=$objectstatic2->fetch_product_fournisseur_price($obj->rowid);
if ($ret > 0)
{
$ret=0; $retm=0; $updatelevel1=false;
$price_base_type='HT';
//$price_base_type = $objectstatic2->price_base_type; // Get price_base_type of product/service to keep the same for update
//if ($price_base_type == 'TTC')
//{
// $newprice=price2num($objectstatic2->price_ttc,'MU'); // Second param must be MU (we want a unit price so 'MU'. If unit price was on 4 decimal, we must keep 4 decimals)
// $newminprice=$objectstatic2->price_min_ttc;
//}
//else
//{
$newprice=price2num($obj->price,'MU'); // Second param must be MU (we want a unit price so 'MU'. If unit price was on 4 decimal, we must keep 4 decimals)
//$newminprice=$objectstatic2->fourn_price_min;
//}
//if ($newminprice > $newprice) $newminprice=$newprice;
$newvat=str_replace('*','',$newvatrate);
//$newnpr=$objectstatic2->recuperableonly;
$newlevel=0;
if (! empty($price_base_type) && ! $updatelevel1)
{
//print "$objectstatic2->id $newprice, $price_base_type, $newvat, $newminprice, $newlevel, $newnpr<br>\n";
$fourn->id=$obj->fk_soc;
$ret=$objectstatic2->update_buyprice($obj->qty, $newprice, $user, $price_base_type, $fourn, $obj->fk_availability, $obj->ref_fourn, $newvat);
}
if ($ret < 0 || $retm < 0) $error++;
else $nbrecordsmodified++;
}
$i++;
}
}
else dol_print_error($db);
if (! $error) if (! $error)
{ {
if ($nbrecordsmodified > 0) setEventMessage($langs->trans("RecordsModified",$nbrecordsmodified));
else setEventMessage($langs->trans("NoRecordFound"),'warnings');
$db->commit(); $db->commit();
} }
else else
{ {
setEventMessage($langs->trans("Error"),'errors');
$db->rollback(); $db->rollback();
} }
// Output result
if (! $error)
{
if ($nbrecordsmodified > 0) setEventMessage($langs->trans("RecordsModified",$nbrecordsmodified));
else setEventMessage($langs->trans("NoRecordFound"),'warnings');
} }
else
{
setEventMessage($langs->trans("Error"),'errors');
}
} }
} }