<?php

use system\entity\marketplace\marketplacePrice;
use system\entity\filter\baseFilter;


class DModelMarketplaceMktPrice extends ModelBase
{

    public function __construct($registry)
    {
        parent::__construct($registry,
            MarketplacePrice::class
        );
        $this->load->dmodel('marketplace/mktadjustment');
    }

    /**
     * @param $tipo_integracao
     * @return array|bool
     */
    public function getMktPriceAdjustments($tipo_integracao)
    {
        $baseFilter = new BaseFilter();
        $baseFilter->setWhere('tipo_integracao', Type_OperationsComparation::Equals, $tipo_integracao);
        $prices = $this->findBy($baseFilter);

        if ($prices) {

            foreach ($prices as $price) {

                $baseFilter = new BaseFilter();
                $baseFilter->setWhere('marketplace_price_id', Type_OperationsComparation::Equals, $price->getMarketplacePriceId());

                $adjustments = $this->dmodel_marketplace_mktadjustment->findBy($baseFilter);

                if ($adjustments) {
                    $price->setMarketplacePriceAdjustments($adjustments);
                }

            }

            return $prices;
        }

        return false;
    }

    /**
     * @param $tipo_integracao
     * @return bool|Entity
     * @throws exception
     */
    public function getStandardMktPrice($tipo_integracao)
    {
        $baseFilter = new BaseFilter();
        $baseFilter->setAndWhere([
            'tipo_integracao = ' . $tipo_integracao,
            'standard = ' . Type_Status::Tp_Habilitado
        ]);

        $price = $this->findByOne($baseFilter);

        if ($price) {

            $baseFilter = new BaseFilter();
            $baseFilter->setWhere('marketplace_price_id', Type_OperationsComparation::Equals, $price->getMarketplacePriceId());

            $adjustments = $this->dmodel_marketplace_mktadjustment->findBy($baseFilter);

            if ($adjustments) {
                $price->setMarketplacePriceAdjustments($adjustments);
            }

            return $price;
        }

        return false;
    }

    /**
     * @param $marketplace_price_id
     * @param $product_id
     * @param $tipo_marketplace
     * @return bool|Entity|mixed
     * @throws exception
     */
    public function getMktPriceOrStandard($marketplace_price_id, $product_id, $tipo_marketplace)
    {
        if (($marketplace_price_id != '') && (!is_null($marketplace_price_id))) {
            $baseFilter = new BaseFilter();
            $baseFilter->setAndWhere([
                'marketplace_price_id = ' . $marketplace_price_id,
            ]);

            $result = $this->findByOne($baseFilter);
        } else {

            $result = $this->getMktPriceProduct($product_id, $tipo_marketplace);

            if (!$result) {
                $result = $this->getStandardMktPrice($tipo_marketplace);
            }
        }

        return $result;
    }

    /**
     * @param $product_id
     * @param $tipo_marketplace
     * @return mixed
     */
    public function getMktPriceProduct($product_id, $tipo_marketplace)
    {
        $sql = "SELECT
                  mp.marketplace_price_id AS marketplace_price_id
                FROM oc_marketplace_product mp
                WHERE mp.tipo_integracao = $tipo_marketplace AND mp.product_key_id = $product_id";

        $result = $this->db->query($sql);

        if ((count($result->rows) > 0) && !is_null($result->row['marketplace_price_id'])) {
            $baseFilter = new BaseFilter();
            $baseFilter->setAndWhere([
                'marketplace_price_id = ' . $result->row['marketplace_price_id'],
            ]);
            $result = $this->findByOne($baseFilter);

            return $result;
        }

        return false;
    }

}