Skip to content

Commit

Permalink
Ahora cuando hacemos una venta a un cliente cuya dirección de factura…
Browse files Browse the repository at this point in the history
…ción está vacía:

- Podemos escribir la dirección, código postal, etc desde el formulario de venta.
- Al guardar el documento (insert) se actualiza la dirección de facturación del cliente con los datos introducidos en la venta.
  • Loading branch information
NeoRazorX committed Jul 25, 2022
1 parent 3792dd1 commit 7b9efc2
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 15 deletions.
29 changes: 21 additions & 8 deletions Core/Base/AjaxForms/SalesHeaderHTML.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 '<div class="col-sm-' . $size . '">'
Expand Down Expand Up @@ -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 '<div class="col-sm-6">'
. '<div class="form-group">'
. '<a href="' . $pais->url() . '">' . $i18n->trans('country') . '</a>'
Expand Down
31 changes: 24 additions & 7 deletions Core/Model/Base/SalesDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -401,26 +405,39 @@ 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;
$this->codpais = $contact->codpais;
$this->codpostal = $contact->codpostal;
$this->direccion = $contact->direccion;
$this->provincia = $contact->provincia;
return true;
break;
}
return false;
}

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).
*/
Expand Down

0 comments on commit 7b9efc2

Please sign in to comment.