Skip to content

Commit

Permalink
Merged in init-module (pull request #1)
Browse files Browse the repository at this point in the history
Initialize Giao Hang Nhanh extension.
  • Loading branch information
LeHuuNhi authored and thaopvp committed Jul 6, 2020
2 parents ab9a46e + 004b677 commit 9f58ebb
Show file tree
Hide file tree
Showing 74 changed files with 4,857 additions and 20 deletions.
74 changes: 74 additions & 0 deletions Block/Customer/DataProviders/AdditionalConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php declare(strict_types=1);
/************************************************************
* *
* * Copyright © Boolfly. All rights reserved.
* * See COPYING.txt for license details.
* *
* * @author info@boolfly.com
* * @project Giao hang nhanh
*/
namespace Boolfly\GiaoHangNhanh\Block\Customer\DataProviders;

use Boolfly\GiaoHangNhanh\Model\Config;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\Framework\View\Element\Block\ArgumentInterface;

/**
* Class AdditionalConfig
*
* @package Boolfly\GiaoHangNhanh\Block\Customer\DataProviders
*/
class AdditionalConfig implements ArgumentInterface
{
/**
* @var SerializerInterface
*/
private $serializer;

/**
* @var Config
*/
private $config;

/**
* Constructor
*
* @param Config $config
* @param SerializerInterface $serializer
*/
public function __construct(
Config $config,
SerializerInterface $serializer
) {
$this->config = $config;
$this->serializer = $serializer;
}

/**
* Get districts
*
* @return array
*/
private function getDistricts()
{
$districts = $this->config->getDistricts();
$data = [];
foreach ($districts as $district) {
$data[$district['region_id']][] = [
'districtName' => $district['district_name'],
'districtID' => $district['district_id']
];
}
return $data;
}

/**
* @return string
*/
public function getJsonData(): string
{
return $this->serializer->serialize([
'districts' => $this->getDistricts()
]);
}
}
155 changes: 155 additions & 0 deletions Console/GenerateRegionCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?php declare(strict_types=1);
/************************************************************
* *
* * Copyright © Boolfly. All rights reserved.
* * See COPYING.txt for license details.
* *
* * @author info@boolfly.com
* * @project Giao hang nhanh
*/
namespace Boolfly\GiaoHangNhanh\Console;

use Boolfly\GiaoHangNhanh\Model\Service\Helper\SubjectReader;
use Boolfly\IntegrationBase\Model\Service\Command\CommandPoolInterface;
use Magento\Directory\Model\RegionFactory;
use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Exception\LocalizedException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Class GenerateRegionCommand
*
* @package Boolfly\GiaoHangNhanh\Console
*/
class GenerateRegionCommand extends Command
{
/**
* @var ResourceConnection
*/
private $resourceConnection;

/**
* @var RegionFactory
*/
private $regionFactory;

/**
* @var CommandPoolInterface
*/
private $commandPool;

/**
* GeneratingRegionData constructor.
* @param ResourceConnection $resourceConnection
* @param RegionFactory $regionFactory
* @param CommandPoolInterface $commandPool
* @param string|null $name
*/
public function __construct(
ResourceConnection $resourceConnection,
RegionFactory $regionFactory,
CommandPoolInterface $commandPool,
$name = null
) {
$this->resourceConnection = $resourceConnection;
$this->regionFactory = $regionFactory;
$this->commandPool = $commandPool;
parent::__construct($name);
}

protected function configure()
{
$this->setDescription('Generate region data.');
parent::configure();
}

/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int|void|null
* @throws LocalizedException
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$commandResult = $this->commandPool->get('get_districts')->execute([]);
$data = SubjectReader::readDistricts($commandResult->get());

if ($data) {
$output->writeln('<info>Generating data. Please wait...</info>');
foreach ($data as $item) {
$provinceId = $item['ProvinceID'];
$districtId = $item['DistrictID'];
$region = $this->regionFactory->create()
->loadByCode($provinceId, 'VN');

$this->insertData(
'boolfly_giaohangnhanh_district',
[
'district_id' => $districtId,
'province_id' => $provinceId,
'district_name' => $item['DistrictName']
],
['col' => 'district_id', 'val' => $districtId]
);

if (!$region->getId()) {
$this->insertData(
'directory_country_region',
[
'country_id' => 'VN',
'code' => $provinceId,
'default_name' => $item['ProvinceName']
]
);
}
}
$output->writeln('<info>Generate data successfully.</info>');
} else {
$output->writeln('<error>Generating data was interrupted. Please try again!</error>');
}
}

/**
* @param string $tableName
* @param array $data
* @param array $pairOfColAndVal
*/
private function insertData($tableName, $data, $pairOfColAndVal = [])
{
if (!$this->checkRecordExist($tableName, $pairOfColAndVal)) {
$this->resourceConnection->getConnection()->insert(
$this->resourceConnection->getTableName($tableName),
$data
);
}
}


/**
* @param string $tableName
* @param array $pairOfColAndVal
* @return bool
*/
private function checkRecordExist($tableName, $pairOfColAndVal = [])
{
$checkingFlag = false;

if ($pairOfColAndVal) {
$connection = $this->resourceConnection->getConnection();
$sql = $connection->select()->from(
['districtTable' => $this->resourceConnection->getTableName($tableName)],
$pairOfColAndVal['col']
)->where($pairOfColAndVal['col'] . ' = ?', $pairOfColAndVal['val']);

$rows = $connection->fetchAll($sql);

if (count($rows)) {
$checkingFlag = true;
}
}

return $checkingFlag;
}
}
67 changes: 67 additions & 0 deletions Controller/Customer/Address/FormPost.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php declare(strict_types=1);
/************************************************************
* *
* * Copyright © Boolfly. All rights reserved.
* * See COPYING.txt for license details.
* *
* * @author info@boolfly.com
* * @project Giao hang nhanh
*/
namespace Boolfly\GiaoHangNhanh\Controller\Customer\Address;

use Magento\Framework\Controller\Result\Redirect;
use Magento\Framework\Exception\InputException;
use Magento\Customer\Controller\Address\FormPost as MageFormPostController;

/**
* Class FormPost
*
* @package Boolfly\GiaoHangNhanh\Controller\Customer\Address
*/
class FormPost extends MageFormPostController
{
/**
* @return Redirect
*/
public function execute()
{
$redirectUrl = null;
if (!$this->_formKeyValidator->validate($this->getRequest())) {
return $this->resultRedirectFactory->create()->setPath('*/*/');
}

if (!$this->getRequest()->isPost()) {
$this->_getSession()->setAddressFormData($this->getRequest()->getPostValue());
return $this->resultRedirectFactory->create()->setUrl(
$this->_redirect->error($this->_buildUrl('*/*/edit'))
);
}

try {
$address = $this->_extractAddress();
/*custom code*/
$address->setCustomAttribute('district', $this->getRequest()->getParam('district_id'));
/*end*/
$this->_addressRepository->save($address);
$this->messageManager->addSuccessMessage(__('You saved the address.'));
$url = $this->_buildUrl('*/*/index', ['_secure' => true]);
return $this->resultRedirectFactory->create()->setUrl($this->_redirect->success($url));
} catch (InputException $e) {
$this->messageManager->addErrorMessage($e->getMessage());
foreach ($e->getErrors() as $error) {
$this->messageManager->addErrorMessage($error->getMessage());
}
} catch (\Exception $e) {
$redirectUrl = $this->_buildUrl('*/*/index');
$this->messageManager->addExceptionMessage($e, __('We can\'t save the address.'));
}

$url = $redirectUrl;
if (!$redirectUrl) {
$this->_getSession()->setAddressFormData($this->getRequest()->getPostValue());
$url = $this->_buildUrl('*/*/edit', ['id' => $this->getRequest()->getParam('id')]);
}

return $this->resultRedirectFactory->create()->setUrl($this->_redirect->error($url));
}
}
Loading

0 comments on commit 9f58ebb

Please sign in to comment.