<?php

namespace App\Http\Controllers\Api\Transacciones;

use App\Danoral\Transacciones\TransDT;
use App\Danoral\Transacciones\TransDTView;
use App\Http\Controllers\Controller;

use App\Http\Resources\TransDTView as TransDTViewResources;
use App\Http\Resources\BalanceItem as BalanceItemResources;
use Illuminate\Http\Request;

class TransDTController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $input = $request->all();

        $tdtV = TransDTView::getTransDTView($input);

        $tdtV_BS = [];
        $tdtV_USD = [];

        $sumDebeBS = 0;
        $sumHaberBS = 0;
        $sumDebeUSD = 0;
        $sumHaberUSD = 0;
        foreach($tdtV as $idt){

            switch($idt->idMoneda){
                case 1001: //Bs
                    $tdtV_BS[] = $idt;

                    if($idt->dhDT==0) {                
                        $sumDebeBS += $idt->montoDT;
                    }else{
                        $sumHaberBS += $idt->montoDT;
                    }
                    break;

                case 1002: //USD
                    $tdtV_USD[] = $idt;

                    if($idt->dhDT==0) {                
                        $sumDebeUSD += $idt->montoDT;
                    }else{
                        $sumHaberUSD += $idt->montoDT;
                    }               
                    break; 
            }
        }

        $res = [
            'sumDebeBS'     => number_format($sumDebeBS, 2, ',', '.'),
            'sumHaberBS'    => number_format($sumHaberBS, 2, ',', '.'),
            'saldoBS'       => number_format(($sumDebeBS - $sumHaberBS), 2, ',', '.'),
            'sumDebeUSD'    => number_format($sumDebeUSD, 2, ',', '.'),
            'sumHaberUSD'   => number_format($sumHaberUSD, 2, ',', '.'),
            'saldoUSD'      => number_format(($sumDebeUSD - $sumHaberUSD), 2, ',', '.'),
            'countBS'       => count($tdtV_BS),
            'countUSD'      => count($tdtV_USD),
            'tDTViewBS'     => TransDTViewResources::Collection($tdtV_BS),
            'tDTViewUSD'    => TransDTViewResources::Collection($tdtV_USD),
        ];

        return response()->json($res, 200);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Danoral\Transacciones\TransDT  $transDT
     * @return \Illuminate\Http\Response
     */
    public function show(TransDT $transDT)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Danoral\Transacciones\TransDT  $transDT
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, TransDT $transDT)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Danoral\Transacciones\TransDT  $transDT
     * @return \Illuminate\Http\Response
     */
    public function destroy(TransDT $transDT)
    {
        //
    }

    public function getTransDTSearch(Request $request) {
        $input = $request->all();

        $tdtV = TransDTView::getTransDTView($input);

        $tdtV_BS = [];
        $tdtV_USD = [];

        $sumDebeBS = 0;
        $sumHaberBS = 0;
        $sumDebeUSD = 0;
        $sumHaberUSD = 0;
        foreach($tdtV as $idt){

            switch($idt->idMoneda){
                case 1001: //Bs
                    $tdtV_BS[] = $idt;

                    if($idt->dhDT==0) {                
                        $sumDebeBS += $idt->montoDT;
                    }else{
                        $sumHaberBS += $idt->montoDT;
                    }
                    break;

                case 1002: //USD
                    $tdtV_USD[] = $idt;

                    if($idt->dhDT==0) {                
                        $sumDebeUSD += $idt->montoDT;
                    }else{
                        $sumHaberUSD += $idt->montoDT;
                    }               
                    break; 
            }
        }

        $res = [
            'sumDebeBS'     => number_format($sumDebeBS, 2, ',', '.'),
            'sumHaberBS'    => number_format($sumHaberBS, 2, ',', '.'),
            'saldoBS'       => number_format(($sumDebeBS - $sumHaberBS), 2, ',', '.'),
            'sumDebeUSD'    => number_format($sumDebeUSD, 2, ',', '.'),
            'sumHaberUSD'   => number_format($sumHaberUSD, 2, ',', '.'),
            'saldoUSD'      => number_format(($sumDebeUSD - $sumHaberUSD), 2, ',', '.'),
            'countBS'       => count($tdtV_BS),
            'countUSD'      => count($tdtV_USD),
            'tDTViewBS'     => TransDTViewResources::Collection($tdtV_BS),
            'tDTViewUSD'    => TransDTViewResources::Collection($tdtV_USD),
        ];

        return response()->json($res, 200);

    }

    public function getBalance(Request $request) {
        $idE = $request->idE;

        $balance_BS     = TransDT::getBalance($idE, 1001);
        $balance_USD    = TransDT::getBalance($idE, 1002);

        $sumDebeBS = 0;
        $sumHaberBS = 0;
        $sumDebeUSD = 0;
        $sumHaberUSD = 0;

        foreach($balance_BS as $iBal){
            $sumDebeBS += $iBal->debe;
            $sumHaberBS += $iBal->haber;
        }

        foreach($balance_USD as $iBal){
            $sumDebeUSD += $iBal->debe;
            $sumHaberUSD += $iBal->haber;
        }

        $res = [
            'sumDebeBS'     => number_format($sumDebeBS, 2, ',', '.'),
            'sumHaberBS'    => number_format($sumHaberBS, 2, ',', '.'),
            'saldoBS'       => number_format(($sumDebeBS - $sumHaberBS), 2, ',', '.'),
            'sumDebeUSD'    => number_format($sumDebeUSD, 2, ',', '.'),
            'sumHaberUSD'   => number_format($sumHaberUSD, 2, ',', '.'),
            'saldoUSD'      => number_format(($sumDebeUSD - $sumHaberUSD), 2, ',', '.'),
            'countBS'       => count($balance_BS),
            'countUSD'      => count($balance_USD),
            'balanceBS'     => BalanceItemResources::Collection($balance_BS),
            'balanceUSD'    => BalanceItemResources::Collection($balance_USD),
        ];

        return response()->json($res, 200);

    }



}
