Skip to content

Commit

Permalink
fixed recurring command #315
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdulici committed May 1, 2018
1 parent 26d25f4 commit 0a5136b
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 26 deletions.
38 changes: 21 additions & 17 deletions app/Console/Commands/RecurringCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -68,39 +63,38 @@ public function handle()

$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
if ($this->today->ne($current)) {
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;
}
}
Expand All @@ -112,9 +106,14 @@ public function handle()

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));

Expand Down Expand Up @@ -152,9 +151,14 @@ protected function recurInvoice($company, $model)

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));

Expand Down
1 change: 1 addition & 0 deletions app/Http/Controllers/Expenses/Bills.php
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,7 @@ public function update(Bill $bill, Request $request)
*/
public function destroy(Bill $bill)
{
$bill->recurring()->delete();
$bill->delete();

/*
Expand Down
1 change: 1 addition & 0 deletions app/Http/Controllers/Expenses/Payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ public function destroy(Payment $payment)
return redirect('expenses/payments');
}

$payment->recurring()->delete();
$payment->delete();

$message = trans('messages.success.deleted', ['type' => trans_choice('general.payments', 1)]);
Expand Down
1 change: 1 addition & 0 deletions app/Http/Controllers/Incomes/Invoices.php
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,7 @@ public function update(Invoice $invoice, Request $request)
*/
public function destroy(Invoice $invoice)
{
$invoice->recurring()->delete();
$invoice->delete();

/*
Expand Down
3 changes: 3 additions & 0 deletions app/Http/Controllers/Incomes/Revenues.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ public function edit(Revenue $revenue)

$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'));
}

Expand Down Expand Up @@ -224,6 +226,7 @@ public function destroy(Revenue $revenue)
return redirect('incomes/revenues');
}

$revenue->recurring()->delete();
$revenue->delete();

$message = trans('messages.success.deleted', ['type' => trans_choice('general.revenues', 1)]);
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Common/Recurring.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
4 changes: 2 additions & 2 deletions app/Models/Expense/Bill.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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()
{
Expand Down
4 changes: 2 additions & 2 deletions app/Models/Expense/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -50,7 +50,7 @@ class Payment extends Model
*
* @var array
*/
protected $cloneable_relations = ['recurring'];
public $cloneable_relations = ['recurring'];

public function account()
{
Expand Down
4 changes: 2 additions & 2 deletions app/Models/Income/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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()
{
Expand Down
4 changes: 2 additions & 2 deletions app/Models/Income/Revenue.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -51,7 +51,7 @@ class Revenue extends Model
*
* @var array
*/
protected $cloneable_relations = ['recurring'];
public $cloneable_relations = ['recurring'];

public function user()
{
Expand Down
54 changes: 54 additions & 0 deletions database/migrations/2018_04_30_000000_add_parent_column.php
Original file line number Diff line number Diff line change
@@ -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');
});
}
}

0 comments on commit 0a5136b

Please sign in to comment.