Fixes for invoice rounding, where invoice total was different from reporting
This commit is contained in:
@@ -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 */
|
||||
|
Reference in New Issue
Block a user