47 $this->
name = preg_replace(
'/^Interface/i',
'', get_class($this));
48 $this->family =
"core";
49 $this->
description =
"Triggers of this module allows to manage workflows";
50 $this->version = self::VERSIONS[
'prod'];
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);
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(
'invoice') &&
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);
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')) {
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 ".json_encode($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(
'order') && !empty($conf->workflow->enabled) &&
getDolGlobalString(
'WORKFLOW_INVOICE_AMOUNT_CLASSIFY_BILLED_ORDER')) {
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 ".json_encode($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')) {
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 ".json_encode($totalonlinkedelements ==
$object->total_ht));
191 foreach (
$object->linkedObjects[
'propal'] as $element) {
192 $ret = $element->classifyBilled($user);
199 if (isModEnabled(
"shipping") && !empty($conf->workflow->enabled) &&
getDolGlobalString(
'WORKFLOW_SHIPPING_CLASSIFY_CLOSED_INVOICE')) {
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 ".json_encode($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(
"shipping") && !empty($conf->workflow->enabled) &&
getDolGlobalString(
'WORKFLOW_SHIPPING_CLASSIFY_BILLED_INVOICE')) {
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 ".json_encode($totalonlinkedelements ==
$object->total_ht), LOG_DEBUG);
230 if ($totalonlinkedelements ==
$object->total_ht) {
231 foreach (
$object->linkedObjects[
'shipping'] as $element) {
232 $ret = $element->setBilled();
242 if (isModEnabled(
'order') && isModEnabled(
'workflow') &&
getDolGlobalString(
'WORKFLOW_SUM_INVOICES_AMOUNT_CLASSIFY_BILLED_ORDER')) {
244 if (!empty(
$object->linkedObjects[
'commande']) && count(
$object->linkedObjects[
'commande']) == 1) {
245 $orderLinked = reset(
$object->linkedObjects[
'commande']);
246 $orderLinked->fetchObjectLinked($orderLinked->id,
'', $orderLinked->element);
247 if (count($orderLinked->linkedObjects[
'facture']) >= 1) {
248 $totalHTInvoices = 0;
249 $areAllInvoicesValidated =
true;
250 foreach ($orderLinked->linkedObjects[
'facture'] as $key => $invoice) {
252 $totalHTInvoices += (float) $invoice->total_ht;
254 $areAllInvoicesValidated =
false;
258 if ($areAllInvoicesValidated) {
259 $isSameTotal = (
price2num($totalHTInvoices,
'MT') ==
price2num($orderLinked->total_ht,
'MT'));
260 dol_syslog(
"Amount of linked invoices = ".$totalHTInvoices.
", of order = ".$orderLinked->total_ht.
", isSameTotal = ".(
string) $isSameTotal, LOG_DEBUG);
262 $ret = $orderLinked->classifyBilled($user);
275 if ($action ==
'BILL_SUPPLIER_VALIDATE') {
276 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".
$object->id);
280 if ((isModEnabled(
"supplier_order") || isModEnabled(
"supplier_invoice")) &&
getDolGlobalString(
'WORKFLOW_INVOICE_AMOUNT_CLASSIFY_BILLED_SUPPLIER_ORDER')) {
282 if (!empty(
$object->linkedObjects)) {
283 $totalonlinkedelements = 0;
284 foreach (
$object->linkedObjects[
'order_supplier'] as $element) {
286 $totalonlinkedelements += $element->total_ht;
289 dol_syslog(
"Amount of linked orders = ".$totalonlinkedelements.
", of invoice = ".
$object->total_ht.
", egality is ".json_encode($totalonlinkedelements ==
$object->total_ht));
291 foreach (
$object->linkedObjects[
'order_supplier'] as $element) {
292 $ret = $element->classifyBilled($user);
302 if (isModEnabled(
'supplier_proposal') &&
getDolGlobalString(
'WORKFLOW_INVOICE_CLASSIFY_BILLED_SUPPLIER_PROPOSAL')) {
304 if (!empty(
$object->linkedObjects)) {
305 $totalonlinkedelements = 0;
306 foreach (
$object->linkedObjects[
'supplier_proposal'] as $element) {
308 $totalonlinkedelements += $element->total_ht;
311 dol_syslog(
"Amount of linked supplier proposals = ".$totalonlinkedelements.
", of supplier invoice = ".
$object->total_ht.
", egality is ".json_encode($totalonlinkedelements ==
$object->total_ht));
313 foreach (
$object->linkedObjects[
'supplier_proposal'] as $element) {
314 $ret = $element->classifyBilled($user);
348 if (isModEnabled(
"reception") && !empty($conf->workflow->enabled) &&
getDolGlobalString(
'WORKFLOW_RECEPTION_CLASSIFY_BILLED_INVOICE')) {
350 if (!empty(
$object->linkedObjects)) {
351 $totalonlinkedelements = 0;
352 foreach (
$object->linkedObjects[
'reception'] as $element) {
353 if ($element->statut == Reception::STATUS_VALIDATED || $element->statut == Reception::STATUS_CLOSED) {
354 $totalonlinkedelements += $element->total_ht;
357 dol_syslog(
"Amount of linked reception = ".$totalonlinkedelements.
", of invoice = ".
$object->total_ht.
", egality is ".json_encode($totalonlinkedelements ==
$object->total_ht), LOG_DEBUG);
358 if ($totalonlinkedelements ==
$object->total_ht) {
359 foreach (
$object->linkedObjects[
'reception'] as $element) {
360 $ret = $element->setBilled();
373 if ($action ==
'BILL_PAYED') {
374 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".
$object->id);
376 if (isModEnabled(
'order') &&
getDolGlobalString(
'WORKFLOW_INVOICE_CLASSIFY_BILLED_ORDER')) {
378 if (!empty(
$object->linkedObjects)) {
379 $totalonlinkedelements = 0;
380 foreach (
$object->linkedObjects[
'commande'] as $element) {
382 $totalonlinkedelements += $element->total_ht;
385 dol_syslog(
"Amount of linked orders = ".$totalonlinkedelements.
", of invoice = ".
$object->total_ht.
", egality is ".json_encode($totalonlinkedelements ==
$object->total_ht));
387 foreach (
$object->linkedObjects[
'commande'] as $element) {
388 $ret = $element->classifyBilled($user);
397 if (($action ==
'SHIPPING_VALIDATE') || ($action ==
'SHIPPING_CLOSED')) {
398 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".
$object->id);
400 if (isModEnabled(
'order') && isModEnabled(
"shipping") && !empty($conf->workflow->enabled) &&
402 (
getDolGlobalString(
'WORKFLOW_ORDER_CLASSIFY_SHIPPED_SHIPPING') && ($action ==
'SHIPPING_VALIDATE')) ||
403 (
getDolGlobalString(
'WORKFLOW_ORDER_CLASSIFY_SHIPPED_SHIPPING_CLOSED') && ($action ==
'SHIPPING_CLOSED'))
406 $qtyshipped = array();
407 $qtyordred = array();
412 if (in_array(
$object->origin, array(
'order',
'commande')) &&
$object->origin_id > 0) {
413 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
415 $ret = $order->fetch(
$object->origin_id);
420 $ret = $order->fetchObjectLinked($order->id,
'commande',
null,
'shipping');
426 if (is_array($order->linkedObjects) && count($order->linkedObjects) > 0) {
427 foreach ($order->linkedObjects as $type => $shipping_array) {
428 if ($type !=
'shipping' || !is_array($shipping_array) || count($shipping_array) == 0) {
432 foreach ($shipping_array as $shipping) {
433 if ($shipping->status <= 0 || !is_array($shipping->lines) || count($shipping->lines) == 0) {
437 foreach ($shipping->lines as $shippingline) {
438 if (isset($qtyshipped[$shippingline->fk_product])) {
439 $qtyshipped[$shippingline->fk_product] += $shippingline->qty;
441 $qtyshipped[$shippingline->fk_product] = $shippingline->qty;
449 if (is_array($order->lines) && count($order->lines) > 0) {
450 foreach ($order->lines as $orderline) {
455 if (isset($qtyordred[$shippingline->fk_product])) {
456 $qtyordred[$orderline->fk_product] += $orderline->qty;
458 $qtyordred[$orderline->fk_product] = $orderline->qty;
465 $diff_array = array_diff_assoc($qtyordred, $qtyshipped);
466 if (count($diff_array) == 0) {
479 if (($action ==
'RECEPTION_VALIDATE') || ($action ==
'RECEPTION_CLOSED')) {
480 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".
$object->id);
482 if ((isModEnabled(
"fournisseur") || isModEnabled(
"supplier_order")) && isModEnabled(
"reception") && isModEnabled(
'workflow') &&
484 (
getDolGlobalString(
'WORKFLOW_ORDER_CLASSIFY_RECEIVED_RECEPTION') && ($action ==
'RECEPTION_VALIDATE')) ||
485 (
getDolGlobalString(
'WORKFLOW_ORDER_CLASSIFY_RECEIVED_RECEPTION_CLOSED') && ($action ==
'RECEPTION_CLOSED'))
488 $qtyshipped = array();
489 $qtyordred = array();
494 if (in_array(
$object->origin, array(
'order_supplier',
'supplier_order',
'commandeFournisseur')) &&
$object->origin_id > 0) {
495 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.commande.class.php';
497 $ret = $order->fetch(
$object->origin_id);
502 $ret = $order->fetchObjectLinked($order->id, $order->element,
null,
'reception');
509 if (is_array($order->linkedObjects) && count($order->linkedObjects) > 0) {
510 foreach ($order->linkedObjects as $type => $shipping_array) {
511 if ($type !=
'reception' || !is_array($shipping_array) || count($shipping_array) == 0) {
515 foreach ($shipping_array as $shipping) {
516 if (!is_array($shipping->lines) || count($shipping->lines) == 0) {
520 foreach ($shipping->lines as $shippingline) {
521 $qtyshipped[$shippingline->fk_product] += $shippingline->qty;
528 if (is_array($order->lines) && count($order->lines) > 0) {
529 foreach ($order->lines as $orderline) {
534 $qtyordred[$orderline->fk_product] += $orderline->qty;
540 $diff_array = array_diff_assoc($qtyordred, $qtyshipped);
541 if (count($diff_array) == 0) {
553 if ($action ==
'TICKET_CREATE') {
554 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".
$object->id);
556 if (isModEnabled(
'contract') && isModEnabled(
'ticket') && isModEnabled(
'workflow') &&
getDolGlobalString(
'WORKFLOW_TICKET_LINK_CONTRACT') &&
getDolGlobalString(
'TICKET_PRODUCT_CATEGORY') && !empty(
$object->fk_soc)) {
557 $societe =
new Societe($this->db);
560 $contrat =
new Contrat($this->db);
561 $number_contracts_found = 0;
562 foreach ($company_ids as $company_id) {
563 $contrat->socid = $company_id;
564 $list = $contrat->getListOfContracts(
'all', array(Contrat::STATUS_DRAFT, Contrat::STATUS_VALIDATED), array(
getDolGlobalString(
'TICKET_PRODUCT_CATEGORY')), array(ContratLigne::STATUS_INITIAL, ContratLigne::STATUS_OPEN));
565 if (!is_array($list) || empty($list)) {
568 $number_contracts_found = count($list);
569 if ($number_contracts_found == 0) {
573 foreach ($list as $linked_contract) {
574 $object->setContract($linked_contract->id);
578 if ($number_contracts_found > 1 && !defined(
'NOLOGIN')) {
579 setEventMessages($langs->trans(
'TicketManyContractsLinked'),
null,
'warnings');
583 if ($number_contracts_found == 0 && !defined(
'NOLOGIN')) {
584 setEventMessages($langs->trans(
'TicketNoContractFoundToLink'),
null,
'mesgs');
588 if (isModEnabled(
'intervention') && isModEnabled(
'ticket') && isModEnabled(
'workflow') &&
getDolGlobalString(
'WORKFLOW_TICKET_CREATE_INTERVENTION')) {
590 $fichinter->socid = (int)
$object->fk_soc;
591 $fichinter->fk_project = (int)
$object->fk_project;
592 $fichinter->fk_contrat = (int)
$object->fk_contract;
593 $fichinter->author = $user->id;
595 $fichinter->origin =
$object->element;
596 $fichinter->origin_id =
$object->id;
600 $extrafields->fetch_name_optionals_label($fichinter->table_element);
601 $array_options = $extrafields->getOptionalsFromPost($fichinter->table_element);
602 $fichinter->array_options = $array_options;
604 $id = $fichinter->create($user);
630 return (
price2num($totalonlinkedelements,
'MT') ==
price2num($object_total_ht,
'MT'));