BookingListService.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. namespace App\Service;
  3. use App\Model\BookingList;
  4. use App\Model\BookingListInfo;
  5. use App\Model\Employee;
  6. use App\Model\SalesOrder;
  7. use Illuminate\Support\Facades\DB;
  8. class BookingListService extends Service
  9. {
  10. public function customerEdit($data,$user){
  11. list($status,$msg) = $this->customerRule($data,$user, false);
  12. if(!$status) return [$status,$msg];
  13. try {
  14. DB::beginTransaction();
  15. $model = BookingList::where('id',$data['id'])->first();
  16. $model->type = $data['type'];
  17. $model->mark = $data['mark'] ?? '';
  18. $model->amount = $data['amount'] ?? 0;
  19. $model->save();
  20. $time = time();
  21. BookingListInfo::where('del_time',0)
  22. ->where('booking_list_id',$data['id'])
  23. ->update(['del_time' => $time]);
  24. if(! empty($data['file'])){
  25. $insert = [];
  26. foreach ($data['file'] as $value){
  27. $insert[] = [
  28. 'booking_list_id' => $model->id,
  29. 'file' => $value['url'],
  30. 'type' => BookingListInfo::type_one,
  31. 'name' => $value['name'],
  32. 'crt_time' => $time,
  33. ];
  34. }
  35. BookingListInfo::insert($insert);
  36. }
  37. DB::commit();
  38. }catch (\Exception $exception){
  39. DB::rollBack();
  40. return [false,$exception->getMessage()];
  41. }
  42. return [true,''];
  43. }
  44. public function customerAdd($data,$user){
  45. list($status,$msg) = $this->customerRule($data,$user);
  46. if(!$status) return [$status,$msg];
  47. try {
  48. DB::beginTransaction();
  49. $model = new BookingList();
  50. $model->data_id = $data['data_id'];
  51. $model->data_type = $data['data_type'] ?? 0;
  52. $model->type = $data['type'] ?? 0;
  53. $model->amount = $data['amount'] ?? 0;
  54. $model->mark = $data['mark'] ?? '';
  55. $model->crt_id = $user['id'];
  56. $model->save();
  57. if(! empty($data['file'])){
  58. $insert = [];
  59. foreach ($data['file'] as $value){
  60. $insert[] = [
  61. 'booking_list_id' => $model->id,
  62. 'file' => $value['url'],
  63. 'type' => BookingListInfo::type_one,
  64. 'name' => $value['name'],
  65. 'crt_time' => time(),
  66. ];
  67. }
  68. BookingListInfo::insert($insert);
  69. }
  70. DB::commit();
  71. }catch (\Exception $exception){
  72. DB::rollBack();
  73. return [false,$exception->getMessage()];
  74. }
  75. return [true,''];
  76. }
  77. public function customerDel($data){
  78. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  79. $booking = BookingList::where('del_time',0)->where('id',$data['id'])->first();
  80. if(empty($booking)) return [false,'记录不存在或已被删除'];
  81. $booking = $booking->toArray();
  82. if($booking['state'] != BookingList::STATE_ZERO) return [false,'请确认记录状态,删除失败'];
  83. try {
  84. DB::beginTransaction();
  85. $time = time();
  86. BookingList::where('id',$data['id'])->update([
  87. 'del_time'=> $time
  88. ]);
  89. BookingListInfo::where('del_time',0)
  90. ->where('booking_list_id',$data['id'])
  91. ->update(['del_time' => $time]);
  92. DB::commit();
  93. }catch (\Exception $exception){
  94. DB::rollBack();
  95. return [false,$exception->getMessage()];
  96. }
  97. return [true,''];
  98. }
  99. public function customerDetail($data){
  100. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  101. $customer = BookingList::where('del_time',0)
  102. ->where('id',$data['id'])
  103. ->first();
  104. if(empty($customer)) return [false,'记录不存在或已被删除'];
  105. $customer = $customer->toArray();
  106. $customer['state_title'] = BookingList::$name[$customer['state']] ?? "";
  107. $customer['type_title'] = BookingList::$model_type[$customer['type']] ?? "";
  108. $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('emp_name');
  109. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  110. $file = BookingListInfo::where('del_time',0)
  111. ->where('booking_list_id',$data['id'])
  112. ->get()->toArray();
  113. $customer['file'] = [];
  114. foreach ($file as $value){
  115. if($value['type'] == BookingListInfo::type_one){
  116. $tmp = [
  117. 'url' => $value['file'],
  118. 'name' => $value['name'],
  119. ];
  120. $customer['file'][] = $tmp;
  121. }
  122. }
  123. return [true, $customer];
  124. }
  125. public function customerList($data,$user){
  126. $model = BookingList::where('del_time',0)
  127. ->select('id','data_id','data_type','type','amount','crt_id','crt_time','state','mark')
  128. ->orderby('id', 'asc');
  129. if(! empty($data['data_id'])) $model->where('data_id', $data['data_id']);
  130. if(! empty($data['data_type'])) $model->where('data_type',$data['data_type']);
  131. $list = $this->limit($model,'',$data);
  132. $list = $this->fillData($list);
  133. return [true, $list];
  134. }
  135. public function customerRule(&$data, $user, $is_add = true){
  136. if(empty($data['data_id'])) return [false,'数据ID不能为空'];
  137. if(empty($data['data_type'])) return [false,'数据类型不能为空'];
  138. if(empty($data['type'])) return [false,'金额类型不能为空'];
  139. if(empty($data['amount'])) return [false,'金额不能为空'];
  140. $res = $this->checkNumber($data['amount']);
  141. if(! $res) return [false, '金额请输入不超过两位小数并且大于0的数值'];
  142. if($data['data_type'] == BookingList::data_type_one){
  143. $bool = SalesOrder::where('id',$data['data_id'])->where('state',SalesOrder::State2_zero)->exists();
  144. if($bool) return [false,'合同还未指派,添加金额记录失败'];
  145. }
  146. if($is_add){
  147. }else{
  148. if(empty($data['id'])) return [false,'ID不能为空'];
  149. $booking = BookingList::where('del_time',0)->where('id',$data['id'])->first();
  150. if(empty($booking)) return [false,'记录不存在或已被删除'];
  151. $booking = $booking->toArray();
  152. if($booking['state'] != BookingList::STATE_ZERO) return [false,'请确认记录状态,编辑失败'];
  153. }
  154. return [true, ''];
  155. }
  156. public function fillData($data){
  157. if(empty($data['data'])) return $data;
  158. $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
  159. ->pluck('emp_name','id')
  160. ->toArray();
  161. foreach ($data['data'] as $key => $value){
  162. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  163. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  164. $data['data'][$key]['state_title'] = BookingList::$name[$value['state']] ?? '';
  165. $data['data'][$key]['type_title'] = BookingList::$model_type[$value['type']] ?? '';
  166. }
  167. return $data;
  168. }
  169. public function customerConfirm($data){
  170. if(empty($data['id'])) return [false,'记录ID不能为空'];
  171. $customer = BookingList::where('del_time',0)
  172. ->where('id',$data['id'])
  173. ->first();
  174. if(empty($customer)) return [false,'记录不存在或已被删除'];
  175. $customer = $customer->toArray();
  176. if($customer['state'] != BookingList::STATE_ZERO) return [false,'请确认记录状态,确认金额失败'];
  177. BookingList::where('id',$data['id'])->update(['state' => BookingList::STATE_ONE]);
  178. return [true,''];
  179. }
  180. public function getAllAmount($data_id = [], $data_type = 0){
  181. if(empty($data_id) || empty($data_type)) return [];
  182. $booking = BookingList::where('del_time',0)
  183. ->where('state',BookingList::STATE_ONE)
  184. ->whereIn('data_id',$data_id)
  185. ->where('data_type',$data_type)
  186. ->select('data_id','amount','type')
  187. ->get()->toArray();
  188. $return = [];
  189. foreach ($booking as $value){
  190. if(isset($return[$value['data_id']][$value['type']])){
  191. $return[$value['data_id']][$value['type']] += $value['amount'];
  192. }else{
  193. $return[$value['data_id']][$value['type']] = $value['amount'];
  194. }
  195. }
  196. return $return;
  197. }
  198. }