function bonus($bonus_config) { //$fid,$uid,$aid static $show_bonus = false; $bonus = ActivityBonus::where("fid", $bonus_config['fid'])->where('uid', $bonus_config['uid'])->where('activity_id', 3)->where('status', 0)->get(); if ($bonus->count() > 0 && $show_bonus == false) { $select_html = "红包:<select name=\"bonus_id\"><option value=\"0\">无</option>"; foreach ($bonus as $bonus_item) { $select_html .= "<option value=\"" . $bonus_item->getKey() . "\">" . $bonus_item->bonus . "</option>"; } $select_html .= "</select>"; $show_bonus = true; return $select_html; } return ''; }
public function saveScore(Request $request) { $stores_ids = $this->user->stores->pluck('id'); $this->_brands = Brand::join('store_brand as s', 's.bid', '=', 'brands.id')->whereIn('s.sid', $stores_ids)->get(['brands.*']); $fids = Product::whereIn('bid', $this->_brands->pluck('id'))->pluck('fid'); $key = 'bonus_' . $this->user->getKey() . '_game'; $times = intval(Cache::get($key, 0)); $save_code_key = 'save_put_code_' . $this->user->getKey(); $ver_code = $request->get('ver_code'); if (session($save_code_key) == $ver_code && !empty($fids) && $times > 0) { $fids = array_unique((array) $fids); //把关注店铺,相关的厂商都加红包 $score = $request->get('score'); //分数 if ($score < 1) { $data = ['err_msg' => '红包金额不能为空.']; return $this->failure(NULL, false, $data, true); } $type_id = $request->get('type_id'); //活动id foreach ($fids as $fid) { $activity_bouns = new ActivityBonus(); $activity_bouns->uid = $this->user->getKey(); $activity_bouns->fid = $fid; $activity_bouns->activity_id = with(Activity::where('fid', $fid)->where('type_id', $type_id)->first())->id; $activity_bouns->bonus = intval($score) > 98 ? 98 : intval($score); $activity_bouns->status = 0; $activity_bouns->save(); } Cache::decrement($key); session([$save_code_key => '']); //红包的个数 $bonus_cnt = ActivityBonus::where('uid', $this->user->getKey())->where('status', 0)->count(); $data = ['times' => --$times, 'bonus_cnt' => $bonus_cnt]; return $this->success(NULL, false, $data); } else { $data = ['err_msg' => '暂时没添加相关活动。']; return $this->failure(NULL, false, $data, true); } }
public function pay($total_fee, $transaction = TRUE) { $transaction && DB::beginTransaction(); $order = static::where($this->getKeyName(), $this->getKey())->lockForUpdate()->first(); if ($order->status != static::INIT) { $transaction && DB::rollback(); return false; } //对账失败 if (abs($total_fee - $order->total_money * 100) >= 0.01) { $order->status = static::COMPARE_BILL_FAIL; $order->save(); $transaction && DB::commit(); return false; } //用户已消费 $order->bills()->create(['value' => -$order->total_money, 'uid' => $order->uid, 'event' => Bill::PURCHASE]); //一个订单里面只能有一个厂家的details $details = $order->details; $total = $details->sum('money'); $factory_money = $order->total_money; $store_money = $agent_money = []; $user_stores = User::find($order->uid)->stores()->get(['stores.id']); //关注的店铺 foreach ($details as $detail) { $rate = $order->details_money * $detail->money / $total; $brand = $detail->product->brand; $store = Store::whereIn('stores.id', $user_stores->pluck('id')->toArray())->join('store_brand', 'store_brand.sid', '=', 'stores.id')->where('store_brand.bid', $brand->getKey())->first(['stores.*']); if (!empty($store)) { !isset($store_money[$store->getKey()]) && ($store_money[$store->getKey()] = 0); $store_money[$store->getKey()] += $value = $rate * $detail->product->shop_rate / 100; $factory_money -= $value; $agent = Agent::whereIn('agents.id', $store->agent_ids()->toArray())->join('agent_brand', 'agent_brand.aid', '=', 'agents.id')->where('agent_brand.bid', $brand->getKey())->first(['agents.*']); if (!empty($agent)) { !isset($agent_money[$agent->getKey()]) && ($agent_money[$agent->getKey()] = 0); $agent_money[$agent->getKey()] += $value = $rate * $detail->product->agent_rate / 100; $factory_money -= $value; } } } if (!empty($details)) { //门店提成 foreach ($store_money as $uid => $value) { $order->bills()->create(['value' => $value, 'uid' => $uid, 'event' => Bill::COMMISSION]); } //代理商提成 foreach ($agent_money as $uid => $value) { $order->bills()->create(['value' => $value, 'uid' => $uid, 'event' => Bill::COMMISSION]); } //厂商收入 $order->bills()->create(['value' => $factory_money < 0 ? 0 : $factory_money, 'uid' => $details[0]->product->fid, 'event' => Bill::INCOME]); } $order->status = static::PAID; $order->save(); if (intval($order->bonus_id) > 0) { $bonus = ActivityBonus::where('id', $order->bonus_id)->lockForUpdate()->first(); $bonus->status = 2; //已使用红包 $bonus->save(); } $transaction && DB::commit(); return true; }