Line 0
Link Here
|
|
|
1 |
<template> |
2 |
<router-link :to="{ name: 'CirculationTriggersList' }" class="close_modal"> |
3 |
<i class="fa fa-fw fa-close"></i> |
4 |
</router-link> |
5 |
<div v-if="initialized" class="modal-content"> |
6 |
<form @submit="addCircRule($event)"> |
7 |
<div class="modal-header"> |
8 |
<h1 v-if="!editMode">{{ $__("Add circulation trigger") }}</h1> |
9 |
<h1 v-else>{{ $__("Edit circulation trigger") }}</h1> |
10 |
</div> |
11 |
<div class="modal-body"> |
12 |
<div class="page-section bg-info" v-if="circRules.length"> |
13 |
<h2>{{ $__("Trigger context") }}</h2> |
14 |
<TriggerContext :ruleInfo="ruleInfo" /> |
15 |
<h2>{{ $__("Existing rules") }}</h2> |
16 |
<p>{{ $__("Notice") }} {{ " " + newTriggerNumber - 1 }}</p> |
17 |
<TriggersTable |
18 |
:circRules="circRules" |
19 |
:triggerNumber="newTriggerNumber - 1" |
20 |
:modal="true" |
21 |
:ruleBeingEdited="ruleBeingEdited" |
22 |
/> |
23 |
</div> |
24 |
<fieldset class="rows"> |
25 |
<ol> |
26 |
<li> |
27 |
<label for="library_id" class="required" |
28 |
>{{ $__("Library") }}:</label |
29 |
> |
30 |
<v-select |
31 |
id="library_id" |
32 |
v-model="circRuleTrigger.library_id" |
33 |
label="name" |
34 |
:reduce="lib => lib.library_id" |
35 |
:options="libraries" |
36 |
@update:modelValue="handleContextChange($event)" |
37 |
:disabled="editMode ? true : false" |
38 |
> |
39 |
<template #search="{ attributes, events }"> |
40 |
<input |
41 |
:required="!circRuleTrigger.library_id" |
42 |
class="vs__search" |
43 |
v-bind="attributes" |
44 |
v-on="events" |
45 |
/> |
46 |
</template> |
47 |
</v-select> |
48 |
<span class="required">{{ $__("Required") }}</span> |
49 |
</li> |
50 |
<li> |
51 |
<label for="patron_category_id" class="required" |
52 |
>{{ $__("Patron category") }}:</label |
53 |
> |
54 |
<v-select |
55 |
id="patron_category_id" |
56 |
v-model="circRuleTrigger.patron_category_id" |
57 |
label="name" |
58 |
:reduce="cat => cat.patron_category_id" |
59 |
:options="categories" |
60 |
@update:modelValue="handleContextChange($event)" |
61 |
:disabled="editMode ? true : false" |
62 |
> |
63 |
<template #search="{ attributes, events }"> |
64 |
<input |
65 |
:required=" |
66 |
!circRuleTrigger.patron_category_id |
67 |
" |
68 |
class="vs__search" |
69 |
v-bind="attributes" |
70 |
v-on="events" |
71 |
/> |
72 |
</template> |
73 |
</v-select> |
74 |
<span class="required">{{ $__("Required") }}</span> |
75 |
</li> |
76 |
<li> |
77 |
<label for="item_type_id" class="required" |
78 |
>{{ $__("Item type") }}:</label |
79 |
> |
80 |
<v-select |
81 |
id="item_type_id" |
82 |
v-model="circRuleTrigger.item_type_id" |
83 |
label="description" |
84 |
:reduce="type => type.item_type_id" |
85 |
:options="itemTypes" |
86 |
@update:modelValue="handleContextChange($event)" |
87 |
:disabled="editMode ? true : false" |
88 |
> |
89 |
<template #search="{ attributes, events }"> |
90 |
<input |
91 |
:required=" |
92 |
!circRuleTrigger.item_type_id |
93 |
" |
94 |
class="vs__search" |
95 |
v-bind="attributes" |
96 |
v-on="events" |
97 |
/> |
98 |
</template> |
99 |
</v-select> |
100 |
<span class="required">{{ $__("Required") }}</span> |
101 |
</li> |
102 |
<li> |
103 |
<label for="overdue_delay" |
104 |
>{{ $__("Delay") }}: |
105 |
</label> |
106 |
<input |
107 |
id="overdue_delay" |
108 |
v-model="circRuleTrigger.delay" |
109 |
type="number" |
110 |
/> |
111 |
</li> |
112 |
<li> |
113 |
<label for="letter_code" |
114 |
>{{ $__("Letter") }}:</label |
115 |
> |
116 |
<v-select |
117 |
id="letter_code" |
118 |
v-model="circRuleTrigger.notice" |
119 |
label="name" |
120 |
:reduce="type => type.code" |
121 |
:options="letters" |
122 |
> |
123 |
<template #search="{ attributes, events }"> |
124 |
<input |
125 |
class="vs__search" |
126 |
v-bind="attributes" |
127 |
v-on="events" |
128 |
/> |
129 |
</template> |
130 |
</v-select> |
131 |
</li> |
132 |
<li> |
133 |
<label for="mtt" |
134 |
>{{ $__("Transport type(s)") }}:</label |
135 |
> |
136 |
<v-select |
137 |
id="mtt" |
138 |
v-model="circRuleTrigger.mtt" |
139 |
label="name" |
140 |
:reduce="type => type.code" |
141 |
:options="mtts" |
142 |
multiple |
143 |
> |
144 |
<template #search="{ attributes, events }"> |
145 |
<input |
146 |
class="vs__search" |
147 |
v-bind="attributes" |
148 |
v-on="events" |
149 |
/> |
150 |
</template> |
151 |
</v-select> |
152 |
</li> |
153 |
<li> |
154 |
<label for="restricts" |
155 |
>{{ $__("Restricts checkouts") }}:</label |
156 |
> |
157 |
<input |
158 |
type="checkbox" |
159 |
id="restricts" |
160 |
:checked="false" |
161 |
true-value="1" |
162 |
false-value="0" |
163 |
v-model="circRuleTrigger.restrict" |
164 |
/> |
165 |
</li> |
166 |
</ol> |
167 |
</fieldset> |
168 |
</div> |
169 |
<div class="modal-footer"> |
170 |
<ButtonSubmit /> |
171 |
<router-link |
172 |
:to="{ |
173 |
name: 'CirculationTriggersList', |
174 |
}" |
175 |
>Cancel</router-link |
176 |
> |
177 |
</div> |
178 |
</form> |
179 |
</div> |
180 |
<div v-else> |
181 |
<p>{{ $__("Loading...") }}</p> |
182 |
</div> |
183 |
</template> |
184 |
|
185 |
<script> |
186 |
import { APIClient } from "../../../fetch/api-client.js" |
187 |
import TriggersTable from "./TriggersTable.vue" |
188 |
import { inject } from "vue" |
189 |
import { storeToRefs } from "pinia" |
190 |
import ButtonSubmit from "../../ButtonSubmit.vue" |
191 |
import TriggerContext from "./TriggerContext.vue" |
192 |
|
193 |
export default { |
194 |
setup() { |
195 |
const circRulesStore = inject("circRulesStore") |
196 |
const { splitCircRulesByTriggerNumber } = circRulesStore |
197 |
const { letters } = storeToRefs(circRulesStore) |
198 |
|
199 |
return { |
200 |
splitCircRulesByTriggerNumber, |
201 |
letters, |
202 |
} |
203 |
}, |
204 |
data() { |
205 |
return { |
206 |
initialized: false, |
207 |
libraries: null, |
208 |
categories: null, |
209 |
itemTypes: null, |
210 |
circRules: [], |
211 |
circRuleTrigger: { |
212 |
item_type_id: "*", |
213 |
library_id: "*", |
214 |
patron_category_id: "*", |
215 |
delay: null, |
216 |
notice: null, |
217 |
mtt: null, |
218 |
restrict: "0", |
219 |
}, |
220 |
effectiveRule: { |
221 |
item_type_id: "*", |
222 |
library_id: "*", |
223 |
patron_category_id: "*", |
224 |
delay: null, |
225 |
notice: null, |
226 |
mtt: null, |
227 |
restrict: "0", |
228 |
}, |
229 |
newTriggerNumber: 1, |
230 |
mtts: [ |
231 |
{ code: "email", name: "Email" }, |
232 |
{ code: "sms", name: "SMS" }, |
233 |
{ code: "print", name: "Print" }, |
234 |
], |
235 |
ruleInfo: { |
236 |
issuelength: null, |
237 |
decreaseloanholds: null, |
238 |
fine: null, |
239 |
chargeperiod: null, |
240 |
lengthunit: null, |
241 |
}, |
242 |
editMode: false, |
243 |
ruleBeingEdited: null, |
244 |
} |
245 |
}, |
246 |
beforeRouteEnter(to, from, next) { |
247 |
next(vm => { |
248 |
vm.getLibraries().then(() => |
249 |
vm.getCategories().then(() => |
250 |
vm.getItemTypes().then(() => { |
251 |
const { query } = to |
252 |
vm.checkForExistingRules(query).then( |
253 |
() => (vm.initialized = true) |
254 |
) |
255 |
}) |
256 |
) |
257 |
) |
258 |
}) |
259 |
}, |
260 |
methods: { |
261 |
async addCircRule(e) { |
262 |
e.preventDefault() |
263 |
|
264 |
const context = { |
265 |
library_id: this.circRuleTrigger.library_id || "*", |
266 |
item_type_id: this.circRuleTrigger.item_type_id || "*", |
267 |
patron_category_id: |
268 |
this.circRuleTrigger.patron_category_id || "*", |
269 |
} |
270 |
|
271 |
const delay = |
272 |
this.circRuleTrigger.delay === this.effectiveRule.delay |
273 |
? null |
274 |
: this.circRuleTrigger.delay |
275 |
const notice = |
276 |
this.circRuleTrigger.notice === this.effectiveRule.notice |
277 |
? null |
278 |
: this.circRuleTrigger.notice |
279 |
const restrict = |
280 |
this.circRuleTrigger.restrict === this.effectiveRule.restrict |
281 |
? null |
282 |
: this.circRuleTrigger.restrict |
283 |
const mtt = |
284 |
this.circRuleTrigger.mtt.join(",") === |
285 |
this.effectiveRule.mtt.join(",") |
286 |
? null |
287 |
: this.circRuleTrigger.mtt.join(",") |
288 |
|
289 |
const circRule = { |
290 |
context, |
291 |
} |
292 |
circRule[`overdue_${this.newTriggerNumber}_delay`] = delay |
293 |
circRule[`overdue_${this.newTriggerNumber}_notice`] = notice |
294 |
circRule[`overdue_${this.newTriggerNumber}_restrict`] = restrict |
295 |
circRule[`overdue_${this.newTriggerNumber}_mtt`] = mtt |
296 |
|
297 |
if (this.editMode) { |
298 |
Object.keys(circRule).forEach(key => { |
299 |
if (key === "context") return |
300 |
if (!circRule[key]) delete circRule[key] |
301 |
}) |
302 |
} |
303 |
|
304 |
const client = APIClient.circRule |
305 |
await client.circRules.update(circRule).then( |
306 |
() => { |
307 |
this.$router |
308 |
.push({ name: "CirculationTriggersList" }) |
309 |
.then(() => this.$router.go(0)) |
310 |
}, |
311 |
error => {} |
312 |
) |
313 |
}, |
314 |
async getLibraries() { |
315 |
const client = APIClient.library |
316 |
await client.libraries.getAll().then( |
317 |
libraries => { |
318 |
libraries.unshift({ |
319 |
library_id: "*", |
320 |
name: "All libraries", |
321 |
}) |
322 |
this.libraries = libraries |
323 |
}, |
324 |
error => {} |
325 |
) |
326 |
}, |
327 |
async getCategories() { |
328 |
const client = APIClient.patron |
329 |
await client.patronCategories.getAll().then( |
330 |
categories => { |
331 |
categories.unshift({ |
332 |
patron_category_id: "*", |
333 |
name: "All categories", |
334 |
}) |
335 |
this.categories = categories |
336 |
}, |
337 |
error => {} |
338 |
) |
339 |
}, |
340 |
async getItemTypes() { |
341 |
const client = APIClient.item |
342 |
await client.itemTypes.getAll().then( |
343 |
types => { |
344 |
types.unshift({ |
345 |
item_type_id: "*", |
346 |
description: "All item types", |
347 |
}) |
348 |
this.itemTypes = types |
349 |
}, |
350 |
error => {} |
351 |
) |
352 |
}, |
353 |
async handleContextChange() { |
354 |
await this.checkForExistingRules() |
355 |
}, |
356 |
async checkForExistingRules(routeParams) { |
357 |
// We always pass library_id so we need to check for the existence of either item type or patron category |
358 |
const editMode = routeParams && routeParams.triggerNumber |
359 |
if (editMode) { |
360 |
this.editMode = editMode |
361 |
this.ruleBeingEdited = routeParams.triggerNumber |
362 |
} |
363 |
const library_id = |
364 |
routeParams && routeParams.library_id |
365 |
? routeParams.library_id |
366 |
: this.circRuleTrigger.library_id || "*" |
367 |
const item_type_id = |
368 |
routeParams && routeParams.item_type_id |
369 |
? routeParams.item_type_id |
370 |
: this.circRuleTrigger.item_type_id || "*" |
371 |
const patron_category_id = |
372 |
routeParams && routeParams.patron_category_id |
373 |
? routeParams.patron_category_id |
374 |
: this.circRuleTrigger.patron_category_id || "*" |
375 |
const params = { |
376 |
library_id, |
377 |
item_type_id, |
378 |
patron_category_id, |
379 |
} |
380 |
|
381 |
const client = APIClient.circRule |
382 |
await client.circRules.getAll({}, params).then( |
383 |
rules => { |
384 |
if (rules.length === 0) { |
385 |
this.newTriggerNumber = 1 |
386 |
} else { |
387 |
const { rulesPerTrigger } = |
388 |
this.splitCircRulesByTriggerNumber(rules) |
389 |
this.circRules = rulesPerTrigger |
390 |
this.ruleInfo = { |
391 |
issuelength: rules[0].issuelength, |
392 |
decreaseloanholds: rules[0].decreaseloanholds, |
393 |
fine: rules[0].fine, |
394 |
chargeperiod: rules[0].chargeperiod, |
395 |
lengthunit: rules[0].lengthunit, |
396 |
} |
397 |
} |
398 |
}, |
399 |
error => {} |
400 |
) |
401 |
|
402 |
params.effective = false |
403 |
await client.circRules.getAll({}, params).then( |
404 |
rules => { |
405 |
if (rules.length === 0) { |
406 |
this.newTriggerNumber = 1 |
407 |
|
408 |
this.assignTriggerValues( |
409 |
"circRuleTrigger", |
410 |
this.circRules, |
411 |
this.newTriggerNumber, |
412 |
params |
413 |
) |
414 |
this.assignTriggerValues( |
415 |
"effectiveRule", |
416 |
this.circRules, |
417 |
this.newTriggerNumber, |
418 |
params |
419 |
) |
420 |
} else { |
421 |
const regex = /overdue_(\d+)_delay/g |
422 |
const numberOfTriggers = Object.keys(rules[0]).filter( |
423 |
key => regex.test(key) && rules[0][key] !== null |
424 |
).length |
425 |
this.newTriggerNumber = editMode |
426 |
? routeParams.triggerNumber |
427 |
: numberOfTriggers + 1 |
428 |
const { rulesPerTrigger } = |
429 |
this.splitCircRulesByTriggerNumber(rules) |
430 |
const triggerToDisplay = editMode |
431 |
? routeParams.triggerNumber |
432 |
: numberOfTriggers |
433 |
this.assignTriggerValues( |
434 |
"circRuleTrigger", |
435 |
rulesPerTrigger, |
436 |
triggerToDisplay |
437 |
) |
438 |
this.assignTriggerValues( |
439 |
"effectiveRule", |
440 |
rulesPerTrigger, |
441 |
triggerToDisplay |
442 |
) |
443 |
} |
444 |
}, |
445 |
error => {} |
446 |
) |
447 |
}, |
448 |
assignTriggerValues(prop, rules, triggerNumber, context = null) { |
449 |
this[prop] = { |
450 |
item_type_id: context |
451 |
? context.item_type_id |
452 |
: rules[triggerNumber - 1].context.item_type_id || "*", |
453 |
library_id: context |
454 |
? context.library_id |
455 |
: rules[triggerNumber - 1].context.library_id || "*", |
456 |
patron_category_id: context |
457 |
? context.patron_category_id |
458 |
: rules[triggerNumber - 1].context.patron_category_id || |
459 |
"*", |
460 |
delay: rules[triggerNumber - 1][ |
461 |
`overdue_${triggerNumber}_delay` |
462 |
], |
463 |
notice: rules[triggerNumber - 1][ |
464 |
`overdue_${triggerNumber}_notice` |
465 |
], |
466 |
mtt: rules[triggerNumber - 1][`overdue_${triggerNumber}_mtt`] |
467 |
? rules[triggerNumber - 1][ |
468 |
`overdue_${triggerNumber}_mtt` |
469 |
].split(",") |
470 |
: [], |
471 |
restrict: |
472 |
rules[triggerNumber - 1][ |
473 |
`overdue_${triggerNumber}_restrict` |
474 |
], |
475 |
} |
476 |
}, |
477 |
}, |
478 |
watch: { |
479 |
$route: { |
480 |
immediate: true, |
481 |
handler: function (newVal, oldVal) { |
482 |
if ( |
483 |
oldVal && |
484 |
oldVal.query.triggerNumber && |
485 |
newVal.query.triggerNumber !== oldVal.query.triggerNumber |
486 |
) { |
487 |
this.$router.go(0) |
488 |
} |
489 |
}, |
490 |
}, |
491 |
}, |
492 |
components: { TriggersTable, ButtonSubmit, TriggerContext }, |
493 |
} |
494 |
</script> |
495 |
|
496 |
<style scoped> |
497 |
.close_modal { |
498 |
position: absolute; |
499 |
top: 0.5rem; |
500 |
right: 0.5rem; |
501 |
font-size: 2rem; |
502 |
} |
503 |
|
504 |
form li { |
505 |
display: flex; |
506 |
align-items: center; |
507 |
} |
508 |
|
509 |
.dialog.alert |
510 |
fieldset:not(.bg-danger):not(.bg-warning):not(.bg-info):not(.bg-success):not(.bg-primary):not(.action), |
511 |
.dialog.error |
512 |
fieldset:not(.bg-danger):not(.bg-warning):not(.bg-info):not(.bg-success):not(.bg-primary):not(.action) { |
513 |
margin: 0; |
514 |
background-color: rgba(255, 255, 255, 1); |
515 |
} |
516 |
|
517 |
.router-link-active { |
518 |
margin-left: 10px; |
519 |
} |
520 |
</style> |