SupplierService.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. if(! empty($customer['address1'])) {
  98. $tmp = json_decode($customer['address1'],true);
  99. $customer['address1'] = $tmp;
  100. $tmp = implode(' ',$tmp);
  101. $tmp .= ' ' . $customer['address2'];
  102. $address = $tmp;
  103. }else{
  104. $address = $customer['address2'];
  105. }
  106. $customer['address'] = $address;
  107. $customer['crt_name'] = Employee::where('id',$customer['crt_id'])->value('emp_name');
  108. $customer['crt_time'] = $customer['crt_time'] ? date("Y-m-d H:i:s",$customer['crt_time']): '';
  109. //可见范围
  110. $return = (new RangeService())->RangeDetail($customer['id'],SeeRange::type_nine);
  111. $customer['depart'] = $return[0] ?? [];
  112. $customer['employee'] = $return[1] ?? [];
  113. return [true, $customer];
  114. }
  115. /**
  116. * 供应商列表
  117. * @param $data
  118. * @param $user
  119. * @return array
  120. */
  121. public function customerList($data,$user){
  122. $model = Supplier::Clear($user,$data);
  123. $model = $model->where('del_time',0)
  124. ->select('id','title','address1','address2','mobile','crt_id','crt_time','is_main','mark','code')
  125. ->orderby('id', 'asc');
  126. if(! empty($data['title'])) $model->where('title', 'LIKE', '%'.$data['title'].'%');
  127. if(! empty($data['mobile'])) $model->where('mobile', 'LIKE', '%'.$data['mobile'].'%');
  128. if(! empty($data['mark'])) $model->where('mark', 'LIKE', '%'.$data['mark'].'%');
  129. if(! empty($data['code'])) $model->where('code', 'LIKE', '%'.$data['code'].'%');
  130. if(! empty($data['crt_time'][0]) && ! empty($data['crt_time'][1])) {
  131. $return = $this->changeDateToTimeStampAboutRange($data['crt_time']);
  132. $model->where('crt_time','>=',$return[0]);
  133. $model->where('crt_time','<=',$return[1]);
  134. }
  135. if(! empty($data['get_all'])) $model->orWhere('is_main',Supplier::is_main);
  136. $list = $this->limit($model,'',$data);
  137. $list = $this->fillData($list);
  138. return [true, $list];
  139. }
  140. /**
  141. * 供应商参数规则
  142. * @param $data
  143. * @param $is_add
  144. * @return array
  145. */
  146. public function customerRule(&$data, $user, $is_add = true){
  147. if(empty($data['title'])) return [false,'供应商名称不能为空'];
  148. // if(empty($data['code'])) return [false,'供应商编码不能为空'];
  149. //所属部门 以及 顶级部门
  150. if(empty($data['depart_id'])) {
  151. $data['depart_id'] = $this->getDepart($user);
  152. $data['top_depart_id'] = $user['depart_map'][$data['depart_id']] ?? 0;
  153. }
  154. if($is_add){
  155. $bool = Supplier::where('del_time',0)
  156. ->where('title',$data['title'])
  157. ->where('top_depart_id',$data['top_depart_id'])
  158. ->exists();
  159. }else{
  160. if(empty($data['id'])) return [false,'ID不能为空'];
  161. $bool = Supplier::where('del_time',0)
  162. ->where('id','<>',$data['id'])
  163. ->where('top_depart_id',$data['top_depart_id'])
  164. ->where('title',$data['title'])
  165. ->exists();
  166. }
  167. if($bool) return [false,'供应商名称不能重复'];
  168. return [true, ''];
  169. }
  170. /**
  171. * 拼接数据
  172. * @param $data
  173. * @return array
  174. */
  175. public function fillData($data){
  176. if(empty($data['data'])) return $data;
  177. $emp = Employee::whereIn('id',array_unique(array_column($data['data'],'crt_id')))
  178. ->pluck('emp_name','id')
  179. ->toArray();
  180. foreach ($data['data'] as $key => $value){
  181. if(! empty($value['address1'])) {
  182. $tmp = json_decode($value['address1'],true);
  183. $tmp = implode(' ',$tmp);
  184. $tmp .= ' ' . $value['address2'];
  185. $address = $tmp;
  186. }else{
  187. $address = $value['address2'];
  188. }
  189. $data['data'][$key]['address'] = $address;
  190. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date('Y-m-d H:i:s',$value['crt_time']) : '';
  191. $data['data'][$key]['crt_name'] = $emp[$value['crt_id']] ?? '';
  192. }
  193. return $data;
  194. }
  195. }