diff --git a/app/controllers/InvoiceController.php b/app/controllers/InvoiceController.php index 1ee6f854c3aa..4427c1ffdbf2 100755 --- a/app/controllers/InvoiceController.php +++ b/app/controllers/InvoiceController.php @@ -3,20 +3,23 @@ use ninja\mailers\ContactMailer as Mailer; use ninja\repositories\InvoiceRepository; use ninja\repositories\ClientRepository; +use ninja\repositories\TaxRateRepository; class InvoiceController extends \BaseController { protected $mailer; protected $invoiceRepo; protected $clientRepo; + protected $taxRateRepo; - public function __construct(Mailer $mailer, InvoiceRepository $invoiceRepo, ClientRepository $clientRepo) + public function __construct(Mailer $mailer, InvoiceRepository $invoiceRepo, ClientRepository $clientRepo, TaxRateRepository $taxRateRepo) { parent::__construct(); $this->mailer = $mailer; $this->invoiceRepo = $invoiceRepo; $this->clientRepo = $clientRepo; + $this->taxRateRepo = $taxRateRepo; } public function index() @@ -391,13 +394,15 @@ class InvoiceController extends \BaseController { } else { + $this->taxRateRepo->save($input->tax_rates); + $clientData = (array) $input->client; $client = $this->clientRepo->save($input->client->public_id, $clientData); $invoiceData = (array) $input; $invoiceData['client_id'] = $client->id; $invoice = $this->invoiceRepo->save($publicId, $invoiceData); - + if ($action == 'email' && $invoice->invoice_status_id == INVOICE_STATUS_DRAFT) { $invoice->invoice_status_id = INVOICE_STATUS_SENT; 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 524b1fe977e1..e75d0d49ff60 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 @@ -359,7 +359,6 @@ class ConfideSetupUsersTable extends Migration { $t->unsignedInteger('user_id'); $t->unsignedInteger('invoice_id')->index(); $t->unsignedInteger('product_id')->nullable(); - $t->unsignedInteger('tax_rate_id')->nullable(); $t->timestamps(); $t->softDeletes(); @@ -373,7 +372,6 @@ class ConfideSetupUsersTable extends Migration { $t->foreign('invoice_id')->references('id')->on('invoices')->onDelete('cascade'); $t->foreign('product_id')->references('id')->on('products'); - $t->foreign('tax_rate_id')->references('id')->on('tax_rates'); $t->foreign('user_id')->references('id')->on('users'); $t->unsignedInteger('public_id'); diff --git a/app/models/Account.php b/app/models/Account.php index 1a0c90e8b4a4..32fb275172a9 100755 --- a/app/models/Account.php +++ b/app/models/Account.php @@ -25,6 +25,11 @@ class Account extends Eloquent return $this->hasMany('AccountGateway'); } + public function tax_rates() + { + return $this->hasMany('TaxRate'); + } + public function country() { return $this->belongsTo('Country'); diff --git a/app/models/TaxRate.php b/app/models/TaxRate.php index c30d0d1d8569..692686b07f37 100755 --- a/app/models/TaxRate.php +++ b/app/models/TaxRate.php @@ -2,5 +2,6 @@ class TaxRate extends EntityModel { + } \ No newline at end of file diff --git a/app/ninja/repositories/InvoiceRepository.php b/app/ninja/repositories/InvoiceRepository.php index cb74400f1e42..270422fc0f83 100755 --- a/app/ninja/repositories/InvoiceRepository.php +++ b/app/ninja/repositories/InvoiceRepository.php @@ -135,24 +135,6 @@ class InvoiceRepository $product->save(); } - $taxRate = false; - if ($item->tax) - { - if ($item->tax->public_id) - { - $taxRate = TaxRate::scope($item->tax->public_id)->firstOrFail(); - } - else - { - $taxRate = TaxRate::createNew(); - } - - $taxRate->rate = floatval($item->tax->rate); - $taxRate->name = trim($item->tax->name); - - $taxRate->save(); - } - $invoiceItem = InvoiceItem::createNew(); $invoiceItem->product_id = isset($product) ? $product->id : null; $invoiceItem->product_key = trim($item->product_key); @@ -160,11 +142,10 @@ class InvoiceRepository $invoiceItem->cost = floatval($item->cost); $invoiceItem->qty = floatval($item->qty); - if ($taxRate) + if ($item->tax && isset($item->tax->rate) && isset($item->tax->name)) { - $invoiceItem->tax_rate_id = $taxRate->id; - $invoiceItem->tax_rate = $taxRate->rate; - $invoiceItem->tax_name = $taxRate->name; + $invoiceItem->tax_rate = floatval($item->tax->rate); + $invoiceItem->tax_name = trim($item->tax->name); } $invoice->invoice_items()->save($invoiceItem); diff --git a/app/ninja/repositories/TaxRateRepository.php b/app/ninja/repositories/TaxRateRepository.php new file mode 100755 index 000000000000..4f91999c8519 --- /dev/null +++ b/app/ninja/repositories/TaxRateRepository.php @@ -0,0 +1,49 @@ +rate) || $record->is_deleted) + { + continue; + } + + if (!floatval($record->rate) || !trim($record->name)) + { + continue; + } + + if ($record->public_id) + { + $taxRate = TaxRate::scope($record->public_id)->firstOrFail(); + } + else + { + $taxRate = TaxRate::createNew(); + } + + $taxRate->rate = floatval($record->rate); + $taxRate->name = trim($record->name); + $taxRate->save(); + + $taxRateIds[] = $taxRate->public_id; + } + + $taxRates = TaxRate::scope()->get(); + + foreach($taxRates as $taxRate) + { + if (!in_array($taxRate->public_id, $taxRateIds)) + { + $taxRate->delete(); + } + } + } +} \ No newline at end of file diff --git a/app/views/invoices/edit.blade.php b/app/views/invoices/edit.blade.php index ed8cb3e586c9..3094fb3b6fcd 100755 --- a/app/views/invoices/edit.blade.php +++ b/app/views/invoices/edit.blade.php @@ -111,10 +111,10 @@ - + - - + + @@ -286,7 +286,7 @@ - +   @@ -298,7 +298,7 @@ @@ -392,9 +392,9 @@ var product = products[i]; if (product.product_key == key) { var model = ko.dataFor(this); - model.notes(product.notes); - model.cost(product.cost); - model.qty(product.qty); + //model.notes(product.notes); + //model.cost(product.cost); + //model.qty(product.qty); break; } } @@ -611,12 +611,7 @@ self.addItem = function() { var itemModel = new ItemModel(); self.invoice_items.push(itemModel); - applyComboboxListeners(); - - itemModel.tax.subscribe(function (data) { - console.log('Tax change...'); - console.log(data) - }); + applyComboboxListeners(); } self.removeTaxRate = function(taxRate) { @@ -625,7 +620,8 @@ } self.addTaxRate = function(data) { - self.tax_rates.push(new TaxRateModel(data)); + var itemModel = new TaxRateModel(data); + self.tax_rates.push(itemModel); applyComboboxListeners(); } @@ -737,15 +733,27 @@ self.public_id = ko.observable(''); self.rate = ko.observable(); self.name = ko.observable(''); + self.is_deleted = ko.observable(false); self.actionsVisible = ko.observable(false); if (data) { ko.mapping.fromJS(data, {}, this); } + this.prettyRate = ko.computed({ + read: function () { + return this.rate() ? parseFloat(this.rate()) : ''; + }, + write: function (value) { + this.rate(value); + }, + owner: this + }); + + self.displayName = ko.computed(function() { var name = self.name() ? self.name() : false; - var rate = self.rate() ? self.rate() : false; + var rate = self.rate() ? parseFloat(self.rate()) : false; return (name && rate) ? (rate + '%' + ' - ' + name) : ''; }); @@ -771,19 +779,40 @@ this.tax = ko.observable(); this.actionsVisible = ko.observable(false); + this.prettyQty = ko.computed({ + read: function () { + return this.qty() ? parseFloat(this.qty()) : ''; + }, + write: function (value) { + this.qty(value); + }, + owner: this + }); + if (data) { - ko.mapping.fromJS(data, {}, this); + ko.mapping.fromJS(data, {}, this); } - console.log('data: ' + data); for (var i=0; iinvoice_terms }}', 250)); @endif - model.addTaxRate(); model.addItem(); ko.applyBindings(model);