2016-02-23 23:03:52 +01:00
#!/usr/bin/env php
< ? php
/* Copyright ( C ) 2016 Laurent Destailleur < eldy @ users . sourceforge . net >
2016-07-15 16:58:26 +02:00
* Copyright ( C ) 2016 Juanjo Menent < jmenent @ 2 byte . es >
2016-02-23 23:03:52 +01: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
* the Free Software Foundation ; either version 3 of the License , or
* ( 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
* along with this program . If not , see < http :// www . gnu . org / licenses />.
*
* WARNING , THIS WILL LOAD MASS DATA ON YOUR INSTANCE
*/
/**
* \file dev / initdata / import - thirdparties . php
2017-06-09 09:25:15 +02:00
* \brief Script example to insert thirdparties from a csv file .
2016-02-23 23:03:52 +01:00
* To purge data , you can have a look at purge - data . php
*/
// Test si mode batch
$sapi_type = php_sapi_name ();
$script_file = basename ( __FILE__ );
$path = dirname ( __FILE__ ) . '/' ;
if ( substr ( $sapi_type , 0 , 3 ) == 'cgi' ) {
echo " Erreur: Vous utilisez l'interpreteur PHP pour le mode CGI. Pour executer mailing-send.php en ligne de commande, vous devez utiliser l'interpreteur PHP pour le mode CLI. \n " ;
exit ;
}
// Recupere root dolibarr
$path = preg_replace ( '/import-thirdparties.php/i' , '' , $_SERVER [ " PHP_SELF " ]);
require ( $path . " ../../htdocs/master.inc.php " );
include_once DOL_DOCUMENT_ROOT . '/societe/class/societe.class.php' ;
include_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php' ;
$delimiter = ',' ;
$enclosure = '"' ;
$linelength = 10000 ;
$escape = '/' ;
// Global variables
$version = DOL_VERSION ;
$confirmed = 1 ;
$error = 0 ;
/*
* Main
*/
@ set_time_limit ( 0 );
print " ***** " . $script_file . " ( " . $version . " ) pid= " . dol_getmypid () . " ***** \n " ;
2016-07-15 16:58:26 +02:00
dol_syslog ( $script_file . " launched with arg " . implode ( ',' , $argv ));
2016-02-23 23:03:52 +01:00
$mode = $argv [ 1 ];
$filepath = $argv [ 2 ];
$filepatherr = $filepath . '.err' ;
//$defaultlang = empty($argv[3])?'en_US':$argv[3];
$startlinenb = empty ( $argv [ 3 ]) ? 1 : $argv [ 3 ];
$endlinenb = empty ( $argv [ 4 ]) ? 0 : $argv [ 4 ];
if ( empty ( $mode ) || ! in_array ( $mode , array ( 'test' , 'confirm' , 'confirmforced' )) || empty ( $filepath )) {
print " Usage: $script_file (test|confirm|confirmforced) filepath.csv [startlinenb] [endlinenb] \n " ;
print " Usage: $script_file test myfilepath.csv 2 1002 \n " ;
print " \n " ;
exit ( - 1 );
}
if ( ! file_exists ( $filepath )) {
print " Error: File " . $filepath . " not found. \n " ;
print " \n " ;
exit ( - 1 );
}
$ret = $user -> fetch ( '' , 'admin' );
if ( ! $ret > 0 )
{
print 'A user with login "admin" and all permissions must be created to use this script.' . " \n " ;
exit ;
}
$user -> getrights ();
// Ask confirmation
if ( ! $confirmed )
{
print " Hit Enter to continue or CTRL+C to stop... \n " ;
$input = trim ( fgets ( STDIN ));
}
// Open input and ouput files
$fhandle = fopen ( $filepath , 'r' );
if ( ! $fhandle )
{
print 'Error: Failed to open file ' . $filepath . " \n " ;
exit ( 1 );
}
$fhandleerr = fopen ( $filepatherr , 'w' );
if ( ! $fhandleerr )
{
print 'Error: Failed to open file ' . $filepatherr . " \n " ;
exit ( 1 );
}
//$langs->setDefaultLang($defaultlang);
$db -> begin ();
$i = 0 ;
2016-02-24 17:27:28 +01:00
$nboflines = 0 ;
2016-02-23 23:03:52 +01:00
while ( $fields = fgetcsv ( $fhandle , $linelength , $delimiter , $enclosure , $escape ))
{
$i ++ ;
$errorrecord = 0 ;
if ( $startlinenb && $i < $startlinenb ) continue ;
if ( $endlinenb && $i > $endlinenb ) continue ;
2016-02-24 17:27:28 +01:00
$nboflines ++ ;
2017-06-09 09:25:15 +02:00
2016-02-23 23:03:52 +01:00
$object = new Societe ( $db );
$object -> state = $fields [ 6 ];
$object -> client = $fields [ 7 ];
$object -> fournisseur = $fields [ 8 ];
2017-06-09 09:25:15 +02:00
2016-02-24 17:27:28 +01:00
$object -> name = $fields [ 13 ] ? trim ( $fields [ 13 ]) : $fields [ 0 ];
$object -> name_alias = $fields [ 0 ] != $fields [ 13 ] ? trim ( $fields [ 0 ]) : '' ;
2017-06-09 09:25:15 +02:00
2016-02-23 23:03:52 +01:00
$object -> address = trim ( $fields [ 14 ]);
$object -> zip = trim ( $fields [ 15 ]);
$object -> town = trim ( $fields [ 16 ]);
2016-02-24 12:23:34 +01:00
$object -> country_id = dol_getIdFromCode ( $db , trim ( $fields [ 21 ]), 'c_country' , 'code' , 'rowid' );
$object -> phone = trim ( $fields [ 22 ]);
$object -> fax = trim ( $fields [ 23 ]);
2016-02-23 23:03:52 +01:00
$object -> email = trim ( $fields [ 26 ]);
2016-02-24 12:23:34 +01:00
$object -> idprof2 = trim ( $fields [ 29 ]);
2016-02-23 23:03:52 +01:00
$object -> tva_intra = trim ( $fields [ 34 ]);
$object -> default_lang = trim ( $fields [ 43 ]);
2016-02-24 12:23:34 +01:00
//$condpayment = dol_string_unaccent(trim($fields[36]));
2016-02-24 17:27:28 +01:00
if ( $fields [ 36 ])
{
$condpayment = trim ( $fields [ 36 ]);
if ( $condpayment == 'A la commande' ) $condpayment = 'A réception de commande' ;
if ( $condpayment == 'A reception facture' ) $condpayment = 'Réception de facture' ;
2017-10-02 11:59:17 +02:00
$object -> cond_reglement_id = dol_getIdFromCode ( $db , $condpayment , 'c_payment_term' , 'libelle_facture' , 'rowid' , 1 );
2016-02-24 17:27:28 +01:00
if ( empty ( $object -> cond_reglement_id ))
{
print " - Error cant find payment mode for " . $condpayment . " \n " ;
$errorrecord ++ ;
}
}
2016-02-24 12:23:34 +01:00
2016-02-23 23:03:52 +01:00
$object -> code_client = $fields [ 9 ];
$object -> code_fournisseur = $fields [ 10 ];
$labeltype = trim ( $fields [ 1 ]);
2016-02-24 12:23:34 +01:00
$object -> typent_id = dol_getIdFromCode ( $db , $labeltype , 'c_typent' , 'libelle' );
2016-02-23 23:03:52 +01:00
2016-02-24 12:23:34 +01:00
// Set price level
$object -> price_level = 1 ;
if ( $labeltype == 'Revendeur' ) $object -> price_level = 2 ;
2017-06-09 09:25:15 +02:00
2016-02-23 23:03:52 +01:00
print " Process line nb " . $i . " , name " . $object -> name ;
// Extrafields
2016-02-24 12:23:34 +01:00
$object -> array_options [ 'options_anastate' ] = price2num ( $fields [ 20 ]);
$object -> array_options [ 'options_anaregion' ] = price2num ( $fields [ 17 ]);
2016-02-24 17:27:28 +01:00
if ( ! $errorrecord )
{
$ret = $object -> create ( $user );
if ( $ret < 0 )
{
print " - Error in create result code = " . $ret . " - " . $object -> errorsToString ();
$errorrecord ++ ;
}
2017-06-09 09:25:15 +02:00
else
2016-02-24 17:27:28 +01:00
{
print " - Creation OK with name " . $object -> name . " - id = " . $ret ;
}
}
if ( ! $errorrecord )
2016-02-23 23:03:52 +01:00
{
2016-02-24 17:27:28 +01:00
dol_syslog ( " Set price level " );
$object -> set_price_level ( $object -> price_level , $user );
2016-02-23 23:03:52 +01:00
}
2016-02-24 12:23:34 +01:00
// Assign sales representative
2016-02-24 17:27:28 +01:00
if ( ! $errorrecord && $fields [ 3 ])
2016-02-24 12:23:34 +01:00
{
$salesrep = new User ( $db );
2017-06-09 09:25:15 +02:00
2016-02-24 12:23:34 +01:00
$tmp = explode ( ' ' , $fields [ 3 ], 2 );
$salesrep -> firstname = trim ( $tmp [ 0 ]);
$salesrep -> lastname = trim ( $tmp [ 1 ]);
if ( $salesrep -> lastname ) $salesrep -> login = strtolower ( substr ( $salesrep -> firstname , 0 , 1 )) . strtolower ( substr ( $salesrep -> lastname , 0 ));
else $salesrep -> login = strtolower ( $salesrep -> firstname );
$salesrep -> login = preg_replace ( '/ /' , '' , $salesrep -> login );
$salesrep -> fetch ( 0 , $salesrep -> login );
2017-06-09 09:25:15 +02:00
2016-02-24 12:23:34 +01:00
$result = $object -> add_commercial ( $user , $salesrep -> id );
if ( $result < 0 )
{
print " - Error in create link with sale representative result code = " . $result . " - " . $object -> errorsToString ();
$errorrecord ++ ;
}
else
{
print " - create link sale representative OK " ;
}
2017-06-09 09:25:15 +02:00
}
2016-02-24 12:23:34 +01:00
dol_syslog ( " Add invoice contacts " );
// Insert an invoice contact if there is an invoice email != standard email
2016-02-23 23:03:52 +01:00
if ( ! $errorrecord && $fields [ 27 ] && $fields [ 26 ] != $fields [ 27 ])
{
2016-02-24 12:23:34 +01:00
$ret1 = $ret2 = 0 ;
2017-06-09 09:25:15 +02:00
2016-02-23 23:03:52 +01:00
$contact = new Contact ( $db );
2016-02-24 12:23:34 +01:00
$contact -> lastname = $object -> name ;
$contact -> address = $object -> address ;
$contact -> zip = $object -> zip ;
$contact -> town = $object -> town ;
$contact -> country_id = $object -> country_id ;
$contact -> email = $fields [ 27 ];
$contact -> socid = $object -> id ;
2017-06-09 09:25:15 +02:00
2016-02-24 12:23:34 +01:00
$ret1 = $contact -> create ( $user );
if ( $ret1 > 0 )
{
//$ret2=$contact->add_contact($object->id, 'BILLING');
}
2016-02-23 23:03:52 +01:00
if ( $ret1 < 0 || $ret2 < 0 )
{
print " - Error in create contact result code = " . $ret1 . " " . $ret2 . " - " . $object -> errorsToString ();
$errorrecord ++ ;
}
2017-06-09 09:25:15 +02:00
else
2016-02-23 23:03:52 +01:00
{
print " - create contact OK " ;
}
}
2017-06-09 09:25:15 +02:00
2016-02-24 12:23:34 +01:00
dol_syslog ( " Add delivery contacts " );
2016-02-23 23:03:52 +01:00
// Insert a delivery contact
2016-02-24 17:27:28 +01:00
if ( ! $errorrecord && $fields [ 47 ])
2016-02-23 23:03:52 +01:00
{
2016-02-24 12:23:34 +01:00
$ret1 = $ret2 = 0 ;
2017-06-09 09:25:15 +02:00
2016-02-24 12:23:34 +01:00
$contact2 = new Contact ( $db );
2016-02-24 17:27:28 +01:00
$contact2 -> lastname = 'Service livraison - ' . $fields [ 47 ];
2016-02-24 12:23:34 +01:00
$contact2 -> address = $fields [ 48 ];
$contact2 -> zip = $fields [ 50 ];
$contact2 -> town = $fields [ 51 ];
$contact2 -> country_id = dol_getIdFromCode ( $db , trim ( $fields [ 52 ]), 'c_country' , 'code' , 'rowid' );
$contact2 -> note_public = $fields [ 54 ];
$contact2 -> socid = $object -> id ;
2017-06-09 09:25:15 +02:00
2016-02-24 12:23:34 +01:00
// Extrafields
$contact2 -> array_options [ 'options_anazoneliv' ] = price2num ( $fields [ 53 ]);
2017-06-09 09:25:15 +02:00
2016-02-24 12:23:34 +01:00
$ret1 = $contact2 -> create ( $user );
if ( $ret1 > 0 )
{
//$ret2=$contact2->add_contact($object->id, 'SHIPPING');
}
2016-02-23 23:03:52 +01:00
if ( $ret1 < 0 || $ret2 < 0 )
{
print " - Error in create contact result code = " . $ret1 . " " . $ret2 . " - " . $object -> errorsToString ();
$errorrecord ++ ;
}
2017-06-09 09:25:15 +02:00
else
2016-02-23 23:03:52 +01:00
{
print " - create contact OK " ;
}
}
2017-06-09 09:25:15 +02:00
2016-02-23 23:03:52 +01:00
print " \n " ;
2017-06-09 09:25:15 +02:00
if ( $errorrecord )
2016-02-23 23:03:52 +01:00
{
fwrite ( $fhandleerr , 'Error on record nb ' . $i . " - " . $object -> errorsToString () . " \n " );
$error ++ ; // $errorrecord will be reset
}
}
// commit or rollback
2016-02-24 17:27:28 +01:00
print " Nb of lines qualified: " . $nboflines . " \n " ;
print " Nb of errors: " . $error . " \n " ;
2016-02-23 23:03:52 +01:00
if ( $mode != 'confirmforced' && ( $error || $mode != 'confirm' ))
{
print " Rollback any changes. \n " ;
$db -> rollback ();
}
else
{
print " Commit all changes. \n " ;
$db -> commit ();
}
$db -> close ();
fclose ( $fhandle );
fclose ( $fhandleerr );
exit ( $error );