hrjy 3 年之前
父节点
当前提交
9fe25692e2

+ 12 - 2
app/admin/controller/system/SystemStoreStaff.php

@@ -67,7 +67,9 @@ class SystemStoreStaff extends AuthController
             Form::input('staff_name', '店员名称')->col(Form::col(24)),
             Form::input('phone', '手机号码')->col(Form::col(24)),
             Form::radio('verify_status', '核销开关', 1)->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '关闭']]),
-            Form::radio('status', '状态', 1)->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '关闭']])
+            Form::radio('status', '状态', 1)->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '关闭']]),
+            Form::radio('coupon_status', '添加优惠券状态', 1)->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '关闭']]),
+            Form::radio('price_status', '收款状态', 0)->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '关闭']]),
         ];
         $form = Form::make_post_form('添加评论', $field, Url::buildUrl('save'), 2);
         $this->assign(compact('form'));
@@ -108,7 +110,9 @@ class SystemStoreStaff extends AuthController
             Form::input('staff_name', '店员名称', $service['staff_name'])->col(Form::col(24)),
             Form::input('phone', '手机号码', $service['phone'])->col(Form::col(24)),
             Form::radio('verify_status', '统计管理开关', $service['verify_status'])->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '关闭']]),
-            Form::radio('status', '状态', $service['status'])->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '关闭']])
+            Form::radio('status', '状态', $service['status'])->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '关闭']]),
+            Form::radio('coupon_status', '添加优惠券状态', $service['coupon_status'])->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '关闭']]),
+            Form::radio('price_status', '收款状态', $service['price_status'])->options([['value' => 1, 'label' => '开启'], ['value' => 0, 'label' => '关闭']]),
         ];
 
         $form = Form::make_post_form('修改数据', $f, Url::buildUrl('save', compact('id')));
@@ -160,12 +164,18 @@ class SystemStoreStaff extends AuthController
             ['phone', ''],
             ['verify_status', 1],
             ['status', 1],
+            ['coupon_status'],
+            ['price_status']
         ]);
         if (!$id) {
             if (StaffModel::where('uid', $data['uid'])->count()) return Json::fail('添加的店员用户已存在!');
         }
         if ($data['uid'] == 0) return Json::fail('请选择用户');
         if ($data['store_id'] == '') return Json::fail('请选择所属门店');
+        if ($data['price_status'] == 1) {
+            $store = \app\admin\model\system\SystemStoreStaff::where('store_id', $data['store_id'])->where('uid', '<>',$data['uid'])->where('price_status', 1)->find();
+            if ($store) return Json::fail('已有收款账户不能添加第二位');
+        }
         if ($id) {
             $res = StaffModel::edit($data, $id);
             if ($res) {

+ 1 - 1
app/api/controller/PublicController.php

@@ -380,7 +380,7 @@ class PublicController
         $data['slider_image'] = json_decode($data['slider_image']);
         $data['images'] = json_decode($data['gatehead']);
         $coupon = StoreCoupon::where('store_id', $data['id'])->column('id');
-        $data['coupon'] = StoreProduct::field('id,store_name,price,ot_price,image')->where('coupon', 'in',$coupon)->where('id', '>', 2)->select();
+        $data['coupon'] = StoreProduct::field('id,store_name,price,ot_price,image')->where('is_del', 0)->where('is_show', 1)->where('coupon', 'in',$coupon)->where('id', '>', 2)->select();
         return app('json')->successful($data);
     }
 

+ 93 - 0
app/api/controller/user/UserController.php

@@ -3,6 +3,9 @@
 namespace app\api\controller\user;
 
 use app\admin\model\store\ProductCode;
+use app\admin\model\store\StoreProduct as ProductModel;
+use app\admin\model\store\StoreProductAttr;
+use app\admin\model\store\StoreProductCate;
 use app\admin\model\ump\WriteOff;
 use app\http\validates\user\AddressValidate;
 use app\models\store\StoreCoupon;
@@ -10,6 +13,7 @@ use app\models\store\StoreProduct;
 use app\models\system\SystemCity;
 use app\models\system\SystemStoreStaff;
 use app\models\user\UserVisit;
+use crmeb\services\JsonService as Json;
 use think\db\exception\DataNotFoundException;
 use think\db\exception\DbException;
 use think\db\exception\ModelNotFoundException;
@@ -31,6 +35,7 @@ use app\models\user\UserNotice;
 use crmeb\services\GroupDataService;
 use crmeb\services\UtilService;
 use think\facade\Db;
+use think\facade\Validate;
 
 /**
  * 用户类
@@ -784,4 +789,92 @@ class UserController
         }
     }
 
+    public function store_coupon(Request $request)
+    {
+        $user = SystemStoreStaff::where('uid', $request->uid())->find();
+        if (!$user) return app('json')->fail('不是店员不能添加优惠券');
+        if ($user['coupon_status'] == 0) return app('json')->fail('未开启添加优惠券功能');
+
+        $data = UtilService::postMore([
+            ['title', ''],
+            ['price', 0],
+            ['ot_price', 0],
+            ['coupon_price', ''],
+            ['start_time', ''],
+            ['end_time', ''],
+            ['store_info', ''],
+            ['stock', 0]
+        ]);
+        $validate = Validate::rule(['title' => 'require', 'price' => 'require', 'ot_price' => 'require', 'coupon_price' => 'require', 'start_time' => 'require', 'end_time' => 'require', 'store_info' => 'require', 'stock' => 'require',]);
+        $validate->message([
+            'title.require' => '请填写优惠券名称',
+            'price.require' => '请填写价格',
+            'ot_price.require' => '请填写原价',
+            'coupon_price.require' => '请填写优惠券面值',
+            'start_time.require' => '请填写开始时间',
+            'end_time.require' => '请填写结束时间',
+            'store_info.require' => '请填写优惠券介绍',
+            'stock.require' => '请填写数量',
+        ]);
+        if (!$validate->check($data)) return app('json')->fail($validate->getError());
+        ProductModel::beginTrans();
+        $coupon = StoreCoupon::create([
+            'title' => $data['title'],
+            'coupon_price' => $data['coupon_price'],
+            'start_time' => strtotime($data['start_time']),
+            'end_time' => strtotime($data['end_time']),
+            'store_id' => $user['store_id'],
+            'status' =>  1
+        ]);
+        $product = StoreProduct::create([
+            'image' => 'http://xl.frp.liuniu946.com/uploads/attach/2022/05/20220510/1ab4cc277e63c34e2157f0ea67d870f2.jpg',
+            'slider_image' => json_encode([]),
+            'store_name' => $data['title'],
+            'store_info' => $data['store_info'],
+            'cate_id' => 4,
+            'price' => $data['price'],
+            'ot_price' => $data['ot_price'],
+            'activity' => '1,2,3',
+            'store_type' => 1,
+            'coupon' => $coupon['id'],
+            'stock' => $data['stock'],
+            'ficti' => 0
+        ]);
+        $cateData[] = ['product_id' => $product['id'], 'cate_id' => 4, 'add_time' => time()];
+        StoreProductCate::insertAll($cateData);
+        $attr = [
+            [
+                'value' => '规格',
+                'detailValue' => '',
+                'attrHidden' => '',
+                'detail' => ['默认']
+            ]
+        ];
+        $detail[] = [
+            'pic' => 'http://xl.frp.liuniu946.com/uploads/attach/2022/05/20220510/1ab4cc277e63c34e2157f0ea67d870f2.jpg',
+            'price' => $data['price'],
+            'integral' => 0,
+            'cost' => 0,
+            'ot_price' => $data['ot_price'],
+            'stock' => $data['stock'],
+            'bar_code' => '',
+            'volume' => 0,
+            'weight' => 0,
+            'brokerage' => 0,
+            'brokerage_two' => 0,
+            'value1' => '规格',
+            'detail' => [
+                '规格' => '默认'
+            ]
+        ];
+        $attr_res = StoreProductAttr::createProductAttr($attr, $detail, $product['id']);
+        if ($attr_res) {
+            ProductModel::commitTrans();
+            return Json::success('添加产品成功!');
+        } else {
+            ProductModel::rollbackTrans();
+            return Json::fail(StoreProductAttr::getErrorInfo());
+        }
+    }
+
 }

+ 25 - 0
app/models/store/StoreCoupon.php

@@ -31,4 +31,29 @@ class StoreCoupon extends BaseModel
     protected $name = 'store_coupon';
 
     use ModelTrait;
+
+
+    /**
+     * 优惠券过期
+     * @return void
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public static function end_coupon()
+    {
+        $coupon = StoreCoupon::where('end_time', '<', time())->select();
+        if ($coupon){
+            foreach ($coupon as $item)
+            {
+                if ($item['end_time'] > 0){
+                    $product = StoreProduct::where('coupon', $item['id'])->where('is_show', 1)->find();
+                    if ($product){
+                        $product['is_show'] = 0;
+                        $product->save();
+                    }
+                }
+            }
+        }
+    }
 }

+ 13 - 0
app/models/store/StoreOrder.php

@@ -9,6 +9,7 @@ namespace app\models\store;
 
 use app\admin\model\system\ShippingTemplatesFree;
 use app\admin\model\system\ShippingTemplatesRegion;
+use app\models\system\SystemStoreStaff;
 use crmeb\basic\BaseModel;
 use think\db\exception\DataNotFoundException;
 use think\db\exception\DbException;
@@ -862,6 +863,18 @@ class StoreOrder extends BaseModel
             }else if ($product['price'] == 399 and $product['cate_id'] == 2){
                 if ($user['spread_uid']) self::superior($user['spread_uid'],$user['nickname']);
                 if ($user['level'] < 1) $user['level'] = 1;
+            }else if ($product['cate_id'] == 4){
+                $coupon = StoreCoupon::where('id', 'in', $product['coupon'])->find();
+                $uid = SystemStoreStaff::where('store_id', $coupon['store_id'])->where('price_status', 1)->value('uid');
+                if ($uid){
+                    $user = User::where('uid', $uid)->find();
+                    $charge = SystemConfigService::get('coupon_charge')/100;
+                    $user['brokerage_price'] += $product['price'] - ($product['price'] * $charge);
+                    $order['status'] = 2;
+                    $user->save();
+                    $order->save();
+                    UserBill::income('优惠券卖出', $uid, 'now_money', 'brokerage', $product['price'] - ($product['price'] * $charge), '', $user['brokerage_price'], '优惠券'.$product['store_name'].'卖出');
+                }
             }
             $user->save();
         }

+ 8 - 0
crmeb/subscribes/TaskSubscribe.php

@@ -4,6 +4,7 @@ namespace crmeb\subscribes;
 
 use app\admin\model\system\SystemAttachment;
 use app\models\store\StoreBargainUser;
+use app\models\store\StoreCoupon;
 use app\models\store\StoreOrder;
 use app\models\store\StorePink;
 use app\models\user\UserToken;
@@ -75,6 +76,13 @@ class TaskSubscribe
         } catch (\Exception $e) {
             Db::rollback();
         }
+        try {
+            Db::startTrans();
+            StoreCoupon::end_coupon();
+            Db::commit();
+        } catch (\Exception $e) {
+            Db::rollback();
+        }
     }
 
     /**

+ 2 - 0
route/api/route.php

@@ -172,6 +172,8 @@ Route::group(function () {
     Route::post('exchange', 'user.UserController/exchange')->name('exchange');// 兑换码兑换
     Route::post('donation', 'order.StoreOrderController/donation')->name('donation');// 转赠
 
+    Route::post('store_coupon', 'user.UserController/store_coupon')->name('store_coupon');// 添加优惠券
+
 })->middleware(\app\http\middleware\AllowOriginMiddleware::class)->middleware(\app\http\middleware\AuthTokenMiddleware::class, true);
 //未授权接口
 Route::group(function () {