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\Order; |
8
|
|
|
use App\Model\Product\Product; |
9
|
|
|
use App\User; |
10
|
|
|
use Bugsnag; |
11
|
|
|
use Crypt; |
12
|
|
|
use DateTime; |
13
|
|
|
use DateTimeZone; |
14
|
|
|
|
15
|
|
|
class BaseOrderController extends ExtendedOrderController |
16
|
|
|
{ |
17
|
|
|
public function getEndDate($model) |
18
|
|
|
{ |
19
|
|
|
$end = '--'; |
20
|
|
|
$ends = $model->subscription()->first(); |
21
|
|
|
if ($ends) { |
22
|
|
|
if ($ends->ends_at != '0000-00-00 00:00:00') { |
23
|
|
|
$date1 = new DateTime($ends->ends_at); |
24
|
|
|
$tz = \Auth::user()->timezone()->first()->name; |
25
|
|
|
$date1->setTimezone(new DateTimeZone($tz)); |
26
|
|
|
$end = $date1->format('M j, Y, g:i a '); |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
return $end; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function getUrl($model, $status, $sub) |
34
|
|
|
{ |
35
|
|
|
$url = ''; |
36
|
|
|
if ($status == 'success') { |
37
|
|
|
if ($sub) { |
38
|
|
|
$url = '<a href='.url('renew/'.$sub->id)." |
39
|
|
|
class='btn btn-sm btn-primary btn-xs'><i class='fa fa-refresh' |
40
|
|
|
style='color:white;'> </i> Renew</a>"; |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return '<p><a href='.url('orders/'.$model->id)." |
45
|
|
|
class='btn btn-sm btn-primary btn-xs'><i class='fa fa-eye' |
46
|
|
|
style='color:white;'> </i> View</a> $url</p>"; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* inserting the values to orders table. |
51
|
|
|
* |
52
|
|
|
* @param type $invoiceid |
53
|
|
|
* @param type $order_status |
54
|
|
|
* |
55
|
|
|
* @throws \Exception |
56
|
|
|
* |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
public function executeOrder($invoiceid, $order_status = 'executed') |
60
|
|
|
{ |
61
|
|
|
try { |
62
|
|
|
$invoice_items = $this->invoice_items->where('invoice_id', $invoiceid)->get(); |
63
|
|
|
$user_id = $this->invoice->find($invoiceid)->user_id; |
64
|
|
|
if (count($invoice_items) > 0) { |
65
|
|
|
foreach ($invoice_items as $item) { |
66
|
|
|
if ($item) { |
67
|
|
|
$items = $this->getIfItemPresent($item, $invoiceid, $user_id, $order_status); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return 'success'; |
73
|
|
|
} catch (\Exception $ex) { |
74
|
|
|
dd($ex); |
75
|
|
|
Bugsnag::notifyException($ex); |
76
|
|
|
|
77
|
|
|
throw new \Exception($ex->getMessage()); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function getIfItemPresent($item, $invoiceid, $user_id, $order_status) |
82
|
|
|
{ |
83
|
|
|
|
84
|
|
|
try{ |
85
|
|
|
|
86
|
|
|
$product = $this->product->where('name', $item->product_name)->first()->id; |
87
|
|
|
$version = $this->product->where('name', $item->product_name)->first()->version; |
88
|
|
|
if ($version == null) { |
89
|
|
|
//Get Version from Product Upload Table |
90
|
|
|
$version = $this->product_upload->where('product_id', $product)->pluck('version')->first(); |
91
|
|
|
} |
92
|
|
|
$serial_key = $this->generateSerialKey($product,$item->agents);//Send Product Id and Agents to generate Serial Key |
|
|
|
|
93
|
|
|
$domain = $item->domain; |
94
|
|
|
$plan_id = $this->plan($item->id); |
95
|
|
|
$order = $this->order->create([ |
96
|
|
|
|
97
|
|
|
'invoice_id' => $invoiceid, |
98
|
|
|
'invoice_item_id' => $item->id, |
99
|
|
|
'client' => $user_id, |
100
|
|
|
'order_status' => $order_status, |
101
|
|
|
'serial_key' => Crypt::encrypt($serial_key), |
102
|
|
|
'product' => $product, |
103
|
|
|
'price_override' => $item->subtotal, |
104
|
|
|
'qty' => $item->quantity, |
105
|
|
|
'domain' => $domain, |
106
|
|
|
'number' => $this->generateNumber(), |
107
|
|
|
]); |
108
|
|
|
$this->addOrderInvoiceRelation($invoiceid, $order->id); |
109
|
|
|
$this->addSubscription($order->id, $plan_id, $version, $product, $serial_key); |
110
|
|
|
$this->sendOrderMail($user_id, $order->id, $item->id); |
111
|
|
|
//Update Subscriber To Mailchimp |
112
|
|
|
// $mailchimp = new \App\Http\Controllers\Common\MailChimpController(); |
113
|
|
|
$email = User::where('id', $user_id)->pluck('email')->first(); |
114
|
|
|
// if ($item->subtotal > 0) { |
115
|
|
|
// $r = $mailchimp->updateSubscriberForPaidProduct($email, $product); |
116
|
|
|
// } else { |
117
|
|
|
// $r = $mailchimp->updateSubscriberForFreeProduct($email, $product); |
118
|
|
|
// } |
119
|
|
|
|
120
|
|
|
} catch (\Exception $ex) { |
121
|
|
|
dd($ex); |
122
|
|
|
Bugsnag::notifyException($ex); |
123
|
|
|
app('log')->error($ex->getMessage()); |
124
|
|
|
throw new \Exception('Can not Generate Order'); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
|
128
|
|
|
throw new \Exception('Can not Generate Order'); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* inserting the values to subscription table. |
134
|
|
|
* |
135
|
|
|
* @param int $orderid |
136
|
|
|
* @param int $planid |
137
|
|
|
* @param string $version |
138
|
|
|
* @param int $product |
139
|
|
|
* @param string $serial_key |
140
|
|
|
* |
141
|
|
|
* @throws \Exception |
142
|
|
|
* |
143
|
|
|
* @author Ashutosh Pathak <[email protected]> |
144
|
|
|
*/ |
145
|
|
|
public function addSubscription($orderid, $planid, $version, $product, $serial_key) |
|
|
|
|
146
|
|
|
{ |
147
|
|
|
try { |
148
|
|
|
$permissions = LicensePermissionsController::getPermissionsForProduct($product); |
149
|
|
|
if ($version == null) { |
150
|
|
|
$version = ''; |
151
|
|
|
} |
152
|
|
|
if ($planid != 0) { |
153
|
|
|
$days = $this->plan->where('id', $planid)->first()->days; |
154
|
|
|
$licenseExpiry = $this->getLicenseExpiryDate($permissions['generateLicenseExpiryDate'], $days); |
155
|
|
|
$updatesExpiry = $this->getUpdatesExpiryDate($permissions['generateUpdatesxpiryDate'], $days); |
156
|
|
|
$supportExpiry = $this->getSupportExpiryDate($permissions['generateSupportExpiryDate'], $days); |
157
|
|
|
$user_id = $this->order->find($orderid)->client; |
158
|
|
|
$this->subscription->create(['user_id' => $user_id, |
159
|
|
|
'plan_id' => $planid, 'order_id' => $orderid, 'update_ends_at' =>$updatesExpiry, 'ends_at' => $licenseExpiry, 'support_ends_at'=>$supportExpiry, 'version'=> $version, 'product_id' =>$product, ]); |
|
|
|
|
160
|
|
|
} |
161
|
|
|
$licenseStatus = StatusSetting::pluck('license_status')->first(); |
162
|
|
|
if ($licenseStatus == 1) { |
163
|
|
|
$cont = new \App\Http\Controllers\License\LicenseController(); |
164
|
|
|
$createNewLicense = $cont->createNewLicene($orderid, $product, $user_id, $licenseExpiry, $updatesExpiry, $supportExpiry, $serial_key); |
|
|
|
|
165
|
|
|
} |
166
|
|
|
} catch (\Exception $ex) { |
167
|
|
|
Bugsnag::notifyException($ex); |
168
|
|
|
app('log')->error($ex->getMessage()); |
169
|
|
|
|
170
|
|
|
throw new \Exception('Can not Generate Subscription'); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Get the Expiry Date for License. |
176
|
|
|
* |
177
|
|
|
* @param bool $permissions [Whether Permissons for generating License Expiry Date are there or not] |
178
|
|
|
* @param int $days [No of days that would get addeed to the current date ] |
179
|
|
|
* |
180
|
|
|
* @return string [The final License Expiry date that is generated] |
181
|
|
|
*/ |
182
|
|
|
protected function getLicenseExpiryDate(bool $permissions, int $days) |
183
|
|
|
{ |
184
|
|
|
$ends_at = ''; |
185
|
|
|
if ($days > 0 && $permissions == 1) { |
186
|
|
|
$dt = \Carbon\Carbon::now(); |
187
|
|
|
$ends_at = $dt->addDays($days); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
return $ends_at; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Get the Expiry Date for Updates. |
195
|
|
|
* |
196
|
|
|
* @param bool $permissions [Whether Permissons for generating Updates Expiry Date are there or not] |
197
|
|
|
* @param int $days [No of days that would get added to the current date ] |
198
|
|
|
* |
199
|
|
|
* @return string [The final Updates Expiry date that is generated] |
200
|
|
|
*/ |
201
|
|
|
protected function getUpdatesExpiryDate(bool $permissions, int $days) |
202
|
|
|
{ |
203
|
|
|
$update_ends_at = ''; |
204
|
|
|
if ($days > 0 && $permissions == 1) { |
205
|
|
|
$dt = \Carbon\Carbon::now(); |
206
|
|
|
$update_ends_at = $dt->addDays($days); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
return $update_ends_at; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Get the Expiry Date for Support. |
214
|
|
|
* |
215
|
|
|
* @param bool $permissions [Whether Permissons for generating Updates Expiry Date are there or not] |
216
|
|
|
* @param int $days [No of days that would get added to the current date ] |
217
|
|
|
* |
218
|
|
|
* @return string [The final Suport Expiry date that is generated] |
219
|
|
|
*/ |
220
|
|
|
protected function getSupportExpiryDate(bool $permissions, int $days) |
221
|
|
|
{ |
222
|
|
|
$support_ends_at = ''; |
223
|
|
|
if ($days > 0 && $permissions == 1) { |
224
|
|
|
$dt = \Carbon\Carbon::now(); |
225
|
|
|
$support_ends_at = $dt->addDays($days); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
return $support_ends_at; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
public function addOrderInvoiceRelation($invoiceid, $orderid) |
232
|
|
|
{ |
233
|
|
|
try { |
234
|
|
|
$relation = new \App\Model\Order\OrderInvoiceRelation(); |
235
|
|
|
$relation->create(['order_id' => $orderid, 'invoice_id' => $invoiceid]); |
236
|
|
|
} catch (\Exception $ex) { |
237
|
|
|
Bugsnag::notifyException($ex); |
238
|
|
|
|
239
|
|
|
throw new \Exception($ex->getMessage()); |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
public function sendOrderMail($userid, $orderid, $itemid) |
244
|
|
|
{ |
245
|
|
|
//order |
246
|
|
|
$order = $this->order->find($orderid); |
247
|
|
|
//product |
248
|
|
|
$product = $this->product($itemid); |
249
|
|
|
//user |
250
|
|
|
$productId = Product::where('name', $product)->pluck('id')->first(); |
251
|
|
|
$users = new User(); |
252
|
|
|
$user = $users->find($userid); |
253
|
|
|
//check in the settings |
254
|
|
|
$settings = new \App\Model\Common\Setting(); |
255
|
|
|
$setting = $settings->where('id', 1)->first(); |
256
|
|
|
$orders = new Order(); |
257
|
|
|
$order = $orders->where('id', $orderid)->first(); |
258
|
|
|
$invoice = $this->invoice->find($order->invoice_id); |
259
|
|
|
$number = $invoice->number; |
260
|
|
|
$downloadurl = ''; |
261
|
|
|
if ($user && $order->order_status == 'Executed') { |
262
|
|
|
$downloadurl = url('product/'.'download'.'/'.$productId.'/'.$number); |
263
|
|
|
} |
264
|
|
|
// $downloadurl = $this->downloadUrl($userid, $orderid,$productId); |
265
|
|
|
$myaccounturl = url('my-order/'.$orderid); |
266
|
|
|
$invoiceurl = $this->invoiceUrl($orderid); |
267
|
|
|
//template |
268
|
|
|
$mail = $this->getMail($setting, $user, $downloadurl, $invoiceurl, $order, $product, $orderid, $myaccounturl); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
public function getMail($setting, $user, $downloadurl, $invoiceurl, $order, $product, $orderid, $myaccounturl) |
272
|
|
|
{ |
273
|
|
|
$templates = new \App\Model\Common\Template(); |
274
|
|
|
$temp_id = $setting->order_mail; |
275
|
|
|
$template = $templates->where('id', $temp_id)->first(); |
276
|
|
|
$from = $setting->email; |
277
|
|
|
$to = $user->email; |
278
|
|
|
$subject = $template->name; |
279
|
|
|
$data = $template->data; |
280
|
|
|
$replace = [ |
281
|
|
|
'name' => $user->first_name.' '.$user->last_name, |
282
|
|
|
'serialkeyurl'=> $myaccounturl, |
283
|
|
|
'downloadurl' => $downloadurl, |
284
|
|
|
'invoiceurl' => $invoiceurl, |
285
|
|
|
'product' => $product, |
286
|
|
|
'number' => $order->number, |
287
|
|
|
'expiry' => $this->expiry($orderid), |
288
|
|
|
'url' => $this->renew($orderid), |
289
|
|
|
|
290
|
|
|
]; |
291
|
|
|
$type = ''; |
292
|
|
|
if ($template) { |
293
|
|
|
$type_id = $template->type; |
294
|
|
|
$temp_type = new \App\Model\Common\TemplateType(); |
295
|
|
|
$type = $temp_type->where('id', $type_id)->first()->name; |
296
|
|
|
} |
297
|
|
|
$templateController = new \App\Http\Controllers\Common\TemplateController(); |
298
|
|
|
$mail = $templateController->mailing($from, $to, $data, $subject, $replace, $type); |
299
|
|
|
|
300
|
|
|
return $mail; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
public function invoiceUrl($orderid) |
304
|
|
|
{ |
305
|
|
|
$orders = new Order(); |
306
|
|
|
$order = $orders->where('id', $orderid)->first(); |
307
|
|
|
$invoiceid = $order->invoice_id; |
308
|
|
|
$url = url('my-invoice/'.$invoiceid); |
309
|
|
|
|
310
|
|
|
return $url; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* get the price of a product by id. |
315
|
|
|
* |
316
|
|
|
* @param type $product_id |
317
|
|
|
* |
318
|
|
|
* @throws \Exception |
319
|
|
|
* |
320
|
|
|
* @return type collection |
321
|
|
|
*/ |
322
|
|
|
public function getPrice($product_id) |
323
|
|
|
{ |
324
|
|
|
try { |
325
|
|
|
return $this->price->where('product_id', $product_id)->first(); |
326
|
|
|
} catch (\Exception $ex) { |
327
|
|
|
Bugsnag::notifyException($ex); |
328
|
|
|
|
329
|
|
|
throw new \Exception($ex->getMessage()); |
330
|
|
|
} |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
public function downloadUrl($userid, $orderid) |
334
|
|
|
{ |
335
|
|
|
$orders = new Order(); |
336
|
|
|
$order = $orders->where('id', $orderid)->first(); |
337
|
|
|
$invoice = $this->invoice->find($order->invoice_id); |
338
|
|
|
$number = $invoice->number; |
339
|
|
|
$url = url('download/'.$userid.'/'.$number); |
340
|
|
|
|
341
|
|
|
return $url; |
342
|
|
|
} |
343
|
|
|
} |
344
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.