/** * Create new account. * * @param CreateAccountRequest $request * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response */ public function register(CreateAccountRequest $request) { $response = new AjaxResponse(); $roles = new Roles(); // Build user data array $data = ['email' => $request->get('email'), 'password' => bcrypt($request->get('password')), 'role_id' => $roles->getUserRoleId()]; // Insert user $user = User::create($data); // User settings UserSetting::insert(['user_id' => $user->id, 'language_id' => Settings::defaultLanguageId()]); // Create trial period UserTrialPeriod::create(['user_id' => $user->id, 'trial_period_id' => TrialPeriod::where('validity_days', 90)->first()->id]); Auth::login($user); $response->setSuccessMessage(trans('register.account_created')); return response($response->get()); }
/** * Initialize required stuff. */ public function __construct() { if (Auth::check()) { // Check if admin center should be displayed $roles = new Roles(); $showAdminCenter = false; if ($roles->getAdminRoleId() === Auth::user()->role_id || $roles->getModeratorRoleId() === Auth::user()->role_id) { $showAdminCenter = true; } View::share(['showAdminCenter' => $showAdminCenter, 'validSubscription' => UserHelper::validSubscription()]); // Set language App::setLocale(Settings::language()); // Check if user subscription is expired if (UserHelper::subscriptionLeftDays(Auth::user()->id) < 0) { UserTrialPeriod::where('user_id', Auth::user()->id)->update(['expired' => true]); } } }
/** * Paginate searched products. * * @param string $searchTerm * @param int $page * @return LengthAwarePaginator */ protected static function paginateSearchedProducts($searchTerm, $page) { // Query for results $results = \DB::table('application_products')->where('code', 'like', "{$searchTerm}%")->orWhere('name', 'like', "{$searchTerm}%")->orderBy('created_at', 'desc')->get(); // Make sure page is always positive if ($page < 1) { $page = 1; } $perPage = Settings::displayedBills(); // Calculate start from $startFrom = $perPage * ($page - 1); $sliced = array_slice($results, $startFrom, $perPage); $paginate = new LengthAwarePaginator($sliced, count($results), $perPage); if (isset($searchTerm) && strlen($searchTerm) > 0) { $paginate->setPath('/admin-center/products-manager/get/search'); $paginate->appends(['term' => $searchTerm]); } else { $paginate->setPath('/admin-center/products-manager/get'); } return $paginate; }
/** * Perform query and return pagination results. * * @param string $searchTerm * @param int $page * @return LengthAwarePaginator */ protected static function paginate($searchTerm, $page) { // Query database to get results $results = DB::table('products')->where('code', 'like', "{$searchTerm}%")->orWhere('name', 'like', "{$searchTerm}%")->orderBy('code', 'asc')->get(); // Make sure page is always positive self::validatePage($searchTerm); $perPage = Settings::displayedBills(); // Calculate start from $startFrom = self::getStartFrom($page, $perPage); // Slice the results $sliced = self::getSlicedResults($results, $startFrom, $perPage); // Initialize the paginator $paginate = new LengthAwarePaginator($sliced, count($results), $perPage); // Check if search term should be appended to the search path if (isset($searchTerm) && strlen($searchTerm) > 0) { $paginate->setPath('/my-products/get/search'); $paginate->appends(['term' => $searchTerm]); } else { $paginate->setPath('/my-products/get'); } return $paginate; }
/** * Paginate bills of given client. * * @param int $clientId * @param int $page * @param int $paid * @return LengthAwarePaginator */ public static function paginateBills($clientId, $page = 1, $paid = 0) { $bills = self::lastBills($clientId, 'all', $paid); // Make sure page is positive if ($page < 1) { $page = 1; } $perPage = Settings::displayedBills(); // Calculate start from $start = $perPage * ($page - 1); // Slice results $sliced = array_slice($bills, $start, $perPage); // Build paginator $paginate = new LengthAwarePaginator($sliced, count($bills), $perPage); // Set path in function of wanted bills $path = '/clients/' . $clientId . '/bills/'; if ($paid) { $path .= 'paid/get'; } else { $path .= 'unpaid/get'; } $paginate->setPath($path); return $paginate; }
protected static function performBillsPaginationQuery($config, $questionMarks, $billIds) { // Build query $query = "SELECT SUM(bills.final_price) AS final_price, SUM(bills.quantity) AS number_of_products, bills.id, bills.client_name, bills.campaign_order, bills.campaign_year, bills.campaign_number, bills.payment_term, bills.created_at FROM "; $query .= "(SELECT bill_products.final_price AS final_price, bill_products.quantity, bills.id, clients.name AS client_name, bills.campaign_order, "; $query .= "campaigns.year AS campaign_year, campaigns.number AS campaign_number, bills.payment_term, bills.created_at "; $query .= "FROM bills LEFT JOIN bill_products ON bill_products.bill_id = bills.id "; // Join campaigns $query .= "LEFT JOIN campaigns ON bills.campaign_id = campaigns.id "; // Join clients table $query .= "LEFT JOIN clients ON clients.id = bills.client_id WHERE bills.id IN ({$questionMarks}) "; $query .= "UNION ALL SELECT bill_application_products.final_price as final_price, bill_application_products.quantity as quantity, bills.id, "; $query .= "clients.name as client_name, bills.campaign_order, campaigns.year as campaign_year, campaigns.number as campaign_number, bills.payment_term, bills.created_at FROM bills "; // Join bill application products table $query .= "LEFT JOIN bill_application_products ON bill_application_products.bill_id = bills.id "; // Join campaigns table $query .= "LEFT JOIN campaigns ON campaigns.id = bills.campaign_id "; // Join clients table $query .= "LEFT JOIN clients ON clients.id = bills.client_id "; $query .= "WHERE bills.id IN ({$questionMarks}) AND clients.name=bills.id) bills "; $query .= "GROUP BY bills.id ORDER BY bills.created_at DESC"; // Execute query $results = DB::select($query, $billIds); // Make sure page is always positive if ($config['page'] < 1) { $config['page'] = 1; } $perPage = Settings::displayedBills(); // Calculate start from $startFrom = $perPage * ($config['page'] - 1); $sliced = array_slice($results, $startFrom, $perPage); $paginate = new LengthAwarePaginator($sliced, count($results), $perPage); if (isset($config['searchTerm']) && strlen($config['searchTerm']) > 0) { $paginate->setPath('/bills/get/search'); $paginate->appends(['term' => $config['searchTerm']]); } else { $paginate->setPath('/bills/get'); } return $paginate; }
/** * Paginate clients. * * @return mixed */ public function getClients() { return Client::select(DB::raw('clients.*, COUNT(bills.id) as number_of_orders'))->leftJoin('bills', 'bills.client_id', '=', 'clients.id')->where('clients.user_id', Auth::user()->id)->orderBy('clients.created_at', 'desc')->groupBy('clients.id')->paginate(Settings::displayedClients()); }
/** * Paginate products in json * * @param Request $request * @return mixed */ public function getProducts(Request $request) { return ApplicationProduct::orderBy('code', 'asc')->paginate(Settings::displayedProducts()); }
/** * Allow admin to create new user. * * @param CreateNewUserRequest $request * @return mixed */ public function createNewUser(CreateNewUserRequest $request) { $roles = new Roles(); $user = User::create(['email' => $request->get('new_user_email'), 'password' => bcrypt($request->get('new_user_password')), 'special_user' => (bool) $request->get('make_special_user'), 'role_id' => $roles->getUserRoleId()]); // Generate user settings UserSetting::insert(['user_id' => $user->id, 'language_id' => Settings::defaultLanguageId()]); $response = new AjaxResponse(); $response->setSuccessMessage(trans('users_manager.user_created_successfully')); return response($response->get())->header('Content-Type', 'application/json'); }
/** * Reset user settings to default. * * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response */ public function resetToDefaultValues() { $response = new AjaxResponse(); $defaultSettings = UserDefaultSetting::first(); Auth::user()->settings()->update(['displayed_bills' => $defaultSettings->displayed_bills, 'displayed_clients' => $defaultSettings->displayed_clients, 'displayed_products' => $defaultSettings->displayed_products, 'displayed_custom_products' => $defaultSettings->displayed_custom_products]); $response->setSuccessMessage(trans('settings.restored_to_default_settings')); $response->addExtraFields(Settings::all()); return response($response->get()); }
/** * Paginate products * * @return mixed */ public function getProducts() { return Product::where('user_id', Auth::user()->id)->orderBy('code', 'asc')->paginate(Settings::displayedCustomProducts()); }