enterpriseId = $enterpriseId; $this->userCenterId = $userCenterId; $this->objDWarehouse = new DWarehouse('stock'); $this->objDWarehouseBeginning = new DWarehouseBeginning('stock'); $this->objDWarehouse->setTable('qianniao_warehouse_'.$enterpriseId); $this->objDWarehouseBeginning->setTable('qianniao_warehouse_beginning_'.$enterpriseId); } /** * 仓库添加 * @param $params * @return ResultWrapper * @throws Exception */ public function addWarehouse($params) { $params['createTime'] = time(); $params['updateTime'] = time(); $dbResult = $this->objDWarehouse->insert($params); if($dbResult === false){ return ResultWrapper::fail($this->objDWarehouse->error(), ErrorCode::$dberror); } $warehouseId = $dbResult; unset($dbResult); $update = [ 'warehouseCode' => createCode(StatusCode::$code['warehouse']['prefix'], $warehouseId, StatusCode::$code['warehouse']['length']), 'updateTime' => time(), ]; $result = $this->objDWarehouse->update($update, ['id' => $warehouseId]); if($result === false){ return ResultWrapper::fail($this->objDWarehouse->error(), ErrorCode::$dberror); } return ResultWrapper::success($warehouseId); } /** * 仓库期初记录添加 * @param $params * @return ResultWrapper * @throws Exception */ public function addWarehouseBeginning($params) { $warehouseId = 0; $addWarehouseBeginningData = []; foreach($params as $value){ $warehouseId = $value['warehouseId']; $addWarehouseBeginningData[] = [ 'no' => createOrderSn(StatusCode::$source['manage'], StatusCode::$orderType['warehouseBeginning'],$value['operatorId'] ), 'warehouseId' => $value['warehouseId'], 'operatorId' => $value['operatorId'], 'operatorName' => $value['operatorName'], 'remark' => $value['remark'], 'materielId' => $value['materielId'], 'materielName' => $value['materielName'], 'materielCode' => $value['materielCode'], 'skuId' => $value['skuId'], 'num' => $value['num'], 'costPrice' => $value['costPrice'], 'updateTime' => time(), 'createTime' => time(), ]; } if(empty($warehouseId)) return ResultWrapper::fail('参数错误', ErrorCode::$paramError); $dbResult = $this->objDWarehouse->get($warehouseId, 'id,beginningStatus'); if($dbResult === false){ return ResultWrapper::fail($this->objDWarehouse->error(), ErrorCode::$dberror); } if($dbResult['beginningStatus'] == StatusCode::$delete){ return ResultWrapper::fail('期初功能已失效', ErrorCode::$paramError); } $this->objDWarehouseBeginning->beginTransaction(); //添加期初记录 $dbResult = $this->objDWarehouseBeginning->insert($addWarehouseBeginningData, true); if($dbResult === false){ $this->objDWarehouseBeginning->rollBack(); return ResultWrapper::fail($this->objDWarehouseBeginning->error(), ErrorCode::$dberror); } $addIds = $dbResult; unset($dbResult); //修改期初状态 $dbResult = $this->objDWarehouse->update(['beginningStatus'=>StatusCode::$delete],['id'=>$warehouseId]); if($dbResult === false){ return ResultWrapper::fail($this->objDWarehouse->error(), ErrorCode::$dberror); } //添加库存 $objMInventory = new MInventory($this->enterpriseId, $this->userCenterId); $modelResult = $objMInventory->addInventoryByWarehouseBeginning($addWarehouseBeginningData, $addIds); if(!$modelResult->isSuccess()){ $this->objDWarehouseBeginning->rollBack(); return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } $this->objDWarehouseBeginning->commit(); return ResultWrapper::success($dbResult); } /** * 仓库删除 * @param $where * @return ResultWrapper */ public function deleteWarehouse($where) { $params = [ 'deleteStatus' => StatusCode::$delete, 'updateTime' => time(), ]; $dbResult = $this->objDWarehouse->update($params, $where); if($dbResult === false){ return ResultWrapper::fail($this->objDWarehouse->error(), ErrorCode::$dberror); }else{ return ResultWrapper::success($dbResult); } } /** * 仓库启用/禁用 * @param $where * @return ResultWrapper */ public function enableWarehouse($where) { $params['updateTime'] = time(); $params['enableStatus'] = $where['enableStatus']; unset($where['enableStatus']); $dbResult = $this->objDWarehouse->update($params, ['id' => $where['id']]); if($dbResult === false){ return ResultWrapper::fail($this->objDWarehouse->error(), ErrorCode::$dberror); }else{ return ResultWrapper::success($dbResult); } } /** * 仓库修改 * @param $params * @param $where * @return ResultWrapper * @throws Exception */ public function updateWarehouse($params, $where) { $dbResult = $this->objDWarehouse->update($params,$where); if($dbResult === false){ return ResultWrapper::fail($this->objDWarehouse->error(), ErrorCode::$dberror); }else{ return ResultWrapper::success($dbResult); } } /** * 仓库列表 * @param $selectParams * @return ResultWrapper */ public function getAllWarehouse($selectParams) { $limit = $selectParams['limit']; unset($selectParams['limit']); $offset = $selectParams['offset']; unset($selectParams['offset']); if(isset($selectParams['shopId'])){ // 如果多门店/多商户只查看自己店铺对应仓库 $objMShop = new MShop($this->enterpriseId, 1); $result = $objMShop->getShopById($selectParams['shopId']); if(!$result->isSuccess()){ return ResultWrapper::fail('查询店铺对应仓库数据为空', ErrorCode::$contentNotExists); } $shopData = $result->getData(); $warehouseId = $shopData['warehouseId']; if(!is_numeric($warehouseId)){ $warehouseId = explode(",", $warehouseId); } $dbResult = $this->objDWarehouse->select(['id'=>$warehouseId,'deleteStatus' => StatusCode::$standard], '*', 'createTime desc', $limit, $offset); if ($dbResult === false) { return ResultWrapper::fail($this->objDWarehouse->error(), ErrorCode::$dberror); } $total = count($dbResult); }else{ // 企业后台查看所有仓库 $sql = 'deleteStatus = '.StatusCode::$standard; isset($selectParams['id']) && $sql .= ' and id != '.$selectParams['id']; isset($selectParams['enableStatus']) && $sql .= ' and enableStatus = '.$selectParams['enableStatus']; $dbResult = $this->objDWarehouse->select($sql, '*', 'createTime desc', $limit, $offset); if ($dbResult === false) { return ResultWrapper::fail($this->objDWarehouse->error(), ErrorCode::$dberror); } $sql = 'select count(id) count from '.$this->objDWarehouse->get_Table().' where '.$sql; $countData = $this->objDWarehouse->query($sql); $total = array_shift($countData)['count']; } $return = [ 'data' => self::format($dbResult), 'total' => ($total) ? intval($total) : 0, ]; if($return === false){ return ResultWrapper::fail($this->objDWarehouse->error(), ErrorCode::$dberror); }else{ return ResultWrapper::success($return); } } public function format($data) { $objMSysAreaChina = new MSysAreaChina(); foreach ($data as &$value) { $value['warehouseCode'] = createCode(StatusCode::$code['warehouse']['prefix'], $value['id'], StatusCode::$code['warehouse']['length']); $areaName = $objMSysAreaChina->getNameByCode([ $value['provinceCode'], $value['cityCode'], $value['districtCode'] ]); $value['area']['provinceName'] = isset($areaName[$value['provinceCode']]) ? $areaName[$value['provinceCode']] : ''; $value['area']['cityName'] = isset($areaName[$value['cityCode']]) ? $areaName[$value['cityCode']] : ''; $value['area']['districtName'] = isset($areaName[$value['districtCode']]) ? $areaName[$value['districtCode']] : ''; $value['area']['contactAddress'] = $value['contactAddress']; } return $data; } /** * 仓库详情 * @param $params * @return ResultWrapper */ public function getWarehouseInfo($params) { $dbResult = $this->objDWarehouse->get($params); if($dbResult === false){ return ResultWrapper::fail($this->objDWarehouse->error(), ErrorCode::$dberror); }else{ if(empty($dbResult)){ return ResultWrapper::fail('没有仓库数据', ErrorCode::$contentNotExists); } $dbResult['warehouseCode'] = createCode(StatusCode::$code['warehouse']['prefix'], $dbResult['id'], StatusCode::$code['warehouse']['length']); return ResultWrapper::success($dbResult); } } /** * 根据ids数组获取name * @param $ids * @return array|ResultWrapper */ public function getNameByIds($ids) { $where = ['id' => $ids]; $dbResult = $this->objDWarehouse->select($where, 'id,warehouseName'); if ($dbResult === false) { return ResultWrapper::fail($this->objDWarehouse->error(), ErrorCode::$dberror); } $returnData = []; foreach($dbResult as $key => $value){ $returnData[$value['id']]['id'] = $value['id']; $returnData[$value['id']]['warehouseName'] = $value['warehouseName']; } if(empty($dbResult)){ return ResultWrapper::fail('仓库数据为空', ErrorCode::$dberror); } return ResultWrapper::success($returnData); } /** * 获取仓库数据 * @param array $where * @param string $field * @return ResultWrapper */ public function selectWarehouse($where = [], $field = '*') { $dbResult = $this->objDWarehouse->select($where, $field); if($dbResult === false){ return ResultWrapper::fail($this->objDWarehouse->error(), ErrorCode::$dberror); } return ResultWrapper::success($dbResult); } /** * 获取仓库数据 * @param array $where * @param string $field * @return ResultWrapper */ public function getWarehouse($where = [], $field = '*') { $dbResult = $this->objDWarehouse->get($where, $field); if($dbResult === false){ return ResultWrapper::fail($this->objDWarehouse->error(), ErrorCode::$dberror); } return ResultWrapper::success($dbResult); } /** * 查询仓库名称 * @param $id */ public function getWarehouseName($id) { $objGoodsBasicRelevantCache = new GoodsBasicRelevant($this->enterpriseId); $result = $objGoodsBasicRelevantCache->getNameByWarehouseId($id); $warehouseName = ''; if(!$result){ $dbResult = $this->objDWarehouse->get(['id' => $id]); if($dbResult){ $warehouseName = isset($dbResult['warehouseName']) ? $dbResult['warehouseName'] : ''; $objGoodsBasicRelevantCache->cacheWarehouseIdRelationName($id, $dbResult); } }else{ $warehouseName = $result; } return $warehouseName; } }