import { showSuccess, showError } from "@nextcloud/dialogs";
import { translate as t, translatePlural as n } from '@nextcloud/l10n'
import { baseUrl, showLoader , cur} from "../mainFunction.mjs";
/**
* Update order date
* @param orderId
* @param dateValue
*/
export function updateOrderDate(orderId,dateValue) {
var payload = {
orderDate: dateValue
};
$.ajax({
url: baseUrl + '/order/'+orderId+'/updateDate',
type: 'PUT',
async: false,
contentType: 'application/json',
data: JSON.stringify(payload)
}).done(function (response, code) {
showSuccess(t('gestion', 'Succès'));
}).fail(function (response, code) {
showError(t('gestion', 'Erreur dans la mise à jour de la date du commande'));
});
}
/**
* Get order products
*/
export function getOrderItemsByOrderId(orderId) {
$.ajax({
url: baseUrl + '/order/'+orderId+'/items',
type: 'PROPFIND',
async: false,
contentType: 'application/json'
}).done(function (response, code) {
$('#orderItems tbody').empty();
$.each(JSON.parse(response), function (arrayID, myresp) {
$('#orderItems tbody').append(
'
' +
'| '+
''+
' ' + myresp.order_product_reference + ' | ' +
'' + myresp.order_product_label + ' | ' +
'' + myresp.order_item_quantity + ' | ' +
'' + cur.format(myresp.order_product_ht_amount) + ' | ' +
'' + cur.format((myresp.order_item_quantity * myresp.order_product_ht_amount)) + ' |
');
});
$("#totalOrderItems tbody").empty();
getTotalAmountOfOrder(orderId);
}).fail(function (response, code) {
showError(response);
});
}
/**
*
* @param {*} orderId
*/
export function getTotalAmountOfOrder(orderId) {
$.ajax({
url: baseUrl + '/order/'+orderId+'/totalAmount',
type: 'PROPFIND',
contentType: 'application/json'
}).done((function (totalAmountResult) {
var totalAmount = JSON.parse(totalAmountResult);
$('#totalOrderItems tbody').empty();
$('#totalOrderItems tbody').append(
''+
'| ' + cur.format(totalAmount.totalHt)+' | '+
'' + totalAmount.tva + ' % | '+
'' + cur.format(Math.round((totalAmount.totalHt * totalAmount.tva)) / 100) + ' | '+
'' + cur.format(Math.round((totalAmount.totalHt * (totalAmount.tva + 100))) / 100) + ' |
');
}));
}
/**
*
* @param {*} select
* @param {*} orderItemId
* @param {*} orderProductId
*/
export function loadOrderProductIntoSelect(selectSelector, orderItemId, orderProductId,orderId) {
$.ajax({
url: baseUrl + '/orderProduct/list',
type: 'PROPFIND',
contentType: 'application/json'
}).done(function (response) {
selectSelector.append('');
$.each(JSON.parse(response), function (arrayID, myresp) {
var selected = "";
if (orderProductId == myresp.id) {
selected = "selected";
}
selectSelector.append('');
});
}).fail(function (response, code) {
showError(response);
});
}