Withdraw.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Class Withdraw
  5. * Author: Yitan
  6. * Date: 2017/11/06
  7. * @package app\common\models
  8. */
  9. namespace app\common\models;
  10. use app\common\facades\Setting;
  11. use app\common\traits\CreateOrderSnTrait;
  12. /**
  13. * @property int $id
  14. * @property int $member_id
  15. * @property int $status
  16. * @property int $type_id
  17. * @property int $arrival_at
  18. * @property string $type_name
  19. * @property string $withdraw_sn
  20. * @property double $poundage
  21. * @property double $amounts
  22. * @property double $actual_amounts
  23. *
  24. * @property Member $hasOneMember
  25. *
  26. * @method self initial()
  27. * @method self audit()
  28. * @method self paying()
  29. * @method self payed()
  30. * @method self rebut()
  31. * @method self invalid()
  32. *
  33. * Class Withdraw
  34. * @package app\common\models
  35. */
  36. class Withdraw extends BaseModel
  37. {
  38. use CreateOrderSnTrait;
  39. /**
  40. * 提现审核状态:无效
  41. */
  42. const STATUS_INVALID = -1;
  43. /**
  44. * 提现审核状态:未审核
  45. */
  46. const STATUS_INITIAL = 0;
  47. /**
  48. * 提现审核状态:待打款
  49. */
  50. const STATUS_AUDIT = 1;
  51. /**
  52. * 提现审核状态:已打款
  53. */
  54. const STATUS_PAY = 2;
  55. /**
  56. * 提现审核状态:已驳回
  57. */
  58. const STATUS_REBUT = 3;
  59. /**
  60. * 提现审核状态:打款中
  61. */
  62. const STATUS_PAYING = 4;
  63. /**
  64. * 提现打款方式:打款至余额
  65. */
  66. const WITHDRAW_WITH_BALANCE = 'balance';
  67. /**
  68. * 提现打款方式:打款至微信
  69. */
  70. const WITHDRAW_WITH_WECHAT = 'wechat';
  71. /**
  72. * 提现打款方式:打款至支付宝
  73. */
  74. const WITHDRAW_WITH_ALIPAY = 'alipay';
  75. /**
  76. * 提现打款方式:手动打款
  77. */
  78. const WITHDRAW_WITH_MANUAL = 'manual';
  79. /**
  80. * 提现打款方式:打款到环迅
  81. */
  82. const WITHDRAW_WITH_HUANXUN = 'huanxun';
  83. /**
  84. * 提现打款方式:打款到EUP
  85. */
  86. const WITHDRAW_WITH_EUP_PAY = 'eup_pay';
  87. /**
  88. * @author blank
  89. * 提现打款方式:打款到易宝
  90. */
  91. const WITHDRAW_WITH_YOP = 'yop_pay';
  92. const WITHDRAW_WITH_SEPARATE_UNION_PAY = 'separate';
  93. /**
  94. * 提现打款方式:打款到汇聚
  95. */
  96. const WITHDRAW_WITH_CONVERGE_PAY = 'converge_pay';
  97. /**
  98. * 提现打款方式:打款到易宝代付
  99. */
  100. const WITHDRAW_WITH_YEE_PAY = 'yee_pay';
  101. /**
  102. * 提现打款方式:打款到微信-高灯
  103. */
  104. const WITHDRAW_WITH_HIGH_LIGHT_WECHAT = 'high_light_wechat';
  105. /**
  106. * 提现打款方式:打款到支付宝-高灯
  107. */
  108. const WITHDRAW_WITH_HIGH_LIGHT_ALIPAY = 'high_light_alipay';
  109. /**
  110. * 提现打款方式:打款到银行卡-高灯
  111. */
  112. const WITHDRAW_WITH_HIGH_LIGHT_BANK = 'high_light_bank';
  113. /**
  114. * 提现打款方式:打款到微信-好灵工
  115. */
  116. const WITHDRAW_WITH_WORK_WITHDRAW_WECHAT = 'worker_withdraw_wechat';
  117. /**
  118. * 提现打款方式:打款到支付宝-好灵工
  119. */
  120. const WITHDRAW_WITH_WORK_WITHDRAW_ALIPAY = 'worker_withdraw_alipay';
  121. /**
  122. * 提现打款方式:打款到银行卡-好灵工
  123. */
  124. const WITHDRAW_WITH_WORK_WITHDRAW_BANK = 'worker_withdraw_bank';
  125. /**
  126. * 提现打款方式:打款到银行卡-智E+
  127. */
  128. const WITHDRAW_WITH_EPLUS_WITHDRAW_BANK = 'eplus_withdraw_bank';
  129. /**
  130. * 提现打款方式:打款到银典支付
  131. */
  132. const WITHDRAW_WITH_SILVER_POINT = 'silver_point';
  133. /**
  134. * 手动打款方式:手动至银行卡
  135. */
  136. const MANUAL_TO_BANK = 1;
  137. /**
  138. * 手动打款方式:手动至微信
  139. */
  140. const MANUAL_TO_WECHAT = 2;
  141. /**
  142. * 手动打款方式:手动至支付宝
  143. */
  144. const MANUAL_TO_ALIPAY = 3;
  145. /**
  146. * 审核通过的收入 ids 集合
  147. *
  148. * @var array
  149. */
  150. public $audit_ids = [];
  151. /**
  152. * 审核驳回的收入 ids 集合
  153. *
  154. * @var array
  155. */
  156. public $rebut_ids = [];
  157. /**
  158. * 审核无效的收入 ids 集合
  159. *
  160. * @var array
  161. */
  162. public $invalid_ids = [];
  163. /**
  164. * 提现打款方式集合
  165. *
  166. * @var array
  167. */
  168. public static $payWayComment = [
  169. self::WITHDRAW_WITH_BALANCE => '提现到余额',
  170. self::WITHDRAW_WITH_WECHAT => '提现到微信',
  171. self::WITHDRAW_WITH_ALIPAY => '提现到支付宝',
  172. self::WITHDRAW_WITH_MANUAL => '提现手动打款',
  173. self::WITHDRAW_WITH_HUANXUN => '提现到银行卡',
  174. self::WITHDRAW_WITH_EUP_PAY => '提现EUP',
  175. self::WITHDRAW_WITH_YOP => '提现易宝',
  176. self::WITHDRAW_WITH_SEPARATE_UNION_PAY => '提现银联',
  177. self::WITHDRAW_WITH_CONVERGE_PAY => '提现到银行卡-HJ',
  178. self::WITHDRAW_WITH_YEE_PAY => '提现易宝代付',
  179. self::WITHDRAW_WITH_HIGH_LIGHT_WECHAT => '提现到微信-高灯',
  180. self::WITHDRAW_WITH_HIGH_LIGHT_ALIPAY => '提现到支付宝-高灯',
  181. self::WITHDRAW_WITH_HIGH_LIGHT_BANK => '提现到银行卡-高灯',
  182. self::WITHDRAW_WITH_WORK_WITHDRAW_WECHAT => '提现到微信-好灵工',
  183. self::WITHDRAW_WITH_WORK_WITHDRAW_ALIPAY => '提现到支付宝-好灵工',
  184. self::WITHDRAW_WITH_WORK_WITHDRAW_BANK => '提现到银行卡-好灵工',
  185. self::WITHDRAW_WITH_EPLUS_WITHDRAW_BANK => '提现到银行卡-智E+',
  186. self::WITHDRAW_WITH_SILVER_POINT => '提现到银典支付'
  187. ];
  188. public static $noDeductionServicetax = [//不扣除劳务税的提现类型
  189. 'Yunshop\StoreCashier\common\models\StoreOrder',
  190. ];
  191. /**
  192. * 提现审核状态集合
  193. *
  194. * @var array
  195. */
  196. public static $statusComment = [
  197. self::STATUS_INVALID => '已无效',
  198. self::STATUS_INITIAL => '待审核',
  199. self::STATUS_AUDIT => '待打款',
  200. self::STATUS_PAY => '已打款',
  201. self::STATUS_REBUT => '已驳回',
  202. self::STATUS_PAYING => '打款中',
  203. ];
  204. /**
  205. * 数据表名称
  206. *
  207. * @var string
  208. */
  209. protected $table = 'yz_withdraw';
  210. /**
  211. * @var array
  212. */
  213. protected $guarded = [];
  214. /**
  215. * @var array
  216. */
  217. protected $dates = [
  218. 'audit_at',
  219. 'pay_at',
  220. 'arrival_at'
  221. ];
  222. /**
  223. * @var array
  224. */
  225. protected $appends = ['status_name', 'pay_way_name'];
  226. public function member()
  227. {
  228. return $this->hasOne(Member::class, 'uid', 'member_id');
  229. }
  230. public function hasOneMember()
  231. {
  232. return $this->hasOne(Member::class, 'uid', 'member_id');
  233. }
  234. public function hasOneYzMember()
  235. {
  236. return $this->hasOne('app\backend\modules\member\models\MemberShopInfo', 'member_id', 'member_id');
  237. }
  238. public function bankCard()
  239. {
  240. return $this->hasOne('app\common\models\member\BankCard', 'member_id', 'member_id');
  241. }
  242. /**
  243. * 通过 $status 值获取 $status 名称
  244. *
  245. * @param $status
  246. * @return mixed|string
  247. */
  248. public static function getStatusComment($status)
  249. {
  250. return isset(static::$statusComment[$status]) ? static::$statusComment[$status] : '';
  251. }
  252. /**
  253. * 通过 $pay_way 值获取 $pay_way 名称
  254. *
  255. * @param $pay_way
  256. * @return mixed|string
  257. */
  258. public static function getPayWayComment($pay_way)
  259. {
  260. static::$payWayComment[self::WITHDRAW_WITH_BALANCE] = Setting::get('shop.shop.credit') ? '提现到' . Setting::get(
  261. 'shop.shop.credit'
  262. ) : '提现到余额';
  263. if (app('plugins')->isEnabled('high-light')) {
  264. static::$payWayComment[self::WITHDRAW_WITH_HIGH_LIGHT_WECHAT] = '提现到微信-' . \Yunshop\HighLight\services\SetService::getDiyName(
  265. );
  266. static::$payWayComment[self::WITHDRAW_WITH_HIGH_LIGHT_ALIPAY] = '提现到支付宝-' . \Yunshop\HighLight\services\SetService::getDiyName(
  267. );
  268. static::$payWayComment[self::WITHDRAW_WITH_HIGH_LIGHT_BANK] = '提现到银行卡-' . \Yunshop\HighLight\services\SetService::getDiyName(
  269. );
  270. }
  271. return isset(static::$payWayComment[$pay_way]) ? static::$payWayComment[$pay_way] : '';
  272. }
  273. /**
  274. * 通过字段 status 输出 status_name ;
  275. *
  276. * @return string
  277. */
  278. public function getStatusNameAttribute()
  279. {
  280. return static::getStatusComment($this->attributes['status']);
  281. }
  282. /**
  283. * 通过字段 pay_way 输出 pay_way_name ;
  284. *
  285. * @return string
  286. */
  287. public function getPayWayNameAttribute()
  288. {
  289. return static::getPayWayComment($this->attributes['pay_way']);
  290. }
  291. /**
  292. * 待审核状态
  293. *
  294. * @param $query
  295. */
  296. public function scopeInitial($query)
  297. {
  298. $query->where('status', self::STATUS_INITIAL);
  299. }
  300. /**
  301. * 待打款状态
  302. *
  303. * @param $query
  304. */
  305. public function scopeAudit($query)
  306. {
  307. $query->where('status', self::STATUS_AUDIT);
  308. }
  309. /**
  310. * 打款中状态
  311. *
  312. * @param $query
  313. */
  314. public function scopePaying($query)
  315. {
  316. $query->where('status', self::STATUS_PAYING);
  317. }
  318. /**
  319. * 已打款状态
  320. *
  321. * @param $query
  322. */
  323. public function scopePayed($query)
  324. {
  325. $query->where('status', self::STATUS_PAY);
  326. }
  327. /**
  328. * 已驳回状态
  329. *
  330. * @param $query
  331. */
  332. public function scopeRebut($query)
  333. {
  334. $query->where('status', self::STATUS_REBUT);
  335. }
  336. /**
  337. * 已无效状态
  338. *
  339. * @param $query
  340. */
  341. public function scopeInvalid($query)
  342. {
  343. $query->where('status', self::STATUS_INVALID);
  344. }
  345. public function scopeRecords($query)
  346. {
  347. $types = static::getIncomeTypes();
  348. return $query->uniacid()->whereIn('type', $types);
  349. }
  350. public function scopeOfStatus($query, $status)
  351. {
  352. return $query->where('status', $status);
  353. }
  354. public function scopeOfWithdrawSn($query, $withdraw_sn)
  355. {
  356. return $query->where('withdraw_sn', $withdraw_sn);
  357. }
  358. public function atributeNames()
  359. {
  360. return [
  361. 'member_id' => '会员ID',
  362. 'type' => '提现类型',
  363. 'amounts' => '提现金额',
  364. 'pay_way' => '打款方式',
  365. ];
  366. }
  367. public function rules()
  368. {
  369. return [
  370. 'member_id' => 'required',
  371. 'type' => 'required',
  372. 'amounts' => 'required',
  373. 'pay_way' => 'required',
  374. ];
  375. }
  376. /**
  377. * todo 应该剔出本类
  378. *
  379. * 获取已开启插件 type 字段集
  380. *
  381. * @return array
  382. */
  383. public static function getIncomeTypes()
  384. {
  385. $configs = \app\backend\modules\income\Income::current()->getItems();
  386. $types = [];
  387. foreach ($configs as $config) {
  388. $types[] = $config['class'];
  389. }
  390. return $types;
  391. }
  392. /********************* todo 以下代码不确定功能逻辑,需要处理删除 yitian 2017-12-19 ****************/
  393. public $separate = [];
  394. public $attributes = [];
  395. public $StatusService;
  396. public $PayWayService;
  397. public $TypeData;
  398. /**
  399. * @return string
  400. */
  401. public function getTypeDataAttribute()
  402. {
  403. if (!isset($this->TypeData)) {
  404. $configs = \app\backend\modules\income\Income::current()->getItems();
  405. foreach ($configs as $key => $config) {
  406. if ($config['class'] === $this->type) {
  407. $orders = Income::getIncomeByIds($this->type_id)->get();
  408. // $is_pay = Income::getIncomeByIds($this->type_id)->where('pay_status','1')->get()->sum(amount);
  409. if ($orders) {
  410. $this->TypeData['income_total'] = $orders->count();
  411. // $this->TypeData['is_pay'] = $is_pay;
  412. $this->TypeData['incomes'] = $orders->toArray();
  413. // foreach ($orders as $k => $order) {
  414. //// $this->TypeData['orders'][$k] = $order->incometable->ordertable->toArray();
  415. // $this->TypeData['incomes'][$k] = $order->incometable->toArray();
  416. // }
  417. }
  418. }
  419. }
  420. }
  421. return $this->TypeData;
  422. }
  423. public static function getWithdrawByWithdrawSN($withdrawSN)
  424. {
  425. return self::uniacid()->where('withdraw_sn', $withdrawSN)->first();
  426. }
  427. public static function getBalanceWithdrawById($id)
  428. {
  429. return self::uniacid()->where('id', $id)
  430. ->with([
  431. 'hasOneMember' => function ($query) {
  432. return $query->select('uid', 'mobile', 'realname', 'nickname', 'avatar')
  433. ->with([
  434. 'yzMember' => function ($member) {
  435. return $member->select(
  436. 'member_id',
  437. 'group_id',
  438. 'alipayname',
  439. 'alipay',
  440. 'wechat',
  441. 'level_id'
  442. )
  443. ->with([
  444. 'group' => function ($group) {
  445. return $group->select('id', 'group_name');
  446. },
  447. 'level' => function ($level) {
  448. return $level->select('id', 'level', 'level_name');
  449. }
  450. ]);
  451. }
  452. ]);
  453. }
  454. ])
  455. ->with([
  456. 'bankCard' => function ($bank) {
  457. return $bank->select(
  458. 'member_id',
  459. 'bank_card',
  460. 'member_name',
  461. 'bank_card',
  462. 'bank_province',
  463. 'bank_city',
  464. 'bank_branch',
  465. 'bank_name'
  466. );
  467. }
  468. ])
  469. ->first();
  470. }
  471. public static function getWithdrawById($id)
  472. {
  473. $Model = self::where('id', $id);
  474. $Model->orWhere('withdraw_sn', $id);
  475. $Model->with([
  476. 'hasOneMember' => function ($query) {
  477. $query->select('uid', 'mobile', 'realname', 'nickname', 'avatar');
  478. }
  479. ]);
  480. // $Model->with(['hasOneAgent' => function ($query) {
  481. // $query->select('member_id', 'agent_level_id', 'commission_total');
  482. // }]);
  483. return $Model;
  484. }
  485. // public function hasOneAgent()
  486. // {
  487. // return $this->hasOne('Yunshop\Commission\models\Agents', 'member_id', 'member_id');
  488. // }
  489. public static function updatedWithdrawStatus($id, $updatedData)
  490. {
  491. return self::where('id', $id)
  492. ->orWhere('withdraw_sn', (string)$id)
  493. ->update($updatedData);
  494. }
  495. //统计当天的
  496. public static function successfulWithdrawals($pay_type, $start, $end)
  497. {
  498. return self::where([
  499. ['member_id', \YunShop::app()->getMemberId()],
  500. //['status','=',self::STATUS_PAY],
  501. ['pay_way', '=', $pay_type],
  502. ['created_at', '>=', $start],
  503. ['created_at', '<=', $end]
  504. ])->count();
  505. }
  506. public static function SuccessfulCashWithdrawal($pay_type, $start, $end)
  507. {
  508. return self::where([
  509. ['member_id', \YunShop::app()->getMemberId()],
  510. ['status', '=', self::STATUS_PAY],
  511. ['pay_way', '=', $pay_type],
  512. ['pay_at', '>=', $start],
  513. ['pay_at', '<=', $end]
  514. ])->sum('actual_amounts');
  515. }
  516. }