78 lines
1.9 KiB
PHP
78 lines
1.9 KiB
PHP
<?php
|
|
/**
|
|
* Created by Pensare Software.
|
|
* User: Theo Weijmer
|
|
* Date: 23/01/2018
|
|
* Time: 15:58
|
|
*/
|
|
|
|
|
|
class Invoice extends BaseModel {
|
|
|
|
public function project()
|
|
{
|
|
return $this->belongsTo( 'Project' );
|
|
}
|
|
|
|
public function payments()
|
|
{
|
|
return $this->hasMany( 'Payment' );
|
|
}
|
|
|
|
public function vat()
|
|
{
|
|
return $this->belongsTo( 'Vat' );
|
|
}
|
|
|
|
public static function invoicePerYear( $projectID, $year, $thatYear = TRUE )
|
|
{
|
|
$invoices = Invoice::selectRAW( 'SUM(invoiceamount * ( 1 +(vatrate / 100))) AS invoicetotal' )
|
|
->join( 'vats', 'vats.id', '=', 'invoices.vat_id' )
|
|
->whereProjectId( $projectID );
|
|
|
|
if ( $thatYear )
|
|
{
|
|
$invoices = $invoices->whereRaw( "YEAR(invoicedate) = {$year}" );
|
|
}
|
|
else
|
|
{
|
|
$invoices = $invoices->whereRaw( "YEAR(invoicedate) != {$year}" );
|
|
}
|
|
|
|
if ( $invoices->count() == 0 ) return 0;
|
|
|
|
$invoices = $invoices->first();
|
|
|
|
return $invoices->invoicetotal;
|
|
}
|
|
|
|
public static function saveInvoice( $invoiceID, $invoice )
|
|
{
|
|
$invoice = (array) $invoice;
|
|
|
|
if ( $invoiceID == 0 )
|
|
{
|
|
$invoice = Invoice::create( $invoice );
|
|
$invoiceID = $invoice['id'];
|
|
}
|
|
else
|
|
{
|
|
Invoice::whereId( $invoiceID )->update( $invoice );
|
|
}
|
|
|
|
return $invoiceID;
|
|
}
|
|
|
|
public static function getInvoiced( $year, $orderType='invoicedate', $orderDirection='DESC' )
|
|
{
|
|
$invoiced = Invoice::selectRaw('invoicenumber, PRJ.projectname, projectnumber, CUS.name, invoicedate, CUS.name, ROUND(invoiceamount, 0) AS invoiceamount')
|
|
->join('projects AS PRJ', 'PRJ.id', '=', 'invoices.project_id')
|
|
->join('customers AS CUS', 'CUS.id', '=', 'PRJ.customer_id')
|
|
->whereRaw("DATE_FORMAT(invoicedate, '%Y') = {$year}")
|
|
->orderBy($orderType, $orderDirection)
|
|
->get();
|
|
|
|
return $invoiced;
|
|
}
|
|
}
|