Пример #1
0
 /**
  * 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;
 }
Пример #3
0
 /**
  * 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;
 }
Пример #4
0
 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;
 }