CostManagementService.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. namespace App\Service;
  3. use App\Model\CustomerSupply;
  4. use App\Model\Employee;
  5. use App\Model\CostManagement;
  6. use App\Model\CostManagementDetails;
  7. use Illuminate\Support\Facades\DB;
  8. class CostManagementService extends Service
  9. {
  10. public function costManagementEdit($data,$user){
  11. list($status,$msg) = $this->costManagementRule($data, $user, false);
  12. if(!$status) return [$status,$msg];
  13. try {
  14. DB::beginTransaction();
  15. $model = CostManagement::where('id',$data['id'])->first();
  16. $model->customer_supply_id = $data['customer_supply_id'] ?? 0;
  17. $model->order_time = $data['order_time'] ?? 0;
  18. $model->man_mark = $data['man_mark'] ?? '';
  19. $model->mark = $data['mark'] ?? '';
  20. $model->amount = $data['amount'] ?? 0;
  21. $model->is_open = $data['is_open'] ?? 1;
  22. $model->save();
  23. DB::commit();
  24. }catch (\Exception $exception){
  25. DB::rollBack();
  26. return [false,$exception->getMessage()];
  27. }
  28. return [true, ''];
  29. }
  30. public function costManagementAdd($data,$user){
  31. list($status,$msg) = $this->costManagementRule($data, $user);
  32. if(!$status) return [$status,$msg];
  33. try {
  34. DB::beginTransaction();
  35. $model = new CostManagement();
  36. $model->customer_supply_id = $data['customer_supply_id'] ?? 0;
  37. $model->order_time = $data['order_time'] ?? 0;
  38. $model->man_mark = $data['man_mark'] ?? '';
  39. $model->mark = $data['mark'] ?? '';
  40. $model->amount = $data['amount'] ?? 0;
  41. $model->crt_id = $user['id'];
  42. $model->is_open = $data['is_open'] ?? 1;
  43. $model->save();
  44. DB::commit();
  45. }catch (\Exception $exception){
  46. DB::rollBack();
  47. return [false,$exception->getMessage()];
  48. }
  49. return [true, ''];
  50. }
  51. private function saveDetail($id, $time, $data){
  52. }
  53. private function getDetail($id){
  54. }
  55. public function costManagementDel($data){
  56. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  57. try {
  58. DB::beginTransaction();
  59. $time = time();
  60. CostManagement::where('del_time',0)
  61. ->whereIn('id',$data['id'])
  62. ->update(['del_time' => $time]);
  63. DB::commit();
  64. }catch (\Exception $exception){
  65. DB::rollBack();
  66. return [false,$exception->getMessage()];
  67. }
  68. return [true, ''];
  69. }
  70. public function costManagementDetail($data, $user){
  71. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  72. $customer = CostManagement::where('del_time',0)
  73. ->where('id',$data['id'])
  74. ->first();
  75. if(empty($customer)) return [false,'费用不存在或已被删除'];
  76. $customer = $customer->toArray();
  77. $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('emp_name');
  78. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  79. $customer['order_time'] = $customer['order_time'] ? date("Y-m-d",$customer['order_time']): '';
  80. $customer['customer_supply_title'] = $customer['customer_supply_id'] ? CustomerSupply::where('id',$customer['customer_supply_id'])->value('title') : "";
  81. return [true, $customer];
  82. }
  83. public function costManagementCommon($data,$user, $field = []){
  84. if(empty($field)) $field = CostManagement::$field;
  85. $model = CostManagement::Clear($user,$data);
  86. $model = $model->where('del_time',0)
  87. ->select($field)
  88. ->orderby('id', 'desc');
  89. if(! empty($data['man_mark'])) $model->where('man_mark', 'LIKE', '%'.$data['man_mark'].'%');
  90. if(! empty($data['id'])) $model->whereIn('id', $data['id']);
  91. if(isset($data['is_open'])) $model->where('is_open', $data['is_open']);
  92. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  93. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  94. $model->where('crt_time','>=',$return[0]);
  95. $model->where('crt_time','<=',$return[1]);
  96. }
  97. return $model;
  98. }
  99. public function costManagementList($data,$user){
  100. $model = $this->costManagementCommon($data, $user);
  101. $list = $this->limit($model,'',$data);
  102. $list = $this->fillData($list,$user,$data);
  103. $list['count'] = $this->countTotal($list['data'], $user['header_default']);
  104. return [true, $list];
  105. }
  106. public function costManagementRule(&$data, $user, $is_add = true){
  107. if(empty($data['order_time'])) return [false, '费用日期不能为空'];
  108. $data['order_time'] = $this->changeDateToDate($data['order_time']);
  109. if(empty($data['amount'])) return [false, '总金额不能为空'];
  110. $res = $this->checkNumber($data['amount'],2,'positive');
  111. if(! $res['valid']) return [false,'总金额:' . $res['error']];
  112. if(! isset($data['is_open']) || ! isset(CostManagement::IS_OPEN_title[$data['is_open']])) return [false, '费用状态不能为空'];
  113. if($is_add){
  114. }else{
  115. if(empty($data['id'])) return [false,'ID不能为空'];
  116. $bool = CostManagement::where('id','<>',$data['id'])
  117. ->where('del_time',0)
  118. ->exists();
  119. if(! $bool) return [false, '费用不存在'];
  120. }
  121. return [true, ''];
  122. }
  123. public function fillData($data, $user, $search){
  124. if(empty($data['data'])) return $data;
  125. $emp = (new EmployeeService())->getEmployeeMap(array_unique(array_column($data['data'],'crt_id')));
  126. $emp_2 = (new CustomerSupplyService())->getEmployeeMap(array_unique(array_column($data['data'],'customer_supply_id')));
  127. foreach ($data['data'] as $key => $value){
  128. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  129. $data['data'][$key]['order_time'] = $value['order_time'] ? date('Y-m-d',$value['order_time']) : '';
  130. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  131. $data['data'][$key]['customer_supply_title'] = $emp_2[$value['customer_supply_id']] ?? '';
  132. $data['data'][$key]['is_open_title'] = CostManagement::IS_OPEN_title[$value['is_open']] ?? '';
  133. }
  134. return $data;
  135. }
  136. }