Changeset View
Changeset View
Standalone View
Standalone View
src/app/Documents/Receipt.php
Show First 20 Lines • Show All 117 Lines • ▼ Show 20 Lines | protected function build() | ||||
$start = Carbon::create($this->year, $this->month, 1, 0, 0, 0); | $start = Carbon::create($this->year, $this->month, 1, 0, 0, 0); | ||||
$end = $start->copy()->endOfMonth(); | $end = $start->copy()->endOfMonth(); | ||||
$month = \trans('documents.month' . intval($this->month)); | $month = \trans('documents.month' . intval($this->month)); | ||||
$title = \trans('documents.receipt-title', ['year' => $this->year, 'month' => $month]); | $title = \trans('documents.receipt-title', ['year' => $this->year, 'month' => $month]); | ||||
$company = $this->companyData(); | $company = $this->companyData(); | ||||
if (self::$fakeMode) { | if (self::$fakeMode) { | ||||
$country = 'CH'; | |||||
$customer = [ | $customer = [ | ||||
'id' => $this->wallet->owner->id, | 'id' => $this->wallet->owner->id, | ||||
'wallet_id' => $this->wallet->id, | 'wallet_id' => $this->wallet->id, | ||||
'customer' => 'Freddie Krüger<br>7252 Westminster Lane<br>Forest Hills, NY 11375', | 'customer' => 'Freddie Krüger<br>7252 Westminster Lane<br>Forest Hills, NY 11375', | ||||
]; | ]; | ||||
$items = collect([ | $items = collect([ | ||||
(object) [ | (object) [ | ||||
Show All 13 Lines | protected function build() | ||||
(object) [ | (object) [ | ||||
'amount' => 99, | 'amount' => 99, | ||||
// @phpstan-ignore-next-line | // @phpstan-ignore-next-line | ||||
'updated_at' => $start->copy()->next()->next()->next(), | 'updated_at' => $start->copy()->next()->next()->next(), | ||||
], | ], | ||||
]); | ]); | ||||
} else { | } else { | ||||
$customer = $this->customerData(); | $customer = $this->customerData(); | ||||
$country = $this->wallet->owner->getSetting('country'); | |||||
$items = $this->wallet->payments() | $items = $this->wallet->payments() | ||||
->where('status', PaymentProvider::STATUS_PAID) | ->where('status', PaymentProvider::STATUS_PAID) | ||||
->where('updated_at', '>=', $start) | ->where('updated_at', '>=', $start) | ||||
->where('updated_at', '<', $end) | ->where('updated_at', '<', $end) | ||||
->where('amount', '>', 0) | ->where('amount', '>', 0) | ||||
->orderBy('updated_at') | ->orderBy('updated_at') | ||||
->get(); | ->get(); | ||||
} | } | ||||
$vatRate = \config('app.vat.rate'); | |||||
$vatCountries = explode(',', \config('app.vat.countries')); | |||||
$vatCountries = array_map('strtoupper', array_map('trim', $vatCountries)); | |||||
if (!$country || !in_array(strtoupper($country), $vatCountries)) { | |||||
$vatRate = 0; | |||||
} | |||||
$totalVat = 0; | |||||
$total = 0; | $total = 0; | ||||
$items = $items->map(function ($item) use (&$total, $appName) { | $items = $items->map(function ($item) use (&$total, &$totalVat, $appName, $vatRate) { | ||||
$total += $item->amount; | $amount = $item->amount; | ||||
if ($vatRate > 0) { | |||||
$amount = round($amount * ((100 - $vatRate) / 100)); | |||||
$totalVat += $item->amount - $amount; | |||||
} | |||||
$total += $amount; | |||||
return [ | return [ | ||||
'amount' => sprintf('%.2f %s', $item->amount / 100, $this->wallet->currency), | 'amount' => $this->wallet->money($amount), | ||||
'description' => \trans('documents.receipt-item-desc', ['site' => $appName]), | 'description' => \trans('documents.receipt-item-desc', ['site' => $appName]), | ||||
'date' => $item->updated_at->toDateString(), | 'date' => $item->updated_at->toDateString(), | ||||
]; | ]; | ||||
}); | }); | ||||
$total = sprintf('%.2f %s', $total / 100, $this->wallet->currency); | |||||
// Load the template | // Load the template | ||||
$view = view('documents.receipt') | $view = view('documents.receipt') | ||||
->with([ | ->with([ | ||||
'site' => $appName, | 'site' => $appName, | ||||
'title' => $title, | 'title' => $title, | ||||
'company' => $company, | 'company' => $company, | ||||
'customer' => $customer, | 'customer' => $customer, | ||||
'items' => $items, | 'items' => $items, | ||||
'total' => $total, | 'subTotal' => $this->wallet->money($total), | ||||
'total' => $this->wallet->money($total + $totalVat), | |||||
'totalVat' => $this->wallet->money($totalVat), | |||||
'vatRate' => preg_replace('/([.,]00|0|[.,])$/', '', sprintf('%.2f', $vatRate)), | |||||
'vat' => $vatRate > 0, | |||||
]); | ]); | ||||
return $view; | return $view; | ||||
} | } | ||||
/** | /** | ||||
* Prepare customer data for the template | * Prepare customer data for the template | ||||
* | * | ||||
▲ Show 20 Lines • Show All 48 Lines • Show Last 20 Lines |