/**
* Creating new order
* User have to give restaurant_id and it should exist in table restaurants
* Other attributes is optional, gives status value of Order::STATUS_CREATED
*
* @throws \yii\db\Exception
*/
public function actionCreate()
{
$role = UserRoleDetector::getUserRole();
Yii::$app->response->format = Response::FORMAT_JSON;
if ($role != 3 && $role != 4) {
echo json_encode(array('error_code' => Codes::$UNAUTHORIZED, 'errors' => StatusCodeMessage::$UNAUTHORIZED), JSON_PRETTY_PRINT);
} else {
$params = $_REQUEST;
//checking restaurant_id
if (!isset($params['restaurant_id'])) {
echo json_encode(array('status_code' => CODES::$BAD_REQUEST, 'message' => StatusCodeMessage::$BAD_REQUEST), JSON_PRETTY_PRINT);
exit;
} else {
$restaurant = Restaurant::findOne(['restaurant_id' => $params['restaurant_id']]);
if ($restaurant == null) {
echo json_encode(array('status_code' => CODES::$NOT_FOUND, 'message' => StatusCodeMessage::$NOT_FOUND), JSON_PRETTY_PRINT);
exit;
}
}
$query = new Query();
$result = $query->createCommand()->insert('orders', ["user_id" => isset($params['user_id']) ? User::findOne(['user_id' => $params['user_id']]) != null ? $params['user_id'] : null : null, "user_info" => isset($params['user_info']) ? $params['user_info'] : null, "restaurant_id" => $params['restaurant_id'], "order_info" => isset($params['order_info']) ? $params['order_info'] : '', "status" => Order::STATUS_CREATED, "other_users" => ''])->execute();
if ($result == null) {
echo json_encode(array('status_code' => Codes::$INERNAL, 'message' => StatusCodeMessage::$INERNAL), JSON_PRETTY_PRINT);
exit;
} else {
$model = $query->from('orders')->select(['order_id', 'user_id', 'user_info', 'restaurant_id', 'order_info'])->where(['order_id' => $query->max('order_id')])->createCommand()->queryAll();
echo json_encode(array('status_code' => Codes::$CREATED, 'message' => StatusCodeMessage::$CREATED, 'data' => $model), JSON_PRETTY_PRINT);
}
}
}