PaymentReceiptService.php 47 KB

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