mirror of
https://github.com/Bigherollc/wticreatorstudio.git
synced 2026-08-04 00:16:08 -04:00
267 lines
10 KiB
PHP
267 lines
10 KiB
PHP
<?php
|
|
|
|
namespace Modules\StaffPayroll\Http\Controllers\Web;
|
|
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Http\Request;
|
|
use Barryvdh\DomPDF\Facade\Pdf;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Routing\Controller;
|
|
use Modules\StaffPayroll\Entities\Invoice;
|
|
use Illuminate\Contracts\Support\Renderable;
|
|
use Modules\StaffPayroll\Entities\InvoiceItem;
|
|
use Modules\StaffPayroll\Entities\InvoiceSetting;
|
|
use Modules\GeneralSetting\Entities\GeneralSetting;
|
|
use Modules\StaffPayroll\Transformers\InvoiceResource;
|
|
|
|
class InvoiceController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
return view('staffpayroll::index');
|
|
}
|
|
|
|
public function show(Invoice $invoice)
|
|
{
|
|
if(!permissionCheck('invoices.view.all') && $invoice->user_id != auth()->user()->id){
|
|
return redirect()->route('invoices.index');
|
|
}
|
|
|
|
$invoice->load(['user.staff']);
|
|
|
|
$videoUploadPayout = InvoiceItem::query()
|
|
->where('invoice_id', $invoice->id)
|
|
//->where(fn ($query) => $query->where('name', 'Video upload payout'))
|
|
->where(function($query) {
|
|
$query
|
|
->where(function($query) {
|
|
$query->where('name', 'Video upload payout')->where('description', 'REGEXP', 'ASIN\s+[A-Z0-9]{10}$');
|
|
})
|
|
->orWhere(function($query) {
|
|
$query->where('name', 'Item')->where('description', 'REGEXP', 'ASIN\s+[A-Z0-9]{10}$');
|
|
});
|
|
})
|
|
->where(function ($query) {
|
|
$query->whereHas('video', function($query) {
|
|
$query->where('is_short', false);
|
|
})
|
|
->orWhereNull('studio_plus_video_id');
|
|
})
|
|
->orderBy('issue_date', 'asc')
|
|
->get();
|
|
|
|
$shortVideos = InvoiceItem::query()
|
|
->where('invoice_id', $invoice->id)
|
|
->where(function ($query) {
|
|
$query
|
|
->where(function($query) {
|
|
$query->where('name', 'Video upload payout')->where('description', 'REGEXP', 'ASIN\s*([A-Z0-9]{10},)+');
|
|
})
|
|
->orWhere(function($query) {
|
|
$query->where('name', 'Item')->where('description', 'REGEXP', 'ASIN\s*([A-Z0-9]{10},)+');
|
|
});
|
|
})
|
|
->where(function ($query) {
|
|
$query->whereHas('video', function($query) {
|
|
$query->where('is_short', true);
|
|
})
|
|
->orWhereNull('studio_plus_video_id');
|
|
})
|
|
->orderBy('issue_date', 'asc')
|
|
->get();
|
|
|
|
$otherInvoiceItems = InvoiceItem::query()
|
|
->where('invoice_id', $invoice->id)
|
|
->where(fn ($query) => $query->where('name', '<>', 'Video upload payout')->where('description', 'NOT REGEXP', 'ASIN\s+[A-Z0-9]{10}')->where('description', 'NOT REGEXP', 'ASIN\s*([A-Z0-9]{10},)+'))
|
|
->orderBy('issue_date', 'asc')
|
|
->get();
|
|
|
|
$invoiceItems = collect($videoUploadPayout)->merge($shortVideos)->merge($otherInvoiceItems);
|
|
$invoiceItems = collect($invoiceItems)->map(function ($item) {
|
|
$item->issue_date = \Carbon\Carbon::parse($item->issue_date)->format('m-d-Y');
|
|
|
|
return $item;
|
|
});
|
|
|
|
//$totalItemsAmount = $invoiceItems->pluck('amount')->sum();
|
|
$totalItemsAmount = $invoiceItems->map(function($item) {
|
|
return $item->quantity * $item->amount;
|
|
})->sum();
|
|
|
|
$logoSetting = InvoiceSetting::query()
|
|
->where('name', 'logo')
|
|
->first();
|
|
|
|
$logo = $logoSetting
|
|
? asset("/public/storage/{$logoSetting->value}")
|
|
: asset('/Modules/StaffPayroll/Public/icons/wti.png');
|
|
|
|
$generalSetting = GeneralSetting::query()->first();
|
|
|
|
$last_day_of_the_month = Carbon::createFromFormat('Y-m-d', $invoice->issue_date)->endOfMonth()->format('m/d/Y');
|
|
|
|
return view('staffpayroll::pdfView', [
|
|
'invoice' => $invoice,
|
|
'invoice_number' => Str::padLeft($invoice->id, 7, '0'),
|
|
'user' => $invoice->user,
|
|
'items' => $invoiceItems,
|
|
'total' => number_format($totalItemsAmount, 2),
|
|
'logo' => $logo,
|
|
'generalSetting' => $generalSetting,
|
|
'last_day_of_the_month' => $last_day_of_the_month,
|
|
'notes' => $invoice->note,
|
|
]);
|
|
}
|
|
|
|
public function pdf(Invoice $invoice)
|
|
{
|
|
|
|
if(!permissionCheck('invoices.view.all') && $invoice->user_id != auth()->user()->id){
|
|
return redirect()->route('invoices.index');
|
|
}
|
|
|
|
$invoice->load(['user.staff']);
|
|
|
|
$videpUploadPayout = InvoiceItem::query()
|
|
->where('invoice_id', $invoice->id)
|
|
//->where(fn ($query) => $query->where('name', 'Video upload payout'))
|
|
->where(function($query) {
|
|
//$query->where('name', 'Video upload payout')->where('description', 'REGEXP', 'ASIN\s+[A-Z0-9]{10}$');
|
|
$query
|
|
->where(function($query) {
|
|
$query->where('name', 'Video upload payout')->where('description', 'REGEXP', 'ASIN\s+[A-Z0-9]{10}$');
|
|
})
|
|
->orWhere(function($query) {
|
|
$query->where('name', 'Item')->where('description', 'REGEXP', 'ASIN\s+[A-Z0-9]{10}$');
|
|
});
|
|
})
|
|
->where(function ($query) {
|
|
$query->whereHas('video', function($query) {
|
|
$query->where('is_short', false);
|
|
})
|
|
->orWhereNull('studio_plus_video_id');
|
|
})
|
|
->orderBy('issue_date', 'asc')
|
|
->get();
|
|
|
|
$shortVideos = InvoiceItem::query()
|
|
->where('invoice_id', $invoice->id)
|
|
->where(function ($query) {
|
|
$query
|
|
->where(function($query) {
|
|
$query->where('name', 'Video upload payout')->where('description', 'REGEXP', 'ASIN\s*([A-Z0-9]{10},)+');
|
|
})
|
|
->orWhere(function($query) {
|
|
$query->where('name', 'Item')->where('description', 'REGEXP', 'ASIN\s*([A-Z0-9]{10},)+');
|
|
});
|
|
})
|
|
->where(function ($query) {
|
|
$query->whereHas('video', function($query) {
|
|
$query->where('is_short', true);
|
|
})
|
|
->orWhereNull('studio_plus_video_id');
|
|
})
|
|
->orderBy('issue_date', 'asc')
|
|
->get();
|
|
|
|
$otherInvoiceItems = InvoiceItem::query()
|
|
->where('invoice_id', $invoice->id)
|
|
->where(fn ($query) => $query->where('name', '!=', 'Video upload payout')->where('description', 'NOT REGEXP', 'ASIN\s+[A-Z0-9]{10}'))
|
|
->orderBy('issue_date', 'asc')
|
|
->get();
|
|
|
|
$invoiceItems = collect($videpUploadPayout)->merge($shortVideos)->merge($otherInvoiceItems);
|
|
/*$invoiceItems = collect($invoiceItems)->map(function ($item) {
|
|
$item->issue_date = \Carbon\Carbon::parse($item->issue_date)->format('m-d-Y');
|
|
|
|
return $item;
|
|
});*/
|
|
|
|
//$totalItemsAmount = $invoiceItems->pluck('amount')->sum();
|
|
$totalItemsAmount = $invoiceItems->map(function($item) {
|
|
return $item->quantity * $item->amount;
|
|
})->sum();
|
|
|
|
$logoSetting = InvoiceSetting::query()
|
|
->where('name', 'logo')
|
|
->first();
|
|
|
|
$logo = $logoSetting
|
|
? asset("/public/storage/{$logoSetting->value}")
|
|
: asset('/Modules/StaffPayroll/Public/icons/wti.png');
|
|
|
|
$generalSetting = GeneralSetting::query()->first();
|
|
|
|
$last_day_of_the_month = Carbon::createFromFormat('Y-m-d', $invoice->issue_date)->endOfMonth()->format('m/d/Y');
|
|
|
|
$pdf = Pdf::loadView('staffpayroll::pdf.invoice', [
|
|
'invoice' => $invoice,
|
|
'invoice_number' => Str::padLeft($invoice->id, 7, '0'),
|
|
'user' => $invoice->user,
|
|
'items' => $invoiceItems,
|
|
'total' => number_format($totalItemsAmount, 2),
|
|
'logo' => $logo,
|
|
'generalSetting' => $generalSetting,
|
|
'last_day_of_the_month' => $last_day_of_the_month,
|
|
'notes' => $invoice->note,
|
|
]);
|
|
|
|
$time = now()->format('Y-m-d-H-i-s');
|
|
return $pdf->download("invoice-{$time}.pdf");
|
|
}
|
|
|
|
public function pdfx(Invoice $invoice)
|
|
{
|
|
$invoice->load(['user.staff']);
|
|
|
|
$videpUploadPayout = InvoiceItem::query()
|
|
->where('invoice_id', $invoice->id)
|
|
->where(fn ($query) => $query->where('name', 'Video upload payout')->orWhere('type', InvoiceItem::TYPE_VIDEO_UPLOAD_PAYOUT))
|
|
->orderBy('issue_date', 'asc')
|
|
->get();
|
|
|
|
$otherInvoiceItems = InvoiceItem::query()
|
|
->where('invoice_id', $invoice->id)
|
|
->where(fn ($query) => $query->where('name', '!=', 'Video upload payout')->orWhere('type', '!=', InvoiceItem::TYPE_VIDEO_UPLOAD_PAYOUT))
|
|
->orderBy('issue_date', 'asc')
|
|
->get();
|
|
|
|
$invoiceItems = collect($videpUploadPayout)->merge($otherInvoiceItems);
|
|
$invoiceItems = collect($invoiceItems)->map(function ($item) {
|
|
$item->issue_date = \Carbon\Carbon::parse($item->issue_date)->format('m-d-Y');
|
|
|
|
return $item;
|
|
});
|
|
|
|
$totalItemsAmount = $invoiceItems->pluck('amount')->sum();
|
|
|
|
$logoSetting = InvoiceSetting::query()
|
|
->where('name', 'logo')
|
|
->first();
|
|
|
|
$logo = $logoSetting
|
|
? asset("/public/storage/{$logoSetting->value}")
|
|
: asset('/Modules/StaffPayroll/Public/icons/wti.png');
|
|
|
|
$generalSetting = GeneralSetting::query()->first();
|
|
|
|
$pdf = Pdf::loadView('staffpayroll::pdf.invoice', [
|
|
'invoice' => $invoice,
|
|
'user' => $invoice->user,
|
|
'items' => $invoiceItems,
|
|
'total' => number_format($totalItemsAmount, 2),
|
|
'logo' => $logo,
|
|
'generalSetting' => $generalSetting
|
|
]);
|
|
|
|
$time = now()->format('Y-m-d-H-i-s');
|
|
return $pdf->download("invoice-{$time}.pdf");
|
|
}
|
|
|
|
public function summary()
|
|
{
|
|
return view('staffpayroll::invoice-summary.index');
|
|
}
|
|
}
|