1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Console\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
use App\Expense; |
7
|
|
|
use Carbon\Carbon; |
8
|
|
|
|
9
|
|
|
class RepeatExpense extends Command |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* The name and signature of the console command. |
13
|
|
|
* |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $signature = 'repeat:expense'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The console command description. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $description = 'Checks repeat interval of expenses and creates new record'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Create a new command instance. |
27
|
|
|
* |
28
|
|
|
* @return void |
29
|
|
|
*/ |
30
|
|
|
public function __construct() |
31
|
|
|
{ |
32
|
|
|
parent::__construct(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Execute the console command. |
37
|
|
|
* |
38
|
|
|
* @return mixed |
39
|
|
|
*/ |
40
|
|
|
public function handle() |
41
|
|
|
{ |
42
|
|
|
$expenses = Expense::where('due_date','=',Carbon::today()->format('Y-m-d'))->get(); |
43
|
|
|
|
44
|
|
|
foreach($expenses as $expense) |
45
|
|
|
{ |
46
|
|
|
if ($expense->repeat == 1) |
47
|
|
|
{ |
48
|
|
|
$expenseData = array('name' => $expense->name, |
49
|
|
|
'category_id' => $expense->category_id, |
50
|
|
|
'due_date' => $expense->due_date->addDays(1), |
51
|
|
|
'repeat' => $expense->repeat, |
52
|
|
|
'note' => $expense->note, |
53
|
|
|
'amount' => $expense->amount, |
54
|
|
|
'paid' => 0, |
55
|
|
|
'created_by' => 1, |
56
|
|
|
'updated_by' => 1); |
57
|
|
|
|
58
|
|
|
$newExpense = new Expense($expenseData); |
59
|
|
|
$newExpense->save(); |
60
|
|
|
} |
61
|
|
|
elseif ($expense->repeat == 2) |
62
|
|
|
{ |
63
|
|
|
$expenseData = array('name' => $expense->name, |
64
|
|
|
'category_id' => $expense->category_id, |
65
|
|
|
'due_date' => $expense->due_date->addWeek(), |
66
|
|
|
'repeat' => $expense->repeat, |
67
|
|
|
'note' => $expense->note, |
68
|
|
|
'amount' => $expense->amount, |
69
|
|
|
'paid' => 0, |
70
|
|
|
'created_by' => 1, |
71
|
|
|
'updated_by' => 1); |
72
|
|
|
|
73
|
|
|
$newExpense = new Expense($expenseData); |
74
|
|
|
$newExpense->save(); |
75
|
|
|
} |
76
|
|
|
elseif ($expense->repeat == 3) |
77
|
|
|
{ |
78
|
|
|
$expenseData = array('name' => $expense->name, |
79
|
|
|
'category_id' => $expense->category_id, |
80
|
|
|
'due_date' => $expense->due_date->addMonth(), |
81
|
|
|
'repeat' => $expense->repeat, |
82
|
|
|
'note' => $expense->note, |
83
|
|
|
'amount' => $expense->amount, |
84
|
|
|
'paid' => 0, |
85
|
|
|
'created_by' => 1, |
86
|
|
|
'updated_by' => 1); |
87
|
|
|
|
88
|
|
|
$newExpense = new Expense($expenseData); |
89
|
|
|
$newExpense->save(); |
90
|
|
|
} |
91
|
|
|
elseif ($expense->repeat == 4) |
92
|
|
|
{ |
93
|
|
|
$expenseData = array('name' => $expense->name, |
94
|
|
|
'category_id' => $expense->category_id, |
95
|
|
|
'due_date' => $expense->due_date->addYear(), |
96
|
|
|
'repeat' => $expense->repeat, |
97
|
|
|
'note' => $expense->note, |
98
|
|
|
'amount' => $expense->amount, |
99
|
|
|
'paid' => 0, |
100
|
|
|
'created_by' => 1, |
101
|
|
|
'updated_by' => 1); |
102
|
|
|
|
103
|
|
|
$newExpense = new Expense($expenseData); |
104
|
|
|
$newExpense->save(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|