Files
dbois/application/models/eloquent/Payment.php
T
2020-02-21 15:51:54 +01:00

74 lines
1.7 KiB
PHP

<?php
/**
* Created by Pensare Software.
* User: Theo Weijmer
* Date: 16/01/2018
* Time: 10:27
*/
class Payment extends BaseModel {
protected $table = "payments";
public function invoice()
{
return $this->belongsTo( 'Invoice' );
}
public static function getPaymentPerProject( $projectID )
{
$projectPayment = self::selectRAW( 'id, invoice_id, SUM(paymentamount) as payment' )
->whereHas( 'invoice', function ( $q ) use ( $projectID ) {
$q->whereProjectId( $projectID );
} )
->first();
if( ! $projectPayment) return 0;
return $projectPayment->payment;
}
public static function getProjectsWithPayments($year)
{
$projects = self::selectRaw('INV.project_id')
->distinct()
->join('invoices AS INV', 'INV.id', '=', 'payments.invoice_id')
->whereRaw('DATE_FORMAT(INV.invoicedate, "%Y") = "'. $year.'"')
->lists('project_id');
return $projects->toArray();
}
public static function getPaymentInYear( $year )
{
$payments = self::selectRAW('SUM(paymentamount) AS totalpayment')
->whereRaw("DATE_FORMAT(paymentdate, '%Y') = '{$year}'")
->first();
return $payments->totalpayment;
}
public static function savePayment( $paymentID, $payment )
{
$payment = (array) $payment;
if ( $paymentID == 0 )
{
$payment = Payment::create( $payment );
$paymentID = $payment['id'];
}
else
{
Payment::whereId( $paymentID )->update( $payment );
}
return $paymentID;
}
public static function removePayment( $paymentID )
{
Payment::whereId( $paymentID )->delete();
}
}