<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace ArtabanMarketplace\Loop;

use ArtabanMarketplace\Model\Map\AmpSellerOrderTableMap;
use ArtabanMarketplace\Model\AmpSellerOrderQuery;
use ArtabanMarketplace\Model\AmpSellerQuery;
use ArtabanMarketplace\Model\AmpSellerAdminQuery;
use Propel\Runtime\ActiveQuery\Criteria;
use Propel\Runtime\ActiveQuery\Join;
use Thelia\Core\Template\Loop\Argument\Argument;
use Thelia\Core\Template\Loop\Argument\ArgumentCollection;
use Thelia\Core\Template\Loop\Customer;
use Thelia\Model\OrderQuery;
use Thelia\Model\AdminQuery;
use Thelia\Model\Map\OrderTableMap;
use Thelia\Model\Map\CustomerTableMap;
use Propel\Runtime\Propel;
use Thelia\Core\Template\Element\BaseLoop;
use Thelia\Core\Template\Element\LoopResult;
use Thelia\Core\Template\Element\LoopResultRow;

/**
 * Description of SellerCustomersLoop
 *
 * @author LENGY
 */
class SellerCustomersLoop extends Customer {

    /**
     * @inheritDoc
     */
    public function getArgDefinitions() {
        /** @var ArgumentCollection $arguments */
        $arguments = parent::getArgDefinitions();

        $arguments->addArgument(
                Argument::createIntListTypeArgument("amp_seller_id")
        );

        return $arguments;
    }

    public function buildModelCriteria() {
        $query = parent::buildModelCriteria();

        if (null != $idSeller = $this->getAmpSellerId()) {

            if (is_array($idSeller)) {
                $idSeller = implode(",", $idSeller);
            }
            $ampCustomerJoin = new Join(CustomerTableMap::ID, OrderTableMap::CUSTOMER_ID, Criteria::LEFT_JOIN);
            $query
                    ->addJoinObject($ampCustomerJoin, "ampCustomerJoin");


            $ampOrderJoin = new Join(OrderTableMap::ID, AmpSellerOrderTableMap::ORDER_ID, Criteria::LEFT_JOIN);
            $query
                    ->addJoinObject($ampOrderJoin, "ampOrderJoin")
                    ->where(AmpSellerOrderTableMap::AMP_SELLER_ID . " " . Criteria::IN . " (" . $idSeller . ")");
        }
        $query->setDistinct();

        return $query;
    }

}
