BookingListService.php 9.2 KB

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