<?php
namespace lib\Controller;

use lib\Core\Controller;

class Coupon extends Controller
{
    public function __construct()
    {
        parent::__construct();
    } //
    final public function getCouponInfo($data, $field)
    {
        $client = $this->getModel("Client")->getClient(sprintf("%s='%s'", $field, $data));
        $service = $this->getModel("Service")->getOneTime($client->Client_Name);
        return array(
            "client" => $client,
            "service" => $service,
        );
    } //

    public function execute(array $lines, $field = "Client_Code", $debug=false)
    {
	$loop=1;
        $successes = array();
        $failes = array();
        foreach ($lines as $line) {
            list($info, $coupon_cnt) = explode("||", rtrim($line));
	    $coupon = $this->getCouponInfo($info, $field);
	    if(isset($coupon['service']->service_code)){
		    //쿠폰 설정
		    $where = sprintf("service_code='%s'", $coupon['service']->service_code);
		    $this->getModel("Service")->setCoupon($coupon_cnt, $where, $debug);

		    $result = sprintf("%20s[%20s:%15s]=>쿠폰 %3d개 설정 성공\n", $coupon['client']->Client_Name,
		    $coupon['service']->server_code, $coupon['service']->service_code, $coupon_cnt);
		    array_push($successes, $result);
	    } else {
		    $result = sprintf("%5d번째: %s 설정 실패\n", $loop,rtrim($line));
		    array_push($failes, $result);
            }
	    $loop++;
        }
        return array($successes,$failes);
    }
}
