Goods.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace addons\shopro\service\commission;
  3. use app\admin\model\shopro\commission\Level as LevelModel;
  4. use app\admin\model\shopro\commission\CommissionGoods as CommissionGoodsModel;
  5. /**
  6. * 分销商品
  7. */
  8. class Goods
  9. {
  10. public $commissionGoods; // 分销商品
  11. protected $commissionConfig = NULL; // 独立分销设置 默认无
  12. protected $commissionRules; // 分销规则
  13. protected $skuPriceId; // 商品规格ID
  14. /**
  15. * 获取商品实时分佣规则
  16. *
  17. * @param int $goods 商品
  18. * @param int $skuId 商品规格ID
  19. */
  20. public function __construct($goods, $skuPriceId = 0)
  21. {
  22. $commissionRules = CommissionGoodsModel::GOODS_COMMISSION_STATUS_OFF;
  23. if(is_numeric($goods)) {
  24. $this->commissionGoods = CommissionGoodsModel::where(['goods_id' => $goods, 'status' => CommissionGoodsModel::GOODS_COMMISSION_STATUS_ON])->find();
  25. }else {
  26. $this->commissionGoods = $goods;
  27. }
  28. $this->skuPriceId = $skuPriceId;
  29. if ($this->commissionGoods) {
  30. $commission_config = $this->commissionGoods->commission_config;
  31. if ($commission_config['status']) {
  32. $this->commissionConfig = $commission_config;
  33. }
  34. switch ($this->commissionGoods->self_rules) {
  35. // 默认分销规则
  36. case CommissionGoodsModel::GOODS_COMMISSION_RULES_DEFAULT:
  37. $commissionRules = $this->getDefaultCommissionRules();
  38. break;
  39. // 独立分销规则
  40. case CommissionGoodsModel::GOODS_COMMISSION_RULES_SELF:
  41. $commissionRules = $this->getSelfCommissionRules();
  42. break;
  43. // 批量分销规则
  44. case CommissionGoodsModel::GOODS_COMMISSION_RULES_BATCH:
  45. $commissionRules = $this->getBatchCommissionRules();
  46. break;
  47. }
  48. }
  49. $this->commissionRules = $commissionRules;
  50. }
  51. public function getCommissionConfig()
  52. {
  53. return $this->commissionConfig;
  54. }
  55. public function getCommissionRules()
  56. {
  57. return $this->commissionRules;
  58. }
  59. /**
  60. * 获取对应分销商等级、对应层级的商品佣金规则
  61. *
  62. * @param int $agentLevel 分销商等级(不是id)
  63. * @param int $commissionLevel 分销商层级(默认一级)
  64. */
  65. public function getCommissionLevelRule($agentLevel, $commissionLevel = 1)
  66. {
  67. if (isset($this->commissionRules[$agentLevel]) && isset($this->commissionRules[$agentLevel][$commissionLevel])) {
  68. $commissionRule = $this->commissionRules[$agentLevel][$commissionLevel];
  69. return $commissionRule;
  70. }
  71. return false;
  72. }
  73. /**
  74. * 计算对应规则分销佣金
  75. *
  76. * @param int $commissionRule 分销规则
  77. * @param int $amount 结算价格
  78. * @param int $goodsNum 购买数量
  79. */
  80. public function caculateGoodsCommission($commissionRule, $amount, $goodsNum = 1)
  81. {
  82. $commission = 0;
  83. if (!empty($commissionRule['rate']) && $commissionRule['rate'] > 0) {
  84. $commission = round($amount * $commissionRule['rate'] * 0.01, 2);
  85. }
  86. if (!empty($commissionRule['money']) && $commissionRule['money'] > 0) {
  87. $commission = $commissionRule['money'] * $goodsNum;
  88. }
  89. return number_format($commission, 2, '.', '');
  90. }
  91. // 获取分销商等级默认规则
  92. private function getDefaultCommissionRules()
  93. {
  94. $agentLevelRules = LevelModel::order('level asc')->column('commission_rules', 'level');
  95. $commissionRules = [];
  96. foreach ($agentLevelRules as $agentLevel => $rule) {
  97. $rule = json_decode($rule, true);
  98. foreach ($rule as $commission_level => $percent) {
  99. $commission_level = explode('_', $commission_level);
  100. $level = intval($commission_level[1]);
  101. $commissionRules[$agentLevel][$level] = ['rate' => $percent];
  102. }
  103. }
  104. return $commissionRules;
  105. }
  106. // 获取分销商品独立分佣规则
  107. private function getSelfCommissionRules()
  108. {
  109. $commissionRules = [];
  110. if (isset($this->commissionGoods->commission_rules[$this->skuPriceId])) {
  111. $commissionRules = $this->commissionGoods->commission_rules[$this->skuPriceId];
  112. }
  113. return $commissionRules;
  114. }
  115. // 获取商品批量分佣规则
  116. private function getBatchCommissionRules()
  117. {
  118. return $this->commissionGoods->commission_rules;
  119. }
  120. }