Reward.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace addons\CloudStockTwo\common\logic\reward;
  3. use addons\CloudStockTwo\common\models\CloudStockTwoFillOrder;
  4. use common\enums\AddonsEnum;
  5. use common\models\order\Order;
  6. abstract class Reward
  7. {
  8. protected $bonus_agent; //分得奖励的代理商模型
  9. protected $stock_goods; //库存商品模型
  10. protected $after_distribution_unit_goods_price; //扣分润后的商品单价,(货款单价)
  11. public $reward_unit_price = 0; //商品奖励单价
  12. protected $after_count_unit_goods_price; //分配奖励后的货款单价
  13. protected $order_id; //订单id;
  14. protected $goods_num; //商城数量;
  15. protected $is_frozen; //是否结算
  16. protected $change_type; //资金变动类型
  17. protected $change_desc; //资金变动描述
  18. protected $mall_id;
  19. protected $is_job;
  20. protected $order_type;
  21. public function __construct($bonus_agent,
  22. $stock_goods,
  23. $after_distribution_unit_goods_price,
  24. $order_id,
  25. $goods_num,
  26. $is_frozen,
  27. $order_type,
  28. $change_desc, $is_job = 0)
  29. {
  30. $this->bonus_agent = $bonus_agent;
  31. $this->mall_id = $bonus_agent->mall_id;
  32. $this->stock_goods = $stock_goods;
  33. $this->after_distribution_unit_goods_price = $after_distribution_unit_goods_price;
  34. $this->order_id = $order_id;
  35. $this->goods_num = $goods_num;
  36. $this->is_frozen = $is_frozen;
  37. $this->order_type = $order_type;
  38. $this->change_desc = $change_desc;
  39. $this->is_job = $is_job;
  40. }
  41. /**
  42. * 记录奖励
  43. */
  44. abstract protected function addRewardLog();
  45. /**
  46. * 获取对应等级商品奖励金额单价
  47. */
  48. abstract protected function getRewardUnitPrice();
  49. protected function countAfterCountUnitGoodsPrice()
  50. {
  51. $this->getRewardUnitPrice();
  52. $configs = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_CLOUD_STOCK_TWO, 'basic');
  53. //平级奖扣除来源,平台还是货款
  54. switch ($configs['bonus']) {
  55. case 'payment':
  56. $money = $this->after_count_unit_goods_price = $this->after_distribution_unit_goods_price - $this->reward_unit_price;
  57. break;
  58. case 'platform':
  59. $money = $this->after_count_unit_goods_price = $this->after_distribution_unit_goods_price;
  60. break;
  61. default:
  62. $money = $this->after_count_unit_goods_price = $this->after_distribution_unit_goods_price - $this->reward_unit_price;
  63. }
  64. return $money;
  65. }
  66. /**
  67. * 获取分配奖励后的货款总价
  68. * @return mixed
  69. */
  70. protected function getAfterCountTotalGoodsPrice()
  71. {
  72. $this->countAfterCountUnitGoodsPrice();
  73. return $this->after_count_unit_goods_price * $this->goods_num;
  74. }
  75. /**
  76. * 计算并记录奖励,返回分配奖励后的货款总价
  77. * @return float|int|mixed
  78. */
  79. public function sendReward()
  80. {
  81. if ($this->after_distribution_unit_goods_price > 0) {
  82. $this->addRewardLog();
  83. return $this->getAfterCountTotalGoodsPrice();
  84. }
  85. return 0;
  86. }
  87. public function getInsertWallet($capital_type)
  88. {
  89. $insert_wallet = [];
  90. $commission_num = $this->reward_unit_price * $this->goods_num;
  91. if(empty($commission_num)) return $insert_wallet;
  92. if ($this->order_type == 'buying') {
  93. $source_table = Order::tableName();
  94. } else {
  95. $source_table = CloudStockTwoFillOrder::tableName();
  96. }
  97. $insert_wallet[] = [
  98. 'mall_id' => $this->mall_id,
  99. 'user_id' => $this->bonus_agent->user_id,
  100. 'is_frozen' => $this->is_frozen,
  101. 'wallet_type' => 'commission',
  102. 'money' => $commission_num,
  103. 'desc' => $this->change_desc,
  104. 'source_table' => $source_table,
  105. 'source_table_id' => $this->order_id,
  106. 'from_type' => AddonsEnum::ADDONS_NAME_CLOUD_STOCK_TWO,
  107. 'capital_type' => $capital_type,
  108. ];
  109. return $insert_wallet;
  110. }
  111. }