| Current Path : /var/www/html/app/code/FlipsDigital/HappyMind/Cron/ |
| Current File : /var/www/html/app/code/FlipsDigital/HappyMind/Cron/PushCustomerOrderData.php |
<?php
namespace FlipsDigital\HappyMind\Cron;
class PushCustomerOrderData extends AbstractCron
{
protected $_className = __CLASS__;
public function __construct(
\Magento\Framework\ObjectManagerInterface $objectManagerInterface,
\FlipsDigital\CustomerTextbookList\Model\CustomerTextbookListFactory $customerTextbookListFactory,
\FlipsDigital\EducationInstitution\Model\EducationInstitutionFactory $educationInstitutionFactory,
\FlipsDigital\TextbookList\Model\TextbookListFactory $textbookListFactory,
\FlipsDigital\TextbookListBook\Model\TextbookListBookFactory $textbookListBookFactory,
\FlipsDigital\TextbookListStudent\Model\TextbookListStudentFactory $textbookListStudentFactory,
\Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
\Magento\Framework\Stdlib\DateTime\TimezoneInterface $timezoneInterface,
\Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface,
\Magento\Store\Model\StoreManagerInterface $storeManager
) {
parent::__construct($objectManagerInterface);
$this->_ctlFactory = $customerTextbookListFactory;
$this->_eiFactory = $educationInstitutionFactory;
$this->_tlFactory = $textbookListFactory;
$this->_tlbFactory = $textbookListBookFactory;
$this->_tlsFactory = $textbookListStudentFactory;
$this->orderRepository = $orderRepository;
$this->timezoneInterface = $timezoneInterface;
$this->_customerRepositoryInterface = $customerRepositoryInterface;
$this->_storeManager = $storeManager;
}
public function execute()
{
if( $this->_connection === false ) return;
$this->_logger->info("START: " . __METHOD__ );
$objCTL = $this->_ctlFactory->create();
$objEI = $this->_eiFactory->create();
$objTL = $this->_tlFactory->create();
$objTLB = $this->_tlbFactory->create();
$objTLS = $this->_tlsFactory->create();
$collectionCTL = $objCTL->getCollection();
$collectionCTL->addAttributeToSelect('*');
$collectionCTL->addFieldToFilter('mers_stock_sync_g_magento_order_id', ['null' => true]);
$collectionCTL->addFieldToFilter('order_id', ['neq' => 'NULL']);
$collectionCTL->setOrder('entity_id','ASC');
$collectionCTL->setPageSize(10);
$arrData = [];
if ($collectionCTL->count()){
$arrEI = [];
$arrTL = [];
$arrTLS = [];
$arrTLB = [];
foreach ($collectionCTL->getItems() as $key => $value) {
$arrOrder = $this->_getOrderData();
$order = $this->orderRepository->get($value->getData('order_id'));
$customer = $this->_customerRepositoryInterface->getById($value->getData('customer_id'));
if($customer){
$arrOrder["Magento_ORDER_ID"] = $value->getData('order_id');
$arrOrder["Magento_Parent_Login_ID"] = $value->getData('customer_id');
$arrOrder["Magento_Student_Name"] = $value->getData('chi_name');
if ($value->getData('eng_name')){
$arrOrder["Magento_Student_Name"] .= " ".$value->getData('eng_name');
}
$arrOrder["Magento_Student_Class"] = $value->getData('student_class');
$arrOrder["Magento_Student_No"] = $value->getData('student_no');
switch($value->getData('payment_method')) {
case 'payment_alipay_hk':
$paymentName = 'Alipay HK';
break;
case 'payment_alipay_cn':
$paymentName = 'Alipay CN';
break;
case 'payment_tng_fps':
$paymentName = 'Tap & Go / FPS';
break;
case 'payment_visa_master':
$paymentName = 'Visa/Master';
break;
case 'payment_wechatpay':
$paymentName = 'WeChatPay';
break;
default:
$paymentName = '「7-ELEVEN」便利店';
}
$arrOrder["Magento_Payment_Method"] = $paymentName;
$education_institution_id = $value->getData('education_institution_id');
$booklist_id = $value->getData('booklist_id');
$booklist_student_id = $value->getData('booklist_student_id');
if (!isset($arrEI[$education_institution_id])){
$modelEI = $objEI->load($education_institution_id);
if ($modelEI){
$arrEI[$education_institution_id] = $modelEI->getData();
}
}
if (isset($arrEI[$education_institution_id])){
$arrOrder["CUS"] = $arrEI[$education_institution_id]['mers_stock_source_v_cus_cus'];
$arrOrder["PCAT"] = $arrEI[$education_institution_id]['group_id'];
$arrOrder["Magento_School_Name"] = $arrEI[$education_institution_id]['name'];
if (
$arrEI[$education_institution_id]['name'] != $arrEI[$education_institution_id]['chinese_name'] &&
$arrEI[$education_institution_id]['chinese_name']
){
$arrOrder["Magento_School_Name"] .= " ".$arrEI[$education_institution_id]['chinese_name'];
}
}
if (!isset($arrTL[$booklist_id])){
$modelTL = $objTL->load($booklist_id);
if ($modelTL){
$arrTL[$booklist_id] = $modelTL->getData();
}
}
if (isset($arrTL[$booklist_id])){
$arrOrder["GL_CODE"] = $arrTL[$booklist_id]['mers_stock_source_g_gl_code'];
}
if (!isset($arrTLS[$booklist_student_id])){
$modelTLS = $objTLS->load($booklist_student_id);
if ($modelTLS){
$arrTLS[$booklist_student_id] = $modelTLS->getData();
}
}
if (isset($arrTLS[$booklist_student_id]['mers_stock_source_g_gl_student_barcode711'])){
$arrOrder["BARCODE711"] = $arrTLS[$booklist_student_id]['mers_stock_source_g_gl_student_barcode711'];
}
if ($order){
$orderHandlingFee = number_format($order->getShippingAmount(), 2, null, '');
$orderFinalAmount = number_format($order->getGrandTotal(), 2, null, '');
$arrOrder["Magento_ORDER_NO"] = $order->getRealOrderId();
$arrOrder["Magento_ORDER_DATE"] = $this->timezoneInterface->date(new \DateTime($order->getCreatedAt()))->format("Y-m-d H:i:s");
$arrOrder["Magento_ORDER_Amount"] = $orderFinalAmount - $orderHandlingFee;
$arrOrder["Magento_Handling_Fee"] = $orderHandlingFee;
$arrOrder["Magento_Final_Amount"] = $orderFinalAmount;
if ($arrTL[$booklist_id]['mers_stock_source_g_gl_plastic_bag_levy']){
$arrOrder["PLASTIC_BAG_LEVY"] = $arrTL[$booklist_id]['mers_stock_source_g_gl_plastic_bag_levy'];
}
if (isset($arrOrder["BARCODE711"])){
$arrOrder["Magento_Barcode_711"] = '07202'.$arrOrder["BARCODE711"].str_pad($arrOrder["Magento_Final_Amount"] * 100, 10, "0", STR_PAD_LEFT);
}
}
if ($customer){
$arrOrder["Magento_Parent_Name"] = $customer->getFirstname().' '.$customer->getLastname();
$arrOrder["Magento_Parent_Email"] = $customer->getEmail();
}
$arrOrder['Magento_INPUT_DATE'] = $this->timezoneInterface->date()->format("Y-m-d H:i:s");
$arrOrder['Magento_EDIT_DATE'] = $arrOrder['Magento_INPUT_DATE'];
$arrOrder['Magento_ORDER_Remark'] = 'Production - '.$this->_storeManager->getStore()->getBaseUrl();
$sql = $this->_genOrderInsertSql("G_MAGENTO_ORDER", $arrOrder);
$stmt = $this->_executeQuery($sql, array_values($arrOrder));
sqlsrv_next_result($stmt);
sqlsrv_fetch($stmt);
$lastInsertId = sqlsrv_get_field($stmt, 0);
if ($lastInsertId){
$this->_logger->info("Push Data: Order Id - " . $order->getId() . " " . $order->getRealOrderId() . " | G_MAGENTO_ORDER - " . $lastInsertId);
$arrCTLData = $value->getData();
$arrCTLData['mers_stock_sync_g_magento_order_id'] = $lastInsertId;
$arrCTLData['mers_stock_sync_g_magento_order_time'] = $this->timezoneInterface->date()->format("Y-m-d H:i:s");
$value->setData($arrCTLData);
$value->save();
if (!isset($arrTLB[$booklist_id])){
$collectionTLB = $objTLB->getCollection();
$collectionTLB->addAttributeToSelect('*');
$collectionTLB->addFieldToFilter('booklist_id', $booklist_id);
if ($collectionTLB->count()){
foreach ($collectionTLB->getItems() as $key1 => $value1) {
if (!isset($arrTLB[$booklist_id][$value1->getData('product_id')])){
$arrTLB[$booklist_id][$value1->getData('product_id')] = $value1->getData();
}
}
}
}
if (isset($arrTLB[$booklist_id])){
foreach ($arrTLB[$booklist_id] as $key1 => $value1) {
$arrOrderDetailsData = $this->_getOrderDetailsData();
$arrOrderDetailsData['BOOKLIST_SECTION'] = $value1['mers_stock_source_table'];
$arrOrderDetailsData['PRINT_SEQ'] = $value1['print_seq'];
$arrOrderDetailsData['CODE'] = $value1['mers_stock_source_s_product_code'];
$arrOrderDetailsData['BOOK'] = $value1['name'];
$arrOrderDetailsData['PRICE_ORIGINAL'] = $value1['price_original'];
$arrOrderDetailsData['PRICE_SELLING'] = $value1['price_selling'];
$arrOrderDetailsData['PRICE_NOT_BUY_ALL'] = $value1['price_not_buy_all'];
$arrOrderDetailsData['MUST_BUY'] = ($value1['must_buy'])?1:0;
$arrOrderDetailsData['Magento_ORDER_ID'] = $arrOrder['Magento_ORDER_ID'];
$arrOrderDetailsData['Magento_ORDER_NO'] = $arrOrder['Magento_ORDER_NO'];
$arrOrderDetailsData['Magento_INPUT_DATE'] = $arrOrder['Magento_INPUT_DATE'];
$arrOrderDetailsData['Magento_EDIT_DATE'] = $arrOrderDetailsData['Magento_INPUT_DATE'];
foreach ($order->getAllItems() as $key2 => $value2) {
if ($value2->getProductId() == $value1['product_id']){
$arrOrderDetailsData['Magento_ORDER_Details_ID'] = $value2->getItemId();
$arrOrderDetailsData['Magento_Qty'] = (int)$value2->getQtyOrdered();
$arrOrderDetailsData['Magento_Unit_Price'] = $value2->getPrice();
$arrOrderDetailsData['Magento_Amount'] = $value2->getBaseRowTotal();
break;
}
}
$sql = $this->_genOrderInsertSql("G_MAGENTO_ORDER_Details", $arrOrderDetailsData);
$stmt = $this->_executeQuery($sql, array_values($arrOrderDetailsData));
sqlsrv_next_result($stmt);
sqlsrv_fetch($stmt);
$lastDetailInsertId = sqlsrv_get_field($stmt, 0);
$this->_logger->info("Push Data: Order Id - " . $order->getId() . " " . $order->getRealOrderId() . " Product Id - ".$value1['product_id']." | G_MAGENTO_ORDER_Details - " . $lastDetailInsertId);
}
}
}
}else{
$this->_logger->info("Customer does not exist - " . $order->getId() . " " . $order->getRealOrderId() . " | Customer Id - " . $value->getData('customer_id'));
}
}
}
$this->_logger->info("END: " . __METHOD__ );
}
public function _getOrderData()
{
return [
"GL_CODE" => "",
"BARCODE711" => "",
"CUS" => "",
"PCAT"=> "",
"PLASTIC_BAG_LEVY" => 1,
"Magento_ORDER_ID" => "",
"Magento_ORDER_NO" => "",
"Magento_ORDER_DATE" => "",
"Magento_Barcode_711" => "",
"Magento_Barcode_CircleK" => "",
"Magento_Parent_Login_ID" => "",
"Magento_Parent_Name" => "",
"Magento_Parent_Tel" => "",
"Magento_Parent_Email" => "",
"Magento_School_Name" => "",
"Magento_Student_Name" => "",
"Magento_Student_Class" => "",
"Magento_Student_No" => "",
"Magento_ORDER_Amount" => "",
"Magento_Payment_Method" => "", // New table field from 20220809
"Magento_Handling_Fee" => 0, // New table field from 20220809
"Magento_Final_Amount" => 0, // New table field from 20220809
"Magento_Delivery_Address" => "",
"Magento_ORDER_Remark" => "",
"Magento_RECORD_STATUS" => "",
"Magento_INPUT_DATE" => "",
"Magento_EDIT_DATE" => "",
"Magento_Student_Name_Chinese" => "",
"Magento_Student_Name_English" => "",
];
}
public function _getOrderDetailsData()
{
return [
"BOOKLIST_SECTION" => "",
"PRINT_SEQ" => "",
"CODE" => "",
"BOOK" => "",
"BOOK_REMARK" => "",
"PRICE_ORIGINAL" => "",
"PRICE_SELLING" => "",
"PRICE_NOT_BUY_ALL" => "",
"MUST_BUY" => "",
"Magento_ORDER_ID" => "",
"Magento_ORDER_NO" => "",
"Magento_ORDER_Details_ID" => 0,
"Magento_Qty" => 0,
"Magento_Unit_Price" => 0,
"Magento_Amount" => 0,
"Magento_ORDER_Details_Remark" => "",
"Magento_RECORD_STATUS" => "",
"Magento_INPUT_DATE" => "",
"Magento_EDIT_DATE" => "",
];
}
public function _genOrderInsertSql($table, $data)
{
$sql = "INSERT INTO ".$table." (" . implode(",", array_keys($data)) . ") VALUES (" . implode(",", array_fill(0, count($data), '?')) . ");SELECT SCOPE_IDENTITY();";
return $sql;
}
}