Browse Source

MVC order client v1 + refernce twig and name bug problem

master
Ramdan Katakpawou 2 years ago
parent
commit
07013f918a

BIN
private/webshop.db View File


+ 2
- 2
public/index.php View File

@@ -53,7 +53,7 @@ $app->map(['GET', 'POST'], "/orders/{client}/create", function (Request $request
});

// 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();

if ($args['client']) {
@@ -93,7 +93,7 @@ $app->get('/orders[/{client}]', function (Request $request, Response $response,
$response->getBody()->write('<a href="/customers">Back to customer list</a> <a href="/orders">Back to order list</a>');

return $response;
});
});*/

$app->map(['GET', 'POST'], "/order/{id}/create", function (Request $request, Response $response, array $args) {


+ 24
- 0
src/Controller/Shop.php View File

@@ -7,6 +7,7 @@ use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Container\ContainerInterface;
use Shop\DB\CustomersDB;
use Shop\DB\CustomerOrderDB;

class Shop {
@@ -33,4 +34,27 @@ public function customers(ServerRequestInterface $request, ResponseInterface $re
$response->getBody()->write($render);
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;
}
}

+ 0
- 13
src/DB/ClientOrder.php View File

@@ -1,13 +0,0 @@
<?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

+ 68
- 0
src/DB/CustomerOrderDB.php View File

@@ -0,0 +1,68 @@
<?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

+ 104
- 0
src/Model/CustomerOrder.php View File

@@ -0,0 +1,104 @@
<?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;
}
}

+ 0
- 27
templates/allCustomerOrders.html.twig View File

@@ -1,27 +0,0 @@
{% 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%}

+ 8
- 1
templates/base.html.twig View File

@@ -8,5 +8,12 @@
<body>
<div id="main">{% block main%} {% endblock %}</div>
</body>
<html></html>
<style>
table,
th,
td {
border: 1px solid black;
padding: 5px;
}
</style>
</html>

+ 7
- 10
templates/customerOrders.html.twig View File

@@ -1,26 +1,23 @@
{% extends "base.html.twig" %}
{% block main%}
<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/>
<table>
<tr>
<th>Id</th>
<th>Reference</th>
<th>Customer ID</th>
<th>Subtotal</th>
<th>VAT</th>
<th>Total</th>
</tr>
{% for client in clientOrders%}
{% for customer in customerOrders%}
<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>
{% endfor%}
</table>

Loading…
Cancel
Save