c
This commit is contained in:
235
inc/Hura8/Components/Order/AdminController/AOrderController.php
Normal file
235
inc/Hura8/Components/Order/AdminController/AOrderController.php
Normal file
@@ -0,0 +1,235 @@
|
||||
<?php
|
||||
|
||||
namespace Hura8\Components\Order\AdminController;
|
||||
|
||||
|
||||
use Hura8\Components\Order\Controller\OrderStatus;
|
||||
use Hura8\Components\Order\Model\OrderModel;
|
||||
use Hura8\Events\EventName;
|
||||
use Hura8\Events\RealEvents\OrderEvent;
|
||||
use Hura8\System\Controller\aAdminEntityBaseController;
|
||||
|
||||
class AOrderController extends aAdminEntityBaseController
|
||||
{
|
||||
|
||||
/* @var OrderModel $objOrderModel */
|
||||
protected $objOrderModel;
|
||||
|
||||
/* @var AOrderStatusController $objAOrderStatusController */
|
||||
protected $objAOrderStatusController;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->objAOrderStatusController = new AOrderStatusController();
|
||||
$this->objOrderModel = new OrderModel();
|
||||
parent::__construct($this->objOrderModel);
|
||||
}
|
||||
|
||||
public function updateOrderFulfillmentStatus($order_id, $new_status, $comment, $payment_data = []) {
|
||||
|
||||
if(!array_key_exists($new_status, OrderStatus::FULFILLMENT_STATUS)) {
|
||||
return [
|
||||
"status" => 'error',
|
||||
'message' => '',
|
||||
];
|
||||
}
|
||||
|
||||
$this->objAOrderStatusController->createHistory($order_id, 'fulfillment', $new_status, $comment, $payment_data );
|
||||
|
||||
//cap nhat don hang
|
||||
$this->objOrderModel->update( $order_id, [
|
||||
'fulfillment_status' => $new_status
|
||||
]);
|
||||
|
||||
$this->updateDerivedOrderStatus($order_id);
|
||||
|
||||
return [
|
||||
"status" => 'success',
|
||||
'message' => 'Cập nhật thành công',
|
||||
];
|
||||
}
|
||||
|
||||
public function updateOrderPaymentStatus($order_id, $new_status, $comment, $payment_data = []) {
|
||||
|
||||
if(!array_key_exists($new_status, OrderStatus::PAYMENT_STATUS)) {
|
||||
return [
|
||||
"status" => 'error',
|
||||
'message' => '',
|
||||
];
|
||||
}
|
||||
|
||||
$this->objAOrderStatusController->createHistory($order_id, 'payment', $new_status, $comment, $payment_data );
|
||||
|
||||
//cap nhat don hang
|
||||
$this->objOrderModel->update( $order_id, [
|
||||
'payment_status' => $new_status
|
||||
]);
|
||||
|
||||
$this->updateDerivedOrderStatus($order_id);
|
||||
|
||||
return [
|
||||
"status" => 'success',
|
||||
'message' => 'Cập nhật thành công',
|
||||
];
|
||||
}
|
||||
|
||||
protected function updateDerivedOrderStatus($order_id) {
|
||||
|
||||
$current_info = $this->getInfo($order_id);
|
||||
|
||||
// order must be success
|
||||
if(
|
||||
$current_info['payment_status'] == OrderStatus::PAYMENT_STATUS['paid']['id'] &&
|
||||
$current_info['fulfillment_status'] == OrderStatus::FULFILLMENT_STATUS['fulfilled']['id']
|
||||
) {
|
||||
|
||||
return $this->objOrderModel->update($order_id, [
|
||||
'order_status' => OrderStatus::ORDER_STATUS['success']['id']
|
||||
]);
|
||||
|
||||
}
|
||||
|
||||
|
||||
// update processing
|
||||
return $this->objOrderModel->update($order_id, [
|
||||
'order_status' => OrderStatus::ORDER_STATUS['processing']['id']
|
||||
]);
|
||||
|
||||
}
|
||||
|
||||
public function updateOrderStatus($order_id, $status, $comment) {
|
||||
|
||||
$current_info = $this->getInfo($order_id);
|
||||
|
||||
// cannot update if order has: success or cancel
|
||||
if(in_array($current_info['order_status'], [
|
||||
OrderStatus::ORDER_STATUS['success']['id'],
|
||||
OrderStatus::ORDER_STATUS['canceled']['id'],
|
||||
])) {
|
||||
return [
|
||||
"status" => 'error',
|
||||
'message' => '',
|
||||
];
|
||||
}
|
||||
|
||||
if(!array_key_exists($status, OrderStatus::ORDER_STATUS)) {
|
||||
return [
|
||||
"status" => 'error',
|
||||
'message' => '',
|
||||
];
|
||||
}
|
||||
|
||||
$this->objAOrderStatusController->createHistory($order_id, 'order', $status, $comment );
|
||||
|
||||
$this->objOrderModel->update($order_id, [
|
||||
'order_status' => $status
|
||||
]);
|
||||
|
||||
// dispatch
|
||||
$objOrderEvent = new OrderEvent();
|
||||
$objOrderEvent->setUpdateOrderInfo([
|
||||
'orderId' => $order_id,
|
||||
'order_status' => $status,
|
||||
'old_status' => $current_info['order_status']
|
||||
]);
|
||||
get_dispatcher()->dispatch(EventName::ORDER['updated_status'], $objOrderEvent);
|
||||
|
||||
return [
|
||||
"status" => 'success',
|
||||
'message' => 'Cập nhật thành công',
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
protected function formatItemInList(array $order_info ) {
|
||||
|
||||
$rs = $order_info;
|
||||
|
||||
$rs["order_date"] = date("d-m-Y", $rs['order_time']);
|
||||
$rs["order_hour"] = date("h:i a", $rs['order_time']);
|
||||
|
||||
if($rs['order_discount']) $rs['order_discount'] = \json_decode($rs['order_discount'], true);
|
||||
|
||||
$rs["payment_status_name"] = (array_key_exists($rs["payment_status"], OrderStatus::PAYMENT_STATUS)) ? OrderStatus::PAYMENT_STATUS[$rs["payment_status"]]['title'] : '';
|
||||
|
||||
$rs["fulfillment_status_name"] = (array_key_exists($rs["fulfillment_status"], OrderStatus::FULFILLMENT_STATUS)) ? OrderStatus::FULFILLMENT_STATUS[$rs["fulfillment_status"]]['title'] : '';
|
||||
|
||||
$rs["order_status_name"] = (array_key_exists($rs["order_status"], OrderStatus::ORDER_STATUS)) ? OrderStatus::ORDER_STATUS[$rs["order_status"]]['title'] : '';
|
||||
|
||||
return $rs;
|
||||
}
|
||||
|
||||
protected function getListFilterCondition($list_id) {
|
||||
|
||||
if($list_id == 'mine') {
|
||||
return [
|
||||
'assign_to' => ADMIN_ID,
|
||||
];
|
||||
}
|
||||
|
||||
if($list_id == 'new') {
|
||||
return [
|
||||
'status' => 'new',
|
||||
];
|
||||
}
|
||||
|
||||
if($list_id == 'unpaid') {
|
||||
return [
|
||||
'payment' => 'unpaid',
|
||||
];
|
||||
}
|
||||
|
||||
if($list_id == 'partially-paid') {
|
||||
return [
|
||||
'payment' => 'partially-paid',
|
||||
];
|
||||
}
|
||||
|
||||
if($list_id == 'unfulfilled') {
|
||||
return [
|
||||
'fullfillment' => 'unfulfilled',
|
||||
];
|
||||
}
|
||||
|
||||
if($list_id == 'partially-fulfilled') {
|
||||
return [
|
||||
'fullfillment' => 'partially-fulfilled',
|
||||
];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getStatusHistory($orderId){
|
||||
|
||||
return $this->objAOrderStatusController->getStatusHistory($orderId);
|
||||
|
||||
}
|
||||
|
||||
public function getItemsForOrderList(array $list_ids, $fields = "*") {
|
||||
|
||||
return $this->objOrderModel->getItemsForOrderList($list_ids, $fields);
|
||||
|
||||
}
|
||||
|
||||
public static function createId($order_id){
|
||||
$key_length = strlen($order_id);
|
||||
|
||||
$order_keys = [];
|
||||
for($i=0; $i < 9 - $key_length; $i++){
|
||||
$order_keys[] = "0";
|
||||
}
|
||||
$order_keys[] = $order_id;
|
||||
|
||||
$order_eles = str_split(join("", $order_keys), 3);
|
||||
|
||||
return join("-", $order_eles);
|
||||
}
|
||||
|
||||
|
||||
protected function deleteFileBeforeDeleteItem($item_id): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace Hura8\Components\Order\AdminController;
|
||||
|
||||
use Hura8\Components\Order\Model\OrderStatusModel;
|
||||
|
||||
class AOrderStatusController
|
||||
{
|
||||
/* @var OrderStatusModel $objOrderStatusModel */
|
||||
protected $objOrderStatusModel;
|
||||
|
||||
public function __construct() {
|
||||
$this->objOrderStatusModel = new OrderStatusModel();
|
||||
}
|
||||
|
||||
public function getStatusHistory($orderId){
|
||||
return $this->objOrderStatusModel->getStatusHistory($orderId);
|
||||
}
|
||||
|
||||
public function createHistory($order_id, $status_type = 'order', $system_status = '', $comment = '', array $data= [], array $other_info = []) {
|
||||
return $this->objOrderStatusModel->createHistory($order_id, $status_type , $system_status , $comment, $data, $other_info);
|
||||
}
|
||||
|
||||
public function getInfo($status_id) {
|
||||
return $this->objOrderStatusModel->getInfo($status_id);
|
||||
}
|
||||
|
||||
public function getAll() {
|
||||
return $this->objOrderStatusModel->getAll();
|
||||
}
|
||||
|
||||
public function delete($id) {
|
||||
return $this->objOrderStatusModel->delete($id);
|
||||
}
|
||||
|
||||
public function update($id, array $info) {
|
||||
return $this->objOrderStatusModel->update($id, $info);
|
||||
}
|
||||
|
||||
public function create(array $info) {
|
||||
return $this->objOrderStatusModel->create($info);
|
||||
}
|
||||
}
|
||||
43
inc/Hura8/Components/Order/Controller/OrderStatus.php
Normal file
43
inc/Hura8/Components/Order/Controller/OrderStatus.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
// https://help.shopify.com/en/manual/orders/order-status
|
||||
|
||||
namespace Hura8\Components\Order\Controller;
|
||||
|
||||
|
||||
class OrderStatus
|
||||
{
|
||||
|
||||
const ORDER_STATUS = [
|
||||
'new' => ['id' => 'new', 'title' => 'Mới'],// 'Mới', //
|
||||
'processing' => ['id' => 'processing', 'title' => 'Đang xử lý'],// 'Đang xử lý', //
|
||||
'success' => ['id' => 'success', 'title' => 'Thành công'],// 'Thành công', //
|
||||
'closed' => ['id' => 'closed', 'title' => 'Đóng lại'],// 'Đóng lại', //
|
||||
//'archived' => 'Lưu kho', //
|
||||
'canceled' => ['id' => 'canceled', 'title' => 'Hủy'],// 'Hủy', //
|
||||
];
|
||||
|
||||
const PAYMENT_STATUS = [
|
||||
'pending' => ['id' => 'pending', 'title' => 'Chờ'],// 'Chờ', // Orders have the Payment pending status for one of the following reasons: During checkout, You create an order, Manual payment methods, such as Cash On Delivery
|
||||
'authorized' => ['id' => 'authorized', 'title' => 'Đã xác nhận'],// 'Đã xác nhận', // The payment provider validated your customer’s payment information. The order is created, customers can complete their checkout, and inventory is reserved.
|
||||
'overdue' => ['id' => 'overdue', 'title' => 'Quá hạn'],// 'Quá hạn', // Payment wasn't captured before the due date that was set in the payment terms on an order that had the Payment pending status.
|
||||
'expiring' => ['id' => 'expiring', 'title' => 'Sắp hết hạn'],// 'Sắp hết hạn', // Expiring isn't a payment status, but the Expiring badge is displayed two days before the deadline for capturing payment on orders that have the Authorized payment status.
|
||||
'expired' => ['id' => 'expired', 'title' => 'Hết hạn'],// 'Hết hạn', // Payment wasn't captured before the date that was set by the payment provider on an order that had the Authorized payment status.
|
||||
'paid' => ['id' => 'paid', 'title' => 'Đã thanh toán'],// 'Đã thanh toán', // Payment was automatically or manually captured, or the order was marked as paid.
|
||||
'refunded' => ['id' => 'refunded', 'title' => 'Đã hoàn lại'],// 'Đã hoàn lại', // The full amount that the customer paid for an order was returned to the customer.
|
||||
'partially-refunded' => ['id' => 'partially-refunded', 'title' => 'Hoàn lại 1 phần'],// 'Hoàn lại 1 phần', // The amount that was returned to a customer is less than the full amount that the customer paid for an order.
|
||||
'partially-paid' => ['id' => 'partially-paid', 'title' => 'Thanh toán 1 phần'],// 'Thanh toán 1 phần', // You manually captured payment for the order and specified less than the full order value as the payment amount.
|
||||
'voided' => ['id' => 'voided', 'title' => 'Hủy'],// 'Hủy', // An unpaid order was manually canceled.
|
||||
'unpaid' => ['id' => 'unpaid', 'title' => 'Chưa thanh toán'],// 'Chưa thanh toán', // Unpaid payment status includes orders that are in Authorized, Pending, Expired, and Partially paid payment status.
|
||||
];
|
||||
|
||||
const FULFILLMENT_STATUS = [
|
||||
'fulfilled' => ['id' => 'fulfilled', 'title' => 'Đã chuyển hết'],// 'Đã chuyển hết', // When you've shipped all the items in an order, it is Fulfilled.
|
||||
'unfulfilled' => ['id' => 'unfulfilled', 'title' => 'Chưa chuyển'],// 'Chưa chuyển', // When an order is placed, it has an Unfulfilled fulfillment status, unless you have selected to automatically capture the payment and automatically fulfill all of the order's line items in the checkout settings.
|
||||
'partially-fulfilled' => ['id' => 'partially-fulfilled', 'title' => 'Chuyển 1 phần'],// 'Chuyển 1 phần', // If you have shipped some, but not all, of the items in an order, then the order has a Partially fulfilled fulfillment status.
|
||||
'scheduled' => ['id' => 'scheduled', 'title' => 'Đã lên kế hoạch'],// 'Đã lên kế hoạch', // Prepaid subscription orders have a Scheduled status until the fulfillment date is reached
|
||||
'onhold ' => ['id' => 'onhold', 'title' => 'Giữ hàng'],// 'Giữ hàng', // When upsell offers are presented to customers at checkout, the order fulfillment status is set to On hold temporarily. When a fulfillment is On hold you can reserve inventory for the order, but you can't fulfill the order until the fulfillment hold is released, and the order fulfillment status changes to Unfulfilled.
|
||||
];
|
||||
|
||||
}
|
||||
|
||||
557
inc/Hura8/Components/Order/Model/OrderModel.php
Normal file
557
inc/Hura8/Components/Order/Model/OrderModel.php
Normal file
@@ -0,0 +1,557 @@
|
||||
<?php
|
||||
|
||||
namespace Hura8\Components\Order\Model;
|
||||
|
||||
|
||||
use Hura8\Components\Order\Controller\OrderStatus;
|
||||
use Hura8\Interfaces\AppResponse;
|
||||
use Hura8\Interfaces\iEntityModel;
|
||||
use Hura8\Interfaces\iSearch;
|
||||
use Hura8\Interfaces\EntityType;
|
||||
use Hura8\System\Model\aEntityBaseModel;
|
||||
use Hura8\System\Security\DataClean;
|
||||
use Hura8\System\Security\DataType;
|
||||
use Hura8\System\TimeManager;
|
||||
use Hura8\System\Url;
|
||||
|
||||
class OrderModel extends aEntityBaseModel implements iEntityModel
|
||||
{
|
||||
|
||||
protected $tb_order_detail = "tb_order_detail";
|
||||
|
||||
/* @var iSearch $objSearchModel */
|
||||
protected $objSearchModel;
|
||||
|
||||
public function __construct() {
|
||||
|
||||
$this->objSearchModel = new OrderSearchModel();
|
||||
|
||||
parent::__construct(EntityType::ORDER, '', $this->objSearchModel);
|
||||
}
|
||||
|
||||
|
||||
protected function extendedFilterOptions() : array
|
||||
{
|
||||
return [
|
||||
// empty for now
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
public function getItems($orderId, $fields = "*") {
|
||||
$query = $this->db->runQuery(
|
||||
" SELECT ".$fields." FROM `".$this->tb_order_detail."` WHERE `order_id` = ? ",
|
||||
['d'], [ $orderId ]);
|
||||
|
||||
$order_item = array();
|
||||
foreach ( $this->db->fetchAll($query) as $info ) {
|
||||
|
||||
$item_info = ($info["item_info"]) ? \json_decode($info["item_info"], true) : false;
|
||||
$in_cart = ($info["in_cart"]) ? \json_decode($info["in_cart"], true) : false;
|
||||
|
||||
|
||||
$info['item_info'] = $item_info;
|
||||
$info['in_cart'] = $in_cart;
|
||||
|
||||
$order_item[] = $info;
|
||||
}
|
||||
|
||||
return $order_item;
|
||||
}
|
||||
|
||||
//since we save the entire cart-structure for item_info, we need to re-construct it to make it easier to read and for various reporting/ email etc..
|
||||
protected function buildOrderItemInfo($item_type, array $item_info) {
|
||||
$new_info = [];
|
||||
|
||||
if($item_type == 'product') {
|
||||
|
||||
$new_info = System::stripProperty(array($item_info['info']), [
|
||||
"variant_option",
|
||||
'id',
|
||||
'productId',
|
||||
'priceUnit',
|
||||
'price',
|
||||
'currency',
|
||||
'lastUpdate',
|
||||
'warranty',
|
||||
'productName',
|
||||
'productUrl',
|
||||
'productModel',
|
||||
'productSummary',
|
||||
'marketPrice',
|
||||
'productImage',
|
||||
'brand',
|
||||
'visit',
|
||||
'rating',
|
||||
'reviewCount',
|
||||
'quantity',
|
||||
'productSKU',
|
||||
'hasVAT',
|
||||
'condition',
|
||||
'specialOffer',
|
||||
'specialOfferGroup',
|
||||
'productType',
|
||||
'thum_poster',
|
||||
'promotion_price',
|
||||
'addon'
|
||||
])[0];
|
||||
|
||||
//find variants bought
|
||||
if($new_info['config_count'] > 0) {
|
||||
|
||||
foreach ($item_info['in_cart'] as $_variant) {
|
||||
$new_info['variants'][] = array(
|
||||
"id" => $_variant['id'],
|
||||
"sku" => $_variant['sku'],
|
||||
"title" => $_variant['name'],
|
||||
"image" => ($_variant['image']) ? $_variant['image'] : $item_info['info']['productImage']["medium"],
|
||||
"url" => $item_info['info']['productUrl'],
|
||||
"attribute" => $_variant['attribute'],
|
||||
"price" => $_variant['price'],
|
||||
"addon_total" => 0,
|
||||
"currency" => $item_info['info']['currency'],
|
||||
"priceUnit" => $item_info['info']['priceUnit'],
|
||||
"quantity" => $_variant['quantity'],
|
||||
"buyer_note" => $_variant['buyer_note'],
|
||||
"addon" => [],
|
||||
"promotion" => [
|
||||
"list" => $item_info['info']['specialOffer'],
|
||||
"group" => (isset($_variant['promotion'])) ? $this->buildPromotionFromGroup( $item_info['info']['specialOfferGroup'], $_variant['promotion']) : $this->buildPromotionFromGroup( $item_info['info']['specialOfferGroup'], array() ),
|
||||
],
|
||||
//..any other
|
||||
);
|
||||
}
|
||||
|
||||
}else{
|
||||
|
||||
//product does not have variant
|
||||
$build_addon = [];
|
||||
$total_addon_price = 0;
|
||||
if(isset($item_info['info']['addon']) && isset($item_info['in_cart'][0]['addon'])) {
|
||||
$build_addon = $this->getSelectedAddon($item_info['info']['addon'], $item_info['in_cart'][0]['addon']);
|
||||
foreach ($build_addon as $addon) {
|
||||
$total_addon_price += $addon['price'] * $addon['quantity'];
|
||||
}
|
||||
}
|
||||
|
||||
$variant_in_cart = (isset($item_info['in_cart'][0])) ? $item_info['in_cart'][0] : false;
|
||||
|
||||
$new_info['variants'][] = array(
|
||||
"id" => 0,
|
||||
"sku" => $item_info['info']['productSKU'],
|
||||
"title" => $item_info['info']['productName'],
|
||||
"image" => $item_info['info']['productImage']["medium"],
|
||||
"url" => $item_info['info']['productUrl'],
|
||||
"attribute" => [],
|
||||
"price" => $item_info['info']['price'],
|
||||
"addon_total" => $total_addon_price,
|
||||
"currency" => $item_info['info']['currency'],
|
||||
"priceUnit" => $item_info['info']['priceUnit'],
|
||||
"quantity" => $variant_in_cart['quantity'],
|
||||
"buyer_note" => $variant_in_cart['buyer_note'],
|
||||
"addon" => $build_addon,
|
||||
"promotion" => [
|
||||
"list" => $item_info['info']['specialOffer'],
|
||||
"group" => (isset($variant_in_cart['promotion'])) ? $this->buildPromotionFromGroup( $item_info['info']['specialOfferGroup'], $variant_in_cart['promotion']) : $this->buildPromotionFromGroup( $item_info['info']['specialOfferGroup'], array() ),
|
||||
],
|
||||
//..any other
|
||||
);
|
||||
}
|
||||
|
||||
//
|
||||
return $new_info;
|
||||
}
|
||||
|
||||
if($item_type == 'combo') {
|
||||
|
||||
$new_info = [
|
||||
"price" => $item_info['info']['sale_price'],
|
||||
"quantity" => $item_info['in_cart']['quantity'],
|
||||
"buyer_note" => $item_info['in_cart']['buyer_note'],
|
||||
"url" => '',
|
||||
];
|
||||
|
||||
$new_info['product_list'] = System::stripProperty($item_info['info']['product_list'], [
|
||||
"variant_option",
|
||||
'id',
|
||||
'productId',
|
||||
'priceUnit',
|
||||
'price',
|
||||
'currency',
|
||||
'lastUpdate',
|
||||
'warranty',
|
||||
//'productName',
|
||||
//'productUrl',
|
||||
//'productModel',
|
||||
'productSummary',
|
||||
'marketPrice',
|
||||
'productImage',
|
||||
'brand',
|
||||
'visit',
|
||||
'rating',
|
||||
'reviewCount',
|
||||
'config_count',
|
||||
'quantity',
|
||||
//'productSKU',
|
||||
'hasVAT',
|
||||
'condition',
|
||||
'specialOffer',
|
||||
'specialOfferGroup',
|
||||
'productType',
|
||||
'thum_poster',
|
||||
'promotion_price',
|
||||
'addon',
|
||||
'imageCollection',
|
||||
'variants'
|
||||
]);
|
||||
|
||||
return $new_info;
|
||||
}
|
||||
|
||||
if($item_type == 'deal') {
|
||||
|
||||
$new_info = [
|
||||
"price" => $item_info['info']['price'],
|
||||
"quantity" => $item_info['in_cart']['quantity'],
|
||||
"buyer_note" => $item_info['in_cart']['buyer_note'],
|
||||
"url" => '/deal/'.$item_info['info']['id'],
|
||||
];
|
||||
|
||||
return $new_info;
|
||||
}
|
||||
|
||||
return $new_info;
|
||||
}
|
||||
|
||||
protected function getSelectedAddon($all_product_addons, $in_cart) {
|
||||
|
||||
if(!is_array($all_product_addons) || !is_array($in_cart)) return [];
|
||||
|
||||
$result = [];
|
||||
foreach ($all_product_addons as $item) {
|
||||
foreach ($in_cart as $selected) {
|
||||
if($item['addon_id'] == $selected['id']) {
|
||||
$result[] = [
|
||||
"id" => $selected['id'],
|
||||
"title" => $item['title'],
|
||||
"price" => $item['price'],
|
||||
"quantity" => (isset($selected['quantity'])) ? intval($selected['quantity']) : 1,
|
||||
"related_article_url" => $item['related_article_url'],
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
protected function buildPromotionFromGroup( $offer_group, $selected_one_promotion) {
|
||||
|
||||
if(!is_array($offer_group) && !is_array($selected_one_promotion)) return false;
|
||||
|
||||
$promotion = [];
|
||||
|
||||
foreach ($offer_group as $group) {
|
||||
if($group['type'] == 'one') {
|
||||
|
||||
$selected_promotion = [];
|
||||
foreach ($group['promotion'] as $promo) {
|
||||
foreach ($selected_one_promotion as $selected) {
|
||||
if($selected['promotion_id'] == $promo['id'] && $selected['group_id'] == $group['id']) {
|
||||
$selected_promotion[] = $promo;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$promotion[] = [
|
||||
"id" => $group['id'],
|
||||
"title" => $group['title'],
|
||||
"note" => $group['note'],
|
||||
"promotion" => $selected_promotion,
|
||||
];
|
||||
|
||||
|
||||
}else{
|
||||
$promotion[] = [
|
||||
"id" => $group['id'],
|
||||
"title" => $group['title'],
|
||||
"note" => $group['note'],
|
||||
"promotion" => $group['promotion'],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return $promotion;
|
||||
|
||||
}
|
||||
|
||||
|
||||
protected function getListFilterCondition($list_id) {
|
||||
|
||||
if($list_id == 'mine') {
|
||||
return [
|
||||
'assign_to' => ADMIN_ID,
|
||||
];
|
||||
}
|
||||
|
||||
if($list_id == 'new') {
|
||||
return [
|
||||
'status' => 'new',
|
||||
];
|
||||
}
|
||||
|
||||
if($list_id == 'unpaid') {
|
||||
return [
|
||||
'payment' => 'unpaid',
|
||||
];
|
||||
}
|
||||
|
||||
if($list_id == 'partially-paid') {
|
||||
return [
|
||||
'payment' => 'partially-paid',
|
||||
];
|
||||
}
|
||||
|
||||
if($list_id == 'unfulfilled') {
|
||||
return [
|
||||
'fullfillment' => 'unfulfilled',
|
||||
];
|
||||
}
|
||||
|
||||
if($list_id == 'partially-fulfilled') {
|
||||
return [
|
||||
'fullfillment' => 'partially-fulfilled',
|
||||
];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getStatusHistory($orderId){
|
||||
$query = $this->db->runQuery("
|
||||
select * from ".TB_ORDER_STATUS_HISTORY."
|
||||
WHERE order_id = ?
|
||||
order by id desc
|
||||
limit 100
|
||||
", ['d'], [ $orderId ] ) ;
|
||||
|
||||
return $this->db->fetchAll($query);
|
||||
}
|
||||
|
||||
public function getItemsForOrderList(array $list_ids, $fields = "*") {
|
||||
|
||||
if(!sizeof($list_ids)) return [];
|
||||
|
||||
list($parameterized_ids, $bind_types) = create_bind_sql_parameter_from_value_list($list_ids, 'int');
|
||||
|
||||
$query = $this->db->runQuery("
|
||||
SELECT ".$fields." FROM `".$this->tb_order_detail."`
|
||||
WHERE `order_id` IN (".$parameterized_ids.")
|
||||
", $bind_types, $list_ids);
|
||||
|
||||
$result = array();
|
||||
foreach ( $this->db->fetchAll($query) as $rs ) {
|
||||
|
||||
$item_info = unserialize($rs['item_info']);
|
||||
if( isset($item_info["info"]) && isset($item_info["in_cart"])) {
|
||||
$rs['item_info'] = $this->buildOrderItemInfo($rs['item_type'], $item_info );
|
||||
} else {
|
||||
$rs['item_info'] = null;
|
||||
}
|
||||
|
||||
$result[$rs['order_id']][] = $rs;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
protected function _buildQueryConditionExtend(array $conditions) : ?array
|
||||
{
|
||||
$catCondition = [];
|
||||
$bind_types = [];
|
||||
$bind_values = [];
|
||||
|
||||
/*
|
||||
$conditions = array(
|
||||
'orderCode' => '',
|
||||
'query' => '',
|
||||
'coupon' => '',
|
||||
'cus_id' => '',
|
||||
'province' => '',
|
||||
'district' => '',
|
||||
'folder' => '',
|
||||
'view_status' => '',
|
||||
'update_by' => '',
|
||||
'shipping_status' => '',
|
||||
'assign_to' => '',
|
||||
'from_date' => '',
|
||||
'to_date' => '',
|
||||
'from_hour' => '',
|
||||
'to_hour' => '',
|
||||
'excluded_ids' => '',
|
||||
'included_ids' => '',
|
||||
'payment' => '',
|
||||
'fullfillment' => '',
|
||||
'list' => '',
|
||||
);*/
|
||||
|
||||
|
||||
// merge with special list
|
||||
if(isset($conditions['list']) && $conditions['list']) {
|
||||
$list_condition = $this->getListFilterCondition($conditions['list']);
|
||||
if($list_condition) {
|
||||
// update and over-write any key in $conditions if exist
|
||||
foreach ($list_condition as $key => $value) {
|
||||
$conditions[$key] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(isset($conditions['orderCode']) && $conditions['orderCode']) {
|
||||
$orderCode = DataClean::makeInputSafe($conditions['orderCode'],DataType::INTEGER);
|
||||
$catCondition[] = " AND `orderId` LIKE '".$orderCode."%' ";
|
||||
}
|
||||
|
||||
if(isset($conditions['coupon']) && $conditions['coupon']) {
|
||||
$coupon = preg_replace("/[^a-z0-9_\-\.]/i","", $conditions['coupon']);
|
||||
if($coupon) $catCondition[] = " AND ( LENGTH(`order_discount`) > 10 AND `order_discount` LIKE '%".$coupon."%' ) ";
|
||||
}
|
||||
|
||||
if(isset($conditions['cus_id']) && $conditions['cus_id']) {
|
||||
$catCondition[] = " AND buyerId = '".intval($conditions['cus_id'])."'";
|
||||
}
|
||||
|
||||
if(isset($conditions['province']) && $conditions['province']) {
|
||||
$catCondition[] = " AND province = '".intval($conditions['province'])."'";
|
||||
}
|
||||
|
||||
if(isset($conditions['district']) && $conditions['district']) {
|
||||
$catCondition[] = " AND district = '".intval($conditions['district'])."'";
|
||||
}
|
||||
|
||||
if(isset($conditions['folder']) && $conditions['folder']) {
|
||||
$catCondition[] = " AND `folder` = '". preg_replace("/[^a-z0-9_\-\.]/i", "", $conditions['folder'])."' ";
|
||||
}
|
||||
|
||||
if(isset($conditions['payment']) && array_key_exists($conditions['payment'], OrderStatus::PAYMENT_STATUS ) ) {
|
||||
$catCondition[] = " AND `payment_status` = '". $this->db->escape($conditions['payment'])."' ";
|
||||
}
|
||||
|
||||
if(isset($conditions['fullfillment']) && array_key_exists($conditions['fullfillment'], OrderStatus::FULFILLMENT_STATUS) ) {
|
||||
$catCondition[] = " AND `fulfillment_status` = '". $this->db->escape($conditions['fullfillment'])."' ";
|
||||
}
|
||||
|
||||
if(isset($conditions['status']) && array_key_exists($conditions['status'], OrderStatus::ORDER_STATUS) ) {
|
||||
$catCondition[] = " AND `order_status` = '". $this->db->escape($conditions['status'])."' ";
|
||||
}
|
||||
|
||||
if(isset($conditions['view_status']) && $conditions['view_status']) {
|
||||
if($conditions['view_status'] == 'no-status') {
|
||||
$catCondition[] = " AND `status_id` = 0 ";
|
||||
} else {
|
||||
$catCondition[] = " AND `status_id` = '". intval($conditions['view_status'])."' ";
|
||||
}
|
||||
}
|
||||
|
||||
if(isset($conditions['update_by']) && $conditions['update_by']) {
|
||||
$catCondition[] = " AND `status_update_by` = '". $this->db->escape($conditions['update_by'])."' ";
|
||||
}
|
||||
|
||||
if(isset($conditions['shipping_status']) && $conditions['shipping_status']) {
|
||||
$catCondition[] = " AND `admin_shipping_status` = '". preg_replace("/[^a-z0-9_\-\.]/i", "", $conditions['shipping_status'])."' ";
|
||||
}
|
||||
|
||||
if(isset($conditions['assign_to']) && $conditions['assign_to']) {
|
||||
$catCondition[] = " AND `assign_to` = '". intval($conditions['assign_to'])."' ";
|
||||
}
|
||||
|
||||
//filter by date
|
||||
if(isset($conditions['from_date']) && $conditions['from_date']) {
|
||||
$catCondition[] = " AND order_time >= '". intval(strtotime(TimeManager::convert_date_from_javascript($conditions['from_date'])." 00:00"))."' ";
|
||||
}
|
||||
|
||||
if(isset($conditions['to_date']) && $conditions['to_date']) {
|
||||
$catCondition[] = " AND order_time <= '".strtotime(TimeManager::convert_date_from_javascript($conditions['to_date'])." 23:59")."' ";
|
||||
}
|
||||
|
||||
//filter by hour
|
||||
if(isset($conditions['from_hour']) && $conditions['from_hour']) {
|
||||
$catCondition[] = " AND `order_hour` >= '".intval($conditions['from_hour'])."' ";
|
||||
}
|
||||
if(isset($conditions['to_hour']) && $conditions['to_hour']) {
|
||||
$catCondition[] = " AND `order_hour` <= '".intval($conditions['to_hour'])."' ";
|
||||
}
|
||||
|
||||
if(isset($conditions["excluded_ids"]) && $conditions["excluded_ids"] ){
|
||||
$list_ids = filterNumber(explode(",", $conditions["excluded_ids"]));
|
||||
if(sizeof($list_ids)) $catCondition[] = " AND `orderId` NOT IN (".join(',', $list_ids ).") ";
|
||||
}
|
||||
|
||||
if(isset($conditions["included_ids"]) && $conditions["included_ids"] ){
|
||||
$list_ids = filterNumber(explode(",", $conditions["included_ids"]));
|
||||
if(sizeof($list_ids)) $catCondition[] = " AND `orderId` IN (".join(',', $list_ids ).") ";
|
||||
}
|
||||
|
||||
|
||||
return array( join(" ", $catCondition), $bind_types, $bind_values);
|
||||
}
|
||||
|
||||
|
||||
protected function beforeCreateItem(array $input_info) : AppResponse
|
||||
{
|
||||
$info = $input_info;
|
||||
|
||||
if(isset($info['file_external_url'])) {
|
||||
if($info['file_external_url'] && !Url::isUrlValid($info['file_external_url'])) {
|
||||
$info['file_external_url'] = '';
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$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 beforeUpdateItem($item_id, $current_item_info, $new_input_info) : AppResponse
|
||||
{
|
||||
$info = $new_input_info;
|
||||
|
||||
unset($info['id']);
|
||||
|
||||
if(isset($info['file_external_url']) && $info['file_external_url'] && !Url::isUrlValid($info['file_external_url'])) {
|
||||
$info['file_external_url'] = '';
|
||||
}
|
||||
|
||||
$info['last_update'] = CURRENT_TIME;
|
||||
$info['last_update_by'] = ADMIN_NAME;
|
||||
|
||||
return new AppResponse('ok', null, $info);
|
||||
}
|
||||
|
||||
protected function beforeDeleteItem($item_id, $item_info) : AppResponse
|
||||
{
|
||||
return new AppResponse('ok');
|
||||
}
|
||||
|
||||
protected function afterDeleteItem($item_id, $item_info)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
protected function afterCreateItem($new_item_id, $new_item_info)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
protected function afterUpdateItem($item_id, $old_item_info, $new_item_info)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
30
inc/Hura8/Components/Order/Model/OrderSearchModel.php
Normal file
30
inc/Hura8/Components/Order/Model/OrderSearchModel.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace Hura8\Components\Order\Model;
|
||||
|
||||
use Hura8\Interfaces\iSearch;
|
||||
use Hura8\System\Model\aSearchBaseModel;
|
||||
|
||||
|
||||
class OrderSearchModel extends aSearchBaseModel implements iSearch
|
||||
{
|
||||
|
||||
private $filter_fields = [
|
||||
"folder" => "tb_order.price",
|
||||
];
|
||||
|
||||
private $fulltext_fields = [
|
||||
"keywords" => ["tb_order.buyerName", ],
|
||||
];
|
||||
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct(
|
||||
"tb_order",
|
||||
$this->fulltext_fields,
|
||||
$this->filter_fields
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
94
inc/Hura8/Components/Order/Model/OrderStatusModel.php
Normal file
94
inc/Hura8/Components/Order/Model/OrderStatusModel.php
Normal file
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
|
||||
namespace Hura8\Components\Order\Model;
|
||||
|
||||
use Hura8\Database\iConnectDB;
|
||||
|
||||
class OrderStatusModel
|
||||
{
|
||||
|
||||
/* @var iConnectDB $db */
|
||||
protected $db;
|
||||
|
||||
protected $tb_status = 'tb_order_status';
|
||||
protected $tb_status_history = 'tb_order_status_history';
|
||||
|
||||
public function __construct() {
|
||||
$this->db = get_db('', ENABLE_DB_DEBUG);
|
||||
}
|
||||
|
||||
public function getStatusHistory($orderId){
|
||||
$query = $this->db->runQuery("
|
||||
select * from ".$this->tb_status_history."
|
||||
WHERE order_id = ?
|
||||
order by id desc
|
||||
limit 100
|
||||
", ['d'], [ $orderId ] ) ;
|
||||
|
||||
return $this->db->fetchAll($query);
|
||||
}
|
||||
|
||||
public function createHistory($order_id, $status_type = 'order', $system_status = '', $comment = '', array $data= [], array $other_info = []) {
|
||||
|
||||
$info = $other_info;
|
||||
|
||||
$info['order_id'] = $order_id;
|
||||
$info['status_type'] = $status_type;
|
||||
$info['system_status'] = $system_status;
|
||||
$info['data'] = \json_encode($data);
|
||||
$info['comment'] = substr($comment, 0, 150);
|
||||
$info['create_time'] = CURRENT_TIME;
|
||||
$info['create_by'] = (defined('ADMIN_NAME')) ? ADMIN_NAME : '';
|
||||
|
||||
return $this->db->insert($this->tb_status_history, $info);
|
||||
}
|
||||
|
||||
public function getCustomerCancelStatusId() {
|
||||
$info = $this->db->select($this->tb_status, ['id'], [
|
||||
"system_status" => ["=", "cancel"],
|
||||
'message' => 'Khách hàng hủy',
|
||||
], '', 1);
|
||||
|
||||
if($info) {
|
||||
return $info['id'];
|
||||
}
|
||||
|
||||
// create and return
|
||||
return $this->create([
|
||||
'message' => 'Khách hàng hủy',
|
||||
'system_status' => "cancel",
|
||||
'create_time' => CURRENT_TIME,
|
||||
'create_by' => 'System',
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
public function getInfo($status_id) {
|
||||
return $this->db->getItemInfo($this->tb_status, $status_id, 'id');
|
||||
}
|
||||
|
||||
public function getAll() {
|
||||
$query = $this->db->runQuery(" select * from ".$this->tb_status." order by `id` desc ") ;
|
||||
$item_list = array();
|
||||
foreach ( $this->db->fetchAll($query) as $rs ) {
|
||||
if(!$rs['system_status']) $rs['system_status'] = 'new';
|
||||
//$rs['system_order_status'] = System::$ORDER_STATUS[$rs['system_status']];
|
||||
$item_list[] = $rs;
|
||||
}
|
||||
|
||||
return $item_list;
|
||||
}
|
||||
|
||||
public function delete($id) {
|
||||
return $this->db->runQuery("DELETE FROM ".$this->tb_status." WHERE `id` = ? limit 1 ", ['d'], [ $id ]) ;
|
||||
}
|
||||
|
||||
public function update($id, array $info) {
|
||||
return $this->db->update($this->tb_status, $info, ['id' => $id]);
|
||||
}
|
||||
|
||||
public function create(array $info) {
|
||||
return $this->db->insert($this->tb_status, $info);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user