Fixes for invoice rounding, where invoice total was different from reporting
This commit is contained in:
parent
622147d584
commit
ac702397a6
@ -329,11 +329,11 @@ class Account extends Model implements IDs
|
||||
->from(
|
||||
(new Payment)
|
||||
->select([
|
||||
'invoice_id',
|
||||
DB::raw('0 as item'),
|
||||
DB::raw('0 as tax'),
|
||||
DB::raw('0 as discount'),
|
||||
DB::raw('0 as item_total'),
|
||||
'invoice_id',
|
||||
DB::raw('SUM(amount) AS payments'),
|
||||
DB::raw('SUM(fees_amt) AS payment_fees'),
|
||||
])
|
||||
@ -342,22 +342,13 @@ class Account extends Model implements IDs
|
||||
->where('payment_items.active',TRUE)
|
||||
->groupBy(['payment_items.invoice_id'])
|
||||
->union(
|
||||
(new InvoiceItem)
|
||||
->select([
|
||||
'invoice_id',
|
||||
DB::raw('ROUND(CAST(SUM(quantity*price_base) AS NUMERIC),2) AS item'),
|
||||
DB::raw('ROUND(CAST(SUM(COALESCE(amount,0)) AS NUMERIC),2) AS tax'),
|
||||
DB::raw('ROUND(CAST(SUM(COALESCE(invoice_items.discount_amt,0)) AS NUMERIC),2) AS discount'),
|
||||
DB::raw('ROUND(CAST(SUM(ROUND(CAST(quantity*(price_base-COALESCE(invoice_items.discount_amt,0)) AS NUMERIC),2))+SUM(ROUND(CAST(COALESCE(amount,0) AS NUMERIC),2)) AS NUMERIC),2) AS item_total'),
|
||||
DB::raw('0 as payments'),
|
||||
DB::raw('0 as payment_fees'),
|
||||
])
|
||||
->leftjoin('invoice_item_taxes',['invoice_item_taxes.invoice_item_id'=>'invoice_items.id'])
|
||||
InvoiceItem::invoice_items()
|
||||
->addSelect('invoice_items.invoice_id')
|
||||
->addSelect(DB::raw('0 as payments'))
|
||||
->addSelect(DB::raw('0 as payment_fees'))
|
||||
->rightjoin('invoices',['invoices.id'=>'invoice_items.invoice_id'])
|
||||
->where('invoice_items.active',TRUE)
|
||||
->where(fn($query)=>$query->where('invoice_item_taxes.active',TRUE)->orWhereNull('invoice_item_taxes.active'))
|
||||
->where('invoices.active',TRUE)
|
||||
->groupBy(['invoice_items.invoice_id']),
|
||||
->groupBy(['invoice_items.invoice_id'])
|
||||
),'p')
|
||||
->join('invoices',['invoices.id'=>'invoice_id'])
|
||||
->when(($all === FALSE),fn($query)=>$query->where('invoices.account_id',$this->id))
|
||||
@ -384,7 +375,7 @@ class Account extends Model implements IDs
|
||||
->join('payment_items',['payment_items.invoice_id'=>'invoices.id'])
|
||||
->join('payments',['payments.id'=>'payment_items.payment_id'])
|
||||
->addSelect(DB::raw('max(paid_at) as _paid_at'))
|
||||
->havingRaw('ROUND(CAST(SUM(item_total)-COALESCE(invoices.discount_amt,0)-SUM(payments) AS NUMERIC),2) <= 0');
|
||||
->havingRaw('ROUND(CAST(SUM(item_total)-COALESCE(invoices.discount_amt,0)-SUM(payments) AS NUMERIC),2) = 0');
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -422,7 +422,7 @@ class Invoice extends Model implements IDs
|
||||
*/
|
||||
public function getDueAttribute(): float
|
||||
{
|
||||
return sprintf('%3.2f',$this->getTotalAttribute()-$this->getPaidAttribute());
|
||||
return round($this->getTotalAttribute()-$this->getPaidAttribute(),2);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -2,9 +2,11 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Leenooks\Carbon as LeenooksCarbon;
|
||||
|
||||
use App\Traits\PushNew;
|
||||
@ -68,6 +70,30 @@ class InvoiceItem extends Model
|
||||
});
|
||||
}
|
||||
|
||||
public static function invoice_items(): Builder
|
||||
{
|
||||
// Choose your select tag and group by tag
|
||||
return (new InvoiceItem)
|
||||
->select([
|
||||
//'invoice_id',
|
||||
DB::raw('ROUND(CAST(SUM(quantity*price_base) AS NUMERIC),2) AS item'),
|
||||
DB::raw('ROUND(CAST(SUM(COALESCE(amount,0)) AS NUMERIC),2) AS tax'),
|
||||
DB::raw('ROUND(CAST(SUM(COALESCE(invoice_items.discount_amt,0)) AS NUMERIC),2) AS discount'),
|
||||
DB::raw('ROUND(CAST(SUM(ROUND(CAST(ROUND(CAST(quantity*(price_base-COALESCE(invoice_items.discount_amt,0)) AS NUMERIC),2)+COALESCE(amount,0) AS NUMERIC),2)) AS NUMERIC),2) AS item_total'),
|
||||
//DB::raw('0 as payments'),
|
||||
//DB::raw('0 as payment_fees'),
|
||||
])
|
||||
->leftjoin('invoice_item_taxes',['invoice_item_taxes.invoice_item_id'=>'invoice_items.id'])
|
||||
//->rightjoin('invoices',['invoices.id'=>'invoice_items.invoice_id'])
|
||||
->where('invoice_items.active',TRUE)
|
||||
->where(fn($query)=>$query
|
||||
->where('invoice_item_taxes.active',TRUE)
|
||||
->orWhereNull('invoice_item_taxes.active'))
|
||||
//->where('invoices.active',TRUE)
|
||||
//->groupBy(['invoice_id'])
|
||||
;
|
||||
}
|
||||
|
||||
/* RELATIONS */
|
||||
|
||||
public function invoice()
|
||||
@ -144,7 +170,7 @@ class InvoiceItem extends Model
|
||||
*/
|
||||
public function getSubTotalAttribute(): float
|
||||
{
|
||||
return sprintf('%3.2f',$this->quantity * ($this->price_base - $this->discount_amt));
|
||||
return round($this->quantity*($this->price_base-$this->discount_amt),2);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -154,7 +180,7 @@ class InvoiceItem extends Model
|
||||
*/
|
||||
public function getTaxAttribute(): float
|
||||
{
|
||||
return sprintf('%3.2f',$this->taxes->sum('amount'));
|
||||
return round($this->taxes->sum('amount'),2);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -164,7 +190,7 @@ class InvoiceItem extends Model
|
||||
*/
|
||||
public function getTotalAttribute(): float
|
||||
{
|
||||
return sprintf('%3.2f',$this->getSubTotalAttribute()+$this->getTaxAttribute());
|
||||
return round($this->getSubTotalAttribute()+$this->getTaxAttribute(),2);
|
||||
}
|
||||
|
||||
/* METHODS */
|
||||
|
@ -130,6 +130,6 @@ class Payment extends Model implements IDs
|
||||
*/
|
||||
public function getTotalAttribute(): float
|
||||
{
|
||||
return sprintf('%3.2f',$this->total_amt);
|
||||
return round($this->total_amt,2);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user