Browse Source

Is there an award for this? Cost worker improvement

remotes/origin/mohammad
Mohammad Akbari 4 years ago
parent
commit
34998e384f
Signed by: akbarjimi GPG Key ID: 55726AEFECE5E683
  1. 104
      app/Console/Commands/CostCommand.php
  2. 6
      app/Http/Controllers/FileController.php
  3. 2
      app/Http/Resources/FileResource.php
  4. 1
      app/Models/Business.php
  5. 4
      app/Models/Cost.php
  6. 2
      app/Providers/AuthServiceProvider.php
  7. 2
      composer.json

104
app/Console/Commands/CostCommand.php

@ -4,78 +4,60 @@ namespace App\Console\Commands;
use Throwable; use Throwable;
use Carbon\Carbon; use Carbon\Carbon;
use App\Models\Cost;
use App\Models\Business; use App\Models\Business;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Morilog\Jalali\CalendarUtils; use Morilog\Jalali\CalendarUtils;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Storage;
class CostCommand extends Command class CostCommand extends Command
{ {
public const USER_FEE = 400;
public const FILE_FEE = [
200 => 0,
400 => 1000,
800 => 2000,
];
protected $signature = 'cost:work'; protected $signature = 'cost:work';
protected $description = 'Run the cost worker'; protected $description = 'Run the cost worker';
public function __construct()
{
parent::__construct();
}
public function handle() public function handle()
{ {
while (true) { while (true) {
$business = Business::find(221);
if ($business === null) {
$lock = Cache::get('lock', false);
if ($lock) {
$this->info('There is no business for auditing.');
continue; continue;
} }
$year = jdate()->getYear();
$month = jdate()->getMonth();
$day = jdate()->getDay();
$hour = jdate()->getHour();
$minute = jdate()->getMinute();
$second = jdate()->getSecond();
if (jdate()->getYear() > jdate($business->calculated_at)->getYear()) {
$year = jdate()->getYear();
$month = 1;
$day = 1;
$business = Business::orderBy('calculated_at')->first();
if ($business === null) {
continue;
} }
if (
jdate()->getYear() > jdate($business->calculated_at)->getYear()
&&
jdate()->getMonth() > jdate($business->calculated_at)->getMonth()
) {
$year = jdate()->getYear();
$month = jdate()->getMonth();
$day = 1;
if ($business->calculated_at->isFuture()) {
continue;
} }
$gDate = CalendarUtils::toGregorian($year, $month, $day);
$carbon = Carbon::createFromDate($gDate[0], $gDate[1], $gDate[2]);
$carbon->setTime($hour, $minute, $second);
$next_month = jdate($business->calculated_at)->addMonths();
[$year, $month, $day] = CalendarUtils::toGregorian(
$next_month->getYear(), $next_month->getMonth(), 1
);
$now = Carbon::createFromDate($year, $month, $day);
$now->setTime(0, 0, 0);
$now = $carbon;
if ($now->isFuture()) {
$now = Carbon::now();
}
// if calculated_at less than an hour stop // if calculated_at less than an hour stop
if ($now->diffInMinutes($business->calculated_at)) {
if ($now->diffInMinutes($business->calculated_at) <= 59) {
$this->info('Must be one hour after the last audit.'); $this->info('Must be one hour after the last audit.');
Cache::put('lock', true, $now->diffInSeconds($business->calculated_at));
continue; continue;
} }
try { try {
DB::beginTransaction(); DB::beginTransaction();
// Fixed amounts of expenses // Fixed amounts of expenses
$business->load('users', 'cost');
$business->load('users', 'files');
$costs = 0; $costs = 0;
$costs += $this->calculateCostOfBusinessUsers($business, $now); $costs += $this->calculateCostOfBusinessUsers($business, $now);
@ -86,11 +68,13 @@ class CostCommand extends Command
// make sure save the calculated_at // make sure save the calculated_at
$business->update([ $business->update([
'wallet' => $business->wallet - $costs, 'wallet' => $business->wallet - $costs,
'calculated_at' => Carbon::now(),
'calculated_at' => $now,
]); ]);
DB::commit(); DB::commit();
$this->info("The business #{$business->id} was audited.");
} catch (Throwable $throwable) { } catch (Throwable $throwable) {
throw $throwable;
DB::rollback(); DB::rollback();
report($throwable); report($throwable);
continue; continue;
@ -98,16 +82,18 @@ class CostCommand extends Command
} }
} }
public function calculateCostOfBusinessUsers($business, $until_now)
public function calculateCostOfBusinessUsers($business, $now)
{ {
$user_fee = enum('business.fee.user'); $user_fee = enum('business.fee.user');
$calculated_at = $business->calculated_at;
$recorded_month = jdate($business->calculated_at)->format("Y-m-01"); $recorded_month = jdate($business->calculated_at)->format("Y-m-01");
if ($business->users->isEmpty()) {
return 0;
}
// get business employee // get business employee
$users_cost = $business->cost $users_cost = $business->cost
->where('type', '=', 'users')
->where('type', '=', Cost::USER_TYPE)
->where('fee', '=', $user_fee) ->where('fee', '=', $user_fee)
->where('month', '=', $recorded_month) ->where('month', '=', $recorded_month)
->where('amount', '=', $business->users->count()) ->where('amount', '=', $business->users->count())
@ -115,16 +101,16 @@ class CostCommand extends Command
if ($users_cost === null) { if ($users_cost === null) {
$business->cost()->create([ $business->cost()->create([
'type' => 'users',
'type' => Cost::USER_TYPE,
'month' => $recorded_month, 'month' => $recorded_month,
'amount' => $business->users->count(), 'amount' => $business->users->count(),
'fee' => $user_fee, 'fee' => $user_fee,
'duration' => $duration = $until_now->diffInSeconds($calculated_at), // from the created_at time of the newset fifth user
'duration' => $duration = $now->diffInMinutes($business->calculated_at), // from the created_at time of the newset fifth user
'additional' => $business->users->pluck('id')->toArray(), 'additional' => $business->users->pluck('id')->toArray(),
]); ]);
} else { } else {
$users_cost->update([ $users_cost->update([
'duration' => $duration = $until_now->diffInMinutes($calculated_at) + $users_cost->duration, // last calc - (current month - now else last calc - end of the past month),
'duration' => $duration = $now->diffInMinutes($business->calculated_at) + $users_cost->duration, // last calc - (current month - now else last calc - end of the past month),
'additional' => $business->users->pluck('id')->toArray(), 'additional' => $business->users->pluck('id')->toArray(),
]); ]);
} }
@ -132,7 +118,7 @@ class CostCommand extends Command
return $user_fee * $duration; return $user_fee * $duration;
} }
public function calculateCostOfBusinessFiles($business, $until_now)
public function calculateCostOfBusinessFiles($business, $now)
{ {
$file_fee = enum('business.fee.file'); $file_fee = enum('business.fee.file');
$calculated_at = $business->calculated_at; $calculated_at = $business->calculated_at;
@ -140,31 +126,33 @@ class CostCommand extends Command
// do the math in php // do the math in php
if (intdiv($business->files_volume, 200) === 0) {
$pads = 0;
$packs = intdiv($business->files_volume, 200);
if ($packs === 0) {
return 0;
} else { } else {
$pads = intdiv($business->files_volume, 200) - 1;
$packs--;
} }
$files = $business->cost $files = $business->cost
->where('type', '=', 'files')
->where('type', '=', Cost::FILE_TYPE)
->where('fee', '=', $file_fee) ->where('fee', '=', $file_fee)
->where('month', '=', $recorded_month) ->where('month', '=', $recorded_month)
->where('amount', '=', $business->files_volume)
->where('amount', '=', $packs)
->first(); ->first();
if ($files === null) { if ($files === null) {
$business->cost()->create([ $business->cost()->create([
'type' => 'files',
'type' => Cost::FILE_TYPE,
'month' => $recorded_month, 'month' => $recorded_month,
'amount' => $pads,
'amount' => $packs,
'fee' => $file_fee, 'fee' => $file_fee,
'duration' => $duration = $until_now->diffInMinutes($calculated_at), // how to determine the file?,
'duration' => $duration = $now->diffInMinutes($calculated_at), // how to determine the file?,
'additional' => ['volume' => $business->files_volume],
]); ]);
} else { } else {
$files->update([ $files->update([
'duration' => $duration = $until_now->diffInMinutes($calculated_at) + $files->duration, // last calc - (current month - now else last calc - end of the past month),,
'duration' => $duration = $now->diffInMinutes($calculated_at) + $files->duration, // last calc - (current month - now else last calc - end of the past month),,
'additional' => ['volume' => $business->files_volume],
]); ]);
} }

6
app/Http/Controllers/FileController.php

@ -9,8 +9,8 @@ use App\Models\Business;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Http\UploadedFile; use Illuminate\Http\UploadedFile;
use App\Http\Resources\FileResource;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use App\HiLib\Resources\FileResource;
use Spatie\QueryBuilder\QueryBuilder; use Spatie\QueryBuilder\QueryBuilder;
use Spatie\QueryBuilder\AllowedFilter; use Spatie\QueryBuilder\AllowedFilter;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
@ -127,6 +127,10 @@ class FileController extends Controller
'description' => $request->description 'description' => $request->description
]); ]);
$business->update([
'files_volume' => $business->files_volume + $file_record->size
]);
return new FileResource($file_record); return new FileResource($file_record);
} }

2
app/Http/Resources/FileResource.php

@ -1,9 +1,7 @@
<?php <?php
namespace App\Http\Resources; namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource; use Illuminate\Http\Resources\Json\JsonResource;
class FileResource extends JsonResource class FileResource extends JsonResource

1
app/Models/Business.php

@ -42,6 +42,7 @@ class Business extends Model implements HasMedia
protected $casts = [ protected $casts = [
'has_avatar' => 'boolean', 'has_avatar' => 'boolean',
'calculated_at' => 'datetime',
]; ];
public function getValueOf(?string $key) public function getValueOf(?string $key)

4
app/Models/Cost.php

@ -6,6 +6,10 @@ use App\Models\Model;
class Cost extends Model class Cost extends Model
{ {
public const USER_TYPE = 'users';
public const FILE_TYPE = 'files';
public $perPage = 12; public $perPage = 12;
protected $fillable = [ protected $fillable = [

2
app/Providers/AuthServiceProvider.php

@ -40,7 +40,7 @@ class AuthServiceProvider extends ServiceProvider
'token' => $request->bearerToken(), 'token' => $request->bearerToken(),
'agent' => $request->getAgent(), 'agent' => $request->getAgent(),
'os' => $request->getOS(), 'os' => $request->getOS(),
])->first();
])->firstOrFail();
return $fingerprint->user->setAttribute('token', $fingerprint->token); return $fingerprint->user->setAttribute('token', $fingerprint->token);
}); });

2
composer.json

@ -20,6 +20,8 @@
"jenssegers/agent": "^2.6", "jenssegers/agent": "^2.6",
"laravel/socialite": "^5.1", "laravel/socialite": "^5.1",
"laravel/framework": "^8.12", "laravel/framework": "^8.12",
"laravel/legacy-factories": "^1",
"fruitcake/laravel-cors": "^2.0",
"league/flysystem-aws-s3-v3": "~1.0", "league/flysystem-aws-s3-v3": "~1.0",
"spatie/laravel-medialibrary": "^9.0", "spatie/laravel-medialibrary": "^9.0",
"spatie/laravel-query-builder": "^3.3", "spatie/laravel-query-builder": "^3.3",

Loading…
Cancel
Save