From 4f72b722aa97a7b37e2a7697f2125c93b8e0c1b8 Mon Sep 17 00:00:00 2001
From: heshupeng <hsp@bronet.cn>
Date: Thu, 14 Jan 2021 22:50:59 +0800
Subject: [PATCH] 经销商修改库存

---
 application/admin/controller/dealer/Dealer.php       |  76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 application/admin/controller/shopro/goods/Goods.php  |   7 +++++++
 application/admin/view/dealer/dealer/dealer_sku.html | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 application/admin/view/dealer/dealer/goods.html      |  22 ++++++++++++++++++++++
 public/assets/js/backend/dealer/dealer.js            | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 316 insertions(+), 2 deletions(-)
 create mode 100644 application/admin/view/dealer/dealer/dealer_sku.html
 create mode 100644 application/admin/view/dealer/dealer/goods.html

diff --git a/application/admin/controller/dealer/Dealer.php b/application/admin/controller/dealer/Dealer.php
index 5a3dab4..8c6fffc 100644
--- a/application/admin/controller/dealer/Dealer.php
+++ b/application/admin/controller/dealer/Dealer.php
@@ -293,9 +293,21 @@ class Dealer extends Backend
     }
 
     /**
-     * 供货商品
+     * 供应商品
      */
-    public function goods($id)
+    public function goods()
+    {
+        if($this->request->isAjax()){
+            (new \app\admin\controller\shopro\goods\Goods)->index();
+        }
+        $this->assignconfig('dealer_id', $this->request->param('dealer_id'));
+        return $this->view->fetch();
+    }
+
+    /**
+     * 经销商规格
+     */
+    public function dealerSku($id, $dealer_id)
     {
         $skuList = \app\admin\model\shopro\goods\Sku::all(['pid' => 0, 'goods_id' => $id]);
         if ($skuList) {
@@ -306,14 +318,74 @@ class Dealer extends Backend
         $skuPrice = \app\admin\model\shopro\goods\SkuPrice::all(['goods_id' => $id]);
 
 
+        //编辑
+        foreach ($skuPrice as $k => &$p) {
+            $actSkuPrice[$k] = \app\admin\model\dealer\DealerStock::get(['sku_price_id' => $p['id'], 'dealer_id' => $dealer_id]);
+
+            if (!$actSkuPrice[$k]) {
+
+                $actSkuPrice[$k]['id'] = 0;
+                $actSkuPrice[$k]['dealer_id'] = $dealer_id;
+                $actSkuPrice[$k]['goods_id'] = $id;
+                $actSkuPrice[$k]['status'] = 'down';
+                $actSkuPrice[$k]['stock'] = '';
+                $actSkuPrice[$k]['sales'] = '0';
+                $actSkuPrice[$k]['sku_price_id'] = $p['id'];
+
+            }else{
+                $actSkuPrice[$k]['status'] = 'up';
+            }
+        }
+
         $this->assignconfig('skuList', $skuList);
 
         $this->assignconfig('skuPrice', $skuPrice);
+        $this->assignconfig('actSkuPrice', $actSkuPrice);
 
         return $this->view->fetch();
 
     }
 
+    /**
+     * 更新经销商规格
+     */
+    public function createOrUpdateSku($actSkuPrice)
+    {
+        $actSkuPrice = json_decode($actSkuPrice, true);
+
+        $list = [];
+        $act = new \app\admin\model\dealer\DealerStock;
+
+        Db::startTrans();
+        try {
+            foreach ($actSkuPrice as $a => $c) {
+                if($c['status'] == 'down'){
+                    $act->where('id',$c['id'])->delete();
+                }else{
+                    if ($c['id'] == 0) {
+                        unset($c['id']);
+                    }
+                    $list[] = $c;
+                }
+            }
+            $act->allowField(true)->saveAll($list);
+
+            // 更新规格库存
+            foreach ($list as $v) {
+                $sum_stock = $act->where('sku_price_id',$v['sku_price_id'])->sum('stock');
+                \app\admin\model\shopro\goods\SkuPrice::where('id',$v['sku_price_id'])->update(['stock'=>$sum_stock]);
+            }
+            Db::commit();
+        } catch (PDOException $e) {
+            Db::rollback();
+            $this->error($e->getMessage());
+        } catch (Exception $e) {
+            Db::rollback();
+            $this->error($e->getMessage());
+        }
+        $this->success();
+    }
+
     public function selectpage()
     {
         return parent::selectpage(); // TODO: Change the autogenerated stub
diff --git a/application/admin/controller/shopro/goods/Goods.php b/application/admin/controller/shopro/goods/Goods.php
index 3115d02..6521284 100644
--- a/application/admin/controller/shopro/goods/Goods.php
+++ b/application/admin/controller/shopro/goods/Goods.php
@@ -529,6 +529,7 @@ class Goods extends Backend
         $min_price = $this->request->get("min_price", "");
         $max_price = $this->request->get("max_price", "");
         $category_id = $this->request->get('category_id', 0);
+        $dealer_id = $this->request->get('dealer_id', 0);
 
         $name = $this->model->getQuery()->getTable();
         $tableName = $name . '.';
@@ -596,6 +597,12 @@ class Goods extends Backend
             });
         }
 
+        if(isset($dealer_id) && $dealer_id != 0) {
+            $goods_ids = \app\admin\model\dealer\DealerStock::where('dealer_id',$dealer_id)->column('goods_id');
+            $goods_ids = $goods_ids ? array_unique($goods_ids) : [];
+            $goods = $goods->where('id','in',$goods_ids);
+        }
+
         return $goods;
     }
 }
diff --git a/application/admin/view/dealer/dealer/dealer_sku.html b/application/admin/view/dealer/dealer/dealer_sku.html
new file mode 100644
index 0000000..4e4cc49
--- /dev/null
+++ b/application/admin/view/dealer/dealer/dealer_sku.html
@@ -0,0 +1,101 @@
+<style>
+    .skuChildrenBox {
+        display: flex;
+        padding: 0 15px;
+        flex-wrap: wrap;
+    }
+
+    .skuChildren {
+        margin-right: 10px;
+        margin-top: 10px;
+        width: 50px;
+        height: 24px;
+        line-height: 24px;
+        color: rgba(80, 80, 80, 1);
+        border-radius: 2px;
+        font-size: 12px;
+        text-align: center;
+        border: 1px solid rgba(231, 231, 231, 1);
+        position: relative;
+    }
+
+    .skuChildrenSelect {
+        border: 1px solid rgba(212, 48, 48, 1)
+    }
+
+    .skuChildren-close {
+        position: absolute;
+        background: rgba(212, 48, 48, 1);
+        color: #fff;
+        border-radius: 50%;
+        width: 12px;
+        height: 12px;
+        top: -6px;
+        right: -6px;
+        font-style: 12px;
+        -webkit-transform: scale(0.8);
+    }
+
+    .status {
+        border: none;
+        cursor: pointer;
+        color: #3498db;
+        font-size: 14px;
+    }
+
+    .status-s {
+        color: #ED3C30;
+    }
+
+    [v-cloak] {
+        display: none
+    }
+</style>
+<script src="__CDN__/assets/addons/shopro/libs/vue.js"></script>
+<form id="add-form" class="form-horizontal" role="form" data-toggle="validator" method="POST" action="">
+    <div class="panel panel-default panel-intro">
+        <div class="panel-body">
+            <div id="myTabContent" class="tab-content">
+                <div class="tab-pane fade active in" id="basic">
+                    <div class="widget-body no-padding">
+                        <table class="table" id="skuPrce" v-cloak>
+                            <thead>
+                                <tr>
+                                    <th v-for="(item, i) in skuList" :key="i">
+                                        {{item.name}}
+                                    </th>
+                                    <th width="80">库存</th>
+                                    <th width="80">价格</th>
+                                    <th width="80">参与库存</th>
+                                    <th width="80">操作</th>
+                                </tr>
+                            </thead>
+                            <tbody>
+                                <tr v-for="(item, i) in skuPrice" :key="i" >
+                                    <td v-for="(v, j) in item.goods_sku_text" :key="j">
+                                        {{v}}
+                                    </td>
+                                    <td width="80"><input type="text" v-model="item.stock" class="input-sm form-control"
+                                            disabled>
+                                    </td>
+                                    <td width="80">
+                                        <input type="text" v-model="item.price" class="input-sm form-control" disabled>
+                                    </td>
+                                    <td width="80"><input v-if="actSkuPrice[i].status=='up'" type="number" v-model="actSkuPrice[i].stock" class="input-sm form-control">
+                                    </td>
+                                    <td width="80">
+                                        <span class="input-sm form-control status" @click="goJoin(i)">{{actSkuPrice[i].status=='up'?'取消':'参与'}}</span></td>
+                                </tr>
+                            </tbody>
+                        </table>
+                    </div>
+                </div>
+            </div>
+        </div>
+        <div class="form-group layer-footer">
+            <label class="control-label col-xs-12 col-sm-2"></label>
+            <div class="col-xs-12 col-sm-8">
+                <button type="button" class="btn btn-success btn-embossed" id="stockSub">{:__('OK')}</button>
+            </div>
+        </div>
+</form>
\ No newline at end of file
diff --git a/application/admin/view/dealer/dealer/goods.html b/application/admin/view/dealer/dealer/goods.html
new file mode 100644
index 0000000..75082ea
--- /dev/null
+++ b/application/admin/view/dealer/dealer/goods.html
@@ -0,0 +1,22 @@
+<div class="panel panel-default panel-intro">
+    {:build_heading()}
+
+    <div class="panel-body">
+        <div id="myTabContent" class="tab-content">
+            <div class="tab-pane fade active in" id="one">
+                <div class="widget-body no-padding">
+                    <div id="toolbar" class="toolbar">
+                        <a href="javascript:;" class="btn btn-primary btn-refresh" title="{:__('Refresh')}" ><i class="fa fa-refresh"></i> </a>
+
+                    </div>
+                    <table id="table" class="table table-striped table-bordered table-hover table-nowrap"
+                           data-operate-edit="{:$auth->check('banner/edit')}" 
+                           data-operate-del="{:$auth->check('banner/del')}" 
+                           width="100%">
+                    </table>
+                </div>
+            </div>
+
+        </div>
+    </div>
+</div>
diff --git a/public/assets/js/backend/dealer/dealer.js b/public/assets/js/backend/dealer/dealer.js
index 3e1e92d..183fa62 100644
--- a/public/assets/js/backend/dealer/dealer.js
+++ b/public/assets/js/backend/dealer/dealer.js
@@ -43,6 +43,14 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
                                     classname: 'btn btn-xs btn-primary btn-dialog',
                                     icon: 'fa fa-list',
                                     url: 'shopro/order/order?dealer_id={id}',
+                                },
+                                {
+                                    name: 'goods',
+                                    text: __('供应商品'),
+                                    title: __('供应商品'),
+                                    classname: 'btn btn-xs btn-primary btn-dialog',
+                                    icon: 'fa fa-list',
+                                    url: 'dealer/dealer/goods?dealer_id={id}&page_type=select',
                                 }
                             ],
                             formatter: Table.api.formatter.operate
@@ -139,6 +147,110 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
             })
             Controller.api.bindevent();
         },
+        goods: function () {
+            // 初始化表格参数配置
+            Table.api.init({
+                extend: {
+                    index_url: 'shopro/goods/goods/index' + location.search,
+                    table: 'shopro_goods',
+                }
+            });
+
+            var table = $("#table");
+
+            // 初始化表格
+            table.bootstrapTable({
+                url: $.fn.bootstrapTable.defaults.extend.index_url,
+                pk: 'id',
+                sortName: 'id',
+                columns: [
+                    [
+                        {checkbox: true},
+                        {field: 'id', title: __('Id')},
+                        {field: 'image', title: __('商品图片'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
+                        {field: 'title', title: __('商品名称'), operate: 'LIKE'},
+                        {field: 'price', title: __('价格'), operate:'BETWEEN'},
+                        {field: 'stock', title: __('库存'), operate:false},
+                        {field: 'sales', title: __('销量'), operate:false},
+                        // {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
+                        //操作栏,默认有编辑、删除或排序按钮,可自定义配置buttons来扩展按钮
+                        {
+                            field: 'operate',
+                            title: __('Operate'),
+                            table: table,
+                            events: Table.api.events.operate,
+                            buttons: [
+                                {
+                                    name: 'stock',
+                                    text: __('修改库存'),
+                                    title: __('修改库存'),
+                                    classname: 'btn btn-xs btn-success btn-dialog',
+                                    icon: 'fa fa-edit',
+                                    url: 'dealer/dealer/dealerSku?id={id}&dealer_id='+Config.dealer_id,
+                                }
+                            ],
+                            formatter: Table.api.formatter.operate
+                        }
+                    ]
+                ]
+            });
+
+            // 为表格绑定事件
+            Table.api.bindevent(table);
+        },
+        dealersku: function () {
+            var vueSku = new Vue({
+                el: "#skuPrce",
+                data() {
+                    return {
+                        skuList: Config.skuList,
+                        skuPrice: Config.skuPrice,
+                        actSkuPrice: Config.actSkuPrice,
+                    }
+                },
+                methods: {
+                    goJoin(i) {
+                        let status = this.actSkuPrice[i].status === 'up' ? 'down' : 'up';
+                        this.$set(this.actSkuPrice[i], 'status', status)
+                    },
+                },
+
+            })
+            $(document).on("click", "#stockSub", function () {
+                Layer.confirm('确认提交吗', {
+                    btn: ['确认', '取消']
+                }, function () {
+                    let isSubmit = true
+                    isSubmit = !(vueSku.$data.actSkuPrice.every(function (item, index, array) {
+                        return item.status == 'down';
+                    }))
+                    vueSku.$data.actSkuPrice.forEach(i => {
+                        if (i.status == 'up' && !i.stock) {
+                            isSubmit = false
+                        }
+                    })
+                    if (isSubmit) {
+                        Fast.api.ajax({
+                            url : 'dealer/dealer/createOrUpdateSku',
+                            data : {'actSkuPrice':JSON.stringify(vueSku.$data.actSkuPrice)}
+                        },function () {
+                            Layer.closeAll();
+                            location.reload();
+                        },function () {
+                            layer.msg('更新失败');
+                        });
+                        return true;
+                    } else {
+                        layer.msg('请把信息填写完整');
+                    }
+                    return true;
+                }, function () {
+                    Layer.closeAll();
+                    return false;
+                });
+            })
+            Controller.api.bindevent();
+        },
         api: {
             bindevent: function () {
                 Form.api.bindevent($("form[role=form]"));
--
libgit2 0.24.0