SupplierService.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. namespace App\Service;
  3. use App\Model\SeeRange;
  4. use App\Model\Supplier;
  5. use App\Model\Employee;
  6. use Illuminate\Support\Facades\DB;
  7. class SupplierService extends Service
  8. {
  9. /**
  10. * 供应商编辑
  11. * @param $data
  12. * @param $user
  13. * @return array
  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 = Supplier::where('id',$data['id'])->first();
  21. $model->title = $data['title'];
  22. $model->code = $data['code'];
  23. $model->address1 = ! empty($data['address1']) ? json_encode($data['address1']) : '';
  24. $model->address2 = $data['address2'] ?? '';
  25. $model->mark = $data['mark'] ?? '';
  26. $model->is_main = $data['is_main'] ?? 0;
  27. $model->save();
  28. DB::commit();
  29. }catch (\Exception $exception){
  30. DB::rollBack();
  31. return [false,$exception->getMessage()];
  32. }
  33. return [true,''];
  34. }
  35. /**
  36. * 供应商新增
  37. * @param $data
  38. * @param $user
  39. * @return array
  40. */
  41. public function customerAdd($data,$user){
  42. list($status,$msg) = $this->customerRule($data,$user);
  43. if(!$status) return [$status,$msg];
  44. try {
  45. DB::beginTransaction();
  46. $model = new Supplier();
  47. $model->title = $data['title'];
  48. $model->code = $data['code'];
  49. $model->mobile = $data['mobile'] ?? "";
  50. $model->address1 = ! empty($data['address1']) ? json_encode($data['address1']) : '';
  51. $model->address2 = $data['address2'] ?? '';
  52. $model->crt_id = $user['id'];
  53. $model->mark = $data['mark'] ?? '';
  54. $model->depart_id = $data['depart_id'] ?? 0;
  55. $model->top_depart_id = $data['top_depart_id'] ?? 0;
  56. $model->is_main = $data['is_main'] ?? 0;
  57. $model->save();
  58. DB::commit();
  59. }catch (\Exception $exception){
  60. DB::rollBack();
  61. return [false,$exception->getMessage()];
  62. }
  63. return [true,''];
  64. }
  65. /**
  66. * 供应商删除
  67. * @param $data
  68. * @return array
  69. */
  70. public function customerDel($data){
  71. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  72. try {
  73. DB::beginTransaction();
  74. Supplier::where('id',$data['id'])->update([
  75. 'del_time'=> time()
  76. ]);
  77. (new RangeService())->RangeDelete($data['id'],SeeRange::type_nine);
  78. DB::commit();
  79. }catch (\Exception $exception){
  80. DB::rollBack();
  81. return [false,$exception->getMessage()];
  82. }
  83. return [true,''];
  84. }
  85. /**
  86. * 供应商详情
  87. * @param $data
  88. * @return array
  89. */
  90. public function customerDetail($data){
  91. if($this->isEmpty($data,'id')) return [false,'请选择数据!'];
  92. $customer = Supplier::where('del_time',0)
  93. ->where('id',$data['id'])
  94. ->first();
  95. if(empty($customer)) return [false,'供应商不存在或已被删除'];
  96. $customer = $customer->toArray();
  97. $address = '';
  98. if(! empty($customer['address1'])) {
  99. $tmp = json_decode($customer['address1'],true);
  100. $customer['address1'] = $tmp;
  101. $tmp = implode(' ',$tmp);
  102. $tmp .= ' ' . $customer['address2'];
  103. $address = $tmp;
  104. }
  105. $customer['address'] = $address;
  106. $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('emp_name');
  107. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  108. //可见范围
  109. $return = (new RangeService())->RangeDetail($customer['id'],SeeRange::type_nine);
  110. $customer['depart'] = $return[0] ?? [];
  111. $customer['employee'] = $return[1] ?? [];
  112. return [true, $customer];
  113. }
  114. /**
  115. * 供应商列表
  116. * @param $data
  117. * @param $user
  118. * @return array
  119. */
  120. public function customerList($data,$user){
  121. $model = Supplier::Clear($user,$data);
  122. $model = $model->where('del_time',0)
  123. ->select('id','title','address1','address2','mobile','crt_id','crt_time','is_main','mark','code')
  124. ->orderby('id', 'asc');
  125. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  126. if(! empty($data['mobile'])) $model->where('mobile', 'LIKE', '%'.$data['mobile'].'%');
  127. if(! empty($data['mark'])) $model->where('mark', 'LIKE', '%'.$data['mark'].'%');
  128. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  129. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  130. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  131. $model->where('crt_time','>=',$return[0]);
  132. $model->where('crt_time','<=',$return[1]);
  133. }
  134. if(! empty($data['get_all'])) $model->orWhere('is_main',Supplier::is_main);
  135. $list = $this->limit($model,'',$data);
  136. $list = $this->fillData($list);
  137. return [true, $list];
  138. }
  139. /**
  140. * 供应商参数规则
  141. * @param $data
  142. * @param $is_add
  143. * @return array
  144. */
  145. public function customerRule(&$data, $user, $is_add = true){
  146. if(empty($data['title'])) return [false,'供应商名称不能为空'];
  147. if(empty($data['code'])) return [false,'供应商编码不能为空'];
  148. //所属部门 以及 顶级部门
  149. if(empty($data['depart_id'])) {
  150. $data['depart_id'] = $this->getDepart($user);
  151. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  152. }
  153. if($is_add){
  154. $bool = Supplier::where('del_time',0)
  155. ->where('title',$data['title'])
  156. ->where('top_depart_id',$data['top_depart_id'])
  157. ->exists();
  158. }else{
  159. if(empty($data['id'])) return [false,'ID不能为空'];
  160. $bool = Supplier::where('del_time',0)
  161. ->where('id','<>',$data['id'])
  162. ->where('top_depart_id',$data['top_depart_id'])
  163. ->where('title',$data['title'])
  164. ->exists();
  165. }
  166. if($bool) return [false,'供应商名称不能重复'];
  167. return [true, ''];
  168. }
  169. /**
  170. * 拼接数据
  171. * @param $data
  172. * @return array
  173. */
  174. public function fillData($data){
  175. if(empty($data['data'])) return $data;
  176. $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
  177. ->pluck('emp_name','id')
  178. ->toArray();
  179. foreach ($data['data'] as $key => $value){
  180. $address = '';
  181. if(! empty($value['address1'])) {
  182. $tmp = json_decode($value['address1'],true);
  183. $tmp = implode(' ',$tmp);
  184. $tmp .= ' ' . $value['address2'];
  185. $address = $tmp;
  186. }
  187. $data['data'][$key]['address'] = $address;
  188. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  189. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  190. }
  191. return $data;
  192. }
  193. }