PaymentReceiptService.php 37 KB

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