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 $this->error = $e->getMessage();
885 $sourcearray = array(
'internal',
'external');
886 $contact_arrray = array();
887 foreach ($sourcearray as $source) {
888 $contact_temp =
$object->liste_contact(-1, $source);
889 if ((is_array($contact_temp) && count($contact_temp) > 0)) {
890 $contact_arrray = array_merge($contact_arrray, $contact_temp);
893 if ((is_array($contact_arrray) && count($contact_arrray) > 0)) {
895 $listlines = $odfHandler->setSegment(
'projectcontacts');
897 foreach ($contact_arrray as $contact) {
898 if ($contact[
'source'] ==
'internal') {
899 $objectdetail =
new User($this->db);
900 $objectdetail->fetch($contact[
'id']);
901 $contact[
'socname'] = $mysoc->name;
902 } elseif ($contact[
'source'] ==
'external') {
903 $objectdetail =
new Contact($this->db);
904 $objectdetail->fetch($contact[
'id']);
907 $soc->fetch($contact[
'socid']);
908 $contact[
'socname'] = $soc->name;
910 $contact[
'fullname'] = $objectdetail->getFullName($outputlangs, 1);
913 foreach ($tmparray as $key => $val) {
915 $listlines->setVars($key, $val,
true,
'UTF-8');
916 }
catch (SegmentException $e) {
922 $odfHandler->mergeSegment($listlines);
923 }
catch (OdfException $e) {
924 $this->error = $e->getMessage();
932 $listofreferent = array(
934 'title' =>
"ListProposalsAssociatedProject",
937 'test' => isModEnabled(
'propal') && $user->hasRight(
'propal',
'lire')
940 'title' =>
"ListOrdersAssociatedProject",
941 'class' =>
'Commande',
942 'table' =>
'commande',
943 'test' => isModEnabled(
'order') && $user->hasRight(
'commande',
'lire')
946 'title' =>
"ListInvoicesAssociatedProject",
947 'class' =>
'Facture',
948 'table' =>
'facture',
949 'test' => isModEnabled(
'invoice') && $user->hasRight(
'facture',
'lire')
951 'invoice_predefined' => array(
952 'title' =>
"ListPredefinedInvoicesAssociatedProject",
953 'class' =>
'FactureRec',
954 'table' =>
'facture_rec',
955 'test' => isModEnabled(
'invoice') && $user->hasRight(
'facture',
'lire')
957 'proposal_supplier' => array(
958 'title' =>
"ListSupplierProposalsAssociatedProject",
959 'class' =>
'SupplierProposal',
960 'table' =>
'supplier_proposal',
961 'test' => isModEnabled(
'supplier_proposal') && $user->hasRight(
'supplier_proposal',
'lire')
963 'order_supplier' => array(
964 'title' =>
"ListSupplierOrdersAssociatedProject",
965 'table' =>
'commande_fournisseur',
966 'class' =>
'CommandeFournisseur',
967 'test' => (isModEnabled(
"fournisseur") && !
getDolGlobalString(
'MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight(
'fournisseur',
'commande',
'lire')) || (isModEnabled(
"supplier_order") && $user->hasRight(
'supplier_order',
'lire'))
969 'invoice_supplier' => array(
970 'title' =>
"ListSupplierInvoicesAssociatedProject",
971 'table' =>
'facture_fourn',
972 'class' =>
'FactureFournisseur',
973 'test' => (isModEnabled(
"fournisseur") && !
getDolGlobalString(
'MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight(
'fournisseur',
'facture',
'lire')) || (isModEnabled(
"supplier_invoice") && $user->hasRight(
'supplier_invoice',
'lire'))
976 'title' =>
"ListContractAssociatedProject",
977 'class' =>
'Contrat',
978 'table' =>
'contrat',
979 'test' => isModEnabled(
'contract') && $user->hasRight(
'contrat',
'lire')
981 'intervention' => array(
982 'title' =>
"ListFichinterAssociatedProject",
983 'class' =>
'Fichinter',
984 'table' =>
'fichinter',
985 'disableamount' => 1,
986 'test' => isModEnabled(
'intervention') && $user->hasRight(
'ficheinter',
'lire')
989 'title' =>
"ListShippingAssociatedProject",
990 'class' =>
'Expedition',
991 'table' =>
'expedition',
992 'disableamount' => 1,
993 'test' => isModEnabled(
'shipping') && $user->hasRight(
'expedition',
'lire')
996 'title' =>
"ListTripAssociatedProject",
997 'class' =>
'Deplacement',
998 'table' =>
'deplacement',
999 'disableamount' => 1,
1000 'test' => isModEnabled(
'deplacement') && $user->hasRight(
'deplacement',
'lire')
1002 'expensereport' => array(
1003 'title' =>
"ListExpenseReportsAssociatedProject",
1004 'class' =>
'ExpenseReportLine',
1005 'table' =>
'expensereport_det',
1006 'test' => isModEnabled(
'expensereport') && $user->hasRight(
'expensereport',
'lire')
1008 'donation' => array(
1009 'title' =>
"ListDonationsAssociatedProject",
1012 'test' => isModEnabled(
'don') && $user->hasRight(
'don',
'lire')
1015 'title' =>
"ListLoanAssociatedProject",
1018 'test' => isModEnabled(
'loan') && $user->hasRight(
'loan',
'read')
1020 'chargesociales' => array(
1021 'title' =>
"ListSocialContributionAssociatedProject",
1022 'class' =>
'ChargeSociales',
1023 'table' =>
'chargesociales',
1024 'urlnew' => DOL_URL_ROOT.
'/compta/sociales/card.php?action=create&projectid='.
$object->id,
1025 'test' => isModEnabled(
'tax') && $user->hasRight(
'tax',
'charges',
'lire')
1027 'stock_mouvement' => array(
1028 'title' =>
"ListMouvementStockProject",
1029 'class' =>
'MouvementStock',
1030 'table' =>
'stock_mouvement',
1031 'test' => (isModEnabled(
'stock') && $user->hasRight(
'stock',
'mouvement',
'lire') &&
getDolGlobalString(
'STOCK_MOVEMENT_INTO_PROJECT_OVERVIEW'))
1034 'title' =>
"ListActionsAssociatedProject",
1035 'class' =>
'ActionComm',
1036 'table' =>
'actioncomm',
1037 'disableamount' => 1,
1038 'test' => isModEnabled(
'agenda') && $user->hasRight(
'agenda',
'allactions',
'lire')
1044 $listlines = $odfHandler->setSegment(
'projectrefs');
1046 foreach ($listofreferent as $keyref => $valueref) {
1047 $title = $valueref[
'title'];
1048 $tablename = $valueref[
'table'];
1049 $classname = $valueref[
'class'];
1050 $qualified = $valueref[
'test'];
1052 $elementarray =
$object->get_element_list($keyref, $tablename);
1053 if (count($elementarray) > 0 && is_array($elementarray)) {
1056 $num = count($elementarray);
1057 for ($i = 0; $i < $num; $i++) {
1058 $ref_array = array();
1059 $ref_array[
'type'] = (string) $langs->trans($classname);
1061 $element =
new $classname($this->db);
1062 $element->fetch((
int) $elementarray[$i]);
1063 $element->fetch_thirdparty();
1066 $ref_array[
'ref'] = $element->ref;
1069 $dateref = $element->date;
1070 if (empty($dateref)) {
1071 $dateref = $element->datep;
1073 if (empty($dateref)) {
1074 $dateref = $element->date_contrat;
1076 $ref_array[
'date'] = (string) $dateref;
1079 if (is_object($element->thirdparty)) {
1080 $ref_array[
'socname'] = $element->thirdparty->name;
1082 $ref_array[
'socname'] =
'';
1086 if (empty($valueref[
'disableamount'])) {
1087 if (!empty($element->total_ht)) {
1088 $ref_array[
'amountht'] = $element->total_ht;
1089 $ref_array[
'amountttc'] = $element->total_ttc;
1091 $ref_array[
'amountht'] = 0;
1092 $ref_array[
'amountttc'] = 0;
1095 $ref_array[
'amountht'] =
'';
1096 $ref_array[
'amountttc'] =
'';
1099 $ref_array[
'status'] = $element->getLibStatut(0);
1103 foreach ($tmparray as $key => $val) {
1105 $listlines->setVars($key, $val,
true,
'UTF-8');
1106 }
catch (SegmentException $e) {
1110 $listlines->merge();
1114 $odfHandler->mergeSegment($listlines);
1116 }
catch (OdfExceptionSegmentNotFound $e) {
1118 }
catch (OdfException $e) {
1119 $this->error = $e->getMessage();
1125 $tmparray = $outputlangs->get_translations_for_substitutions();
1126 foreach ($tmparray as $key => $value) {
1128 $odfHandler->setVars($key, $value,
true,
'UTF-8');
1129 }
catch (OdfException $e) {
1135 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
1136 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
1142 $odfHandler->exportAsAttachedPDF($file);
1144 $this->error = $e->getMessage();
1149 $odfHandler->saveToDisk($file);
1151 $this->error = $e->getMessage();
1156 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
1157 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
1163 $this->result = array(
'fullpath' => $file);
1167 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);