OutlayService.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. *
  5. * User: king/QQ:995265288
  6. * Date: 2018/6/11 下午2:24
  7. * Email: livsyitian@163.com
  8. */
  9. namespace app\frontend\modules\withdraw\services;
  10. use app\common\exceptions\AppException;
  11. use app\common\facades\Setting;
  12. use app\frontend\modules\withdraw\models\Withdraw;
  13. class OutlayService
  14. {
  15. /**
  16. * @var array
  17. */
  18. private $income_set;
  19. /**
  20. * @var array
  21. */
  22. private $withdraw_set;
  23. /**
  24. * @var Withdraw
  25. */
  26. private $withdrawModel;
  27. public function __construct(Withdraw $withdrawModel)
  28. {
  29. $this->withdrawModel = $withdrawModel;
  30. $this->income_set = $this->incomeSet();
  31. $this->withdraw_set = $this->withdrawSet();
  32. }
  33. /**
  34. * @return float
  35. */
  36. public function getPoundageRate()
  37. {
  38. return $this->getIncomeSet('poundage_rate');
  39. }
  40. public function getPoundageType()
  41. {
  42. return $this->withdrawModel->poundage_type;
  43. }
  44. /**
  45. * @return float
  46. */
  47. public function getPoundage()
  48. {
  49. $rate = $this->getPoundageRate();
  50. $amount = $this->getWithdrawAmount();
  51. $type = $this->getPoundageType();
  52. return $this->calculate($amount, $rate,$type);
  53. }
  54. /**
  55. * @return float
  56. */
  57. public function getServiceTaxRate()
  58. {
  59. if(in_array($this->withdrawModel->mark, ['StoreCashier','StoreWithdraw','StoreBossWithdraw']))
  60. {
  61. return 0;
  62. }
  63. return $this->getWithdrawSet('servicetax_rate');
  64. }
  65. /**
  66. * 劳务税 = (提现金额 - 手续费)* 劳务税比例
  67. *
  68. * @return float
  69. */
  70. public function getServiceTax()
  71. {
  72. $rate = $this->getServiceTaxRate();
  73. $amount = $this->getWithdrawAmount();
  74. if(!(\Setting::get('withdraw.income.service_tax_calculation'))){
  75. $withdraw_poundage = $this->getPoundage();
  76. $amount = bcsub($amount, $withdraw_poundage, 2);
  77. }
  78. return $this->calculate($amount, $rate);
  79. }
  80. /**
  81. * @return float
  82. */
  83. public function getToBalancePoundageRate()
  84. {
  85. return $this->getWithdrawSet('special_poundage');
  86. }
  87. /**
  88. * @return float
  89. */
  90. public function getToBalancePoundageType()
  91. {
  92. return $this->getWithdrawSet('special_poundage_type');
  93. }
  94. /**
  95. * @return float
  96. */
  97. public function getToBalancePoundage()
  98. {
  99. $rate = $this->getToBalancePoundageRate();
  100. $amount = $this->getWithdrawAmount();
  101. $type = $this->getToBalancePoundageType();
  102. return $this->calculate($amount, $rate,$type);
  103. }
  104. /**
  105. * @return float
  106. */
  107. public function getToBalanceServiceTaxRate()
  108. {
  109. if(in_array($this->withdrawModel->mark, ['StoreCashier','StoreWithdraw','StoreBossWithdraw']))
  110. {
  111. return 0;
  112. }
  113. return $this->getWithdrawSet('special_service_tax');
  114. }
  115. /**
  116. * 余额独立
  117. * 劳务税 = (提现金额 - 手续费)* 劳务税比例
  118. * @return float
  119. */
  120. public function getToBalanceServiceTax()
  121. {
  122. $rate = $this->getToBalanceServiceTaxRate();
  123. $amount = $this->getWithdrawAmount();
  124. if(!(\Setting::get('withdraw.income.service_tax_calculation'))){
  125. $withdraw_poundage = $this->getToBalancePoundage();
  126. $amount = bcsub($amount, $withdraw_poundage, 2);
  127. }
  128. return $this->calculate($amount, $rate);
  129. }
  130. /**
  131. * @return float
  132. */
  133. private function getWithdrawAmount()
  134. {
  135. return $this->withdrawModel->amounts;
  136. }
  137. /**
  138. * Calculate
  139. *
  140. * @param $amount
  141. * @param $rate
  142. * @return float
  143. */
  144. private function calculate($amount, $rate,$type=0)
  145. {
  146. if($type == 1)
  147. {
  148. return $rate;
  149. }
  150. return bcdiv(bcmul($amount, $rate, 2), 100, 2);
  151. }
  152. private function getIncomeSet($key)
  153. {
  154. $result = array_get($this->income_set, $key, '0.00');
  155. return empty($result) ? '0.00' : $result;
  156. }
  157. /**
  158. * @param $key
  159. * @return string
  160. */
  161. private function getWithdrawSet($key)
  162. {
  163. $result = array_get($this->withdraw_set, $key, '0.00');
  164. return empty($result) ? '0.00' : $result;
  165. }
  166. /**
  167. * @return array
  168. */
  169. private function withdrawSet()
  170. {
  171. return $this->withdrawModel->withdraw_set;
  172. //return Setting::get('withdraw.income');
  173. }
  174. private function incomeSet()
  175. {
  176. return $this->withdrawModel->income_set;
  177. /*$mark = $this->withdrawModel->mark;
  178. if (!$mark) {
  179. throw new AppException('Mark error!');
  180. }
  181. if (!empty($this->withdrawModel->income_set)) {
  182. return $this->withdrawModel->income_set;
  183. }
  184. return Setting::get('withdraw.' . $mark);*/
  185. }
  186. }