mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-06-23 02:40:56 -04:00
Working on client saves
This commit is contained in:
parent
4cae3fdcfb
commit
3fde744d77
@ -102,7 +102,8 @@ class ClientController extends BaseController
|
|||||||
*/
|
*/
|
||||||
public function update(UpdateClientRequest $request, Client $client)
|
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);
|
return $this->itemResponse($client);
|
||||||
|
|
||||||
@ -115,6 +116,7 @@ class ClientController extends BaseController
|
|||||||
*/
|
*/
|
||||||
public function create(CreateClientRequest $request)
|
public function create(CreateClientRequest $request)
|
||||||
{
|
{
|
||||||
|
|
||||||
$client = ClientFactory::create(auth()->user()->company()->id, auth()->user()->id);
|
$client = ClientFactory::create(auth()->user()->company()->id, auth()->user()->id);
|
||||||
|
|
||||||
return $this->itemResponse($client);
|
return $this->itemResponse($client);
|
||||||
@ -130,7 +132,7 @@ class ClientController extends BaseController
|
|||||||
public function store(StoreClientRequest $request)
|
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');
|
$client->load('contacts', 'primary_contact');
|
||||||
|
|
||||||
|
@ -4,9 +4,11 @@ namespace App\Http\Requests\Client;
|
|||||||
|
|
||||||
use App\Http\Requests\Request;
|
use App\Http\Requests\Request;
|
||||||
use App\Models\Client;
|
use App\Models\Client;
|
||||||
|
use App\Utils\Traits\GeneratesNumberCounter;
|
||||||
|
|
||||||
class StoreClientRequest extends Request
|
class StoreClientRequest extends Request
|
||||||
{
|
{
|
||||||
|
use GeneratesNumberCounter;
|
||||||
/**
|
/**
|
||||||
* Determine if the user is authorized to make this request.
|
* Determine if the user is authorized to make this request.
|
||||||
*
|
*
|
||||||
@ -20,8 +22,11 @@ class StoreClientRequest extends Request
|
|||||||
|
|
||||||
public function rules()
|
public function rules()
|
||||||
{
|
{
|
||||||
|
// $this->sanitize();
|
||||||
|
|
||||||
/* Ensure we have a client name, and that all emails are unique*/
|
/* Ensure we have a client name, and that all emails are unique*/
|
||||||
$rules['name'] = 'required';
|
$rules['name'] = 'required';
|
||||||
|
$rules['id_number'] = 'unique:clients,id_number,,id,company_id,' . auth()->user()->company()->id;
|
||||||
|
|
||||||
$contacts = request('contacts');
|
$contacts = request('contacts');
|
||||||
|
|
||||||
@ -39,6 +44,15 @@ class StoreClientRequest extends Request
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function sanitize()
|
||||||
|
{
|
||||||
|
$input = $this->all();
|
||||||
|
|
||||||
|
$this->replace($input);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public function messages()
|
public function messages()
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
|
@ -37,7 +37,7 @@ public function sanitize()
|
|||||||
/** If we have an email address instead of a client_id - harvest the client_id here */
|
/** If we have an email address instead of a client_id - harvest the client_id here */
|
||||||
if(isset($input['email']) && !$input['client_id'])
|
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)
|
if($contact)
|
||||||
$input['client_id'] = $contact->client_id;
|
$input['client_id'] = $contact->client_id;
|
||||||
|
@ -14,7 +14,7 @@ class StoreClient
|
|||||||
{
|
{
|
||||||
use Dispatchable;
|
use Dispatchable;
|
||||||
|
|
||||||
protected $request;
|
protected $data;
|
||||||
|
|
||||||
protected $client;
|
protected $client;
|
||||||
|
|
||||||
@ -24,9 +24,10 @@ class StoreClient
|
|||||||
* @return void
|
* @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;
|
$this->client = $client;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,11 +36,11 @@ class StoreClient
|
|||||||
*
|
*
|
||||||
* @return void
|
* @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);
|
|
||||||
|
|
||||||
$contacts = $clientContactRepo->save($this->request->input('contacts'), $client);
|
$client = $client_repo->save($this->data, $this->client);
|
||||||
|
|
||||||
|
$contacts = $client_contact_repo->save($data['contacts']), $client);
|
||||||
|
|
||||||
return $client;
|
return $client;
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@ class UpdateClient
|
|||||||
{
|
{
|
||||||
use Dispatchable;
|
use Dispatchable;
|
||||||
|
|
||||||
protected $request;
|
protected $data;
|
||||||
|
|
||||||
protected $client;
|
protected $client;
|
||||||
|
|
||||||
@ -25,9 +25,9 @@ class UpdateClient
|
|||||||
* @return void
|
* @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;
|
$this->client = $client;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -36,11 +36,11 @@ class UpdateClient
|
|||||||
*
|
*
|
||||||
* @return void
|
* @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();
|
return $client->fresh();
|
||||||
}
|
}
|
||||||
|
@ -36,6 +36,7 @@ class BaseModel extends Model
|
|||||||
public function scopeCompany($query, $company_id)
|
public function scopeCompany($query, $company_id)
|
||||||
{
|
{
|
||||||
$query->where('company_id', $company_id);
|
$query->where('company_id', $company_id);
|
||||||
|
|
||||||
return $query;
|
return $query;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@ use App\Models\Client;
|
|||||||
use App\Models\ClientContact;
|
use App\Models\ClientContact;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* ClientContactRepository
|
||||||
*/
|
*/
|
||||||
class ClientContactRepository extends BaseRepository
|
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 */
|
/* 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){
|
collect($client->contacts->pluck('id'))->diff($contacts->pluck('id'))->each(function($contact){
|
||||||
|
|
||||||
ClientContact::destroy($contact);
|
ClientContact::destroy($contact);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
/* Set first record to primary - always*/
|
/* Set first record to primary - always*/
|
||||||
$contacts = $contacts->sortBy('is_primary');
|
$contacts = $contacts->sortBy('is_primary');
|
||||||
|
|
||||||
$contacts->first(function($contact){
|
$contacts->first(function($contact){
|
||||||
|
|
||||||
$contact['is_primary'] = true;
|
$contact['is_primary'] = true;
|
||||||
$contact->save();
|
$contact->save();
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
//loop and update/create contacts
|
//loop and update/create contacts
|
||||||
@ -43,13 +47,11 @@ class ClientContactRepository extends BaseRepository
|
|||||||
);
|
);
|
||||||
|
|
||||||
$update_contact->fill($contact);
|
$update_contact->fill($contact);
|
||||||
|
|
||||||
$update_contact->save();
|
$update_contact->save();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
@ -5,10 +5,9 @@ namespace App\Repositories;
|
|||||||
use App\Models\Client;
|
use App\Models\Client;
|
||||||
use App\Repositories\ClientContactRepository;
|
use App\Repositories\ClientContactRepository;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Facades\Log;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* ClientRepository
|
||||||
*/
|
*/
|
||||||
class ClientRepository extends BaseRepository
|
class ClientRepository extends BaseRepository
|
||||||
{
|
{
|
||||||
@ -16,32 +15,55 @@ class ClientRepository extends BaseRepository
|
|||||||
/**
|
/**
|
||||||
* @var ClientContactRepository
|
* @var ClientContactRepository
|
||||||
*/
|
*/
|
||||||
protected $contactRepo;
|
protected $contact_repo;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ClientController constructor.
|
* 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()
|
public function getClassName()
|
||||||
{
|
{
|
||||||
|
|
||||||
return Client::class;
|
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();
|
$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;
|
return $client;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -12,18 +12,16 @@ use Illuminate\Http\Request;
|
|||||||
class InvoiceRepository extends BaseRepository
|
class InvoiceRepository extends BaseRepository
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the class name.
|
* Gets the class name.
|
||||||
*
|
*
|
||||||
* @return ::class The class name.
|
* @return string The class name.
|
||||||
*/
|
*/
|
||||||
public function getClassName()
|
public function getClassName()
|
||||||
{
|
{
|
||||||
return Invoice::class;
|
return Invoice::class;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Saves the invoices
|
* Saves the invoices
|
||||||
*
|
*
|
||||||
@ -49,7 +47,6 @@ class InvoiceRepository extends BaseRepository
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mark the invoice as sent.
|
* Mark the invoice as sent.
|
||||||
*
|
*
|
||||||
|
@ -35,7 +35,7 @@ class RandomDataSeeder extends Seeder
|
|||||||
|
|
||||||
$user = factory(\App\Models\User::class)->create([
|
$user = factory(\App\Models\User::class)->create([
|
||||||
'email' => $faker->email,
|
'email' => $faker->email,
|
||||||
'account_id' => $account->id,
|
// 'account_id' => $account->id,
|
||||||
'confirmation_code' => $this->createDbHash(config('database.default'))
|
'confirmation_code' => $this->createDbHash(config('database.default'))
|
||||||
]);
|
]);
|
||||||
|
|
||||||
@ -94,6 +94,12 @@ class RandomDataSeeder extends Seeder
|
|||||||
/** Invoice Factory */
|
/** Invoice Factory */
|
||||||
factory(\App\Models\Invoice::class,50)->create(['user_id' => $user->id, 'company_id' => $company->id, 'client_id' => $client->id]);
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user