Bill_Product.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. require_once("DataBase/database.php");
  3. require_once("DataBase/Customer.php");
  4. require_once("DataBase/Bill.php");
  5. require_once("DataBase/Product.php");
  6. require_once("DataBase/Price.php");
  7. require_once("DataBase/Tax.php");
  8. class Bill_Product
  9. {
  10. function __construct()
  11. {
  12. $this->db = new Database();
  13. }
  14. function create($bill_id, $product_id, $amount, $details, $rabatt, $price, $tax)
  15. {
  16. echo "create bill product -".$value[0]."-";
  17. $query = "INSERT INTO ".$this->db->bill_product_table." (bill_id, product_id, amount, details, rabatt, price, tax) VALUES(".$bill_id.",'".$product_id."',".$amount.",'".$details."',".$rabatt.",".$price.",".$tax.")";
  18. echo "=== ".$query." ===";
  19. $this->db->query($query);
  20. }
  21. function update($bill_product_id, $value)
  22. {
  23. $query = "UPDATE ".$this->db->bill_product_table." SET ";
  24. $query .= "bill_id=".$value[0].", ";
  25. $query .= "product_id='".$value[1]."', ";
  26. $query .= "amount=".$value[2].", ";
  27. $query .= "details='".$value[3]."', ";
  28. $query .= "rabatt=".$value[4].", ";
  29. $query .= "price=".$value[5].", ";
  30. $query .= "tax=".$value[6]." ";
  31. $query .= "WHERE bill_product_id='".$bill_product_id."'";
  32. $this->db->query($query);
  33. }
  34. function delete($bill_product_id)
  35. {
  36. $query = "DELETE FROM ".$this->db->bill_product_table." WHERE bill_product_id='$bill_product_id'";
  37. $this->db->query($query);
  38. }
  39. function get($bill_product_id = "", $row_names = array(), $search = "")
  40. {
  41. if($bill_product_id == "")
  42. {
  43. if(count($row_names) == 0)
  44. $query = "SELECT * FROM ".$this->db->bill_product_table;
  45. else
  46. {
  47. $query = "SELECT ".$row_names[0];
  48. for($i = 1; $i < count($row_names); $i++)
  49. $query .= ", ".$row_names[$i];
  50. $query .= " FROM ".$this->db->bill_product_table;
  51. }
  52. if($search != "")
  53. $query .= " WHERE ".$search;
  54. $query .= " ORDER BY product_id ASC";
  55. return $this->db->fetchRows($this->db->query($query));
  56. }
  57. else
  58. {
  59. $query = "SELECT * FROM ".$this->db->bill_product_table." WHERE bill_product_id='".$bill_product_id."'";
  60. return $this->db->fetchRows($this->db->query($query));
  61. }
  62. }
  63. function getByBillId($bill_id)
  64. {
  65. $query = "SELECT * FROM ".$this->db->bill_product_table." WHERE bill_id='".$bill_id."'";
  66. $query .= " ORDER BY product_id ASC";
  67. return $this->db->fetchRows($this->db->query($query));
  68. }
  69. function getByBillIdAndProductId($bill_id, $product_id)
  70. {
  71. $query = "SELECT * FROM ".$this->db->bill_product_table." WHERE bill_id='".$bill_id."' AND product_id='".$product_id."'";
  72. $query .= " ORDER BY product_id ASC";
  73. return $this->db->fetchRows($this->db->query($query));
  74. }
  75. function getByBillIdsAndProductId($bill_id, $product_id)
  76. {
  77. $query = "SELECT * FROM ".$this->db->bill_product_table." WHERE product_id='".$product_id."' AND";
  78. $query .=" (bill_id='".$bill_id[0][0]."'";
  79. for($i=1;$i<count($bill_id); $i++)
  80. $query.=" OR bill_id='".$bill_id[$i][0]."'";
  81. $query .= ") ORDER BY product_id ASC";
  82. return $this->db->fetchRows($this->db->query($query));
  83. }
  84. function fix($bill_id)
  85. {
  86. /*$products = $this->getByBillId($bill_id);
  87. $product = new Product();
  88. $price = new Price();
  89. $tax = new Tax();
  90. $bill = new Bill();
  91. $tmp = $bill->get($bill_id, array("customer_id"));
  92. $customer = new Customer();
  93. $tmp = $customer->get($tmp[0]);
  94. $customer_data = $tmp[0];
  95. for($i = 0; $i < count($products); $i++)
  96. {
  97. $tmp = $this->getByBillIdAndProductId($bill_id, $products[$i][2]);
  98. $bill_product_data = $tmp[0];
  99. $tmp = $product->get($products[$i][2], array("product_id","tax_id"));
  100. $product_data = $tmp[0];
  101. $tmp = $price->getPrice($customer_data[11], $product_data[0]);
  102. $price_data = $tmp[0];
  103. $tmp = $tax->get($product_data[1]);
  104. $tax_data = $tmp[0];
  105. $this->update($bill_product_data[0], array($bill_product_data[1], $bill_product_data[2], $bill_product_data[3], $bill_product_data[4], $bill_product_data[5], $price_data[3], $tax_data[2]));
  106. }*/
  107. $query = "SELECT product.product_id,price.price FROM price,customer,product,bill_product,bill WHERE bill.bill_id='".$bill_id."' AND bill_product.bill_id='".$bill_id."' AND bill_product.product_id=product.product_id AND customer.customer_id=bill.customer_id AND customer.pricelist_id=price.priceset_id AND price.product_id=bill_product.product_id";
  108. $result = $this->db->fetchRows($this->db->query($query));
  109. for($i = 0; $i < count($result); $i++)
  110. {
  111. $query = "UPDATE bill_product SET price='".$result[$i][1]."' WHERE product_id='".$result[$i][0]."' AND bill_id='".$bill_id."'";
  112. $this->db->query($query);
  113. }
  114. }
  115. function getTourZusammenfassung($tour_id, $date)
  116. {
  117. $query = "SELECT DISTINCT product.product_id, product.name, SUM(bill_product.amount), product.order_number FROM product, bill_product,bill WHERE product.product_id=bill_product.product_id AND bill_product.bill_id=bill.bill_id AND bill_product.amount > 0 AND bill.date='".$date."' AND bill.tour_id=".$tour_id." GROUP BY product.product_id ORDER BY product.product_id";
  118. return $this->db->fetchRows($this->db->query($query));
  119. }
  120. function getTourZusammenfassungFromTo($tour_id, $date_from, $date_to)
  121. {
  122. $query = "SELECT DISTINCT product.product_id, product.name, SUM(bill_product.amount), product.order_number FROM product, bill_product,bill WHERE product.product_id=bill_product.product_id AND bill_product.bill_id=bill.bill_id AND bill_product.amount > 0 AND bill.date>='".$date_from."' AND bill.date<='".$date_to."' GROUP BY product.product_id ORDER BY product.product_id";
  123. return $this->db->fetchRows($this->db->query($query));
  124. }
  125. function clearTable()
  126. {
  127. $query = "TRUNCATE TABLE ".$this->db->bill_product_table;
  128. $this->db->query($query);
  129. }
  130. function export($path)
  131. {
  132. $csv_data = "";
  133. // $query = "SELECT * FROM ".$this->db->bill_product_table;
  134. $query = "SELECT * FROM ".$this->db->bill_product_table." WHERE timestamp >= (UNIX_TIMESTAMP(NOW() - INTERVAL 1 MONTH))";
  135. $result = $this->db->query($query);
  136. while ($row = mysqli_fetch_assoc($result))
  137. $csv_data .= implode('|', $row)."\n";
  138. $status = $this->db->writeToFile($path, $csv_data);
  139. return $status;
  140. // if($status)
  141. // $this->clearTable();
  142. }
  143. }
  144. ?>