Bill.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. require_once(__DIR__."/database.php");
  3. class Bill
  4. {
  5. function __construct()
  6. {
  7. $this->db = new Database();
  8. }
  9. function create($customer_id, $tour_id, $assistant_id, $bill_number, $date)
  10. {
  11. $query = "INSERT INTO ".$this->db->bill_table." (customer_id, tour_id, assistant_id, bill_number, timestamp, date) VALUES(".$customer_id.", ".$tour_id.",".$assistant_id.",".$bill_number.",CURRENT_TIMESTAMP(),'".$date."')";
  12. $this->db->query($query);
  13. $query = "SELECT bill_id from ".$this->db->bill_table." ORDER BY bill_id DESC";
  14. $tmp = $this->db->fetchRows($this->db->query($query));
  15. return $tmp[0][0];
  16. }
  17. function update($bill_id, $value)
  18. {
  19. $query = "UPDATE ".$this->db->bill_table." SET ";
  20. $query .= "customer_id=".$value[0].", ";
  21. $query .= "tour_id=".$value[1].", ";
  22. $query .= "date='".$value[2]."', ";
  23. $query .= "assistant_id=".$value[3].", ";
  24. $query .= "bill_number=".$value[4]." ";
  25. $query .= "WHERE bill_id=".$bill_id."";
  26. $this->db->query($query);
  27. }
  28. function setNumber($bill_id)
  29. {
  30. $data = $this->get($bill_id);
  31. $query = "UPDATE ".$this->db->bill_table." SET ";
  32. $query .= "bill_number=".$this->getNextFreeNumberByAssistant($data[0][4])." ";
  33. $query .= "WHERE bill_id=".$bill_id."";
  34. $this->db->query($query);
  35. }
  36. function delete($bill_id)
  37. {
  38. $query = "DELETE FROM ".$this->db->bill_table." WHERE bill_id=".$bill_id."";
  39. $this->db->query($query);
  40. }
  41. function get($bill_id = "", $row_names = array(), $search = "")
  42. {
  43. if($bill_id == "")
  44. {
  45. if(count($row_names) == 0)
  46. $query = "SELECT * FROM ".$this->db->bill_table.", ".$this->db->customer_table;
  47. else
  48. {
  49. $query = "SELECT ".$row_names[0];
  50. for($i = 1; $i < count($row_names); $i++)
  51. $query .= ", ".$row_names[$i];
  52. $query .= " FROM ".$this->db->bill_table.", ".$this->db->customer_table;
  53. }
  54. $query .= " WHERE bill.customer_id=customer.customer_id ";
  55. if($search != "")
  56. $query .= " AND ( ".$search." )";
  57. $query .= " ORDER BY bill_number ASC";
  58. return $this->db->fetchRows($this->db->query($query));
  59. }
  60. else
  61. {
  62. $query = "SELECT * FROM ".$this->db->bill_table." WHERE bill_id=".$bill_id."";
  63. $query .= " ORDER BY bill_number ASC";
  64. return $this->db->fetchRows($this->db->query($query));
  65. }
  66. }
  67. function getNextFreeNumberByAssistant($assistant_id)
  68. {
  69. $query = "SELECT bill_number FROM ".$this->db->bill_table." WHERE assistant_id=".$assistant_id." ORDER BY bill_number DESC";
  70. $result = $this->db->fetchRows($this->db->query($query));
  71. return $result[0][0]+1;
  72. }
  73. }
  74. ?>