db = new Database(); } function exists($priceset_id, $product_id) { $query = "SELECT * FROM ".$this->db->price_table." WHERE priceset_id='".$priceset_id."' AND product_id='".$product_id."'"; if(count($this->db->fetchRows($this->db->query($query))) > 0) return true; else return false; } function create($priceset_id, $product_id, $value) { $query = "INSERT INTO ".$this->db->price_table." VALUES('', '".$priceset_id."', '".$product_id."'"; for($i = 0; $i < count($value); $i++) $query .= ", '".str_replace("'", "\'", $value[$i])."'"; $query .= ")"; $this->db->query($query); } function update($priceset_id, $product_id, $value) { $query = "UPDATE ".$this->db->price_table." SET "; $query .= "price='".$value[0]."' "; $query .= "WHERE priceset_id='".$priceset_id."' AND product_id='".$product_id."'"; $this->db->query($query); } function delete($price_id) { $query = "DELETE FROM ".$this->db->price_table." WHERE price_id='$price_id'"; $this->db->query($query); } function get($price_id = "", $col_names = array(), $search = "") { if($price_id == "") { if(count($col_names) == 0) $query = "SELECT * FROM ".$this->db->price_table; else { $query = "SELECT ".$col_names[0]; for($i = 1; $i < count($col_names); $i++) $query .= ", ".$col_names[$i]; $query .= " FROM ".$this->db->price_table; } if($search != "") $query .= " WHERE ".$search; $query .= " ORDER BY price_id ASC"; return $this->db->fetchRows($this->db->query($query)); } else { $query = "SELECT * FROM ".$this->db->price_table." WHERE price_id='".$price_id."'"; $query .= " ORDER BY price_id ASC"; return $this->db->fetchRows($this->db->query($query)); } } function getPrice($priceset_id, $product_id) { $query = "SELECT * FROM ".$this->db->price_table." WHERE priceset_id='".$priceset_id."' AND product_id='".$product_id."'"; return $this->db->fetchRows($this->db->query($query)); } function getPriceTax($priceset_id, $product_id, $netto, $tax_id = -1) { $query = "SELECT * FROM ".$this->db->price_table." WHERE priceset_id='".$priceset_id."' AND product_id='".$product_id."'"; $tmp = $this->db->fetchRows($this->db->query($query)); $price = $tmp[0][3]; if($tax_id == -1) { $product = new Product(); $tmp_product = $product->get($product_id); if(count($tmp_product) > 0) $tax_id = $tmp_product[0][4]; else return 0; } if($netto) { $tax = new Tax(); $tmp_tax = $tax->get($tax_id); if(count($tmp_tax) <= 0) return 0; $price = $price * ( 1 + $tmp_tax[0][2]); } return $price; } function export($path) { $csv_data = ""; $query = "SELECT * FROM ".$this->db->price_table; $result = $this->db->query($query); while ($row = mysqli_fetch_assoc($result)) $csv_data .= implode(', ', $row)."\n"; $this->db->writeToFile($path, $csv_data); } function clearTable() { $query = "TRUNCATE TABLE ".$this->db->price_table; $this->db->query($query); } function import($path) { $file_access_ok = true; $file_open = false; $file_handle = false; $data = ""; clearstatcache(); if (!$file_handle = fopen($path, "r")) { echo "could not open file"; $file_access_ok = false; } if($file_handle) { $this->clearTable(); while( $line = fgets($file_handle, 1024)) { $values = explode("|", $line); for($i = 0; $i < count($values); $i++) $values[$i] = trim($values[$i]); $this->create($values[1], $values[2], array($values[3])); } fclose($file_handle); } } } ?>