Ejemplo n.º 1
0
 /**
  * @param array $errors
  * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  */
 public function response(array $errors)
 {
     $response = new AjaxResponse();
     $response->setFailMessage('error');
     $response->addExtraFields(['errors' => $errors]);
     return response($response->get(), $response->badRequest())->header('Content-Type', 'application/json');
 }
Ejemplo n.º 2
0
 /**
  * Get client details.
  *
  * @param int $clientId
  * @return array
  */
 public function getClient($clientId)
 {
     $response = new AjaxResponse();
     // Get client
     $client = Client::where('clients.id', $clientId)->where('clients.user_id', Auth::user()->id)->join('bills', 'clients.id', '=', 'bills.client_id')->select('clients.*', DB::raw('COUNT(bills.id) as total_bills'))->first();
     // Make sure client exists
     if (!$client->id) {
         $response->setFailMessage(trans('clients.client_not_found'));
         $response->addExtraFields(['redirect_to' => url('/clients')]);
         return response($response->get(), $response->getDefaultErrorResponseCode());
     }
     // Get client last unpaid bills
     $client->last_unpaid_bills = Clients::lastUnpaidBills($clientId);
     // Get client last paid bills
     $client->last_paid_bills = Clients::lastPaidBills($clientId);
     // Get client statistics
     $client->statistics = ClientStatistics::all($clientId);
     $client->money_generated = trans('clients.money_generated', ['money' => $client->statistics['earnings']]);
     $client->money_generated_in_current_year = trans('clients.money_generated_by_this_client_in_this_year_more_details', ['money' => $client->statistics['earnings_in_current_year']]);
     $client->number_of_products_sold = trans('clients.number_of_products_sold', ['number' => $client->statistics['number_of_products_ordered']]);
     $client->number_of_products_sold_this_year = trans('clients.number_of_products_sold_this_year', ['number' => $client->statistics['number_of_products_ordered_this_year']]);
     // Money user has to receive from this client
     $client->money_user_has_to_receive = 0;
     if ($client->statistics['money_user_has_to_receive'] > 0) {
         $client->money_user_has_to_receive = trans('clients.client_has_to_pay', ['sum' => $client->statistics['money_user_has_to_receive']]);
     }
     // Money client owes
     $client->money_owed_due_passed_payment_term = 0;
     if ($client->statistics['money_owed_due_passed_payment_term'] > 0) {
         $client->money_owed_due_passed_payment_term = trans('clients.client_has_to_pay_due_passed_payment_term', ['sum' => $client->statistics['money_owed_due_passed_payment_term']]);
     }
     $response->setSuccessMessage('');
     $response->addExtraFields(['data' => $client]);
     return response($response->get());
 }
Ejemplo n.º 3
0
 /**
  * @param array $errors
  * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  */
 public function response(array $errors)
 {
     $response = new AjaxResponse();
     $response->setFailMessage('error');
     $response->addExtraFields(['errors' => $errors]);
     return response($response->get(), $response->badRequest());
 }
Ejemplo n.º 4
0
 /**
  * @return mixed
  */
 public function getUnreadNotifications()
 {
     $response = new AjaxResponse();
     $response->setSuccessMessage(trans('common.success'));
     $notificationDetails = Notifications::getUnread(\Auth::user()->id);
     $response->addExtraFields($notificationDetails);
     return response($response->get())->header('Content-Type', 'application/json');
 }
Ejemplo n.º 5
0
 /**
  * Check if given product code is used by some user or not.
  *
  * @param CheckIfProductCodeIsUsedRequest $request
  * @param AjaxResponse $response
  * @return mixed
  */
 public function checkIfCodeIsUsed(CheckIfProductCodeIsUsedRequest $request, AjaxResponse $response)
 {
     $response->setSuccessMessage(trans('common.success'));
     // Assume product is not used and update status if is used
     $used = false;
     if (Product::where('code', $request->get('product_code'))->count() || ApplicationProduct::where('code', $request->get('product_code'))->count()) {
         $used = true;
     }
     $response->addExtraFields(['used' => $used]);
     return response($response->get())->header('Content-Type', 'application/json');
 }
Ejemplo n.º 6
0
 /**
  * Get product data.
  *
  * @param int $productId
  * @param string $productCode
  * @param GetProductRequest $request
  * @param AjaxResponse $response
  * @return mixed
  */
 public function get($productId, $productCode, GetProductRequest $request, AjaxResponse $response)
 {
     // Make sure product exists
     if (!ApplicationProduct::where('code', $productCode)->where('id', $productId)->count()) {
         $response->setFailMessage(trans('common.general_error'));
         return response($response->get())->header('Content-Type', 'application/json');
     }
     $response->setSuccessMessage(trans('common.success'));
     $response->addExtraFields(['product' => ProductsManagerHelper::productDetails($productCode, $productId)]);
     return response($response->get())->header('Content-Type', 'application/json');
 }
Ejemplo n.º 7
0
 public static function changeAccountStatus($status = 1, $userId = false)
 {
     $response = new AjaxResponse();
     $message = trans('users_manager.account_enabled');
     if ($status == 0) {
         $message = trans('users_manager.account_disabled');
     }
     if (!$userId) {
         $userId = Auth::user()->id;
     }
     \App\User::where('id', $userId)->update(['active' => $status]);
     $response->setSuccessMessage($message);
     $response->addExtraFields(['active' => $status]);
     return response($response->get())->header('Content-Type', 'application/json');
 }
Ejemplo n.º 8
0
 /**
  * Insert new product in database
  *
  * @param AddCustomProductRequest $request
  * @return mixed
  */
 public function addProduct(AddCustomProductRequest $request)
 {
     $response = new AjaxResponse();
     $code = $request->get('code');
     $name = $request->get('name');
     // Make sure product code is not already used by current user
     if (Product::where('code', $code)->where('user_id', Auth::user()->id)->count()) {
         $response->setFailMessage(trans('common.error'));
         $response->addExtraFields(['errors' => ['code' => trans('my_products.product_code_already_used')]]);
         return response($response->get(), 400)->header('Content-Type', 'application/json');
     }
     // Insert product
     $product = new Product();
     $product->name = $name;
     $product->code = $code;
     $product->user_id = Auth::user()->id;
     $product->save();
     $response->setSuccessMessage(trans('my_products.product_added'));
     return response($response->get())->header('Content-Type', 'application/json');
 }
Ejemplo n.º 9
0
 /**
  * Return all campaign numbers for given year.
  *
  * @param Requests\Statistics\GetCampaignNumbersRequest $request
  * @return mixed
  */
 public function getCampaignNumbers(GetCampaignNumbersRequest $request)
 {
     $response = new AjaxResponse();
     $response->setSuccessMessage(trans('common.success'));
     $response->addExtraFields(['numbers' => Campaign::select('number')->distinct()->where('year', $request->get('year'))->get()]);
     return response($response->get())->header('Content-Type', 'application/json');
 }
Ejemplo n.º 10
0
 /**
  * Get question categories.
  *
  * @param GetQuestionCategoriesRequest $request
  * @return mixed
  */
 public function getQuestionCategories(GetQuestionCategoriesRequest $request)
 {
     $response = new AjaxResponse();
     $response->setSuccessMessage(trans('common.success'));
     $response->addExtraFields(['question_categories' => QuestionCategory::all()]);
     return response($response->get())->header('Content-Type', 'application/json');
 }
Ejemplo n.º 11
0
 /**
  * Edit article title and content.
  *
  * @param int $categoryId
  * @param EditArticleRequest $request
  * @return mixed
  */
 public function editArticle($categoryId, EditArticleRequest $request)
 {
     $response = new AjaxResponse();
     $category = HelpCenterCategory::where('id', $categoryId)->first();
     if (!$category) {
         $response->setFailMessage(trans('help_center.category_not_found'));
         return response($response->get(), $response->getDefaultErrorResponseCode())->header('Content-Type', 'application/json');
     }
     // Edit article
     $article = HelpCenterArticle::find($request->get('article_id'));
     $article->title = $request->get('article_title');
     $article->content = $request->get('article_content');
     $article->save();
     // Get updated version of articles
     $extraFields = [];
     $articles = HelpCenterManagerHelper::getCategoryArticles($categoryId);
     if (count($articles)) {
         $extraFields['articles'] = $articles;
     }
     $response->setSuccessMessage(trans('help_center.article_updated'));
     $response->addExtraFields($extraFields);
     return response($response->get())->header('Content-Type', 'application/json');
 }
Ejemplo n.º 12
0
 /**
  * Get product details.
  *
  * @param string $productCode
  * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  */
 public static function details($productCode)
 {
     $response = new AjaxResponse();
     $isApplicationProduct = false;
     // Check if is in products table
     $product = Product::where('user_id', Auth::user()->id)->where('code', $productCode)->first();
     if (!$product) {
         $product = ApplicationProduct::where('code', $productCode)->first();
         $isApplicationProduct = true;
     }
     // Check if is in application_products table
     if (!$product) {
         $response->setFailMessage('not found');
         return response($response->get(), $response->getDefaultErrorResponseCode());
     }
     $response->setSuccessMessage('ok');
     if ($isApplicationProduct) {
         $data = ['id' => $product->id, 'code' => $product->code, 'name' => $product->name, 'created_at' => $product->created_at, 'sold_pieces' => self::productSoldPieces($product->id), 'total_price' => self::productTotalPrice($product->id), 'paid_bills' => self::paidBillsThatContainProduct($product->id), 'not_paid_bills' => self::notPaidBillsThatContainProduct($product->id), 'is_application_product' => $isApplicationProduct];
         $response->addExtraFields($data);
         return response($response->get());
     }
     $response->addExtraFields(['id' => $product->id, 'code' => $product->code, 'name' => $product->name, 'created_at' => $product->created_at, 'sold_pieces' => self::productSoldPieces($product->id, true), 'total_price' => self::productTotalPrice($product->id, true), 'paid_bills' => self::paidBillsThatContainProduct($product->id, true), 'not_paid_bills' => self::notPaidBillsThatContainProduct($product->id, true), 'is_application_product' => $isApplicationProduct]);
     return response($response->get());
 }
Ejemplo n.º 13
0
 /**
  * Return targeted users.
  *
  * @param GetTargetedUsersRequest $request
  * @param AjaxResponse $response
  * @return mixed
  */
 public function getTargetedUsers(GetTargetedUsersRequest $request, AjaxResponse $response)
 {
     $targetedUsers = TargetedUser::all();
     $response->setSuccessMessage(trans('notifications.targeted_users_returned'));
     $response->addExtraFields(['targeted_users' => $targetedUsers, 'number_of_targeted_users' => count($targetedUsers)]);
     return response($response->get())->header('Content-Type', 'application/json');
 }
Ejemplo n.º 14
0
 /**
  * @param DenyUsersToChangeLanguageRequest $request
  * @return mixed
  */
 public function denyUsersToChangeLanguage(DenyUsersToChangeLanguageRequest $request)
 {
     $securitySetting = SecuritySetting::first();
     $securitySetting->allow_users_to_change_language = 0;
     $securitySetting->save();
     // Success response
     $response = new AjaxResponse();
     $response->setSuccessMessage(trans('application_settings.users_are_not_allowed_to_change_language'));
     $response->addExtraFields(['allow_users_to_change_language' => trans('common.no'), 'allow_users_to_change_language_bool' => false]);
     return response($response->get())->header('Content-Type', 'application/json');
 }
Ejemplo n.º 15
0
 /**
  * Allow admin to edit user email.
  *
  * @param int $userId
  * @param EditUserEmailRequest $request
  * @return mixed
  */
 public function editUserEmail($userId, EditUserEmailRequest $request)
 {
     $response = new AjaxResponse();
     $user = User::find($userId);
     // Make sure user exists
     if (!$user) {
         $response->setFailMessage(trans('users_manager.user_not_found'));
         return response($response->get(), $response->badRequest())->header('Content-Type', 'application/json');
     }
     // Do nothing if email is the same
     if ($user->email === $request->get('email')) {
         $response->setSuccessMessage(trans('users_manager.user_email_updated'));
         $response->addExtraFields(['email' => $user->email]);
         return response($response->get())->header('Content-Type', 'application/json');
     }
     // Check if email is already used by another user
     if (User::where('email', $request->get('email'))->count()) {
         $response->setFailMessage(trans('users_manager.email_already_used'));
         return response($response->get(), $response->badRequest())->header('Content-Type', 'application/json');
     }
     // Update
     User::where('id', $userId)->update(['email' => $request->get('email')]);
     $response->setSuccessMessage(trans('users_manager.user_email_updated'));
     $response->addExtraFields(['email' => $request->get('email')]);
     return response($response->get())->header('Content-Type', 'application/json');
 }
Ejemplo n.º 16
0
 /**
  * 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());
 }
Ejemplo n.º 17
0
 /**
  * Disable offer.
  *
  * @param int $offerId
  * @param DisableOfferRequest $request
  * @return mixed
  */
 public function disableOffer($offerId, DisableOfferRequest $request)
 {
     $offer = Offer::find($offerId);
     $response = new AjaxResponse();
     // Make sure offer exists
     if (!$offer) {
         $response->setFailMessage(trans('offers.offer_not_found'));
         return response($response->get());
     }
     $offer->disabled = true;
     $offer->save();
     $response->setSuccessMessage(trans('offers.offer_disabled'));
     $response->addExtraFields(['offer' => Offer::countAssociatedSubscriptions()->where('offers.id', $offerId)->first()]);
     return response($response->get())->header('Content-Type', 'application/json');
 }
Ejemplo n.º 18
0
 /**
  * Mark bill as unpaid.
  *
  * @param int $billId
  * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  */
 public static function markAsUnpaid($billId)
 {
     $response = new AjaxResponse();
     // Make sure bill exists
     if (!Bill::where('id', $billId)->where('user_id', Auth::user()->id)->count()) {
         $response->setFailMessage(trans('bill.bill_not_found'));
         return response($response->get(), 404)->header('Content-Type', 'application/json');
     }
     Auth::user()->bills()->where('id', $billId)->update(['paid' => 0]);
     $response->setSuccessMessage(trans('bill.marked_as_unpaid'));
     $response->addExtraFields(['paid' => 0]);
     return response($response->get());
 }