Product.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. require_once(__DIR__."/database.php");
  3. class Product
  4. {
  5. function __construct()
  6. {
  7. $this->db = new Database();
  8. }
  9. function create($product_id, $value)
  10. {
  11. $query = "INSERT INTO ".$this->db->product_table." VALUES('".$product_id."'";
  12. for($i = 0; $i < count($value); $i++)
  13. $query .= ", '".str_replace("'", "\'", $value[$i])."'";
  14. $query .= ")";
  15. $this->db->query($query);
  16. }
  17. function update($product_id, $value)
  18. {
  19. $query = "UPDATE ".$this->db->product_table." SET ";
  20. $query .= "name='".str_replace("'", "\'", $value[0])."', ";
  21. $query .= "details='".str_replace("'", "\'", $value[1])."', ";
  22. $query .= "category_id='".str_replace("'", "\'", $value[2])."', ";
  23. $query .= "tax_id='".str_replace("'", "\'", $value[3])."', ";
  24. $query .= "available='".str_replace("'", "\'", $value[4])."', ";
  25. $query .= "group_id='".str_replace("'", "\'", $value[5])."', ";
  26. $query .= "order_number='".str_replace("'", "\'", $value[6])."', ";
  27. $query .= "anmerkung='".str_replace("'", "\'", $value[7])."' ";
  28. $query .= "WHERE product_id='".$product_id."'";
  29. $this->db->query($query);
  30. }
  31. function delete($product_id)
  32. {
  33. $query = "DELETE FROM ".$this->db->product_table." WHERE product_id='$product_id'";
  34. $this->db->query($query);
  35. }
  36. function exists($product_id, $search = "")
  37. {
  38. $query = "SELECT product_id FROM ".$this->db->product_table." WHERE product_id='".$product_id."'";
  39. if($search != "")
  40. $query .= " AND ".$search;
  41. $result = $this->db->fetchRows($this->db->query($query));
  42. if(count($result) > 0)
  43. return true;
  44. else
  45. return false;
  46. }
  47. function get($product_id = "", $row_names = array(), $search = "")
  48. {
  49. if($product_id == "")
  50. {
  51. if(count($row_names) == 0)
  52. $query = "SELECT * FROM ".$this->db->product_table;
  53. else
  54. {
  55. $query = "SELECT ".$row_names[0];
  56. for($i = 1; $i < count($row_names); $i++)
  57. $query .= ", ".$row_names[$i];
  58. $query .= " FROM ".$this->db->product_table;
  59. }
  60. if($search != "")
  61. $query .= " WHERE ".$search;
  62. $query .= " ORDER BY product_id ASC";
  63. return $this->db->fetchRows($this->db->query($query));
  64. }
  65. else
  66. {
  67. if(count($row_names) == 0)
  68. $query = "SELECT * FROM ".$this->db->product_table;
  69. else
  70. {
  71. $query = "SELECT ".$row_names[0];
  72. for($i = 1; $i < count($row_names); $i++)
  73. $query .= ", ".$row_names[$i];
  74. $query .= " FROM ".$this->db->product_table;
  75. }
  76. $query .= " WHERE product_id='".$product_id."'";
  77. if($search != "")
  78. $query .= " AND ".$search;
  79. $query .= " ORDER BY product_id ASC";
  80. return $this->db->fetchRows($this->db->query($query));
  81. }
  82. }
  83. function getAmountByDate($product_id, $date_from, $date_to, $assistant_id)
  84. {
  85. $query = "SELECT SUM(".$this->db->bill_product_table.".amount) FROM ".$this->db->bill_table." ";
  86. $query .= "RIGHT JOIN ".$this->db->bill_product_table." ON ".$this->db->bill_table.".bill_id=".$this->db->bill_product_table.".bill_id ";
  87. $query .= "WHERE ".$this->db->bill_table.".date>='".$date_from."' ";
  88. $query .= "AND ".$this->db->bill_table.".date<='".$date_to."' ";
  89. $query .= "AND ".$this->db->bill_table.".assistant_id<='".$assistant_id."' ";
  90. $query .= "AND ".$this->db->bill_product_table.".product_id='".$product_id."' ";
  91. $retval = $this->db->fetchRows($this->db->query($query));
  92. return $retval[0][0];
  93. }
  94. function getBillsByDate($product_id, $date_from, $date_to, $assistant_id)
  95. {
  96. $query = "SELECT DISTINCT ".$this->db->bill_table.".bill_number,SUM(".$this->db->bill_product_table.".amount) FROM ".$this->db->bill_table." ";
  97. $query .= "RIGHT JOIN ".$this->db->bill_product_table." ON ".$this->db->bill_table.".bill_id=".$this->db->bill_product_table.".bill_id ";
  98. $query .= "WHERE ".$this->db->bill_table.".date>='".$date_from."' ";
  99. $query .= "AND ".$this->db->bill_table.".date<='".$date_to."' ";
  100. $query .= "AND ".$this->db->bill_table.".assistant_id<='".$assistant_id."' ";
  101. $query .= "AND ".$this->db->bill_product_table.".product_id='".$product_id."' ";
  102. $query .= "GROUP BY ".$this->db->bill_table.".customer_id ";
  103. return $this->db->fetchRows($this->db->query($query));
  104. }
  105. function getCustomersByDate($product_id, $date_from, $date_to, $assistant_id)
  106. {
  107. $query = "SELECT DISTINCT ".$this->db->bill_table.".customer_id,SUM(".$this->db->bill_product_table.".amount) FROM ".$this->db->bill_table." ";
  108. $query .= "RIGHT JOIN ".$this->db->bill_product_table." ON ".$this->db->bill_table.".bill_id=".$this->db->bill_product_table.".bill_id ";
  109. $query .= "WHERE ".$this->db->bill_table.".date>='".$date_from."' ";
  110. $query .= "AND ".$this->db->bill_table.".date<='".$date_to."' ";
  111. $query .= "AND ".$this->db->bill_table.".assistant_id<='".$assistant_id."' ";
  112. $query .= "AND ".$this->db->bill_product_table.".product_id='".$product_id."' ";
  113. $query .= "GROUP BY ".$this->db->bill_table.".customer_id ";
  114. return $this->db->fetchRows($this->db->query($query));
  115. }
  116. function getByFilter($search_product, $search_category, $item_offset, $item_count)
  117. {
  118. $query = "SELECT * FROM ".$this->db->product_table." ";
  119. if($search_product != "" || $search_category != "")
  120. {
  121. $query .= " WHERE ";
  122. if($search_product != "")
  123. $query .= " (product_id LIKE '%".$search_product."%' OR name LIKE '%".$search_product."%' OR details LIKE '%".$search_product."%') ";
  124. if($search_category != "" && $search_product != "")
  125. $query .= " AND category_id=".$search_category." ";
  126. elseif($search_category != "" && $search_product == "")
  127. $query .= " category_id=".$search_category." ";
  128. }
  129. $query .= " ORDER BY product_id ASC";
  130. if($item_count != 0)
  131. $query .= " LIMIT ".$item_count." ";
  132. if($item_offset != 0)
  133. $query .= " OFFSET ".$item_offset." ";
  134. return $this->db->fetchRows($this->db->query($query));
  135. }
  136. function getCountByFilter($search_product, $search_category)
  137. {
  138. $query = "SELECT count(*) FROM ".$this->db->product_table." ";
  139. if($search_product != "" || $search_category != "")
  140. {
  141. $query .= " WHERE ";
  142. if($search_product != "")
  143. $query .= " (product_id LIKE '%".$search_product."%' OR name LIKE'%".$search_product."%' OR details LIKE '%".$search_product."%') AND ";
  144. if($search_category != "")
  145. $query .= " category_id=".$search_category." ";
  146. else
  147. $query .= " 1=1 ";
  148. }
  149. $query .= " ORDER BY product_id ASC";
  150. return $this->db->fetchRows($this->db->query($query));
  151. }
  152. }
  153. ?>