SettingForm.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. namespace app\modules\admin\models\integralAppreciation;
  3. use app\models\CashExt;
  4. use app\models\Cat;
  5. use app\models\Goods;
  6. use app\models\GoodsCat;
  7. use app\models\IntegralAppreciationPool;
  8. use app\models\IntegralAppreciationPoolSub;
  9. use app\models\Option;
  10. use yii\base\Model;
  11. class SettingForm extends Model
  12. {
  13. public $store_id;
  14. public $integral_project_name;//增值积分活动名称
  15. public $integral_bg;//增值积分活动背景图片
  16. public $integral_custom_name;//增值积分自定义名称
  17. public $integral_init_price;//增值积分初始价格
  18. public $integral_payout_pool_profit;//增值积分提现回流奖金池比例
  19. public $integral_give_profit;//增值积分转赠手续费比例
  20. public $integral_amount_to_integral_profit;//奖金池铸造积分百分比
  21. public $pay_type;
  22. public $bank;
  23. public $lg;
  24. public $remaining_sum;
  25. public $cash_service_charge;
  26. public $min_money;
  27. public $cash_max_single_day;
  28. public $cash_max_day;
  29. public $cash_price_amount;
  30. public $cash_price_integral;
  31. public $cash_price_balance;
  32. public $cash_price_type;
  33. public $is_open_integral_to_balance; //是否开启增积分转余额【0否 1是】
  34. public $is_open_balance_to_integral; //是否开启余额转增积分【0否 1是】
  35. public $integral_to_balance_min; //增积分转余额最低限额
  36. public $integral_to_balance_max; //增积分转余额最高限额
  37. public $balance_to_integral_min; //余额转增积分最低限额
  38. public $balance_to_integral_max; //余额转增积分最高限额
  39. public $balance_to_integral_fee; //余额转增积分手续费比例
  40. public $integral_to_balance_fee; //增积分转余额手续费比例
  41. public $amount;//奖金池金额
  42. public $total_integral;//奖金池总积分
  43. public function rules()
  44. {
  45. return [
  46. [['store_id', 'pay_type', 'bank', 'remaining_sum'], 'integer'],
  47. [[ 'cash_service_charge',
  48. 'min_money', 'cash_max_single_day', 'cash_max_day', 'cash_price_amount', 'cash_price_integral', 'cash_price_balance'
  49. , 'integral_init_price', 'integral_payout_pool_profit', 'integral_give_profit', 'integral_amount_to_integral_profit',
  50. 'is_open_integral_to_balance','is_open_balance_to_integral', 'integral_to_balance_min','integral_to_balance_max',
  51. 'balance_to_integral_min','balance_to_integral_max','integral_to_balance_fee','balance_to_integral_fee','amount','total_integral'], 'number'],
  52. [['cash_price_type', 'integral_project_name', 'integral_bg' ,'integral_custom_name'], 'string'],
  53. [['bank', 'remaining_sum', 'lg','is_open_integral_to_balance','is_open_balance_to_integral'], 'in', 'range' => [0, 1]],
  54. [['pay_type'], 'in', 'range' => [0, 1, 2, 3]]
  55. ];
  56. }
  57. public function attributeLabels()
  58. {
  59. return [
  60. 'store_id' => 'Store Id',
  61. 'integral_project_name' => '增值积分活动名称',
  62. 'integral_bg' => '增值积分活动背景图片',
  63. 'integral_custom_name' => '增值积分自定义名称',
  64. 'integral_init_price' => '增值积分初始价格',
  65. 'integral_payout_pool_profit' => '增值积分提现回流奖金池比例',
  66. 'integral_give_profit' => '增值积分转赠手续费比例',
  67. 'integral_amount_to_integral_profit' => '奖金池铸造积分百分比',
  68. 'is_open_integral_to_balance'=>'是否开启增值积分转余额【0否 1是】',
  69. 'integral_to_balance_fee'=> '增值积分转余额手续费比例',
  70. 'integral_to_balance_min'=> '增值积分转余额最低限额',
  71. 'integral_to_balance_max'=> '增值积分转余额最高限额',
  72. 'is_open_balance_to_integral' => '是否开启余额转启增值积分【0否 1是】',
  73. 'balance_to_integral_fee'=> '余额转增值积分手续费比例',
  74. 'balance_to_integral_min'=> '余额转增值积分最低限额',
  75. 'balance_to_integral_max'=> '余额转增值积分最高限额',
  76. 'amount'=>'积分底池金额',
  77. 'total_integral'=>'池子总积分',
  78. 'pay_type' => '提现方式【0微信 1支付宝 2微信&支付宝】',
  79. 'bank' => '是否开启银行卡提现',
  80. 'lg' => '是否开启灵工提现',
  81. 'remaining_sum' => '是否开启余额提现',
  82. 'cash_service_charge' => '提现手续费',
  83. 'min_money' => '最少提现金额',
  84. 'cash_max_single_day' => '每日提现上限',
  85. 'cash_max_day' => '平台提现上限',
  86. ];
  87. }
  88. public function settingIndex() {
  89. try {
  90. $store_id = $this->store_id;
  91. $baseUrl = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl;
  92. $data = [
  93. 'integral_project_name' => '',
  94. 'integral_bg' => $baseUrl . '/web/v1/statics/clientImg/integral_appreciation/integral_bg.png',
  95. 'integral_custom_name' => '增值积分',
  96. 'integral_init_price' => '0',
  97. 'integral_payout_pool_profit' => '0',
  98. 'integral_give_profit' => '0',
  99. 'integral_amount_to_integral_profit' => '0',
  100. 'pay_type' => '',
  101. 'bank' => 0,
  102. 'remaining_sum' => 0,
  103. 'lg' => 0,
  104. 'cash_service_charge' => 0,
  105. 'min_money' => 0,
  106. 'cash_max_single_day' => 0,
  107. 'cash_max_day' => 0,
  108. 'cash_price_type' => '1',
  109. 'cash_price_amount' => 100,
  110. 'cash_price_integral' => 0,
  111. 'cash_price_balance' => 0,
  112. 'is_open_integral_to_balance' => 0,//是否开启增值积分转余额 0否 1是
  113. 'is_open_balance_to_integral' => 0,//是否开启余额转启增值积分 0否 1是
  114. 'integral_to_balance_min'=>0, //增值积分转余额最低限额 0不限制
  115. 'integral_to_balance_max'=>0, //增值积分转余额最高限额 0不限制
  116. 'balance_to_integral_min'=>0, //余额转增值积分最低限额 0不限制
  117. 'balance_to_integral_max'=>0, //余额转增值积分最高限额 0不限制
  118. 'integral_to_balance_fee'=>0, //增积分转余额手续费比例 0不限制
  119. 'balance_to_integral_fee'=>0, //余额转增积分手续费比例 0不限制
  120. 'amount'=>0, //积分底池金额
  121. 'total_integral'=>0, //池子总积分
  122. ];
  123. $integral_appreciation_setting = Option::get('integral_appreciation_setting', $store_id, 'integral_appreciation')['value'];
  124. $integral_appreciation_setting = json_decode($integral_appreciation_setting ?? '', true);
  125. if (empty($integral_appreciation_setting)) {
  126. $integral_appreciation_setting = $data;
  127. }
  128. $integralAppreciationPool = IntegralAppreciationPool::findOne(['store_id' => $store_id]);
  129. $total_integral = $integralAppreciationPool->total_integral ?: 0;
  130. $amount = $integralAppreciationPool->amount ?: 0;
  131. $integral_price = $integralAppreciationPool->integral_price ?: 0;
  132. $integral_appreciation_setting['total_integral'] = $total_integral;
  133. $integral_appreciation_setting['amount'] = $amount;
  134. $integral_appreciation_setting['integral_init_price'] = $integral_price;
  135. return [
  136. 'code' => 0,
  137. 'msg' => '获取成功',
  138. 'data' => $integral_appreciation_setting
  139. ];
  140. } catch (\Exception $e) {
  141. return [
  142. 'code' => 1,
  143. 'msg' => $e->getMessage()
  144. ];
  145. }
  146. }
  147. public function settingEdit() {
  148. try {
  149. $store_id = $this->store_id;
  150. if (!$this->validate()) {
  151. throw new \Exception($this->getErrorSummary(false)[0]);
  152. }
  153. $baseUrl = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl;
  154. $data = [
  155. 'integral_project_name' => $this->integral_project_name ?: '增值积分中心',
  156. 'integral_bg' => $this->integral_bg ?: $baseUrl . '/web/v1/statics/clientImg/integral_appreciation/integral_bg.png',
  157. 'integral_custom_name' => $this->integral_custom_name ?: '增值积分',
  158. 'integral_init_price' => $this->integral_init_price ?: '0',
  159. 'integral_payout_pool_profit' => $this->integral_payout_pool_profit ?: '0',
  160. 'integral_give_profit' => $this->integral_give_profit ?: '0',
  161. 'integral_amount_to_integral_profit' => $this->integral_amount_to_integral_profit ?: '0',
  162. 'pay_type' => intval($this->pay_type),
  163. 'bank' => intval($this->bank),
  164. 'lg' => intval($this->lg),
  165. 'remaining_sum' => intval($this->remaining_sum),
  166. 'cash_service_charge' => floatval($this->cash_service_charge),
  167. 'min_money' => floatval($this->min_money),
  168. 'cash_max_single_day' => floatval($this->cash_max_single_day),
  169. 'cash_max_day' => floatval($this->cash_max_day),
  170. 'cash_price_type' => $this->cash_price_type ?? '1',
  171. 'cash_price_amount' => $this->cash_price_amount ?? 100,
  172. 'cash_price_integral' => $this->cash_price_integral ?? 0,
  173. 'cash_price_balance' => $this->cash_price_balance ?? 0,
  174. 'is_open_integral_to_balance' => $this->is_open_integral_to_balance,
  175. 'is_open_balance_to_integral' => $this->is_open_balance_to_integral,
  176. 'integral_to_balance_min'=>$this->integral_to_balance_min,
  177. 'integral_to_balance_max'=>$this->integral_to_balance_max,
  178. 'balance_to_integral_min'=>$this->balance_to_integral_min,
  179. 'balance_to_integral_max'=>$this->balance_to_integral_max,
  180. 'integral_to_balance_fee'=>$this->integral_to_balance_fee,
  181. 'balance_to_integral_fee'=>$this->balance_to_integral_fee,
  182. 'amount'=>$this->amount,
  183. 'total_integral'=>$this->total_integral,
  184. ];
  185. $total_profit = 0;
  186. $cash_price_type = explode(',', $this->cash_price_type);
  187. // if (in_array(CashExt::CASH_PRICE_TYPE_AMOUNT, $cash_price_type)) {
  188. // $total_profit = bcadd($total_profit, $this->cash_price_amount, 2);
  189. // }
  190. // if (in_array(CashExt::CASH_PRICE_TYPE_INTEGRAL, $cash_price_type)) {
  191. // $total_profit = bcadd($total_profit, $this->cash_price_integral, 2);
  192. // }
  193. // if (in_array(CashExt::CASH_PRICE_TYPE_BALANCE, $cash_price_type)) {
  194. // $total_profit = bcadd($total_profit, $this->cash_price_balance, 2);
  195. // }
  196. // if ($total_profit != 100) {
  197. // return [
  198. // 'code' => 1,
  199. // 'msg' => '佣金/积分/余额比例设置错误'
  200. // ];
  201. // }
  202. if ($data['integral_init_price'] <= 0) {
  203. throw new \Exception('初始积分价格必须大于0');
  204. }
  205. if($data['amount'] <= 0){
  206. throw new \Exception('底池金额必须大于0');
  207. }
  208. if($data['total_integral'] <= 0){
  209. throw new \Exception('总积分必须大于0');
  210. }
  211. $integral_price = round($data['amount'] / $data['total_integral'], 2);
  212. if($data['integral_init_price']!=$integral_price){
  213. throw new \Exception('底池金额除总积分不等于积分价格');
  214. }
  215. Option::set('integral_appreciation_setting', json_encode($data, JSON_UNESCAPED_UNICODE), $store_id, 'integral_appreciation');
  216. //初始化池子
  217. $pool = IntegralAppreciationPool::findOne(['store_id' => $store_id]);
  218. if(!$pool){
  219. $pool = new IntegralAppreciationPool();
  220. $pool->store_id = $store_id;
  221. $pool->amount = $data['amount'];
  222. $pool->total_integral = $data['total_integral'];
  223. $pool->integral_price = $data['integral_init_price'];
  224. $pool->save();
  225. //资金池记录
  226. $poolSub = new IntegralAppreciationPoolSub();
  227. $poolSub->store_id = $store_id;
  228. $poolSub->amount = $data['amount'];
  229. $poolSub->integral_price = $data['integral_init_price'];
  230. $poolSub->integral = $data['total_integral'];
  231. $poolSub->reflux_type=3;
  232. $poolSub->after_integral_price=$data['integral_init_price'];
  233. $poolSub->save();
  234. }else{
  235. //如果价格不一致,则记录资金池变动
  236. if($pool['integral_price']!=$data['integral_init_price']){
  237. $pool->amount = $data['amount'];
  238. $pool->total_integral = $data['total_integral'];
  239. $pool->integral_price = $data['integral_init_price'];
  240. $pool->save();
  241. $poolSub = new IntegralAppreciationPoolSub();
  242. $poolSub->store_id = $store_id;
  243. $poolSub->amount = $data['amount']-$pool['amount'];
  244. $poolSub->integral_price = $pool['integral_price'];
  245. $poolSub->integral = $data['total_integral'];
  246. $poolSub->reflux_type=3;
  247. $poolSub->after_integral_price=$data['integral_init_price'];
  248. $poolSub->save();
  249. }
  250. }
  251. return [
  252. 'code' => 0,
  253. 'msg' => '保存成功'
  254. ];
  255. } catch (\Exception $e) {
  256. return [
  257. 'code' => 1,
  258. 'msg' => $e->getMessage()
  259. ];
  260. }
  261. }
  262. }