46 $this->
name = preg_replace(
'/^Interface/i',
'', get_class($this));
47 $this->family =
"core";
48 $this->
description =
"Triggers of this module allows to manage workflows";
50 $this->version = self::VERSION_DOLIBARR;
51 $this->picto =
'technic';
67 if (empty($conf->workflow) || empty($conf->workflow->enabled)) {
74 if ($action ==
'PROPAL_CLOSE_SIGNED') {
75 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
76 if (isModEnabled(
'commande') &&
getDolGlobalString(
'WORKFLOW_PROPAL_AUTOCREATE_ORDER')) {
77 $object->fetchObjectLinked();
78 if (!empty($object->linkedObjectsIds[
'commande'])) {
79 if (empty($object->context[
'closedfromonlinesignature'])) {
80 $langs->load(
"orders");
86 include_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
87 $newobject =
new Commande($this->db);
89 $newobject->context[
'createfrompropal'] =
'createfrompropal';
90 $newobject->context[
'origin'] = $object->element;
91 $newobject->context[
'origin_id'] = $object->id;
93 $ret = $newobject->createFromProposal($object, $user);
98 $object->clearObjectLinkedCache();
105 if ($action ==
'ORDER_CLOSE') {
106 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
107 if (isModEnabled(
'facture') &&
getDolGlobalString(
'WORKFLOW_ORDER_AUTOCREATE_INVOICE')) {
108 include_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
109 $newobject =
new Facture($this->db);
111 $newobject->context[
'createfromorder'] =
'createfromorder';
112 $newobject->context[
'origin'] = $object->element;
113 $newobject->context[
'origin_id'] = $object->id;
115 $ret = $newobject->createFromOrder($object, $user);
119 if (empty($object->fk_account) && !empty($object->thirdparty->fk_account) && !
getDolGlobalInt(
'BANK_ASK_PAYMENT_BANK_DURING_ORDER')) {
120 $res = $newobject->setBankAccount($object->thirdparty->fk_account,
true, $user);
127 $object->clearObjectLinkedCache();
134 if ($action ==
'ORDER_CLASSIFY_BILLED') {
135 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
136 if (isModEnabled(
"propal") && !empty($conf->workflow->enabled) &&
getDolGlobalString(
'WORKFLOW_ORDER_CLASSIFY_BILLED_PROPAL')) {
137 $object->fetchObjectLinked(
'',
'propal', $object->id, $object->element);
138 if (!empty($object->linkedObjects)) {
139 $totalonlinkedelements = 0;
140 foreach ($object->linkedObjects[
'propal'] as $element) {
142 $totalonlinkedelements += $element->total_ht;
145 dol_syslog(
"Amount of linked proposals = ".$totalonlinkedelements.
", of order = ".$object->total_ht.
", egality is ".($totalonlinkedelements == $object->total_ht));
146 if ($this->
shouldClassify($conf, $totalonlinkedelements, $object->total_ht)) {
147 foreach ($object->linkedObjects[
'propal'] as $element) {
148 $ret = $element->classifyBilled($user);
157 if ($action ==
'BILL_VALIDATE') {
158 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
161 if (isModEnabled(
'commande') && !empty($conf->workflow->enabled) &&
getDolGlobalString(
'WORKFLOW_INVOICE_AMOUNT_CLASSIFY_BILLED_ORDER')) {
162 $object->fetchObjectLinked(
'',
'commande', $object->id, $object->element);
163 if (!empty($object->linkedObjects)) {
164 $totalonlinkedelements = 0;
165 foreach ($object->linkedObjects[
'commande'] as $element) {
167 $totalonlinkedelements += $element->total_ht;
170 dol_syslog(
"Amount of linked orders = ".$totalonlinkedelements.
", of invoice = ".$object->total_ht.
", egality is ".($totalonlinkedelements == $object->total_ht));
171 if ($this->
shouldClassify($conf, $totalonlinkedelements, $object->total_ht)) {
172 foreach ($object->linkedObjects[
'commande'] as $element) {
173 $ret = $element->classifyBilled($user);
180 if (isModEnabled(
"propal") && !empty($conf->workflow->enabled) &&
getDolGlobalString(
'WORKFLOW_INVOICE_CLASSIFY_BILLED_PROPAL')) {
181 $object->fetchObjectLinked(
'',
'propal', $object->id, $object->element);
182 if (!empty($object->linkedObjects)) {
183 $totalonlinkedelements = 0;
184 foreach ($object->linkedObjects[
'propal'] as $element) {
186 $totalonlinkedelements += $element->total_ht;
189 dol_syslog(
"Amount of linked proposals = ".$totalonlinkedelements.
", of invoice = ".$object->total_ht.
", egality is ".($totalonlinkedelements == $object->total_ht));
190 if ($this->
shouldClassify($conf, $totalonlinkedelements, $object->total_ht)) {
191 foreach ($object->linkedObjects[
'propal'] as $element) {
192 $ret = $element->classifyBilled($user);
199 if (isModEnabled(
"expedition") && !empty($conf->workflow->enabled) &&
getDolGlobalString(
'WORKFLOW_SHIPPING_CLASSIFY_CLOSED_INVOICE')) {
200 $object->fetchObjectLinked(
'',
'shipping', $object->id, $object->element);
201 if (!empty($object->linkedObjects)) {
202 $totalonlinkedelements = 0;
203 foreach ($object->linkedObjects[
'shipping'] as $element) {
205 $totalonlinkedelements += $element->total_ht;
208 dol_syslog(
"Amount of linked shipment = ".$totalonlinkedelements.
", of invoice = ".$object->total_ht.
", egality is ".($totalonlinkedelements == $object->total_ht), LOG_DEBUG);
209 if ($totalonlinkedelements == $object->total_ht) {
210 foreach ($object->linkedObjects[
'shipping'] as $element) {
211 $ret = $element->setClosed();
220 if (isModEnabled(
"expedition") && !empty($conf->workflow->enabled) &&
getDolGlobalString(
'WORKFLOW_SHIPPING_CLASSIFY_BILLED_INVOICE')) {
221 $object->fetchObjectLinked(
'',
'shipping', $object->id, $object->element);
222 if (!empty($object->linkedObjects)) {
223 $totalonlinkedelements = 0;
224 foreach ($object->linkedObjects[
'shipping'] as $element) {
226 $totalonlinkedelements += $element->total_ht;
229 dol_syslog(
"Amount of linked shipment = ".$totalonlinkedelements.
", of invoice = ".$object->total_ht.
", egality is ".($totalonlinkedelements == $object->total_ht), LOG_DEBUG);
230 if ($totalonlinkedelements == $object->total_ht) {
231 foreach ($object->linkedObjects[
'shipping'] as $element) {
232 $ret = $element->setBilled();
245 if ($action ==
'BILL_SUPPLIER_VALIDATE') {
246 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
250 if ((isModEnabled(
"supplier_order") || isModEnabled(
"supplier_invoice")) &&
getDolGlobalString(
'WORKFLOW_INVOICE_AMOUNT_CLASSIFY_BILLED_SUPPLIER_ORDER')) {
251 $object->fetchObjectLinked(
'',
'order_supplier', $object->id, $object->element);
252 if (!empty($object->linkedObjects)) {
253 $totalonlinkedelements = 0;
254 foreach ($object->linkedObjects[
'order_supplier'] as $element) {
256 $totalonlinkedelements += $element->total_ht;
259 dol_syslog(
"Amount of linked orders = ".$totalonlinkedelements.
", of invoice = ".$object->total_ht.
", egality is ".($totalonlinkedelements == $object->total_ht));
260 if ($this->
shouldClassify($conf, $totalonlinkedelements, $object->total_ht)) {
261 foreach ($object->linkedObjects[
'order_supplier'] as $element) {
262 $ret = $element->classifyBilled($user);
272 if (isModEnabled(
'supplier_proposal') &&
getDolGlobalString(
'WORKFLOW_INVOICE_CLASSIFY_BILLED_SUPPLIER_PROPOSAL')) {
273 $object->fetchObjectLinked(
'',
'supplier_proposal', $object->id, $object->element);
274 if (!empty($object->linkedObjects)) {
275 $totalonlinkedelements = 0;
276 foreach ($object->linkedObjects[
'supplier_proposal'] as $element) {
278 $totalonlinkedelements += $element->total_ht;
281 dol_syslog(
"Amount of linked supplier proposals = ".$totalonlinkedelements.
", of supplier invoice = ".$object->total_ht.
", egality is ".($totalonlinkedelements == $object->total_ht));
282 if ($this->
shouldClassify($conf, $totalonlinkedelements, $object->total_ht)) {
283 foreach ($object->linkedObjects[
'supplier_proposal'] as $element) {
284 $ret = $element->classifyBilled($user);
318 if (isModEnabled(
"reception") && !empty($conf->workflow->enabled) &&
getDolGlobalString(
'WORKFLOW_RECEPTION_CLASSIFY_BILLED_INVOICE')) {
319 $object->fetchObjectLinked(
'',
'reception', $object->id, $object->element);
320 if (!empty($object->linkedObjects)) {
321 $totalonlinkedelements = 0;
322 foreach ($object->linkedObjects[
'reception'] as $element) {
323 if ($element->statut == Reception::STATUS_VALIDATED || $element->statut == Reception::STATUS_CLOSED) {
324 $totalonlinkedelements += $element->total_ht;
327 dol_syslog(
"Amount of linked reception = ".$totalonlinkedelements.
", of invoice = ".$object->total_ht.
", egality is ".($totalonlinkedelements == $object->total_ht), LOG_DEBUG);
328 if ($totalonlinkedelements == $object->total_ht) {
329 foreach ($object->linkedObjects[
'reception'] as $element) {
330 $ret = $element->setBilled();
343 if ($action ==
'BILL_PAYED') {
344 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
346 if (isModEnabled(
'commande') &&
getDolGlobalString(
'WORKFLOW_INVOICE_CLASSIFY_BILLED_ORDER')) {
347 $object->fetchObjectLinked(
'',
'commande', $object->id, $object->element);
348 if (!empty($object->linkedObjects)) {
349 $totalonlinkedelements = 0;
350 foreach ($object->linkedObjects[
'commande'] as $element) {
352 $totalonlinkedelements += $element->total_ht;
355 dol_syslog(
"Amount of linked orders = ".$totalonlinkedelements.
", of invoice = ".$object->total_ht.
", egality is ".($totalonlinkedelements == $object->total_ht));
356 if ($this->
shouldClassify($conf, $totalonlinkedelements, $object->total_ht)) {
357 foreach ($object->linkedObjects[
'commande'] as $element) {
358 $ret = $element->classifyBilled($user);
367 if (($action ==
'SHIPPING_VALIDATE') || ($action ==
'SHIPPING_CLOSED')) {
368 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
370 if (isModEnabled(
'commande') && isModEnabled(
"expedition") && !empty($conf->workflow->enabled) &&
372 (
getDolGlobalString(
'WORKFLOW_ORDER_CLASSIFY_SHIPPED_SHIPPING') && ($action ==
'SHIPPING_VALIDATE')) ||
373 (
getDolGlobalString(
'WORKFLOW_ORDER_CLASSIFY_SHIPPED_SHIPPING_CLOSED') && ($action ==
'SHIPPING_CLOSED'))
376 $qtyshipped = array();
377 $qtyordred = array();
378 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
382 $ret = $order->fetch($object->origin_id);
387 $ret = $order->fetchObjectLinked($order->id,
'commande',
null,
'shipping');
393 if (is_array($order->linkedObjects) && count($order->linkedObjects) > 0) {
394 foreach ($order->linkedObjects as $type => $shipping_array) {
395 if ($type !=
'shipping' || !is_array($shipping_array) || count($shipping_array) == 0) {
399 foreach ($shipping_array as $shipping) {
400 if ($shipping->status <= 0 || !is_array($shipping->lines) || count($shipping->lines) == 0) {
404 foreach ($shipping->lines as $shippingline) {
405 $qtyshipped[$shippingline->fk_product] += $shippingline->qty;
412 if (is_array($order->lines) && count($order->lines) > 0) {
413 foreach ($order->lines as $orderline) {
418 $qtyordred[$orderline->fk_product] += $orderline->qty;
424 $diff_array = array_diff_assoc($qtyordred, $qtyshipped);
425 if (count($diff_array) == 0) {
437 if (($action ==
'RECEPTION_VALIDATE') || ($action ==
'RECEPTION_CLOSED')) {
438 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
440 if ((isModEnabled(
"fournisseur") || isModEnabled(
"supplier_order")) && isModEnabled(
"reception") && !empty($conf->workflow->enabled) &&
442 (
getDolGlobalString(
'WORKFLOW_ORDER_CLASSIFY_RECEIVED_RECEPTION') && ($action ==
'RECEPTION_VALIDATE')) ||
443 (
getDolGlobalString(
'WORKFLOW_ORDER_CLASSIFY_RECEIVED_RECEPTION_CLOSED') && ($action ==
'RECEPTION_CLOSED'))
446 $qtyshipped = array();
447 $qtyordred = array();
448 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.commande.class.php';
452 $ret = $order->fetch($object->origin_id);
457 $ret = $order->fetchObjectLinked($order->id, $order->element,
null,
'reception');
463 if (is_array($order->linkedObjects) && count($order->linkedObjects) > 0) {
464 foreach ($order->linkedObjects as $type => $shipping_array) {
465 if ($type !=
'reception' || !is_array($shipping_array) || count($shipping_array) == 0) {
469 foreach ($shipping_array as $shipping) {
470 if (!is_array($shipping->lines) || count($shipping->lines) == 0) {
474 foreach ($shipping->lines as $shippingline) {
475 $qtyshipped[$shippingline->fk_product] += $shippingline->qty;
482 if (is_array($order->lines) && count($order->lines) > 0) {
483 foreach ($order->lines as $orderline) {
488 $qtyordred[$orderline->fk_product] += $orderline->qty;
494 $diff_array = array_diff_assoc($qtyordred, $qtyshipped);
495 if (count($diff_array) == 0) {
506 if ($action ==
'TICKET_CREATE') {
507 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
509 if (isModEnabled(
'contract') && isModEnabled(
'ticket') && isModEnabled(
'workflow') &&
getDolGlobalString(
'WORKFLOW_TICKET_LINK_CONTRACT') &&
getDolGlobalString(
'TICKET_PRODUCT_CATEGORY') && !empty($object->fk_soc)) {
510 $societe =
new Societe($this->db);
511 $company_ids = (!
getDolGlobalString(
'WORKFLOW_TICKET_USE_PARENT_COMPANY_CONTRACTS')) ? [$object->fk_soc] : $societe->getParentsForCompany($object->fk_soc, [$object->fk_soc]);
513 $contrat =
new Contrat($this->db);
514 $number_contracts_found = 0;
515 foreach ($company_ids as $company_id) {
516 $contrat->socid = $company_id;
518 $list = $contrat->getListOfContracts($option =
'all', $status = [Contrat::STATUS_DRAFT, Contrat::STATUS_VALIDATED], $product_categories = [$conf->global->TICKET_PRODUCT_CATEGORY], $line_status = [ContratLigne::STATUS_INITIAL, ContratLigne::STATUS_OPEN]);
519 if (!is_array($list) || empty($list)) {
522 $number_contracts_found = count($list);
523 if ($number_contracts_found == 0) {
527 foreach ($list as $linked_contract) {
528 $object->setContract($linked_contract->id);
532 if ($number_contracts_found > 1 && !defined(
'NOLOGIN')) {
533 setEventMessage($langs->trans(
'TicketManyContractsLinked'),
'warnings');
537 if ($number_contracts_found == 0 && !defined(
'NOLOGIN')) {
538 setEventMessage($langs->trans(
'TicketNoContractFoundToLink'),
'mesgs');
542 if (isModEnabled(
'ficheinter') && isModEnabled(
'ticket') && isModEnabled(
'workflow') &&
getDolGlobalString(
'WORKFLOW_TICKET_CREATE_INTERVENTION')) {
544 $fichinter->socid = (int) $object->fk_soc;
545 $fichinter->fk_project = (int) $object->fk_project;
546 $fichinter->fk_contrat = (int) $object->fk_contract;
547 $fichinter->author = $user->id;
549 $fichinter->origin = $object->element;
550 $fichinter->origin_id = $object->id;
554 $extrafields->fetch_name_optionals_label($fichinter->table_element);
555 $array_options = $extrafields->getOptionalsFromPost($fichinter->table_element);
556 $fichinter->array_options = $array_options;
558 $id = $fichinter->create($user);
584 return (
price2num($totalonlinkedelements,
'MT') ==
price2num($object_total_ht,
'MT'));