40 public $array_export_code = array();
41 public $array_export_code_for_sort = array();
42 public $array_export_module = array();
43 public $array_export_label = array();
44 public $array_export_sql_start = array();
45 public $array_export_sql_end = array();
46 public $array_export_sql_order = array();
48 public $array_export_fields = array();
49 public $array_export_TypeFields = array();
50 public $array_export_FilterValue = array();
51 public $array_export_entities = array();
52 public $array_export_dependencies = array();
53 public $array_export_special = array();
54 public $array_export_examplevalues = array();
55 public $array_export_help = array();
59 public $hexafiltervalue;
63 public $sqlusedforexport;
88 global $langs, $conf, $mysoc;
90 dol_syslog(get_class($this).
"::load_arrays user=".$user->id.
" filter=".$filter);
95 require_once DOL_DOCUMENT_ROOT.
'/core/lib/functions2.lib.php';
99 foreach ($modulesdir as $dir)
103 if (is_resource($handle))
106 while (($file = readdir($handle)) !==
false)
109 if (is_readable($dir.$file) && preg_match(
"/^(mod.*)\.class\.php$/i", $file, $reg))
111 $modulename = $reg[1];
115 $part = strtolower(preg_replace(
'/^mod/i',
'', $modulename));
116 if ($part ==
'propale') $part =
'propal';
117 if (empty($conf->$part->enabled)) $enabled =
false;
122 $file = $dir.$modulename.
".class.php";
123 $classname = $modulename;
125 $module =
new $classname($this->
db);
127 if (isset($module->export_code) && is_array($module->export_code))
129 foreach ($module->export_code as $r => $value)
132 if ($filter && ($filter != $module->export_code[$r]))
continue;
135 if (!empty($module->export_enabled[$r]) && !
verifCond($module->export_enabled[$r]))
continue;
139 if (isset($module->export_permission))
141 foreach ($module->export_permission[$r] as $val)
145 if (!empty($perm[2])) {
146 $bool = isset($user->rights->{$perm[0]}->{$perm[1]}->{$perm[2]}) ? $user->rights->{$perm[0]}->{$perm[1]}->{$perm[2]} :
false;
148 $bool = isset($user->rights->{$perm[0]}->{$perm[1]}) ? $user->rights->{$perm[0]}->{$perm[1]} :
false;
150 if ($perm[0] ==
'user' && $user->admin) $bool =
true;
160 $langtoload = $module->getLangFilesArray();
161 if (is_array($langtoload))
163 foreach ($langtoload as $key)
171 $this->array_export_module[$i] = $module;
173 $this->array_export_perms[$i] = $bool;
175 $this->array_export_icon[$i] = (isset($module->export_icon[$r]) ? $module->export_icon[$r] : $module->picto);
177 $this->array_export_code[$i] = $module->export_code[$r];
179 $this->array_export_code_for_sort[$i] = $module->module_position.
'_'.$module->export_code[$r];
181 $this->array_export_label[$i] = $module->getExportDatasetLabel($r);
183 $this->array_export_fields[$i] = $module->export_fields_array[$r];
185 $this->array_export_TypeFields[$i] = (isset($module->export_TypeFields_array[$r]) ? $module->export_TypeFields_array[$r] :
'');
187 $this->array_export_entities[$i] = $module->export_entities_array[$r];
189 $this->array_export_dependencies[$i] = (!empty($module->export_dependencies_array[$r]) ? $module->export_dependencies_array[$r] :
'');
191 $this->array_export_special[$i] = (!empty($module->export_special_array[$r]) ? $module->export_special_array[$r] :
'');
193 $this->array_export_examplevalues[$i] = (!empty($module->export_examplevalues_array[$r]) ? $module->export_examplevalues_array[$r] : null);
195 $this->array_export_help[$i] = (!empty($module->export_help_array[$r]) ? $module->export_help_array[$r] :
'');
198 $this->array_export_sql_start[$i] = $module->export_sql_start[$r];
199 $this->array_export_sql_end[$i] = $module->export_sql_end[$r];
200 $this->array_export_sql_order[$i] = (!empty($module->export_sql_order[$r]) ? $module->export_sql_order[$r] : null);
203 dol_syslog(get_class($this).
"::load_arrays loaded for module ".$modulename.
" with index ".$i.
", dataset=".$module->export_code[$r].
", nb of fields=".(!empty($module->export_fields_code[$r]) ?count($module->export_fields_code[$r]) :
''));
229 public function build_sql($indice, $array_selected, $array_filterValue)
233 $sql = $this->array_export_sql_start[$indice];
237 foreach ($this->array_export_fields[$indice] as $key => $value)
239 if (!array_key_exists($key, $array_selected))
continue;
240 if (preg_match(
'/^none\./', $key))
continue;
241 if ($i > 0) $sql .=
', ';
244 if (strpos($key,
' as ') ===
false) {
245 $newfield = $key.
' as '.str_replace(array(
'.',
'-',
'(',
')'),
'_', $key);
252 $sql .= $this->array_export_sql_end[$indice];
255 if (is_array($array_filterValue) && !empty($array_filterValue))
259 foreach ($array_filterValue as $key => $value)
261 if (preg_match(
'/GROUP_CONCAT/i', $key))
continue;
262 if ($value !=
'') $sqlWhere .=
" and ".$this->build_filterQuery($this->array_export_TypeFields[$indice][$key], $key, $array_filterValue[$key]);
268 $sql .= $this->array_export_sql_order[$indice];
271 if (is_array($array_filterValue) && !empty($array_filterValue))
274 foreach ($array_filterValue as $key => $value)
276 if (preg_match(
'/GROUP_CONCAT/i', $key) and $value !=
'') $sql .=
" HAVING ".$this->build_filterQuery($this->array_export_TypeFields[$indice][$key], $key, $array_filterValue[$key]);
296 $InfoFieldList = explode(
":", $TypeField);
298 switch ($InfoFieldList[0]) {
300 if (!(strpos($ValueField,
'%') ===
false))
301 $szFilterQuery .=
" ".$NameField.
" LIKE '".$ValueField.
"'";
302 else $szFilterQuery .=
" ".$NameField.
" = '".$ValueField.
"'";
305 if (strpos($ValueField,
"+") > 0)
308 $ValueArray = explode(
"+", $ValueField);
309 $szFilterQuery =
"(".$this->conditionDate($NameField, trim($ValueArray[0]),
">=");
310 $szFilterQuery .=
" AND ".$this->conditionDate($NameField, trim($ValueArray[1]),
"<=").
")";
312 if (is_numeric(substr($ValueField, 0, 1)))
313 $szFilterQuery = $this->conditionDate($NameField, trim($ValueField),
"=");
314 else $szFilterQuery = $this->conditionDate($NameField, trim(substr($ValueField, 1)), substr($ValueField, 0, 1));
321 if (strpos($ValueField,
"+") > 0)
324 $ValueArray = explode(
"+", $ValueField);
325 $szFilterQuery =
"(".$NameField.
">=".$ValueArray[0];
326 $szFilterQuery .=
" AND ".$NameField.
"<=".$ValueArray[1].
")";
328 if (is_numeric(substr($ValueField, 0, 1)))
329 $szFilterQuery =
" ".$NameField.
"=".$ValueField;
330 else $szFilterQuery =
" ".$NameField.substr($ValueField, 0, 1).substr($ValueField, 1);
334 $szFilterQuery =
" ".$NameField.
"=".(is_numeric($ValueField) ? $ValueField : ($ValueField ==
'yes' ? 1 : 0));
338 if (is_numeric($ValueField))
339 $szFilterQuery =
" ".$NameField.
"=".$ValueField;
341 if (!(strpos($ValueField,
'%') ===
false))
342 $szFilterQuery =
" ".$NameField.
" LIKE '".$ValueField.
"'";
343 else $szFilterQuery =
" ".$NameField.
" = '".$ValueField.
"'";
347 dol_syslog(
"Error we try to forge an sql export request with a condition on a field with type ".$InfoFieldList[0].
" (defined into module descriptor) but this type is unknown/not supported. It looks like a bug into module descriptor.", LOG_ERR);
350 return $szFilterQuery;
364 if (strlen($Value) == 4) $Condition =
" date_format(".$Field.
",'%Y') ".$Sens.
" '".$Value.
"'";
365 elseif (strlen($Value) == 6) $Condition =
" date_format(".$Field.
",'%Y%m') ".$Sens.
" '".$Value.
"'";
366 else $Condition =
" date_format(".$Field.
",'%Y%m%d') ".$Sens.
" ".$Value;
382 global $conf, $langs;
385 $InfoFieldList = explode(
":", $TypeField);
388 switch ($InfoFieldList[0])
392 $szFilterField =
'<input type="text" name="'.$NameField.
'" value="'.$ValueField.
'">';
398 $szFilterField =
'<input type="text" size="6" name="'.$NameField.
'" value="'.$ValueField.
'">';
401 $szFilterField =
'<input type="number" size="6" name="'.$NameField.
'" value="'.$ValueField.
'">';
404 $szFilterField =
'<select name="'.$NameField.
'" class="flat">';
405 $szFilterField .=
'<option ';
406 if ($ValueField ==
'') $szFilterField .=
' selected ';
407 $szFilterField .=
' value=""> </option>';
409 $szFilterField .=
'<option ';
410 if ($ValueField ==
'yes' || $ValueField ==
'1') $szFilterField .=
' selected ';
411 $szFilterField .=
' value="1">'.yn(1).
'</option>';
413 $szFilterField .=
'<option ';
414 if ($ValueField ==
'no' || $ValueField ==
'0') $szFilterField .=
' selected ';
415 $szFilterField .=
' value="0">'.yn(0).
'</option>';
416 $szFilterField .=
"</select>";
425 if (!empty($InfoFieldList[3]))
426 $keyList = $InfoFieldList[3];
427 else $keyList =
'rowid';
428 $sql =
'SELECT '.$keyList.
' as rowid, '.$InfoFieldList[2].
' as label'.(empty($InfoFieldList[3]) ?
'' :
', '.$InfoFieldList[3].
' as code');
429 if ($InfoFieldList[1] ==
'c_stcomm') $sql =
'SELECT id as id, '.$keyList.
' as rowid, '.$InfoFieldList[2].
' as label'.(empty($InfoFieldList[3]) ?
'' :
', '.$InfoFieldList[3].
' as code');
430 if ($InfoFieldList[1] ==
'c_country') $sql =
'SELECT '.$keyList.
' as rowid, '.$InfoFieldList[2].
' as label, code as code';
431 $sql .=
' FROM '.MAIN_DB_PREFIX.$InfoFieldList[1];
432 if (!empty($InfoFieldList[4])) {
433 $sql .=
' WHERE entity IN ('.getEntity($InfoFieldList[4]).
')';
439 $szFilterField =
'<select class="flat" name="'.$NameField.
'">';
440 $szFilterField .=
'<option value="0"> </option>';
448 $obj = $this->
db->fetch_object(
$resql);
449 if ($obj->label ==
'-')
456 $labeltoshow =
dol_trunc($obj->label, 18);
457 if ($InfoFieldList[1] ==
'c_stcomm')
459 $langs->load(
"companies");
460 $labeltoshow = (($langs->trans(
"StatusProspect".$obj->id) !=
"StatusProspect".$obj->id) ? $langs->trans(
"StatusProspect".$obj->id) : $obj->label);
462 if ($InfoFieldList[1] ==
'c_country')
465 $langs->load(
"dict");
466 $labeltoshow = (($langs->trans(
"Country".$obj->code) !=
"Country".$obj->code) ? $langs->trans(
"Country".$obj->code) : $obj->label);
468 if (!empty($ValueField) && $ValueField == $obj->rowid)
470 $szFilterField .=
'<option value="'.$obj->rowid.
'" selected>'.$labeltoshow.
'</option>';
472 $szFilterField .=
'<option value="'.$obj->rowid.
'" >'.$labeltoshow.
'</option>';
477 $szFilterField .=
"</select>";
484 return $szFilterField;
498 $InfoFieldList = explode(
":", $TypeField);
500 switch ($InfoFieldList[0]) {
502 $szMsg = $langs->trans(
'ExportStringFilter');
505 $szMsg = $langs->trans(
'ExportDateFilter');
510 $szMsg = $langs->trans(
'ExportNumericFilter');
534 public function build_file($user, $model, $datatoexport, $array_selected, $array_filterValue, $sqlquery =
'')
537 global $conf, $langs, $mysoc;
540 asort($array_selected);
542 dol_syslog(__METHOD__.
" ".$model.
", ".$datatoexport.
", ".implode(
",", $array_selected));
545 if (empty($this->array_export_fields) || !is_array($this->array_export_fields))
547 $this->error =
"ErrorBadParameter";
553 $dir = DOL_DOCUMENT_ROOT.
"/core/modules/export/";
554 $file =
"export_".$model.
".modules.php";
555 $classname =
"Export".$model;
556 require_once $dir.$file;
557 $objmodel =
new $classname($this->
db);
559 if (!empty($sqlquery)) $sql = $sqlquery;
563 foreach ($this->array_export_code as $key => $dataset)
565 if ($datatoexport == $dataset)
573 if (empty($foundindice))
575 $this->error =
"ErrorBadParameter can't find dataset ".$datatoexport.
" into preload arrays this->array_export_code";
578 $sql = $this->build_sql($indice, $array_selected, $array_filterValue);
582 $this->sqlusedforexport = $sql;
588 if (!empty($conf->global->EXPORT_PREFIX_SPEC)) {
589 $filename = $conf->global->EXPORT_PREFIX_SPEC.
"_".$datatoexport;
592 $filename =
"export_".$datatoexport;
594 $filename .=
'.'.$objmodel->getDriverExtension();
595 $dirname = $conf->export->dir_temp.
'/'.$user->id;
597 $outputlangs = clone $langs;
601 $result = $objmodel->open_file($dirname.
"/".$filename, $outputlangs);
606 $objmodel->write_header($outputlangs);
609 $objmodel->write_title($this->array_export_fields[$indice], $array_selected, $outputlangs, isset($this->array_export_TypeFields[$indice]) ? $this->array_export_TypeFields[$indice] : null);
611 while ($obj = $this->
db->fetch_object(
$resql))
614 if (!empty($this->array_export_special[$indice]))
616 foreach ($this->array_export_special[$indice] as $key => $value)
618 if (!array_key_exists($key, $array_selected))
continue;
620 if ($this->array_export_special[$indice][$key] ==
'NULLIFNEG')
623 $alias = str_replace(array(
'.',
'-',
'(',
')'),
'_', $key);
624 if ($obj->$alias < 0) $obj->$alias =
'';
627 elseif ($this->array_export_special[$indice][$key] ==
'ZEROIFNEG')
630 $alias = str_replace(array(
'.',
'-',
'(',
')'),
'_', $key);
631 if ($obj->$alias < 0) $obj->$alias =
'0';
634 elseif ($this->array_export_special[$indice][$key] ==
'getNumOpenDays')
636 include_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
638 $alias = str_replace(array(
'.',
'-',
'(',
')'),
'_', $key);
642 elseif ($this->array_export_special[$indice][$key] ==
'getRemainToPay')
645 $alias = str_replace(array(
'.',
'-',
'(',
')'),
'_', $key);
647 if ($obj->f_rowid > 0)
649 global $tmpobjforcomputecall;
650 if (!is_object($tmpobjforcomputecall))
652 include_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
653 $tmpobjforcomputecall =
new Facture($this->
db);
655 $tmpobjforcomputecall->id = $obj->f_rowid;
656 $tmpobjforcomputecall->total_ttc = $obj->f_total_ttc;
657 $tmpobjforcomputecall->close_code = $obj->f_close_code;
658 $remaintopay = $tmpobjforcomputecall->getRemainToPay();
660 $obj->$alias = $remaintopay;
665 $computestring = $this->array_export_special[$indice][$key];
669 $this->error =
"ERROPNOTSUPPORTED. Operation ".$computestring.
" not supported. Export of 'computed' extrafields is not yet supported, please remove field.";
675 $objmodel->write_record($array_selected, $obj, $outputlangs, isset($this->array_export_TypeFields[$indice]) ? $this->array_export_TypeFields[$indice] : null);
679 $objmodel->write_footer($outputlangs);
682 $objmodel->close_file();
686 $this->error = $objmodel->error;
687 dol_syslog(
"Export::build_file Error: ".$this->error, LOG_ERR);
691 $this->error = $this->
db->error().
" - sql=".$sql;
712 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.
'export_model (';
718 $sql .=
') VALUES (';
719 $sql .=
"'".$this->db->escape($this->model_name).
"',";
720 $sql .=
" '".$this->db->escape($this->datatoexport).
"',";
721 $sql .=
" '".$this->db->escape($this->hexa).
"',";
722 $sql .=
' '.($user->id > 0 ? $user->id :
'null').
",";
723 $sql .=
" '".$this->db->escape($this->hexafiltervalue).
"'";
726 dol_syslog(get_class($this).
"::create", LOG_DEBUG);
733 $this->error = $this->
db->lasterror();
734 $this->errno = $this->
db->lasterrno();
735 $this->
db->rollback();
748 $sql =
'SELECT em.rowid, em.label, em.type, em.field, em.filter';
749 $sql .=
' FROM '.MAIN_DB_PREFIX.
'export_model as em';
750 $sql .=
' WHERE em.rowid = '.((int) $id);
753 $result = $this->
db->query($sql);
756 $obj = $this->
db->fetch_object($result);
759 $this->
id = $obj->rowid;
760 $this->model_name = $obj->label;
761 $this->datatoexport = $obj->type;
763 $this->hexa = $obj->field;
764 $this->hexafiltervalue = $obj->filter;
768 $this->error =
"ModelNotFound";
785 public function delete($user, $notrigger = 0)
787 global $conf, $langs;
790 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"export_model";
791 $sql .=
" WHERE rowid=".$this->id;
795 dol_syslog(get_class($this).
"::delete", LOG_DEBUG);
797 if (!
$resql) { $error++; $this->errors[] =
"Error ".$this->db->lasterror(); }
802 foreach ($this->errors as $errmsg)
804 dol_syslog(get_class($this).
"::delete ".$errmsg, LOG_ERR);
805 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
807 $this->
db->rollback();
825 global $conf, $langs;
827 $sql =
"SELECT em.rowid, em.field, em.label, em.type, em.filter";
828 $sql .=
" FROM ".MAIN_DB_PREFIX.
"export_model as em";
829 $sql .=
" ORDER BY rowid";
831 $result = $this->
db->query($sql);
834 $num = $this->
db->num_rows($result);
838 $obj = $this->
db->fetch_object($result);
839 $keyModel = array_search($obj->type, $this->array_export_code);
841 print '<td><a href=export.php?step=2&action=select_model&exportmodelid='.$obj->rowid.
'&datatoexport='.$obj->type.
'>'.$obj->label.
'</a></td>';
843 print img_object($this->array_export_module[$keyModel]->getName(), $this->array_export_icon[$keyModel]).
' ';
844 print $this->array_export_module[$keyModel]->getName().
' - ';
847 $string = $langs->trans($this->array_export_label[$keyModel]);
848 print ($string != $this->array_export_label[$keyModel] ? $string : $this->array_export_label[$keyModel]);
851 print '<td>'.str_replace(
',',
' , ', $obj->field).
'</td>';
852 if (!empty($obj->filter)) {
853 $filter = json_decode($obj->filter,
true);
854 print '<td>'.str_replace(
',',
' , ', $filter[
'field']).
'</td>';
855 print '<td>'.str_replace(
',',
' , ', $filter[
'value']).
'</td>';
858 print '<td class="right">';
859 print '<a href="'.$_SERVER[
"PHP_SELF"].
'?action=deleteprof&token='.
newToken().
'&id='.$obj->rowid.
'">';
verifCond($strRights)
Verify if condition in string is ok or not.
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
dolGetModulesDirs($subdir= '')
Return list of modules directories.
conditionDate($Field, $Value, $Sens)
conditionDate
dol_stringtotime($string, $gm=1)
Convert a string date into a GM Timestamps date Warning: YYYY-MM-DDTHH:MM:SS+02:00 (RFC3339) is not s...
build_filterQuery($TypeField, $NameField, $ValueField)
Build the conditionnal string from filter the query.
__construct($db)
Constructor.
build_filterField($TypeField, $NameField, $ValueField)
Build an input field used to filter the query.
fetch($id)
Load an export profil from database.
$conf db
API class for accounts.
build_sql($indice, $array_selected, $array_filterValue)
Build the sql export request.
genDocFilter($TypeField)
Build an input field used to filter the query.
load_arrays($user, $filter= '')
Load an exportable dataset.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
num_open_day($timestampStart, $timestampEnd, $inhour=0, $lastday=0, $halfday=0, $country_code= '')
Function to return number of working days (and text of units) between two dates (working days) ...
img_object($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
list_export_model()
Output list all export models TODO Move this into a class htmlxxx.class.php.
print
Draft customers invoices.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
build_file($user, $model, $datatoexport, $array_selected, $array_filterValue, $sqlquery= '')
Build export file.
newToken()
Return the value of token currently saved into session with name 'newtoken'.
dol_trunc($string, $size=40, $trunc= 'right', $stringencoding= 'UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '...' if string larger than length.
Class to manage invoices.
img_delete($titlealt= 'default', $other= 'class="pictodelete"', $morecss= '')
Show delete logo.
if(!defined('CSRFCHECK_WITH_TOKEN')) define('CSRFCHECK_WITH_TOKEN'
Draft customers invoices.
create($user)
Save an export model in database.
dol_mkdir($dir, $dataroot= '', $newmask=null)
Creation of a directory (this can create recursive subdir)