Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
213 changes: 111 additions & 102 deletions Model/ServicioAT.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* This file is part of Servicios plugin for FacturaScripts
* Copyright (C) 2020-2024 Carlos Garcia Gomez <carlos@facturascripts.com>
* Copyright (C) 2020-2026 Carlos Garcia Gomez <carlos@facturascripts.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
Expand Down Expand Up @@ -43,8 +43,8 @@
*/
class ServicioAT extends ModelClass
{
use ModelTrait;
use CompanyRelationTrait;
use ModelTrait;

/** @var string */
public $asignado;
Expand Down Expand Up @@ -88,24 +88,24 @@ class ServicioAT extends ModelClass
/** @var int */
public $idmaquina4;

/** @var int */
public $idtipo;

/** @var int */
public $idprioridad;

/** @var int */
public $idservicio;

/** @var string */
public $material;
/** @var int */
public $idtipo;

/** @var string */
public $nick;
public $material;

/** @var double */
public $neto;

/** @var string */
public $nick;

/** @var string */
public $observaciones;

Expand Down Expand Up @@ -180,7 +180,9 @@ public function delete(): bool

// añadimos el cambio al log
$messageLog = Tools::trans('deleted-service');
$this->log($messageLog);
if (false === $this->log($messageLog)) {
Tools::log()->error('record-save-error', ['%modelo%' => 'ServicioATLog']);
}

return true;
}
Expand Down Expand Up @@ -208,19 +210,19 @@ public function getAvailablePriority(): array
}

/**
* @return TipoAT[]
* @return EstadoAT[]
*/
public function getAvailableTypes(): array
public function getAvailableStatus(): array
{
return TipoAT::all();
return EstadoAT::all();
}

/**
* @return EstadoAT[]
* @return TipoAT[]
*/
public function getAvailableStatus(): array
public function getAvailableTypes(): array
{
return EstadoAT::all();
return TipoAT::all();
}

public function getCustomer(?string $codcliente = null): Cliente
Expand Down Expand Up @@ -251,26 +253,19 @@ public function getMachines(): array
return $result;
}

public function getStatus(?int $idestado = null): EstadoAT
{
$idestado = $idestado ?? $this->idestado;
$status = new EstadoAT();
$status->load($idestado);
return $status;
}

public function getPriority(): PrioridadAT
{
$priority = new PrioridadAT();
$priority->load($this->idprioridad);
return $priority;
}

public function getType(): TipoAT
public function getStatus(?int $idestado = null): EstadoAT
{
$type = new TipoAT();
$type->load($this->idtipo);
return $type;
$idestado = $idestado ?? $this->idestado;
$status = new EstadoAT();
$status->load($idestado);
return $status;
}

public function getSubject(): Cliente
Expand All @@ -290,6 +285,13 @@ public function getTrabajos(): array
return DinTrabajoAT::all($where, $order);
}

public function getType(): TipoAT
{
$type = new TipoAT();
$type->load($this->idtipo);
return $type;
}

public function getUser(?string $nick = null): User
{
$nick = is_null($nick) ? $this->nick : $nick;
Expand Down Expand Up @@ -393,6 +395,78 @@ public function url(string $type = 'auto', string $list = 'List'): string
return $type === 'new' ? 'NewServicioAT' : parent::url($type, $list);
}

protected function notifyAgent(string $notification): void
{
$agent = new Agente();
if (false === $agent->load($this->codagente) || empty($agent->email)) {
return;
}

MailNotifier::send($notification, $agent->email, $agent->nombre, [
'number' => $this->idservicio,
'code' => $this->codigo,
'date' => $this->fecha,
'customer' => $this->getSubject()->nombre,
'author' => $this->nick,
'status' => $this->getStatus()->nombre,
'url' => Tools::siteUrl() . '/EditServicioAT?code=' . $this->idservicio
]);
}

protected function notifyAssignedUser(string $notification): void
{
$assigned = new User();
if (false === $assigned->load($this->asignado)) {
return;
}

MailNotifier::send($notification, $assigned->email, $assigned->nick, [
'number' => $this->idservicio,
'code' => $this->codigo,
'date' => $this->fecha,
'customer' => $this->getSubject()->nombre,
'author' => $this->nick,
'status' => $this->getStatus()->nombre,
'url' => Tools::siteUrl() . '/EditServicioAT?code=' . $this->idservicio
]);
}

protected function notifyCustomer(string $notification): void
{
$customer = $this->getSubject();
if (empty($customer) || empty($customer->email)) {
return;
}

MailNotifier::send($notification, $customer->email, $customer->nombre, [
'number' => $this->idservicio,
'code' => $this->codigo,
'date' => $this->fecha,
'customer' => $customer->nombre,
'author' => $this->nick,
'status' => $this->getStatus()->nombre,
'url' => Tools::siteUrl() . '/EditServicioAT?code=' . $this->idservicio
]);
}

protected function notifyUser(string $notification): void
{
$user = new User();
if (false === $user->load($this->nick)) {
return;
}

MailNotifier::send($notification, $user->email, $user->nick, [
'number' => $this->idservicio,
'code' => $this->codigo,
'date' => $this->fecha,
'customer' => $this->getSubject()->nombre,
'author' => $this->nick,
'status' => $this->getStatus()->nombre,
'url' => Tools::siteUrl() . '/EditServicioAT?code=' . $this->idservicio
]);
}

protected function onChange(string $field): bool
{
if ($field == 'idestado') {
Expand All @@ -411,7 +485,9 @@ protected function onChange(string $field): bool
'%oldStatus%' => $this->getStatus($this->getOriginal('idestado'))->nombre,
'%newStatus%' => $newStatus->nombre
]);
$this->log($messageLog);
if (false === $this->log($messageLog)) {
return false;
}
}

return parent::onChange($field);
Expand All @@ -430,8 +506,13 @@ protected function onInsert(): void
$this->notifyCustomer('new-service-customer');
}

$message = Tools::trans('new-service-created', ['%number%' => $this->id()]);
$this->log($message);
$message = Tools::trans('new-service-created', [
'%number%' => $this->id(),
'%status%' => $this->getStatus()->nombre
]);
if (false === $this->log($message)) {
Tools::log()->error('record-save-error', ['%modelo%' => 'ServicioATLog']);
}

parent::onInsert();
}
Expand Down Expand Up @@ -560,76 +641,4 @@ protected function onUpdateUser(): void
$this->notifyUser('new-service-user');
}
}

protected function notifyAgent(string $notification): void
{
$agent = new Agente();
if (false === $agent->load($this->codagente) || empty($agent->email)) {
return;
}

MailNotifier::send($notification, $agent->email, $agent->nombre, [
'number' => $this->idservicio,
'code' => $this->codigo,
'date' => $this->fecha,
'customer' => $this->getSubject()->nombre,
'author' => $this->nick,
'status' => $this->getStatus()->nombre,
'url' => Tools::siteUrl() . '/EditServicioAT?code=' . $this->idservicio
]);
}

protected function notifyAssignedUser(string $notification): void
{
$assigned = new User();
if (false === $assigned->load($this->asignado)) {
return;
}

MailNotifier::send($notification, $assigned->email, $assigned->nick, [
'number' => $this->idservicio,
'code' => $this->codigo,
'date' => $this->fecha,
'customer' => $this->getSubject()->nombre,
'author' => $this->nick,
'status' => $this->getStatus()->nombre,
'url' => Tools::siteUrl() . '/EditServicioAT?code=' . $this->idservicio
]);
}

protected function notifyCustomer(string $notification): void
{
$customer = $this->getSubject();
if (empty($customer) || empty($customer->email)) {
return;
}

MailNotifier::send($notification, $customer->email, $customer->nombre, [
'number' => $this->idservicio,
'code' => $this->codigo,
'date' => $this->fecha,
'customer' => $customer->nombre,
'author' => $this->nick,
'status' => $this->getStatus()->nombre,
'url' => Tools::siteUrl() . '/EditServicioAT?code=' . $this->idservicio
]);
}

protected function notifyUser(string $notification): void
{
$user = new User();
if (false === $user->load($this->nick)) {
return;
}

MailNotifier::send($notification, $user->email, $user->nick, [
'number' => $this->idservicio,
'code' => $this->codigo,
'date' => $this->fecha,
'customer' => $this->getSubject()->nombre,
'author' => $this->nick,
'status' => $this->getStatus()->nombre,
'url' => Tools::siteUrl() . '/EditServicioAT?code=' . $this->idservicio
]);
}
}
2 changes: 1 addition & 1 deletion Translation/es_ES.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"new-service-agent-body": "Hola {name}, {author} te ha asignado el servicio {number} para el cliente {customer}. Puedes ver más detalles desde aquí: {url}",
"new-service-assignee": "Servicio {number} asignado",
"new-service-assignee-body": "Hola {name}, {author} te ha asignado el servicio {number} para el cliente {customer}. Puedes ver más detalles desde aquí: {url}",
"new-service-created": "Servicio creado",
"new-service-created": "Servicio creado con el estado %status%",
"new-service-customer": "Servicio {number} registrado",
"new-service-customer-body": "Hola {name}, hemos registrado el servicio {number} para atenderte.",
"new-service-p": "Para crear un servicio primero debes seleccionar o crear un cliente y después una máquina (opcional)",
Expand Down