Revised database drivers, query builder, and file/inflector helpers for improved consistency and functionality. Updated copyrights, links, and standardized function names including deprecating `is_countable` in favor of `word_is_countable`. Added support for new SQL features and optimized query handling.
113 lines
2.9 KiB
PHP
113 lines
2.9 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 getPaymentsPerCustomerMonth( $year )
|
|
{
|
|
$projects = self::selectRaw( 'PRJ.id, projectname, CUS.name, SUM(paymentamount) AS totalProjectPayments' )
|
|
->join( 'invoices AS INV', 'INV.id', '=', 'payments.invoice_id' )
|
|
->join( 'projects AS PRJ', 'PRJ.id', '=', 'INV.project_id' )
|
|
->join( 'customers AS CUS', 'CUS.id', '=', 'PRJ.customer_id' )
|
|
->groupBy( 'PRJ.id' )
|
|
->whereRaw( 'DATE_FORMAT(paymentdate, "%Y") = ' . $year )
|
|
->get()
|
|
->toArray();
|
|
|
|
for ( $x = 0; $x < count( $projects ); $x ++ )
|
|
{
|
|
$projects[ $x ]['monthPayments'] = [];
|
|
$monthPayments = self::getMonthPaymentPerProject( $projects[ $x ]['id'], $year );
|
|
for ( $i = 1; $i <= 12; $i ++ )
|
|
{
|
|
$projects[ $x ]['monthPayment'][ $i ] = 0;
|
|
foreach ( $monthPayments as $monthPayment )
|
|
{
|
|
if ( $monthPayment->month == $i )
|
|
$projects[ $x ]['monthPayment'][ $i ] = $monthPayment->paymentamount;
|
|
}
|
|
}
|
|
}
|
|
|
|
return $projects;
|
|
}
|
|
|
|
public static function getMonthPaymentPerProject( $projectID, $year )
|
|
{
|
|
return self::selectRaw( 'SUM(paymentamount) AS paymentamount, DATE_FORMAT(paymentdate, "%m") AS month' )
|
|
->join( 'invoices AS INV', 'INV.id', '=', 'payments.invoice_id' )
|
|
->whereRaw( 'DATE_FORMAT(paymentdate, "%Y") = ' . $year )
|
|
->whereProjectId( $projectID )
|
|
->groupBy( \DB::raw( 'DATE_FORMAT(paymentdate, "%m")' ) )
|
|
->get();
|
|
}
|
|
|
|
public static function getProjectsWithPayments( $year )
|
|
{
|
|
$projects = self::selectRaw( 'INV.project_id' )
|
|
->distinct()
|
|
->join( 'invoices AS INV', 'INV.id', '=', 'payments.invoice_id' )
|
|
->whereRaw( 'DATE_FORMAT(paymentdate, "%Y") = "' . $year . '"' )
|
|
->pluck( '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();
|
|
}
|
|
|
|
}
|