<?php

namespace App\Services\Reports;

use App\AkruNet;
use App\AkruNetPecahan;
use App\Cawangan;
use App\imbangdugabackup;
use App\Services\Reports\Account\GeneralLedger;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;

class AccountService{
    public function checkAccount($kodcaw,$month){
        $cawangan = Cawangan::on('mysql7')->where('kodcaw',$kodcaw)->first();
        $date = Carbon::now()->month($month);
        $firstDay = (clone $date)->startOfMonth();
        $lastDay =  (clone $date)->endOfMonth();
        $previousMonth = (clone $date)->subMonthNoOverflow()->endOfMonth();
        
        $BSheetPreviousMonth = imbangdugabackup::on($cawangan['mysql'])->where('tarikh',$previousMonth->toDateString())->first()->toArray();
        $BSheetCurrentMonth = imbangdugabackup::on($cawangan['mysql'])->where('tarikh',$lastDay->toDateString())->first()->toArray();

        $akru_net = AkruNet::on($cawangan['mysql'])->where('tahun',$date->year)->where('bulan','<=',$month)->sum('jumlah');
        $akru_net_ujrah = AkruNetPecahan::on($cawangan['mysql'])->where('tahun',$date->year)->where('bulan','<=',$month)->sum('pend_akru_ujrah');
        $akru_net_onepercent = AkruNetPecahan::on($cawangan['mysql'])->where('tahun',$date->year)->where('bulan','<=',$month)->sum('pend_akru_1persen');
        $model_class = "App\StokAudit{$month}";
        $keun_penggadai = $model_class::on($cawangan['mysql'])->select(DB::raw('SUM(upahblmbyar + utgonepercent + ujrah) as total_sum'))->where('tahun',$date->year)->first()->toArray();
        
        $gledger = new GeneralLedger($kodcaw,$firstDay,$lastDay);
        $loan_given = $gledger->getPinjamanDiberi();
        $loan_tawarruq = $gledger->getPembiayaanTawarruq();
        $advance_payment = $gledger->getBayaranPendahuluan();
        $submission = $gledger->getPendahuluanSerahan();
        $cash = $gledger->getpanjar();
        $remain_auction = $gledger->BakiLelongCalculation();
        $real_profit = $gledger->getKeuntunganSebenar();
        $current_profit = $gledger->SemakanKeuntunganLejer();
        $unclaimed_money =$gledger->getwtdLejer();
        $akrual_onepercent = $gledger->getTotalKeuntungan();
        $akrual_ujrah = $gledger->getTotalUjrah();

        $debit = /* $loan_given['Gadai']  + */ $loan_tawarruq['Gadai'] + $advance_payment['keluar'] +
                 $submission['serahan'] + $cash['bds'] + $cash['perubatan'] + $cash['keraian'] +
                 $remain_auction['rugi'] + $remain_auction['bakilelong'];
        $credit = /* $loan_given['Tebus'] + */ $loan_tawarruq['Tebus'] + $advance_payment['masuk'] +
                  $remain_auction['rugilelong'] + $remain_auction['baki'] + $real_profit +
                  $current_profit['keuntungan_semasa'] + $submission['pendahuluan'] + $remain_auction['caslelong'] + 
                  $current_profit['keuntungan_sebenar'] + $unclaimed_money + $akrual_onepercent + $akrual_ujrah;

        $cash_credit = $debit;
        $cash_debit = $credit;

        $total_tunaiditangan = $cash_debit - $cash_credit;
        $total_pembiayaantawarruq = $loan_tawarruq['Gadai'] - $loan_tawarruq['Tebus'];
        $total_bayaranpendahuluan = $advance_payment['keluar'] - $advance_payment['masuk'];
        $total_serahan = $submission['serahan'];
        $total_pendahuluan = $submission['pendahuluan'];
        $total_bds = -($cash['bds']);
        $total_perubatan = -($cash['perubatan']);
        $total_keraian = -($cash['keraian']);
        $total_keun_sebenar = $current_profit['keuntungan_sebenar'];
        $total_rugilelong = $remain_auction['rugi'] - $remain_auction['rugilelong'];
        $total_bakilelong = $remain_auction['baki'] - $remain_auction['bakilelong'];
        $total_pend_terakru = $akru_net;
        $total_ujrah = $akru_net_ujrah;
        $total_onepercent = $akru_net_onepercent;
        $total_keun_penggadai = $keun_penggadai['total_sum'];

        /* Current Balance Sheets */
        $array_tally = [];
        foreach($BSheetCurrentMonth as $key=>$value){
            if($key !== 'recno' && $key !== 'kodcaw' && $key !== 'tarikh' && $key !== 'tagtally' && $key !== 'created_at' && $key !== 'updated_at'){
                if(isset(${"total_$key"})){
                    if($key !== 'pend_terakru' && $key !== 'ujrah' && $key !== 'onepercent' && $key !== 'keun_penggadai'){
                        ${"temp_$key"} = $BSheetPreviousMonth[$key] + ${"total_$key"};
                    }else{
                        ${"temp_$key"} = ${"total_$key"};
                    }
                    $temp_array = ['key' => $key, 'totallejer' => (float)round(${"temp_$key"},2), 'currentbsheet' => (float)round($BSheetCurrentMonth[$key],2),'tally' => (float)round(${"temp_$key"},2) == (float)round($BSheetCurrentMonth[$key],2)];
                    array_push($array_tally,$temp_array);
                }
            }
        }

        return $array_tally;        
    }
}