<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;

class ReportController extends Controller
{
    public function index()
    {
        return view('admin.report.index');
    }

    public function cekLaporan($tglMulai, $tglSelesai, $status)
    {

        $data = DB::table('orders')
            ->leftJoin('users', 'users.id', '=', 'orders.user_id')
            ->leftJoin('order_items', 'orders.id', '=', 'order_items.order_id')
            ->leftJoin('products', 'products.id', '=', 'order_items.product_id')
            ->whereDate('orders.created_at', '>=', $tglMulai)
            ->whereDate('orders.created_at', '<=', $tglSelesai)
            ->where('orders.status', $status)
            ->select(
                'orders.created_at as tanggal',
                'users.name as kasir',
                'products.name as pesanan',
                'order_items.price as harga_satuan',
                'order_items.quantity as jumlah',
                'orders.discount as diskon',
                'orders.status as status'
            )
            ->orderBy('orders.id', 'desc')
            ->get();
        // dd($data);

        $jumlah = DB::table('orders')
            ->leftJoin('order_items', 'orders.id', '=', 'order_items.order_id')
            ->whereBetween('orders.transaction_time', [$tglMulai, $tglSelesai])
            ->where('orders.status', $status)
            ->sum('order_items.quantity');

        $sumTotal = DB::table('order_items')
            ->leftJoin('orders', 'orders.id', '=', 'order_items.order_id')
            ->where('orders.status', $status)
            ->whereBetween('orders.transaction_time', [$tglMulai, $tglSelesai])
            ->selectRaw('SUM(IF(
            orders.discount IS NULL OR orders.discount = 0,
            order_items.price * order_items.quantity,
            (order_items.price * order_items.quantity) - (orders.discount / 100 * (order_items.price * order_items.quantity))
        )) as total')
            ->value('total');

        return view('admin.report.cekLaporan', [
            'data' => $data,
            'sumTotal' => $sumTotal,
            'jumlah' => $jumlah,
            'tglMulai' => $tglMulai,
            'tglSelesai' => $tglSelesai,
            'status' => $status
        ]);
    }

    public function cetak($tglMulai, $tglSelesai, $status)
    {
        $data = DB::table('orders')
            ->leftJoin('users', 'users.id', '=', 'orders.user_id')
            ->leftJoin('order_items', 'orders.id', '=', 'order_items.order_id')
            ->leftJoin('products', 'products.id', '=', 'order_items.product_id')
            ->whereDate('orders.created_at', '>=', $tglMulai)
            ->whereDate('orders.created_at', '<=', $tglSelesai)
            ->where('orders.status', $status)
            ->select(
                'orders.created_at as tanggal',
                'orders.id as nomer_penjualan',
                'users.name as kasir',
                'products.name as pesanan',
                'order_items.price as harga_satuan',
                'order_items.quantity as jumlah',
                'orders.discount as diskon',
                'orders.status as status'
            )
            ->orderBy('orders.id', 'desc')
            ->get();

        $jumlah = DB::table('orders')
            ->leftJoin('order_items', 'orders.id', '=', 'order_items.order_id')
            ->whereBetween('orders.transaction_time', [$tglMulai, $tglSelesai])
            ->where('orders.status', $status)
            ->sum('order_items.quantity');

        $sumTotal = DB::table('order_items')
            ->leftJoin('orders', 'orders.id', '=', 'order_items.order_id')
            ->where('orders.status', $status)
            ->whereBetween('orders.transaction_time', [$tglMulai, $tglSelesai])
            ->selectRaw('SUM(IF(
            orders.discount IS NULL OR orders.discount = 0,
            order_items.price * order_items.quantity,
            (order_items.price * order_items.quantity) - (orders.discount / 100 * (order_items.price * order_items.quantity))
        )) as total')
            ->value('total');

        return view('admin.report.cetak', [
            'data' => $data,
            'sumTotal' => $sumTotal,
            'jumlah' => $jumlah,
            'tglMulai' => $tglMulai,
            'tglSelesai' => $tglSelesai,
            'status' => $status
        ]);
    }
}
