497 public function write_file($object, $outputlangs, $srctemplatepath)
500 global $user, $langs, $conf, $mysoc, $hookmanager;
502 if (empty($srctemplatepath)) {
503 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
508 if (!is_object($hookmanager)) {
509 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
512 $hookmanager->initHooks(array(
'odtgeneration'));
515 if (!is_object($outputlangs)) {
516 $outputlangs = $langs;
518 $sav_charset_output = $outputlangs->charset_output;
519 $outputlangs->charset_output =
'UTF-8';
522 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"projects"));
524 if ($conf->project->dir_output) {
526 if (!is_object($object)) {
528 $object =
new Project($this->db);
529 $result = $object->fetch($id);
536 $object->fetch_thirdparty();
538 $dir = $conf->project->dir_output;
540 if (!preg_match(
'/specimen/i', $objectref)) {
541 $dir .=
"/".$objectref;
543 $file = $dir.
"/".$objectref.
".odt";
545 if (!file_exists($dir)) {
547 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
552 if (file_exists($dir)) {
554 $newfile = basename($srctemplatepath);
555 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
556 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
557 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
558 $newfiletmp = $objectref .
'_' . $newfiletmp;
561 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
564 if ($format ==
'1') {
565 $format =
'%Y%m%d%H%M%S';
569 $filename = $newfiletmp .
'.' . $newfileformat;
571 $file = $dir .
'/' . $filename;
578 if (!is_writable($conf->project->dir_temp)) {
579 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->project->dir_temp);
580 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
586 $arrayidcontact = $object->getIdContact(
'external',
'PROJECTLEADER');
587 if (count($arrayidcontact) > 0) {
589 $result = $object->fetch_contact($arrayidcontact[0]);
593 $contactobject =
null;
594 if (!empty($usecontact)) {
596 $contactobject = $object->contact;
599 $socobject = $object->thirdparty;
602 $substitutionarray = array(
603 '__FROM_NAME__' => $this->emetteur->name,
604 '__FROM_EMAIL__' => $this->emetteur->email,
608 $parameters = array(
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$substitutionarray);
609 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
612 require_once ODTPHP_PATH.
'odf.php';
614 $odfHandler =
new Odf(
617 'PATH_TO_TMP' => $conf->project->dir_temp,
619 'DELIMITER_LEFT' =>
'{',
620 'DELIMITER_RIGHT' =>
'}'
624 $this->error = $e->getMessage();
644 $array_project_contact = array();
645 if ($usecontact && is_object($contactobject)) {
649 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_project_contact);
653 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
654 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
656 foreach ($tmparray as $key => $value) {
658 if (preg_match(
'/logo$/', $key)) {
659 if (file_exists($value)) {
660 $odfHandler->setImage($key, $value);
662 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
665 $odfHandler->setVars($key, $value,
true,
'UTF-8');
667 }
catch (OdfException $e) {
674 $listlines = $odfHandler->setSegment(
'tasks');
676 $taskstatic =
new Task($this->db);
680 if (!empty($object->fk_soc)) {
681 $socid = $object->fk_soc;
684 $tasksarray = $taskstatic->getTasksArray(0, 0, $object->id, $socid, 0);
687 foreach ($tasksarray as $task) {
690 foreach ($tmparray as $key => $val) {
692 $listlines->setVars($key, $val,
true,
'UTF-8');
693 }
catch (OdfException $e) {
695 }
catch (SegmentException $e) {
700 $taskobj =
new Task($this->db);
701 $taskobj->fetch($task->id);
704 $sourcearray = array(
'internal',
'external');
705 $contact_arrray = array();
706 foreach ($sourcearray as $source) {
707 $contact_temp = $taskobj->liste_contact(-1, $source);
708 if ((is_array($contact_temp) && count($contact_temp) > 0)) {
709 $contact_arrray = array_merge($contact_arrray, $contact_temp);
712 if ((is_array($contact_arrray) && count($contact_arrray) > 0)) {
713 $listlinestaskres = $listlines->__get(
'tasksressources');
715 foreach ($contact_arrray as $contact) {
716 if ($contact[
'source'] ==
'internal') {
717 $objectdetail =
new User($this->db);
718 $objectdetail->fetch($contact[
'id']);
719 $contact[
'socname'] = $mysoc->name;
720 } elseif ($contact[
'source'] ==
'external') {
721 $objectdetail =
new Contact($this->db);
722 $objectdetail->fetch($contact[
'id']);
725 $soc->fetch($contact[
'socid']);
726 $contact[
'socname'] = $soc->name;
728 $contact[
'fullname'] = $objectdetail->getFullName($outputlangs, 1);
732 foreach ($tmparray as $key => $val) {
734 $listlinestaskres->setVars($key, $val,
true,
'UTF-8');
735 }
catch (OdfException $e) {
737 }
catch (SegmentException $e) {
741 $listlinestaskres->merge();
746 $sql =
"SELECT t.rowid, t.element_date as task_date, t.element_duration as task_duration, t.fk_user, t.note";
747 $sql .=
", u.lastname, u.firstname, t.thm";
748 $sql .=
" FROM ".MAIN_DB_PREFIX.
"element_time as t";
749 $sql .=
" , ".MAIN_DB_PREFIX.
"user as u";
750 $sql .=
" WHERE t.fk_element =".((int) $task->id);
751 $sql .=
" AND t.elementtype = 'task'";
752 $sql .=
" AND t.fk_user = u.rowid";
753 $sql .=
" ORDER BY t.element_date DESC";
755 $resql = $this->db->query($sql);
757 $num = $this->db->num_rows($resql);
761 $listlinestasktime = $listlines->__get(
'taskstimes');
764 $row[
'task_date'] =
'';
765 $row[
'task_duration'] =
'';
766 $row[
'$tasktime'] =
'';
768 $row[
'fk_user'] =
'';
770 $row[
'firstname'] =
'';
771 $row[
'fullcivname'] =
'';
772 $row[
'amountht'] =
'';
773 $row[
'amountttc'] =
'';
776 foreach ($tmparray as $key => $val) {
778 $listlinestasktime->setVars($key, $val,
true,
'UTF-8');
779 }
catch (OdfException $e) {
781 }
catch (SegmentException $e) {
785 $listlinestasktime->merge();
788 $row = $this->db->fetch_array($resql);
789 if (!empty($row[
'fk_user'])) {
790 $objectdetail =
new User($this->db);
791 $objectdetail->fetch($row[
'fk_user']);
792 $row[
'fullcivname'] = $objectdetail->getFullName($outputlangs, 1);
794 $row[
'fullcivname'] =
'';
797 if (!empty($row[
'thm'])) {
798 $row[
'amountht'] = ($row[
'task_duration'] / 3600) * $row[
'thm'];
800 $row[
'amountttc'] =
price2num($row[
'amountht'] * (1 + ($defaultvat / 100)),
'MT');
802 $row[
'amountht'] = 0;
803 $row[
'amountttc'] = 0;
809 foreach ($tmparray as $key => $val) {
811 $listlinestasktime->setVars($key, $val,
true,
'UTF-8');
812 }
catch (OdfException $e) {
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 (OdfException $e) {
840 }
catch (SegmentException $e) {
844 $listtasksfiles->merge();
848 $odfHandler->mergeSegment($listlines);
849 }
catch (OdfException $e) {
850 $ExceptionTrace = $e->getTrace();
852 if ($ExceptionTrace[0][
'function'] !=
'setSegment') {
853 $this->error = $e->getMessage();
861 $listlines = $odfHandler->setSegment(
'projectfiles');
864 $filearray =
dol_dir_list($upload_dir,
"files", 0,
'',
'(\.meta|_preview.*\.png)$',
'name', SORT_ASC, 1);
866 foreach ($filearray as $filedetail) {
870 foreach ($tmparray as $key => $val) {
872 $listlines->setVars($key, $val,
true,
'UTF-8');
873 }
catch (OdfException $e) {
875 }
catch (SegmentException $e) {
881 $odfHandler->mergeSegment($listlines);
882 }
catch (OdfException $e) {
883 $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 (OdfException $e) {
922 }
catch (SegmentException $e) {
928 $odfHandler->mergeSegment($listlines);
929 }
catch (OdfException $e) {
930 $this->error = $e->getMessage();
938 $listofreferent = array(
940 'title' =>
"ListProposalsAssociatedProject",
943 'test' => isModEnabled(
'propal') && $user->hasRight(
'propal',
'lire')
946 'title' =>
"ListOrdersAssociatedProject",
947 'class' =>
'Commande',
948 'table' =>
'commande',
949 'test' => isModEnabled(
'commande') && $user->hasRight(
'commande',
'lire')
952 'title' =>
"ListInvoicesAssociatedProject",
953 'class' =>
'Facture',
954 'table' =>
'facture',
955 'test' => isModEnabled(
'facture') && $user->hasRight(
'facture',
'lire')
957 'invoice_predefined' => array(
958 'title' =>
"ListPredefinedInvoicesAssociatedProject",
959 'class' =>
'FactureRec',
960 'table' =>
'facture_rec',
961 'test' => isModEnabled(
'facture') && $user->hasRight(
'facture',
'lire')
963 'proposal_supplier' => array(
964 'title' =>
"ListSupplierProposalsAssociatedProject",
965 'class' =>
'SupplierProposal',
966 'table' =>
'supplier_proposal',
967 'test' => isModEnabled(
'supplier_proposal') && $user->rights->supplier_proposal->lire
969 'order_supplier' => array(
970 'title' =>
"ListSupplierOrdersAssociatedProject",
971 'table' =>
'commande_fournisseur',
972 'class' =>
'CommandeFournisseur',
973 'test' => (isModEnabled(
"fournisseur") && !
getDolGlobalString(
'MAIN_USE_NEW_SUPPLIERMOD') && $user->rights->fournisseur->commande->lire) || (isModEnabled(
"supplier_order") && $user->rights->supplier_order->lire)
975 'invoice_supplier' => array(
976 'title' =>
"ListSupplierInvoicesAssociatedProject",
977 'table' =>
'facture_fourn',
978 'class' =>
'FactureFournisseur',
979 'test' => (isModEnabled(
"fournisseur") && !
getDolGlobalString(
'MAIN_USE_NEW_SUPPLIERMOD') && $user->rights->fournisseur->facture->lire) || (isModEnabled(
"supplier_invoice") && $user->rights->supplier_invoice->lire)
982 'title' =>
"ListContractAssociatedProject",
983 'class' =>
'Contrat',
984 'table' =>
'contrat',
985 'test' => isModEnabled(
'contrat') && $user->hasRight(
'contrat',
'lire')
987 'intervention' => array(
988 'title' =>
"ListFichinterAssociatedProject",
989 'class' =>
'Fichinter',
990 'table' =>
'fichinter',
991 'disableamount' => 1,
992 'test' => isModEnabled(
'ficheinter') && $user->hasRight(
'ficheinter',
'lire')
995 'title' =>
"ListShippingAssociatedProject",
996 'class' =>
'Expedition',
997 'table' =>
'expedition',
998 'disableamount' => 1,
999 'test' => isModEnabled(
'expedition') && $user->rights->expedition->lire
1002 'title' =>
"ListTripAssociatedProject",
1003 'class' =>
'Deplacement',
1004 'table' =>
'deplacement',
1005 'disableamount' => 1,
1006 'test' => isModEnabled(
'deplacement') && $user->rights->deplacement->lire
1008 'expensereport' => array(
1009 'title' =>
"ListExpenseReportsAssociatedProject",
1010 'class' =>
'ExpenseReportLine',
1011 'table' =>
'expensereport_det',
1012 'test' => isModEnabled(
'expensereport') && $user->rights->expensereport->lire
1014 'donation' => array(
1015 'title' =>
"ListDonationsAssociatedProject",
1018 'test' => isModEnabled(
'don') && $user->rights->don->lire
1021 'title' =>
"ListLoanAssociatedProject",
1024 'test' => isModEnabled(
'loan') && $user->rights->loan->read
1026 'chargesociales' => array(
1027 'title' =>
"ListSocialContributionAssociatedProject",
1028 'class' =>
'ChargeSociales',
1029 'table' =>
'chargesociales',
1030 'urlnew' => DOL_URL_ROOT.
'/compta/sociales/card.php?action=create&projectid='.$object->id,
1031 'test' => isModEnabled(
'tax') && $user->rights->tax->charges->lire
1033 'stock_mouvement' => array(
1034 'title' =>
"ListMouvementStockProject",
1035 'class' =>
'MouvementStock',
1036 'table' =>
'stock_mouvement',
1037 'test' => (isModEnabled(
'stock') && $user->rights->stock->mouvement->lire &&
getDolGlobalString(
'STOCK_MOVEMENT_INTO_PROJECT_OVERVIEW'))
1040 'title' =>
"ListActionsAssociatedProject",
1041 'class' =>
'ActionComm',
1042 'table' =>
'actioncomm',
1043 'disableamount' => 1,
1044 'test' => isModEnabled(
'agenda') && $user->rights->agenda->allactions->lire
1050 $listlines = $odfHandler->setSegment(
'projectrefs');
1052 foreach ($listofreferent as $keyref => $valueref) {
1053 $title = $valueref[
'title'];
1054 $tablename = $valueref[
'table'];
1055 $classname = $valueref[
'class'];
1056 $qualified = $valueref[
'test'];
1058 $elementarray = $object->get_element_list($keyref, $tablename);
1059 if (count($elementarray) > 0 && is_array($elementarray)) {
1062 $num = count($elementarray);
1063 for ($i = 0; $i < $num; $i++) {
1064 $ref_array = array();
1065 $ref_array[
'type'] = $langs->trans($classname);
1067 $element =
new $classname($this->db);
1068 $element->fetch($elementarray[$i]);
1069 $element->fetch_thirdparty();
1072 $ref_array[
'ref'] = $element->ref;
1075 $dateref = $element->date;
1076 if (empty($dateref)) {
1077 $dateref = $element->datep;
1079 if (empty($dateref)) {
1080 $dateref = $element->date_contrat;
1082 $ref_array[
'date'] = $dateref;
1085 if (is_object($element->thirdparty)) {
1086 $ref_array[
'socname'] = $element->thirdparty->name;
1088 $ref_array[
'socname'] =
'';
1092 if (empty($valueref[
'disableamount'])) {
1093 if (!empty($element->total_ht)) {
1094 $ref_array[
'amountht'] = $element->total_ht;
1095 $ref_array[
'amountttc'] = $element->total_ttc;
1097 $ref_array[
'amountht'] = 0;
1098 $ref_array[
'amountttc'] = 0;
1101 $ref_array[
'amountht'] =
'';
1102 $ref_array[
'amountttc'] =
'';
1105 $ref_array[
'status'] = $element->getLibStatut(0);
1109 foreach ($tmparray as $key => $val) {
1111 $listlines->setVars($key, $val,
true,
'UTF-8');
1112 }
catch (OdfException $e) {
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);