CostManagementService.php 5.9 KB

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