PaymentReceiptService.php 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  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['type_title'] = PaymentReceipt::$model_type[$customer['type']] ?? "";
  235. $customer['data_type_title'] = PaymentReceipt::$data_type[$customer['data_type']] ?? "";
  236. $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('emp_name');
  237. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  238. $customer['payment_receipt_date'] = $customer['payment_receipt_date'] ? date("Y-m-d",$customer['payment_receipt_date']): '';
  239. //订单状态数据组织
  240. $state_array = $this->getStateMake([$customer]);
  241. $customer['state_title'] = $this->makeState($customer,$state_array);
  242. $file = PaymentReceiptInfo::where('del_time',0)
  243. ->whereIn('type',[PaymentReceiptInfo::type_one,PaymentReceiptInfo::type_two])
  244. ->where('payment_receipt_id',$data['id'])
  245. ->get()->toArray();
  246. $emp_id = [];
  247. foreach ($file as $value){
  248. if(in_array($value['type'],PaymentReceiptInfo::$man)){
  249. $emp_id[] = $value['data_id'];
  250. }
  251. }
  252. $emp_map = Employee::whereIn('id',array_unique($emp_id))
  253. ->pluck('emp_name','id')
  254. ->toArray();
  255. $customer['file'] = $customer['employee_one'] = [];
  256. $fileUploadService = new FileUploadService();
  257. foreach ($file as $value){
  258. if($value['type'] == PaymentReceiptInfo::type_one){
  259. $tmp = [
  260. 'url' => $value['file'],
  261. 'name' => $value['name'],
  262. 'show_url' => $fileUploadService->getFileShow($value['file']),
  263. ];
  264. $customer['file'][] = $tmp;
  265. }elseif (in_array($value['type'],PaymentReceiptInfo::$man)){
  266. $tt = $emp_map[$value['data_id']] ?? '';
  267. if(! empty($tt)){
  268. $tmp = [
  269. 'id' => $value['data_id'],
  270. 'name' => $emp_map[$value['data_id']] ?? '',
  271. ];
  272. if($value['type'] == PaymentReceiptInfo::type_two){
  273. $customer['employee_one'][] = $tmp;
  274. }
  275. }
  276. }
  277. }
  278. //组织金额
  279. $customer['amount_list'] = $this->makeDetail($customer);
  280. return [true, $customer];
  281. }
  282. public function makeDetail($data){
  283. $return = [];
  284. $info = PaymentReceiptInfo::where('del_time',0)
  285. ->where('type',PaymentReceiptInfo::type_three)
  286. ->where('payment_receipt_id', $data['id'])
  287. ->select('payment_receipt_id','data_order_type','data_type','amount','data_order_no')
  288. ->get()->toArray();
  289. $order_no = array_column($info,'data_order_no');
  290. //退换货金额
  291. if($data['type'] == PaymentReceipt::type_one){
  292. if($data['data_type'] == PaymentReceipt::data_type_one){
  293. $order = SalesOrder::where('del_time',0)
  294. ->whereIn('order_number',$order_no)
  295. ->pluck('contract_fee','order_number')
  296. ->toArray();
  297. $getDifferentAmountALL = (new ReturnExchangeOrderService())->getDifferentAmountALL2(array_keys($order));
  298. }else{
  299. $order = PurchaseOrder::where('del_time',0)
  300. ->whereIn('order_number',$order_no)
  301. ->pluck('purchase_total','order_number')
  302. ->toArray();
  303. $getDifferentAmountALL = (new ReturnExchangeOrderService())->getDifferentAmountALL2(array_keys($order), ReturnExchangeOrder::Order_type2);
  304. }
  305. }
  306. //除自己这个收付款单外 所有订单已填的金额
  307. $infos = PaymentReceiptInfo::where('del_time',0)
  308. ->where('type',PaymentReceiptInfo::type_three)
  309. ->where('data_type',$data['type'])
  310. ->where('payment_receipt_id','<>',$data['id'])
  311. ->whereIn('data_order_no',$order_no)
  312. ->get()->toArray();
  313. $infos_map = [];
  314. foreach ($infos as $value){
  315. if(isset($infos_map[$value['data_order_no']])){
  316. $infos_map[$value['data_order_no']] += $value['amount'];
  317. }else{
  318. $infos_map[$value['data_order_no']] = $value['amount'];
  319. }
  320. }
  321. //收款金额(审核后)
  322. $info_one_array = [];
  323. if($data['type'] == PaymentReceipt::type_three){
  324. $info_one = PaymentReceiptInfo::from('payment_receipt_info as a')
  325. ->leftJoin('payment_receipt as b','b.id','a.payment_receipt_id')
  326. ->where('b.state',PaymentReceipt::STATE_TWO)
  327. ->where('b.del_time',0)
  328. ->where('a.del_time',0)
  329. ->where('a.type',PaymentReceiptInfo::type_three)
  330. ->where('a.data_order_type',PaymentReceipt::data_type_one)
  331. ->whereIn('a.data_order_no',$order_no)
  332. ->where('a.data_type',PaymentReceipt::type_one)
  333. ->select('a.data_order_no','a.amount')
  334. ->get()->toArray();
  335. $info_one_array = [];
  336. foreach ($info_one as $value){
  337. if(isset($info_one_array[$value['data_order_no']])){
  338. $info_one_array[$value['data_order_no']] += $value['amount'];
  339. }else{
  340. $info_one_array[$value['data_order_no']] = $value['amount'];
  341. }
  342. }
  343. }
  344. foreach ($info as $value){
  345. $total = $tmp_receipt = 0;
  346. if($data['type'] == PaymentReceipt::type_one){
  347. //收款 = 总金额 - 已收 - 退货退款
  348. //总金额
  349. $total_amount = $order[$value['data_order_no']] ?? 0;
  350. //已收
  351. $tmp_receipt = $infos_map[$value['data_order_no']] ?? 0;
  352. //退货退款
  353. $tmp_return = $getDifferentAmountALL[$value['data_order_no']] ?? 0;
  354. //收款
  355. $total = bcsub(bcsub($total_amount, $tmp_receipt,2), $tmp_return, 2);
  356. }elseif($data['type'] == PaymentReceipt::type_three){
  357. //红冲 = 审核后的收款金额 - 已红冲
  358. //总金额 收款金额(审核后)
  359. $total_amount = $info_one_array[$value['data_order_no']] ?? 0;
  360. //已红冲
  361. $tmp_receipt = $infos_map[$value['data_order_no']] ?? 0;
  362. //红冲
  363. $total = bcsub($total_amount, $tmp_receipt, 2);
  364. }
  365. $tmp = [
  366. 'data_order_no' => $value['data_order_no'],
  367. 'total_amount' => $total,//总共能填的金额
  368. 'has_amount' => $tmp_receipt,//已经填的金额
  369. 'amount' => $value['amount'],//本单金额
  370. 'receipt_amount' => $value['amount'],//本单金额
  371. ];
  372. $return[] = $tmp;
  373. }
  374. return $return;
  375. }
  376. public function customerList($data,$user){
  377. $model = PaymentReceipt::Clear($user,$data);
  378. $model = $model->where('del_time',0)
  379. ->select('type','id','data_type','order_number','data_order_no','amount','account','pay_way','crt_id','crt_time','mark','state','payment_receipt_date')
  380. ->orderby('id', 'desc');
  381. if(isset($data['state'])) $model->where('state', $data['state']);
  382. if(! empty($data['data_order_no'])) {
  383. $info = PaymentReceiptInfo::where('del_time',0)
  384. ->where('type',PaymentReceiptInfo::type_three)
  385. ->where('data_order_no', 'LIKE', '%'.$data['data_order_no'].'%')
  386. ->select('payment_receipt_id')
  387. ->get()->toArray();
  388. $model->whereIn('id', array_unique(array_column($info,'payment_receipt_id')));
  389. }
  390. if(! empty($data['order_number'])) $model->where('order_number', 'LIKE', '%'.$data['order_number'].'%');
  391. if(! empty($data['data_type'])) $model->where('data_type', $data['data_type']);
  392. if(! empty($data['type'])) $model->where('type', $data['type']);
  393. if(! empty($data['account'])) $model->where('account',$data['account']);
  394. if(! empty($data['pay_way'])) $model->where('pay_way',$data['pay_way']);
  395. if(! empty($data['mark'])) $model->where('mark', 'LIKE', '%'.$data['mark'].'%');
  396. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  397. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  398. $model->where('crt_time','>=',$return[0]);
  399. $model->where('crt_time','<=',$return[1]);
  400. }
  401. if(! empty($data['payment_receipt_date'][0]) && ! empty($data['payment_receipt_date'][1])){
  402. $return = $this->changeDateToTimeStampAboutRange($data['payment_receipt_date']);
  403. $model->where('payment_receipt_date','>=',$return[0]);
  404. $model->where('payment_receipt_date','<=',$return[1]);
  405. }
  406. if(! empty($data['belong'])){
  407. $id = (new RangeService())->paymentReceiptSearch($data);
  408. $model->whereIn('id',$id);
  409. }
  410. $list = $this->limit($model,'',$data);
  411. $list = $this->fillData($list);
  412. return [true, $list];
  413. }
  414. public function customerRule(&$data, $user, $is_add = true){
  415. if(empty($data['order_number'])) return [false,'收付款单编号不能为空'];
  416. if(empty($data['data_type'])) return [false,'单号类型不能为空'];
  417. if(empty($data['type'])) return [false,'收付款类型不能为空'];
  418. if(empty($data['amount_list']) || ! is_array($data['amount_list'])) return [false,'关联单号与金额不能为空'];
  419. foreach ($data['amount_list'] as $value){
  420. if(empty($value['data_order_no'])) return [false,'关联单号不能为空'];
  421. if(empty($value['amount'])) return [false,'金额不能为空'];
  422. $res = $this->checkNumber($value['amount']);
  423. if(! $res) return [false, '金额请输入不超过两位小数并且大于0的数值'];
  424. }
  425. if(! empty($data['payment_receipt_date'])) $data['payment_receipt_date'] = $this->changeDateToDate($data['payment_receipt_date']);
  426. //所属部门 以及 顶级部门
  427. if(empty($data['depart_id'])) {
  428. $data['depart_id'] = $this->getDepart($user);
  429. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  430. }
  431. if($is_add){
  432. $bool = PaymentReceipt::where('del_time',0)
  433. ->where('order_number',$data['order_number'])
  434. ->exists();
  435. if($bool) return [false,'收付款单编号已存在,请重新获取'];
  436. }else{
  437. if(empty($data['id'])) return [false,'ID不能为空'];
  438. $booking = PaymentReceipt::where('del_time',0)->where('id',$data['id'])->first();
  439. if(empty($booking)) return [false,'收付款单不存在或已被删除'];
  440. $booking = $booking->toArray();
  441. if($booking['state'] != PaymentReceipt::STATE_ZERO) return [false,'请确认收付款单状态,编辑失败'];
  442. }
  443. // list($status,$msg) = $this->limitingSendRequestBackgExpire("paymentReceipt" . $data['order_number']);
  444. // if(! $status) return [false, $msg];
  445. list($status,$msg) = $this->checkRule($data);
  446. if(! $status) return [false, $msg];
  447. return [true, ''];
  448. }
  449. public function checkRule($data){
  450. $payment_receipt_id = $data['id'] ?? 0;
  451. $map = [];
  452. foreach ($data['amount_list'] as $value){
  453. $map[$value['data_order_no']] = $value['amount'];
  454. }
  455. $search = array_keys($map);
  456. if($data['data_type'] == PaymentReceipt::data_type_one){ //合同逻辑
  457. //总金额
  458. $result = SalesOrder::where('del_time',0)
  459. ->whereIn('order_number',$search)
  460. ->select('id','order_number','contract_fee as total_amount')
  461. ->get()->toArray();
  462. if(count($search) != count($result)) {
  463. foreach ($result as $value){
  464. if(! isset($map[$value['order_number']])) return [false, $value['order_number'] . '不存在或已被删除'];
  465. }
  466. }
  467. $result_map = array_column($result,'order_number','id');
  468. //收付款金额
  469. $info = PaymentReceiptInfo::where('del_time',0)
  470. ->where('type',PaymentReceiptInfo::type_three)
  471. ->where('data_order_type',PaymentReceipt::data_type_one)
  472. ->whereIn('data_order_no',$search)
  473. ->where('data_type',$data['type'])
  474. ->select('payment_receipt_id','data_order_no','amount')
  475. ->get()->toArray();
  476. $info_array = [];
  477. foreach ($info as $value){
  478. if($payment_receipt_id > 0 && $value['payment_receipt_id'] == $payment_receipt_id) continue;
  479. if(isset($info_array[$value['data_order_no']])){
  480. $info_array[$value['data_order_no']] += $value['amount'];
  481. }else{
  482. $info_array[$value['data_order_no']] = $value['amount'];
  483. }
  484. }
  485. //退货退款金额
  486. $return_exchange = (new ReturnExchangeOrderService())->getDifferentAmountALL(array_column($result,'id'));
  487. if($data['type'] == PaymentReceipt::type_one){
  488. $return_exchange_array = [];
  489. foreach ($result_map as $key => $value){
  490. // order_number => amount
  491. if(isset($return_exchange[$key])) $return_exchange_array[$value] = $return_exchange[$key];
  492. }
  493. foreach ($result as $value){
  494. //收款 = 总金额 - 已收 - 退货退款
  495. //$max = $value['total_amount'] - $tmp_receipt - $tmp_return;
  496. $tmp_receipt = $info_array[$value['order_number']] ?? 0;
  497. $tmp_return = $return_exchange_array[$value['order_number']] ?? 0;
  498. $max = bcsub(bcsub($value['total_amount'], $tmp_receipt,2), $tmp_return, 2);
  499. if($map[$value['order_number']] > $max) return [false, $value['order_number'] . '的金额填写上限是' . $max];
  500. }
  501. }elseif($data['type'] == PaymentReceipt::type_three){
  502. //收款金额(审核后)
  503. $info_one = PaymentReceiptInfo::from('payment_receipt_info as a')
  504. ->leftJoin('payment_receipt as b','b.id','a.payment_receipt_id')
  505. ->where('b.state',PaymentReceipt::STATE_TWO)
  506. ->where('b.del_time',0)
  507. ->where('a.del_time',0)
  508. ->where('a.type',PaymentReceiptInfo::type_three)
  509. ->where('a.data_order_type',PaymentReceipt::data_type_one)
  510. ->whereIn('a.data_order_no',$search)
  511. ->where('a.data_type',PaymentReceipt::type_one)
  512. ->select('a.payment_receipt_id','a.data_order_no','a.amount')
  513. ->get()->toArray();
  514. $info_one_array = [];
  515. foreach ($info_one as $value){
  516. if(isset($info_one_array[$value['data_order_no']])){
  517. $info_one_array[$value['data_order_no']] += $value['amount'];
  518. }else{
  519. $info_one_array[$value['data_order_no']] = $value['amount'];
  520. }
  521. }
  522. foreach ($result as $value){
  523. //红冲 = 审核后的收款单金额 - 已红冲
  524. $tmp_receipt = $info_one_array[$value['order_number']] ?? 0;
  525. $tmp_return = $info_array[$value['order_number']] ?? 0;
  526. $max = bcsub($tmp_receipt, $tmp_return, 2);
  527. if($map[$value['order_number']] > $max) return [false, $value['order_number'] . '的金额填写上限是' . $max];
  528. }
  529. }
  530. }elseif ($data['data_type'] == PaymentReceipt::data_type_two){//采购逻辑
  531. //总金额
  532. $result = PurchaseOrder::where('del_time',0)
  533. ->whereIn('order_number',$search)
  534. ->select('id','order_number','purchase_total as total_amount')
  535. ->get()->toArray();
  536. if(count($search) != count($result)) {
  537. foreach ($result as $value){
  538. if(! isset($map[$value['order_number']])) return [false, $value['order_number'] . '不存在或已被删除'];
  539. }
  540. }
  541. $result_map = array_column($result,'order_number','id');
  542. //收付款金额
  543. $info = PaymentReceiptInfo::where('del_time',0)
  544. ->where('type',PaymentReceiptInfo::type_three)
  545. ->where('data_order_type',PaymentReceipt::data_type_two)
  546. ->whereIn('data_order_no',$search)
  547. ->where('data_type',$data['type'])
  548. ->select('payment_receipt_id','data_order_no','amount')
  549. ->get()->toArray();
  550. $info_array = [];
  551. foreach ($info as $value){
  552. if($payment_receipt_id > 0 && $value['payment_receipt_id'] == $payment_receipt_id) continue;
  553. if(isset($info_array[$value['data_order_no']])){
  554. $info_array[$value['data_order_no']] += $value['amount'];
  555. }else{
  556. $info_array[$value['data_order_no']] = $value['amount'];
  557. }
  558. }
  559. //退货退款金额
  560. $return_exchange = (new ReturnExchangeOrderService())->getDifferentAmountALL(array_column($result,'id'),1);
  561. if($data['type'] == PaymentReceipt::type_one){
  562. $return_exchange_array = [];
  563. foreach ($result_map as $key => $value){
  564. // order_number => amount
  565. if(isset($return_exchange[$key])) $return_exchange_array[$value] = $return_exchange[$key];
  566. }
  567. foreach ($result as $value){
  568. //收款 = 总金额 - 已收 - 退货退款
  569. //$max = $value['total_amount'] - $tmp_receipt - $tmp_return;
  570. $tmp_receipt = $info_array[$value['order_number']] ?? 0;
  571. $tmp_return = $return_exchange_array[$value['order_number']] ?? 0;
  572. $max = bcsub(bcsub($value['total_amount'], $tmp_receipt,2), $tmp_return, 2);
  573. if($map[$value['order_number']] > $max) return [false, $value['order_number'] . '的金额填写上限是' . $max];
  574. }
  575. }elseif($data['type'] == PaymentReceipt::type_three){
  576. //收款金额(审核后)
  577. $info_one = PaymentReceiptInfo::from('payment_receipt_info as a')
  578. ->leftJoin('payment_receipt as b','b.id','a.payment_receipt_id')
  579. ->where('b.state',PaymentReceipt::STATE_TWO)
  580. ->where('b.del_time',0)
  581. ->where('a.del_time',0)
  582. ->where('a.type',PaymentReceiptInfo::type_three)
  583. ->where('a.data_order_type',PaymentReceipt::data_type_one)
  584. ->whereIn('a.data_order_no',$search)
  585. ->where('a.data_type',PaymentReceipt::type_one)
  586. ->select('a.payment_receipt_id','a.data_order_no','a.amount')
  587. ->get()->toArray();
  588. $info_one_array = [];
  589. foreach ($info_one as $value){
  590. if(isset($info_one_array[$value['data_order_no']])){
  591. $info_one_array[$value['data_order_no']] += $value['amount'];
  592. }else{
  593. $info_one_array[$value['data_order_no']] = $value['amount'];
  594. }
  595. }
  596. foreach ($result as $value){
  597. //红冲 = 审核后的收款单金额 - 已红冲
  598. $tmp_receipt = $info_one_array[$value['order_number']] ?? 0;
  599. $tmp_return = $info_array[$value['order_number']] ?? 0;
  600. $max = bcsub($tmp_receipt, $tmp_return, 2);
  601. if($map[$value['order_number']] > $max) return [false, $value['order_number'] . '的金额填写上限是' . $max];
  602. }
  603. }
  604. }
  605. return [true, ''];
  606. }
  607. public function fillData($data){
  608. if(empty($data['data'])) return $data;
  609. $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
  610. ->pluck('emp_name','id')
  611. ->toArray();
  612. $array = array_unique(array_merge_recursive(array_column($data['data'],'account'),array_column($data['data'],'pay_way')));
  613. $basic_map = BasicType::whereIn('id',$array)
  614. ->pluck('title','id')
  615. ->toArray();
  616. $map = [];
  617. $info = PaymentReceiptInfo::where('del_time',0)
  618. ->where('type',PaymentReceiptInfo::type_three)
  619. ->whereIn('payment_receipt_id', array_unique(array_column($data['data'],'id')))
  620. ->select('payment_receipt_id','data_order_no')
  621. ->get()->toArray();
  622. foreach ($info as $value){
  623. $map[$value['payment_receipt_id']][] = $value['data_order_no'];
  624. }
  625. //订单状态数据组织
  626. $state_array = $this->getStateMake($data['data']);
  627. foreach ($data['data'] as $key => $value){
  628. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  629. $data['data'][$key]['payment_receipt_date'] = $value['payment_receipt_date'] ? date('Y-m-d',$value['payment_receipt_date']) : '';
  630. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  631. $data['data'][$key]['state_title'] = $this->makeState($data['data'][$key], $state_array);
  632. $data['data'][$key]['type_title'] = PaymentReceipt::$model_type[$value['type']] ?? '';
  633. $data['data'][$key]['data_type_title'] = PaymentReceipt::$data_type[$value['data_type']] ?? '';
  634. $data['data'][$key]['account_title'] = $basic_map[$value['account']] ?? '';
  635. $data['data'][$key]['pay_way_title'] = $basic_map[$value['pay_way']] ?? '';
  636. $data['data'][$key]['data_order_no'] = $map[$value['id']] ?? [];
  637. }
  638. return $data;
  639. }
  640. public function getStateMake($data){
  641. if(empty($data)) return [];
  642. $order_no = [];
  643. foreach ($data as $value){
  644. if(! in_array($value['state'], [PaymentReceipt::State_minus_one,PaymentReceipt::STATE_ONE])) continue;
  645. $order_no[] = $value['order_number'];
  646. }
  647. return (new OaService())->getOaTeamDetailList($order_no);
  648. }
  649. public function makeState($value, $state_array){
  650. if(! empty($state_array[$value['order_number']])){
  651. $return = $state_array[$value['order_number']];
  652. if($value['state'] == PaymentReceipt::State_minus_one){
  653. $state = "驳回:" . $return;
  654. }else{
  655. $state = "待" . $return . "审核";
  656. }
  657. }elseif($value['state'] == PaymentReceipt::STATE_ZERO){
  658. $state = "待" . $value['crt_name'] . "提交";
  659. }else{
  660. $state = PaymentReceipt::$name[$value['state']] ?? '';
  661. }
  662. return $state;
  663. }
  664. //详情里
  665. public function getPaymentReceiptDataList($data,$type){
  666. $data['data_order_no'] = $data['order_number'];
  667. $info_array = PaymentReceiptInfo::from('payment_receipt_info as a')
  668. ->leftJoin('payment_receipt as b','b.id','a.payment_receipt_id')
  669. ->where('b.del_time',0)
  670. ->where('a.del_time',0)
  671. ->where('a.type',PaymentReceiptInfo::type_three)
  672. ->where('a.data_order_type',$type)
  673. ->where('a.data_order_no',$data['data_order_no'])
  674. ->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')
  675. ->get()->toArray();
  676. $emp_id = PaymentReceiptInfo::where('del_time',0)
  677. ->whereIn('payment_receipt_id',array_column($info_array,'payment_receipt_id'))
  678. ->where('type',PaymentReceiptInfo::type_two)
  679. ->get()->toArray();
  680. $info = [];
  681. if(! empty($emp_id)){
  682. $emp_map = Employee::whereIn('id',array_unique(array_column($emp_id,'data_id')))
  683. ->pluck('emp_name','id')
  684. ->toArray();
  685. foreach ($emp_id as $value){
  686. $name = $emp_map[$value['data_id']] ?? "";
  687. if(isset($info[$value['payment_receipt_id']])){
  688. $info[$value['payment_receipt_id']] .= ',' . $name;
  689. }else{
  690. $info[$value['payment_receipt_id']] = $name;
  691. }
  692. }
  693. }
  694. //四个金额类型
  695. $one = $two = $three = $four = $not_confirm_receipt_amount = 0;
  696. foreach ($info_array as $key => $value){
  697. //归属人
  698. $info_array[$key]['belong'] = $info[$value['payment_receipt_id']] ?? '';
  699. $info_array[$key]['state_title'] = PaymentReceipt::$name[$value['state']] ?? '';
  700. $info_array[$key]['type_title'] = PaymentReceipt::$model_type[$value['data_type']] ?? '';
  701. $info_array[$key]['payment_receipt_date'] = $value['payment_receipt_date'] ? date('Y-m-d',$value['payment_receipt_date']) : '';
  702. if($value['data_type'] == PaymentReceipt::type_one) $not_confirm_receipt_amount += $value['amount'];
  703. if($value['data_type'] == PaymentReceipt::type_one && $value['state'] == PaymentReceipt::STATE_TWO){
  704. $one += $value['amount'];
  705. }elseif ($value['data_type'] == PaymentReceipt::type_three && $value['state'] == PaymentReceipt::STATE_TWO){
  706. $three += $value['amount'];
  707. }
  708. }
  709. $return['receipt_amount'] = bcsub($one, $three, 2); // 已回款金额
  710. $return['not_receipt_amount'] = 0;
  711. $return['red_amount'] = $three;// 已红冲金额
  712. $return['bad_amount'] = $four;
  713. $return['all_count'] = count($info_array);
  714. $return['not_confirm_receipt_amount'] = $not_confirm_receipt_amount;
  715. $return['list'] = $info_array;
  716. return $return;
  717. }
  718. //列表里 默认:(收款)
  719. public function getPaymentReceiptDataCountList($data){
  720. $data_order_no = $data;
  721. if(empty($data_order_no)) return [];
  722. $order = PaymentReceiptInfo::from('payment_receipt_info as a')
  723. ->leftJoin('payment_receipt as b','b.id','a.payment_receipt_id')
  724. ->where('b.del_time',0)
  725. ->where('a.del_time',0)
  726. ->where('a.type',PaymentReceiptInfo::type_three)
  727. ->whereIn('a.data_order_no',$data_order_no)
  728. ->select('a.payment_receipt_id','a.data_order_no','a.amount','a.data_type','b.state','b.payment_receipt_date')
  729. ->get()->toArray();
  730. // $order = PaymentReceiptInfo::where('del_time',0)
  731. // ->where('type',PaymentReceiptInfo::type_three)
  732. // ->whereIn('data_order_no',$data_order_no)
  733. // ->get()->toArray();
  734. // 所有状态都统计 审核成功的统计
  735. $return = $return1 = [];
  736. foreach ($order as $value){
  737. $key = $value['data_order_no'] . $value['data_type'];
  738. if(isset($return[$key])){
  739. $return[$key] += $value['amount'];
  740. }else{
  741. $return[$key] = $value['amount'];
  742. }
  743. if($value['state'] == PaymentReceipt::STATE_TWO){
  744. if(isset($return1[$key])){
  745. $return1[$key] += $value['amount'];
  746. }else{
  747. $return1[$key] = $value['amount'];
  748. }
  749. }
  750. }
  751. return [$return, $return1];
  752. }
  753. public function maked(){
  754. $payment = PaymentReceipt::where('del_time',0)
  755. ->where('data_order_no','LIKE', '%'."T9SO.".'%')
  756. ->where('amount','>',0)
  757. ->get()->toArray();
  758. $insert = [];
  759. foreach ($payment as $value){
  760. $insert[] = [
  761. 'payment_receipt_id' => $value['id'],
  762. 'crt_time' => $value['crt_time'],
  763. 'type' => PaymentReceiptInfo::type_three,
  764. 'data_order_no' => $value['data_order_no'],
  765. 'amount' => $value['amount'],
  766. 'data_order_type' => $value['type'],
  767. 'data_type' => PaymentReceipt::type_one,
  768. ];
  769. }
  770. if(! empty($insert)) PaymentReceiptInfo::insert($insert);
  771. }
  772. }