CashForm.php 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\client\models\v1;
  8. use app\models\Cash;
  9. use app\models\CashExt;
  10. use app\models\IntegralAppreciationCashLog;
  11. use app\models\IntegralAppreciationUser;
  12. use app\models\IntegralAppreciationUserIntegralLog;
  13. use app\models\Lg;
  14. use app\models\Option;
  15. use app\constants\OptionSetting;
  16. use app\jobs\LgCashJob;
  17. use app\models\QueueAccountLog;
  18. use app\models\SaasUser;
  19. use app\models\ShareGroupPurchaseUser;
  20. use app\models\Shop;
  21. use app\models\ShopSetting;
  22. use app\models\ShopShare;
  23. use app\models\SuperSalesUser;
  24. use app\models\TeamGrades;
  25. use app\models\User;
  26. use app\utils\OrderNo;
  27. use yii\base\Model;
  28. use yii\helpers\Json;
  29. use app\models\AccountLog;
  30. use app\models\Store;
  31. use app\models\StoreMoneyLog;
  32. use app\models\WechatConfig;
  33. use app\modules\admin\controllers\ShareController;
  34. use app\utils\Alipay\Alipay;
  35. use app\utils\LgApi;
  36. use app\utils\Notice\NoticeSend;
  37. use yii\helpers\VarDumper;
  38. class CashForm extends Model
  39. {
  40. public $user_id;
  41. public $store_id;
  42. public $cash;
  43. public $name;
  44. public $mobile;
  45. public $pay_type;
  46. public $form_id;
  47. public $type;
  48. public $bank_name;
  49. public $is_queue;
  50. public $cash_type = 0;
  51. public function rules()
  52. {
  53. return [
  54. [['cash'], 'required'],
  55. [['name', 'mobile', 'pay_type'], 'required', 'on' => 'CASH'],
  56. [['cash'], 'number', 'min' => 0,],
  57. [['cash_type','is_queue'], 'integer'],
  58. [['name', 'mobile', 'form_id', 'type'], 'trim'],
  59. [['pay_type'], 'in', 'range' => [0, 1, 2, 3]],
  60. [['bank_name'], 'string'],
  61. ];
  62. }
  63. public function attributeLabels()
  64. {
  65. return [
  66. 'cash' => '提现金额',
  67. 'name' => '姓名',
  68. 'pay_type' => '提现方式',
  69. 'mobile' => '账号',
  70. 'bank_name' => '开户行',
  71. ];
  72. }
  73. public function save()
  74. {
  75. if ($this->validate()) {
  76. $cash_service_charge = 0;
  77. $integral_appreciation_price = 0;
  78. $integral_appreciation_result = [];
  79. $cash_price = $this->cash;
  80. //兼容团队业绩分红
  81. if (intval($this->cash_type) !== Cash::IS_CASH_TYPE_TEAM_GRADES) {
  82. $setting = Option::get('share_basic_setting', $this->store_id);
  83. $setting = $setting ? Json::decode($setting['value']) : [];
  84. $cash_max_day = floatval($setting['cash_max_day']['value']);
  85. $cash_max_single_day = floatval($setting['cash_max_single_day']['value']);
  86. $cash_service_charge = floatval($setting['cash_service_charge']['value']);
  87. if (in_array(intval($this->cash_type), [Cash::IS_CASH_TYPE_SHARE_GROUP_PURCHASE, Cash::IS_CASH_TYPE_SUPER_SALES, Cash::IS_CASH_TYPE_INTEGRAL_APPRECIATION])) {
  88. if (intval($this->cash_type) === Cash::IS_CASH_TYPE_SHARE_GROUP_PURCHASE) {
  89. $setting = Option::get('share_group_setting', $this->store_id);
  90. } elseif (intval($this->cash_type) === Cash::IS_CASH_TYPE_INTEGRAL_APPRECIATION) {
  91. $setting = Option::get('integral_appreciation_setting', $this->store_id);
  92. $integral_appreciation_price = $this->cash;
  93. $integral_appreciation_result = CashExt::integral_appreciation_cash($this->cash, $this->store_id);
  94. $this->cash = $integral_appreciation_result['cash_price'];
  95. } else {
  96. $setting = Option::get('super_sales_setting', $this->store_id);
  97. }
  98. $setting = $setting ? Json::decode($setting['value']) : [];
  99. $cash_max_day = floatval($setting['cash_max_day']);
  100. $cash_max_single_day = floatval($setting['cash_max_single_day']);
  101. $cash_service_charge = floatval($setting['cash_service_charge']);
  102. }
  103. $user_id = $this->user_id;
  104. if ($cash_max_day) {
  105. $query = Cash::find()->where([
  106. 'store_id' => $this->store_id,
  107. 'is_delete' => 0,
  108. 'status' => [0, 1, 2, 5],
  109. ]);
  110. if (intval($this->cash_type) === Cash::IS_CASH_TYPE_SHARE_GROUP_PURCHASE) {
  111. $query->andWhere(['cash_type' => Cash::IS_CASH_TYPE_SHARE_GROUP_PURCHASE]);
  112. } elseif (intval($this->cash_type) === Cash::IS_CASH_TYPE_SUPER_SALES) {
  113. $query->andWhere(['cash_type' => Cash::IS_CASH_TYPE_SUPER_SALES]);
  114. } else {
  115. $query->andWhere(['cash_type' => Cash::IS_CASH_TYPE_SHARE]);
  116. }
  117. $cash_sum = $query->andWhere([
  118. 'AND',
  119. ['>=', 'created_at', strtotime(date('Y-m-d 00:00:00'))],
  120. ['<=', 'created_at', strtotime(date('Y-m-d 23:59:59'))],
  121. ])->sum('price');
  122. $cash_max_day = $cash_max_day - ($cash_sum ? $cash_sum : 0);
  123. if ($this->cash > $cash_max_day) {
  124. return [
  125. 'code' => 1,
  126. 'msg' => '提现金额不能超过' . $cash_max_day . '元'
  127. ];
  128. }
  129. }
  130. if ($cash_max_single_day) {
  131. $cash_sum = Cash::find()->where([
  132. 'store_id' => $this->store_id,
  133. 'is_delete' => 0,
  134. 'status' => [0, 1, 2, 5],
  135. 'user_id' => $user_id,
  136. 'cash_type' => $this->cash_type
  137. ])->andWhere([
  138. 'AND',
  139. ['>=', 'created_at', strtotime(date('Y-m-d 00:00:00'))],
  140. ['<=', 'created_at', strtotime(date('Y-m-d 23:59:59'))],
  141. ])->sum('price');
  142. $cash_max_single_day_ = $cash_max_single_day - ($cash_sum ?: 0);
  143. if ($this->cash > $cash_max_single_day_) {
  144. return [
  145. 'code' => 1,
  146. 'msg' => '每人每天提现金额不能超过'. $cash_max_single_day .'元'
  147. ];
  148. }
  149. }
  150. }
  151. $user = User::findOne(['id' => $this->user_id, 'store_id' => $this->store_id]);
  152. if (!$user) {
  153. return [
  154. 'code' => 1,
  155. 'msg' => '网络异常'
  156. ];
  157. }
  158. if (intval($this->cash_type) !== Cash::IS_CASH_TYPE_TEAM_GRADES) {
  159. // 判断最小提现金
  160. if ($this->cash_type == 1) {
  161. $share_setting = ShopSetting::find()->where(['store_id' => $this->store_id])->one()->toArray();
  162. } else {
  163. if (in_array(intval($this->cash_type), [Cash::IS_CASH_TYPE_SHARE_GROUP_PURCHASE, Cash::IS_CASH_TYPE_SUPER_SALES, Cash::IS_CASH_TYPE_INTEGRAL_APPRECIATION])) {
  164. $share_setting = [
  165. 'min_money' => $setting['min_money'],
  166. 'auto_money' => 0,
  167. ];
  168. } else {
  169. $share_setting = [
  170. 'min_money' => $setting['min_money']['value'],
  171. 'auto_money' => $setting['auto_money']['value'],
  172. ];
  173. }
  174. }
  175. if ($this->cash < $share_setting['min_money']) {
  176. return [
  177. 'code' => 1,
  178. 'msg' => '提现金额不能小于' . $share_setting['min_money'] . '元'
  179. ];
  180. }
  181. }
  182. // 判断提现金额
  183. if (in_array($this->cash_type, [0, Cash::IS_CASH_TYPE_TEAM_GRADES, Cash::IS_CASH_TYPE_SHARE_GROUP_PURCHASE, Cash::IS_CASH_TYPE_SUPER_SALES]) && $user->price < $this->cash) {
  184. return [
  185. 'code' => 1,
  186. 'msg' => '提现金额不能超过剩余金额'
  187. ];
  188. } elseif(($this->cash_type == Cash::IS_CASH_TYPE_INTEGRAL_APPRECIATION)){
  189. $integralAppreciationUser = IntegralAppreciationUser::findOne(['user_id' => $user->id]);
  190. if ($integralAppreciationUser->integral < $cash_price) {
  191. return [
  192. 'code' => 1,
  193. 'msg' => '提现金额不能超过剩余金额'
  194. ];
  195. }
  196. $user_total_integral = IntegralAppreciationUser::find()->where(['store_id' => $this->store_id])->sum('integral') ?: 0;
  197. if (bcsub($user_total_integral, $cash_price, 2) <= 0.1) {
  198. return [
  199. 'code' => 1,
  200. 'msg' => '提现金额受限'
  201. ];
  202. }
  203. } elseif(($this->cash_type == 1) && $user->tuan_price < $this->cash){
  204. return [
  205. 'code' => 1,
  206. 'msg' => '提现金额不能超过剩余金额'
  207. ];
  208. }
  209. if (intval($this->cash_type) === Cash::IS_CASH_TYPE_TEAM_GRADES) {
  210. $userTeamGrades = TeamGrades::findOne(['user_id' => get_user_id(), 'is_delete' => 0]);
  211. if (!$userTeamGrades) {
  212. return [
  213. 'code' => 1,
  214. 'msg' => '非团队业绩成员'
  215. ];
  216. }
  217. if ($userTeamGrades->price < $this->cash) {
  218. return [
  219. 'code' => 1,
  220. 'msg' => '提现金额不足'
  221. ];
  222. }
  223. }
  224. if (in_array(intval($this->cash_type), [Cash::IS_CASH_TYPE_SHARE_GROUP_PURCHASE, Cash::IS_CASH_TYPE_SUPER_SALES])) {
  225. if (intval($this->cash_type) === Cash::IS_CASH_TYPE_SHARE_GROUP_PURCHASE) {
  226. $activityUser = ShareGroupPurchaseUser::findOne(['user_id' => get_user_id()]);
  227. if (empty($activityUser)) {
  228. return [
  229. 'code' => 1,
  230. 'msg' => '未参与37拼购活动'
  231. ];
  232. }
  233. } else {
  234. $activityUser = SuperSalesUser::findOne(['user_id' => get_user_id()]);
  235. }
  236. if ($activityUser->price < $this->cash) {
  237. return [
  238. 'code' => 1,
  239. 'msg' => '提现金额不足'
  240. ];
  241. }
  242. }
  243. if (intval($this->cash_type) === Cash::IS_CASH_TYPE_INTEGRAL_APPRECIATION) {
  244. $integral_appreciation_user = IntegralAppreciationUser::findOne(['user_id' => get_user_id()]);
  245. if (empty($integral_appreciation_user)) {
  246. return [
  247. 'code' => 1,
  248. 'msg' => '提现金额不足'
  249. ];
  250. }
  251. if ($integral_appreciation_user->integral < $integral_appreciation_price) {
  252. return [
  253. 'code' => 1,
  254. 'msg' => '提现金额不足'
  255. ];
  256. }
  257. }
  258. // 判断是否存在未完成提现
  259. $exit = Cash::find()->andWhere(['=', 'status', 0])->andWhere(['user_id' => $this->user_id,
  260. 'store_id' => $this->store_id, 'cash_type' => $this->cash_type ?: 0])->exists();
  261. if ($exit) {
  262. return [
  263. 'code' => 1,
  264. 'msg' => '尚有未完成的提现申请'
  265. ];
  266. }
  267. if ($this->cash_type == 1) {
  268. $shop = Shop::findOne(['user_id'=> $this->user_id, 'store_id' => $this->store_id, 'is_delete' => 0, 'shop_audit' => 1]);
  269. $shop_form = new ShopShare();
  270. $shop_form->store_id = $this->store_id;
  271. $shop_form->shop_id = $shop->id;
  272. $shop_form->type = 0;
  273. $shop_form->amount = -$this->cash;
  274. $shop_form->the_desc = '自提点'.$user['nickname'].'提现';
  275. $shop_form->created_at = time();
  276. $shop_form->status = 1;
  277. $shop_form->save();
  278. }
  279. $t = \Yii::$app->db->beginTransaction();
  280. // $cash_service_charge = intval($this->cash_type) === Cash::IS_CASH_TYPE_TEAM_GRADES ? 0 :floatval($setting['cash_service_charge']['value']);
  281. $cash = new Cash();
  282. $cash->order_no = OrderNo::getOrderNo(OrderNo::ORDER_CASH);
  283. $cash->is_delete = 0;
  284. $cash->status = 0;
  285. $cash->price = $this->cash;
  286. $cash->created_at = time();
  287. $cash->user_id = $this->user_id;
  288. $cash->store_id = $this->store_id;
  289. if (in_array($this->cash_type, [0, Cash::IS_CASH_TYPE_TEAM_GRADES, Cash::IS_CASH_TYPE_SHARE_GROUP_PURCHASE, Cash::IS_CASH_TYPE_SUPER_SALES, Cash::IS_CASH_TYPE_INTEGRAL_APPRECIATION])) {
  290. $saas_user = get_saas_user();
  291. $withdraw_method = Json::decode($saas_user->withdraw_method);
  292. $withdraw_method = array_column((array)$withdraw_method, null, 'type');
  293. if ($this->type == 'money') {
  294. $cash->type = 3;
  295. } else {
  296. if ($this->type == 'wechat') {
  297. $cash->type = 0;
  298. $cash->name = $withdraw_method['wechat']['name'];
  299. $cash->mobile = $withdraw_method['wechat']['account'];
  300. }
  301. if ($this->type == 'alipay') {
  302. $cash->type = 1;
  303. $cash->name = $withdraw_method['alipay']['name'];
  304. $cash->mobile = $withdraw_method['alipay']['account'];
  305. }
  306. if ($this->type == 'bank_card') {
  307. $cash->type = 2;
  308. $cash->name = $withdraw_method['bank_card']['name'];
  309. $cash->mobile = $withdraw_method['bank_card']['account'];
  310. $cash->bank_name = $withdraw_method['bank_card']['bank'];
  311. $cash->bank_branch = $withdraw_method['bank_card']['branch'];
  312. }
  313. if ($this->type == 'lg') {
  314. //灵工提现
  315. $lg_info = Lg::find()->where(['user_id'=>$saas_user->id,'is_delete'=>0,'status'=>1])->one();
  316. $cash->type = 4;
  317. $cash->name = $lg_info->name;
  318. $cash->mobile = $lg_info->mobile;
  319. }
  320. }
  321. } else {
  322. $cash->type = $this->pay_type;
  323. $cash->name = $this->name;
  324. $cash->mobile = $this->mobile;
  325. $cash->bank_name = $this->bank_name;
  326. }
  327. $cash->pay_time = 0;
  328. // 如果是 提现方式是余额的时候 不收取手续费
  329. $cash->service_charge = $cash->type == Cash::TYPE_RECHARGE ? 0 : $cash_service_charge;
  330. $cash->cash_type = $this->cash_type;
  331. if ($cash->save()) {
  332. //如果是团长提现,扣除会员里的团长佣金字段 add by 甜心100 mrlu 20190110
  333. if ($this->cash_type == 1) {
  334. $user->tuan_price -= $this->cash;
  335. } elseif ($this->cash_type == 0) {
  336. $user->price -= $this->cash;
  337. }
  338. if ($this->cash_type == Cash::IS_CASH_TYPE_TEAM_GRADES) {
  339. $userTeamGrades->price -= $this->cash;
  340. if (!$userTeamGrades->save()) {
  341. $t->rollBack();
  342. return [
  343. 'code' => 1,
  344. 'msg' => implode(';', array_values($userTeamGrades->firstErrors))
  345. ];
  346. };
  347. $user->price -= $this->cash;
  348. }
  349. if (intval($this->cash_type) === Cash::IS_CASH_TYPE_INTEGRAL_APPRECIATION) {
  350. $integral_appreciation_user->integral -= $integral_appreciation_price;
  351. if (!$integral_appreciation_user->save()) {
  352. $t->rollBack();
  353. return [
  354. 'code' => 1,
  355. 'msg' => implode(';', array_values($integral_appreciation_user->firstErrors))
  356. ];
  357. };
  358. $integral_appreciation_cash = new IntegralAppreciationCashLog();
  359. $integral_appreciation_cash->user_id = $this->user_id;
  360. $integral_appreciation_cash->cash_id = $cash->id;
  361. $integral_appreciation_cash->integral = $integral_appreciation_price;
  362. $integral_appreciation_cash->integral_price = $integral_appreciation_result['integral_price'];;
  363. $integral_appreciation_cash->reflux_profit = $integral_appreciation_result['integral_payout_pool_profit'];
  364. $integral_appreciation_cash->reflux_price = $integral_appreciation_result['payout_pool_price'];
  365. $integral_appreciation_cash->cash_price = $this->cash;
  366. if (!$integral_appreciation_cash->save()) {
  367. $t->rollBack();
  368. return [
  369. 'code' => 1,
  370. 'msg' => implode(';', array_values($integral_appreciation_user->firstErrors))
  371. ];
  372. };
  373. $result = IntegralAppreciationUserIntegralLog::saveIntegralLog($integral_appreciation_user->id, $integral_appreciation_price, IntegralAppreciationUserIntegralLog::TYPE_EXPEND, IntegralAppreciationUserIntegralLog::SOURCE_TYPE_WITHDRAW);
  374. if ($result['code']) {
  375. $t->rollBack();
  376. return $result;
  377. }
  378. // $user->price -= $this->cash;
  379. }
  380. if (in_array(intval($this->cash_type), [Cash::IS_CASH_TYPE_SHARE_GROUP_PURCHASE, Cash::IS_CASH_TYPE_SUPER_SALES])) {
  381. $activityUser->price -= $this->cash;
  382. if (!$activityUser->save()) {
  383. $t->rollBack();
  384. return [
  385. 'code' => 1,
  386. 'msg' => implode(';', array_values($activityUser->firstErrors))
  387. ];
  388. };
  389. $user->price -= $this->cash;
  390. }
  391. if (!$user->save()) {
  392. $t->rollBack();
  393. return [
  394. 'code' => 1,
  395. 'msg' => '网络异常'
  396. ];
  397. }
  398. if ($cash->type == Cash::TYPE_RECHARGE || ($this->cash_type == 0 && $this->cash <= $share_setting['auto_money'] && $share_setting['auto_money'] > 0)) {
  399. // TODO 在设置金额内,分销/充值佣金提现自动打款
  400. debug_log([$cash->id, $cash->type == Cash::TYPE_RECHARGE ? 4 : 2, $this->store_id],'cash.log');
  401. $aa = $this->confirm($cash->id, $cash->type == Cash::TYPE_RECHARGE ? 4 : 2, $this->store_id);
  402. debug_log($aa,'cash.log');
  403. }
  404. $t->commit();
  405. return [
  406. 'code' => 0,
  407. 'msg' => '申请成功'
  408. ];
  409. } else {
  410. $t->rollBack();
  411. return [
  412. 'code' => 1,
  413. 'msg' => '网络异常'
  414. ];
  415. }
  416. } else {
  417. return [
  418. 'code' => 1,
  419. 'msg' => $this->getErrorSummary(false)[0],
  420. ];
  421. }
  422. }
  423. public function confirm($id, $status, $store_id)
  424. {
  425. $store = Store::findOne($store_id);
  426. $cash = Cash::findOne([
  427. 'id' => $id,
  428. 'is_delete' => Cash::IS_DELETE_NO,
  429. 'store_id' => $store_id
  430. ]);
  431. if($status == Cash::STATUS_GIVEN){
  432. $store_wechat_cash = json_decode(Option::get('store_wechat_cash', $store_id, 'store')['value'], true);
  433. $val = 0;
  434. foreach ((array)$store_wechat_cash as $value) {
  435. if (empty($value)) {
  436. $val = 1;
  437. break;
  438. } else {
  439. $val = 2;
  440. break;
  441. }
  442. }
  443. if ($store->is_platform_transfers == 0 && $val == 2) {
  444. return [
  445. 'code' => 1,
  446. 'msg' => '未开启平台转账'
  447. ];
  448. } elseif ($store->is_platform_transfers == 1 && $val == 1) {
  449. return [
  450. 'code' => 1,
  451. 'msg' => '未配置平台微信提现设置'
  452. ];
  453. }
  454. }
  455. if (!$cash->order_no) {
  456. $order_no = null;
  457. while (true) {
  458. $order_no = date('YmdHis') . mt_rand(100000, 999999);
  459. $exist_order_no = Cash::find()->where(['order_no' => $order_no])->exists();
  460. if (!$exist_order_no) {
  461. break;
  462. }
  463. }
  464. $cash->order_no = $order_no;
  465. $cash->save();
  466. }
  467. $res = [];
  468. $cashExt = CashExt::findOne(['cash_id' => $cash['id'], 'cash_price_type' => CashExt::CASH_PRICE_TYPE_AMOUNT]);
  469. if ($cashExt) {
  470. $price = $cashExt->real_price;
  471. } else {
  472. $price = Cash::getServiceMoney($cash);
  473. }
  474. $servePrice = $cash->price * ($cash->service_charge / 100);
  475. if ($store->store_balance < $price && $store->is_platform_transfers == 1) {
  476. return [
  477. 'code' => 1,
  478. 'msg' => '店铺剩余提现余额不足'
  479. ];
  480. }
  481. $wechat_type = 1;
  482. $wechat_cash = Option::get('wechat_cash', $store_id, 'store')['value'];
  483. // if (empty($wechat_cash)) {
  484. // $wechat_cash = Option::get('store_wechat_cash', $store_id, 'store')['value'];
  485. // if ($wechat_cash) {
  486. // $wechat_type = 0;
  487. // }
  488. // }
  489. foreach ((array)json_decode($wechat_cash, true) as $value) {
  490. if (empty($value)) {
  491. $wechat_cash = Option::get('store_wechat_cash', $store_id, 'store')['value'];
  492. if ($wechat_cash) {
  493. $wechat_type = 0;
  494. }
  495. break;
  496. }
  497. }
  498. $t = \Yii::$app->db->beginTransaction();
  499. if ($status == Cash::STATUS_GIVEN) { //微信自动打款
  500. $cash->status = Cash::STATUS_GIVEN;
  501. $cash->pay_time = time();
  502. $cash->pay_type = Cash::PAY_TYPE_WX;
  503. $user = User::findOne(['id' => $cash->user_id]);
  504. $data = [
  505. 'partner_trade_no' => $cash->order_no,
  506. 'openid' => $user->wechat_open_id,
  507. 'check_name' => 'NO_CHECK',
  508. 'amount' => $price * 100,
  509. 'desc' => '转账'
  510. ];
  511. if ((int)$cash->type === 0) {
  512. if (\Yii::$app->prod_is_dandianpu() && !\app\models\Store::hasIncoming($store_id)) {
  513. //商城是否进件
  514. //商城余额是否充足
  515. $storeMoney = \app\models\StoreCash::getMaxCash($store);
  516. if ($storeMoney < $price) {
  517. $t->rollBack();
  518. return [
  519. 'code' => 1,
  520. 'msg' => '操作错误,商城余额不足! 当前余额:¥' . $storeMoney
  521. ];
  522. }
  523. //扣除商城余额
  524. $subMoney = \app\models\Store::subMoney($store, $price);
  525. if (!$subMoney) {
  526. $t->rollBack();
  527. return [
  528. 'code' => 1,
  529. 'msg' => '网络异常,subMoney失败1',
  530. ];
  531. }
  532. } else {
  533. if (\Yii::$app->prod_is_duli()) {
  534. $WechatConfig = WechatConfig::findOne(['store_id' => get_store_id()]);
  535. if (empty($WechatConfig->mch_id) || empty($WechatConfig->pay_key) || empty($WechatConfig->app_id) || empty($WechatConfig->cert_pem) || empty($WechatConfig->key_pem)) {
  536. return [
  537. 'code' => 1,
  538. 'msg' => "后台参数配置错误,请检查参数后重试"
  539. ];
  540. }
  541. }
  542. }
  543. if ($wechat_type == 0) {
  544. $before = $store->store_balance;
  545. $store->store_balance -= $price;
  546. $store->price -= $price;
  547. $store->store_withdrawn_cash += $price;
  548. $after = $store->store_balance;
  549. if ($store->save()) {
  550. StoreMoneyLog::saveLog(get_store_id(), StoreMoneyLog::LOG_TYPE_EXPEND, StoreMoneyLog::TYPE_SHARE, $price, " ID{$id}:用户提现{$cash->price}元,扣除手续费{$servePrice}元,提现实际扣除{$price}元", $before, $after, $wechat_type);
  551. }
  552. }
  553. $wechat = \Yii::$app->controller->wechatPay;
  554. // $res = $wechat->transfer->toBalance($data);
  555. $res = (new \app\utils\WechatMerchant\WxV3($wechat))->transferBatches(get_store_id(), $data);
  556. //判断是否使用新版本转账 增加转账标识
  557. $wechat_cash = json_decode($wechat_cash, true);
  558. if (intval($wechat_cash['is_open']) === 2) {
  559. if (!$res['code']) {
  560. $cash->is_platform_transfers = intval($store->is_platform_transfers);
  561. $cash->wx_cash_type = Cash::WX_CASH_TYPE_NEW;
  562. $cash->wx_cash_state = $res['data']['state'];
  563. $cash->wx_cash_result_info = json_encode($res['data'], JSON_UNESCAPED_UNICODE);
  564. $cash->save();
  565. }
  566. }
  567. NoticeSend::CashSuccess($cash->user_id, $user->binding, $price, '微信自动打款', ($cash->price - $price));
  568. } elseif ((int)$cash->type === 1) {
  569. $order = (object)[
  570. 'store_id' => $cash->store_id,
  571. 'order_no' => $cash->order_no,
  572. 'pay_price' => sprintf("%.2f", $price),
  573. 'name' => $cash->name
  574. ];
  575. $result = Alipay::transfer($order, $cash->mobile);
  576. if (isset($result['code']) && $result['code'] == 1) {
  577. if (strpos($result['msg'], 'aop.invalid-app-auth-token-no-api')) {
  578. $result['msg'] = '接口未授权,请前往支付宝开放平台查询是否开通产品或授权支付宝转账产品';
  579. }
  580. if (strpos($result['msg'], 'PAYEE_NOT_EXIST')) {
  581. $result['msg'] = '收款账号不存在或姓名有误,建议核实账号和姓名是否准确';
  582. }
  583. if (strpos($result['msg'], 'BALANCE_IS_NOT_ENOUGH')) {
  584. $result['msg'] = '商户余额不足';
  585. }
  586. return $result;
  587. } else {
  588. // TODO 扣除店铺提现金额
  589. if ($wechat_type == 0) {
  590. $before = $store->store_balance;
  591. $store->store_balance -= $price;
  592. $store->price -= $price;
  593. $store->store_withdrawn_cash += $price;
  594. $after = $store->store_balance;
  595. if ($store->save()) {
  596. StoreMoneyLog::saveLog(get_store_id(), StoreMoneyLog::LOG_TYPE_EXPEND, StoreMoneyLog::TYPE_SHARE, $price, " ID{$id}:用户提现{$cash->price}元,扣除手续费{$servePrice}元,提现实际扣除{$price}元", $before, $after, $wechat_type);
  597. }
  598. }
  599. }
  600. $cash->save();
  601. $t->commit();
  602. return [
  603. 'code' => 0,
  604. 'msg' => '成功'
  605. ];
  606. } elseif ((int)$cash->type === 4) {
  607. $user = \app\models\User::findOne($cash->user_id);
  608. $saas_user = \app\models\SaasUser::findOne(['mobile' => $user->binding, 'is_delete' => 0]);
  609. $lg_info = Lg::find()->where(['user_id' => $saas_user->id, 'status' => 1, 'is_delete' => 0])->one();
  610. $lgApi = new LgApi($cash->store_id);
  611. //灵工提现
  612. $post_data = [
  613. 'store_id' => $cash->store_id,
  614. 'outTradeNo' => $cash->order_no, //唯一批次号
  615. 'accNo' => bcmul($price, 100),
  616. 'amt' => bcmul($price, 100),
  617. 'name' => $lg_info->name,
  618. 'certCard' => $lg_info->cert_card //身份证号
  619. ];
  620. $result = $lgApi->FlexiblePay($post_data);
  621. if (isset($result['status']) && $result['status'] == 999) {
  622. return $result;
  623. }
  624. $cash->status = 6; //灵工待打款
  625. $cash->save();
  626. $t->commit();
  627. //灵工提现接口调用后消息队列查询状态
  628. \queue_push(new LgCashJob(['id' => $cash->id, 'store_id' => $store_id, 'type' => 0, 'retry' => 5]), 60);
  629. return [
  630. 'code' => 0,
  631. 'msg' => '成功'
  632. ];
  633. }
  634. } elseif ($status == Cash::STATUS_HAND) { //手动打款
  635. // TODO 扣除店铺提现金额
  636. if ($wechat_type == 0) {
  637. $before = $store->store_balance;
  638. $store->store_balance -= $price;
  639. $store->price -= $price;
  640. $store->store_withdrawn_cash += $price;
  641. $after = $store->store_balance;
  642. if ($store->save()) {
  643. StoreMoneyLog::saveLog(get_store_id(), StoreMoneyLog::LOG_TYPE_EXPEND, StoreMoneyLog::TYPE_SHARE, $price, " ID{$id}:用户提现{$cash->price}元,扣除手续费{$servePrice}元,提现实际扣除{$price}元", $before, $after, $wechat_type);
  644. } else {
  645. $t->rollBack();
  646. }
  647. }
  648. $cash->status = Cash::STATUS_HAND;
  649. $cash->pay_time = time();
  650. $cash->pay_type = Cash::PAY_TYPE_HAND;
  651. if ($cash->type == Cash::TYPE_RECHARGE) {
  652. $user = User::findOne(['id' => $cash->user_id]);
  653. $cashExt = CashExt::findOne(['cash_id' => $cash->id, 'cash_price_type' => CashExt::CASH_PRICE_TYPE_AMOUNT]);
  654. if ($cashExt && $cashExt->real_price > 0) {
  655. AccountLog::saveLog($cash->user_id, $cashExt->real_price, 2, 1, 0, 0, '佣金提现打款');
  656. }
  657. }
  658. $res['result_code'] = "SUCCESS";
  659. NoticeSend::CashSuccess($cash->user_id, $user->binding, $price, '手动打款', ($cash->price - $price));
  660. }
  661. if (isset($res['result_code']) && $res['result_code'] == 'SUCCESS') {
  662. $cash->save();
  663. $t->commit();
  664. return [
  665. 'code' => 0,
  666. 'msg' => '成功'
  667. ];
  668. } else {
  669. debug_log('shibai','auto_money.log');
  670. $t->rollBack();
  671. return [
  672. 'code' => 1,
  673. 'msg' => !empty($res['err_code_des']) ? $res['err_code_des'] : '请稍后重试',
  674. 'data' => $res
  675. ];
  676. }
  677. }
  678. public function cashWorker()
  679. {
  680. $this->cash_type = Cash::IS_CASH_TYPE_WORKER;
  681. if ($this->validate()) {
  682. $user = User::findOne(['id' => $this->user_id, 'store_id' => $this->store_id]);
  683. if (!$user) {
  684. return [
  685. 'code' => 1,
  686. 'msg' => '网络异常'
  687. ];
  688. }
  689. $key = 'cash_user_' . $user->id;
  690. if (cache()->exists($key)) {
  691. return [
  692. 'code' => 1,
  693. 'msg' => '请勿重复提交'
  694. ];
  695. }
  696. cache()->set($key, 1, 60);
  697. // 判断最小提现金
  698. if ($this->cash < 0.01) {
  699. return [
  700. 'code' => 1,
  701. 'msg' => '提现金额不能小于' . 0.01 . '元'
  702. ];
  703. }
  704. // 判断提现金额
  705. if ($user->price < $this->cash) {
  706. return [
  707. 'code' => 1,
  708. 'msg' => '提现金额不能超过剩余金额'
  709. ];
  710. }
  711. // 判断是否存在未完成提现
  712. $exit = Cash::find()->andWhere(['=', 'status', 0])->andWhere(['user_id' => $this->user_id,
  713. 'store_id' => $this->store_id, 'cash_type' => $this->cash_type])->exists();
  714. if ($exit) {
  715. // return [
  716. // 'code' => 1,
  717. // 'msg' => '尚有未完成的提现申请'
  718. // ];
  719. }
  720. $t = \Yii::$app->db->beginTransaction();
  721. $cash = new Cash();
  722. $cash->order_no = OrderNo::getOrderNo(OrderNo::ORDER_CASH);
  723. $cash->is_delete = 0;
  724. $cash->status = 0;
  725. $cash->price = $this->cash;
  726. $cash->created_at = time();
  727. $cash->user_id = $this->user_id;
  728. $cash->store_id = $this->store_id;
  729. $saas_user = get_saas_user();
  730. $withdraw_method = Json::decode($saas_user->withdraw_method);
  731. $withdraw_method = array_column((array)$withdraw_method, null, 'type');
  732. if ($this->type == 'wechat') {
  733. $cash->type = 0;
  734. $cash->name = $withdraw_method['wechat']['name'];
  735. $cash->mobile = $withdraw_method['wechat']['account'];
  736. }
  737. if ($this->type == 'alipay') {
  738. $cash->type = 1;
  739. $cash->name = $withdraw_method['alipay']['name'];
  740. $cash->mobile = $withdraw_method['alipay']['account'];
  741. }
  742. if ($this->type == 'bank_card') {
  743. $cash->type = 2;
  744. $cash->name = $withdraw_method['bank_card']['name'];
  745. $cash->mobile = $withdraw_method['bank_card']['account'];
  746. $cash->bank_name = $withdraw_method['bank_card']['bank'];
  747. $cash->bank_branch = $withdraw_method['bank_card']['bank_branch'] ?? '';
  748. }
  749. if ($this->type == 'money') {
  750. $cash->type = 3;
  751. }
  752. $cash->pay_time = 0;
  753. $cash->service_charge = 0;
  754. $cash->cash_type = $this->cash_type;
  755. if (!$cash->save()) {
  756. $t->rollBack();
  757. return [
  758. 'code' => 1,
  759. 'msg' => '网络异常'
  760. ];
  761. }
  762. $user->price -= $this->cash;
  763. if (!$user->save()) {
  764. $t->rollBack();
  765. return [
  766. 'code' => 1,
  767. 'msg' => '网络异常'
  768. ];
  769. }
  770. $t->commit();
  771. return [
  772. 'code' => 0,
  773. 'msg' => '申请成功'
  774. ];
  775. } else {
  776. return [
  777. 'code' => 1,
  778. 'msg' => $this->getErrorSummary(false)[0],
  779. ];
  780. }
  781. }
  782. public function cashBalance() {
  783. $this->cash_type = Cash::IS_CASH_TYPE_BALANCE;
  784. if ($this->validate()) {
  785. $user = User::findOne(['id' => $this->user_id, 'store_id' => $this->store_id]);
  786. if (!$user) {
  787. return [
  788. 'code' => 1,
  789. 'msg' => '网络异常'
  790. ];
  791. }
  792. $balancToCash = Option::get(OptionSetting::BALANCE_TO_CASH, $this->store_id, 'recharge', 0)['value'];
  793. if (!$balancToCash) {
  794. return [
  795. 'code' => 1,
  796. 'msg' => '提现功能未开启'
  797. ];
  798. }
  799. // 判断提现金额
  800. if ($user->money < $this->cash) {
  801. return [
  802. 'code' => 1,
  803. 'msg' => '提现金额不能超过剩余金额'
  804. ];
  805. }
  806. $balancToCashSet = json_decode(Option::get(OptionSetting::BALANCE_TO_CASH_SETTING, $this->store_id, 'recharge', '{}')['value'], true);
  807. if (!isset($balancToCashSet['cash_service_charge'])) {
  808. return [
  809. 'code' => 1,
  810. 'msg' => '请先配置提现设置'
  811. ];
  812. }
  813. // 判断最小提现金
  814. if ($this->cash < $balancToCashSet['min_money']) {
  815. return [
  816. 'code' => 1,
  817. 'msg' => '提现金额不能小于' . $balancToCashSet['min_money'] . '元'
  818. ];
  819. }
  820. // 判断是否存在未完成提现
  821. $exit = Cash::find()->andWhere(['=', 'status', 0])->andWhere(['user_id' => $this->user_id,
  822. 'store_id' => $this->store_id, 'cash_type' => $this->cash_type])->exists();
  823. if ($exit) {
  824. // return [
  825. // 'code' => 1,
  826. // 'msg' => '尚有未完成的提现申请'
  827. // ];
  828. }
  829. if ($balancToCashSet['cash_max_single_day']) {
  830. $cash_sum = Cash::find()->where([
  831. 'store_id' => $this->store_id,
  832. 'is_delete' => 0,
  833. 'status' => [0, 1, 2, 5],
  834. 'user_id' => $this->user_id,
  835. 'cash_type' => $this->cash_type,
  836. ])->andWhere([
  837. 'AND',
  838. ['>=', 'created_at', strtotime(date('Y-m-d 00:00:00'))],
  839. ['<=', 'created_at', strtotime(date('Y-m-d 23:59:59'))],
  840. ])->sum('price');
  841. $cash_max_single_day_ = $balancToCashSet['cash_max_single_day'] - ($cash_sum ?: 0);
  842. if ($this->cash > $cash_max_single_day_) {
  843. return [
  844. 'code' => 1,
  845. 'msg' => '每人每天提现金额不能超过'. $balancToCashSet['cash_max_single_day'] .'元'
  846. ];
  847. }
  848. }
  849. if ($balancToCashSet['cash_max_day']) {
  850. $cash_sum = Cash::find()->where([
  851. 'store_id' => $this->store_id,
  852. 'is_delete' => 0,
  853. 'status' => [0, 1, 2, 5],
  854. 'cash_type' => $this->cash_type,
  855. ])->andWhere([
  856. 'AND',
  857. ['>=', 'created_at', strtotime(date('Y-m-d 00:00:00'))],
  858. ['<=', 'created_at', strtotime(date('Y-m-d 23:59:59'))],
  859. ])->sum('price');
  860. $cash_max_day_ = $balancToCashSet['cash_max_day'] - ($cash_sum ?: 0);
  861. if ($this->cash > $cash_max_day_) {
  862. return [
  863. 'code' => 1,
  864. 'msg' => '超过平台每天限额'. $balancToCashSet['cash_max_day'] .'元'
  865. ];
  866. }
  867. }
  868. $t = \Yii::$app->db->beginTransaction();
  869. $cash = new Cash();
  870. $cash->order_no = OrderNo::getOrderNo(OrderNo::ORDER_CASH);
  871. $cash->is_delete = 0;
  872. $cash->status = 0;
  873. $cash->price = $this->cash;
  874. $cash->service_charge = $balancToCashSet['cash_service_charge'];
  875. $cash->created_at = time();
  876. $cash->user_id = $this->user_id;
  877. $cash->store_id = $this->store_id;
  878. $saas_user = get_saas_user();
  879. $withdraw_method = Json::decode($saas_user->withdraw_method);
  880. $withdraw_method = array_column((array)$withdraw_method, null, 'type');
  881. if ($this->type == 'wechat') {
  882. $cash->type = 0;
  883. $cash->name = $withdraw_method['wechat']['name'];
  884. $cash->mobile = $withdraw_method['wechat']['account'];
  885. }
  886. if ($this->type == 'alipay') {
  887. $cash->type = 1;
  888. $cash->name = $withdraw_method['alipay']['name'];
  889. $cash->mobile = $withdraw_method['alipay']['account'];
  890. }
  891. if ($this->type == 'bank_card') {
  892. $cash->type = 2;
  893. $cash->name = $withdraw_method['bank_card']['name'];
  894. $cash->mobile = $withdraw_method['bank_card']['account'];
  895. $cash->bank_name = $withdraw_method['bank_card']['bank'];
  896. }
  897. if ($this->type == 'lg') {
  898. //灵工提现
  899. $lg_info = Lg::find()->where(['user_id'=>$saas_user->id,'is_delete'=>0,'status'=>1])->one();
  900. $cash->type = 4;
  901. $cash->name = $lg_info->name;
  902. $cash->mobile = $lg_info->mobile;
  903. }
  904. $cash->pay_time = 0;
  905. $cash->cash_type = $this->cash_type;
  906. if (!$cash->save()) {
  907. $t->rollBack();
  908. return [
  909. 'code' => 1,
  910. 'msg' => '网络异常,提现保存失败'
  911. ];
  912. }
  913. // 免单返现提现记录
  914. if ($this->is_queue){
  915. $queue_account_log = new QueueAccountLog();
  916. $queue_account_log->store_id = $this->store_id;
  917. $queue_account_log->user_id = $this->user_id;
  918. $queue_account_log->order_id = $cash->id;
  919. $queue_account_log->amount = $this->cash;
  920. $queue_account_log->desc = '免单提现';
  921. $queue_account_log->log_type = 2;
  922. $queue_account_log->type = 1;
  923. $queue_account_log->created_at = time();
  924. $queue_account_log->save();
  925. }
  926. if ($this->cash_type == 7 && $this->cash <= $balancToCashSet['auto_money'] && $balancToCashSet['auto_money'] > 0) {
  927. // TODO 在设置金额内,余额提现自动打款
  928. $this->confirm($cash->id, 2, $this->store_id);
  929. }
  930. $saveLog = AccountLog::saveLog($this->user_id, $cash->price, AccountLog::TYPE_BALANCE, AccountLog::LOG_TYPE_EXPEND, 0, 0, "余额提现,单号:{$cash->order_no}");
  931. if (!$saveLog) {
  932. $t->rollBack();
  933. return [
  934. 'code' => 1,
  935. 'msg' => '网络异常,账户保存失败'
  936. ];
  937. }
  938. $t->commit();
  939. return [
  940. 'code' => 0,
  941. 'msg' => '申请成功'
  942. ];
  943. } else {
  944. return [
  945. 'code' => 1,
  946. 'msg' => $this->getErrorSummary(false)[0],
  947. ];
  948. }
  949. }
  950. private function getOrderNo()
  951. {
  952. $order_no = null;
  953. while (true) {
  954. $order_no = date('YmdHis') . mt_rand(100000, 999999);
  955. $exist_order_no = Cash::find()->where(['order_no' => $order_no])->exists();
  956. if (!$exist_order_no) {
  957. break;
  958. }
  959. }
  960. return $order_no;
  961. }
  962. public static function getPayTyle()
  963. {
  964. $store_id = get_store_id();
  965. $setting = Option::get('share_basic_setting', $store_id);
  966. $setting = $setting ? Json::decode($setting['value']) : [];
  967. $res['pay_type'] = $setting['pay_type']['value'];
  968. $res['bank'] = $setting['bank']['value'];
  969. $res['remaining_sum'] = $setting['remaining_sum']['value'];
  970. $cash_last = Cash::find()->where(['store_id' => $store_id, 'user_id' => get_user_id(), 'is_delete' => 0])
  971. ->orderBy(['id' => SORT_DESC])->select(['name', 'mobile', 'type', 'bank_name'])->asArray()->one();
  972. $res['cash_last'] = $cash_last;
  973. $cash_max_day = floatval($setting['cash_max_day']['value']);
  974. if ($cash_max_day) {
  975. $cash_sum = Cash::find()->where([
  976. 'store_id' => $store_id,
  977. 'is_delete' => 0,
  978. 'status' => [0, 1, 2, 5],
  979. ])->andWhere([
  980. 'AND',
  981. ['>=', 'created_at', strtotime(date('Y-m-d 00:00:00'))],
  982. ['<=', 'created_at', strtotime(date('Y-m-d 23:59:59'))],
  983. ])->sum('price');
  984. $cash_max_day = $cash_max_day - ($cash_sum ? $cash_sum : 0);
  985. $res['cash_max_day'] = max(0, floatval(sprintf('%.2f', $cash_max_day)));
  986. } else {
  987. $res['cash_max_day'] = -1;
  988. }
  989. $cashServiceCharge = floatval($setting['cash_service_charge']['value']);
  990. $res['cash_service_charge'] = $cashServiceCharge;
  991. if($cashServiceCharge == 0){
  992. $res['service_content'] = "";
  993. }else{
  994. $res['service_content'] = "提现需要加收{$cashServiceCharge}%手续费";
  995. }
  996. $payTypeList = [
  997. [
  998. 'name' => '微信',
  999. 'is_show' => false
  1000. ],
  1001. [
  1002. 'name' => '支付宝',
  1003. 'is_show' => false
  1004. ],
  1005. [
  1006. 'name' => '银行卡',
  1007. 'is_show' => false
  1008. ],
  1009. [
  1010. 'name' => '余额',
  1011. 'is_show' => false
  1012. ],
  1013. ];
  1014. switch($res['pay_type']){
  1015. case 0:
  1016. $payTypeList[0]['is_show'] = true;
  1017. break;
  1018. case 1:
  1019. $payTypeList[1]['is_show'] = true;
  1020. break;
  1021. case 2:
  1022. $payTypeList[0]['is_show'] = true;
  1023. $payTypeList[1]['is_show'] = true;
  1024. break;
  1025. default:
  1026. break;
  1027. }
  1028. if($res['bank'] && $res['bank'] == 1){
  1029. $payTypeList[2]['is_show'] = true;
  1030. }
  1031. if($res['remaining_sum'] && $res['remaining_sum'] == 1){
  1032. $payTypeList[3]['is_show'] = true;
  1033. }
  1034. $res['pay_type_list'] = $payTypeList;
  1035. return $res;
  1036. }
  1037. public function saveNew()
  1038. {
  1039. if ($this->validate()) {
  1040. $cash_price = $this->cash;
  1041. $cash = Option::get(OptionSetting::CASH_SETTING, get_store_id(), 'store')['value'];
  1042. $setting = $cash ? Json::decode($cash, true) : [];
  1043. $cash_max_day = floatval($setting['cash_max_day']);
  1044. $cash_max_single_day = floatval($setting['cash_max_single_day']);
  1045. $cash_service_charge = floatval($setting['cash_service_charge']);
  1046. $integral_appreciation_price = 0;
  1047. $integral_appreciation_result = [];
  1048. if (intval($this->cash_type) === Cash::IS_CASH_TYPE_INTEGRAL_APPRECIATION) {
  1049. $integral_appreciation_price = $this->cash;
  1050. $integral_appreciation_result = CashExt::integral_appreciation_cash($this->cash, $this->store_id);
  1051. $this->cash = $integral_appreciation_result['cash_price'];
  1052. }
  1053. if ($cash_max_day) {
  1054. $query = Cash::find()->where([
  1055. 'store_id' => $this->store_id,
  1056. 'is_delete' => 0,
  1057. 'status' => [0, 1, 2, 5],
  1058. ]);
  1059. $cash_sum = $query->andWhere([
  1060. 'AND',
  1061. ['>=', 'created_at', strtotime(date('Y-m-d 00:00:00'))],
  1062. ['<=', 'created_at', strtotime(date('Y-m-d 23:59:59'))],
  1063. ])->sum('price');
  1064. $cash_max_day = $cash_max_day - ($cash_sum ? $cash_sum : 0);
  1065. if ($this->cash > $cash_max_day) {
  1066. return [
  1067. 'code' => 1,
  1068. 'msg' => '超过平台提现限额,请明日提现'
  1069. ];
  1070. }
  1071. }
  1072. if ($cash_max_single_day) {
  1073. $cash_sum = Cash::find()->where([
  1074. 'store_id' => $this->store_id,
  1075. 'is_delete' => 0,
  1076. 'status' => [0, 1, 2, 5],
  1077. 'user_id' => $this->user_id,
  1078. ])->andWhere([
  1079. 'AND',
  1080. ['>=', 'created_at', strtotime(date('Y-m-d 00:00:00'))],
  1081. ['<=', 'created_at', strtotime(date('Y-m-d 23:59:59'))],
  1082. ])->sum('price');
  1083. $cash_max_single_day_ = $cash_max_single_day - ($cash_sum ?: 0);
  1084. if ($this->cash > $cash_max_single_day_) {
  1085. return [
  1086. 'code' => 1,
  1087. 'msg' => '每人每天提现金额不能超过' . $cash_max_single_day . '元'
  1088. ];
  1089. }
  1090. }
  1091. $user = User::findOne(['id' => $this->user_id, 'store_id' => $this->store_id]);
  1092. if (!$user) {
  1093. return [
  1094. 'code' => 1,
  1095. 'msg' => '网络异常'
  1096. ];
  1097. }
  1098. $share_setting = [
  1099. 'min_money' => $setting['min_money'],
  1100. 'auto_money' => 0,
  1101. ];
  1102. if ($this->cash < $share_setting['min_money']) {
  1103. return [
  1104. 'code' => 1,
  1105. 'msg' => '提现金额不能小于' . $share_setting['min_money'] . '元'
  1106. ];
  1107. }
  1108. // 判断提现金额
  1109. if (in_array($this->cash_type, [0, Cash::IS_CASH_TYPE_TEAM_GRADES, Cash::IS_CASH_TYPE_SHARE_GROUP_PURCHASE, Cash::IS_CASH_TYPE_SUPER_SALES, 3]) && $user->price < $this->cash) {
  1110. return [
  1111. 'code' => 1,
  1112. 'msg' => '提现金额不能超过剩余金额!'
  1113. ];
  1114. } elseif (($this->cash_type == Cash::IS_CASH_TYPE_INTEGRAL_APPRECIATION)) {
  1115. $integralAppreciationUser = IntegralAppreciationUser::findOne(['user_id' => $user->id]);
  1116. if ($integralAppreciationUser->integral < $cash_price) {
  1117. return [
  1118. 'code' => 1,
  1119. 'msg' => '提现金额不能超过剩余金额。'
  1120. ];
  1121. }
  1122. $user_total_integral = IntegralAppreciationUser::find()->where(['store_id' => $this->store_id])->sum('integral') ?: 0;
  1123. if (bcsub($user_total_integral, $cash_price, 2) <= 0.1) {
  1124. return [
  1125. 'code' => 1,
  1126. 'msg' => '提现金额受限'
  1127. ];
  1128. }
  1129. } elseif (($this->cash_type == 1) && $user->tuan_price < $this->cash) {
  1130. return [
  1131. 'code' => 1,
  1132. 'msg' => '提现金额不能超过剩余金额'
  1133. ];
  1134. }
  1135. if (intval($this->cash_type) === Cash::IS_CASH_TYPE_TEAM_GRADES) {
  1136. $userTeamGrades = TeamGrades::findOne(['user_id' => get_user_id(), 'is_delete' => 0]);
  1137. if (!$userTeamGrades) {
  1138. return [
  1139. 'code' => 1,
  1140. 'msg' => '非团队业绩成员'
  1141. ];
  1142. }
  1143. if ($userTeamGrades->price < $this->cash) {
  1144. return [
  1145. 'code' => 1,
  1146. 'msg' => '提现金额不足'
  1147. ];
  1148. }
  1149. }
  1150. if (in_array(intval($this->cash_type), [Cash::IS_CASH_TYPE_SHARE_GROUP_PURCHASE, Cash::IS_CASH_TYPE_SUPER_SALES])) {
  1151. if (intval($this->cash_type) === Cash::IS_CASH_TYPE_SHARE_GROUP_PURCHASE) {
  1152. $activityUser = ShareGroupPurchaseUser::findOne(['user_id' => get_user_id()]);
  1153. if (empty($activityUser)) {
  1154. return [
  1155. 'code' => 1,
  1156. 'msg' => '未参与37拼购活动'
  1157. ];
  1158. }
  1159. } else {
  1160. $activityUser = SuperSalesUser::findOne(['user_id' => get_user_id()]);
  1161. }
  1162. if ($activityUser->price < $this->cash) {
  1163. return [
  1164. 'code' => 1,
  1165. 'msg' => '提现金额不足'
  1166. ];
  1167. }
  1168. }
  1169. if (intval($this->cash_type) === Cash::IS_CASH_TYPE_INTEGRAL_APPRECIATION) {
  1170. $integral_appreciation_user = IntegralAppreciationUser::findOne(['user_id' => get_user_id()]);
  1171. if (empty($integral_appreciation_user)) {
  1172. return [
  1173. 'code' => 1,
  1174. 'msg' => '提现金额不足'
  1175. ];
  1176. }
  1177. if ($integral_appreciation_user->integral < $integral_appreciation_price) {
  1178. return [
  1179. 'code' => 1,
  1180. 'msg' => '提现金额不足'
  1181. ];
  1182. }
  1183. }
  1184. $t = \Yii::$app->db->beginTransaction();
  1185. $cash = new Cash();
  1186. $cash->order_no = OrderNo::getOrderNo(OrderNo::ORDER_CASH);
  1187. $cash->is_delete = 0;
  1188. $cash->status = 0;
  1189. $cash->price = $this->cash;
  1190. $cash->created_at = time();
  1191. $cash->user_id = $this->user_id;
  1192. $cash->store_id = $this->store_id;
  1193. $saas_user = get_saas_user();
  1194. $withdraw_method = Json::decode($saas_user->withdraw_method);
  1195. $withdraw_method = array_column((array)$withdraw_method, null, 'type');
  1196. if ($this->type == 'money') {
  1197. $cash->type = 3;
  1198. } else {
  1199. if ($this->type == 'wechat') {
  1200. $cash->type = 0;
  1201. $cash->name = $withdraw_method['wechat']['name'];
  1202. $cash->mobile = $withdraw_method['wechat']['account'];
  1203. }
  1204. if ($this->type == 'alipay') {
  1205. $cash->type = 1;
  1206. $cash->name = $withdraw_method['alipay']['name'];
  1207. $cash->mobile = $withdraw_method['alipay']['account'];
  1208. }
  1209. if ($this->type == 'bank_card') {
  1210. $cash->type = 2;
  1211. $cash->name = $withdraw_method['bank_card']['name'];
  1212. $cash->mobile = $withdraw_method['bank_card']['account'];
  1213. $cash->bank_name = $withdraw_method['bank_card']['bank'];
  1214. $cash->bank_branch = $withdraw_method['bank_card']['branch'];
  1215. }
  1216. if ($this->type == 'lg') {
  1217. //灵工提现
  1218. $lg_info = Lg::find()->where(['user_id' => $saas_user->id, 'is_delete' => 0, 'status' => 1])->one();
  1219. $cash->type = 4;
  1220. $cash->name = $lg_info->name;
  1221. $cash->mobile = $lg_info->mobile;
  1222. }
  1223. }
  1224. $cash->pay_time = 0;
  1225. // 如果是 提现方式是余额的时候 不收取手续费
  1226. $cash->service_charge = $cash->type == Cash::TYPE_RECHARGE ? 0 : $cash_service_charge;
  1227. $cash->cash_type = $this->cash_type;
  1228. if ($cash->save()) {
  1229. //如果是团长提现,扣除会员里的团长佣金字段
  1230. if ($this->cash_type == 1) {
  1231. $user->tuan_price -= $this->cash;
  1232. } else {
  1233. $user->price -= $this->cash;
  1234. }
  1235. if ($this->cash_type == Cash::IS_CASH_TYPE_TEAM_GRADES) {
  1236. $userTeamGrades->price -= $this->cash;
  1237. if (!$userTeamGrades->save()) {
  1238. $t->rollBack();
  1239. return [
  1240. 'code' => 1,
  1241. 'msg' => implode(';', array_values($userTeamGrades->firstErrors))
  1242. ];
  1243. };
  1244. $user->price -= $this->cash;
  1245. }
  1246. if (intval($this->cash_type) === Cash::IS_CASH_TYPE_INTEGRAL_APPRECIATION) {
  1247. $integral_appreciation_user->integral -= $integral_appreciation_price;
  1248. if (!$integral_appreciation_user->save()) {
  1249. $t->rollBack();
  1250. return [
  1251. 'code' => 1,
  1252. 'msg' => implode(';', array_values($integral_appreciation_user->firstErrors))
  1253. ];
  1254. };
  1255. $integral_appreciation_cash = new IntegralAppreciationCashLog();
  1256. $integral_appreciation_cash->user_id = $this->user_id;
  1257. $integral_appreciation_cash->cash_id = $cash->id;
  1258. $integral_appreciation_cash->integral = $integral_appreciation_price;
  1259. $integral_appreciation_cash->integral_price = $integral_appreciation_result['integral_price'];;
  1260. $integral_appreciation_cash->reflux_profit = $integral_appreciation_result['integral_payout_pool_profit'];
  1261. $integral_appreciation_cash->reflux_price = $integral_appreciation_result['payout_pool_price'];
  1262. $integral_appreciation_cash->cash_price = $this->cash;
  1263. if (!$integral_appreciation_cash->save()) {
  1264. $t->rollBack();
  1265. return [
  1266. 'code' => 1,
  1267. 'msg' => implode(';', array_values($integral_appreciation_user->firstErrors))
  1268. ];
  1269. };
  1270. $result = IntegralAppreciationUserIntegralLog::saveIntegralLog($integral_appreciation_user->id, $integral_appreciation_price, IntegralAppreciationUserIntegralLog::TYPE_EXPEND, IntegralAppreciationUserIntegralLog::SOURCE_TYPE_WITHDRAW);
  1271. if ($result['code']) {
  1272. $t->rollBack();
  1273. return $result;
  1274. }
  1275. // $user->price -= $this->cash;
  1276. }
  1277. if (in_array(intval($this->cash_type), [Cash::IS_CASH_TYPE_SHARE_GROUP_PURCHASE, Cash::IS_CASH_TYPE_SUPER_SALES])) {
  1278. $activityUser->price -= $this->cash;
  1279. if (!$activityUser->save()) {
  1280. $t->rollBack();
  1281. return [
  1282. 'code' => 1,
  1283. 'msg' => implode(';', array_values($activityUser->firstErrors))
  1284. ];
  1285. };
  1286. $user->price -= $this->cash;
  1287. }
  1288. if (!$user->save()) {
  1289. $t->rollBack();
  1290. return [
  1291. 'code' => 1,
  1292. 'msg' => '网络异常'
  1293. ];
  1294. }
  1295. if ($cash->type == Cash::TYPE_RECHARGE || ($this->cash_type == 0 && $this->cash <= $share_setting['auto_money'] && $share_setting['auto_money'] > 0)) {
  1296. // TODO 在设置金额内,分销/充值佣金提现自动打款
  1297. debug_log([$cash->id, $cash->type == Cash::TYPE_RECHARGE ? 4 : 2, $this->store_id], 'cash.log');
  1298. $aa = $this->confirm($cash->id, $cash->type == Cash::TYPE_RECHARGE ? 4 : 2, $this->store_id);
  1299. debug_log($aa, 'cash.log');
  1300. }
  1301. $t->commit();
  1302. return [
  1303. 'code' => 0,
  1304. 'msg' => '申请成功'
  1305. ];
  1306. } else {
  1307. $t->rollBack();
  1308. return [
  1309. 'code' => 1,
  1310. 'msg' => '网络异常'
  1311. ];
  1312. }
  1313. } else {
  1314. return [
  1315. 'code' => 1,
  1316. 'msg' => $this->getErrorSummary(false)[0],
  1317. ];
  1318. }
  1319. }
  1320. }