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,
618 'ZIP_PROXY' =>
'PclZipProxy',
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');
666 $odfHandler->setVars($key, $value,
true,
'UTF-8');
668 }
catch (OdfException $e) {
675 $listlines = $odfHandler->setSegment(
'tasks');
677 $taskstatic =
new Task($this->db);
681 if (!empty($object->fk_soc)) {
682 $socid = $object->fk_soc;
685 $tasksarray = $taskstatic->getTasksArray(0, 0, $object->id, $socid, 0);
688 foreach ($tasksarray as $task) {
691 foreach ($tmparray as $key => $val) {
693 $listlines->setVars($key, $val,
true,
'UTF-8');
694 }
catch (OdfException $e) {
696 }
catch (SegmentException $e) {
701 $taskobj =
new Task($this->db);
702 $taskobj->fetch($task->id);
705 $sourcearray = array(
'internal',
'external');
706 $contact_arrray = array();
707 foreach ($sourcearray as $source) {
708 $contact_temp = $taskobj->liste_contact(-1, $source);
709 if ((is_array($contact_temp) && count($contact_temp) > 0)) {
710 $contact_arrray = array_merge($contact_arrray, $contact_temp);
713 if ((is_array($contact_arrray) && count($contact_arrray) > 0)) {
714 $listlinestaskres = $listlines->__get(
'tasksressources');
716 foreach ($contact_arrray as $contact) {
717 if ($contact[
'source'] ==
'internal') {
718 $objectdetail =
new User($this->db);
719 $objectdetail->fetch($contact[
'id']);
720 $contact[
'socname'] = $mysoc->name;
721 } elseif ($contact[
'source'] ==
'external') {
722 $objectdetail =
new Contact($this->db);
723 $objectdetail->fetch($contact[
'id']);
726 $soc->fetch($contact[
'socid']);
727 $contact[
'socname'] = $soc->name;
729 $contact[
'fullname'] = $objectdetail->getFullName($outputlangs, 1);
733 foreach ($tmparray as $key => $val) {
735 $listlinestaskres->setVars($key, $val,
true,
'UTF-8');
736 }
catch (OdfException $e) {
738 }
catch (SegmentException $e) {
742 $listlinestaskres->merge();
747 $sql =
"SELECT t.rowid, t.element_date as task_date, t.element_duration as task_duration, t.fk_user, t.note";
748 $sql .=
", u.lastname, u.firstname, t.thm";
749 $sql .=
" FROM ".MAIN_DB_PREFIX.
"element_time as t";
750 $sql .=
" , ".MAIN_DB_PREFIX.
"user as u";
751 $sql .=
" WHERE t.fk_element =".((int) $task->id);
752 $sql .=
" AND t.elementtype = 'task'";
753 $sql .=
" AND t.fk_user = u.rowid";
754 $sql .=
" ORDER BY t.element_date DESC";
756 $resql = $this->db->query($sql);
758 $num = $this->db->num_rows($resql);
762 $listlinestasktime = $listlines->__get(
'taskstimes');
765 $row[
'task_date'] =
'';
766 $row[
'task_duration'] =
'';
767 $row[
'$tasktime'] =
'';
769 $row[
'fk_user'] =
'';
771 $row[
'firstname'] =
'';
772 $row[
'fullcivname'] =
'';
773 $row[
'amountht'] =
'';
774 $row[
'amountttc'] =
'';
777 foreach ($tmparray as $key => $val) {
779 $listlinestasktime->setVars($key, $val,
true,
'UTF-8');
780 }
catch (OdfException $e) {
782 }
catch (SegmentException $e) {
786 $listlinestasktime->merge();
789 $row = $this->db->fetch_array($resql);
790 if (!empty($row[
'fk_user'])) {
791 $objectdetail =
new User($this->db);
792 $objectdetail->fetch($row[
'fk_user']);
793 $row[
'fullcivname'] = $objectdetail->getFullName($outputlangs, 1);
795 $row[
'fullcivname'] =
'';
798 if (!empty($row[
'thm'])) {
799 $row[
'amountht'] = ($row[
'task_duration'] / 3600) * $row[
'thm'];
801 $row[
'amountttc'] =
price2num($row[
'amountht'] * (1 + ($defaultvat / 100)),
'MT');
803 $row[
'amountht'] = 0;
804 $row[
'amountttc'] = 0;
810 foreach ($tmparray as $key => $val) {
812 $listlinestasktime->setVars($key, $val,
true,
'UTF-8');
813 }
catch (OdfException $e) {
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 (OdfException $e) {
841 }
catch (SegmentException $e) {
845 $listtasksfiles->merge();
849 $odfHandler->mergeSegment($listlines);
850 }
catch (OdfException $e) {
851 $ExceptionTrace = $e->getTrace();
853 if ($ExceptionTrace[0][
'function'] !=
'setSegment') {
854 $this->error = $e->getMessage();
862 $listlines = $odfHandler->setSegment(
'projectfiles');
865 $filearray =
dol_dir_list($upload_dir,
"files", 0,
'',
'(\.meta|_preview.*\.png)$',
'name', SORT_ASC, 1);
867 foreach ($filearray as $filedetail) {
871 foreach ($tmparray as $key => $val) {
873 $listlines->setVars($key, $val,
true,
'UTF-8');
874 }
catch (OdfException $e) {
876 }
catch (SegmentException $e) {
882 $odfHandler->mergeSegment($listlines);
883 }
catch (OdfException $e) {
884 $this->error = $e->getMessage();
890 $sourcearray = array(
'internal',
'external');
891 $contact_arrray = array();
892 foreach ($sourcearray as $source) {
893 $contact_temp = $object->liste_contact(-1, $source);
894 if ((is_array($contact_temp) && count($contact_temp) > 0)) {
895 $contact_arrray = array_merge($contact_arrray, $contact_temp);
898 if ((is_array($contact_arrray) && count($contact_arrray) > 0)) {
900 $listlines = $odfHandler->setSegment(
'projectcontacts');
902 foreach ($contact_arrray as $contact) {
903 if ($contact[
'source'] ==
'internal') {
904 $objectdetail =
new User($this->db);
905 $objectdetail->fetch($contact[
'id']);
906 $contact[
'socname'] = $mysoc->name;
907 } elseif ($contact[
'source'] ==
'external') {
908 $objectdetail =
new Contact($this->db);
909 $objectdetail->fetch($contact[
'id']);
912 $soc->fetch($contact[
'socid']);
913 $contact[
'socname'] = $soc->name;
915 $contact[
'fullname'] = $objectdetail->getFullName($outputlangs, 1);
918 foreach ($tmparray as $key => $val) {
920 $listlines->setVars($key, $val,
true,
'UTF-8');
921 }
catch (OdfException $e) {
923 }
catch (SegmentException $e) {
929 $odfHandler->mergeSegment($listlines);
930 }
catch (OdfException $e) {
931 $this->error = $e->getMessage();
939 $listofreferent = array(
941 'title' =>
"ListProposalsAssociatedProject",
944 'test' => isModEnabled(
'propal') && $user->hasRight(
'propal',
'lire')
947 'title' =>
"ListOrdersAssociatedProject",
948 'class' =>
'Commande',
949 'table' =>
'commande',
950 'test' => isModEnabled(
'commande') && $user->hasRight(
'commande',
'lire')
953 'title' =>
"ListInvoicesAssociatedProject",
954 'class' =>
'Facture',
955 'table' =>
'facture',
956 'test' => isModEnabled(
'facture') && $user->hasRight(
'facture',
'lire')
958 'invoice_predefined' => array(
959 'title' =>
"ListPredefinedInvoicesAssociatedProject",
960 'class' =>
'FactureRec',
961 'table' =>
'facture_rec',
962 'test' => isModEnabled(
'facture') && $user->hasRight(
'facture',
'lire')
964 'proposal_supplier' => array(
965 'title' =>
"ListSupplierProposalsAssociatedProject",
966 'class' =>
'SupplierProposal',
967 'table' =>
'supplier_proposal',
968 'test' => isModEnabled(
'supplier_proposal') && $user->rights->supplier_proposal->lire
970 'order_supplier' => array(
971 'title' =>
"ListSupplierOrdersAssociatedProject",
972 'table' =>
'commande_fournisseur',
973 'class' =>
'CommandeFournisseur',
974 'test' => (isModEnabled(
"fournisseur") && empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->commande->lire) || (isModEnabled(
"supplier_order") && $user->rights->supplier_order->lire)
976 'invoice_supplier' => array(
977 'title' =>
"ListSupplierInvoicesAssociatedProject",
978 'table' =>
'facture_fourn',
979 'class' =>
'FactureFournisseur',
980 'test' => (isModEnabled(
"fournisseur") && empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire) || (isModEnabled(
"supplier_invoice") && $user->rights->supplier_invoice->lire)
983 'title' =>
"ListContractAssociatedProject",
984 'class' =>
'Contrat',
985 'table' =>
'contrat',
986 'test' => isModEnabled(
'contrat') && $user->hasRight(
'contrat',
'lire')
988 'intervention' => array(
989 'title' =>
"ListFichinterAssociatedProject",
990 'class' =>
'Fichinter',
991 'table' =>
'fichinter',
992 'disableamount' => 1,
993 'test' => isModEnabled(
'ficheinter') && $user->hasRight(
'ficheinter',
'lire')
996 'title' =>
"ListShippingAssociatedProject",
997 'class' =>
'Expedition',
998 'table' =>
'expedition',
999 'disableamount' => 1,
1000 'test' => isModEnabled(
'expedition') && $user->rights->expedition->lire
1003 'title' =>
"ListTripAssociatedProject",
1004 'class' =>
'Deplacement',
1005 'table' =>
'deplacement',
1006 'disableamount' => 1,
1007 'test' => isModEnabled(
'deplacement') && $user->rights->deplacement->lire
1009 'expensereport' => array(
1010 'title' =>
"ListExpenseReportsAssociatedProject",
1011 'class' =>
'ExpenseReportLine',
1012 'table' =>
'expensereport_det',
1013 'test' => isModEnabled(
'expensereport') && $user->rights->expensereport->lire
1015 'donation' => array(
1016 'title' =>
"ListDonationsAssociatedProject",
1019 'test' => isModEnabled(
'don') && $user->rights->don->lire
1022 'title' =>
"ListLoanAssociatedProject",
1025 'test' => isModEnabled(
'loan') && $user->rights->loan->read
1027 'chargesociales' => array(
1028 'title' =>
"ListSocialContributionAssociatedProject",
1029 'class' =>
'ChargeSociales',
1030 'table' =>
'chargesociales',
1031 'urlnew' => DOL_URL_ROOT.
'/compta/sociales/card.php?action=create&projectid='.$object->id,
1032 'test' => isModEnabled(
'tax') && $user->rights->tax->charges->lire
1034 'stock_mouvement' => array(
1035 'title' =>
"ListMouvementStockProject",
1036 'class' =>
'MouvementStock',
1037 'table' =>
'stock_mouvement',
1038 'test' => (isModEnabled(
'stock') && $user->rights->stock->mouvement->lire && !empty($conf->global->STOCK_MOVEMENT_INTO_PROJECT_OVERVIEW))
1041 'title' =>
"ListActionsAssociatedProject",
1042 'class' =>
'ActionComm',
1043 'table' =>
'actioncomm',
1044 'disableamount' => 1,
1045 'test' => isModEnabled(
'agenda') && $user->rights->agenda->allactions->lire
1051 $listlines = $odfHandler->setSegment(
'projectrefs');
1053 foreach ($listofreferent as $keyref => $valueref) {
1054 $title = $valueref[
'title'];
1055 $tablename = $valueref[
'table'];
1056 $classname = $valueref[
'class'];
1057 $qualified = $valueref[
'test'];
1059 $elementarray = $object->get_element_list($keyref, $tablename);
1060 if (count($elementarray) > 0 && is_array($elementarray)) {
1063 $num = count($elementarray);
1064 for ($i = 0; $i < $num; $i++) {
1065 $ref_array = array();
1066 $ref_array[
'type'] = $langs->trans($classname);
1068 $element =
new $classname($this->db);
1069 $element->fetch($elementarray[$i]);
1070 $element->fetch_thirdparty();
1073 $ref_array[
'ref'] = $element->ref;
1076 $dateref = $element->date;
1077 if (empty($dateref)) {
1078 $dateref = $element->datep;
1080 if (empty($dateref)) {
1081 $dateref = $element->date_contrat;
1083 $ref_array[
'date'] = $dateref;
1086 if (is_object($element->thirdparty)) {
1087 $ref_array[
'socname'] = $element->thirdparty->name;
1089 $ref_array[
'socname'] =
'';
1093 if (empty($valueref[
'disableamount'])) {
1094 if (!empty($element->total_ht)) {
1095 $ref_array[
'amountht'] = $element->total_ht;
1096 $ref_array[
'amountttc'] = $element->total_ttc;
1098 $ref_array[
'amountht'] = 0;
1099 $ref_array[
'amountttc'] = 0;
1102 $ref_array[
'amountht'] =
'';
1103 $ref_array[
'amountttc'] =
'';
1106 $ref_array[
'status'] = $element->getLibStatut(0);
1110 foreach ($tmparray as $key => $val) {
1112 $listlines->setVars($key, $val,
true,
'UTF-8');
1113 }
catch (OdfException $e) {
1115 }
catch (SegmentException $e) {
1119 $listlines->merge();
1123 $odfHandler->mergeSegment($listlines);
1125 }
catch (OdfException $e) {
1126 $this->error = $e->getMessage();
1132 $tmparray = $outputlangs->get_translations_for_substitutions();
1133 foreach ($tmparray as $key => $value) {
1135 $odfHandler->setVars($key, $value,
true,
'UTF-8');
1136 }
catch (OdfException $e) {
1142 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
1143 $reshook = $hookmanager->executeHooks(
'beforeODTSave', $parameters, $this, $action);
1147 if (!empty($conf->global->MAIN_ODT_AS_PDF)) {
1149 $odfHandler->exportAsAttachedPDF($file);
1151 $this->error = $e->getMessage();
1156 $odfHandler->saveToDisk($file);
1158 $this->error = $e->getMessage();
1163 $parameters = array(
'odfHandler'=>&$odfHandler,
'file'=>$file,
'object'=>$object,
'outputlangs'=>$outputlangs,
'substitutionarray'=>&$tmparray);
1164 $reshook = $hookmanager->executeHooks(
'afterODTCreation', $parameters, $this, $action);
1170 $this->result = array(
'fullpath'=>$file);
1174 $this->error = $langs->transnoentities(
"ErrorCanNotCreateDir", $dir);