diff --git a/app/Http/Controllers/Expenses/Bills.php b/app/Http/Controllers/Expenses/Bills.php index a327d79be..7ee651476 100644 --- a/app/Http/Controllers/Expenses/Bills.php +++ b/app/Http/Controllers/Expenses/Bills.php @@ -161,7 +161,7 @@ class Bills extends Controller { $success = true; - $allowed_sheets = ['bills', 'bill_items', 'bill_histories', 'bill_payments', 'bill_totals']; + $allowed_sheets = ['bills', 'bill_items', 'bill_item_taxes', 'bill_histories', 'bill_payments', 'bill_totals']; // Loop through all sheets $import->each(function ($sheet) use (&$success, $allowed_sheets) { @@ -245,7 +245,7 @@ class Bills extends Controller */ public function destroy(Bill $bill) { - $this->deleteRelationships($bill, ['items', 'itemTaxes', 'histories', 'payments', 'recurring', 'totals']); + $this->deleteRelationships($bill, ['items', 'item_taxes', 'histories', 'payments', 'recurring', 'totals']); $bill->delete(); $message = trans('messages.success.deleted', ['type' => trans_choice('general.bills', 1)]); @@ -263,15 +263,15 @@ class Bills extends Controller public function export() { \Excel::create('bills', function ($excel) { - $bills = Bill::with(['items', 'histories', 'payments', 'totals'])->filter(request()->input())->get(); + $bills = Bill::with(['items', 'item_taxes', 'histories', 'payments', 'totals'])->filter(request()->input())->get(); $excel->sheet('bills', function ($sheet) use ($bills) { $sheet->fromModel($bills->makeHidden([ - 'company_id', 'parent_id', 'created_at', 'updated_at', 'deleted_at', 'attachment', 'discount', 'items', 'histories', 'payments', 'totals', 'media', 'paid' + 'company_id', 'parent_id', 'created_at', 'updated_at', 'deleted_at', 'attachment', 'discount', 'items', 'item_taxes', 'histories', 'payments', 'totals', 'media', 'paid', 'amount_without_tax' ])); }); - $tables = ['items', 'histories', 'payments', 'totals']; + $tables = ['items', 'item_taxes', 'histories', 'payments', 'totals']; foreach ($tables as $table) { $excel->sheet('bill_' . $table, function ($sheet) use ($bills, $table) { $hidden_fields = ['id', 'company_id', 'created_at', 'updated_at', 'deleted_at', 'title']; diff --git a/app/Http/Controllers/Incomes/Invoices.php b/app/Http/Controllers/Incomes/Invoices.php index 45b4b014a..4d5a9c0d8 100644 --- a/app/Http/Controllers/Incomes/Invoices.php +++ b/app/Http/Controllers/Incomes/Invoices.php @@ -173,7 +173,7 @@ class Invoices extends Controller { $success = true; - $allowed_sheets = ['invoices', 'invoice_items', 'invoice_histories', 'invoice_payments', 'invoice_totals']; + $allowed_sheets = ['invoices', 'invoice_items', 'invoice_item_taxes', 'invoice_histories', 'invoice_payments', 'invoice_totals']; // Loop through all sheets $import->each(function ($sheet) use (&$success, $allowed_sheets) { @@ -257,7 +257,7 @@ class Invoices extends Controller */ public function destroy(Invoice $invoice) { - $this->deleteRelationships($invoice, ['items', 'itemTaxes', 'histories', 'payments', 'recurring', 'totals']); + $this->deleteRelationships($invoice, ['items', 'item_taxes', 'histories', 'payments', 'recurring', 'totals']); $invoice->delete(); $message = trans('messages.success.deleted', ['type' => trans_choice('general.invoices', 1)]); @@ -275,15 +275,15 @@ class Invoices extends Controller public function export() { \Excel::create('invoices', function ($excel) { - $invoices = Invoice::with(['items', 'histories', 'payments', 'totals'])->filter(request()->input())->get(); + $invoices = Invoice::with(['items', 'item_taxes', 'histories', 'payments', 'totals'])->filter(request()->input())->get(); $excel->sheet('invoices', function ($sheet) use ($invoices) { $sheet->fromModel($invoices->makeHidden([ - 'company_id', 'parent_id', 'created_at', 'updated_at', 'deleted_at', 'attachment', 'discount', 'items', 'histories', 'payments', 'totals', 'media', 'paid' + 'company_id', 'parent_id', 'created_at', 'updated_at', 'deleted_at', 'attachment', 'discount', 'items', 'item_taxes', 'histories', 'payments', 'totals', 'media', 'paid', 'amount_without_tax' ])); }); - $tables = ['items', 'histories', 'payments', 'totals']; + $tables = ['items', 'item_taxes', 'histories', 'payments', 'totals']; foreach ($tables as $table) { $excel->sheet('invoice_' . $table, function ($sheet) use ($invoices, $table) { $hidden_fields = ['id', 'company_id', 'created_at', 'updated_at', 'deleted_at', 'title']; diff --git a/app/Jobs/Expense/CreateBillItem.php b/app/Jobs/Expense/CreateBillItem.php index 9df227f78..b1d7af6d3 100644 --- a/app/Jobs/Expense/CreateBillItem.php +++ b/app/Jobs/Expense/CreateBillItem.php @@ -176,7 +176,6 @@ class CreateBillItem 'quantity' => (double) $this->data['quantity'], 'price' => (double) $this->data['price'], 'tax' => $item_tax_total, - 'tax_id' => 0, 'total' => $item_amount, ]); diff --git a/app/Jobs/Income/CreateInvoiceItem.php b/app/Jobs/Income/CreateInvoiceItem.php index 3478baff1..83ec50bfd 100644 --- a/app/Jobs/Income/CreateInvoiceItem.php +++ b/app/Jobs/Income/CreateInvoiceItem.php @@ -203,7 +203,6 @@ class CreateInvoiceItem 'quantity' => (double) $this->data['quantity'], 'price' => (double) $this->data['price'], 'tax' => $item_tax_total, - 'tax_id' => 0, 'total' => $item_amount, ]); diff --git a/app/Listeners/Updates/V13/Version139.php b/app/Listeners/Updates/V13/Version139.php new file mode 100644 index 000000000..4b8be2fd4 --- /dev/null +++ b/app/Listeners/Updates/V13/Version139.php @@ -0,0 +1,58 @@ +check($event)) { + return; + } + + $this->copyOldInvoiceItemTaxes(); + + // Update database + Artisan::call('migrate', ['--force' => true]); + } + + protected function copyOldInvoiceItemTaxes() + { + $invoice_items = InvoiceItem::where('company_id', '<>', '0')->where('tax_id', '<>', '0')->get(); + + foreach ($invoice_items as $invoice_item) { + $tax = Tax::where('company_id', $invoice_item->company_id)->where('id', $invoice_item->tax_id)->first(); + + if (empty($tax)) { + continue; + } + + InvoiceItemTax::create([ + 'company_id' => $invoice_item->company_id, + 'invoice_id' => $invoice_item->invoice_id, + 'invoice_item_id' => $invoice_item->id, + 'tax_id' => $invoice_item->tax_id, + 'name' => $tax->name, + 'amount' => $invoice_item->tax, + ]); + } + } +} diff --git a/app/Models/Expense/Bill.php b/app/Models/Expense/Bill.php index aaf7cbe98..44931eb09 100644 --- a/app/Models/Expense/Bill.php +++ b/app/Models/Expense/Bill.php @@ -83,7 +83,7 @@ class Bill extends Model return $this->hasMany('App\Models\Expense\BillItem'); } - public function itemTaxes() + public function item_taxes() { return $this->hasMany('App\Models\Expense\BillItemTax'); } diff --git a/app/Models/Expense/BillItem.php b/app/Models/Expense/BillItem.php index b40f6fdf5..144487ac2 100644 --- a/app/Models/Expense/BillItem.php +++ b/app/Models/Expense/BillItem.php @@ -17,7 +17,7 @@ class BillItem extends Model * * @var array */ - protected $fillable = ['company_id', 'bill_id', 'item_id', 'name', 'sku', 'quantity', 'price', 'total', 'tax', 'tax_id']; + protected $fillable = ['company_id', 'bill_id', 'item_id', 'name', 'sku', 'quantity', 'price', 'total', 'tax']; public function bill() { @@ -29,16 +29,11 @@ class BillItem extends Model return $this->belongsTo('App\Models\Common\Item'); } - public function itemTaxes() + public function taxes() { return $this->hasMany('App\Models\Expense\BillItemTax', 'bill_item_id', 'id'); } - public function tax() - { - return $this->belongsTo('App\Models\Setting\Tax'); - } - /** * Convert price to double. * @@ -71,23 +66,4 @@ class BillItem extends Model { $this->attributes['tax'] = (double) $value; } - - /** - * Convert tax to double. - * - * @param string $value - * @return void - */ - public function getTaxIdAttribute($value) - { - $tax_ids = []; - - if (!empty($value)) { - $tax_ids[] = $value; - - return $tax_ids; - } - - return $this->itemTaxes->pluck('tax_id'); - } } diff --git a/app/Models/Expense/BillItemTax.php b/app/Models/Expense/BillItemTax.php index 8f7e2862a..8e83e453c 100644 --- a/app/Models/Expense/BillItemTax.php +++ b/app/Models/Expense/BillItemTax.php @@ -24,11 +24,6 @@ class BillItemTax extends Model return $this->belongsTo('App\Models\Expense\Bill'); } - public function item() - { - return $this->belongsTo('App\Models\Common\Item'); - } - public function tax() { return $this->belongsTo('App\Models\Setting\Tax'); diff --git a/app/Models/Income/Invoice.php b/app/Models/Income/Invoice.php index 3e6810828..6a4dae419 100644 --- a/app/Models/Income/Invoice.php +++ b/app/Models/Income/Invoice.php @@ -86,7 +86,7 @@ class Invoice extends Model return $this->hasMany('App\Models\Income\InvoiceItem'); } - public function itemTaxes() + public function item_taxes() { return $this->hasMany('App\Models\Income\InvoiceItemTax'); } diff --git a/app/Models/Income/InvoiceItem.php b/app/Models/Income/InvoiceItem.php index dff192aec..1fa829a92 100644 --- a/app/Models/Income/InvoiceItem.php +++ b/app/Models/Income/InvoiceItem.php @@ -17,7 +17,7 @@ class InvoiceItem extends Model * * @var array */ - protected $fillable = ['company_id', 'invoice_id', 'item_id', 'name', 'sku', 'quantity', 'price', 'total', 'tax', 'tax_id']; + protected $fillable = ['company_id', 'invoice_id', 'item_id', 'name', 'sku', 'quantity', 'price', 'total', 'tax']; public function invoice() { @@ -29,16 +29,11 @@ class InvoiceItem extends Model return $this->belongsTo('App\Models\Common\Item'); } - public function itemTaxes() + public function taxes() { return $this->hasMany('App\Models\Income\InvoiceItemTax', 'invoice_item_id', 'id'); } - public function tax() - { - return $this->belongsTo('App\Models\Setting\Tax'); - } - /** * Convert price to double. * @@ -71,23 +66,4 @@ class InvoiceItem extends Model { $this->attributes['tax'] = (double) $value; } - - /** - * Convert tax to double. - * - * @param string $value - * @return void - */ - public function getTaxIdAttribute($value) - { - $tax_ids = []; - - if (!empty($value)) { - $tax_ids[] = $value; - - return $tax_ids; - } - - return $this->itemTaxes->pluck('tax_id'); - } } diff --git a/app/Models/Income/InvoiceItemTax.php b/app/Models/Income/InvoiceItemTax.php index ddaea16ad..71931e1f3 100644 --- a/app/Models/Income/InvoiceItemTax.php +++ b/app/Models/Income/InvoiceItemTax.php @@ -24,11 +24,6 @@ class InvoiceItemTax extends Model return $this->belongsTo('App\Models\Income\Invoice'); } - public function item() - { - return $this->belongsTo('App\Models\Common\Item'); - } - public function tax() { return $this->belongsTo('App\Models\Setting\Tax'); diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index 29037dd78..86267942b 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -32,6 +32,7 @@ class EventServiceProvider extends ServiceProvider 'App\Listeners\Updates\V13\Version132', 'App\Listeners\Updates\V13\Version135', 'App\Listeners\Updates\V13\Version138', + 'App\Listeners\Updates\V13\Version139', ], 'Illuminate\Auth\Events\Login' => [ 'App\Listeners\Auth\Login', diff --git a/database/migrations/2019_01_07_000000_drop_tax_id_column.php b/database/migrations/2019_01_07_000000_drop_tax_id_column.php new file mode 100644 index 000000000..3b78caa10 --- /dev/null +++ b/database/migrations/2019_01_07_000000_drop_tax_id_column.php @@ -0,0 +1,40 @@ +dropColumn('tax_id'); + }); + + Schema::table('invoice_items', function (Blueprint $table) { + $table->dropColumn('tax_id'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('bill_items', function (Blueprint $table) { + $table->integer('tax_id')->default(0); + }); + + Schema::table('invoice_items', function (Blueprint $table) { + $table->integer('tax_id')->default(0); + }); + } +} diff --git a/resources/views/expenses/bills/item.blade.php b/resources/views/expenses/bills/item.blade.php index 528c1d439..65ab64a0d 100644 --- a/resources/views/expenses/bills/item.blade.php +++ b/resources/views/expenses/bills/item.blade.php @@ -35,7 +35,7 @@ @stack('taxes_td_start')
:message
') !!} @stack('tax_id_input_end'):message
') !!} @stack('tax_id_input_end')