@@ -78,55 +78,8 @@ $app->get('/orders[/{client}]', function (Request $request, Response $response, | |||
return $response; | |||
}); | |||
$app->map(['GET', 'POST'], "/order/{id}/create", function (Request $request, Response $response, array $args) { | |||
if ($request->getMethod() == "GET") { | |||
$db2 = new myDB(); | |||
$stmt2 = $db2->prepare("SELECT * FROM orders WHERE id = " . $args['id']); | |||
$res2 = $stmt2->execute(); | |||
$a = $res2->fetchArray(SQLITE3_ASSOC); | |||
$response->getBody()->write("<h1>Create orderline for" . $a['reference'] . "</h1><hr/>"); | |||
$response->getBody()->write('<form action="/order/' . $args['id'] . '/create" method="post">'); | |||
$response->getBody()->write('<label for="id">product: </label>'); | |||
$response->getBody()->write('<input type="text" name="id">'); | |||
$response->getBody()->write('<label for="qty">qty: </label>'); | |||
$response->getBody()->write('<input type="text" name="qty">'); | |||
$response->getBody()->write('<input type="submit">'); | |||
$response->getBody()->write('</form>'); | |||
$response->getBody()->write('<hr/>'); | |||
$response->getBody()->write('<a href="/customers">Back to customer list</a> <a href="/orders">Back to order list</a>'); | |||
return $response; | |||
} else { | |||
$productid = $request->getParsedBody()['id']; | |||
$qty = $request->getParsedBody()['qty']; | |||
$db2 = new myDB(); | |||
// vraag details van het product op | |||
$sqlproduct = "select * from products where id = " . $productid; | |||
$stmtproduct = $db2->prepare($sqlproduct); | |||
$resproduct = $stmtproduct->execute(); | |||
$product = $resproduct->fetchArray(SQLITE3_ASSOC); | |||
$order_id = $args['id']; | |||
$unitprice = $product['unitprice']; | |||
$vatperc = $product['vatperc']; | |||
// bereken de prijs van de lijn | |||
$subtotal = $qty * $unitprice; | |||
$vat = $subtotal * $vatperc / 100; | |||
$total = $subtotal + $vat; | |||
// voeg orderlijn toe | |||
$sql = "insert into order_lines (order_id, product_id, qty, subtotal, vat, total) values ($order_id, $productid, $qty, $subtotal, $vat, $total)"; | |||
$db2->exec($sql); | |||
// update het order met nieuwe totalen | |||
$sql = "update orders set total = total + $total, subtotal = subtotal + $subtotal, vat = vat + $vat where id = $order_id"; | |||
$db2->exec($sql); | |||
return $response->withHeader('Location', '/order/' . $args['id'] . '/create'); | |||
}; | |||
}); | |||
$app->get("/order/{id}/create", \Shop\Controller\OrderCreate::class . ':createGet'); | |||
$app->post("/order/{id}/create", \Shop\Controller\OrderCreate::class . ':createPost'); | |||
$app->get('/order/{id}', \Shop\Controller\OrderID::class . ':orderID'); | |||
@@ -0,0 +1,45 @@ | |||
<?php | |||
namespace Shop\Controller; | |||
use Psr\Http\Message\ResponseInterface; | |||
use Psr\Http\Message\ServerRequestInterface; | |||
use Psr\Container\ContainerInterface; | |||
use Shop\DB\Order; | |||
class OrderCreate | |||
{ | |||
protected $container; | |||
public function __construct(ContainerInterface $container){ | |||
$this->container = $container; | |||
} | |||
public function createGet(ServerRequestInterface $request, ResponseInterface $response, array $args) | |||
{ | |||
$container = $this->container; | |||
$twig = $container->get('twig'); | |||
$db = new Order($container); | |||
$order = $db->getOrderDetail($args['id']); | |||
$vars = ["order" => $order, "args" => $args['id']]; | |||
$a = $twig->render('ordercreate.html.twig', $vars); | |||
$response->getBody()->write($a); | |||
return $response; | |||
} | |||
public function createPost(ServerRequestInterface $request, ResponseInterface $response, array $args) | |||
{ | |||
$container = $this->container; | |||
$twig = $container->get('twig'); | |||
$db = new Order($container); | |||
$productid = $request->getParsedBody()['id']; | |||
$qty = $request->getParsedBody()['qty']; | |||
$order = $db->getProductDetails($args['id'], $productid, $qty); | |||
return $response->withHeader('Location', '/order/' . $args['id'] . '/create'); | |||
} | |||
} |
@@ -12,6 +12,7 @@ class Order | |||
protected $orderlines = []; | |||
protected $details = []; | |||
protected $clients = []; | |||
protected $product = []; | |||
public function __construct($container) { | |||
$this->container = $container; | |||
@@ -51,6 +52,34 @@ class Order | |||
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'); |
@@ -16,6 +16,8 @@ class Order | |||
protected $subtotal; | |||
protected $vat; | |||
protected $total; | |||
protected $unitprice; | |||
protected $vatperc; | |||
public function __container($container) { | |||
$this->container = $container; | |||
@@ -61,6 +63,12 @@ class Order | |||
$this->total = $value; | |||
} | |||
public function setUnitprice($value) { | |||
$this->unitprice = $value; | |||
} | |||
public function setVatperc($value) { | |||
$this->vatperc = $value; | |||
} | |||
} |
@@ -0,0 +1,15 @@ | |||
{% extends "base.html.twig" %} {% block main %} | |||
<h1>Create orderline for {{ order.reference }}</h1> | |||
<hr /> | |||
<form action="/order/{{ args }}/create" method="post"> | |||
<label for="id">product: </label> | |||
<input type="text" name="id" /> | |||
<label for="qty">qty: </label> | |||
<input type="text" name="qty" /> | |||
<input type="submit" /> | |||
</form> | |||
<hr /> | |||
<a href="/customers">Back to customer list</a> | |||
<a href="/orders">Back to order list</a> | |||
{% endblock %} |