<?php

namespace YSquare\GiftCert\Controller\Admin;

class GiftCertLogController extends \OxidEsales\Eshop\Application\Controller\Admin\AdminController
{

    protected $_sThisTemplate = 'ys_giftcert__admin_logs.tpl';

    protected $_aGiftcertList = null;

    /**
     * @param $blOpen All giftcerts or only available giftcerts
     * @return mixed|null
     */
    public function getGiftcertList($blOpen = false) {
        if(null === $this->_aGiftcertList) {
            $sQ = "SELECT oxid from ysgiftcert";
            if($blOpen){
                $sQ .= " where ysrestdiscount > 0";
            }
            $oDb = \OxidEsales\Eshop\Core\DatabaseProvider::getDb(\OxidEsales\Eshop\Core\DatabaseProvider::FETCH_MODE_ASSOC);

            $rs = $oDb->select($sQ);
            if ($rs != false && $rs->count() > 0) {
                while (!$rs->EOF) {
                    $giftcert = oxNew(\YSquare\GiftCert\Model\GiftCert::class);
                    $giftcert->load($rs->fields['oxid']);
                    $this->_aGiftcertList[$rs->fields['oxid']] = $giftcert;
                    $rs->fetchRow();
                }
            }
        }
        return $this->_aGiftcertList;
    }

    public function getCustomerStringById($custId) {
        $customer = oxNew(\OxidEsales\Eshop\Application\Model\User::class);
        if($customer->load($custId)) {
            return $customer->oxuser__oxcustnr->value;
        } else {
            return "N/A";
        }
    }

    public function getOrderStringById($orderId) {
        $order = oxNew(\OxidEsales\Eshop\Application\Model\Order::class);
        if($order->load($orderId)) {
            return $order->oxorder__oxordernr->value;
        } else {
            return "N/A";
        }
    }


    public function canBeRedeemedPartly() {
        return \OxidEsales\Eshop\Core\Registry::getConfig()->getConfigParam('blYsGiftcertRedeemPartly');
    }

    public function shouldRedeemToPool() {
        // Only available if redeemPartly is allowed
        if($this->canBeRedeemedPartly()) {
            return \OxidEsales\Eshop\Core\Registry::getConfig()->getConfigParam('blYsGiftcertRedeemToPool');
        }
        return false;
    }

    public function getGiftcertSummary(){
        $oDb = \OxidEsales\Eshop\Core\DatabaseProvider::getDb();
        $sql = "select sum(ysrestdiscount) from ysgiftcert";
        return $oDb->getOne($sql);
    }

    public function getGiftcertOpenCredits(){
        $oDb = \OxidEsales\Eshop\Core\DatabaseProvider::getDb();
        $sql = "SELECT sum(ys_giftcert_pool_amount) from oxuser";
        return $oDb->getOne($sql);
    }

    public function getGiftcertCount(){
        $oDb = \OxidEsales\Eshop\Core\DatabaseProvider::getDb();
        $sql = "select count(*) from ysgiftcert where ysrestdiscount > 0";
        return $oDb->getOne($sql);
    }

    public function exportOpenGiftcerts(){
        $giftcerts = $this->getOpenGiftcerts();
        if ($giftcerts) {
            $csv = new \ParseCsv\Csv();
            $csv->linefeed = "\n";
            $header = array('code', 'amount', 'valid until');
            $csv->output('open_giftcerts_' . date('Ymd') .'.csv', $giftcerts, $header, ';');
        }
        die();
    }

    public function exportUserGiftcerts(){
        $giftcerts = $this->getUserGiftcerts();
        if ($giftcerts) {
            $csv = new \ParseCsv\Csv();
            $csv->linefeed = "\n";
            $header = array('email', 'amount');
            $csv->output('user_giftcert_amount_' . date('Ymd') .'.csv', $giftcerts, $header, ';');
        }
        die();
    }

    public function exportGeneratedGiftcerts(){
        $oSession = $this->getSession();
        $codes = $oSession->getVariable("generatedCode");
        if ($codes) {
            $giftcerts = $this->getGiftcertsByCode($codes);
            $csv = new \ParseCsv\Csv();
            $csv->linefeed = "\n";
            $header = array('date', 'code', 'amount');
            $csv->output('generated_giftcerts_' . date('Ymd') .'.csv', $giftcerts, $header, ';');
        }
        die();
    }

    protected function getOpenGiftcerts()
    {
        $oGiftcertList = $this->getGiftcertList(true);
        $giftcerts = array();
        if($oGiftcertList){
            foreach($oGiftcertList as $oGiftcert){
                $giftcerts[] = array($oGiftcert->ysgiftcert__ysgiftcertnr->value, $oGiftcert->ysgiftcert__ysrestdiscount->value, $oGiftcert->getGiftcertExpirationDate());
            }
        }

        return $giftcerts;
    }

    protected function getUserGiftcerts()
    {
        $sQ = "SELECT oxusername, ys_giftcert_pool_amount from oxuser where ys_giftcert_pool_amount > 0";

        $oDb = \OxidEsales\Eshop\Core\DatabaseProvider::getDb(\OxidEsales\Eshop\Core\DatabaseProvider::FETCH_MODE_ASSOC);

        $giftcerts = array();
        $rs = $oDb->select($sQ);
        if ($rs != false && $rs->count() > 0) {
            while (!$rs->EOF) {
                $giftcerts[] = array($rs->fields['oxusername'], $rs->fields['ys_giftcert_pool_amount']);
                $rs->fetchRow();
            }
        }

        return $giftcerts;
    }

    protected function getGiftcertsByCode($codes)
    {
        $oDb = \OxidEsales\Eshop\Core\DatabaseProvider::getDb(\OxidEsales\Eshop\Core\DatabaseProvider::FETCH_MODE_ASSOC);
        if($codes && is_array($codes)){
            $inCodes = "";
            foreach($codes as $code){
                if(!empty($inCodes)){
                    $inCodes .= ", ";
                }
                $inCodes .= $oDb->quote($code);
            }
        }else{
            return;
        }
        $sQ = "SELECT ysdatepurchased, ysgiftcertnr, ysrestdiscount from ysgiftcert where ysgiftcertnr in ($inCodes)";

        $giftcerts = array();
        $rs = $oDb->select($sQ);
        if ($rs != false && $rs->count() > 0) {
            while (!$rs->EOF) {
                $giftcerts[] = array($rs->fields['ysdatepurchased'], $rs->fields['ysgiftcertnr'], $rs->fields['ysrestdiscount']);
                $rs->fetchRow();
            }
        }

        return $giftcerts;
    }
}