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