1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- <?php
-
- namespace Shop\DB;
-
- use Shop\Model\Order as OrderModel;
- use Shop\Model\OrderCreate as OrderCreate;
- use Shop\DB\myDB;
-
- class Order
- {
- protected $container;
- protected $orderlines = [];
- protected $details = [];
- protected $clients = [];
- protected $product = [];
-
- public function __construct($container) {
- $this->container = $container;
- }
-
- public function getOrderLines($id)
- {
- $db = $this->container->get('db');
- $sql = "SELECT * FROM order_lines o LEFT JOIN products p on o.product_id = p.id where o.order_id = :id;";
- $stmt = $db->prepare($sql);
- $stmt->bindValue(':id', $id, SQLITE3_INTEGER);
- $res = $stmt->execute();
- while ($result = $res->fetchArray(SQLITE3_ASSOC)) {
- $orderLine = new OrderModel($this->container);
- $orderLine->setId($result['id']);
- $orderLine->setName($result['name']);
- $orderLine->setItemnumber($result['itemnumber']);
- $orderLine->setQty($result['qty']);
- $orderLine->setSubtotal($result['subtotal']);
- $orderLine->setVat($result['vat']);
- $orderLine->setTotal($result['total']);
- $this->orderlines[] = $orderLine;
- }
- return $this->orderlines;
- }
-
- public function getOrderDetail($id) {
- $db = $this->container->get('db');
- $stmt = $db->prepare("SELECT * FROM orders WHERE id = :id");
- $stmt->bindValue(':id', $id, SQLITE3_INTEGER);
- $res = $stmt->execute();
- while ($result = $res->fetchArray(SQLITE3_ASSOC)) {
- $detail = new OrderModel($this->container);
- $detail->setReference($result['reference']);
- $this->details[] = $detail;
- }
- return $this->details;
- }
-
- public function getProductDetails($id, $productid, $qty) {
-
- $db = $this->container->get('db');
- $sqlproduct = "select * from products where id = :id;";
- $stmtproduct = $db->prepare($sqlproduct);
- $stmtproduct->bindValue(':id', $productid, SQLITE3_INTEGER);
- $resproduct = $stmtproduct->execute();
- while ($product = $resproduct->fetchArray(SQLITE3_ASSOC)) {
- $order = new OrderModel($this->container);
- $order->setUnitprice($product['unitprice']);
- $order->setVatperc($product['vatperc']);
- $this->product[] = $order;
- }
- $order_id = $id;
- $unitprice = $product['unitprice'];
- $vatperc = $product['vatperc'];
-
- $subtotal = $qty * $unitprice;
- $vat = $subtotal * $vatperc / 100;
- $total = $subtotal + $vat;
-
- $sql = "insert into order_lines (order_id, product_id, qty, subtotal, vat, total) values ($order_id, $productid, $qty, $subtotal, $vat, $total)";
- $db->exec($sql);
-
- $sql = "update orders set total = total + $total, subtotal = subtotal + $subtotal, vat = vat + $vat where id = $order_id";
- $db->exec($sql);
- }
-
- public function getClient($id)
- {
- $db = $this->container->get('db');
- $sql = "SELECT * FROM clients WHERE id = :id;";
- $stmt = $db->prepare($sql);
- $stmt->bindValue(':id', $id, SQLITE3_INTEGER);
- $res = $stmt->execute();
- while ($result = $res->fetchArray(SQLITE3_ASSOC)) {
- $client = new OrderCreate($this->container);
- $client->setFirstname($result['firstname']);
- $client->setLastname($result['lastname']);
- $this->clients[] = $client;
- }
- return $this->clients;
- }
- }
|