RecordsController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. <?php
  2. /****************************************************************
  3. * Author: libaojia
  4. * Date: 2017/11/14 上午10:22/2019-06-28 下午14:47
  5. * Email: livsyitian@163.com
  6. * QQ: 995265288
  7. * User: 芸众商城 www.yunzshop.com
  8. * Tool: Created by PhpStorm.
  9. ****************************************************************/
  10. namespace app\backend\modules\withdraw\controllers;
  11. use app\backend\models\Withdraw;
  12. use app\backend\modules\member\models\MemberBankCard;
  13. use app\backend\modules\member\models\MemberShopInfo;
  14. use app\common\components\BaseController;
  15. use app\common\helpers\PaginationHelper;
  16. use app\common\models\WithdrawMergeServicetaxRate;
  17. use app\common\services\ExportService;
  18. class RecordsController extends BaseController
  19. {
  20. /**
  21. * @var Withdraw
  22. */
  23. private $withdrawModel;
  24. public function __construct()
  25. {
  26. parent::__construct();
  27. $this->withdrawModel = Withdraw::records();
  28. }
  29. //全部记录
  30. public function index()
  31. {
  32. $this->searchRecords();
  33. return $this->isExport() ? $this->export() : $this->view();
  34. }
  35. //待审核记录
  36. public function initial()
  37. {
  38. $this->withdrawModel->initial();
  39. return $this->index();
  40. }
  41. //待打款记录
  42. public function audit()
  43. {
  44. $this->withdrawModel->audit();
  45. return $this->index();
  46. }
  47. //打款中记录
  48. public function paying()
  49. {
  50. $this->withdrawModel->paying();
  51. return $this->index();
  52. }
  53. //已打款记录
  54. public function payed()
  55. {
  56. $this->withdrawModel->payed();
  57. return $this->index();
  58. }
  59. //已驳回记录
  60. public function rebut()
  61. {
  62. $this->withdrawModel->rebut();
  63. return $this->index();
  64. }
  65. //已无效记录
  66. public function invalid()
  67. {
  68. $this->withdrawModel->invalid();
  69. return $this->index();
  70. }
  71. /**
  72. * 视图
  73. *
  74. * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
  75. */
  76. private function view()
  77. {
  78. return view('withdraw.records', $this->resultData());
  79. }
  80. /**
  81. * 导出 Excel
  82. */
  83. private function export()
  84. {
  85. return $this->_export();
  86. }
  87. /**
  88. * @return array
  89. */
  90. private function resultData()
  91. {
  92. $records = $this->withdrawModel->paginate();
  93. $page = PaginationHelper::show($records->total(), $records->currentPage(), $records->perPage());
  94. return [
  95. 'records' => $records,
  96. 'search' => $this->searchParams(),
  97. 'types' => Withdraw::getTypes(),
  98. 'page' => $page,
  99. ];
  100. }
  101. /**
  102. * 记录搜索
  103. */
  104. private function searchRecords()
  105. {
  106. $search = $this->searchParams();
  107. if ($search) {
  108. $this->withdrawModel->search($search);
  109. }
  110. $this->withdrawModel->orderBy('created_at', 'desc');
  111. }
  112. /**
  113. * @return array
  114. */
  115. private function searchParams()
  116. {
  117. $search = \YunShop::request()->search;
  118. return $search ?: [];
  119. }
  120. /**
  121. * @return bool
  122. */
  123. private function isExport()
  124. {
  125. $isExport = \YunShop::request()->export;
  126. return $isExport ? true : false;
  127. }
  128. /**
  129. * 导出Excel
  130. */
  131. private function _export()
  132. {
  133. $records = $this->withdrawModel;
  134. $export_page = request()->export_page ? request()->export_page : 1;
  135. $export_model = new ExportService($records, $export_page);
  136. $file_name = date('Ymdhis', time()) . '提现记录导出';
  137. $export_data[0] = [
  138. '提现编号',
  139. '会员ID',
  140. '粉丝',
  141. '姓名、手机',
  142. '收入类型',
  143. '提现方式',
  144. '申请金额',
  145. '手续费',
  146. '劳务税',
  147. '应打款金额',
  148. '申请时间',
  149. '审核时间',
  150. '打款时间',
  151. '到账时间',
  152. '自定义信息',
  153. '打款至',
  154. '打款微信号',
  155. '支付宝姓名',
  156. '支付宝账号',
  157. '开户行',
  158. '开户行省份',
  159. '开户行城市',
  160. '开户行支行',
  161. '银行卡信息',
  162. '开户人姓名',
  163. ];
  164. foreach ($export_model->builder_model as $key => $item)
  165. {
  166. $nickname = $item->hasOneMember->nickname;
  167. $realname = $item->hasOneMember->realname.'/'.$item->hasOneMember->mobile;
  168. $export_data[$key + 1] = [
  169. $item->withdraw_sn,
  170. $item->member_id,
  171. strpos($nickname,'=') === 0 ? ' ' . $nickname : $nickname,
  172. strpos($realname,'=') === 0 ? ' ' . $realname : $realname,
  173. $item->type_name,
  174. $item->pay_way_name,
  175. $item->amounts,
  176. $this->getEstimatePoundage($item),//$item->actual_poundage,
  177. ($item->type == 'balance'?0:$this->getEstimateServiceTax($item)),//$item->actual_servicetax,
  178. // $item->actual_amounts,
  179. $this->getActualAmount($item),
  180. $item->created_at->toDateTimeString(),
  181. $item->audit_at ? $item->audit_at->toDateTimeString() : '',
  182. $item->pay_at ? $item->pay_at->toDateTimeString() : '',
  183. $item->arrival_at ? $item->arrival_at->toDateTimeString() : '',
  184. $this->getCustomValue($item->member_id),
  185. ];
  186. switch ($item->manual_type) {
  187. case 2:
  188. $export_data[$key + 1][] = '微信';
  189. $export_data[$key + 1] = array_merge($export_data[$key + 1], $this->getMemberWeChat($item->member_id));
  190. break;
  191. case 3:
  192. $export_data[$key + 1][] = '支付宝';
  193. $export_data[$key + 1] = array_merge($export_data[$key + 1], $this->getMemberAlipay($item->member_id));
  194. break;
  195. default:
  196. $export_data[$key + 1][] = '银行卡';
  197. $export_data[$key + 1] = array_merge($export_data[$key + 1], $this->getMemberBankCard($item->member_id));
  198. break;
  199. }
  200. //判断字体,针对性的防止𠂆字,使xsl终止不完整的bug
  201. $zit = strpos($export_data[$key + 1][21],'𠂆');
  202. if ($zit) {
  203. $export_data[$key + 1][21] = '*';
  204. }
  205. }
  206. $export_model->export($file_name, $export_data, \Request::query('route'));
  207. }
  208. private function getMemberAlipay($member_id)
  209. {
  210. $yzMember = MemberShopInfo::select('alipayname','alipay')->where('member_id',$member_id)->first();
  211. return $yzMember ? [ '', $yzMember->alipayname ?: '', $yzMember->alipay ?: '' ] : ['', ''];
  212. }
  213. private function getMemberWeChat($member_id)
  214. {
  215. $yzMember = MemberShopInfo::select('wechat')->where('member_id',$member_id)->first();
  216. return $yzMember ? [ $yzMember->wechat ?: '' ] : [''];
  217. }
  218. private function getMemberBankCard($member_id)
  219. {
  220. $bankCard = MemberBankCard::where('member_id',$member_id)->first();
  221. if ($bankCard) {
  222. return [
  223. '', '', '',
  224. $bankCard->bank_name ?: '',
  225. $bankCard->bank_province ?: '',
  226. $bankCard->bank_city ?: '',
  227. $bankCard->bank_branch ?: '',
  228. $bankCard->bank_card ? $bankCard->bank_card . ",": '',
  229. $bankCard->member_name ?: ''
  230. ];
  231. }
  232. return ['','','','','','','','',''];
  233. }
  234. private function getEstimatePoundage($item)
  235. {
  236. if (!(float)$item->actual_poundage > 0 || is_null($item->actual_poundage)) {
  237. return bcdiv(bcmul($item->amounts, $item->poundage_rate, 4), 100, 2);
  238. }
  239. return $item->actual_poundage;
  240. }
  241. private function getEstimateServiceTax($withdraw)
  242. {
  243. // if (!(float)$item->actual_servicetax > 0 || is_null($item->actual_servicetax)) {
  244. // $poundage = $this->getEstimatePoundage($item);
  245. // $amount = bcsub($item->amounts, $poundage, 2);
  246. // return bcdiv(bcmul($amount, $item->servicetax_rate, 4), 100, 2);
  247. // }
  248. $withdraw->servicetax = $this->setWithdraw($withdraw)->servicetax;
  249. return $withdraw->servicetax;
  250. }
  251. private function getCustomValue($member_id)
  252. {
  253. $yzMember = MemberShopInfo::select('member_form')->where('member_id',$member_id)->first();
  254. $str = '';
  255. if($yzMember->member_form){
  256. foreach(json_decode($yzMember->member_form) as $value){
  257. $str .= '<'.$value->name.':'.$value->value.'> ';
  258. }
  259. }
  260. return $str;
  261. }
  262. public function getActualAmount($withdraw)
  263. {
  264. $withdraw_data = $this->setWithdraw($withdraw);
  265. if ($withdraw_data->type == 'balance') {//余额不减劳务税
  266. $withdraw_data->actual_amounts = bcsub($withdraw_data->amounts, $withdraw_data->poundage, 2);
  267. }
  268. // 暂时屏蔽, 等之后有误重新计算
  269. // else {
  270. // $withdraw_data->actual_amounts = bcsub(bcsub($withdraw_data->amounts, $withdraw_data->poundage, 2), $withdraw_data->servicetax, 2);
  271. // }
  272. return $withdraw_data->actual_amounts;
  273. }
  274. private function setWithdraw($withdraw)
  275. {
  276. if ($withdraw->status == 0) {
  277. $withdraw_set = \Setting::get('withdraw.income');
  278. if ($withdraw->pay_way == 'balance' && $withdraw_set['balance_special']) {
  279. $merge_percent = null;
  280. } else {
  281. $merge_percent = WithdrawMergeServicetaxRate::uniacid()->where('withdraw_id', $withdraw->id)->where('is_disabled', 0)->first();
  282. }
  283. if ($merge_percent) {
  284. $withdraw->servicetax_rate = $merge_percent->servicetax_rate;
  285. $base_amount = !$withdraw_set['service_tax_calculation'] ? bcsub($withdraw->amounts, $withdraw->poundage, 2) : $withdraw->amounts;
  286. $withdraw->servicetax = bcmul($base_amount, bcdiv($withdraw->servicetax_rate, 100, 4), 2);
  287. } elseif ($withdraw->pay_way != 'balance' || !$withdraw_set['balance_special']) {
  288. $base_amount = !$withdraw_set['service_tax_calculation'] ? bcsub($withdraw->amounts, $withdraw->poundage, 2) : $withdraw->amounts;
  289. $res = \app\common\services\finance\Withdraw::getWithdrawServicetaxPercent($base_amount,$withdraw);
  290. $withdraw->servicetax_rate = $res['servicetax_percent'];
  291. $withdraw->servicetax = $res['servicetax_amount'];
  292. }
  293. }
  294. return $withdraw;
  295. }
  296. }