<?php
namespace WpBookingHotelAdmin\app\models;
defined('_WPBOOKINGHOTEL_EXEC') or die;

use WpBookingHotel\Factory;
use WpBookingHotel\Mvc\Model\ModelForm;
use WpBookingHotel\Form\Form;
use WpBookingHotel\Mvc\Model\ModelList;
use WpBookingHotel\Registry\Registry;
use WpBookingHotelFrontend\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 hotel.*')
                ->from($this->getTableName('hotel').' AS hotel')
                ->leftJoin($this->getTableName('hotelproperty').' AS hotel_property ON hotel_property.hotel_id=hotel.id')
                ->where('hotel_property.object_id='.(int)$coupon->id)
                ->where('hotel_property.type='.$query->quote(FrontendHelper::getHotelCouponKey()))
                ;
            $coupon->list_hotel=$this->_db->setQuery($query)->loadObjectList();
        }
        return $list_coupon;
    }

}
