<?php

namespace nota\controllers;

use Yii;
use yii\web\Controller;
use nota\models\KasirShift;
use nota\models\Nota;

class LaporanController extends Controller
{
    public function actionDashboard()
{
    $bulan = date('m');
    $tahun = date('Y');

    $ranking = \nota\models\KasirShift::find()
        ->alias('s')
        ->select([
            'u.vNama AS nama_kasir',
            'SUM(s.total_penjualan) AS total_omset',
            'COUNT(s.id) AS total_shift'
        ])
        ->leftJoin('member u', 'u.id = s.user_id')
        ->where(['MONTH(s.tanggal_buka)' => $bulan])
        ->andWhere(['YEAR(s.tanggal_buka)' => $tahun])
        ->andWhere(['s.status' => 'closed'])
        ->andWhere(['u.idCluster'=> Yii::$app->user->identity->idCluster])
        ->groupBy('s.user_id')
        ->orderBy(['total_omset' => SORT_DESC])
        ->asArray()
        ->all();

    return $this->render('dashboard', [
        'ranking' => $ranking,
    ]);
}


    public function actionTopProduk()
    {
        $bulan = date('m');
        $tahun = date('Y');

        $data = (new \yii\db\Query())
    ->select([
        'b.nama',
        'SUM(ni.qty) as total_qty',
        'SUM(ni.subtotal) as total_omset'
    ])
    ->from(['ni' => 'nota_item'])
    ->innerJoin(['n' => 'nota'], 'n.id = ni.nota_id')
    ->innerJoin(['b' => 'barang'], 'b.id = ni.barang_id')
    ->where(['MONTH(n.tanggal)' => $bulan])
    ->andWhere(['YEAR(n.tanggal)' => $tahun])
    ->andWhere(['n.idCluster'=> Yii::$app->user->identity->idCluster])
    ->groupBy('ni.barang_id')
    ->orderBy(['total_qty' => SORT_DESC])
    ->limit(10)
    ->createCommand(Yii::$app->dbnota)
    ->queryAll();


        return $this->render('top-produk', [
            'data' => $data
        ]);
    }



}