PaymentReceiptService.php 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  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\PurchaseOrderSpecial;
  9. use App\Model\ReturnExchangeOrder;
  10. use App\Model\SalesOrder;
  11. use Carbon\Carbon;
  12. use Illuminate\Database\Eloquent\Builder;
  13. use Illuminate\Support\Facades\DB;
  14. class PaymentReceiptService extends Service
  15. {
  16. public function paymentReceiptGet($data,$user){
  17. $order_number = (new OrderNoService())->createOrderNumber(PaymentReceipt::prefix);
  18. if(! $order_number) return [false,'工单编号生成失败!'];
  19. return [true,['order_number' => $order_number]];
  20. }
  21. public function customerEdit($data,$user){
  22. list($status,$msg) = $this->customerRule($data,$user, false);
  23. if(!$status) return [$status,$msg];
  24. try {
  25. DB::beginTransaction();
  26. $model = PaymentReceipt::where('id',$data['id'])->first();
  27. // $model->data_order_no = $data['data_order_no'];
  28. // $model->data_type = $data['data_type'];
  29. $model->type = $data['type'];
  30. $model->account = $data['account'] ?? 0;
  31. $model->pay_way = $data['pay_way'] ?? 0;
  32. // $model->amount = $data['amount'] ?? 0;
  33. $model->mark = $data['mark'] ?? '';
  34. $model->payment_receipt_date = $data['payment_receipt_date'] ?? 0;
  35. $model->save();
  36. $time = time();
  37. $old = PaymentReceiptInfo::where('del_time',0)
  38. ->where('payment_receipt_id',$data['id'])
  39. ->select('file')
  40. ->get()->toArray();
  41. $old = array_column($old,'file');
  42. PaymentReceiptInfo::where('del_time',0)
  43. ->where('payment_receipt_id',$data['id'])
  44. ->update(['del_time' => $time]);
  45. $new = [];
  46. if(! empty($data['file'])){
  47. $insert = [];
  48. foreach ($data['file'] as $value){
  49. $insert[] = [
  50. 'payment_receipt_id' => $model->id,
  51. 'file' => $value['url'],
  52. 'type' => PaymentReceiptInfo::type_one,
  53. 'name' => $value['name'],
  54. 'crt_time' => $time,
  55. ];
  56. if(in_array($value['url'], $old)) {
  57. foreach ($old as $o_k => $o_v){
  58. if($o_v == $value['url']) unset($old[$o_k]);
  59. }
  60. }else{
  61. $new[] = $value['url'];
  62. }
  63. }
  64. PaymentReceiptInfo::insert($insert);
  65. }
  66. if(! empty($data['employee_one'])){
  67. $insert = [];
  68. foreach ($data['employee_one'] as $value){
  69. $insert[] = [
  70. 'payment_receipt_id' => $model->id,
  71. 'data_id' => $value,
  72. 'type' => PaymentReceiptInfo::type_two,
  73. 'crt_time' => $time,
  74. ];
  75. }
  76. PaymentReceiptInfo::insert($insert);
  77. }
  78. if(! empty($data['amount_list'])){
  79. $insert = [];
  80. foreach ($data['amount_list'] as $value){
  81. $insert[] = [
  82. 'payment_receipt_id' => $model->id,
  83. 'data_type' => $data['type'],
  84. 'data_order_no' => $value['data_order_no'],
  85. 'data_order_type' => $data['data_type'],
  86. 'amount' => $value['amount'],
  87. 'type' => PaymentReceiptInfo::type_three,
  88. 'crt_time' => $time,
  89. ];
  90. }
  91. PaymentReceiptInfo::insert($insert);
  92. }
  93. DB::commit();
  94. }catch (\Exception $exception){
  95. DB::rollBack();
  96. return [false,$exception->getMessage()];
  97. }
  98. if(! empty($data['check'])) {
  99. list($status,$msg) = (new CheckService())->checkAll([
  100. "id" => $data['id'],
  101. "order_number" => $data['order_number'],
  102. "opt_case" => CheckService::ten,
  103. "menu_id" => $data['menu_id']
  104. ],$user);
  105. // if(! $status) return [true, '保存成功,收付款确认失败,异常信息:' . $msg];
  106. }
  107. return [true, ['file' => ['new' => $new, 'old' => $old]]];
  108. }
  109. public function customerAdd($data,$user){
  110. list($status,$msg) = $this->customerRule($data,$user);
  111. if(!$status) return [$status,$msg];
  112. try {
  113. DB::beginTransaction();
  114. $model = new PaymentReceipt();
  115. $model->order_number = $data['order_number'];
  116. // $model->data_order_no = $data['data_order_no'];
  117. $model->data_type = $data['data_type'];
  118. $model->type = $data['type'];
  119. $model->account = $data['account'] ?? 0;
  120. $model->pay_way = $data['pay_way'] ?? 0;
  121. // $model->amount = $data['amount'] ?? 0;
  122. $model->mark = $data['mark'] ?? '';
  123. $model->crt_id = $user['id'];
  124. $model->depart_id = $data['depart_id'];
  125. $model->top_depart_id = $data['top_depart_id'];
  126. $model->payment_receipt_date = $data['payment_receipt_date'] ?? 0;
  127. $model->save();
  128. $time = time();
  129. $new = [];
  130. if(! empty($data['file'])){
  131. $insert = [];
  132. foreach ($data['file'] as $value){
  133. $insert[] = [
  134. 'payment_receipt_id' => $model->id,
  135. 'file' => $value['url'],
  136. 'type' => PaymentReceiptInfo::type_one,
  137. 'name' => $value['name'],
  138. 'crt_time' => $time,
  139. ];
  140. if(! empty($value['url'])) $new[] = $value['url'];
  141. }
  142. PaymentReceiptInfo::insert($insert);
  143. }
  144. if(! empty($data['employee_one'])){
  145. $insert = [];
  146. foreach ($data['employee_one'] as $value){
  147. $insert[] = [
  148. 'payment_receipt_id' => $model->id,
  149. 'data_id' => $value,
  150. 'type' => PaymentReceiptInfo::type_two,
  151. 'crt_time' => $time,
  152. ];
  153. }
  154. PaymentReceiptInfo::insert($insert);
  155. }
  156. if(! empty($data['amount_list'])){
  157. $insert = [];
  158. foreach ($data['amount_list'] as $value){
  159. $insert[] = [
  160. 'payment_receipt_id' => $model->id,
  161. 'data_type' => $data['type'],
  162. 'data_order_no' => $value['data_order_no'],
  163. 'data_order_type' => $data['data_type'],
  164. 'amount' => $value['amount'],
  165. 'type' => PaymentReceiptInfo::type_three,
  166. 'crt_time' => $time,
  167. ];
  168. }
  169. PaymentReceiptInfo::insert($insert);
  170. }
  171. DB::commit();
  172. }catch (\Exception $exception){
  173. DB::rollBack();
  174. if (str_contains($exception->getMessage(), '1062') || str_contains($exception->getMessage(), 'Duplicate entry')) {
  175. return [false, '收付款单编号已存在,请重新获取!'];
  176. }
  177. return [false,$exception->getMessage()];
  178. }
  179. if(! empty($data['check'])) {
  180. list($status,$msg) = (new CheckService())->checkAll([
  181. "id" => $model->id,
  182. "order_number" => $data['order_number'],
  183. "opt_case" => CheckService::ten,
  184. "menu_id" => $data['menu_id']
  185. ],$user);
  186. // if(! $status) return [true, '保存成功,收付款确认失败,异常信息:' . $msg];
  187. }
  188. return [true, ['file' => ['new' => $new]]];
  189. }
  190. public function customerDel($data){
  191. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  192. $booking = PaymentReceipt::where('del_time',0)->where('id',$data['id'])->first();
  193. if(empty($booking)) return [false,'收付款单不存在或已被删除'];
  194. $booking = $booking->toArray();
  195. if($booking['state'] > PaymentReceipt::STATE_ZERO) return [false,'请确认收付款单状态,删除失败'];
  196. try {
  197. DB::beginTransaction();
  198. $time = time();
  199. $old = PaymentReceiptInfo::where('del_time',0)
  200. ->where('payment_receipt_id',$data['id'])
  201. ->select('file')
  202. ->get()->toArray();
  203. $old = array_column($old,'file');
  204. PaymentReceipt::where('id',$data['id'])->update([
  205. 'del_time'=> $time
  206. ]);
  207. PaymentReceiptInfo::where('del_time',0)
  208. ->where('payment_receipt_id',$data['id'])
  209. ->update(['del_time' => $time]);
  210. DB::commit();
  211. }catch (\Exception $exception){
  212. DB::rollBack();
  213. return [false,$exception->getMessage()];
  214. }
  215. return [true, ['file' => ['old' => $old]]];
  216. }
  217. public function customerDetail($data){
  218. if(empty($data['id']) && empty($data['order_number'])) return [false,'请选择数据!'];
  219. if(! empty($data['id'])){
  220. $customer = PaymentReceipt::where('del_time',0)
  221. ->where('id',$data['id'])
  222. ->first();
  223. }else{
  224. $customer = PaymentReceipt::where('del_time',0)
  225. ->where('order_number',$data['order_number'])
  226. ->first();
  227. $data['id'] = empty($customer->id) ? 0 : $customer->id;
  228. }
  229. if(empty($customer)) return [false,'收付款记录不存在或已被删除'];
  230. $customer = $customer->toArray();
  231. $array = [
  232. $customer['account'],
  233. $customer['pay_way'],
  234. ];
  235. $basic_map = BasicType::whereIn('id',$array)
  236. ->pluck('title','id')
  237. ->toArray();
  238. $customer['account_title'] = $basic_map[$customer['account']] ?? "";
  239. $customer['pay_way_title'] = $basic_map[$customer['pay_way']] ?? "";
  240. $customer['type_title'] = PaymentReceipt::$model_type[$customer['type']] ?? "";
  241. $customer['data_type_title'] = PaymentReceipt::$data_type[$customer['data_type']] ?? "";
  242. $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('emp_name');
  243. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  244. $customer['payment_receipt_date'] = $customer['payment_receipt_date'] ? date("Y-m-d",$customer['payment_receipt_date']): '';
  245. //订单状态数据组织
  246. $state_array = $this->getStateMake([$customer]);
  247. $customer['state_title'] = $this->makeState($customer,$state_array);
  248. $file = PaymentReceiptInfo::where('del_time',0)
  249. ->whereIn('type',[PaymentReceiptInfo::type_one,PaymentReceiptInfo::type_two])
  250. ->where('payment_receipt_id',$data['id'])
  251. ->get()->toArray();
  252. $emp_id = [];
  253. foreach ($file as $value){
  254. if(in_array($value['type'],PaymentReceiptInfo::$man)){
  255. $emp_id[] = $value['data_id'];
  256. }
  257. }
  258. $emp_map = Employee::whereIn('id',array_unique($emp_id))
  259. ->pluck('emp_name','id')
  260. ->toArray();
  261. $customer['file'] = $customer['employee_one'] = [];
  262. $fileUploadService = new FileUploadService();
  263. foreach ($file as $value){
  264. if($value['type'] == PaymentReceiptInfo::type_one){
  265. $tmp = [
  266. 'url' => $value['file'],
  267. 'name' => $value['name'],
  268. 'show_url' => $fileUploadService->getFileShow($value['file']),
  269. ];
  270. $customer['file'][] = $tmp;
  271. }elseif (in_array($value['type'],PaymentReceiptInfo::$man)){
  272. $tt = $emp_map[$value['data_id']] ?? '';
  273. if(! empty($tt)){
  274. $tmp = [
  275. 'id' => $value['data_id'],
  276. 'name' => $emp_map[$value['data_id']] ?? '',
  277. ];
  278. if($value['type'] == PaymentReceiptInfo::type_two){
  279. $customer['employee_one'][] = $tmp;
  280. }
  281. }
  282. }
  283. }
  284. //组织金额
  285. $customer['amount_list'] = $this->makeDetail($customer);
  286. return [true, $customer];
  287. }
  288. public function makeDetail($data){
  289. $return = [];
  290. $info = PaymentReceiptInfo::where('del_time',0)
  291. ->where('type',PaymentReceiptInfo::type_three)
  292. ->where('payment_receipt_id', $data['id'])
  293. ->select('payment_receipt_id','data_order_type','data_type','amount','data_order_no')
  294. ->get()->toArray();
  295. $order_no = array_column($info,'data_order_no');
  296. //退换货金额
  297. if($data['type'] == PaymentReceipt::type_one){
  298. if($data['data_type'] == PaymentReceipt::data_type_one){
  299. $order = SalesOrder::where('del_time',0)
  300. ->whereIn('order_number',$order_no)
  301. ->pluck('contract_fee','order_number')
  302. ->toArray();
  303. $getDifferentAmountALL = (new ReturnExchangeOrderService())->getDifferentAmountALL2(array_keys($order));
  304. }else{
  305. $order = PurchaseOrder::where('del_time',0)
  306. ->whereIn('order_number',$order_no)
  307. ->pluck('purchase_total','order_number')
  308. ->toArray();
  309. $getDifferentAmountALL = (new ReturnExchangeOrderService())->getDifferentAmountALL2(array_keys($order), ReturnExchangeOrder::Order_type2);
  310. }
  311. }
  312. //除自己这个收付款单外 所有订单已填的金额
  313. $infos = PaymentReceiptInfo::where('del_time',0)
  314. ->where('type',PaymentReceiptInfo::type_three)
  315. ->where('data_type',$data['type'])
  316. ->where('payment_receipt_id','<>',$data['id'])
  317. ->whereIn('data_order_no',$order_no)
  318. ->get()->toArray();
  319. $infos_map = [];
  320. foreach ($infos as $value){
  321. if(isset($infos_map[$value['data_order_no']])){
  322. $infos_map[$value['data_order_no']] += $value['amount'];
  323. }else{
  324. $infos_map[$value['data_order_no']] = $value['amount'];
  325. }
  326. }
  327. //收款金额(审核后)
  328. $info_one_array = [];
  329. if($data['type'] == PaymentReceipt::type_three){
  330. $info_one = PaymentReceiptInfo::from('payment_receipt_info as a')
  331. ->leftJoin('payment_receipt as b','b.id','a.payment_receipt_id')
  332. ->where('b.state',PaymentReceipt::STATE_TWO)
  333. ->where('b.del_time',0)
  334. ->where('a.del_time',0)
  335. ->where('a.type',PaymentReceiptInfo::type_three)
  336. ->where('a.data_order_type',PaymentReceipt::data_type_one)
  337. ->whereIn('a.data_order_no',$order_no)
  338. ->where('a.data_type',PaymentReceipt::type_one)
  339. ->select('a.data_order_no','a.amount')
  340. ->get()->toArray();
  341. $info_one_array = [];
  342. foreach ($info_one as $value){
  343. if(isset($info_one_array[$value['data_order_no']])){
  344. $info_one_array[$value['data_order_no']] += $value['amount'];
  345. }else{
  346. $info_one_array[$value['data_order_no']] = $value['amount'];
  347. }
  348. }
  349. }
  350. foreach ($info as $value){
  351. $total = $tmp_receipt = 0;
  352. if($data['type'] == PaymentReceipt::type_one){
  353. //收款 = 总金额 - 已收 - 退货退款
  354. //总金额
  355. $total_amount = $order[$value['data_order_no']] ?? 0;
  356. //已收
  357. $tmp_receipt = $infos_map[$value['data_order_no']] ?? 0;
  358. //退货退款
  359. $tmp_return = $getDifferentAmountALL[$value['data_order_no']] ?? 0;
  360. //收款
  361. $total = bcsub(bcsub($total_amount, $tmp_receipt,2), $tmp_return, 2);
  362. }elseif($data['type'] == PaymentReceipt::type_three){
  363. //红冲 = 审核后的收款金额 - 已红冲
  364. //总金额 收款金额(审核后)
  365. $total_amount = $info_one_array[$value['data_order_no']] ?? 0;
  366. //已红冲
  367. $tmp_receipt = $infos_map[$value['data_order_no']] ?? 0;
  368. //红冲
  369. $total = bcsub($total_amount, $tmp_receipt, 2);
  370. }
  371. $tmp = [
  372. 'data_order_no' => $value['data_order_no'],
  373. 'total_amount' => $total,//总共能填的金额
  374. 'has_amount' => $tmp_receipt,//已经填的金额
  375. 'amount' => $value['amount'],//本单金额
  376. 'receipt_amount' => $value['amount'],//本单金额
  377. ];
  378. $return[] = $tmp;
  379. }
  380. return $return;
  381. }
  382. public function customerCommon($data,$user,$field = []){
  383. if(empty($field)){
  384. $field = ['type','id','data_type','order_number','data_order_no','amount','account','pay_way','crt_id','crt_time','mark','state','payment_receipt_date'];
  385. }
  386. $model = PaymentReceipt::Clear($user,$data);
  387. $model = $model->where('del_time',0)
  388. ->select($field)
  389. ->orderby('id', 'desc');
  390. if(isset($data['state'])) $model->where('state', $data['state']);
  391. if(! empty($data['data_order_no'])) {
  392. $id = $this->searchForDataOrder($data['data_order_no']);
  393. $model->whereIn('id', $id);
  394. }
  395. if(! empty($data['order_number'])) $model->where('order_number', 'LIKE', '%'.$data['order_number'].'%');
  396. if(! empty($data['data_type'])) $model->where('data_type', $data['data_type']);
  397. if(! empty($data['type'])) $model->where('type', $data['type']);
  398. if(! empty($data['account'])) $model->where('account',$data['account']);
  399. if(! empty($data['account_title'])){
  400. $id = (new BasicTypeService())->basicTypeSearch($data['account_title']);
  401. $model->whereIn('account',$id);
  402. }
  403. if(! empty($data['pay_way'])) $model->where('pay_way',$data['pay_way']);
  404. if(! empty($data['mark'])) $model->where('mark', 'LIKE', '%'.$data['mark'].'%');
  405. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  406. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  407. $model->where('crt_time','>=',$return[0]);
  408. $model->where('crt_time','<=',$return[1]);
  409. }
  410. if(! empty($data['payment_receipt_date'][0]) && ! empty($data['payment_receipt_date'][1])){
  411. $return = $this->changeDateToTimeStampAboutRange($data['payment_receipt_date']);
  412. $model->where('payment_receipt_date','>=',$return[0]);
  413. $model->where('payment_receipt_date','<=',$return[1]);
  414. }
  415. if(! empty($data['belong'])){
  416. $id = (new RangeService())->paymentReceiptSearch($data);
  417. $model->whereIn('id',$id);
  418. }
  419. if(! empty($data['smart_search'])){
  420. $id = $this->searchForDataOrder($data['smart_search']);
  421. $model->where(function (Builder $query) use ($data, $id) {
  422. $query->where('order_number', 'LIKE', '%'.$data['smart_search'].'%')
  423. ->OrWhereIn('id',$id);
  424. });
  425. }
  426. if(! empty($data['wx_crt_time'][0]) && ! empty($data['wx_crt_time'][1])) {
  427. $model->where('crt_time','>=',$data['wx_crt_time'][0]);
  428. $model->where('crt_time','<=',$data['wx_crt_time'][1]);
  429. }
  430. return $model;
  431. }
  432. public function searchForDataOrder($message){
  433. $info = PaymentReceiptInfo::where('del_time',0)
  434. ->where('type',PaymentReceiptInfo::type_three)
  435. ->where('data_order_no', 'LIKE', '%'.$message.'%')
  436. ->select('payment_receipt_id')
  437. ->get()->toArray();
  438. return array_unique(array_column($info,'payment_receipt_id'));
  439. }
  440. public function customerList($data,$user){
  441. $model = $this->customerCommon($data, $user);
  442. $list = $this->limit($model,'',$data);
  443. $list = $this->fillData($list, $data);
  444. $count = $this->countData("crt_time", $data, $user);
  445. $list['today'] = $count[0] ?? 0;
  446. $list['yesterday'] = $count[1] ?? 0;
  447. return [true, $list];
  448. }
  449. public function customerRule(&$data, $user, $is_add = true){
  450. if(empty($data['order_number'])) return [false,'收付款单编号不能为空'];
  451. if(empty($data['data_type'])) return [false,'关联单号类型不能为空'];
  452. if(! isset(PaymentReceipt::$data_type[$data['data_type']])) return [false, '关联单号类型不存在'];
  453. if(empty($data['type'])) return [false,'收付款类型不能为空'];
  454. if(! isset(PaymentReceipt::$model_type[$data['type']])) return [false, '收付款类型不存在'];
  455. if(empty($data['amount_list']) || ! is_array($data['amount_list'])) return [false,'关联单号与金额不能为空'];
  456. foreach ($data['amount_list'] as $value){
  457. if(empty($value['data_order_no'])) return [false,'关联单号不能为空'];
  458. if(empty($value['amount'])) return [false,'金额不能为空'];
  459. if(floatval($value['amount']) == 0) return [false, '金额请输入大于0的数值'];
  460. $res = $this->checkNumber($value['amount']);
  461. if(! $res) return [false, '金额请输入不超过两位小数并且大于0的数值'];
  462. }
  463. if(! empty($data['payment_receipt_date'])) $data['payment_receipt_date'] = $this->changeDateToDate($data['payment_receipt_date']);
  464. //所属部门 以及 顶级部门
  465. if(empty($data['depart_id'])) {
  466. $data['depart_id'] = $this->getDepart($user);
  467. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  468. }
  469. if($is_add){
  470. $bool = PaymentReceipt::where('del_time',0)
  471. ->where('order_number',$data['order_number'])
  472. ->exists();
  473. if($bool) return [false,'收付款单编号已存在,请重新获取'];
  474. }else{
  475. if(empty($data['id'])) return [false,'ID不能为空'];
  476. $booking = PaymentReceipt::where('del_time',0)->where('id',$data['id'])->first();
  477. if(empty($booking)) return [false,'收付款单不存在或已被删除'];
  478. $booking = $booking->toArray();
  479. if(! in_array($booking['state'],[PaymentReceipt::STATE_ZERO,PaymentReceipt::State_minus_one])) return [false,'请确认收付款单状态,编辑失败'];
  480. //订单编辑提交限制
  481. $current_top_depart_id = $this->getMyTopDepart($user);
  482. list($status, $msg) = $this->returnOrderEditErrorCommon($current_top_depart_id, $booking['top_depart_id']);
  483. if(! $status) return [false, $msg];
  484. }
  485. list($status,$msg) = $this->checkRule($data);
  486. if(! $status) return [false, $msg];
  487. return [true, ''];
  488. }
  489. public function checkRule($data){
  490. $payment_receipt_id = $data['id'] ?? 0;
  491. $map = [];
  492. foreach ($data['amount_list'] as $value){
  493. $map[$value['data_order_no']] = $value['amount'];
  494. }
  495. $search = array_keys($map);
  496. if($data['data_type'] == PaymentReceipt::data_type_one){ //合同逻辑
  497. //总金额
  498. $result = SalesOrder::where('del_time',0)
  499. ->whereIn('order_number',$search)
  500. ->select('id','order_number','contract_fee as total_amount')
  501. ->get()->toArray();
  502. $result_map = array_column($result,null,'order_number');
  503. if(count($search) != count($result)) {
  504. foreach ($search as $value){
  505. if(! isset($result_map[$value])) return [false, $value . '不存在或已被删除'];
  506. }
  507. }
  508. $result_map = array_column($result,'order_number','id');
  509. //收付款金额
  510. $info = PaymentReceiptInfo::where('del_time',0)
  511. ->where('type',PaymentReceiptInfo::type_three)
  512. ->where('data_order_type',PaymentReceipt::data_type_one)
  513. ->whereIn('data_order_no',$search)
  514. // ->where('data_type',$data['type'])
  515. ->select('payment_receipt_id','data_order_no','amount','data_type')
  516. ->get()->toArray();
  517. $info_array = $red = [];
  518. foreach ($info as $value){
  519. if($payment_receipt_id > 0 && $value['payment_receipt_id'] == $payment_receipt_id) continue;
  520. if($value['data_type'] == PaymentReceipt::type_one){
  521. if(isset($info_array[$value['data_order_no']])){
  522. $info_array[$value['data_order_no']] += $value['amount'];
  523. }else{
  524. $info_array[$value['data_order_no']] = $value['amount'];
  525. }
  526. }elseif($value['data_type'] == PaymentReceipt::type_three){
  527. if(isset($red[$value['data_order_no']])){
  528. $red[$value['data_order_no']] += $value['amount'];
  529. }else{
  530. $red[$value['data_order_no']] = $value['amount'];
  531. }
  532. }
  533. }
  534. //退货退款金额
  535. $return_exchange = (new ReturnExchangeOrderService())->getDifferentAmountALL(array_column($result,'id'));
  536. if($data['type'] == PaymentReceipt::type_one){
  537. // order_number => amount
  538. $return_exchange_array = [];
  539. foreach ($result_map as $key => $value){
  540. if(isset($return_exchange[$key])) $return_exchange_array[$value] = $return_exchange[$key];
  541. }
  542. foreach ($result as $value){
  543. //收款 = 总金额 - (已收 - 红冲) - 退货退款
  544. //$max = $value['total_amount'] - $tmp_receipt - $tmp_return;
  545. $tmp_receipt = $info_array[$value['order_number']] ?? 0;
  546. $tmp_red = $red[$value['order_number']] ?? 0;
  547. $tmp_return = $return_exchange_array[$value['order_number']] ?? 0;
  548. $max = bcsub(bcsub($value['total_amount'], bcsub($tmp_receipt,$tmp_red,2),2), $tmp_return, 2);
  549. if($map[$value['order_number']] > $max) return [false, $value['order_number'] . '的金额填写上限是' . $max];
  550. }
  551. }elseif($data['type'] == PaymentReceipt::type_three){
  552. //收款金额(审核后)
  553. $info_one = PaymentReceiptInfo::from('payment_receipt_info as a')
  554. ->leftJoin('payment_receipt as b','b.id','a.payment_receipt_id')
  555. ->where('b.state',PaymentReceipt::STATE_TWO)
  556. ->where('b.del_time',0)
  557. ->where('a.del_time',0)
  558. ->where('a.type',PaymentReceiptInfo::type_three)
  559. ->where('a.data_order_type',PaymentReceipt::data_type_one)
  560. ->whereIn('a.data_order_no',$search)
  561. ->where('a.data_type',PaymentReceipt::type_one)
  562. ->select('a.payment_receipt_id','a.data_order_no','a.amount')
  563. ->get()->toArray();
  564. $info_one_array = [];
  565. foreach ($info_one as $value){
  566. if(isset($info_one_array[$value['data_order_no']])){
  567. $info_one_array[$value['data_order_no']] += $value['amount'];
  568. }else{
  569. $info_one_array[$value['data_order_no']] = $value['amount'];
  570. }
  571. }
  572. foreach ($result as $value){
  573. //红冲 = 审核后的收款单金额 - 已红冲
  574. $tmp_receipt = $info_one_array[$value['order_number']] ?? 0;
  575. $tmp_return = $red[$value['order_number']] ?? 0;
  576. $max = bcsub($tmp_receipt, $tmp_return, 2);
  577. if($map[$value['order_number']] > $max) return [false, $value['order_number'] . '的金额填写上限是' . $max];
  578. }
  579. }
  580. }elseif ($data['data_type'] == PaymentReceipt::data_type_two){//采购逻辑
  581. //总金额
  582. $result = PurchaseOrder::where('del_time',0)
  583. ->whereIn('order_number',$search)
  584. ->select('id','order_number','purchase_total as total_amount')
  585. ->get()->toArray();
  586. $result_map = array_column($result,null,'order_number');
  587. if(count($search) != count($result)) {
  588. foreach ($search as $value){
  589. if(! isset($result_map[$value])) return [false, $value . '不存在或已被删除'];
  590. }
  591. }
  592. $result_map = array_column($result,'order_number','id');
  593. //收付款金额
  594. $info = PaymentReceiptInfo::where('del_time',0)
  595. ->where('type',PaymentReceiptInfo::type_three)
  596. ->where('data_order_type',PaymentReceipt::data_type_two)
  597. ->whereIn('data_order_no',$search)
  598. // ->where('data_type',$data['type'])
  599. ->select('payment_receipt_id','data_order_no','amount','data_type')
  600. ->get()->toArray();
  601. $info_array = $red = [];
  602. foreach ($info as $value){
  603. if($payment_receipt_id > 0 && $value['payment_receipt_id'] == $payment_receipt_id) continue;
  604. if($value['data_type'] == PaymentReceipt::type_one){
  605. if(isset($info_array[$value['data_order_no']])){
  606. $info_array[$value['data_order_no']] += $value['amount'];
  607. }else{
  608. $info_array[$value['data_order_no']] = $value['amount'];
  609. }
  610. }elseif($value['data_type'] == PaymentReceipt::type_three){
  611. if(isset($red[$value['data_order_no']])){
  612. $red[$value['data_order_no']] += $value['amount'];
  613. }else{
  614. $red[$value['data_order_no']] = $value['amount'];
  615. }
  616. }
  617. }
  618. //退货退款金额
  619. $return_exchange = (new ReturnExchangeOrderService())->getDifferentAmountALL(array_column($result,'id'),1);
  620. if($data['type'] == PaymentReceipt::type_one){
  621. $return_exchange_array = [];
  622. foreach ($result_map as $key => $value){
  623. // order_number => amount
  624. if(isset($return_exchange[$key])) $return_exchange_array[$value] = $return_exchange[$key];
  625. }
  626. foreach ($result as $value){
  627. //收款 = 总金额 - (已收 - 红冲) - 退货退款
  628. //$max = $value['total_amount'] - $tmp_receipt - $tmp_return;
  629. $tmp_receipt = $info_array[$value['order_number']] ?? 0;
  630. $tmp_red = $red[$value['order_number']] ?? 0;
  631. $tmp_return = $return_exchange_array[$value['order_number']] ?? 0;
  632. $max = bcsub(bcsub($value['total_amount'], bcsub($tmp_receipt,$tmp_red,2),2), $tmp_return, 2);
  633. if($map[$value['order_number']] > $max) return [false, $value['order_number'] . '的金额填写上限是' . $max];
  634. }
  635. }elseif($data['type'] == PaymentReceipt::type_three){
  636. //收款金额(审核后)
  637. $info_one = PaymentReceiptInfo::from('payment_receipt_info as a')
  638. ->leftJoin('payment_receipt as b','b.id','a.payment_receipt_id')
  639. ->where('b.state',PaymentReceipt::STATE_TWO)
  640. ->where('b.del_time',0)
  641. ->where('a.del_time',0)
  642. ->where('a.type',PaymentReceiptInfo::type_three)
  643. ->where('a.data_order_type',PaymentReceipt::data_type_one)
  644. ->whereIn('a.data_order_no',$search)
  645. ->where('a.data_type',PaymentReceipt::type_one)
  646. ->select('a.payment_receipt_id','a.data_order_no','a.amount')
  647. ->get()->toArray();
  648. $info_one_array = [];
  649. foreach ($info_one as $value){
  650. if(isset($info_one_array[$value['data_order_no']])){
  651. $info_one_array[$value['data_order_no']] += $value['amount'];
  652. }else{
  653. $info_one_array[$value['data_order_no']] = $value['amount'];
  654. }
  655. }
  656. foreach ($result as $value){
  657. //红冲 = 审核后的收款单金额 - 已红冲
  658. $tmp_receipt = $info_one_array[$value['order_number']] ?? 0;
  659. $tmp_return = $red[$value['order_number']] ?? 0;
  660. $max = bcsub($tmp_receipt, $tmp_return, 2);
  661. if($map[$value['order_number']] > $max) return [false, $value['order_number'] . '的金额填写上限是' . $max];
  662. }
  663. }
  664. }elseif ($data['data_type'] == PaymentReceipt::data_type_three){//虚拟采购
  665. //总金额
  666. $result = PurchaseOrderSpecial::where('del_time',0)
  667. ->whereIn('order_number',$search)
  668. ->select('id','order_number','purchase_total as total_amount','other_fee','sales_order_id')
  669. ->get()->toArray();
  670. $result_map = array_column($result,null,'order_number');
  671. if(count($search) != count($result)) {
  672. foreach ($search as $value){
  673. if(! isset($result_map[$value])) return [false, $value . '不存在或已被删除'];
  674. }
  675. }
  676. $result_map = array_column($result,'order_number','sales_order_id');
  677. //收付款金额
  678. $info = PaymentReceiptInfo::where('del_time',0)
  679. ->where('type',PaymentReceiptInfo::type_three)
  680. ->where('data_order_type',PaymentReceipt::data_type_three)
  681. ->whereIn('data_order_no',$search)
  682. // ->where('data_type',$data['type'])
  683. ->select('payment_receipt_id','data_order_no','amount','data_type')
  684. ->get()->toArray();
  685. $info_array = $red = [];
  686. foreach ($info as $value){
  687. if($payment_receipt_id > 0 && $value['payment_receipt_id'] == $payment_receipt_id) continue;
  688. if($value['data_type'] == PaymentReceipt::type_one){
  689. if(isset($info_array[$value['data_order_no']])){
  690. $info_array[$value['data_order_no']] += $value['amount'];
  691. }else{
  692. $info_array[$value['data_order_no']] = $value['amount'];
  693. }
  694. }elseif($value['data_type'] == PaymentReceipt::type_three){
  695. if(isset($red[$value['data_order_no']])){
  696. $red[$value['data_order_no']] += $value['amount'];
  697. }else{
  698. $red[$value['data_order_no']] = $value['amount'];
  699. }
  700. }
  701. }
  702. //退货退款金额
  703. $return_exchange = (new ReturnExchangeOrderService())->getDifferentAmountALL(array_column($result,'sales_order_id'));
  704. if($data['type'] == PaymentReceipt::type_one){
  705. // order_number => amount
  706. $return_exchange_array = [];
  707. foreach ($result_map as $key => $value){
  708. if(isset($return_exchange[$key])) $return_exchange_array[$value] = $return_exchange[$key];
  709. }
  710. foreach ($result as $value){
  711. //收款 = 总金额(订单金额 + 其他费用) - (已收 - 红冲) - 退换货
  712. $tmp_receipt = $info_array[$value['order_number']] ?? 0;
  713. $tmp_red = $red[$value['order_number']] ?? 0;
  714. $total = bcadd($value['total_amount'],$value['other_fee'],2);
  715. $tmp_return = $return_exchange_array[$value['order_number']] ?? 0;
  716. $max = bcsub($total, bcsub($tmp_receipt,$tmp_red,2), 2);
  717. $max = bcsub($max, $tmp_return, 2);
  718. if($map[$value['order_number']] > $max) return [false, $value['order_number'] . '的金额填写上限是' . $max];
  719. }
  720. }elseif($data['type'] == PaymentReceipt::type_three){
  721. //收款金额(审核后)
  722. $info_one = PaymentReceiptInfo::from('payment_receipt_info as a')
  723. ->leftJoin('payment_receipt as b','b.id','a.payment_receipt_id')
  724. ->where('b.state',PaymentReceipt::STATE_TWO)
  725. ->where('b.del_time',0)
  726. ->where('a.del_time',0)
  727. ->where('a.type',PaymentReceiptInfo::type_three)
  728. ->where('a.data_order_type',PaymentReceipt::data_type_three)
  729. ->whereIn('a.data_order_no',$search)
  730. ->where('a.data_type',PaymentReceipt::type_one)
  731. ->select('a.payment_receipt_id','a.data_order_no','a.amount')
  732. ->get()->toArray();
  733. $info_one_array = [];
  734. foreach ($info_one as $value){
  735. if(isset($info_one_array[$value['data_order_no']])){
  736. $info_one_array[$value['data_order_no']] += $value['amount'];
  737. }else{
  738. $info_one_array[$value['data_order_no']] = $value['amount'];
  739. }
  740. }
  741. foreach ($result as $value){
  742. //红冲 = 审核后的收款单金额 - 已红冲
  743. $tmp_receipt = $info_one_array[$value['order_number']] ?? 0;
  744. $tmp_return = $red[$value['order_number']] ?? 0;
  745. $max = bcsub($tmp_receipt, $tmp_return, 2);
  746. if($map[$value['order_number']] > $max) return [false, $value['order_number'] . '的金额填写上限是' . $max];
  747. }
  748. }
  749. }
  750. return [true, ''];
  751. }
  752. public function fillData($data, $ergs){
  753. if(empty($data['data'])) return $data;
  754. $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
  755. ->pluck('emp_name','id')
  756. ->toArray();
  757. $array = array_unique(array_merge_recursive(array_column($data['data'],'account'),array_column($data['data'],'pay_way')));
  758. $basic_map = BasicType::whereIn('id',$array)
  759. ->pluck('title','id')
  760. ->toArray();
  761. $map = [];
  762. $info = PaymentReceiptInfo::where('del_time',0)
  763. ->where('type',PaymentReceiptInfo::type_three)
  764. ->whereIn('payment_receipt_id', array_unique(array_column($data['data'],'id')))
  765. ->select('payment_receipt_id','data_order_no','data_order_type')
  766. ->get()->toArray();
  767. $order_no = [];
  768. foreach ($info as $value){
  769. $map[$value['payment_receipt_id']][] = $value['data_order_no'];
  770. if($value['data_order_type'] == 1) $order_no[] = $value['data_order_no'];
  771. }
  772. //订单状态数据组织
  773. $state_array = $this->getStateMake($data['data']);
  774. $pt = [];
  775. if(! empty($ergs['pt']) && ! empty($order_no)) $pt = $this->getOtherMessage($order_no);
  776. foreach ($data['data'] as $key => $value){
  777. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  778. $data['data'][$key]['payment_receipt_date'] = $value['payment_receipt_date'] ? date('Y-m-d',$value['payment_receipt_date']) : '';
  779. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  780. $data['data'][$key]['state_title'] = $this->makeState($data['data'][$key], $state_array);
  781. $data['data'][$key]['type_title'] = PaymentReceipt::$model_type[$value['type']] ?? '';
  782. $data['data'][$key]['data_type_title'] = PaymentReceipt::$data_type[$value['data_type']] ?? '';
  783. $data['data'][$key]['account_title'] = $basic_map[$value['account']] ?? '';
  784. $data['data'][$key]['pay_way_title'] = $basic_map[$value['pay_way']] ?? '';
  785. $t_no = $map[$value['id']] ?? [];
  786. $data['data'][$key]['data_order_no'] = $t_no;
  787. $t = [];
  788. if(! empty($t_no)){
  789. foreach ($t_no as $no){
  790. $tmp = $pt[$no] ?? [];
  791. if(! empty($tmp)){
  792. $t[] = $tmp;
  793. }
  794. }
  795. }
  796. $data['data'][$key]['pt_info'] = $t;
  797. }
  798. return $data;
  799. }
  800. private function getOtherMessage($order_no){
  801. $list = SalesOrder::whereIn('order_number',$order_no)
  802. ->select('order_number','plat_type','plat_order')
  803. ->get()->toArray();
  804. $basic_map = BasicType::whereIn('id',array_unique(array_column($list,'plat_type')))
  805. ->pluck('title','id')
  806. ->toArray();
  807. $map = [];
  808. foreach ($list as $value){
  809. $map[$value['order_number']] = [
  810. 'plat_type' => $basic_map[$value['plat_type']] ?? "",
  811. 'plat_order' => $value['plat_order']
  812. ];
  813. }
  814. return $map;
  815. }
  816. public function countData($column = "", $data, $user){
  817. if(empty($column) || empty($data['from_wx'])) return [0, 0];
  818. // 获取今天的开始和结束时间戳
  819. $todayStart = Carbon::today()->startOfDay()->timestamp;
  820. $todayEnd = Carbon::today()->endOfDay()->timestamp;
  821. // 获取昨天的开始和结束时间戳
  822. $yesterdayStart = Carbon::yesterday()->startOfDay()->timestamp;
  823. $yesterdayEnd = Carbon::yesterday()->endOfDay()->timestamp;
  824. $data['wx_' . $column] = [$todayStart, $todayEnd];
  825. $model = $this->customerCommon($data, $user);
  826. // 查询今天的数据条数
  827. $todayCount = $model->count();
  828. $data['wx_' . $column] = [$yesterdayStart, $yesterdayEnd];
  829. $model = $this->customerCommon($data, $user);
  830. // 查询昨天的数据条数
  831. $yesterdayCount = $model->count();
  832. return [$todayCount, $yesterdayCount];
  833. }
  834. public function getStateMake($data){
  835. if(empty($data)) return [];
  836. $order_no = [];
  837. foreach ($data as $value){
  838. if(! in_array($value['state'], [PaymentReceipt::State_minus_one,PaymentReceipt::STATE_ONE])) continue;
  839. $order_no[] = $value['order_number'];
  840. }
  841. return (new OaService())->getOaTeamDetailList($order_no);
  842. }
  843. public function makeState($value, $state_array){
  844. if(! empty($state_array[$value['order_number']])){
  845. $return = $state_array[$value['order_number']];
  846. if($value['state'] == PaymentReceipt::State_minus_one){
  847. $state = "驳回:" . $return;
  848. }else{
  849. $state = "待" . $return . "审核";
  850. }
  851. }elseif($value['state'] == PaymentReceipt::STATE_ZERO){
  852. $state = "待" . $value['crt_name'] . "提交";
  853. }else{
  854. $state = PaymentReceipt::$name[$value['state']] ?? '';
  855. }
  856. return $state;
  857. }
  858. //详情里
  859. public function getPaymentReceiptDataList($data,$type){
  860. $data['data_order_no'] = $data['order_number'];
  861. $info_array = PaymentReceiptInfo::from('payment_receipt_info as a')
  862. ->leftJoin('payment_receipt as b','b.id','a.payment_receipt_id')
  863. ->where('b.del_time',0)
  864. ->where('a.del_time',0)
  865. ->where('a.type',PaymentReceiptInfo::type_three)
  866. ->where('a.data_order_type',$type)
  867. ->where('a.data_order_no',$data['data_order_no'])
  868. ->select('a.payment_receipt_id','a.data_order_no','a.amount','a.data_type','b.state','b.payment_receipt_date','b.crt_time','b.order_number','b.account','b.pay_way')
  869. ->get()->toArray();
  870. $array = array_unique(array_merge_recursive(array_column($info_array,'account'),array_column($info_array,'pay_way')));
  871. $basic_map = BasicType::whereIn('id',$array)
  872. ->pluck('title','id')
  873. ->toArray();
  874. $emp_id = PaymentReceiptInfo::where('del_time',0)
  875. ->whereIn('payment_receipt_id',array_column($info_array,'payment_receipt_id'))
  876. ->where('type',PaymentReceiptInfo::type_two)
  877. ->get()->toArray();
  878. $info = [];
  879. if(! empty($emp_id)){
  880. $emp_map = Employee::whereIn('id',array_unique(array_column($emp_id,'data_id')))
  881. ->pluck('emp_name','id')
  882. ->toArray();
  883. foreach ($emp_id as $value){
  884. $name = $emp_map[$value['data_id']] ?? "";
  885. if(isset($info[$value['payment_receipt_id']])){
  886. $info[$value['payment_receipt_id']] .= ',' . $name;
  887. }else{
  888. $info[$value['payment_receipt_id']] = $name;
  889. }
  890. }
  891. }
  892. //四个金额类型
  893. $one = $two = $three = $four = $not_confirm_receipt_amount = 0;
  894. foreach ($info_array as $key => $value){
  895. //归属人
  896. $info_array[$key]['belong'] = $info[$value['payment_receipt_id']] ?? '';
  897. $info_array[$key]['account'] = $basic_map[$value['account']] ?? '';
  898. $info_array[$key]['pay_way'] = $basic_map[$value['pay_way']] ?? '';
  899. $info_array[$key]['state_title'] = PaymentReceipt::$name[$value['state']] ?? '';
  900. $info_array[$key]['type_title'] = PaymentReceipt::$model_type[$value['data_type']] ?? '';
  901. $info_array[$key]['payment_receipt_date'] = $value['payment_receipt_date'] ? date('Y-m-d',$value['payment_receipt_date']) : '';
  902. if($value['data_type'] == PaymentReceipt::type_one) $not_confirm_receipt_amount += $value['amount'];
  903. if($value['data_type'] == PaymentReceipt::type_one && $value['state'] == PaymentReceipt::STATE_TWO){
  904. $one += $value['amount'];
  905. }elseif ($value['data_type'] == PaymentReceipt::type_three && $value['state'] == PaymentReceipt::STATE_TWO){
  906. $three += $value['amount'];
  907. }
  908. }
  909. $return['receipt_amount'] = bcsub($one, $three, 2); // 已回款金额
  910. $return['not_receipt_amount'] = 0;
  911. $return['red_amount'] = $three;// 已红冲金额
  912. $return['bad_amount'] = $four;
  913. $return['all_count'] = count($info_array);
  914. $return['not_confirm_receipt_amount'] = $not_confirm_receipt_amount;
  915. $return['list'] = $info_array;
  916. return $return;
  917. }
  918. //列表里 默认:(收款)
  919. public function getPaymentReceiptDataCountList($data){
  920. $data_order_no = $data;
  921. if(empty($data_order_no)) return [];
  922. $order = PaymentReceiptInfo::from('payment_receipt_info as a')
  923. ->leftJoin('payment_receipt as b','b.id','a.payment_receipt_id')
  924. ->where('b.del_time',0)
  925. ->where('a.del_time',0)
  926. ->where('a.type',PaymentReceiptInfo::type_three)
  927. ->whereIn('a.data_order_no',$data_order_no)
  928. ->select('a.payment_receipt_id','a.data_order_no','a.amount','a.data_type','b.state','b.payment_receipt_date')
  929. ->get()->toArray();
  930. // $order = PaymentReceiptInfo::where('del_time',0)
  931. // ->where('type',PaymentReceiptInfo::type_three)
  932. // ->whereIn('data_order_no',$data_order_no)
  933. // ->get()->toArray();
  934. // 所有状态都统计 审核成功的统计
  935. $return = $return1 = [];
  936. foreach ($order as $value){
  937. $key = $value['data_order_no'] . $value['data_type'];
  938. if(isset($return[$key])){
  939. $return[$key] += $value['amount'];
  940. }else{
  941. $return[$key] = $value['amount'];
  942. }
  943. if($value['state'] == PaymentReceipt::STATE_TWO){
  944. if(isset($return1[$key])){
  945. $return1[$key] += $value['amount'];
  946. }else{
  947. $return1[$key] = $value['amount'];
  948. }
  949. }
  950. }
  951. return [$return, $return1];
  952. }
  953. public function maked(){
  954. $payment = PaymentReceipt::where('del_time',0)
  955. ->where('data_order_no','LIKE', '%'."T9SO.".'%')
  956. ->where('amount','>',0)
  957. ->get()->toArray();
  958. $insert = [];
  959. foreach ($payment as $value){
  960. $insert[] = [
  961. 'payment_receipt_id' => $value['id'],
  962. 'crt_time' => $value['crt_time'],
  963. 'type' => PaymentReceiptInfo::type_three,
  964. 'data_order_no' => $value['data_order_no'],
  965. 'amount' => $value['amount'],
  966. 'data_order_type' => $value['type'],
  967. 'data_type' => PaymentReceipt::type_one,
  968. ];
  969. }
  970. if(! empty($insert)) PaymentReceiptInfo::insert($insert);
  971. }
  972. public function checkForEdit($data_order_no = ""){
  973. if(empty($data_order_no)) return [false, '校验参数异常'];
  974. $bool = PaymentReceipt::where('data_order_no', $data_order_no)
  975. ->where('del_time',0)
  976. ->exists();
  977. if(! $bool) return [true, ''];
  978. return [false, "单号:" . $data_order_no . "已建收付款单,编辑操作有可能改变单据原始总金额导致收付款金额错乱,请先删除收付款单据!"];
  979. }
  980. }