c
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace Hura8\Components\Deal\AdminController;
|
||||
|
||||
use Hura8\Components\Deal\Model\DealCollectionModel;
|
||||
use Hura8\System\Controller\aAdminEntityBaseController;
|
||||
|
||||
class ADealCollectionController extends aAdminEntityBaseController
|
||||
{
|
||||
|
||||
/* @var DealCollectionModel $objDealCollectionModel */
|
||||
protected $objDealCollectionModel;
|
||||
|
||||
public function __construct() {
|
||||
$this->objDealCollectionModel = new DealCollectionModel();
|
||||
parent::__construct($this->objDealCollectionModel);
|
||||
}
|
||||
|
||||
public function getAllProductIdInCollection($collection_id) {
|
||||
return $this->objDealCollectionModel->getAllProductIdInCollection($collection_id);
|
||||
}
|
||||
|
||||
public function getAllDealIdInCollection($collection_id) {
|
||||
return $this->objDealCollectionModel->getAllDealIdInCollection($collection_id);
|
||||
}
|
||||
|
||||
public function updateAllDealInCollection($collection_id, $price, $time){
|
||||
return $this->objDealCollectionModel->updateAllDealInCollection($collection_id, $price, $time);
|
||||
}
|
||||
|
||||
public function addProductToCollection($product_id, $collection_id){
|
||||
return $this->objDealCollectionModel->addProductToCollection($product_id, $collection_id);
|
||||
}
|
||||
|
||||
public function removeFromCollection($deal_id, $collection_id){
|
||||
$this->objDealCollectionModel->removeFromCollection($deal_id, $collection_id);
|
||||
}
|
||||
|
||||
public function addToCollection($deal_id, $collection_id){
|
||||
$this->objDealCollectionModel->addToCollection($deal_id, $collection_id);
|
||||
}
|
||||
|
||||
public function updateCollectionView($id){
|
||||
$this->objDealCollectionModel->updateCollectionView($id);
|
||||
}
|
||||
|
||||
protected function deleteFileBeforeDeleteItem($item_id): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace Hura8\Components\Deal\AdminController;
|
||||
|
||||
use Hura8\Components\Deal\Controller\bDealController;
|
||||
use Hura8\Interfaces\iEntityAdminController;
|
||||
use Hura8\Traits\AdminEntityBaseControllerTraits;
|
||||
|
||||
class ADealController extends bDealController implements iEntityAdminController
|
||||
{
|
||||
|
||||
use AdminEntityBaseControllerTraits;
|
||||
|
||||
|
||||
public function autoRenew() {
|
||||
$deal_list = $this->objDealModel->getAllAutoRenewableDeal();
|
||||
|
||||
//then renew & log history
|
||||
foreach ($deal_list as $info) {
|
||||
$this->renewDeal($info['id'], $info['from_time'], $info['to_time'], $info['auto_renew_history']);
|
||||
}
|
||||
}
|
||||
|
||||
protected function renewDeal($id, $from_time, $to_time, array $auto_renew_history) {
|
||||
/*$obj_from_date = new \DateTime(date("Y-m-d", $from_time));
|
||||
$obj_to_date = new \DateTime(date("Y-m-d", $to_time));
|
||||
$day_diff = $obj_to_date->diff($obj_from_date)->format('%a');
|
||||
|
||||
$obj_to_date->add(new \DateInterval('P'.$day_diff.'D'));
|
||||
$new_date = $obj_to_date->format('Y-m-d');
|
||||
|
||||
$current_date = date("Y-m-d");
|
||||
$from_time_minute = ($from_time > 0) ? date("H:i", $from_time) : "00:00";
|
||||
$to_time_minute = ($to_time > 0) ? date("H:i", $to_time) : "00:00";*/
|
||||
|
||||
$to_time_new = CURRENT_TIME + ($to_time - $from_time);
|
||||
|
||||
$auto_renew_history[] = [
|
||||
"renew_time" => show_datetime_from_unix(CURRENT_TIME),
|
||||
"from_time" => show_datetime_from_unix(CURRENT_TIME),
|
||||
"to_time" => show_datetime_from_unix($to_time_new),
|
||||
];
|
||||
|
||||
return $this->updateFields( $id,
|
||||
[
|
||||
'from_time' => CURRENT_TIME,
|
||||
'to_time' => $to_time_new,
|
||||
'auto_renew_history' => serialize($auto_renew_history),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
protected function deleteFileBeforeDeleteItem($item_id): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
48
inc/Hura8/Components/Deal/Controller/bDealController.php
Normal file
48
inc/Hura8/Components/Deal/Controller/bDealController.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace Hura8\Components\Deal\Controller;
|
||||
|
||||
use Hura8\Components\Deal\Model\DealModel;
|
||||
use Hura8\Components\Product\AdminController\AProductController;
|
||||
use Hura8\System\Controller\aEntityBaseController;
|
||||
|
||||
class bDealController extends aEntityBaseController
|
||||
{
|
||||
|
||||
protected $objDealModel;
|
||||
|
||||
|
||||
public function __construct() {
|
||||
$this->objDealModel = new DealModel();
|
||||
parent::__construct($this->objDealModel);
|
||||
}
|
||||
|
||||
|
||||
public function getList(array $condition) : array
|
||||
{
|
||||
$deal_list = parent::getList($condition);
|
||||
|
||||
$product_list_info = [];
|
||||
$product_list_ids = [];
|
||||
$final_list = [];
|
||||
foreach ($deal_list as $item) {
|
||||
if(!isset($product_list_info[$item['pro_id']])) {
|
||||
$product_list_info[$item['pro_id']]= null;
|
||||
$product_list_ids[] = $item['pro_id'];
|
||||
}
|
||||
|
||||
$copy = $item;
|
||||
$copy['product_info'] = &$product_list_info[$item['pro_id']];
|
||||
$final_list[] = $copy;
|
||||
}
|
||||
|
||||
$objAProductController = new AProductController();
|
||||
foreach ($objAProductController->getListByIds($product_list_ids) as $pro_id => $info) {
|
||||
$product_list_info[$pro_id] = $info;
|
||||
}
|
||||
|
||||
return $final_list;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
223
inc/Hura8/Components/Deal/Model/DealCollectionModel.php
Normal file
223
inc/Hura8/Components/Deal/Model/DealCollectionModel.php
Normal file
@@ -0,0 +1,223 @@
|
||||
<?php
|
||||
|
||||
namespace Hura8\Components\Deal\Model;
|
||||
|
||||
use Hura8\Components\Product\AdminController\AProductController;
|
||||
use Hura8\Interfaces\AppResponse;
|
||||
use Hura8\Interfaces\TableName;
|
||||
use Hura8\System\Model\aEntityBaseModel;
|
||||
use Hura8\System\TimeManager;
|
||||
|
||||
class DealCollectionModel extends aEntityBaseModel
|
||||
{
|
||||
|
||||
protected $tb_deal = "tb_deal";
|
||||
protected $tb_collection_item = "tb_deal_collection_item";
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('deal_collection');
|
||||
}
|
||||
|
||||
protected function extendedFilterOptions(): array
|
||||
{
|
||||
return [
|
||||
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
protected function _buildQueryConditionExtend(array $filter_condition): ?array
|
||||
{
|
||||
$where_clause = [];
|
||||
$bind_types = [];
|
||||
$bind_values = [];
|
||||
|
||||
return [
|
||||
join(" ", $where_clause),
|
||||
$bind_types,
|
||||
$bind_values
|
||||
];
|
||||
}
|
||||
|
||||
public function getAllProductIdInCollection($collection_id) {
|
||||
$deal_id_list = $this->getAllDealIdInCollection($collection_id);
|
||||
|
||||
if(!sizeof($deal_id_list)) return array();
|
||||
|
||||
$query = $this->db->runQuery("SELECT `pro_id` FROM `".$this->tb_deal."` WHERE `id` IN (". join(',', $deal_id_list).") ");
|
||||
|
||||
return array_map(function ($item){
|
||||
return $item['pro_id'];
|
||||
}, $this->db->fetchAll($query));
|
||||
}
|
||||
|
||||
public function getAllDealIdInCollection($collection_id) {
|
||||
$query = $this->db->runQuery("SELECT `deal_id` FROM `".$this->tb_collection_item."` WHERE `collection_id` = ? ", ['d'], [$collection_id]);
|
||||
|
||||
return array_map(function ($item){
|
||||
return $item['deal_id'];
|
||||
}, $this->db->fetchAll($query));
|
||||
}
|
||||
|
||||
public function updateAllDealInCollection($collection_id, $price, $time){
|
||||
|
||||
$from_time = ($time['from_date'] != '') ? strtotime(TimeManager::convert_date_from_javascript($time['from_date'])." ".$time['from_time_minute'].":00") : 0;
|
||||
$to_time = ($time['to_date'] != '') ? strtotime(TimeManager::convert_date_from_javascript($time['to_date'])." ".$time['to_time_minute'].":00") : 0;
|
||||
|
||||
$deal_id_list = $this->getAllDealIdInCollection($collection_id);
|
||||
|
||||
if(!sizeof($deal_id_list)) return false;
|
||||
|
||||
$query = $this->db->runQuery("SELECT `id`, `pro_id` FROM `".$this->tb_deal."` WHERE `id` IN (". join(',', $deal_id_list).") ");
|
||||
$product_list_id = array();
|
||||
foreach ( $this->db->fetchAll($query) as $info ) {
|
||||
$product_list_id[$info['pro_id']] = $info['id'];
|
||||
}
|
||||
|
||||
//get product prices
|
||||
$query = $this->db->runQuery("
|
||||
SELECT `id`, price FROM ".TableName::PRODUCT."
|
||||
WHERE `id` IN (". join(',', array_keys($product_list_id)) .")
|
||||
");
|
||||
|
||||
$objDealModel = new DealModel();
|
||||
|
||||
foreach ( $this->db->fetchAll($query) as $_info ) {
|
||||
$product_id = $_info['id'];
|
||||
$product_price = $_info['price'];
|
||||
$deal_price = ($price['type'] == 'percent') ? round($product_price * (100 - $price['value']) / 100) : ($product_price - $price['value']);
|
||||
|
||||
//update
|
||||
$objDealModel->updatePriceAndTime(
|
||||
$product_list_id[$product_id],
|
||||
array(
|
||||
"price" => $deal_price,
|
||||
"from_time" => $from_time,
|
||||
"to_time" => $to_time,
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function addProductToCollection($product_id, $collection_id){
|
||||
|
||||
$objAProductController = new AProductController();
|
||||
$product_info = $objAProductController->getInfo($product_id);
|
||||
|
||||
if($product_info){
|
||||
|
||||
$objDealModel = new DealModel();
|
||||
|
||||
$deal_id = $objDealModel->create(array(
|
||||
"pro_id" => $product_id,
|
||||
"title" => $product_info['title'],
|
||||
"price" => $product_info['price'],
|
||||
"quantity" => $product_info['quantity'],
|
||||
"min_purchase" => 1,
|
||||
"from_time" => 0,
|
||||
"to_time" => 0,
|
||||
"ordering" => $product_info[''],
|
||||
"description" => '',
|
||||
));
|
||||
|
||||
$this->addToCollection($deal_id, $collection_id);
|
||||
|
||||
return $deal_id;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
public function removeFromCollection($deal_id, $collection_id){
|
||||
$this->db->runQuery(
|
||||
"DELETE FROM `".$this->tb_collection_item."` WHERE `deal_id` = ? AND `collection_id` = ? ",
|
||||
['d', 'd'],
|
||||
[$deal_id, $collection_id]
|
||||
);
|
||||
|
||||
$this->updateCollectionCount($collection_id);
|
||||
}
|
||||
|
||||
public function addToCollection($deal_id, $collection_id){
|
||||
$query = $this->db->runQuery(
|
||||
"SELECT `deal_id` FROM `".$this->tb_collection_item."` WHERE `deal_id`= ? AND `collection_id` = ? LIMIT 1 ",
|
||||
['d', 'd'], [$deal_id, $collection_id]
|
||||
);
|
||||
|
||||
if( ! $this->db->fetchAssoc($query) ){
|
||||
$this->db->insert(
|
||||
$this->tb_collection_item ,
|
||||
[
|
||||
'collection_id' => $collection_id ,
|
||||
'deal_id' => $deal_id,
|
||||
'create_by' => ADMIN_ID,
|
||||
'create_time' => CURRENT_TIME,
|
||||
]
|
||||
);
|
||||
|
||||
$this->updateCollectionCount($collection_id);
|
||||
}
|
||||
}
|
||||
|
||||
protected function updateCollectionCount($collection_id){
|
||||
$this->db->runQuery(
|
||||
"UPDATE `".$this->tb_entity."` SET
|
||||
`deal_count` = (SELECT COUNT(*) FROM `".$this->tb_collection_item."` WHERE `collection_id` = ? )
|
||||
WHERE `id` = ? LIMIT 1 ",
|
||||
['d', 'd'],
|
||||
[$collection_id, $collection_id]
|
||||
);
|
||||
}
|
||||
|
||||
public function updateCollectionView($id){
|
||||
$this->db->runQuery("UPDATE `".$this->tb_entity."` SET `visit` = `visit` + 1 WHERE `id` = ? LIMIT 1 ", ['d'], [ $id ]);
|
||||
}
|
||||
|
||||
|
||||
protected function beforeCreateItem(array $input_info) : AppResponse
|
||||
{
|
||||
$info = $input_info;
|
||||
|
||||
$info['create_time'] = CURRENT_TIME;
|
||||
$info['create_by'] = ADMIN_NAME;
|
||||
$info['last_update'] = CURRENT_TIME;
|
||||
$info['last_update_by'] = ADMIN_NAME;
|
||||
|
||||
return new AppResponse('ok', null, $info);
|
||||
}
|
||||
|
||||
protected function afterCreateItem($new_item_id, $new_item_info)
|
||||
{
|
||||
// TODO: Implement afterCreateItem() method.
|
||||
}
|
||||
|
||||
protected function beforeUpdateItem($item_id, $current_item_info, $new_input_info) : AppResponse
|
||||
{
|
||||
$info = $new_input_info;
|
||||
|
||||
$info['last_update'] = CURRENT_TIME;
|
||||
$info['last_update_by'] = ADMIN_NAME;
|
||||
|
||||
return new AppResponse('ok', null, $info);
|
||||
}
|
||||
|
||||
protected function afterUpdateItem($item_id, $old_item_info, $new_item_info)
|
||||
{
|
||||
// TODO: Implement afterUpdateItem() method.
|
||||
}
|
||||
|
||||
protected function beforeDeleteItem($item_id, $item_info) : AppResponse
|
||||
{
|
||||
return new AppResponse('ok');
|
||||
}
|
||||
|
||||
protected function afterDeleteItem($item_id, $item_info)
|
||||
{
|
||||
// TODO: Implement afterDeleteItem() method.
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
196
inc/Hura8/Components/Deal/Model/DealModel.php
Normal file
196
inc/Hura8/Components/Deal/Model/DealModel.php
Normal file
@@ -0,0 +1,196 @@
|
||||
<?php
|
||||
|
||||
namespace Hura8\Components\Deal\Model;
|
||||
|
||||
use Hura8\Interfaces\AppResponse;
|
||||
use Hura8\System\Model\aEntityBaseModel;
|
||||
use Hura8\System\Security\DataValidator;
|
||||
use Hura8\System\TimeManager;
|
||||
|
||||
class DealModel extends aEntityBaseModel
|
||||
{
|
||||
|
||||
protected $tb_collection_item = "tb_deal_collection_item";
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('deal');
|
||||
}
|
||||
|
||||
|
||||
protected function extendedFilterOptions() : array
|
||||
{
|
||||
return [
|
||||
// empty for now
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
public function getAllAutoRenewableDeal() {
|
||||
$query = $this->db->runQuery(
|
||||
"SELECT `id`, `from_time`, `to_time` FROM `". $this->tb_entity ."`
|
||||
WHERE `to_time` < '".CURRENT_TIME."' AND `to_time` > 0 AND `auto_renew` = 1 "
|
||||
);
|
||||
|
||||
return $this->db->fetchAll($query);
|
||||
}
|
||||
|
||||
protected function _buildQueryConditionExtend(array $filter_condition) : ?array
|
||||
{
|
||||
|
||||
$where_clause = [];
|
||||
$bind_types = [];
|
||||
$bind_values = [];
|
||||
|
||||
$limit_by_time = false;
|
||||
$limit_by_time_condition = '';
|
||||
|
||||
// get deal by start time
|
||||
// require format: YY-mm-dd h:m:i or YY-mm-dd h:m or timestamp
|
||||
$datetime_pattern = '/^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}\s[0-9]{1,2}:[0-9]{1,2}(:[0-9]{1,2})?$/i';
|
||||
|
||||
if(isset($filter_condition['start_time']) && $filter_condition['start_time'] ) {
|
||||
$limit_by_time = true;
|
||||
if(preg_match($datetime_pattern, trim($filter_condition['start_time']))) {
|
||||
$check_time = strtotime($filter_condition['start_time']);
|
||||
}else{
|
||||
$check_time = intval($filter_condition['start_time']);
|
||||
}
|
||||
|
||||
$limit_by_time_condition .= " AND `from_time` >= '".$check_time."' ";
|
||||
|
||||
$bind_types[] = 'd';
|
||||
$bind_values[] = $check_time;
|
||||
}
|
||||
|
||||
// get deal by end time
|
||||
// require format: YY-mm-dd h:m:i or YY-mm-dd h:m or timestamp
|
||||
if(isset($filter_condition['end_time']) && $filter_condition['end_time'] ) {
|
||||
$limit_by_time = true;
|
||||
if(preg_match($datetime_pattern, trim($filter_condition['end_time']))) {
|
||||
$check_time = strtotime($filter_condition['end_time']);
|
||||
}else{
|
||||
$check_time = intval($filter_condition['end_time']);
|
||||
}
|
||||
|
||||
$limit_by_time_condition .= " AND `to_time` <= '".$check_time."' ";
|
||||
$bind_types[] = 'd';
|
||||
$bind_values[] = $check_time;
|
||||
}
|
||||
|
||||
if($limit_by_time) {
|
||||
$where_clause[] = " AND `status`= 1 ".$limit_by_time_condition;
|
||||
}
|
||||
|
||||
//type expire
|
||||
if(isset($filter_condition['type']) && $filter_condition['type'] == 'expire' ) {
|
||||
$where_clause[] = "AND `status`= 1 AND `to_time` < ? ";
|
||||
$bind_types[] = 'd';
|
||||
$bind_values[] = CURRENT_TIME;
|
||||
}
|
||||
|
||||
//type active: might not have begun yet
|
||||
if(isset($filter_condition['type']) && $filter_condition['type'] == 'active' && !$limit_by_time ) {
|
||||
$where_clause[] = " AND `status` = 1 AND `to_time` >= ? ";
|
||||
$bind_types[] = 'd';
|
||||
$bind_values[] = CURRENT_TIME;
|
||||
}
|
||||
|
||||
// Đang bắt đầu
|
||||
if(isset($filter_condition['type']) && $filter_condition['type'] == 'started' && !$limit_by_time ) {
|
||||
$where_clause[] = " AND `status` = 1 AND `to_time` >= ? AND from_time < ? ";
|
||||
$bind_types[] = 'd';
|
||||
$bind_values[] = CURRENT_TIME;
|
||||
$bind_types[] = 'd';
|
||||
$bind_values[] = CURRENT_TIME;
|
||||
}
|
||||
|
||||
// Chưa bắt đầu
|
||||
if(isset($filter_condition['type']) && $filter_condition['type'] == 'coming' && !$limit_by_time ) {
|
||||
$where_clause[] = " AND `status` = 1 AND `from_time` >= ? ";
|
||||
$bind_types[] = 'd';
|
||||
$bind_values[] = CURRENT_TIME;
|
||||
}
|
||||
|
||||
//deal collection
|
||||
if(isset($filter_condition['collection_id']) && $filter_condition['collection_id'] > 0) {
|
||||
$where_clause[] = " AND `id` IN ( SELECT `deal_id` FROM `".$this->tb_collection_item."` WHERE `collection_id` = ? ) ";
|
||||
$bind_types[] = 'd';
|
||||
$bind_values[] = $filter_condition['collection_id'];
|
||||
}
|
||||
|
||||
// exclude from collection
|
||||
if(isset($filter_condition['add_to_collection']) && $filter_condition['add_to_collection'] > 0) {
|
||||
$where_clause[] = " AND `id` NOT IN ( SELECT `deal_id` FROM `".$this->tb_collection_item."` WHERE `collection_id` = ? ) ";
|
||||
$bind_types[] = 'd';
|
||||
$bind_values[] = $filter_condition['add_to_collection'];
|
||||
}
|
||||
|
||||
// by word filter
|
||||
$filter = $filter_condition['filter'] ?? '';
|
||||
switch ($filter) {
|
||||
case "not-started": // Chưa bắt đầu
|
||||
$where_clause[] = " AND ( ? - `from_time` ) < 0 ";
|
||||
$bind_types[] = 'd';
|
||||
$bind_values[] = CURRENT_TIME;
|
||||
break;
|
||||
|
||||
case "started": // Đang bắt đầu
|
||||
$where_clause[] = " AND ( ? - `from_time` ) > 0 AND ( `to_time` - ?) > 0 ";
|
||||
$bind_types[] = 'd';
|
||||
$bind_types[] = 'd';
|
||||
$bind_values[] = CURRENT_TIME;
|
||||
$bind_values[] = CURRENT_TIME;
|
||||
break;
|
||||
|
||||
case "ended": // Hết thời gian
|
||||
$where_clause[] = " AND (`to_time` - ?) < 0 ";
|
||||
$bind_types[] = 'd';
|
||||
$bind_values[] = CURRENT_TIME;
|
||||
break;
|
||||
|
||||
case "hidden": // Ẩn hiển thị
|
||||
$where_clause[] = " AND `status` = 0 ";
|
||||
break;
|
||||
|
||||
case "featured": // Đang nổi bật
|
||||
$where_clause[] = " AND `is_featured` = 1 ";
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
return [
|
||||
join(" ", $where_clause),
|
||||
$bind_types,
|
||||
$bind_values
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
protected function formatItemInList(array $item_info): array
|
||||
{
|
||||
$copy = $item_info;
|
||||
|
||||
$copy['deal_time_happen'] = CURRENT_TIME - $item_info['from_time'];
|
||||
$copy['deal_time_left'] = $item_info['to_time'] - CURRENT_TIME;
|
||||
$copy['is_start'] = (CURRENT_TIME - $item_info['from_time'] > 0) ? 1 : 0;
|
||||
$copy['is_end'] = ($item_info['to_time'] - CURRENT_TIME > 0) ? 0 : 1;
|
||||
|
||||
return $copy;
|
||||
}
|
||||
|
||||
protected function formatItemInfo(array $item_info): array
|
||||
{
|
||||
$copy = $item_info;
|
||||
|
||||
$copy['deal_time_happen'] = CURRENT_TIME - $item_info['from_time'];
|
||||
$copy['deal_time_left'] = $item_info['to_time'] - CURRENT_TIME;
|
||||
$copy['is_start'] = (CURRENT_TIME - $item_info['from_time'] > 0) ? 1 : 0;
|
||||
$copy['is_end'] = ($item_info['to_time'] - CURRENT_TIME > 0) ? 0 : 1;
|
||||
|
||||
return $copy;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user