actions_mmiworkflow.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. <?php
  2. /* Copyright (C) 2022-2024 Mathieu Moulin <mathieu@iprospective.fr>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file mmiworkflow/class/actions_mmiworkflow.class.php
  19. * \ingroup mmiworkflow
  20. * \brief Example hook overload.
  21. *
  22. * Put detailed description here.
  23. */
  24. require_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
  25. dol_include_once('custom/mmicommon/class/mmi_actions.class.php');
  26. dol_include_once('custom/mmiworkflow/class/mmi_workflow.class.php');
  27. class ActionsMMIWorkflow extends MMI_Actions_1_0
  28. {
  29. const MOD_NAME = 'mmiworkflow';
  30. /**
  31. * Overloading the addMoreMassActions function : replacing the parent's function with the one below
  32. *
  33. * @param array $parameters Hook metadatas (context, etc...)
  34. * @param CommonObject $object The object to process (an invoice if you are in invoice module, a propale in propale's module, etc...)
  35. * @param string $action Current action (if set). Generally create or edit or null
  36. * @param HookManager $hookmanager Hook manager propagated to allow calling another hook
  37. * @return int < 0 on error, 0 on success, 1 to replace standard code
  38. */
  39. public function addMoreActionsButtons($parameters, &$object, &$action, $hookmanager)
  40. {
  41. global $conf, $user, $langs;
  42. $error = 0; // Error counter
  43. $disabled = 1;
  44. // Fiche Commande
  45. if ($this->in_context($parameters, 'ordercard')) {
  46. /** @var Commande $object */
  47. // Blocage validation si ligne libre avec produit (pas grave en service)
  48. if (!empty($conf->global->SFYCUSTOM_LOCK)) {
  49. $blockValid='';
  50. foreach($object->lines as $line) {
  51. if (empty($line->fk_product) && $line->product_type == 0 && $line->qty>0) {
  52. $blockValid = $line->description;
  53. break;
  54. }
  55. }
  56. if (!empty($blockValid)) {
  57. print '
  58. <script type="text/javascript">
  59. $(document).ready(function() {
  60. $(\'a.butAction[href*="action=validate"\').removeClass(\'butAction\')
  61. .addClass(\'butActionRefused\').attr(\'href\',\'#\').text(\''.$langs->transnoentities('SfCstCannotValidWithFreeLine').'\')
  62. .attr(\'title\',\''.dol_string_nohtmltag($blockValid).'\');
  63. })
  64. </script>';
  65. }
  66. }
  67. // Ajout action Facture & Expé en 1 Clic
  68. // SSI statut commande = validé
  69. //var_dump($object);
  70. if (!empty($conf->global->MMI_ORDER_1CLIC_INVOICE_SHIPPING) && (int)$object->status===Commande::STATUS_VALIDATED) {
  71. $link = '?id='.$object->id.'&action=1clic_invoice_shipping';
  72. // Test si déjà facture
  73. // Test si déjà expédition
  74. echo "<a class='butAction' href='".$link."' onclick='return confirm(\"".addslashes($langs->trans("MMI1ClickOrderInvoiceShippingConfirm"))."\")'>".$langs->trans("MMI1ClickOrderInvoiceShipping")."</a>";
  75. }
  76. // Bouton remise en brouillon
  77. if (!empty($conf->global->MMI_ORDER_DRAFTIFY) && $user->rights->mmiworkflow->commande->draftify) {
  78. $q = $this->db->query("SELECT 1
  79. FROM ".MAIN_DB_PREFIX."commande c
  80. WHERE c.rowid=".$object->id." AND c.fk_statut > 0");
  81. //var_dump($q);
  82. if ($q->num_rows) {
  83. $link = '?id='.$object->id.'&action=draftify';
  84. echo "<a class='butAction' href='".$link."'>".$langs->trans("MMIInvoiceDraftify")."</a>";
  85. }
  86. }
  87. // Fix bug 1ct Presta & co
  88. if (!empty($conf->global->MMI_1CT_FIX)) {
  89. $link = '?id='.$object->id.'&action=1ct_fix';
  90. echo "<a class='butAction' href='".$link."'>".$langs->trans("MMI1ctFix")."</a>";
  91. }
  92. // Fix bug TVA Presta & co
  93. if (!empty($conf->global->MMI_VAT_TX_FIX)) {
  94. $link = '?id='.$object->id.'&action=vat_tx_fix';
  95. echo "<a class='butAction' href='".$link."'>".$langs->trans("MMIVATTxFix")."</a>";
  96. }
  97. // Marquer expédié tout
  98. if (!empty($conf->global->MMI_ORDER_SET_EXPE_OK) && (int)$object->status>=Commande::STATUS_VALIDATED && empty(
  99. $object->array_options['options_expe_ok'])) {
  100. $link = '?id='.$object->id.'&action=expe_ok';
  101. // Test si déjà facture
  102. // Test si déjà expédition
  103. if ($user->rights->expedition->creer)
  104. echo "<a class='butAction' href='".$link."'>".$langs->trans("MMISetExpeOK")."</a>";
  105. else
  106. echo "<span class='butActionRefused classfortooltip' title='".$langs->trans("MMISetExpeOKNotRight")."'>".$langs->trans("MMISetExpeOK")."</span>";
  107. }
  108. }
  109. // Fiche Facture
  110. elseif ($this->in_context($parameters, 'invoicecard')) {
  111. /** @var Facture $object */
  112. $q = $this->db->query("SELECT 1
  113. FROM ".MAIN_DB_PREFIX."facture i
  114. LEFT JOIN ".MAIN_DB_PREFIX."accounting_bookkeeping j ON j.doc_type='customer_invoice' AND j.fk_doc=i.rowid
  115. WHERE i.rowid=".$object->id." AND i.fk_statut > 0 AND j.rowid IS NULL");
  116. //var_dump($q);
  117. $nocompta = ($q->num_rows);
  118. // Bouton remise en brouillon
  119. if ($conf->global->MMI_INVOICE_DRAFTIFY && $user->rights->mmiworkflow->facture->draftify) {
  120. if (!isset($conf->global->MMI_INVOICE_DRAFTIFY_TYPES) || $conf->global->MMI_INVOICE_DRAFTIFY_TYPES=='' || in_array($object->type, explode(',', $conf->global->MMI_INVOICE_DRAFTIFY_TYPES))) {
  121. if ($nocompta) {
  122. $link = '?facid='.$object->id.'&action=draftify';
  123. echo "<a class='butAction' href='".$link."'>".$langs->trans("MMIInvoiceDraftify")."</a>";
  124. }
  125. else {
  126. echo '<a class="butActionRefused" title="'.$langs->trans('DisabledBecauseDispatchedInBookkeeping').'" href="javascript:;">'.$langs->trans("MMIInvoiceDraftify").'</a>';
  127. }
  128. }
  129. else {
  130. echo '<a class="butActionRefused" title="'.$langs->trans('NotAllowed').'" href="javascript:;">'.$langs->trans("MMIInvoiceDraftify").'</a>';
  131. }
  132. }
  133. // Fix bug 1ct Presta & co
  134. if ($conf->global->MMI_1CT_FIX) {
  135. if ($nocompta) {
  136. $link = '?facid='.$object->id.'&action=1ct_fix';
  137. echo "<a class='butAction' href='".$link."'>".$langs->trans("MMI1ctFix")."</a>";
  138. }
  139. else {
  140. echo '<a class="butActionRefused" title="'.$langs->trans('DisabledBecauseDispatchedInBookkeeping').'" href="javascript:;">'.$langs->trans("MMI1ctFix")."</a>";
  141. }
  142. }
  143. // Fix bug TVA Presta & co
  144. if ($conf->global->MMI_VAT_TX_FIX) {
  145. $link = '?id='.$object->id.'&action=vat_tx_fix';
  146. echo "<a class='butAction' href='".$link."'>".$langs->trans("MMIVATTxFix")."</a>";
  147. }
  148. // Envoi facture par email
  149. if ($conf->global->MMI_INVOICE_EMAILSEND) {
  150. $link = '?facid='.$object->id.'&action=email_send';
  151. echo "<a class='butAction' href='".$link."'>".$langs->trans("MMIInvoiceEmailSend")."</a>";
  152. }
  153. }
  154. if (!$error) {
  155. return 0; // or return 1 to replace standard code
  156. } else {
  157. $this->errors[] = 'Error message';
  158. return -1;
  159. }
  160. }
  161. function doActions($parameters, &$object, &$action, $hookmanager)
  162. {
  163. global $conf, $user, $langs;
  164. $error = 0; // Error counter
  165. $disabled = 1;
  166. // Commande
  167. if ($this->in_context($parameters, 'ordercard')) {
  168. /** @var Commande $object */
  169. // 1 click invoice shipping
  170. if ($action === '1clic_invoice_shipping') {
  171. if ($conf->global->MMI_ORDER_1CLIC_INVOICE_SHIPPING) {
  172. mmi_workflow::order_1clic_invoice_shipping($user, $object);
  173. }
  174. }
  175. // Indraft again
  176. if ($action === 'draftify') {
  177. if ($conf->global->MMI_ORDER_DRAFTIFY && $user->rights->mmiworkflow->commande->draftify) {
  178. // @todo check pas envoyée au client !
  179. $sql = "UPDATE ".MAIN_DB_PREFIX."commande c
  180. SET c.fk_statut=0
  181. WHERE c.rowid=".$object->id;
  182. //var_dump($object);
  183. //die($sql);
  184. $this->db->query($sql);
  185. }
  186. }
  187. // Fix bug 1ct Presta & co
  188. if ($action === '1ct_fix') {
  189. if ($conf->global->MMI_1CT_FIX) {
  190. mmi_workflow::order_1ctfix($user, $object);
  191. }
  192. }
  193. // Fix bug 1ct Presta & co
  194. if ($action === '1ct_fix') {
  195. if ($conf->global->MMI_1CT_FIX) {
  196. mmi_workflow::order_1ctfix($user, $object);
  197. }
  198. }
  199. // Fix bug TVA Presta & co
  200. if ($action === 'vat_tx_fix') {
  201. if ($conf->global->MMI_VAT_TX_FIX) {
  202. mmi_workflow::order_vat_tx_fix($user, $object);
  203. }
  204. }
  205. // Marquer expe ok
  206. if ($action === 'expe_ok') {
  207. if ($user->rights->expedition->creer) {
  208. $object->array_options['options_expe_ok'] = 1;
  209. $ret = $object->update($user);
  210. }
  211. }
  212. }
  213. // Invoice
  214. elseif ($this->in_context($parameters, 'invoicecard')) {
  215. // Indraft again
  216. if ($action === 'draftify') {
  217. if ($conf->global->MMI_INVOICE_DRAFTIFY && $user->rights->mmiworkflow->facture->draftify) {
  218. // @todo check pas envoyée au client !
  219. mmi_workflow::invoice_draftify($user, $object);
  220. }
  221. }
  222. // Email send
  223. if ($action === 'email_send') {
  224. if ($conf->global->MMI_INVOICE_EMAILSEND) {
  225. mmi_workflow::invoice_email($user, $object);
  226. }
  227. }
  228. // Fix bug 1ct Presta & co
  229. if ($action === '1ct_fix') {
  230. if ($conf->global->MMI_1CT_FIX) {
  231. mmi_workflow::invoice_1ctfix($user, $object);
  232. }
  233. }
  234. // Fix bug TVA Presta & co
  235. if ($action === 'vat_tx_fix') {
  236. if ($conf->global->MMI_VAT_TX_FIX) {
  237. mmi_workflow::invoice_vat_tx_fix($user, $object);
  238. }
  239. }
  240. }
  241. // Shippement
  242. if ($this->in_context($parameters, 'shipmentlist') && !empty($conf->global->SFY_ALERT_ORDER_NOT_SHIPPED)) {
  243. $order = new Commande($this->db);
  244. $sql="SELECT count(rowid) as cnt FROM ".MAIN_DB_PREFIX."commande where fk_statut=".$order::STATUS_VALIDATED;
  245. $resql = $this->db->query($sql);
  246. if (!$resql) {
  247. setEventMessage($this->db->error,'errors');
  248. } else {
  249. $obj=$this->db->fetch_object($resql);
  250. if ($obj->cnt>0) {
  251. $urlList = dol_buildpath('/commande/list.php',2).'?search_status=1';
  252. $urlList='<a href="'.$urlList.'">'.$langs->trans('List').'</a>';
  253. setEventMessage($langs->transnoentities('OrderInValidatedStatusAlert',$urlList),'warnings');
  254. }
  255. }
  256. }
  257. if (!$error) {
  258. return 0; // or return 1 to replace standard code
  259. } else {
  260. $this->errors[] = 'Error message';
  261. return -1;
  262. }
  263. }
  264. function beforePDFCreation($parameters, &$object, &$action='', $hookmanager=NULL)
  265. {
  266. global $conf, $user, $langs;
  267. $error = 0; // Error counter
  268. $disabled = 1;
  269. if (!is_object($object))
  270. return -1;
  271. $object_type = get_class($object);
  272. //var_dump($parameters); die();
  273. //var_dump($this->in_context($parameters, 'pdfgeneration'), $object_type);
  274. if ($this->in_context($parameters, 'pdfgeneration') && $object_type=='Expedition') {
  275. // Sort by Product Ref
  276. // -> directement dans le coeur ça merde l'association avec les images sinon
  277. //usort($object->lines, function($i, $j){
  278. // return ($i->ref > $j->ref) ?1 :(($i->ref < $j->ref) ?-1 :0);
  279. //});
  280. // Bonbons
  281. if (!empty($conf->global->MMI_SHIPPING_PDF_MESSAGE)) {
  282. // Shipping message
  283. if (!empty($conf->global->MMI_SHIPPING_PDF_MESSAGE))
  284. $object->note_public .= (!empty($object->note_public) ?'<br />' :'').$conf->global->MMI_SHIPPING_PDF_MESSAGE.'<br />';
  285. }
  286. }
  287. if (!$error) {
  288. return 0; // or return 1 to replace standard code
  289. } else {
  290. $this->errors[] = 'Error message';
  291. return -1;
  292. }
  293. }
  294. function pdf_writelinedesc($parameters, &$object, &$action='', $hookmanager=NULL)
  295. {
  296. global $conf, $user, $langs;
  297. $error = 0; // Error counter
  298. $disabled = 1;
  299. if (!is_object($object))
  300. return -1;
  301. $object_type = get_class($object);
  302. //var_dump($object_type); die();
  303. if (!empty($conf->global->MMI_SHIPPING_PDF_LABEL_BOLD) && $this->in_context($parameters, 'pdfgeneration') && $object_type=='Expedition') {
  304. $i = $parameters['i'];
  305. //$object = $parameters['object'];
  306. if ($object->lines[$i]->ref)
  307. $object->lines[$i]->ref = '<b>'.$object->lines[$i]->ref.'</b>';
  308. if ($object->lines[$i]->product_ref)
  309. $object->lines[$i]->product_ref = '<b>'.$object->lines[$i]->product_ref.'</b>';
  310. if ($object->lines[$i]->label)
  311. $object->lines[$i]->label = '<b>'.$object->lines[$i]->label.'</b>';
  312. //var_dump($object->lines[$i]->label); die();
  313. //var_dump($object->lines[$i]); die();
  314. //var_dump('yo'); die();
  315. //$this->resPrint = '<b>'.$object->lines[$i]->ref.'</b>';
  316. }
  317. //$this->resprints = ' - TOTO';
  318. if (!$error) {
  319. return 0; // or return 1 to replace standard code
  320. } else {
  321. $this->errors[] = 'Error message';
  322. return -1;
  323. }
  324. }
  325. function pdf_build_address($parameters, &$object, &$action='', $hookmanager=NULL)
  326. {
  327. global $conf, $user, $langs;
  328. $error = 0; // Error counter
  329. $disabled = 1;
  330. if (!is_object($object))
  331. return -1;
  332. $object_type = get_class($object);
  333. if ($this->in_context($parameters, 'pdfgeneration') && $object_type=='Expedition' && $parameters['mode']=='targetwithdetails') {
  334. $arrayidcontact = $object->commande->getIdContact('external', 'SHIPPING');
  335. if (count($arrayidcontact) > 0) {
  336. $object->fetch_contact($arrayidcontact[0]);
  337. if (!empty($object->contact->array_options['options_p_company']))
  338. $this->resprints = $object->contact->array_options['options_p_company'];
  339. }
  340. }
  341. //$this->resprints = ' - TOTO';
  342. if (!$error) {
  343. return 0; // or return 1 to replace standard code
  344. } else {
  345. $this->errors[] = 'Error message';
  346. return -1;
  347. }
  348. }
  349. }
  350. ActionsMMIWorkflow::__init();