public function save(OrderItemModel $orderItem) { try { $sql = "INSERT INTO {$this->dbTable} (" . self::$orderIdColumn . "," . self::$productIdColumn . ") VALUE (?,?)"; $params = array($orderItem->getOrderId(), $orderItem->getProductId()); $query = $this->database->prepare($sql); $query->execute($params); } catch (\PDOException $e) { return false; } }
public function purchaseProducts() { $basket = $this->productView->getAllOrderItems(); //If the quantity of each items in basket is less than what exists in database, it returns true. if ($basket === true) { try { $newCustomer = new \model\CustomerModel($this->productView->getCheckoutSSN(), $this->productView->getCheckoutFirstName(), $this->productView->getCheckoutLastName(), $this->productView->getCheckoutEmail()); } catch (\model\InvalidSSNException $e) { return Messages::$wrongSsn; } catch (\model\InvalidFirstNameException $e) { return Messages::$wrongFirstName; } catch (\model\InvalidLastNameException $e) { return Messages::$wrongLastName; } catch (\model\InvalidEmailException $e) { return Messages::$wrongEmail; } $customerCatalog = new CustomerCatalog(); if ($newCustomer == true) { $customerCatalog->saveNewCustomerInRepository($newCustomer); $customer = $customerCatalog->getCustomerBySsn($newCustomer->getSSN()); //create order on that customer $order = new OrderModel($customer->getId()); $order->saveNewOrderInRepository($order); $getOrder = $order->getLatestOrderByCustomerId($customer->getId()); $products = $this->productView->getProductsToOrder(); foreach ($products as $orderItem) { $orderItemInModel = new OrderItemModel($getOrder->getId(), $orderItem->getId()); $orderItemInModel->saveNewOrderItemInRepository($orderItemInModel); $getOrderItem = $this->productCatalog->getProductById($orderItemInModel->getProductId()); //Reduce quantity on that product $newQuantity = $getOrderItem->getQuantity() - 1; $this->productCatalog->reduceQuantity($orderItem->getId(), $newQuantity); } $this->productView->forgetBasket(); $this->persistentBasketDAL->clearBasket(); return $customer; } // OBS! this code is not used at the moment, not implemented fully. //$customerEmail = $customer->getEmail(); //$administratorEmail = \Settings::ADMIN_EMAIL; //$this->sendCustomerEmail($customer, $getOrder); } else { $this->productView->setMessage(Messages::$orderCouldNotBeCreated); return Messages::$orderCouldNotBeCreated; } }