fix for monthpayments

This commit is contained in:
2020-05-15 16:21:22 +02:00
parent dfeeb0b5e3
commit 437d969313
2 changed files with 92 additions and 52 deletions
@@ -86,6 +86,7 @@ class MonthPayments {
private function loopPayments() private function loopPayments()
{ {
$this->totalProjectPayments = 0;
foreach ( $this->project->invoices as $invoice ) foreach ( $this->project->invoices as $invoice )
{ {
foreach ( $invoice->payments as $this->payment ) foreach ( $invoice->payments as $this->payment )
+40 -1
View File
@@ -28,12 +28,51 @@ class Payment extends BaseModel {
return $projectPayment->payment; 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 ) public static function getProjectsWithPayments( $year )
{ {
$projects = self::selectRaw( 'INV.project_id' ) $projects = self::selectRaw( 'INV.project_id' )
->distinct() ->distinct()
->join( 'invoices AS INV', 'INV.id', '=', 'payments.invoice_id' ) ->join( 'invoices AS INV', 'INV.id', '=', 'payments.invoice_id' )
->whereRaw('DATE_FORMAT(INV.invoicedate, "%Y") = "'. $year.'"') ->whereRaw( 'DATE_FORMAT(paymentdate, "%Y") = "' . $year . '"' )
->lists( 'project_id' ); ->lists( 'project_id' );
return $projects->toArray(); return $projects->toArray();