Rebate.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace App\Models;
  3. use Carbon\Carbon;
  4. use Illuminate\Database\Eloquent\Builder;
  5. use Illuminate\Foundation\Auth\User as Authenticatable;
  6. use Illuminate\Notifications\Notifiable;
  7. use Laravel\Sanctum\HasApiTokens;
  8. /**
  9. * @mixin Builder
  10. * @method static Builder|static where($column, $operator = null, $value = null, $boolean = 'and')
  11. */
  12. class Rebate extends Authenticatable
  13. {
  14. use HasApiTokens, Notifiable;
  15. protected $table = 'rebates';
  16. // protected $hidden = ['created_at', 'updated_at'];
  17. protected $fillable = ['date', 'member_id', 'betting_amount', 'rebate_ratio',
  18. 'amount', 'status', 'first_name', 'username', 'audited_by',
  19. 'profit', 'huishui_percentage', 'huishui_restriction', 'huishui_amount',
  20. ];
  21. static function updateProfit($data)
  22. {
  23. $data['date'] = Carbon::now('America/New_York')->format('Y-m-d');
  24. $rebate = static::where('date', $data['date'])
  25. ->where('member_id', $data['member_id'])->first();
  26. $rebate->increment('profit', $data['profit']);
  27. return $rebate;
  28. }
  29. static function addOrUpdate($data)
  30. {
  31. $rebate = static::where('date', $data['date'])
  32. ->where('member_id', $data['member_id'])->first();
  33. if ($rebate) {
  34. $rebate->rebate_ratio = $data['rebate_ratio'];
  35. $rebate->first_name = $data['first_name'];
  36. $rebate->username = $data['username'];
  37. $rebate->save();
  38. $rebate->increment("betting_amount", $data['betting_amount']);
  39. } else {
  40. $rebate = static::create($data);
  41. }
  42. return $rebate;
  43. }
  44. protected function getHuishuiRestrictionAttribute($value){
  45. $huishui_restriction = Config::where('field', 'huishui_restriction')->first()->val;
  46. return $huishui_restriction;
  47. }
  48. protected function getHuishuiAmountAttribute($value)
  49. {
  50. $value = 0;
  51. $huishui_restriction = Config::where('field', 'huishui_restriction')->first()->val;
  52. $huishui_percentage = Config::where('field', 'huishui_percentage')->first()->val;
  53. $lose = $this->profit * -1;
  54. if ($lose >= $huishui_restriction) {
  55. $value = bcmul($lose, $huishui_percentage, 2); // 返利金额
  56. }
  57. return $value;
  58. }
  59. protected function getCreatedAtAttribute($value)
  60. {
  61. return \Carbon\Carbon::parse($value)->setTimezone('Asia/Shanghai')->format('Y-m-d H:i:s');
  62. }
  63. protected function getUpdatedAtAttribute($value)
  64. {
  65. return \Carbon\Carbon::parse($value)->setTimezone('Asia/Shanghai')->format('Y-m-d H:i:s');
  66. }
  67. }