<?php
namespace App\Http\Controllers;
use App\Bank;
use App\BankTransaction;
use App\Expense;
use App\ExpensesType;
use App\Order;
use App\ReturnProduct;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ExpensesController extends Controller
{

    public function index()
    {
        $type_id = request('type');
        $tresury = auth()->user()->treasury->id;
        if($type_id){
            if($type_id=='all'){
                $expenses = Expense::query();
                $from = request()->fromdate;
                $to = request()->todate;
                if($from){
                    $expenses->whereRaw("DATE(created_at) >= '{$from}'");
                }
                if($to) {
                    $expenses->whereRaw("DATE(created_at) <= '{$to}'");
                }

                $expenses= $expenses->where('bank_id',$tresury)->latest()->get();
                $title = 'كل المصروفات';
            }else{
                $type = ExpensesType::find($type_id);
                $title = $type->name;

                $expenses = Expense::where('bank_id',$tresury)->where('expenses_type_id',request('type'))->latest()->get();
            }
            return view('expenses.list',compact('expenses','title'));
        }
        $expenses = Expense::query();
        $from = request()->fromdate;
        $to = request()->todate;
        if($from){
            $expenses->whereRaw("DATE(expenses.created_at) >= '{$from}'");
        }
        if($to) {
            $expenses->whereRaw("DATE(expenses.created_at) <= '{$to}'");
        }
        $expenses = $expenses->join('expenses_type',function($qry){
            $qry->on('expenses_type.id','=','expenses_type_id');
        })->select(DB::raw('expenses_type.id,expenses_type.name,sum(value) as total'))
            ->groupBy('expenses_type.id')
            ->where('bank_id',$tresury)
            ->get();
        return view('expenses.index',compact('expenses'));
    }

    public function report()
    {
        $list = Expense::query();
        $from = request()->fromdate;
        $to = request()->todate;
        if($from){
            $list->whereRaw("DATE(created_at) >= '{$from}'");
        }
        if($to) {
            $list->whereRaw("DATE(created_at) <= '{$to}'");
        }
        $list = $list->get();
        return view('expenses.report',compact('list'));
    }

    public function generaltaxreturnreport()
{
    $from = request('fromdate');
    $to = request('todate');

    // Initialize totals
    $salesTotal = $salesTax = $purchasesTotal = $purchasesTax = 0;
    $expensesTotal = $expensesTax = $returnsTotal = $returnsTax = 0;

    // Fetch all orders
    $orders = Order::query();
    if (!empty($from)) {
        $orders->whereDate('invoice_date', '>=', $from);
    }
    if (!empty($to)) {
        $orders->whereDate('invoice_date', '<=', $to);
    }
    $orders = $orders->get();

    // Group orders
    foreach ($orders as $order) {
        if ($order->invoice_type == 'sales') {
            $salesTotal += ($order->total - $order->tax_value);
            $salesTax += $order->tax_value;
        }
        if ($order->invoice_type == 'purchase') {
            $purchasesTotal += ($order->total - $order->tax_value);
            $purchasesTax += $order->tax_value;
        }
    }

    // Fetch expenses
    $expenses = Expense::query();
    if (!empty($from)) {
        $expenses->whereDate('created_at', '>=', $from);
    }
    if (!empty($to)) {
        $expenses->whereDate('created_at', '<=', $to);
    }
    $expenses = $expenses->get();
    foreach ($expenses as $expense) {
        $expensesTotal += ($expense->value - $expense->tax_value);
        $expensesTax += $expense->tax_value;
    }

    // Fetch returns
    $returns = ReturnProduct::query()->where('return_type', 'sales');
    if (!empty($from)) {
        $returns->whereDate('return_date', '>=', $from);
    }
    if (!empty($to)) {
        $returns->whereDate('return_date', '<=', $to);
    }
    $returns = $returns->get();
    foreach ($returns as $return) {
        $returnsTotal += ($return->total - $return->tax_value);
        $returnsTax += $return->tax_value;
    }

    // Build the list
    $list = [
        [
            'bcolor' => 'box-warning',
            'title' => 'المشتريات',
            'values' => [
                ['name' => 'إجمالي المشتريات بدون الضريبة', 'value' => $purchasesTotal],
                ['name' => 'إجمالي ضريبة المشتريات', 'value' => $purchasesTax],
                ['name' => 'إجمالي المشتريات مع الضريبة', 'value' => $purchasesTotal + $purchasesTax],
            ],
        ],
        [
            'bcolor' => 'box-success',
            'title' => 'المبيعات',
            'values' => [
                ['name' => 'إجمالي المبيعات بدون الضريبة', 'value' => $salesTotal],
                ['name' => 'إجمالي ضريبة المبيعات', 'value' => $salesTax],
                ['name' => 'إجمالي المبيعات مع الضريبة', 'value' => $salesTotal + $salesTax],
            ],
        ],
        [
            'bcolor' => 'box-info',
            'title' => 'المصروفات',
            'values' => [
                ['name' => 'إجمالي المصروفات بدون الضريبة', 'value' => $expensesTotal],
                ['name' => 'إجمالي ضريبة المصروفات', 'value' => $expensesTax],
                ['name' => 'إجمالي المصروفات مع الضريبة', 'value' => $expensesTotal + $expensesTax],
            ],
        ],
        [
            'bcolor' => 'box-danger',
            'title' => 'مرتجع المبيعات',
            'values' => [
                ['name' => 'إجمالي مرتجع المبيعات بدون الضريبة', 'value' => $returnsTotal],
                ['name' => 'إجمالي ضريبة مرتجع المبيعات', 'value' => $returnsTax],
                ['name' => 'إجمالي مرتجع المبيعات مع الضريبة', 'value' => $returnsTotal + $returnsTax],
            ],
        ],
    ];

    // Calculate final tax
    $finalTotalTax = ($salesTax) - ($purchasesTax + $expensesTax + $returnsTax);

    return view('reports.generaltaxreturnreport', compact('list', 'finalTotalTax'));
}

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        $expense = new Expense;
        $employee_id = $request->employee_id;
        return view('expenses.create',compact('expense','employee_id'));
    }

    /**
     * Expense a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $inputs = $request->except('_token');
        $inputs["creator_id"] = auth()->user()->id;
        $expense = Expense::create($inputs);
        $bankId = $request->bank_id;
        // activity()
        //     ->performedOn($expense)
        //     ->log("إضافة مصروف بقيمة ".$expense->value);
        if ($bankId) {
            $trans["bank_id"] = $bankId;
            $bank = Bank::find($bankId);
            $trans["op_date"] = date('Y-m-d');
            $trans["total"] = $bank->balance;
            $grand = currency($inputs['value'], currency()->getUserCurrency(), $bank->currency, $format = false);
            $note = 'المصروفات  | ' . $request->note;
            $trans["due"] = $bank->balance - $grand;
            $bank->balance -= $grand;
            $trans["type"] = "1";
            $trans["note"] = $note;
            $trans["value"] = $grand;
            $bank->save();
            $expense->transaction()->create($trans);
        }
        return redirect(route('expenses.index'))->with('alert-success', trans('front.Successfully added'));
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $expense = Expense::findOrFail($id);
        return view('expenses.show',compact('expense'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit(Expense $expense)
    {
        $employee_id = $expense->employee_id;
        return view('expenses.edit',compact('expense','employee_id'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Expense $expense)
    {
        $inputs = $request->except('_token');
        if($inputs['employee_id']){
            $inputs['partner_id'] = null;
        }
        $expense->update($inputs);
        $bankId = $request->bank_id;
        if ($bankId) {
            $oldtrans = $expense->transaction;
            if($oldtrans){
                $oldtrans->bank->balance += $oldtrans->getOriginal('value');
                $oldtrans->bank->save();
            }
            $trans["bank_id"] = $bankId;
            $bank = Bank::find($bankId);
            $trans["op_date"] = date('Y-m-d');
            $trans["total"] = $bank->balance;
            $grand = currency($inputs['value'], currency()->getUserCurrency(), $bank->currency, $format = false);
            $note = 'المصروفات  | ' . $request->note;
            $trans["due"] = $bank->balance - $grand;
            $bank->balance -= $grand;
            $trans["type"] = "1";
            $trans["note"] = $note;
            $trans["value"] = $grand;
            $bank->save();
            $expense->transaction()->update($trans);
        }
        return redirect(route('expenses.index'))->with('alert-success', trans('front.Modified successfully'));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Expense $expense)
    {
        $oldtrans = $expense->transaction;
        if($oldtrans){
            $bankId = $oldtrans->bank->id;
            $trans["bank_id"] = $bankId;
            $bank = Bank::find($bankId);
            $trans["op_date"] = date('Y-m-d');
            $trans["total"] = $bank->balance;
            $grand = $oldtrans->getOriginal('value');
            $note = 'مسح المصروف | '.$expense->note;
            $trans["due"] = $bank->balance - $grand;
            $bank->balance += $grand;
            $trans["type"] = "2";
            $trans["note"] = $note;
            $trans["value"] = $grand;
            $bank->save();
            $expense->transaction()->create($trans);

            $oldtrans->delete();
        }
        if($expense->delete()){
            return "done";
        }
        return "failed";
    }


}
