]> _ Git - cubedesigners_userdatabase.git/commitdiff
wait #6199 @0.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 22 Aug 2023 16:07:11 +0000 (18:07 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 22 Aug 2023 16:07:11 +0000 (18:07 +0200)
src/app/Models/Company.php
src/app/SubForms/Address.php
src/app/SubForms/AddressAndCompanyName.php [new file with mode: 0644]

index 0e4decdd9bd40b1a72ba81230ccc5a1d1c7568d8..eaab74107634ce878b25a637e3904ffda2059cc5 100644 (file)
@@ -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);
+    }
 }
index 61601bb7e93bceb10fc815797bfe64a51953cca9..3d3df01fb6ab655c060f9a34806bc70b5eec39ec 100644 (file)
@@ -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 (file)
index 0000000..9826bb4
--- /dev/null
@@ -0,0 +1,14 @@
+<?php
+
+namespace Cubedesigners\UserDatabase\SubForms;
+
+use Cubist\Backpack\Magic\Fields\Text;
+
+class AddressAndCompanyName extends Address
+{
+    public function init()
+    {
+        $this->addField('name', Text::class, __('Nom de l\'entreprise'));
+        parent::init();
+    }
+}