diff --git a/inside/Order.php b/inside/Order.php index b605d98..21b090e 100644 --- a/inside/Order.php +++ b/inside/Order.php @@ -19,7 +19,7 @@ class Order { $this->__construct($id, $data); } - public + public function __construct($id = NULL, $data = NULL){ $this->conn = db_connect(); @@ -40,7 +40,7 @@ class Order { $this->order_status_id = $data['order_status_id']; $this->order_status_value = $data['order_status_value']; $this->comment = $data['comment']; - + } } //Common initializations @@ -50,10 +50,10 @@ class Order { function store(){ if ($this->id == NULL){ $this->id = getNextId("din_order"); - $sql = sprintf("INSERT INTO din_order + $sql = sprintf("INSERT INTO din_order (id, user_id, timestamp, comment) - VALUES - (%s, %s, NOW(), %s)", + VALUES + (%s, %s, NOW(), %s)", $this->conn->quoteSmart($this->id), $this->conn->quoteSmart($this->user_id), $this->conn->quoteSmart($this->comment) @@ -63,41 +63,58 @@ class Order { //notify("Ny ordre lagret."); }else { error("New order: " . $result->toString()); - } + } } } - + public function setStatus($value) { $sql = "UPDATE din_order SET order_status_id = $value WHERE " . " id = $this->id"; - + $result = $this->conn->query($sql); if (DB::isError($result) != true){ //notify("Orderstatus oppdatert ($value)"); }else { error("Set status - order: " . $result->toString()); - } + } } public function addOrderItem($data) { - $data['order_id'] = $this->id; - $data['discount'] = 0; + $data['order_id'] = $this->id; + $data['discount'] = 0; + + $added = false; + + // fetch items in order and check if item allready is in order + $items = $this->getItems(); + while ($row =& $items->fetchRow(DB_FETCHMODE_ASSOC)){ + $item = new OrderItem($row['id']); + if ($item->product_id == $data['product_id']) { + // item already exists in order, update quantity + $item->setQuantity($item->quantity + $data[quantity]); + $added = true; + } + } + + // new product id for this order, add order item + if (!$added) { $item = new OrderItem(NULL, $data); $item->store(); - return true; - //$this->_updateTotalPrice(); + } + + return true; } - public + public function _updateTotalPrice(){ } - - public + + public function _retrieveData(){ $sql = "SELECT o.*, os.value AS order_status_value FROM din_order o, din_order_status os @@ -115,21 +132,21 @@ class Order { } public - /*static*/ + /*static*/ function delete($id){ $order = new Order($id); $order->setStatus(5); } - + public function getItems() { $sql = "SELECT oi.id " . "FROM din_order_item oi " . "WHERE oi.order_id = $this->id"; - $result =& $this->conn->query($sql); + $result =& $this->conn->query($sql); return $result; } - + public function calculateTotalAmount() { $items = $this->getItems(); @@ -142,8 +159,8 @@ class Order { } return $amount; } - - public + + public function display(){ $items = $this->getItems(); ?> @@ -152,19 +169,19 @@ class Order { numRows() > 0){?> - +