PaymentReceiptService.php 44 KB

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