501 global $user, $langs, $conf,
$mysoc, $hookmanager;
503 if (empty($srctemplatepath)) {
504 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
509 if (!is_object($hookmanager)) {
510 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
513 $hookmanager->initHooks(array(
'odtgeneration'));
516 if (!is_object($outputlangs)) {
517 $outputlangs = $langs;
519 $sav_charset_output = $outputlangs->charset_output;
520 $outputlangs->charset_output =
'UTF-8';
523 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"projects"));
525 if ($conf->project->dir_output) {
539 $dir = $conf->project->dir_output;
541 if (!preg_match(
'/specimen/i', $objectref)) {
542 $dir .=
"/".$objectref;
544 $file = $dir.
"/".$objectref.
".odt";
546 if (!file_exists($dir)) {
548 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
553 if (file_exists($dir)) {
555 $newfile = basename($srctemplatepath);
556 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
557 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
558 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
559 $newfiletmp = $objectref .
'_' . $newfiletmp;
562 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
565 if ($format ==
'1') {
566 $format =
'%Y%m%d%H%M%S';
570 $filename = $newfiletmp .
'.' . $newfileformat;
572 $file = $dir .
'/' . $filename;
579 if (!is_writable($conf->project->dir_temp)) {
580 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->project->dir_temp);
581 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
587 $arrayidcontact =
$object->getIdContact(
'external',
'PROJECTLEADER');
588 if (count($arrayidcontact) > 0) {
590 $result =
$object->fetch_contact($arrayidcontact[0]);
594 $contactobject =
null;
595 if (!empty($usecontact)) {
597 $contactobject =
$object->contact;
600 $socobject =
$object->thirdparty;
603 $substitutionarray = array(
604 '__FROM_NAME__' => $this->emetteur->name,
605 '__FROM_EMAIL__' => $this->emetteur->email,
609 $parameters = array(
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$substitutionarray);
610 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
613 require_once ODTPHP_PATH.
'odf.php';
615 $odfHandler =
new Odf(
618 'PATH_TO_TMP' => $conf->project->dir_temp,
620 'DELIMITER_LEFT' =>
'{',
621 'DELIMITER_RIGHT' =>
'}'
625 $this->error = $e->getMessage();
634 $substitutionarray = getCommonSubstitutionArray($outputlangs, 0,
null,
$object);
642 $array_project_contact = array();
643 if ($usecontact && is_object($contactobject)) {
647 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_project_contact);
651 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
652 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
661 foreach ($tmparray as $key => $value) {
663 if (preg_match(
'/logo$/', $key)) {
664 if (file_exists($value)) {
665 $odfHandler->setImage($key, $value, $ratio);
667 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
670 $odfHandler->setVars($key, $value,
true,
'UTF-8');
672 }
catch (OdfException $e) {
679 $listlines = $odfHandler->setSegment(
'tasks');
681 $taskstatic =
new Task($this->db);
689 $tasksarray = $taskstatic->getTasksArray(
null,
null,
$object->id, $socid, 0);
692 foreach ($tasksarray as $task) {
695 foreach ($tmparray as $key => $val) {
697 $listlines->setVars($key, $val,
true,
'UTF-8');
698 }
catch (SegmentException $e) {
703 $taskobj =
new Task($this->db);
704 $taskobj->fetch($task->id);
707 $sourcearray = array(
'internal',
'external');
708 $contact_arrray = array();
709 foreach ($sourcearray as $source) {
710 $contact_temp = $taskobj->liste_contact(-1, $source);
711 if ((is_array($contact_temp) && count($contact_temp) > 0)) {
712 $contact_arrray = array_merge($contact_arrray, $contact_temp);
715 if ((is_array($contact_arrray) && count($contact_arrray) > 0)) {
716 $listlinestaskres = $listlines->__get(
'tasksressources');
718 foreach ($contact_arrray as $contact) {
719 if ($contact[
'source'] ==
'internal') {
720 $objectdetail =
new User($this->db);
721 $objectdetail->fetch($contact[
'id']);
722 $contact[
'socname'] =
$mysoc->name;
723 } elseif ($contact[
'source'] ==
'external') {
724 $objectdetail =
new Contact($this->db);
725 $objectdetail->fetch($contact[
'id']);
728 $soc->fetch($contact[
'socid']);
729 $contact[
'socname'] = $soc->name;
731 dol_syslog(get_class().
'::'.__METHOD__.
' Unexpected contact source:'.$contact[
'source'], LOG_ERR);
734 $contact[
'fullname'] = $objectdetail->getFullName($outputlangs, 1);
738 foreach ($tmparray as $key => $val) {
740 $listlinestaskres->setVars($key, $val,
true,
'UTF-8');
741 }
catch (SegmentException $e) {
745 $listlinestaskres->merge();
750 $sql =
"SELECT t.rowid, t.element_date as task_date, t.element_duration as task_duration, t.fk_user, t.note";
751 $sql .=
", u.lastname, u.firstname, t.thm";
752 $sql .=
" FROM ".MAIN_DB_PREFIX.
"element_time as t";
753 $sql .=
" , ".MAIN_DB_PREFIX.
"user as u";
754 $sql .=
" WHERE t.fk_element =".((int) $task->id);
755 $sql .=
" AND t.elementtype = 'task'";
756 $sql .=
" AND t.fk_user = u.rowid";
757 $sql .=
" ORDER BY t.element_date DESC";
759 $resql = $this->db->query($sql);
761 $num = $this->db->num_rows($resql);
765 $listlinestasktime = $listlines->__get(
'taskstimes');
768 $row[
'task_date'] = 0;
769 $row[
'task_duration'] = 0;
774 $row[
'firstname'] =
'';
775 $row[
'fullcivname'] =
'';
776 $row[
'amountht'] = 0;
777 $row[
'amountttc'] = 0;
780 foreach ($tmparray as $key => $val) {
782 $listlinestasktime->setVars($key, $val,
true,
'UTF-8');
783 }
catch (SegmentException $e) {
787 $listlinestasktime->merge();
790 $row = $this->db->fetch_array($resql);
791 if (!empty($row[
'fk_user'])) {
792 $objectdetail =
new User($this->db);
793 $objectdetail->fetch($row[
'fk_user']);
794 $row[
'fullcivname'] = $objectdetail->getFullName($outputlangs, 1);
796 $row[
'fullcivname'] =
'';
799 if (!empty($row[
'thm'])) {
800 $row[
'amountht'] = ($row[
'task_duration'] / 3600) * $row[
'thm'];
802 $row[
'amountttc'] =
price2num($row[
'amountht'] * (1 + ($defaultvat / 100)),
'MT');
804 $row[
'amountht'] = 0;
805 $row[
'amountttc'] = 0;
811 foreach ($tmparray as $key => $val) {
813 $listlinestasktime->setVars($key, $val,
true,
'UTF-8');
814 }
catch (SegmentException $e) {
818 $listlinestasktime->merge();
821 $this->db->free($resql);
826 $listtasksfiles = $listlines->__get(
'tasksfiles');
829 $filearray =
dol_dir_list($upload_dir,
"files", 0,
'',
'(\.meta|_preview.*\.png)$',
'name', SORT_ASC, 1);
832 foreach ($filearray as $filedetail) {
835 foreach ($tmparray as $key => $val) {
837 $listtasksfiles->setVars($key, $val,
true,
'UTF-8');
838 }
catch (SegmentException $e) {
842 $listtasksfiles->merge();
846 $odfHandler->mergeSegment($listlines);
847 }
catch (OdfException $e) {
848 $ExceptionTrace = $e->getTrace();
850 if ($ExceptionTrace[0][
'function'] !=
'setSegment') {
851 $this->error = $e->getMessage();
859 $listlines = $odfHandler->setSegment(
'projectfiles');
862 $filearray =
dol_dir_list($upload_dir,
"files", 0,
'',
'(\.meta|_preview.*\.png)$',
'name', SORT_ASC, 1);
864 foreach ($filearray as $filedetail) {
868 foreach ($tmparray as $key => $val) {
870 $listlines->setVars($key, $val,
true,
'UTF-8');
871 }
catch (SegmentException $e) {
877 $odfHandler->mergeSegment($listlines);
878 }
catch (OdfException $e) {
879 $ExceptionTrace = $e->getTrace();
881 if ($ExceptionTrace[0][
'function'] !=
'setSegment') {
882 $this->error = $e->getMessage();
889 $sourcearray = array(
'internal',
'external');
890 $contact_arrray = array();
891 foreach ($sourcearray as $source) {
892 $contact_temp =
$object->liste_contact(-1, $source);
893 if ((is_array($contact_temp) && count($contact_temp) > 0)) {
894 $contact_arrray = array_merge($contact_arrray, $contact_temp);
897 if ((is_array($contact_arrray) && count($contact_arrray) > 0)) {
899 $listlines = $odfHandler->setSegment(
'projectcontacts');
901 foreach ($contact_arrray as $contact) {
902 if ($contact[
'source'] ==
'internal') {
903 $objectdetail =
new User($this->db);
904 $objectdetail->fetch($contact[
'id']);
905 $contact[
'socname'] =
$mysoc->name;
906 } elseif ($contact[
'source'] ==
'external') {
907 $objectdetail =
new Contact($this->db);
908 $objectdetail->fetch($contact[
'id']);
911 $soc->fetch($contact[
'socid']);
912 $contact[
'socname'] = $soc->name;
914 $contact[
'fullname'] = $objectdetail->getFullName($outputlangs, 1);
917 foreach ($tmparray as $key => $val) {
919 $listlines->setVars($key, $val,
true,
'UTF-8');
920 }
catch (SegmentException $e) {
926 $odfHandler->mergeSegment($listlines);
927 }
catch (OdfException $e) {
928 $ExceptionTrace = $e->getTrace();
930 if ($ExceptionTrace[0][
'function'] !=
'setSegment') {
931 $this->error = $e->getMessage();
940 $listofreferent = array(
942 'title' =>
"ListProposalsAssociatedProject",
945 'test' =>
isModEnabled(
'propal') && $user->hasRight(
'propal',
'lire')
948 'title' =>
"ListOrdersAssociatedProject",
949 'class' =>
'Commande',
950 'table' =>
'commande',
951 'test' =>
isModEnabled(
'order') && $user->hasRight(
'commande',
'lire')
954 'title' =>
"ListInvoicesAssociatedProject",
955 'class' =>
'Facture',
956 'table' =>
'facture',
957 'test' =>
isModEnabled(
'invoice') && $user->hasRight(
'facture',
'lire')
959 'invoice_predefined' => array(
960 'title' =>
"ListPredefinedInvoicesAssociatedProject",
961 'class' =>
'FactureRec',
962 'table' =>
'facture_rec',
963 'test' =>
isModEnabled(
'invoice') && $user->hasRight(
'facture',
'lire')
965 'proposal_supplier' => array(
966 'title' =>
"ListSupplierProposalsAssociatedProject",
967 'class' =>
'SupplierProposal',
968 'table' =>
'supplier_proposal',
969 'test' =>
isModEnabled(
'supplier_proposal') && $user->hasRight(
'supplier_proposal',
'lire')
971 'order_supplier' => array(
972 'title' =>
"ListSupplierOrdersAssociatedProject",
973 'table' =>
'commande_fournisseur',
974 'class' =>
'CommandeFournisseur',
975 'test' => (
isModEnabled(
"fournisseur") && !
getDolGlobalString(
'MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight(
'fournisseur',
'commande',
'lire')) || (
isModEnabled(
"supplier_order") && $user->hasRight(
'supplier_order',
'lire'))
977 'invoice_supplier' => array(
978 'title' =>
"ListSupplierInvoicesAssociatedProject",
979 'table' =>
'facture_fourn',
980 'class' =>
'FactureFournisseur',
981 'test' => (
isModEnabled(
"fournisseur") && !
getDolGlobalString(
'MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight(
'fournisseur',
'facture',
'lire')) || (
isModEnabled(
"supplier_invoice") && $user->hasRight(
'supplier_invoice',
'lire'))
984 'title' =>
"ListContractAssociatedProject",
985 'class' =>
'Contrat',
986 'table' =>
'contrat',
987 'test' =>
isModEnabled(
'contract') && $user->hasRight(
'contrat',
'lire')
989 'intervention' => array(
990 'title' =>
"ListFichinterAssociatedProject",
991 'class' =>
'Fichinter',
992 'table' =>
'fichinter',
993 'disableamount' => 1,
994 'test' =>
isModEnabled(
'intervention') && $user->hasRight(
'ficheinter',
'lire')
997 'title' =>
"ListShippingAssociatedProject",
998 'class' =>
'Expedition',
999 'table' =>
'expedition',
1000 'disableamount' => 1,
1001 'test' =>
isModEnabled(
'shipping') && $user->hasRight(
'expedition',
'lire')
1004 'title' =>
"ListTripAssociatedProject",
1005 'class' =>
'Deplacement',
1006 'table' =>
'deplacement',
1007 'disableamount' => 1,
1008 'test' =>
isModEnabled(
'deplacement') && $user->hasRight(
'deplacement',
'lire')
1010 'expensereport' => array(
1011 'title' =>
"ListExpenseReportsAssociatedProject",
1012 'class' =>
'ExpenseReportLine',
1013 'table' =>
'expensereport_det',
1014 'test' =>
isModEnabled(
'expensereport') && $user->hasRight(
'expensereport',
'lire')
1016 'donation' => array(
1017 'title' =>
"ListDonationsAssociatedProject",
1020 'test' =>
isModEnabled(
'don') && $user->hasRight(
'don',
'lire')
1023 'title' =>
"ListLoanAssociatedProject",
1026 'test' =>
isModEnabled(
'loan') && $user->hasRight(
'loan',
'read')
1028 'chargesociales' => array(
1029 'title' =>
"ListSocialContributionAssociatedProject",
1030 'class' =>
'ChargeSociales',
1031 'table' =>
'chargesociales',
1032 'urlnew' => DOL_URL_ROOT.
'/compta/sociales/card.php?action=create&projectid='.
$object->id,
1033 'test' =>
isModEnabled(
'tax') && $user->hasRight(
'tax',
'charges',
'lire')
1035 'stock_mouvement' => array(
1036 'title' =>
"ListMouvementStockProject",
1037 'class' =>
'MouvementStock',
1038 'table' =>
'stock_mouvement',
1042 'title' =>
"ListActionsAssociatedProject",
1043 'class' =>
'ActionComm',
1044 'table' =>
'actioncomm',
1045 'disableamount' => 1,
1046 'test' =>
isModEnabled(
'agenda') && $user->hasRight(
'agenda',
'allactions',
'lire')
1052 $listlines = $odfHandler->setSegment(
'projectrefs');
1054 foreach ($listofreferent as $keyref => $valueref) {
1055 $title = $valueref[
'title'];
1056 $tablename = $valueref[
'table'];
1057 $classname = $valueref[
'class'];
1058 $qualified = $valueref[
'test'];
1060 $elementarray =
$object->get_element_list($keyref, $tablename);
1061 if (count($elementarray) > 0 && is_array($elementarray)) {
1064 $num = count($elementarray);
1065 for ($i = 0; $i < $num; $i++) {
1066 $ref_array = array();
1067 $ref_array[
'type'] = (string) $langs->trans($classname);
1069 $element =
new $classname($this->db);
1070 $element->fetch((
int) $elementarray[$i]);
1071 $element->fetch_thirdparty();
1074 $ref_array[
'ref'] = $element->ref;
1077 $dateref = $element->date;
1078 if (empty($dateref)) {
1079 $dateref = $element->datep;
1081 if (empty($dateref)) {
1082 $dateref = $element->date_contrat;
1084 $ref_array[
'date'] = (string) $dateref;
1087 if (is_object($element->thirdparty)) {
1088 $ref_array[
'socname'] = $element->thirdparty->name;
1090 $ref_array[
'socname'] =
'';
1094 if (empty($valueref[
'disableamount'])) {
1095 if (!empty($element->total_ht)) {
1096 $ref_array[
'amountht'] = $element->total_ht;
1097 $ref_array[
'amountttc'] = $element->total_ttc;
1099 $ref_array[
'amountht'] = 0;
1100 $ref_array[
'amountttc'] = 0;
1103 $ref_array[
'amountht'] =
'';
1104 $ref_array[
'amountttc'] =
'';
1107 $ref_array[
'status'] = $element->getLibStatut(0);
1111 foreach ($tmparray as $key => $val) {
1113 $listlines->setVars($key, $val,
true,
'UTF-8');
1114 }
catch (SegmentException $e) {
1118 $listlines->merge();
1122 $odfHandler->mergeSegment($listlines);
1124 }
catch (OdfExceptionSegmentNotFound $e) {
1126 }
catch (OdfException $e) {
1127 $this->error = $e->getMessage();
1133 $tmparray = $outputlangs->get_translations_for_substitutions();
1134 foreach ($tmparray as $key => $value) {
1136 $odfHandler->setVars($key, $value,
true,
'UTF-8');
1137 }
catch (OdfException $e) {
1143 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
1144 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
1150 $odfHandler->exportAsAttachedPDF($file);
1152 $this->error = $e->getMessage();
1157 $odfHandler->saveToDisk($file);
1159 $this->error = $e->getMessage();
1164 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
1165 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
1171 $this->result = array(
'fullpath' => $file);
1175 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);