diff --git a/Controller/NewServicioAT.php b/Controller/NewServicioAT.php
index d18bc18..5883019 100644
--- a/Controller/NewServicioAT.php
+++ b/Controller/NewServicioAT.php
@@ -29,6 +29,7 @@
use FacturaScripts\Dinamic\Model\MaquinaAT;
use FacturaScripts\Dinamic\Model\RoleAccess;
use FacturaScripts\Dinamic\Model\ServicioAT;
+use Throwable;
/**
* Description of NewServicioAT
@@ -123,10 +124,6 @@ public function privateCore(&$response, $user, $permissions)
}
switch ($action) {
- case 'checkDuplicateCustomer':
- $data = $this->checkDuplicateCustomerAction();
- break;
-
case 'findCustomer':
$data = $this->findCustomerAction();
break;
@@ -164,34 +161,6 @@ public function privateCore(&$response, $user, $permissions)
}
}
- protected function checkDuplicateCustomerAction(): array
- {
- $wheres = [];
- $name = $this->request->get('name', '');
- $cifnif = $this->request->get('cifnif', '');
-
- if (false === empty($name)) {
- $wheres[] = 'LOWER(nombre) = ' . $this->dataBase->var2str(strtolower($name));
- $wheres[] = 'LOWER(razonsocial) = ' . $this->dataBase->var2str(strtolower($name));
- }
-
- if (false === empty($cifnif)) {
- $wheres[] = 'LOWER(cifnif) = ' . $this->dataBase->var2str(strtolower($cifnif));
- }
-
- if (empty($wheres)) {
- return ['checkDuplicateCustomer' => false];
- }
-
- $sql = 'SELECT codcliente'
- . ' FROM clientes'
- . ' WHERE ' . implode(' OR ', $wheres);
-
- return count($this->dataBase->select($sql)) > 0
- ? ['checkDuplicateCustomer' => true]
- : ['checkDuplicateCustomer' => false];
- }
-
protected function checkMachine(): bool
{
if (empty($this->idmaquina)) {
@@ -311,10 +280,33 @@ protected function saveNewCustomerAction(): array
return ['saveNewCustomer' => false];
}
+ $name = trim($this->request->get('name', ''));
+ if ($name === '') {
+ Tools::log()->warning('invalid-request');
+ return ['saveNewCustomer' => false];
+ }
+
+ // si el cifnif ya existe en otro cliente avisamos, pero permitimos crearlo igualmente
+ $cifnif = trim($this->request->get('cifnif', ''));
+ $confirmed = $this->request->get('cifnif_confirmed', '0') === '1';
+ if ($cifnif !== '' && false === $confirmed) {
+ $duplicated = $this->findCustomersByCifnif($cifnif);
+ if (false === empty($duplicated)) {
+ return [
+ 'saveNewCustomer' => false,
+ 'duplicatedCifnif' => true,
+ 'duplicatedCifnifMessage' => Tools::trans('duplicated-cifnif-customer', [
+ '%cifnif%' => $cifnif,
+ '%customers%' => implode(', ', $duplicated)
+ ]),
+ ];
+ }
+ }
+
// creamos el cliente
$customer = new Cliente();
- $customer->nombre = $this->request->get('name');
- $customer->cifnif = $this->request->get('cifnif', '');
+ $customer->nombre = $name;
+ $customer->cifnif = $cifnif;
$customer->email = $this->request->get('email');
$customer->telefono1 = $this->request->get('phone1');
$customer->telefono2 = $this->request->get('phone2');
@@ -324,20 +316,34 @@ protected function saveNewCustomerAction(): array
$customer = $resultExtension;
}
- if (false === $customer->save()) {
- Tools::log()->error('save-error');
- return ['saveNewCustomer' => false];
- }
+ $this->dataBase->beginTransaction();
+ try {
+ if (false === $customer->save()) {
+ $this->dataBase->rollback();
+ Tools::log()->error('save-error');
+ return ['saveNewCustomer' => false];
+ }
- // modificamos la dirección
- foreach ($customer->getAddresses() as $address) {
- $address->direccion = $this->request->get('address');
- $address->codpostal = $this->request->get('zip');
- $address->ciudad = $this->request->get('city');
- $address->provincia = $this->request->get('province');
- $address->codpais = $this->request->get('country');
- $address->save();
- break;
+ // modificamos la dirección
+ foreach ($customer->getAddresses() as $address) {
+ $address->direccion = $this->request->get('address');
+ $address->codpostal = $this->request->get('zip');
+ $address->ciudad = $this->request->get('city');
+ $address->provincia = $this->request->get('province');
+ $address->codpais = $this->request->get('country');
+ if (false === $address->save()) {
+ $this->dataBase->rollback();
+ Tools::log()->error('save-error');
+ return ['saveNewCustomer' => false];
+ }
+ break;
+ }
+
+ $this->dataBase->commit();
+ } catch (Throwable $e) {
+ $this->dataBase->rollback();
+ Tools::log()->error($e->getMessage());
+ return ['saveNewCustomer' => false];
}
return [
@@ -346,6 +352,18 @@ protected function saveNewCustomerAction(): array
];
}
+ /** Devuelve los clientes que ya tienen este cifnif, como 'código - nombre'. */
+ private function findCustomersByCifnif(string $cifnif): array
+ {
+ $names = [];
+ $where = [Where::eq('cifnif', $cifnif)];
+ foreach (Cliente::all($where, ['LOWER(nombre)' => 'ASC'], 0, 5) as $customer) {
+ $names[] = $customer->codcliente . ' - ' . $customer->nombre;
+ }
+
+ return $names;
+ }
+
protected function saveNewMachineAction(): array
{
if (false === $this->user->can('EditMaquinaAT', 'update')) {
diff --git a/Test/main/NewServicioAtTest.php b/Test/main/NewServicioAtTest.php
new file mode 100644
index 0000000..4c7f337
--- /dev/null
+++ b/Test/main/NewServicioAtTest.php
@@ -0,0 +1,173 @@
+
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program. If not, see