Merge branch 'dillon/90-e-invoice-f' into vapor/development

This commit is contained in:
Dillon Ngo
2025-08-01 19:36:49 +08:00
@@ -71,6 +71,8 @@ class ExportsSalesInvoiceReport implements FromQuery, WithHeadings, WithHeadingR
public function map($booking): array
{
$records = [];
$averageCurrencyRate = 0;
$currencyId = 0;
$purchaseOrder = $booking->transactions()->where('type', TransactionType::PURCHASE_ORDER)->first();
$company = $booking->company()->first();
@@ -79,6 +81,29 @@ class ExportsSalesInvoiceReport implements FromQuery, WithHeadings, WithHeadingR
if(!$lastPaymentTransaction){
return $records;
}
$currencyId = $booking->fix_currency_id;
$averageCurrencyRate = $lastPaymentTransaction->currency_rate;
$paymentSum = $booking->transactions()
->where('type', TransactionType::PAYMENT)
->where('status', ApprovalStatus::COMPLETED)
->get()
->sum(function ($transaction) {
return round($transaction->amount, 2);
});
if ($paymentSum){
$averageCurrencyRate = $booking->transactions()
->where('type', TransactionType::PAYMENT)
->where('status', ApprovalStatus::COMPLETED)
->get()
->sum(function ($transaction) {
return $transaction->currency_rate;
}) / $booking->transactions()
->where('type', TransactionType::PAYMENT)
->where('status', ApprovalStatus::COMPLETED)
->count();
}
$documentDate = $lastPaymentTransaction->created_at;
if(Carbon::parse($booking->updated_at)->isAfter($lastPaymentTransaction->created_at)){
$documentDate = $booking->updated_at;
@@ -88,6 +113,12 @@ class ExportsSalesInvoiceReport implements FromQuery, WithHeadings, WithHeadingR
$firstItem = true;
$transactionDetails = $purchaseOrder->transactionDetails;
foreach ($transactionDetails as $detail) {
$detailPrice = 0;
if($averageCurrencyRate && $currencyId){
$exactUnitPrice = ($currencyId) === 1 ? $detail->price : bcdiv($detail->price, $averageCurrencyRate, 7);
$displayUnitPrice = round($exactUnitPrice, 2);
$detailPrice = round(bcmul($displayUnitPrice, $detail->quantity, 7), 2);
}
$records[] = [
$firstItem ? '<<New>>' : '',
$formattedDocumentDate,
@@ -100,7 +131,7 @@ class ExportsSalesInvoiceReport implements FromQuery, WithHeadings, WithHeadingR
'022',
'C',
$detail->quantity,
number_format($detail->price, 2),
$detailPrice ? number_format($detailPrice, 2): 0,
$firstItem ? 'T' : '',
$company->e_invoice ? 'F' : 'T'
];