PaymentReceiptService.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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 Illuminate\Support\Facades\DB;
  8. class PaymentReceiptService extends Service
  9. {
  10. public function paymentReceiptGet($data,$user){
  11. $order_number = (new OrderNoService())->createOrderNumber(PaymentReceipt::prefix);
  12. if(! $order_number) return [false,'工单编号生成失败!'];
  13. return [true,['order_number' => $order_number]];
  14. }
  15. public function customerEdit($data,$user){
  16. list($status,$msg) = $this->customerRule($data,$user, false);
  17. if(!$status) return [$status,$msg];
  18. try {
  19. DB::beginTransaction();
  20. $model = PaymentReceipt::where('id',$data['id'])->first();
  21. $model->data_order_no = $data['data_order_no'];
  22. $model->data_type = $data['data_type'];
  23. $model->type = $data['type'];
  24. $model->account = $data['account'] ?? 0;
  25. $model->pay_way = $data['pay_way'] ?? 0;
  26. $model->amount = $data['amount'] ?? 0;
  27. $model->mark = $data['mark'] ?? '';
  28. $model->payment_receipt_date = $data['payment_receipt_date'] ?? 0;
  29. $model->save();
  30. $time = time();
  31. $old = PaymentReceiptInfo::where('del_time',0)
  32. ->where('payment_receipt_id',$data['id'])
  33. ->select('file')
  34. ->get()->toArray();
  35. $old = array_column($old,'file');
  36. PaymentReceiptInfo::where('del_time',0)
  37. ->where('payment_receipt_id',$data['id'])
  38. ->update(['del_time' => $time]);
  39. $new = [];
  40. if(! empty($data['file'])){
  41. $insert = [];
  42. foreach ($data['file'] as $value){
  43. $insert[] = [
  44. 'payment_receipt_id' => $model->id,
  45. 'file' => $value['url'],
  46. 'type' => PaymentReceiptInfo::type_one,
  47. 'name' => $value['name'],
  48. 'crt_time' => $time,
  49. ];
  50. }
  51. $new[]= $value['url'];
  52. PaymentReceiptInfo::insert($insert);
  53. }
  54. if(! empty($data['employee_one'])){
  55. $insert = [];
  56. foreach ($data['employee_one'] as $value){
  57. $insert[] = [
  58. 'payment_receipt_id' => $model->id,
  59. 'data_id' => $value,
  60. 'type' => PaymentReceiptInfo::type_two,
  61. 'crt_time' => $time,
  62. ];
  63. }
  64. PaymentReceiptInfo::insert($insert);
  65. }
  66. DB::commit();
  67. }catch (\Exception $exception){
  68. DB::rollBack();
  69. return [false,$exception->getMessage()];
  70. }
  71. $this->delStorageFile($old, $new);
  72. return [true,''];
  73. }
  74. public function customerAdd($data,$user){
  75. list($status,$msg) = $this->customerRule($data,$user);
  76. if(!$status) return [$status,$msg];
  77. try {
  78. DB::beginTransaction();
  79. $model = new PaymentReceipt();
  80. $model->order_number = $data['order_number'];
  81. $model->data_order_no = $data['data_order_no'];
  82. $model->data_type = $data['data_type'];
  83. $model->type = $data['type'];
  84. $model->account = $data['account'] ?? 0;
  85. $model->pay_way = $data['pay_way'] ?? 0;
  86. $model->amount = $data['amount'] ?? 0;
  87. $model->mark = $data['mark'] ?? '';
  88. $model->crt_id = $user['id'];
  89. $model->depart_id = $data['depart_id'];
  90. $model->top_depart_id = $data['top_depart_id'];
  91. $model->payment_receipt_date = $data['payment_receipt_date'] ?? 0;
  92. $model->save();
  93. $time = time();
  94. if(! empty($data['file'])){
  95. $insert = [];
  96. foreach ($data['file'] as $value){
  97. $insert[] = [
  98. 'payment_receipt_id' => $model->id,
  99. 'file' => $value['url'],
  100. 'type' => PaymentReceiptInfo::type_one,
  101. 'name' => $value['name'],
  102. 'crt_time' => $time,
  103. ];
  104. }
  105. PaymentReceiptInfo::insert($insert);
  106. }
  107. if(! empty($data['employee_one'])){
  108. $insert = [];
  109. foreach ($data['employee_one'] as $value){
  110. $insert[] = [
  111. 'payment_receipt_id' => $model->id,
  112. 'data_id' => $value,
  113. 'type' => PaymentReceiptInfo::type_two,
  114. 'crt_time' => $time,
  115. ];
  116. }
  117. PaymentReceiptInfo::insert($insert);
  118. }
  119. DB::commit();
  120. }catch (\Exception $exception){
  121. DB::rollBack();
  122. return [false,$exception->getMessage()];
  123. }
  124. return [true,''];
  125. }
  126. public function customerDel($data){
  127. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  128. $booking = PaymentReceipt::where('del_time',0)->where('id',$data['id'])->first();
  129. if(empty($booking)) return [false,'收付款单不存在或已被删除'];
  130. $booking = $booking->toArray();
  131. if($booking['state'] != PaymentReceipt::STATE_ZERO) return [false,'请确认收付款单状态,删除失败'];
  132. try {
  133. DB::beginTransaction();
  134. $time = time();
  135. $old = PaymentReceiptInfo::where('del_time',0)
  136. ->where('payment_receipt_id',$data['id'])
  137. ->select('file')
  138. ->get()->toArray();
  139. $old = array_column($old,'file');
  140. PaymentReceipt::where('id',$data['id'])->update([
  141. 'del_time'=> $time
  142. ]);
  143. PaymentReceiptInfo::where('del_time',0)
  144. ->where('payment_receipt_id',$data['id'])
  145. ->update(['del_time' => $time]);
  146. DB::commit();
  147. }catch (\Exception $exception){
  148. DB::rollBack();
  149. return [false,$exception->getMessage()];
  150. }
  151. $this->delStorageFile($old);
  152. return [true,''];
  153. }
  154. public function customerDetail($data){
  155. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  156. $customer = PaymentReceipt::where('del_time',0)
  157. ->where('id',$data['id'])
  158. ->first();
  159. if(empty($customer)) return [false,'记录不存在或已被删除'];
  160. $customer = $customer->toArray();
  161. $array = [
  162. $customer['account'],
  163. $customer['pay_way'],
  164. ];
  165. $basic_map = BasicType::whereIn('id',$array)
  166. ->pluck('title','id')
  167. ->toArray();
  168. $customer['account_title'] = $basic_map[$customer['account']] ?? "";
  169. $customer['pay_way_title'] = $basic_map[$customer['pay_way']] ?? "";
  170. $customer['state_title'] = PaymentReceipt::$name[$customer['state']] ?? "";
  171. $customer['type_title'] = PaymentReceipt::$model_type[$customer['type']] ?? "";
  172. $customer['data_type_title'] = PaymentReceipt::$data_type[$customer['data_type']] ?? "";
  173. $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('emp_name');
  174. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  175. $customer['payment_receipt_date'] = $customer['payment_receipt_date'] ? date("Y-m-d",$customer['payment_receipt_date']): '';
  176. $customer['receipt_amount'] = $customer['state'] == PaymentReceipt::STATE_TWO ? $customer['amount'] : 0;
  177. $file = PaymentReceiptInfo::where('del_time',0)
  178. ->where('payment_receipt_id',$data['id'])
  179. ->get()->toArray();
  180. $emp_id = [];
  181. foreach ($file as $value){
  182. if(in_array($value['type'],PaymentReceiptInfo::$man)){
  183. $emp_id[] = $value['data_id'];
  184. }
  185. }
  186. $emp_map = Employee::whereIn('id',array_unique($emp_id))
  187. ->where('del_time',0)
  188. ->pluck('emp_name','id')
  189. ->toArray();
  190. $customer['file'] = $customer['employee_one'] = [];
  191. foreach ($file as $value){
  192. if($value['type'] == PaymentReceiptInfo::type_one){
  193. $tmp = [
  194. 'url' => $value['file'],
  195. 'name' => $value['name'],
  196. ];
  197. $customer['file'][] = $tmp;
  198. }elseif (in_array($value['type'],PaymentReceiptInfo::$man)){
  199. $tt = $emp_map[$value['data_id']] ?? '';
  200. if(! empty($tt)){
  201. $tmp = [
  202. 'id' => $value['data_id'],
  203. 'name' => $emp_map[$value['data_id']] ?? '',
  204. ];
  205. if($value['type'] == PaymentReceiptInfo::type_two){
  206. $customer['employee_one'][] = $tmp;
  207. }
  208. }
  209. }
  210. }
  211. return [true, $customer];
  212. }
  213. public function customerList($data,$user){
  214. $model = PaymentReceipt::Clear($user,$data);
  215. $model = $model->where('del_time',0)
  216. ->select('type','id','data_type','order_number','data_order_no','amount','account','pay_way','crt_id','crt_time','mark','state','payment_receipt_date')
  217. ->orderby('id', 'desc');
  218. if(isset($data['state'])) $model->where('state', $data['state']);
  219. if(! empty($data['data_order_no'])) $model->where('data_order_no', 'LIKE', '%'.$data['data_order_no'].'%');
  220. if(! empty($data['order_number'])) $model->where('order_number', 'LIKE', '%'.$data['order_number'].'%');
  221. if(! empty($data['data_type'])) $model->where('data_type', $data['data_type']);
  222. if(! empty($data['type'])) $model->where('type', $data['type']);
  223. if(! empty($data['account'])) $model->where('account',$data['account']);
  224. if(! empty($data['pay_way'])) $model->where('pay_way',$data['pay_way']);
  225. if(! empty($data['mark'])) $model->where('mark', 'LIKE', '%'.$data['mark'].'%');
  226. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  227. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  228. $model->where('crt_time','>=',$return[0]);
  229. $model->where('crt_time','<=',$return[1]);
  230. }
  231. if(! empty($data['payment_receipt_date'][0]) && ! empty($data['payment_receipt_date'][1])) {
  232. $return = $this->changeDateToTimeStampAboutRange($data['payment_receipt_date']);
  233. $model->where('payment_receipt_date','>=',$return[0]);
  234. $model->where('payment_receipt_date','<=',$return[1]);
  235. }
  236. if(! empty($data['belong'])){
  237. $id = (new RangeService())->paymentReceiptSearch($data);
  238. $model->whereIn('id',$id);
  239. }
  240. $list = $this->limit($model,'',$data);
  241. $list = $this->fillData($list);
  242. return [true, $list];
  243. }
  244. public function customerRule(&$data, $user, $is_add = true){
  245. if(empty($data['order_number'])) return [false,'收付款单编号不能为空'];
  246. if(empty($data['data_order_no'])) return [false,'关联单号不能为空'];
  247. if(empty($data['data_type'])) return [false,'单号类型不能为空'];
  248. if(empty($data['type'])) return [false,'金额类型不能为空'];
  249. if(empty($data['amount'])) return [false,'金额不能为空'];
  250. $res = $this->checkNumber($data['amount']);
  251. if(! $res) return [false, '金额请输入不超过两位小数并且大于0的数值'];
  252. if(! empty($data['payment_receipt_date'])) $data['payment_receipt_date'] = $this->changeDateToDate($data['payment_receipt_date']);
  253. //所属部门 以及 顶级部门
  254. if(empty($data['depart_id'])) {
  255. $data['depart_id'] = $this->getDepart($user);
  256. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  257. }
  258. if($is_add){
  259. $bool = PaymentReceipt::where('del_time',0)
  260. ->where('order_number',$data['order_number'])
  261. ->exists();
  262. if($bool) return [false,'收付款单编号已存在,请重新获取'];
  263. }else{
  264. if(empty($data['id'])) return [false,'ID不能为空'];
  265. $booking = PaymentReceipt::where('del_time',0)->where('id',$data['id'])->first();
  266. if(empty($booking)) return [false,'收付款单不存在或已被删除'];
  267. $booking = $booking->toArray();
  268. if($booking['state'] != PaymentReceipt::STATE_ZERO) return [false,'请确认收付款单状态,编辑失败'];
  269. }
  270. return [true, ''];
  271. }
  272. public function fillData($data){
  273. if(empty($data['data'])) return $data;
  274. $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
  275. ->pluck('emp_name','id')
  276. ->toArray();
  277. $array = array_unique(array_merge_recursive(array_column($data['data'],'account'),array_column($data['data'],'pay_way')));
  278. $basic_map = BasicType::whereIn('id',$array)
  279. ->pluck('title','id')
  280. ->toArray();
  281. foreach ($data['data'] as $key => $value){
  282. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  283. $data['data'][$key]['payment_receipt_date'] = $value['payment_receipt_date'] ? date('Y-m-d',$value['payment_receipt_date']) : '';
  284. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  285. $data['data'][$key]['state_title'] = PaymentReceipt::$name[$value['state']] ?? '';
  286. $data['data'][$key]['type_title'] = PaymentReceipt::$model_type[$value['type']] ?? '';
  287. $data['data'][$key]['data_type_title'] = PaymentReceipt::$data_type[$value['data_type']] ?? '';
  288. $data['data'][$key]['account_title'] = $basic_map[$value['account']] ?? '';
  289. $data['data'][$key]['pay_way_title'] = $basic_map[$value['pay_way']] ?? '';
  290. }
  291. return $data;
  292. }
  293. public function getPaymentReceiptDataList($data){
  294. $data['data_order_no'] = $data['order_number'];
  295. $order = PaymentReceipt::where('del_time',0)
  296. ->where('data_order_no',$data['data_order_no'])
  297. ->get()->toArray();
  298. $emp_id = PaymentReceiptInfo::where('del_time',0)
  299. ->whereIn('payment_receipt_id',array_column($order,'id'))
  300. ->where('type',PaymentReceiptInfo::type_two)
  301. ->get()->toArray();
  302. $info = [];
  303. if(! empty($emp_id)){
  304. $emp_map = Employee::whereIn('id',array_unique(array_column($emp_id,'data_id')))
  305. ->pluck('emp_name','id')
  306. ->toArray();
  307. foreach ($emp_id as $value){
  308. $name = $emp_map[$value['data_id']] ?? "";
  309. if(isset($info[$value['data_id']])){
  310. $info[$value['data_id']] .= ',' . $name;
  311. }else{
  312. $info[$value['data_id']] = $name;
  313. }
  314. }
  315. }
  316. $finished = $not_finished = 0;
  317. foreach ($order as $key => $value){
  318. $tmp = $info[$value['id']] ?? '';
  319. $order[$key]['belong'] = $tmp;
  320. $order[$key]['state_title'] = PaymentReceipt::$name[$value['state']] ?? '';
  321. $order[$key]['payment_receipt_date'] = $value['payment_receipt_date'] ? date('Y-m-d',$value['payment_receipt_date']) : '';
  322. if($value['state'] == PaymentReceipt::STATE_TWO){
  323. $finished += $value['amount'];
  324. }else{
  325. $not_finished += $value['amount'];
  326. }
  327. }
  328. $return['receipt_amount'] = $finished;
  329. $return['not_receipt_amount'] = $not_finished;
  330. $return['all_count'] = count($order);
  331. $return['list'] = $order;
  332. return $return;
  333. }
  334. public function getPaymentReceiptDeatail($data){
  335. $customer = PaymentReceipt::where('del_time',0)
  336. ->where('crt_time',$data['crt_time'])
  337. ->where('id',$data['id'])
  338. ->first();
  339. if(empty($customer)) return [];
  340. $customer = $customer->toArray();
  341. $array = [
  342. $customer['account'],
  343. $customer['pay_way'],
  344. ];
  345. $basic_map = BasicType::whereIn('id',$array)
  346. ->pluck('title','id')
  347. ->toArray();
  348. $customer['account_title'] = $basic_map[$customer['account']] ?? "";
  349. $customer['pay_way_title'] = $basic_map[$customer['pay_way']] ?? "";
  350. $customer['state_title'] = PaymentReceipt::$name[$customer['state']] ?? "";
  351. $customer['type_title'] = PaymentReceipt::$model_type[$customer['type']] ?? "";
  352. $customer['data_type_title'] = PaymentReceipt::$data_type[$customer['data_type']] ?? "";
  353. $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('emp_name');
  354. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  355. $customer['payment_receipt_date'] = $customer['payment_receipt_date'] ? date("Y-m-d",$customer['payment_receipt_date']): '';
  356. $customer['receipt_amount'] = $customer['state'] == PaymentReceipt::STATE_TWO ? $customer['amount'] : 0;
  357. $file = PaymentReceiptInfo::where('del_time',0)
  358. ->where('payment_receipt_id',$data['id'])
  359. ->get()->toArray();
  360. $emp_id = [];
  361. foreach ($file as $value){
  362. if(in_array($value['type'],PaymentReceiptInfo::$man)){
  363. $emp_id[] = $value['data_id'];
  364. }
  365. }
  366. $emp_map = Employee::whereIn('id',array_unique($emp_id))
  367. ->where('del_time',0)
  368. ->pluck('emp_name','id')
  369. ->toArray();
  370. $customer['file'] = $customer['employee_one'] = [];
  371. foreach ($file as $value){
  372. if($value['type'] == PaymentReceiptInfo::type_one){
  373. $tmp = [
  374. 'url' => $value['file'],
  375. 'name' => $value['name'],
  376. ];
  377. $customer['file'][] = $tmp;
  378. }elseif (in_array($value['type'],PaymentReceiptInfo::$man)){
  379. $tt = $emp_map[$value['data_id']] ?? '';
  380. if(! empty($tt)){
  381. $tmp = [
  382. 'id' => $value['data_id'],
  383. 'name' => $emp_map[$value['data_id']] ?? '',
  384. ];
  385. if($value['type'] == PaymentReceiptInfo::type_two){
  386. $customer['employee_one'][] = $tmp;
  387. }
  388. }
  389. }
  390. }
  391. return $customer;
  392. }
  393. }