<?php

namespace App\Http\Controllers\pos;

use App\CurrencyModel;
use App\Model\payment\CashInOutModel;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use Auth;
use Illuminate\Validation\ValidationException;
use Yajra\DataTables\DataTables;

class CashInOutController extends Controller
{
    function __construct()

    {
        // $this->middleware('permission:Purchase', ['only' => ['show', 'create', 'store']]);
    }

    public function index(Request $request)
    {
        if ($request->ajax()) {
            $date = $request->date;
            $date = $date ? $date : date('Y-m-d');
            $cash_in_out_old = CashInOutModel::where(function ($q) {
                $q->where('user_id', Auth::user()->id);
            })
                ->where(function ($q) use ($date) {
                    if ($date) {
                        $q->where('date', '<', date('Y-m-d', strtotime($date)));
                    }
                })
                ->orderBy('id', 'DESC')->get();
            $cash_in = 0;
            $cash_out = 0;
            foreach ($cash_in_out_old as $row) {
                if($row->currency_id==2){
                    $amount = $row->amount;
                }
                if($row->currency_id==1){
                    $amount = $row->amount/$row->rate;
                }
                if ($row->cash_type == 'cash_in') {
                    $cash_in += $amount;
                }
                if ($row->cash_type == 'cash_out') {
                    $cash_out += $amount;
                }
            }
            $old_amount = round($cash_in,5) - round($cash_out,5);
            $cash_in_out = CashInOutModel::where(function ($q) {
                $q->where('user_id', Auth::user()->id);
            })
                ->where(function ($q) use ($date) {
                    if ($date) {
                        $q->where('date', date('Y-m-d', strtotime($date)));
                    }
                })
                ->orderBy('id', 'DESC')->paginate(20);
            return view('pos.cash-in-out.get-data-table', compact('cash_in_out', 'old_amount'));
        }
        return view('pos.cash-in-out.index');
    }

    public function create()
    {
        $currency = getCurrency();
        return view('pos.cash-in-out.create', compact('currency'));
    }

    public function edit($id)
    {
        $currency = getCurrency();
        $data = CashInOutModel::find($id);
        return view('pos.cash-in-out.edit', compact('data', 'currency'));
    }

    public function store(Request $request)
    {
        $this->update($request, null);
        return 'ok';
    }

    public function update(Request $request, $id)
    {
        $this->validate($request, [
            'date' => 'required',
            'cash_type' => 'required',
            'pay_to_receive_from' => 'required',
            'currency_id' => 'required',
            'amount' => 'required',
            'invoice_number' => 'required|unique:nso007_user_cash_in_out,invoice_number,' . $id,
        ]);
        $amount = $request->amount;
        if (!$amount) {
            throw ValidationException::withMessages(['error' => 'Please Insert <b>Amount</b> !!!']);
        }
        CashInOutModel::UpdateOrCreate([
            'id' => $id
        ], [
            'branch_id' => Auth::user()->branch_id,
            'type' => 'input',
            'cash_type' => $request->cash_type,
            'year' => date('Y'),
            'user_id' => Auth::user()->id,
            'date' => date('Y-m-d', strtotime($request->date)),
            'invoice_number' => $request->invoice_number,
            'pay_to_receive_from' => $request->pay_to_receive_from,
            'amount' => $amount,
            'description' => $request->description,
            'note' => $request->note,
            'rate' => getRate(),
            'currency_id' => $request->currency_id,
        ]);
        return 'ok';
    }

}
