Bill_Product_Import.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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_Import
  9. {
  10. function Bill_Product_Import()
  11. {
  12. $this->db = new Database();
  13. }
  14. function create($value)
  15. {
  16. $query = "INSERT INTO ".$this->db->bill_product_import_table." VALUES(''";
  17. for($i = 0; $i < count($value); $i++)
  18. $query .= ", '".$value[$i]."'";
  19. $query .= ")";
  20. $this->db->query($query);
  21. }
  22. function delete($bill_product_id)
  23. {
  24. $query = "DELETE FROM ".$this->db->bill_product_import_table." WHERE bill_product_id='$bill_product_id'";
  25. $this->db->query($query);
  26. }
  27. function get($bill_product_id = "", $row_names = array(), $search = "")
  28. {
  29. if($bill_product_id == "")
  30. {
  31. if(count($row_names) == 0)
  32. $query = "SELECT * FROM ".$this->db->bill_product_import_table;
  33. else
  34. {
  35. $query = "SELECT ".$row_names[0];
  36. for($i = 1; $i < count($row_names); $i++)
  37. $query .= ", ".$row_names[$i];
  38. $query .= " FROM ".$this->db->bill_product_import_table;
  39. }
  40. if($search != "")
  41. $query .= " WHERE ".$search;
  42. $query .= " ORDER BY product_id ASC";
  43. return $this->db->fetchRows($this->db->query($query));
  44. }
  45. else
  46. {
  47. $query = "SELECT * FROM ".$this->db->bill_product_import_table." WHERE bill_product_id='".$bill_product_id."'";
  48. return $this->db->fetchRows($this->db->query($query));
  49. }
  50. }
  51. function getByBillId($bill_id)
  52. {
  53. $query = "SELECT * FROM ".$this->db->bill_product_import_table." WHERE bill_id='".$bill_id."'";
  54. $query .= " ORDER BY product_id ASC";
  55. return $this->db->fetchRows($this->db->query($query));
  56. }
  57. function clearTable()
  58. {
  59. $query = "TRUNCATE TABLE ".$this->db->bill_product_import_table;
  60. $this->db->query($query);
  61. }
  62. function hasTimestampProductPair($timestamp, $product_id)
  63. {
  64. $query = "SELECT * FROM ".$this->db->bill_product_import_table." WHERE timestamp='".$timestamp."' and product_id='".$product_id."'";
  65. if(count($this->db->fetchRows($this->db->query($query))) > 0)
  66. return True;
  67. else
  68. return False;
  69. }
  70. function import($path)
  71. {
  72. $file_access_ok = true;
  73. $file_open = false;
  74. $file_handle = false;
  75. $data = "";
  76. clearstatcache();
  77. try
  78. {
  79. if (!$file_handle = @fopen($path, "r"))
  80. return -1;
  81. if($file_handle)
  82. {
  83. while( $zeile = fgets($file_handle, 1024))
  84. {
  85. $values = explode("|", $zeile);
  86. for($i = 0; $i < count($values); $i++)
  87. $values[$i] = trim($values[$i]);
  88. if(!$this->hasTimestampProductPair($values[8], $values[2]))
  89. $this->create(array( $values[1],
  90. $values[2],
  91. $values[3],
  92. $values[4],
  93. $values[5],
  94. $values[6],
  95. $values[7],
  96. $values[8]));
  97. }
  98. fclose($file_handle);
  99. }
  100. return 0;
  101. }
  102. catch (Exception $e)
  103. {
  104. return -1;
  105. }
  106. }
  107. }
  108. ?>