PaymentReceiptService.php 38 KB

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