<?php

namespace App\Http\Controllers\report;

use Auth;
use App\Fun\Fun;
use App\Model\payment\PaymentDetailModel;
use App\Model\payment\PaymentModel;
use App\Model\people\SupplierModel;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Yajra\DataTables\DataTables;

class ReportPurchasePaymentController extends Controller
{


    function __construct()
    {
        $this->middleware('permission:Report Purchase Payment List', ['only' => ['index']]);

    }

    public function index(Request $request)
    {

        $supplier = SupplierModel::pluck('supplier_name', 'id');
        $user = PaymentModel::where('type', 'purchase')
            ->where('status', 1)
            ->pluck('user_id');
        $user = User::whereIn('id', $user)->pluck('name', 'id');
        if ($request->ajax()) {
            $supplier = $request->supplier_id;
            $user = $request->user_id;
            $status = $request->status;
            $from_date = $request->start_date;
            $to_date = $request->end_date;


            $data = PaymentModel::select(
                'nso007_payment.*',
                'nso007_purchases.invoice_number',
                'nso007_suppliers.supplier_name',
                'nso007_suppliers.supplier_phone'
            )
                ->join('nso007_purchases', 'nso007_purchases.id', 'nso007_payment.invoice_id')
                ->join('nso007_suppliers', 'nso007_suppliers.id', 'nso007_payment.supplier_id')
                ->where(function ($query) {
                    $query->where('nso007_payment.type', 'purchase');
                })
                ->where(function ($query) use ($from_date, $to_date) {
                    if ($from_date) {
                        $query->whereDate('nso007_payment.payment_date', '>=', date('Y-m-d', strtotime($from_date)));
                    }
                    if ($to_date) {
                        $query->whereDate('nso007_payment.payment_date', '<=', date('Y-m-d', strtotime($to_date)));
                    }

                })
                ->where(function ($query) use ($user) {
                    if ($user) {
                        $query->where('nso007_payment.user_id', $user);
                    }

                })
                ->where(function ($query) use ($supplier) {
                    if ($supplier) {
                        $query->where('nso007_payment.supplier_id', $supplier);
                    }
                })
                ->where(function ($query) use ($status) {
                    if ($status) {
                        $query->where('nso007_payment.payment_status', $status);
                    }
                })
                ->orderBy('nso007_payment.id', 'DESC');
            return DataTables::of($data)
                ->editColumn('payment_date', function ($data) {
                    return myDate($data->payment_date);
                })
                ->editColumn('user_id', function ($data) {
                    return findUser($data->user_id)->name;
                })
                ->editColumn('invoice_number', function ($data) {
                    return "<u class='text-primary getHtmlFormShow cursor' data-width='900px' data-href='".route('purchases.show',$data->invoice_id)."'>$data->invoice_number</u>";
                })
                ->editColumn('payment_amount', function ($data) {
                    $data->cur = findCurrency($data->currency_id);
                    return ($data->cur->symbol??'') . number_format($data->payment_amount, 2).
                        "<i class='payment_amount' data-amount='$data->payment_amount' data-currency_id='$data->currency_id'></i>";
                })
                ->editColumn('paid_amount', function ($data) {
                    return ($data->cur->symbol??'') . number_format($data->paid_amount, 2).
                        "<i class='paid_amount' data-amount='$data->paid_amount'></i>";
                })
                ->editColumn('remain', function ($data) {
                    if ($data->returned_status == 1) {
                        $data->remain_amount = 0;
                    }
                    $data->remain_amount =  ($data->payment_amount - $data->paid_amount);
                    return ($data->cur->symbol??'').number_format($data->remain_amount,2).
                        "<i class='remain_amount' data-amount='$data->remain_amount'></i>";
                })
                ->editColumn('payment_status', function ($data) {
                    return checkPaymentStatus($data->payment_status);
                })
                ->addIndexColumn()
                ->rawColumns(['payment_status','supplier_name','supplier_phone','remain','invoice_number','paid_amount','payment_amount'])
                ->make(true);
        }

        return view('administrator.report-purchase-payment.index', compact('supplier', 'user'));
    }

}

