/** * Get the new price after applying a group of coupon. * @author Anyun * @param int $sale_id * @param array $promo_codes * @return object [newPrice,promoCode] */ public static function applyCoupon($sale_id, $promo_codes = []) { $app = \Slim\Slim::getInstance(); $sale = Price::find($sale_id); if (!$sale) { $app->halt(400, 'Invalid Sale Item'); } $result = array("price" => $sale->price, "coupon" => ""); if (!$sale) { // this sale not exist $app->halt('400', $sale_id); } //Important Rule: //Only ONE code will be applied to one item //If multiply codes effect, chose the lowest one $prices = Coupon::where('course_sale_id', '=', $sale_id)->whereIn('code', $promo_codes)->valid()->get(); foreach ($prices as $key => $value) { if ($value->new_price < $result['price']) { $result['old_price'] = $sale->price; $result['price'] = $value->new_price; $result['coupon'] = $value->code; } } return (object) $result; }
/** * @api {post} /managers/:idUser/assign Assign Content by Manager * @apiName Assign Content by Manager * @apiGroup Manager * @apiHeader (Header) {String} X_Authorization Authorization value. * @apiParam (url Parameter) {Number} idUser User unique ID. * @apiParam {Number} idBin Bin's unique ID. * @apiParam {Number} idUser User's unique ID. The change will apply to this user. * * @apiError 400 Input Invalid. This will happen if the param is missing or not the valid format. * @apiError 404 Not found. This will happen if the bin id/user id/course id/sale id is not in our system. * @apiError 401 Not authorized. This will happen if the header value is not attached. * @apiError 403 The user is not a manager yet. * @apiError 409 User already enrolled. * @apiErrorExample {json} Error-Response: * HTTP/1.1 412 Precondition Failed. User doesn't complete the prerequisite course. * [ * { * "id": 45951, * "code": "Test", * "name": "TurboChef: High h conveyor C", * "manufacturer": "TurboChef", * "shortDescription": null, * "description": null, * "note": null, * "isPublished": 1, * "time": 90, * "type": "course", * "safety": 1, * "html5": 1, * "video": null, * "thumbnail": "http://localhost:9090/ignitor-api/courses/thumbnail/45951" * } * ] * * */ public static function assignContent($idUser) { $app = \Slim\Slim::getInstance(); $request = $app->request->post(); $validata = $app->validata; $validator = $validata::key('idBin', $validata::digit()->notEmpty())->key('idUser', $validata::digit()->notEmpty()); if (!$validator->validate($request)) { $app->halt("400", json_encode("Input Invalid")); } if (!GroupController::isMemberOfAdmin($request['idUser'], $idUser)) { $app->halt("403", json_encode("Permission denied.")); } if ($idUser != $request['idUser'] && GroupController::isManagerOfAdmin($request['idUser'], $idUser)) { $app->halt("403", json_encode("Permission denied. You can only transfer content to manager user.")); } $bin_id = $request['idBin']; $bin = Manager_Bin::where('id', '=', $bin_id)->lockForUpdate()->first(); if (!$bin) { $app->halt("404", json_encode("manager content record does not exist")); } if ($bin->user_id != $idUser) { $app->halt("401"); } if ($bin->quantity < 1) { $app->halt("404", json_encode("No available seat found.")); } $sale = Price::find($bin->course_sale_id); if (!$sale) { $app->halt("404", json_encode("sale record does not exist")); } $isEnroll = EnrollmentController::isEnroll($sale->course_id, $request['idUser']); if ($isEnroll) { $app->halt("409", json_encode("This User already has this content.")); } EnrollmentController::meetPrerequisite($sale->course_id, $request['idUser']); $enrollment = EnrollmentController::enroll($sale->course_id, $request['idUser'], $bin->expiration_dt); if ($enrollment) { $bin->seats()->attach($enrollment, array('sender_id' => $idUser, 'receiver_id' => $request['idUser'], 'course_sale_id' => $bin->course_sale_id)); $bin = Manager_Bin::where('id', '=', $bin_id)->lockForUpdate()->first(); if ($bin->quantity < 1) { $app->halt("404", json_encode("No available seat found.")); } $bin->quantity = $bin->quantity - 1; $bin->save(); } }
/** * Show the form for editing the specified resource. * * @param int $id * @return Response */ public function edit($id) { $data = Price::find($id); return View::make('admin.price.edit', array('data' => $data)); }
public static function afterPurchaseEnroll($items, $idUser) { $isAdmin = GroupController::adminCheck($idUser); foreach ($items as $key => $item) { $sale = Price::find($item->id); if (is_null($sale->length)) { $end_at = Null; } else { $date = strtotime("+" . $sale->length . " day"); $end_at = date('Y-m-d H:i:s', $date); } if (!$isAdmin) { self::enroll($item->course_id, $idUser, $end_at); } else { ManagerController::addToBin($item->id, $item->qty, $idUser, $end_at); } } }