Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
48.84% covered (danger)
48.84%
21 / 43
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
SendEmailFollowUps
48.84% covered (danger)
48.84%
21 / 43
0.00% covered (danger)
0.00%
0 / 1
13.56
0.00% covered (danger)
0.00%
0 / 1
 handle
48.84% covered (danger)
48.84%
21 / 43
0.00% covered (danger)
0.00%
0 / 1
13.56
1<?php
2
3namespace App\Console\Commands;
4
5use App\Http\Controllers\Companies;
6use App\Http\Controllers\Quotations;
7use App\Http\Controllers\Users;
8use App\Models\TblEmailConfiguration;
9use Illuminate\Console\Command;
10use Illuminate\Http\Request;
11use Illuminate\Support\Facades\Log;
12
13class SendEmailFollowUps extends Command
14{
15    /**
16     * The name and signature of the console command.
17     *
18     * @var string
19     */
20    protected $signature = 'send:follow-ups';
21
22    /**
23     * The console command description.
24     *
25     * @var string
26     */
27    protected $description = 'Send email follow-ups';
28
29    /**
30     * Execute the console command.
31     */
32    public function handle(): void
33    {
34        try {
35
36            $order = new Quotations;
37            $user = new Users;
38            $company = new Companies;
39
40            $listCompanies = $company->list_companies();
41
42            foreach ($listCompanies->original['data'] as $item) {
43                $getAllUsers = $user->get_commercial_with_pendings($item->company_id);
44                if (! empty($getAllUsers->original['commercialWithPendings'])) {
45                    $commercialWithPendings = $getAllUsers->original['commercialWithPendings'];
46                    for ($i = 0; $i < count($commercialWithPendings); $i++) {
47                        // PHP-LARAVEL-1Q: these rows come from
48                        // Users::get_commercial_with_pendings, which builds them
49                        // with DB::select - an array of stdClass, not of arrays.
50                        // Reading them with ['key'] raised "Cannot use object of
51                        // type stdClass as array", an Error rather than an
52                        // Exception, so the catch below never saw it and the
53                        // whole 00:00 cron died on the first company - no
54                        // follow-ups went out for any of them.
55                        $total = $commercialWithPendings[$i]->totalPendingFollowUps;
56                        $userId = $commercialWithPendings[$i]->userId;
57                        $commercial = $commercialWithPendings[$i]->commercial;
58
59                        $getEmailTemplate = TblEmailConfiguration::where('company_id', $item->company_id)
60                            ->where('type', 'followUps')
61                            ->where('cron_default', 1)
62                            ->first();
63
64                        if ($getEmailTemplate) {
65                            if ($total > 0) {
66
67                                $filterModel = [
68                                    'status' => [
69                                        'values' => ['Enviado'],
70                                        'filterType' => 'set',
71                                    ],
72                                    'commercial' => [
73                                        'values' => [$commercial],
74                                        'filterType' => 'set',
75                                    ],
76                                ];
77
78                                $r = new Request([
79                                    'company_id' => $item->company_id,
80                                    'user_id' => $userId,
81                                    'sortModel' => [],
82                                    'filterModel' => $filterModel,
83                                    'searchText' => '',
84                                    'ids' => [],
85                                    'ids_not_in' => [],
86                                    'email_template_id' => $getEmailTemplate->id,
87                                ]);
88
89                                $followUps = $order->send_email_follow_ups($r, 999999999);
90
91                                Log::channel('cron_send_email_follow_ups')->info($followUps->original);
92                            }
93                        } else {
94                            Log::channel('cron_send_email_follow_ups')->error("No default email template (companyId: {$item->company_id})");
95                        }
96                    }
97                }
98            }
99        } catch (\Exception $e) {
100            Log::channel('cron_send_email_follow_ups')->error($e->getMessage());
101        }
102    }
103}