| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\models;
- use app\constants\OptionSetting;
- use Yii;
- use yii\db\ActiveRecord;
- use yii\behaviors\TimestampBehavior;
- /**
- * This is the model class for table "{{%cash_ext}}".
- *
- * @property integer $id
- * @property integer $cash_id
- * @property integer $cash_price_type
- * @property float $price
- * @property integer $is_pay
- * @property float $service_charge
- * @property float $real_price
- * @property integer $created_at
- * @property integer $updated_at
- */
- class CashExt extends \yii\db\ActiveRecord
- {
- const CASH_PRICE_TYPE_AMOUNT = 1;
- const CASH_PRICE_TYPE_INTEGRAL = 2;
- const CASH_PRICE_TYPE_BALANCE = 3;
- const CASH_TYPE_SHARE = 'share';
- const CASH_TYPE_SHARE_HOLDER = 'shareHolder';
- const CASH_TYPE_AREA_DIVIDEND = 'areaDividend';
- const CASH_TYPE_TEAM_BONUS = 'teamBonus';
- const CASH_TYPE_SHARE_GROUP = 'shareGroup';
- const CASH_TYPE_SUPER_SALES = 'superSales';
- const CASH_TYPE_INTEGRAL_APPRECIATION = 'integralAppreciation';
- const CASH_TYPE_LOCAL_PUBLIC_RANKING = 'localPublicRanking';
- const CASH_TYPE_ARR = [
- self::CASH_TYPE_SHARE,
- self::CASH_TYPE_SHARE_HOLDER,
- self::CASH_TYPE_AREA_DIVIDEND,
- self::CASH_TYPE_TEAM_BONUS,
- self::CASH_TYPE_SHARE_GROUP,
- self::CASH_TYPE_SUPER_SALES,
- self::CASH_TYPE_INTEGRAL_APPRECIATION,
- self::CASH_TYPE_LOCAL_PUBLIC_RANKING
- ];
- public static $cashPriceTypeMap = [
- self::CASH_PRICE_TYPE_AMOUNT => '现金',
- self::CASH_PRICE_TYPE_INTEGRAL => '积分',
- self::CASH_PRICE_TYPE_BALANCE => '余额'
- ];
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%cash_ext}}';
- }
- public function behaviors()
- {
- return [
- [
- 'class' => TimestampBehavior::class,
- 'attributes' => [
- ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
- ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at']
- ]
- ]
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['id', 'cash_id', 'cash_price_type', 'is_pay', 'created_at', 'updated_at'], 'integer'],
- [['price', 'service_charge', 'real_price'], 'number']
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => '',
- 'cash_id' => '提现ID',
- 'cash_price_type' => '提现金额类型:1现金;2积分;3余额',
- 'price' => '提现金额',
- 'is_pay' => '是否打款',
- 'service_charge' => '手续费',
- 'real_price' => '实际打款金额',
- 'created_at' => '创建时间',
- 'updated_at' => '',
- ];
- }
- /**
- * 提现前计算各项提现金额
- * @param $type
- * @param $store_id
- * @param $cash_price
- * @return array
- */
- public static function cashFront($type, $store_id, $cash_price,$cashType = 0) {
- try {
- $cash_price_type = '';
- $cash_price_amount = 0;
- $cash_price_integral = 0;
- $cash_price_balance = 0;
- $cash_service_charge = 0;
- if ($type == self::CASH_TYPE_SHARE) {
- $share_basic_setting = Option::get(OptionSetting::SHARE_BASIC_SETTING, $store_id, OptionSetting::SHARE_GROUP_NAME)['value'];
- $share_basic_setting = json_decode($share_basic_setting ?: '', true);
- $cash_price_type = $share_basic_setting['cash_price_type']['value'] ?? '1';
- $cash_price_amount = $share_basic_setting['cash_price_amount']['value'] ?? 100;
- $cash_price_integral = $share_basic_setting['cash_price_integral']['value'] ?? 0;
- $cash_price_balance = $share_basic_setting['cash_price_balance']['value'] ?? 0;
- $cash_service_charge = $share_basic_setting['cash_service_charge']['value'] ?? 0;
- }
- if ($type == self::CASH_TYPE_SHARE_HOLDER) {
- $cash_price_type = Option::get('cash_price_type', $store_id, 'bonus_pool', '1')['value'];
- $cash_price_amount = Option::get('cash_price_amount', $store_id, 'bonus_pool', 100)['value'];
- $cash_price_integral = Option::get('cash_price_integral', $store_id, 'bonus_pool', 0)['value'];
- $cash_price_balance = Option::get('cash_price_balance', $store_id, 'bonus_pool', 0)['value'];
- $cash_service_charge = Option::get('shareHolderCashProfit', $store_id, 'bonus_pool', 0)['value'];
- }
- if ($type == self::CASH_TYPE_AREA_DIVIDEND) {
- $cash_price_type = Option::get('cash_price_type', $store_id, 'bonus_pool_area_agent', '1')['value'];
- $cash_price_amount = Option::get('cash_price_amount', $store_id, 'bonus_pool_area_agent', 100)['value'];
- $cash_price_integral = Option::get('cash_price_integral', $store_id, 'bonus_pool_area_agent', 0)['value'];
- $cash_price_balance = Option::get('cash_price_balance', $store_id, 'bonus_pool_area_agent', 0)['value'];
- $cash_service_charge = Option::get('shareHolderCashProfit', $store_id, 'bonus_pool_area_agent', 0)['value'];
- }
- if ($type == self::CASH_TYPE_TEAM_BONUS) {
- $teamBonus = Option::get(OptionSetting::TEAM_BONUS_SETTING, $store_id, 'store')['value'];
- $teamBonus = json_decode($teamBonus ?: '', true);
- $cash_price_type = $teamBonus['cash_price_type'] ?? '1';
- $cash_price_amount = $teamBonus['cash_price_amount'] ?? 100;
- $cash_price_integral = $teamBonus['cash_price_integral'] ?? 0;
- $cash_price_balance = $teamBonus['cash_price_balance'] ?? 0;
- $cash_service_charge = $setting['cash_rate'] ?? 0;
- }
- if ($type == self::CASH_TYPE_SHARE_GROUP) {
- $share_group_setting = Option::get('share_group_setting', $store_id, 'share_group')['value'];
- $share_group_setting = json_decode($share_group_setting ?: '', true);
- $cash_price_type = $share_group_setting['cash_price_type'] ?? '1';
- $cash_price_amount = $share_group_setting['cash_price_amount'] ?? 100;
- $cash_price_integral = $share_group_setting['cash_price_integral'] ?? 0;
- $cash_price_balance = $share_group_setting['cash_price_balance'] ?? 0;
- $cash_service_charge = $share_group_setting['cash_service_charge'] ?? 0;
- }
- if ($type == self::CASH_TYPE_SUPER_SALES) {
- $share_group_setting = Option::get('super_sales_setting', $store_id, 'super_sales')['value'];
- $share_group_setting = json_decode($share_group_setting ?: '', true);
- $cash_price_type = $share_group_setting['cash_price_type'] ?? '1';
- $cash_price_amount = $share_group_setting['cash_price_amount'] ?? 100;
- $cash_price_integral = $share_group_setting['cash_price_integral'] ?? 0;
- $cash_price_balance = $share_group_setting['cash_price_balance'] ?? 0;
- $cash_service_charge = $share_group_setting['cash_service_charge'] ?? 0;
- }
- if ($type == self::CASH_TYPE_INTEGRAL_APPRECIATION) {
- $integral_appreciation_setting = Option::get('integral_appreciation_setting', $store_id, 'integral_appreciation')['value'];
- $integral_appreciation_setting = json_decode($integral_appreciation_setting ?: '', true);
- $cash_price_type = $integral_appreciation_setting['cash_price_type'] ?? '1';
- $cash_price_amount = $integral_appreciation_setting['cash_price_amount'] ?? 100;
- $cash_price_integral = $integral_appreciation_setting['cash_price_integral'] ?? 0;
- $cash_price_balance = $integral_appreciation_setting['cash_price_balance'] ?? 0;
- $cash_service_charge = $integral_appreciation_setting['cash_service_charge'] ?? 0;
- }
- $cash = Option::get(OptionSetting::CASH_SETTING, get_store_id(), 'store')['value'];
- if ($cash) {
- $res = json_decode($cash, true);
- $cash_price_type = $res['cash_price_type'] ?? '1';
- $cash_price_amount = $res['cash_price_amount'] ?? 100;
- $cash_price_integral = $res['cash_price_integral'] ?? 0;
- $cash_price_balance = $res['cash_price_balance'] ?? 0;
- $cash_service_charge = $res['cash_service_charge'] ?? 0;
- }
- if($cashType == Cash::TYPE_RECHARGE){
- $cash_service_charge = 0;
- }
- $amount_price = 0;
- $integral_price = 0;
- $balance_price = 0;
- $real_amount_price = 0;
- $real_integral_price = 0;
- $real_balance_price = 0;
- $real_cash_price = bcsub($cash_price, bcdiv(bcmul($cash_price, $cash_service_charge, 2), 100, 2), 2);//实际可提现金额
- $total_profit = 0;
- $cash_price_type = explode(',', $cash_price_type);
- if (in_array(self::CASH_PRICE_TYPE_AMOUNT, $cash_price_type)) {
- $total_profit = bcadd($total_profit, $cash_price_amount, 2);
- $amount_price = bcdiv(bcmul($cash_price, $cash_price_amount, 2), 100, 2);
- $real_amount_price = bcdiv(bcmul($real_cash_price, $cash_price_amount, 2), 100, 2);//99.4 * 1
- }
- if (in_array(self::CASH_PRICE_TYPE_INTEGRAL, $cash_price_type)) {
- $total_profit = bcadd($total_profit, $cash_price_integral, 2);
- $integral_price = bcdiv(bcmul($cash_price, $cash_price_integral, 2), 100, 2);
- //兼容计算精度问题
- if ($cash_price_balance <= 0 || !in_array(self::CASH_PRICE_TYPE_BALANCE, $cash_price_type)) {
- $real_integral_price = bcsub($real_cash_price, $real_amount_price, 2);
- } else {
- $real_integral_price = bcdiv(bcmul($real_cash_price, $cash_price_integral, 2), 100, 2);
- }
- }
- if (in_array(self::CASH_PRICE_TYPE_BALANCE, $cash_price_type)) {
- $total_profit = bcadd($total_profit, $cash_price_balance, 2);
- $balance_price = bcdiv(bcmul($cash_price, $cash_price_balance, 2), 100, 2);
- // $real_balance_price = bcmul($cash_price_service, bcdiv($cash_price_balance, 100, 2), 2);
- //兼容计算精度问题
- $real_balance_price = bcsub($real_cash_price, bcadd($real_amount_price, $real_integral_price, 2), 2);
- }
- if ($total_profit != 100) {
- throw new \Exception('计算失败,佣金配置错误');
- }
- return [
- 'code' => 0,
- 'msg' => '计算成功',
- 'data' => [
- 'amount_price' => $amount_price,
- 'integral_price' => $integral_price,
- 'balance_price' => $balance_price,
- 'cash_service_charge' => $cash_service_charge,
- 'real_amount_price' => $real_amount_price,
- 'real_integral_price' => $real_integral_price,
- 'real_balance_price' => $real_balance_price,
- ]
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage(),
- ];
- }
- }
- public static function cashExtSave($cash_id, $cash_price_type, $price, $service_charge, $real_price) {
- try {
- $model = new self();
- $model->cash_id = $cash_id;
- $model->cash_price_type = $cash_price_type;
- $model->price = $price;
- $model->service_charge = $service_charge;
- $model->real_price = $real_price;
- if (!$model->save()) {
- throw new \Exception(implode(';', array_values($model->firstErrors)));
- }
- return [
- 'code' => 0,
- 'msg' => '添加成功'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage(),
- ];
- }
- }
- //计算增值积分提现金额
- public static function integral_appreciation_cash($cash_price, $store_id) {
- $integral_pool = IntegralAppreciationPool::findOne(['store_id' => $store_id]);
- $integral_appreciation_setting = Option::get('integral_appreciation_setting', $store_id, 'integral_appreciation')['value'];
- $integral_appreciation_setting = json_decode($integral_appreciation_setting ?: '', true);
- //提现回流奖金池比例
- $integral_payout_pool_profit = $integral_appreciation_setting['integral_payout_pool_profit'];
- //增值积分初始金额
- $integral_price = $integral_appreciation_setting['integral_init_price'] ?? 0;
- //将增值积分转为实际金额进行计算
- if ($integral_pool) {
- $integral_price = $integral_pool->integral_price;
- }
- //计算积分回流后的实际提现到账金额
- $cash_price = bcmul($cash_price, $integral_price, 2);
- $payout_pool_price = bcdiv(bcmul($cash_price, $integral_payout_pool_profit, 2), 100, 2);
- $cash_price = bcsub($cash_price, $payout_pool_price, 2);
- if ($cash_price <= 0) {
- $cash_price = 0;
- }
- return [
- 'cash_price' => $cash_price,//实际提现金额
- 'integral_payout_pool_profit' => $integral_payout_pool_profit,//回流比例
- 'integral_price' => $integral_price,//增值积分价格
- 'payout_pool_price' => $payout_pool_price//回流金额
- ];
- }
- }
|