}); | }); | ||||
// Route with optional params, see https://www.slimframework.com/docs/v4/objects/routing.html#how-to-create-routes section Optional segments | // Route with optional params, see https://www.slimframework.com/docs/v4/objects/routing.html#how-to-create-routes section Optional segments | ||||
$app->get('/orders[/{client}]', function (Request $request, Response $response, array $args) { | |||||
$app->get('/orders[/{client}]',\Shop\Controller\Shop::class . ':customerOrders'); /*function (Request $request, Response $response, array $args) { | |||||
$db = new DB(); | $db = new DB(); | ||||
if ($args['client']) { | if ($args['client']) { | ||||
$response->getBody()->write('<a href="/customers">Back to customer list</a> <a href="/orders">Back to order list</a>'); | $response->getBody()->write('<a href="/customers">Back to customer list</a> <a href="/orders">Back to order list</a>'); | ||||
return $response; | return $response; | ||||
}); | |||||
});*/ | |||||
$app->map(['GET', 'POST'], "/order/{id}/create", function (Request $request, Response $response, array $args) { | $app->map(['GET', 'POST'], "/order/{id}/create", function (Request $request, Response $response, array $args) { | ||||
use Psr\Http\Message\ServerRequestInterface; | use Psr\Http\Message\ServerRequestInterface; | ||||
use Psr\Container\ContainerInterface; | use Psr\Container\ContainerInterface; | ||||
use Shop\DB\CustomersDB; | use Shop\DB\CustomersDB; | ||||
use Shop\DB\CustomerOrderDB; | |||||
class Shop { | class Shop { | ||||
$response->getBody()->write($render); | $response->getBody()->write($render); | ||||
return $response; | return $response; | ||||
} | } | ||||
public function customerOrders(ServerRequestInterface $request, ResponseInterface $response, array $args){ | |||||
$container = $this->container; | |||||
$twig = $container->get('twig'); | |||||
$db = new CustomerOrderDB($this->container); | |||||
$customerOrders = $db->customerOrders($args); | |||||
$var = ["customerOrders" => $customerOrders]; | |||||
$render = $twig->render('customerOrders.html.twig', $var); | |||||
$response->getBody()->write($render); | |||||
return $response; | |||||
} | |||||
public function nameCustomer(ServerRequestInterface $request, ResponseInterface $response, array $args){ | |||||
$container = $this->container; | |||||
$twig = $container->get('twig'); | |||||
$db = new CustomerOrderDB($this->container); | |||||
$customerName = $db->nameCustomer($args); | |||||
$var = ["customerName" => $customerName]; | |||||
$render = $twig->render('customerOrders.html.twig', $var); | |||||
$response->getBody()->write($render); | |||||
return $response; | |||||
} | |||||
} | } |
<?php | |||||
namespace Shop\DB; | |||||
use Shop\DB\DB; | |||||
class ClientOrder extends DB { | |||||
public function showcart(){ | |||||
$stmt = $this->prepare("SELECT * FROM order_lines o LEFT JOIN products p on o.product_id = p.id where o.order_id"); | |||||
$res = $stmt->execute(); | |||||
return $res;} | |||||
} | |||||
//vergeet args niet zie index route order/id |
<?php | |||||
namespace Shop\DB; | |||||
use Shop\DB\DB; | |||||
use Psr\Container\ContainerInterface; | |||||
use Shop\Model\CustomerOrder; | |||||
class CustomerOrderDB extends DB { | |||||
protected $container; | |||||
protected $customerOrder = []; | |||||
protected $customerName = []; | |||||
public function __construct(ContainerInterface $container) | |||||
{ | |||||
$this->container = $container; | |||||
} | |||||
public function customerOrders($args) | |||||
{ | |||||
$stmt = $this->container->get('db')->prepare | |||||
("SELECT firstname,lastname, o.* FROM orders o | |||||
LEFT JOIN clients c on o.customer_id = c.id | |||||
WHERE o.customer_id =". $args['client']); | |||||
$stmt ->bindValue(':id',SQLITE3_NUM); | |||||
$stmt ->bindValue(':customer',SQLITE3_TEXT); | |||||
$stmt ->bindValue(':reference', SQLITE3_TEXT); | |||||
$stmt ->bindValue(':customer_id', SQLITE3_INTEGER); | |||||
$stmt ->bindValue(':subtotal', SQLITE3_NUM); | |||||
$stmt ->bindValue(':vat', SQLITE3_NUM); | |||||
$stmt ->bindValue(':total', SQLITE3_NUM); | |||||
$res = $stmt->execute(); | |||||
while($result = $res->fetchArray(SQLITE3_ASSOC)){ | |||||
$customerOrder = new CustomerOrder($this->container); | |||||
$customerOrder->setId($result['id']); | |||||
$customerOrder->setRef($result['reference']); | |||||
$customerOrder->setCustomerId($result['customer_id']); | |||||
$customerOrder->setSubtotal($result['subtotal']); | |||||
$customerOrder->setVat($result['vat']); | |||||
$customerOrder->setTotal($result['total']); | |||||
$this->customerOrders[] = $customerOrder; | |||||
} | |||||
return $this->customerOrders; | |||||
} | |||||
public function nameCustomer($args){ | |||||
$stmt2 = $this->container->get('db')->prepare("SELECT * FROM clients WHERE id = " . $args['client']); | |||||
$stmt2 ->bindValue(':firstname', SQLITE3_TEXT); | |||||
$stmt2 ->bindValue(':lastname', SQLITE3_TEXT); | |||||
$res2 = $stmt2->execute(); | |||||
while($result = $res2->fetchArray(SQLITE3_ASSOC));{ | |||||
$customerName = new CustomerOrder($this->container); | |||||
$customerName->setFirstname($result['firstname']); | |||||
$customerName->setLastname($result['lastname']); | |||||
$this->customersName[] = $customerName; | |||||
} | |||||
} | |||||
} | |||||
//vergeet args niet zie index route order/id , gebruik psr container; done |
<?php | |||||
namespace Shop\Model; | |||||
class CustomerOrder { | |||||
protected $id; | |||||
protected $firstname; | |||||
protected $lastname; | |||||
protected $reference; | |||||
protected $customerId; | |||||
protected $subTotal; | |||||
protected $vat; | |||||
protected $total; | |||||
public function __construct($container){ | |||||
$this->container = $container; | |||||
} | |||||
public function setId($value) | |||||
{ | |||||
$this->id = $value; | |||||
} | |||||
public function getId() | |||||
{ | |||||
return $this->id; | |||||
} | |||||
public function setFirstname($value) | |||||
{ | |||||
$this->firstname = $value; | |||||
} | |||||
public function getFirstname() | |||||
{ | |||||
return $this->firstname; | |||||
} | |||||
public function setLastName($value) | |||||
{ | |||||
$this->lastname = $value; | |||||
} | |||||
public function getLastName(){ | |||||
return $this->lastname; | |||||
} | |||||
public function setRef($value) | |||||
{ | |||||
$this->reference = $value; | |||||
} | |||||
public function getRef() | |||||
{ | |||||
return $this->reference; | |||||
} | |||||
public function setCustomerId($value) | |||||
{ | |||||
$this->customerId = $value; | |||||
} | |||||
public function getCustomerId() | |||||
{ | |||||
return $this->customerId; | |||||
} | |||||
public function setSubtotal($value) | |||||
{ | |||||
$this->subTotal = $value; | |||||
} | |||||
public function getSubtotal() | |||||
{ | |||||
return $this->subTotal; | |||||
} | |||||
public function setVat($value) | |||||
{ | |||||
$this->vat = $value; | |||||
} | |||||
public function getVat() | |||||
{ | |||||
return $this->vat; | |||||
} | |||||
public function setTotal($value) | |||||
{ | |||||
$this->total = $value; | |||||
} | |||||
public function getTotal() | |||||
{ | |||||
return $this->total; | |||||
} | |||||
} |
{% extends "base.html.twig" %} | |||||
{% block main%} | |||||
<h1> ORDERS LIST FOR ALL CLIENTS </h1> | |||||
<hr/> | |||||
<table> | |||||
<tr> | |||||
<th>Id</th> | |||||
<th>Reference</th> | |||||
<th>Customer ID</th> | |||||
<th>Subtotal</th> | |||||
<th>VAT</th> | |||||
<th>Total</th> | |||||
</tr> | |||||
{% for order in allOrders%} | |||||
<tr> | |||||
<td><a href="orders/{{order.id}}">{{order.id}}</td> | |||||
<td>{{order.reference}}</td> | |||||
<td>{{order.customer_id}}</td> | |||||
<td>>{{order.subtotal}}</td> | |||||
<td>>{{order.vat}}</td> | |||||
<td>>{{order.total}}</td> | |||||
</tr> | |||||
{% endfor%} | |||||
</table> | |||||
<hr/> | |||||
<a href="/customers">Back to customer list</a> | |||||
{% endblock%} |
<body> | <body> | ||||
<div id="main">{% block main%} {% endblock %}</div> | <div id="main">{% block main%} {% endblock %}</div> | ||||
</body> | </body> | ||||
<html></html> | |||||
<style> | |||||
table, | |||||
th, | |||||
td { | |||||
border: 1px solid black; | |||||
padding: 5px; | |||||
} | |||||
</style> | |||||
</html> | </html> |
{% extends "base.html.twig" %} | {% extends "base.html.twig" %} | ||||
{% block main%} | {% block main%} | ||||
<a href="/order/{id}/create"> Create Order<a/> | <a href="/order/{id}/create"> Create Order<a/> | ||||
<h1> Order list for {{clientOrders.firstname}} {{clientOrders.lastname}}</h1> | |||||
<h1> Order list for {% for customer in CustomerName%}<h1>{{customer.firstname}} {{customer.lastname}}</h1>{% endfor%}</h1> | |||||
<hr/> | <hr/> | ||||
<table> | <table> | ||||
<tr> | <tr> | ||||
<th>Id</th> | |||||
<th>Reference</th> | <th>Reference</th> | ||||
<th>Customer ID</th> | <th>Customer ID</th> | ||||
<th>Subtotal</th> | <th>Subtotal</th> | ||||
<th>VAT</th> | <th>VAT</th> | ||||
<th>Total</th> | <th>Total</th> | ||||
</tr> | </tr> | ||||
{% for client in clientOrders%} | |||||
{% for customer in customerOrders%} | |||||
<tr> | <tr> | ||||
<td><a href="orders/{{order.id}}">{{order.id}}</td> | |||||
<td>{{client.id}}</td> | |||||
<td>{{client.reference}}</td> | |||||
<td>{{client.customerId}}</td> | |||||
<td>>{{client.subtotal}}</td> | |||||
<td>>{{client.vat}}</td> | |||||
<td>>{{client.total}}</td> | |||||
<td><a href="{{customer.reference}}">{{customer.reference}}</td> | |||||
<td>{{customer.customerId}}</td> | |||||
<td>>{{customer.subtotal}}</td> | |||||
<td>>{{customer.vat}}</td> | |||||
<td>>{{customer.total}}</td> | |||||
</tr> | </tr> | ||||
{% endfor%} | {% endfor%} | ||||
</table> | </table> |