diff --git a/application/libraries/reports/invoice/MonthPayments.php b/application/libraries/reports/invoice/MonthPayments.php index cf301ee..01edb38 100644 --- a/application/libraries/reports/invoice/MonthPayments.php +++ b/application/libraries/reports/invoice/MonthPayments.php @@ -86,6 +86,7 @@ class MonthPayments { private function loopPayments() { + $this->totalProjectPayments = 0; foreach ( $this->project->invoices as $invoice ) { foreach ( $invoice->payments as $this->payment ) diff --git a/application/models/eloquent/Payment.php b/application/models/eloquent/Payment.php index 5afbb1a..2da13a0 100644 --- a/application/models/eloquent/Payment.php +++ b/application/models/eloquent/Payment.php @@ -8,66 +8,105 @@ class Payment extends BaseModel { - protected $table = "payments"; + 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) + public function invoice() { - $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 $this->belongsTo( 'Invoice' ); + } - return $projects->toArray(); - } - - public static function getPaymentInYear( $year ) - { - $payments = self::selectRAW('SUM(paymentamount) AS totalpayment') - ->whereRaw("DATE_FORMAT(paymentdate, '%Y') = '{$year}'") + 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(); - return $payments->totalpayment; - } + if ( ! $projectPayment ) return 0; - public static function savePayment( $paymentID, $payment ) - { - $payment = (array) $payment; + return $projectPayment->payment; + } - if ( $paymentID == 0 ) - { - $payment = Payment::create( $payment ); - $paymentID = $payment['id']; - } - else - { - Payment::whereId( $paymentID )->update( $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(); - return $paymentID; - } + 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; + } + } + } - public static function removePayment( $paymentID ) - { - Payment::whereId( $paymentID )->delete(); - } + 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 . '"' ) + ->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(); + } }