Skip to content

Commit

Permalink
Merge pull request #16 from ThomasDaSilva/master
Browse files Browse the repository at this point in the history
change stock admin alert
  • Loading branch information
julescournut authored Dec 19, 2022
2 parents 868a682 + 61ab436 commit d13711d
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 39 deletions.
37 changes: 37 additions & 0 deletions Command/SendMailStockPse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace StockAlert\Command;

use StockAlert\Model\StockProductAlertQuery;
use StockAlert\Service\adminMailService;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Thelia\Command\ContainerAwareCommand;

class SendMailStockPse extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName("stockalert:adminMail")
->setDescription("send mail with all products with no stock");
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$this->initRequest();
$productIds = StockProductAlertQuery::create()
->select('product_id')
->find();

/** @var adminMailService $adminMailService */
$adminMailService = $this->getContainer()->get('stockalert.alert.service');

$adminMailService->sendEmailForAdmin($productIds);
$output->writeln("StockAlert : Mail envoyé");

StockProductAlertQuery::create()
->deleteAll();
$output->writeln("StockAlert : Table vidé");
}
}
7 changes: 7 additions & 0 deletions Config/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,18 @@
<loop name="restocking-alert" class="StockAlert\Loop\RestockingAlertLoop" />
</loops>

<commands>
<command class="StockAlert\Command\SendMailStockPse" />
</commands>

<services>
<service id="stockalert.alert.manager" class="StockAlert\EventListeners\StockAlertManager" scope="request">
<argument type="service" id="mailer"/>
<tag name="kernel.event_subscriber"/>
</service>
<service id="stockalert.alert.service" class="StockAlert\Service\adminMailService">
<argument type="service" id="mailer"/>
</service>
</services>

<hooks>
Expand Down
2 changes: 1 addition & 1 deletion Config/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<language>en_US</language>
<language>fr_FR</language>
</languages>
<version>1.2.8</version>
<version>1.2.9</version>
<author>
<name>Julien Chanséaume</name>
<email>julien@thelia.net</email>
Expand Down
13 changes: 13 additions & 0 deletions Config/schema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@

</table>

<table name="stock_product_alert" namespace="StockAlert\Model">

<column name="id" primaryKey="true" autoIncrement="true" required="true" type="INTEGER" />
<column name="product_id" required="true" type="INTEGER" />

<foreign-key foreignTable="product" name="fk_stock_pse_alert_product_id" onDelete="CASCADE" >
<reference foreign="id" local="product_id" />
</foreign-key>

<behavior name="timestampable" />

</table>

<external-schema filename="local/config/schema.xml" referenceOnly="true" />

</database>
2 changes: 2 additions & 0 deletions Config/sqldb.map
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Sqlfile -> Database map
thelia.sql=thelia
22 changes: 21 additions & 1 deletion Config/thelia.sql
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,32 @@ CREATE TABLE `restocking_alert`
`created_at` DATETIME,
`updated_at` DATETIME,
PRIMARY KEY (`id`),
INDEX `FI_restocking_alert_product_sale_elements_id` (`product_sale_elements_id`),
INDEX `fi_restocking_alert_product_sale_elements_id` (`product_sale_elements_id`),
CONSTRAINT `fk_restocking_alert_product_sale_elements_id`
FOREIGN KEY (`product_sale_elements_id`)
REFERENCES `product_sale_elements` (`id`)
ON DELETE CASCADE
) ENGINE=InnoDB;

-- ---------------------------------------------------------------------
-- stock_product_alert
-- ---------------------------------------------------------------------

DROP TABLE IF EXISTS `stock_product_alert`;

CREATE TABLE `stock_product_alert`
(
`id` INTEGER NOT NULL AUTO_INCREMENT,
`product_id` INTEGER NOT NULL,
`created_at` DATETIME,
`updated_at` DATETIME,
PRIMARY KEY (`id`),
INDEX `fi_stock_pse_alert_product_id` (`product_id`),
CONSTRAINT `fk_stock_pse_alert_product_id`
FOREIGN KEY (`product_id`)
REFERENCES `product` (`id`)
ON DELETE CASCADE
) ENGINE=InnoDB;

# This restores the fkey checks, after having unset them earlier
SET FOREIGN_KEY_CHECKS = 1;
50 changes: 13 additions & 37 deletions EventListeners/StockAlertManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
use StockAlert\Event\StockAlertEvents;
use StockAlert\Model\RestockingAlert;
use StockAlert\Model\RestockingAlertQuery;
use StockAlert\Model\StockProductAlert;
use StockAlert\Model\StockProductAlertQuery;
use StockAlert\StockAlert;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Thelia\Core\Event\Newsletter\NewsletterEvent;
Expand Down Expand Up @@ -93,7 +95,7 @@ public function subscribe(StockAlertEvent $event)
}

if ($subscribeToNewsLetter) {
$this->subscribeNewsletter($email,$event);
$this->subscribeNewsletter($email, $event);
}


Expand All @@ -106,7 +108,7 @@ protected function subscribeNewsletter($email, StockAlertEvent $event)

if (!$customer) {

$newsletter = new NewsletterEvent($email,"fr_FR");
$newsletter = new NewsletterEvent($email, "fr_FR");
$event->getDispatcher()->dispatch(TheliaEvents::NEWSLETTER_SUBSCRIBE, $newsletter);

}
Expand Down Expand Up @@ -161,8 +163,8 @@ public function sendEmail(RestockingAlert $subscriber)

$this->mailer->sendEmailMessage(
'stockalert_customer',
[ $contactEmail => ConfigQuery::read('store_name') ],
[ $subscriber->getEmail() => ConfigQuery::read('store_name') ],
[$contactEmail => ConfigQuery::read('store_name')],
[$subscriber->getEmail() => ConfigQuery::read('store_name')],
[
'locale' => $subscriber->getLocale(),
'pse_id' => $pse->getId(),
Expand All @@ -183,7 +185,6 @@ public function sendEmail(RestockingAlert $subscriber)
public function checkStockForAdmin(ProductSaleElementsEvent $event)
{
$pseIds = $event->getModel()->getId();

$config = StockAlert::getConfig();

if ($config['enabled']) {
Expand All @@ -201,40 +202,15 @@ public function checkStockForAdmin(ProductSaleElementsEvent $event)
->toArray();

if (!empty($productIds)) {
$this->sendEmailForAdmin($config['emails'], $productIds);
}
}
}

public function sendEmailForAdmin($emails, $productIds)
{
$locale = Lang::getDefaultLanguage()->getLocale();

$contactEmail = ConfigQuery::read('store_email');

if ($contactEmail) {
$storeName = ConfigQuery::read('store_name');
foreach ($productIds as $productId) {

$to = [];

foreach ($emails as $recipient) {
$to[$recipient] = $storeName;
if (!StockProductAlertQuery::create()->findOneByProductId($productId)){
$stockPseAlert = new StockProductAlert();
$stockPseAlert->setProductId($productId);
$stockPseAlert->save();
}
}
}

$this->mailer->sendEmailMessage(
'stockalert_administrator',
[ $contactEmail => $storeName ],
$to,
[
'locale' => $locale,
'products_id' => $productIds
],
$locale
);

Tlog::getInstance()->debug("Stock Alert sent to administrator " . implode(', ', $emails));
} else {
Tlog::getInstance()->debug("Restocking Alert: no contact email is defined !");
}
}
}
53 changes: 53 additions & 0 deletions Service/adminMailService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace StockAlert\Service;

use StockAlert\StockAlert;
use Thelia\Log\Tlog;
use Thelia\Mailer\MailerFactory;
use Thelia\Model\ConfigQuery;
use Thelia\Model\Lang;

class adminMailService
{
protected $mailer;

public function __construct(MailerFactory $mailer)
{
$this->mailer = $mailer;
}

public function sendEmailForAdmin($productIds)
{
$locale = Lang::getDefaultLanguage()->getLocale();

$config = StockAlert::getConfig();

$contactEmail = ConfigQuery::read('store_email');

if ($contactEmail) {
$storeName = ConfigQuery::read('store_name');

$to = [];

foreach ($config['emails'] as $recipient) {
$to[$recipient] = $storeName;
}

$this->mailer->sendEmailMessage(
'stockalert_administrator',
[$contactEmail => $storeName],
$to,
[
'locale' => $locale,
'products_id' => $productIds
],
$locale
);

Tlog::getInstance()->debug("Stock Alert sent to administrator " . implode(', ', $config['emails']));
} else {
Tlog::getInstance()->debug("Restocking Alert: no contact email is defined !");
}
}
}

0 comments on commit d13711d

Please sign in to comment.