<?php

/**
 * @since 2.0.0
 * @package B1_Accounting
 * @subpackage B1_Accounting/includes
 * @author Site.pro <apskaita@site.pro>
 * @link https://www.site.pro
 */

class B1_Accounting_Order
{

    public static function assignB1OrderId($b1OrderId, $orderId)
    {
        global $wpdb;
        $postsTable = "{$wpdb->prefix}posts";
        if (!$b1OrderId) {
            // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
            $wpdb->query($wpdb->prepare("UPDATE %i SET b1_reference_id = NULL WHERE ID = %d", $postsTable, $orderId));
        } else {
            // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
            $wpdb->query($wpdb->prepare("UPDATE %i SET b1_reference_id = %d WHERE ID = %d", $postsTable, $b1OrderId, $orderId));
        }
    }

    public static function fetch_all_orders($sync_from, $sync_order_status, $iteration, $sync_to = null)
    {
        global $wpdb;
        $postsTableName = "{$wpdb->prefix}posts";
        $postsMetaTableName = "{$wpdb->prefix}postmeta";

        if(!empty($sync_to) && $sync_to !== '') {
            // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
            return $wpdb->get_results(
                $wpdb->prepare(
                    "SELECT p.* FROM %i p inner join %i pm on pm.post_id=p.ID and pm.meta_key=%s WHERE p.b1_reference_id IS NULL AND p.post_type = %s AND p.post_status = %s  AND p.post_date >= %s AND p.post_date <= %s order by pm.meta_value LIMIT %d",
                    $postsTableName, $postsMetaTableName, '_date_paid','shop_order', $sync_order_status, $sync_from, $sync_to, $iteration)
            );
        } else {
            // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
            return $wpdb->get_results(
                $wpdb->prepare(
                    "SELECT p.* FROM %i p inner join %i pm on pm.post_id=p.ID and pm.meta_key=%s WHERE p.b1_reference_id IS NULL AND p.post_type = %s AND p.post_status = %s  AND p.post_date >= %s order by pm.meta_value LIMIT %d",
                    $postsTableName, $postsMetaTableName, '_date_paid', 'shop_order', $sync_order_status,$sync_from, $iteration)
            );
        }

    }

    public static function reset_order_b1_reference_id()
    {
        global $wpdb;
        $postsTableName = "{$wpdb->prefix}posts";
        // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
        $wpdb->query($wpdb->prepare("UPDATE %i SET `b1_reference_id` = NULL WHERE `post_type` = %s", $postsTableName, 'shop_order'));
    }

    public static function get_data_by_sql($sql, $orderId)
    {
        if (isset($sql) && !empty($sql)) {
            global $wpdb;
            // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
            return $wpdb->get_var($wpdb->prepare($sql, $orderId));
        } else {
            return false;
        }
    }

    public static function get_product_order_id($orderId, $productId)
    {
        global $wpdb;
        $wcItemsTableName = "{$wpdb->prefix}woocommerce_order_items";
        $wcItemMetaTableName = "{$wpdb->prefix}woocommerce_order_itemmeta";
        // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
        return $wpdb->get_var(
            $wpdb->prepare(
                "SELECT woi.order_item_id 
                FROM %i woi
                LEFT JOIN %i woim on woi.order_item_id = woim.order_item_id
                WHERE woi.`order_id` = %d AND (woim.`meta_key` = %s  AND woim.`meta_value` = %d)",
                $wcItemsTableName, $wcItemMetaTableName, $orderId, '_product_id', $productId)
        );
    }

    public static function get_product_variation_order_id($orderId, $variationId)
    {
        global $wpdb;
        $wcItemsTableName = "{$wpdb->prefix}woocommerce_order_items";
        $wcItemMetaTableName = "{$wpdb->prefix}woocommerce_order_itemmeta";
        // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
        return $wpdb->get_var(
            $wpdb->prepare(
                "SELECT woi.order_item_id 
                FROM %i woi
                LEFT JOIN %i woim on woi.order_item_id = woim.order_item_id
                WHERE woi.`order_id` = %d AND (woim.`meta_key` = %s  AND woim.`meta_value` = %d)",
                $wcItemsTableName, $wcItemMetaTableName, $orderId, '_variation_id', $variationId)
        );
    }

    public static function getOrderB1rReference($orderId)
    {
        global $wpdb;
        $postsTableName = "{$wpdb->prefix}posts";
        // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
        return $wpdb->get_var($wpdb->prepare("SELECT b1_reference_id from %i WHERE ID = %d", $postsTableName, $orderId));
    }

}
