Commit cd2cb866 authored by HEVELINE Thomas's avatar HEVELINE Thomas Committed by HEVELINE Thomas

(44): delivery refactor

parent 107327be
......@@ -4,70 +4,207 @@ window.addEventListener("load", function (e) {
let button = begin_delivery[i];
let id = button.closest("article").dataset.id;
button.addEventListener("click", function() {
let cb = function(data){
button.addEventListener("click", function () {
let cb = function (data) {
add_message(data["status"], data["data"]);
setTimeout(function () {
document.location.reload()
}, 2000)
};
queryJson(document.location, {
message: "begin-delivery",
id: id,
}, cb, "POST");
}, cb, "OPTIONS");
});
}
});
window.addEventListener("load", function (e) {
let stop_delivery = document.getElementsByClassName("stop-delivery")
for (let i = stop_delivery.length - 1; i >= 0; i--) {
let button = stop_delivery[i];
let id = button.closest("article").dataset.id;
button.addEventListener("click", function () {
let cb = function (data) {
add_message(data["status"], data["data"]);
setTimeout(function () {
document.location.reload()
}, 2000)
};
queryJson(document.location, {
message: "stop-delivery",
id: id,
}, cb, "OPTIONS");
});
}
});
window.addEventListener("load", function (e) {
let see_orders = document.getElementsByClassName("see-orders")
for (let i = see_orders.length - 1; i >= 0; i--) {
let button = see_orders[i];
let id = button.closest("article").dataset.id;
button.addEventListener("click", function() {
let cb = function(data){
button.addEventListener("click", function () {
let popup = new Popup("Commandes de " + button.closest("article").querySelector("h2").innerText);
let orders = data["data"];
let table = document.createElement('table');
table.insertAdjacentHTML("beforeend", "<tr><th>Utilisateur</th><th>Personnalisation</th><th>Opt 1</th><th>Opt 2</th><th>Payé</th><th>Quantité</th></tr>");
for (let i = orders.length - 1; i >= 0; i--) {
let order = orders[i];
let line = document.createElement("tr")
let li = document.createElement('li');
let c1 = document.createElement("td")
let c2 = document.createElement("td")
let c3 = document.createElement("td")
let c4 = document.createElement("td")
let c5 = document.createElement("td")
let c6 = document.createElement("td")
c1.appendChild(document.createTextNode(order.user))
c2.appendChild(document.createTextNode(order.customization))
c3.appendChild(document.createTextNode(order.option))
c4.appendChild(document.createTextNode(order.second_option))
c5.appendChild(document.createTextNode(order.paid))
c6.appendChild(document.createTextNode(order.quantity))
line.appendChild(c1)
line.appendChild(c2)
line.appendChild(c3)
line.appendChild(c4)
line.appendChild(c5)
line.appendChild(c6)
table.appendChild(line);
}
popup.main.appendChild(table);
orders = document.createElement('div');
orders.setAttribute('id', 'orders')
popup.pop();
popup.main.appendChild(orders)
new SeeOrdersList('orders', document.location, id);
};
queryJson(document.location, {
message: "see-orders",
id: id,
}, cb, "POST");
});
}
});
(function () {
var SeeOrdersList = function (containerId, listProvider, id) {
TableList.call(this, containerId);
queryJson(listProvider, {
message: "see-orders",
id: id,
}, this.populate.bind(this));
};
SeeOrdersList.prototype = Object.create(TableList.prototype, {
populate: {
value: function (json) {
this.searchInput.setAttribute('placeholder', 'Acheteur')
this.elems = json['data']
var header = document.createElement('tr');
header.appendChild(document.createElement('th')).appendChild(document.createTextNode('Acheteur'));
header.appendChild(document.createElement('th')).appendChild(document.createTextNode('Personalisation'));
header.appendChild(document.createElement('th')).appendChild(document.createTextNode('Payement'));
header.appendChild(document.createElement('th')).appendChild(document.createTextNode('Remis'));
header.appendChild(document.createElement('th'));
this.header.appendChild(header);
for (var i in this.elems) {
var item = this.elems[i];
let customization = document.createElement('td');
if (item['customization'] !== '') customization.appendChild(document.createElement('div'))
.appendChild(document.createTextNode('Personalisation : ' + item['customization']));
if (item['option_label'] !== '') customization.appendChild(document.createElement('div'))
.appendChild(document.createTextNode(item['option_label'] + ' : ' + item['option']));
if (item['second_option_label'] !== '') customization.appendChild(document.createElement('div'))
.appendChild(document.createTextNode(item['second_option_label'] + ' : ' + item['second_option']));
let payement = document.createElement('td');
if (!item['paid']) {
payement.innerHTML = 'Non payé';
payement.setAttribute('style', 'color: red');
} else {
const mean = {"card": "Carte", "online": "Lydia", "cash": "Espèces", "check": "Chèque"};
const date = new Date(item['ordered_date']);
function pad(s) {
return (s < 10) ? '0' + s : s;
}
payement.innerHTML = (mean[item['mean']] ? mean[item['mean']] : item['mean'])
+ '<br>'
+ [pad(date.getDate()), pad(date.getMonth() + 1), date.getFullYear()].join('/');
}
let buyer = document.createElement('td');
buyer.innerHTML = (item['display_name'] === item['email'] ? item['display_name'] : item['display_name'] + '<br>' + item['email'])
item.element = document.createElement('tr');
item.element.setAttribute('data-id', item['id']);
item.element.setAttribute('data-quantity_ordered', item['quantity']);
item.element.setAttribute('data-quantity_delivered', item['quantity_delivered']);
item.element.setAttribute('data-display_name', item['display_name']);
item.element.setAttribute('data-paid', item['paid']);
item.element.appendChild(buyer);
item.element.appendChild(customization);
item.element.appendChild(payement);
item.element.appendChild(document.createElement('td')).innerHTML = item['quantity_delivered'] + ' / ' + item['quantity'];
this.body.appendChild(item.element);
}
this.matchingElems = this.elems;
console.log(this.matchingElems);
this.matchingElems.sort(function (a, b) {
var nameA = a['display_name'].toLowerCase();
var nameB = b['display_name'].toLowerCase();
if (nameA > nameB) return 1;
else if (nameA < nameB) return -1;
else return 0;
});
this.render();
},
},
match: {
value: function (item) {
var match = false;
var regex = this.cachedSearchRegex;
match |= regex.test(item['display_name']);
return match;
},
},
});
window.SeeOrdersList = SeeOrdersList;
window.SeeOrdersList.ItemAction = Action;
})();
function sell(event) {
const item = event.target.closest('article');
const item_id = item.dataset.pk;
let data = {
"item": item_id,
"user": 0,
"quantity": 1,
"mean": null,
"customization": '',
"option": '',
"second_option": ''
};
let customization_popup = new CustomizationPopup("Options et personalisation", item, function (quantity, customization, option, second_option) {
data.quantity = quantity;
data.customization = customization;
data.option = option;
data.second_option = second_option;
mean_popup.pop();
});
let mean_popup = new SelectionPopup(
"Moyen de paiement",
{"card": "Carte", "online": "Lydia", "cash": "Espèces", "check": "Chèque"},
function (mean) {
data.mean = mean;
console.log(data);
queryJson("/boutique/item/sell", data, function (response) {
add_message(response["status"], response["message"]);
}, "PUT");
});
let user_popup = new UserSelectionPopup("Client", function (uid, dname) {
console.log(uid);
data.user = uid;
customization_popup.pop();
});
user_popup.pop();
};
\ No newline at end of file
......@@ -11,103 +11,104 @@
{% endblock %}
{% block main %}
<h1>Livraison</h1>
<script>
var url = new URL(window.location.href);
console.log(url.searchParams)
function applyFilter() {
url.searchParams.set('delivered', document.getElementById('delivered').checked);
url.searchParams.set('delivery_in_progress', document.getElementById('delivery_in_progress').checked);
window.location.href = url
}
</script>
<form onchange="generateTable()">
<label for="delivered">Déjà remis</label>
<select id="delivered"
onchange="generateTable()">
<option value="both">Tous</option>
<option value="true">Oui</option>
<option value="false">Non</option>
</select>
<label for="beeing_delivered">Distribution en cours</label>
<select id="beeing_delivered"
onchange="generateTable()">
<option value="true">Oui</option>
<option value="false">Non</option>
<option value="both">Tous</option>
</select>
<label for="hidden">Produits cachés</label>
<select id="hidden"
onchange="generateTable()">
<option value="true">Oui</option>
<option value="false">Non</option>
<option value="both">Tous</option>
</select>
<label for="paid">Payé</label>
<select id="paid"
onchange="generateTable()">
<option value="true">Oui</option>
<option value="false">Non</option>
<option value="both">Tous</option>
</select>
<label for="sort">Tri</label>
<select id="sort">
<option value="true"> --</option>
<option value="false">Personalisation</option>
<option value="both">Acheteur</option>
<option value="both">Date de commande</option>
<option value="both">Option 1</option>
<option value="both">Option 2</option>
</select>
</form>
<form onblur="generateTable()"
onkeypress="onEnter(event)">
<input placeholder="Email"
type="search"
id="emailContains">
<input placeholder="Produit"
type="search"
id="productContains">
<input placeholder="Option (valeur exacte)"
type="search"
id="optionContains">
<input placeholder="Personalisation"
type="search"
id="customizationContains">
</form>
<div id="deliveries"></div>
<script>
window.addEventListener('load', generateTable);
function onEnter(event) {
if (event.keyCode === 13) {
blur(event.target)
generateTable()
<div class="delivery">
<h1>Livraison</h1>
<script>
var url = new URL(window.location.href);
function applyFilter() {
url.searchParams.set('delivered', document.getElementById('delivered').checked);
url.searchParams.set('delivery_in_progress', document.getElementById('delivery_in_progress').checked);
window.location.href = url
}
</script>
<form onchange="generateTable()">
<label for="delivered">Déjà remis :</label>
<select id="delivered"
onchange="generateTable()">
<option value="both">Tous</option>
<option value="true">Oui</option>
<option value="false">Non</option>
</select>
<label for="beeing_delivered">Distribution en cours :</label>
<select id="beeing_delivered"
onchange="generateTable()">
<option value="true">Oui</option>
<option value="false">Non</option>
<option value="both">Tous</option>
</select>
<label for="hidden">Produits cachés :</label>
<select id="hidden"
onchange="generateTable()">
<option value="true">Oui</option>
<option selected value="false">Non</option>
<option value="both">Tous</option>
</select>
<label for="paid">Payé :</label>
<select id="paid"
onchange="generateTable()">
<option value="true">Oui</option>
<option value="false">Non</option>
<option value="both">Tous</option>
</select>
<label for="sort">Tri :</label>
<select id="sort">
<option value="none"> --</option>
<option value="false">Personalisation</option>
<option value="both">Acheteur</option>
<option value="date">Date de commande</option>
<option value="option">Option 1</option>
<option value="second_option">Option 2</option>
</select>
</form>
<form onblur="generateTable()"
onkeypress="onEnter(event)">
<input placeholder="Produit"
type="search"
id="productContains">
<input placeholder="Email"
type="search"
id="emailContains">
<input placeholder="Option (valeur exacte)"
type="search"
id="optionContains">
<input placeholder="Personalisation"
type="search"
id="customizationContains">
</form>
<div id="deliveries"></div>
<script>
window.addEventListener('load', generateTable);
function onEnter(event) {
if (event.keyCode === 13) {
blur(event.target)
generateTable()
}
}
function generateTable() {
var params = new URLSearchParams();
params.set('delivered', document.getElementById('delivered').value);
params.set('beeing_delivered', document.getElementById('beeing_delivered').value);
params.set('hidden', document.getElementById('hidden').value);
params.set('paid', document.getElementById('paid').value);
params.set('customizationContains', document.getElementById('customizationContains').value);
params.set('optionContains', document.getElementById('optionContains').value);
params.set('productContains', document.getElementById('productContains').value);
params.set('emailContains', document.getElementById('emailContains').value);
var orderedItemList = new OrderedItemList('deliveries', '/boutique/delivery?' + params.toString());
}
}
function generateTable() {
var params = new URLSearchParams();
params.set('delivered', document.getElementById('delivered').value);
params.set('beeing_delivered', document.getElementById('beeing_delivered').value);
params.set('hidden', document.getElementById('hidden').value);
params.set('paid', document.getElementById('paid').value);
params.set('customizationContains', document.getElementById('customizationContains').value);
params.set('optionContains', document.getElementById('optionContains').value);
params.set('productContains', document.getElementById('productContains').value);
params.set('emailContains', document.getElementById('emailContains').value);
var orderedItemList = new OrderedItemList('deliveries', '/boutique/delivery?' + params.toString());
}
</script>
</script>
</div>
{% endblock %}
......
......@@ -15,35 +15,66 @@
{% block main %}
<h1>Gestion des Produits</h1>
<a href="{% url 'boutique:item_create' %}"><button>Ajouter un produit</button></a>
<a href="{% url 'boutique:delivery' %}" ><button>Livraison</button></a>
<a href="{% url 'boutique:item_sell' %}"><button>Vente pour autrui</button></a>
<a href="{% url 'boutique:manage_category' %}"><button>Ajouter une catégorie</button></a>
<a href="{% url 'boutique:item_create' %}">
<button>Ajouter un produit</button>
</a>
<a href="{% url 'boutique:delivery' %}">
<button>Distribution</button>
</a>
<a href="{% url 'boutique:item_sell' %}">
<button>Vente en directe</button>
</a>
<a href="{% url 'boutique:manage_category' %}">
<button>Ajouter une catégorie</button>
</a>
<br/>
<section class="item-manage-list">
{% for item in object_list %}
{% if item.hidden %}
<article data-id="{{ item.id }}" class="hidden">
{% else %}
<article data-id="{{ item.id }}">
{% endif %}
<img class="image" src="{{ item.get_image_url }}" alt="">
<h2>{{ item.name }}</h2>
<p class="numcommands">{{ item.order_items.count }} commandes</p>
<div>
<a href="{% url 'boutique:item_edit' pk=item.pk %}" class="delivery_list button tooltip">
<i class="fas fa-edit"><span class="tooltiptext">Modifier</span></i>
</a>
<button class="delivery_list tooltip begin-delivery"><i class="fas fa-paper-plane"></i> <span
class="tooltiptext">Commencer la livraison</span>
</button>
<button class="delivery_list tooltip see-orders"><i class="fas fa-eye"></i> <span class="tooltiptext">Voir les commandes</span>
</button>
<button class="delivery_list tooltip dl-orders" onclick="window.location.href = '{% url 'boutique:csvexport' pk=item.id %}'"><i class="fas fa-download"></i> <span class="tooltiptext">Voir les commandes</span>
</button>
</div>
</article>
<article data-id="{{ item.id }}"
data-pk="{{ item.pk }}"
data-customization="{{ item.customization }}"
data-optionLabel="{{ item.option_label }}"
data-optionChoices="{{ item.option }}"
data-secondOptionLabel="{{ item.second_option_label }}"
data-secondOptionChoices="{{ item.second_option }}"
data-customizable="{{ item.customizable }}"
data-special="{{ item.special }}"
{% if item.hidden %}
class="hidden"
{% endif %}
>
<img class="image" src="{{ item.get_image_url }}" alt="">
<h2>{{ item.name }}</h2>
<p class="numcommands">Total commandé: {{ item.total_quantity }} </p>
<p class="numcommands">[{{ item.quantity_delivered }}/{{ item.quantity_to_deliver }} distribué(s)]</p>
<div>
<a href="{% url 'boutique:item_edit' pk=item.pk %}" class="delivery_list button tooltip">
<i class="fas fa-edit"><span class="tooltiptext">Modifier</span></i>
</a>
<button class="delivery_list tooltip begin-delivery"><i class="fas fa-paper-plane"></i> <span
class="tooltiptext">Commencer la distribution des articles commandés</span>
</button>
<button class="delivery_list tooltip stop-delivery"><i class="fas fa-ban"></i> <span
class="tooltiptext">Arrêter la distribution des articles</span>
</button>
<button class="delivery_list tooltip see-orders"><i class="fas fa-eye"></i> <span
class="tooltiptext">Voir les commandes</span>
</button>
<button class="delivery_list tooltip dl-orders"
onclick="window.location.href = '{% url 'boutique:csvexport' pk=item.id %}'"><i
class="fas fa-download"></i> <span class="tooltiptext">Exporter les commandes</span>
</button>
{% if perms.boutique.sell %}
<button class="delivery_list tooltip" onclick="sell(event)"><i class="fas fa-hand-holding-usd"></i> <span
class="tooltiptext">Vente en direct</span>
</button>
{% endif %}
</div>
</article>
{% endfor %}
</section>
......@@ -52,5 +83,6 @@
{% block scripts %}
<script src="{% static 'js/json_request.js' %}"></script>
<script src="{% static 'js/popup.js' %}"></script>
<script src="{% static 'js/table_list.js' %}"></script>
<script src="{% static 'boutique/js/item_manage.js' %}"></script>
{% endblock %}
......@@ -12,9 +12,11 @@ from django.contrib.auth.models import AnonymousUser
from django.contrib.auth.models import User
from django.core.exceptions import SuspiciousOperation
from django.core.exceptions import ValidationError
from django.db.models import F
from django.db.models import F, Value
from django.db.models import Q
from django.db.models import QuerySet
from django.db.models.aggregates import Sum
from django.db.models.functions import Coalesce
from django.forms import BaseForm
from django.forms import BaseInlineFormSet
from django.forms import BaseModelForm
......@@ -210,7 +212,7 @@ class ItemManageView(PermissionRequiredMixin, ListView):
template_name = "boutique/item_manage.html"
permission_required = "boutique.management"
def post(self, request: HttpRequest, *args, **kwargs) -> JsonResponse:
def options(self, request: HttpRequest, *args, **kwargs) -> JsonResponse:
req = json.loads(request.read().decode())
pk = req.get("id")
......@@ -221,8 +223,12 @@ class ItemManageView(PermissionRequiredMixin, ListView):
message = req.get("message")
if message == "begin-delivery":
item_qs.get().order_items.update(beeing_delivered=True)
return JsonResponse({"status": "success", "data": "Bien reçu"})
item_qs.get().order_items.filter(order__payment__paid=True).update(beeing_delivered=True)
return JsonResponse({"status": "success", "data": "Début des livraisons des articles déjà commandé"})
if message == "stop-delivery":
item_qs.get().order_items.update(beeing_delivered=False)
return JsonResponse({"status": "success", "data": "Arrêt des livraisons des articles"})
elif message == "see-orders":
item = item_qs.prefetch_related(
......@@ -233,15 +239,22 @@ class ItemManageView(PermissionRequiredMixin, ListView):
"status": "success",
"data": [
{
"user": str(oi.order.user.profile),
"display_name": str(oi.order.user.profile),
"email": str(oi.order.user.email),
"quantity": oi.quantity,
"paid": oi.order.paid,
"customization": oi.customization,
"quantity_delivered": oi.quantity_delivered,
"option_label": oi.item.option_label,
"option": oi.option,
"second_option_label": oi.item.second_option_label,
"second_option": oi.second_option,
"quantity_delivered": oi.quantity_delivered,
"customization": oi.customization,
"paid": oi.order.paid,
"ordered_date": oi.order.ordered_date,