11<?php
22/**
33 * This file is part of Servicios plugin for FacturaScripts
4- * Copyright (C) 2020-2024 Carlos Garcia Gomez <carlos@facturascripts.com>
4+ * Copyright (C) 2020-2026 Carlos Garcia Gomez <carlos@facturascripts.com>
55 *
66 * This program is free software: you can redistribute it and/or modify
77 * it under the terms of the GNU Lesser General Public License as
4343 */
4444class ServicioAT extends ModelClass
4545{
46- use ModelTrait;
4746 use CompanyRelationTrait;
47+ use ModelTrait;
4848
4949 /** @var string */
5050 public $ asignado ;
@@ -88,24 +88,24 @@ class ServicioAT extends ModelClass
8888 /** @var int */
8989 public $ idmaquina4 ;
9090
91- /** @var int */
92- public $ idtipo ;
93-
9491 /** @var int */
9592 public $ idprioridad ;
9693
9794 /** @var int */
9895 public $ idservicio ;
9996
100- /** @var string */
101- public $ material ;
97+ /** @var int */
98+ public $ idtipo ;
10299
103100 /** @var string */
104- public $ nick ;
101+ public $ material ;
105102
106103 /** @var double */
107104 public $ neto ;
108105
106+ /** @var string */
107+ public $ nick ;
108+
109109 /** @var string */
110110 public $ observaciones ;
111111
@@ -180,7 +180,9 @@ public function delete(): bool
180180
181181 // añadimos el cambio al log
182182 $ messageLog = Tools::trans ('deleted-service ' );
183- $ this ->log ($ messageLog );
183+ if (false === $ this ->log ($ messageLog )) {
184+ Tools::log ()->error ('record-save-error ' , ['%modelo% ' => 'ServicioATLog ' ]);
185+ }
184186
185187 return true ;
186188 }
@@ -208,19 +210,19 @@ public function getAvailablePriority(): array
208210 }
209211
210212 /**
211- * @return TipoAT []
213+ * @return EstadoAT []
212214 */
213- public function getAvailableTypes (): array
215+ public function getAvailableStatus (): array
214216 {
215- return TipoAT ::all ();
217+ return EstadoAT ::all ();
216218 }
217219
218220 /**
219- * @return EstadoAT []
221+ * @return TipoAT []
220222 */
221- public function getAvailableStatus (): array
223+ public function getAvailableTypes (): array
222224 {
223- return EstadoAT ::all ();
225+ return TipoAT ::all ();
224226 }
225227
226228 public function getCustomer (?string $ codcliente = null ): Cliente
@@ -251,26 +253,19 @@ public function getMachines(): array
251253 return $ result ;
252254 }
253255
254- public function getStatus (?int $ idestado = null ): EstadoAT
255- {
256- $ idestado = $ idestado ?? $ this ->idestado ;
257- $ status = new EstadoAT ();
258- $ status ->load ($ idestado );
259- return $ status ;
260- }
261-
262256 public function getPriority (): PrioridadAT
263257 {
264258 $ priority = new PrioridadAT ();
265259 $ priority ->load ($ this ->idprioridad );
266260 return $ priority ;
267261 }
268262
269- public function getType ( ): TipoAT
263+ public function getStatus (? int $ idestado = null ): EstadoAT
270264 {
271- $ type = new TipoAT ();
272- $ type ->load ($ this ->idtipo );
273- return $ type ;
265+ $ idestado = $ idestado ?? $ this ->idestado ;
266+ $ status = new EstadoAT ();
267+ $ status ->load ($ idestado );
268+ return $ status ;
274269 }
275270
276271 public function getSubject (): Cliente
@@ -290,6 +285,13 @@ public function getTrabajos(): array
290285 return DinTrabajoAT::all ($ where , $ order );
291286 }
292287
288+ public function getType (): TipoAT
289+ {
290+ $ type = new TipoAT ();
291+ $ type ->load ($ this ->idtipo );
292+ return $ type ;
293+ }
294+
293295 public function getUser (?string $ nick = null ): User
294296 {
295297 $ nick = is_null ($ nick ) ? $ this ->nick : $ nick ;
@@ -393,6 +395,78 @@ public function url(string $type = 'auto', string $list = 'List'): string
393395 return $ type === 'new ' ? 'NewServicioAT ' : parent ::url ($ type , $ list );
394396 }
395397
398+ protected function notifyAgent (string $ notification ): void
399+ {
400+ $ agent = new Agente ();
401+ if (false === $ agent ->load ($ this ->codagente ) || empty ($ agent ->email )) {
402+ return ;
403+ }
404+
405+ MailNotifier::send ($ notification , $ agent ->email , $ agent ->nombre , [
406+ 'number ' => $ this ->idservicio ,
407+ 'code ' => $ this ->codigo ,
408+ 'date ' => $ this ->fecha ,
409+ 'customer ' => $ this ->getSubject ()->nombre ,
410+ 'author ' => $ this ->nick ,
411+ 'status ' => $ this ->getStatus ()->nombre ,
412+ 'url ' => Tools::siteUrl () . '/EditServicioAT?code= ' . $ this ->idservicio
413+ ]);
414+ }
415+
416+ protected function notifyAssignedUser (string $ notification ): void
417+ {
418+ $ assigned = new User ();
419+ if (false === $ assigned ->load ($ this ->asignado )) {
420+ return ;
421+ }
422+
423+ MailNotifier::send ($ notification , $ assigned ->email , $ assigned ->nick , [
424+ 'number ' => $ this ->idservicio ,
425+ 'code ' => $ this ->codigo ,
426+ 'date ' => $ this ->fecha ,
427+ 'customer ' => $ this ->getSubject ()->nombre ,
428+ 'author ' => $ this ->nick ,
429+ 'status ' => $ this ->getStatus ()->nombre ,
430+ 'url ' => Tools::siteUrl () . '/EditServicioAT?code= ' . $ this ->idservicio
431+ ]);
432+ }
433+
434+ protected function notifyCustomer (string $ notification ): void
435+ {
436+ $ customer = $ this ->getSubject ();
437+ if (empty ($ customer ) || empty ($ customer ->email )) {
438+ return ;
439+ }
440+
441+ MailNotifier::send ($ notification , $ customer ->email , $ customer ->nombre , [
442+ 'number ' => $ this ->idservicio ,
443+ 'code ' => $ this ->codigo ,
444+ 'date ' => $ this ->fecha ,
445+ 'customer ' => $ customer ->nombre ,
446+ 'author ' => $ this ->nick ,
447+ 'status ' => $ this ->getStatus ()->nombre ,
448+ 'url ' => Tools::siteUrl () . '/EditServicioAT?code= ' . $ this ->idservicio
449+ ]);
450+ }
451+
452+ protected function notifyUser (string $ notification ): void
453+ {
454+ $ user = new User ();
455+ if (false === $ user ->load ($ this ->nick )) {
456+ return ;
457+ }
458+
459+ MailNotifier::send ($ notification , $ user ->email , $ user ->nick , [
460+ 'number ' => $ this ->idservicio ,
461+ 'code ' => $ this ->codigo ,
462+ 'date ' => $ this ->fecha ,
463+ 'customer ' => $ this ->getSubject ()->nombre ,
464+ 'author ' => $ this ->nick ,
465+ 'status ' => $ this ->getStatus ()->nombre ,
466+ 'url ' => Tools::siteUrl () . '/EditServicioAT?code= ' . $ this ->idservicio
467+ ]);
468+ }
469+
396470 protected function onChange (string $ field ): bool
397471 {
398472 if ($ field == 'idestado ' ) {
@@ -411,7 +485,9 @@ protected function onChange(string $field): bool
411485 '%oldStatus% ' => $ this ->getStatus ($ this ->getOriginal ('idestado ' ))->nombre ,
412486 '%newStatus% ' => $ newStatus ->nombre
413487 ]);
414- $ this ->log ($ messageLog );
488+ if (false === $ this ->log ($ messageLog )) {
489+ return false ;
490+ }
415491 }
416492
417493 return parent ::onChange ($ field );
@@ -430,8 +506,13 @@ protected function onInsert(): void
430506 $ this ->notifyCustomer ('new-service-customer ' );
431507 }
432508
433- $ message = Tools::trans ('new-service-created ' , ['%number% ' => $ this ->id ()]);
434- $ this ->log ($ message );
509+ $ message = Tools::trans ('new-service-created ' , [
510+ '%number% ' => $ this ->id (),
511+ '%status% ' => $ this ->getStatus ()->nombre
512+ ]);
513+ if (false === $ this ->log ($ message )) {
514+ Tools::log ()->error ('record-save-error ' , ['%modelo% ' => 'ServicioATLog ' ]);
515+ }
435516
436517 parent ::onInsert ();
437518 }
@@ -560,76 +641,4 @@ protected function onUpdateUser(): void
560641 $ this ->notifyUser ('new-service-user ' );
561642 }
562643 }
563-
564- protected function notifyAgent (string $ notification ): void
565- {
566- $ agent = new Agente ();
567- if (false === $ agent ->load ($ this ->codagente ) || empty ($ agent ->email )) {
568- return ;
569- }
570-
571- MailNotifier::send ($ notification , $ agent ->email , $ agent ->nombre , [
572- 'number ' => $ this ->idservicio ,
573- 'code ' => $ this ->codigo ,
574- 'date ' => $ this ->fecha ,
575- 'customer ' => $ this ->getSubject ()->nombre ,
576- 'author ' => $ this ->nick ,
577- 'status ' => $ this ->getStatus ()->nombre ,
578- 'url ' => Tools::siteUrl () . '/EditServicioAT?code= ' . $ this ->idservicio
579- ]);
580- }
581-
582- protected function notifyAssignedUser (string $ notification ): void
583- {
584- $ assigned = new User ();
585- if (false === $ assigned ->load ($ this ->asignado )) {
586- return ;
587- }
588-
589- MailNotifier::send ($ notification , $ assigned ->email , $ assigned ->nick , [
590- 'number ' => $ this ->idservicio ,
591- 'code ' => $ this ->codigo ,
592- 'date ' => $ this ->fecha ,
593- 'customer ' => $ this ->getSubject ()->nombre ,
594- 'author ' => $ this ->nick ,
595- 'status ' => $ this ->getStatus ()->nombre ,
596- 'url ' => Tools::siteUrl () . '/EditServicioAT?code= ' . $ this ->idservicio
597- ]);
598- }
599-
600- protected function notifyCustomer (string $ notification ): void
601- {
602- $ customer = $ this ->getSubject ();
603- if (empty ($ customer ) || empty ($ customer ->email )) {
604- return ;
605- }
606-
607- MailNotifier::send ($ notification , $ customer ->email , $ customer ->nombre , [
608- 'number ' => $ this ->idservicio ,
609- 'code ' => $ this ->codigo ,
610- 'date ' => $ this ->fecha ,
611- 'customer ' => $ customer ->nombre ,
612- 'author ' => $ this ->nick ,
613- 'status ' => $ this ->getStatus ()->nombre ,
614- 'url ' => Tools::siteUrl () . '/EditServicioAT?code= ' . $ this ->idservicio
615- ]);
616- }
617-
618- protected function notifyUser (string $ notification ): void
619- {
620- $ user = new User ();
621- if (false === $ user ->load ($ this ->nick )) {
622- return ;
623- }
624-
625- MailNotifier::send ($ notification , $ user ->email , $ user ->nick , [
626- 'number ' => $ this ->idservicio ,
627- 'code ' => $ this ->codigo ,
628- 'date ' => $ this ->fecha ,
629- 'customer ' => $ this ->getSubject ()->nombre ,
630- 'author ' => $ this ->nick ,
631- 'status ' => $ this ->getStatus ()->nombre ,
632- 'url ' => Tools::siteUrl () . '/EditServicioAT?code= ' . $ this ->idservicio
633- ]);
634- }
635644}
0 commit comments