Price.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. require_once("DataBase/database.php");
  3. class Price
  4. {
  5. function __construct()
  6. {
  7. $this->db = new Database();
  8. }
  9. function exists($priceset_id, $product_id)
  10. {
  11. $query = "SELECT * FROM ".$this->db->price_table." WHERE priceset_id='".$priceset_id."' AND product_id='".$product_id."'";
  12. if(count($this->db->fetchRows($this->db->query($query))) > 0)
  13. return true;
  14. else
  15. return false;
  16. }
  17. function create($priceset_id, $product_id, $value)
  18. {
  19. $query = "INSERT INTO ".$this->db->price_table." VALUES('', '".$priceset_id."', '".$product_id."'";
  20. for($i = 0; $i < count($value); $i++)
  21. $query .= ", '".str_replace("'", "\'", $value[$i])."'";
  22. $query .= ")";
  23. $this->db->query($query);
  24. }
  25. function update($priceset_id, $product_id, $value)
  26. {
  27. $query = "UPDATE ".$this->db->price_table." SET ";
  28. $query .= "price='".$value[0]."' ";
  29. $query .= "WHERE priceset_id='".$priceset_id."' AND product_id='".$product_id."'";
  30. $this->db->query($query);
  31. }
  32. function delete($price_id)
  33. {
  34. $query = "DELETE FROM ".$this->db->price_table." WHERE price_id='$price_id'";
  35. $this->db->query($query);
  36. }
  37. function get($price_id = "", $col_names = array(), $search = "")
  38. {
  39. if($price_id == "")
  40. {
  41. if(count($col_names) == 0)
  42. $query = "SELECT * FROM ".$this->db->price_table;
  43. else
  44. {
  45. $query = "SELECT ".$col_names[0];
  46. for($i = 1; $i < count($col_names); $i++)
  47. $query .= ", ".$col_names[$i];
  48. $query .= " FROM ".$this->db->price_table;
  49. }
  50. if($search != "")
  51. $query .= " WHERE ".$search;
  52. $query .= " ORDER BY price_id ASC";
  53. return $this->db->fetchRows($this->db->query($query));
  54. }
  55. else
  56. {
  57. $query = "SELECT * FROM ".$this->db->price_table." WHERE price_id='".$price_id."'";
  58. $query .= " ORDER BY price_id ASC";
  59. return $this->db->fetchRows($this->db->query($query));
  60. }
  61. }
  62. function getPrice($priceset_id, $product_id)
  63. {
  64. $query = "SELECT * FROM ".$this->db->price_table." WHERE priceset_id='".$priceset_id."' AND product_id='".$product_id."'";
  65. return $this->db->fetchRows($this->db->query($query));
  66. }
  67. function getPriceTax($priceset_id, $product_id, $netto, $tax_id = -1)
  68. {
  69. $query = "SELECT * FROM ".$this->db->price_table." WHERE priceset_id='".$priceset_id."' AND product_id='".$product_id."'";
  70. $tmp = $this->db->fetchRows($this->db->query($query));
  71. $price = $tmp[0][3];
  72. if($tax_id == -1)
  73. {
  74. $product = new Product();
  75. $tmp_product = $product->get($product_id);
  76. if(count($tmp_product) > 0)
  77. $tax_id = $tmp_product[0][4];
  78. else
  79. return 0;
  80. }
  81. if($netto)
  82. {
  83. $tax = new Tax();
  84. $tmp_tax = $tax->get($tax_id);
  85. if(count($tmp_tax) <= 0)
  86. return 0;
  87. $price = $price * ( 1 + $tmp_tax[0][2]);
  88. }
  89. return $price;
  90. }
  91. function export($path)
  92. {
  93. $csv_data = "";
  94. $query = "SELECT * FROM ".$this->db->price_table;
  95. $result = $this->db->query($query);
  96. while ($row = mysqli_fetch_assoc($result))
  97. $csv_data .= implode(', ', $row)."\n";
  98. $this->db->writeToFile($path, $csv_data);
  99. }
  100. function clearTable()
  101. {
  102. $query = "TRUNCATE TABLE ".$this->db->price_table;
  103. $this->db->query($query);
  104. }
  105. function import($path)
  106. {
  107. $file_access_ok = true;
  108. $file_open = false;
  109. $file_handle = false;
  110. $data = "";
  111. clearstatcache();
  112. if (!$file_handle = fopen($path, "r"))
  113. {
  114. echo "could not open file";
  115. $file_access_ok = false;
  116. }
  117. if($file_handle)
  118. {
  119. $this->clearTable();
  120. while( $line = fgets($file_handle, 1024))
  121. {
  122. $values = explode("|", $line);
  123. for($i = 0; $i < count($values); $i++)
  124. $values[$i] = trim($values[$i]);
  125. $this->create($values[1], $values[2], array($values[3]));
  126. }
  127. fclose($file_handle);
  128. }
  129. }
  130. }
  131. ?>