2012-05-22 18:35:14 +02:00
< ? php
2018-10-27 14:43:12 +02:00
/* Copyright ( C ) 2012 Regis Houssin < regis . houssin @ inodbox . com >
2015-08-31 12:13:23 +02:00
* Copyright ( C ) 2013 - 2015 Laurent Destailleur < eldy @ users . sourceforge . net >
2012-05-22 18:35:14 +02:00
*
* 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
2013-01-16 15:36:08 +01:00
* the Free Software Foundation ; either version 3 of the License , or
2012-05-22 18:35:14 +02:00
* ( 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
2019-09-23 21:55:30 +02:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
2012-05-22 18:35:14 +02:00
*/
/**
* \file htdocs / product / admin / product_tools . php
* \ingroup product
* \brief Setup page of product module
*/
2012-08-27 12:07:50 +02:00
// TODO We must add a confirmation on button because this will make a mass change
2012-12-30 21:56:53 +01:00
// FIXME Should also change table product_price for price levels
2012-08-27 12:07:50 +02:00
2012-08-22 23:24:21 +02:00
require '../../main.inc.php' ;
2012-08-22 23:11:24 +02:00
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 . '/product/class/product.class.php' ;
2013-12-07 02:29:24 +01:00
require_once DOL_DOCUMENT_ROOT . '/fourn/class/fournisseur.product.class.php' ;
2012-05-22 18:35:14 +02:00
2018-05-26 17:57:30 +02:00
// Load translation files required by the page
$langs -> loadLangs ( array ( 'admin' , 'products' ));
2012-05-22 18:35:14 +02:00
// Security check
2021-02-26 14:25:17 +01:00
if ( ! $user -> admin ) {
accessforbidden ();
}
2012-05-22 18:35:14 +02:00
2020-09-16 19:39:50 +02:00
$action = GETPOST ( 'action' , 'aZ09' );
2020-04-10 10:59:32 +02:00
$oldvatrate = GETPOST ( 'oldvatrate' , 'alpha' );
$newvatrate = GETPOST ( 'newvatrate' , 'alpha' );
2012-08-29 14:13:13 +02:00
//$price_base_type=GETPOST('price_base_type');
2012-05-22 18:35:14 +02:00
/*
* Actions
*/
2021-02-26 14:25:17 +01:00
if ( $action == 'convert' ) {
2020-04-10 10:59:32 +02:00
$error = 0 ;
2012-05-23 02:31:38 +02:00
2021-02-26 14:25:17 +01:00
if ( $oldvatrate == $newvatrate ) {
2012-08-29 14:13:13 +02:00
$langs -> load ( " errors " );
2015-12-12 10:59:02 +01:00
setEventMessages ( $langs -> trans ( " ErrorNewValueCantMatchOldValue " ), null , 'errors' );
2012-08-29 14:13:13 +02:00
$error ++ ;
}
2012-05-23 02:31:38 +02:00
2021-02-26 14:25:17 +01:00
if ( ! $error ) {
2020-04-10 10:59:32 +02:00
$country_id = $mysoc -> country_id ; // TODO Allow to choose country into form
2012-08-29 14:13:13 +02:00
2020-04-10 10:59:32 +02:00
$nbrecordsmodified = 0 ;
2012-08-29 14:13:13 +02:00
2013-12-07 02:29:24 +01:00
$db -> begin ();
2017-09-25 21:41:53 +02:00
// Clean vat code old
2020-04-10 10:59:32 +02:00
$vat_src_code_old = '' ;
2021-02-26 14:25:17 +01:00
if ( preg_match ( '/\((.*)\)/' , $oldvatrate , $reg )) {
2017-09-25 21:41:53 +02:00
$vat_src_code_old = $reg [ 1 ];
2020-04-10 10:59:32 +02:00
$oldvatrateclean = preg_replace ( '/\s*\(.*\)/' , '' , $oldvatrate ); // Remove code into vatrate.
2021-02-26 14:25:17 +01:00
} else {
$oldvatrateclean = $oldvatrate ;
}
2017-09-25 21:41:53 +02:00
// Clean vat code new
2020-04-10 10:59:32 +02:00
$vat_src_code_new = '' ;
2021-02-26 14:25:17 +01:00
if ( preg_match ( '/\((.*)\)/' , $newvatrate , $reg )) {
2017-09-25 21:41:53 +02:00
$vat_src_code_new = $reg [ 1 ];
2020-04-10 10:59:32 +02:00
$newvatrateclean = preg_replace ( '/\s*\(.*\)/' , '' , $newvatrate ); // Remove code into vatrate.
2021-02-26 14:25:17 +01:00
} else {
$newvatrateclean = $newvatrate ;
}
2017-09-25 21:41:53 +02:00
2013-12-07 02:29:24 +01:00
// If country to edit is my country, so we change customer prices
2021-02-26 14:25:17 +01:00
if ( $country_id == $mysoc -> country_id ) {
2013-12-07 02:29:24 +01:00
$sql = 'SELECT rowid' ;
2020-04-10 10:59:32 +02:00
$sql .= ' FROM ' . MAIN_DB_PREFIX . 'product' ;
$sql .= ' WHERE entity IN (' . getEntity ( 'product' ) . ')' ;
$sql .= " AND tva_tx = ' " . $db -> escape ( $oldvatrateclean ) . " ' " ;
2021-02-26 14:25:17 +01:00
if ( $vat_src_code_old ) {
$sql .= " AND default_vat_code = ' " . $db -> escape ( $vat_src_code_old ) . " ' " ;
} else {
" AND default_vat_code = IS NULL " ;
}
2012-08-29 14:13:13 +02:00
2020-04-10 10:59:32 +02:00
$resql = $db -> query ( $sql );
2021-02-26 14:25:17 +01:00
if ( $resql ) {
2013-12-07 02:29:24 +01:00
$num = $db -> num_rows ( $resql );
2012-05-23 02:31:38 +02:00
2013-12-07 02:29:24 +01:00
$i = 0 ;
2021-02-26 14:25:17 +01:00
while ( $i < $num ) {
2013-12-07 02:29:24 +01:00
$obj = $db -> fetch_object ( $resql );
2012-05-23 02:31:38 +02:00
2020-04-10 10:59:32 +02:00
$objectstatic = new Product ( $db ); // Object init must be into loop to avoid to get value of previous step
$ret = $objectstatic -> fetch ( $obj -> rowid );
2021-02-26 14:25:17 +01:00
if ( $ret > 0 ) {
2020-04-10 10:59:32 +02:00
$ret = 0 ; $retm = 0 ; $updatelevel1 = false ;
2013-12-07 02:29:24 +01:00
// Update multiprice
2020-04-10 10:59:32 +02:00
$listofmulti = array_reverse ( $objectstatic -> multiprices , true ); // To finish with level 1
2021-02-26 14:25:17 +01:00
foreach ( $listofmulti as $level => $multiprices ) {
2020-04-10 10:59:32 +02:00
$price_base_type = $objectstatic -> multiprices_base_type [ $level ]; // Get price_base_type of product/service to keep the same for update
2021-02-26 14:25:17 +01:00
if ( empty ( $price_base_type )) {
continue ; // Discard not defined price levels
}
2013-12-07 02:29:24 +01:00
2021-02-26 14:25:17 +01:00
if ( $price_base_type == 'TTC' ) {
2020-04-10 10:59:32 +02:00
$newprice = price2num ( $objectstatic -> multiprices_ttc [ $level ], '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 = $objectstatic -> multiprices_min_ttc [ $level ];
2020-05-21 15:05:19 +02:00
} else {
2020-04-10 10:59:32 +02:00
$newprice = price2num ( $objectstatic -> multiprices [ $level ], '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 = $objectstatic -> multiprices_min [ $level ];
2013-12-07 02:29:24 +01:00
}
2021-02-26 14:25:17 +01:00
if ( $newminprice > $newprice ) {
$newminprice = $newprice ;
}
2017-09-25 21:41:53 +02:00
2020-04-10 10:59:32 +02:00
$newvat = str_replace ( '*' , '' , $newvatrate );
$localtaxes_type = getLocalTaxesFromRate ( $newvat , 0 , $mysoc , $mysoc );
$newnpr = $objectstatic -> multiprices_recuperableonly [ $level ];
$newdefaultvatcode = $vat_src_code_new ;
$newlevel = $level ;
2013-12-07 02:29:24 +01:00
//print "$objectstatic->id $newprice, $price_base_type, $newvat, $newminprice, $newlevel, $newnpr<br>\n";
2020-04-10 10:59:32 +02:00
$retm = $objectstatic -> updatePrice ( $newprice , $price_base_type , $user , $newvatrateclean , $newminprice , $newlevel , $newnpr , 0 , 0 , $localtaxes_type , $newdefaultvatcode );
2021-02-26 14:25:17 +01:00
if ( $retm < 0 ) {
2013-12-07 02:29:24 +01:00
$error ++ ;
break ;
}
2021-02-26 14:25:17 +01:00
if ( $newlevel == 1 ) {
$updatelevel1 = true ;
}
2013-12-07 02:29:24 +01:00
}
2013-06-24 17:34:22 +02:00
2013-12-07 02:29:24 +01:00
// Update single price
2020-04-10 10:59:32 +02:00
$price_base_type = $objectstatic -> price_base_type ; // Get price_base_type of product/service to keep the same for update
2021-02-26 14:25:17 +01:00
if ( $price_base_type == 'TTC' ) {
2020-04-10 10:59:32 +02:00
$newprice = price2num ( $objectstatic -> 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 = $objectstatic -> price_min_ttc ;
2020-05-21 15:05:19 +02:00
} else {
2020-04-10 10:59:32 +02:00
$newprice = price2num ( $objectstatic -> 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 = $objectstatic -> price_min ;
2013-06-24 17:34:22 +02:00
}
2021-02-26 14:25:17 +01:00
if ( $newminprice > $newprice ) {
$newminprice = $newprice ;
}
2020-04-10 10:59:32 +02:00
$newvat = str_replace ( '*' , '' , $newvatrate );
$localtaxes_type = getLocalTaxesFromRate ( $newvat , 0 , $mysoc , $mysoc );
$newnpr = $objectstatic -> recuperableonly ;
$newdefaultvatcode = $vat_src_code_new ;
$newlevel = 0 ;
2021-02-26 14:25:17 +01:00
if ( ! empty ( $price_base_type ) && ! $updatelevel1 ) {
2013-12-07 02:29:24 +01:00
//print "$objectstatic->id $newprice, $price_base_type, $newvat, $newminprice, $newlevel, $newnpr<br>\n";
2020-04-10 10:59:32 +02:00
$ret = $objectstatic -> updatePrice ( $newprice , $price_base_type , $user , $newvatrateclean , $newminprice , $newlevel , $newnpr , 0 , 0 , $localtaxes_type , $newdefaultvatcode );
2013-06-14 00:11:15 +02:00
}
2013-06-14 01:23:54 +02:00
2021-02-26 14:25:17 +01:00
if ( $ret < 0 || $retm < 0 ) {
$error ++ ;
} else {
$nbrecordsmodified ++ ;
}
2013-06-14 00:11:15 +02:00
}
2020-10-31 14:32:18 +01:00
unset ( $objectstatic );
2017-09-25 21:41:53 +02:00
2013-12-07 02:29:24 +01:00
$i ++ ;
}
2021-02-26 14:25:17 +01:00
} else {
dol_print_error ( $db );
}
2013-12-07 02:29:24 +01:00
}
$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' ;
2020-04-10 10:59:32 +02:00
$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' ) . ')' ;
$sql .= " AND tva_tx = ' " . $db -> escape ( $oldvatrate ) . " ' " ;
2021-02-26 14:25:17 +01:00
if ( $vat_src_code_old ) {
$sql .= " AND default_vat_code = ' " . $db -> escape ( $vat_src_code_old ) . " ' " ;
} else {
" AND default_vat_code = IS NULL " ;
}
2020-09-20 02:57:15 +02:00
$sql .= " AND s.fk_pays = " . (( int ) $country_id );
2013-12-07 02:29:24 +01:00
//print $sql;
2020-04-10 10:59:32 +02:00
$resql = $db -> query ( $sql );
2021-02-26 14:25:17 +01:00
if ( $resql ) {
2013-12-07 02:29:24 +01:00
$num = $db -> num_rows ( $resql );
$i = 0 ;
2021-02-26 14:25:17 +01:00
while ( $i < $num ) {
2013-12-07 02:29:24 +01:00
$obj = $db -> fetch_object ( $resql );
2020-04-10 10:59:32 +02:00
$objectstatic2 = new ProductFournisseur ( $db ); // Object init must be into loop to avoid to get value of previous step
$ret = $objectstatic2 -> fetch_product_fournisseur_price ( $obj -> rowid );
2021-02-26 14:25:17 +01:00
if ( $ret > 0 ) {
2020-04-10 10:59:32 +02:00
$ret = 0 ; $retm = 0 ; $updatelevel1 = false ;
2013-12-07 02:29:24 +01:00
2020-04-10 10:59:32 +02:00
$price_base_type = 'HT' ;
2013-12-07 02:29:24 +01:00
//$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
//{
2020-04-10 10:59:32 +02:00
$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)
2013-12-07 02:29:24 +01:00
//$newminprice=$objectstatic2->fourn_price_min;
//}
//if ($newminprice > $newprice) $newminprice=$newprice;
2020-04-10 10:59:32 +02:00
$newvat = str_replace ( '*' , '' , $newvatrate );
$localtaxes_type = getLocalTaxesFromRate ( $newvat , 0 , $mysoc , $mysoc );
2013-12-07 02:29:24 +01:00
//$newnpr=$objectstatic2->recuperableonly;
2020-04-10 10:59:32 +02:00
$newnpr = 0 ;
$newdefaultvatcode = $vat_src_code_new ;
2017-09-25 21:41:53 +02:00
$newpercent = $objectstatic2 -> fourn_remise_percent ;
$newdeliverydelay = $objectstatic2 -> delivery_time_days ;
$newsupplierreputation = $objectstatic2 -> supplier_reputation ;
2020-04-10 10:59:32 +02:00
$newlevel = 0 ;
2021-02-26 14:25:17 +01:00
if ( ! empty ( $price_base_type ) && ! $updatelevel1 ) {
2013-12-07 02:29:24 +01:00
//print "$objectstatic2->id $newprice, $price_base_type, $newvat, $newminprice, $newlevel, $newnpr<br>\n";
2020-04-10 10:59:32 +02:00
$fourn -> id = $obj -> fk_soc ;
$ret = $objectstatic2 -> update_buyprice ( $obj -> qty , $newprice , $user , $price_base_type , $fourn , $obj -> fk_availability , $obj -> ref_fourn , $newvat , '' , $newpercent , 0 , $newnpr , $newdeliverydelay , $newsupplierreputation , $localtaxes_type , $newdefaultvatcode );
2013-06-24 17:34:22 +02:00
}
2021-02-26 14:25:17 +01:00
if ( $ret < 0 || $retm < 0 ) {
$error ++ ;
} else {
$nbrecordsmodified ++ ;
}
2012-05-22 18:35:14 +02:00
}
2017-04-24 12:42:54 +02:00
unset ( $objectstatic2 );
2017-09-25 21:41:53 +02:00
2012-09-07 17:23:16 +02:00
$i ++ ;
2012-05-22 18:35:14 +02:00
}
2021-02-26 14:25:17 +01:00
} else {
dol_print_error ( $db );
}
2012-05-23 02:31:38 +02:00
2021-02-26 14:25:17 +01:00
if ( ! $error ) {
2013-12-07 02:29:24 +01:00
$db -> commit ();
2020-05-21 15:05:19 +02:00
} else {
2013-12-07 02:29:24 +01:00
$db -> rollback ();
}
// Output result
2021-02-26 14:25:17 +01:00
if ( ! $error ) {
if ( $nbrecordsmodified > 0 ) {
setEventMessages ( $langs -> trans ( " RecordsModified " , $nbrecordsmodified ), null , 'mesgs' );
} else {
setEventMessages ( $langs -> trans ( " NoRecordFound " ), null , 'warnings' );
}
2020-05-21 15:05:19 +02:00
} else {
2015-12-12 10:59:02 +01:00
setEventMessages ( $langs -> trans ( " Error " ), null , 'errors' );
2013-12-07 02:29:24 +01:00
}
2012-05-22 18:35:14 +02:00
}
}
/*
* View
*/
2020-04-10 10:59:32 +02:00
$form = new Form ( $db );
2012-12-30 21:56:53 +01:00
2017-02-22 02:19:05 +01:00
$title = $langs -> trans ( 'ProductVatMassChange' );
2012-05-22 18:35:14 +02:00
2019-01-27 11:55:16 +01:00
llxHeader ( '' , $title );
2012-05-22 18:35:14 +02:00
2019-01-27 11:55:16 +01:00
print load_fiche_titre ( $title , '' , 'title_setup' );
2012-05-22 18:35:14 +02:00
2012-08-29 14:13:13 +02:00
print $langs -> trans ( " ProductVatMassChangeDesc " ) . '<br><br>' ;
2012-05-22 18:35:14 +02:00
2021-02-26 14:25:17 +01:00
if ( empty ( $mysoc -> country_code )) {
2013-01-10 08:27:12 +01:00
$langs -> load ( " errors " );
2020-04-10 10:59:32 +02:00
$warnpicto = img_error ( $langs -> trans ( " WarningMandatorySetupNotComplete " ));
2013-01-10 08:27:12 +01:00
print '<br><a href="' . DOL_URL_ROOT . '/admin/company.php?mainmenu=home">' . $warnpicto . ' ' . $langs -> trans ( " WarningMandatorySetupNotComplete " ) . '</a>' ;
2020-05-21 15:05:19 +02:00
} else {
2012-12-30 21:56:53 +01:00
print '<form method="POST" action="' . $_SERVER [ 'PHP_SELF' ] . '">' ;
2019-12-18 23:12:31 +01:00
print '<input type="hidden" name="token" value="' . newToken () . '" />' ;
2012-12-30 21:56:53 +01:00
print '<input type="hidden" name="action" value="convert" />' ;
2019-11-05 21:24:41 +01:00
print '<table class="noborder centpercent">' ;
2012-12-30 21:56:53 +01:00
print '<tr class="liste_titre">' ;
print '<td>' . $langs -> trans ( " Parameters " ) . '</td>' . " \n " ;
2019-03-15 09:49:07 +01:00
print '<td class="right" width="60">' . $langs -> trans ( " Value " ) . '</td>' . " \n " ;
2012-12-30 21:56:53 +01:00
print '</tr>' . " \n " ;
2017-09-25 21:41:53 +02:00
2017-04-14 13:02:29 +02:00
print '<tr class="oddeven">' . " \n " ;
2012-12-30 21:56:53 +01:00
print '<td>' . $langs -> trans ( " OldVATRates " ) . '</td>' . " \n " ;
2019-03-15 09:49:07 +01:00
print '<td width="60" class="right">' . " \n " ;
2017-09-25 21:41:53 +02:00
print $form -> load_tva ( 'oldvatrate' , $oldvatrate , $mysoc , null , 0 , 0 , '' , false , 1 );
2012-12-30 21:56:53 +01:00
print '</td>' . " \n " ;
print '</tr>' . " \n " ;
2017-09-25 21:41:53 +02:00
2017-04-14 13:02:29 +02:00
print '<tr class="oddeven">' . " \n " ;
2012-12-30 21:56:53 +01:00
print '<td>' . $langs -> trans ( " NewVATRates " ) . '</td>' . " \n " ;
2019-03-15 09:49:07 +01:00
print '<td width="60" class="right">' . " \n " ;
2017-09-25 21:41:53 +02:00
print $form -> load_tva ( 'newvatrate' , $newvatrate , $mysoc , null , 0 , 0 , '' , false , 1 );
2012-12-30 21:56:53 +01:00
print '</td>' . " \n " ;
print '</tr>' . " \n " ;
/*
2017-09-25 21:41:53 +02:00
2017-04-14 13:02:29 +02:00
print '<tr class="oddeven">' . " \n " ;
2012-12-30 21:56:53 +01:00
print '<td>' . $langs -> trans ( " PriceBaseTypeToChange " ) . '</td>' . " \n " ;
2019-03-15 09:49:07 +01:00
print '<td width="60" class="right">' . " \n " ;
2015-06-06 14:21:18 +02:00
print $form -> selectPriceBaseType ( $price_base_type );
2012-12-30 21:56:53 +01:00
print '</td>' . " \n " ;
print '</tr>' . " \n " ;
*/
print '</table>' ;
2015-08-31 12:13:23 +02:00
print '<br>' ;
2017-09-25 21:41:53 +02:00
2019-06-17 17:53:43 +02:00
// Buttons for actions
2014-11-23 13:14:03 +01:00
print '<div class="center">' ;
2012-12-30 21:56:53 +01:00
print '<input type="submit" id="convert_vatrate" name="convert_vatrate" value="' . $langs -> trans ( " MassConvert " ) . '" class="button" />' ;
print '</div>' ;
print '</form>' ;
}
2012-05-22 18:35:14 +02:00
2018-08-14 09:55:58 +02:00
// End of page
2012-05-22 18:35:14 +02:00
llxFooter ();
$db -> close ();