From 0a5136bd40f3a4d551b07ba0261987564e83b6c8 Mon Sep 17 00:00:00 2001 From: denisdulici Date: Tue, 1 May 2018 19:00:33 +0300 Subject: [PATCH] fixed recurring command #315 --- app/Console/Commands/RecurringCheck.php | 38 +++++++------ app/Http/Controllers/Expenses/Bills.php | 1 + app/Http/Controllers/Expenses/Payments.php | 1 + app/Http/Controllers/Incomes/Invoices.php | 1 + app/Http/Controllers/Incomes/Revenues.php | 3 ++ app/Models/Common/Recurring.php | 2 +- app/Models/Expense/Bill.php | 4 +- app/Models/Expense/Payment.php | 4 +- app/Models/Income/Invoice.php | 4 +- app/Models/Income/Revenue.php | 4 +- .../2018_04_30_000000_add_parent_column.php | 54 +++++++++++++++++++ 11 files changed, 90 insertions(+), 26 deletions(-) create mode 100644 database/migrations/2018_04_30_000000_add_parent_column.php diff --git a/app/Console/Commands/RecurringCheck.php b/app/Console/Commands/RecurringCheck.php index 6d4aebbd4..049391287 100644 --- a/app/Console/Commands/RecurringCheck.php +++ b/app/Console/Commands/RecurringCheck.php @@ -3,13 +3,8 @@ namespace App\Console\Commands; use App\Models\Company\Company; -use App\Models\Common\Recurring; -use App\Models\Expense\Bill; use App\Models\Expense\BillHistory; -use App\Models\Expense\Payment; -use App\Models\Income\Invoice; use App\Models\Income\InvoiceHistory; -use App\Models\Income\Revenue; use App\Notifications\Expense\Bill as BillNotification; use App\Notifications\Income\Invoice as InvoiceNotification; use App\Traits\Incomes; @@ -68,9 +63,7 @@ class RecurringCheck extends Command $company->setSettings(); - $recurring = $company->recurring(); - - foreach ($recurring as $recur) { + foreach ($company->recurring as $recur) { $current = Date::parse($recur->schedule()->current()->getStart()->format('Y-m-d')); // Check if should recur today @@ -78,29 +71,30 @@ class RecurringCheck extends Command continue; } - $type = end(explode('\\', $recur->recurable_type)); - - $model = $type::find($recur->recurable_id); + $model = $recur->recurable; if (!$model) { continue; } - switch ($type) { - case 'Bill': + switch ($recur->recurable_type) { + case 'App\Models\Expense\Bill': $this->recurBill($company, $model); break; - case 'Invoice': + case 'App\Models\Income\Invoice': $this->recurInvoice($company, $model); break; - case 'Payment': - case 'Revenue': + case 'App\Models\Expense\Payment': + case 'App\Models\Income\Revenue': + $model->cloneable_relations = []; + // Create new record $clone = $model->duplicate(); - // Update date + $clone->parent_id = $model->id; $clone->paid_at = $this->today->format('Y-m-d'); $clone->save(); + break; } } @@ -112,9 +106,14 @@ class RecurringCheck extends Command protected function recurInvoice($company, $model) { + $model->cloneable_relations = ['items', 'totals']; + // Create new record $clone = $model->duplicate(); + // Set original invoice id + $clone->parent_id = $model->id; + // Days between invoiced and due date $diff_days = Date::parse($clone->due_at)->diffInDays(Date::parse($clone->invoiced_at)); @@ -152,9 +151,14 @@ class RecurringCheck extends Command protected function recurBill($company, $model) { + $model->cloneable_relations = ['items', 'totals']; + // Create new record $clone = $model->duplicate(); + // Set original bill id + $clone->parent_id = $model->id; + // Days between invoiced and due date $diff_days = Date::parse($clone->due_at)->diffInDays(Date::parse($clone->invoiced_at)); diff --git a/app/Http/Controllers/Expenses/Bills.php b/app/Http/Controllers/Expenses/Bills.php index ecb0594c1..0fcd92356 100644 --- a/app/Http/Controllers/Expenses/Bills.php +++ b/app/Http/Controllers/Expenses/Bills.php @@ -469,6 +469,7 @@ class Bills extends Controller */ public function destroy(Bill $bill) { + $bill->recurring()->delete(); $bill->delete(); /* diff --git a/app/Http/Controllers/Expenses/Payments.php b/app/Http/Controllers/Expenses/Payments.php index fcb6ac7ed..25fc05f3a 100644 --- a/app/Http/Controllers/Expenses/Payments.php +++ b/app/Http/Controllers/Expenses/Payments.php @@ -222,6 +222,7 @@ class Payments extends Controller return redirect('expenses/payments'); } + $payment->recurring()->delete(); $payment->delete(); $message = trans('messages.success.deleted', ['type' => trans_choice('general.payments', 1)]); diff --git a/app/Http/Controllers/Incomes/Invoices.php b/app/Http/Controllers/Incomes/Invoices.php index f56140581..51b45357b 100644 --- a/app/Http/Controllers/Incomes/Invoices.php +++ b/app/Http/Controllers/Incomes/Invoices.php @@ -490,6 +490,7 @@ class Invoices extends Controller */ public function destroy(Invoice $invoice) { + $invoice->recurring()->delete(); $invoice->delete(); /* diff --git a/app/Http/Controllers/Incomes/Revenues.php b/app/Http/Controllers/Incomes/Revenues.php index 835968775..7bdc017db 100644 --- a/app/Http/Controllers/Incomes/Revenues.php +++ b/app/Http/Controllers/Incomes/Revenues.php @@ -172,6 +172,8 @@ class Revenues extends Controller $payment_methods = Modules::getPaymentMethods(); + $a = $revenue->recurring->schedule()->next(); + return view('incomes.revenues.edit', compact('revenue', 'accounts', 'currencies', 'account_currency_code', 'customers', 'categories', 'payment_methods')); } @@ -224,6 +226,7 @@ class Revenues extends Controller return redirect('incomes/revenues'); } + $revenue->recurring()->delete(); $revenue->delete(); $message = trans('messages.success.deleted', ['type' => trans_choice('general.revenues', 1)]); diff --git a/app/Models/Common/Recurring.php b/app/Models/Common/Recurring.php index 925e9bdc6..81f4529ed 100644 --- a/app/Models/Common/Recurring.php +++ b/app/Models/Common/Recurring.php @@ -23,7 +23,7 @@ class Recurring extends Model /** - * Get all of the owning recurrable models. + * Get all of the owning recurable models. */ public function recurable() { diff --git a/app/Models/Expense/Bill.php b/app/Models/Expense/Bill.php index dba2b8044..06338223f 100644 --- a/app/Models/Expense/Bill.php +++ b/app/Models/Expense/Bill.php @@ -30,7 +30,7 @@ class Bill extends Model * * @var array */ - protected $fillable = ['company_id', 'bill_number', 'order_number', 'bill_status_code', 'billed_at', 'due_at', 'amount', 'currency_code', 'currency_rate', 'vendor_id', 'vendor_name', 'vendor_email', 'vendor_tax_number', 'vendor_phone', 'vendor_address', 'notes', 'category_id']; + protected $fillable = ['company_id', 'bill_number', 'order_number', 'bill_status_code', 'billed_at', 'due_at', 'amount', 'currency_code', 'currency_rate', 'vendor_id', 'vendor_name', 'vendor_email', 'vendor_tax_number', 'vendor_phone', 'vendor_address', 'notes', 'category_id', 'parent_id']; /** * Sortable columns. @@ -59,7 +59,7 @@ class Bill extends Model * * @var array */ - protected $cloneable_relations = ['items', 'recurring', 'totals']; + public $cloneable_relations = ['items', 'recurring', 'totals']; public function category() { diff --git a/app/Models/Expense/Payment.php b/app/Models/Expense/Payment.php index d179d5854..3924e4d8a 100644 --- a/app/Models/Expense/Payment.php +++ b/app/Models/Expense/Payment.php @@ -24,7 +24,7 @@ class Payment extends Model * * @var array */ - protected $fillable = ['company_id', 'account_id', 'paid_at', 'amount', 'currency_code', 'currency_rate', 'vendor_id', 'description', 'category_id', 'payment_method', 'reference']; + protected $fillable = ['company_id', 'account_id', 'paid_at', 'amount', 'currency_code', 'currency_rate', 'vendor_id', 'description', 'category_id', 'payment_method', 'reference', 'parent_id']; /** * Sortable columns. @@ -50,7 +50,7 @@ class Payment extends Model * * @var array */ - protected $cloneable_relations = ['recurring']; + public $cloneable_relations = ['recurring']; public function account() { diff --git a/app/Models/Income/Invoice.php b/app/Models/Income/Invoice.php index cbbe8a094..9a1588fea 100644 --- a/app/Models/Income/Invoice.php +++ b/app/Models/Income/Invoice.php @@ -31,7 +31,7 @@ class Invoice extends Model * * @var array */ - protected $fillable = ['company_id', 'invoice_number', 'order_number', 'invoice_status_code', 'invoiced_at', 'due_at', 'amount', 'currency_code', 'currency_rate', 'customer_id', 'customer_name', 'customer_email', 'customer_tax_number', 'customer_phone', 'customer_address', 'notes', 'category_id']; + protected $fillable = ['company_id', 'invoice_number', 'order_number', 'invoice_status_code', 'invoiced_at', 'due_at', 'amount', 'currency_code', 'currency_rate', 'customer_id', 'customer_name', 'customer_email', 'customer_tax_number', 'customer_phone', 'customer_address', 'notes', 'category_id', 'parent_id']; /** * Sortable columns. @@ -60,7 +60,7 @@ class Invoice extends Model * * @var array */ - protected $cloneable_relations = ['items', 'recurring', 'totals']; + public $cloneable_relations = ['items', 'recurring', 'totals']; public function category() { diff --git a/app/Models/Income/Revenue.php b/app/Models/Income/Revenue.php index 95c3d008b..db4ced07e 100644 --- a/app/Models/Income/Revenue.php +++ b/app/Models/Income/Revenue.php @@ -24,7 +24,7 @@ class Revenue extends Model * * @var array */ - protected $fillable = ['company_id', 'account_id', 'paid_at', 'amount', 'currency_code', 'currency_rate', 'customer_id', 'description', 'category_id', 'payment_method', 'reference']; + protected $fillable = ['company_id', 'account_id', 'paid_at', 'amount', 'currency_code', 'currency_rate', 'customer_id', 'description', 'category_id', 'payment_method', 'reference', 'parent_id']; /** * Sortable columns. @@ -51,7 +51,7 @@ class Revenue extends Model * * @var array */ - protected $cloneable_relations = ['recurring']; + public $cloneable_relations = ['recurring']; public function user() { diff --git a/database/migrations/2018_04_30_000000_add_parent_column.php b/database/migrations/2018_04_30_000000_add_parent_column.php new file mode 100644 index 000000000..89ba2776a --- /dev/null +++ b/database/migrations/2018_04_30_000000_add_parent_column.php @@ -0,0 +1,54 @@ +integer('parent_id')->default(0); + }); + + Schema::table('revenues', function ($table) { + $table->integer('parent_id')->default(0); + }); + + Schema::table('bills', function ($table) { + $table->integer('parent_id')->default(0); + }); + + Schema::table('payments', function ($table) { + $table->integer('parent_id')->default(0); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('invoices', function ($table) { + $table->dropColumn('parent_id'); + }); + + Schema::table('revenues', function ($table) { + $table->dropColumn('parent_id'); + }); + + Schema::table('bills', function ($table) { + $table->dropColumn('parent_id'); + }); + + Schema::table('payments', function ($table) { + $table->dropColumn('parent_id'); + }); + } +}