Commit 565034da by Damien Moulard

choose period to calculate sells stats

parent aeba0458
Pipeline #1254 failed with stage
in 1 minute 17 seconds
......@@ -92,17 +92,27 @@
}
#order_forms_container {
margin-top: 30px;
margin-top: 20px;
display: flex;
flex-wrap: wrap;
justify-content: space-evenly;
}
.order_form_item {
margin-top: 10px;
}
#supplier_input {
width: 350px;
border-radius: 3px;
}
#date_planned_input, #coverage_days_input {
#stats_date_period_select {
margin-left: 5px;
min-width: 200px;
}
#date_planned_input, #coverage_days_input, #stats_date_period_select {
border-radius: 3px;
}
......
......@@ -16,6 +16,7 @@ var dbc = null,
order_doc = {
_id: null,
coverage_days: null,
stats_date_period: '',
last_update: {
timestamp: null,
fingerprint: null
......@@ -42,6 +43,7 @@ function reset_data() {
order_doc = {
_id: null,
coverage_days: null,
stats_date_period: '',
last_update : {
timestamp: null,
fingerprint: null
......@@ -81,6 +83,37 @@ function dates_diff(date1, date2) {
return diff;
}
/**
* Compute the date from which to calculate stats of sells,
* depending on the selected parameter.
*
* @returns String value of the date, ISO format
*/
function _compute_stats_date_from() {
let val = '';
if (order_doc.stats_date_period !== '') {
let date = new Date();
switch (order_doc.stats_date_period) {
case '1week':
date.setDate(date.getDate() - 7);
break;
case '2weeks':
date.setDate(date.getDate() - 14);
break;
}
let day = ("0" + date.getDate()).slice(-2);
let month = ("0" + (date.getMonth() +1)).slice(-2);
let year = date.getFullYear();
val = `${year}-${month}-${day}`;
}
return val;
}
/* - PRODUCTS */
/**
......@@ -110,10 +143,15 @@ function add_product() {
return -1;
}
let data = {
pids: [product.tpl_id],
stats_from: _compute_stats_date_from()
}
$.ajax({
type: 'POST',
url: '/products/get_product_for_order_helper',
data: JSON.stringify([product.tpl_id]),
data: JSON.stringify(data),
dataType:"json",
traditional: true,
contentType: "application/json; charset=utf-8",
......@@ -199,7 +237,8 @@ function check_products_data() {
type: 'GET',
url: '/orders/get_supplier_products',
data: {
sids: suppliers_id
sids: suppliers_id,
stats_from: _compute_stats_date_from()
},
dataType:"json",
traditional: true,
......@@ -285,14 +324,14 @@ function add_supplier() {
supplier.total_value = 0;
selected_suppliers.push(supplier);
let url = "/orders/get_supplier_products";
url += "?sids=" + encodeURIComponent(supplier.id);
// Fetch supplier products
$.ajax({
type: 'GET',
url: url,
url: "/orders/get_supplier_products",
data: {
sids: [supplier.id],
stats_from: _compute_stats_date_from()
},
dataType:"json",
traditional: true,
contentType: "application/json; charset=utf-8",
......@@ -1384,7 +1423,6 @@ function display_products(params) {
const prod_id = id_split[1];
const supplier_id = id_split[3];
console.log(val);
if (val == -1) {
let modal_remove_supplier_product_association = $('#templates #modal_remove_supplier_product_association');
......@@ -1640,6 +1678,12 @@ function update_main_screen(params) {
} else {
$("#coverage_days_input").val('');
}
if (order_doc.stats_date_period !== undefined && order_doc.stats_date_period !== null) {
$("#stats_date_period_select").val(order_doc.stats_date_period);
} else {
$("#stats_date_period_select").val('');
}
}
/**
......@@ -1816,6 +1860,22 @@ $(document).ready(function() {
}
});
$("#stats_date_period_select").on("change", function(e) {
e.preventDefault();
if (is_time_to('change_stats_date_period', 1000)) {
openModal();
order_doc.stats_date_period = $(this).val();
check_products_data()
.then(() => {
update_cdb_order();
update_main_screen();
closeModal();
});
}
});
$("#do_inventory").on("click", function() {
if (is_time_to('generate_inventory', 1000)) {
generate_inventory();
......
......@@ -42,7 +42,8 @@ def get_supplier_products(request):
""" Get supplier products """
suppliers_id = request.GET.getlist('sids', '')
res = CagetteProducts.get_products_for_order_helper(suppliers_id)
stats_from = request.GET.get('stats_from')
res = CagetteProducts.get_products_for_order_helper(suppliers_id, [], stats_from)
if 'error' in res:
return JsonResponse(res, status=500)
......
......@@ -472,11 +472,11 @@ class CagetteProducts(models.Model):
return res
@staticmethod
def get_products_for_order_helper(supplier_ids, pids = []):
def get_products_for_order_helper(supplier_ids, pids = [], stats_from = None):
"""
One of the two parameters must be not empty.
Get products by supplier if one or more supplier_id is set.
If supplier_ids is empty, get products specified in pids. In this case, suppliers info won't be fetched.
supplier_ids: Get products by supplier if one or more supplier id is set. If set, pids is ignored.
pids: If set & supplier_ids is None/empty, get products specified in pids. In this case, suppliers info won't be fetched.
stats_from: date from which we should calculate sells stats.
"""
api = OdooAPI()
res = {}
......@@ -522,6 +522,10 @@ class CagetteProducts(models.Model):
# 'from': '2019-04-10',
# 'to': '2019-08-10',
}
if stats_from is not None and stats_from != '':
sales_average_params['from'] = stats_from
sales = CagetteProducts.get_template_products_sales_average(sales_average_params)
if 'list' in sales and len(sales['list']) > 0:
......
......@@ -42,8 +42,10 @@ def get_simple_list(request):
def get_product_for_order_helper(request):
res = {}
try:
pids = json.loads(request.body.decode())
res = CagetteProducts.get_products_for_order_helper(None, pids)
data = json.loads(request.body.decode())
pids = data['pids']
stats_from = data['stats_from']
res = CagetteProducts.get_products_for_order_helper(None, pids, stats_from)
except Exception as e:
coop_logger.error("get_product_for_help_order_line : %s", str(e))
res['error'] = str(e)
......
......@@ -57,14 +57,23 @@
</div>
<div class="txtcenter" id="order_forms_container">
<form action="javascript:;" id="coverage_form">
<form action="javascript:;" id="coverage_form" class="order_form_item">
<input type="number" name="coverage_days" id="coverage_days_input" placeholder="Nb jours de couverture" min="1">
<button type="submit" class='btn--primary'>Calculer les besoins</button>
</form>
<form action="javascript:;" id="supplier_form">
<form action="javascript:;" id="supplier_form" class="order_form_item">
<input type="text" name="supplier" id="supplier_input" placeholder="Rechercher un fournisseur par son nom">
<button type="submit" class='btn--primary'>Ajouter le fournisseur</button>
</form>
<form action="javascript:;" id="stats_date_from_form" class="order_form_item">
<label for="stats_date_period_select">Période de calcul de la conso moyenne </label>
<select name="stats_date_period_select" id="stats_date_period_select">
<option value="">Par défaut</option>
<option value="1week">1 semaine</option>
<option value="2weeks">2 semaines</option>
</select>
</form>
</div>
<div id="suppliers_container"></div>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment