delete( 'products', [ 'id' => $product_id ] ); return true; } static public function delete_products( $product_ids ) { global $mdb; if ( empty( $product_ids ) || !is_array( $product_ids ) ) { return false; } foreach ( $product_ids as $product_id ) { $mdb -> delete( 'products', [ 'id' => $product_id ] ); } return true; } static public function get_product_comments( $product_id ) { global $mdb; return $mdb -> query( 'SELECT id, comment, date_add FROM products_comments WHERE product_id = \'' . $product_id . '\' ORDER BY date_add DESC' ) -> fetchAll( \PDO::FETCH_ASSOC ); } static public function delete_product_comment( $comment_id ) { global $mdb; return $mdb -> delete( 'products_comments', [ 'id' => $comment_id ] ); } static public function get_product_comment_by_date( $product_id, $date ) { global $mdb; return $mdb -> get( 'products_comments', [ 'id', 'comment' ], [ 'AND' => [ 'product_id' => $product_id, 'date_add' => $date ] ] ); } static public function get_min_roas( $product_id ) { global $mdb; return $mdb -> get( 'products', 'min_roas', [ 'id' => $product_id ] ); } static public function get_client_bestseller_min_roas( $client_id ) { global $mdb; return $mdb -> get( 'clients', 'bestseller_min_roas', [ 'id' => $client_id ] ); } static public function save_client_bestseller_min_roas( $client_id, $min_roas ) { global $mdb; return $mdb -> update( 'clients', [ 'bestseller_min_roas' => $min_roas ], [ 'id' => $client_id ] ); } static public function save_min_roas( $product_id, $min_roas ) { global $mdb; return $mdb -> update( 'products', [ 'min_roas' => $min_roas ], [ 'id' => $product_id ] ); } static public function get_products( $client_id, $search, $limit, $start, $order_name, $order_dir ) { global $mdb; if ( $search ) return $mdb -> query( 'SELECT pt.*, p.offer_id, p.min_roas FROM products_temp AS pt INNER JOIN products AS p ON p.id = pt.product_id WHERE client_id = \'' . $client_id . '\' AND ( pt.name LIKE \'%' . $search . '%\' OR offer_id LIKE \'%' . $search . '%\' ) ORDER BY ' . $order_name . ' ' . $order_dir . ', id DESC LIMIT ' . $start . ', ' . $limit ) -> fetchAll(); else return $mdb -> query( 'SELECT pt.*, p.offer_id, p.min_roas FROM products_temp AS pt INNER JOIN products AS p ON p.id = pt.product_id WHERE client_id = \'' . $client_id . '\' ORDER BY ' . $order_name . ' ' . $order_dir . ', id DESC LIMIT ' . $start . ', ' . $limit ) -> fetchAll(); } // \factory\Products.php public static function get_roas_bounds(int $client_id, ?string $search = null): array { global $mdb; $params = [':client_id' => $client_id]; $sql = 'SELECT MIN(p.min_roas) AS min_roas, MAX(pt.roas) AS max_roas FROM products_temp AS pt INNER JOIN products AS p ON p.id = pt.product_id WHERE p.client_id = :client_id AND conversions > 10'; if ($search) { $sql .= ' AND (pt.name LIKE :search OR p.offer_id LIKE :search)'; $params[':search'] = '%' . $search . '%'; } $row = $mdb->query($sql, $params)->fetch(\PDO::FETCH_ASSOC); return [ 'min' => isset($row['min_roas']) ? (float)$row['min_roas'] : 0.0, 'max' => isset($row['max_roas']) ? (float)$row['max_roas'] : 0.0, ]; } static public function get_records_total_products( $client_id, $search ) { global $mdb; if ( $search ) return $mdb -> query( 'SELECT COUNT(0) FROM products_temp AS pt INNER JOIN products AS p ON p.id = pt.product_id WHERE client_id = \'' . $client_id . '\' AND ( pt.name LIKE \'%' . $search . '%\' OR offer_id LIKE \'%' . $search . '%\' )' ) -> fetchColumn(); else return $mdb -> query( 'SELECT COUNT(0) FROM products_temp AS pt INNER JOIN products AS p ON p.id = pt.product_id WHERE client_id = \'' . $client_id . '\'' ) -> fetchColumn(); } static public function get_product_full_context( $product_id ) { global $mdb; return $mdb -> query( 'SELECT p.id, p.offer_id, p.name, p.min_roas, pt.impressions, pt.impressions_30, pt.clicks, pt.clicks_30, pt.ctr, pt.cost, pt.cpc, pt.conversions, pt.conversions_value, pt.roas FROM products AS p LEFT JOIN products_temp AS pt ON pt.product_id = p.id WHERE p.id = :pid', [ ':pid' => $product_id ] ) -> fetch( \PDO::FETCH_ASSOC ); } static public function get_product_data( $product_id, $field ) { global $mdb; return $mdb -> get( 'products_data', $field, [ 'product_id' => $product_id ] ); } static public function set_product_data( $product_id, $field, $value ) { global $mdb; if ( !$mdb -> count( 'products_data', [ 'product_id' => $product_id ] ) ) $result = $mdb -> insert( 'products_data', [ 'product_id' => $product_id, $field => $value ] ); else $result = $mdb -> update( 'products_data', [ $field => $value ], [ 'product_id' => $product_id ] ); return $result; } static public function get_product_history( $client_id, $product_id, $start, $limit ) { global $mdb; return $mdb -> query( 'SELECT * FROM products_history AS ph WHERE ph.product_id = \'' . $product_id . '\' ORDER BY ph.date_add DESC LIMIT ' . $start . ', ' . $limit ) -> fetchAll( \PDO::FETCH_ASSOC ); } static public function get_records_total_product_history( $client_id, $product_id ) { global $mdb; return $mdb -> query( 'SELECT COUNT(0) FROM products_history AS ph WHERE ph.product_id = \'' . $product_id . '\'' ) -> fetchColumn(); } static public function get_product_history_30( $client_id, $product_id, $start, $limit ) { global $mdb; return $mdb -> query( 'SELECT * FROM products_history_30 AS ph3 WHERE ph3.product_id = \'' . $product_id . '\' ORDER BY ph3.date_add ASC LIMIT ' . $start . ', ' . $limit ) -> fetchAll( \PDO::FETCH_ASSOC ); } static public function get_impressions_30( $product_id ) { global $mdb; return $mdb -> query( 'SELECT SUM(impressions) FROM products_history WHERE product_id = \'' . $product_id . '\' AND date_add >= \'' . date( 'Y-m-d', strtotime( '-30 days', time() ) ) . '\'' ) -> fetchColumn(); } static public function get_clicks_30( $product_id ) { global $mdb; return $mdb -> query( 'SELECT SUM(clicks) FROM products_history WHERE product_id = \'' . $product_id . '\' AND date_add >= \'' . date( 'Y-m-d', strtotime( '-30 days', time() ) ) . '\'' ) -> fetchColumn(); } static public function add_product_comment( $product_id, $comment, $date = null ) { global $mdb; if ( !$date ) $date = date( 'Y-m-d' ); else $date = date( 'Y-m-d', strtotime( $date ) ); if ( $mdb -> count( 'products_comments', [ 'AND' => [ 'product_id' => $product_id, 'date_add' => $date ] ] ) ) return $mdb -> update( 'products_comments', [ 'comment' => $comment ], [ 'AND' => [ 'product_id' => $product_id, 'date_add' => $date ] ] ); else return $mdb -> insert( 'products_comments', [ 'product_id' => $product_id, 'comment' => $comment, 'date_add' => $date ] ); } }