| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models\agent\front_erp;
- use app\models\Admin;
- use app\models\AgentFrontBind;
- use app\models\AgentFrontErpCloudGoods;
- use app\models\Supplier;
- use app\utils\OrderNo;
- use app\models\AgentFrontErpInventory;
- use app\models\AgentFrontErpInventoryLog;
- use app\models\AgentFrontErpPurchase;
- use app\models\AgentFrontErpPurchaseOrder;
- use app\models\Goods;
- use app\models\Option;
- use app\constants\OptionSetting;
- use app\models\Store;
- use app\models\District;
- class PurchaseForm extends Model
- {
- public $export;
- public $store_id;
- public $id;
- public $ids;
- public $goods_name;
- public $supplier_name;
- public $goods_id;
- public $supplier_id;
- public $is_delete;
- public $status;
- public $purchase = [];
- public $front_agent_admin_id;
- public $admin_supplier_id;
- public function rules()
- {
- return [
- [['id', 'store_id', 'is_delete', 'status', 'export', 'front_agent_admin_id', 'admin_supplier_id'], 'integer'],
- [['ids', 'goods_name', 'supplier_name'], 'string'],
- [['is_delete', 'goods_id', 'supplier_id', 'purchase'], 'safe'],
- ];
- }
- public function init() {
- parent::init();
- if(empty($this->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()
- ];
- }
- }
- }
|