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();
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);
655 foreach ($tmparray as $key => $value) {
657 if (preg_match(
'/logo$/', $key)) {
658 if (file_exists($value)) {
659 $odfHandler->setImage($key, $value);
661 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
664 $odfHandler->setVars($key, $value,
true,
'UTF-8');
666 }
catch (OdfException $e) {
673 $listlines = $odfHandler->setSegment(
'tasks');
675 $taskstatic =
new Task($this->db);
683 $tasksarray = $taskstatic->getTasksArray(
null,
null,
$object->id, $socid, 0);
686 foreach ($tasksarray as $task) {
689 foreach ($tmparray as $key => $val) {
691 $listlines->setVars($key, $val,
true,
'UTF-8');
692 }
catch (SegmentException $e) {
697 $taskobj =
new Task($this->db);
698 $taskobj->fetch($task->id);
701 $sourcearray = array(
'internal',
'external');
702 $contact_arrray = array();
703 foreach ($sourcearray as $source) {
704 $contact_temp = $taskobj->liste_contact(-1, $source);
705 if ((is_array($contact_temp) && count($contact_temp) > 0)) {
706 $contact_arrray = array_merge($contact_arrray, $contact_temp);
709 if ((is_array($contact_arrray) && count($contact_arrray) > 0)) {
710 $listlinestaskres = $listlines->__get(
'tasksressources');
712 foreach ($contact_arrray as $contact) {
713 if ($contact[
'source'] ==
'internal') {
714 $objectdetail =
new User($this->db);
715 $objectdetail->fetch($contact[
'id']);
716 $contact[
'socname'] = $mysoc->name;
717 } elseif ($contact[
'source'] ==
'external') {
718 $objectdetail =
new Contact($this->db);
719 $objectdetail->fetch($contact[
'id']);
722 $soc->fetch($contact[
'socid']);
723 $contact[
'socname'] = $soc->name;
725 dol_syslog(get_class().
'::'.__METHOD__.
' Unexpected contact source:'.$contact[
'source'], LOG_ERR);
728 $contact[
'fullname'] = $objectdetail->getFullName($outputlangs, 1);
732 foreach ($tmparray as $key => $val) {
734 $listlinestaskres->setVars($key, $val,
true,
'UTF-8');
735 }
catch (SegmentException $e) {
739 $listlinestaskres->merge();
744 $sql =
"SELECT t.rowid, t.element_date as task_date, t.element_duration as task_duration, t.fk_user, t.note";
745 $sql .=
", u.lastname, u.firstname, t.thm";
746 $sql .=
" FROM ".MAIN_DB_PREFIX.
"element_time as t";
747 $sql .=
" , ".MAIN_DB_PREFIX.
"user as u";
748 $sql .=
" WHERE t.fk_element =".((int) $task->id);
749 $sql .=
" AND t.elementtype = 'task'";
750 $sql .=
" AND t.fk_user = u.rowid";
751 $sql .=
" ORDER BY t.element_date DESC";
753 $resql = $this->db->query($sql);
755 $num = $this->db->num_rows($resql);
759 $listlinestasktime = $listlines->__get(
'taskstimes');
762 $row[
'task_date'] = 0;
763 $row[
'task_duration'] = 0;
768 $row[
'firstname'] =
'';
769 $row[
'fullcivname'] =
'';
770 $row[
'amountht'] = 0;
771 $row[
'amountttc'] = 0;
774 foreach ($tmparray as $key => $val) {
776 $listlinestasktime->setVars($key, $val,
true,
'UTF-8');
777 }
catch (SegmentException $e) {
781 $listlinestasktime->merge();
784 $row = $this->db->fetch_array($resql);
785 if (!empty($row[
'fk_user'])) {
786 $objectdetail =
new User($this->db);
787 $objectdetail->fetch($row[
'fk_user']);
788 $row[
'fullcivname'] = $objectdetail->getFullName($outputlangs, 1);
790 $row[
'fullcivname'] =
'';
793 if (!empty($row[
'thm'])) {
794 $row[
'amountht'] = ($row[
'task_duration'] / 3600) * $row[
'thm'];
796 $row[
'amountttc'] =
price2num($row[
'amountht'] * (1 + ($defaultvat / 100)),
'MT');
798 $row[
'amountht'] = 0;
799 $row[
'amountttc'] = 0;
805 foreach ($tmparray as $key => $val) {
807 $listlinestasktime->setVars($key, $val,
true,
'UTF-8');
808 }
catch (SegmentException $e) {
812 $listlinestasktime->merge();
815 $this->db->free($resql);
820 $listtasksfiles = $listlines->__get(
'tasksfiles');
823 $filearray =
dol_dir_list($upload_dir,
"files", 0,
'',
'(\.meta|_preview.*\.png)$',
'name', SORT_ASC, 1);
826 foreach ($filearray as $filedetail) {
829 foreach ($tmparray as $key => $val) {
831 $listtasksfiles->setVars($key, $val,
true,
'UTF-8');
832 }
catch (SegmentException $e) {
836 $listtasksfiles->merge();
840 $odfHandler->mergeSegment($listlines);
841 }
catch (OdfException $e) {
842 $ExceptionTrace = $e->getTrace();
844 if ($ExceptionTrace[0][
'function'] !=
'setSegment') {
845 $this->error = $e->getMessage();
853 $listlines = $odfHandler->setSegment(
'projectfiles');
856 $filearray =
dol_dir_list($upload_dir,
"files", 0,
'',
'(\.meta|_preview.*\.png)$',
'name', SORT_ASC, 1);
858 foreach ($filearray as $filedetail) {
862 foreach ($tmparray as $key => $val) {
864 $listlines->setVars($key, $val,
true,
'UTF-8');
865 }
catch (SegmentException $e) {
871 $odfHandler->mergeSegment($listlines);
872 }
catch (OdfException $e) {
873 $this->error = $e->getMessage();
879 $sourcearray = array(
'internal',
'external');
880 $contact_arrray = array();
881 foreach ($sourcearray as $source) {
882 $contact_temp =
$object->liste_contact(-1, $source);
883 if ((is_array($contact_temp) && count($contact_temp) > 0)) {
884 $contact_arrray = array_merge($contact_arrray, $contact_temp);
887 if ((is_array($contact_arrray) && count($contact_arrray) > 0)) {
889 $listlines = $odfHandler->setSegment(
'projectcontacts');
891 foreach ($contact_arrray as $contact) {
892 if ($contact[
'source'] ==
'internal') {
893 $objectdetail =
new User($this->db);
894 $objectdetail->fetch($contact[
'id']);
895 $contact[
'socname'] = $mysoc->name;
896 } elseif ($contact[
'source'] ==
'external') {
897 $objectdetail =
new Contact($this->db);
898 $objectdetail->fetch($contact[
'id']);
901 $soc->fetch($contact[
'socid']);
902 $contact[
'socname'] = $soc->name;
904 $contact[
'fullname'] = $objectdetail->getFullName($outputlangs, 1);
907 foreach ($tmparray as $key => $val) {
909 $listlines->setVars($key, $val,
true,
'UTF-8');
910 }
catch (SegmentException $e) {
916 $odfHandler->mergeSegment($listlines);
917 }
catch (OdfException $e) {
918 $this->error = $e->getMessage();
926 $listofreferent = array(
928 'title' =>
"ListProposalsAssociatedProject",
931 'test' => isModEnabled(
'propal') && $user->hasRight(
'propal',
'lire')
934 'title' =>
"ListOrdersAssociatedProject",
935 'class' =>
'Commande',
936 'table' =>
'commande',
937 'test' => isModEnabled(
'order') && $user->hasRight(
'commande',
'lire')
940 'title' =>
"ListInvoicesAssociatedProject",
941 'class' =>
'Facture',
942 'table' =>
'facture',
943 'test' => isModEnabled(
'invoice') && $user->hasRight(
'facture',
'lire')
945 'invoice_predefined' => array(
946 'title' =>
"ListPredefinedInvoicesAssociatedProject",
947 'class' =>
'FactureRec',
948 'table' =>
'facture_rec',
949 'test' => isModEnabled(
'invoice') && $user->hasRight(
'facture',
'lire')
951 'proposal_supplier' => array(
952 'title' =>
"ListSupplierProposalsAssociatedProject",
953 'class' =>
'SupplierProposal',
954 'table' =>
'supplier_proposal',
955 'test' => isModEnabled(
'supplier_proposal') && $user->hasRight(
'supplier_proposal',
'lire')
957 'order_supplier' => array(
958 'title' =>
"ListSupplierOrdersAssociatedProject",
959 'table' =>
'commande_fournisseur',
960 'class' =>
'CommandeFournisseur',
961 'test' => (isModEnabled(
"fournisseur") && !
getDolGlobalString(
'MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight(
'fournisseur',
'commande',
'lire')) || (isModEnabled(
"supplier_order") && $user->hasRight(
'supplier_order',
'lire'))
963 'invoice_supplier' => array(
964 'title' =>
"ListSupplierInvoicesAssociatedProject",
965 'table' =>
'facture_fourn',
966 'class' =>
'FactureFournisseur',
967 'test' => (isModEnabled(
"fournisseur") && !
getDolGlobalString(
'MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight(
'fournisseur',
'facture',
'lire')) || (isModEnabled(
"supplier_invoice") && $user->hasRight(
'supplier_invoice',
'lire'))
970 'title' =>
"ListContractAssociatedProject",
971 'class' =>
'Contrat',
972 'table' =>
'contrat',
973 'test' => isModEnabled(
'contract') && $user->hasRight(
'contrat',
'lire')
975 'intervention' => array(
976 'title' =>
"ListFichinterAssociatedProject",
977 'class' =>
'Fichinter',
978 'table' =>
'fichinter',
979 'disableamount' => 1,
980 'test' => isModEnabled(
'intervention') && $user->hasRight(
'ficheinter',
'lire')
983 'title' =>
"ListShippingAssociatedProject",
984 'class' =>
'Expedition',
985 'table' =>
'expedition',
986 'disableamount' => 1,
987 'test' => isModEnabled(
'shipping') && $user->hasRight(
'expedition',
'lire')
990 'title' =>
"ListTripAssociatedProject",
991 'class' =>
'Deplacement',
992 'table' =>
'deplacement',
993 'disableamount' => 1,
994 'test' => isModEnabled(
'deplacement') && $user->hasRight(
'deplacement',
'lire')
996 'expensereport' => array(
997 'title' =>
"ListExpenseReportsAssociatedProject",
998 'class' =>
'ExpenseReportLine',
999 'table' =>
'expensereport_det',
1000 'test' => isModEnabled(
'expensereport') && $user->hasRight(
'expensereport',
'lire')
1002 'donation' => array(
1003 'title' =>
"ListDonationsAssociatedProject",
1006 'test' => isModEnabled(
'don') && $user->hasRight(
'don',
'lire')
1009 'title' =>
"ListLoanAssociatedProject",
1012 'test' => isModEnabled(
'loan') && $user->hasRight(
'loan',
'read')
1014 'chargesociales' => array(
1015 'title' =>
"ListSocialContributionAssociatedProject",
1016 'class' =>
'ChargeSociales',
1017 'table' =>
'chargesociales',
1018 'urlnew' => DOL_URL_ROOT.
'/compta/sociales/card.php?action=create&projectid='.
$object->id,
1019 'test' => isModEnabled(
'tax') && $user->hasRight(
'tax',
'charges',
'lire')
1021 'stock_mouvement' => array(
1022 'title' =>
"ListMouvementStockProject",
1023 'class' =>
'MouvementStock',
1024 'table' =>
'stock_mouvement',
1025 'test' => (isModEnabled(
'stock') && $user->hasRight(
'stock',
'mouvement',
'lire') &&
getDolGlobalString(
'STOCK_MOVEMENT_INTO_PROJECT_OVERVIEW'))
1028 'title' =>
"ListActionsAssociatedProject",
1029 'class' =>
'ActionComm',
1030 'table' =>
'actioncomm',
1031 'disableamount' => 1,
1032 'test' => isModEnabled(
'agenda') && $user->hasRight(
'agenda',
'allactions',
'lire')
1038 $listlines = $odfHandler->setSegment(
'projectrefs');
1040 foreach ($listofreferent as $keyref => $valueref) {
1041 $title = $valueref[
'title'];
1042 $tablename = $valueref[
'table'];
1043 $classname = $valueref[
'class'];
1044 $qualified = $valueref[
'test'];
1046 $elementarray =
$object->get_element_list($keyref, $tablename);
1047 if (count($elementarray) > 0 && is_array($elementarray)) {
1050 $num = count($elementarray);
1051 for ($i = 0; $i < $num; $i++) {
1052 $ref_array = array();
1053 $ref_array[
'type'] = (string) $langs->trans($classname);
1055 $element =
new $classname($this->db);
1056 $element->fetch($elementarray[$i]);
1057 $element->fetch_thirdparty();
1060 $ref_array[
'ref'] = $element->ref;
1063 $dateref = $element->date;
1064 if (empty($dateref)) {
1065 $dateref = $element->datep;
1067 if (empty($dateref)) {
1068 $dateref = $element->date_contrat;
1070 $ref_array[
'date'] = (string) $dateref;
1073 if (is_object($element->thirdparty)) {
1074 $ref_array[
'socname'] = $element->thirdparty->name;
1076 $ref_array[
'socname'] =
'';
1080 if (empty($valueref[
'disableamount'])) {
1081 if (!empty($element->total_ht)) {
1082 $ref_array[
'amountht'] = $element->total_ht;
1083 $ref_array[
'amountttc'] = $element->total_ttc;
1085 $ref_array[
'amountht'] = 0;
1086 $ref_array[
'amountttc'] = 0;
1089 $ref_array[
'amountht'] =
'';
1090 $ref_array[
'amountttc'] =
'';
1093 $ref_array[
'status'] = $element->getLibStatut(0);
1097 foreach ($tmparray as $key => $val) {
1099 $listlines->setVars($key, $val,
true,
'UTF-8');
1100 }
catch (SegmentException $e) {
1104 $listlines->merge();
1108 $odfHandler->mergeSegment($listlines);
1110 }
catch (OdfExceptionSegmentNotFound $e) {
1112 }
catch (OdfException $e) {
1113 $this->error = $e->getMessage();
1119 $tmparray = $outputlangs->get_translations_for_substitutions();
1120 foreach ($tmparray as $key => $value) {
1122 $odfHandler->setVars($key, $value,
true,
'UTF-8');
1123 }
catch (OdfException $e) {
1129 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
1130 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
1136 $odfHandler->exportAsAttachedPDF($file);
1138 $this->error = $e->getMessage();
1143 $odfHandler->saveToDisk($file);
1145 $this->error = $e->getMessage();
1150 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
1151 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
1157 $this->result = array(
'fullpath' => $file);
1161 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);