<?php

namespace App\Http\Controllers\report;

use App\Model\payment\PaymentModel;
use App\Model\people\CustomerModel;
use App\Model\pos\SaleModel;
use Auth;
use App\Fun\Fun;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Yajra\DataTables\DataTables;
use App\User;

class ReportPaymentController extends Controller
{

    function __construct()
    {
        $this->middleware('permission:Report Payment List', ['only' => ['index']]);
        Fun::lang();
    }
    public function index(Request $request)
    {
        $seller = SaleModel::where('status', '1')->pluck('user_id');
        $seller = User::whereIn('id',$seller)->pluck('name','id');
        if ($request->ajax()) {
            $path = $request->path;
            $search = $request->input('search.value');
            $status = $request->status;
            $customer= $request->customer_id;
            $from_date = $request->start_date;
            $seller = $request->seller;
            $to_date = $request->end_date;
            $data = PaymentModel::select(
                'nso007_payment.*',
                'nso007_sales.invoice_number',
                'nso007_customers.phone',
                'nso007_customers.name',
                'nso007_sales.return_status as returned_status'
            )
                ->join('nso007_sales', 'nso007_sales.id', 'nso007_payment.invoice_id')
                ->leftjoin('nso007_customers', 'nso007_customers.id', 'nso007_sales.customer_id')
                ->where('nso007_payment.status', 1)
                ->where('nso007_sales.return_status', 0)
                ->where('nso007_sales.is_role', 'user')
                ->where('nso007_sales.status', 1)

                ->where(function ($query) use ($path) {
                    if ($path==='report-sale-payment') {
                        $query->whereIn('nso007_payment.type', ['retail','wholesale']);
                    }else{
                        $query->where('nso007_payment.type', 'sale-request');
                    }
                })
                ->where(function ($q) {
                    $mybr = myBranch();
                    if (!auth()->user()->can('View Sale Request All Branch')) {
                        $q->where('nso007_sales.user_id', auth()->id());
                    }
                    if (!$mybr->main) {
                        $q->where('nso007_sales.branch_id', $mybr->id);
                    }
                })
                ->where(function ($q) use ($from_date,$to_date) {
                    if ($from_date&&$to_date) {
                        $q->whereBetween('nso007_payment.payment_date', [sDate($from_date),sDate($to_date)]);
                    }
                })
                ->where(function ($query) use ($customer) {
                    if ($customer) {
                        $query->where('nso007_sales.customer_id',$customer);
                    }
                })
                ->where(function ($query) use ($status) {
                    if ($status) {
                        $query->where('nso007_payment.payment_status',$status);
                    }
                })
                ->where(function ($query) use ($seller) {
                    if ($seller) {
                        $query->where('nso007_sales.user_id',$seller);
                    }
                })



                ->orderBy('nso007_sales.id', 'DESC');
            return DataTables::of($data)
                ->editColumn('date', function ($data) {
                    return myDate($data->date);
                })
                ->editColumn('invoice_number', function ($data) {
                    return "<u class='text-primary getHtmlFormShow cursor' data-width='700px' data-href='".route('get-wholesale-invoice',$data->invoice_id)."'>$data->invoice_number</u>";
                })
                ->editColumn('user_id', function ($data) {
                    return findUser($data->user_id)->name;
                })
                ->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);
                    $data->remain_amount = $data->remain_amount>0?$data->remain_amount:0;
                    return ($data->cur->symbol??'').number_format($data->remain_amount,2).
                        "<i class='remain_amount' data-amount='$data->remain_amount'></i>";
                })
                ->editColumn('payment_date', function ($data) {
                    return myDate($data->payment_date);
                })
                ->editColumn('payment_status', function ($data) {
                    if($data->remain_amount==0){
                        $data->payment_status = 'paid';
                    }
                    if($data->discount==100){
                        $data->payment_status = 'paid';
                    }
                    return checkPaymentStatus($data->payment_status);
                })
                ->addIndexColumn()
                ->rawColumns(['payment_status', 'invoice_number', 'remain','payment_amount','paid_amount'])
                ->make(true);
        }

        return view('administrator.reportpayment.index',compact('seller'));
    }

}

