store_id)){ $this->store_id = get_store_id(); } } public function search () { try { $where = [ 'epo.is_delete' => 0 ]; if (isset($this->admin_supplier_id)) { $where['epo.supplier_id'] = $this->admin_supplier_id; } else { $where['epo.front_agent_admin_id'] = $this->front_agent_admin_id; } $query = AgentFrontErpPurchaseOrder::find()->alias('epo')->where($where); $query->leftJoin(['s' => Supplier::tableName()], 's.id = epo.supplier_id'); if (!empty($this->goods_name)) { $query1 = AgentFrontErpPurchase::find()->alias('ep') ->leftJoin(['ei' => AgentFrontErpInventory::tableName()], 'ei.id = ep.inventory_id') ->leftJoin(['g' => Goods::tableName()], 'ei.goods_id = g.id') ->where(['like', 'g.name', $this->goods_name]) ->groupBy('ep.purchase_order_id') ->select('ep.purchase_order_id'); $query->andWhere(['epo.id' => $query1]); } if (!empty($this->goods_id)) { $query2 = AgentFrontErpPurchase::find()->alias('ep') ->leftJoin(['ei' => AgentFrontErpInventory::tableName()], 'ei.id = ep.inventory_id') ->where(['ei.goods_id' => $this->goods_id]) ->groupBy('ep.purchase_order_id') ->select('ep.purchase_order_id'); $query->andWhere(['epo.id' => $query2]); } if (!empty($this->supplier_name)) { $query->andWhere(['like', 's.supplier_name', $this->supplier_name]); } if (!empty($this->supplier_id)) { $query->andWhere(['epo.supplier_id' => $this->supplier_id]); } if ($this->status > -1) { $query->andWhere(['epo.status' => $this->status]); } if ($this->ids) { $query->andWhere(['epo.id' => $this->ids]); } $query->orderBy('epo.id DESC'); $query->select('epo.*, s.supplier_name'); $pagination = pagination_make($query); foreach ($pagination['list'] as &$item) { $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']); $item['order_status'] = intval($item['order_status']); $item['order_status_text'] = AgentFrontErpPurchaseOrder::ORDER_STATUS_ARR[$item['order_status']]; } return [ 'code' => 0, 'msg' => 'success', 'data' => $pagination, // 'q' => $query->createCommand()->getRawSql(), ]; } catch (\Exception $e) { \Yii::error($e); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function info () { try { $where = ['id' => $this->id]; if (isset($this->admin_supplier_id)) { $where['supplier_id'] = $this->admin_supplier_id; } else { $where['front_agent_admin_id'] = $this->front_agent_admin_id; } $epo = AgentFrontErpPurchaseOrder::find()->where($where)->one(); if (!$epo) { throw new \Exception('查询采购详情失败'); } $epo['created_at'] = date("Y-m-d H:i:s", $epo['created_at']); $supplier_name = Supplier::findOne($epo['supplier_id'])['supplier_name']; $ep = AgentFrontErpPurchase::find()->alias('ep') ->leftJoin(['ei' => AgentFrontErpInventory::tableName()], 'ei.id = ep.inventory_id') ->leftJoin(['g' => AgentFrontErpCloudGoods::tableName()], 'ei.goods_id = g.cloud_goods_id') ->where(['ep.purchase_order_id' => $this->id]) ->select('ep.id, ep.num, g.cover_pic, g.goods_name, ei.attr_info') ->asArray()->all(); $totalPrice = 0; foreach ($ep as &$item) { $attr_info = json_decode($item['attr_info'], true); $item['attr_names'] = implode(',', array_column($attr_info['attr_list'], 'attr_name')); $item['goods_price'] = $attr_info['price']; $totalPrice += $item['goods_price'] * $item['num']; } $admin = Admin::findOne($epo['front_agent_admin_id']); $district = implode('', District::find()->where(['id' => [$admin->province_id, $admin->city_id, $admin->district_id]])->select('name')->column()); return [ 'code' => 0, 'msg' => 'success', 'data' => [ 'supplier_name' => $supplier_name, 'epo' => $epo, 'ep' => $ep, 'totalPrice' => $totalPrice, 'store' => [ 'name' => $admin->name, 'contact_tel' => $admin->mobile, 'address' => $admin->address, 'district' => $district, ] ], // 'q' => $query->createCommand()->getRawSql(), ]; } catch (\Exception $e) { \Yii::error($e); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function searchPurchase () { try { $where = [ 'epo.is_delete' => 0 ]; if (isset($this->admin_supplier_id)) { $where['epo.supplier_id'] = $this->admin_supplier_id; } else { $where['epo.front_agent_admin_id'] = $this->front_agent_admin_id; } $query = AgentFrontErpPurchase::find()->alias('ep') ->leftJoin(['epo' => AgentFrontErpPurchaseOrder::tableName()], 'ep.purchase_order_id = epo.id') ->leftJoin(['ei' => AgentFrontErpInventory::tableName()], 'ei.id = ep.inventory_id') ->leftJoin(['g' => AgentFrontErpCloudGoods::tableName()], 'ei.goods_id = g.cloud_goods_id') ->where($where); $query->leftJoin(['es' => Supplier::tableName()], 'es.id = epo.supplier_id'); if ($this->ids) { if(!is_array($this->ids)){ $this->ids = explode(',', $this->ids); } $query->andWhere(['epo.id' => $this->ids]); } $query->orderBy('epo.id DESC'); $query->select('ep.id, ep.num, es.supplier_name, epo.order_no, epo.created_at, g.cover_pic, g.goods_name, ei.attr_info, epo.front_agent_admin_id'); $pagination = pagination_make($query); foreach ($pagination['list'] as &$item) { $admin = Admin::findOne($item['front_agent_admin_id']); $item['front_agent_name'] = $admin->name ?? ''; $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']); $attr_info = json_decode($item['attr_info'], true); $item['attr_names'] = implode(',', array_column($attr_info['attr_list'], 'attr_name')); $item['goods_price'] = $attr_info['price']; } if($this->export){ return $this->export($pagination['list']); } return [ 'code' => 0, 'msg' => 'success', 'data' => $pagination, // 'q' => $query->createCommand()->getRawSql(), ]; } catch (\Exception $e) { \Yii::error($e); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } private function export($list) { $rows = [[ 'ID', '采购单号', isset($this->admin_supplier_id) ? '仓库' : '供货商', '商品', '规格', '数量', '单价', '时间', ]]; foreach($list as $item){ $r = [ $item['id'], $item['order_no'], isset($this->admin_supplier_id) ? $item['front_agent_name'] : $item['supplier_name'], $item['goods_name'], $item['attr_names'], $item['num'], $item['goods_price'], $item['created_at'], ]; $rows[] = $r; } $writer = \Spatie\SimpleExcel\SimpleExcelWriter::streamDownload(time() . '.xlsx')->noHeaderRow() ->addRows($rows)->toBrowser(); } public function save () { $t = \Yii::$app->db->beginTransaction(); try { $supplier = Supplier::findOne(['id' => $this->supplier_id, 'is_delete' => 0]); if (!$supplier) { throw new \Exception('供货商不存在'); } if (!in_array($supplier->cloud_supplier_id, AgentFrontBind::getAgentFrontBindSupplierId($this->front_agent_admin_id) ?? [])) { throw new \Exception('当前供货商商品未与仓库关联'); } //采购单生成时候给云仓通知减少对应规格的库存(生成时候判断云仓库存是否足够,足够再减少) start $purchase = $this->purchase; foreach($purchase as $purchase_index => $purchase_item){ $updateAttrList = []; $erpInventory = AgentFrontErpInventory::findOne($purchase_item['id']); if ($erpInventory) { $attr_info = json_decode($erpInventory->attr_info, true); if (!empty($attr_info)) { $updateAttrList = [ [ 'attr_id_list' => array_column($attr_info['attr_list'], 'attr_id'), 'num' => -$purchase_item['num'] ] ]; } $form = new \app\modules\admin\models\SupplierForm(); $res = $form->setGoodsAttrNum($this->supplier_id, $erpInventory->goods_id, $updateAttrList); //兼容选择多个但是云仓报错的问题 if ($res['code'] !== 0) { unset($purchase[$purchase_index]); } } } //end $model = new AgentFrontErpPurchaseOrder(); $model->order_no = OrderNo::getOrderNo(OrderNo::ERP_PURCHASE); $model->front_agent_admin_id = $this->front_agent_admin_id; $model->supplier_id = $this->supplier_id; $model->num = count($this->purchase); if (!$model->save()) { \Yii::error([__METHOD__, $model->attributes]); throw new \Exception(array_shift($model->getFirstErrors())); } AgentFrontErpPurchase::saveList($model->id, $this->purchase); $t->commit(); return [ 'code' => 0, 'msg' => '操作成功!' ]; } catch (\Exception $e) { \Yii::error($e); $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function statusPrint () { $t = \Yii::$app->db->beginTransaction(); try { $model = AgentFrontErpPurchaseOrder::findOne($this->id); $model->status = 1; if (!$model->save()) { \Yii::error([__METHOD__, $model->attributes]); throw new \Exception(array_shift($model->getFirstErrors())); } AgentFrontErpPurchase::saveList($model->id, $this->purchase); $t->commit(); return [ 'code' => 0, 'msg' => '操作成功!' ]; } catch (\Exception $e) { \Yii::error($e); $t->rollBack(); return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function setPurchaseOrderStatus() { try { $admin_supplier_id = $this->admin_supplier_id; $front_agent_admin_id = $this->front_agent_admin_id; $id = $this->id; $where = ['id' => $id]; if (isset($admin_supplier_id)) { $where['supplier_id'] = $admin_supplier_id; } else { $where['front_agent_admin_id'] = $front_agent_admin_id; } $epo = AgentFrontErpPurchaseOrder::findOne($where); if (!$epo) { throw new \Exception('查询采购信息失败'); } //仓库身份不可发货 if (isset($front_agent_admin_id) && $epo->order_status == AgentFrontErpPurchaseOrder::ORDER_STATUS_CREATE) { throw new \Exception('当前身份操作权限失败,不可发货'); } //供货商身份不可收货 if (isset($admin_supplier_id) && $epo->order_status == AgentFrontErpPurchaseOrder::ORDER_STATUS_SEND) { throw new \Exception('当前身份操作权限失败,不可收货'); } if (intval($epo->order_status) === AgentFrontErpPurchaseOrder::ORDER_STATUS_CONFIRM) { throw new \Exception('状态已达最终态不可操作'); } //发货 if (intval($epo->order_status) === AgentFrontErpPurchaseOrder::ORDER_STATUS_CREATE) { $epo->order_status = AgentFrontErpPurchaseOrder::ORDER_STATUS_SEND; } elseif (intval($epo->order_status) === AgentFrontErpPurchaseOrder::ORDER_STATUS_SEND) { //收货 $epo->order_status = AgentFrontErpPurchaseOrder::ORDER_STATUS_CONFIRM; } //收货完成 开始添加库存 if (!$epo->save()) { throw new \Exception(json_encode($epo->errors, JSON_UNESCAPED_UNICODE)); } //产品入库时候增加商品库存 //下单以及转单时候只判断仓库是否有货 //转单时候减少仓库库存 云仓判断如果是仓库配送产品转单时候就不减少库存 return [ 'code' => 0, 'msg' => '操作成功' ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } }