public function getAdDeactive() { // Get all ad actived $arr_ids = array(); $records = AdActive::where('status', '=', 1)->take(self::$limit_crawl_ad_active)->get(); if ($records->count()) { $now = Carbon::now(); foreach ($records as $record) { $created = new Carbon($record->created_at); if ($created->diff($now)->days >= $record->day) { $arr_ids[] = $record->ad_id; } } if (count($arr_ids)) { DB::beginTransaction(); try { Ad::whereIn('id', $arr_ids)->update(array('status' => 0)); AdActive::whereIn('ad_id', $arr_ids)->update(array('status' => 0)); } catch (Exception $ex) { DB::rollback(); } DB::commit(); } } return Response::json(array('status' => 'success', 'data' => $arr_ids), 200); }
public function putActiveAd($ad_id = 0) { try { $data = post(); $default = array('ad_id' => '', 'day' => ''); $merge = array_merge($default, $data); $merge = \DLNLab\Classified\Classes\HelperClassified::trim_value($merge); extract($merge); if (intval($ad_id) || intval($day)) { return Response::json(array('status' => 'error', 'message' => trans(CLF_LANG_MESSAGE . 'not_valid')), 500); } // Get user_id $user = Auth::getUser(); $user_id = $user->id; // Check user money $user_money = Money::get_user_charge_money($user_id); $money = AdActive::calc_money($day); if ($user_money - $money < 0) { // No active ad when user not enough money return Response::json(array('status' => 'error', 'message' => trans(CLF_LANG_MESSAGE . 'user_not_enough_money')), 500); } // Get Ad $ad = Ad::whereRaw('id = ? AND user_id = ? AND status != ?', array($id, $user_id, 0))->first(); if (empty($ad)) { return Response::json(array('status' => 'error', 'message' => trans(CLF_LANG_MESSAGE . 'user_not_perm')), 500); } DB::beginTransaction(); try { // Minus money $o_money = Money::minus_money_user($user_id, $money); // Active ad $ad->status = 1; $ad->published_at = time(); $ad->save(); // Update add has activated to DB $now = \Carbon\Carbon::now(); $record = new self(); $record->ad_id = $ad_id; $record->money = $money; $record->start_date = $now->toDateTimeString(); $record->end_date = $now->addDays($day)->toDateTimeString(); $record->status = 1; $record->save(); $user->money_spent = $user->money_spent + $money; $user->save(); } catch (Exception $ex) { DB::rollback(); return $ex->getMessage(); } DB::commit(); return true; $message = AdActive::active_ad($data, null, $user_id); $message = $message ? $message : 'fail'; } catch (Exception $ex) { $code = 403; $message = $ex->getMessage(); } return Response::json(response_message($code, $message), $code); }