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'])) {
78 if (empty($object->context[
'closedfromonlinesignature'])) {
79 $langs->load(
"orders");
84 include_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
85 $newobject =
new Commande($this->db);
87 $newobject->context[
'createfrompropal'] =
'createfrompropal';
88 $newobject->context[
'origin'] = $object->element;
89 $newobject->context[
'origin_id'] = $object->id;
91 $ret = $newobject->createFromProposal($object, $user);
93 $this->error = $newobject->error;
94 $this->errors[] = $newobject->error;
97 $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') && !empty($conf->global->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);
117 $this->error = $newobject->error;
118 $this->errors[] = $newobject->error;
120 if (empty($object->fk_account) && !empty($object->thirdparty->fk_account) && !
getDolGlobalInt(
'BANK_ASK_PAYMENT_BANK_DURING_ORDER')) {
121 $res = $newobject->setBankAccount($object->thirdparty->fk_account,
true, $user);
123 $this->error = $newobject->error;
124 $this->errors[] = $newobject->error;
129 $object->clearObjectLinkedCache();
136 if ($action ==
'ORDER_CLASSIFY_BILLED') {
137 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
138 if (isModEnabled(
"propal") && !empty($conf->workflow->enabled) && !empty($conf->global->WORKFLOW_ORDER_CLASSIFY_BILLED_PROPAL)) {
139 $object->fetchObjectLinked(
'',
'propal', $object->id, $object->element);
140 if (!empty($object->linkedObjects)) {
141 $totalonlinkedelements = 0;
142 foreach ($object->linkedObjects[
'propal'] as $element) {
144 $totalonlinkedelements += $element->total_ht;
147 dol_syslog(
"Amount of linked proposals = ".$totalonlinkedelements.
", of order = ".$object->total_ht.
", egality is ".($totalonlinkedelements == $object->total_ht));
148 if ($this->
shouldClassify($conf, $totalonlinkedelements, $object->total_ht)) {
149 foreach ($object->linkedObjects[
'propal'] as $element) {
150 $ret = $element->classifyBilled($user);
159 if ($action ==
'BILL_VALIDATE') {
160 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
163 if (isModEnabled(
'commande') && !empty($conf->workflow->enabled) && !empty($conf->global->WORKFLOW_INVOICE_AMOUNT_CLASSIFY_BILLED_ORDER)) {
164 $object->fetchObjectLinked(
'',
'commande', $object->id, $object->element);
165 if (!empty($object->linkedObjects)) {
166 $totalonlinkedelements = 0;
167 foreach ($object->linkedObjects[
'commande'] as $element) {
169 $totalonlinkedelements += $element->total_ht;
172 dol_syslog(
"Amount of linked orders = ".$totalonlinkedelements.
", of invoice = ".$object->total_ht.
", egality is ".($totalonlinkedelements == $object->total_ht));
173 if ($this->
shouldClassify($conf, $totalonlinkedelements, $object->total_ht)) {
174 foreach ($object->linkedObjects[
'commande'] as $element) {
175 $ret = $element->classifyBilled($user);
182 if (isModEnabled(
"propal") && !empty($conf->workflow->enabled) && !empty($conf->global->WORKFLOW_INVOICE_CLASSIFY_BILLED_PROPAL)) {
183 $object->fetchObjectLinked(
'',
'propal', $object->id, $object->element);
184 if (!empty($object->linkedObjects)) {
185 $totalonlinkedelements = 0;
186 foreach ($object->linkedObjects[
'propal'] as $element) {
188 $totalonlinkedelements += $element->total_ht;
191 dol_syslog(
"Amount of linked proposals = ".$totalonlinkedelements.
", of invoice = ".$object->total_ht.
", egality is ".($totalonlinkedelements == $object->total_ht));
192 if ($this->
shouldClassify($conf, $totalonlinkedelements, $object->total_ht)) {
193 foreach ($object->linkedObjects[
'propal'] as $element) {
194 $ret = $element->classifyBilled($user);
200 if (isModEnabled(
"expedition") && !empty($conf->workflow->enabled) && !empty($conf->global->WORKFLOW_SHIPPING_CLASSIFY_CLOSED_INVOICE)) {
201 $object->fetchObjectLinked(
'',
'shipping', $object->id, $object->element);
202 if (!empty($object->linkedObjects)) {
203 $totalonlinkedelements = 0;
204 foreach ($object->linkedObjects[
'shipping'] as $element) {
206 $totalonlinkedelements += $element->total_ht;
209 dol_syslog(
"Amount of linked shipment = ".$totalonlinkedelements.
", of invoice = ".$object->total_ht.
", egality is ".($totalonlinkedelements == $object->total_ht), LOG_DEBUG);
210 if ($totalonlinkedelements == $object->total_ht) {
211 foreach ($object->linkedObjects[
'shipping'] as $element) {
212 $ret = $element->setClosed();
225 if ($action ==
'BILL_SUPPLIER_VALIDATE') {
226 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
230 if ((isModEnabled(
"supplier_order") || isModEnabled(
"supplier_invoice")) && !empty($conf->global->WORKFLOW_INVOICE_AMOUNT_CLASSIFY_BILLED_SUPPLIER_ORDER)) {
231 $object->fetchObjectLinked(
'',
'order_supplier', $object->id, $object->element);
232 if (!empty($object->linkedObjects)) {
233 $totalonlinkedelements = 0;
234 foreach ($object->linkedObjects[
'order_supplier'] as $element) {
236 $totalonlinkedelements += $element->total_ht;
239 dol_syslog(
"Amount of linked orders = ".$totalonlinkedelements.
", of invoice = ".$object->total_ht.
", egality is ".($totalonlinkedelements == $object->total_ht));
240 if ($this->
shouldClassify($conf, $totalonlinkedelements, $object->total_ht)) {
241 foreach ($object->linkedObjects[
'order_supplier'] as $element) {
242 $ret = $element->classifyBilled($user);
252 if (isModEnabled(
'supplier_proposal') && !empty($conf->global->WORKFLOW_INVOICE_CLASSIFY_BILLED_SUPPLIER_PROPOSAL)) {
253 $object->fetchObjectLinked(
'',
'supplier_proposal', $object->id, $object->element);
254 if (!empty($object->linkedObjects)) {
255 $totalonlinkedelements = 0;
256 foreach ($object->linkedObjects[
'supplier_proposal'] as $element) {
258 $totalonlinkedelements += $element->total_ht;
261 dol_syslog(
"Amount of linked supplier proposals = ".$totalonlinkedelements.
", of supplier invoice = ".$object->total_ht.
", egality is ".($totalonlinkedelements == $object->total_ht));
262 if ($this->
shouldClassify($conf, $totalonlinkedelements, $object->total_ht)) {
263 foreach ($object->linkedObjects[
'supplier_proposal'] as $element) {
264 $ret = $element->classifyBilled($user);
274 if (isModEnabled(
"reception") && !empty($conf->workflow->enabled) && !empty($conf->global->WORKFLOW_RECEPTION_CLASSIFY_CLOSED_INVOICE)) {
275 $object->fetchObjectLinked(
'',
'reception', $object->id, $object->element);
276 if (!empty($object->linkedObjects)) {
277 $totalonlinkedelements = 0;
278 foreach ($object->linkedObjects[
'reception'] as $element) {
279 if ($element->statut == Reception::STATUS_VALIDATED || $element->statut == Reception::STATUS_CLOSED) {
280 $totalonlinkedelements += $element->total_ht;
283 dol_syslog(
"Amount of linked reception = ".$totalonlinkedelements.
", of invoice = ".$object->total_ht.
", egality is ".((
string) $totalonlinkedelements == (
string) $object->total_ht), LOG_DEBUG);
284 if ( (
string) $totalonlinkedelements == (
string) $object->total_ht) {
285 foreach ($object->linkedObjects[
'reception'] as $element) {
286 $ret = $element->setBilled();
299 if ($action ==
'BILL_PAYED') {
300 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
302 if (isModEnabled(
'commande') && !empty($conf->global->WORKFLOW_INVOICE_CLASSIFY_BILLED_ORDER)) {
303 $object->fetchObjectLinked(
'',
'commande', $object->id, $object->element);
304 if (!empty($object->linkedObjects)) {
305 $totalonlinkedelements = 0;
306 foreach ($object->linkedObjects[
'commande'] as $element) {
308 $totalonlinkedelements += $element->total_ht;
311 dol_syslog(
"Amount of linked orders = ".$totalonlinkedelements.
", of invoice = ".$object->total_ht.
", egality is ".($totalonlinkedelements == $object->total_ht));
312 if ($this->
shouldClassify($conf, $totalonlinkedelements, $object->total_ht)) {
313 foreach ($object->linkedObjects[
'commande'] as $element) {
314 $ret = $element->classifyBilled($user);
323 if (($action ==
'SHIPPING_VALIDATE') || ($action ==
'SHIPPING_CLOSED')) {
324 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
326 if (isModEnabled(
'commande') && isModEnabled(
"expedition") && !empty($conf->workflow->enabled) &&
328 (!empty($conf->global->WORKFLOW_ORDER_CLASSIFY_SHIPPED_SHIPPING) && ($action ==
'SHIPPING_VALIDATE')) ||
329 (!empty($conf->global->WORKFLOW_ORDER_CLASSIFY_SHIPPED_SHIPPING_CLOSED) && ($action ==
'SHIPPING_CLOSED'))
332 $qtyshipped = array();
333 $qtyordred = array();
334 require_once DOL_DOCUMENT_ROOT.
'/commande/class/commande.class.php';
338 $ret = $order->fetch($object->origin_id);
340 $this->error = $order->error;
341 $this->errors = $order->errors;
344 $ret = $order->fetchObjectLinked($order->id,
'commande',
null,
'shipping');
346 $this->error = $order->error;
347 $this->errors = $order->errors;
351 if (is_array($order->linkedObjects) && count($order->linkedObjects) > 0) {
352 foreach ($order->linkedObjects as $type => $shipping_array) {
353 if ($type ==
'shipping' && is_array($shipping_array) && count($shipping_array) > 0) {
354 foreach ($shipping_array as $shipping) {
355 if (is_array($shipping->lines) && count($shipping->lines) > 0) {
356 foreach ($shipping->lines as $shippingline) {
357 $qtyshipped[$shippingline->fk_product] += $shippingline->qty;
366 if (is_array($order->lines) && count($order->lines) > 0) {
367 foreach ($order->lines as $orderline) {
369 if (empty($conf->global->STOCK_SUPPORTS_SERVICES) && $orderline->product_type > 0) {
372 $qtyordred[$orderline->fk_product] += $orderline->qty;
378 $diff_array = array_diff_assoc($qtyordred, $qtyshipped);
379 if (count($diff_array) == 0) {
383 $this->error = $order->error;
384 $this->errors = $order->errors;
392 if (($action ==
'RECEPTION_VALIDATE') || ($action ==
'RECEPTION_CLOSED')) {
393 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
395 if ((isModEnabled(
"fournisseur") || isModEnabled(
"supplier_order")) && isModEnabled(
"reception") && !empty($conf->workflow->enabled) &&
397 (!empty($conf->global->WORKFLOW_ORDER_CLASSIFY_RECEIVED_RECEPTION) && ($action ==
'RECEPTION_VALIDATE')) ||
398 (!empty($conf->global->WORKFLOW_ORDER_CLASSIFY_RECEIVED_RECEPTION_CLOSED) && ($action ==
'RECEPTION_CLOSED'))
401 $qtyshipped = array();
402 $qtyordred = array();
403 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.commande.class.php';
407 $ret = $order->fetch($object->origin_id);
409 $this->error = $order->error;
410 $this->errors = $order->errors;
413 $ret = $order->fetchObjectLinked($order->id, $order->element,
null,
'reception');
415 $this->error = $order->error;
416 $this->errors = $order->errors;
420 if (is_array($order->linkedObjects) && count($order->linkedObjects) > 0) {
421 foreach ($order->linkedObjects as $type => $shipping_array) {
422 if ($type ==
'reception' && is_array($shipping_array) && count($shipping_array) > 0) {
423 foreach ($shipping_array as $shipping) {
424 if (is_array($shipping->lines) && count($shipping->lines) > 0) {
425 foreach ($shipping->lines as $shippingline) {
426 $qtyshipped[$shippingline->fk_product] += $shippingline->qty;
435 if (is_array($order->lines) && count($order->lines) > 0) {
436 foreach ($order->lines as $orderline) {
438 if (empty($conf->global->STOCK_SUPPORTS_SERVICES) && $orderline->product_type > 0) {
441 $qtyordred[$orderline->fk_product] += $orderline->qty;
447 $diff_array = array_diff_assoc($qtyordred, $qtyshipped);
448 if (count($diff_array) == 0) {
452 $this->error = $order->error;
453 $this->errors = $order->errors;
460 if ($action ==
'TICKET_CREATE') {
461 dol_syslog(
"Trigger '".$this->
name.
"' for action '$action' launched by ".__FILE__.
". id=".$object->id);
463 if (isModEnabled(
'contract') && isModEnabled(
'ticket') && isModEnabled(
'workflow') &&
getDolGlobalString(
'WORKFLOW_TICKET_LINK_CONTRACT') &&
getDolGlobalString(
'TICKET_PRODUCT_CATEGORY') && !empty($object->fk_soc)) {
464 $societe =
new Societe($this->db);
465 $company_ids = (!
getDolGlobalString(
'WORKFLOW_TICKET_USE_PARENT_COMPANY_CONTRACTS')) ? [$object->fk_soc] : $societe->getParentsForCompany($object->fk_soc, [$object->fk_soc]);
467 $contrat =
new Contrat($this->db);
468 $number_contracts_found = 0;
469 foreach ($company_ids as $company_id) {
470 $contrat->socid = $company_id;
471 $list = $contrat->getListOfContracts(
'all', array(Contrat::STATUS_DRAFT, Contrat::STATUS_VALIDATED), array(
getDolGlobalString(
'TICKET_PRODUCT_CATEGORY')), array(ContratLigne::STATUS_INITIAL, ContratLigne::STATUS_OPEN));
472 if (!is_array($list) || empty($list)) {
475 $number_contracts_found = count($list);
477 foreach ($list as $linked_contract) {
478 $object->setContract($linked_contract->id);
482 if ($number_contracts_found > 1 && !defined(
'NOLOGIN')) {
483 setEventMessage($langs->trans(
'TicketManyContractsLinked'),
'warnings');
486 if ($number_contracts_found == 0 && !defined(
'NOLOGIN')) {
487 setEventMessage($langs->trans(
'TicketNoContractFoundToLink'),
'mesgs');
491 if (isModEnabled(
'ficheinter') && isModEnabled(
'ticket') && isModEnabled(
'workflow') &&
getDolGlobalString(
'WORKFLOW_TICKET_CREATE_INTERVENTION')) {
493 $fichinter->socid = (int) $object->fk_soc;
494 $fichinter->fk_project = (int) $object->fk_project;
495 $fichinter->fk_contrat = (int) $object->fk_contract;
496 $fichinter->author = $user->id;
498 $fichinter->origin = $object->element;
499 $fichinter->origin_id = $object->id;
503 $extrafields->fetch_name_optionals_label($fichinter->table_element);
504 $array_options = $extrafields->getOptionalsFromPost($fichinter->table_element);
505 $fichinter->array_options = $array_options;
507 $id = $fichinter->create($user);