@abdulrehmandar2234
Here:
<?php
namespace App\Http\Livewire\Invoices;
use Livewire\Component;
use App\Models\Customer;
use App\Models\Service;
use Illuminate\Support\Facades\Validator;
use Carbon\Carbon;
use App\Models\PaymentInvoice;
use App\Models\PaymentInvoiceItems;
use Illuminate\Support\Facades\Http;
use LaravelDaily\Invoices\Invoice;
use LaravelDaily\Invoices\Classes\Buyer;
use LaravelDaily\Invoices\Classes\Party;
use LaravelDaily\Invoices\Classes\InvoiceItem;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
use Illuminate\Http\Request;
use Livewire\WithPagination;
class IncomesShow extends Component
{
use WithPagination;
public $sortBy = 'id';
public $sortDirection = 'asc';
public $perPage = '10';
public $invoiceID;
public $inv_comments,$cost,$vat_percentage,$inv_date,$start_date ,$end_date;
public $search;
public $modalConfirmDeleteVisible,$modalInvoiceFormVisible , $modalFormVisible ,$selectAll= false;
public function deleteShowModal($invoiceID)
{
$this->invoiceID = $invoiceID;
$this->modalConfirmDeleteVisible = true;
}
public function delete()
{
if(PaymentInvoiceItems::where('invoice_id', $this->invoiceID)->delete());
PaymentInvoice::destroy($this->invoiceID);
$this->modalConfirmDeleteVisible = false;
$this->resetPage();
$this->dispatchBrowserEvent('alert', ['type' => 'error', 'message' => 'Data Deleted!']);
}
public function updateModal($invoiceID)
{
$this->invoiceID = $invoiceID;
$this->modalFormVisible = true;
$this->loadInvoice();
}
public function loadInvoice()
{
$invoice = PaymentInvoice::find($this->invoiceID);
$this->start_date=$invoice->service->start_date ? $invoice->service->start_date->format('Y-m-d') : $invoice->service->start_date;
$this->end_date=$invoice->service->end_date ? $invoice->service->end_date->format('Y-m-d') : $invoice->service->end_date;
$this->inv_date = $invoice->inv_date->format('Y-m-d');
$this->inv_comments = $invoice->inv_comments;
$this->cost = $invoice->cost;
$this->vat_percentage = $invoice->vat_percentage;
}
protected function rules()
{
return [
'cost' => 'required',
'vat_percentage' => 'required',
];
}
public function update()
{
$this->validate();
$invoice = PaymentInvoice::where('id',$this->invoiceID)->with('service','main_invoice')->first();
$total_cost = null;
$total_vat_cost = floatval($this->cost * $this->vat_percentage / 100);
$vat_percentage = $this->vat_percentage;
$Withholding = null;
$WithholdingPercentage = null;
if (($invoice->invoice_type == "service_invoice") && (($invoice->service->type == "Subscription" && $invoice->service->name == "Support")) && $this->cost >= floatval(300)) {
$FinalCost = floatval($this->cost + ($this->cost * $vat_percentage / 100));
$WithholdingPercentage = 20;
$Withholding = floatval($this->cost * $WithholdingPercentage / 100);
$total_cost = $FinalCost - $Withholding;
} else {
$total_cost = floatval($this->cost * $this->vat_percentage / 100) + $this->cost;
}
$data = [
'inv_comments' => $this->inv_comments,
'inv_date' => $this->inv_date,
'cost' => $this->cost,
'vat_percentage' => $vat_percentage,
'total_cost' => $total_cost,
'total_vat_cost' => $total_vat_cost,
'with_holding' => $Withholding,
'with_holding_percentage' => $WithholdingPercentage
];
PaymentInvoice::find($this->invoiceID)->update($data);
//close modal form
$this->modalFormVisible = false;
//Show success message
$this->dispatchBrowserEvent('alert', ['type' => 'info', 'message' => 'Data Edited!']);
}
public function sortBy($field)
{
if ($this->sortDirection == 'asc') {
$this->sortDirection = 'desc';
} else {
$this->sortDirection = 'asc';
}
return $this->sortBy = $field;
}
public function mount()
{
$this->resetPage();
}
public function invoice($invoiceID)
{
$invoice = PaymentInvoice::where('id', $invoiceID)->with('items.service', 'customer', 'website')->first();
$seller = new Party([
'name' => 'NetGen',
'phone' => '(+30) 694 2624846',
'custom_fields' => [
// 'note' => 'IDDQD',
// 'Λογ.Τραπέζης' => '23423423432',
],
]);
$customer = new Party([
'name' => $invoice->customer->name,
'custom_fields' => [
'Επωνυμία' => $invoice->customer->brand_name ? $invoice->customer->brand_name : "null",
'Επάγγελμα' => $invoice->customer->profession ? $invoice->customer->profession : "null",
'ΑΦΜ' => $invoice->customer->vat ? $invoice->customer->vat : "null",
'Website' => $invoice->website->name,
'Email' => $invoice->customer->email ? $invoice->customer->email : "null",
'Διεύθυνση' => $invoice->customer->address ? $invoice->customer->address : "null",
'ΔΟΥ' => $invoice->customer->doi ? $invoice->customer->doi : "null",
'ΤΚ' => $invoice->customer->postal_code ? $invoice->customer->postal_code : "null",
],
]);
$items = [];
foreach ($invoice->items as $invoiceItem) {
array_push($items,
(new InvoiceItem())->title($invoiceItem->service->name)->pricePerUnit($invoiceItem->cost)->subTotalPrice($invoiceItem->total_cost),
);
}
$invoice = Invoice::make('receipt')
->setCustomData([
'reference_invoice_id' => !empty($invoice->main_invoice->invoice_id) ? $invoice->main_invoice->invoice_id : NULL,
'vat_percentage' => $invoice->vat_percentage,
'invoice_type' => ucwords(str_replace("_", " ", $invoice->invoice_type)),
'invoice_id' => $invoice->invoice_id,
'with_holding' => $invoice->with_holding,
'payment_type' => $invoice->payment_type,
'invoice_items' => $invoice->items,
'main_invoice_type' => $invoice->main_invoice_type,
'invoice_comments' => $invoice->inv_comments,
'invoice_date' => $invoice->inv_date,
'vat' => $invoice->total_vat_cost,
])
->series('NETGEN')
->serialNumberFormat('{SEQUENCE}')
->seller($seller)
->buyer($customer)
->date($invoice->created_at)
->dateFormat('d/m/Y')
->payUntilDays(14)
->currencySymbol('€')
->currencyCode('EURO')
->currencyFormat('{SYMBOL}{VALUE}')
->currencyThousandsSeparator('.')
->currencyDecimalPoint(',')
->taxableAmount($invoice->cost)
->totalTaxes($invoice->total_vat_cost)
->totalAmount($invoice->total_cost)
->filename($seller->name . ' ' . $customer->name)
->addItems($items)
->logo(public_path('/img/logo.png'));
$this->modalInvoiceFormVisible = false;
$this->dispatchBrowserEvent('alert', ['type' => 'success', 'message' => 'Invoice Created Successfully!']);
$invoices = PaymentInvoice::where('id', $invoiceID)->with('items.service', 'customer', 'website')->first();
$link = $invoice->url();
return response()->streamDownload(function () use ($invoice) {
echo $invoice->stream();
}, 'Invoice_'.$customer->name.'_'.$invoices->inv_date->format('d-m-Y').'.pdf');
}
public function render(Request $request)
{
$this->data = PaymentInvoice::query()
->with('items')
->search($this->search)
->orderBy($this->sortBy, $this->sortDirection)
->paginate($this->perPage);
return view('livewire.invoices.IncomesShow', ['invoices' => $this->data])
->extends('layouts.maindashboard')
->section('content');
}
}