Your IP : 216.73.216.220


Current Path : /var/www/html/app/code/FlipsDigital/HappyMind/Cron/
Upload File :
Current File : /var/www/html/app/code/FlipsDigital/HappyMind/Cron/SyncCustomerTextbookListPaymentData.php

<?php
namespace FlipsDigital\HappyMind\Cron;
use Magento\Sales\Model\Order;

class SyncCustomerTextbookListPaymentData 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
    ) {
        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;
    }
    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('mers_stock_sync_g_magento_order_id', ['neq' => 'NULL']);
        $collectionCTL->addFieldToFilter('order_id', ['neq' => 'NULL']);
        $collectionCTL->addFieldToFilter('payment_method', ['eq' => '711']);
        $collectionCTL->addFieldToFilter('payment_amount', ['eq' => 0]);
        $arrData = [];

        if ($collectionCTL->count()){
            foreach ($collectionCTL->getItems() as $key => $value) {
                $booklist_student_id = $value->getData('booklist_student_id');
                // $booklist_student_id = '486';
                $modelTLS = $objTLS->load($booklist_student_id);
                $studentData = $modelTLS->getData();
                // var_dump($studentData);
                $studentBarcode = $studentData['mers_stock_source_g_gl_student_barcode711'];
                // $studentBarcode = '2103017205285';

                $query = "SELECT * FROM [MERS_STOCK].[dbo].[VIEW_G_DAILY_REPORT_711] WHERE ACCOUNT_NO = '".$studentBarcode."'";
                // echo $query;
                $arrRawData = $this->_executeFetchArray($query);
                if(count($arrRawData)){
                    foreach ($arrRawData as $key2 => $value2) {
                        // $value->setPaymentMethod('711');
                        $value->setPaymentAmount($value2['AMOUNT']);
                        $value->setPaymentDate($value2['INPUT_DATE']);
                        $value->save();
                    }
    
                    $orderId = $value->getData('order_id');
                    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
                    $order = $objectManager->create('\Magento\Sales\Model\Order')->load($orderId);
                    if ($order->canInvoice()) {
                        $invoice = $objectManager->create('Magento\Sales\Model\Service\InvoiceService')->prepareInvoice($order);
                    
                        if (!$invoice->getTotalQty()) {
                            throw new \Magento\Framework\Exception\LocalizedException(
                                __('You can\'t create an invoice without products.')
                            );
                        }
                    
                        $invoice->setRequestedCaptureCase(\Magento\Sales\Model\Order\Invoice::CAPTURE_OFFLINE);
                        $invoice->register();
                    
                        $transaction = $objectManager->create('Magento\Framework\DB\Transaction')
                        ->addObject($invoice)
                        ->addObject($invoice->getOrder());
                        $transaction->save();
                    
                        // $this->invoiceSender->send($invoice); // Send Invoice email
                        // $order->addStatusHistoryComment(__('Invoice generated #%1.', $invoice->getId()))->setIsCustomerNotified(true)->save();
    
                        $orderState = Order::STATE_PROCESSING;
                        $order->setState($orderState)->setStatus(Order::STATE_PROCESSING);
                        $order->save();
                    }
                    $this->_logger->info('Order ID: '.$orderId.' payment data updated.');
                }else{
                    $this->_logger->info('Student ID: '.$booklist_student_id.' not found in [MERS_STOCK].[dbo].[VIEW_G_DAILY_REPORT_711] table.');
                }
            }
        }else{
            $this->_logger->info("No result.");
        }
        $this->_logger->info("END: " . __METHOD__ );

        // // Check if order can be shipped or has already shipped
        // if (!$order->canShip()) {
        //     throw new \Magento\Framework\Exception\LocalizedException(
        //         __('You can\'t create an shipment.')
        //     );
        // }

        // // Initialize the order shipment object
        // $convertOrder = $objectManager->create('Magento\Sales\Model\Convert\Order');
        // $shipment = $convertOrder->toShipment($order);

        // // Loop through order items
        // foreach ($order->getAllItems() AS $orderItem) {
        //     // Check if order item has qty to ship or is virtual
        //     if (! $orderItem->getQtyToShip() || $orderItem->getIsVirtual()) {
        //         continue;
        //     }

        //     $qtyShipped = $orderItem->getQtyToShip();

        //     // Create shipment item with qty
        //     $shipmentItem = $convertOrder->itemToShipmentItem($orderItem)->setQty($qtyShipped);

        //     // Add shipment item to shipment
        //     $shipment->addItem($shipmentItem);
        // }

        // // Register shipment
        // $shipment->register();
        // $shipment->getOrder()->setIsInProcess(true);

        // try {
        //     // Save created shipment and order
        //     $shipment->save();
        //     $shipment->getOrder()->save();

        //     // Send email
        //     $objectManager->create('Magento\Shipping\Model\ShipmentNotifier')->notify($shipment);

        //     $shipment->save();
        // } catch (\Exception $e) {
        //     throw new \Magento\Framework\Exception\LocalizedException(
        //         __($e->getMessage())
        //     );
        // }

        // $query = 'SELECT * FROM [MERS_STOCK].[dbo].[G_GL_GROUP_OPTION] where GL_ID = 23122 and GL_GROUP_ID = 30550 order by PRINT_SEQ+0 ASC';
    }
}