Browse Source

酒店订单核销

Panda 6 days ago
parent
commit
1a7a8cf74c

+ 44 - 0
application/admin/controller/Hotel.php

@@ -3,6 +3,9 @@
 namespace app\admin\controller;
 
 use app\common\controller\Backend;
+use think\Db;
+use think\exception\PDOException;
+use think\exception\ValidateException;
 
 /**
  * 【酒店】信息
@@ -33,6 +36,47 @@ class Hotel extends Backend
      * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
      * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
      */
+    /**
+     * 添加
+     *
+     * @return string
+     * @throws \think\Exception
+     */
+    public function add()
+    {
+        if (false === $this->request->isPost()) {
+            return $this->view->fetch();
+        }
+        $params = $this->request->post('row/a');
+        if (empty($params)) {
+            $this->error(__('Parameter %s can not be empty', ''));
+        }
+        $params = $this->preExcludeFields($params);
 
+        if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
+            $params[$this->dataLimitField] = $this->auth->id;
+        }
+        $result = false;
+        Db::startTrans();
+        try {
+            //是否采用模型验证
+            if ($this->modelValidate) {
+                $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
+                $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
+                $this->model->validateFailException()->validate($validate);
+            }
+            $result = $this->model->allowField(true)->save($params);
+
+            Db::name('hotel_wallet')->insert(['shop_id' =>$this->model->id]);
+            Db::commit();
+        } catch (ValidateException|PDOException|Exception $e) {
+            Db::rollback();
+            $this->error($e->getMessage());
+        }
+        if ($result === false) {
+            $this->error(__('No rows were inserted'));
+        }
+        $this->success();
+    }
 
 }

+ 45 - 0
application/admin/controller/HotelCanteen.php

@@ -3,6 +3,9 @@
 namespace app\admin\controller;
 
 use app\common\controller\Backend;
+use think\Db;
+use think\exception\PDOException;
+use think\exception\ValidateException;
 
 /**
  * 【酒店】餐厅
@@ -33,5 +36,47 @@ class HotelCanteen extends Backend
      * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
      */
 
+    /**
+     * 添加
+     *
+     * @return string
+     * @throws \think\Exception
+     */
+    public function add()
+    {
+        if (false === $this->request->isPost()) {
+            return $this->view->fetch();
+        }
+        $params = $this->request->post('row/a');
+        if (empty($params)) {
+            $this->error(__('Parameter %s can not be empty', ''));
+        }
+        $params = $this->preExcludeFields($params);
+
+        if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
+            $params[$this->dataLimitField] = $this->auth->id;
+        }
+        $result = false;
+        Db::startTrans();
+        try {
+            //是否采用模型验证
+            if ($this->modelValidate) {
+                $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
+                $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
+                $this->model->validateFailException()->validate($validate);
+            }
+            $result = $this->model->allowField(true)->save($params);
+
+            Db::name('hotel_canteen_wallet')->insert(['shop_id' =>$this->model->id]);
+            Db::commit();
+        } catch (ValidateException|PDOException|Exception $e) {
+            Db::rollback();
+            $this->error($e->getMessage());
+        }
+        if ($result === false) {
+            $this->error(__('No rows were inserted'));
+        }
+        $this->success();
+    }
 
 }

+ 45 - 0
application/admin/controller/UniversityEvent.php

@@ -3,6 +3,9 @@
 namespace app\admin\controller;
 
 use app\common\controller\Backend;
+use think\Db;
+use think\exception\PDOException;
+use think\exception\ValidateException;
 
 /**
  * 【老年大学】活动
@@ -33,5 +36,47 @@ class UniversityEvent extends Backend
      * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
      */
 
+    /**
+     * 添加
+     *
+     * @return string
+     * @throws \think\Exception
+     */
+    public function add()
+    {
+        if (false === $this->request->isPost()) {
+            return $this->view->fetch();
+        }
+        $params = $this->request->post('row/a');
+        if (empty($params)) {
+            $this->error(__('Parameter %s can not be empty', ''));
+        }
+        $params = $this->preExcludeFields($params);
+
+        if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
+            $params[$this->dataLimitField] = $this->auth->id;
+        }
+        $result = false;
+        Db::startTrans();
+        try {
+            //是否采用模型验证
+            if ($this->modelValidate) {
+                $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
+                $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
+                $this->model->validateFailException()->validate($validate);
+            }
+            $result = $this->model->allowField(true)->save($params);
+
+            Db::name('university_event_wallet')->insert(['shop_id' =>$this->model->id]);
+            Db::commit();
+        } catch (ValidateException|PDOException|Exception $e) {
+            Db::rollback();
+            $this->error($e->getMessage());
+        }
+        if ($result === false) {
+            $this->error(__('No rows were inserted'));
+        }
+        $this->success();
+    }
 
 }

+ 56 - 0
application/api/controller/shop/ShopHotel.php

@@ -2,9 +2,11 @@
 
 namespace app\api\controller\shop;
 
+use app\common\business\ShopWalletBusiness;
 use app\common\controller\Api;
 use app\common\model\BillModel;
 use app\common\model\HotelModel;
+use app\common\model\HotelOrderModel;
 use app\common\model\OfflineShopModel;
 use app\common\model\OfflineTypeModel;
 use app\utils\DataUtil;
@@ -118,6 +120,60 @@ class ShopHotel extends Api
         return $this->success('success',$list);
     }
 
+    // 酒店订单核销
+    public function deal()
+    {
+        $params = $this->request->param();
+        $user_id = $this->auth->id;
+
+        $model = new HotelModel();
+        $info = $model->getDetail(
+            params: ['user_id' => $user_id],
+            select: ['id','invite_id','user_id','name','image','images','back_rate','address']
+        );
+        if (!$info){
+            return $this->error('未开通门店');
+        }
+
+        $params['status_in'] = [1,2,3];
+
+        $model = new BillModel();
+        $order = $model->getDetail(
+            params: array_merge(['shop_id'=>$info['id'],'table_name'=>'hotel_order'],$params),
+            with: [
+                'hotelorder' => function ($query) {
+                    $query->field('id,hotel_id,room_id,name,phone');
+                }
+            ]
+        );
+        if (empty($order)){
+            return $this->error('未找到该订单');
+        }
+
+        if ($params['status'] == 3){
+            return $this->error('订单已核销');
+        }
+
+        if ($params['status'] != 2){
+            return $this->error('订单状态错误');
+        }
+
+        Db::startTrans();
+        // 更改订单状态
+        BillModel::where('id',$order['id'])->update(['status'=>3,'back_status' => 1,'back_time' => time()]);
+        HotelOrderModel::where('id',$order['table_id'])->update(['is_use'=>1,'update_time' => time()]);
+        // 发放商家收益
+        $money = bcsub($order['total_amount'],$order['back_amount'],2);// 除去让利后的金额
+        $wallet = new ShopWalletBusiness('hotel_order');
+        if (!$wallet->change($info['id'],$money,'money',ShopWalletBusiness::log_type[40],'订单核销','bill',$order['id'])){
+            Db::rollback();
+            return $this->error($wallet->getMessage(),$wallet->getData());
+        }
+        Db::commit();
+
+        return $this->success('操作成功');
+    }
+
     // 交易统计
     public function statistics()
     {

+ 128 - 0
application/common/business/ShopWalletBusiness.php

@@ -0,0 +1,128 @@
+<?php
+
+namespace app\common\business;
+
+use app\common\model\BillModel;
+use app\common\model\HotelCanteenOrderModel;
+use app\common\model\HotelOrderModel;
+use app\common\model\OfflineShopOrderModel;
+use app\common\model\UniversityEventApplyModel;
+use app\utils\Common;
+use think\Db;
+
+class ShopWalletBusiness extends BusinessResult
+{
+    // 日志变动类型
+    const log_type = [
+        1  => '系统调节',//money + -
+        40 => '酒店预定',//money + -
+        50 => '餐厅预定',//money + -
+        60 => '老年大学活动报名',//money + -
+        70 => '线下消费',//money + -
+    ];
+
+    /**
+     * 主表订单对应各自店铺表
+     */
+    public const WALLET_TABLE = [
+        'hotel_order'            => 'hotel_wallet',
+        'hotel_canteen_order'    => 'hotel_canteen_wallet',
+        'university_event_apply' => 'university_event_wallet',
+        'offline_shop_order'     => 'offline_shop_wallet',
+    ];
+
+    /**
+     * 主表订单对应各自店铺表
+     */
+    public const LOG_TABLE = [
+        'hotel_wallet'            => 'hotel_money_log',
+        'hotel_canteen_wallet'    => 'hotel_canteen_money_log',
+        'university_event_wallet' => 'university_event_money_log',
+        'offline_shop_wallet'     => 'offline_shop_money_log',
+    ];
+
+    // 操作钱包余额类型
+    const money_type = [
+        'money' => '余额',
+    ];
+
+    protected int    $userId    = 0;
+    protected string $tableName = '';
+
+    public function __construct($tableName) {
+        $this->tableName = $tableName;
+    }
+
+    //获取钱包名称
+    public function getWalletName($name = '')
+    {
+        $conf = self::money_type;
+        if ($name) {
+            return $conf[$name] ?: $name;
+        }
+        return $conf;
+    }
+
+    /**
+     * 余额变更
+     * @param int $shop_id 店铺id
+     * @param float $number 金额(正数进账,负数出账)
+     * @param string $accountType 货币类型 self::money_type
+     * @param int $log_type 日志的类型 self::log_type
+     * @param string $remark 备注
+     * @param string $table 来源表
+     * @param int $table_id 表id
+     * @return bool
+     */
+    public function change(int $shop_id, float $number, string $accountType = 'money', int $log_type = 0, string $remark = '', string $table = '', int $table_id = 0)
+    {
+        //获取小数点
+        $point = 0;
+        if(in_array($accountType,['money','bean'])){
+            $point = 2;
+        }
+        bcscale($point);
+
+        //钱包名称
+        $wallet_name = $this->getWalletName($accountType);
+
+        //检测
+        if ($number == 0 || 0 === bccomp($number, 0)) {
+            return $this->error('交易金额:0');
+        }
+
+        //检测
+        if (!$wallet = Db::name($this->tableName)->lock(true)->where(['shop_id' => $shop_id])->find()) {
+            return $this->error('不存在的用户');
+        }
+
+        if (bccomp(bcadd($wallet[$accountType], $number), 0) === -1) {
+            return $this->error("{$wallet_name}余额不足!");
+        }
+
+        //钱币记录
+        $data = [
+            'shop_id'      => $shop_id,
+            'log_type'     => $log_type,
+            'before'       => $wallet[$accountType],
+            'change_value' => $number,
+            'remain'       => bcadd($wallet[$accountType], $number),
+            'table'        => $table,
+            'table_id'     => $table_id,
+            'remark'       => $remark,
+            'createtime'   => time(),
+            'updatetime'   => time(),
+        ];
+        //新的方式
+        $upWallet = Db::name($this->tableName)->where(['shop_id' => $shop_id])->update([$accountType => $data['remain']]);
+        if ($upWallet === false) {
+            return $this->error("更新账户余额失败!");
+        }
+        $logTableName = self::LOG_TABLE[$this->tableName];
+        if (!Db::name($logTableName)->insertGetId($data)) {
+            return $this->error("更新财务记录失败!");
+        }
+
+        return $this->success("账户余额已更新!");
+    }
+}