<?php
namespace TourOperatorProAdmin\app\models;
defined('_TOUROPERATOR_EXEC') or die;

use TourOperatorPro\Factory;
use TourOperatorPro\Mvc\Model\ModelForm;
use TourOperatorPro\Form\Form;
use TourOperatorPro\Mvc\Model\ModelList;
use TourOperatorPro\Registry\Registry;
use TourOperatorProFrontend\app\helpers\FrontendHelper;

class CouponsModel extends ModelList
{
    /**
     * @var string
     */
    public $context = "coupons";
    /**
     * @var string
     */
    public $table_name = "coupon";

    public function getList()
    {
        $list_coupon =  parent::getList(); // TODO: Change the autogenerated stub
        foreach ($list_coupon as &$coupon) {
            $query = $this->_db->getQuery(true);
            $query->clear()
                ->select('DISTINCT tour.*')
                ->from($this->getTableName('tour').' AS tour')
                ->leftJoin($this->getTableName('tourproperty').' AS tour_property ON tour_property.tour_id=tour.id')
                ->where('tour_property.object_id='.(int)$coupon->id)
                ->where('tour_property.type='.$query->quote(FrontendHelper::getTourCouponKey()))
                ;
            $coupon->list_tour=$this->_db->setQuery($query)->loadObjectList();
        }
        return $list_coupon;
    }

}
