<commit_before><?php
namespace Admin\Controller;
use Cake\Datasource\Exception\RecordNotFoundException;
use Cake\Core\Configure;

/**
* FoodCoopShop - The open source software for your foodcoop
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @since         FoodCoopShop 1.1.0
* @license       https://opensource.org/licenses/mit-license.php MIT License
* @author        <NAME> <<EMAIL>>
* @copyright     Copyright (c) <NAME>, https://www.rothauer-it.com
* @link          https://www.foodcoopshop.com
*/

class DepositsController extends AdminAppController
{

    public $manufacturerId;

    public function isAuthorized($user)
    {
        switch ($this->getRequest()->getParam('action')) {
            case 'overviewDiagram':
                return $this->AppAuth->isSuperadmin();
                break;
            case 'index':
            case 'detail':
                return $this->AppAuth->isSuperadmin() || $this->AppAuth->isAdmin();
                break;
            case 'myIndex':
            case 'myDetail':
                return $this->AppAuth->isManufacturer();
                break;
            default:
                return $this->AppAuth->isManufacturer();
                break;
        }
    }

    /**
     * $this->manufacturerId needs to be set in calling method
     * @return int
     */
    private function getManufacturerId()
    {
        $manufacturerId = '';
        if (!empty($this->getRequest()->getQuery('manufacturerId'))) {
            $manufacturerId = h($this->getRequest()->getQuery('manufacturerId'));
        } if ($this->manufacturerId > 0) {
            $manufacturerId = $this->manufacturerId;
        }
        return $manufacturerId;
    }

    public function overviewDiagram()
    {
        $dateFrom = Configure::read('app.timeHelper')->getFirstDayOfThisYear();
        if (! empty($this->getRequest()->getQuery('dateFrom'))) {
            $dateFrom = h($this->getRequest()->getQuery('dateFrom'));
        }
        $this->set('dateFrom', $dateFrom);

        $dateTo = Configure::read('app.timeHelper')->getLastDayOfThisYear();
        if (! empty($this->getRequest()->getQuery('dateTo'))) {
            $dateTo = h($this->getRequest()->getQuery('dateTo'));
        }
        $this->set('dateTo', $dateTo);

        $this->set('title_for_layout', __d('admin', 'Deposit_overview'));

        $this->Payment = $this->getTableLocator()->get('Payments');
        $manufacturerDepositSumEmptyGlassesByCalendarWeek = $this->Payment->getManufacturerDepositSumByCalendarWeekAndType('empty_glasses');
        $preparedManufacturerEmptyGlassesData = [];
        foreach($manufacturerDepositSumEmptyGlassesByCalendarWeek as $week) {
            $week->YearWeekPrepared = str_replace('-', 'W', $week->YearWeek);
            $preparedManufacturerEmptyGlassesData[$week->YearWeek] = $week->SumAmount;
        }

        $manufacturerDepositSumMoneyByCalendarWeek = $this->Payment->getManufacturerDepositSumByCalendarWeekAndType('money');
        $preparedManufacturerMoneyData = [];
        foreach($manufacturerDepositSumMoneyByCalendarWeek as $week) {
            $week->YearWeekPrepared = str_replace('-', 'W', $week->YearWeek);
            $preparedManufacturerMoneyData[$week->YearWeek] = $week->SumAmount;
        }

        $customerDepositSumByCalendarWeek = $this->Payment->getCustomerDepositSumByCalendarWeek();
        $preparedCustomerData = [];
        foreach($customerDepositSumByCalendarWeek as $week) {
            $week->YearWeekPrepared = str_replace('-', 'W', $week->YearWeek);
            $preparedCustomerData[$week->YearWeek] = $week->SumAmount;
        }

        $this->OrderDetail = $this->getTableLocator()->get('OrderDetails');
        $depositsDeliveredByYear = $this->OrderDetail->getDepositSum(false, 'year');

        if (empty($manufacturerDepositSumEmptyGlassesByCalendarWeek) && empty($customerDepositSumByCalendarWeek)) {
            return;
        }

        if (empty($customerDepositSumByCalendarWeek)) {
            $firstWeek = strtotime($manufacturerDepositSumEmptyGlassesByCalendarWeek[0]->YearWeekPrepared);
        }
        if (empty($manufacturerDepositSumEmptyGlassesByCalendarWeek)) {
            $firstWeek = strtotime($customerDepositSumByCalendarWeek[0]->YearWeekPrepared);
        }

        if (!isset($firstWeek)) {
            $firstWeek = strtotime($customerDepositSumByCalendarWeek[0]->YearWeekPrepared);
            if (strtotime($customerDepositSumByCalendarWeek[0]->YearWeekPrepared) > strtotime($manufacturerDepositSumEmptyGlassesByCalendarWeek[0]->YearWeekPrepared)) {
                $firstWeek = strtotime($manufacturerDepositSumEmptyGlassesByCalendarWeek[0]->YearWeekPrepared);
            }
        }

        $allCalendarWeeksUntilNow = Configure::read('app.timeHelper')->getAllCalendarWeeksUntilNow($firstWeek);
        $xAxisData1LineChart = [];
        $xAxisData2LineChart = [];
        $xAxisData3LineChart = [];
        $yAxisDataLineChart= [];
        $manufacturerEmptyGlassesSum = 0;
        $manufacturerMoneySum = 0;
        $yearlyManufacturerEmptyGlasses = [];
        $yearlyManufacturerMoney = [];
        $yearlyDepositsDelivered = [];
        $yearlyOverallDeltas = [];
        $years = [];

        foreach($allCalendarWeeksUntilNow as $calendarWeek) {

            $year = explode('-', $calendarWeek)[0];
            $years[] = $year;

            $yAxisDataLineChart[] = $calendarWeek;

            $manufacturerEmptyGlasses = $preparedManufacturerEmptyGlassesData[$calendarWeek] ?? $preparedManufacturerEmptyGlassesData[$calendarWeek] ?? 0;
            $xAxisData1LineChart[] = $manufacturerEmptyGlasses;

            $customerDeposit = $preparedCustomerData[$calendarWeek] ?? $preparedCustomerData[$calendarWeek] ?? 0;
            $xAxisData2LineChart[]= $customerDeposit;

            $manufacturerMoney = $preparedManufacturerMoneyData[$calendarWeek] ?? $preparedManufacturerMoneyData[$calendarWeek] ?? 0;
            $xAxisData3LineChart[] = $manufacturerMoney;

            @$yearlyManufacturerEmptyGlasses[$year] += $manufacturerEmptyGlasses;
            @$yearlyManufacturerMoney[$year] += $manufacturerMoney;

            @$yearlyOverallDeltas[$year] += $manufacturerEmptyGlasses + $manufacturerMoney;

        }
<commit_msg>wrong usage of ??<commit_after><?php
namespace Admin\Controller;
use Cake\Datasource\Exception\RecordNotFoundException;
use Cake\Core\Configure;

/**
* FoodCoopShop - The open source software for your foodcoop
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @since         FoodCoopShop 1.1.0
* @license       https://opensource.org/licenses/mit-license.php MIT License
* @author        <NAME> <<EMAIL>>
* @copyright     Copyright (c) <NAME>, https://www.rothauer-it.com
* @link          https://www.foodcoopshop.com
*/

class DepositsController extends AdminAppController
{

    public $manufacturerId;

    public function isAuthorized($user)
    {
        switch ($this->getRequest()->getParam('action')) {
            case 'overviewDiagram':
                return $this->AppAuth->isSuperadmin();
                break;
            case 'index':
            case 'detail':
                return $this->AppAuth->isSuperadmin() || $this->AppAuth->isAdmin();
                break;
            case 'myIndex':
            case 'myDetail':
                return $this->AppAuth->isManufacturer();
                break;
            default:
                return $this->AppAuth->isManufacturer();
                break;
        }
    }

    /**
     * $this->manufacturerId needs to be set in calling method
     * @return int
     */
    private function getManufacturerId()
    {
        $manufacturerId = '';
        if (!empty($this->getRequest()->getQuery('manufacturerId'))) {
            $manufacturerId = h($this->getRequest()->getQuery('manufacturerId'));
        } if ($this->manufacturerId > 0) {
            $manufacturerId = $this->manufacturerId;
        }
        return $manufacturerId;
    }

    public function overviewDiagram()
    {
        $dateFrom = Configure::read('app.timeHelper')->getFirstDayOfThisYear();
        if (! empty($this->getRequest()->getQuery('dateFrom'))) {
            $dateFrom = h($this->getRequest()->getQuery('dateFrom'));
        }
        $this->set('dateFrom', $dateFrom);

        $dateTo = Configure::read('app.timeHelper')->getLastDayOfThisYear();
        if (! empty($this->getRequest()->getQuery('dateTo'))) {
            $dateTo = h($this->getRequest()->getQuery('dateTo'));
        }
        $this->set('dateTo', $dateTo);

        $this->set('title_for_layout', __d('admin', 'Deposit_overview'));

        $this->Payment = $this->getTableLocator()->get('Payments');
        $manufacturerDepositSumEmptyGlassesByCalendarWeek = $this->Payment->getManufacturerDepositSumByCalendarWeekAndType('empty_glasses');
        $preparedManufacturerEmptyGlassesData = [];
        foreach($manufacturerDepositSumEmptyGlassesByCalendarWeek as $week) {
            $week->YearWeekPrepared = str_replace('-', 'W', $week->YearWeek);
            $preparedManufacturerEmptyGlassesData[$week->YearWeek] = $week->SumAmount;
        }

        $manufacturerDepositSumMoneyByCalendarWeek = $this->Payment->getManufacturerDepositSumByCalendarWeekAndType('money');
        $preparedManufacturerMoneyData = [];
        foreach($manufacturerDepositSumMoneyByCalendarWeek as $week) {
            $week->YearWeekPrepared = str_replace('-', 'W', $week->YearWeek);
            $preparedManufacturerMoneyData[$week->YearWeek] = $week->SumAmount;
        }

        $customerDepositSumByCalendarWeek = $this->Payment->getCustomerDepositSumByCalendarWeek();
        $preparedCustomerData = [];
        foreach($customerDepositSumByCalendarWeek as $week) {
            $week->YearWeekPrepared = str_replace('-', 'W', $week->YearWeek);
            $preparedCustomerData[$week->YearWeek] = $week->SumAmount;
        }

        $this->OrderDetail = $this->getTableLocator()->get('OrderDetails');
        $depositsDeliveredByYear = $this->OrderDetail->getDepositSum(false, 'year');

        if (empty($manufacturerDepositSumEmptyGlassesByCalendarWeek) && empty($customerDepositSumByCalendarWeek)) {
            return;
        }

        if (empty($customerDepositSumByCalendarWeek)) {
            $firstWeek = strtotime($manufacturerDepositSumEmptyGlassesByCalendarWeek[0]->YearWeekPrepared);
        }
        if (empty($manufacturerDepositSumEmptyGlassesByCalendarWeek)) {
            $firstWeek = strtotime($customerDepositSumByCalendarWeek[0]->YearWeekPrepared);
        }

        if (!isset($firstWeek)) {
            $firstWeek = strtotime($customerDepositSumByCalendarWeek[0]->YearWeekPrepared);
            if (strtotime($customerDepositSumByCalendarWeek[0]->YearWeekPrepared) > strtotime($manufacturerDepositSumEmptyGlassesByCalendarWeek[0]->YearWeekPrepared)) {
                $firstWeek = strtotime($manufacturerDepositSumEmptyGlassesByCalendarWeek[0]->YearWeekPrepared);
            }
        }

        $allCalendarWeeksUntilNow = Configure::read('app.timeHelper')->getAllCalendarWeeksUntilNow($firstWeek);
        $xAxisData1LineChart = [];
        $xAxisData2LineChart = [];
        $xAxisData3LineChart = [];
        $yAxisDataLineChart= [];
        $manufacturerEmptyGlassesSum = 0;
        $manufacturerMoneySum = 0;
        $yearlyManufacturerEmptyGlasses = [];
        $yearlyManufacturerMoney = [];
        $yearlyDepositsDelivered = [];
        $yearlyOverallDeltas = [];
        $years = [];

        foreach($allCalendarWeeksUntilNow as $calendarWeek) {

            $year = explode('-', $calendarWeek)[0];
            $years[] = $year;

            $yAxisDataLineChart[] = $calendarWeek;

            $manufacturerEmptyGlasses = $preparedManufacturerEmptyGlassesData[$calendarWeek] ?? 0;
            $xAxisData1LineChart[] = $manufacturerEmptyGlasses;

            $customerDeposit = $preparedCustomerData[$calendarWeek] ?? 0;
            $xAxisData2LineChart[]= $customerDeposit;

            $manufacturerMoney = $preparedManufacturerMoneyData[$calendarWeek] ?? 0;
            $xAxisData3LineChart[] = $manufacturerMoney;

            @$yearlyManufacturerEmptyGlasses[$year] += $manufacturerEmptyGlasses;
            @$yearlyManufacturerMoney[$year] += $manufacturerMoney;

            @$yearlyOverallDeltas[$year] += $manufacturerEmptyGlasses + $manufacturerMoney;

        }

{'fraction_non_alphanumeric': 0.13117344922955354, 'fraction_numerical': 0.0034768866060845517, 'mean_word_length': 3.0305732484076433, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 145, 'https://': 6, 'lorem ipsum': 0, 'www.': 4, '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': '4147886', 'n_tokens_mistral': 3757, 'n_tokens_neox': 3470, 'n_words': 504}