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.

136 lines
4.9 KiB

  1. <?php
  2. namespace App\Console\Commands;
  3. use DB;
  4. use App\Business;
  5. use Illuminate\Console\Command;
  6. use Illuminate\Support\Facades\Cache;
  7. class CostCommand extends Command
  8. {
  9. public const USER_FEE = 400;
  10. public const FILE_FEE = [
  11. 200 => 0,
  12. 400 => 1000,
  13. 800 => 2000,
  14. ];
  15. protected $signature = 'cost:work';
  16. protected $description = 'Run the cost worker';
  17. public function __construct()
  18. {
  19. parent::__construct();
  20. }
  21. public function handle()
  22. {
  23. // infinte loop
  24. while (true) {
  25. // to baghali ha
  26. $recorded_month = jdate($business->calculated_at)->format("Y-m-01");
  27. $calculated_at = Cache::get('calculated_at');
  28. if ($calculated_at === null) {
  29. $business = Business::orderBy('calculated_at')->first()->load('users', 'cost');
  30. $calculated_at = $business->calculated_at;
  31. $until_now = jdate()->getMonth() > jdate($business->calculated_at)->getMonth()
  32. ? jdate($business->calculated_at)->toCarbon()->setTime("00", "00", "00")
  33. : \Carbon\Carbon::now();
  34. Cache::put('calculated_at', $until_now, 60);
  35. }
  36. // if calculated_at less than an hour stop
  37. if (\Carbon\Carbon::now()->diffInMinutes($until_now) <= 60) {
  38. $this->info('nothing to cost');
  39. continue;
  40. }
  41. try {
  42. DB::beginTransaction();
  43. // business order by last_calculated_at take first
  44. if (!isset($business)) {
  45. $business = Business::orderBy('calculated_at')->first()->load('users', 'cost');
  46. }
  47. $user_fee = enum('business.fee.user');
  48. // get business employee
  49. $users_cost = $business->cost
  50. ->where('type', '=', 'users')
  51. ->where('fee', '=', $user_fee)
  52. ->where('month', '=', $recorded_month)
  53. ->where('amount', '=', $business->users->count())
  54. ->first();
  55. if ($users_cost === null) {
  56. $business->cost()->create([
  57. 'type' => 'users',
  58. 'month' => $recorded_month,
  59. 'amount' => $business->users->count(),
  60. 'fee' => $user_fee,
  61. 'duration' => $duration = $until_now->diffInSeconds($calculated_at), // from the created_at time of the newset fifth user
  62. 'additional' => $business->users->pluck('id')->toArray(),
  63. ]);
  64. } else {
  65. $users_cost->update([
  66. 'duration' => $duration = $until_now->diffInMinutes($calculated_at), // last calc - (current month - now else last calc - end of the past month),
  67. 'additional' => $business->users->pluck('id')->toArray(),
  68. ]);
  69. }
  70. $costs = $user_fee * $duration;
  71. // do the math in php
  72. if (intdiv($business->files_volume, 200) === 0) {
  73. $pads = 0;
  74. } else {
  75. $pads = intdiv($business->files_volume, 200) - 1;
  76. }
  77. $file_fee = enum('business.fee.file');
  78. $files = $business->cost
  79. ->where('type', '=', 'files')
  80. ->where('fee', '=', $file_fee)
  81. ->where('month', '=', $recorded_month)
  82. ->where('amount', '=', $business->files_volume)
  83. ->first();
  84. if ($files === null) {
  85. $business->cost()->create([
  86. 'type' => 'files',
  87. 'month' => $recorded_month,
  88. 'amount' => $pads,
  89. 'fee' => $file_fee,
  90. 'duration' => $duration = $until_now->diffInMinutes($calculated_at), // how to determine the file?,
  91. ]);
  92. } else {
  93. $files->update([
  94. 'duration' => $duration = $until_now->diffInMinutes($calculated_at), // last calc - (current month - now else last calc - end of the past month),,
  95. ]);
  96. }
  97. $costs += $file_fee * $duration;
  98. // increment and decrement of wallet in php
  99. // deduct costs from your business wallet
  100. // make sure save the calculated_at
  101. $business->update([
  102. 'wallet' => $business->wallet - $costs,
  103. 'calculated_at' => \Carbon\Carbon::now(),
  104. ]);
  105. DB::commit();
  106. } catch (Throwable $thr) {
  107. DB::rollback();
  108. throw $thr;
  109. continue;
  110. }
  111. }
  112. }
  113. }