<?php

class ControllerExtensionShippingInpostShipping extends Controller
{
    public function index()
    {
        $data = json_decode($_POST['inpostHookData'], 1);

        $this->load->model('extension/shipping/inpost_shipping');
        $this->model_extension_shipping_inpost_shipping->updateShipmentStatus($data);

        if (($data['event'] == 'shipment_delivered' || $data['event'] == 'delivered')  && !empty($this->config->get('shipping_inpost_shipping_settings')['order_status_update_ondelivery'])) {
            $this->load->model('checkout/order');
            $order_id = $this->db->query("SELECT * FROM `".DB_PREFIX."inpost_shipping_shipments` WHERE idInpost = '".(int)$data['payload']['shipment_id']."'")->row['reference'];

            $this->model_checkout_order->addOrderHistory($order_id, $this->config->get('shipping_inpost_shipping_settings')['order_status_update_ondelivery'], '', true);
        }
    }

    public function cron()
    {
        $this->load->model('extension/shipping/inpost_shipping');
        $this->model_extension_shipping_inpost_shipping->syncShipmentStatuses(
            $this->config->get('shipping_inpost_shipping_sandbox_mode'),
            $this,
        );
    }
}