<?php

namespace App\Services\Reports;

use App\Cawangan;
use App\Gadai;
use App\TransaksiKeuntungan;

class KawalanStokService
{
    private $kodCawangan;
    private $startDate;
    private $endDate;

    public function __construct($kodCawangan, $request)
    {
        $this->kodCawangan = $kodCawangan;
        $this->startDate = $request->startDate;
        $this->endDate = $request->endDate;
    }

    public function getLaporanCawangan()
    {

        $cawangan = Cawangan::on('mysql7')->where('kodcaw', '=', $this->kodCawangan)->first();

        $array_kawalanstok = [];

        /* Gadai */
        $gadai1 = Gadai::on($cawangan['mysql'])
            ->selectRaw('COUNT(*) AS bil, SUM(pinjaman) AS pinj')
            ->where('t_gadai', '<', $this->startDate)
            ->where('tagpalsu', 0)
            ->whereBetween('nilaimarhun', [1, 400.99])
            ->get();

        $gadai2 = Gadai::on($cawangan['mysql'])
            ->selectRaw('COUNT(*) AS bil, SUM(pinjaman) AS pinj')
            ->where('t_gadai', '<', $this->startDate)
            ->where('tagpalsu', 0)
            ->whereBetween('nilaimarhun', [401, 2000.99])
            ->get();

        $gadai3 = Gadai::on($cawangan['mysql'])
            ->selectRaw('COUNT(*) AS bil, SUM(pinjaman) AS pinj')
            ->where('t_gadai', '<', $this->startDate)
            ->where('tagpalsu', 0)
            ->whereBetween('nilaimarhun', [2001, 1000000])
            ->get();

        $gadaiini1 = Gadai::on($cawangan['mysql'])
            ->selectRaw('COUNT(norujukan) AS bil, SUM(pinjaman) AS pinj')
            ->where('t_gadai', '>=', $this->startDate)->where('t_gadai', '<=', $this->endDate)
            ->where('tagpalsu', 0)
            ->where('nilaimarhun', '>=', 1)
            ->where('nilaimarhun', '<=', 400.99)
            ->get();

        $gadaiini2 = Gadai::on($cawangan['mysql'])
            ->selectRaw('COUNT(norujukan) AS bil, SUM(pinjaman) AS pinj')
            ->where('t_gadai', '>=', $this->startDate)->where('t_gadai', '<=', $this->endDate)
            ->where('tagpalsu', 0)
            ->where('nilaimarhun', '>=', 401)
            ->where('nilaimarhun', '<=', 2000.99)
            ->get();

        $gadaiini3 = Gadai::on($cawangan['mysql'])
            ->selectRaw('COUNT(norujukan) AS bil, SUM(pinjaman) AS pinj')
            ->where('t_gadai', '>=', $this->startDate)->where('t_gadai', '<=', $this->endDate)
            ->where('tagpalsu', 0)
            ->where('nilaimarhun', '>=', 2001)
            ->where('nilaimarhun', '<=', 1000000)
            ->get();

        /*tebus*/

        $tebus1 = Gadai::on($cawangan['mysql'])
            ->selectRaw('COUNT(norujukan) AS bil, SUM(pinjaman) AS pinj')
            ->where('sttebus', 'T')
            ->where(function ($query) {
                $query->where('t_update', '<', $this->startDate)
                    ->orWhereNull('t_update');
            })
            ->where('tagpalsu', 0)
            ->where('nilaimarhun', '>=', 1)
            ->where('nilaimarhun', '<=', 400.99)
            ->get();

        $tebus2 = Gadai::on($cawangan['mysql'])
            ->selectRaw('COUNT(norujukan) AS bil, SUM(pinjaman) AS pinj')
            ->where('sttebus', 'T')
            ->where(function ($query) {
                $query->where('t_update', '<', $this->startDate)
                    ->orWhereNull('t_update');
            })
            ->where('tagpalsu', 0)
            ->where('nilaimarhun', '>=', 401)
            ->where('nilaimarhun', '<=', 2000.99)
            ->get();

        $tebus3 = Gadai::on($cawangan['mysql'])
            ->selectRaw('COUNT(norujukan) AS bil, SUM(pinjaman) AS pinj')
            ->where('sttebus', 'T')
            ->where(function ($query) {
                $query->where('t_update', '<', $this->startDate)
                    ->orWhereNull('t_update');
            })
            ->where('tagpalsu', 0)
            ->where('nilaimarhun', '>=', 2001)
            ->where('nilaimarhun', '<=', 1000000)
            ->get();

        $tebusini1 = Gadai::on($cawangan['mysql'])
            ->selectRaw('COUNT(norujukan) AS bil, SUM(pinjaman) AS pinj')
            ->where('sttebus', 'T')
            ->where('t_update', '>=', $this->startDate)->where('t_update', '<=', $this->endDate)
            ->where('tagpalsu', 0)
            ->where('nilaimarhun', '>=', 1)
            ->where('nilaimarhun', '<=', 400.99)
            ->get();

        $tebusini2 = Gadai::on($cawangan['mysql'])
            ->selectRaw('COUNT(norujukan) AS bil, SUM(pinjaman) AS pinj')
            ->where('sttebus', 'T')
            ->where('t_update', '>=', $this->startDate)->where('t_update', '<=', $this->endDate)
            ->where('tagpalsu', 0)
            ->where('nilaimarhun', '>=', 401)
            ->where('nilaimarhun', '<=', 2000.99)
            ->get();

        $tebusini3 = Gadai::on($cawangan['mysql'])
            ->selectRaw('COUNT(norujukan) AS bil, SUM(pinjaman) AS pinj')
            ->where('sttebus', 'T')
            ->where('t_update', '>=', $this->startDate)->where('t_update', '<=', $this->endDate)
            ->where('tagpalsu', 0)
            ->where('nilaimarhun', '>=', 2001)
            ->where('nilaimarhun', '<=', 1000000)
            ->get();

        /*lelong*/

        $lelong1 = Gadai::on($cawangan['mysql'])
            ->selectRaw('COUNT(norujukan) AS bil, SUM(pinjaman) AS pinj')
            ->where('sttebus', 'L')
            ->where(function ($query) {
                $query->where('t_update', '<', $this->startDate)
                    ->orWhereNull('t_update');
            })
            ->where('tagpalsu', 0)
            ->where('nilaimarhun', '>=', 1)
            ->where('nilaimarhun', '<=', 400.99)
            ->get();

        $lelong2 = Gadai::on($cawangan['mysql'])
            ->selectRaw('COUNT(norujukan) AS bil, SUM(pinjaman) AS pinj')
            ->where('sttebus', 'L')
            ->where(function ($query) {
                $query->where('t_update', '<', $this->startDate)
                    ->orWhereNull('t_update');
            })
            ->where('tagpalsu', 0)
            ->where('nilaimarhun', '>=', 401)
            ->where('nilaimarhun', '<=', 2000.99)
            ->get();

        $lelong3 = Gadai::on($cawangan['mysql'])
            ->selectRaw('COUNT(norujukan) AS bil, SUM(pinjaman) AS pinj')
            ->where('sttebus', 'L')
            ->where(function ($query) {
                $query->where('t_update', '<', $this->startDate)
                    ->orWhereNull('t_update');
            })
            ->where('tagpalsu', 0)
            ->where('nilaimarhun', '>=', 2001)
            ->where('nilaimarhun', '<=', 1000000)
            ->get();

        $lelongini1 = Gadai::on($cawangan['mysql'])
            ->selectRaw('COUNT(norujukan) AS bil, SUM(pinjaman) AS pinj')
            ->where('sttebus', 'L')
            ->where('t_update', '>=', $this->startDate)->where('t_update', '<=', $this->endDate)
            ->where('tagpalsu', 0)
            ->where('nilaimarhun', '>=', 1)
            ->where('nilaimarhun', '<=', 400.99)
            ->get();

        $lelongini2 = Gadai::on($cawangan['mysql'])
            ->selectRaw('COUNT(norujukan) AS bil, SUM(pinjaman) AS pinj')
            ->where('sttebus', 'L')
            ->where('t_update', '>=', $this->startDate)->where('t_update', '<=', $this->endDate)
            ->where('tagpalsu', 0)
            ->where('nilaimarhun', '>=', 401)
            ->where('nilaimarhun', '<=', 2000.99)
            ->get();

        $lelongini3 = Gadai::on($cawangan['mysql'])
            ->selectRaw('COUNT(norujukan) AS bil, SUM(pinjaman) AS pinj')
            ->where('sttebus', 'L')
            ->where('t_update', '>=', $this->startDate)->where('t_update', '<=', $this->endDate)
            ->where('tagpalsu', 0)
            ->where('nilaimarhun', '>=', 2001)
            ->where('nilaimarhun', '<=', 1000000)
            ->get();

        /*ansuran*/

        $nomborRujukanAnsuran = $this->getFilteredNomborRujukanAnsuran();

        $ansuran1 = Gadai::on($cawangan['mysql'])
            ->whereIn('norujukan', $nomborRujukanAnsuran)
            ->where([['nilaimarhun', '>=', 1], ['nilaimarhun', '<=', 400.99], ['sttebus', ''], ['hargajualan', '<>', 'null']])
            ->orWhere([['nilaimarhun', '>=', 1], ['nilaimarhun', '<=', 400.99], ['sttebus', '!=', ''], ['t_update', '>', $this->endDate], ['hargajualan', '<>', 'null']])
            ->pluck('norujukan');

        $transaksi1 = TransaksiKeuntungan::on('mysql7')
            ->selectRaw('COUNT(*) as bil, SUM(bayaran_pembiayaan) AS total')
            ->whereIn('norujukan', $ansuran1)
            ->where('kodcaw', $this->kodCawangan)
            ->where('tarikh_bayaran', '<=', $this->startDate)
            ->get();

        $nomborRujukanAnsuranHariIni = $this->getFilteredNomborRujukanAnsuranHariIni();

        $ansuran1harini = Gadai::on($cawangan['mysql'])
            ->whereIn('norujukan', $nomborRujukanAnsuranHariIni)
            ->where([['nilaimarhun', '>=', 1], ['nilaimarhun', '<=', 400.99], ['sttebus', ''], ['hargajualan', '<>', 'null']])
            ->pluck('norujukan');

        $transaksi1harini = TransaksiKeuntungan::on('mysql7')
            ->selectRaw('COUNT(*) as bil, SUM(bayaran_pembiayaan) AS total')
            ->whereIn('norujukan', $ansuran1harini)
            ->whereDate('tarikh_bayaran', '>=', $this->startDate)
            ->whereDate('tarikh_bayaran', '<=', $this->endDate)
            ->where('kodcaw', $this->kodCawangan)
            ->get();

        $ansuran2 = Gadai::on($cawangan['mysql'])
            ->whereIn('norujukan', $nomborRujukanAnsuran)
            ->where([['nilaimarhun', '>=', 401], ['nilaimarhun', '<=', 2000.99], ['sttebus', ''], ['hargajualan', '<>', 'null']])
            ->orWhere([['nilaimarhun', '>=', 401], ['nilaimarhun', '<=', 2000.99], ['sttebus', '!=', ''], ['t_update', '>', $this->endDate], ['hargajualan', '<>', 'null']])
            ->pluck('norujukan');

        $transaksi2 = TransaksiKeuntungan::on('mysql7')
            ->selectRaw('COUNT(*) as bil, SUM(bayaran_pembiayaan) AS total')
            ->whereIn('norujukan', $ansuran2)
            ->where('tarikh_bayaran', '<=', $this->startDate)
            ->where('kodcaw', $this->kodCawangan)
            ->get();

        $ansuran2harini = Gadai::on($cawangan['mysql'])
            ->whereIn('norujukan', $nomborRujukanAnsuranHariIni)
            ->where([['nilaimarhun', '>=', 401], ['nilaimarhun', '<=', 2000.99], ['sttebus', ''], ['hargajualan', '<>', 'null']])
            ->pluck('norujukan');

        $transaksi2harini = TransaksiKeuntungan::on('mysql7')
            ->selectRaw('COUNT(*) as bil, SUM(bayaran_pembiayaan) AS total')
            ->whereIn('norujukan', $ansuran2harini)
            ->whereDate('tarikh_bayaran', '>=', $this->startDate)
            ->whereDate('tarikh_bayaran', '<=', $this->endDate)
            ->where('kodcaw', $this->kodCawangan)
            ->get();

        $ansuran3 = Gadai::on($cawangan['mysql'])
            ->whereIn('norujukan', $nomborRujukanAnsuran)
            ->where([['nilaimarhun', '>=', 2001], ['nilaimarhun', '<=', 1000000], ['sttebus', ''], ['hargajualan', '<>', 'null']])
            ->orWhere([['nilaimarhun', '>=', 2001], ['nilaimarhun', '<=', 1000000], ['sttebus', '!=', ''], ['t_update', '>', $this->endDate], ['hargajualan', '<>', 'null']])
            ->pluck('norujukan');

        $transaksi3 = TransaksiKeuntungan::on('mysql7')
            ->selectRaw('COUNT(*) as bil, SUM(bayaran_pembiayaan) AS total')
            ->whereIn('norujukan', $ansuran3)
            ->where('tarikh_bayaran', '<=', $this->startDate)
            ->where('kodcaw', $this->kodCawangan)
            ->get();

        $ansuran3harini = Gadai::on($cawangan['mysql'])
            ->whereIn('norujukan', $nomborRujukanAnsuranHariIni)
            ->where([['nilaimarhun', '>=', 2001], ['nilaimarhun', '<=', 1000000], ['sttebus', ''], ['hargajualan', '<>', 'null']])
            ->pluck('norujukan');

        $transaksi3harini = TransaksiKeuntungan::on('mysql7')
            ->selectRaw('COUNT(*) as bil, SUM(bayaran_pembiayaan) AS total')
            ->whereIn('norujukan', $ansuran3harini)
            ->whereDate('tarikh_bayaran', '>=', $this->startDate)
            ->whereDate('tarikh_bayaran', '<=', $this->endDate)
            ->where('kodcaw', $this->kodCawangan)
            ->get();

        ##push in array##
        $arrayans1 = ['bil' => $transaksi1[0]->bil, 'pinj' => $transaksi1[0]->total];
        $arrayharinians1 = ['bil' => $transaksi1harini[0]->bil, 'pinj' => $transaksi1harini[0]->total];
        $arrayans2 = ['bil' => $transaksi2[0]->bil, 'pinj' => $transaksi2[0]->total];
        $arrayharinians2 = ['bil' => $transaksi2harini[0]->bil, 'pinj' => $transaksi2harini[0]->total];
        $arrayans3 = ['bil' => $transaksi3[0]->bil, 'pinj' => $transaksi3[0]->total];
        $arrayharinians3 = ['bil' => $transaksi3harini[0]->bil, 'pinj' => $transaksi3harini[0]->total];

        /*hapus kira*/

        $hapuskira1 = Gadai::on($cawangan['mysql'])
            ->selectRaw('COUNT(norujukan) AS bil, SUM(pinjaman) AS pinj')
            ->where('sttebus', 'H')
            ->where(function ($query) {
                $query->where('t_update', '<', $this->startDate)
                    ->orWhereNull('t_update');
            })
            ->where('tagpalsu', 0)
            ->where('nilaimarhun', '>=', 1)
            ->where('nilaimarhun', '<=', 400.99)
            ->get();

        $hapuskira2 = Gadai::on($cawangan['mysql'])
            ->selectRaw('COUNT(norujukan) AS bil, SUM(pinjaman) AS pinj')
            ->where('sttebus', 'H')
            ->where(function ($query) {
                $query->where('t_update', '<', $this->startDate)
                    ->orWhereNull('t_update');
            })
            ->where('tagpalsu', 0)
            ->where('nilaimarhun', '>=', 401)
            ->where('nilaimarhun', '<=', 2000.99)
            ->get();

        $hapuskira3 = Gadai::on($cawangan['mysql'])
            ->selectRaw('COUNT(norujukan) AS bil, SUM(pinjaman) AS pinj')
            ->where('sttebus', 'H')
            ->where(function ($query) {
                $query->where('t_update', '<', $this->startDate)
                    ->orWhereNull('t_update');
            })
            ->where('tagpalsu', 0)
            ->where('nilaimarhun', '>=', 2001)
            ->where('nilaimarhun', '<=', 1000000)
            ->get();

        $hapuskiraini1 = Gadai::on($cawangan['mysql'])
            ->selectRaw('COUNT(norujukan) AS bil, SUM(pinjaman) AS pinj')
            ->where('sttebus', 'H')
            ->where('t_update', '>=', $this->startDate)->where('t_update', '<=', $this->endDate)
            ->where('tagpalsu', 0)
            ->where('nilaimarhun', '>=', 1)
            ->where('nilaimarhun', '<=', 400.99)
            ->get();

        $hapuskiraini2 = Gadai::on($cawangan['mysql'])
            ->selectRaw('COUNT(norujukan) AS bil, SUM(pinjaman) AS pinj')
            ->where('sttebus', 'H')
            ->where('t_update', '>=', $this->startDate)->where('t_update', '<=', $this->endDate)
            ->where('tagpalsu', 0)
            ->where('nilaimarhun', '>=', 401)
            ->where('nilaimarhun', '<=', 2000.99)
            ->get();

        $hapuskiraini3 = Gadai::on($cawangan['mysql'])
            ->selectRaw('COUNT(norujukan) AS bil, SUM(pinjaman) AS pinj')
            ->where('sttebus', 'H')
            ->where('t_update', '>=', $this->startDate)->where('t_update', '<=', $this->endDate)
            ->where('tagpalsu', 0)
            ->where('nilaimarhun', '>=', 2001)
            ->where('nilaimarhun', '<=', 1000000)
            ->get();

        array_push($array_kawalanstok, ["gadai1" => $gadai1, "gadai2" => $gadai2, "gadai3" => $gadai3, "gadaiini1" => $gadaiini1, "gadaiini2" => $gadaiini2, "gadaiini3" => $gadaiini3]);
        array_push($array_kawalanstok, ["tebus1" => $tebus1, "tebus2" => $tebus2, "tebus3" => $tebus3, "tebusini1" => $tebusini1, "tebusini2" => $tebusini2, "tebusini3" => $tebusini3]);
        array_push($array_kawalanstok, ["lelong1" => $lelong1, "lelong2" => $lelong2, "lelong3" => $lelong3, "lelongini1" => $lelongini1, "lelongini2" => $lelongini2, "lelongini3" => $lelongini3]);
        array_push($array_kawalanstok, ["hapuskira1" => $hapuskira1, "hapuskira2" => $hapuskira2, "hapuskira3" => $hapuskira3, "hapuskiraini1" => $hapuskiraini1, "hapuskiraini2" => $hapuskiraini2, "hapuskiraini3" => $hapuskiraini3]);
        array_push($array_kawalanstok, ["ans1" => $arrayans1, "ans2" => $arrayans2, "ans3" => $arrayans3, "ansini1" => $arrayharinians1, "ansini2" => $arrayharinians2, "ansini3" => $arrayharinians3]);
        return response()->json($array_kawalanstok);

    }

    private function getFilteredNomborRujukanAnsuran()
    {
        return TransaksiKeuntungan::on('mysql7')
            ->leftJoin('transaction as trans', function ($join) {
                $join->on('transaksi_keuntungan.norujukan', '=', 'trans.norujukan');
                $join->on('transaksi_keuntungan.tarikh_bayaran', '=', 'trans.created_at');
            })
            ->where('trans.created_at', '<', $this->startDate)
            ->where('trans.trans_type', '=', 'A')
            ->groupBy('trans.id')
            ->where('trans.kodcaw', $this->kodCawangan)
            ->pluck('trans.norujukan');

    }

    private function getFilteredNomborRujukanAnsuranHariIni()
    {
        return TransaksiKeuntungan::on('mysql7')
            ->leftJoin('transaction as trans', function ($join) {
                $join->on('transaksi_keuntungan.norujukan', '=', 'trans.norujukan');
                $join->on('transaksi_keuntungan.tarikh_bayaran', '=', 'trans.created_at');
            })
            ->whereDate('trans.created_at', '>=', $this->startDate)
            ->whereDate('trans.created_at', '<=', $this->endDate)
            ->where('trans.trans_type', '=', 'A')
            ->groupBy('trans.id')
            ->where('trans.kodcaw', $this->kodCawangan)
            ->pluck('trans.norujukan');

    }

}
