PaymentReceiptService.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. <?php
  2. namespace App\Service;
  3. use App\Model\BasicType;
  4. use App\Model\Employee;
  5. use App\Model\PaymentReceipt;
  6. use App\Model\PaymentReceiptInfo;
  7. use App\Model\PurchaseOrder;
  8. use App\Model\ReturnExchangeOrder;
  9. use App\Model\SalesOrder;
  10. use Illuminate\Support\Facades\DB;
  11. class PaymentReceiptService extends Service
  12. {
  13. public function paymentReceiptGet($data,$user){
  14. $order_number = (new OrderNoService())->createOrderNumber(PaymentReceipt::prefix);
  15. if(! $order_number) return [false,'工单编号生成失败!'];
  16. return [true,['order_number' => $order_number]];
  17. }
  18. public function customerEdit($data,$user){
  19. list($status,$msg) = $this->customerRule($data,$user, false);
  20. if(!$status) return [$status,$msg];
  21. try {
  22. DB::beginTransaction();
  23. $model = PaymentReceipt::where('id',$data['id'])->first();
  24. // $model->data_order_no = $data['data_order_no'];
  25. // $model->data_type = $data['data_type'];
  26. $model->type = $data['type'];
  27. $model->account = $data['account'] ?? 0;
  28. $model->pay_way = $data['pay_way'] ?? 0;
  29. // $model->amount = $data['amount'] ?? 0;
  30. $model->mark = $data['mark'] ?? '';
  31. $model->payment_receipt_date = $data['payment_receipt_date'] ?? 0;
  32. $model->save();
  33. $time = time();
  34. $old = PaymentReceiptInfo::where('del_time',0)
  35. ->where('payment_receipt_id',$data['id'])
  36. ->select('file')
  37. ->get()->toArray();
  38. $old = array_column($old,'file');
  39. PaymentReceiptInfo::where('del_time',0)
  40. ->where('payment_receipt_id',$data['id'])
  41. ->update(['del_time' => $time]);
  42. $new = [];
  43. if(! empty($data['file'])){
  44. $insert = [];
  45. foreach ($data['file'] as $value){
  46. $insert[] = [
  47. 'payment_receipt_id' => $model->id,
  48. 'file' => $value['url'],
  49. 'type' => PaymentReceiptInfo::type_one,
  50. 'name' => $value['name'],
  51. 'crt_time' => $time,
  52. ];
  53. }
  54. $new[]= $value['url'];
  55. PaymentReceiptInfo::insert($insert);
  56. }
  57. if(! empty($data['employee_one'])){
  58. $insert = [];
  59. foreach ($data['employee_one'] as $value){
  60. $insert[] = [
  61. 'payment_receipt_id' => $model->id,
  62. 'data_id' => $value,
  63. 'type' => PaymentReceiptInfo::type_two,
  64. 'crt_time' => $time,
  65. ];
  66. }
  67. PaymentReceiptInfo::insert($insert);
  68. }
  69. if(! empty($data['amount_list'])){
  70. $insert = [];
  71. foreach ($data['amount_list'] as $value){
  72. $insert[] = [
  73. 'payment_receipt_id' => $model->id,
  74. 'data_type' => $data['type'],
  75. 'data_order_no' => $value['data_order_no'],
  76. 'data_order_type' => $data['data_type'],
  77. 'amount' => $value['amount'],
  78. 'type' => PaymentReceiptInfo::type_three,
  79. 'crt_time' => $time,
  80. ];
  81. }
  82. PaymentReceiptInfo::insert($insert);
  83. }
  84. DB::commit();
  85. }catch (\Exception $exception){
  86. DB::rollBack();
  87. return [false,$exception->getMessage()];
  88. }
  89. $this->delStorageFile($old, $new);
  90. return [true,''];
  91. }
  92. public function customerAdd($data,$user){
  93. list($status,$msg) = $this->customerRule($data,$user);
  94. if(!$status) return [$status,$msg];
  95. try {
  96. DB::beginTransaction();
  97. $model = new PaymentReceipt();
  98. $model->order_number = $data['order_number'];
  99. // $model->data_order_no = $data['data_order_no'];
  100. $model->data_type = $data['data_type'];
  101. $model->type = $data['type'];
  102. $model->account = $data['account'] ?? 0;
  103. $model->pay_way = $data['pay_way'] ?? 0;
  104. // $model->amount = $data['amount'] ?? 0;
  105. $model->mark = $data['mark'] ?? '';
  106. $model->crt_id = $user['id'];
  107. $model->depart_id = $data['depart_id'];
  108. $model->top_depart_id = $data['top_depart_id'];
  109. $model->payment_receipt_date = $data['payment_receipt_date'] ?? 0;
  110. $model->save();
  111. $time = time();
  112. if(! empty($data['file'])){
  113. $insert = [];
  114. foreach ($data['file'] as $value){
  115. $insert[] = [
  116. 'payment_receipt_id' => $model->id,
  117. 'file' => $value['url'],
  118. 'type' => PaymentReceiptInfo::type_one,
  119. 'name' => $value['name'],
  120. 'crt_time' => $time,
  121. ];
  122. }
  123. PaymentReceiptInfo::insert($insert);
  124. }
  125. if(! empty($data['employee_one'])){
  126. $insert = [];
  127. foreach ($data['employee_one'] as $value){
  128. $insert[] = [
  129. 'payment_receipt_id' => $model->id,
  130. 'data_id' => $value,
  131. 'type' => PaymentReceiptInfo::type_two,
  132. 'crt_time' => $time,
  133. ];
  134. }
  135. PaymentReceiptInfo::insert($insert);
  136. }
  137. if(! empty($data['amount_list'])){
  138. $insert = [];
  139. foreach ($data['amount_list'] as $value){
  140. $insert[] = [
  141. 'payment_receipt_id' => $model->id,
  142. 'data_type' => $data['type'],
  143. 'data_order_no' => $value['data_order_no'],
  144. 'data_order_type' => $data['data_type'],
  145. 'amount' => $value['amount'],
  146. 'type' => PaymentReceiptInfo::type_three,
  147. 'crt_time' => $time,
  148. ];
  149. }
  150. PaymentReceiptInfo::insert($insert);
  151. }
  152. DB::commit();
  153. }catch (\Exception $exception){
  154. DB::rollBack();
  155. return [false,$exception->getMessage()];
  156. }
  157. return [true,''];
  158. }
  159. public function customerDel($data){
  160. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  161. $booking = PaymentReceipt::where('del_time',0)->where('id',$data['id'])->first();
  162. if(empty($booking)) return [false,'收付款单不存在或已被删除'];
  163. $booking = $booking->toArray();
  164. if($booking['state'] != PaymentReceipt::STATE_ZERO) return [false,'请确认收付款单状态,删除失败'];
  165. try {
  166. DB::beginTransaction();
  167. $time = time();
  168. $old = PaymentReceiptInfo::where('del_time',0)
  169. ->where('payment_receipt_id',$data['id'])
  170. ->select('file')
  171. ->get()->toArray();
  172. $old = array_column($old,'file');
  173. PaymentReceipt::where('id',$data['id'])->update([
  174. 'del_time'=> $time
  175. ]);
  176. PaymentReceiptInfo::where('del_time',0)
  177. ->where('payment_receipt_id',$data['id'])
  178. ->update(['del_time' => $time]);
  179. DB::commit();
  180. }catch (\Exception $exception){
  181. DB::rollBack();
  182. return [false,$exception->getMessage()];
  183. }
  184. $this->delStorageFile($old);
  185. return [true,''];
  186. }
  187. public function customerDetail($data){
  188. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  189. $customer = PaymentReceipt::where('del_time',0)
  190. ->where('id',$data['id'])
  191. ->first();
  192. if(empty($customer)) return [false,'收付款记录不存在或已被删除'];
  193. $customer = $customer->toArray();
  194. $array = [
  195. $customer['account'],
  196. $customer['pay_way'],
  197. ];
  198. $basic_map = BasicType::whereIn('id',$array)
  199. ->pluck('title','id')
  200. ->toArray();
  201. $customer['account_title'] = $basic_map[$customer['account']] ?? "";
  202. $customer['pay_way_title'] = $basic_map[$customer['pay_way']] ?? "";
  203. $customer['state_title'] = PaymentReceipt::$name[$customer['state']] ?? "";
  204. $customer['type_title'] = PaymentReceipt::$model_type[$customer['type']] ?? "";
  205. $customer['data_type_title'] = PaymentReceipt::$data_type[$customer['data_type']] ?? "";
  206. $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('emp_name');
  207. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  208. $customer['payment_receipt_date'] = $customer['payment_receipt_date'] ? date("Y-m-d",$customer['payment_receipt_date']): '';
  209. $customer['receipt_amount'] = $customer['state'] == PaymentReceipt::STATE_TWO ? $customer['amount'] : 0;
  210. $file = PaymentReceiptInfo::where('del_time',0)
  211. ->where('payment_receipt_id',$data['id'])
  212. ->get()->toArray();
  213. $emp_id = [];
  214. $order_no = [];
  215. foreach ($file as $value){
  216. if(in_array($value['type'],PaymentReceiptInfo::$man)){
  217. $emp_id[] = $value['data_id'];
  218. }
  219. if(! empty($value['data_order_no'])) $order_no[] = $value['data_order_no'];
  220. }
  221. if($customer['data_type'] == PaymentReceipt::data_type_one){
  222. $order = SalesOrder::where('del_time',0)
  223. ->whereIn('order_number',$order_no)
  224. ->pluck('contract_fee','order_number')
  225. ->toArray();
  226. $getDifferentAmountALL = (new ReturnExchangeOrderService())->getDifferentAmountALL2(array_keys($order));
  227. }else{
  228. $order = PurchaseOrder::where('del_time',0)
  229. ->whereIn('order_number',$order_no)
  230. ->pluck('purchase_total','order_number')
  231. ->toArray();
  232. $getDifferentAmountALL = (new ReturnExchangeOrderService())->getDifferentAmountALL2(array_keys($order), ReturnExchangeOrder::Order_type2);
  233. }
  234. $infos = PaymentReceiptInfo::where('del_time',0)
  235. ->where('type',PaymentReceiptInfo::type_three)
  236. ->where('payment_receipt_id','<>',$customer['id'])
  237. ->get()->toArray();
  238. $infos_map = [];
  239. foreach ($infos as $value){
  240. $keys = $value['data_order_no'] . $value['data_type'];
  241. if(isset($infos_map[$keys])){
  242. $infos_map[$keys] += $value['amount'];
  243. }else{
  244. $infos_map[$keys] = $value['amount'];
  245. }
  246. }
  247. $emp_map = Employee::whereIn('id',array_unique($emp_id))
  248. ->where('del_time',0)
  249. ->pluck('emp_name','id')
  250. ->toArray();
  251. $customer['file'] = $customer['employee_one'] = $customer['amount_list'] = [];
  252. foreach ($file as $value){
  253. if($value['type'] == PaymentReceiptInfo::type_one){
  254. $tmp = [
  255. 'url' => $value['file'],
  256. 'name' => $value['name'],
  257. ];
  258. $customer['file'][] = $tmp;
  259. }elseif (in_array($value['type'],PaymentReceiptInfo::$man)){
  260. $tt = $emp_map[$value['data_id']] ?? '';
  261. if(! empty($tt)){
  262. $tmp = [
  263. 'id' => $value['data_id'],
  264. 'name' => $emp_map[$value['data_id']] ?? '',
  265. ];
  266. if($value['type'] == PaymentReceiptInfo::type_two){
  267. $customer['employee_one'][] = $tmp;
  268. }
  269. }
  270. }elseif ($value['type'] == PaymentReceiptInfo::type_three){
  271. $total = $has_amount = 0;
  272. $receipt_amount = $value['amount'];
  273. if($customer['type'] == PaymentReceipt::type_one){
  274. //坏账金额
  275. $bad_amount = $infos_map[$value['data_order_no'] . PaymentReceipt::type_four] ?? 0;
  276. //除了本单外的已收金额
  277. $has_amount = $infos_map[$value['data_order_no'] . PaymentReceipt::type_one] ?? 0;
  278. //总回款金额 = 合同金额 - 退换货金额 - 已收金额 - 坏账金额
  279. $total = ($order[$value['data_order_no']] ?? 0) - ($getDifferentAmountALL[$value['data_order_no']] ?? 0) - $bad_amount - $has_amount;
  280. }elseif($customer['type'] == PaymentReceipt::type_three){
  281. //已回款金额
  282. $one = $infos_map[$value['data_order_no'] . PaymentReceipt::type_one] ?? 0;
  283. //除了本单外的已红冲金额
  284. $has_amount = $infos_map[$value['data_order_no'] . PaymentReceipt::type_three] ?? 0;
  285. //总红冲金额 = 已回款金额 - 已红冲金额
  286. $total = $one - $has_amount;
  287. }elseif($customer['type'] == PaymentReceipt::type_four){
  288. //已回款金额
  289. $one = $infos_map[$value['data_order_no'] . PaymentReceipt::type_one] ?? 0;
  290. //已红冲金额
  291. $three = $infos_map[$value['data_order_no'] . PaymentReceipt::type_three] ?? 0;
  292. //除了本单外的已坏账金额
  293. $has_amount = $infos_map[$value['data_order_no'] . PaymentReceipt::type_four] ?? 0;
  294. //总坏账金额 = 合同金额 - 退换货金额 - 已收金额 + 已红冲金额 - 坏账金额
  295. $total = ($order[$value['data_order_no']] ?? 0) - ($getDifferentAmountALL[$value['data_order_no']] ?? 0) - $one + $three - $has_amount;
  296. }
  297. $tmp = [
  298. 'data_order_no' => $value['data_order_no'],
  299. 'amount' => $value['amount'],
  300. 'total_amount' => $total,
  301. 'has_amount' => $has_amount,
  302. 'receipt_amount' => $receipt_amount
  303. ];
  304. $customer['amount_list'][] = $tmp;
  305. }
  306. }
  307. return [true, $customer];
  308. }
  309. public function customerList($data,$user){
  310. $model = PaymentReceipt::Clear($user,$data);
  311. $model = $model->where('del_time',0)
  312. ->select('type','id','data_type','order_number','data_order_no','amount','account','pay_way','crt_id','crt_time','mark','state','payment_receipt_date')
  313. ->orderby('id', 'desc');
  314. if(isset($data['state'])) $model->where('state', $data['state']);
  315. if(! empty($data['data_order_no'])) {
  316. $info = PaymentReceiptInfo::where('del_time',0)
  317. ->where('type',PaymentReceiptInfo::type_three)
  318. ->where('data_order_no', 'LIKE', '%'.$data['data_order_no'].'%')
  319. ->select('payment_receipt_id')
  320. ->get()->toArray();
  321. $model->whereIn('id', array_unique(array_column($info,'payment_receipt_id')));
  322. }
  323. if(! empty($data['order_number'])) $model->where('order_number', 'LIKE', '%'.$data['order_number'].'%');
  324. if(! empty($data['data_type'])) $model->where('data_type', $data['data_type']);
  325. if(! empty($data['type'])) $model->where('type', $data['type']);
  326. if(! empty($data['account'])) $model->where('account',$data['account']);
  327. if(! empty($data['pay_way'])) $model->where('pay_way',$data['pay_way']);
  328. if(! empty($data['mark'])) $model->where('mark', 'LIKE', '%'.$data['mark'].'%');
  329. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  330. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  331. $model->where('crt_time','>=',$return[0]);
  332. $model->where('crt_time','<=',$return[1]);
  333. }
  334. if(! empty($data['payment_receipt_date'][0]) && ! empty($data['payment_receipt_date'][1])){
  335. $return = $this->changeDateToTimeStampAboutRange($data['payment_receipt_date']);
  336. $model->where('payment_receipt_date','>=',$return[0]);
  337. $model->where('payment_receipt_date','<=',$return[1]);
  338. }
  339. if(! empty($data['belong'])){
  340. $id = (new RangeService())->paymentReceiptSearch($data);
  341. $model->whereIn('id',$id);
  342. }
  343. $list = $this->limit($model,'',$data);
  344. $list = $this->fillData($list);
  345. return [true, $list];
  346. }
  347. public function customerRule(&$data, $user, $is_add = true){
  348. if(empty($data['order_number'])) return [false,'收付款单编号不能为空'];
  349. if(empty($data['data_type'])) return [false,'单号类型不能为空'];
  350. if(empty($data['type'])) return [false,'收付款类型不能为空'];
  351. if(empty($data['amount_list']) || ! is_array($data['amount_list'])) return [false,'关联单号与回款金额不能为空'];
  352. foreach ($data['amount_list'] as $value){
  353. if(empty($value['data_order_no'])) return [false,'关联单号不能为空'];
  354. if(empty($value['amount'])) return [false,'金额不能为空'];
  355. $res = $this->checkNumber($value['amount']);
  356. if(! $res) return [false, '金额请输入不超过两位小数并且大于0的数值'];
  357. }
  358. if(! empty($data['payment_receipt_date'])) $data['payment_receipt_date'] = $this->changeDateToDate($data['payment_receipt_date']);
  359. //所属部门 以及 顶级部门
  360. if(empty($data['depart_id'])) {
  361. $data['depart_id'] = $this->getDepart($user);
  362. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  363. }
  364. if($is_add){
  365. $bool = PaymentReceipt::where('del_time',0)
  366. ->where('order_number',$data['order_number'])
  367. ->exists();
  368. if($bool) return [false,'收付款单编号已存在,请重新获取'];
  369. }else{
  370. if(empty($data['id'])) return [false,'ID不能为空'];
  371. $booking = PaymentReceipt::where('del_time',0)->where('id',$data['id'])->first();
  372. if(empty($booking)) return [false,'收付款单不存在或已被删除'];
  373. $booking = $booking->toArray();
  374. if($booking['state'] != PaymentReceipt::STATE_ZERO) return [false,'请确认收付款单状态,编辑失败'];
  375. }
  376. return [true, ''];
  377. }
  378. public function fillData($data){
  379. if(empty($data['data'])) return $data;
  380. $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
  381. ->pluck('emp_name','id')
  382. ->toArray();
  383. $array = array_unique(array_merge_recursive(array_column($data['data'],'account'),array_column($data['data'],'pay_way')));
  384. $basic_map = BasicType::whereIn('id',$array)
  385. ->pluck('title','id')
  386. ->toArray();
  387. $map = [];
  388. $info = PaymentReceiptInfo::where('del_time',0)
  389. ->where('type',PaymentReceiptInfo::type_three)
  390. ->whereIn('payment_receipt_id', array_unique(array_column($data['data'],'id')))
  391. ->select('payment_receipt_id','data_order_no')
  392. ->get()->toArray();
  393. foreach ($info as $value){
  394. $map[$value['payment_receipt_id']][] = $value['data_order_no'];
  395. }
  396. foreach ($data['data'] as $key => $value){
  397. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  398. $data['data'][$key]['payment_receipt_date'] = $value['payment_receipt_date'] ? date('Y-m-d',$value['payment_receipt_date']) : '';
  399. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  400. $data['data'][$key]['state_title'] = PaymentReceipt::$name[$value['state']] ?? '';
  401. $data['data'][$key]['type_title'] = PaymentReceipt::$model_type[$value['type']] ?? '';
  402. $data['data'][$key]['data_type_title'] = PaymentReceipt::$data_type[$value['data_type']] ?? '';
  403. $data['data'][$key]['account_title'] = $basic_map[$value['account']] ?? '';
  404. $data['data'][$key]['pay_way_title'] = $basic_map[$value['pay_way']] ?? '';
  405. $data['data'][$key]['data_order_no'] = $map[$value['id']] ?? [];
  406. }
  407. return $data;
  408. }
  409. //详情里
  410. public function getPaymentReceiptDataList($data){
  411. $data['data_order_no'] = $data['order_number'];
  412. $info1 = PaymentReceiptInfo::where('del_time',0)
  413. ->where('data_order_no',$data['data_order_no'])
  414. ->where('type',PaymentReceiptInfo::type_three)
  415. ->get()->toArray();
  416. $map = [];
  417. foreach ($info1 as $value){
  418. $key = $value['payment_receipt_id'] . $value['data_type'];
  419. if(isset($map[$key])){
  420. $map[$key] += $value['amount'];
  421. }else{
  422. $map[$key] = $value['amount'];
  423. }
  424. }
  425. $order = PaymentReceipt::where('del_time',0)
  426. ->whereIn('id',array_column($info1,'payment_receipt_id'))
  427. ->get()->toArray();
  428. $emp_id = PaymentReceiptInfo::where('del_time',0)
  429. ->whereIn('payment_receipt_id',array_column($order,'id'))
  430. ->where('type',PaymentReceiptInfo::type_two)
  431. ->get()->toArray();
  432. $info = [];
  433. if(! empty($emp_id)){
  434. $emp_map = Employee::whereIn('id',array_unique(array_column($emp_id,'data_id')))
  435. ->pluck('emp_name','id')
  436. ->toArray();
  437. foreach ($emp_id as $value){
  438. $name = $emp_map[$value['data_id']] ?? "";
  439. if(isset($info[$value['data_id']])){
  440. $info[$value['data_id']] .= ',' . $name;
  441. }else{
  442. $info[$value['data_id']] = $name;
  443. }
  444. }
  445. }
  446. //四个金额类型
  447. $one = $two = $three = $four = 0;
  448. foreach ($order as $key => $value){
  449. $keys = $value['id'] . $value['type'];
  450. //归属人
  451. $order[$key]['belong'] = $info[$value['id']] ?? '';
  452. $order[$key]['state_title'] = PaymentReceipt::$name[$value['state']] ?? '';
  453. $order[$key]['type_title'] = PaymentReceipt::$model_type[$value['type']] ?? '';
  454. $order[$key]['payment_receipt_date'] = $value['payment_receipt_date'] ? date('Y-m-d',$value['payment_receipt_date']) : '';
  455. //统计每个收付款单 =》总收付款金额
  456. $amount = $map[$keys] ?? 0;
  457. $order[$key]['amount'] = $amount;
  458. if($value['type'] == PaymentReceipt::type_one){
  459. $one += $amount;
  460. }elseif($value['type'] == PaymentReceipt::type_two){
  461. $two += $amount;
  462. }elseif($value['type'] == PaymentReceipt::type_three){
  463. $three += $amount;
  464. }elseif($value['type'] == PaymentReceipt::type_four){
  465. $four += $amount;
  466. }
  467. }
  468. $return['receipt_amount'] = $one;
  469. $return['not_receipt_amount'] = 0;
  470. $return['red_amount'] = $three;
  471. $return['bad_amount'] = $four;
  472. $return['all_count'] = count($order);
  473. $return['list'] = $order;
  474. return $return;
  475. }
  476. //列表里 默认:(收款)
  477. public function getPaymentReceiptDataCountList($data){
  478. $data_order_no = [];
  479. foreach ($data as $value){
  480. $data_order_no[] = $value['order_number'];
  481. }
  482. if(empty($data_order_no)) return [];
  483. $order = PaymentReceiptInfo::where('del_time',0)
  484. ->where('type',PaymentReceiptInfo::type_three)
  485. ->whereIn('data_order_no',$data_order_no)
  486. ->get()->toArray();
  487. $return = [];
  488. foreach ($order as $value){
  489. $key = $value['data_order_no'] . $value['data_type'];
  490. if(isset($return[$key])){
  491. $return[$key] += $value['amount'];
  492. }else{
  493. $return[$key] = $value['amount'];
  494. }
  495. }
  496. return $return;
  497. }
  498. //废弃
  499. public function getPaymentReceiptDeatail($data){
  500. $customer = PaymentReceipt::where('del_time',0)
  501. ->where('crt_time',$data['crt_time'])
  502. ->where('id',$data['id'])
  503. ->first();
  504. if(empty($customer)) return [];
  505. $customer = $customer->toArray();
  506. $array = [
  507. $customer['account'],
  508. $customer['pay_way'],
  509. ];
  510. $basic_map = BasicType::whereIn('id',$array)
  511. ->pluck('title','id')
  512. ->toArray();
  513. $customer['account_title'] = $basic_map[$customer['account']] ?? "";
  514. $customer['pay_way_title'] = $basic_map[$customer['pay_way']] ?? "";
  515. $customer['state_title'] = PaymentReceipt::$name[$customer['state']] ?? "";
  516. $customer['type_title'] = PaymentReceipt::$model_type[$customer['type']] ?? "";
  517. $customer['data_type_title'] = PaymentReceipt::$data_type[$customer['data_type']] ?? "";
  518. $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('emp_name');
  519. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  520. $customer['payment_receipt_date'] = $customer['payment_receipt_date'] ? date("Y-m-d",$customer['payment_receipt_date']): '';
  521. $customer['receipt_amount'] = $customer['state'] == PaymentReceipt::STATE_TWO ? $customer['amount'] : 0;
  522. $file = PaymentReceiptInfo::where('del_time',0)
  523. ->where('payment_receipt_id',$data['id'])
  524. ->get()->toArray();
  525. $emp_id = [];
  526. foreach ($file as $value){
  527. if(in_array($value['type'],PaymentReceiptInfo::$man)){
  528. $emp_id[] = $value['data_id'];
  529. }
  530. }
  531. $emp_map = Employee::whereIn('id',array_unique($emp_id))
  532. ->where('del_time',0)
  533. ->pluck('emp_name','id')
  534. ->toArray();
  535. $customer['file'] = $customer['employee_one'] = [];
  536. foreach ($file as $value){
  537. if($value['type'] == PaymentReceiptInfo::type_one){
  538. $tmp = [
  539. 'url' => $value['file'],
  540. 'name' => $value['name'],
  541. ];
  542. $customer['file'][] = $tmp;
  543. }elseif (in_array($value['type'],PaymentReceiptInfo::$man)){
  544. $tt = $emp_map[$value['data_id']] ?? '';
  545. if(! empty($tt)){
  546. $tmp = [
  547. 'id' => $value['data_id'],
  548. 'name' => $emp_map[$value['data_id']] ?? '',
  549. ];
  550. if($value['type'] == PaymentReceiptInfo::type_two){
  551. $customer['employee_one'][] = $tmp;
  552. }
  553. }
  554. }
  555. }
  556. return $customer;
  557. }
  558. public function maked(){
  559. $payment = PaymentReceipt::where('del_time',0)
  560. ->where('data_order_no','<>','')
  561. ->where('amount','>',0)
  562. ->get()->toArray();
  563. $insert = [];
  564. foreach ($payment as $value){
  565. $insert[] = [
  566. 'payment_receipt_id' => $value['id'],
  567. 'crt_time' => $value['crt_time'],
  568. 'type' => PaymentReceiptInfo::type_three,
  569. 'data_order_no' => $value['data_order_no'],
  570. 'amount' => $value['amount'],
  571. 'data_order_type' => $value['type'],
  572. ];
  573. }
  574. if(! empty($insert)) PaymentReceiptInfo::insert($insert);
  575. }
  576. }