From d5112c0c820cace60269d97e17506c91e43c8617 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Wed, 24 Feb 2016 12:23:34 +0100 Subject: [PATCH] Better sample import scripts --- dev/initdata/import-thirdparties.php | 99 ++++++++++++++++++++++------ dev/initdata/import-users.php | 1 - dev/initdata/purge-data.php | 64 +++++++++++++----- 3 files changed, 126 insertions(+), 38 deletions(-) diff --git a/dev/initdata/import-thirdparties.php b/dev/initdata/import-thirdparties.php index 1b4f9f57df0..53f03ff0d98 100755 --- a/dev/initdata/import-thirdparties.php +++ b/dev/initdata/import-thirdparties.php @@ -131,29 +131,35 @@ while ($fields=fgetcsv($fhandle, $linelength, $delimiter, $enclosure, $escape)) $object->address = trim($fields[14]); $object->zip = trim($fields[15]); $object->town = trim($fields[16]); - $object->country_code = trim($fields[22]); - $object->phone = trim($fields[23]); - $object->fax = trim($fields[24]); + $object->country_id = dol_getIdFromCode($db, trim($fields[21]), 'c_country', 'code', 'rowid'); + $object->phone = trim($fields[22]); + $object->fax = trim($fields[23]); $object->email = trim($fields[26]); - $object->siret = trim($fields[29]); + $object->idprof2 = trim($fields[29]); $object->tva_intra = trim($fields[34]); $object->default_lang = trim($fields[43]); - + + //$condpayment = dol_string_unaccent(trim($fields[36])); $condpayment = trim($fields[36]); - $object->cond_reglement_id = dol_getIdFromCode($db, $condpayment, 'c_paiement_term', 'label'); - + $object->cond_reglement_id = dol_getIdFromCode($db, $condpayment, 'c_payment_term', 'libelle_facture', 'rowid'); + dol_syslog("cond_reglement id=".$object->cond_reglement_id); + $object->code_client = $fields[9]; $object->code_fournisseur = $fields[10]; $labeltype = trim($fields[1]); - $object->typent_id = dol_getIdFromCode($db, $labeltype, 'c_typent', 'label'); + $object->typent_id = dol_getIdFromCode($db, $labeltype, 'c_typent', 'libelle'); + // Set price level + $object->price_level = 1; + if ($labeltype == 'Revendeur') $object->price_level = 2; + print "Process line nb ".$i.", name ".$object->name; // Extrafields - //$object->array_options['options_state']=price2num($fields[20]); - //$object->array_options['options_region']=price2num($fields[18]); + $object->array_options['options_anastate']=price2num($fields[20]); + $object->array_options['options_anaregion']=price2num($fields[17]); $ret=$object->create($user); if ($ret < 0) @@ -166,15 +172,54 @@ while ($fields=fgetcsv($fhandle, $linelength, $delimiter, $enclosure, $escape)) print " - Creation OK with name ".$object->name." - id = ".$ret; } - dol_syslog("Add contacts"); + dol_syslog("Set price level"); + $object->set_price_level($object->price_level, $user); - // Insert an invoice contact if there is an invoice email != standard email + // Assign sales representative + if ($fields[3]) + { + $salesrep=new User($db); + + $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); + + $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"; + } + } + + dol_syslog("Add invoice contacts"); + // Insert an invoice contact if there is an invoice email != standard email if (! $errorrecord && $fields[27] && $fields[26] != $fields[27]) { + $ret1=$ret2=0; + $contact = new Contact($db); - $contact->firstname = ''; - $contact->lastname = ''; - + $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; + + $ret1=$contact->create($user); + if ($ret1 > 0) + { + //$ret2=$contact->add_contact($object->id, 'BILLING'); + } if ($ret1 < 0 || $ret2 < 0) { print " - Error in create contact result code = ".$ret1." ".$ret2." - ".$object->errorsToString(); @@ -186,13 +231,29 @@ while ($fields=fgetcsv($fhandle, $linelength, $delimiter, $enclosure, $escape)) } } + dol_syslog("Add delivery contacts"); // Insert a delivery contact if (! $errorrecord && 1) { - $contact = new Contact($db); - $contact->firstname = ''; - $contact->lastname = ''; - + $ret1=$ret2=0; + + $contact2 = new Contact($db); + $contact2->lastname = $fields[47]; + $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; + + // Extrafields + $contact2->array_options['options_anazoneliv']=price2num($fields[53]); + + $ret1=$contact2->create($user); + if ($ret1 > 0) + { + //$ret2=$contact2->add_contact($object->id, 'SHIPPING'); + } if ($ret1 < 0 || $ret2 < 0) { print " - Error in create contact result code = ".$ret1." ".$ret2." - ".$object->errorsToString(); diff --git a/dev/initdata/import-users.php b/dev/initdata/import-users.php index 0b3566423fd..61896cae8b7 100755 --- a/dev/initdata/import-users.php +++ b/dev/initdata/import-users.php @@ -124,7 +124,6 @@ while ($fields=fgetcsv($fhandle, $linelength, $delimiter, $enclosure, $escape)) $object->statut = 1; $tmp=explode(' ',$fields[3],2); - $object->firstname = trim($tmp[0]); $object->lastname = trim($tmp[1]); if ($object->lastname) $object->login = strtolower(substr($object->firstname, 0, 1)) . strtolower(substr($object->lastname, 0)); diff --git a/dev/initdata/purge-data.php b/dev/initdata/purge-data.php index 82448a5d857..64054a7a798 100755 --- a/dev/initdata/purge-data.php +++ b/dev/initdata/purge-data.php @@ -124,6 +124,7 @@ $sqls=array( 'DELETE FROM '.MAIN_DB_PREFIX.'socpeople', ), 'thirdparty'=>array( + '@contact', 'DELETE FROM '.MAIN_DB_PREFIX.'cabinetmed_cons', 'UPDATE '.MAIN_DB_PREFIX.'adherent SET fk_soc = NULL', 'DELETE FROM '.MAIN_DB_PREFIX.'categorie_fournisseur', @@ -184,6 +185,46 @@ if (! $confirmed) } +/** + * Process sql requests of a family + */ +function processfamily($family) +{ + global $db, $sqls; + + $error=0; + foreach($sqls[$family] as $sql) + { + if (preg_match('/^@/',$sql)) + { + $newfamily=preg_replace('/@/','',$sql); + processfamily($newfamily); + continue; + } + + print "Run sql: ".$sql."\n"; + $resql=$db->query($sql); + if (! $resql) + { + if ($db->errno() != 'DB_ERROR_NOSUCHTABLE') + { + $error++; + } + } + + if ($error) + { + print $db->lasterror(); + $error++; + break; + } + } + + if ($error) return -1; + else return 1; +} + + $db->begin(); $oldfamily=''; @@ -193,25 +234,12 @@ foreach($sqls as $family => $familysql) if ($family != $oldfamily) print "Process action for family ".$family."\n"; $oldfamily = $family; - - foreach($familysql as $sql) + + $result=processfamily($family); + if ($result < 0) { - print "Run sql: ".$sql."\n"; - $resql=$db->query($sql); - if (! $resql) - { - if ($db->errno() != 'DB_ERROR_NOSUCHTABLE') - { - $error++; - } - } - - if ($error) - { - print $db->lasterror(); - $error++; - break 2; - } + $error++; + break; } }