ShoppingCart.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. require_once("DataBase/database.php");
  3. require_once("DataBase/Product.php");
  4. require_once("DataBase/Priceset.php");
  5. require_once("DataBase/Price.php");
  6. require_once("DataBase/Customer.php");
  7. require_once("DataBase/Bill.php");
  8. require_once("DataBase/Tour.php");
  9. require_once("DataBase/Bill_Product.php");
  10. require_once("DataBase/Customer_Template.php");
  11. class ShoppingCart
  12. {
  13. function __construct($param_customer_id, $param_product_id_array = array(), $param_product_amount_array = array(), $param_product_hint_array = array())
  14. {
  15. $this->product = new Product();
  16. $this->customer = new Customer();
  17. $this->product = new Product();
  18. $this->price = new Price();
  19. $this->priceset = new Priceset();
  20. $this->customer_template = new Customer_Template();
  21. $this->product_id_array = $param_product_id_array;
  22. $this->product_amount_array = $param_product_amount_array;
  23. $this->product_hint_array = $param_product_hint_array;
  24. $this->customer_id = $param_customer_id;
  25. if(count($this->product_id_array) > count($this->product_amount_array))
  26. echo "<div class=\"error\">Fehler in Session, bitte neu anmelden</div><br>\n";
  27. $this->priceset_id = 0;
  28. $customer_data = $this->customer->get($param_customer_id);
  29. if(count($customer_data) > 0)
  30. $this->priceset_id = $customer_data[0][11];
  31. $tmp_priceset = $this->priceset->get($this->priceset_id);
  32. $this->netto = 0;
  33. if(count($tmp_priceset) > 0)
  34. $this->netto = $tmp_priceset[0][2];
  35. }
  36. function getItemCount()
  37. {
  38. return count($this->product_id_array);
  39. }
  40. function getPriceSum()
  41. {
  42. $ret = 0;
  43. for($i = 0; $i < count($this->product_id_array); $i++)
  44. {
  45. $price = $this->price->getPriceTax($this->priceset_id, $this->product_id_array[$i], $this->netto);
  46. $ret += $price * $this->product_amount_array[$i];
  47. }
  48. return $ret;
  49. }
  50. function addProduct($param_product_id, $param_amount, $param_hint = "")
  51. {
  52. $product_data = $this->product->get($param_product_id);
  53. if(count($product_data) == 0)
  54. {
  55. echo "ShoppingCart::addProduct()", $param_product_id;
  56. echo "<div class=\"error\">Fehler in Session, bitte neu anmelden</div><br>\n";
  57. return;
  58. }
  59. /* check if product already in cart */
  60. for($i = 0; $i < count($this->product_id_array); $i++)
  61. {
  62. if($this->product_id_array[$i] == $param_product_id)
  63. {
  64. $this->product_amount_array[$i] += $param_amount;
  65. $this->saveShoppingCartSession();
  66. return;
  67. }
  68. }
  69. /* new product in cart */
  70. $this->product_id_array []= $param_product_id;
  71. $this->product_amount_array []= $param_amount;
  72. $this->product_hint_array []= $param_hint;
  73. $this->saveShoppingCartSession();
  74. }
  75. function removeProduct($param_product_id)
  76. {
  77. $tmp_id_array = array();
  78. $tmp_amount_array = array();
  79. $tmp_hint_array = array();
  80. for($i = 0; $i < count($this->product_id_array); $i++)
  81. {
  82. if($this->product_id_array[$i] != $param_product_id)
  83. {
  84. $tmp_id_array []= $this->product_id_array[$i];
  85. $tmp_amount_array []= $this->product_amount_array[$i];
  86. $tmp_hint_array []= $this->product_hint_array[$i];
  87. }
  88. }
  89. $this->product_id_array = $tmp_id_array;
  90. $this->product_amount_array = $tmp_amount_array;
  91. $this->product_hint_array = $tmp_hint_array;
  92. $this->saveShoppingCartSession();
  93. }
  94. function editProduct($param_product_id, $param_amount, $param_hint = "")
  95. {
  96. for($i = 0; $i < count($this->product_id_array); $i++)
  97. {
  98. if($this->product_id_array[$i] == $param_product_id)
  99. {
  100. $this->product_amount_array[$i] = $param_amount;
  101. $this->product_hint_array[$i] = $param_hint;
  102. }
  103. }
  104. $this->saveShoppingCartSession();
  105. }
  106. function saveShoppingCartSession()
  107. {
  108. $_SESSION["cart_product_id_array"] = $this->product_id_array;
  109. $_SESSION["cart_product_amount_array"] = $this->product_amount_array;
  110. $_SESSION["cart_product_hint_array"] = $this->product_hint_array;
  111. }
  112. function clearShoppingCart()
  113. {
  114. unset($_SESSION["cart_product_id_array"]);
  115. unset($_SESSION["cart_product_amount_array"]);
  116. unset($_SESSION["cart_product_hint_array"]);
  117. }
  118. function saveShoppingCartDatabase()
  119. {
  120. }
  121. function saveTemplate()
  122. {
  123. $this->customer_template->clear($this->customer_id);
  124. $list = $this->getItems();
  125. for($i = 0; $i < count($list); $i++)
  126. $this->customer_template->set($this->customer_id, $list[$i][0], $list[$i][2], $list[$i][5]);
  127. }
  128. function loadTemplate()
  129. {
  130. $list = $this->customer_template->get($this->customer_id);
  131. for($i = 0; $i < count($list); $i++) {
  132. // check if still exists and available
  133. $result = $this->product->get($list[$i][1]);
  134. $product = $result[0];
  135. if ($product && $product[5]) $this->addProduct($list[$i][1], $list[$i][2], $list[$i][3]);
  136. }
  137. $this->saveShoppingCartSession();
  138. }
  139. function getItems()
  140. {
  141. $ret = array();
  142. for($i = 0; $i < count($this->product_id_array); $i++)
  143. {
  144. $tmp_product = $this->product->get($this->product_id_array[$i]);
  145. if(count($tmp_product) == 0)
  146. {
  147. echo "<div class=\"error\">Fehler in Session, bitte neu anmelden</div><br>\n";
  148. return -1;
  149. }
  150. $price = $this->price->getPriceTax($this->priceset_id, $this->product_id_array[$i], $this->netto);
  151. $ret []= array($this->product_id_array[$i], $tmp_product[0][1], $this->product_amount_array[$i], $price, $tmp_product[0][2], $this->product_hint_array[$i]);
  152. }
  153. return $ret;
  154. }
  155. function createBill()
  156. {
  157. $bill = new Bill();
  158. $customer = new Customer();
  159. $tour = new Tour();
  160. $bill_product = new Bill_Product();
  161. $customer_data = $customer->get($this->customer_id);
  162. if(count($customer_data) == 0)
  163. {
  164. echo "<div class=\"error\">Fehler in Session, bitte neu anmelden</div><br>\n";
  165. return -1;
  166. }
  167. $tour_data = $tour->get($customer_data[0][12]);
  168. $bill_id = $bill->create($this->customer_id, $customer_data[0][12], $tour_data[0][2], -2, date("Y-m-d"));
  169. $list = $this->getItems();
  170. for($i = 0; $i < count($list); $i++) {
  171. if ($list[$i][2] > 0) {
  172. $bill_product->create($bill_id, $list[$i][0], $list[$i][2], $list[$i][5], 0, 0, 0);
  173. }
  174. }
  175. return 0;
  176. }
  177. }
  178. ?>