<?php

namespace App\Http\Controllers\Administrator\payment;

use App\Fun\Fun;
use App\Http\Controllers\Controller;
use App\Model\account\JournalModel;
use App\Model\OtherIncomeExpense;
use App\Model\payment\PaymentItemModel;
use App\Model\payment\PaymentDetailModel;
use App\Model\payment\PaymentModel;
use App\Model\production\ProductionDoneDetailModel;
use App\Model\production\ProductionDoneModel;
use App\Model\production\ProductionRequestModel;
use App\Model\stock_inventory\PurchaseDetailsModel;
use App\Model\pos\SaleModel;
use App\Model\stock_inventory\TransferStockModel;
use App\User;
use Auth;
use DB;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use Illuminate\Validation\ValidationException;
use stdClass;
use Yajra\DataTables\DataTables;

class OtherIncomeExpenseController extends Controller
{
    function __construct()
    {
        $this->middleware('permission:Other Income Expense', ['only' => ['index']]);
        $this->middleware('permission:Other Income Expense Create', ['only' => ['create','store']]);
        $this->middleware('permission:Other Income Expense Edit', ['only' => ['edit','update']]);
    }

    public function index(Request $request)
    {
        if ($request->ajax()) {
            Fun::lang();
            $data = OtherIncomeExpense::where(function ($q)use($request){
                if($request->from_date&&$request->to_date){
                    $q->whereBetween('date',[sDate($request->from_date),sDate($request->to_date)]);
                }
                if($request->type){
                    $q->where('type',$request->type);
                }
                if($request->user_id){
                    $q->where('user_id',$request->user_id);
                }
                if(!uCan('Other Income Expense List')){
                    $q->where('user_id',auth()->id());
                }
            })->latest();
            return Datatables::of($data)
                ->addColumn('date', function ($master) {
                    return date('d-m-Y', strtotime($master->date));
                })
                ->addColumn('type', function ($master) {
                    return __("administrator.$master->type");
                })
                ->addColumn('user_id', function ($master) {
                    return findUser($master->user_id)->name;
                })
                ->addColumn('amount', function ($master) {
                    return number_format($master->amount,2).findCurrency($master->currency_id)->symbol;
                })
                ->addColumn('action', function ($master) {
                    $btn = '';
                    if (Gate::check('Other Income Expense Edit')) {
                        $btn .= '<button title="' . __('administrator.edit') . '" class="button_warning getHtmlForm" data-href = "' . route("other-income-expenses.edit", $master->id) . '">' . __('administrator.edit') . '</button>';
                    }
                    return $btn;
                })
                ->addIndexColumn()
                ->rawColumns([ 'action'])
                ->make(true);
        }
        return view('administrator.payment.other-income-expense.index');
    }
    public function create()

    {
        return view('administrator.payment.other-income-expense.create');
    }

    public function edit($id)

    {
        $master = OtherIncomeExpense::find($id);
        return view('administrator.payment.other-income-expense.edit', compact('master'));
    }
    public function store(Request $request)
    {
        return $this->update($request,null);
    }
    public function update(Request $request,$id)
    {
        $this->validate($request,[
            'date'=>'required',
            'type'=>'required',
            'title'=>'required',
            'amount'=>'required',
        ]);
        $data = $request->all();
        $data['date'] = sDate($request->date);
        $data['user_id'] = auth()->id();
        $data['branch_id'] = myBranch()->id;
        $data['currency_id'] = user_currency()->id;
        OtherIncomeExpense::updateOrCreate (['id'=> $id],$data);
        return response()->json(['status'=>'success','reload'=>1]);
    }

}
