<?php
namespace lib\Controller;

use lib\Core\Controller;

class Coupon extends Controller
{
    public function __construct()
    {
        parent::__construct();
    } //
    final public function getCouponInfo($line, $field)
    {
        list($clientData, $coupon) = explode("||", rtrim($line));
        $client = $this->getModel("Client")->getData(sprintf("%s='%s'", $field, $clientData));
        $service = $this->getModel("Service")->getData(sprintf("client_code='%s'", $client['Client_Code']));
        return array(
            "client" => $client,
            "service" => $service,
            "coupon" => $coupon,
        );
    } //

    public function execute(array $lines, $field = "Client_Code")
    {
        $results = array();
        foreach ($this->getCouponInfo($lines, $field) as $coupon) {
            //쿠폰 설정
            $where = sprintf("coupon='%s'", $coupon['service']['service_code']);
            $this->getModel("Service")->setCoupon($coupon['coupon'], $where);

            //결과정리
            $result = sprintf("%20s[%20s:%15s]=>쿠폰 %3d개 설정\n", $coupon['client']['Client_Name'],
                $coupon['service']['server_code'], $coupon['service']['service_code'], $coupon['coupon']);
            array_push($results, $result);
        }
        return $results;
    }
}
