CashForm.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\alliance\models;
  8. use app\models\Cash;
  9. use app\models\Option;
  10. use app\models\SaasUser;
  11. use app\models\Shop;
  12. use app\models\ShopSetting;
  13. use app\models\ShopShare;
  14. use app\models\User;
  15. use app\utils\OrderNo;
  16. use yii\base\Model;
  17. use yii\helpers\Json;
  18. class CashForm extends Model
  19. {
  20. public $user_id;
  21. public $saas_id;
  22. public $store_id;
  23. public $cash;
  24. public $name;
  25. public $mobile;
  26. public $pay_type;
  27. public $form_id;
  28. public $type;
  29. public $bank_name;
  30. public $cash_type = 0;
  31. public function rules()
  32. {
  33. return [
  34. [['cash'], 'required'],
  35. [['name', 'mobile', 'pay_type'], 'required', 'on' => 'CASH'],
  36. [['cash'], 'number', 'min' => 0,],
  37. [['cash_type'], 'integer'],
  38. [['name', 'mobile', 'form_id', 'type'], 'trim'],
  39. [['pay_type'], 'in', 'range' => [0, 1, 2, 3]],
  40. [['bank_name'], 'string'],
  41. ];
  42. }
  43. public function attributeLabels()
  44. {
  45. return [
  46. 'cash' => '提现金额',
  47. 'name' => '姓名',
  48. 'pay_type' => '提现方式',
  49. 'mobile' => '账号',
  50. 'bank_name' => '开户行',
  51. ];
  52. }
  53. public function save()
  54. {
  55. if (!$this->validate()) {
  56. return [
  57. 'code' => 1,
  58. 'msg' => $this->getErrorSummary(false)[0],
  59. ];
  60. }
  61. $setting = Option::get('share_basic_setting', $this->store_id);
  62. $setting = $setting ? Json::decode($setting['value']) : [];
  63. $cash_max_day = floatval($setting['cash_max_day']['value']);
  64. if ($cash_max_day) {
  65. $cash_sum = Cash::find()->where([
  66. 'store_id' => $this->store_id,
  67. 'is_delete' => 0,
  68. 'status' => [0, 1, 2, 5],
  69. ])->andWhere([
  70. 'AND',
  71. ['>=', 'created_at', strtotime(date('Y-m-d 00:00:00'))],
  72. ['<=', 'created_at', strtotime(date('Y-m-d 23:59:59'))],
  73. ])->sum('price');
  74. $cash_max_day = $cash_max_day - ($cash_sum ? $cash_sum : 0);
  75. if ($this->cash > $cash_max_day) {
  76. return [
  77. 'code' => 1,
  78. 'msg' => '提现金额不能超过' . $cash_max_day . '元'
  79. ];
  80. }
  81. }
  82. $saas = SaasUser::findOne(['id' => $this->saas_id]);
  83. if (!$saas) {
  84. return [
  85. 'code' => 1,
  86. 'msg' => '网络异常'
  87. ];
  88. }
  89. $share_setting = [
  90. 'min_money' => $setting['min_money']['value'],
  91. ];
  92. if ($this->cash < $share_setting['min_money']) {
  93. return [
  94. 'code' => 1,
  95. 'msg' => '提现金额不能小于' . $share_setting['min_money'] . '元'
  96. ];
  97. }
  98. // 判断提现金额
  99. if ($this->cash_type == 0 && $saas->price < $this->cash) {
  100. return [
  101. 'code' => 1,
  102. 'msg' => '提现金额不能超过剩余金额'
  103. ];
  104. }
  105. $exit = Cash::find()->andWhere(['=', 'status', 0])->andWhere(['saas_id' => $this->saas_id,
  106. 'store_id' => $this->store_id,'cash_type' => 0])->exists();
  107. if ($exit) {
  108. // return [
  109. // 'code' => 1,
  110. // 'msg' => '尚有未完成的提现申请'
  111. // ];
  112. }
  113. $t = \Yii::$app->db->beginTransaction();
  114. $cash_service_charge = floatval($setting['cash_service_charge']['value']);
  115. $cash = new Cash();
  116. $cash->order_no = OrderNo::getOrderNo(OrderNo::ORDER_CASH);
  117. $cash->is_delete = 0;
  118. $cash->status = 0;
  119. $cash->price = $this->cash;
  120. $cash->created_at = time();
  121. $cash->user_id = 0;
  122. $cash->saas_id = $this->saas_id;
  123. $cash->store_id = $this->store_id;
  124. if ($this->cash_type == 0) {
  125. $saas_user = get_saas_user();
  126. $withdraw_method = Json::decode($saas_user->withdraw_method);
  127. $withdraw_method = array_column($withdraw_method, null, 'type');
  128. if ($this->type == 'wechat') {
  129. $cash->type = 0;
  130. $cash->name = $withdraw_method['wechat']['name'];
  131. $cash->mobile = $withdraw_method['wechat']['account'];
  132. }
  133. if ($this->type == 'alipay') {
  134. $cash->type = 1;
  135. $cash->name = $withdraw_method['alipay']['name'];
  136. $cash->mobile = $withdraw_method['alipay']['account'];
  137. }
  138. if ($this->type == 'bank_card') {
  139. $cash->type = 2;
  140. $cash->name = $withdraw_method['bank_card']['name'];
  141. $cash->mobile = $withdraw_method['bank_card']['account'];
  142. $cash->bank_name = $withdraw_method['bank_card']['bank'];
  143. $cash->bank_branch = $withdraw_method['bank_card']['branch'];
  144. }
  145. } else {
  146. $cash->type = $this->pay_type;
  147. $cash->name = $this->name;
  148. $cash->mobile = $this->mobile;
  149. $cash->bank_name = $this->bank_name;
  150. }
  151. $cash->pay_time = 0;
  152. $cash->service_charge = $cash_service_charge;
  153. $cash->cash_type = $this->cash_type;
  154. if ($cash->save()) {
  155. if ($this->cash_type == 0) {
  156. $saas->price -= $this->cash;
  157. }
  158. if (!$saas->save()) {
  159. $t->rollBack();
  160. return [
  161. 'code' => 1,
  162. 'msg' => '网络异常'
  163. ];
  164. }
  165. $t->commit();
  166. return [
  167. 'code' => 0,
  168. 'msg' => '申请成功'
  169. ];
  170. } else {
  171. $t->rollBack();
  172. return [
  173. 'code' => 1,
  174. 'msg' => '网络异常'
  175. ];
  176. }
  177. }
  178. private function getOrderNo()
  179. {
  180. $order_no = null;
  181. while (true) {
  182. $order_no = date('YmdHis') . mt_rand(100000, 999999);
  183. $exist_order_no = Cash::find()->where(['order_no' => $order_no])->exists();
  184. if (!$exist_order_no) {
  185. break;
  186. }
  187. }
  188. return $order_no;
  189. }
  190. public static function getPayTyle()
  191. {
  192. $store_id = get_store_id();
  193. $setting = Option::get('share_basic_setting', $store_id);
  194. $setting = $setting ? Json::decode($setting['value']) : [];
  195. $res['pay_type'] = $setting['pay_type']['value'];
  196. $res['bank'] = $setting['bank']['value'];
  197. $res['remaining_sum'] = $setting['remaining_sum']['value'];
  198. $cash_last = Cash::find()->where(['store_id' => $store_id, 'user_id' => get_user_id(), 'is_delete' => 0])
  199. ->orderBy(['id' => SORT_DESC])->select(['name', 'mobile', 'type', 'bank_name'])->asArray()->one();
  200. $res['cash_last'] = $cash_last;
  201. $cash_max_day = floatval($setting['cash_max_day']['value']);
  202. if ($cash_max_day) {
  203. $cash_sum = Cash::find()->where([
  204. 'store_id' => $store_id,
  205. 'is_delete' => 0,
  206. 'status' => [0, 1, 2, 5],
  207. ])->andWhere([
  208. 'AND',
  209. ['>=', 'created_at', strtotime(date('Y-m-d 00:00:00'))],
  210. ['<=', 'created_at', strtotime(date('Y-m-d 23:59:59'))],
  211. ])->sum('price');
  212. $cash_max_day = $cash_max_day - ($cash_sum ? $cash_sum : 0);
  213. $res['cash_max_day'] = max(0, floatval(sprintf('%.2f', $cash_max_day)));
  214. } else {
  215. $res['cash_max_day'] = -1;
  216. }
  217. $cashServiceCharge = floatval($setting['cash_service_charge']['value']);
  218. $res['cash_service_charge'] = $cashServiceCharge;
  219. if($cashServiceCharge == 0){
  220. $res['service_content'] = "";
  221. }else{
  222. $res['service_content'] = "提现需要加收{$cashServiceCharge}%手续费";
  223. }
  224. $payTypeList = [
  225. [
  226. 'name' => '微信',
  227. 'is_show' => false
  228. ],
  229. [
  230. 'name' => '支付宝',
  231. 'is_show' => false
  232. ],
  233. [
  234. 'name' => '银行卡',
  235. 'is_show' => false
  236. ],
  237. [
  238. 'name' => '余额',
  239. 'is_show' => false
  240. ],
  241. ];
  242. switch($res['pay_type']){
  243. case 0:
  244. $payTypeList[0]['is_show'] = true;
  245. break;
  246. case 1:
  247. $payTypeList[1]['is_show'] = true;
  248. break;
  249. case 2:
  250. $payTypeList[0]['is_show'] = true;
  251. $payTypeList[1]['is_show'] = true;
  252. break;
  253. default:
  254. break;
  255. }
  256. if($res['bank'] && $res['bank'] == 1){
  257. $payTypeList[2]['is_show'] = true;
  258. }
  259. if($res['remaining_sum'] && $res['remaining_sum'] == 1){
  260. $payTypeList[3]['is_show'] = true;
  261. }
  262. $res['pay_type_list'] = $payTypeList;
  263. return $res;
  264. }
  265. }