onlineUserId = $onlineUserId; $this->onlineEnterpriseId = $onlineEnterpriseId; parent::__construct($this->onlineEnterpriseId, $this->onlineUserId); $this->objDPurchaseOut = new DPurchaseOut('stock'); $this->objDPurchaseOutGoods = new DPurchaseOutGoods('stock'); $this->objDPurchaseOutGoods->setTable($this->objDPurchaseOutGoods->get_Table() . '_' . $this->onlineEnterpriseId); $this->objDPurchaseOut->setTable($this->objDPurchaseOut->get_Table() . '_' . $this->onlineEnterpriseId); $this->objDMerchantApply = new DMerchantApply(); } /** * 增加采购退货订单 * @param $params * @return ResultWrapper * @throws Exception */ public function addPurchaseOut($params) { $goodData = $params['goodsData']; unset($params['goodsData']); $deleteArray = $params['deleteArray']; unset($params['deleteArray']); $purchaseAmount = 0; foreach ($goodData as $key => $val) { $purchaseAmount = bcadd($purchaseAmount, $val['subtotalPrice'], 4); } $params['purchaseAmount'] = $purchaseAmount;//采购总额 //$params['couponAmount'] = sprintf("%.4f", array_sum(array_column($goodData, 'couponAmount')));//优惠总额 //$params['otherAmount'] = sprintf("%.4f", array_sum(array_column($goodData, 'otherAmount')));//其他总额 //添加采购退货退货订单 // 生成编号 $dbResult = $this->objDPurchaseOut->get('createTime >='.strtotime(date('Ymd'.'0:0:0')), 'no', 'createTime desc'); if ($dbResult === false) { return ResultWrapper::fail($this->objDPurchaseOut->error(), ErrorCode::$dberror); } if(empty($dbResult)){ $params['no'] = createSerialNumberByDate(''); }else{ $params['no'] = createSerialNumberByDate($dbResult['no']); } //$no = createOrderSn(StatusCode::$source['manage'], StatusCode::$orderType['purchaseReturn'], $this->onlineUserId); //$params['no'] = $no; $params['createTime'] = time(); $params['updateTime'] = time(); $objMInventory = new MInventory($this->onlineEnterpriseId, $this->onlineUserId); $skuIds = []; foreach($goodData as $value){ $skuIds[] = $value['skuId']; } //查询仓库库存剩余数量 $modelResult = $objMInventory->getWarehouseInventoryData($params['warehouseId'], $skuIds); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } $inventoryData = $modelResult->getData(); unset($modelResult); if(empty($inventoryData)){ return ResultWrapper::fail('库存查询失败', ErrorCode::$paramError); } //查询批次批次剩余数量 $modelResult = $objMInventory->getBatchDataByOriginId($params['warehouseId'], $params['originId'], $skuIds); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } $inventoryBatch = $modelResult->getData(); unset($modelResult); if(empty($inventoryBatch)){ return ResultWrapper::fail('批次库存查询失败', ErrorCode::$dberror); } foreach($goodData as $value){ //判断库存剩余数量 if(isset($inventoryData[$value['skuId']])){ if($value['buyerNum'] > $inventoryData[$value['skuId']]['num']){ return ResultWrapper::fail($value['goodsName'].'库存不足', ErrorCode::$dberror); } }else{ return ResultWrapper::fail($value['goodsName'].'库存为空', ErrorCode::$dberror); } //判断批次剩余数量 if(isset($inventoryBatch[$value['skuId']])){ if($value['buyerNum'] > $inventoryBatch[$value['skuId']]['num']){ return ResultWrapper::fail($value['goodsName'].' 批次库存不足', ErrorCode::$dberror); } }else{ return ResultWrapper::fail($value['goodsName'].' 批次库存为空', ErrorCode::$dberror); } } $beginStatus = $this->objDPurchaseOut->beginTransaction(); $purchaseOutId = $this->objDPurchaseOut->insert($params); if ($purchaseOutId === false) { $this->objDPurchaseOut->rollBack(); return ResultWrapper::fail($this->objDPurchaseOut->error(), ErrorCode::$dberror); } foreach ($goodData as $key => &$val) { $val['purchaseOutId'] = $purchaseOutId; } unset($val); $objPurchaseGoods = new MPurchaseOutGoods($this->onlineUserId, $this->onlineEnterpriseId); $dbResult = $objPurchaseGoods->addPurchaseGoods($goodData); if (!$dbResult->isSuccess()) { $this->objDPurchaseOut->rollBack(); return ResultWrapper::fail($dbResult->getData(), ErrorCode::$dberror); } $beginStatus && $this->objDPurchaseOut->commit(); return ResultWrapper::success($purchaseOutId); } /** * 采购退货订单详情 * @param $id * @return ResultWrapper * @throws Exception */ public function getPurchaseOutInfoById($id) { $dbResult = $this->objDPurchaseOut->get($id); if ($dbResult === false) { return ResultWrapper::fail($this->objDPurchaseOut->error(), ErrorCode::$dberror); } if(empty($dbResult)){ return ResultWrapper::fail('单据不存在', ErrorCode::$paramError); } $returnData = $dbResult; $returnData['type'] = StatusCode::$orderType['purchaseReturn']; unset($dbResult); $dbResult = $this->objDPurchaseOutGoods->select(['purchaseOutId' => $id, 'deleteStatus' => StatusCode::$standard]); if ($dbResult === false) { return ResultWrapper::fail($this->objDPurchaseOut->error(), ErrorCode::$dberror); } $skuIds = []; foreach($dbResult as $value){ $skuIds[] = $value['skuId']; } //查询出采购数量 $objMPurchaseGoods = new MPurchaseGoods($this->onlineUserId, $this->onlineEnterpriseId); $purchaseDetailsWhere = [ 'purchaseId' => $returnData['originId'], 'skuId' => $skuIds ]; $modelResult = $objMPurchaseGoods->getPurchaseGoodsData($purchaseDetailsWhere); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } $purchaseGoodsData = $modelResult->getData(); unset($modelResult); //查询库存剩余数量 $objMPurchase = new MPurchase($this->onlineUserId, $this->onlineEnterpriseId); $purchaseWhere = [ 'id' => $returnData['originId'], 'warehouseId' => $returnData['warehouseId'], ]; $modelResult = $objMPurchase->getPurchaseAndBatchInfoById($purchaseWhere); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } $batchResult = $modelResult->getData(); unset($modelResult); $batchData = []; foreach($batchResult as $value){ $batchData[$value['skuId']] = $value; } foreach($dbResult as &$value){ $value['purchaseNum'] = isset($purchaseGoodsData[$value['skuId']]) ? $purchaseGoodsData[$value['skuId']]['buyerNum'] : 0; $value['inNum'] = isset($purchaseGoodsData[$value['skuId']]) ? $purchaseGoodsData[$value['skuId']]['inNum'] : 0; $value['inOfNum'] = isset($purchaseGoodsData[$value['skuId']]) ? $purchaseGoodsData[$value['skuId']]['inOfNum'] : 0; $value['returnOnNum'] = isset($purchaseGoodsData[$value['skuId']]) ? $purchaseGoodsData[$value['skuId']]['returnOnNum'] : 0; $value['returnNum'] = isset($purchaseGoodsData[$value['skuId']]) ? $purchaseGoodsData[$value['skuId']]['returnNum'] : 0; $value['inventoryNum'] = isset($batchData[$value['skuId']]) ? $batchData[$value['skuId']]['num'] : 0; } unset($value); $returnData['details'] = $dbResult; unset($dbResult); $formatResult = self::format($returnData); return ResultWrapper::success(empty($formatResult) ? [] : $formatResult[0]); } /** * 编辑采购退货订单 * @param $params * @return ResultWrapper * @throws Exception */ public function editPurchase($params) { if (empty($params['id'])) { return ResultWrapper::fail('id参数为空', ErrorCode::$paramError); } if( isset($params['no']) ){ unset($params['no']); } $id = $params['id']; unset($params['id']); unset($params['source']); $deleteArray = $params['deleteArray']; unset($params['deleteArray']); $beginStatus = $this->objDPurchaseOut->beginTransaction(); $goodData = $params['goodsData']; unset($params['goodsData']); $dbResult = $this->objDPurchaseOut->update($params, $id); if ($dbResult === false) { $this->objDPurchaseOut->rollBack(); return ResultWrapper::fail($this->objDPurchaseOut->error(), ErrorCode::$dberror); } $objPurchaseOutGoods = new MPurchaseOutGoods($this->onlineUserId, $this->onlineEnterpriseId); $modelResult = $objPurchaseOutGoods->editPurchaseOutGoods($goodData, $id, $deleteArray); if (!$modelResult->isSuccess()) { $this->objDPurchaseOut->rollBack(); return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } $beginStatus && $this->objDPurchaseOut->commit(); return ResultWrapper::success($dbResult); } /** * 更新审核状态 * @param $params * @return ResultWrapper * @throws Exception */ public function updateAuditStatus($params) { $id = $params['id']; unset($params['id']); $beginStatus = $this->objDPurchaseOut->beginTransaction(); $dbResult = $this->objDPurchaseOut->update($params, $id); if ($dbResult === false) { $this->objDPurchaseOut->rollBack(); return ResultWrapper::fail($this->objDPurchaseOut->error(), ErrorCode::$dberror); } $return = $dbResult; unset($dbResult); $dbResult = $this->objDPurchaseOut->get(['id' => $id]); if ($dbResult === false) { return ResultWrapper::fail($this->objDPurchaseOut->error(), ErrorCode::$dberror); } $purchaseOutData = $dbResult; unset($dbResult); $dbResult = $this->objDPurchaseOutGoods->select(['purchaseOutId' => $id]); if ($dbResult === false) { return ResultWrapper::fail($this->objDPurchaseOut->error(), ErrorCode::$dberror); } $purchaseOutGoodsData = $dbResult; //兼容商户 if($purchaseOutData['purchaseStatus'] == StatusCode::$standard){//商户 //查询商户信息 $sql = 'select contactMobile as mobile from qianniao_merchant_apply where id = '.$purchaseOutData['merchantId'] .' and enterpriseId='.$this->onlineEnterpriseId; unset($dbResult); $dbResult = $this->objDMerchantApply->query($sql); if ($dbResult === false) { return ResultWrapper::fail($this->objDPurchaseOut->error(), ErrorCode::$dberror); } if(empty($dbResult)){ return ResultWrapper::fail('供应商信息为空', ErrorCode::$paramError); } }else{ //查询供应商信息 $sql = 'select code,mobile from qianniao_supplier_'.$this->onlineEnterpriseId.' where id = '.$purchaseOutData['supplierId']; unset($dbResult); $dbResult = $this->objDPurchaseOut->query($sql); if ($dbResult === false) { return ResultWrapper::fail($this->objDPurchaseOut->error(), ErrorCode::$dberror); } if(empty($dbResult)){ return ResultWrapper::fail('供应商信息为空', ErrorCode::$paramError); } } $supplierData = array_shift($dbResult); unset($dbResult); //拼数据加出库 $outData = [ 'sourceId' => $purchaseOutData['id'], 'sourceNo' => $purchaseOutData['no'], 'originNo' => $purchaseOutData['originNo'], 'originId' => $purchaseOutData['originId'], 'materielNum' => 0, 'amount' => $purchaseOutData['purchaseAmount'], 'customerId' => $purchaseOutData['supplierId'],//供应商id 'customerName' => $purchaseOutData['supplierName'],//供应商名称 'customerCode' => $supplierData['code'] ? $supplierData['code'] : '',//供应商编码 'customerMobile' => $supplierData['mobile'],//供应商联系方式 'operatorId' => $params['auditId'], 'operatorName' => $params['auditName'], 'remark' => $purchaseOutData['remark'], 'source' => StatusCode::$orderType['purchaseReturn'], 'type' => StatusCode::$orderType['purchaseReturnOut'], 'deleteStatus' => StatusCode::$standard, 'auditStatus' => StatusCode::$auditStatus['auditing'], 'createTime' => time(), 'updateTime' => time(), 'orderGoodsData' => [], 'shopId' => $purchaseOutData['shopId'], 'shopName' => $purchaseOutData['shopName'], ]; $detailsData = []; foreach ($purchaseOutGoodsData as $value) { $detailsData[$value['skuId']]['num'] = $value['buyerNum']; $outData['materielNum']++; $details = [ 'materielId' => $value['goodsId'], 'materielName' => $value['goodsName'], 'materielCode' => $value['goodsCode'], 'outWarehouse' => json_encode([['warehouseId' => $purchaseOutData['warehouseId'], 'num' => $value['buyerNum']]]), 'skuId' => $value['skuId'], 'unitName' => $value['unitName'], 'skuName' => $value['skuName'], 'num' => 0, 'outNum' => $value['buyerNum'], 'otherNum' => $value['otherNum'], 'total' => $value['buyerNum'], 'unitPrice' => $value['buyerUnitPrice'], 'totalPrice' => $value['subtotalPrice'], ]; $outData['orderGoodsData'][] = $details; } //增加出库单 $objMInventoryOut = new MInventoryOut($this->onlineEnterpriseId, $this->onlineUserId); $modelResult = $objMInventoryOut->addInventoryOut($outData); if (!$modelResult->isSuccess()) { $this->objDPurchaseOut->rollBack(); return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } unset($modelResult); //修改采购订单退货状态 $objMPurchase = new MPurchase($this->onlineUserId, $this->onlineEnterpriseId); $modelResult = $objMPurchase->updatePurchaseDetailsReturnStatus($purchaseOutData['originId'], $detailsData); if(!$modelResult->isSuccess()){ $this->objDPurchaseOut->rollBack(); return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } $beginStatus && $this->objDPurchaseOut->commit(); return ResultWrapper::success($return); } /** * 删除采购退货单 * @param $id * @return ResultWrapper */ public function delPurchaseOut($id) { $dbResult = $this->objDPurchaseOut->update(['deleteStatus' => StatusCode::$delete], $id); if ($dbResult === false) { return ResultWrapper::fail($this->objDPurchaseOut->error(), ErrorCode::$dberror); } else { return ResultWrapper::success($dbResult); } } /** * 采购退货单列表 * @param $selectParams * @param $export * @return ResultWrapper * @throws Exception */ public function getAllPurchaseOut($selectParams, $export) { $selectParams = parent::getStaffQueryParams($selectParams, 'buyerId'); $selectParams = parent::getShopIdQueryParams($selectParams); $limit = $selectParams['limit']; unset($selectParams['limit']); $offset = $selectParams['offset']; unset($selectParams['offset']); $selectParams['deleteStatus'] = StatusCode::$standard; if($export){ $modelResult = self::export($selectParams); if(!$modelResult->isSuccess()){ return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } exit; } $dbResult = $this->objDPurchaseOut->select($selectParams, '*', 'createTime desc', $limit, $offset); if ($dbResult === false) { return ResultWrapper::fail($this->objDPurchaseOut->error(), ErrorCode::$dberror); } $total = $this->objDPurchaseOut->count($selectParams); $return = [ 'data' => self::format($dbResult), 'total' => ($total) ? intval($total) : 0, ]; return ResultWrapper::success($return); } /** * 格式化数据 * @param $data * @return array * @throws Exception */ public function format($data) { if (isset($data['id'])) { $dimension = $data; unset($data); $data[] = $dimension; } $allPurchaseOutIds = []; foreach ($data as $key => $val) { $allPurchaseOutIds[] = $val['id']; $data[$key]['no'] = StatusCode::$noPrefix[4].'-'.$val['no']; } $objMPurchaseOutGoods = new MPurchaseOutGoods($this->onlineUserId, $this->onlineEnterpriseId); $dbResult = $objMPurchaseOutGoods->getGoodsByPurchaseIds($allPurchaseOutIds); if (!$dbResult->isSuccess()) { return $data; } $goodsData = $dbResult->getData(); foreach ($goodsData as $key => $value) { $allGoodsData[$value['purchaseId']][] = $value; } foreach ($data as $key => $value) { $data[$key]['goodsData'] = isset($allGoodsData[$value['id']]) ? $allGoodsData[$value['id']] : []; } return $data; } /** * 根据商铺id查询物料退货记录 * @param array $shopIds //商铺id * @return ResultWrapper */ public function getReturnDataByShopIds(array $shopIds) { if(empty($shopIds)) return ResultWrapper::success([]); $sql = 'SELECT g.goodsId,SUM(g.buyerNum) AS num FROM '.$this->objDPurchaseOut->get_Table().' p LEFT JOIN '.$this->objDPurchaseOutGoods->get_Table().' g ON p.id = g.purchaseOutId WHERE p.auditStatus = '.StatusCode::$auditStatus['auditPass'].' AND p.deleteStatus = '.StatusCode::$standard.' AND g.deleteStatus = '.StatusCode::$standard.' AND p.shopId in('.implode(',', $shopIds).') GROUP BY g.goodsId'; $dbResult = $this->objDPurchaseOut->query($sql); if($dbResult === false){ return ResultWrapper::fail($this->objDPurchaseOut->error(), ErrorCode::$dberror); } $formatData = []; foreach($dbResult as $value){ $formatData[$value['goodsId']] = $value; } unset($dbResult); return ResultWrapper::success($formatData); } /** * 导出方法 * @param $condition * @return ResultWrapper */ public function export($condition) { $result = $this->objDPurchaseOut->exportSelect($condition,'id,no,originNo,supplierName,purchaseAmount,operatorName,createTime,auditStatus,outStatus'); if($result === false){ return ResultWrapper::fail($this->objDPurchaseOut->error(), ErrorCode::$dberror); } $formatIds = []; $formatData = []; foreach($result as $value){ $formatData[$value['id']] = $value; $formatIds[] = $value['id']; } $dbResult = $this->objDPurchaseOutGoods->exportSelect(['purchaseOutId' => $formatIds, 'deleteStatus'=> StatusCode::$standard]); if($dbResult === false){ return ResultWrapper::fail($this->objDPurchaseOutGoods->error(), ErrorCode::$paramError); } //导出到本地 header ( "Content-type:application/vnd.ms-excel" ); header ( "Content-Disposition:filename=采购退货记录表.csv" ); header ('Cache-Control: max-age=0'); $fp = fopen('php://output', 'a'); $head = ['编号','采购退货单号','供应商','退货金额','采购关联单号','制单人员','制单日期','审核状态','出库状态','商品名称','商品编码','单位','采购数量','采购单价','采购总价'];//定义标题 foreach ($head as $i => $v) { $head[$i] = mb_convert_encoding($v, 'GBK', 'utf-8');//将中文标题转换编码,否则乱码 } fputcsv($fp, $head); $limit = 10000; $num = 0; //计数器 foreach ( $dbResult as $value ) { //循环数据 $num++; if($num == $limit){ ob_flush(); //释放内存 flush(); } $value = parent::formatOrderMan($this->onlineEnterpriseId, $value); $v = $formatData[$value['purchaseOutId']]; $rows['id'] = isset($v['id']) ? $v['id'] : null; $rows['no'] = isset($v['no']) ? $v['no'] : null; $rows['supplierName'] = isset($v['supplierName']) ? $v['supplierName'] : null; $rows['purchaseAmount'] = isset($v['purchaseAmount']) ? $v['purchaseAmount'] : null; $rows['originNo'] = isset($v['originNo']) ? $v['originNo'] : null; $rows['operatorName'] = isset($v['operatorName']) ? $v['operatorName'] : null; $rows['createTime'] = isset($v['operatorName']) ? date('Y-m-d H:i:s',$v['createTime']) : null; $rows['auditStatus'] = isset($v['auditStatus']) ? $v['auditStatus'] == StatusCode::$auditStatus['auditPass'] ? '已审核' : '未审核' : null; $rows['outStatus'] = isset($v['outStatus']) ? $v['outStatus'] == StatusCode::$standard ? '已出库' : '未出库' : null; $rows['goodsName'] = isset($value['goodsName']) ? $value['goodsName'] : null; $rows['goodsCode'] = isset($value['goodsCode']) ? $value['goodsCode'] : null; $rows['skuName'] = isset($value['skuName']) ? $value['skuName'] : null; $rows['buyerNum'] = isset($value['buyerNum']) ? $value['buyerNum'] : null; $rows['buyerUnitPrice'] = isset($value['buyerUnitPrice']) ? $value['buyerUnitPrice'] : null; $rows['subtotalPrice'] = isset($value['subtotalPrice']) ? $value['subtotalPrice'] : null; foreach ( $rows as $kk => $vv){ $rs[$kk] = mb_convert_encoding($vv, 'GBK', 'utf-8'); //转译编码 } fputcsv($fp, $rs); $rows = []; } return ResultWrapper::success('成功'); } /** * 修改采购退货 * @param $updateData * @param $where * @return ResultWrapper */ public function updatePurchaseOutData($updateData, $where) { $dbResult = $this->objDPurchaseOut->update($updateData, $where); if($dbResult === false){ return ResultWrapper::fail($this->objDPurchaseOut->error(), ErrorCode::$dberror); } return ResultWrapper::success($dbResult); } }