1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/app/Console/Commands/CalculatePayouts.php

127 lines
3.3 KiB
PHP
Raw Normal View History

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Models\DbServer;
use App\Models\User;
use App\Models\Company;
2017-12-12 13:25:39 +01:00
use App\Libraries\CurlUtils;
class CalculatePayouts extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
2017-12-12 13:25:39 +01:00
protected $signature = 'ninja:calculate-payouts {--type=} {--url=} {--password=}';
/**
* The console command description.
*
* @var string
*/
2017-12-12 13:25:39 +01:00
protected $description = 'Calculate payouts';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
2017-12-12 13:25:39 +01:00
$type = strtolower($this->option('type'));
switch ($type) {
case 'referral':
$this->referralPayouts();
break;
case 'reseller':
$this->resellerPayouts();
break;
}
}
2017-12-12 13:25:39 +01:00
private function referralPayouts()
{
$servers = DbServer::orderBy('id')->get(['name']);
$userMap = [];
foreach ($servers as $server) {
config(['database.default' => $server->name]);
$users = User::where('referral_code', '!=', '')
->get(['email', 'referral_code']);
foreach ($users as $user) {
$userMap[$user->referral_code] = $user->email;
}
}
foreach ($servers as $server) {
config(['database.default' => $server->name]);
$companies = Company::where('referral_code', '!=', '')
->with('payment.client.payments')
->whereNotNull('payment_id')
->get();
2020-02-23 11:13:39 +01:00
$this->info('User,Client,Date,Amount,Reference');
foreach ($companies as $company) {
2020-02-23 11:01:37 +01:00
if (!isset($userMap[$company->referral_code])) {
continue;
}
$user = $userMap[$company->referral_code];
$payment = $company->payment;
2018-07-08 13:01:01 +02:00
if ($payment) {
$client = $payment->client;
2018-07-08 13:01:01 +02:00
foreach ($client->payments as $payment) {
$amount = $payment->getCompletedAmount();
2020-02-23 11:13:39 +01:00
$this->info('"' . $user . '",' .
'"' . $client->getDisplayName() . '",' .
$payment->payment_date . ',' .
$amount . ',' .
$payment->transaction_reference
);
2018-07-08 13:01:01 +02:00
}
}
}
}
}
2017-12-12 13:25:39 +01:00
private function resellerPayouts()
{
$response = CurlUtils::post($this->option('url') . '/reseller_stats', [
'password' => $this->option('password')
]);
$this->info('Response:');
$this->info($response);
}
protected function getOptions()
{
return [
2017-12-12 13:25:39 +01:00
['type', null, InputOption::VALUE_OPTIONAL, 'Type', null],
['url', null, InputOption::VALUE_OPTIONAL, 'Url', null],
['password', null, InputOption::VALUE_OPTIONAL, 'Password', null],
];
}
}