Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 43
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
SendEmailFollowUps
0.00% covered (danger)
0.00%
0 / 43
0.00% covered (danger)
0.00%
0 / 1
56
0.00% covered (danger)
0.00%
0 / 1
 handle
0.00% covered (danger)
0.00%
0 / 43
0.00% covered (danger)
0.00%
0 / 1
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                        $total = $commercialWithPendings[$i]['totalPendingFollowUps'];
48                        $userId = $commercialWithPendings[$i]['userId'];
49                        $commercial = $commercialWithPendings[$i]['commercial'];
50
51                        $getEmailTemplate = TblEmailConfiguration::where('company_id', $item->company_id)
52                            ->where('type', 'followUps')
53                            ->where('cron_default', 1)
54                            ->first();
55
56                        if ($getEmailTemplate) {
57                            if ($total > 0) {
58
59                                $filterModel = [
60                                    "status" => [
61                                        "values" => ["Enviado"],
62                                        "filterType" => "set"
63                                    ],
64                                    "commercial" => [
65                                        "values" => [$commercial],
66                                        "filterType" => "set"
67                                    ]
68                                ];
69
70                                $r = new Request([
71                                    'company_id' => $item->company_id,
72                                    'user_id' => $userId,
73                                    'sortModel' => [],
74                                    'filterModel' => $filterModel,
75                                    'searchText' => '',
76                                    'ids' => [],
77                                    'ids_not_in' => [],
78                                    'email_template_id' => $getEmailTemplate->id,
79                                ]);
80
81                                $followUps = $order->send_email_follow_ups($r, 999999999);
82
83                                Log::channel('cron_send_email_follow_ups')->info($followUps->original);
84                            }
85                        } else {
86                            Log::channel('cron_send_email_follow_ups')->error("No default email template (companyId: {$item->company_id})");
87                        }
88                    }
89                }
90            }
91        } catch (\Exception $e) {
92            Log::channel('cron_send_email_follow_ups')->error($e->getMessage());
93        }
94    }
95}