ShopAccount.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 山西牛酷信息科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com.cn
  8. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用。
  9. * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
  10. * =========================================================
  11. */
  12. namespace app\model\shop;
  13. use app\model\system\Config;
  14. use app\model\BaseModel;
  15. use app\model\system\Cron;
  16. use Carbon\Carbon;
  17. /**
  18. * 店铺账户(无缓存)
  19. */
  20. class ShopAccount extends BaseModel
  21. {
  22. public $from_type = [
  23. 'order' => [
  24. 'type_name' => '店铺结算',
  25. 'type_url' => '',
  26. ],
  27. 'withdraw' => [
  28. 'type_name' => '提现',
  29. 'type_url' => '',
  30. ],
  31. ];
  32. /**************************************************************店铺账户****************************************************************/
  33. /**
  34. * 添加店铺账户数据
  35. * @param int $site_id
  36. * @param int $account_type 账户类型 默认account
  37. * @param float $account_data
  38. * @param string $relate_url
  39. * @param string $remark
  40. */
  41. public function addShopAccount($site_id, $account_type = 'account', $account_data, $from_type, $relate_tag, $remark)
  42. {
  43. $account_no = $this->getWithdrawNo();
  44. $data = array(
  45. 'site_id' => $site_id,
  46. 'account_no' => $account_no,
  47. 'account_type' => $account_type,
  48. 'account_data' => $account_data,
  49. 'from_type' => $from_type,
  50. 'type_name' => $this->from_type[$from_type]['type_name'],
  51. 'relate_tag' => $relate_tag,
  52. 'create_time' => time(),
  53. 'remark' => $remark
  54. );
  55. $shop_account = model('shop')->getInfo([
  56. 'site_id' => $site_id
  57. ], $account_type);
  58. $account_new_data = (float) $shop_account[ $account_type ] + (float) $account_data;
  59. if ((float) $account_new_data < 0) {
  60. return $this->error('', 'RESULT_ERROR');
  61. }
  62. $res = model('shop_account')->add($data);
  63. $res = model('shop')->update([
  64. $account_type => $account_new_data
  65. ], [
  66. 'site_id' => $site_id
  67. ]);
  68. event("AddShopAccount", $data);
  69. return $this->success($res);
  70. }
  71. /**
  72. * 获取账户列表
  73. * @param array $condition
  74. * @param string $field
  75. * @param string $order
  76. * @param string $limit
  77. */
  78. public function getAccountList($condition = [], $field = 'site_id,type,money,order_id,text,create_time', $order = '', $limit = null)
  79. {
  80. $list = model('shop_account')->getList($condition, $field, $order, '', '', '', $limit);
  81. return $this->success($list);
  82. }
  83. /**
  84. * 获取账户分页列表
  85. * @param array $condition
  86. * @param number $page
  87. * @param string $page_size
  88. * @param string $order
  89. * @param string $field
  90. */
  91. public function getAccountPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*')
  92. {
  93. $list = model('shop_account')->pageList($condition, $field, $order, $page, $page_size);
  94. return $this->success($list);
  95. }
  96. /**************************************************************店铺账户结束*************************************************************/
  97. /**************************************************************店铺提现转账*************************************************************/
  98. /**
  99. * 添加店铺转账
  100. * @param unknown $data
  101. */
  102. public function addShopWithdraw($data)
  103. {
  104. $res = model("shop_withdraw")->add($data);
  105. if($res)
  106. {
  107. $this->addShopAccount($data['site_id'], 'account', $data['money']*(-1), 'withdraw', $res, "店铺提现,账单编号:".$data['withdraw_no']);
  108. }
  109. return $this->success($res);
  110. }
  111. /**
  112. * 编辑店铺转账
  113. * @param unknown $data
  114. * @param unknown $condition
  115. */
  116. public function editShopWithdraw($data, $condition)
  117. {
  118. $res = model('shop_withdraw')->update($data, $condition);
  119. return $this->success($res);
  120. }
  121. /**
  122. * 获取提现申请流水号
  123. */
  124. public function getWithdrawNo()
  125. {
  126. return date('YmdHi').rand(1111,9999);
  127. }
  128. /**
  129. * 店铺申请转账
  130. * @param unknown $site_id
  131. * @param unknown $money
  132. */
  133. public function applyWithdraw($site_id, $money)
  134. {
  135. //查询店铺信息
  136. $shop = new Shop();
  137. $shop_info = $shop->getShopInfo([['site_id', '=', $site_id]], 'site_name,account,account_withdraw_apply');
  138. //查询店铺认证信息
  139. $shop_cert_info = $shop->getShopCert([['site_id', '=', $site_id]], 'contacts_name,contacts_mobile,bank_type, settlement_bank_account_name, settlement_bank_account_number, settlement_bank_name, settlement_bank_address');
  140. if($shop_cert_info['data']['settlement_bank_account_number'] == ''){
  141. return $this->error("", "请先添加结算账户");
  142. }
  143. //开始记录申请
  144. if(($shop_info['data']['account'] - $shop_info['data']['account_withdraw_apply']) < $money)
  145. {
  146. return $this->error("", "SHOP_APPLY_MONEY_NOT_ENOUGH");
  147. }
  148. $withdraw_no = $this->getWithdrawNo();
  149. model("shop_withdraw")->startTrans();
  150. try{
  151. $data = [
  152. 'withdraw_no' => $withdraw_no,
  153. 'site_id' => $site_id,
  154. 'site_name' => $shop_info['data']['site_name'],
  155. 'name' => $shop_cert_info['data']['contacts_name'],
  156. 'mobile' => $shop_cert_info['data']['contacts_mobile'],
  157. 'bank_type' => $shop_cert_info['data']['bank_type'],
  158. 'settlement_bank_account_name' => $shop_cert_info['data']['settlement_bank_account_name'],
  159. 'settlement_bank_account_number' => $shop_cert_info['data']['settlement_bank_account_number'],
  160. 'settlement_bank_name' => $shop_cert_info['data']['settlement_bank_name'],
  161. 'settlement_bank_address' => $shop_cert_info['data']['settlement_bank_address'],
  162. 'money' => $money,
  163. 'apply_time' => time(),
  164. ];
  165. model("shop_withdraw")->add($data);
  166. model("shop")->setInc([ [ 'site_id', '=', $site_id ] ], 'account_withdraw_apply',$money);
  167. // $res = $this->addShopWithdraw($data);
  168. model("shop_withdraw")->commit();
  169. return $this->success();
  170. }catch(\Exception $e)
  171. {
  172. model("shop_withdraw")->rollback();
  173. return $this->error('', $e->getMessage());
  174. }
  175. }
  176. /**
  177. * 审核通过
  178. * @param unknown $apply_ids
  179. */
  180. public function applyPass($apply_ids)
  181. {
  182. $res = model('shop_withdraw')->update(['status' => 1], [['id', 'in', $apply_ids], ['status', '=', 0]]);
  183. return $this->success($res);
  184. }
  185. /**
  186. * 审核拒绝
  187. * @param unknown $apply_id
  188. */
  189. public function applyReject($apply_id)
  190. {
  191. $res = model('shop_withdraw')->update(['status' => -1], [['id', '=', $apply_id], ['status', '=', 0]]);
  192. if($res)
  193. {
  194. $apply_info = model('shop_withdraw')->getInfo([['id', '=', $apply_id]], 'site_id,money,withdraw_no');
  195. model('shop')->setDec([ [ 'site_id', '=', $apply_info['site_id'] ] ], 'account_withdraw_apply',$apply_info['money']);
  196. }
  197. return $this->success($res);
  198. }
  199. /**
  200. * 转账数据
  201. * @param unknown $apply_ids
  202. */
  203. public function applyPay($apply_ids)
  204. {
  205. $res = model('shop_withdraw')->update(['status' => 2, 'payment_time' => time()], [['id', 'in', $apply_ids], ['status', '=', 1]]);
  206. return $this->success($res);
  207. }
  208. /**
  209. * 商家转账
  210. * @param array $condition
  211. * @param $data
  212. */
  213. public function shopWithdrawPass($id,$data)
  214. {
  215. $data['status'] = 2;
  216. $data['payment_time'] = time();
  217. model('shop_withdraw')->startTrans();
  218. try{
  219. model('shop_withdraw')->update($data,[ ['id','=',$id] ]);
  220. $apply_info = model('shop_withdraw')->getInfo([['id', '=', $id]], 'site_id,money,withdraw_no');
  221. //减少提现中金额
  222. model('shop')->setDec([ [ 'site_id', '=', $apply_info['site_id'] ] ], 'account_withdraw_apply',$apply_info['money']);
  223. //增加已提现金额
  224. model('shop')->setInc([ [ 'site_id', '=', $apply_info['site_id'] ] ], 'account_withdraw',$apply_info['money']);
  225. //增加流水
  226. $this->addShopAccount($apply_info['site_id'], 'account', $apply_info['money']*(-1), 'withdraw', $id, "店铺提现,提现账单编号:".$apply_info['withdraw_no']);
  227. model("shop_withdraw")->commit();
  228. return $this->success();
  229. }catch(\Exception $e)
  230. {
  231. model("shop_withdraw")->rollback();
  232. return $this->error('', $e->getMessage());
  233. }
  234. }
  235. /**
  236. * 获取店铺提现
  237. * @param array $condition
  238. * @param string $field
  239. */
  240. public function getShopWithdrawInfo($condition = [], $field = '*')
  241. {
  242. $info = model('shop_withdraw')->getInfo($condition, $field);
  243. return $this->success($info);
  244. }
  245. /**
  246. * 获取店铺转账列表
  247. * @param array $condition
  248. * @param string $field
  249. * @param string $order
  250. * @param string $limit
  251. */
  252. public function getShopWithdrawList($condition = [], $field = '*', $order = '', $limit = null)
  253. {
  254. $list = model('shop_withdraw')->getList($condition, $field, $order, '', '', '', $limit);
  255. return $this->success($list);
  256. }
  257. /**
  258. * 获取店铺转账分页列表
  259. * @param array $condition
  260. * @param number $page
  261. * @param string $page_size
  262. * @param string $order
  263. * @param string $field
  264. */
  265. public function getShopWithdrawPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*')
  266. {
  267. $list = model('shop_withdraw')->pageList($condition, $field, $order, $page, $page_size);
  268. return $this->success($list);
  269. }
  270. /**
  271. * 获取商家转账设置
  272. */
  273. public function getShopWithdrawConfig()
  274. {
  275. $config = new Config();
  276. $res = $config->getConfig([['site_id', '=', 0], ['app_module', '=', 'admin'], ['config_key', '=', 'SHOP_WITHDRAW']]);
  277. if(empty($res['data']['value']))
  278. {
  279. //默认数据管理
  280. $res['data']['value'] = [
  281. 'is_period_settlement' => 1, //是否账期转账
  282. 'period_type' => 3, //转账周期类型1.天 2. 周 3. 月
  283. 'min_withdraw' => 0, //最低提现金额,对手动申请提现有效
  284. 'max_withdraw' => 0, //最高提现金额 , 对手动申请提现有效
  285. 'withdraw_rate' => 0, //提现或者转账手续费,对整体有效
  286. 'id_experience' => 0,
  287. 'expire_time' => 14,
  288. 'group_id' => 0
  289. ];
  290. }
  291. return $res;
  292. }
  293. /**
  294. * 设置商家转账设置
  295. */
  296. public function setShopWithdrawConfig($data)
  297. {
  298. $config = new Config();
  299. $res = $config->setConfig($data, '商家转账设置', 1, [['site_id', '=', 0], ['app_module', '=', 'admin'], ['config_key', '=', 'SHOP_WITHDRAW']]);
  300. $cron = new Cron();
  301. switch($data['period_type'])
  302. {
  303. case 1://天
  304. $date = strtotime(date('Y-m-d 00:00:00'));
  305. $execute_time = strtotime('+1day',$date);
  306. break;
  307. case 2://周
  308. $execute_time = Carbon::parse('next monday')->timestamp;
  309. break;
  310. case 3://月
  311. $execute_time = Carbon::now()->addMonth()->firstOfMonth()->timestamp;
  312. break;
  313. }
  314. $cron->deleteCron([ [ 'event', '=', 'ShopWithdrawPeriodCalc' ] ]);
  315. $cron->addCron('2','1','店铺周期结算','ShopWithdrawPeriodCalc',$execute_time,'0',$data['period_type']);
  316. return $res;
  317. }
  318. /**************************************************************店铺提现转账结束**********************************************************/
  319. /**************************************************************店铺提现转账周期结算**********************************************************/
  320. /**
  321. * 店铺周期结算
  322. */
  323. public function shopWithdrawPeriodCalc()
  324. {
  325. //查询设置结算周期
  326. $config = $this->getShopWithdrawConfig();
  327. if($config['data']['value']['is_period_settlement'] != 1)
  328. {
  329. return $this->error();
  330. }
  331. switch ($config['data']['value']['period_type'])
  332. {
  333. case 3:
  334. $period_name = date('Y-m-d')."月结";
  335. break;
  336. case 2:
  337. $period_name = date('Y-m-d')."周结";
  338. break;
  339. case 1:
  340. $period_name = date('Y-m-d')."日结";
  341. break;
  342. }
  343. model("shop_withdraw_period")->startTrans();
  344. try{
  345. $period_data = [
  346. 'remark' => $period_name,
  347. 'end_time' => time(),
  348. 'period_type' => $config['data']['value']['period_type']
  349. ];
  350. $period_id = model("shop_withdraw_period")->add($period_data);
  351. $field = 'ns.site_id, ns.site_name, ns.is_own, ns.account, ns.account_withdraw, nsc.bank_type, nsc.settlement_bank_account_name, nsc.settlement_bank_account_number, nsc.settlement_bank_name, nsc.settlement_bank_address, nsc.contacts_name, nsc.contacts_mobile';
  352. $alias = 'ns';
  353. $join = [
  354. [
  355. 'shop_cert nsc',
  356. ' ns.cert_id = nsc.cert_id',
  357. 'left'
  358. ],
  359. ];
  360. $shop_list = model("shop")->getList([], $field, '', $alias, $join);
  361. $cache = 1111;
  362. $money = 0;
  363. $shop_count = 0;
  364. foreach ($shop_list as $k => $v)
  365. {
  366. $data = [
  367. 'withdraw_no' => date('YmdHi').$cache,
  368. 'site_id' => $v['site_id'],
  369. 'site_name' => $v['site_name'],
  370. 'name' => $v['contacts_name'] == null ? '' : $v['contacts_name'],
  371. 'mobile' => $v['contacts_mobile'] == null ? '' : $v['contacts_mobile'],
  372. 'bank_type' => $v['bank_type'] == null ? 0 : $v['bank_type'],
  373. 'settlement_bank_account_name' => $v['settlement_bank_account_name'] == null ? '' : $v['settlement_bank_account_name'],
  374. 'settlement_bank_account_number' => $v['settlement_bank_account_number'] == null ? '' : $v['settlement_bank_account_number'],
  375. 'settlement_bank_name' => $v['settlement_bank_name'] == null ? '' : $v['settlement_bank_name'],
  376. 'settlement_bank_address' => $v['settlement_bank_address'] == null ? '' : $v['settlement_bank_address'],
  377. 'money' => $v['account'] == null ? '' : $v['account'],
  378. 'apply_time' => time(),
  379. 'status' => 1,
  380. 'is_period' => 1,
  381. 'period_id' => $period_id,
  382. 'period_name' => date('Y-m-d')."结算"
  383. ];
  384. $cache += 1;
  385. $shop_count++;
  386. $money += $v['account'];
  387. $this->addShopWithdraw($data);
  388. }
  389. //添加周期
  390. model("shop_withdraw_period")->update(['money' => $money, 'shop_num' => $shop_count], [['period_id', '=', $period_id]]);
  391. model("shop_withdraw_period")->commit();
  392. return $this->success();
  393. }catch(\Exception $e)
  394. {
  395. model("shop_withdraw_period")->rollback();
  396. return $this->success('', $e->getMessage());
  397. }
  398. }
  399. /**
  400. * 获取店铺转账周期结算分页列表
  401. * @param array $condition
  402. * @param number $page
  403. * @param string $page_size
  404. * @param string $order
  405. * @param string $field
  406. */
  407. public function getShopWithdrawPeriodPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*')
  408. {
  409. $list = model('shop_withdraw_period')->pageList($condition, $field, $order, $page, $page_size);
  410. return $this->success($list);
  411. }
  412. }