27 require_once DOL_DOCUMENT_ROOT.
'/core/triggers/dolibarrtriggers.class.php';
45 $this->
name = preg_replace(
'/^Interface/i',
'', get_class($this));
46 $this->family =
"core";
47 $this->
description =
"Triggers of this module allows to manage workflows";
49 $this->version = self::VERSION_DOLIBARR;
50 $this->picto =
'technic';
66 if (empty($conf->workflow) || empty($conf->workflow->enabled)) {
73 if ($action ==
'PROPAL_CLOSE_SIGNED') {
74 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
75 if (
isModEnabled(
'commande') && !empty($conf->global->WORKFLOW_PROPAL_AUTOCREATE_ORDER)) {
76 $object->fetchObjectLinked();
77 if (!empty($object->linkedObjectsIds[
'commande'])) {
81 include_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
84 $newobject->context[
'createfrompropal'] =
'createfrompropal';
85 $newobject->context[
'origin'] = $object->element;
86 $newobject->context[
'origin_id'] = $object->id;
88 $ret = $newobject->createFromProposal($object, $user);
90 $this->error = $newobject->error;
91 $this->errors[] = $newobject->error;
94 $object->clearObjectLinkedCache();
102 if ($action ==
'ORDER_CLOSE') {
103 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
104 if (
isModEnabled(
'facture') && !empty($conf->global->WORKFLOW_ORDER_AUTOCREATE_INVOICE)) {
105 include_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
108 $newobject->context[
'createfromorder'] =
'createfromorder';
109 $newobject->context[
'origin'] = $object->element;
110 $newobject->context[
'origin_id'] = $object->id;
112 $ret = $newobject->createFromOrder($object, $user);
114 $this->error = $newobject->error;
115 $this->errors[] = $newobject->error;
118 $object->clearObjectLinkedCache();
125 if ($action ==
'ORDER_CLASSIFY_BILLED') {
126 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
127 if (
isModEnabled(
"propal") && !empty($conf->workflow->enabled) && !empty($conf->global->WORKFLOW_ORDER_CLASSIFY_BILLED_PROPAL)) {
128 $object->fetchObjectLinked(
'',
'propal', $object->id, $object->element);
129 if (!empty($object->linkedObjects)) {
130 $totalonlinkedelements = 0;
131 foreach ($object->linkedObjects[
'propal'] as $element) {
133 $totalonlinkedelements += $element->total_ht;
136 dol_syslog(
"Amount of linked proposals = ".$totalonlinkedelements.
", of order = ".$object->total_ht.
", egality is ".($totalonlinkedelements == $object->total_ht));
137 if ($this->
shouldClassify($conf, $totalonlinkedelements, $object->total_ht)) {
138 foreach ($object->linkedObjects[
'propal'] as $element) {
139 $ret = $element->classifyBilled($user);
148 if ($action ==
'BILL_VALIDATE') {
149 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
152 if (
isModEnabled(
'commande') && !empty($conf->workflow->enabled) && !empty($conf->global->WORKFLOW_INVOICE_AMOUNT_CLASSIFY_BILLED_ORDER)) {
153 $object->fetchObjectLinked(
'',
'commande', $object->id, $object->element);
154 if (!empty($object->linkedObjects)) {
155 $totalonlinkedelements = 0;
156 foreach ($object->linkedObjects[
'commande'] as $element) {
158 $totalonlinkedelements += $element->total_ht;
161 dol_syslog(
"Amount of linked orders = ".$totalonlinkedelements.
", of invoice = ".$object->total_ht.
", egality is ".($totalonlinkedelements == $object->total_ht));
162 if ($this->
shouldClassify($conf, $totalonlinkedelements, $object->total_ht)) {
163 foreach ($object->linkedObjects[
'commande'] as $element) {
164 $ret = $element->classifyBilled($user);
171 if (
isModEnabled(
"propal") && !empty($conf->workflow->enabled) && !empty($conf->global->WORKFLOW_INVOICE_CLASSIFY_BILLED_PROPAL)) {
172 $object->fetchObjectLinked(
'',
'propal', $object->id, $object->element);
173 if (!empty($object->linkedObjects)) {
174 $totalonlinkedelements = 0;
175 foreach ($object->linkedObjects[
'propal'] as $element) {
177 $totalonlinkedelements += $element->total_ht;
180 dol_syslog(
"Amount of linked proposals = ".$totalonlinkedelements.
", of invoice = ".$object->total_ht.
", egality is ".($totalonlinkedelements == $object->total_ht));
181 if ($this->
shouldClassify($conf, $totalonlinkedelements, $object->total_ht)) {
182 foreach ($object->linkedObjects[
'propal'] as $element) {
183 $ret = $element->classifyBilled($user);
189 if (
isModEnabled(
"expedition") && !empty($conf->workflow->enabled) && !empty($conf->global->WORKFLOW_SHIPPING_CLASSIFY_CLOSED_INVOICE)) {
190 $object->fetchObjectLinked(
'',
'shipping', $object->id, $object->element);
191 if (!empty($object->linkedObjects)) {
192 $totalonlinkedelements = 0;
193 foreach ($object->linkedObjects[
'shipping'] as $element) {
195 $totalonlinkedelements += $element->total_ht;
198 dol_syslog(
"Amount of linked shipment = ".$totalonlinkedelements.
", of invoice = ".$object->total_ht.
", egality is ".($totalonlinkedelements == $object->total_ht), LOG_DEBUG);
199 if ($totalonlinkedelements == $object->total_ht) {
200 foreach ($object->linkedObjects[
'shipping'] as $element) {
201 $ret = $element->setClosed();
214 if ($action ==
'BILL_SUPPLIER_VALIDATE') {
215 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
219 if (((
isModEnabled(
"fournisseur") && empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)) ||
isModEnabled(
"supplier_order") ||
isModEnabled(
"supplier_invoice")) && !empty($conf->global->WORKFLOW_INVOICE_AMOUNT_CLASSIFY_BILLED_SUPPLIER_ORDER)) {
220 $object->fetchObjectLinked(
'',
'order_supplier', $object->id, $object->element);
221 if (!empty($object->linkedObjects)) {
222 $totalonlinkedelements = 0;
223 foreach ($object->linkedObjects[
'order_supplier'] as $element) {
225 $totalonlinkedelements += $element->total_ht;
228 dol_syslog(
"Amount of linked orders = ".$totalonlinkedelements.
", of invoice = ".$object->total_ht.
", egality is ".($totalonlinkedelements == $object->total_ht));
229 if ($this->
shouldClassify($conf, $totalonlinkedelements, $object->total_ht)) {
230 foreach ($object->linkedObjects[
'order_supplier'] as $element) {
231 $ret = $element->classifyBilled($user);
241 if (
isModEnabled(
'supplier_proposal') && !empty($conf->global->WORKFLOW_INVOICE_CLASSIFY_BILLED_SUPPLIER_PROPOSAL)) {
242 $object->fetchObjectLinked(
'',
'supplier_proposal', $object->id, $object->element);
243 if (!empty($object->linkedObjects)) {
244 $totalonlinkedelements = 0;
245 foreach ($object->linkedObjects[
'supplier_proposal'] as $element) {
247 $totalonlinkedelements += $element->total_ht;
250 dol_syslog(
"Amount of linked supplier proposals = ".$totalonlinkedelements.
", of supplier invoice = ".$object->total_ht.
", egality is ".($totalonlinkedelements == $object->total_ht));
251 if ($this->
shouldClassify($conf, $totalonlinkedelements, $object->total_ht)) {
252 foreach ($object->linkedObjects[
'supplier_proposal'] as $element) {
253 $ret = $element->classifyBilled($user);
263 if (
isModEnabled(
"reception") && !empty($conf->workflow->enabled) && !empty($conf->global->WORKFLOW_EXPEDITION_CLASSIFY_CLOSED_INVOICE)) {
264 $object->fetchObjectLinked(
'',
'reception', $object->id, $object->element);
265 if (!empty($object->linkedObjects)) {
266 $totalonlinkedelements = 0;
267 foreach ($object->linkedObjects[
'reception'] as $element) {
268 if ($element->statut == Reception::STATUS_VALIDATED) {
269 $totalonlinkedelements += $element->total_ht;
272 dol_syslog(
"Amount of linked reception = ".$totalonlinkedelements.
", of invoice = ".$object->total_ht.
", egality is ".($totalonlinkedelements == $object->total_ht), LOG_DEBUG);
273 if ($totalonlinkedelements == $object->total_ht) {
274 foreach ($object->linkedObjects[
'reception'] as $element) {
275 $ret = $element->setBilled();
288 if ($action ==
'BILL_PAYED') {
289 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
291 if (
isModEnabled(
'commande') && !empty($conf->global->WORKFLOW_INVOICE_CLASSIFY_BILLED_ORDER)) {
292 $object->fetchObjectLinked(
'',
'commande', $object->id, $object->element);
293 if (!empty($object->linkedObjects)) {
294 $totalonlinkedelements = 0;
295 foreach ($object->linkedObjects[
'commande'] as $element) {
297 $totalonlinkedelements += $element->total_ht;
300 dol_syslog(
"Amount of linked orders = ".$totalonlinkedelements.
", of invoice = ".$object->total_ht.
", egality is ".($totalonlinkedelements == $object->total_ht));
301 if ($this->
shouldClassify($conf, $totalonlinkedelements, $object->total_ht)) {
302 foreach ($object->linkedObjects[
'commande'] as $element) {
303 $ret = $element->classifyBilled($user);
312 if (($action ==
'SHIPPING_VALIDATE') || ($action ==
'SHIPPING_CLOSED')) {
313 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
317 (!empty($conf->global->WORKFLOW_ORDER_CLASSIFY_SHIPPED_SHIPPING) && ($action ==
'SHIPPING_VALIDATE')) ||
318 (!empty($conf->global->WORKFLOW_ORDER_CLASSIFY_SHIPPED_SHIPPING_CLOSED) && ($action ==
'SHIPPING_CLOSED'))
321 $qtyshipped = array();
322 $qtyordred = array();
323 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
327 $ret = $order->fetch($object->origin_id);
329 $this->error = $order->error;
330 $this->errors = $order->errors;
333 $ret = $order->fetchObjectLinked($order->id,
'commande',
null,
'shipping');
335 $this->error = $order->error;
336 $this->errors = $order->errors;
340 if (is_array($order->linkedObjects) && count($order->linkedObjects) > 0) {
341 foreach ($order->linkedObjects as $type => $shipping_array) {
342 if ($type ==
'shipping' && is_array($shipping_array) && count($shipping_array) > 0) {
343 foreach ($shipping_array as $shipping) {
344 if (is_array($shipping->lines) && count($shipping->lines) > 0) {
345 foreach ($shipping->lines as $shippingline) {
346 $qtyshipped[$shippingline->fk_product] += $shippingline->qty;
355 if (is_array($order->lines) && count($order->lines) > 0) {
356 foreach ($order->lines as $orderline) {
358 if (empty($conf->global->STOCK_SUPPORTS_SERVICES) && $orderline->product_type > 0) {
361 $qtyordred[$orderline->fk_product] += $orderline->qty;
367 $diff_array = array_diff_assoc($qtyordred, $qtyshipped);
368 if (count($diff_array) == 0) {
372 $this->error = $order->error;
373 $this->errors = $order->errors;
381 if (($action ==
'RECEPTION_VALIDATE') || ($action ==
'RECEPTION_CLOSED')) {
382 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
386 (!empty($conf->global->WORKFLOW_ORDER_CLASSIFY_RECEIVED_RECEPTION) && ($action ==
'RECEPTION_VALIDATE')) ||
387 (!empty($conf->global->WORKFLOW_ORDER_CLASSIFY_RECEIVED_RECEPTION_CLOSED) && ($action ==
'RECEPTION_CLOSED'))
390 $qtyshipped = array();
391 $qtyordred = array();
392 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.commande.class.php';
396 $ret = $order->fetch($object->origin_id);
398 $this->error = $order->error;
399 $this->errors = $order->errors;
402 $ret = $order->fetchObjectLinked($order->id,
'supplier_order',
null,
'reception');
404 $this->error = $order->error;
405 $this->errors = $order->errors;
409 if (is_array($order->linkedObjects) && count($order->linkedObjects) > 0) {
410 foreach ($order->linkedObjects as $type => $shipping_array) {
411 if ($type ==
'reception' && is_array($shipping_array) && count($shipping_array) > 0) {
412 foreach ($shipping_array as $shipping) {
413 if (is_array($shipping->lines) && count($shipping->lines) > 0) {
414 foreach ($shipping->lines as $shippingline) {
415 $qtyshipped[$shippingline->fk_product] += $shippingline->qty;
424 if (is_array($order->lines) && count($order->lines) > 0) {
425 foreach ($order->lines as $orderline) {
427 if (empty($conf->global->STOCK_SUPPORTS_SERVICES) && $orderline->product_type > 0) {
430 $qtyordred[$orderline->fk_product] += $orderline->qty;
436 $diff_array = array_diff_assoc($qtyordred, $qtyshipped);
437 if (count($diff_array) == 0) {
441 $this->error = $order->error;
442 $this->errors = $order->errors;
449 if ($action ==
'TICKET_CREATE') {
450 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
452 if (!empty($conf->contract->enabled) &&
isModEnabled(
'ticket') &&
isModEnabled(
'ficheinter') && !empty($conf->workflow->enabled) && !empty($conf->global->WORKFLOW_TICKET_LINK_CONTRACT) && !empty($conf->global->TICKET_PRODUCT_CATEGORY) && !empty($object->fk_soc)) {
454 $company_ids = (empty($conf->global->WORKFLOW_TICKET_USE_PARENT_COMPANY_CONTRACTS)) ? [$object->fk_soc] : $societe->getParentsForCompany($object->fk_soc, [$object->fk_soc]);
457 $number_contracts_found = 0;
458 foreach ($company_ids as $company_id) {
459 $contrat->socid = $company_id;
460 $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]);
461 if (is_array($list) && !empty($list)) {
462 $number_contracts_found = count($list);
463 if ($number_contracts_found == 1) {
464 foreach ($list as $linked_contract) {
465 $object->setContract($linked_contract->id);
468 } elseif ($number_contracts_found > 1) {
469 foreach ($list as $linked_contract) {
470 $object->setContract($linked_contract->id);
473 if (empty(NOLOGIN))
setEventMessage($langs->trans(
'TicketManyContractsLinked'),
'warnings');
478 if ($number_contracts_found == 0) {
479 if (empty(NOLOGIN))
setEventMessage($langs->trans(
'TicketNoContractFoundToLink'),
'mesgs');
483 if (
isModEnabled(
'ficheinter') &&
isModEnabled(
'ticket') && !empty($conf->workflow->enabled) && !empty($conf->global->WORKFLOW_TICKET_CREATE_INTERVENTION)) {
485 $fichinter->socid = (int) $object->fk_soc;
486 $fichinter->fk_project = $projectid;
487 $fichinter->fk_contrat = (
int) $object->fk_contract;
488 $fichinter->author = $user->id;
489 $fichinter->model_pdf = (!empty($conf->global->FICHEINTER_ADDON_PDF)) ? $conf->global->FICHEINTER_ADDON_PDF :
'soleil';
490 $fichinter->origin = $object->element;
491 $fichinter->origin_id = $object->id;
495 $extrafields->fetch_name_optionals_label($fichinter->table_element);
496 $array_options = $extrafields->getOptionalsFromPost($fichinter->table_element);
497 $fichinter->array_options = $array_options;
499 $id = $fichinter->create($user);
521 if (!empty($conf->global->WORKFLOW_CLASSIFY_IF_AMOUNTS_ARE_DIFFERENTS)) {
525 return (
price2num($totalonlinkedelements,
'MT') ==
price2num($object_total_ht,
'MT'));
Class to manage predefined suppliers products.
const STATUS_RECEIVED_PARTIALLY
Received partially.
const STATUS_RECEIVED_COMPLETELY
Received completely.
const STATUS_ACCEPTED
Accepted.
const STATUS_ORDERSENT
Order sent, shipment on process.
Class to manage customers orders.
const STATUS_SHIPMENTONPROCESS
Shipment on process.
const STATUS_CLOSED
Closed (Sent, billed or not)
const STATUS_VALIDATED
Validated status.
Class to stock current configuration.
Class to manage contracts.
Class that all the triggers must extend.
const STATUS_VALIDATED
Validated status.
Class to manage invoices.
Class to manage interventions.
Class of triggers for workflow module.
runTrigger($action, $object, User $user, Translate $langs, Conf $conf)
Function called when a Dolibarrr business event is done.
__construct($db)
Constructor.
shouldClassify($conf, $totalonlinkedelements, $object_total_ht)
const STATUS_SIGNED
Signed quote.
const STATUS_BILLED
Billed or processed quote.
Class to manage third parties objects (customers, suppliers, prospects...)
const STATUS_SIGNED
Signed quote.
Class to manage translations.
Class to manage Dolibarr users.
print *****$script_file(".$version.") pid cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
setEventMessage($mesgs, $style='mesgs')
Set event message in dol_events session object.
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
$conf db name
Only used if Module[ID]Name translation string is not found.
$conf db
API class for accounts.