PaymentReceiptService.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  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. if(in_array($value['url'], $old)) {
  54. foreach ($old as $o_k => $o_v){
  55. if($o_v == $value['url']) unset($old[$o_k]);
  56. }
  57. }else{
  58. $new[] = $value['url'];
  59. }
  60. }
  61. PaymentReceiptInfo::insert($insert);
  62. }
  63. if(! empty($data['employee_one'])){
  64. $insert = [];
  65. foreach ($data['employee_one'] as $value){
  66. $insert[] = [
  67. 'payment_receipt_id' => $model->id,
  68. 'data_id' => $value,
  69. 'type' => PaymentReceiptInfo::type_two,
  70. 'crt_time' => $time,
  71. ];
  72. }
  73. PaymentReceiptInfo::insert($insert);
  74. }
  75. if(! empty($data['amount_list'])){
  76. $insert = [];
  77. foreach ($data['amount_list'] as $value){
  78. $insert[] = [
  79. 'payment_receipt_id' => $model->id,
  80. 'data_type' => $data['type'],
  81. 'data_order_no' => $value['data_order_no'],
  82. 'data_order_type' => $data['data_type'],
  83. 'amount' => $value['amount'],
  84. 'type' => PaymentReceiptInfo::type_three,
  85. 'crt_time' => $time,
  86. ];
  87. }
  88. PaymentReceiptInfo::insert($insert);
  89. }
  90. DB::commit();
  91. }catch (\Exception $exception){
  92. DB::rollBack();
  93. return [false,$exception->getMessage()];
  94. }
  95. if(! empty($data['check'])) {
  96. list($status,$msg) = (new CheckService())->checkAll([
  97. "id" => $data['id'],
  98. "order_number" => $data['order_number'],
  99. "opt_case" => CheckService::ten,
  100. "menu_id" => $data['menu_id']
  101. ],$user);
  102. // if(! $status) return [true, '保存成功,收付款确认失败,异常信息:' . $msg];
  103. }
  104. return [true, ['file' => ['new' => $new, 'old' => $old]]];
  105. }
  106. public function customerAdd($data,$user){
  107. list($status,$msg) = $this->customerRule($data,$user);
  108. if(!$status) return [$status,$msg];
  109. try {
  110. DB::beginTransaction();
  111. $model = new PaymentReceipt();
  112. $model->order_number = $data['order_number'];
  113. // $model->data_order_no = $data['data_order_no'];
  114. $model->data_type = $data['data_type'];
  115. $model->type = $data['type'];
  116. $model->account = $data['account'] ?? 0;
  117. $model->pay_way = $data['pay_way'] ?? 0;
  118. // $model->amount = $data['amount'] ?? 0;
  119. $model->mark = $data['mark'] ?? '';
  120. $model->crt_id = $user['id'];
  121. $model->depart_id = $data['depart_id'];
  122. $model->top_depart_id = $data['top_depart_id'];
  123. $model->payment_receipt_date = $data['payment_receipt_date'] ?? 0;
  124. $model->save();
  125. $time = time();
  126. $new = [];
  127. if(! empty($data['file'])){
  128. $insert = [];
  129. foreach ($data['file'] as $value){
  130. $insert[] = [
  131. 'payment_receipt_id' => $model->id,
  132. 'file' => $value['url'],
  133. 'type' => PaymentReceiptInfo::type_one,
  134. 'name' => $value['name'],
  135. 'crt_time' => $time,
  136. ];
  137. if(! empty($value['url'])) $new[] = $value['url'];
  138. }
  139. PaymentReceiptInfo::insert($insert);
  140. }
  141. if(! empty($data['employee_one'])){
  142. $insert = [];
  143. foreach ($data['employee_one'] as $value){
  144. $insert[] = [
  145. 'payment_receipt_id' => $model->id,
  146. 'data_id' => $value,
  147. 'type' => PaymentReceiptInfo::type_two,
  148. 'crt_time' => $time,
  149. ];
  150. }
  151. PaymentReceiptInfo::insert($insert);
  152. }
  153. if(! empty($data['amount_list'])){
  154. $insert = [];
  155. foreach ($data['amount_list'] as $value){
  156. $insert[] = [
  157. 'payment_receipt_id' => $model->id,
  158. 'data_type' => $data['type'],
  159. 'data_order_no' => $value['data_order_no'],
  160. 'data_order_type' => $data['data_type'],
  161. 'amount' => $value['amount'],
  162. 'type' => PaymentReceiptInfo::type_three,
  163. 'crt_time' => $time,
  164. ];
  165. }
  166. PaymentReceiptInfo::insert($insert);
  167. }
  168. DB::commit();
  169. }catch (\Exception $exception){
  170. DB::rollBack();
  171. return [false,$exception->getMessage()];
  172. }
  173. if(! empty($data['check'])) {
  174. list($status,$msg) = (new CheckService())->checkAll([
  175. "id" => $model->id,
  176. "order_number" => $data['order_number'],
  177. "opt_case" => CheckService::ten,
  178. "menu_id" => $data['menu_id']
  179. ],$user);
  180. // if(! $status) return [true, '保存成功,收付款确认失败,异常信息:' . $msg];
  181. }
  182. return [true, ['file' => ['new' => $new]]];
  183. }
  184. public function customerDel($data){
  185. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  186. $booking = PaymentReceipt::where('del_time',0)->where('id',$data['id'])->first();
  187. if(empty($booking)) return [false,'收付款单不存在或已被删除'];
  188. $booking = $booking->toArray();
  189. if($booking['state'] != PaymentReceipt::STATE_ZERO) return [false,'请确认收付款单状态,删除失败'];
  190. try {
  191. DB::beginTransaction();
  192. $time = time();
  193. $old = PaymentReceiptInfo::where('del_time',0)
  194. ->where('payment_receipt_id',$data['id'])
  195. ->select('file')
  196. ->get()->toArray();
  197. $old = array_column($old,'file');
  198. PaymentReceipt::where('id',$data['id'])->update([
  199. 'del_time'=> $time
  200. ]);
  201. PaymentReceiptInfo::where('del_time',0)
  202. ->where('payment_receipt_id',$data['id'])
  203. ->update(['del_time' => $time]);
  204. DB::commit();
  205. }catch (\Exception $exception){
  206. DB::rollBack();
  207. return [false,$exception->getMessage()];
  208. }
  209. return [true, ['file' => ['old' => $old]]];
  210. }
  211. public function customerDetail($data){
  212. if(empty($data['id']) && empty($data['order_number'])) return [false,'请选择数据!'];
  213. if(! empty($data['id'])){
  214. $customer = PaymentReceipt::where('del_time',0)
  215. ->where('id',$data['id'])
  216. ->first();
  217. }else{
  218. $customer = PaymentReceipt::where('del_time',0)
  219. ->where('order_number',$data['order_number'])
  220. ->first();
  221. $data['id'] = empty($customer->id) ? 0 : $customer->id;
  222. }
  223. if(empty($customer)) return [false,'收付款记录不存在或已被删除'];
  224. $customer = $customer->toArray();
  225. $array = [
  226. $customer['account'],
  227. $customer['pay_way'],
  228. ];
  229. $basic_map = BasicType::whereIn('id',$array)
  230. ->pluck('title','id')
  231. ->toArray();
  232. $customer['account_title'] = $basic_map[$customer['account']] ?? "";
  233. $customer['pay_way_title'] = $basic_map[$customer['pay_way']] ?? "";
  234. $customer['state_title'] = PaymentReceipt::$name[$customer['state']] ?? "";
  235. $customer['type_title'] = PaymentReceipt::$model_type[$customer['type']] ?? "";
  236. $customer['data_type_title'] = PaymentReceipt::$data_type[$customer['data_type']] ?? "";
  237. $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('emp_name');
  238. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  239. $customer['payment_receipt_date'] = $customer['payment_receipt_date'] ? date("Y-m-d",$customer['payment_receipt_date']): '';
  240. $file = PaymentReceiptInfo::where('del_time',0)
  241. ->whereIn('type',[PaymentReceiptInfo::type_one,PaymentReceiptInfo::type_two])
  242. ->where('payment_receipt_id',$data['id'])
  243. ->get()->toArray();
  244. $emp_id = [];
  245. foreach ($file as $value){
  246. if(in_array($value['type'],PaymentReceiptInfo::$man)){
  247. $emp_id[] = $value['data_id'];
  248. }
  249. }
  250. $emp_map = Employee::whereIn('id',array_unique($emp_id))
  251. ->pluck('emp_name','id')
  252. ->toArray();
  253. $customer['file'] = $customer['employee_one'] = [];
  254. $fileUploadService = new FileUploadService();
  255. foreach ($file as $value){
  256. if($value['type'] == PaymentReceiptInfo::type_one){
  257. $tmp = [
  258. 'url' => $value['file'],
  259. 'name' => $value['name'],
  260. 'show_url' => $fileUploadService->getFileShow($value['file']),
  261. ];
  262. $customer['file'][] = $tmp;
  263. }elseif (in_array($value['type'],PaymentReceiptInfo::$man)){
  264. $tt = $emp_map[$value['data_id']] ?? '';
  265. if(! empty($tt)){
  266. $tmp = [
  267. 'id' => $value['data_id'],
  268. 'name' => $emp_map[$value['data_id']] ?? '',
  269. ];
  270. if($value['type'] == PaymentReceiptInfo::type_two){
  271. $customer['employee_one'][] = $tmp;
  272. }
  273. }
  274. }
  275. }
  276. //组织金额
  277. $customer['amount_list'] = $this->makeDetail($customer);
  278. return [true, $customer];
  279. }
  280. public function makeDetail($data){
  281. $return = [];
  282. $info = PaymentReceiptInfo::where('del_time',0)
  283. ->where('type',PaymentReceiptInfo::type_three)
  284. ->where('payment_receipt_id', $data['id'])
  285. ->select('payment_receipt_id','data_order_type','data_type','amount','data_order_no')
  286. ->get()->toArray();
  287. $order_no = array_column($info,'data_order_no');
  288. //退换货金额
  289. if($data['type'] == PaymentReceipt::type_one){
  290. if($data['data_type'] == PaymentReceipt::data_type_one){
  291. $order = SalesOrder::where('del_time',0)
  292. ->whereIn('order_number',$order_no)
  293. ->pluck('contract_fee','order_number')
  294. ->toArray();
  295. $getDifferentAmountALL = (new ReturnExchangeOrderService())->getDifferentAmountALL2(array_keys($order));
  296. }else{
  297. $order = PurchaseOrder::where('del_time',0)
  298. ->whereIn('order_number',$order_no)
  299. ->pluck('purchase_total','order_number')
  300. ->toArray();
  301. $getDifferentAmountALL = (new ReturnExchangeOrderService())->getDifferentAmountALL2(array_keys($order), ReturnExchangeOrder::Order_type2);
  302. }
  303. }
  304. //除自己这个收付款单外 所有订单已填的金额
  305. $infos = PaymentReceiptInfo::where('del_time',0)
  306. ->where('type',PaymentReceiptInfo::type_three)
  307. ->where('data_type',$data['type'])
  308. ->where('payment_receipt_id','<>',$data['id'])
  309. ->whereIn('data_order_no',$order_no)
  310. ->get()->toArray();
  311. $infos_map = [];
  312. foreach ($infos as $value){
  313. if(isset($infos_map[$value['data_order_no']])){
  314. $infos_map[$value['data_order_no']] += $value['amount'];
  315. }else{
  316. $infos_map[$value['data_order_no']] = $value['amount'];
  317. }
  318. }
  319. //收款金额(审核后)
  320. $info_one_array = [];
  321. if($data['type'] == PaymentReceipt::type_three){
  322. $info_one = PaymentReceiptInfo::from('payment_receipt_info as a')
  323. ->leftJoin('payment_receipt as b','b.id','a.payment_receipt_id')
  324. ->where('b.state',PaymentReceipt::STATE_TWO)
  325. ->where('b.del_time',0)
  326. ->where('a.del_time',0)
  327. ->where('a.type',PaymentReceiptInfo::type_three)
  328. ->where('a.data_order_type',PaymentReceipt::data_type_one)
  329. ->whereIn('a.data_order_no',$order_no)
  330. ->where('a.data_type',PaymentReceipt::type_one)
  331. ->select('a.data_order_no','a.amount')
  332. ->get()->toArray();
  333. $info_one_array = [];
  334. foreach ($info_one as $value){
  335. if(isset($info_one_array[$value['data_order_no']])){
  336. $info_one_array[$value['data_order_no']] += $value['amount'];
  337. }else{
  338. $info_one_array[$value['data_order_no']] = $value['amount'];
  339. }
  340. }
  341. }
  342. foreach ($info as $value){
  343. $total = $tmp_receipt = 0;
  344. if($data['type'] == PaymentReceipt::type_one){
  345. //收款 = 总金额 - 已收 - 退货退款
  346. //总金额
  347. $total_amount = $order[$value['data_order_no']] ?? 0;
  348. //已收
  349. $tmp_receipt = $infos_map[$value['data_order_no']] ?? 0;
  350. //退货退款
  351. $tmp_return = $getDifferentAmountALL[$value['data_order_no']] ?? 0;
  352. //收款
  353. $total = bcsub(bcsub($total_amount, $tmp_receipt,2), $tmp_return, 2);
  354. }elseif($data['type'] == PaymentReceipt::type_three){
  355. //红冲 = 审核后的收款金额 - 已红冲
  356. //总金额 收款金额(审核后)
  357. $total_amount = $info_one_array[$value['data_order_no']] ?? 0;
  358. //已红冲
  359. $tmp_receipt = $infos_map[$value['data_order_no']] ?? 0;
  360. //红冲
  361. $total = bcsub($total_amount, $tmp_receipt, 2);
  362. }
  363. $tmp = [
  364. 'data_order_no' => $value['data_order_no'],
  365. 'total_amount' => $total,//总共能填的金额
  366. 'has_amount' => $tmp_receipt,//已经填的金额
  367. 'amount' => $value['amount'],//本单金额
  368. 'receipt_amount' => $value['amount'],//本单金额
  369. ];
  370. $return[] = $tmp;
  371. }
  372. return $return;
  373. }
  374. public function customerList($data,$user){
  375. $model = PaymentReceipt::Clear($user,$data);
  376. $model = $model->where('del_time',0)
  377. ->select('type','id','data_type','order_number','data_order_no','amount','account','pay_way','crt_id','crt_time','mark','state','payment_receipt_date')
  378. ->orderby('id', 'desc');
  379. if(isset($data['state'])) $model->where('state', $data['state']);
  380. if(! empty($data['data_order_no'])) {
  381. $info = PaymentReceiptInfo::where('del_time',0)
  382. ->where('type',PaymentReceiptInfo::type_three)
  383. ->where('data_order_no', 'LIKE', '%'.$data['data_order_no'].'%')
  384. ->select('payment_receipt_id')
  385. ->get()->toArray();
  386. $model->whereIn('id', array_unique(array_column($info,'payment_receipt_id')));
  387. }
  388. if(! empty($data['order_number'])) $model->where('order_number', 'LIKE', '%'.$data['order_number'].'%');
  389. if(! empty($data['data_type'])) $model->where('data_type', $data['data_type']);
  390. if(! empty($data['type'])) $model->where('type', $data['type']);
  391. if(! empty($data['account'])) $model->where('account',$data['account']);
  392. if(! empty($data['pay_way'])) $model->where('pay_way',$data['pay_way']);
  393. if(! empty($data['mark'])) $model->where('mark', 'LIKE', '%'.$data['mark'].'%');
  394. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  395. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  396. $model->where('crt_time','>=',$return[0]);
  397. $model->where('crt_time','<=',$return[1]);
  398. }
  399. if(! empty($data['payment_receipt_date'][0]) && ! empty($data['payment_receipt_date'][1])){
  400. $return = $this->changeDateToTimeStampAboutRange($data['payment_receipt_date']);
  401. $model->where('payment_receipt_date','>=',$return[0]);
  402. $model->where('payment_receipt_date','<=',$return[1]);
  403. }
  404. if(! empty($data['belong'])){
  405. $id = (new RangeService())->paymentReceiptSearch($data);
  406. $model->whereIn('id',$id);
  407. }
  408. $list = $this->limit($model,'',$data);
  409. $list = $this->fillData($list);
  410. return [true, $list];
  411. }
  412. public function customerRule(&$data, $user, $is_add = true){
  413. if(empty($data['order_number'])) return [false,'收付款单编号不能为空'];
  414. if(empty($data['data_type'])) return [false,'单号类型不能为空'];
  415. if(empty($data['type'])) return [false,'收付款类型不能为空'];
  416. if(empty($data['amount_list']) || ! is_array($data['amount_list'])) return [false,'关联单号与金额不能为空'];
  417. foreach ($data['amount_list'] as $value){
  418. if(empty($value['data_order_no'])) return [false,'关联单号不能为空'];
  419. if(empty($value['amount'])) return [false,'金额不能为空'];
  420. $res = $this->checkNumber($value['amount']);
  421. if(! $res) return [false, '金额请输入不超过两位小数并且大于0的数值'];
  422. }
  423. if(! empty($data['payment_receipt_date'])) $data['payment_receipt_date'] = $this->changeDateToDate($data['payment_receipt_date']);
  424. //所属部门 以及 顶级部门
  425. if(empty($data['depart_id'])) {
  426. $data['depart_id'] = $this->getDepart($user);
  427. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  428. }
  429. if($is_add){
  430. $bool = PaymentReceipt::where('del_time',0)
  431. ->where('order_number',$data['order_number'])
  432. ->exists();
  433. if($bool) return [false,'收付款单编号已存在,请重新获取'];
  434. }else{
  435. if(empty($data['id'])) return [false,'ID不能为空'];
  436. $booking = PaymentReceipt::where('del_time',0)->where('id',$data['id'])->first();
  437. if(empty($booking)) return [false,'收付款单不存在或已被删除'];
  438. $booking = $booking->toArray();
  439. if($booking['state'] != PaymentReceipt::STATE_ZERO) return [false,'请确认收付款单状态,编辑失败'];
  440. }
  441. // list($status,$msg) = $this->limitingSendRequestBackgExpire("paymentReceipt" . $data['order_number']);
  442. // if(! $status) return [false, $msg];
  443. list($status,$msg) = $this->checkRule($data);
  444. if(! $status) return [false, $msg];
  445. return [true, ''];
  446. }
  447. public function checkRule($data){
  448. $payment_receipt_id = $data['id'] ?? 0;
  449. $map = [];
  450. foreach ($data['amount_list'] as $value){
  451. $map[$value['data_order_no']] = $value['amount'];
  452. }
  453. $search = array_keys($map);
  454. if($data['data_type'] == PaymentReceipt::data_type_one){ //合同逻辑
  455. //总金额
  456. $result = SalesOrder::where('del_time',0)
  457. ->whereIn('order_number',$search)
  458. ->select('id','order_number','contract_fee as total_amount')
  459. ->get()->toArray();
  460. if(count($search) != count($result)) {
  461. foreach ($result as $value){
  462. if(! isset($map[$value['order_number']])) return [false, $value['order_number'] . '不存在或已被删除'];
  463. }
  464. }
  465. $result_map = array_column($result,'order_number','id');
  466. //收付款金额
  467. $info = PaymentReceiptInfo::where('del_time',0)
  468. ->where('type',PaymentReceiptInfo::type_three)
  469. ->where('data_order_type',PaymentReceipt::data_type_one)
  470. ->whereIn('data_order_no',$search)
  471. ->where('data_type',$data['type'])
  472. ->select('payment_receipt_id','data_order_no','amount')
  473. ->get()->toArray();
  474. $info_array = [];
  475. foreach ($info as $value){
  476. if($payment_receipt_id > 0 && $value['payment_receipt_id'] == $payment_receipt_id) continue;
  477. if(isset($info_array[$value['data_order_no']])){
  478. $info_array[$value['data_order_no']] += $value['amount'];
  479. }else{
  480. $info_array[$value['data_order_no']] = $value['amount'];
  481. }
  482. }
  483. //退货退款金额
  484. $return_exchange = (new ReturnExchangeOrderService())->getDifferentAmountALL(array_column($result,'id'));
  485. if($data['type'] == PaymentReceipt::type_one){
  486. $return_exchange_array = [];
  487. foreach ($result_map as $key => $value){
  488. // order_number => amount
  489. if(isset($return_exchange[$key])) $return_exchange_array[$value] = $return_exchange[$key];
  490. }
  491. foreach ($result as $value){
  492. //收款 = 总金额 - 已收 - 退货退款
  493. //$max = $value['total_amount'] - $tmp_receipt - $tmp_return;
  494. $tmp_receipt = $info_array[$value['order_number']] ?? 0;
  495. $tmp_return = $return_exchange_array[$value['order_number']] ?? 0;
  496. $max = bcsub(bcsub($value['total_amount'], $tmp_receipt,2), $tmp_return, 2);
  497. if($map[$value['order_number']] > $max) return [false, $value['order_number'] . '的金额填写上限是' . $max];
  498. }
  499. }elseif($data['type'] == PaymentReceipt::type_three){
  500. //收款金额(审核后)
  501. $info_one = PaymentReceiptInfo::from('payment_receipt_info as a')
  502. ->leftJoin('payment_receipt as b','b.id','a.payment_receipt_id')
  503. ->where('b.state',PaymentReceipt::STATE_TWO)
  504. ->where('b.del_time',0)
  505. ->where('a.del_time',0)
  506. ->where('a.type',PaymentReceiptInfo::type_three)
  507. ->where('a.data_order_type',PaymentReceipt::data_type_one)
  508. ->whereIn('a.data_order_no',$search)
  509. ->where('a.data_type',PaymentReceipt::type_one)
  510. ->select('a.payment_receipt_id','a.data_order_no','a.amount')
  511. ->get()->toArray();
  512. $info_one_array = [];
  513. foreach ($info_one as $value){
  514. if(isset($info_one_array[$value['data_order_no']])){
  515. $info_one_array[$value['data_order_no']] += $value['amount'];
  516. }else{
  517. $info_one_array[$value['data_order_no']] = $value['amount'];
  518. }
  519. }
  520. foreach ($result as $value){
  521. //红冲 = 审核后的收款单金额 - 已红冲
  522. $tmp_receipt = $info_one_array[$value['order_number']] ?? 0;
  523. $tmp_return = $info_array[$value['order_number']] ?? 0;
  524. $max = bcsub($tmp_receipt, $tmp_return, 2);
  525. if($map[$value['order_number']] > $max) return [false, $value['order_number'] . '的金额填写上限是' . $max];
  526. }
  527. }
  528. }elseif ($data['data_type'] == PaymentReceipt::data_type_two){//采购逻辑
  529. //总金额
  530. $result = PurchaseOrder::where('del_time',0)
  531. ->whereIn('order_number',$search)
  532. ->select('id','order_number','purchase_total as total_amount')
  533. ->get()->toArray();
  534. if(count($search) != count($result)) {
  535. foreach ($result as $value){
  536. if(! isset($map[$value['order_number']])) return [false, $value['order_number'] . '不存在或已被删除'];
  537. }
  538. }
  539. $result_map = array_column($result,'order_number','id');
  540. //收付款金额
  541. $info = PaymentReceiptInfo::where('del_time',0)
  542. ->where('type',PaymentReceiptInfo::type_three)
  543. ->where('data_order_type',PaymentReceipt::data_type_two)
  544. ->whereIn('data_order_no',$search)
  545. ->where('data_type',$data['type'])
  546. ->select('payment_receipt_id','data_order_no','amount')
  547. ->get()->toArray();
  548. $info_array = [];
  549. foreach ($info as $value){
  550. if($payment_receipt_id > 0 && $value['payment_receipt_id'] == $payment_receipt_id) continue;
  551. if(isset($info_array[$value['data_order_no']])){
  552. $info_array[$value['data_order_no']] += $value['amount'];
  553. }else{
  554. $info_array[$value['data_order_no']] = $value['amount'];
  555. }
  556. }
  557. //退货退款金额
  558. $return_exchange = (new ReturnExchangeOrderService())->getDifferentAmountALL(array_column($result,'id'),1);
  559. if($data['type'] == PaymentReceipt::type_one){
  560. $return_exchange_array = [];
  561. foreach ($result_map as $key => $value){
  562. // order_number => amount
  563. if(isset($return_exchange[$key])) $return_exchange_array[$value] = $return_exchange[$key];
  564. }
  565. foreach ($result as $value){
  566. //收款 = 总金额 - 已收 - 退货退款
  567. //$max = $value['total_amount'] - $tmp_receipt - $tmp_return;
  568. $tmp_receipt = $info_array[$value['order_number']] ?? 0;
  569. $tmp_return = $return_exchange_array[$value['order_number']] ?? 0;
  570. $max = bcsub(bcsub($value['total_amount'], $tmp_receipt,2), $tmp_return, 2);
  571. if($map[$value['order_number']] > $max) return [false, $value['order_number'] . '的金额填写上限是' . $max];
  572. }
  573. }elseif($data['type'] == PaymentReceipt::type_three){
  574. //收款金额(审核后)
  575. $info_one = PaymentReceiptInfo::from('payment_receipt_info as a')
  576. ->leftJoin('payment_receipt as b','b.id','a.payment_receipt_id')
  577. ->where('b.state',PaymentReceipt::STATE_TWO)
  578. ->where('b.del_time',0)
  579. ->where('a.del_time',0)
  580. ->where('a.type',PaymentReceiptInfo::type_three)
  581. ->where('a.data_order_type',PaymentReceipt::data_type_one)
  582. ->whereIn('a.data_order_no',$search)
  583. ->where('a.data_type',PaymentReceipt::type_one)
  584. ->select('a.payment_receipt_id','a.data_order_no','a.amount')
  585. ->get()->toArray();
  586. $info_one_array = [];
  587. foreach ($info_one as $value){
  588. if(isset($info_one_array[$value['data_order_no']])){
  589. $info_one_array[$value['data_order_no']] += $value['amount'];
  590. }else{
  591. $info_one_array[$value['data_order_no']] = $value['amount'];
  592. }
  593. }
  594. foreach ($result as $value){
  595. //红冲 = 审核后的收款单金额 - 已红冲
  596. $tmp_receipt = $info_one_array[$value['order_number']] ?? 0;
  597. $tmp_return = $info_array[$value['order_number']] ?? 0;
  598. $max = bcsub($tmp_receipt, $tmp_return, 2);
  599. if($map[$value['order_number']] > $max) return [false, $value['order_number'] . '的金额填写上限是' . $max];
  600. }
  601. }
  602. }
  603. return [true, ''];
  604. }
  605. public function fillData($data){
  606. if(empty($data['data'])) return $data;
  607. $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
  608. ->pluck('emp_name','id')
  609. ->toArray();
  610. $array = array_unique(array_merge_recursive(array_column($data['data'],'account'),array_column($data['data'],'pay_way')));
  611. $basic_map = BasicType::whereIn('id',$array)
  612. ->pluck('title','id')
  613. ->toArray();
  614. $map = [];
  615. $info = PaymentReceiptInfo::where('del_time',0)
  616. ->where('type',PaymentReceiptInfo::type_three)
  617. ->whereIn('payment_receipt_id', array_unique(array_column($data['data'],'id')))
  618. ->select('payment_receipt_id','data_order_no')
  619. ->get()->toArray();
  620. foreach ($info as $value){
  621. $map[$value['payment_receipt_id']][] = $value['data_order_no'];
  622. }
  623. foreach ($data['data'] as $key => $value){
  624. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  625. $data['data'][$key]['payment_receipt_date'] = $value['payment_receipt_date'] ? date('Y-m-d',$value['payment_receipt_date']) : '';
  626. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  627. $data['data'][$key]['state_title'] = PaymentReceipt::$name[$value['state']] ?? '';
  628. $data['data'][$key]['type_title'] = PaymentReceipt::$model_type[$value['type']] ?? '';
  629. $data['data'][$key]['data_type_title'] = PaymentReceipt::$data_type[$value['data_type']] ?? '';
  630. $data['data'][$key]['account_title'] = $basic_map[$value['account']] ?? '';
  631. $data['data'][$key]['pay_way_title'] = $basic_map[$value['pay_way']] ?? '';
  632. $data['data'][$key]['data_order_no'] = $map[$value['id']] ?? [];
  633. }
  634. return $data;
  635. }
  636. //详情里
  637. public function getPaymentReceiptDataList($data,$type){
  638. $data['data_order_no'] = $data['order_number'];
  639. $info_array = PaymentReceiptInfo::from('payment_receipt_info as a')
  640. ->leftJoin('payment_receipt as b','b.id','a.payment_receipt_id')
  641. ->where('b.del_time',0)
  642. ->where('a.del_time',0)
  643. ->where('a.type',PaymentReceiptInfo::type_three)
  644. ->where('a.data_order_type',$type)
  645. ->where('a.data_order_no',$data['data_order_no'])
  646. ->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')
  647. ->get()->toArray();
  648. $emp_id = PaymentReceiptInfo::where('del_time',0)
  649. ->whereIn('payment_receipt_id',array_column($info_array,'payment_receipt_id'))
  650. ->where('type',PaymentReceiptInfo::type_two)
  651. ->get()->toArray();
  652. $info = [];
  653. if(! empty($emp_id)){
  654. $emp_map = Employee::whereIn('id',array_unique(array_column($emp_id,'data_id')))
  655. ->pluck('emp_name','id')
  656. ->toArray();
  657. foreach ($emp_id as $value){
  658. $name = $emp_map[$value['data_id']] ?? "";
  659. if(isset($info[$value['payment_receipt_id']])){
  660. $info[$value['payment_receipt_id']] .= ',' . $name;
  661. }else{
  662. $info[$value['payment_receipt_id']] = $name;
  663. }
  664. }
  665. }
  666. //四个金额类型
  667. $one = $two = $three = $four = $not_confirm_receipt_amount = 0;
  668. foreach ($info_array as $key => $value){
  669. //归属人
  670. $info_array[$key]['belong'] = $info[$value['payment_receipt_id']] ?? '';
  671. $info_array[$key]['state_title'] = PaymentReceipt::$name[$value['state']] ?? '';
  672. $info_array[$key]['type_title'] = PaymentReceipt::$model_type[$value['data_type']] ?? '';
  673. $info_array[$key]['payment_receipt_date'] = $value['payment_receipt_date'] ? date('Y-m-d',$value['payment_receipt_date']) : '';
  674. if($value['data_type'] == PaymentReceipt::type_one) $not_confirm_receipt_amount += $value['amount'];
  675. if($value['data_type'] == PaymentReceipt::type_one && $value['state'] == PaymentReceipt::STATE_TWO){
  676. $one += $value['amount'];
  677. }elseif ($value['data_type'] == PaymentReceipt::type_three && $value['state'] == PaymentReceipt::STATE_TWO){
  678. $three += $value['amount'];
  679. }
  680. }
  681. $return['receipt_amount'] = bcsub($one, $three, 2); // 已回款金额
  682. $return['not_receipt_amount'] = 0;
  683. $return['red_amount'] = $three;// 已红冲金额
  684. $return['bad_amount'] = $four;
  685. $return['all_count'] = count($info_array);
  686. $return['not_confirm_receipt_amount'] = $not_confirm_receipt_amount;
  687. $return['list'] = $info_array;
  688. return $return;
  689. }
  690. //列表里 默认:(收款)
  691. public function getPaymentReceiptDataCountList($data){
  692. $data_order_no = $data;
  693. if(empty($data_order_no)) return [];
  694. $order = PaymentReceiptInfo::from('payment_receipt_info as a')
  695. ->leftJoin('payment_receipt as b','b.id','a.payment_receipt_id')
  696. ->where('b.del_time',0)
  697. ->where('a.del_time',0)
  698. ->where('a.type',PaymentReceiptInfo::type_three)
  699. ->whereIn('a.data_order_no',$data_order_no)
  700. ->select('a.payment_receipt_id','a.data_order_no','a.amount','a.data_type','b.state','b.payment_receipt_date')
  701. ->get()->toArray();
  702. // $order = PaymentReceiptInfo::where('del_time',0)
  703. // ->where('type',PaymentReceiptInfo::type_three)
  704. // ->whereIn('data_order_no',$data_order_no)
  705. // ->get()->toArray();
  706. // 所有状态都统计 审核成功的统计
  707. $return = $return1 = [];
  708. foreach ($order as $value){
  709. $key = $value['data_order_no'] . $value['data_type'];
  710. if(isset($return[$key])){
  711. $return[$key] += $value['amount'];
  712. }else{
  713. $return[$key] = $value['amount'];
  714. }
  715. if($value['state'] == PaymentReceipt::STATE_TWO){
  716. if(isset($return1[$key])){
  717. $return1[$key] += $value['amount'];
  718. }else{
  719. $return1[$key] = $value['amount'];
  720. }
  721. }
  722. }
  723. return [$return, $return1];
  724. }
  725. public function maked(){
  726. $payment = PaymentReceipt::where('del_time',0)
  727. ->where('data_order_no','LIKE', '%'."T9SO.".'%')
  728. ->where('amount','>',0)
  729. ->get()->toArray();
  730. $insert = [];
  731. foreach ($payment as $value){
  732. $insert[] = [
  733. 'payment_receipt_id' => $value['id'],
  734. 'crt_time' => $value['crt_time'],
  735. 'type' => PaymentReceiptInfo::type_three,
  736. 'data_order_no' => $value['data_order_no'],
  737. 'amount' => $value['amount'],
  738. 'data_order_type' => $value['type'],
  739. 'data_type' => PaymentReceipt::type_one,
  740. ];
  741. }
  742. if(! empty($insert)) PaymentReceiptInfo::insert($insert);
  743. }
  744. }