From ec69d162b070ff02415c211f84105341b60a40a9 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Tue, 22 Aug 2023 18:07:11 +0200 Subject: [PATCH] wait #6199 @0.5 --- src/app/Models/Company.php | 27 +++++++++++++++++++--- src/app/SubForms/Address.php | 4 +++- src/app/SubForms/AddressAndCompanyName.php | 14 +++++++++++ 3 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 src/app/SubForms/AddressAndCompanyName.php diff --git a/src/app/Models/Company.php b/src/app/Models/Company.php index 0e4decd..eaab741 100644 --- a/src/app/Models/Company.php +++ b/src/app/Models/Company.php @@ -6,11 +6,13 @@ use Cubedesigners\UserDatabase\Fields\Users; use Cubedesigners\UserDatabase\Jobs\ApplyPermissionsToUsers; use Cubedesigners\UserDatabase\Permissions; use Cubedesigners\UserDatabase\SubForms\Address; +use Cubedesigners\UserDatabase\SubForms\AddressAndCompanyName; use Cubist\Backpack\Magic\Fields\Checkbox; +use Cubist\Backpack\Magic\Fields\Hidden; use Cubist\Backpack\Magic\Fields\Integer; -use Cubist\Backpack\Magic\Fields\Range; use Cubist\Backpack\Magic\Fields\Text; use Cubist\Backpack\Magic\Models\CubistMagicAbstractModel; +use Cubist\Locale\Country; use Illuminate\Database\Eloquent\Builder; class Company extends CubistMagicAbstractModel @@ -113,10 +115,12 @@ class Company extends CubistMagicAbstractModel $this->addField(['name' => 'billing_address', 'type' => 'BunchOfFields', - 'bunch' => Address::class, + 'bunch' => AddressAndCompanyName::class, 'label' => __('Adresse de facturation'), + 'hint' => __('Ne remplir que les champs qui diffèrent de l\'adresse indiquée ci-dessus'), 'tab' => __('Adresses')]); + $this->addField('c_billing_address', Hidden::class); $this->addField([ 'name' => 'users', @@ -151,7 +155,6 @@ class Company extends CubistMagicAbstractModel 'read_only' => true]); } - $this->addField(['name' => 'c_unpaid', 'label' => __('Impayés'), 'type' => Integer::class, @@ -229,10 +232,28 @@ class Company extends CubistMagicAbstractModel return $res; } + public function onSaving(): bool + { + $this->setComposedAttributes(); + return parent::onSaving(); + } + public function onSaved(): bool { ApplyPermissionsToUsers::dispatch(); return parent::onSaved(); } + + protected function setComposedAttributes() + { + $billingAddress = ($this->billing_address['name'] ?: $this->name) . "\n"; + $billingAddress .= ($this->billing_address['address'] ?: $this->address['address']) . "\n"; + $billingAddress .= ($this->billing_address['postcode'] ?: $this->address['postcode']) . ' ' . ($this->billing_address['city'] ?: $this->address['city']) . "\n"; + $country = ($this->billing_address['country'] ?: $this->address['country']); + if ($country !== 'FR') { + $billingAddress .= Country::translate($country, 'en'); + } + $this->c_billing_address = trim($billingAddress); + } } diff --git a/src/app/SubForms/Address.php b/src/app/SubForms/Address.php index 61601bb..3d3df01 100644 --- a/src/app/SubForms/Address.php +++ b/src/app/SubForms/Address.php @@ -24,6 +24,8 @@ class Address extends SubForm $this->addField(['name' => 'country', 'label' => __('Pays'), - 'type' => 'Country']); + 'type' => 'Country', + 'allows_null' => true, + 'default' => null]); } } diff --git a/src/app/SubForms/AddressAndCompanyName.php b/src/app/SubForms/AddressAndCompanyName.php new file mode 100644 index 0000000..9826bb4 --- /dev/null +++ b/src/app/SubForms/AddressAndCompanyName.php @@ -0,0 +1,14 @@ +addField('name', Text::class, __('Nom de l\'entreprise')); + parent::init(); + } +} -- 2.39.5