<commit_before><?php

namespace App\Utility\Chinghwa\Report\DailySaleRecord\NewExcel\Helper;

use App\Utility\Chinghwa\Report\DailySaleRecord\DailySaleRecord;
use App\Utility\Chinghwa\Database\Query\Processors\Processor;

class DataHelper
{
	public $erpGroups = [];
    public $posGroups = [];
    public $erpStatistics = [];
    public $posStatistics = [];
    public $totalPospGroup = [];
    public $ctiCallLog = [];

    protected $date;

    public function __construct(\DateTime $date, $isToday = false)
    {
        $this->date = $date;
        $this->fetchErpGroups($isToday)->fetchCtiCallLog($isToday)->fetchPosGroups($isToday);
    }

    public function fetchPosGroups($isToday = false)
    {
        $this->posGroups = $this->splitPosDataByGroup(Processor::getArrayResult($this->getQuery('POS', $isToday), 'Pos'));

        return $this;
    }

    public function fetchCtiCallLog($isToday = false)
    {
        $this->ctiCallLog = Processor::getArrayResult($this->getQuery('CTI', $isToday), 'Cti');

        return $this;
    }

    public function fetchErpGroups($isToday = false)
    {
        $this->erpGroups = $this->splitErpDataByGroup(Processor::getArrayResult($this->getQuery('ERP', $isToday), 'Erp'));

        return $this;
    }

    protected function getQuery($db, $isToday = false)
    {
        $startDate = $isToday ? with(new \DateTime)->modify('-1 days')->format('Ymd') : $this->date->modify('first day of this month')->format('Ymd');

        return str_replace(
            ['$startDate', '$endDate'],
            [$startDate, $this->date->modify('last day of this month')->format('Ymd')],
            file_get_contents(__DIR__ . "/../../../../../../../storage/sql/DailySaleRecord/{$db}.sql")
        );
    }

    /**
     * 將 ERP DB 撈取到的資料分類，非直效的部分算在外部通路
     *
     * 將分類好的資料傳回
     * 
     * @param  array  $erpData
     * @return array  $groupIndexData       
     */
    public function splitErpDataByGroup(array $erpData)
    {<commit_msg>Fix DailySaleRecord today part logic error<commit_after><?php

namespace App\Utility\Chinghwa\Report\DailySaleRecord\NewExcel\Helper;

use App\Utility\Chinghwa\Report\DailySaleRecord\DailySaleRecord;
use App\Utility\Chinghwa\Database\Query\Processors\Processor;

class DataHelper
{
	public $erpGroups = [];
    public $posGroups = [];
    public $erpStatistics = [];
    public $posStatistics = [];
    public $totalPospGroup = [];
    public $ctiCallLog = [];

    protected $date;

    public function __construct(\DateTime $date, $isToday = false)
    {
        $this->date = $date;
        $this->fetchErpGroups($isToday)->fetchCtiCallLog($isToday)->fetchPosGroups($isToday);
    }

    public function fetchPosGroups($isToday = false)
    {
        $this->posGroups = $this->splitPosDataByGroup(Processor::getArrayResult($this->getQuery('POS', $isToday), 'Pos'));

        return $this;
    }

    public function fetchCtiCallLog($isToday = false)
    {
        $this->ctiCallLog = Processor::getArrayResult($this->getQuery('CTI', $isToday), 'Cti');

        return $this;
    }

    public function fetchErpGroups($isToday = false)
    {
        $this->erpGroups = $this->splitErpDataByGroup(Processor::getArrayResult($this->getQuery('ERP', $isToday), 'Erp'));

        return $this;
    }

    protected function getQuery($db, $isToday = false)
    {
        $startDate = $isToday ? with(new \DateTime)->modify('-1 days')->format('Ymd') : $this->date->modify('first day of this month')->format('Ymd');

        $endDate = $isToday ? $startDate : $this->date->modify('last day of this month')->format('Ymd');

        return str_replace(
            ['$startDate', '$endDate'],
            [$startDate, $endDate],
            file_get_contents(__DIR__ . "/../../../../../../../storage/sql/DailySaleRecord/{$db}.sql")
        );
    }

    /**
     * 將 ERP DB 撈取到的資料分類，非直效的部分算在外部通路
     *
     * 將分類好的資料傳回
     * 
     * @param  array  $erpData
     * @return array  $groupIndexData       
     */
    public function splitErpDataByGroup(array $erpData)
    {
{'fraction_non_alphanumeric': 0.15970333745364648, 'fraction_numerical': 0.0004944375772558715, 'mean_word_length': 2.966666666666667, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1892661', 'n_tokens_mistral': 1372, 'n_tokens_neox': 1325, 'n_words': 217}