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

(44): delivery refactor

parent 107327be
...@@ -4,70 +4,207 @@ window.addEventListener("load", function (e) { ...@@ -4,70 +4,207 @@ window.addEventListener("load", function (e) {
let button = begin_delivery[i]; let button = begin_delivery[i];
let id = button.closest("article").dataset.id; let id = button.closest("article").dataset.id;
button.addEventListener("click", function() { button.addEventListener("click", function () {
let cb = function(data){ let cb = function (data) {
add_message(data["status"], data["data"]); add_message(data["status"], data["data"]);
setTimeout(function () {
document.location.reload()
}, 2000)
}; };
queryJson(document.location, { queryJson(document.location, {
message: "begin-delivery", message: "begin-delivery",
id: id, 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) { window.addEventListener("load", function (e) {
let see_orders = document.getElementsByClassName("see-orders") let see_orders = document.getElementsByClassName("see-orders")
for (let i = see_orders.length - 1; i >= 0; i--) { for (let i = see_orders.length - 1; i >= 0; i--) {
let button = see_orders[i]; let button = see_orders[i];
let id = button.closest("article").dataset.id; let id = button.closest("article").dataset.id;
button.addEventListener("click", function() { button.addEventListener("click", function () {
let cb = function(data){
let popup = new Popup("Commandes de " + button.closest("article").querySelector("h2").innerText); let popup = new Popup("Commandes de " + button.closest("article").querySelector("h2").innerText);
let orders = data["data"]; orders = document.createElement('div');
let table = document.createElement('table'); orders.setAttribute('id', 'orders')
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);
popup.pop(); 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,10 +11,10 @@ ...@@ -11,10 +11,10 @@
{% endblock %} {% endblock %}
{% block main %} {% block main %}
<div class="delivery">
<h1>Livraison</h1> <h1>Livraison</h1>
<script> <script>
var url = new URL(window.location.href); var url = new URL(window.location.href);
console.log(url.searchParams)
function applyFilter() { function applyFilter() {
...@@ -25,28 +25,28 @@ ...@@ -25,28 +25,28 @@
</script> </script>
<form onchange="generateTable()"> <form onchange="generateTable()">
<label for="delivered">Déjà remis</label> <label for="delivered">Déjà remis :</label>
<select id="delivered" <select id="delivered"
onchange="generateTable()"> onchange="generateTable()">
<option value="both">Tous</option> <option value="both">Tous</option>
<option value="true">Oui</option> <option value="true">Oui</option>
<option value="false">Non</option> <option value="false">Non</option>
</select> </select>
<label for="beeing_delivered">Distribution en cours</label> <label for="beeing_delivered">Distribution en cours :</label>
<select id="beeing_delivered" <select id="beeing_delivered"
onchange="generateTable()"> onchange="generateTable()">
<option value="true">Oui</option> <option value="true">Oui</option>
<option value="false">Non</option> <option value="false">Non</option>
<option value="both">Tous</option> <option value="both">Tous</option>
</select> </select>
<label for="hidden">Produits cachés</label> <label for="hidden">Produits cachés :</label>
<select id="hidden" <select id="hidden"
onchange="generateTable()"> onchange="generateTable()">
<option value="true">Oui</option> <option value="true">Oui</option>
<option value="false">Non</option> <option selected value="false">Non</option>
<option value="both">Tous</option> <option value="both">Tous</option>
</select> </select>
<label for="paid">Payé</label> <label for="paid">Payé :</label>
<select id="paid" <select id="paid"
onchange="generateTable()"> onchange="generateTable()">
<option value="true">Oui</option> <option value="true">Oui</option>
...@@ -54,25 +54,25 @@ ...@@ -54,25 +54,25 @@
<option value="both">Tous</option> <option value="both">Tous</option>
</select> </select>
<label for="sort">Tri</label> <label for="sort">Tri :</label>
<select id="sort"> <select id="sort">
<option value="true"> --</option> <option value="none"> --</option>
<option value="false">Personalisation</option> <option value="false">Personalisation</option>
<option value="both">Acheteur</option> <option value="both">Acheteur</option>
<option value="both">Date de commande</option> <option value="date">Date de commande</option>
<option value="both">Option 1</option> <option value="option">Option 1</option>
<option value="both">Option 2</option> <option value="second_option">Option 2</option>
</select> </select>
</form> </form>
<form onblur="generateTable()" <form onblur="generateTable()"
onkeypress="onEnter(event)"> onkeypress="onEnter(event)">
<input placeholder="Email"
type="search"
id="emailContains">
<input placeholder="Produit" <input placeholder="Produit"
type="search" type="search"
id="productContains"> id="productContains">
<input placeholder="Email"
type="search"
id="emailContains">
<input placeholder="Option (valeur exacte)" <input placeholder="Option (valeur exacte)"
type="search" type="search"
id="optionContains"> id="optionContains">
...@@ -108,6 +108,7 @@ ...@@ -108,6 +108,7 @@
} }
</script> </script>
</div>
{% endblock %} {% endblock %}
......
...@@ -15,32 +15,63 @@ ...@@ -15,32 +15,63 @@
{% block main %} {% block main %}
<h1>Gestion des Produits</h1> <h1>Gestion des Produits</h1>
<a href="{% url 'boutique:item_create' %}"><button>Ajouter un produit</button></a> <a href="{% url 'boutique:item_create' %}">
<a href="{% url 'boutique:delivery' %}" ><button>Livraison</button></a> <button>Ajouter un produit</button>
<a href="{% url 'boutique:item_sell' %}"><button>Vente pour autrui</button></a> </a>
<a href="{% url 'boutique:manage_category' %}"><button>Ajouter une catégorie</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/> <br/>
<section class="item-manage-list"> <section class="item-manage-list">
{% for item in object_list %} {% for item in object_list %}
<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 %} {% if item.hidden %}
<article data-id="{{ item.id }}" class="hidden"> class="hidden"
{% else %}
<article data-id="{{ item.id }}">
{% endif %} {% endif %}
>
<img class="image" src="{{ item.get_image_url }}" alt=""> <img class="image" src="{{ item.get_image_url }}" alt="">
<h2>{{ item.name }}</h2> <h2>{{ item.name }}</h2>
<p class="numcommands">{{ item.order_items.count }} commandes</p> <p class="numcommands">Total commandé: {{ item.total_quantity }} </p>
<p class="numcommands">[{{ item.quantity_delivered }}/{{ item.quantity_to_deliver }} distribué(s)]</p>
<div> <div>
<a href="{% url 'boutique:item_edit' pk=item.pk %}" class="delivery_list button tooltip"> <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> <i class="fas fa-edit"><span class="tooltiptext">Modifier</span></i>
</a> </a>
<button class="delivery_list tooltip begin-delivery"><i class="fas fa-paper-plane"></i> <span <button class="delivery_list tooltip begin-delivery"><i class="fas fa-paper-plane"></i> <span
class="tooltiptext">Commencer la livraison</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>
<button class="delivery_list tooltip see-orders"><i class="fas fa-eye"></i> <span class="tooltiptext">Voir les commandes</span> <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> </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> {% 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> </button>
{% endif %}
</div> </div>
</article> </article>
...@@ -52,5 +83,6 @@ ...@@ -52,5 +83,6 @@
{% block scripts %} {% block scripts %}
<script src="{% static 'js/json_request.js' %}"></script> <script src="{% static 'js/json_request.js' %}"></script>
<script src="{% static 'js/popup.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> <script src="{% static 'boutique/js/item_manage.js' %}"></script>
{% endblock %} {% endblock %}
...@@ -12,9 +12,11 @@ from django.contrib.auth.models import AnonymousUser ...@@ -12,9 +12,11 @@ from django.contrib.auth.models import AnonymousUser
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.core.exceptions import SuspiciousOperation from django.core.exceptions import SuspiciousOperation
from django.core.exceptions import ValidationError 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 Q
from django.db.models import QuerySet 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 BaseForm
from django.forms import BaseInlineFormSet from django.forms import BaseInlineFormSet
from django.forms import BaseModelForm from django.forms import BaseModelForm
...@@ -210,7 +212,7 @@ class ItemManageView(PermissionRequiredMixin, ListView): ...@@ -210,7 +212,7 @@ class ItemManageView(PermissionRequiredMixin, ListView):
template_name = "boutique/item_manage.html" template_name = "boutique/item_manage.html"
permission_required = "boutique.management" 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()) req = json.loads(request.read().decode())
pk = req.get("id") pk = req.get("id")
...@@ -221,8 +223,12 @@ class ItemManageView(PermissionRequiredMixin, ListView): ...@@ -221,8 +223,12 @@ class ItemManageView(PermissionRequiredMixin, ListView):
message = req.get("message") message = req.get("message")
if message == "begin-delivery": if message == "begin-delivery":
item_qs.get().order_items.update(beeing_delivered=True) item_qs.get().order_items.filter(order__payment__paid=True).update(beeing_delivered=True)
return JsonResponse({"status": "success", "data": "Bien reçu"}) 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": elif message == "see-orders":
item = item_qs.prefetch_related( item = item_qs.prefetch_related(
...@@ -233,15 +239,22 @@ class ItemManageView(PermissionRequiredMixin, ListView): ...@@ -233,15 +239,22 @@ class ItemManageView(PermissionRequiredMixin, ListView):
"status": "success", "status": "success",
"data": [ "data": [
{ {
"user": str(oi.order.user.profile),
"display_name": str(oi.order.user.profile),
"email": str(oi.order.user.email),
"quantity": oi.quantity, "quantity": oi.quantity,
"paid": oi.order.paid, "quantity_delivered": oi.quantity_delivered,
"customization": oi.customization, "option_label": oi.item.option_label,
"option": oi.option, "option": oi.option,
"second_option_label": oi.item.second_option_label,
"second_option": oi.second_option, "second_option": oi.second_option,
"quantity_delivered": oi.quantity_delivered, "customization": oi.customization,
"paid": oi.order.paid,
"ordered_date": oi.order.ordered_date,
"mean": oi.order.mean,
} }
for oi in item.order_items.all() for oi in item.order_items.all().order_by("-order__ordered_date")
], ],
} }
) )
...@@ -249,7 +262,13 @@ class ItemManageView(PermissionRequiredMixin, ListView): ...@@ -249,7 +262,13 @@ class ItemManageView(PermissionRequiredMixin, ListView):
raise SuspiciousOperation() raise SuspiciousOperation()