fixed recurring command #315

This commit is contained in:
denisdulici 2018-05-01 19:00:33 +03:00
parent 26d25f41ae
commit 0a5136bd40
11 changed files with 90 additions and 26 deletions

View File

@ -3,13 +3,8 @@
namespace App\Console\Commands; namespace App\Console\Commands;
use App\Models\Company\Company; use App\Models\Company\Company;
use App\Models\Common\Recurring;
use App\Models\Expense\Bill;
use App\Models\Expense\BillHistory; use App\Models\Expense\BillHistory;
use App\Models\Expense\Payment;
use App\Models\Income\Invoice;
use App\Models\Income\InvoiceHistory; use App\Models\Income\InvoiceHistory;
use App\Models\Income\Revenue;
use App\Notifications\Expense\Bill as BillNotification; use App\Notifications\Expense\Bill as BillNotification;
use App\Notifications\Income\Invoice as InvoiceNotification; use App\Notifications\Income\Invoice as InvoiceNotification;
use App\Traits\Incomes; use App\Traits\Incomes;
@ -68,9 +63,7 @@ class RecurringCheck extends Command
$company->setSettings(); $company->setSettings();
$recurring = $company->recurring(); foreach ($company->recurring as $recur) {
foreach ($recurring as $recur) {
$current = Date::parse($recur->schedule()->current()->getStart()->format('Y-m-d')); $current = Date::parse($recur->schedule()->current()->getStart()->format('Y-m-d'));
// Check if should recur today // Check if should recur today
@ -78,29 +71,30 @@ class RecurringCheck extends Command
continue; continue;
} }
$type = end(explode('\\', $recur->recurable_type)); $model = $recur->recurable;
$model = $type::find($recur->recurable_id);
if (!$model) { if (!$model) {
continue; continue;
} }
switch ($type) { switch ($recur->recurable_type) {
case 'Bill': case 'App\Models\Expense\Bill':
$this->recurBill($company, $model); $this->recurBill($company, $model);
break; break;
case 'Invoice': case 'App\Models\Income\Invoice':
$this->recurInvoice($company, $model); $this->recurInvoice($company, $model);
break; break;
case 'Payment': case 'App\Models\Expense\Payment':
case 'Revenue': case 'App\Models\Income\Revenue':
$model->cloneable_relations = [];
// Create new record // Create new record
$clone = $model->duplicate(); $clone = $model->duplicate();
// Update date $clone->parent_id = $model->id;
$clone->paid_at = $this->today->format('Y-m-d'); $clone->paid_at = $this->today->format('Y-m-d');
$clone->save(); $clone->save();
break; break;
} }
} }
@ -112,9 +106,14 @@ class RecurringCheck extends Command
protected function recurInvoice($company, $model) protected function recurInvoice($company, $model)
{ {
$model->cloneable_relations = ['items', 'totals'];
// Create new record // Create new record
$clone = $model->duplicate(); $clone = $model->duplicate();
// Set original invoice id
$clone->parent_id = $model->id;
// Days between invoiced and due date // Days between invoiced and due date
$diff_days = Date::parse($clone->due_at)->diffInDays(Date::parse($clone->invoiced_at)); $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) protected function recurBill($company, $model)
{ {
$model->cloneable_relations = ['items', 'totals'];
// Create new record // Create new record
$clone = $model->duplicate(); $clone = $model->duplicate();
// Set original bill id
$clone->parent_id = $model->id;
// Days between invoiced and due date // Days between invoiced and due date
$diff_days = Date::parse($clone->due_at)->diffInDays(Date::parse($clone->invoiced_at)); $diff_days = Date::parse($clone->due_at)->diffInDays(Date::parse($clone->invoiced_at));

View File

@ -469,6 +469,7 @@ class Bills extends Controller
*/ */
public function destroy(Bill $bill) public function destroy(Bill $bill)
{ {
$bill->recurring()->delete();
$bill->delete(); $bill->delete();
/* /*

View File

@ -222,6 +222,7 @@ class Payments extends Controller
return redirect('expenses/payments'); return redirect('expenses/payments');
} }
$payment->recurring()->delete();
$payment->delete(); $payment->delete();
$message = trans('messages.success.deleted', ['type' => trans_choice('general.payments', 1)]); $message = trans('messages.success.deleted', ['type' => trans_choice('general.payments', 1)]);

View File

@ -490,6 +490,7 @@ class Invoices extends Controller
*/ */
public function destroy(Invoice $invoice) public function destroy(Invoice $invoice)
{ {
$invoice->recurring()->delete();
$invoice->delete(); $invoice->delete();
/* /*

View File

@ -172,6 +172,8 @@ class Revenues extends Controller
$payment_methods = Modules::getPaymentMethods(); $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')); 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'); return redirect('incomes/revenues');
} }
$revenue->recurring()->delete();
$revenue->delete(); $revenue->delete();
$message = trans('messages.success.deleted', ['type' => trans_choice('general.revenues', 1)]); $message = trans('messages.success.deleted', ['type' => trans_choice('general.revenues', 1)]);

View File

@ -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() public function recurable()
{ {

View File

@ -30,7 +30,7 @@ class Bill extends Model
* *
* @var array * @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. * Sortable columns.
@ -59,7 +59,7 @@ class Bill extends Model
* *
* @var array * @var array
*/ */
protected $cloneable_relations = ['items', 'recurring', 'totals']; public $cloneable_relations = ['items', 'recurring', 'totals'];
public function category() public function category()
{ {

View File

@ -24,7 +24,7 @@ class Payment extends Model
* *
* @var array * @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. * Sortable columns.
@ -50,7 +50,7 @@ class Payment extends Model
* *
* @var array * @var array
*/ */
protected $cloneable_relations = ['recurring']; public $cloneable_relations = ['recurring'];
public function account() public function account()
{ {

View File

@ -31,7 +31,7 @@ class Invoice extends Model
* *
* @var array * @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. * Sortable columns.
@ -60,7 +60,7 @@ class Invoice extends Model
* *
* @var array * @var array
*/ */
protected $cloneable_relations = ['items', 'recurring', 'totals']; public $cloneable_relations = ['items', 'recurring', 'totals'];
public function category() public function category()
{ {

View File

@ -24,7 +24,7 @@ class Revenue extends Model
* *
* @var array * @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. * Sortable columns.
@ -51,7 +51,7 @@ class Revenue extends Model
* *
* @var array * @var array
*/ */
protected $cloneable_relations = ['recurring']; public $cloneable_relations = ['recurring'];
public function user() public function user()
{ {

View File

@ -0,0 +1,54 @@
<?php
use Illuminate\Database\Migrations\Migration;
class AddParentColumn extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('invoices', function ($table) {
$table->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');
});
}
}