zet-lib 3.1.1 → 3.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -53,12 +53,13 @@ router.get('/', async function (req, res, next) {
53
53
  }
54
54
  })
55
55
  if(copyMenu){
56
- copyMenu.json = JSON.stringify(copyMenu.json);
56
+ copyMenu.json = '[]';
57
57
  copyMenu.updated_by = res.locals.userId;
58
58
  copyMenu.created_by = res.locals.userId;
59
59
  copyMenu.created_at = Util.now();
60
60
  copyMenu.updated_at = Util.now();
61
61
  copyMenu.company_id = companyId;
62
+ company.name = 'Default';
62
63
  delete copyMenu.id;
63
64
  await connection.insert({
64
65
  table : "zmenu",
package/lib/zRoute.js CHANGED
@@ -3766,7 +3766,9 @@ zRoute.viewForm = (
3766
3766
  obj[key].value = data[key] ? Util.formatNumber(data[key], ".") : "";
3767
3767
  break;
3768
3768
  case "money":
3769
- obj[key].value = data[key] ? `${widgets[key].symbol} ${Util.formatNumber(data[key], ".")}` : "";
3769
+ // Set value in standard format (number with dot as decimal separator)
3770
+ // Let formatCurrency handle the formatting
3771
+ obj[key].value = data[key] ? `${widgets[key].symbol || ""} ${Util.formatNumber(data[key], widgets[key].thousandSeparator)}` : "";
3770
3772
  break;
3771
3773
  case "typeahead":
3772
3774
  obj[key].type = "text";
@@ -4277,6 +4279,50 @@ zRoute.generateJS = (req, res, MYMODEL, relations, zForms = "", data = {}) => {
4277
4279
  if(moneys.length > 0) {
4278
4280
  let moneyScript = ``
4279
4281
  moneys.map((money) => {
4282
+ // Clean and normalize the value to standard format (number with dot as decimal separator)
4283
+ // This must be done BEFORE formatCurrency is called
4284
+ moneyScript += `var ${money}Val = $('#${money}').val();`
4285
+ moneyScript += `if(${money}Val) {`
4286
+ // Remove all non-numeric characters (symbols, spaces, etc)
4287
+ moneyScript += ` ${money}Val = ${money}Val.toString().replace(/[^0-9.,-]/g, '');`
4288
+ // Normalize to standard format based on thousand separator configuration
4289
+ moneyScript += ` if('${widgets[money].thousandSeparator || "."}' === '.') {`
4290
+ // Thousand separator is ".", so "," is decimal separator
4291
+ // If value has multiple dots or comma, it's already formatted - normalize it
4292
+ moneyScript += ` var dotCount = (${money}Val.match(/\\./g) || []).length;`
4293
+ moneyScript += ` var commaCount = (${money}Val.match(/,/g) || []).length;`
4294
+ moneyScript += ` if(dotCount > 1 || commaCount > 0) {`
4295
+ // Remove all dots (thousand separators) and replace comma with dot (decimal separator)
4296
+ moneyScript += ` ${money}Val = ${money}Val.replace(/\\./g, '').replace(',', '.');`
4297
+ moneyScript += ` }`
4298
+ // If dotCount === 1 and commaCount === 0, value is already in standard format, keep it
4299
+ moneyScript += ` } else {`
4300
+ // Thousand separator is ",", so "." is decimal separator
4301
+ moneyScript += ` var commaCount = (${money}Val.match(/,/g) || []).length;`
4302
+ moneyScript += ` if(commaCount > 1) {`
4303
+ // Multiple commas means formatted - remove all commas (thousand separators)
4304
+ moneyScript += ` ${money}Val = ${money}Val.replace(/,/g, '');`
4305
+ moneyScript += ` }`
4306
+ // If commaCount <= 1, value is already in standard format, keep it
4307
+ moneyScript += ` }`
4308
+ // Convert to number first to ensure we have a valid number, then back to string
4309
+ moneyScript += ` var ${money}Num = parseFloat(${money}Val);`
4310
+ moneyScript += ` if(!isNaN(${money}Num)) {`
4311
+ // Always use toFixed to ensure proper decimal format
4312
+ moneyScript += ` var ${money}Decimals = ${widgets[money].digitDecimal || 0};`
4313
+ moneyScript += ` ${money}Val = ${money}Num.toFixed(${money}Decimals);`
4314
+ // If thousand separator is ".", formatCurrency expects comma as decimal separator
4315
+ // So we need to replace dot with comma before formatCurrency processes it
4316
+ moneyScript += ` if('${widgets[money].thousandSeparator || "."}' === '.') {`
4317
+ moneyScript += ` ${money}Val = ${money}Val.replace('.', ',');`
4318
+ moneyScript += ` }`
4319
+ moneyScript += ` } else {`
4320
+ moneyScript += ` ${money}Val = '';`
4321
+ moneyScript += ` }`
4322
+ // Set the normalized value back to input
4323
+ moneyScript += ` $('#${money}').val(${money}Val);`
4324
+ moneyScript += `}`
4325
+ // Now formatCurrency will format the standard format value correctly
4280
4326
  moneyScript += `$('#${money}').formatCurrency({
4281
4327
  symbol: '${widgets[money].symbol || "" }',
4282
4328
  decimalSymbol: '${widgets[money].decimalPlaces || "," }',
package/lib/zdataTable.js CHANGED
@@ -20,10 +20,23 @@ class dataTable {
20
20
  this.types = {}
21
21
  this.roles = myCache.get('ROLES')[datas.zuser.role_id] || {}
22
22
  this.level_approval = this.roles.approvals && this.roles.approvals[datas.routeName] ? this.roles.approvals[datas.routeName] : {}
23
- this.hasLevels = Object.keys(this.level_approval).length == 0 ? false : true
23
+ this.hasLevels = Object.keys(this.level_approval).length > 0
24
24
  this.visiblesKey = this.visibles.map((item) => item.key)
25
25
  this.gridType = datas.gridType || 1
26
26
  this.additionalCss = ''
27
+ this.moneyAttributes = {}
28
+ this.hasMoney = false
29
+ if (this.MYMODEL && this.MYMODEL.widgets) {
30
+ const widgets = this.MYMODEL.widgets
31
+ for (let i = 0; i < this.visiblesKey.length; i++) {
32
+ const item = this.visiblesKey[i]
33
+ const widget = widgets[item]
34
+ if (widget && widget.name == 'money') {
35
+ this.moneyAttributes[item] = widget
36
+ this.hasMoney = true
37
+ }
38
+ }
39
+ }
27
40
  if (this.gridType == 1) {
28
41
  this.dataTableScript = '/modules/datatable/normal.js'
29
42
  } else if (this.gridType == 2) {
@@ -54,7 +67,26 @@ class dataTable {
54
67
  this.relations = obj.relations
55
68
  this.routeName = this.MYMODEL.routeName
56
69
  this.types = obj.TYPES
57
- this.totalFields = this.visiblesKey.filter((item) => this.MYMODEL.widgets[item] && this.MYMODEL.widgets[item].name == 'number')
70
+ this.totalFields = []
71
+ this.moneyAttributes = {}
72
+ this.hasMoney = false
73
+ if (this.MYMODEL && this.MYMODEL.widgets) {
74
+ const widgets = this.MYMODEL.widgets
75
+ for (let i = 0; i < this.visiblesKey.length; i++) {
76
+ const item = this.visiblesKey[i]
77
+ const widget = widgets[item]
78
+ if (widget) {
79
+ const widgetName = widget.name
80
+ if (widgetName == 'number' || widgetName == 'money') {
81
+ this.totalFields.push(item)
82
+ }
83
+ if (widgetName == 'money') {
84
+ this.moneyAttributes[item] = widget
85
+ this.hasMoney = true
86
+ }
87
+ }
88
+ }
89
+ }
58
90
  delete obj.MYMODEL
59
91
  delete obj.relations
60
92
  delete obj.TYPES
@@ -72,9 +104,10 @@ class dataTable {
72
104
  if (this.setColumns) return this.setColumns
73
105
  let html = ''
74
106
  if (Array.isArray(this.visibles)) {
75
- this.visibles.map((item) => {
107
+ for (let i = 0; i < this.visibles.length; i++) {
108
+ const item = this.visibles[i]
76
109
  html += `<th id="data_${item.key}_label">${item.name || ''}</th>`
77
- })
110
+ }
78
111
  } else {
79
112
  for (const key in this.visibles) {
80
113
  html += `<th id="data_${key}_label">${this.visibles[key]}</th>`
@@ -87,9 +120,10 @@ class dataTable {
87
120
  get columnsFilter() {
88
121
  let html = '<tr class="filters">'
89
122
  if (Array.isArray(this.visibles)) {
90
- this.visibles.map((item) => {
123
+ for (let i = 0; i < this.visibles.length; i++) {
124
+ const item = this.visibles[i]
91
125
  html += `<th id="data_${item.key}_label">${this.searchColumns[item.key]}</th>`
92
- })
126
+ }
93
127
  } else {
94
128
  for (const key in this.visibles) {
95
129
  html += `<th id="data_${key}">${this.searchColumns[key]}</th>`
@@ -193,6 +227,9 @@ class dataTable {
193
227
  script += `var dataTableTypes = ${JSON.stringify(this.types, null, 2)};${Util.newLine}`
194
228
  script += `var dataTableRoute = "${this.routeName}";${Util.newLine}`
195
229
  script += `var dataTableHasTotalFields = ${JSON.stringify(this.totalFields)};${Util.newLine}`
230
+ if(this.hasMoney) {
231
+ script += `var dataTableMoneyAttributes = ${JSON.stringify(this.moneyAttributes, null, 2)};${Util.newLine}`
232
+ }
196
233
  script += `</script>${Util.newLine}`
197
234
  script += `<script type="text/javascript" src="${this.dataTableScript}"></script>${Util.newLine}`
198
235
  if (this.searchColumns.FILTERKEY) {
@@ -202,9 +239,10 @@ class dataTable {
202
239
 
203
240
  //additional script for typeahead in grid
204
241
  let typeaheadScript = ''
205
- for (let key in this.MYMODEL.widgets) {
206
- if (this.MYMODEL.widgets[key].name == 'typeahead') {
207
- typeaheadScript += `if($("#${key}").val()){
242
+ if (this.MYMODEL && this.MYMODEL.widgets) {
243
+ for (let key in this.MYMODEL.widgets) {
244
+ if (this.MYMODEL.widgets[key].name == 'typeahead') {
245
+ typeaheadScript += `if($("#${key}").val()){
208
246
  setTimeout(()=>{
209
247
  ajaxPost('/ztypeaheadpost/${this.MYMODEL.table}/${key}', {
210
248
  id: $("#${key}").val()
@@ -217,11 +255,13 @@ class dataTable {
217
255
  },1500)
218
256
  };
219
257
  `
258
+ }
220
259
  }
221
260
  }
222
261
  if (typeaheadScript) {
223
262
  script += `<script type="text/javascript">$(function () {${typeaheadScript}})</script>${Util.newLine}`
224
263
  }
264
+
225
265
  if (this.hasLevels) {
226
266
  //modal-body-approval
227
267
  let APPROVAL_LEVELS = myCache.get('APPROVAL_LEVELS')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zet-lib",
3
- "version": "3.1.1",
3
+ "version": "3.1.3",
4
4
  "description": "zet is a library that part of zet generator.",
5
5
  "engines": {
6
6
  "node": ">=18"