2
0
forked from Wavyzz/dolibarr

NEW : Add module resources import/export

This commit is contained in:
florian HENRY
2017-04-18 15:25:04 +02:00
parent 3da0aea168
commit a52db08cd6
3 changed files with 561 additions and 107 deletions

View File

@@ -201,22 +201,6 @@ class modResource extends DolibarrModules
$this->menu = 1; // This module add menu entries. They are coded into menu manager.
// Add here list of permission defined by
// an id, a label, a boolean and two constant strings.
// Example:
//// Permission id (must not be already used)
//$this->rights[$r][0] = 2000;
//// Permission label
//$this->rights[$r][1] = 'Permision label';
//// Permission by default for new user (0/1)
//$this->rights[$r][3] = 0;
//// In php code, permission will be checked by test
//// if ($user->rights->permkey->level1->level2)
//$this->rights[$r][4] = 'level1';
//// In php code, permission will be checked by test
//// if ($user->rights->permkey->level1->level2)
//$this->rights[$r][5] = 'level2';
//$r++;
// Main menu entries
$this->menu = array(); // List of menus to add
$r = 0;
@@ -267,98 +251,63 @@ class modResource extends DolibarrModules
'user'=> 0
);
// Exports
$r = 1;
// Example:
//$this->export_code[$r]=$this->rights_class.'_'.$r;
//// Translation key (used only if key ExportDataset_xxx_z not found)
//$this->export_label[$r]='CustomersInvoicesAndInvoiceLines';
//// Condition to show export in list (ie: '$user->id==3').
//// Set to 1 to always show when module is enabled.
//$this->export_enabled[$r]='1';
//$this->export_permission[$r]=array(array("facture","facture","export"));
//$this->export_fields_array[$r]=array(
// 's.rowid'=>"IdCompany",
// 's.nom'=>'CompanyName',
// 's.address'=>'Address',
// 's.cp'=>'Zip',
// 's.ville'=>'Town',
// 's.fk_pays'=>'Country',
// 's.tel'=>'Phone',
// 's.siren'=>'ProfId1',
// 's.siret'=>'ProfId2',
// 's.ape'=>'ProfId3',
// 's.idprof4'=>'ProfId4',
// 's.code_compta'=>'CustomerAccountancyCode',
// 's.code_compta_fournisseur'=>'SupplierAccountancyCode',
// 'f.rowid'=>"InvoiceId",
// 'f.facnumber'=>"InvoiceRef",
// 'f.datec'=>"InvoiceDateCreation",
// 'f.datef'=>"DateInvoice",
// 'f.total'=>"TotalHT",
// 'f.total_ttc'=>"TotalTTC",
// 'f.tva'=>"TotalVAT",
// 'f.paye'=>"InvoicePaid",
// 'f.fk_statut'=>'InvoiceStatus',
// 'f.note'=>"InvoiceNote",
// 'fd.rowid'=>'LineId',
// 'fd.description'=>"LineDescription",
// 'fd.price'=>"LineUnitPrice",
// 'fd.tva_tx'=>"LineVATRate",
// 'fd.qty'=>"LineQty",
// 'fd.total_ht'=>"LineTotalHT",
// 'fd.total_tva'=>"LineTotalTVA",
// 'fd.total_ttc'=>"LineTotalTTC",
// 'fd.date_start'=>"DateStart",
// 'fd.date_end'=>"DateEnd",
// 'fd.fk_product'=>'ProductId',
// 'p.ref'=>'ProductRef'
//);
//$this->export_entities_array[$r]=array('s.rowid'=>"company",
// 's.nom'=>'company',
// 's.address'=>'company',
// 's.cp'=>'company',
// 's.ville'=>'company',
// 's.fk_pays'=>'company',
// 's.tel'=>'company',
// 's.siren'=>'company',
// 's.siret'=>'company',
// 's.ape'=>'company',
// 's.idprof4'=>'company',
// 's.code_compta'=>'company',
// 's.code_compta_fournisseur'=>'company',
// 'f.rowid'=>"invoice",
// 'f.facnumber'=>"invoice",
// 'f.datec'=>"invoice",
// 'f.datef'=>"invoice",
// 'f.total'=>"invoice",
// 'f.total_ttc'=>"invoice",
// 'f.tva'=>"invoice",
// 'f.paye'=>"invoice",
// 'f.fk_statut'=>'invoice',
// 'f.note'=>"invoice",
// 'fd.rowid'=>'invoice_line',
// 'fd.description'=>"invoice_line",
// 'fd.price'=>"invoice_line",
// 'fd.total_ht'=>"invoice_line",
// 'fd.total_tva'=>"invoice_line",
// 'fd.total_ttc'=>"invoice_line",
// 'fd.tva_tx'=>"invoice_line",
// 'fd.qty'=>"invoice_line",
// 'fd.date_start'=>"invoice_line",
// 'fd.date_end'=>"invoice_line",
// 'fd.fk_product'=>'product',
// 'p.ref'=>'product'
//);
//$this->export_sql_start[$r] = 'SELECT DISTINCT ';
//$this->export_sql_end[$r] = ' FROM (' . MAIN_DB_PREFIX . 'facture as f, '
// . MAIN_DB_PREFIX . 'facturedet as fd, ' . MAIN_DB_PREFIX . 'societe as s)';
//$this->export_sql_end[$r] .= ' LEFT JOIN ' . MAIN_DB_PREFIX
// . 'product as p on (fd.fk_product = p.rowid)';
//$this->export_sql_end[$r] .= ' WHERE f.fk_soc = s.rowid '
// . 'AND f.rowid = fd.fk_facture';
//$r++;
// Exports
//--------
$r=0;
$r++;
$this->export_code[$r]=$this->rights_class.'_'.$r;
$this->export_label[$r]="ResourceSingular"; // Translation key (used only if key ExportDataset_xxx_z not found)
$this->export_permission[$r]=array(array("resource","read"));
$this->export_fields_array[$r]=array('r.rowid'=>'IdResource','r.ref'=>'ResourceFormLabel_ref','c.code'=>'ResourceTypeCode','c.label'=>'ResourceType','r.description'=>'ResourceFormLabel_description','r.note_private'=>"NotePrivate",'r.note_public'=>"NotePublic",'r.asset_number'=>'AssetNumber','r.datec'=>"DateCreation",'r.tms'=>"DateLastModification");
$this->export_TypeFields_array[$r]=array('r.rowid'=>'List:resource:ref','r.ref'=>'Text','r.asset_number'=>'Text','r.description'=>'Text','c.code'=>'Text','c.label'=>'List:c_type_resource:label','r.datec'=>'Date','r.tms'=>'Date','r.note_private'=>'Text','r.note_public'=>'Text');
$this->export_entities_array[$r]=array('r.rowid'=>'resource','r.ref'=>'resource','c.code'=>'resource','c.label'=>'resource','r.description'=>'resource','r.note_private'=>"resource",'r.resource'=>"resource",'r.asset_number'=>'resource','r.datec'=>"resource",'r.tms'=>"resource");
$keyforselect='resource'; $keyforelement='resource'; $keyforaliasextra='extra';
include DOL_DOCUMENT_ROOT.'/core/extrafieldsinexport.inc.php';
$this->export_dependencies_array[$r]=array('resource'=>array('r.rowid')); // We must keep this until the aggregate_array is used. To add unique key if we ask a field of a child to avoid the DISTINCT to discard them.
$this->export_sql_start[$r]='SELECT DISTINCT ';
$this->export_sql_end[$r] =' FROM '.MAIN_DB_PREFIX.'resource as r ';
$this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'c_type_resource as c ON c.rowid=r.fk_code_type_resource';
$this->export_sql_end[$r] .=' LEFT JOIN '.MAIN_DB_PREFIX.'resource_extrafields as extra ON extra.fk_object = c.rowid';
$this->export_sql_end[$r] .=' AND r.entity IN ('.getEntity('resource',1).')';
// Imports
//--------
$r=0;
// Import list of third parties and attributes
$r++;
$this->import_code[$r]=$this->rights_class.'_'.$r;
$this->import_label[$r]='ImportDataset_resource_1';
$this->import_icon[$r]='resource';
$this->import_entities_array[$r]=array(); // We define here only fields that use another icon that the one defined into import_icon
$this->import_tables_array[$r]=array('r'=>MAIN_DB_PREFIX.'resource','extra'=>MAIN_DB_PREFIX.'resource_extrafields'); // List of tables to insert into (insert done in same order)
$this->import_fields_array[$r]=array('r.ref'=>"ResourceFormLabel_ref*",'r.fk_code_type_resource'=>'ResourceTypeCode','r.description'=>'ResourceFormLabel_description','r.note_private'=>"NotePrivate",'r.note_public'=>"NotePublic",'r.asset_number'=>'AssetNumber','r.datec'=>'DateCreation');
// Add extra fields
$sql="SELECT name, label, fieldrequired FROM ".MAIN_DB_PREFIX."extrafields WHERE elementtype = 'resource' AND entity = ".$conf->entity;
$resql=$this->db->query($sql);
if ($resql) // This can fail when class is used on old database (during migration for example)
{
while ($obj=$this->db->fetch_object($resql))
{
$fieldname='extra.'.$obj->name;
$fieldlabel=ucfirst($obj->label);
$this->import_fields_array[$r][$fieldname]=$fieldlabel.($obj->fieldrequired?'*':'');
}
}
// End add extra fields
$this->import_fieldshidden_array[$r]=array('r.fk_user_author'=>'user->id','extra.fk_object'=>'lastrowid-'.MAIN_DB_PREFIX.'resource'); // aliastable.field => ('user->id' or 'lastrowid-'.tableparent)
$this->import_convertvalue_array[$r]=array(
'r.fk_code_type_resource'=>array('rule'=>'fetchidfromcodeorlabel','classfile'=>'/core/class/ctyperesource.class.php','class'=>'Ctyperesource','method'=>'fetch','dict'=>'DictionaryResourceType'),
);
//$this->import_convertvalue_array[$r]=array('s.fk_soc'=>array('rule'=>'lastrowid',table='t');
$this->import_regex_array[$r]=array('s.datec'=>'^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]( [0-9][0-9]:[0-9][0-9]:[0-9][0-9])?$');
$this->import_examplevalues_array[$r]=array('r.ref'=>"REF1",'r.fk_code_type_resource'=>"Code from dictionnary resource type",'r.datec'=>"2017-01-01 or 2017-01-01 12:30:00");
$this->import_updatekeys_array[$r]=array('r.rf'=>'ResourceFormLabel_ref');
}
/**