DataMigrationController::migrateExcel()   F
last analyzed

Complexity

Conditions 21
Paths > 20000

Size

Total Lines 150
Code Lines 106

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 150
rs 2
c 0
b 0
f 0
cc 21
eloc 106
nc 21086
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
use DB;
7
use Auth;
8
use Spatie\MediaLibrary\Media;
9
use Carbon\Carbon;
10
use App\Member;
11
use App\Setting;
12
use App\Invoice;
13
use App\Invoice_detail;
14
use App\Payment_detail;
15
use App\Plan;
16
use App\Expense;
17
use App\Enquiry;
18
use App\Followup;
19
use App\Subscription;
20
use App\Sms_log;
21
use App\Http\Requests;
22
use App\Http\Controllers\Controller;
23
use Illuminate\Support\Collection;
24
use Excel;
0 ignored issues
show
Bug introduced by
The type Excel was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
26
class DataMigrationController extends Controller
27
{
28
    public function __construct()
29
    {
30
        $this->middleware('auth');
31
    }
32
33
    public function migrate()
34
    {
35
        DB::beginTransaction();
36
        try {
37
            //From: Invoice_detail->tax To: Invoice->tax
38
            $oldInvoiceDetailsTax = Invoice_detail::where('item_name', '=', 'Taxes')->get();
39
40
            foreach ($oldInvoiceDetailsTax as $taxDetail) {
41
                Invoice::where('id', $taxDetail->invoice_id)->update(['tax' => $taxDetail->item_amount]);
42
            }
43
44
            //From: Invoice_detail->admission To: Invoice->additional_fees
45
            $oldInvoiceDetailsAdmission = Invoice_detail::where('item_name', '=', 'Admission')->get();
46
47
            foreach ($oldInvoiceDetailsAdmission as $admissionDetail) {
48
                Invoice::where('id', $admissionDetail->invoice_id)->update(['additional_fees' => $admissionDetail->item_amount]);
49
            }
50
51
            //Now delete the admission and tax rows from Invoice_details
52
            Invoice_detail::where('item_name', '=', 'Admission')->delete();
53
            Invoice_detail::where('item_name', '=', 'Taxes')->delete();
54
55
            //From: Member->plan_id To: Invoice_detail->plan_id
56
            $invoices = Invoice::all();
57
58
            foreach ($invoices as $invoice) {
59
                Invoice_detail::where('invoice_id', $invoice->id)->update(['plan_id' => $invoice->member->plan_id]);
60
            }
61
62
            DB::commit();
63
64
        } catch (\Exception $e) {
65
            DB::rollback();
66
            dd($e);
67
        }
68
    }
69
70
    public function migrateMedia()
71
    {
72
        DB::beginTransaction();
73
74
        try {
75
            $members = Member::all();
76
77
            foreach ($members as $member) {
78
                $profileImage = base_path('public/assets/img/profile/profile_'.$member->id.'.jpg');
79
                $proofImage = base_path('public/assets/img/proof/proof_'.$member->id.'.jpg');
80
81
                if (file_exists($profileImage)) {
82
                    $member->addMedia($profileImage)->usingFileName('profile_'.$member->id.'.jpg')->toCollection('profile');
83
                }
84
85
                if (file_exists($proofImage)) {
86
                    $member->addMedia($proofImage)->usingFileName('proof_'.$member->id.'.jpg')->toCollection('proof');
87
                }
88
89
                DB::commit();
90
            }
91
        } catch (\Exception $e) {
92
            DB::rollback();
93
            dd($e);
94
        }
95
    }
96
97
    public function migrateExcel()
98
    {
99
        $tax_percent = \Utilities::getSetting('taxes');
100
101
        $lines = Excel::load('import.csv')->get();
102
103
        DB::beginTransaction();
104
105
        try {
106
            foreach ($lines as $line) {
107
108
                $invoiceCounter = \Utilities::getSetting('invoice_last_number') + 1;
109
                $invoicePrefix = \Utilities::getSetting('invoice_prefix');
110
                $invoice_number = $invoicePrefix.$invoiceCounter;
111
112
                $memberCounter = \Utilities::getSetting('member_last_number') + 1;
113
                $memberPrefix = \Utilities::getSetting('member_prefix');
114
                $member_code = $memberPrefix.$memberCounter;
115
116
                $dob = Carbon::createFromFormat('d/m/Y', $line->get('dob'))->toDateString();
117
                $gender = ($line->get('gender') == 'male' ? 'm' : 'f');
118
                // $explodedName = explode(" ", $line->get('name'));
119
                $email = $member_code . "@evolvegym.in";
120
121
                $address = (empty($line->get('address')) ? 'Naigaon east' : $line->get('address'));
122
123
                $member = Member::create(['name'=> $line->get('name'),
124
                                        'DOB'=> $dob,
125
                                        'gender'=> $gender,
126
                                        'contact'=> $line->get('contact'),
127
                                        'emergency_contact'=> $line->get('emergency_contact'),
128
                                        'health_issues'=> $line->get('health_issues'),
129
                                        'email'=> $email,
130
                                        'address'=> $address,
131
                                        'proof_name'=> 'none',
132
                                        'member_code'=> $member_code,
133
                                        'status'=> '1',
134
                                        'pin_code'=> 401208,
135
                                        'occupation'=> '5',
136
                                        'aim'=> '0',
137
                                        'source'=> '0',
138
                                        'created_by'=> Auth::user()->id,
139
                                        'updated_by'=> Auth::user()->id
140
                                    ]);
141
142
                $invoice_total = $line->get('total_amount');
143
                $paymentStatus = \constPaymentStatus::Unpaid;
0 ignored issues
show
Unused Code introduced by
The assignment to $paymentStatus is dead and can be removed.
Loading history...
144
                $pending = empty($line->get('pending_amount')) ? '0' : $line->get('pending_amount');
145
                $discount = empty($line->get('discount_amount')) ? '0' : $line->get('discount_amount');
146
                $payment_amount = $invoice_total - (int) $pending;
147
148
                if ($payment_amount == $invoice_total) {
149
                  $paymentStatus = \constPaymentStatus::Paid;
150
                } 
151
                elseif($payment_amount > 0 && $payment_amount < $invoice_total) {
152
                  $paymentStatus = \constPaymentStatus::Partial;
153
                }
154
                elseif($payment_amount == 0) {
155
                  $paymentStatus = \constPaymentStatus::Unpaid;
156
                }
157
                else {
158
                  $paymentStatus = \constPaymentStatus::Overpaid;
159
                }
160
161
                if(empty($line->discount_percent) && !empty($line->discount_amount)) {
162
                    $discountPercent = 'custom';
163
                }
164
                elseif(empty($line->discount_percent) && empty($line->discount_amount)) {
165
                    $discountPercent = '0';
166
                }
167
                elseif(!empty($line->discount_percent)) {
168
                    $discountPercent = str_replace("%", "", $line->discount_percent);
169
                }
170
171
                $invoice = Invoice::create(['invoice_number'=> $invoice_number,
172
                                             'member_id'=> $member->id,
173
                                             'total'=> $invoice_total,
174
                                             'status'=> $paymentStatus,
175
                                             'pending_amount'=> $pending,
176
                                             'discount_amount'=> $discount,
177
                                             'discount_percent'=> $discountPercent,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $discountPercent does not seem to be defined for all execution paths leading up to this point.
Loading history...
178
                                             'discount_note'=> null,
179
                                             'tax'=> ($tax_percent / 100) * $invoice_total,
180
                                             'additional_fees'=> '0',
181
                                             'note'=> null,
182
                                             'created_by'=> Auth::user()->id,
183
                                             'updated_by'=> Auth::user()->id
184
                                            ]);
185
186
                $start_date = Carbon::createFromFormat('d/m/Y', $line->get('start_date'));
187
                $planId = $line->get('plan');
188
189
                if($planId == 3) {
190
                    $end_date = $start_date->copy()->addMonth();
191
                }
192
                elseif($planId == 4) {
193
                    $end_date = $start_date->copy()->addMonths(3);
194
                }
195
                elseif($planId == 5) {
196
                    $end_date = $start_date->copy()->addMonths(6);
197
                }
198
                elseif($planId == 6) {
199
                    $end_date = $start_date->copy()->addMonths(12);
200
                }
201
202
                if($end_date->lt(Carbon::today())) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $end_date does not seem to be defined for all execution paths leading up to this point.
Loading history...
203
                    $subscription_status = \constSubscription::Expired;
204
                }
205
                else {
206
                    $subscription_status = \constSubscription::onGoing;
207
                }
208
209
                $subscription = Subscription::create(['member_id'=> $member->id,
0 ignored issues
show
Unused Code introduced by
The assignment to $subscription is dead and can be removed.
Loading history...
210
                                                    'invoice_id'=> $invoice->id,
211
                                                    'plan_id'=> (int) $planId,
212
                                                    'start_date'=> $start_date->toDateString(),
213
                                                    'end_date'=> $end_date->toDateString(),
214
                                                    'status'=> $subscription_status,
215
                                                    'is_renewal'=>'0',
216
                                                    'created_by'=> Auth::user()->id,
217
                                                    'updated_by'=> Auth::user()->id
218
                                                    ]);
219
220
                $invoiceDetail = Invoice_detail::create(['invoice_id'=> $invoice->id,
0 ignored issues
show
Unused Code introduced by
The assignment to $invoiceDetail is dead and can be removed.
Loading history...
221
                                                       'plan_id'=> (int) $planId,
222
                                                       'item_amount'=> $line->get('total_amount'),
223
                                                       'item_amount'=> $line->get('total_amount'),
224
                                                       'created_by'=> Auth::user()->id,
225
                                                       'updated_by'=> Auth::user()->id
226
                                                        ]);
227
228
                $paymentDetail = Payment_detail::create(['invoice_id'=> $invoice->id,
0 ignored issues
show
Unused Code introduced by
The assignment to $paymentDetail is dead and can be removed.
Loading history...
229
                                                         'payment_amount'=> $payment_amount,
230
                                                         'mode'=> '1',
231
                                                         'note'=> ' ',
232
                                                         'created_by'=> Auth::user()->id,
233
                                                         'updated_by'=> Auth::user()->id
234
                                                        ]);
235
236
                Setting::where('key', '=','invoice_last_number')->update(['value' => $invoiceCounter]);
237
                Setting::where('key', '=','member_last_number')->update(['value' => $memberCounter]);
238
            }
239
            DB::commit();
240
        }
241
242
        catch(\Exception $e) {
243
            DB::rollback();
244
            dd($e);
245
        }
246
        echo "Ho gaya bc";
247
    }
248
}
249