diff --git a/app/controllers/ClientController.php b/app/controllers/ClientController.php index 21f2d28a4a..6da2d94ce0 100755 --- a/app/controllers/ClientController.php +++ b/app/controllers/ClientController.php @@ -95,7 +95,7 @@ class ClientController extends \BaseController { public function show($id) { $client = Client::with('contacts')->find($id); - trackViewed(Request::url(), $client->name); + trackViewed($client->name); return View::make('clients.show')->with('client', $client); @@ -157,7 +157,9 @@ class ClientController extends \BaseController { $client->state = Input::get('state'); $client->notes = Input::get('notes'); $client->postal_code = Input::get('postal_code'); - $client->country_id = Input::get('country_id'); + if (Input::get('country_id')) { + $client->country_id = Input::get('country_id'); + } $client->save(); $data = json_decode(Input::get('data')); diff --git a/app/controllers/InvoiceController.php b/app/controllers/InvoiceController.php index d5680fbb8f..28069dc3f1 100755 --- a/app/controllers/InvoiceController.php +++ b/app/controllers/InvoiceController.php @@ -206,7 +206,7 @@ class InvoiceController extends \BaseController { public function edit($id) { $invoice = Invoice::with('client', 'invoice_items')->find($id); - trackViewed(Request::url(), $invoice->invoice_number . ' - ' . $invoice->client->name); + trackViewed($invoice->invoice_number . ' - ' . $invoice->client->name); $data = array( 'invoice' => $invoice, @@ -223,12 +223,11 @@ class InvoiceController extends \BaseController { public function create($clientId = 0) { $client = null; - if ($clientId) { - $client = Client::find($clientId); - } - + $invoiceNumber = Auth::user()->account->getNextInvoiceNumber(); + $data = array( 'invoice' => null, + 'invoiceNumber' => $invoiceNumber, 'method' => 'POST', 'url' => 'invoices', 'title' => 'New', @@ -312,22 +311,25 @@ class InvoiceController extends \BaseController { $item->qty = 0; } - $product = Product::findProduct($item->product_key); - - if (!$product) + if ($item->product_key) { - $product = new Product; - $product->account_id = Auth::user()->account_id; - $product->key = $item->product_key; + $product = Product::findProduct($item->product_key); + + if (!$product) + { + $product = new Product; + $product->account_id = Auth::user()->account_id; + $product->key = $item->product_key; + } + + $product->notes = $item->notes; + $product->cost = $item->cost; + $product->qty = $item->qty; + $product->save(); } - $product->notes = $item->notes; - $product->cost = $item->cost; - $product->qty = $item->qty; - $product->save(); - $invoiceItem = new InvoiceItem; - $invoiceItem->product_id = $product->id; + $invoiceItem->product_id = isset($product) ? $product->id : null; $invoiceItem->product_key = $item->product_key; $invoiceItem->notes = $item->notes; $invoiceItem->cost = $item->cost; diff --git a/app/database/migrations/2013_11_05_180133_confide_setup_users_table.php b/app/database/migrations/2013_11_05_180133_confide_setup_users_table.php index 031b78a6e1..5f71c186ec 100755 --- a/app/database/migrations/2013_11_05_180133_confide_setup_users_table.php +++ b/app/database/migrations/2013_11_05_180133_confide_setup_users_table.php @@ -10,20 +10,41 @@ class ConfideSetupUsersTable extends Migration { */ public function up() { - Schema::dropIfExists('invoice_statuses'); - Schema::dropIfExists('invitations'); Schema::dropIfExists('activities'); + Schema::dropIfExists('invitations'); Schema::dropIfExists('account_gateways'); Schema::dropIfExists('gateways'); - Schema::dropIfExists('products'); - Schema::dropIfExists('invoice_items'); - Schema::dropIfExists('contacts'); Schema::dropIfExists('payments'); + Schema::dropIfExists('invoice_items'); + Schema::dropIfExists('products'); + Schema::dropIfExists('contacts'); Schema::dropIfExists('invoices'); - Schema::dropIfExists('accounts'); Schema::dropIfExists('users'); Schema::dropIfExists('password_reminders'); Schema::dropIfExists('clients'); + Schema::dropIfExists('accounts'); + Schema::dropIfExists('invoice_statuses'); + Schema::dropIfExists('countries'); + + + Schema::create('countries', function($table) + { + $table->increments('id'); + $table->string('capital', 255)->nullable(); + $table->string('citizenship', 255)->nullable(); + $table->string('country_code', 3)->default(''); + $table->string('currency', 255)->nullable(); + $table->string('currency_code', 255)->nullable(); + $table->string('currency_sub_unit', 255)->nullable(); + $table->string('full_name', 255)->nullable(); + $table->string('iso_3166_2', 2)->default(''); + $table->string('iso_3166_3', 3)->default(''); + $table->string('name', 255)->default(''); + $table->string('region_code', 3)->default(''); + $table->string('sub_region_code', 3)->default(''); + $table->boolean('eea')->default(0); + }); + Schema::create('accounts', function($t) { @@ -41,7 +62,9 @@ class ConfideSetupUsersTable extends Migration { $t->string('city'); $t->string('state'); $t->string('postal_code'); - $t->integer('country_id'); + $t->unsignedInteger('country_id')->nullable(); + + $t->foreign('country_id')->references('id')->on('countries'); }); @@ -59,18 +82,21 @@ class ConfideSetupUsersTable extends Migration { Schema::create('account_gateways', function($t) { $t->increments('id'); - $t->integer('account_id'); - $t->integer('gateway_id'); + $t->unsignedInteger('account_id'); + $t->unsignedInteger('gateway_id'); $t->timestamps(); $t->softDeletes(); $t->text('config'); + + $t->foreign('account_id')->references('id')->on('accounts')->onDelete('cascade'); + $t->foreign('gateway_id')->references('id')->on('gateways'); }); Schema::create('users', function($t) { $t->increments('id'); - $t->integer('account_id'); + $t->unsignedInteger('account_id'); $t->timestamps(); $t->softDeletes(); @@ -84,7 +110,7 @@ class ConfideSetupUsersTable extends Migration { $t->boolean('is_guest')->default(true); $t->boolean('confirmed')->default(false); - //$t->foreign('account_id')->references('id')->on('accounts'); + $t->foreign('account_id')->references('id')->on('accounts')->onDelete('cascade'); }); Schema::create('password_reminders', function($t) @@ -98,7 +124,8 @@ class ConfideSetupUsersTable extends Migration { Schema::create('clients', function($t) { $t->increments('id'); - $t->integer('account_id'); + $t->unsignedInteger('account_id'); + $t->unsignedInteger('country_id')->nullable(); $t->timestamps(); $t->softDeletes(); @@ -108,17 +135,18 @@ class ConfideSetupUsersTable extends Migration { $t->string('city'); $t->string('state'); $t->string('postal_code'); - $t->integer('country_id'); $t->string('work_phone'); $t->text('notes'); $t->decimal('balance', 10, 2); - //$t->foreign('account_id')->references('id')->on('accounts'); + + $t->foreign('account_id')->references('id')->on('accounts')->onDelete('cascade'); + $t->foreign('country_id')->references('id')->on('countries'); }); Schema::create('contacts', function($t) { $t->increments('id'); - $t->integer('client_id'); + $t->unsignedInteger('client_id'); $t->timestamps(); $t->softDeletes(); @@ -128,27 +156,9 @@ class ConfideSetupUsersTable extends Migration { $t->string('phone'); $t->timestamp('last_login'); - //$t->foreign('account_id')->references('id')->on('accounts'); + $t->foreign('client_id')->references('id')->on('clients')->onDelete('cascade'); }); - - Schema::create('invoices', function($t) - { - $t->increments('id'); - $t->integer('client_id'); - $t->integer('account_id'); - $t->integer('invoice_status_id')->default(1); - $t->timestamps(); - $t->softDeletes(); - - $t->string('invoice_number'); - $t->float('discount'); - $t->date('invoice_date'); - $t->date('due_date'); - - //$t->foreign('account_id')->references('id')->on('accounts'); - }); - Schema::create('invoice_statuses', function($t) { $t->increments('id'); @@ -156,37 +166,45 @@ class ConfideSetupUsersTable extends Migration { }); + Schema::create('invoices', function($t) + { + $t->increments('id'); + $t->unsignedInteger('client_id'); + $t->unsignedInteger('account_id'); + $t->unsignedInteger('invoice_status_id')->default(1); + $t->timestamps(); + $t->softDeletes(); + + $t->string('invoice_number'); + $t->float('discount'); + $t->date('invoice_date'); + $t->date('due_date'); + + $t->foreign('client_id')->references('id')->on('clients')->onDelete('cascade'); + $t->foreign('account_id')->references('id')->on('accounts'); + $t->foreign('invoice_status_id')->references('id')->on('invoice_statuses'); + }); + + Schema::create('invitations', function($t) { $t->increments('id'); - $t->integer('user_id'); - $t->integer('contact_id'); - $t->integer('invoice_id'); + $t->unsignedInteger('user_id'); + $t->unsignedInteger('contact_id'); + $t->unsignedInteger('invoice_id'); $t->string('key')->unique(); $t->timestamps(); $t->softDeletes(); - }); - Schema::create('invoice_items', function($t) - { - $t->increments('id'); - $t->integer('invoice_id'); - $t->timestamps(); - $t->softDeletes(); - - $t->integer('product_id'); - $t->string('product_key'); - $t->string('notes'); - $t->decimal('cost', 8, 2); - $t->integer('qty'); - - //$t->foreign('account_id')->references('id')->on('accounts'); + $t->foreign('user_id')->references('id')->on('users'); + $t->foreign('contact_id')->references('id')->on('contacts'); + $t->foreign('invoice_id')->references('id')->on('invoices')->onDelete('cascade'); }); Schema::create('products', function($t) { $t->increments('id'); - $t->integer('account_id'); + $t->unsignedInteger('account_id'); $t->timestamps(); $t->softDeletes(); @@ -195,17 +213,35 @@ class ConfideSetupUsersTable extends Migration { $t->decimal('cost', 8, 2); $t->integer('qty'); - //$t->foreign('account_id')->references('id')->on('accounts'); + $t->foreign('account_id')->references('id')->on('accounts')->onDelete('cascade'); + }); + + + Schema::create('invoice_items', function($t) + { + $t->increments('id'); + $t->unsignedInteger('invoice_id'); + $t->unsignedInteger('product_id')->nullable(); + $t->timestamps(); + $t->softDeletes(); + + $t->string('product_key'); + $t->string('notes'); + $t->decimal('cost', 8, 2); + $t->integer('qty'); + + $t->foreign('invoice_id')->references('id')->on('invoices')->onDelete('cascade'); + $t->foreign('product_id')->references('id')->on('products'); }); Schema::create('payments', function($t) { $t->increments('id'); - $t->integer('invoice_id'); - $t->integer('account_id'); - $t->integer('client_id'); - $t->integer('contact_id'); - $t->integer('user_id'); + $t->unsignedInteger('invoice_id'); + $t->unsignedInteger('account_id'); + $t->unsignedInteger('client_id')->nullable(); + $t->unsignedInteger('contact_id')->nullable(); + $t->unsignedInteger('user_id'); $t->timestamps(); $t->softDeletes(); @@ -213,19 +249,23 @@ class ConfideSetupUsersTable extends Migration { $t->string('transaction_reference'); $t->string('payer_id'); - //$t->foreign('account_id')->references('id')->on('accounts'); + $t->foreign('invoice_id')->references('id')->on('invoices'); + $t->foreign('account_id')->references('id')->on('accounts'); + $t->foreign('client_id')->references('id')->on('clients')->onDelete('cascade'); + $t->foreign('contact_id')->references('id')->on('contacts'); + $t->foreign('user_id')->references('id')->on('users'); }); Schema::create('activities', function($t) { $t->increments('id'); - $t->integer('account_id'); - $t->integer('user_id'); - $t->integer('client_id'); - $t->integer('contact_id'); - $t->integer('invoice_id'); - $t->integer('payment_id'); - $t->integer('invitation_id'); + $t->unsignedInteger('account_id'); + $t->unsignedInteger('client_id'); + $t->unsignedInteger('user_id')->nullable(); + $t->unsignedInteger('contact_id')->nullable(); + $t->unsignedInteger('invoice_id')->nullable(); + $t->unsignedInteger('payment_id')->nullable(); + $t->unsignedInteger('invitation_id')->nullable(); $t->timestamps(); $t->integer('activity_type_id'); @@ -240,20 +280,21 @@ class ConfideSetupUsersTable extends Migration { */ public function down() { - Schema::dropIfExists('invoice_statuses'); - Schema::dropIfExists('invitations'); Schema::dropIfExists('activities'); + Schema::dropIfExists('invitations'); Schema::dropIfExists('account_gateways'); Schema::dropIfExists('gateways'); - Schema::dropIfExists('products'); - Schema::dropIfExists('invoice_items'); - Schema::dropIfExists('contacts'); Schema::dropIfExists('payments'); + Schema::dropIfExists('invoice_items'); + Schema::dropIfExists('products'); + Schema::dropIfExists('contacts'); Schema::dropIfExists('invoices'); - Schema::dropIfExists('clients'); - Schema::dropIfExists('password_reminders'); Schema::dropIfExists('users'); + Schema::dropIfExists('password_reminders'); + Schema::dropIfExists('clients'); Schema::dropIfExists('accounts'); - } + Schema::dropIfExists('invoice_statuses'); + Schema::dropIfExists('countries'); + } } diff --git a/app/database/migrations/2013_11_28_195703_setup_countries_table.php b/app/database/migrations/2013_11_28_195703_setup_countries_table.php index 37d35f884d..09e8c8eca2 100755 --- a/app/database/migrations/2013_11_28_195703_setup_countries_table.php +++ b/app/database/migrations/2013_11_28_195703_setup_countries_table.php @@ -11,6 +11,7 @@ class SetupCountriesTable extends Migration { public function up() { // Creates the users table + /* Schema::create('countries', function($table) { $table->integer('id')->index(); @@ -30,6 +31,7 @@ class SetupCountriesTable extends Migration { $table->primary('id'); }); + */ } /** @@ -39,7 +41,7 @@ class SetupCountriesTable extends Migration { */ public function down() { - Schema::drop('countries'); + //Schema::drop('countries'); } -} +} \ No newline at end of file diff --git a/app/database/seeds/ConstantsSeeder.php b/app/database/seeds/ConstantsSeeder.php index 9dbcbc6de2..3784966a54 100755 --- a/app/database/seeds/ConstantsSeeder.php +++ b/app/database/seeds/ConstantsSeeder.php @@ -31,7 +31,6 @@ class ConstantsSeeder extends Seeder $client->invoices()->save($invoice); */ - InvoiceStatus::create(array('name' => 'Draft')); InvoiceStatus::create(array('name' => 'Sent')); InvoiceStatus::create(array('name' => 'Viewed')); diff --git a/app/models/Account.php b/app/models/Account.php index a3d801f98d..a7bb686d84 100755 --- a/app/models/Account.php +++ b/app/models/Account.php @@ -14,6 +14,11 @@ class Account extends Eloquent return $this->hasMany('Client'); } + public function invoices() + { + return $this->hasMany('Invoice'); + } + public function account_gateways() { return $this->hasMany('AccountGateway'); @@ -60,4 +65,19 @@ class Account extends Eloquent list($width, $height) = getimagesize($this->getLogoPath()); return $height; } + + public function getNextInvoiceNumber() + { + $order = $this->invoices()->orderBy('invoice_number', 'DESC')->first(); + + if ($order) + { + $number = intval($order->invoice_number) + 1; + return str_pad($number, 5, "0", STR_PAD_LEFT); + } + else + { + return DEFAULT_INVOICE_NUMBER; + } + } } \ No newline at end of file diff --git a/app/models/Client.php b/app/models/Client.php index 37c5d3927d..f87fc92886 100755 --- a/app/models/Client.php +++ b/app/models/Client.php @@ -91,7 +91,6 @@ class Client extends Eloquent return $this->created_at->format('m/d/y h:i a'); } } - } Client::created(function($client) diff --git a/app/routes.php b/app/routes.php index 83a538e1e2..768fad1dea 100755 --- a/app/routes.php +++ b/app/routes.php @@ -143,8 +143,9 @@ function processedRequest($url) -function trackViewed($url, $name) +function trackViewed($name) { + $url = Request::url(); $viewed = Session::get(RECENTLY_VIEWED); if (!$viewed) @@ -168,7 +169,7 @@ function trackViewed($url, $name) array_unshift($viewed, $object); - if (count($viewed) > 5) + if (count($viewed) > RECENTLY_VIEWED_LIMIT) { array_pop($viewed); } @@ -190,4 +191,8 @@ define("ACCOUNT_DETAILS", "details"); define("ACCOUNT_SETTINGS", "settings"); define("ACCOUNT_IMPORT", "import"); define("ACCOUNT_MAP", "import_map"); -define("ACCOUNT_EXPORT", "export"); \ No newline at end of file +define("ACCOUNT_EXPORT", "export"); + + +define("DEFAULT_INVOICE_NUMBER", "00001"); +define("RECENTLY_VIEWED_LIMIT", 8); \ No newline at end of file diff --git a/app/views/clients/show.blade.php b/app/views/clients/show.blade.php index dbb2137125..1ddf0c9f33 100755 --- a/app/views/clients/show.blade.php +++ b/app/views/clients/show.blade.php @@ -27,9 +27,9 @@
-

Balance

+

Standing

$0.00 Paid to Date USD

-

$0.00 Outstanding USD

+

$0.00 Balance USD

diff --git a/app/views/header.blade.php b/app/views/header.blade.php index a936aef855..0df8f772f9 100755 --- a/app/views/header.blade.php +++ b/app/views/header.blade.php @@ -267,7 +267,9 @@
  • No items
  • @else @foreach (Session::get(RECENTLY_VIEWED) as $link) + @if (Request::url() != $link->url)
  • {{ $link->name }}
  • + @endif @endforeach @endif diff --git a/app/views/invoices/edit.blade.php b/app/views/invoices/edit.blade.php index 5ba7cfdf9a..23f4d3ed18 100755 --- a/app/views/invoices/edit.blade.php +++ b/app/views/invoices/edit.blade.php @@ -6,7 +6,8 @@ {{ Former::open($url)->method($method)->addClass('main_form')->rules(array( 'invoice_number' => 'required', - 'invoice_date' => 'required' + 'invoice_date' => 'required', + 'product_key' => 'max:14', )); }} @@ -16,6 +17,7 @@ {{ Former::populateField('invoice_date', fromSqlDate($invoice->invoice_date)); }} {{ Former::populateField('due_date', fromSqlDate($invoice->due_date)); }} @else + {{ Former::populateField('invoice_number', $invoiceNumber) }} {{ Former::populateField('invoice_date', date('m/d/Y')) }} @endif @@ -54,18 +56,18 @@ - - {{ Former::text('product_key')->useDatalist(Product::getProductKeys($products), 'key') + + {{ Former::text('product_key')->useDatalist(Product::getProductKeys($products), 'key')->onkeyup('onChange()') ->raw()->data_bind("value: product_key, valueUpdate: 'afterkeydown'")->addClass('datalist') }} - + - + - +