diff --git a/app/Http/Controllers/OpenAPI/VendorSchema.php b/app/Http/Controllers/OpenAPI/VendorSchema.php index 8cf71c4ca9ea..a50df21d6f07 100644 --- a/app/Http/Controllers/OpenAPI/VendorSchema.php +++ b/app/Http/Controllers/OpenAPI/VendorSchema.php @@ -26,7 +26,7 @@ * @OA\Property(property="city", type="string", example="", description="________"), * @OA\Property(property="state", type="string", example="", description="________"), * @OA\Property(property="postal_code", type="string", example="", description="________"), - * @OA\Property(property="work_phone", type="string", example="555-3434-3434", description="The client phone number"), + * @OA\Property(property="phone", type="string", example="555-3434-3434", description="The client phone number"), * @OA\Property(property="country_id", type="string", example="", description="________"), * @OA\Property(property="currency_id", type="string", example="4", description="________"), * @OA\Property(property="custom_value1", type="string", example="", description="________"), diff --git a/app/Import/Transformer/Csv/ClientTransformer.php b/app/Import/Transformer/Csv/ClientTransformer.php index a2c4721bb8eb..543ad1cd205a 100644 --- a/app/Import/Transformer/Csv/ClientTransformer.php +++ b/app/Import/Transformer/Csv/ClientTransformer.php @@ -38,7 +38,7 @@ class ClientTransformer extends BaseTransformer return [ 'company_id' => $this->company->id, 'name' => $this->getString($data, 'client.name'), - 'work_phone' => $this->getString($data, 'client.phone'), + 'phone' => $this->getString($data, 'client.phone'), 'address1' => $this->getString($data, 'client.address1'), 'address2' => $this->getString($data, 'client.address2'), 'postal_code' => $this->getString($data, 'client.postal_code'), diff --git a/app/Import/Transformer/Wave/ClientTransformer.php b/app/Import/Transformer/Wave/ClientTransformer.php index c5681e411152..844560f48679 100644 --- a/app/Import/Transformer/Wave/ClientTransformer.php +++ b/app/Import/Transformer/Wave/ClientTransformer.php @@ -42,7 +42,7 @@ class ClientTransformer extends BaseTransformer 'company_id' => $this->company->id, 'name' => $this->getString($data, 'customer_name'), 'number' => $this->getValueOrNull($data, 'account_number'), - 'work_phone' => $this->getString($data, 'phone'), + 'phone' => $this->getString($data, 'phone'), 'website' => $this->getString($data, 'website'), 'country_id' => ! empty($data['country']) ? $this->getCountryId($data['country']) : null, 'state' => $this->getString($data, 'province/state'), diff --git a/app/Import/Transformers/ClientTransformer.php b/app/Import/Transformers/ClientTransformer.php index 1e7ed43ae226..a77af36b5693 100644 --- a/app/Import/Transformers/ClientTransformer.php +++ b/app/Import/Transformers/ClientTransformer.php @@ -35,7 +35,7 @@ class ClientTransformer extends BaseTransformer return [ 'company_id' => $this->maps['company']->id, 'name' => $this->getString($data, 'client.name'), - 'work_phone' => $this->getString($data, 'client.phone'), + 'phone' => $this->getString($data, 'client.phone'), 'address1' => $this->getString($data, 'client.address1'), 'address2' => $this->getString($data, 'client.address2'), 'city' => $this->getString($data, 'client.city'), diff --git a/app/Import/Transformers/Csv/ClientTransformer.php b/app/Import/Transformers/Csv/ClientTransformer.php index 55d0baa8a1fa..44f826e1bbb5 100644 --- a/app/Import/Transformers/Csv/ClientTransformer.php +++ b/app/Import/Transformers/Csv/ClientTransformer.php @@ -37,7 +37,7 @@ class ClientTransformer extends BaseTransformer return [ 'company_id' => $this->maps['company']->id, 'name' => $this->getString($data, 'client.name'), - 'work_phone' => $this->getString($data, 'client.phone'), + 'phone' => $this->getString($data, 'client.phone'), 'address1' => $this->getString($data, 'client.address1'), 'address2' => $this->getString($data, 'client.address2'), 'postal_code' => $this->getString($data, 'client.postal_code'), diff --git a/app/Import/Transformers/Freshbooks/ClientTransformer.php b/app/Import/Transformers/Freshbooks/ClientTransformer.php index bfa8743ed62d..5b82f400c223 100644 --- a/app/Import/Transformers/Freshbooks/ClientTransformer.php +++ b/app/Import/Transformers/Freshbooks/ClientTransformer.php @@ -34,7 +34,7 @@ class ClientTransformer extends BaseTransformer return [ 'company_id' => $this->maps['company']->id, 'name' => $this->getString($data, 'Organization'), - 'work_phone' => $this->getString($data, 'Phone'), + 'phone' => $this->getString($data, 'Phone'), 'address1' => $this->getString($data, 'Street'), 'city' => $this->getString($data, 'City'), 'state' => $this->getString($data, 'Province/State'), diff --git a/app/Import/Transformers/Invoicely/ClientTransformer.php b/app/Import/Transformers/Invoicely/ClientTransformer.php index ddf3645b3610..5e2d58ae4a09 100644 --- a/app/Import/Transformers/Invoicely/ClientTransformer.php +++ b/app/Import/Transformers/Invoicely/ClientTransformer.php @@ -34,7 +34,7 @@ class ClientTransformer extends BaseTransformer return [ 'company_id' => $this->maps['company']->id, 'name' => $this->getString($data, 'Client Name'), - 'work_phone' => $this->getString($data, 'Phone'), + 'phone' => $this->getString($data, 'Phone'), 'country_id' => isset($data['Country']) ? $this->getCountryIdBy2($data['Country']) : null, 'credit_balance' => 0, 'settings' => new \stdClass, diff --git a/app/Import/Transformers/Waveaccounting/ClientTransformer.php b/app/Import/Transformers/Waveaccounting/ClientTransformer.php index a19616f968f1..f9b95a42e487 100644 --- a/app/Import/Transformers/Waveaccounting/ClientTransformer.php +++ b/app/Import/Transformers/Waveaccounting/ClientTransformer.php @@ -42,7 +42,7 @@ class ClientTransformer extends BaseTransformer 'company_id' => $this->maps['company']->id, 'name' => $this->getString($data, 'customer_name'), 'number' => $this->getString($data, 'account_number'), - 'work_phone' => $this->getString($data, 'phone'), + 'phone' => $this->getString($data, 'phone'), 'website' => $this->getString($data, 'website'), 'country_id' => ! empty($data['country']) ? $this->getCountryId($data['country']) : null, 'state' => $this->getString($data, 'province/state'), diff --git a/app/Import/Transformers/Zoho/ClientTransformer.php b/app/Import/Transformers/Zoho/ClientTransformer.php index e8e618d189be..6895fabdaeb2 100644 --- a/app/Import/Transformers/Zoho/ClientTransformer.php +++ b/app/Import/Transformers/Zoho/ClientTransformer.php @@ -41,7 +41,7 @@ class ClientTransformer extends BaseTransformer return [ 'company_id' => $this->maps['company']->id, 'name' => $this->getString($data, 'Company Name'), - 'work_phone' => $this->getString($data, 'Phone'), + 'phone' => $this->getString($data, 'Phone'), 'private_notes' => $this->getString($data, 'Notes'), 'website' => $this->getString($data, 'Website'), 'id_number' => $this->getString($data, 'Customer ID'), diff --git a/app/Models/Presenters/CompanyPresenter.php b/app/Models/Presenters/CompanyPresenter.php index 57408a788590..9d44bee8c60d 100644 --- a/app/Models/Presenters/CompanyPresenter.php +++ b/app/Models/Presenters/CompanyPresenter.php @@ -119,7 +119,7 @@ class CompanyPresenter extends EntityPresenter $str .= e($country->name).'
'; } if ($settings->phone) { - $str .= ctrans('texts.work_phone').': '.e($settings->phone).'
'; + $str .= ctrans('texts.phone').': '.e($settings->phone).'
'; } if ($settings->email) { $str .= ctrans('texts.work_email').': '.e($settings->email).'
'; diff --git a/app/Repositories/ClientRepository.php b/app/Repositories/ClientRepository.php index 23c6ae8cee28..6114916e80ad 100644 --- a/app/Repositories/ClientRepository.php +++ b/app/Repositories/ClientRepository.php @@ -56,6 +56,8 @@ class ClientRepository extends BaseRepository */ public function save(array $data, Client $client) : ?Client { + $contact_data = $data; + unset($data['contacts']); /* When uploading documents, only the document array is sent, so we must return early*/ if (array_key_exists('documents', $data) && count($data['documents']) >= 1) { @@ -67,7 +69,7 @@ class ClientRepository extends BaseRepository $client->fill($data); if (array_key_exists('settings', $data)) { - $client->saveSettings($data['settings'], $client); + $client->settings = $client->saveSettings($data['settings'], $client); } if (! $client->country_id) { @@ -75,19 +77,9 @@ class ClientRepository extends BaseRepository $client->country_id = $company->settings->country_id; } - try{ - $client->save(); - } - catch(\Exception $e) { - - nlog("client save failed"); - nlog($data); - - } + $client->save(); if (! isset($client->number) || empty($client->number) || strlen($client->number) == 0) { - // $client->number = $this->getNextClientNumber($client); - // $client->save(); $x = 1; @@ -111,7 +103,7 @@ class ClientRepository extends BaseRepository $data['name'] = $client->present()->name(); } - $this->contact_repo->save($data, $client); + $this->contact_repo->save($contact_data, $client); return $client; } diff --git a/app/Utils/Traits/ClientGroupSettingsSaver.php b/app/Utils/Traits/ClientGroupSettingsSaver.php index 67c2b6a774b3..270f2e0588cb 100644 --- a/app/Utils/Traits/ClientGroupSettingsSaver.php +++ b/app/Utils/Traits/ClientGroupSettingsSaver.php @@ -30,7 +30,7 @@ trait ClientGroupSettingsSaver * Saves a setting object. * * Works for groups|clients|companies - * @param array $settings The request input settings array + * @param array|object $settings The request input settings array * @param object $entity The entity which the settings belongs to * @return void */ @@ -64,19 +64,6 @@ trait ClientGroupSettingsSaver $entity_settings->{$key} = $value; } - $entity->settings = $entity_settings; - - try{ - $entity->save(); - } - catch(\Exception $e){ - - nlog("client settings failure"); - nlog($entity_settings); - nlog($e->getMessage()); - - } - return $entity_settings; } diff --git a/tests/Feature/ClientApiTest.php b/tests/Feature/ClientApiTest.php index aae531575656..14633a1c655d 100644 --- a/tests/Feature/ClientApiTest.php +++ b/tests/Feature/ClientApiTest.php @@ -11,12 +11,20 @@ namespace Tests\Feature; +use App\DataMapper\ClientSettings; +use App\Factory\ClientFactory; +use App\Http\Requests\Client\StoreClientRequest; +use App\Models\Client; use App\Models\Country; +use App\Repositories\ClientContactRepository; +use App\Repositories\ClientRepository; use App\Utils\Number; +use App\Utils\Traits\ClientGroupSettingsSaver; use App\Utils\Traits\MakesHash; use Illuminate\Database\Eloquent\Model; use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Support\Facades\Session; +use Illuminate\Support\Facades\Validator; use Illuminate\Validation\ValidationException; use Tests\MockAccountData; use Tests\TestCase; @@ -30,6 +38,7 @@ class ClientApiTest extends TestCase use MakesHash; use DatabaseTransactions; use MockAccountData; + use ClientGroupSettingsSaver; protected function setUp() :void { @@ -44,6 +53,304 @@ class ClientApiTest extends TestCase Model::reguard(); } + public function testCsvImportRepositoryPersistance() + { + Client::unguard(); + + $data = [ + 'company_id' => $this->company->id, + 'name' => 'Christian xx', + 'phone' => '', + 'address1' => '', + 'address2' => '', + 'postal_code' => '', + 'city' => '', + 'state' => '', + 'shipping_address1' => '', + 'shipping_address2' => '', + 'shipping_city' => '', + 'shipping_state' => '', + 'shipping_postal_code' => '', + 'public_notes' => '', + 'private_notes' => '', + 'website' => '', + 'vat_number' => '', + 'id_number' => '', + 'custom_value1' => '', + 'custom_value2' => '', + 'custom_value3' => '', + 'custom_value4' => '', + 'balance' => '0', + 'paid_to_date' => '0', + 'credit_balance' => 0, + 'settings' => [ + 'entity' => 'App\\Models\\Client', + 'currency_id' => '3', + ], + 'client_hash' => 'xx', + 'contacts' => + [ + [ + 'first_name' => '', + 'last_name' => '', + 'email' => '', + 'phone' => '', + 'custom_value1' => '', + 'custom_value2' => '', + 'custom_value3' => '', + 'custom_value4' => '', + ] + ], + 'country_id' => NULL, + 'shipping_country_id' => NULL, + 'user_id' => $this->user->id, + ]; + + $repository_name = ClientRepository::class; + $factory_name = ClientFactory::class; + + $repository = app()->make($repository_name); + $repository->import_mode = true; + + $c = $repository->save(array_diff_key($data, ['user_id' => false]), ClientFactory::create($this->company->id, $this->user->id)); + + Client::reguard(); + + nlog($c->toArray()); + + $c->refresh(); + + $this->assertEquals("3", $c->settings->currency_id); + + } + + public function testClientSettingsSave() + { + + $std = new \stdClass; + $std->entity = 'App\\Models\\Client'; + $std->currency_id = 3; + + $this->settings = $this->client->settings; + + $this->saveSettings($std, $this->client); + + $this->assertTrue(true); + + } + + + public function testClientSettingsSave2() + { + + $std = new \stdClass; + $std->entity = 'App\\Models\\Client'; + $std->industry_id = ''; + $std->size_id = ''; + $std->currency_id = 3; + + $this->settings = $this->client->settings; + + $this->saveSettings($std, $this->client); + + $this->assertTrue(true); + + } + + public function testClientStoreValidation() + { + + auth()->login($this->user, false); + auth()->user()->setCompany($this->company); + + $data = array ( + 'company_id' => $this->company->id, + 'name' => 'Christian xx', + 'phone' => '', + 'address1' => '', + 'address2' => '', + 'postal_code' => '', + 'city' => '', + 'state' => '', + 'shipping_address1' => '', + 'shipping_address2' => '', + 'shipping_city' => '', + 'shipping_state' => '', + 'shipping_postal_code' => '', + 'public_notes' => '', + 'private_notes' => '', + 'website' => '', + 'vat_number' => '', + 'id_number' => '', + 'custom_value1' => '', + 'custom_value2' => '', + 'custom_value3' => '', + 'custom_value4' => '', + 'balance' => '0', + 'paid_to_date' => '0', + 'credit_balance' => 0, + 'settings' => + (object) array( + 'entity' => 'App\\Models\\Client', + 'currency_id' => '3', + ), + 'client_hash' => 'xx', + 'contacts' => + array ( + 0 => + array ( + 'first_name' => '', + 'last_name' => '', + 'email' => '', + 'phone' => '', + 'custom_value1' => '', + 'custom_value2' => '', + 'custom_value3' => '', + 'custom_value4' => '', + ), + ), + 'country_id' => NULL, + 'shipping_country_id' => NULL, + 'user_id' => $this->user->id, + ); + + + $request_name = StoreClientRequest::class; + $repository_name = ClientRepository::class; + $factory_name = ClientFactory::class; + + $repository = app()->make($repository_name); + $repository->import_mode = true; + + $_syn_request_class = new $request_name; + $_syn_request_class->setContainer(app()); + $_syn_request_class->initialize($data); + $_syn_request_class->prepareForValidation(); + + $validator = Validator::make($_syn_request_class->all(), $_syn_request_class->rules()); + + $_syn_request_class->setValidator($validator); + + $this->assertFalse($validator->fails()); + + + } + + + + public function testClientImportDataStructure() + { + + + $data = array ( + 'company_id' => $this->company->id, + 'name' => 'Christian xx', + 'phone' => '', + 'address1' => '', + 'address2' => '', + 'postal_code' => '', + 'city' => '', + 'state' => '', + 'shipping_address1' => '', + 'shipping_address2' => '', + 'shipping_city' => '', + 'shipping_state' => '', + 'shipping_postal_code' => '', + 'public_notes' => '', + 'private_notes' => '', + 'website' => '', + 'vat_number' => '', + 'id_number' => '', + 'custom_value1' => '', + 'custom_value2' => '', + 'custom_value3' => '', + 'custom_value4' => '', + 'balance' => '0', + 'paid_to_date' => '0', + 'credit_balance' => 0, + 'settings' => + (object) array( + 'entity' => 'App\\Models\\Client', + 'currency_id' => '3', + ), + 'client_hash' => 'xx', + 'contacts' => + array ( + 0 => + array ( + 'first_name' => '', + 'last_name' => '', + 'email' => '', + 'phone' => '', + 'custom_value1' => '', + 'custom_value2' => '', + 'custom_value3' => '', + 'custom_value4' => '', + ), + ), + 'country_id' => NULL, + 'shipping_country_id' => NULL, + 'user_id' => $this->user->id, + ); + + $crepo = new ClientRepository(new ClientContactRepository()); + + $c = $crepo->save(array_diff_key($data, ['user_id' => false]), ClientFactory::create($this->company->id, $this->user->id)); + $c->saveQuietly(); + + $this->assertEquals('Christian xx', $c->name); + $this->assertEquals('3', $c->settings->currency_id); + } + + public function testClientCsvImport() + { + + $settings = ClientSettings::defaults(); + $settings->currency_id = "840"; + + $data = [ + 'name' => $this->faker->firstName(), + 'id_number' => 'Coolio', + 'settings' => (array)$settings, + 'contacts' => [ + [ + 'first_name' => '', + 'last_name' => '', + 'email' => '', + 'phone' => '', + 'custom_value1' => '', + 'custom_value2' => '', + 'custom_value3' => '', + 'custom_value4' => '', + ] + ] + ]; + + $response = false; + + try { + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->post('/api/v1/clients/', $data); + } catch (ValidationException $e) { + $message = json_decode($e->validator->getMessageBag(), 1); + nlog($message); + } + + $response->assertStatus(200); + + $crepo = new ClientRepository(new ClientContactRepository()); + + $c = $crepo->save($data, ClientFactory::create($this->company->id, $this->user->id)); + $c->saveQuietly(); + + + } + + + + public function testIllegalPropertiesInClientSettings() { $settings = [