Product.php 6.3 KB

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