Blame view

app/Console/Commands/RecurringCheck.php 5.39 KB
b7c7a5f6   Alexey Boroda   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
  <?php
  
  namespace App\Console\Commands;
  
  use App\Models\Common\Company;
  use App\Models\Expense\BillHistory;
  use App\Models\Income\InvoiceHistory;
  use App\Notifications\Expense\Bill as BillNotification;
  use App\Notifications\Income\Invoice as InvoiceNotification;
  use App\Traits\Incomes;
  use App\Utilities\Overrider;
  use Date;
  use Illuminate\Console\Command;
  use Recurr\Rule;
  use Recurr\Transformer\ArrayTransformer;
  use Recurr\Transformer\ArrayTransformerConfig;
  
  class RecurringCheck extends Command
  {
      use Incomes;
  
      /**
       * The name and signature of the console command.
       *
       * @var string
       */
      protected $signature = 'recurring:check';
  
      /**
       * The console command description.
       *
       * @var string
       */
      protected $description = 'Check for recurring';
      
      /**
       * Create a new command instance.
       */
      public function __construct()
      {
          parent::__construct();
      }
  
      /**
       * Execute the console command.
       *
       * @return mixed
       */
      public function handle()
      {
          $this->today = Date::today();
  
          // Get all companies
          $companies = Company::all();
  
          foreach ($companies as $company) {
              // Set company id
              session(['company_id' => $company->id]);
  
              // Override settings and currencies
              Overrider::load('settings');
              Overrider::load('currencies');
  
              $company->setSettings();
  
              foreach ($company->recurring as $recur) {
                  if (!$current = $recur->current()) {
                      continue;
                  }
  
                  $current_date = Date::parse($current->format('Y-m-d'));
  
                  // Check if should recur today
                  if ($this->today->ne($current_date)) {
                      continue;
                  }
  
                  $model = $recur->recurable;
  
                  if (!$model) {
                      continue;
                  }
  
                  switch ($recur->recurable_type) {
                      case 'App\Models\Expense\Bill':
                          $this->recurBill($company, $model);
                          break;
                      case 'App\Models\Income\Invoice':
                          $this->recurInvoice($company, $model);
                          break;
                      case 'App\Models\Expense\Payment':
                      case 'App\Models\Income\Revenue':
                          $model->cloneable_relations = [];
  
                          // Create new record
                          $clone = $model->duplicate();
  
                          $clone->parent_id = $model->id;
                          $clone->paid_at = $this->today->format('Y-m-d');
                          $clone->save();
  
                          break;
                  }
              }
          }
  
          // Unset company_id
          session()->forget('company_id');
      }
  
      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));
  
          // Update dates
          $clone->invoiced_at = $this->today->format('Y-m-d');
          $clone->due_at = $this->today->addDays($diff_days)->format('Y-m-d');
          $clone->save();
  
          // Add invoice history
          InvoiceHistory::create([
              'company_id' => session('company_id'),
              'invoice_id' => $clone->id,
              'status_code' => 'draft',
              'notify' => 0,
              'description' => trans('messages.success.added', ['type' => $clone->invoice_number]),
          ]);
  
          // Notify the customer
          if ($clone->customer && !empty($clone->customer_email)) {
              $clone->customer->notify(new InvoiceNotification($clone));
          }
  
          // Notify all users assigned to this company
          foreach ($company->users as $user) {
              if (!$user->can('read-notifications')) {
                  continue;
              }
  
              $user->notify(new InvoiceNotification($clone));
          }
  
          // Update next invoice number
          $this->increaseNextInvoiceNumber();
      }
  
      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));
  
          // Update dates
          $clone->billed_at = $this->today->format('Y-m-d');
          $clone->due_at = $this->today->addDays($diff_days)->format('Y-m-d');
          $clone->save();
  
          // Add bill history
          BillHistory::create([
              'company_id' => session('company_id'),
              'bill_id' => $clone->id,
              'status_code' => 'draft',
              'notify' => 0,
              'description' => trans('messages.success.added', ['type' => $clone->bill_number]),
          ]);
  
          // Notify all users assigned to this company
          foreach ($company->users as $user) {
              if (!$user->can('read-notifications')) {
                  continue;
              }
  
              $user->notify(new BillNotification($clone));
          }
      }
  }