1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Order; |
4
|
|
|
|
5
|
|
|
use App\Http\Controllers\License\LicensePermissionsController; |
6
|
|
|
use App\Model\Common\StatusSetting; |
7
|
|
|
use App\Model\Order\Invoice; |
8
|
|
|
use App\Model\Order\InvoiceItem; |
9
|
|
|
use App\Model\Order\Order; |
10
|
|
|
use App\Model\Payment\Plan; |
11
|
|
|
use App\Model\Product\Product; |
12
|
|
|
use App\Model\Product\Subscription; |
13
|
|
|
use App\Traits\TaxCalculation; |
14
|
|
|
use App\User; |
15
|
|
|
use Carbon\Carbon; |
16
|
|
|
use Exception; |
17
|
|
|
use Illuminate\Http\Request; |
18
|
|
|
use Session; |
19
|
|
|
|
20
|
|
|
class RenewController extends BaseRenewController |
21
|
|
|
{ |
22
|
|
|
use TaxCalculation; |
|
|
|
|
23
|
|
|
|
24
|
|
|
protected $sub; |
25
|
|
|
protected $plan; |
26
|
|
|
protected $order; |
27
|
|
|
protected $invoice; |
28
|
|
|
protected $item; |
29
|
|
|
protected $product; |
30
|
|
|
protected $user; |
31
|
|
|
|
32
|
|
|
public function __construct() |
33
|
|
|
{ |
34
|
|
|
$sub = new Subscription(); |
35
|
|
|
$this->sub = $sub; |
36
|
|
|
|
37
|
|
|
$plan = new Plan(); |
38
|
|
|
$this->plan = $plan; |
39
|
|
|
|
40
|
|
|
$order = new Order(); |
41
|
|
|
$this->order = $order; |
42
|
|
|
|
43
|
|
|
$invoice = new Invoice(); |
44
|
|
|
$this->invoice = $invoice; |
45
|
|
|
|
46
|
|
|
$item = new InvoiceItem(); |
47
|
|
|
$this->item = $item; |
48
|
|
|
|
49
|
|
|
$product = new Product(); |
50
|
|
|
$this->product = $product; |
51
|
|
|
|
52
|
|
|
$user = new User(); |
53
|
|
|
$this->user = $user; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
//Renew From admin panel |
57
|
|
|
public function renewBySubId($id, $planid, $payment_method, $cost, $code) |
58
|
|
|
{ |
59
|
|
|
try { |
60
|
|
|
$plan = $this->plan->find($planid); |
61
|
|
|
$days = $plan->days; |
62
|
|
|
$sub = $this->sub->find($id); |
63
|
|
|
$permissions = LicensePermissionsController::getPermissionsForProduct($sub->product_id); |
64
|
|
|
$licenseExpiry = $this->getExpiryDate($permissions['generateLicenseExpiryDate'], $sub, $days); |
65
|
|
|
$updatesExpiry = $this->getUpdatesExpiryDate($permissions['generateUpdatesxpiryDate'], $sub, $days); |
66
|
|
|
$supportExpiry = $this->getSupportExpiryDate($permissions['generateSupportExpiryDate'], $sub, $days); |
67
|
|
|
$sub->ends_at = $licenseExpiry; |
68
|
|
|
$sub->update_ends_at = $updatesExpiry; |
69
|
|
|
$sub->support_ends_at = $supportExpiry; |
70
|
|
|
$sub->save(); |
71
|
|
|
$licenseStatus = StatusSetting::pluck('license_status')->first(); |
72
|
|
|
if ($licenseStatus == 1) { |
73
|
|
|
$this->editDateInAPL($sub, $updatesExpiry, $licenseExpiry, $supportExpiry); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$this->invoiceBySubscriptionId($id, $planid, $cost); |
77
|
|
|
|
78
|
|
|
return $sub; |
79
|
|
|
} catch (Exception $ex) { |
80
|
|
|
throw new Exception($ex->getMessage()); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
//Renewal from ClienT Panel |
85
|
|
|
public function successRenew($invoice) |
86
|
|
|
{ |
87
|
|
|
try { |
88
|
|
|
$invoice->processing_fee = $invoice->processing_fee; |
89
|
|
|
$invoice->status = 'success'; |
90
|
|
|
$invoice->save(); |
91
|
|
|
|
92
|
|
|
$id = Session::get('subscription_id'); |
93
|
|
|
$planid = Session::get('plan_id'); |
94
|
|
|
$plan = $this->plan->find($planid); |
95
|
|
|
$days = $plan->days; |
96
|
|
|
$sub = $this->sub->find($id); |
97
|
|
|
$permissions = LicensePermissionsController::getPermissionsForProduct($sub->product_id); |
98
|
|
|
$licenseExpiry = $this->getExpiryDate($permissions['generateLicenseExpiryDate'], $sub, $days); |
99
|
|
|
$updatesExpiry = $this->getUpdatesExpiryDate($permissions['generateUpdatesxpiryDate'], $sub, $days); |
100
|
|
|
$supportExpiry = $this->getSupportExpiryDate($permissions['generateSupportExpiryDate'], $sub, $days); |
101
|
|
|
$sub->ends_at = $licenseExpiry; |
102
|
|
|
$sub->update_ends_at = $updatesExpiry; |
103
|
|
|
$sub->support_ends_at = $supportExpiry; |
104
|
|
|
$sub->save(); |
105
|
|
|
|
106
|
|
|
$licenseStatus = StatusSetting::pluck('license_status')->first(); |
107
|
|
|
if ($licenseStatus == 1) { |
108
|
|
|
$this->editDateInAPL($sub, $updatesExpiry, $licenseExpiry, $supportExpiry); |
109
|
|
|
} |
110
|
|
|
$this->removeSession(); |
111
|
|
|
} catch (Exception $ex) { |
112
|
|
|
throw new Exception($ex->getMessage()); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function editDateInAPL($sub, $updatesExpiry, $licenseExpiry, $supportExpiry) |
117
|
|
|
{ |
118
|
|
|
$productId = $sub->product_id; |
119
|
|
|
$domain = $sub->order->domain; |
120
|
|
|
$orderNo = $sub->order->number; |
121
|
|
|
$licenseCode = $sub->order->serial_key; |
122
|
|
|
$expiryDate = $updatesExpiry ? Carbon::parse($updatesExpiry)->format('Y-m-d') : ''; |
123
|
|
|
$licenseExpiry = $licenseExpiry ? Carbon::parse($licenseExpiry)->format('Y-m-d') : ''; |
124
|
|
|
$supportExpiry = $supportExpiry ? Carbon::parse($supportExpiry)->format('Y-m-d') : ''; |
125
|
|
|
$noOfAllowedInstallation = ''; |
126
|
|
|
$getInstallPreference = ''; |
127
|
|
|
$cont = new \App\Http\Controllers\License\LicenseController(); |
128
|
|
|
$noOfAllowedInstallation = $cont->getNoOfAllowedInstallation($licenseCode, $productId); |
129
|
|
|
$getInstallPreference = $cont->getInstallPreference($licenseCode, $productId); |
130
|
|
|
$updateLicensedDomain = $cont->updateExpirationDate($licenseCode, $expiryDate, $productId, $domain, $orderNo, $licenseExpiry, $supportExpiry, $noOfAllowedInstallation, $getInstallPreference); |
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
//Tuesday, June 13, 2017 08:06 AM |
134
|
|
|
|
135
|
|
|
public function getProductById($id) |
136
|
|
|
{ |
137
|
|
|
try { |
138
|
|
|
$product = $this->product->where('id', $id)->first(); |
139
|
|
|
if ($product) { |
140
|
|
|
return $product; |
141
|
|
|
} |
142
|
|
|
} catch (Exception $ex) { |
143
|
|
|
throw new Exception($ex->getMessage()); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function getUserById($id) |
148
|
|
|
{ |
149
|
|
|
try { |
150
|
|
|
$user = $this->user->where('id', $id)->first(); |
151
|
|
|
if ($user) { |
152
|
|
|
return $user; |
153
|
|
|
} |
154
|
|
|
} catch (Exception $ex) { |
155
|
|
|
throw new Exception($ex->getMessage()); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function createOrderInvoiceRelation($orderid, $invoiceid) |
160
|
|
|
{ |
161
|
|
|
try { |
162
|
|
|
$relation = new \App\Model\Order\OrderInvoiceRelation(); |
163
|
|
|
$relation->create([ |
164
|
|
|
'order_id' => $orderid, |
165
|
|
|
'invoice_id' => $invoiceid, |
166
|
|
|
]); |
167
|
|
|
} catch (Exception $ex) { |
168
|
|
|
throw new Exception($ex->getMessage()); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function getPriceByProductId($productid, $userid) |
173
|
|
|
{ |
174
|
|
|
try { |
175
|
|
|
$product = $this->getProductById($productid); |
176
|
|
|
if (! $product) { |
177
|
|
|
throw new Exception('Product has removed from database'); |
178
|
|
|
} |
179
|
|
|
$currency = $this->getUserCurrencyById($userid); |
180
|
|
|
$price = $product->price()->where('currency', $currency)->first(); |
181
|
|
|
if (! $price) { |
182
|
|
|
throw new Exception('Price has removed from database'); |
183
|
|
|
} |
184
|
|
|
$cost = $price->sales_price; |
185
|
|
|
if (! $cost) { |
186
|
|
|
$cost = $price->regular_price; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
return $cost; |
190
|
|
|
} catch (Exception $ex) { |
191
|
|
|
throw new Exception($ex->getMessage()); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
public function getUserCurrencyById($userid) |
196
|
|
|
{ |
197
|
|
|
try { |
198
|
|
|
$user = $this->user->find($userid); |
199
|
|
|
if (! $user) { |
200
|
|
|
throw new Exception('User has removed from database'); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
return $user->currency; |
204
|
|
|
} catch (\Exception $ex) { |
205
|
|
|
throw new \Exception($ex->getMessage()); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
public function tax($product, $cost, $user) |
210
|
|
|
{ |
211
|
|
|
try { |
212
|
|
|
$controller = new \App\Http\Controllers\Order\InvoiceController(); |
213
|
|
|
$tax = $this->calculateTax($product->id, $user->state, $user->country); |
214
|
|
|
$tax_name = $tax->getName(); |
215
|
|
|
$tax_rate = $tax->getValue(); |
216
|
|
|
|
217
|
|
|
$grand_total = $controller->calculateTotal($tax_rate, $cost); |
218
|
|
|
|
219
|
|
|
return rounding($grand_total); |
220
|
|
|
} catch (\Exception $ex) { |
221
|
|
|
throw new \Exception($ex->getMessage()); |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/* |
226
|
|
|
Renew From Admin Panel |
227
|
|
|
*/ |
228
|
|
|
public function renew($id, Request $request) |
229
|
|
|
{ |
230
|
|
|
$this->validate($request, [ |
231
|
|
|
'plan' => 'required', |
232
|
|
|
'payment_method' => 'required', |
233
|
|
|
'cost' => 'required', |
234
|
|
|
'code' => 'exists:promotions,code', |
235
|
|
|
]); |
236
|
|
|
|
237
|
|
|
try { |
238
|
|
|
$planid = $request->input('plan'); |
239
|
|
|
$payment_method = $request->input('payment_method'); |
240
|
|
|
$code = $request->input('code'); |
241
|
|
|
$cost = $request->input('cost'); |
242
|
|
|
$renew = $this->renewBySubId($id, $planid, $payment_method, $cost, $code = ''); |
243
|
|
|
|
244
|
|
|
if ($renew) { |
245
|
|
|
return redirect()->back()->with('success', 'Renewed Successfully'); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
return redirect()->back()->with('fails', 'Can not Process'); |
249
|
|
|
} catch (Exception $ex) { |
250
|
|
|
return redirect()->back()->with('fails', $ex->getMessage()); |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Show the Renew Page from by clicking onRenew in All Orders (Admin Panel). |
256
|
|
|
* |
257
|
|
|
* @param int $id Subscription id for the order |
258
|
|
|
*/ |
259
|
|
|
public function renewForm($id) |
260
|
|
|
{ |
261
|
|
|
try { |
262
|
|
|
$sub = $this->sub->find($id); |
263
|
|
|
$userid = $sub->user_id; |
264
|
|
|
if (User::onlyTrashed()->find($userid)) {//If User is soft deleted for this order |
265
|
|
|
throw new \Exception('The user for this order is suspended from the system. Restore the user to renew.'); |
266
|
|
|
} |
267
|
|
|
$productid = $sub->product_id; |
268
|
|
|
$plans = $this->plan->pluck('name', 'id')->toArray(); |
269
|
|
|
|
270
|
|
|
return view('themes.default1.renew.renew', compact('id', 'productid', 'plans', 'userid')); |
271
|
|
|
} catch (Exception $ex) { |
272
|
|
|
return redirect()->back()->with('fails', $ex->getMessage()); |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
public function renewByClient($id, Request $request) |
277
|
|
|
{ |
278
|
|
|
$userId = Subscription::find($id)->user_id; |
279
|
|
|
if (\Auth::user()->role != 'admin' && $userId != \Auth::user()->id) { |
280
|
|
|
throw new \Exception('Permission denied. Invalid modification of data'); |
281
|
|
|
} |
282
|
|
|
$this->validate($request, [ |
283
|
|
|
'plan' => 'required', |
284
|
|
|
'cost' => 'required', |
285
|
|
|
'code' => 'exists:promotions,code', |
286
|
|
|
]); |
287
|
|
|
|
288
|
|
|
try { |
289
|
|
|
$planid = $request->input('plan'); |
290
|
|
|
$code = $request->input('code'); |
291
|
|
|
$cost = $this->planCost($planid, $request->input('user')); |
292
|
|
|
$items = $this->invoiceBySubscriptionId($id, $planid, $cost); |
293
|
|
|
$invoiceid = $items->invoice_id; |
294
|
|
|
$this->setSession($id, $planid); |
295
|
|
|
|
296
|
|
|
return redirect('paynow/'.$invoiceid); |
297
|
|
|
} catch (\Exception $ex) { |
298
|
|
|
throw new \Exception($ex->getMessage()); |
299
|
|
|
} |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
public function setSession($sub_id, $planid) |
303
|
|
|
{ |
304
|
|
|
Session::put('subscription_id', $sub_id); |
305
|
|
|
Session::put('plan_id', $planid); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
public function removeSession() |
309
|
|
|
{ |
310
|
|
|
Session::forget('subscription_id'); |
311
|
|
|
Session::forget('plan_id'); |
312
|
|
|
Session::forget('invoiceid'); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
public function checkRenew() |
316
|
|
|
{ |
317
|
|
|
$res = false; |
318
|
|
|
if (Session::has('subscription_id') && Session::has('plan_id')) { |
319
|
|
|
$res = true; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
return $res; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
//Update License Expiry Date |
326
|
|
|
public function getExpiryDate($permissions, $sub, $days) |
327
|
|
|
{ |
328
|
|
|
$expiry_date = ''; |
329
|
|
|
if ($days > 0 && $permissions == 1) { |
330
|
|
|
$date = \Carbon\Carbon::parse($sub->ends_at); |
331
|
|
|
$expiry_date = $date->addDays($days); |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
return $expiry_date; |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
//Update Updates Expiry Date |
338
|
|
|
public function getUpdatesExpiryDate($permissions, $sub, $days) |
339
|
|
|
{ |
340
|
|
|
$expiry_date = ''; |
341
|
|
|
if ($days > 0 && $permissions == 1) { |
342
|
|
|
$date = \Carbon\Carbon::parse($sub->update_ends_at); |
343
|
|
|
$expiry_date = $date->addDays($days); |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
return $expiry_date; |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
//Update Support Expiry Date |
350
|
|
|
public function getSupportExpiryDate($permissions, $sub, $days) |
351
|
|
|
{ |
352
|
|
|
$expiry_date = ''; |
353
|
|
|
if ($days > 0 && $permissions == 1) { |
354
|
|
|
$date = \Carbon\Carbon::parse($sub->support_ends_at); |
355
|
|
|
$expiry_date = $date->addDays($days); |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
return $expiry_date; |
359
|
|
|
} |
360
|
|
|
} |
361
|
|
|
|