Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 51
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
UpdateCommercialNumbers
0.00% covered (danger)
0.00%
0 / 51
0.00% covered (danger)
0.00%
0 / 3
182
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 handle
0.00% covered (danger)
0.00%
0 / 23
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     * Create a new command instance.
32     *
33     * @return void
34     */
35    public function __construct()
36    {
37        parent::__construct();
38    }
39
40    /**
41     * Execute the console command.
42     *
43     * @return int
44     */
45    public function handle()
46    {
47        try {
48
49            $companyId = (int) ($this->argument('company_id') ?? 0);
50            $user = $this->argument('commercial') ?? null;
51
52            $where = '';
53
54            if ($companyId != 0) {
55
56                if ($user != null) {
57                    $this->update_commercial_numbers($companyId, $user);
58                } else {
59
60                    $query = "SELECT
61                                DISTINCT commercial
62                            FROM
63                                tbl_quotations
64                            WHERE company_id = {$companyId}
65                            ORDER BY
66                                commercial ASC";
67
68                    $commercials = DB::select($query);
69
70                    for ($i = 0; $i < count($commercials); $i++) {
71                        $this->update_commercial_numbers($companyId, $commercials[$i]->commercial);
72                    }
73                }
74
75            } else {
76
77                $companies = TblCompanies::get();
78
79                foreach ($companies as $item) {
80
81                    $query = "SELECT
82                                DISTINCT commercial
83                            FROM
84                                tbl_quotations
85                            WHERE company_id = {$item->company_id}
86                            ORDER BY
87                                commercial ASC";
88
89                    $commercials = DB::select($query);
90
91                    for ($i = 0; $i < count($commercials); $i++) {
92                        $this->update_commercial_numbers($item->company_id, $commercials[$i]->commercial);
93                    }
94                }
95            }
96
97        } catch (\Exception $e) {
98            Log::channel('update_commercial_numbers')->error($e->getMessage());
99        }
100
101        return 0;
102    }
103
104    public function update_commercial_numbers($companyId, $commercial)
105    {
106
107        $r = new Request([
108            'company_id' => $companyId,
109            'commercial' => $commercial,
110        ]);
111
112        $order = new Quotations;
113        $result = $order->get_total_quotations_by_budget_status($r);
114
115        $totalPendingFollowUps = 0;
116        $totalRequestAndVisit = 0;
117        $totalError = 0;
118        $totalG3WError = 0;
119
120        if (isset($result->original['totalPendingFollowUps'])) {
121            $totalPendingFollowUps = $result->original['totalPendingFollowUps'];
122        }
123
124        if (isset($result->original['totalRequestAndVisit'])) {
125            $totalRequestAndVisit = $result->original['totalRequestAndVisit'];
126        }
127
128        if (isset($result->original['totalError'])) {
129            $totalError = $result->original['totalError'];
130        }
131
132        if (isset($result->original['totalG3WError'])) {
133            $totalG3WError = $result->original['totalG3WError'];
134        }
135
136        TblCompanyUsers::where('user_id', $result->original['userId'])->where('company_id', $companyId)->update(
137            [
138                'total_error' => $totalError,
139                'total_pending_follow_ups' => $totalPendingFollowUps,
140                'total_request_and_visits' => $totalRequestAndVisit,
141                'total_g3w_error' => $totalG3WError,
142            ]
143        );
144
145        Cache::flush();
146    }
147}