| 1 |
export default class DeviceIdentifierSequence { |
| 2 |
static uniqueDeviceIdentifierKey = `${odoo.access_token}-unique_device_identifier`; |
| 3 |
|
| 4 |
constructor({ orm }) { |
| 5 |
this.orm = orm; |
| 6 |
this.device_identifier = ""; |
| 7 |
} |
| 8 |
|
| 9 |
get data() { |
| 10 |
const localStorageKey = DeviceIdentifierSequence.uniqueDeviceIdentifierKey; |
| 11 |
return JSON.parse(localStorage.getItem(localStorageKey)); |
| 12 |
} |
| 13 |
|
| 14 |
get identifier() { |
| 15 |
const data = this.data; |
| 16 |
return data.device_identifier; |
| 17 |
} |
| 18 |
|
| 19 |
get unsyncedNumberStack() { |
| 20 |
const data = this.data; |
| 21 |
return data.unsynced_number_stack || []; |
| 22 |
} |
| 23 |
|
| 24 |
get nextNumber() { |
| 25 |
const data = this.data; |
| 26 |
return data.unsynced_number_stack.length |
| 27 |
? data.unsynced_number_stack.sort((a, b) => a - b)[0] |
| 28 |
: data.next_number; |
| 29 |
} |
| 30 |
|
| 31 |
async initialize() { |
| 32 |
const localStorageKey = DeviceIdentifierSequence.uniqueDeviceIdentifierKey; |
| 33 |
const deviceIdentifier = localStorage.getItem(localStorageKey); |
| 34 |
|
| 35 |
if (!deviceIdentifier) { |
| 36 |
const data = await this.orm.call("pos.config", "register_new_device_identifier", [ |
| 37 |
odoo.pos_config_id, |
| 38 |
]); |
| 39 |
|
| 40 |
this.device_identifier = data.device_identifier; |
| 41 |
this.save({ |
| 42 |
device_identifier: data.device_identifier, |
| 43 |
next_number: 1, |
| 44 |
unsynced_number_stack: [], |
| 45 |
}); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
getFirstUnsyncedNumber() { |
| 50 |
const unsyncedNumberStack = this.unsyncedNumberStack; |
| 51 |
const sorted = unsyncedNumberStack.sort((a, b) => a - b); |
| 52 |
if (sorted.length === 0) { |
| 53 |
return null; |
| 54 |
} |
| 55 |
|
| 56 |
this.save({ |
| 57 |
device_identifier: this.data.device_identifier, |
| 58 |
next_number: this.data.next_number, |
| 59 |
unsynced_number_stack: sorted.slice(1), // Remove the first element from the stack |
| 60 |
}); |
| 61 |
|
| 62 |
return sorted[0]; |
| 63 |
} |
| 64 |
|
| 65 |
useNext() { |
| 66 |
const unsyncedNumber = this.getFirstUnsyncedNumber(); |
| 67 |
if (unsyncedNumber) { |
| 68 |
return unsyncedNumber; |
| 69 |
} |
| 70 |
|
| 71 |
const data = this.data; |
| 72 |
const number = data.next_number; |
| 73 |
const newData = { |
| 74 |
device_identifier: data.device_identifier, |
| 75 |
next_number: number + 1, |
| 76 |
unsynced_number_stack: [], // In case of order deletion, its identifier will be added to this stack to be reused later |
| 77 |
}; |
| 78 |
|
| 79 |
this.save(newData); |
| 80 |
return number; |
| 81 |
} |
| 82 |
|
| 83 |
save({ next_number, device_identifier, unsynced_number_stack }) { |
| 84 |
const localStorageKey = DeviceIdentifierSequence.uniqueDeviceIdentifierKey; |
| 85 |
const current = this.data; |
| 86 |
const data = { |
| 87 |
device_identifier: device_identifier || current.device_identifier, |
| 88 |
next_number: next_number || current.next_number, |
| 89 |
unsynced_number_stack: [ |
| 90 |
...new Set(unsynced_number_stack || current.unsynced_number_stack || []), |
| 91 |
], |
| 92 |
}; |
| 93 |
localStorage.setItem(localStorageKey, JSON.stringify(data)); |
| 94 |
} |
| 95 |
|
| 96 |
saveUnusedNumber(orders) { |
| 97 |
const data = this.data; |
| 98 |
if (!data) { |
| 99 |
return; |
| 100 |
} |
| 101 |
const numbers = orders |
| 102 |
.filter((o) => !o.isSynced) |
| 103 |
.map((o) => this.extractNumberFromReference(o.pos_reference)); |
| 104 |
const unsyncedNumberStack = new Set([...data.unsynced_number_stack, ...numbers]); |
| 105 |
|
| 106 |
this.save({ |
| 107 |
device_identifier: data.device_identifier, |
| 108 |
next_number: data.next_number, |
| 109 |
unsynced_number_stack: Array.from(unsyncedNumberStack), |
| 110 |
}); |
| 111 |
} |
| 112 |
|
| 113 |
extractNumberFromReference(reference) { |
| 114 |
return parseInt(reference.split("-")[2]); |
| 115 |
} |
| 116 |
} |