diff --git a/app/controllers/ClientController.php b/app/controllers/ClientController.php index 57dd8a359026..8b5083d7115b 100755 --- a/app/controllers/ClientController.php +++ b/app/controllers/ClientController.php @@ -62,42 +62,7 @@ class ClientController extends \BaseController { */ public function store() { - $rules = array( - 'name' => 'required', - 'email' => 'email' - ); - $validator = Validator::make(Input::all(), $rules); - - if ($validator->fails()) { - return Redirect::to('clients/create') - ->withErrors($validator) - ->withInput(Input::except('password')); - } else { - $client = new Client; - $client->account_id = Auth::user()->account_id; - $client->name = Input::get('name'); - $client->work_phone = Input::get('work_phone'); - $client->address1 = Input::get('address1'); - $client->address2 = Input::get('address2'); - $client->city = Input::get('city'); - $client->state = Input::get('state'); - $client->notes = Input::get('notes'); - $client->postal_code = Input::get('postal_code'); - $client->save(); - - $contact = new Contact; - $contact->email = Input::get('email'); - $contact->first_name = Input::get('first_name'); - $contact->last_name = Input::get('last_name'); - $contact->phone = Input::get('phone'); - $client->contacts()->save($contact); - - $url = 'clients/' . $client->id; - processedRequest($url); - - Session::flash('message', 'Successfully created client'); - return Redirect::to($url); - } + return $this->save(); } /** @@ -120,7 +85,7 @@ class ClientController extends \BaseController { */ public function edit($id) { - $client = Client::find($id); + $client = Client::with('contacts')->find($id); $data = array('client' => $client, 'method' => 'PUT', 'url' => 'clients/' . $id, 'title' => 'Edit'); return View::make('clients.edit', $data); } @@ -132,6 +97,11 @@ class ClientController extends \BaseController { * @return Response */ public function update($id) + { + return $this->save($id); + } + + private function save($id = null) { $rules = array( 'name' => 'required' @@ -143,7 +113,12 @@ class ClientController extends \BaseController { ->withErrors($validator) ->withInput(Input::except('password')); } else { - $client = Client::find($id); + if ($id) { + $client = Client::find($id); + } else { + $client = new Client; + } + $client->name = Input::get('name'); $client->work_phone = Input::get('work_phone'); $client->address1 = Input::get('address1'); @@ -154,12 +129,36 @@ class ClientController extends \BaseController { $client->postal_code = Input::get('postal_code'); $client->save(); - $contact = $client->contacts[0]; - $contact->email = Input::get('email'); - $contact->first_name = Input::get('first_name'); - $contact->last_name = Input::get('last_name'); - $contact->phone = Input::get('phone'); - $contact->save(); + $data = json_decode(Input::get('data')); + $contactIds = []; + + foreach ($data->contacts as $contact) + { + if (isset($contact->id) && $contact->id) + { + $record = Contact::find($contact->id); + } + else + { + $record = new Contact; + } + + $record->email = $contact->email; + $record->first_name = $contact->first_name; + $record->last_name = $contact->last_name; + $record->phone = $contact->phone; + + $client->contacts()->save($record); + $contactIds[] = $record->id; + } + + foreach ($client->contacts as $contact) + { + if (!in_array($contact->id, $contactIds)) + { + $contact->forceDelete(); + } + } Session::flash('message', 'Successfully updated client'); return Redirect::to('clients'); diff --git a/app/controllers/InvoiceController.php b/app/controllers/InvoiceController.php index e8cfd505a567..f0edded2ee80 100755 --- a/app/controllers/InvoiceController.php +++ b/app/controllers/InvoiceController.php @@ -172,11 +172,23 @@ class InvoiceController extends \BaseController { } } - /** - * Show the form for creating a new resource. - * - * @return Response - */ + + public function edit($id) + { + $invoice = Invoice::with('client', 'invoice_items')->find($id); + + $data = array( + 'invoice' => $invoice, + 'method' => 'PUT', + 'url' => 'invoices/' . $id, + 'title' => 'Edit', + 'account' => Auth::user()->account, + 'products' => Product::getProducts()->get(), + 'client' => $invoice->client, + 'clients' => Client::where('account_id','=',Auth::user()->account_id)->orderBy('name')->get()); + return View::make('invoices.edit', $data); + } + public function create($clientId = 0) { $client = null; @@ -193,7 +205,7 @@ class InvoiceController extends \BaseController { 'items' => json_decode(Input::old('items')), 'account' => Auth::user()->account, 'products' => Product::getProducts()->get(), - 'clients' => Client::where('account_id','=',Auth::user()->account_id)->get()); + 'clients' => Client::where('account_id','=',Auth::user()->account_id)->orderBy('name')->get()); return View::make('invoices.edit', $data); } @@ -335,28 +347,6 @@ class InvoiceController extends \BaseController { return View::make('invoices.show')->with('invoice', $invoice); } - /** - * Show the form for editing the specified resource. - * - * @param int $id - * @return Response - */ - public function edit($id) - { - $invoice = Invoice::with('client', 'invoice_items')->find($id); - - $data = array( - 'invoice' => $invoice, - 'method' => 'PUT', - 'url' => 'invoices/' . $id, - 'title' => 'Edit', - 'account' => Auth::user()->account, - 'products' => Product::getProducts()->get(), - 'client' => $invoice->client, - 'clients' => Client::where('account_id','=',Auth::user()->account_id)->get()); - return View::make('invoices.edit', $data); - } - /** * Update the specified resource in storage. * diff --git a/app/views/clients/edit.blade.php b/app/views/clients/edit.blade.php index a02daaeab031..5a98d5f86e28 100755 --- a/app/views/clients/edit.blade.php +++ b/app/views/clients/edit.blade.php @@ -10,15 +10,15 @@ - @if ($client) - {{ Former::populate($client); }} - @endif - {{ Former::open($url)->addClass('col-md-9 col-md-offset-1 main_form')->method($method)->rules(array( 'name' => 'required', 'email' => 'email' )); }} + @if ($client) + {{ Former::populate($client) }} + @endif + {{ Former::legend('Organization') }} {{ Former::text('name') }} @@ -26,19 +26,72 @@ {{ Former::textarea('notes') }} {{ Former::legend('Contacts') }} - {{ Former::text('first_name') }} - {{ Former::text('last_name') }} - {{ Former::text('email') }} - {{ Former::text('phone') }} +