<?php namespace App\Http\Controllers\Api; use App\Http\Controllers\Controller; use App\Http\Controllers\Filter\SupplierFilter; use App\Http\Services\AdminUserService; use App\Http\Services\CompanyService; use App\Http\Services\LogService; use App\Http\Services\SupplierAuditService; use App\Http\Services\SupplierService; use App\Http\Services\SupplierStatisticsService; use App\Http\Services\SyncSupplierService; use App\Http\Transformers\SupplierTransformer; use App\Http\Validators\SupplierValidator; use App\Model\LogModel; use App\Model\RedisModel; use App\Model\SupplierChannelModel; use Illuminate\Http\Request; use Illuminate\Http\Response; use Illuminate\Support\Facades\Auth; use Cookie; class SupplierApiController extends Controller { //默认要接收的数据 private $channelMap = [ 'supplier_id', 'supplier_code', //基础资料 'supplier_name', 'register_company_name', 'supplier_name_en', 'stockup_type', 'supplier_group', 'region', 'currency', 'tax_number', 'supplier_address', 'main_brands', 'main_brands_limit', 'main_brands_limit_type', 'purchase_uid', 'upload_file', 'legal_representative', 'established_time', 'trading_method', 'phone', //省市 'province_city', //收发货地址 'return_phone', 'return_address', 'return_consignee', 'shipping_address', //系数和货期 'cn_ratio', 'us_ratio', 'credit_investigation', 'us_delivery_time', 'cn_delivery_time_period', 'cn_delivery_time', 'us_delivery_time_period', //附加费 'cn', 'hk', //商品上传规则 'sku_upload_ruler', 'sku_audit_ruler', //标签 'system_tags', 'customer_tags', 'level', 'has_certification', 'sku_mode', 'sku_tag', 'main_customers', 'ticket_time', 'billing_period_detail', 'settlement_type', //付款方式 'pay_type', 'pay_type_value', 'pay_type_extra', 'supplier_type', 'registered_capital', 'is_business_abnormal', 'has_legal_ID_card', 'agency_brands', ]; public function Entrance(Request $request, $id) { $this->$id($request, $id); } //添加供应商 public function AddSupplier($request) { //先去表单验证 $validator = new SupplierValidator(); $data = $request->all(); //直接提交的不需要做复杂的表单校验 $isAudit = false; if (request()->get('direct_apply')) { $isAudit = true; $validateResult = $validator->checkSave($data, $isAudit); } else { $validateResult = $validator->checkSave($data, $isAudit); } if ($validateResult) { $this->response(-1, $validateResult); } $channelMap = $this->channelMap; $channelMap = array_merge($channelMap, [ 'supplier_consignee', 'supplier_position', 'supplier_email', 'supplier_mobile', 'supplier_telephone', 'supplier_qq', 'supplier_fax', 'can_check_uids', //附件 'file_name', 'file_url', 'field_name', 'validity_type', 'validity_period', 'description', //银行信息 'receipt_type', 'bank_name', 'bank_adderss', 'account_no', 'account_name', 'account_adderss', 'certificate', 'swift_code', ]); $channel = $request->only($channelMap); //如果是有直接新增权限的(绕过天眼查等校验) if (checkPerm('IgnoreCompanyCheck')) { $channel['is_standard_add'] = -1; } if (SupplierChannelModel::where('supplier_name', trim($channel['supplier_name']))->exists()) { $this->response(0, '操作成功'); } $service = new SupplierService(); $supplierId = $service->saveSupplier($channel); if (!$supplierId) { $this->response(-1, '操作失败'); } //如果是申请审核,还要去修改审核状态 if ($isAudit) { $supplierService = new SupplierService(); $auditData[] = [ 'supplier_id' => $supplierId, //供应商类型为临时供应商,才会去存申请理由 'apply_audit_reason' => $channel['supplier_type'] == 2 ? $request->input('apply_audit_reason') : '', ]; $supplierService->batchApplyInReviewSupplier($auditData); } $this->response(0, '操作成功'); } //添加供应商 public function UpdateSupplier($request) { //先去表单验证 $validator = new SupplierValidator(); $data = $request->all(); $isAudit = (bool)$request->input('is_audit'); $supplierId = $request->input('supplier_id'); $validateResult = $validator->checkSave($data, $isAudit); if ($validateResult) { $this->response(-1, $validateResult); } $channelMap = array_merge($this->channelMap, config('field.AttachmentFields')); $channel = $request->only($channelMap); $service = new SupplierService(); $result = $service->saveSupplier($channel); if (!$result) { $this->response(-1, '修改失败'); } //如果是申请审核,还要去修改审核状态, if ($isAudit) { $supplierService = new SupplierService(); $auditData[] = [ 'supplier_id' => $supplierId, //供应商类型为临时供应商,才会去存申请理由 'apply_audit_reason' => $channel['supplier_type'] == 2 ? $request->input('apply_audit_reason') : '', ]; $supplierService->batchApplyInReviewSupplier($auditData); } $this->response(0, $isAudit ? '申请审核成功' : '修改成功'); } //获取供应商列表 public function GetSupplierList(Request $request) { $list = (new SupplierService())->getSupplierList($request->all()); $this->response(0, 'ok', $list['data'], $list['total']); } //获取供应商详情,目前是给审核小程序用的 public function GetSupplierAuditDetail(Request $request) { $supplierId = $request->input('supplier_id'); if (!$supplierId) { $this->response(-1, '供应商id不能为空'); } $supplier = (new SupplierService())->getSupplier($supplierId, true); $supplier = (new SupplierTransformer())->transformSupplierDetail($supplier); $this->response(0, 'ok', $supplier); } public function DisableSupplier($request) { //禁用不是直接修改为无法交易,而是改为审核中,然后审核通过后,变成无法交易 $supplierId = $request->get('supplier_id'); $disableReason = trim($request->get('disable_reason')); if (empty($disableReason)) { $this->response(-1, '禁用理由必填'); } $model = new SupplierChannelModel(); //先保存原来的状态 $supplier = $model->where('supplier_id', $supplierId)->first()->toArray(); $redis = new RedisModel(); $redis->hset('supplier_status_before_disable', $supplierId, $supplier['status']); $result = $model->where('supplier_id', $supplierId)->update([ 'update_time' => time(), 'status' => $model::STATUS_DISABLE, 'disable_reason' => $disableReason, ]); if ($result) { //写日志 $logService = new LogService(); $logService->AddLog($supplierId, LogModel::UPDATE_OPERATE, '禁用', '禁用供应商,理由是 : ' . $disableReason); $this->response(0, '禁用成功'); } else { $this->response(-1, '禁用失败'); } } //修改状态(禁用,启用(审核通过),驳回,草稿,取消黑名单) public function ChangeSupplierStatus($request) { $status = $request->get('status'); $supplierId = $request->get('supplier_id'); if (empty($status) || empty($supplierId)) { $this->response(-1, '参数缺失'); } $model = new SupplierChannelModel(); $supplier = $model->where('supplier_id', $supplierId)->first()->toArray(); $data = [ 'status' => $status, 'update_time' => time(), ]; //审核通过或者驳回,要更新审核时间 if (($supplier['status'] == 1 || $supplier['status'] == 3)) { if ($status == 2) { $data['audit_time'] = time(); $data['audit_name'] = $request->user->name; $data['audit_uid'] = $request->user->userId; } } if ($status == 3) { $data['audit_time'] = time(); $data['audit_name'] = $request->user->name; $data['audit_uid'] = $request->user->userId; } if ($status == 3) { $rejectReason = trim($request->get('reject_reason')); if (empty($rejectReason)) { $this->response(-1, '没有填写驳回原因'); } $data['reject_reason'] = $rejectReason; } $result = $model->where('supplier_id', $supplierId)->update($data); //保存日志 $logService = new LogService(); $action = array_get(config('fixed.SupplierStatus'), $status); $logService->AddLog($supplierId, LogModel::UPDATE_OPERATE, $action, $action . '-' . $supplier['supplier_name']); if (!$result) { $this->response(-1, '修改状态失败'); } //发送队列消息同步到金蝶 $service = new SyncSupplierService(); $service->syncSupplierToErp($supplierId); $this->response(0, '修改状态成功'); } //检查供应商名称是否相似或者重复 public function CheckSupplierName($request) { $supplierName = trim($request->get('supplier_name')); $taxNumber = $request->get('tax_number'); if (empty($supplierName) && empty($taxNumber)) { $this->response(0, 'ok'); } $supplierId = $request->get('supplier_id'); $region = $request->get('region'); $model = new SupplierChannelModel(); $regionType = $region == SupplierChannelModel::REGION_CN ? 1 : 2; $unitedCompanyInfo = (new CompanyService())->getUnitedCompanyInfo($supplierName, $taxNumber, $regionType); //如果是编辑操作,则要忽略非当前 if ($supplierId) { $originSupplierName = $model->where('supplier_id', $supplierId)->value('supplier_name'); $existedSupplierName = $model->where('supplier_name', $supplierName)->where('supplier_name', '!=', $originSupplierName) ->value('supplier_name'); if ($supplierName !== $existedSupplierName) { } else { $this->response(-1, '供应商已存在,请重新输入或者咨询审批人'); } } else { $existedSupplierName = $model->where('supplier_name', $supplierName) ->value('supplier_name'); if ($existedSupplierName) { $this->response(-1, '供应商已存在,请重新输入或者咨询审批人'); } //还要去检测是否是一体化黑名单 //新增的时候要先去检验下一体化的数据,如果是实体黑名单用户,那么就不允许新增 //还要校验提交上来的公司是否有合法信息 $unitedInfo = $unitedCompanyInfo['united_company_info']; if ($unitedInfo) { if (isset($unitedCompanyInfo['is_entity']) && $unitedCompanyInfo['is_entity'] == 1) { $this->response(-3, '该供应商已经被一体化系统加入黑名单,不能新增'); } if ($unitedInfo['company_category'] != '') { if ($unitedInfo['company_category'] != '普通供应商') { $this->response(-3, '该供应商已经被一体化系统加入黑名单,不能新增'); } } } } $company = []; //判断用户选的地区是不是需要校验的,不需要直接返回即可 if (in_array($region, config('field.NeedCheckCompanyRegion'))) { //还要去请求天眼查获取资料 $company = $unitedCompanyInfo['company']; if (!$company) { $this->response(-2, '公司未进行工商注册'); } } else { $this->response(-2, '供应商跳过天眼查校验'); } $this->response(0, '供应商名称合理', $company); } //审核供应商 public function AuditSupplier($request) { $status = $request->get('status'); $rejectReason = $request->get('reject_reason'); $rejectReason = trim($rejectReason); if (empty($status)) { $this->response(-1, '必须选择一个审核意见'); } if ($status == 3 && empty($rejectReason)) { $this->response(-1, '不同意时必须填写原因'); } $supplierId = $request->get('supplier_id'); $service = new SupplierAuditService(); $result = $service->auditSupplier($supplierId, $status, $rejectReason); if (!$result) { $this->response(-1, '审核失败'); } $this->response(0, '审核成功'); } //分配渠道开发员 public function AllocatePurchaseUser($request) { $purchaseUid = $request->get('purchase_uid'); $supplierId = $request->get('supplier_id'); if (empty($purchaseUid)) { $this->response(-1, '渠道开发员不能为空'); } $adminService = new AdminUserService(); $check = $adminService->checkIsResignedByCodeId($purchaseUid); if ($check) { $this->response(-1, '该渠道开发员已经离职,请选择其他人员'); } $service = new SupplierService(); $result = $service->allocatePurchaseUser($supplierId, $purchaseUid); if (!$result) { $this->response(-1, '分配渠道开发员失败'); } $this->response(0, '分配渠道开发员成功'); } //批量修改渠道开发员 //修改后自动触发转正,资料不完善,进入待提审 public function BatchAllocatePurchaseUser($request) { $purchaseUid = $request->get('purchase_uid'); $supplierIds = $request->get('supplier_ids'); $supplierIds = explode(',', $supplierIds); $supplierService = new SupplierService(); if (empty($purchaseUid)) { $this->response(-1, '请选择渠道开发员'); } $supplierService->batchAllocatePurchaseUser($supplierIds, $purchaseUid); $this->response(0, '批量分配渠道员成功'); } //分配采购员(支持批量操作) public function AllocateChannelUser($request) { $channelUid = $request->get('channel_uid'); $supplierId = $request->get('supplier_id'); if (empty($channelUid)) { $this->response(-1, '采购员不能为空'); } $adminService = new AdminUserService(); $check = $adminService->checkIsResignedByCodeId($channelUid); if ($check) { $this->response(-1, '该采购员已经离职,请选择其他人员'); } $model = new SupplierChannelModel(); $supplier = $model->where('supplier_id', $supplierId)->first(); $supplier = $supplier ? $supplier->toArray() : []; $preChannelUid = $supplier['channel_uid']; $preChannelUid = explode(',', $preChannelUid); //如果之前已经存在对应的采购,直接返回错误 if (in_array($channelUid, $preChannelUid)) { $this->response(-1, '此采购员已经存在,请重新选择'); } $service = new SupplierService(); $result = $service->allocateChannelUser($supplierId, $channelUid); if (!$result) { $this->response(-1, '添加采购员失败'); } $this->response(0, '添加采购员成功'); } //分配采购员(支持批量操作) public function BatchAllocateChannelUser($request) { $channelUid = $request->get('channel_uid'); $supplierIds = $request->get('supplier_ids'); if (empty($channelUid)) { $this->response(-1, '采购员不能为空'); } $adminService = new AdminUserService(); $check = $adminService->checkIsResignedByCodeId($channelUid); if ($check) { $this->response(-1, '该采购员已经离职,请选择其他人员'); } $supplierIds = explode(',', $supplierIds); foreach ($supplierIds as $supplierId) { $service = new SupplierService(); $result = $service->allocateChannelUser($supplierId, $channelUid); if (!$result) { $this->response(-1, '添加采购员失败'); } } $this->response(0, '添加采购员成功'); } //转正供应商 public function ChangeSupplierIsType($request) { $supplierId = $request->get('supplier_id'); $isType = $request->get('is_type'); $service = new SupplierService(); $result = $service->changeSupplierIsType($supplierId, $isType); if (!$result) { $this->response(-1, '转正供应商失败'); } $this->response(0, '转正供应商成功'); } //发送金蝶同步请求 public function SyncToErp($request) { $supplierId = $request->get('supplier_id'); $service = new SyncSupplierService(); $service->syncSupplierToErp($supplierId); $this->response(0, '已发送同步请求'); } //发送一体化同步请求 public function SyncToUnited($request) { $supplierId = $request->get('supplier_id'); $supplier = SupplierChannelModel::where('supplier_id', $supplierId)->first()->toArray(); if (in_array($supplier['supplier_name'], config('field.SkipChangeSupplierTypeNames'))) { $service = new SyncSupplierService(); $service->syncSupplierToUnited($supplierId); $this->response(0, '属于代购供应商,直接发送同步'); } //去调取天眼查数据,有数据的话,更新 $companyInfo = (new CompanyService())->getCompanyInfo($supplier['supplier_name'], ''); //先判断是否是标准添加,如果标准添加,国内地区,但是没有税号的,不允许同步 if ($supplier['is_standard_add'] == 1 && $supplier['region'] == SupplierChannelModel::REGION_CN && !$supplier['tax_number'] && $companyInfo) { $taxNumber = $companyInfo['tax_number']; SupplierChannelModel::where('supplier_id', $supplierId)->update([ 'tax_number' => $taxNumber, ]); // $this->response(-1, '该供应商属于国内,但是没有税号,不允许同步'); } if ($supplier['is_standard_add'] == 1 && $supplier['region'] != SupplierChannelModel::REGION_CN) { // if (empty($companyInfo)) { // $this->response(-1, '该供应商属于海外,但是无法查询公司信息,不允许同步'); // } } $service = new SyncSupplierService(); $service->syncSupplierToUnited($supplierId); $this->response(0, '已发送同步请求'); } //设置云芯账号 public function SetYunxinChannelUid($request) { $supplierId = $request->get('supplier_id'); $channelUid = $request->get('yunxin_channel_uid'); $model = new SupplierChannelModel(); $result = $model->where('supplier_id', $supplierId)->update([ 'yunxin_channel_uid' => $channelUid, ]); if ($result) { //修改供应商状态 $model->where('supplier_id', $supplierId)->update([ 'update_time' => time(), 'status' => SupplierChannelModel::STATUS_PENDING, ]); $adminService = new AdminUserService(); $user = $adminService->getAdminUserInfoByCodeId($channelUid); $logService = new LogService(); $logService->AddLog($supplierId, LogModel::UPDATE_OPERATE, '设置SKU采购员', '设置SKU采购员为 : ' . $user['name']); $this->response(0, '设置SKU采购成功'); } else { $this->response(-1, '设置SKU采购失败'); } } //批量分配云芯采购员 public function BatchAllocateYunxinChannelUser($request) { $channelUid = $request->get('channel_uid'); $supplierIds = $request->get('supplier_ids'); $supplierIds = explode(',', $supplierIds); $supplierService = new SupplierService(); if (empty($channelUid)) { $this->response(-1, '请选择SKU采购员'); } if (!$supplierService->checkCanAllocatYunxinChannelUid($supplierIds, $channelUid)) { $this->response(-1, '选择的供应商里面不存在对应的SKU采购员'); } $supplierService->batchAllocateYunxinChannelUser($supplierIds, $channelUid); $this->response(0, '批量分配SKU采购员成功'); } //检查能否能申请审核 public function CheckCanApplyInReview($request) { $supplierIds = $request->get('supplier_ids'); if (!$supplierIds) { $this->response(-1, '请选择供应商'); } $supplierIds = explode(',', $supplierIds); $supplierAuditService = new SupplierAuditService(); $check = $supplierAuditService->CheckCanApplyInReview($supplierIds); if ($check === true) { $this->response(0, '可以申请审核'); } else { $this->response(-1, $check); } } //检查能否审核(只有最后一次修改人和当前审核人是属于同部门的,才能审核) public function CheckCanAuditSupplier($request) { $supplierId = $request->get('supplier_id'); $supplierAuditService = new SupplierAuditService(); $check = $supplierAuditService->CheckCanAuditSupplier($supplierId); if ($check) { $this->response(0, '可以审核'); } else { $this->response(-1, '无法进行审核操作,因为该供应商最后(修改人/创建人)和您不属于同一个部门'); } } //拉黑供应商 public function BlockSupplier($request) { $supplierId = $request->get('supplier_id'); $blockReason = $request->get('block_reason'); if (empty(trim($blockReason))) { $this->response(-1, '必须填写拉黑原因'); } if (mb_strlen($blockReason) > 200) { $this->response(-1, '拉黑原因不能超过200字'); } $channelModel = new SupplierChannelModel(); $result = $channelModel->where('supplier_id', $supplierId)->update([ 'block_reason' => $request->get('block_reason'), 'status' => $channelModel::STATUS_BLOCK, 'update_time' => time(), ]); if ($result) { $logService = new LogService(); $logService->AddLog($supplierId, LogModel::UPDATE_OPERATE, '拉黑', '拉黑供应商'); $this->response(0, '拉黑成功'); } else { $this->response(-1, '拉黑操作失败'); } } //取消拉黑供应商 public function CancelBlockSupplier($request) { $supplierId = $request->get('supplier_id'); $channelModel = new SupplierChannelModel(); $result = $channelModel->where('supplier_id', $supplierId)->update([ 'status' => SupplierChannelModel::STATUS_IN_REVIEW, 'update_time' => time(), ]); if ($result) { $logService = new LogService(); $logService->AddLog($supplierId, LogModel::UPDATE_OPERATE, '取消拉黑', '取消拉黑供应商'); $this->response(0, '取消拉黑成功'); } else { $this->response(-1, '取消拉黑操作失败'); } } //取消禁用供应商 public function CancelDisableSupplier($request) { $supplierId = $request->get('supplier_id'); $channelModel = new SupplierChannelModel(); //先找出原来的状态 $redis = new RedisModel(); $previousStatus = $redis->hget('supplier_status_before_disable', $supplierId); $result = $channelModel->where('supplier_id', $supplierId)->update([ 'status' => $previousStatus, 'update_time' => time(), ]); if ($result) { //删除redis状态 $redis->hdel('supplier_status_before_disable', $supplierId); $logService = new LogService(); $logService->AddLog($supplierId, LogModel::UPDATE_OPERATE, '取消禁用', '取消禁用供应商'); $this->response(0, '取消禁用成功'); } else { $this->response(-1, '取消禁用操作失败'); } } //批量申请审核供应商 public function BatchApplyInReviewSupplier($request) { $supplierIds = $request->get('supplier_id'); $supplierTypeList = $request->get('supplier_type'); $applyAuditReasonList = $request->get('apply_audit_reason'); $auditData = []; foreach ($supplierIds as $key => $supplierId) { $supplierType = array_get($supplierTypeList, $key); $reason = array_get($applyAuditReasonList, $key); if ($supplierType == SupplierChannelModel::SUPPLIER_TYPE_TEMPORARY && !$reason) { $this->response(-1, '临时类型的供应商申请审核必须填写申请原因'); } $errorMsg = SupplierService::checkSupplierFieldCompleted($supplierId); if ($errorMsg) { $this->response(-1, $errorMsg); } $auditData[] = [ 'supplier_id' => $supplierId, 'apply_audit_reason' => $reason, ]; } $supplierService = new SupplierService(); $supplierService->batchApplyInReviewSupplier($auditData); $this->response(0, '申请审核成功'); } //判断是否有阶梯价设置 public function checkHasLadderPriceSetting($request) { $supplierId = $request->get('supplier_id'); $exists = SupplierService::checkHasLadderPriceSetting($supplierId); if ($exists) { $this->response(0, 'ok'); } $this->response(-1, '不存在'); } //判断是否有全部采购员离职的供应商 public function checkHasAllResignedChannelUserSupplier($request) { setcookie('has_pop_up_all_channel_user_supplier_tips', 1); $count = (new SupplierStatisticsService())->getStatisticsCount('all_channel_user_resigned'); if ($count) { $this->response(0, 'ok', $count); } $this->response(-1, '没有全部采购员离职的供应商'); } //导出供应商 public function exportSupplier($request) { $params = $request->input('params'); $params = json_decode($params, true); (new SupplierService())->exportSupplier($params); exit; } //查询供应商 public function querySupplier($request) { $supplierName = trim($request->input('supplier_name')); if (!$supplierName) { $this->response(-1,'供应商名称不能为空'); } $supplier = (new SupplierService())->querySupplier($supplierName); $this->response(0, '查询成功', $supplier); } }