1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use App\Model\Common\Country; |
4
|
|
|
use App\Model\Common\Setting; |
5
|
|
|
use App\Model\Payment\TaxByState; |
6
|
|
|
use App\Model\Product\ProductUpload; |
7
|
|
|
use App\Traits\TaxCalculation; |
8
|
|
|
use Carbon\Carbon; |
9
|
|
|
|
10
|
|
|
function getLocation() |
11
|
|
|
{ |
12
|
|
|
try { |
13
|
|
|
$location = \GeoIP::getLocation(); |
14
|
|
|
|
15
|
|
|
return $location; |
16
|
|
|
} catch (Exception $ex) { |
17
|
|
|
app('log')->error($ex->getMessage()); |
18
|
|
|
$location = \Config::get('geoip.default_location'); |
19
|
|
|
|
20
|
|
|
return $location; |
21
|
|
|
} |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
function checkArray($key, $array) |
25
|
|
|
{ |
26
|
|
|
$value = ''; |
27
|
|
|
if (is_array($array) && array_key_exists($key, $array)) { |
28
|
|
|
$value = $array[$key]; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
return $value; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
function mime($type) |
35
|
|
|
{ |
36
|
|
|
if ($type == 'jpg' || |
37
|
|
|
$type == 'png' || |
38
|
|
|
$type == 'jpeg' || |
39
|
|
|
$type == 'gif' || |
40
|
|
|
starts_with($type, 'image')) { |
41
|
|
|
return 'image'; |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
function isInstall() |
46
|
|
|
{ |
47
|
|
|
$check = false; |
48
|
|
|
$env = base_path('.env'); |
49
|
|
|
if (\File::exists($env) && env('DB_INSTALL') == 1) { |
50
|
|
|
$check = true; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $check; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
// For API response |
57
|
|
|
/** |
58
|
|
|
* Format the error message into json error response. |
59
|
|
|
* |
60
|
|
|
* @param string|array $message Error message |
61
|
|
|
* @param int $statusCode |
62
|
|
|
* |
63
|
|
|
* @return HTTP json response |
64
|
|
|
*/ |
65
|
|
|
function errorResponse($message, $statusCode = 400) |
66
|
|
|
{ |
67
|
|
|
return response()->json(['success' => false, 'message' => $message], $statusCode); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Format success message/data into json success response. |
72
|
|
|
* |
73
|
|
|
* @param string $message Success message |
74
|
|
|
* @param array|string $data Data of the response |
75
|
|
|
* @param int $statusCode |
76
|
|
|
* |
77
|
|
|
* @return HTTP json response |
78
|
|
|
*/ |
79
|
|
|
function successResponse($message = '', $data = '', $statusCode = 200) |
80
|
|
|
{ |
81
|
|
|
$response = ['success' => true]; |
82
|
|
|
|
83
|
|
|
// if message given |
84
|
|
|
if (! empty($message)) { |
85
|
|
|
$response['message'] = $message; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// If data given |
89
|
|
|
if (! empty($data)) { |
90
|
|
|
$response['data'] = $data; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return response()->json($response, $statusCode); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Gets time in logged in user's timezone. |
98
|
|
|
* @param string $dateTimeString |
99
|
|
|
* @param string $format |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
|
|
function getTimeInLoggedInUserTimeZone(string $dateTimeString, $format = 'M j, Y, g:i a') |
103
|
|
|
{ |
104
|
|
|
// caching for 4 seconds so for consecutive queries, it will be readily available. And even if someone updates their |
105
|
|
|
// timezone, it will start showing the new timezone after 4 seconds |
106
|
|
|
$timezone = Cache::remember('timezone_'.Auth::user()->id, 5, function () { |
107
|
|
|
return Auth::user()->timezone->name; |
108
|
|
|
}); |
109
|
|
|
|
110
|
|
|
return ((new DateTime($dateTimeString))->setTimezone(new DateTimeZone($timezone)))->format($format); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Gets date in a formatted HTML. |
115
|
|
|
* @param string|null $dateTimeString |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
|
|
function getDateHtml(string $dateTimeString = null) |
119
|
|
|
{ |
120
|
|
|
try { |
121
|
|
|
if (! $dateTimeString) { |
122
|
|
|
return '--'; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$date = getTimeInLoggedInUserTimeZone($dateTimeString, 'M j, Y'); |
126
|
|
|
$dateTime = getTimeInLoggedInUserTimeZone($dateTimeString); |
127
|
|
|
|
128
|
|
|
return "<label data-toggle='tooltip' style='font-weight:500;' data-placement='top' title='".$dateTime."'>".$date.'</label>'; |
129
|
|
|
} catch (Exception $e) { |
130
|
|
|
return '--'; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
function getExpiryLabel($expiryDate, $badge = 'badge') |
135
|
|
|
{ |
136
|
|
|
if ($expiryDate < (new Carbon())->toDateTimeString()) { |
137
|
|
|
return getDateHtml($expiryDate).' <span class="'.$badge.' '.$badge.'-danger" <label data-toggle="tooltip" style="font-weight:500;" data-placement="top" title="Order has Expired"> |
138
|
|
|
|
139
|
|
|
</label> |
140
|
|
|
Expired</span>'; |
141
|
|
|
} else { |
142
|
|
|
return getDateHtml($expiryDate); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
function getVersionAndLabel($productVersion, $productId, $badge = 'label') |
147
|
|
|
{ |
148
|
|
|
$latestVersion = \Cache::remember('latest_'.$productId, 10, function () use ($productId) { |
149
|
|
|
return ProductUpload::where('product_id', $productId)->latest()->value('version'); |
150
|
|
|
}); |
151
|
|
|
if ($productVersion) { |
152
|
|
|
if ($productVersion < $latestVersion) { |
153
|
|
|
return '<span class='.'"'.$badge.' '.$badge.'-warning" <label data-toggle="tooltip" style="font-weight:500;" data-placement="top" title="Outdated Version"> |
154
|
|
|
</label>'.$productVersion.'</span>'; |
155
|
|
|
} else { |
156
|
|
|
return '<span class='.'"'.$badge.' '.$badge.'-success" <label data-toggle="tooltip" style="font-weight:500;" data-placement="top" title="Latest Version"> |
157
|
|
|
</label>'.$productVersion.'</span>'; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
function tooltip($tootipText = '') |
163
|
|
|
{ |
164
|
|
|
return '<label data-toggle="tooltip" style="font-weight:500;" data-placement="top" title='.$tootipText.'> |
165
|
|
|
</label>'; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
function getStatusLabel($status, $badge = 'badge') |
169
|
|
|
{ |
170
|
|
|
switch ($status) { |
171
|
|
|
case 'Success': |
172
|
|
|
return '<span class='.'"'.$badge.' '.$badge.'-success">Paid</span>'; |
173
|
|
|
|
174
|
|
|
case 'Pending': |
175
|
|
|
return '<span class='.'"'.$badge.' '.$badge.'-danger">Unpaid</span>'; |
176
|
|
|
|
177
|
|
|
case 'renewed': |
178
|
|
|
return '<span class='.'"'.$badge.' '.$badge.'-primary">Renewed</span>'; |
179
|
|
|
|
180
|
|
|
default: |
181
|
|
|
return '<span class='.'"'.$badge.' '.$badge.'-warning">Partially paid</span>'; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
function getCountryByCode($code) |
186
|
|
|
{ |
187
|
|
|
try { |
188
|
|
|
$country = \App\Model\Common\Country::where('country_code_char2', $code)->first(); |
189
|
|
|
if ($country) { |
190
|
|
|
return $country->nicename; |
191
|
|
|
} |
192
|
|
|
} catch (\Exception $ex) { |
193
|
|
|
throw new \Exception($ex->getMessage()); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
function findCountryByGeoip($iso) |
198
|
|
|
{ |
199
|
|
|
try { |
200
|
|
|
$country = \App\Model\Common\Country::where('country_code_char2', $iso)->first(); |
201
|
|
|
if ($country) { |
202
|
|
|
return $country->country_code_char2; |
203
|
|
|
} else { |
204
|
|
|
return ''; |
205
|
|
|
} |
206
|
|
|
} catch (\Exception $ex) { |
207
|
|
|
throw new \Exception($ex->getMessage()); |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
function findStateByRegionId($iso) |
212
|
|
|
{ |
213
|
|
|
try { |
214
|
|
|
$states = \App\Model\Common\State::where('country_code_char2', $iso) |
215
|
|
|
->pluck('state_subdivision_name', 'state_subdivision_code')->toArray(); |
216
|
|
|
|
217
|
|
|
return $states; |
218
|
|
|
} catch (\Exception $ex) { |
219
|
|
|
throw new \Exception($ex->getMessage()); |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
function getTimezoneByName($name) |
224
|
|
|
{ |
225
|
|
|
try { |
226
|
|
|
$timezone = \App\Model\Common\Timezone::where('name', $name)->first(); |
227
|
|
|
if ($timezone) { |
228
|
|
|
$timezone = $timezone->id; |
229
|
|
|
} else { |
230
|
|
|
$timezone = '114'; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
return $timezone; |
234
|
|
|
} catch (\Exception $ex) { |
235
|
|
|
throw new \Exception($ex->getMessage()); |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
function checkPlanSession() |
240
|
|
|
{ |
241
|
|
|
try { |
242
|
|
|
if (Session::has('plan')) { |
243
|
|
|
return true; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
return false; |
247
|
|
|
} catch (\Exception $ex) { |
248
|
|
|
throw new \Exception($ex->getMessage()); |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
function getStateByCode($code) |
253
|
|
|
{ |
254
|
|
|
try { |
255
|
|
|
$result = ['id' => '', 'name' => '']; |
256
|
|
|
|
257
|
|
|
$subregion = \App\Model\Common\State::where('state_subdivision_code', $code)->first(); |
258
|
|
|
if ($subregion) { |
259
|
|
|
$result = ['id' => $subregion->state_subdivision_code, |
260
|
|
|
'name' => $subregion->state_subdivision_name, ]; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
return $result; |
264
|
|
|
} catch (\Exception $ex) { |
265
|
|
|
throw new \Exception($ex->getMessage()); |
266
|
|
|
} |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
function userCurrency($userid = '') |
270
|
|
|
{ |
271
|
|
|
try { |
272
|
|
|
$currency = Setting::find(1)->default_currency; |
273
|
|
|
$currency_symbol = Setting::find(1)->default_symbol; |
274
|
|
|
if (! \Auth::user()) {//When user is not logged in |
275
|
|
|
$location = getLocation(); |
276
|
|
|
$country = findCountryByGeoip($location['iso_code']); |
277
|
|
|
$userCountry = Country::where('country_code_char2', $country)->first(); |
278
|
|
|
$currencyStatus = $userCountry->currency->status; |
279
|
|
|
if ($currencyStatus) { |
280
|
|
|
$currency = $userCountry->currency->code; |
281
|
|
|
$currency_symbol = $userCountry->currency->symbol; |
282
|
|
|
} |
283
|
|
|
} |
284
|
|
|
if (\Auth::user()) { |
285
|
|
|
$currency = \Auth::user()->currency; |
286
|
|
|
$currency_symbol = \Auth::user()->currency_symbol; |
287
|
|
|
} |
288
|
|
|
if ($userid != '') {//For Admin Panel Clients |
289
|
|
|
$currencyAndSymbol = getCurrency($userid); |
290
|
|
|
$currency = $currencyAndSymbol['currency']; |
291
|
|
|
$currency_symbol = $currencyAndSymbol['symbol']; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
return ['currency'=>$currency, 'symbol'=>$currency_symbol]; |
295
|
|
|
} catch (\Exception $ex) { |
296
|
|
|
throw new \Exception($ex->getMessage()); |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/* |
301
|
|
|
* Get Currency And Symbol For Admin Panel Clients |
302
|
|
|
*/ |
303
|
|
|
function getCurrency($userid) |
304
|
|
|
{ |
305
|
|
|
$user = new \App\User(); |
306
|
|
|
$currency = $user->find($userid)->currency; |
307
|
|
|
$symbol = $user->find($userid)->currency_symbol; |
308
|
|
|
|
309
|
|
|
return ['currency'=>$currency, 'symbol'=>$symbol]; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
function currencyFormat($amount = null, $currency = null, $include_symbol = true) |
313
|
|
|
{ |
314
|
|
|
$amount = rounding($amount); |
315
|
|
|
if ($currency == 'INR') { |
316
|
|
|
$symbol = getIndianCurrencySymbol($currency); |
317
|
|
|
|
318
|
|
|
return $symbol.getIndianCurrencyFormat($amount); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
return app('currency')->format($amount, $currency, $include_symbol); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
function rounding($price) |
325
|
|
|
{ |
326
|
|
|
try { |
327
|
|
|
$tax_rule = new \App\Model\Payment\TaxOption(); |
328
|
|
|
$rule = $tax_rule->findOrFail(1); |
329
|
|
|
$rounding = $rule->rounding; |
330
|
|
|
if ($rounding) { |
331
|
|
|
return round($price); |
332
|
|
|
} else { |
333
|
|
|
return round($price, 2); |
334
|
|
|
} |
335
|
|
|
} catch (\Exception $ex) { |
336
|
|
|
Bugsnag::notifyException($ex); |
337
|
|
|
} |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
function getIndianCurrencySymbol($currency) |
341
|
|
|
{ |
342
|
|
|
return \DB::table('format_currencies')->where('code', $currency)->value('symbol'); |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
function getIndianCurrencyFormat($number) |
346
|
|
|
{ |
347
|
|
|
$explrestunits = ''; |
348
|
|
|
$number = explode('.', $number); |
349
|
|
|
$num = $number[0]; |
350
|
|
|
if (strlen($num) > 3) { |
351
|
|
|
$lastthree = substr($num, strlen($num) - 3, strlen($num)); |
352
|
|
|
$restunits = substr($num, 0, strlen($num) - 3); // extracts the last three digits |
353
|
|
|
$restunits = (strlen($restunits) % 2 == 1) ? '0'.$restunits : $restunits; // explodes the remaining digits in 2's formats, adds a zero in the beginning to maintain the 2's grouping. |
354
|
|
|
$expunit = str_split($restunits, 2); |
355
|
|
|
for ($i = 0; $i < sizeof($expunit); $i++) { |
|
|
|
|
356
|
|
|
// creates each of the 2's group and adds a comma to the end |
357
|
|
|
if ($i == 0) { |
358
|
|
|
$explrestunits .= (int) $expunit[$i].','; // if is first value , convert into integer |
359
|
|
|
} else { |
360
|
|
|
$explrestunits .= $expunit[$i].','; |
361
|
|
|
} |
362
|
|
|
} |
363
|
|
|
$thecash = $explrestunits.$lastthree; |
364
|
|
|
} else { |
365
|
|
|
$thecash = $num; |
366
|
|
|
} |
367
|
|
|
if (! empty($number[1])) { |
368
|
|
|
if (strlen($number[1]) == 1) { |
369
|
|
|
return $thecash.'.'.$number[1].'0'; |
370
|
|
|
} elseif (strlen($number[1]) == 2) { |
371
|
|
|
return $thecash.'.'.$number[1]; |
372
|
|
|
} else { |
373
|
|
|
return 'cannot handle decimal values more than two digits...'; |
374
|
|
|
} |
375
|
|
|
} else { |
376
|
|
|
return $thecash; |
377
|
|
|
} |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
function bifurcateTax($taxName, $taxValue, $currency, $state, $price = '') |
381
|
|
|
{ |
382
|
|
|
if (\Auth::user()->country == 'IN') { |
383
|
|
|
$gst = TaxByState::where('state_code', $state)->select('c_gst', 's_gst', 'ut_gst')->first(); |
384
|
|
|
if ($taxName == 'CGST+SGST') { |
385
|
|
|
$html = 'CGST@'.$gst->c_gst.'%<br>SGST@'.$gst->s_gst.'%'; |
386
|
|
|
|
387
|
|
|
$cgst_value = currencyFormat(TaxCalculation::taxValue($gst->c_gst, $price), $currency); |
388
|
|
|
|
389
|
|
|
$sgst_value = currencyFormat(TaxCalculation::taxValue($gst->s_gst, $price), $currency); |
390
|
|
|
|
391
|
|
|
return ['html'=>$html, 'tax'=>$cgst_value.'<br>'.$sgst_value]; |
392
|
|
|
} elseif ($taxName == 'CGST+UTGST') { |
393
|
|
|
$html = 'CGST@'.$gst->c_gst.'%<br>UTGST@'.$gst->ut_gst.'%'; |
394
|
|
|
|
395
|
|
|
$cgst_value = currencyFormat(TaxCalculation::taxValue($gst->c_gst, $price), $currency); |
396
|
|
|
$utgst_value = currencyFormat(TaxCalculation::taxValue($gst->ut_gst, $price), $currency); |
397
|
|
|
|
398
|
|
|
return ['html'=>$html, 'tax'=>$cgst_value.'<br>'.$utgst_value]; |
399
|
|
|
} else { |
400
|
|
|
$html = $taxName.'@'.$taxValue; |
401
|
|
|
$tax_value = currencyFormat(TaxCalculation::taxValue($taxValue, $price), $currency); |
402
|
|
|
|
403
|
|
|
return ['html'=>$html, 'tax'=>$tax_value]; |
404
|
|
|
} |
405
|
|
|
} else { |
406
|
|
|
$html = $taxName.'@'.$taxValue; |
407
|
|
|
$tax_value = currencyFormat(TaxCalculation::taxValue($taxValue, $price), $currency); |
408
|
|
|
|
409
|
|
|
return ['html'=>$html, 'tax'=>$tax_value]; |
410
|
|
|
} |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
/** |
414
|
|
|
* sets mail config and reloads the config into the container |
415
|
|
|
* NOTE: this is getting used outside the class to set service config. |
416
|
|
|
* @return void |
417
|
|
|
*/ |
418
|
|
|
function setServiceConfig($emailConfig) |
419
|
|
|
{ |
420
|
|
|
$sendingProtocol = $emailConfig->driver; |
421
|
|
|
if ($sendingProtocol && $sendingProtocol != 'smtp' && $sendingProtocol != 'mail') { |
422
|
|
|
$services = \Config::get("services.$sendingProtocol"); |
423
|
|
|
$dynamicServiceConfig = []; |
424
|
|
|
|
425
|
|
|
//loop over it and assign according to the keys given by user |
426
|
|
|
foreach ($services as $key => $value) { |
427
|
|
|
$dynamicServiceConfig[$key] = isset($emailConfig[$key]) ? $emailConfig[$key] : $value; |
428
|
|
|
} |
429
|
|
|
|
430
|
|
|
//setting that service configuration |
431
|
|
|
\Config::set("services.$sendingProtocol", $dynamicServiceConfig); |
432
|
|
|
} else { |
433
|
|
|
\Config::set('mail.host', $emailConfig['host']); |
434
|
|
|
\Config::set('mail.port', $emailConfig['port']); |
435
|
|
|
\Config::set('mail.password', $emailConfig['password']); |
436
|
|
|
\Config::set('mail.security', $emailConfig['encryption']); |
437
|
|
|
} |
438
|
|
|
|
439
|
|
|
//setting mail driver as $sending protocol |
440
|
|
|
\Config::set('mail.driver', $sendingProtocol); |
441
|
|
|
\Config::set('mail.from.address', $emailConfig['email']); |
442
|
|
|
\Config::set('mail.from.name', $emailConfig['company']); |
443
|
|
|
\Config::set('mail.username', $emailConfig['email']); |
444
|
|
|
|
445
|
|
|
//setting the config again in the service container |
446
|
|
|
(new \Illuminate\Mail\MailServiceProvider(app()))->register(); |
447
|
|
|
} |
448
|
|
|
|
449
|
|
|
function persistentCache($key, Closure $closure, $noOfSeconds = 30, array $variables = []) |
450
|
|
|
{ |
451
|
|
|
$keySalt = json_encode($variables); |
452
|
|
|
|
453
|
|
|
return Cache::remember($key.$keySalt, $noOfSeconds, $closure); |
454
|
|
|
} |
455
|
|
|
|
456
|
|
|
function emailSendingStatus() |
457
|
|
|
{ |
458
|
|
|
$status = false; |
459
|
|
|
if (Setting::value('sending_status')) { |
460
|
|
|
$status = true; |
461
|
|
|
} |
462
|
|
|
|
463
|
|
|
return $status; |
464
|
|
|
} |
465
|
|
|
|
466
|
|
|
function installationStatusLabel($lastConnectionDate, $createdAt) |
467
|
|
|
{ |
468
|
|
|
return $lastConnectionDate > (new Carbon('-30 days'))->toDateTimeString() && $lastConnectionDate != $createdAt ? " <span class='badge badge-primary' style='background-color:darkcyan !important;' <label data-toggle='tooltip' style='font-weight:500;' data-placement='top' title='Installation is Active'> |
469
|
|
|
</label>Active</span>" : " <span class='badge badge-info' <label data-toggle='tooltip' style='font-weight:500;background-color:crimson;' data-placement='top' title='Installation inactive for more than 30 days'> |
470
|
|
|
</label>Inactive</span>"; |
471
|
|
|
} |
472
|
|
|
|
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: