496 global $user, $langs, $conf, $mysoc, $hookmanager;
498 if (empty($srctemplatepath)) {
499 dol_syslog(
"doc_generic_odt::write_file parameter srctemplatepath empty", LOG_WARNING);
504 if (!is_object($hookmanager)) {
505 include_once DOL_DOCUMENT_ROOT.
'/core/class/hookmanager.class.php';
508 $hookmanager->initHooks(array(
'odtgeneration'));
511 if (!is_object($outputlangs)) {
512 $outputlangs = $langs;
514 $sav_charset_output = $outputlangs->charset_output;
515 $outputlangs->charset_output =
'UTF-8';
518 $outputlangs->loadLangs(array(
"main",
"dict",
"companies",
"projects"));
520 if ($conf->project->dir_output) {
534 $dir = $conf->project->dir_output;
536 if (!preg_match(
'/specimen/i', $objectref)) {
537 $dir .=
"/".$objectref;
539 $file = $dir.
"/".$objectref.
".odt";
541 if (!file_exists($dir)) {
543 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);
548 if (file_exists($dir)) {
550 $newfile = basename($srctemplatepath);
551 $newfiletmp = preg_replace(
'/\.od[ts]/i',
'', $newfile);
552 $newfiletmp = preg_replace(
'/template_/i',
'', $newfiletmp);
553 $newfiletmp = preg_replace(
'/modele_/i',
'', $newfiletmp);
554 $newfiletmp = $objectref .
'_' . $newfiletmp;
557 $newfileformat = substr($newfile, strrpos($newfile,
'.') + 1);
560 if ($format ==
'1') {
561 $format =
'%Y%m%d%H%M%S';
565 $filename = $newfiletmp .
'.' . $newfileformat;
567 $file = $dir .
'/' . $filename;
574 if (!is_writable($conf->project->dir_temp)) {
575 $this->error = $langs->transnoentities(
"ErrorFailedToWriteInTempDirectory", $conf->project->dir_temp);
576 dol_syslog(
'Error in write_file: ' . $this->error, LOG_ERR);
582 $arrayidcontact =
$object->getIdContact(
'external',
'PROJECTLEADER');
583 if (count($arrayidcontact) > 0) {
585 $result =
$object->fetch_contact($arrayidcontact[0]);
589 $contactobject =
null;
590 if (!empty($usecontact)) {
592 $contactobject =
$object->contact;
595 $socobject =
$object->thirdparty;
598 $substitutionarray = array(
599 '__FROM_NAME__' => $this->emetteur->name,
600 '__FROM_EMAIL__' => $this->emetteur->email,
604 $parameters = array(
'file'=>$file,
'object'=>
$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$substitutionarray);
605 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
608 require_once ODTPHP_PATH.
'odf.php';
610 $odfHandler =
new Odf(
613 'PATH_TO_TMP' => $conf->project->dir_temp,
615 'DELIMITER_LEFT' =>
'{',
616 'DELIMITER_RIGHT' =>
'}'
620 $this->error = $e->getMessage();
637 $array_project_contact = array();
638 if ($usecontact && is_object($contactobject)) {
642 $tmparray = array_merge($substitutionarray, $array_object_from_properties, $array_user, $array_soc, $array_thirdparty, $array_objet, $array_other, $array_project_contact);
646 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>
$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
647 $reshook = $hookmanager->executeHooks(
'ODTSubstitution', $parameters, $this, $action);
649 foreach ($tmparray as $key => $value) {
651 if (preg_match(
'/logo$/', $key)) {
652 if (file_exists($value)) {
653 $odfHandler->setImage($key, $value);
655 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
658 $odfHandler->setVars($key, $value,
true,
'UTF-8');
660 }
catch (OdfException $e) {
667 $listlines = $odfHandler->setSegment(
'tasks');
669 $taskstatic =
new Task($this->db);
677 $tasksarray = $taskstatic->getTasksArray(0, 0,
$object->id, $socid, 0);
680 foreach ($tasksarray as $task) {
683 foreach ($tmparray as $key => $val) {
685 $listlines->setVars($key, $val,
true,
'UTF-8');
686 }
catch (SegmentException $e) {
691 $taskobj =
new Task($this->db);
692 $taskobj->fetch($task->id);
695 $sourcearray = array(
'internal',
'external');
696 $contact_arrray = array();
697 foreach ($sourcearray as $source) {
698 $contact_temp = $taskobj->liste_contact(-1, $source);
699 if ((is_array($contact_temp) && count($contact_temp) > 0)) {
700 $contact_arrray = array_merge($contact_arrray, $contact_temp);
703 if ((is_array($contact_arrray) && count($contact_arrray) > 0)) {
704 $listlinestaskres = $listlines->__get(
'tasksressources');
706 foreach ($contact_arrray as $contact) {
707 if ($contact[
'source'] ==
'internal') {
708 $objectdetail =
new User($this->db);
709 $objectdetail->fetch($contact[
'id']);
710 $contact[
'socname'] = $mysoc->name;
711 } elseif ($contact[
'source'] ==
'external') {
712 $objectdetail =
new Contact($this->db);
713 $objectdetail->fetch($contact[
'id']);
716 $soc->fetch($contact[
'socid']);
717 $contact[
'socname'] = $soc->name;
719 $contact[
'fullname'] = $objectdetail->getFullName($outputlangs, 1);
723 foreach ($tmparray as $key => $val) {
725 $listlinestaskres->setVars($key, $val,
true,
'UTF-8');
726 }
catch (SegmentException $e) {
730 $listlinestaskres->merge();
735 $sql =
"SELECT t.rowid, t.element_date as task_date, t.element_duration as task_duration, t.fk_user, t.note";
736 $sql .=
", u.lastname, u.firstname, t.thm";
737 $sql .=
" FROM ".MAIN_DB_PREFIX.
"element_time as t";
738 $sql .=
" , ".MAIN_DB_PREFIX.
"user as u";
739 $sql .=
" WHERE t.fk_element =".((int) $task->id);
740 $sql .=
" AND t.elementtype = 'task'";
741 $sql .=
" AND t.fk_user = u.rowid";
742 $sql .=
" ORDER BY t.element_date DESC";
744 $resql = $this->db->query($sql);
746 $num = $this->db->num_rows($resql);
750 $listlinestasktime = $listlines->__get(
'taskstimes');
753 $row[
'task_date'] =
'';
754 $row[
'task_duration'] =
'';
755 $row[
'$tasktime'] =
'';
757 $row[
'fk_user'] =
'';
759 $row[
'firstname'] =
'';
760 $row[
'fullcivname'] =
'';
761 $row[
'amountht'] =
'';
762 $row[
'amountttc'] =
'';
765 foreach ($tmparray as $key => $val) {
767 $listlinestasktime->setVars($key, $val,
true,
'UTF-8');
768 }
catch (SegmentException $e) {
772 $listlinestasktime->merge();
775 $row = $this->db->fetch_array($resql);
776 if (!empty($row[
'fk_user'])) {
777 $objectdetail =
new User($this->db);
778 $objectdetail->fetch($row[
'fk_user']);
779 $row[
'fullcivname'] = $objectdetail->getFullName($outputlangs, 1);
781 $row[
'fullcivname'] =
'';
784 if (!empty($row[
'thm'])) {
785 $row[
'amountht'] = ($row[
'task_duration'] / 3600) * $row[
'thm'];
787 $row[
'amountttc'] =
price2num($row[
'amountht'] * (1 + ($defaultvat / 100)),
'MT');
789 $row[
'amountht'] = 0;
790 $row[
'amountttc'] = 0;
796 foreach ($tmparray as $key => $val) {
798 $listlinestasktime->setVars($key, $val,
true,
'UTF-8');
799 }
catch (SegmentException $e) {
803 $listlinestasktime->merge();
806 $this->db->free($resql);
811 $listtasksfiles = $listlines->__get(
'tasksfiles');
814 $filearray =
dol_dir_list($upload_dir,
"files", 0,
'',
'(\.meta|_preview.*\.png)$',
'name', SORT_ASC, 1);
817 foreach ($filearray as $filedetail) {
820 foreach ($tmparray as $key => $val) {
822 $listtasksfiles->setVars($key, $val,
true,
'UTF-8');
823 }
catch (SegmentException $e) {
827 $listtasksfiles->merge();
831 $odfHandler->mergeSegment($listlines);
832 }
catch (OdfException $e) {
833 $ExceptionTrace = $e->getTrace();
835 if ($ExceptionTrace[0][
'function'] !=
'setSegment') {
836 $this->error = $e->getMessage();
844 $listlines = $odfHandler->setSegment(
'projectfiles');
847 $filearray =
dol_dir_list($upload_dir,
"files", 0,
'',
'(\.meta|_preview.*\.png)$',
'name', SORT_ASC, 1);
849 foreach ($filearray as $filedetail) {
853 foreach ($tmparray as $key => $val) {
855 $listlines->setVars($key, $val,
true,
'UTF-8');
856 }
catch (SegmentException $e) {
862 $odfHandler->mergeSegment($listlines);
863 }
catch (OdfException $e) {
864 $this->error = $e->getMessage();
870 $sourcearray = array(
'internal',
'external');
871 $contact_arrray = array();
872 foreach ($sourcearray as $source) {
873 $contact_temp =
$object->liste_contact(-1, $source);
874 if ((is_array($contact_temp) && count($contact_temp) > 0)) {
875 $contact_arrray = array_merge($contact_arrray, $contact_temp);
878 if ((is_array($contact_arrray) && count($contact_arrray) > 0)) {
880 $listlines = $odfHandler->setSegment(
'projectcontacts');
882 foreach ($contact_arrray as $contact) {
883 if ($contact[
'source'] ==
'internal') {
884 $objectdetail =
new User($this->db);
885 $objectdetail->fetch($contact[
'id']);
886 $contact[
'socname'] = $mysoc->name;
887 } elseif ($contact[
'source'] ==
'external') {
888 $objectdetail =
new Contact($this->db);
889 $objectdetail->fetch($contact[
'id']);
892 $soc->fetch($contact[
'socid']);
893 $contact[
'socname'] = $soc->name;
895 $contact[
'fullname'] = $objectdetail->getFullName($outputlangs, 1);
898 foreach ($tmparray as $key => $val) {
900 $listlines->setVars($key, $val,
true,
'UTF-8');
901 }
catch (SegmentException $e) {
907 $odfHandler->mergeSegment($listlines);
908 }
catch (OdfException $e) {
909 $this->error = $e->getMessage();
917 $listofreferent = array(
919 'title' =>
"ListProposalsAssociatedProject",
922 'test' => isModEnabled(
'propal') && $user->hasRight(
'propal',
'lire')
925 'title' =>
"ListOrdersAssociatedProject",
926 'class' =>
'Commande',
927 'table' =>
'commande',
928 'test' => isModEnabled(
'order') && $user->hasRight(
'commande',
'lire')
931 'title' =>
"ListInvoicesAssociatedProject",
932 'class' =>
'Facture',
933 'table' =>
'facture',
934 'test' => isModEnabled(
'invoice') && $user->hasRight(
'facture',
'lire')
936 'invoice_predefined' => array(
937 'title' =>
"ListPredefinedInvoicesAssociatedProject",
938 'class' =>
'FactureRec',
939 'table' =>
'facture_rec',
940 'test' => isModEnabled(
'invoice') && $user->hasRight(
'facture',
'lire')
942 'proposal_supplier' => array(
943 'title' =>
"ListSupplierProposalsAssociatedProject",
944 'class' =>
'SupplierProposal',
945 'table' =>
'supplier_proposal',
946 'test' => isModEnabled(
'supplier_proposal') && $user->hasRight(
'supplier_proposal',
'lire')
948 'order_supplier' => array(
949 'title' =>
"ListSupplierOrdersAssociatedProject",
950 'table' =>
'commande_fournisseur',
951 'class' =>
'CommandeFournisseur',
952 'test' => (isModEnabled(
"fournisseur") && !
getDolGlobalString(
'MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight(
'fournisseur',
'commande',
'lire')) || (isModEnabled(
"supplier_order") && $user->hasRight(
'supplier_order',
'lire'))
954 'invoice_supplier' => array(
955 'title' =>
"ListSupplierInvoicesAssociatedProject",
956 'table' =>
'facture_fourn',
957 'class' =>
'FactureFournisseur',
958 'test' => (isModEnabled(
"fournisseur") && !
getDolGlobalString(
'MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight(
'fournisseur',
'facture',
'lire')) || (isModEnabled(
"supplier_invoice") && $user->hasRight(
'supplier_invoice',
'lire'))
961 'title' =>
"ListContractAssociatedProject",
962 'class' =>
'Contrat',
963 'table' =>
'contrat',
964 'test' => isModEnabled(
'contract') && $user->hasRight(
'contrat',
'lire')
966 'intervention' => array(
967 'title' =>
"ListFichinterAssociatedProject",
968 'class' =>
'Fichinter',
969 'table' =>
'fichinter',
970 'disableamount' => 1,
971 'test' => isModEnabled(
'intervention') && $user->hasRight(
'ficheinter',
'lire')
974 'title' =>
"ListShippingAssociatedProject",
975 'class' =>
'Expedition',
976 'table' =>
'expedition',
977 'disableamount' => 1,
978 'test' => isModEnabled(
'shipping') && $user->hasRight(
'expedition',
'lire')
981 'title' =>
"ListTripAssociatedProject",
982 'class' =>
'Deplacement',
983 'table' =>
'deplacement',
984 'disableamount' => 1,
985 'test' => isModEnabled(
'deplacement') && $user->hasRight(
'deplacement',
'lire')
987 'expensereport' => array(
988 'title' =>
"ListExpenseReportsAssociatedProject",
989 'class' =>
'ExpenseReportLine',
990 'table' =>
'expensereport_det',
991 'test' => isModEnabled(
'expensereport') && $user->hasRight(
'expensereport',
'lire')
994 'title' =>
"ListDonationsAssociatedProject",
997 'test' => isModEnabled(
'don') && $user->hasRight(
'don',
'lire')
1000 'title' =>
"ListLoanAssociatedProject",
1003 'test' => isModEnabled(
'loan') && $user->hasRight(
'loan',
'read')
1005 'chargesociales' => array(
1006 'title' =>
"ListSocialContributionAssociatedProject",
1007 'class' =>
'ChargeSociales',
1008 'table' =>
'chargesociales',
1009 'urlnew' => DOL_URL_ROOT.
'/compta/sociales/card.php?action=create&projectid='.
$object->id,
1010 'test' => isModEnabled(
'tax') && $user->hasRight(
'tax',
'charges',
'lire')
1012 'stock_mouvement' => array(
1013 'title' =>
"ListMouvementStockProject",
1014 'class' =>
'MouvementStock',
1015 'table' =>
'stock_mouvement',
1016 'test' => (isModEnabled(
'stock') && $user->hasRight(
'stock',
'mouvement',
'lire') &&
getDolGlobalString(
'STOCK_MOVEMENT_INTO_PROJECT_OVERVIEW'))
1019 'title' =>
"ListActionsAssociatedProject",
1020 'class' =>
'ActionComm',
1021 'table' =>
'actioncomm',
1022 'disableamount' => 1,
1023 'test' => isModEnabled(
'agenda') && $user->hasRight(
'agenda',
'allactions',
'lire')
1029 $listlines = $odfHandler->setSegment(
'projectrefs');
1031 foreach ($listofreferent as $keyref => $valueref) {
1032 $title = $valueref[
'title'];
1033 $tablename = $valueref[
'table'];
1034 $classname = $valueref[
'class'];
1035 $qualified = $valueref[
'test'];
1037 $elementarray =
$object->get_element_list($keyref, $tablename);
1038 if (count($elementarray) > 0 && is_array($elementarray)) {
1041 $num = count($elementarray);
1042 for ($i = 0; $i < $num; $i++) {
1043 $ref_array = array();
1044 $ref_array[
'type'] = $langs->trans($classname);
1046 $element =
new $classname($this->db);
1047 $element->fetch($elementarray[$i]);
1048 $element->fetch_thirdparty();
1051 $ref_array[
'ref'] = $element->ref;
1054 $dateref = $element->date;
1055 if (empty($dateref)) {
1056 $dateref = $element->datep;
1058 if (empty($dateref)) {
1059 $dateref = $element->date_contrat;
1061 $ref_array[
'date'] = $dateref;
1064 if (is_object($element->thirdparty)) {
1065 $ref_array[
'socname'] = $element->thirdparty->name;
1067 $ref_array[
'socname'] =
'';
1071 if (empty($valueref[
'disableamount'])) {
1072 if (!empty($element->total_ht)) {
1073 $ref_array[
'amountht'] = $element->total_ht;
1074 $ref_array[
'amountttc'] = $element->total_ttc;
1076 $ref_array[
'amountht'] = 0;
1077 $ref_array[
'amountttc'] = 0;
1080 $ref_array[
'amountht'] =
'';
1081 $ref_array[
'amountttc'] =
'';
1084 $ref_array[
'status'] = $element->getLibStatut(0);
1088 foreach ($tmparray as $key => $val) {
1090 $listlines->setVars($key, $val,
true,
'UTF-8');
1091 }
catch (SegmentException $e) {
1095 $listlines->merge();
1099 $odfHandler->mergeSegment($listlines);
1101 }
catch (OdfExceptionSegmentNotFound $e) {
1103 }
catch (OdfException $e) {
1104 $this->error = $e->getMessage();
1110 $tmparray = $outputlangs->get_translations_for_substitutions();
1111 foreach ($tmparray as $key => $value) {
1113 $odfHandler->setVars($key, $value,
true,
'UTF-8');
1114 }
catch (OdfException $e) {
1120 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>
$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
1121 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
1127 $odfHandler->exportAsAttachedPDF($file);
1129 $this->error = $e->getMessage();
1134 $odfHandler->saveToDisk($file);
1136 $this->error = $e->getMessage();
1141 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>
$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
1142 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
1148 $this->result = array(
'fullpath'=>$file);
1152 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);