-
Notifications
You must be signed in to change notification settings - Fork 10
/
Plugin.php
executable file
·381 lines (325 loc) · 14.6 KB
/
Plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
<?php namespace Octommerce\Octommerce;
use Event;
use Yaml;
use File;
use Backend;
use Currency;
use System\Classes\PluginBase;
use Illuminate\Foundation\AliasLoader;
use Rainlab\User\Models\User;
use Rainlab\Location\Models\State;
use Rainlab\User\Controllers\Users as UsersController;
use Responsiv\Pay\Models\InvoiceStatus;
use Octommerce\Octommerce\Helpers\Cms;
use Octommerce\Octommerce\Classes\OrderManager;
use Octommerce\Octommerce\Classes\ProductManager;
use Octommerce\Octommerce\Models\Brand;
use Octommerce\Octommerce\Models\Order;
use Octommerce\Octommerce\Models\Category;
use Octommerce\Octommerce\Models\Product;
use Octommerce\Octommerce\Models\Settings;
use Octommerce\Octommerce\Models\OrderStatusLog;
/**
* Octommerce Plugin Information File
*/
class Plugin extends PluginBase
{
public $require = ['RainLab.User', 'RainLab.Location', 'Responsiv.Currency', 'Responsiv.Pay'];
public function boot()
{
//
// Extend RainLab.Location
//
State::extend(function($model) {
$model->hasMany['cities'] = [
'Octommerce\Octommerce\Models\City'
];
});
//
// Extend RainLab.User
//
User::extend(function($model) {
$model->addFillable([
'phone',
'country_id',
'state_id',
'city_id',
'address',
'postcode',
]);
$model->hasMany['orders'] = [
'Octommerce\Octommerce\Models\Order',
'order' => 'created_at desc',
];
$model->belongsTo['city'] = 'Octommerce\Octommerce\Models\City';
$model->belongsTo['state'] = 'RainLab\Location\Models\State';
$model->belongsToMany['expect_products'] = [
'Octommerce\Octommerce\Models\Product',
'table' => 'octommerce_octommerce_product_user'
];
$model->addDynamicMethod('getSpendAttribute', function($value) use ($model) {
$total = Order::where('user_id', $model->id)
->whereIn('status_code', ['paid', 'shipped', 'packing', 'delivered'])
->get()
->sum('total');
return Currency::format($total);
});
$model->addDynamicMethod('hasExpectProduct', function($productId) use ($model) {
$user = User::where('id', $model->id)
->whereHas('expect_products', function($query) use ($productId) {
return $query->where('product_id', $productId);
})->first();
return !is_null($user);
});
$model->addDynamicMethod('getTransactionsAttribute', function($value) use ($model) {
return Order::where('user_id', $model->id)
->whereIn('status_code', ['paid', 'shipped', 'packing', 'delivered'])
->get()
->count();
});
$model->addDynamicMethod('getLastTransactionAttribute', function($value) use ($model) {
$order = Order::where('user_id', $model->id)->orderBy('created_at', 'desc')->first();
if ($order)
return $order->created_at->diffForHumans();
});
});
/**
* Add profile fields to backend users
*/
UsersController::extendFormFields(function($form, $model, $context) {
if(!$model instanceof User) return;
$configFile = __DIR__ .'/config/profile_fields.yaml';
$config = Yaml::parse(File::get($configFile));
$form->addTabFields($config);
$form->removeField('surname');
});
UsersController::extend(function($users) {
$users->implement[] = 'Backend.Behaviors.RelationController';
$users->relationConfig = '$/octommerce/octommerce/controllers/users/relationConfig.yaml';
});
State::extend(function($model) {
$model->hasMany['users'] = [
'Rainlab\User\Models\User'
];
$model->hasMany['cities'] = [
'Octommerce\Octommerce\Models\City'
];
});
\System\Controllers\Settings::extend(function($controller) {
// Install CMS pages
$controller->addDynamicMethod('onOctommerceCmsInstallPages', function() {
$cms = Cms::instance();
$cms->install();
});
});
Event::listen('backend.list.extendColumns', function($widget) {
// Only for the User controller
if (!$widget->getController() instanceof \RainLab\User\Controllers\Users) {
return;
}
// Only for the User model
if (!$widget->model instanceof \RainLab\User\Models\User) {
return;
}
// Add an extra birthday column
$widget->addColumns([
'spend' => [
'label' => 'Spend',
'select' => '(select SUM(total) from `octommerce_octommerce_orders` where `octommerce_octommerce_orders`.`user_id` = `users`.`id`
and (
`octommerce_octommerce_orders`.`status_code` = "paid"
or `octommerce_octommerce_orders`.`status_code` = "shipped"
or `octommerce_octommerce_orders`.`status_code` = "packing"
or `octommerce_octommerce_orders`.`status_code` = "delivered"
))'
],
'transactions' => [
'label' => 'Transactions',
'select' => '(select count(*) from `octommerce_octommerce_orders` where `octommerce_octommerce_orders`.`user_id` = `users`.`id`
and (
`octommerce_octommerce_orders`.`status_code` = "paid"
or `octommerce_octommerce_orders`.`status_code` = "shipped"
or `octommerce_octommerce_orders`.`status_code` = "packing"
or `octommerce_octommerce_orders`.`status_code` = "delivered"
))'
],
'last_transaction' => [
'label' => 'Last Transaction',
'select' => '(select created_at from `octommerce_octommerce_orders` where `octommerce_octommerce_orders`.`user_id` = `users`.`id`
order by created_at desc limit 1
)'
]
]);
});
// Global variable for settings
Event::listen('cms.page.beforeDisplay', function($controller, $url, $page) {
$controller->vars['octommerce_settings'] = Settings::instance();
});
//
// Built in Types
//
$productManager = ProductManager::instance();
$productManager->registerTypes([
'Octommerce\Octommerce\ProductTypes\Simple',
'Octommerce\Octommerce\ProductTypes\Variable',
]);
\Octommerce\Octommerce\Controllers\Products::extendFormFields(function($form, $model, $context) use($productManager) {
if (!$model instanceof \Octommerce\Octommerce\Models\Product)
return;
$productManager->addCustomFields($form);
});
/*
* Register menu items for the RainLab.Pages plugin
*/
Event::listen('pages.menuitem.listTypes', function() {
return [
'product-category' => 'Product Category',
'all-product-categories' => 'All Product Categories',
'all-brands' => 'All Brands',
'all-products' => 'All Products',
];
});
Event::listen('pages.menuitem.getTypeInfo', function($type) {
if ($type == 'product-category' || $type == 'all-product-categories')
return Category::getMenuTypeInfo($type);
if ($type == 'all-brands')
return Brand::getMenuTypeInfo($type);
if ($type == 'all-products')
return Product::getMenuTypeInfo($type);
});
Event::listen('pages.menuitem.resolveItem', function($type, $item, $url, $theme) {
if ($type == 'product-category' || $type == 'all-product-categories')
return Category::resolveMenuItem($item, $url, $theme);
if ($type == 'all-brands')
return Brand::resolveMenuItem($item, $url, $theme);
if ($type == 'all-products')
return Product::resolveMenuItem($item, $url, $theme);
});
// Update order status
Event::listen('responsiv.pay.beforeUpdateInvoiceStatus', function($record, $invoice, $statusId, $previousStatus) {
$newStatusCode = null;
$oldStatusCode = InvoiceStatus::find($statusId)->code;
switch($oldStatusCode) {
case 'paid':
case 'approved':
$newStatusCode = 'paid';
break;
case 'void':
$newStatusCode = 'void';
break;
}
if ($newStatusCode) {
if ( ! $invoice->related instanceof \Octommerce\Octommerce\Models\Order) return;
$invoice->related->updateStatus($newStatusCode, $record->comment);
}
});
}
/**
* Register method, called when the plugin is first registered.
*/
public function register()
{
$alias = AliasLoader::getInstance();
$alias->alias('Cart', 'Octommerce\Octommerce\Facades\Cart');
$this->registerConsoleCommand('octommerce:dummy-product', 'Octommerce\Octommerce\Console\DummyProduct');
$this->registerConsoleCommand('octommerce:delete-cart', 'Octommerce\Octommerce\Console\DeleteCart');
}
public function registerComponents()
{
return [
'Octommerce\Octommerce\Components\ProductList' => 'productList',
'Octommerce\Octommerce\Components\ProductDetail' => 'productDetail',
'Octommerce\Octommerce\Components\BrandList' => 'brandList',
'Octommerce\Octommerce\Components\CategoryList' => 'categoryList',
'Octommerce\Octommerce\Components\Cart' => 'cart',
'Octommerce\Octommerce\Components\Checkout' => 'checkout',
'Octommerce\Octommerce\Components\OrderDetail' => 'orderDetail',
'Octommerce\Octommerce\Components\OrderList' => 'orderList',
'Octommerce\Octommerce\Components\OrderTracking' => 'orderTracking',
'Octommerce\Octommerce\Components\Review' => 'review',
'Octommerce\Octommerce\Components\Wishlist' => 'wishlist',
// Will be deprecated
'Octommerce\Octommerce\Components\Account' => 'OctommerceAccount',
];
}
public function registerSettings()
{
return [
'config' => [
'label' => 'Octommerce',
'icon' => 'icon-shopping-cart',
'description' => 'Configure Octommerce plugins.',
'class' => 'Octommerce\Octommerce\Models\Settings',
'permissions' => ['octommerce.octommerce.manage_plugins'],
'order' => 60
],
'holidays' => [
'label' => 'Holidays',
'icon' => 'icon-calendar',
'description' => 'Configure holidays.',
'url' => Backend::url('octommerce/octommerce/holidays'),
'permissions' => ['octommerce.octommerce.manage_plugins'],
'order' => 60
]
];
}
public function registerMarkupTags()
{
return [
];
}
public function registerReportWidgets()
{
return [
'Octommerce\Octommerce\ReportWidgets\Summary' => [
'label' => 'E-commerce Summary',
'context' => 'aa'
]
];
}
public function registerFormWidgets()
{
return [
'Octommerce\Octommerce\FormWidgets\ProductList' => 'oc_productlist',
];
}
public function registerMailTemplates()
{
return [
'octommerce.octommerce::mail.new_order' => 'Confirmation email on new order.',
'octommerce.octommerce::mail.payment_reminder' => 'Reminder email for payment.',
'octommerce.octommerce::mail.paid_order' => 'Paid order notification to customer.',
'octommerce.octommerce::mail.expired_order' => 'Expired order notification to customer.',
'octommerce.octommerce::mail.packing_order' => 'Packing order notification to customer.',
'octommerce.octommerce::mail.shipped_order' => 'Shipping status notification to customer.',
'octommerce.octommerce::mail.delivered_order' => 'Delivery status notification to customer.',
'octommerce.octommerce::mail.cancelled_order' => 'Cancelled order notification to customer.',
'octommerce.octommerce::mail.refunded_order' => 'Refunded order notification to user.',
'octommerce.octommerce::mail.abandoned_cart' => 'Abandoned cart reminder to customer.',
'octommerce.octommerce::mail.product_availibility' => 'Notification when a product is available.',
'octommerce.octommerce::mail.backend_order' => 'Order notification to backend users.',
'octommerce.octommerce::mail.backend_low_stock' => 'Low stock notification to backend users.',
'octommerce.octommerce::mail.forgot_password' => 'Forgot password link',
'octommerce.octommerce::mail.stock_ready' => 'Notify user if product stock ready',
'octommerce.octommerce::mail.admin_new_order' => 'Notify admin there is new order',
'octommerce.octommerce::mail.admin_paid_order' => 'Notify admin if user has been paid the order',
];
}
public function registerSchedule($schedule)
{
$orderManager = OrderManager::instance();
// Check abandoned carts, waiting payments every hour
$schedule->call(function() use($orderManager) {
// Abandoned carts
$orderManager->remindAbandonedCarts();
// Waiting for payments
$orderManager->remindWaitingPayments();
})->hourly();
// Check expired orders every minute
$schedule->call(function() use($orderManager) {
$orderManager->checkExpiredOrders();
})->everyMinute();
// Delete unused carts in a few days
$schedule->command('octommerce:delete-cart')->daily();
}
}