diff --git a/app/Http/Controllers/ClientController.php b/app/Http/Controllers/ClientController.php index de1f042a5d6e..902d6e2f9910 100644 --- a/app/Http/Controllers/ClientController.php +++ b/app/Http/Controllers/ClientController.php @@ -102,7 +102,8 @@ class ClientController extends BaseController */ public function update(UpdateClientRequest $request, Client $client) { - $client = $this->client_repo->save($request, $client); + + $client = $this->client_repo->save($request->all(), $client); return $this->itemResponse($client); @@ -115,6 +116,7 @@ class ClientController extends BaseController */ public function create(CreateClientRequest $request) { + $client = ClientFactory::create(auth()->user()->company()->id, auth()->user()->id); return $this->itemResponse($client); @@ -130,7 +132,7 @@ class ClientController extends BaseController public function store(StoreClientRequest $request) { - $client = $this->client_repo->save($request, ClientFactory::create(auth()->user()->company()->id, auth()->user()->id)); + $client = $this->client_repo->save($request->all(), ClientFactory::create(auth()->user()->company()->id, auth()->user()->id)); $client->load('contacts', 'primary_contact'); diff --git a/app/Http/Requests/Client/StoreClientRequest.php b/app/Http/Requests/Client/StoreClientRequest.php index bf4e2da69663..840f77f51fa8 100644 --- a/app/Http/Requests/Client/StoreClientRequest.php +++ b/app/Http/Requests/Client/StoreClientRequest.php @@ -4,9 +4,11 @@ namespace App\Http\Requests\Client; use App\Http\Requests\Request; use App\Models\Client; +use App\Utils\Traits\GeneratesNumberCounter; class StoreClientRequest extends Request { + use GeneratesNumberCounter; /** * Determine if the user is authorized to make this request. * @@ -20,8 +22,11 @@ class StoreClientRequest extends Request public function rules() { +// $this->sanitize(); + /* Ensure we have a client name, and that all emails are unique*/ $rules['name'] = 'required'; + $rules['id_number'] = 'unique:clients,id_number,,id,company_id,' . auth()->user()->company()->id; $contacts = request('contacts'); @@ -39,6 +44,15 @@ class StoreClientRequest extends Request } + + public function sanitize() + { + $input = $this->all(); + + $this->replace($input); + + } + public function messages() { return [ diff --git a/app/Http/Requests/Invoice/StoreInvoiceRequest.php b/app/Http/Requests/Invoice/StoreInvoiceRequest.php index b76b33729c87..8d5612e6bb89 100644 --- a/app/Http/Requests/Invoice/StoreInvoiceRequest.php +++ b/app/Http/Requests/Invoice/StoreInvoiceRequest.php @@ -37,7 +37,7 @@ public function sanitize() /** If we have an email address instead of a client_id - harvest the client_id here */ if(isset($input['email']) && !$input['client_id']) { - $contact = ClientContact::company()->whereEmail($input['email'])->first(); + $contact = ClientContact::company(auth()->user()->company()->id)->whereEmail($input['email'])->first(); if($contact) $input['client_id'] = $contact->client_id; diff --git a/app/Jobs/Client/StoreClient.php b/app/Jobs/Client/StoreClient.php index 21c059e57abd..ce0129e51309 100644 --- a/app/Jobs/Client/StoreClient.php +++ b/app/Jobs/Client/StoreClient.php @@ -14,7 +14,7 @@ class StoreClient { use Dispatchable; - protected $request; + protected $data; protected $client; @@ -24,9 +24,10 @@ class StoreClient * @return void */ - public function __construct(Request $request, Client $client) + public function __construct(array $data, Client $client) { - $this->request = $request; + $this->data = $data; + $this->client = $client; } @@ -35,11 +36,11 @@ class StoreClient * * @return void */ - public function handle(ClientRepository $clientRepo, ClientContactRepository $clientContactRepo) : ?Client - { - $client = $clientRepo->save($this->request, $this->client); + public function handle(ClientRepository $client_repo, ClientContactRepository $client_contact_repo) : ?Client + + $client = $client_repo->save($this->data, $this->client); - $contacts = $clientContactRepo->save($this->request->input('contacts'), $client); + $contacts = $client_contact_repo->save($data['contacts']), $client); return $client; } diff --git a/app/Jobs/Client/UpdateClient.php b/app/Jobs/Client/UpdateClient.php index 7e9db0c248b1..52829f96a596 100644 --- a/app/Jobs/Client/UpdateClient.php +++ b/app/Jobs/Client/UpdateClient.php @@ -15,7 +15,7 @@ class UpdateClient { use Dispatchable; - protected $request; + protected $data; protected $client; @@ -25,9 +25,9 @@ class UpdateClient * @return void */ - public function __construct(Request $request, Client $client) + public function __construct(array $data, Client $client) { - $this->request = $request; + $this->data = $data; $this->client = $client; } @@ -36,11 +36,11 @@ class UpdateClient * * @return void */ - public function handle(ClientRepository $clientRepo, ClientContactRepository $clientContactRepo) :?Client + public function handle(ClientRepository $client_repo, ClientContactRepository $client_contact_repo) :?Client { - $client = $clientRepo->save($this->request, $this->client); + $client = $client_repo->save($this->data, $this->client); - $contacts = $clientContactRepo->save($this->request->input('contacts'), $client); + $contacts = $client_contact_repo->save($data['contacts']), $client); return $client->fresh(); } diff --git a/app/Models/BaseModel.php b/app/Models/BaseModel.php index 5a647acf5f1f..56f74b7671c0 100644 --- a/app/Models/BaseModel.php +++ b/app/Models/BaseModel.php @@ -36,6 +36,7 @@ class BaseModel extends Model public function scopeCompany($query, $company_id) { $query->where('company_id', $company_id); + return $query; } diff --git a/app/Repositories/ClientContactRepository.php b/app/Repositories/ClientContactRepository.php index f14d775ac229..fb83ef932521 100644 --- a/app/Repositories/ClientContactRepository.php +++ b/app/Repositories/ClientContactRepository.php @@ -6,7 +6,7 @@ use App\Models\Client; use App\Models\ClientContact; /** - * + * ClientContactRepository */ class ClientContactRepository extends BaseRepository { @@ -19,15 +19,19 @@ class ClientContactRepository extends BaseRepository /* Get array of IDs which have been removed from the contacts array and soft delete each contact */ collect($client->contacts->pluck('id'))->diff($contacts->pluck('id'))->each(function($contact){ + ClientContact::destroy($contact); + }); /* Set first record to primary - always*/ $contacts = $contacts->sortBy('is_primary'); $contacts->first(function($contact){ + $contact['is_primary'] = true; $contact->save(); + }); //loop and update/create contacts @@ -43,13 +47,11 @@ class ClientContactRepository extends BaseRepository ); $update_contact->fill($contact); + $update_contact->save(); }); } - - - } \ No newline at end of file diff --git a/app/Repositories/ClientRepository.php b/app/Repositories/ClientRepository.php index b986b88bb845..b52f486f1d1c 100644 --- a/app/Repositories/ClientRepository.php +++ b/app/Repositories/ClientRepository.php @@ -5,10 +5,9 @@ namespace App\Repositories; use App\Models\Client; use App\Repositories\ClientContactRepository; use Illuminate\Http\Request; -use Illuminate\Support\Facades\Log; /** - * + * ClientRepository */ class ClientRepository extends BaseRepository { @@ -16,32 +15,55 @@ class ClientRepository extends BaseRepository /** * @var ClientContactRepository */ - protected $contactRepo; + protected $contact_repo; /** * ClientController constructor. - * @param ClientContactRepository $contactRepo + * @param ClientContactRepository $contact_repo */ - public function __construct(ClientContactRepository $contactRepo) + public function __construct(ClientContactRepository $contact_repo) { - $this->contactRepo = $contactRepo; + $this->contact_repo = $contact_repo; } + /** + * Gets the class name. + * + * @return string The class name. + */ public function getClassName() { + return Client::class; + } - - public function save(Request $request, Client $client) : ?Client + + /** + * Saves the client and its contacts + * + * @param array $data The data + * @param \App\Models\Client $client The client + * + * @return Client|\App\Models\Client|null Client Object + */ + public function save(array $data, Client $client) : ?Client { - $client->fill($request->input()); + + $client->fill($data); + $client->save(); - $contacts = $this->contactRepo->save($request->input('contacts'), $client); + $client->id_number = $client->getNextNumber($client); //todo write tests for this and make sure that custom client numbers also works as expected from here + + $client->save(); + + if(isset($data['contacts'])) + $contacts = $this->contact_repo->save($data['contacts'], $client); return $client; + } } \ No newline at end of file diff --git a/app/Repositories/InvoiceRepository.php b/app/Repositories/InvoiceRepository.php index 513b1c3f1f4e..8c0fb36c99d7 100644 --- a/app/Repositories/InvoiceRepository.php +++ b/app/Repositories/InvoiceRepository.php @@ -12,17 +12,15 @@ use Illuminate\Http\Request; class InvoiceRepository extends BaseRepository { - /** * Gets the class name. * - * @return ::class The class name. + * @return string The class name. */ public function getClassName() { return Invoice::class; } - /** * Saves the invoices @@ -49,7 +47,6 @@ class InvoiceRepository extends BaseRepository } - /** * Mark the invoice as sent. * @@ -64,7 +61,7 @@ class InvoiceRepository extends BaseRepository return $invoice; $invoice->status_id = Invoice::STATUS_SENT; - + $invoice->save(); return $invoice; diff --git a/database/seeds/RandomDataSeeder.php b/database/seeds/RandomDataSeeder.php index abf80d84de17..9752f46019d6 100644 --- a/database/seeds/RandomDataSeeder.php +++ b/database/seeds/RandomDataSeeder.php @@ -35,7 +35,7 @@ class RandomDataSeeder extends Seeder $user = factory(\App\Models\User::class)->create([ 'email' => $faker->email, - 'account_id' => $account->id, + // 'account_id' => $account->id, 'confirmation_code' => $this->createDbHash(config('database.default')) ]); @@ -94,6 +94,12 @@ class RandomDataSeeder extends Seeder /** Invoice Factory */ factory(\App\Models\Invoice::class,50)->create(['user_id' => $user->id, 'company_id' => $company->id, 'client_id' => $client->id]); + $clients = Client::all(); + foreach($clients as $client) + { + $client->getNextNumber($client); + $client->save(); + } } }