Skip to content

Commit d2202a8

Browse files
Notify goods receipt
1 parent 23a25c8 commit d2202a8

File tree

15 files changed

+851
-2
lines changed

15 files changed

+851
-2
lines changed

components/ShopComponent.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ class ShopComponent extends Component
8383
*/
8484
public $storeCmsContentId;
8585

86-
86+
/**
87+
* @var Кого уведомить о новых товарах
88+
*/
8789
public $notify_emails;
8890

8991

@@ -401,4 +403,5 @@ public function getNotifyEmails()
401403

402404
return $emailsAll;
403405
}
406+
404407
}

config/agents.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,11 @@
1010
[
1111
'description' => \Yii::t('skeeks/shop/app', 'Removing the old price changes'),
1212
'agent_interval' => 3600*24, //раз в 6
13+
],
14+
15+
'shop/notify/quantity-emails' =>
16+
[
17+
'description' => \Yii::t('skeeks/shop/app', 'Notify admission'),
18+
'agent_interval' => 60*10, //раз в 10 минут
1319
]
1420
];
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
<?php
2+
/**
3+
* @author Semenov Alexander <semenov@skeeks.com>
4+
* @link http://skeeks.com/
5+
* @copyright 2010 SkeekS (СкикС)
6+
* @date 22.03.2016
7+
*/
8+
namespace skeeks\cms\shop\console\controllers;
9+
use skeeks\cms\shop\models\ShopCmsContentElement;
10+
use skeeks\cms\shop\models\ShopFuser;
11+
use skeeks\cms\shop\models\ShopProduct;
12+
use skeeks\cms\shop\models\ShopProductPrice;
13+
use skeeks\cms\shop\models\ShopProductPriceChange;
14+
use skeeks\cms\shop\models\ShopProductQuantityChange;
15+
use skeeks\cms\shop\models\ShopQuantityNoticeEmail;
16+
use yii\console\Controller;
17+
use yii\db\Exception;
18+
use yii\db\Expression;
19+
use yii\helpers\Console;
20+
21+
/**
22+
* Class NotifyController
23+
*
24+
* @package skeeks\cms\shop\console\controllers
25+
*/
26+
class NotifyController extends Controller
27+
{
28+
29+
/**
30+
* Просмотр созданных бекапов баз данных
31+
*/
32+
public function actionQuantityEmails()
33+
{
34+
$countEmails = ShopQuantityNoticeEmail::find()
35+
->andWhere(['is_notified' => 0])
36+
->orderBy(['created_at' => SORT_ASC])
37+
->count()
38+
;
39+
40+
if (!$countEmails)
41+
{
42+
$this->stdout("Уведомить некого\n", Console::BOLD);
43+
return;
44+
}
45+
$this->stdout("Количество клиентов для уведомлений: " . $countEmails . "\n", Console::BOLD);
46+
47+
48+
//Самый старый запрос на уведомление, начинаем искать изменения от него
49+
$shopQuantityNoticeEmail = ShopQuantityNoticeEmail::find()
50+
->andWhere(['is_notified' => 0])
51+
->orderBy(['created_at' => SORT_ASC])
52+
->one()
53+
;
54+
55+
$this->stdout("\tСамый старый запрос на уведомление: " . \Yii::$app->formatter->asDatetime($shopQuantityNoticeEmail->created_at) . "\n");
56+
57+
$productIds = ShopQuantityNoticeEmail::find()
58+
->andWhere(['is_notified' => 0])
59+
->orderBy(['>=', 'created_at', $shopQuantityNoticeEmail->created_at])
60+
->orderBy(['created_at' => SORT_ASC])
61+
->groupBy('shop_product_id')
62+
->indexBy('shop_product_id')
63+
->asArray()
64+
->all()
65+
;
66+
67+
if (!$productIds)
68+
{
69+
$this->stdout("\tТоваров не найдено\n");
70+
}
71+
$this->stdout("\tНужно проверить изменения по товарам на которые подписались: " . count($productIds) . "\n");
72+
73+
$productIds = array_keys($productIds);
74+
75+
//Какие товары появлялись в наличии за это время?
76+
$productIds = ShopProductQuantityChange::find()
77+
->andWhere(['shop_product_id' => $productIds ])
78+
->andWhere(['>=', 'created_at', $shopQuantityNoticeEmail->created_at])
79+
->andWhere(['>', 'quantity', 0])
80+
->groupBy('shop_product_id')
81+
->indexBy('shop_product_id')
82+
->asArray()
83+
->all()
84+
;
85+
86+
if (!$productIds)
87+
{
88+
$this->stdout("\tНе было изменений по товарам\n");
89+
}
90+
$productIds = array_keys($productIds);
91+
$this->stdout("\tИзменилось товаров: " . count($productIds) . "\n");
92+
/**
93+
* @var ShopProduct $product
94+
*/
95+
foreach (ShopProduct::find()->where(['id' => $productIds])->each(10) as $product)
96+
{
97+
$this->stdout("\t\tТовар: {$product->cmsContentElement->name}\n");
98+
$this->stdout("\t\tНаличие: {$product->quantity}\n");
99+
100+
if ($product->quantity <= 0)
101+
{
102+
continue;
103+
}
104+
105+
if ($noticeEmails = $product->getShopQuantityNoticeEmails()
106+
->andWhere(['is_notified' => 0])
107+
->groupBy('email')
108+
->orderBy(['created_at' => SORT_DESC])
109+
->all()
110+
)
111+
{
112+
$count = count($noticeEmails);
113+
$this->stdout("\t\tУведомить клиентов: {$count}\n");
114+
/**
115+
* @var ShopQuantityNoticeEmail $noticeEmail
116+
*/
117+
foreach ($noticeEmails as $noticeEmail)
118+
{
119+
$this->stdout("\t\t\tКлиент: {$noticeEmail->email}\n");
120+
$this->_notifyCleint($noticeEmail);
121+
}
122+
}
123+
}
124+
}
125+
126+
protected function _notifyCleint(ShopQuantityNoticeEmail $noticeEmail)
127+
{
128+
if ($noticeEmail->is_notified == 1)
129+
{
130+
$this->stdout("\t\t\tУже уведомлен\n");
131+
return;
132+
}
133+
134+
/*try
135+
{*/
136+
\Yii::$app->mailer->view->theme->pathMap['@app/mail'][] = '@skeeks/cms/shop/mail';
137+
138+
\Yii::$app->mailer->compose('client-quantity-notice', [
139+
'model' => $noticeEmail,
140+
])
141+
->setFrom([\Yii::$app->cms->adminEmail => \Yii::$app->cms->appName . ''])
142+
->setTo($noticeEmail->email)
143+
->setSubject(\Yii::t('skeeks/shop/app', "We've got the goods interesting you") . " (" . $noticeEmail->shopProduct->cmsContentElement->name . ")")
144+
->send();
145+
146+
$noticeEmail->notified_at = \Yii::$app->formatter->asTimestamp(time());
147+
$noticeEmail->is_notified = 1;
148+
$noticeEmail->save();
149+
150+
$this->stdout("\t\t\tУведомлен\n", Console::FG_GREEN);
151+
152+
ShopQuantityNoticeEmail::updateAll([
153+
'is_notified' => 1,
154+
'notified_at' => \Yii::$app->formatter->asTimestamp(time()),
155+
], [
156+
'shop_product_id' => $noticeEmail->shop_product_id,
157+
'email' => $noticeEmail->email,
158+
'is_notified' => 0,
159+
]);
160+
161+
/*} catch (\Exception $e)
162+
{
163+
$this->stdout("\t\t\tEmail не отправлен: {$e->getMessage()}\n", Console::FG_RED);
164+
}*/
165+
}
166+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* @author Semenov Alexander <semenov@skeeks.com>
4+
* @link http://skeeks.com/
5+
* @copyright 2010 SkeekS (СкикС)
6+
* @date 28.08.2015
7+
*/
8+
namespace skeeks\cms\shop\controllers;
9+
10+
use skeeks\cms\components\Cms;
11+
use skeeks\cms\grid\BooleanColumn;
12+
use skeeks\cms\grid\CreatedAtColumn;
13+
use skeeks\cms\grid\CreatedByColumn;
14+
use skeeks\cms\models\CmsAgent;
15+
use skeeks\cms\models\CmsContent;
16+
use skeeks\cms\modules\admin\actions\modelEditor\AdminMultiModelEditAction;
17+
use skeeks\cms\modules\admin\controllers\AdminModelEditorController;
18+
use skeeks\cms\modules\admin\traits\AdminModelEditorStandartControllerTrait;
19+
use skeeks\cms\modules\admin\widgets\AdminImagePreviewWidget;
20+
use skeeks\cms\shop\models\ShopOrderStatus;
21+
use skeeks\cms\shop\models\ShopPersonType;
22+
use skeeks\cms\shop\models\ShopQuantityNoticeEmail;
23+
use skeeks\cms\shop\models\ShopViewedProduct;
24+
use skeeks\cms\shop\widgets\AdminBuyerUserWidget;
25+
use yii\grid\DataColumn;
26+
use yii\helpers\ArrayHelper;
27+
use yii\helpers\Html;
28+
29+
/**
30+
* Class AdminQuantityNoticeEmailController
31+
*
32+
* @package skeeks\cms\shop\controllers
33+
*/
34+
class AdminQuantityNoticeEmailController extends AdminModelEditorController
35+
{
36+
use AdminModelEditorStandartControllerTrait;
37+
38+
public function init()
39+
{
40+
$this->name = \Yii::t('skeeks/shop/app', 'Notification of receipt products by email');
41+
$this->modelShowAttribute = "name";
42+
$this->modelClassName = ShopQuantityNoticeEmail::className();
43+
44+
parent::init();
45+
}
46+
47+
/**
48+
* @inheritdoc
49+
*/
50+
public function actions()
51+
{
52+
return ArrayHelper::merge(parent::actions(),
53+
[
54+
'create' =>
55+
[
56+
'visible' => false
57+
],
58+
59+
'update' =>
60+
[
61+
'visible' => false
62+
],
63+
64+
]
65+
);
66+
}
67+
68+
}

controllers/NotifyController.php

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
/**
3+
* @author Semenov Alexander <semenov@skeeks.com>
4+
* @link http://skeeks.com/
5+
* @copyright 2010 SkeekS (СкикС)
6+
* @date 21.12.2016
7+
*/
8+
namespace skeeks\cms\shop\controllers;
9+
use skeeks\cms\helpers\RequestResponse;
10+
use skeeks\cms\shop\models\ShopQuantityNoticeEmail;
11+
use yii\filters\VerbFilter;
12+
use yii\helpers\Json;
13+
use yii\web\Controller;
14+
15+
/**
16+
* Class NotifyController
17+
*
18+
* @package skeeks\cms\shop\controllers
19+
*/
20+
class NotifyController extends Controller
21+
{
22+
/**
23+
* @inheritdoc
24+
*/
25+
public function behaviors()
26+
{
27+
return [
28+
'verbs' => [
29+
'class' => VerbFilter::className(),
30+
'actions' => [
31+
'*' => ['post'],
32+
],
33+
],
34+
];
35+
}
36+
37+
/**
38+
* @return string
39+
*/
40+
public function actionAdd()
41+
{
42+
$rr = new RequestResponse();
43+
$model = new ShopQuantityNoticeEmail();
44+
45+
if ($rr->isRequestAjaxPost())
46+
{
47+
if ($model->load(\Yii::$app->request->post()) && $model->save())
48+
{
49+
//Notify admins
50+
try
51+
{
52+
if (\Yii::$app->shop->notifyEmails)
53+
{
54+
foreach (\Yii::$app->shop->notifyEmails as $email)
55+
{
56+
\Yii::$app->mailer->view->theme->pathMap['@app/mail'][] = '@skeeks/cms/shop/mail';
57+
58+
\Yii::$app->mailer->compose('notice-added', [
59+
'model' => $model,
60+
'url' => \Yii::$app->request->referrer,
61+
])
62+
->setFrom([\Yii::$app->cms->adminEmail => \Yii::$app->cms->appName . ''])
63+
->setTo($email)
64+
->setSubject(\Yii::$app->cms->appName . ': ' . \Yii::t('skeeks/shop/app', 'Notify admission') .' #' . $model->id)
65+
->send();
66+
}
67+
}
68+
} catch (\Exception $e)
69+
{
70+
\Yii::error($e->getMessage(), static::class);
71+
}
72+
73+
74+
$rr->success = true;
75+
$rr->message = \Yii::t('skeeks/shop/app', 'Your data has been successfully added');
76+
} else
77+
{
78+
$errors = Json::encode($model->firstErrors);
79+
$rr->success = false;
80+
$rr->message = \Yii::t('skeeks/shop/app', 'Please double-check your data') . " :{$errors}";
81+
}
82+
}
83+
84+
return $rr;
85+
}
86+
87+
/**
88+
* @return array
89+
*/
90+
public function actionAddValidate()
91+
{
92+
$rr = new RequestResponse();
93+
$model = new ShopQuantityNoticeEmail();
94+
95+
return $rr->ajaxValidateForm($model);
96+
}
97+
}

0 commit comments

Comments
 (0)