db->beginTransaction(); try { $user_id = $order_detail->user_id; $mall_id = $order_detail->mall_id; $order_detail['num'] = (int)$order_detail['num']; //创建会员买货订单 $params = [ 'mall_id' => $mall_id, 'user_id' => $user_id, 'after_distribution_price' => $this->getAfterPrice($order_detail,$commission), 'order_id' => $order_detail['order_id'], 'order_detail_id' => $order_detail['id'], 'goods_id' => $order_detail['goods_id'], 'num' => $order_detail['num'], 'payment_type' => $this->payment_type, ]; $buying_order = CloudStockBuyingOrder::setData($params); if ($buying_order == false) throw new \Exception(CloudStockBuyingOrder::getStaticError()); //库存变化记录 $params = [ 'mall_id' => $mall_id, 'goods_id' => $order_detail['goods_id'], 'user_id' => $order_detail['user_id'], 'changes_dir' => CloudStockEnum::STOCK_CHANGE_SUB, 'order_id' => $buying_order['id'], 'num' => $order_detail['num'], 'order_type' => CloudStockEnum::STOCK_ORDER_TYPE_USER, 'changes_type' => CloudStockEnum::STOCK_CHANGE_TYPE200, ]; $stock_log = CloudStockGoodsStockLog::setData($mall_id,$params); if (!$stock_log) throw new \Exception(CloudStockGoodsStockLog::getStaticError()); $user = User::findOne(['id' => $user_id]); $agent = CloudStockAgent::findOne(['mall_id' => $mall_id, 'user_id' => $user_id, 'status' => StatusEnum::ENABLED]); if (empty($user)) throw new \Exception(CloudStockAgent::getStaticError()); $configs = \Yii::$app->services->setting->addons->get($mall_id, AddonsEnum::ADDONS_NAME_CLOUD_STOCK, 'basic'); //升级礼包升级商品扣货逻辑 if(!empty($configs['upgrade_bag_product_deduction']) && in_array($order_detail['goods_id'],$this->upgrade_bag_goods_id_arr)){ //扣除关系链中大于等于指定等级的代理库存 $upgrade_bag_product_deduction_level = $configs['upgrade_bag_product_deduction_level']; $user_parent_agent = CloudStockAgent::getUserParentAgent($user_id, $upgrade_bag_product_deduction_level, 1); }else{ //扣除关系链中大于自身等级的代理库存 $user_parent_agent = CloudStockAgent::getUserParentAgent($user_id, $agent['level'] ?? 0); } //库存递归扣减逻辑 $obj = new BuyDeductionLogic($user, $user_parent_agent, $stock_log, $buying_order, $buying_order->num); $obj->order_type = 'buying'; $main_order = Order::findOne(['id' => $order_detail['order_id']]); $obj->order_no = $main_order['order_no']; $obj->store_id = $main_order['store_id']; $obj->user_agent_level = 0; if (!$obj->deduct()) throw new \Exception('库存递归扣减逻辑异常'); !empty($obj->fill_insert_wallet) && ($this->fill_insert_wallet = $obj->fill_insert_wallet); !empty($obj->payment_insert_wallet) && ($this->payment_insert_wallet = $obj->payment_insert_wallet); $t->commit(); if (!empty($obj->event_arr)) { foreach ($obj->event_arr as $event_data) { $event = new CloudStockChangeEvent($event_data['mall_id']); \Yii::$app->services->events->dispatch($event, $event_data, $event->listeners); } } return true; } catch (\Exception $e) { $t->rollBack(); $exception = FormatHelper::exception($e, $e->getMessage()); echo $e->getMessage() . $e->getFile() . $e->getLine(); \Yii::error($exception); } } protected function getAfterPrice($order_detail,$commission){ $configs = \Yii::$app->services->setting->addons->get($order_detail['mall_id'], AddonsEnum::ADDONS_NAME_CLOUD_STOCK, 'basic'); // $res = $order_detail['goods_price']; // if(!empty($is_deduction_commission))$res = $order_detail['goods_price']-$commission; if ($configs['is_buy_type'] == 1 && $configs['buy_type'] == 2) { //启用会员买货扣除库存方式并且选择成本价时 $res = $order_detail['cost_price']-$commission; } else { $res = $order_detail['goods_price']-$commission; } if($res<0) return 0; return $res; } }