import { showError } from "@nextcloud/dialogs"; import { baseUrl, cur, LoadDT, showDone } from "../modules/mainFunction.mjs"; export class ClientGroupDiscount { /** * * @param myresp instantiate client group discount object */ constructor(myresp) { this.id = myresp.id; this.clientGroupName = ClientGroupDiscount.getClientGroupNameFromClientGroupResponse(myresp); this.productReference = ClientGroupDiscount.getProductReferenceFromClientGroupResponse(myresp); this.htAmount = myresp.ht_amount; this.clientGroupId = myresp.fk_client_group_id; this.productId = myresp.fk_produit_id; } static getProductReferenceFromClientGroupResponse(myresp){ let productReference = '-'; if(myresp.produit_reference != null && myresp.produit_reference.length > 0){ productReference = myresp.produit_reference; } return productReference; } static getClientGroupNameFromClientGroupResponse(myresp){ let clientGroupName = '-'; if(myresp.client_group_name != null && myresp.client_group_name.length > 0){ clientGroupName = myresp.client_group_name; } return clientGroupName; } /** * Get datatable row for a client group discount */ getDTRow() { let clientGroupDiscountRow = [ '
' + this.id + '
', '
' + this.clientGroupName + '
', '
' + this.productReference + '
', '
' + cur.format(this.htAmount) + '
', '
' ]; return clientGroupDiscountRow; } /** * * @param {*} clientGroupDiscountDatatable */ static loadClientGroupDiscountDatatable(clientGroupDiscountDatatable) { var oReq = new XMLHttpRequest(); oReq.open('PROPFIND', baseUrl + '/getClientGroupDiscounts', true); oReq.setRequestHeader("Content-Type", "application/json"); oReq.onload = function(e){ if (this.status == 200) { LoadDT(clientGroupDiscountDatatable, JSON.parse(this.response), ClientGroupDiscount); }else{ showError(this.response); } }; oReq.send(); } /** * * @param {*} dt */ static createDefaultClientGroupDiscount(dt) { var oReq = new XMLHttpRequest(); oReq.open('POST', baseUrl + '/clientGroupDiscount/createDefaultClientGroupDiscount', true); oReq.onload = function(e){ if (this.status == 200) { showDone() ClientGroupDiscount.loadClientGroupDiscountDatatable(dt); }else{ showError(this.response); } }; oReq.send(); } }