150 lines
6.2 KiB
PHP
150 lines
6.2 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\UserInvoice;
|
|
use App\Services\HTMLHelper;
|
|
use App\Services\Payment;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Request;
|
|
|
|
class PaymentInvoiceController extends Controller
|
|
{
|
|
public function __construct()
|
|
{
|
|
$this->middleware('admin');
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
|
|
$this->setFilterVars();
|
|
$data = [
|
|
'filter_months' => HTMLHelper::getTransMonths(),
|
|
'filter_years' => HTMLHelper::getYearRange(),
|
|
];
|
|
|
|
return view('admin.payment.invoice', $data);
|
|
}
|
|
|
|
private function setFilterVars()
|
|
{
|
|
|
|
if (! session('invoice_filter_month')) {
|
|
session(['invoice_filter_month' => intval(date('m'))]);
|
|
}
|
|
if (! session('invoice_filter_year')) {
|
|
session(['invoice_filter_year' => intval(date('Y'))]);
|
|
}
|
|
if (Request::get('invoice_filter_name')) {
|
|
session(['invoice_filter_name' => Request::get('invoice_filter_name')]);
|
|
} else {
|
|
session(['invoice_filter_name' => '']);
|
|
}
|
|
if (Request::get('invoice_filter_month')) {
|
|
session(['invoice_filter_month' => Request::get('invoice_filter_month')]);
|
|
}
|
|
if (Request::get('invoice_filter_year')) {
|
|
session(['invoice_filter_year' => Request::get('invoice_filter_year')]);
|
|
}
|
|
}
|
|
|
|
private function initSearch($archive = false, $request = true)
|
|
{
|
|
$this->setFilterVars();
|
|
|
|
$query = UserInvoice::with('shopping_order')->with('shopping_order.shopping_user')->select('user_invoices.*')
|
|
->where('user_invoices.month', '=', Request::get('invoice_filter_month'))
|
|
->where('user_invoices.year', '=', Request::get('invoice_filter_year'));
|
|
|
|
if (Request::get('invoice_filter_name')) {
|
|
$query->whereHas('shopping_order.shopping_user', function ($query) {
|
|
return $query->where('billing_firstname', 'LIKE', '%'.Request::get('invoice_filter_name').'%')->orWhere('billing_lastname', 'LIKE', '%'.Request::get('invoice_filter_name').'%')->orWhere('billing_email', 'LIKE', '%'.Request::get('invoice_filter_name').'%');
|
|
})->get();
|
|
}
|
|
|
|
return $query;
|
|
}
|
|
|
|
public function stats(): JsonResponse
|
|
{
|
|
$this->setFilterVars();
|
|
|
|
$month = Request::get('invoice_filter_month', session('invoice_filter_month'));
|
|
$year = Request::get('invoice_filter_year', session('invoice_filter_year'));
|
|
$name = Request::get('invoice_filter_name', '');
|
|
|
|
$baseQuery = UserInvoice::query()
|
|
->where('user_invoices.month', $month)
|
|
->where('user_invoices.year', $year);
|
|
|
|
if ($name) {
|
|
$baseQuery->whereHas('shopping_order.shopping_user', function ($query) use ($name) {
|
|
$query->where('billing_firstname', 'LIKE', '%'.$name.'%')
|
|
->orWhere('billing_lastname', 'LIKE', '%'.$name.'%')
|
|
->orWhere('billing_email', 'LIKE', '%'.$name.'%');
|
|
});
|
|
}
|
|
|
|
$count = (clone $baseQuery)->count();
|
|
$total = (clone $baseQuery)
|
|
->join('shopping_orders', 'shopping_orders.id', '=', 'user_invoices.shopping_order_id')
|
|
->sum('shopping_orders.total_shipping');
|
|
|
|
return response()->json([
|
|
'count' => $count,
|
|
'total' => number_format((float) $total, 2, ',', '.'),
|
|
]);
|
|
}
|
|
|
|
public function datatable()
|
|
{
|
|
|
|
$query = $this->initSearch();
|
|
|
|
return \DataTables::eloquent($query)
|
|
->addColumn('id', function (UserInvoice $UserInvoice) {
|
|
if ($UserInvoice->shopping_order->auth_user_id) {
|
|
return '<a href="'.route('admin_sales_users_detail', [$UserInvoice->shopping_order->id]).'" class="btn icon-btn btn-sm btn-primary"><span class="fa fa-edit"></span></a>';
|
|
}
|
|
|
|
return '<a href="'.route('admin_sales_customers_detail', [$UserInvoice->shopping_order->id]).'" class="btn icon-btn btn-sm btn-primary"><span class="fa fa-edit"></span></a>';
|
|
})
|
|
->addColumn('total_shipping', function (UserInvoice $UserInvoice) {
|
|
return '<span class="no-line-break">'.$UserInvoice->shopping_order->getFormattedTotalShipping().' €</span>';
|
|
})
|
|
->addColumn('created_at', function (UserInvoice $UserInvoice) {
|
|
return $UserInvoice->created_at->format('d.m.Y');
|
|
})
|
|
->addColumn('txaction', function (UserInvoice $UserInvoice) {
|
|
if ($UserInvoice->shopping_order) {
|
|
return Payment::getShoppingOrderBadge($UserInvoice->shopping_order);
|
|
}
|
|
|
|
return '-';
|
|
})
|
|
->addColumn('status', function (UserInvoice $UserInvoice) {
|
|
return '<a href="#" data-toggle="modal" data-target="#modals-load-content" data-modal="modal-lg"
|
|
data-id="'.$UserInvoice->id.'" data-route="'.route('modal_load').'" data-action="user-credit-status" data-view="">
|
|
<span class="badge badge-pill badge-'.$UserInvoice->getStatusColor().'">'.$UserInvoice->getStatusType().'</span>
|
|
</a>';
|
|
})
|
|
->addColumn('invoice', function (UserInvoice $UserInvoice) {
|
|
$ret = '';
|
|
$ret .= '<a href="'.route('storage_file', [$UserInvoice->shopping_order->id, 'invoice', 'download']).'" class="btn btn-primary btn-xs"><i class="fa fa-download"></i></a> ';
|
|
$ret .= '<a href="'.route('storage_file', [$UserInvoice->shopping_order->id, 'invoice', 'stream']).'" target="_blank" class="btn btn-warning btn-xs"><i class="fa fa-eye"></i></a>';
|
|
|
|
return $ret;
|
|
})
|
|
->orderColumn('id', 'id $1')
|
|
->orderColumn('invoice_number', 'invoice_number $1')
|
|
->orderColumn('txaction', 'txaction $1')
|
|
->orderColumn('shipped', 'shipped $1')
|
|
->orderColumn('total_shipping', 'total_shipping $1')
|
|
->orderColumn('created_at', 'created_at $1')
|
|
->orderColumn('status', 'status $1')
|
|
|
|
->rawColumns(['id', 'shipping_order', 'txaction', 'total_shipping', 'status', 'txaction', 'invoice'])
|
|
->make(true);
|
|
}
|
|
}
|