502 global $user, $langs, $conf,
$mysoc, $hookmanager;
504 if (empty($srctemplatepath)) {
505 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
510 if (!is_object($hookmanager)) {
511 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
514 $hookmanager->initHooks(array(
'odtgeneration'));
517 if (!is_object($outputlangs)) {
518 $outputlangs = $langs;
520 $sav_charset_output = $outputlangs->charset_output;
521 $outputlangs->charset_output =
'UTF-8';
524 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"projects"));
526 if ($conf->project->dir_output) {
540 $dir = $conf->project->dir_output;
542 if (!preg_match(
'/specimen/i', $objectref)) {
543 $dir .=
"/".$objectref;
545 $file = $dir.
"/".$objectref.
".odt";
547 if (!file_exists($dir)) {
549 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
554 if (file_exists($dir)) {
556 $newfile = basename($srctemplatepath);
557 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
558 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
559 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
560 $newfiletmp = $objectref .
'_' . $newfiletmp;
563 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
566 if ($format ==
'1') {
567 $format =
'%Y%m%d%H%M%S';
571 $filename = $newfiletmp .
'.' . $newfileformat;
573 $file = $dir .
'/' . $filename;
580 if (!is_writable($conf->project->dir_temp)) {
581 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->project->dir_temp);
582 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
588 $arrayidcontact =
$object->getIdContact(
'external',
'PROJECTLEADER');
589 if (count($arrayidcontact) > 0) {
591 $result =
$object->fetch_contact($arrayidcontact[0]);
595 $contactobject =
null;
596 if (!empty($usecontact)) {
598 $contactobject =
$object->contact;
601 $socobject =
$object->thirdparty;
604 $substitutionarray = array(
605 '__FROM_NAME__' => $this->emetteur->name,
606 '__FROM_EMAIL__' => $this->emetteur->email,
610 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$substitutionarray);
611 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
614 require_once ODTPHP_PATH.
'odf.php';
616 $odfHandler =
new Odf(
619 'PATH_TO_TMP' => $conf->project->dir_temp,
621 'DELIMITER_LEFT' =>
'{',
622 'DELIMITER_RIGHT' =>
'}'
626 $this->error = $e->getMessage();
635 $substitutionarray = getCommonSubstitutionArray($outputlangs, 0,
null,
$object);
643 $array_project_contact = array();
644 if ($usecontact && is_object($contactobject)) {
648 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_project_contact);
652 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
653 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
662 foreach ($tmparray as $key => $value) {
664 if (preg_match(
'/logo$/', $key)) {
665 if (file_exists($value)) {
666 $odfHandler->setImage($key, $value, $ratio);
668 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
671 $odfHandler->setVars($key, $value,
true,
'UTF-8');
673 }
catch (OdfException $e) {
680 $listlines = $odfHandler->setSegment(
'tasks');
682 $taskstatic =
new Task($this->db);
690 $tasksarray = $taskstatic->getTasksArray(
null,
null,
$object->id, $socid, 0);
693 foreach ($tasksarray as $task) {
696 foreach ($tmparray as $key => $val) {
698 $listlines->setVars($key, $val,
true,
'UTF-8');
699 }
catch (SegmentException $e) {
704 $taskobj =
new Task($this->db);
705 $taskobj->fetch($task->id);
708 $sourcearray = array(
'internal',
'external');
709 $contact_arrray = array();
710 foreach ($sourcearray as $source) {
711 $contact_temp = $taskobj->liste_contact(-1, $source);
712 if ((is_array($contact_temp) && count($contact_temp) > 0)) {
713 $contact_arrray = array_merge($contact_arrray, $contact_temp);
716 if ((is_array($contact_arrray) && count($contact_arrray) > 0)) {
717 $listlinestaskres = $listlines->__get(
'tasksressources');
719 foreach ($contact_arrray as $contact) {
720 if ($contact[
'source'] ==
'internal') {
721 $objectdetail =
new User($this->db);
722 $objectdetail->fetch($contact[
'id']);
723 $contact[
'socname'] =
$mysoc->name;
724 } elseif ($contact[
'source'] ==
'external') {
725 $objectdetail =
new Contact($this->db);
726 $objectdetail->fetch($contact[
'id']);
729 $soc->fetch($contact[
'socid']);
730 $contact[
'socname'] = $soc->name;
732 dol_syslog(get_class().
'::'.__METHOD__.
' Unexpected contact source:'.$contact[
'source'], LOG_ERR);
735 $contact[
'fullname'] = $objectdetail->getFullName($outputlangs, 1);
739 foreach ($tmparray as $key => $val) {
741 $listlinestaskres->setVars($key, $val,
true,
'UTF-8');
742 }
catch (SegmentException $e) {
746 $listlinestaskres->merge();
751 $sql =
"SELECT t.rowid, t.element_date as task_date, t.element_duration as task_duration, t.fk_user, t.note";
752 $sql .=
", u.lastname, u.firstname, t.thm";
753 $sql .=
" FROM ".MAIN_DB_PREFIX.
"element_time as t";
754 $sql .=
" , ".MAIN_DB_PREFIX.
"user as u";
755 $sql .=
" WHERE t.fk_element =".((int) $task->id);
756 $sql .=
" AND t.elementtype = 'task'";
757 $sql .=
" AND t.fk_user = u.rowid";
758 $sql .=
" ORDER BY t.element_date DESC";
760 $resql = $this->db->query($sql);
762 $num = $this->db->num_rows($resql);
766 $listlinestasktime = $listlines->__get(
'taskstimes');
769 $row[
'task_date'] = 0;
770 $row[
'task_duration'] = 0;
775 $row[
'firstname'] =
'';
776 $row[
'fullcivname'] =
'';
777 $row[
'amountht'] = 0;
778 $row[
'amountttc'] = 0;
781 foreach ($tmparray as $key => $val) {
783 $listlinestasktime->setVars($key, $val,
true,
'UTF-8');
784 }
catch (SegmentException $e) {
788 $listlinestasktime->merge();
791 $row = $this->db->fetch_array($resql);
792 if (!empty($row[
'fk_user'])) {
793 $objectdetail =
new User($this->db);
794 $objectdetail->fetch($row[
'fk_user']);
795 $row[
'fullcivname'] = $objectdetail->getFullName($outputlangs, 1);
797 $row[
'fullcivname'] =
'';
800 if (!empty($row[
'thm'])) {
801 $row[
'amountht'] = ($row[
'task_duration'] / 3600) * $row[
'thm'];
803 $row[
'amountttc'] =
price2num($row[
'amountht'] * (1 + ($defaultvat / 100)),
'MT');
805 $row[
'amountht'] = 0;
806 $row[
'amountttc'] = 0;
812 foreach ($tmparray as $key => $val) {
814 $listlinestasktime->setVars($key, $val,
true,
'UTF-8');
815 }
catch (SegmentException $e) {
819 $listlinestasktime->merge();
822 $this->db->free($resql);
827 $listtasksfiles = $listlines->__get(
'tasksfiles');
830 $filearray =
dol_dir_list($upload_dir,
"files", 0,
'',
'(\.meta|_preview.*\.png)$',
'name', SORT_ASC, 1);
833 foreach ($filearray as $filedetail) {
836 foreach ($tmparray as $key => $val) {
838 $listtasksfiles->setVars($key, $val,
true,
'UTF-8');
839 }
catch (SegmentException $e) {
843 $listtasksfiles->merge();
847 $odfHandler->mergeSegment($listlines);
848 }
catch (OdfException $e) {
849 $ExceptionTrace = $e->getTrace();
851 if ($ExceptionTrace[0][
'function'] !=
'setSegment') {
852 $this->error = $e->getMessage();
860 $listlines = $odfHandler->setSegment(
'projectfiles');
863 $filearray =
dol_dir_list($upload_dir,
"files", 0,
'',
'(\.meta|_preview.*\.png)$',
'name', SORT_ASC, 1);
865 foreach ($filearray as $filedetail) {
869 foreach ($tmparray as $key => $val) {
871 $listlines->setVars($key, $val,
true,
'UTF-8');
872 }
catch (SegmentException $e) {
878 $odfHandler->mergeSegment($listlines);
879 }
catch (OdfException $e) {
880 $ExceptionTrace = $e->getTrace();
882 if ($ExceptionTrace[0][
'function'] !=
'setSegment') {
883 $this->error = $e->getMessage();
890 $sourcearray = array(
'internal',
'external');
891 $contact_arrray = array();
892 foreach ($sourcearray as $source) {
893 $contact_temp =
$object->liste_contact(-1, $source);
894 if ((is_array($contact_temp) && count($contact_temp) > 0)) {
895 $contact_arrray = array_merge($contact_arrray, $contact_temp);
898 if ((is_array($contact_arrray) && count($contact_arrray) > 0)) {
900 $listlines = $odfHandler->setSegment(
'projectcontacts');
902 foreach ($contact_arrray as $contact) {
903 if ($contact[
'source'] ==
'internal') {
904 $objectdetail =
new User($this->db);
905 $objectdetail->fetch($contact[
'id']);
906 $contact[
'socname'] =
$mysoc->name;
907 } elseif ($contact[
'source'] ==
'external') {
908 $objectdetail =
new Contact($this->db);
909 $objectdetail->fetch($contact[
'id']);
912 $soc->fetch($contact[
'socid']);
913 $contact[
'socname'] = $soc->name;
915 $contact[
'fullname'] = $objectdetail->getFullName($outputlangs, 1);
918 foreach ($tmparray as $key => $val) {
920 $listlines->setVars($key, $val,
true,
'UTF-8');
921 }
catch (SegmentException $e) {
927 $odfHandler->mergeSegment($listlines);
928 }
catch (OdfException $e) {
929 $ExceptionTrace = $e->getTrace();
931 if ($ExceptionTrace[0][
'function'] !=
'setSegment') {
932 $this->error = $e->getMessage();
941 $listofreferent = array(
943 'title' =>
"ListProposalsAssociatedProject",
946 'test' =>
isModEnabled(
'propal') && $user->hasRight(
'propal',
'lire')
949 'title' =>
"ListOrdersAssociatedProject",
950 'class' =>
'Commande',
951 'table' =>
'commande',
952 'test' =>
isModEnabled(
'order') && $user->hasRight(
'commande',
'lire')
955 'title' =>
"ListInvoicesAssociatedProject",
956 'class' =>
'Facture',
957 'table' =>
'facture',
958 'test' =>
isModEnabled(
'invoice') && $user->hasRight(
'facture',
'lire')
960 'invoice_predefined' => array(
961 'title' =>
"ListPredefinedInvoicesAssociatedProject",
962 'class' =>
'FactureRec',
963 'table' =>
'facture_rec',
964 'test' =>
isModEnabled(
'invoice') && $user->hasRight(
'facture',
'lire')
966 'proposal_supplier' => array(
967 'title' =>
"ListSupplierProposalsAssociatedProject",
968 'class' =>
'SupplierProposal',
969 'table' =>
'supplier_proposal',
970 'test' =>
isModEnabled(
'supplier_proposal') && $user->hasRight(
'supplier_proposal',
'lire')
972 'order_supplier' => array(
973 'title' =>
"ListSupplierOrdersAssociatedProject",
974 'table' =>
'commande_fournisseur',
975 'class' =>
'CommandeFournisseur',
976 'test' => (
isModEnabled(
"fournisseur") && !
getDolGlobalString(
'MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight(
'fournisseur',
'commande',
'lire')) || (
isModEnabled(
"supplier_order") && $user->hasRight(
'supplier_order',
'lire'))
978 'invoice_supplier' => array(
979 'title' =>
"ListSupplierInvoicesAssociatedProject",
980 'table' =>
'facture_fourn',
981 'class' =>
'FactureFournisseur',
982 'test' => (
isModEnabled(
"fournisseur") && !
getDolGlobalString(
'MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight(
'fournisseur',
'facture',
'lire')) || (
isModEnabled(
"supplier_invoice") && $user->hasRight(
'supplier_invoice',
'lire'))
985 'title' =>
"ListContractAssociatedProject",
986 'class' =>
'Contrat',
987 'table' =>
'contrat',
988 'test' =>
isModEnabled(
'contract') && $user->hasRight(
'contrat',
'lire')
990 'intervention' => array(
991 'title' =>
"ListFichinterAssociatedProject",
992 'class' =>
'Fichinter',
993 'table' =>
'fichinter',
994 'disableamount' => 1,
995 'test' =>
isModEnabled(
'intervention') && $user->hasRight(
'ficheinter',
'lire')
998 'title' =>
"ListShippingAssociatedProject",
999 'class' =>
'Expedition',
1000 'table' =>
'expedition',
1001 'disableamount' => 1,
1002 'test' =>
isModEnabled(
'shipping') && $user->hasRight(
'expedition',
'lire')
1005 'title' =>
"ListTripAssociatedProject",
1006 'class' =>
'Deplacement',
1007 'table' =>
'deplacement',
1008 'disableamount' => 1,
1009 'test' =>
isModEnabled(
'deplacement') && $user->hasRight(
'deplacement',
'lire')
1011 'expensereport' => array(
1012 'title' =>
"ListExpenseReportsAssociatedProject",
1013 'class' =>
'ExpenseReportLine',
1014 'table' =>
'expensereport_det',
1015 'test' =>
isModEnabled(
'expensereport') && $user->hasRight(
'expensereport',
'lire')
1017 'donation' => array(
1018 'title' =>
"ListDonationsAssociatedProject",
1021 'test' =>
isModEnabled(
'don') && $user->hasRight(
'don',
'lire')
1024 'title' =>
"ListLoanAssociatedProject",
1027 'test' =>
isModEnabled(
'loan') && $user->hasRight(
'loan',
'read')
1029 'chargesociales' => array(
1030 'title' =>
"ListSocialContributionAssociatedProject",
1031 'class' =>
'ChargeSociales',
1032 'table' =>
'chargesociales',
1033 'urlnew' => DOL_URL_ROOT.
'/compta/sociales/card.php?action=create&projectid='.
$object->id,
1034 'test' =>
isModEnabled(
'tax') && $user->hasRight(
'tax',
'charges',
'lire')
1036 'stock_mouvement' => array(
1037 'title' =>
"ListMouvementStockProject",
1038 'class' =>
'MouvementStock',
1039 'table' =>
'stock_mouvement',
1043 'title' =>
"ListActionsAssociatedProject",
1044 'class' =>
'ActionComm',
1045 'table' =>
'actioncomm',
1046 'disableamount' => 1,
1047 'test' =>
isModEnabled(
'agenda') && $user->hasRight(
'agenda',
'allactions',
'lire')
1053 $listlines = $odfHandler->setSegment(
'projectrefs');
1055 foreach ($listofreferent as $keyref => $valueref) {
1056 $title = $valueref[
'title'];
1057 $tablename = $valueref[
'table'];
1058 $classname = $valueref[
'class'];
1059 $qualified = $valueref[
'test'];
1061 $elementarray =
$object->get_element_list($keyref, $tablename);
1062 if (count($elementarray) > 0 && is_array($elementarray)) {
1065 $num = count($elementarray);
1066 for ($i = 0; $i < $num; $i++) {
1067 $ref_array = array();
1068 $ref_array[
'type'] = (string) $langs->trans($classname);
1070 $element =
new $classname($this->db);
1071 $element->fetch((
int) $elementarray[$i]);
1072 $element->fetch_thirdparty();
1075 $ref_array[
'ref'] = $element->ref;
1078 $dateref = $element->date;
1079 if (empty($dateref)) {
1080 $dateref = $element->datep;
1082 if (empty($dateref)) {
1083 $dateref = $element->date_contrat;
1085 $ref_array[
'date'] = (string) $dateref;
1088 if (is_object($element->thirdparty)) {
1089 $ref_array[
'socname'] = $element->thirdparty->name;
1091 $ref_array[
'socname'] =
'';
1095 if (empty($valueref[
'disableamount'])) {
1096 if (!empty($element->total_ht)) {
1097 $ref_array[
'amountht'] = $element->total_ht;
1098 $ref_array[
'amountttc'] = $element->total_ttc;
1100 $ref_array[
'amountht'] = 0;
1101 $ref_array[
'amountttc'] = 0;
1104 $ref_array[
'amountht'] =
'';
1105 $ref_array[
'amountttc'] =
'';
1108 $ref_array[
'status'] = $element->getLibStatut(0);
1112 foreach ($tmparray as $key => $val) {
1114 $listlines->setVars($key, $val,
true,
'UTF-8');
1115 }
catch (SegmentException $e) {
1119 $listlines->merge();
1124 $odfHandler->mergeSegment($listlines);
1125 }
catch (OdfExceptionSegmentNotFound $e) {
1127 }
catch (OdfException $e) {
1128 $this->error = $e->getMessage();
1134 $tmparray = $outputlangs->get_translations_for_substitutions();
1135 foreach ($tmparray as $key => $value) {
1137 $odfHandler->setVars($key, $value,
true,
'UTF-8');
1138 }
catch (OdfException $e) {
1144 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
1145 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
1151 $odfHandler->exportAsAttachedPDF($file);
1153 $this->error = $e->getMessage();
1158 $odfHandler->saveToDisk($file);
1160 $this->error = $e->getMessage();
1165 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
1166 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
1172 $this->result = array(
'fullpath' => $file);
1176 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);