Browse Source

批量设置类目信息

wufeng 2 years ago
parent
commit
a7714a6ca7
2 changed files with 82 additions and 0 deletions
  1. 81 0
      app/admin/controller/InvCat.php
  2. 1 0
      app/admin/route/app.php

+ 81 - 0
app/admin/controller/InvCat.php

@@ -4,7 +4,9 @@
 namespace app\admin\controller;
 namespace app\admin\controller;
 use app\admin\BaseController;
 use app\admin\BaseController;
 use think\App;
 use think\App;
+use think\Exception;
 use think\facade\Db;
 use think\facade\Db;
+use think\facade\Validate;
 
 
 class InvCat extends BaseController{
 class InvCat extends BaseController{
     public function __construct(App $app) {parent::__construct($app);}
     public function __construct(App $app) {parent::__construct($app);}
@@ -157,4 +159,83 @@ class InvCat extends BaseController{
         }
         }
       return app_show(0,"获取成功",$list);
       return app_show(0,"获取成功",$list);
     }
     }
+
+    //批量设置类目信息
+    public function addGoodBatch()
+    {
+
+        $list = $this->request->post('list/a', [], 'trim');
+
+        $val = Validate::rule([
+            'spuCode|商品编号' => 'require|max:255',
+            'tax|税率' => 'require|max:4',
+            'cat_code|类目编号简写' => 'require|max:255',
+            'inv_good_name|开票商品名称' => 'require|max:255',
+            'inv_tag|税率标识' => 'require|between:0,3',
+            'is_discount|是否有优惠政策' => 'require|in:0,1',
+            'addTax|增值税管理内容' => 'require|max:255',
+        ]);
+
+        Db::startTrans();
+
+        try {
+
+            $all_spuCode = Db::name('good')
+                ->whereIn('spuCode', array_column($list, 'spuCode'))
+                ->column('id', 'spuCode');
+
+            $all_cat_code = Db::name('inv_cat')
+                ->whereIn('cat_code', array_column($list, 'cat_code'))
+                ->column('tax,merge_code,short_name', 'cat_code');
+
+            $date = date('Y-m-d H:i:s');
+            foreach ($list as $value) {
+                if (!$val->check($value)) throw new Exception($val->getError());
+
+                if (!isset($all_spuCode[$value['spuCode']])) throw new Exception('商品数据未找到');
+                if (!isset($all_cat_code[$value['cat_code']])) throw new Exception('未找到对应的开票类目');
+                if (!in_array($value['tax'], $all_cat_code[$value['cat_code']]['tax'] != '' ? explode('、', $all_cat_code[$value['cat_code']]['tax']) : [])) throw new Exception('未找到对应的开票类目税率');
+                $value['tax'] = str_replace('%', '', $value['tax']);
+                $value['tax'] = round(bcdiv($value['tax'], 100, 3), 2);
+
+
+                if ($value['is_discount'] == 1) {
+                    // 优惠政策下 税率标识只能选择 0非零说率 1免税 2不征税 增值税管理为空"出口零税/免税/不征税"
+                    if ($value['tax'] == 0 && $value['inv_tag'] == 3) throw new Exception('税率标识不能选择零税率');
+
+                    if ($value['addTax'] == '') throw new Exception('参数 addTax 不能为空');
+
+                } else {
+                    // 非优惠政策下 零税率 税率标识只能选择 3 普通零税率  增值税管理为空
+                    // 非优惠政策下 非零税率 税率标识只能选择 0 非零税率  增值税管理为空
+                    if ($value['tax'] == 0 && $value['inv_tag'] != 3) throw new Exception('税率标识只能选择零税率');
+
+                    $value['addTax'] = '';
+                }
+
+                $data = [
+                    'inv_cat_name' => $all_cat_code[$value['cat_code']]['short_name'],
+                    'inv_cat_code' => $all_cat_code[$value['cat_code']]['merge_code'],
+                    'inv_tax' => $value['tax'],
+                    'inv_good_name' => $value['inv_good_name'],
+                    'inv_tag' => $value['inv_tag'],
+                    'is_discount' => $value['is_discount'],
+                    'addTax' => $value['addTax'],
+                    'status' => 1,
+                    'updatetime' => $date
+                ];
+
+                Db::name("good")->where('id', $all_spuCode[$value['spuCode']])->update($data);
+
+            }
+
+            Db::commit();
+
+            return app_show(0, '添加成功');
+        } catch (Exception $exception) {
+            Db::rollback();
+            return error_show(1004, '添加失败,' . $exception->getMessage());
+        }
+
+    }
 }
 }

+ 1 - 0
app/admin/route/app.php

@@ -165,6 +165,7 @@ Route::rule("catlist","admin/InvCat/catlist");
 Route::rule("catquery","admin/InvCat/query");
 Route::rule("catquery","admin/InvCat/query");
 Route::rule("addgood","admin/InvCat/AddGood");
 Route::rule("addgood","admin/InvCat/AddGood");
 Route::rule("goodinfo","admin/InvCat/goodinfo");
 Route::rule("goodinfo","admin/InvCat/goodinfo");
+Route::rule("addGoodBatch","admin/InvCat/addGoodBatch");//批量设置类目信息
 
 
 //流程
 //流程
 Route::rule('process_get_list','admin/Process/getList');
 Route::rule('process_get_list','admin/Process/getList');