From 7b9efc2c3c4c3ee1a1cb9289043bad8f5436cb90 Mon Sep 17 00:00:00 2001 From: Carlos Garcia Gomez Date: Mon, 25 Jul 2022 10:31:08 +0200 Subject: [PATCH] =?UTF-8?q?Ahora=20cuando=20hacemos=20una=20venta=20a=20un?= =?UTF-8?q?=20cliente=20cuya=20direcci=C3=B3n=20de=20facturaci=C3=B3n=20es?= =?UTF-8?q?t=C3=A1=20vac=C3=ADa:=20-=20Podemos=20escribir=20la=20direcci?= =?UTF-8?q?=C3=B3n,=20c=C3=B3digo=20postal,=20etc=20desde=20el=20formulari?= =?UTF-8?q?o=20de=20venta.=20-=20Al=20guardar=20el=20documento=20(insert)?= =?UTF-8?q?=20se=20actualiza=20la=20direcci=C3=B3n=20de=20facturaci=C3=B3n?= =?UTF-8?q?=20del=20cliente=20con=20los=20datos=20introducidos=20en=20la?= =?UTF-8?q?=20venta.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Core/Base/AjaxForms/SalesHeaderHTML.php | 29 ++++++++++++++++------- Core/Model/Base/SalesDocument.php | 31 +++++++++++++++++++------ 2 files changed, 45 insertions(+), 15 deletions(-) diff --git a/Core/Base/AjaxForms/SalesHeaderHTML.php b/Core/Base/AjaxForms/SalesHeaderHTML.php index 218e32f8a1..c2656f0128 100644 --- a/Core/Base/AjaxForms/SalesHeaderHTML.php +++ b/Core/Base/AjaxForms/SalesHeaderHTML.php @@ -113,12 +113,23 @@ public static function apply(SalesDocument &$model, array $formData, User $user) } elseif ($dir->loadFromCode($formData['idcontactofact'])) { // update billing address $model->idcontactofact = $dir->idcontacto; - $model->direccion = $dir->direccion; - $model->apartado = $dir->apartado; - $model->codpostal = $dir->codpostal; - $model->ciudad = $dir->ciudad; - $model->provincia = $dir->provincia; - $model->codpais = $dir->codpais; + + // Is Billing address empty? + if (empty($dir->direccion)) { + $model->direccion = $formData['direccion'] ?? $model->direccion; + $model->apartado = $formData['apartado'] ?? $model->apartado; + $model->codpostal = $formData['codpostal'] ?? $model->codpostal; + $model->ciudad = $formData['ciudad'] ?? $model->ciudad; + $model->provincia = $formData['provincia'] ?? $model->provincia; + $model->codpais = $formData['codpais'] ?? $model->codpais; + } else { + $model->direccion = $dir->direccion; + $model->apartado = $dir->apartado; + $model->codpostal = $dir->codpostal; + $model->ciudad = $dir->ciudad; + $model->provincia = $dir->provincia; + $model->codpais = $dir->codpais; + } } // set shipping address @@ -165,7 +176,7 @@ public static function render(SalesDocument $model): string private static function addressField(Translator $i18n, SalesDocument $model, string $field, string $label, int $size, int $maxlength): string { - $attributes = $model->editable && empty($model->idcontactofact) ? + $attributes = $model->editable && (empty($model->idcontactofact) || empty($model->direccion)) ? 'name="' . $field . '" maxlength="' . $maxlength . '" autocomplete="off"' : 'disabled=""'; return '
' @@ -256,7 +267,9 @@ private static function codpais(Translator $i18n, SalesDocument $model): string } $pais = new Pais(); - $attributes = $model->editable && empty($model->idcontactofact) ? 'name="codpais"' : 'disabled=""'; + $attributes = $model->editable && (empty($model->idcontactofact) || empty($model->direccion)) ? + 'name="codpais"' : + 'disabled=""'; return '
' . '
' . '' . $i18n->trans('country') . '' diff --git a/Core/Model/Base/SalesDocument.php b/Core/Model/Base/SalesDocument.php index e3824f3e5d..99d7b24947 100644 --- a/Core/Model/Base/SalesDocument.php +++ b/Core/Model/Base/SalesDocument.php @@ -384,12 +384,16 @@ protected function onChange($field) return false; } + if (empty($this->idcontactofact)) { + return true; + } + // after parent checks + $contact = new Contacto(); switch ($field) { case 'direccion': - $contact = new Contacto(); // if address is changed and customer billing address is empty, then save new values - if ($contact->loadFromCode($this->idcontactofact) && empty($contact->direccion)) { + if ($this->direccion && $contact->loadFromCode($this->idcontactofact) && empty($contact->direccion)) { $contact->apartado = $this->apartado; $contact->ciudad = $this->ciudad; $contact->codpais = $this->codpais; @@ -401,11 +405,7 @@ protected function onChange($field) break; case 'idcontactofact': - if (empty($this->idcontactofact)) { - return true; - } // if billing address is changed, then change all billing fields - $contact = new Contacto(); if ($contact->loadFromCode($this->idcontactofact)) { $this->apartado = $contact->apartado; $this->ciudad = $contact->ciudad; @@ -413,7 +413,7 @@ protected function onChange($field) $this->codpostal = $contact->codpostal; $this->direccion = $contact->direccion; $this->provincia = $contact->provincia; - return true; + break; } return false; } @@ -421,6 +421,23 @@ protected function onChange($field) return true; } + protected function onInsert() + { + // if billing address is empty, then save new values + $contact = new Contacto(); + if ($this->direccion && $contact->loadFromCode($this->idcontactofact) && empty($contact->direccion)) { + $contact->apartado = $this->apartado; + $contact->ciudad = $this->ciudad; + $contact->codpais = $this->codpais; + $contact->codpostal = $this->codpostal; + $contact->direccion = $this->direccion; + $contact->provincia = $this->provincia; + $contact->save(); + } + + parent::onInsert(); + } + /** * This method is called after a record is updated on the database (saveUpdate). */