-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathExpensesTable.vue
More file actions
292 lines (267 loc) · 7.49 KB
/
ExpensesTable.vue
File metadata and controls
292 lines (267 loc) · 7.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<template>
<!-- Header -->
<div class="flex flex-row justify-between items-center mt-2">
<h2 class="text-base font-semibold text-gray-800">{{ __("Expenses") }} </h2>
<div class="flex flex-row gap-3 items-center">
<span class="text-base font-semibold text-gray-800">
{{ formatCurrency(expenseClaim.total_claimed_amount, currency) }}
</span>
<Button
v-if="!isReadOnly"
id="add-expense-modal"
class="text-sm"
icon="plus"
variant="subtle"
@click="openModal()"
/>
</div>
</div>
<!-- Table -->
<div
v-if="expenseClaim.expenses"
class="flex flex-col bg-white mt-5 rounded border overflow-auto"
>
<div
class="flex flex-row p-3.5 items-center justify-between border-b cursor-pointer"
v-for="(item, idx) in expenseClaim.expenses"
:key="idx"
@click="openModal(item, idx)"
>
<div class="flex flex-col w-full justify-center gap-2.5">
<div class="flex flex-row items-center justify-between">
<div class="flex flex-row items-start gap-3 grow">
<div class="flex flex-col items-start gap-1.5">
<div class="text-base font-normal text-gray-800">
{{ __(item.expense_type) }}
</div>
<div class="text-xs font-normal text-gray-500">
<span>
{{
__("{0}: {1}", [
__("Sanctioned"),
formatCurrency(item.sanctioned_amount || 0, currency),
])
}}
</span>
<span class="whitespace-pre"> · </span>
<span class="whitespace-nowrap" v-if="item.expense_date">
{{ dayjs(item.expense_date).format("D MMM") }}
</span>
</div>
</div>
</div>
<div class="flex flex-row justify-end items-center gap-2">
<span class="text-gray-700 font-normal rounded text-base">
{{ formatCurrency(item.amount, currency) }}
</span>
<FeatherIcon name="chevron-right" class="h-5 w-5 text-gray-500" />
</div>
</div>
</div>
</div>
</div>
<EmptyState v-else :message="__('No expenses added')" :isTableField="true" />
<CustomIonModal :isOpen="isModalOpen" @didDismiss="resetSelectedItem()">
<template #actionSheet>
<!-- Add Expense Action Sheet -->
<div
class="bg-white w-full flex flex-col items-center justify-center pb-5"
>
<div class="w-full pt-8 pb-5 border-b text-center">
<span class="text-gray-900 font-bold text-lg">
{{ modalTitle }}
</span>
</div>
<div class="w-full flex flex-col items-center justify-center gap-5 p-4 max-h-[80vh]">
<div class="flex flex-col w-full space-y-4 overflow-y-auto">
<FormField
v-for="field in expensesTableFields.data"
:key="field.fieldname"
class="w-full"
:label="__(field.label, null, 'Expense Claim Detail')"
:fieldtype="field.fieldtype"
:fieldname="field.fieldname"
:options="field.options"
:hidden="field.hidden"
:reqd="field.reqd"
:default="field.default"
:readOnly="field.read_only || isReadOnly"
v-model="expenseItem[field.fieldname]"
/>
</div>
<div
v-if="!isReadOnly"
class="flex w-full flex-row items-center justify-between gap-3"
>
<Button
v-if="editingIdx !== null"
class="border-red-600 text-red-600 py-5 text-sm"
variant="outline"
theme="red"
@click="deleteExpenseItem()"
>
<template #prefix>
<FeatherIcon name="trash" class="w-4" />
</template>
{{ __("Delete") }}
</Button>
<Button
variant="solid"
class="w-full py-5 text-sm disabled:bg-gray-700 disabled:text-white"
@click="updateExpenseItem()"
:disabled="addButtonDisabled"
>
<template #prefix>
<FeatherIcon
:name="editingIdx === null ? 'plus' : 'check'"
class="w-4"
/>
</template>
{{ editingIdx === null ? __("Add Expense") : __("Update Expense") }}
</Button>
</div>
</div>
</div>
</template>
</CustomIonModal>
</template>
<script setup>
import { FeatherIcon, createResource } from "frappe-ui"
import { computed, ref, watch, inject } from "vue"
import FormField from "@/components/FormField.vue"
import EmptyState from "@/components/EmptyState.vue"
import CustomIonModal from "@/components/CustomIonModal.vue"
import { claimTypesByID } from "@/data/claims"
import { formatCurrency } from "@/utils/formatters"
import { updateCurrencyLabels, updateBaseFieldsAmount } from "@/composables/useCurrencyConversion"
const props = defineProps({
expenseClaim: {
type: Object,
required: true,
},
currency: {
type: String,
required: true,
},
isReadOnly: {
type: Boolean,
default: false,
},
})
const emit = defineEmits([
"add-expense-item",
"update-expense-item",
"delete-expense-item",
])
const dayjs = inject("$dayjs")
const __ = inject("$translate")
const expenseItem = ref({})
const editingIdx = ref(null)
const isModalOpen = ref(false)
const isFirstRender = ref(false)
const openModal = async (item, idx) => {
if (item) {
expenseItem.value = { ...item }
editingIdx.value = idx
}
isFirstRender.value = true
isModalOpen.value = true
}
const deleteExpenseItem = () => {
emit("delete-expense-item", editingIdx.value)
resetSelectedItem()
}
const updateExpenseItem = () => {
if (editingIdx.value === null) {
emit("add-expense-item", expenseItem.value)
} else {
emit("update-expense-item", expenseItem.value, editingIdx.value)
}
resetSelectedItem()
}
function resetSelectedItem() {
isFirstRender.value = false
isModalOpen.value = false
expenseItem.value = {}
editingIdx.value = null
}
const expensesTableFields = createResource({
url: "hrms.api.get_doctype_fields",
params: { doctype: "Expense Claim Detail" },
transform(data) {
const excludeFields = ["description_sb", "amounts_sb"]
return data.filter((field) => !excludeFields.includes(field.fieldname))
},
})
expensesTableFields.reload()
const modalTitle = computed(() => {
if (props.isReadOnly) return __("Expense Item")
return editingIdx.value === null ? __("New Expense Item") : __("Edit Expense Item")
})
const addButtonDisabled = computed(() => {
return expensesTableFields.data?.some((field) => {
if (field.reqd && !expenseItem.value[field.fieldname]) {
return true
}
})
})
// child table form scripts
watch(
() => expenseItem.value.expense_type,
(value) => {
if (!expenseItem.value.description) {
expenseItem.value.description = claimTypesByID[value]?.description
}
expenseItem.value.cost_center = props.expenseClaim.cost_center
}
)
watch(
() => expenseItem.value.amount,
(value) => {
if (!isFirstRender.value) {
expenseItem.value.sanctioned_amount = parseFloat(value)
} else {
isFirstRender.value = false
}
}
)
watch(
() => expensesTableFields.data,
(fields) => {
if (!fields) return
updateCurrencyLabels({
formFields: fields,
doc: props.expenseClaim,
baseFields: ["base_amount", "base_sanctioned_amount"],
transactionFields: ["amount", "sanctioned_amount"],
})
},
{ immediate: true }
)
watch(
() => [expenseItem.value.amount, expenseItem.value.sanctioned_amount],
() => {
if (expenseItem.value) {
updateBaseFieldsAmount({
doc: expenseItem.value,
fields: ['amount', 'sanctioned_amount'],
exchangeRate: props.expenseClaim.exchange_rate,
});
}
}
);
watch(
() => props.expenseClaim.exchange_rate,
(exchangeRate) => {
if (props.expenseClaim.expenses) {
props.expenseClaim.expenses.forEach(row => {
updateBaseFieldsAmount({
doc:row,
fields:['amount', 'sanctioned_amount'],
exchangeRate: exchangeRate
});
});
}
}
);
</script>