|
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
|
|
|
try { |
|
84
|
|
|
$product = $this->product->where('name', $item->product_name)->first()->id; |
|
85
|
|
|
$version = $this->product->where('name', $item->product_name)->first()->version; |
|
86
|
|
|
if ($version == null) { |
|
87
|
|
|
//Get Version from Product Upload Table |
|
88
|
|
|
$version = $this->product_upload->where('product_id', $product)->pluck('version')->first(); |
|
89
|
|
|
} |
|
90
|
|
|
$serial_key = $this->generateSerialKey($product, $item->agents); //Send Product Id and Agents to generate Serial Key |
|
|
|
|
|
|
91
|
|
|
$domain = $item->domain; |
|
92
|
|
|
$plan_id = $this->plan($item->id); |
|
93
|
|
|
$order = $this->order->create([ |
|
94
|
|
|
|
|
95
|
|
|
'invoice_id' => $invoiceid, |
|
96
|
|
|
'invoice_item_id' => $item->id, |
|
97
|
|
|
'client' => $user_id, |
|
98
|
|
|
'order_status' => $order_status, |
|
99
|
|
|
'serial_key' => Crypt::encrypt($serial_key), |
|
100
|
|
|
'product' => $product, |
|
101
|
|
|
'price_override' => $item->subtotal, |
|
102
|
|
|
'qty' => $item->quantity, |
|
103
|
|
|
'domain' => $domain, |
|
104
|
|
|
'number' => $this->generateNumber(), |
|
105
|
|
|
]); |
|
106
|
|
|
$this->addOrderInvoiceRelation($invoiceid, $order->id); |
|
107
|
|
|
$this->addSubscription($order->id, $plan_id, $version, $product, $serial_key); |
|
108
|
|
|
$this->sendOrderMail($user_id, $order->id, $item->id); |
|
109
|
|
|
//Update Subscriber To Mailchimp |
|
110
|
|
|
// $mailchimp = new \App\Http\Controllers\Common\MailChimpController(); |
|
111
|
|
|
$email = User::where('id', $user_id)->pluck('email')->first(); |
|
112
|
|
|
// if ($item->subtotal > 0) { |
|
113
|
|
|
// $r = $mailchimp->updateSubscriberForPaidProduct($email, $product); |
|
114
|
|
|
// } else { |
|
115
|
|
|
// $r = $mailchimp->updateSubscriberForFreeProduct($email, $product); |
|
116
|
|
|
// } |
|
117
|
|
|
} catch (\Exception $ex) { |
|
118
|
|
|
Bugsnag::notifyException($ex); |
|
119
|
|
|
app('log')->error($ex->getMessage()); |
|
120
|
|
|
|
|
121
|
|
|
throw new \Exception('Can not Generate Order'); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* inserting the values to subscription table. |
|
127
|
|
|
* |
|
128
|
|
|
* @param int $orderid |
|
129
|
|
|
* @param int $planid |
|
130
|
|
|
* @param string $version |
|
131
|
|
|
* @param int $product |
|
132
|
|
|
* @param string $serial_key |
|
133
|
|
|
* |
|
134
|
|
|
* @throws \Exception |
|
135
|
|
|
* |
|
136
|
|
|
* @author Ashutosh Pathak <[email protected]> |
|
137
|
|
|
*/ |
|
138
|
|
|
public function addSubscription($orderid, $planid, $version, $product, $serial_key) |
|
139
|
|
|
{ |
|
140
|
|
|
try { |
|
141
|
|
|
$permissions = LicensePermissionsController::getPermissionsForProduct($product); |
|
142
|
|
|
if ($version == null) { |
|
143
|
|
|
$version = ''; |
|
144
|
|
|
} |
|
145
|
|
|
if ($planid != 0) { |
|
146
|
|
|
$days = $this->plan->where('id', $planid)->first()->days; |
|
147
|
|
|
$licenseExpiry = $this->getLicenseExpiryDate($permissions['generateLicenseExpiryDate'], $days); |
|
148
|
|
|
$updatesExpiry = $this->getUpdatesExpiryDate($permissions['generateUpdatesxpiryDate'], $days); |
|
149
|
|
|
$supportExpiry = $this->getSupportExpiryDate($permissions['generateSupportExpiryDate'], $days); |
|
150
|
|
|
$user_id = $this->order->find($orderid)->client; |
|
151
|
|
|
$this->subscription->create(['user_id' => $user_id, |
|
152
|
|
|
'plan_id' => $planid, 'order_id' => $orderid, 'update_ends_at' =>$updatesExpiry, 'ends_at' => $licenseExpiry, 'support_ends_at'=>$supportExpiry, 'version'=> $version, 'product_id' =>$product, ]); |
|
|
|
|
|
|
153
|
|
|
} |
|
154
|
|
|
$licenseStatus = StatusSetting::pluck('license_status')->first(); |
|
155
|
|
|
if ($licenseStatus == 1) { |
|
156
|
|
|
$cont = new \App\Http\Controllers\License\LicenseController(); |
|
157
|
|
|
$createNewLicense = $cont->createNewLicene($orderid, $product, $user_id, $licenseExpiry, $updatesExpiry, $supportExpiry, $serial_key); |
|
|
|
|
|
|
158
|
|
|
} |
|
159
|
|
|
} catch (\Exception $ex) { |
|
160
|
|
|
Bugsnag::notifyException($ex); |
|
161
|
|
|
app('log')->error($ex->getMessage()); |
|
162
|
|
|
|
|
163
|
|
|
throw new \Exception('Can not Generate Subscription'); |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Get the Expiry Date for License. |
|
169
|
|
|
* |
|
170
|
|
|
* @param bool $permissions [Whether Permissons for generating License Expiry Date are there or not] |
|
171
|
|
|
* @param int $days [No of days that would get addeed to the current date ] |
|
172
|
|
|
* |
|
173
|
|
|
* @return string [The final License Expiry date that is generated] |
|
174
|
|
|
*/ |
|
175
|
|
|
protected function getLicenseExpiryDate(bool $permissions, int $days) |
|
176
|
|
|
{ |
|
177
|
|
|
$ends_at = ''; |
|
178
|
|
|
if ($days > 0 && $permissions == 1) { |
|
179
|
|
|
$dt = \Carbon\Carbon::now(); |
|
180
|
|
|
$ends_at = $dt->addDays($days); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
return $ends_at; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Get the Expiry Date for Updates. |
|
188
|
|
|
* |
|
189
|
|
|
* @param bool $permissions [Whether Permissons for generating Updates Expiry Date are there or not] |
|
190
|
|
|
* @param int $days [No of days that would get added to the current date ] |
|
191
|
|
|
* |
|
192
|
|
|
* @return string [The final Updates Expiry date that is generated] |
|
193
|
|
|
*/ |
|
194
|
|
|
protected function getUpdatesExpiryDate(bool $permissions, int $days) |
|
195
|
|
|
{ |
|
196
|
|
|
$update_ends_at = ''; |
|
197
|
|
|
if ($days > 0 && $permissions == 1) { |
|
198
|
|
|
$dt = \Carbon\Carbon::now(); |
|
199
|
|
|
$update_ends_at = $dt->addDays($days); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
return $update_ends_at; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Get the Expiry Date for Support. |
|
207
|
|
|
* |
|
208
|
|
|
* @param bool $permissions [Whether Permissons for generating Updates Expiry Date are there or not] |
|
209
|
|
|
* @param int $days [No of days that would get added to the current date ] |
|
210
|
|
|
* |
|
211
|
|
|
* @return string [The final Suport Expiry date that is generated] |
|
212
|
|
|
*/ |
|
213
|
|
|
protected function getSupportExpiryDate(bool $permissions, int $days) |
|
214
|
|
|
{ |
|
215
|
|
|
$support_ends_at = ''; |
|
216
|
|
|
if ($days > 0 && $permissions == 1) { |
|
217
|
|
|
$dt = \Carbon\Carbon::now(); |
|
218
|
|
|
$support_ends_at = $dt->addDays($days); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
return $support_ends_at; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
public function addOrderInvoiceRelation($invoiceid, $orderid) |
|
225
|
|
|
{ |
|
226
|
|
|
try { |
|
227
|
|
|
$relation = new \App\Model\Order\OrderInvoiceRelation(); |
|
228
|
|
|
$relation->create(['order_id' => $orderid, 'invoice_id' => $invoiceid]); |
|
229
|
|
|
} catch (\Exception $ex) { |
|
230
|
|
|
Bugsnag::notifyException($ex); |
|
231
|
|
|
|
|
232
|
|
|
throw new \Exception($ex->getMessage()); |
|
233
|
|
|
} |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
public function sendOrderMail($userid, $orderid, $itemid) |
|
237
|
|
|
{ |
|
238
|
|
|
//order |
|
239
|
|
|
$order = $this->order->find($orderid); |
|
240
|
|
|
//product |
|
241
|
|
|
$product = $this->product($itemid); |
|
|
|
|
|
|
242
|
|
|
//user |
|
243
|
|
|
$productId = Product::where('name', $product)->pluck('id')->first(); |
|
244
|
|
|
$users = new User(); |
|
245
|
|
|
$user = $users->find($userid); |
|
246
|
|
|
//check in the settings |
|
247
|
|
|
$settings = new \App\Model\Common\Setting(); |
|
248
|
|
|
$setting = $settings->where('id', 1)->first(); |
|
249
|
|
|
$orders = new Order(); |
|
250
|
|
|
$order = $orders->where('id', $orderid)->first(); |
|
251
|
|
|
$invoice = $this->invoice->find($order->invoice_id); |
|
252
|
|
|
$number = $invoice->number; |
|
253
|
|
|
$downloadurl = ''; |
|
254
|
|
|
if ($user && $order->order_status == 'Executed') { |
|
255
|
|
|
$downloadurl = url('product/'.'download'.'/'.$productId.'/'.$number); |
|
256
|
|
|
} |
|
257
|
|
|
// $downloadurl = $this->downloadUrl($userid, $orderid,$productId); |
|
258
|
|
|
$myaccounturl = url('my-order/'.$orderid); |
|
259
|
|
|
$invoiceurl = $this->invoiceUrl($orderid); |
|
260
|
|
|
//template |
|
261
|
|
|
$mail = $this->getMail($setting, $user, $downloadurl, $invoiceurl, $order, $product, $orderid, $myaccounturl); |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
public function getMail($setting, $user, $downloadurl, $invoiceurl, $order, $product, $orderid, $myaccounturl) |
|
265
|
|
|
{ |
|
266
|
|
|
$templates = new \App\Model\Common\Template(); |
|
267
|
|
|
$temp_id = $setting->order_mail; |
|
268
|
|
|
$template = $templates->where('id', $temp_id)->first(); |
|
269
|
|
|
$from = $setting->email; |
|
270
|
|
|
$to = $user->email; |
|
271
|
|
|
$subject = $template->name; |
|
272
|
|
|
$data = $template->data; |
|
273
|
|
|
$replace = [ |
|
274
|
|
|
'name' => $user->first_name.' '.$user->last_name, |
|
275
|
|
|
'serialkeyurl'=> $myaccounturl, |
|
276
|
|
|
'downloadurl' => $downloadurl, |
|
277
|
|
|
'invoiceurl' => $invoiceurl, |
|
278
|
|
|
'product' => $product, |
|
279
|
|
|
'number' => $order->number, |
|
280
|
|
|
'expiry' => $this->expiry($orderid), |
|
|
|
|
|
|
281
|
|
|
'url' => $this->renew($orderid), |
|
282
|
|
|
|
|
283
|
|
|
]; |
|
284
|
|
|
$type = ''; |
|
285
|
|
|
if ($template) { |
|
286
|
|
|
$type_id = $template->type; |
|
287
|
|
|
$temp_type = new \App\Model\Common\TemplateType(); |
|
288
|
|
|
$type = $temp_type->where('id', $type_id)->first()->name; |
|
289
|
|
|
} |
|
290
|
|
|
$templateController = new \App\Http\Controllers\Common\TemplateController(); |
|
291
|
|
|
$mail = $templateController->mailing($from, $to, $data, $subject, $replace, $type); |
|
292
|
|
|
|
|
293
|
|
|
return $mail; |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
public function invoiceUrl($orderid) |
|
297
|
|
|
{ |
|
298
|
|
|
$orders = new Order(); |
|
299
|
|
|
$order = $orders->where('id', $orderid)->first(); |
|
300
|
|
|
$invoiceid = $order->invoice_id; |
|
301
|
|
|
$url = url('my-invoice/'.$invoiceid); |
|
302
|
|
|
|
|
303
|
|
|
return $url; |
|
304
|
|
|
} |
|
305
|
|
|
|
|
306
|
|
|
/** |
|
307
|
|
|
* get the price of a product by id. |
|
308
|
|
|
* |
|
309
|
|
|
* @param type $product_id |
|
310
|
|
|
* |
|
311
|
|
|
* @throws \Exception |
|
312
|
|
|
* |
|
313
|
|
|
* @return type collection |
|
314
|
|
|
*/ |
|
315
|
|
|
public function getPrice($product_id) |
|
316
|
|
|
{ |
|
317
|
|
|
try { |
|
318
|
|
|
return $this->price->where('product_id', $product_id)->first(); |
|
319
|
|
|
} catch (\Exception $ex) { |
|
320
|
|
|
Bugsnag::notifyException($ex); |
|
321
|
|
|
|
|
322
|
|
|
throw new \Exception($ex->getMessage()); |
|
323
|
|
|
} |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
|
|
public function downloadUrl($userid, $orderid) |
|
327
|
|
|
{ |
|
328
|
|
|
$orders = new Order(); |
|
329
|
|
|
$order = $orders->where('id', $orderid)->first(); |
|
330
|
|
|
$invoice = $this->invoice->find($order->invoice_id); |
|
331
|
|
|
$number = $invoice->number; |
|
332
|
|
|
$url = url('download/'.$userid.'/'.$number); |
|
333
|
|
|
|
|
334
|
|
|
return $url; |
|
335
|
|
|
} |
|
336
|
|
|
} |
|
337
|
|
|
|
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.