Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 49
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
UpdateCommercialNumbers
0.00% covered (danger)
0.00%
0 / 49
0.00% covered (danger)
0.00%
0 / 2
156
0.00% covered (danger)
0.00%
0 / 1
 handle
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
56
 update_commercial_numbers
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2
3namespace App\Console\Commands;
4
5use App\Http\Controllers\Quotations;
6use App\Models\TblCompanies;
7use App\Models\TblCompanyUsers;
8use Illuminate\Console\Command;
9use Illuminate\Http\Request;
10use Illuminate\Support\Facades\Cache;
11use Illuminate\Support\Facades\DB;
12use Illuminate\Support\Facades\Log;
13
14class UpdateCommercialNumbers extends Command
15{
16    /**
17     * The name and signature of the console command.
18     *
19     * @var string
20     */
21    protected $signature = 'update:commercial-numbers {company_id?} {commercial?}';
22
23    /**
24     * The console command description.
25     *
26     * @var string
27     */
28    protected $description = 'This process updates commercial totals ( errors, request, visits and pendings )';
29
30    /**
31     * Execute the console command.
32     */
33    public function handle(): void
34    {
35        try {
36
37            $companyId = (int) ($this->argument('company_id') ?? 0);
38            $user = $this->argument('commercial') ?? null;
39
40            $where = '';
41
42            if ($companyId != 0) {
43
44                if ($user != null) {
45                    $this->update_commercial_numbers($companyId, $user);
46                } else {
47
48                    $query = "SELECT
49                                DISTINCT commercial
50                            FROM
51                                tbl_quotations
52                            WHERE company_id = {$companyId}
53                            ORDER BY
54                                commercial ASC";
55
56                    $commercials = DB::select($query);
57
58                    for ($i = 0; $i < count($commercials); $i++) {
59                        $this->update_commercial_numbers($companyId, $commercials[$i]->commercial);
60                    }
61                }
62
63            } else {
64
65                $companies = TblCompanies::get();
66
67                foreach ($companies as $item) {
68
69                    $query = "SELECT
70                                DISTINCT commercial
71                            FROM
72                                tbl_quotations
73                            WHERE company_id = {$item->company_id}
74                            ORDER BY
75                                commercial ASC";
76
77                    $commercials = DB::select($query);
78
79                    for ($i = 0; $i < count($commercials); $i++) {
80                        $this->update_commercial_numbers($item->company_id, $commercials[$i]->commercial);
81                    }
82                }
83            }
84
85        } catch (\Exception $e) {
86            Log::channel('update_commercial_numbers')->error($e->getMessage());
87        }
88    }
89
90    function update_commercial_numbers($companyId, $commercial): void{
91
92        $r = new Request([
93            'company_id' => $companyId,
94            'commercial' => $commercial,
95        ]);
96
97        $order = new Quotations;
98        $result = $order->get_total_quotations_by_budget_status($r);
99
100        $totalPendingFollowUps = 0;
101        $totalRequestAndVisit = 0;
102        $totalError = 0;
103        $totalG3WError = 0;
104
105        if (isset($result->original['totalPendingFollowUps'])) {
106            $totalPendingFollowUps = $result->original['totalPendingFollowUps'];
107        }
108
109        if (isset($result->original['totalRequestAndVisit'])) {
110            $totalRequestAndVisit = $result->original['totalRequestAndVisit'];
111        }
112
113        if (isset($result->original['totalError'])) {
114            $totalError = $result->original['totalError'];
115        }
116
117        if (isset($result->original['totalG3WError'])) {
118            $totalG3WError = $result->original['totalG3WError'];
119        }
120
121        TblCompanyUsers::where('user_id', $result->original['userId'])->where('company_id', $companyId)->update(
122            [
123                'total_error' => $totalError,
124                'total_pending_follow_ups' => $totalPendingFollowUps,
125                'total_request_and_visits' => $totalRequestAndVisit,
126                'total_g3w_error' => $totalG3WError
127            ]
128        );
129
130        Cache::flush();
131    }
132}