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();
635 $substitutionarray = getCommonSubstitutionArray($outputlangs, 0,
null,
$object);
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);
662 foreach ($tmparray as $key => $value) {
664 if (preg_match(
'/logo$/', $key)) {
665 if (file_exists($value)) {
666 $odfHandler->setImage($key, $value, $ratio);
668 $odfHandler->setVars($key,
'ErrorFileNotFound',
true,
'UTF-8');
671 $odfHandler->setVars($key, $value,
true,
'UTF-8');
673 }
catch (OdfException $e) {
680 $listlines = $odfHandler->setSegment(
'tasks');
682 $taskstatic =
new Task($this->db);
690 $tasksarray = $taskstatic->getTasksArray(
null,
null,
$object->id, $socid, 0);
693 foreach ($tasksarray as $task) {
696 foreach ($tmparray as $key => $val) {
698 $listlines->setVars($key, $val,
true,
'UTF-8');
699 }
catch (SegmentException $e) {
704 $taskobj =
new Task($this->db);
705 $taskobj->fetch($task->id);
708 $sourcearray = array(
'internal',
'external');
709 $contact_arrray = array();
710 foreach ($sourcearray as $source) {
711 $contact_temp = $taskobj->liste_contact(-1, $source);
712 if ((is_array($contact_temp) && count($contact_temp) > 0)) {
713 $contact_arrray = array_merge($contact_arrray, $contact_temp);
716 if ((is_array($contact_arrray) && count($contact_arrray) > 0)) {
717 $listlinestaskres = $listlines->__get(
'tasksressources');
719 foreach ($contact_arrray as $contact) {
720 if ($contact[
'source'] ==
'internal') {
721 $objectdetail =
new User($this->db);
722 $objectdetail->fetch($contact[
'id']);
723 $contact[
'socname'] = $mysoc->name;
724 } elseif ($contact[
'source'] ==
'external') {
725 $objectdetail =
new Contact($this->db);
726 $objectdetail->fetch($contact[
'id']);
729 $soc->fetch($contact[
'socid']);
730 $contact[
'socname'] = $soc->name;
732 dol_syslog(get_class().
'::'.__METHOD__.
' Unexpected contact source:'.$contact[
'source'], LOG_ERR);
735 $contact[
'fullname'] = $objectdetail->getFullName($outputlangs, 1);
739 foreach ($tmparray as $key => $val) {
741 $listlinestaskres->setVars($key, $val,
true,
'UTF-8');
742 }
catch (SegmentException $e) {
746 $listlinestaskres->merge();
751 $sql =
"SELECT t.rowid, t.element_date as task_date, t.element_duration as task_duration, t.fk_user, t.note";
752 $sql .=
", u.lastname, u.firstname, t.thm";
753 $sql .=
" FROM ".MAIN_DB_PREFIX.
"element_time as t";
754 $sql .=
" , ".MAIN_DB_PREFIX.
"user as u";
755 $sql .=
" WHERE t.fk_element =".((int) $task->id);
756 $sql .=
" AND t.elementtype = 'task'";
757 $sql .=
" AND t.fk_user = u.rowid";
758 $sql .=
" ORDER BY t.element_date DESC";
760 $resql = $this->db->query($sql);
762 $num = $this->db->num_rows($resql);
766 $listlinestasktime = $listlines->__get(
'taskstimes');
769 $row[
'task_date'] = 0;
770 $row[
'task_duration'] = 0;
775 $row[
'firstname'] =
'';
776 $row[
'fullcivname'] =
'';
777 $row[
'amountht'] = 0;
778 $row[
'amountttc'] = 0;
781 foreach ($tmparray as $key => $val) {
783 $listlinestasktime->setVars($key, $val,
true,
'UTF-8');
784 }
catch (SegmentException $e) {
788 $listlinestasktime->merge();
791 $row = $this->db->fetch_array($resql);
792 if (!empty($row[
'fk_user'])) {
793 $objectdetail =
new User($this->db);
794 $objectdetail->fetch($row[
'fk_user']);
795 $row[
'fullcivname'] = $objectdetail->getFullName($outputlangs, 1);
797 $row[
'fullcivname'] =
'';
800 if (!empty($row[
'thm'])) {
801 $row[
'amountht'] = ($row[
'task_duration'] / 3600) * $row[
'thm'];
803 $row[
'amountttc'] =
price2num($row[
'amountht'] * (1 + ($defaultvat / 100)),
'MT');
805 $row[
'amountht'] = 0;
806 $row[
'amountttc'] = 0;
812 foreach ($tmparray as $key => $val) {
814 $listlinestasktime->setVars($key, $val,
true,
'UTF-8');
815 }
catch (SegmentException $e) {
819 $listlinestasktime->merge();
822 $this->db->free($resql);
827 $listtasksfiles = $listlines->__get(
'tasksfiles');
830 $filearray =
dol_dir_list($upload_dir,
"files", 0,
'',
'(\.meta|_preview.*\.png)$',
'name', SORT_ASC, 1);
833 foreach ($filearray as $filedetail) {
836 foreach ($tmparray as $key => $val) {
838 $listtasksfiles->setVars($key, $val,
true,
'UTF-8');
839 }
catch (SegmentException $e) {
843 $listtasksfiles->merge();
847 $odfHandler->mergeSegment($listlines);
848 }
catch (OdfException $e) {
849 $ExceptionTrace = $e->getTrace();
851 if ($ExceptionTrace[0][
'function'] !=
'setSegment') {
852 $this->error = $e->getMessage();
860 $listlines = $odfHandler->setSegment(
'projectfiles');
863 $filearray =
dol_dir_list($upload_dir,
"files", 0,
'',
'(\.meta|_preview.*\.png)$',
'name', SORT_ASC, 1);
865 foreach ($filearray as $filedetail) {
869 foreach ($tmparray as $key => $val) {
871 $listlines->setVars($key, $val,
true,
'UTF-8');
872 }
catch (SegmentException $e) {
878 $odfHandler->mergeSegment($listlines);
879 }
catch (OdfException $e) {
880 $this->error = $e->getMessage();
886 $sourcearray = array(
'internal',
'external');
887 $contact_arrray = array();
888 foreach ($sourcearray as $source) {
889 $contact_temp =
$object->liste_contact(-1, $source);
890 if ((is_array($contact_temp) && count($contact_temp) > 0)) {
891 $contact_arrray = array_merge($contact_arrray, $contact_temp);
894 if ((is_array($contact_arrray) && count($contact_arrray) > 0)) {
896 $listlines = $odfHandler->setSegment(
'projectcontacts');
898 foreach ($contact_arrray as $contact) {
899 if ($contact[
'source'] ==
'internal') {
900 $objectdetail =
new User($this->db);
901 $objectdetail->fetch($contact[
'id']);
902 $contact[
'socname'] = $mysoc->name;
903 } elseif ($contact[
'source'] ==
'external') {
904 $objectdetail =
new Contact($this->db);
905 $objectdetail->fetch($contact[
'id']);
908 $soc->fetch($contact[
'socid']);
909 $contact[
'socname'] = $soc->name;
911 $contact[
'fullname'] = $objectdetail->getFullName($outputlangs, 1);
914 foreach ($tmparray as $key => $val) {
916 $listlines->setVars($key, $val,
true,
'UTF-8');
917 }
catch (SegmentException $e) {
923 $odfHandler->mergeSegment($listlines);
924 }
catch (OdfException $e) {
925 $this->error = $e->getMessage();
933 $listofreferent = array(
935 'title' =>
"ListProposalsAssociatedProject",
938 'test' => isModEnabled(
'propal') && $user->hasRight(
'propal',
'lire')
941 'title' =>
"ListOrdersAssociatedProject",
942 'class' =>
'Commande',
943 'table' =>
'commande',
944 'test' => isModEnabled(
'order') && $user->hasRight(
'commande',
'lire')
947 'title' =>
"ListInvoicesAssociatedProject",
948 'class' =>
'Facture',
949 'table' =>
'facture',
950 'test' => isModEnabled(
'invoice') && $user->hasRight(
'facture',
'lire')
952 'invoice_predefined' => array(
953 'title' =>
"ListPredefinedInvoicesAssociatedProject",
954 'class' =>
'FactureRec',
955 'table' =>
'facture_rec',
956 'test' => isModEnabled(
'invoice') && $user->hasRight(
'facture',
'lire')
958 'proposal_supplier' => array(
959 'title' =>
"ListSupplierProposalsAssociatedProject",
960 'class' =>
'SupplierProposal',
961 'table' =>
'supplier_proposal',
962 'test' => isModEnabled(
'supplier_proposal') && $user->hasRight(
'supplier_proposal',
'lire')
964 'order_supplier' => array(
965 'title' =>
"ListSupplierOrdersAssociatedProject",
966 'table' =>
'commande_fournisseur',
967 'class' =>
'CommandeFournisseur',
968 'test' => (isModEnabled(
"fournisseur") && !
getDolGlobalString(
'MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight(
'fournisseur',
'commande',
'lire')) || (isModEnabled(
"supplier_order") && $user->hasRight(
'supplier_order',
'lire'))
970 'invoice_supplier' => array(
971 'title' =>
"ListSupplierInvoicesAssociatedProject",
972 'table' =>
'facture_fourn',
973 'class' =>
'FactureFournisseur',
974 'test' => (isModEnabled(
"fournisseur") && !
getDolGlobalString(
'MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight(
'fournisseur',
'facture',
'lire')) || (isModEnabled(
"supplier_invoice") && $user->hasRight(
'supplier_invoice',
'lire'))
977 'title' =>
"ListContractAssociatedProject",
978 'class' =>
'Contrat',
979 'table' =>
'contrat',
980 'test' => isModEnabled(
'contract') && $user->hasRight(
'contrat',
'lire')
982 'intervention' => array(
983 'title' =>
"ListFichinterAssociatedProject",
984 'class' =>
'Fichinter',
985 'table' =>
'fichinter',
986 'disableamount' => 1,
987 'test' => isModEnabled(
'intervention') && $user->hasRight(
'ficheinter',
'lire')
990 'title' =>
"ListShippingAssociatedProject",
991 'class' =>
'Expedition',
992 'table' =>
'expedition',
993 'disableamount' => 1,
994 'test' => isModEnabled(
'shipping') && $user->hasRight(
'expedition',
'lire')
997 'title' =>
"ListTripAssociatedProject",
998 'class' =>
'Deplacement',
999 'table' =>
'deplacement',
1000 'disableamount' => 1,
1001 'test' => isModEnabled(
'deplacement') && $user->hasRight(
'deplacement',
'lire')
1003 'expensereport' => array(
1004 'title' =>
"ListExpenseReportsAssociatedProject",
1005 'class' =>
'ExpenseReportLine',
1006 'table' =>
'expensereport_det',
1007 'test' => isModEnabled(
'expensereport') && $user->hasRight(
'expensereport',
'lire')
1009 'donation' => array(
1010 'title' =>
"ListDonationsAssociatedProject",
1013 'test' => isModEnabled(
'don') && $user->hasRight(
'don',
'lire')
1016 'title' =>
"ListLoanAssociatedProject",
1019 'test' => isModEnabled(
'loan') && $user->hasRight(
'loan',
'read')
1021 'chargesociales' => array(
1022 'title' =>
"ListSocialContributionAssociatedProject",
1023 'class' =>
'ChargeSociales',
1024 'table' =>
'chargesociales',
1025 'urlnew' => DOL_URL_ROOT.
'/compta/sociales/card.php?action=create&projectid='.
$object->id,
1026 'test' => isModEnabled(
'tax') && $user->hasRight(
'tax',
'charges',
'lire')
1028 'stock_mouvement' => array(
1029 'title' =>
"ListMouvementStockProject",
1030 'class' =>
'MouvementStock',
1031 'table' =>
'stock_mouvement',
1032 'test' => (isModEnabled(
'stock') && $user->hasRight(
'stock',
'mouvement',
'lire') &&
getDolGlobalString(
'STOCK_MOVEMENT_INTO_PROJECT_OVERVIEW'))
1035 'title' =>
"ListActionsAssociatedProject",
1036 'class' =>
'ActionComm',
1037 'table' =>
'actioncomm',
1038 'disableamount' => 1,
1039 'test' => isModEnabled(
'agenda') && $user->hasRight(
'agenda',
'allactions',
'lire')
1045 $listlines = $odfHandler->setSegment(
'projectrefs');
1047 foreach ($listofreferent as $keyref => $valueref) {
1048 $title = $valueref[
'title'];
1049 $tablename = $valueref[
'table'];
1050 $classname = $valueref[
'class'];
1051 $qualified = $valueref[
'test'];
1053 $elementarray =
$object->get_element_list($keyref, $tablename);
1054 if (count($elementarray) > 0 && is_array($elementarray)) {
1057 $num = count($elementarray);
1058 for ($i = 0; $i < $num; $i++) {
1059 $ref_array = array();
1060 $ref_array[
'type'] = (string) $langs->trans($classname);
1062 $element =
new $classname($this->db);
1063 $element->fetch((
int) $elementarray[$i]);
1064 $element->fetch_thirdparty();
1067 $ref_array[
'ref'] = $element->ref;
1070 $dateref = $element->date;
1071 if (empty($dateref)) {
1072 $dateref = $element->datep;
1074 if (empty($dateref)) {
1075 $dateref = $element->date_contrat;
1077 $ref_array[
'date'] = (string) $dateref;
1080 if (is_object($element->thirdparty)) {
1081 $ref_array[
'socname'] = $element->thirdparty->name;
1083 $ref_array[
'socname'] =
'';
1087 if (empty($valueref[
'disableamount'])) {
1088 if (!empty($element->total_ht)) {
1089 $ref_array[
'amountht'] = $element->total_ht;
1090 $ref_array[
'amountttc'] = $element->total_ttc;
1092 $ref_array[
'amountht'] = 0;
1093 $ref_array[
'amountttc'] = 0;
1096 $ref_array[
'amountht'] =
'';
1097 $ref_array[
'amountttc'] =
'';
1100 $ref_array[
'status'] = $element->getLibStatut(0);
1104 foreach ($tmparray as $key => $val) {
1106 $listlines->setVars($key, $val,
true,
'UTF-8');
1107 }
catch (SegmentException $e) {
1111 $listlines->merge();
1115 $odfHandler->mergeSegment($listlines);
1117 }
catch (OdfExceptionSegmentNotFound $e) {
1119 }
catch (OdfException $e) {
1120 $this->error = $e->getMessage();
1126 $tmparray = $outputlangs->get_translations_for_substitutions();
1127 foreach ($tmparray as $key => $value) {
1129 $odfHandler->setVars($key, $value,
true,
'UTF-8');
1130 }
catch (OdfException $e) {
1136 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
1137 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
1143 $odfHandler->exportAsAttachedPDF($file);
1145 $this->error = $e->getMessage();
1150 $odfHandler->saveToDisk($file);
1152 $this->error = $e->getMessage();
1157 $parameters = array(
'odfHandler' => &$odfHandler,
'file' => $file,
'object' =>
$object,
'outputlangs' => $outputlangs,
'substitutionarray' => &$tmparray);
1158 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
1164 $this->result = array(
'fullpath' => $file);
1168 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);