diff --git a/app/api/logic/order/OrderLogic.php b/app/api/logic/order/OrderLogic.php index 411272a01..c875b5ab0 100644 --- a/app/api/logic/order/OrderLogic.php +++ b/app/api/logic/order/OrderLogic.php @@ -448,7 +448,6 @@ class OrderLogic extends BaseLogic return true; } catch (\Exception $e) { Db::rollback(); - d($e); self::setError($e->getMessage()); return false; } diff --git a/app/common/logic/CommissionLogic.php b/app/common/logic/CommissionLogic.php new file mode 100644 index 000000000..f9a438e59 --- /dev/null +++ b/app/common/logic/CommissionLogic.php @@ -0,0 +1,195 @@ + 0) { + $address = UserAddress::where(['uid' => $order['uid'], 'is_default' => 1])->find(); + if ($address) { + $arr1 = UserAddress::where(['village' => $address['village'], 'is_default' => 1])->column('uid'); + if ($arr1) { + $uid = User::where('id', 'in', $arr1)->where('user_ship', 2)->value('id'); + if ($uid) { + $village_uid = $uid; + } + } + $arr2 = UserAddress::where(['village' => $address['village'], 'brigade' => $address['brigade'], 'is_default' => 1])->column('uid'); + if ($arr2) { + $uid = User::where('id', 'in', $arr1)->where('user_ship', 3)->value('id'); + if ($uid) { + $brigade_uid = $uid; + } + } + } + } + self::user($order, 0.03, $transaction_id,$village_uid,12);//会员 + self::user($order, 0.01, $transaction_id,$brigade_uid,15);//队长 + self::user($order, 0.01, $transaction_id,0,16);//其他 + } + /** + * 走队长分润 + */ + public static function setBrigade($order, $transaction_id) + { + self::user($order, 0.05, $transaction_id,$order['uid'],14); + $village_uid=0; + $brigade_uid=0; + if ($order['uid'] > 0) { + $address = UserAddress::where(['uid' => $order['uid'], 'is_default' => 1])->find(); + if ($address) { + $arr1 = UserAddress::where(['village' => $address['village'], 'is_default' => 1])->column('uid'); + if ($arr1) { + $uid = User::where('id', 'in', $arr1)->where('user_ship', 2)->value('id'); + if ($uid) { + $village_uid = $uid; + } + } + $arr2 = UserAddress::where(['village' => $address['village'], 'brigade' => $address['brigade'], 'is_default' => 1])->column('uid'); + if ($arr2) { + $uid = User::where('id', 'in', $arr1)->where('user_ship', 3)->value('id'); + if ($uid) { + $brigade_uid = $uid; + } + } + } + } + self::user($order, 0.03, $transaction_id,$village_uid,12);//会员 + self::user($order, 0.01, $transaction_id,$brigade_uid,15);//队长 + self::user($order, 0.01, $transaction_id,0,16);//其他 + } + /** + * 走厨师分润 + */ + public static function setCook($order, $transaction_id) + { + self::user($order, 0.07, $transaction_id,$order['uid'],12);//改到带返还金中 + $village_uid=0; + $brigade_uid=0; + if ($order['uid'] > 0) { + $address = UserAddress::where(['uid' => $order['uid'], 'is_default' => 1])->find(); + if ($address) { + $arr1 = UserAddress::where(['village' => $address['village'], 'is_default' => 1])->column('uid'); + if ($arr1) { + $uid = User::where('id', 'in', $arr1)->where('user_ship', 2)->value('id'); + if ($uid) { + $village_uid = $uid; + } + } + $arr2 = UserAddress::where(['village' => $address['village'], 'brigade' => $address['brigade'], 'is_default' => 1])->column('uid'); + if ($arr2) { + $uid = User::where('id', 'in', $arr1)->where('user_ship', 3)->value('id'); + if ($uid) { + $brigade_uid = $uid; + } + } + } + } + self::user($order, 0.01, $transaction_id,$village_uid,14);//村长 + self::user($order, 0.01, $transaction_id,$brigade_uid,15);//队长 + self::user($order, 0.01, $transaction_id,0,16);//其他 + } + + /** + * 走线下分润 + */ + public static function setStore($order, $transaction_id) + { + self::platform($order, 0.05, $transaction_id);//平台 + self::store($order, 0.02, $transaction_id,0);//门店 + self::user($order, 0.01, $transaction_id,0,16);//其他 + + } + + + /** + * 平台分润 + */ + public static function platform($order, $platformRate, $transaction_id) + { + $financeLogic = new StoreFinanceFlowLogic(); + $financeLogic->order=$order; + $fees = bcdiv(bcmul($order['pay_price'], $platformRate, 2), 1, 2); + if ($fees > 0) { + $financeLogic->in($transaction_id, $fees, OrderEnum::ORDER_HANDLING_FEES, $order['store_id'], 0, 0, $order['pay_type']); //平台手续费 + $financeLogic->out($transaction_id, $fees, OrderEnum::ORDER_HANDLING_FEES, $order['store_id'], $order['staff_id'], 0, $order['pay_type']); //商户平台手续费支出 + } + } + /** + * 门店分润 + */ + public static function store($order, $platformRate, $transaction_id,$uid) + { + $financeLogic = new StoreFinanceFlowLogic(); + $financeLogic->user['uid']=$order['uid']; + $financeLogic->other_arr['vip_uid']=$uid; + $financeLogic->order=$order; + $financeLogic->in($transaction_id, $order['pay_price'], OrderEnum::USER_ORDER_PAY, $order['store_id'], $order['staff_id'], 0, $order['pay_type']); //用户订单支付 + + //缴纳齐全了就加商户没有就加到平台 + $money_limt = SystemStore::where('id', $order['store_id'])->field('paid_deposit,security_deposit')->find(); + $deposit = bcsub($money_limt['security_deposit'], $money_limt['paid_deposit'], 2); //保证金剩余额度 + $store_profit = bcdiv(bcmul($order['pay_price'], $platformRate, 2), 1, 2); + if ($deposit > 0) { + if ($deposit > $store_profit) { + if ($store_profit > 0) { + SystemStore::where('id', $order['store_id'])->inc('paid_deposit', $store_profit)->update(); + $financeLogic->out($transaction_id, $store_profit, OrderEnum::ORDER_MARGIN, $order['store_id'], $order['staff_id'], 0, $order['pay_type']); + $financeLogic->in($transaction_id, 0, OrderEnum::MERCHANT_ORDER_OBTAINS, $order['store_id'], 0, 0, $order['pay_type']); //平台手续费 + } + } else { + $money = bcsub($store_profit, $deposit, 2); + if ($deposit > 0) { + SystemStore::where('id', $order['store_id'])->inc('paid_deposit', $deposit)->update(); + $financeLogic->out($transaction_id, $deposit, OrderEnum::ORDER_MARGIN, $order['store_id'], $order['staff_id'], 0, $order['pay_type']); + } + if ($money) { + SystemStore::where('id', $order['store_id'])->inc('store_money', $money)->update(); + $financeLogic->in($transaction_id, $money, OrderEnum::MERCHANT_ORDER_OBTAINS, $order['store_id'], 0, 0, $order['pay_type']); //平台手续费 + } + } + } else { + if ($store_profit > 0) { + SystemStore::where('id', $order['store_id'])->inc('store_money', $store_profit)->update(); + $financeLogic->in($transaction_id, $store_profit, OrderEnum::MERCHANT_ORDER_OBTAINS, $order['store_id'], 0, 0, $order['pay_type']); //平台手续费 + } + } + } + + /** + * 分给用户 + */ + public static function user($order, $userRate, $transaction_id,$uid=0,$enum = 0) + { + $financeLogic = new StoreFinanceFlowLogic(); + $fees = bcmul($order['pay_price'], $userRate, 2); + if ($fees > 0) { + //记录用户余额收入 + if ($uid) { + $GiveUser = User::where('id', $order['uid'])->find(); + $capitalFlowDao = new CapitalFlowLogic($GiveUser); + $capitalFlowDao->userIncome('system_balance_add', 'order', $order['id'], $fees); + } + $financeLogic->user['uid']=$order['uid']; + $financeLogic->other_arr['vip_uid']=$uid; + $financeLogic->order=$order; + $financeLogic->in($transaction_id, $fees, $enum, $order['store_id'], 0, 0, $order['pay_type']); + $financeLogic->out($transaction_id, $fees, $enum, $order['store_id'], $order['staff_id'], 0, $order['pay_type']); + + + } + } +} diff --git a/app/common/logic/StoreFinanceFlowLogic.php b/app/common/logic/StoreFinanceFlowLogic.php index ac7d66070..fab958091 100644 --- a/app/common/logic/StoreFinanceFlowLogic.php +++ b/app/common/logic/StoreFinanceFlowLogic.php @@ -53,7 +53,7 @@ class StoreFinanceFlowLogic extends BaseLogic 'order_id' => $this->order['id'], 'transaction_id' => $transaction_id, 'order_sn' => $this->order['order_id'], - 'user_id' => $this->user['uid'], + 'user_id' => $this->user['uid']??0, 'other_uid' => $this->other_arr['vip_uid']??0, 'financial_type' => $financialType, 'financial_pm' => $pm, diff --git a/app/common/logic/store_order/StoreOrderLogic.php b/app/common/logic/store_order/StoreOrderLogic.php index da9cf0212..6c8d94725 100644 --- a/app/common/logic/store_order/StoreOrderLogic.php +++ b/app/common/logic/store_order/StoreOrderLogic.php @@ -264,6 +264,7 @@ class StoreOrderLogic extends BaseLogic $query->field(['id', 'oid', 'product_id', 'cart_info']); }])->where($params)->whereIn('shipping_type',OrderEnum::ONLINE) ->page($pageNo, $pageSize) + ->order('id desc') ->select()->toArray(); foreach ($order as &$value){ diff --git a/app/store/controller/store_order/StoreOrderController.php b/app/store/controller/store_order/StoreOrderController.php index e7e587ccd..79b74cfa0 100644 --- a/app/store/controller/store_order/StoreOrderController.php +++ b/app/store/controller/store_order/StoreOrderController.php @@ -18,6 +18,7 @@ use app\common\logic\store_order\StoreOrderLogic; use app\common\model\store_branch_product\StoreBranchProduct; use app\common\model\store_order\StoreOrder; use app\common\model\store_order_cart_info\StoreOrderCartInfo; +use app\common\model\store_product\StoreProduct; use app\common\model\store_product_unit\StoreProductUnit; use app\common\model\system_store\SystemStore; use app\common\model\system_store\SystemStoreStaff; @@ -437,7 +438,7 @@ class StoreOrderController extends BaseAdminController $find['nickname'] = $user['nickname'] ?? ''; $find['user_mobile'] = $user['mobile'] ?? ''; $find['info'] = StoreOrderCartInfo::where('oid', $find['id'])->field('store_id,product_id,cart_num,cart_info')->select()->each(function ($item) { - $goods = StoreBranchProduct::where(['store_id' => $item['store_id'], 'product_id' => $item['product_id']])->field('store_name,unit')->find(); + $goods = StoreProduct::where(['id' => $item['product_id']])->field('store_name,unit')->find(); $item['unit_name'] = StoreProductUnit::where('id', $goods['unit'])->value('name'); $item['store_name'] = $goods['store_name']; $item['total_price'] = $item['cart_info']['total_price'];