You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
123 lines
3.3 KiB
123 lines
3.3 KiB
<?php
|
|
|
|
namespace App\Entities\Reports;
|
|
|
|
use DB;
|
|
use App\Entities\BaseRepository;
|
|
use App\Entities\Payments\Payment;
|
|
use App\Entities\Projects\Project;
|
|
|
|
/**
|
|
* Reports Repository Class.
|
|
*
|
|
* @author Nafies Luthfi <nafiesL@gmail.com>
|
|
*/
|
|
class ReportsRepository extends BaseRepository
|
|
{
|
|
/**
|
|
* Payment model.
|
|
*
|
|
* @var \App\Entities\Payments\Payment
|
|
*/
|
|
protected $model;
|
|
|
|
/**
|
|
* Create ReportsRepository class instance.
|
|
*
|
|
* @param \App\Entities\Payments\Payment $model
|
|
*/
|
|
public function __construct(Payment $model)
|
|
{
|
|
parent::__construct($model);
|
|
}
|
|
|
|
/**
|
|
* Get payment daily report.
|
|
*
|
|
* @param string $date
|
|
* @param string $q
|
|
* @return \Illuminate\Database\Eloquent\Collection
|
|
*/
|
|
public function getDailyReports($date, $q)
|
|
{
|
|
return Payment::orderBy('date', 'desc')
|
|
->where('date', $date)
|
|
->with('partner', 'project')
|
|
->get();
|
|
}
|
|
|
|
/**
|
|
* Get payment montly report by year and month.
|
|
*
|
|
* @param string $year
|
|
* @param string $month
|
|
* @return \Illuminate\Support\Collection
|
|
*/
|
|
public function getMonthlyReports($year, $month)
|
|
{
|
|
$rawQuery = 'date, count(`id`) as count';
|
|
$rawQuery .= ', sum(if(in_out = 1, amount, 0)) AS cashin';
|
|
$rawQuery .= ', sum(if(in_out = 0, amount, 0)) AS cashout';
|
|
|
|
$reportsData = DB::table('payments')->select(DB::raw($rawQuery))
|
|
->where(DB::raw('YEAR(date)'), $year)
|
|
->where(DB::raw('MONTH(date)'), $month)
|
|
->groupBy('date')
|
|
->orderBy('date', 'asc')
|
|
->get();
|
|
|
|
$reports = [];
|
|
foreach ($reportsData as $report) {
|
|
$key = substr($report->date, -2);
|
|
$reports[$key] = $report;
|
|
$reports[$key]->profit = $report->cashin - $report->cashout;
|
|
}
|
|
|
|
return collect($reports);
|
|
}
|
|
|
|
/**
|
|
* Get payment yearly report.
|
|
*
|
|
* @param string $year
|
|
* @return \Illuminate\Support\Collection
|
|
*/
|
|
public function getYearlyReports($year)
|
|
{
|
|
$rawQuery = 'MONTH(date) as month';
|
|
$rawQuery .= ', count(`id`) as count';
|
|
$rawQuery .= ', sum(if(in_out = 1, amount, 0)) AS cashin';
|
|
$rawQuery .= ', sum(if(in_out = 0, amount, 0)) AS cashout';
|
|
|
|
$reportsData = DB::table('payments')->select(DB::raw($rawQuery))
|
|
->where(DB::raw('YEAR(date)'), $year)
|
|
->groupBy(DB::raw('YEAR(date)'))
|
|
->groupBy(DB::raw('MONTH(date)'))
|
|
->orderBy('date', 'asc')
|
|
->get();
|
|
|
|
$reports = [];
|
|
foreach ($reportsData as $report) {
|
|
$key = str_pad($report->month, 2, '0', STR_PAD_LEFT);
|
|
$reports[$key] = $report;
|
|
$reports[$key]->profit = $report->cashin - $report->cashout;
|
|
}
|
|
|
|
return collect($reports);
|
|
}
|
|
|
|
/**
|
|
* Get current credit/receiveble earnings report.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Collection
|
|
*/
|
|
public function getCurrentCredits()
|
|
{
|
|
// On Progress, Done, On Hold
|
|
$projects = Project::whereIn('status_id', [2, 3, 6])->with('payments', 'customer')->get();
|
|
|
|
return $projects->filter(function ($project) {
|
|
return $project->cashInTotal() < $project->project_value;
|
|
})->values();
|
|
}
|
|
}
|