Changes to documents able to edit them now, also support for auto mail attechemnts
This commit is contained in:
@@ -18,6 +18,7 @@ const form = useForm({
|
||||
html_template: props.template?.html_template ?? "",
|
||||
text_template: props.template?.text_template ?? "",
|
||||
entity_types: props.template?.entity_types ?? ["client", "contract"],
|
||||
allow_attachments: props.template?.allow_attachments ?? false,
|
||||
active: props.template?.active ?? true,
|
||||
});
|
||||
|
||||
@@ -1065,6 +1066,10 @@ watch(
|
||||
>
|
||||
</div>
|
||||
</div>
|
||||
<div class="flex items-center gap-2">
|
||||
<input id="allow_attachments" type="checkbox" v-model="form.allow_attachments" />
|
||||
<label for="allow_attachments" class="text-sm text-gray-700">Dovoli priponke</label>
|
||||
</div>
|
||||
<div class="flex items-center gap-2">
|
||||
<input id="active" type="checkbox" v-model="form.active" />
|
||||
<label for="active" class="text-sm text-gray-700">Aktivno</label>
|
||||
|
||||
@@ -4,64 +4,85 @@ import BasicButton from "@/Components/buttons/BasicButton.vue";
|
||||
import DialogModal from "@/Components/DialogModal.vue";
|
||||
import InputLabel from "@/Components/InputLabel.vue";
|
||||
import DatePickerField from "@/Components/DatePickerField.vue";
|
||||
import TextInput from "@/Components/TextInput.vue";
|
||||
import CurrencyInput from "@/Components/CurrencyInput.vue";
|
||||
import { useForm } from "@inertiajs/vue3";
|
||||
import { useForm, usePage } from "@inertiajs/vue3";
|
||||
import { FwbTextarea } from "flowbite-vue";
|
||||
import { ref, watch, computed } from "vue";
|
||||
|
||||
const props = defineProps({
|
||||
show: {
|
||||
type: Boolean,
|
||||
default: false,
|
||||
},
|
||||
client_case: Object,
|
||||
actions: Array,
|
||||
// optionally pre-select a contract to attach the activity to
|
||||
show: { type: Boolean, default: false },
|
||||
client_case: { type: Object, required: true },
|
||||
actions: { type: Array, default: () => [] },
|
||||
contractUuid: { type: String, default: null },
|
||||
phoneMode: { type: Boolean, default: false },
|
||||
// Prefer passing these from parent (e.g., phone view) to avoid reliance on global page props in teleports
|
||||
documents: { type: Array, default: null },
|
||||
contracts: { type: Array, default: null },
|
||||
});
|
||||
|
||||
const decisions = ref(props.actions[0].decisions);
|
||||
const page = usePage();
|
||||
|
||||
console.log(props.actions);
|
||||
const decisions = ref(
|
||||
Array.isArray(props.actions) && props.actions.length > 0
|
||||
? props.actions[0].decisions || []
|
||||
: []
|
||||
);
|
||||
|
||||
const emit = defineEmits(["close"]);
|
||||
|
||||
const close = () => {
|
||||
emit("close");
|
||||
};
|
||||
const close = () => emit("close");
|
||||
|
||||
const form = useForm({
|
||||
due_date: null,
|
||||
amount: null,
|
||||
note: "",
|
||||
action_id: props.actions[0].id,
|
||||
decision_id: props.actions[0].decisions[0].id,
|
||||
action_id:
|
||||
Array.isArray(props.actions) && props.actions.length > 0 ? props.actions[0].id : null,
|
||||
decision_id:
|
||||
Array.isArray(props.actions) &&
|
||||
props.actions.length > 0 &&
|
||||
Array.isArray(props.actions[0].decisions) &&
|
||||
props.actions[0].decisions.length > 0
|
||||
? props.actions[0].decisions[0].id
|
||||
: null,
|
||||
contract_uuid: props.contractUuid,
|
||||
send_auto_mail: true,
|
||||
attach_documents: false,
|
||||
attachment_document_ids: [],
|
||||
});
|
||||
|
||||
watch(
|
||||
() => props.actions,
|
||||
(list) => {
|
||||
if (!Array.isArray(list) || list.length === 0) {
|
||||
decisions.value = [];
|
||||
form.action_id = null;
|
||||
form.decision_id = null;
|
||||
return;
|
||||
}
|
||||
if (!form.action_id) {
|
||||
form.action_id = list[0].id;
|
||||
}
|
||||
const found = list.find((el) => el.id === form.action_id) || list[0];
|
||||
decisions.value = Array.isArray(found.decisions) ? found.decisions : [];
|
||||
if (!form.decision_id && decisions.value.length > 0) {
|
||||
form.decision_id = decisions.value[0].id;
|
||||
}
|
||||
},
|
||||
{ immediate: true }
|
||||
);
|
||||
|
||||
watch(
|
||||
() => form.action_id,
|
||||
(action_id) => {
|
||||
decisions.value = props.actions.filter((el) => el.id === action_id)[0].decisions;
|
||||
form.decision_id = decisions.value[0].id;
|
||||
// reset send flag on action change (will re-evaluate below)
|
||||
const a = Array.isArray(props.actions)
|
||||
? props.actions.find((el) => el.id === action_id)
|
||||
: null;
|
||||
decisions.value = Array.isArray(a?.decisions) ? a.decisions : [];
|
||||
form.decision_id = decisions.value[0]?.id ?? null;
|
||||
form.send_auto_mail = true;
|
||||
}
|
||||
);
|
||||
|
||||
watch(
|
||||
() => form.due_date,
|
||||
(due_date) => {
|
||||
if (due_date) {
|
||||
let date = new Date(form.due_date).toLocaleDateString("en-CA");
|
||||
console.table({ old: due_date, new: date });
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
// keep contract_uuid synced if the prop changes while the drawer is open
|
||||
watch(
|
||||
() => props.contractUuid,
|
||||
(cu) => {
|
||||
@@ -69,48 +90,6 @@ watch(
|
||||
}
|
||||
);
|
||||
|
||||
const store = async () => {
|
||||
console.table({
|
||||
due_date: form.due_date,
|
||||
action_id: form.action_id,
|
||||
decision_id: form.decision_id,
|
||||
amount: form.amount,
|
||||
note: form.note,
|
||||
});
|
||||
// Helper to safely format a selected date (Date instance or parsable value) to YYYY-MM-DD
|
||||
const formatDateForSubmit = (value) => {
|
||||
if (!value) return null; // leave empty as null
|
||||
const d = value instanceof Date ? value : new Date(value);
|
||||
if (isNaN(d.getTime())) return null; // invalid date -> null
|
||||
// Avoid timezone shifting by constructing in local time
|
||||
const y = d.getFullYear();
|
||||
const m = String(d.getMonth() + 1).padStart(2, "0");
|
||||
const day = String(d.getDate()).padStart(2, "0");
|
||||
return `${y}-${m}-${day}`; // matches en-CA style YYYY-MM-DD
|
||||
};
|
||||
|
||||
form
|
||||
.transform((data) => ({
|
||||
...data,
|
||||
due_date: formatDateForSubmit(data.due_date),
|
||||
}))
|
||||
.post(route("clientCase.activity.store", props.client_case), {
|
||||
onSuccess: () => {
|
||||
close();
|
||||
// Preserve selected contract across submissions; reset only user-editable fields
|
||||
form.reset("due_date", "amount", "note");
|
||||
},
|
||||
onError: (errors) => {
|
||||
console.log("Validation or server error:", errors);
|
||||
},
|
||||
onFinish: () => {
|
||||
console.log("Request finished processing.");
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
// When the drawer opens, always sync the current contractUuid into the form,
|
||||
// even if the value hasn't changed (prevents stale/null contract_uuid after reset)
|
||||
watch(
|
||||
() => props.show,
|
||||
(visible) => {
|
||||
@@ -120,14 +99,53 @@ watch(
|
||||
}
|
||||
);
|
||||
|
||||
// Helper to read metadata for the currently selected decision
|
||||
const currentDecision = () => decisions.value.find((d) => d.id === form.decision_id) || decisions.value[0];
|
||||
const store = async () => {
|
||||
const formatDateForSubmit = (value) => {
|
||||
if (!value) return null;
|
||||
const d = value instanceof Date ? value : new Date(value);
|
||||
if (isNaN(d.getTime())) return null;
|
||||
const y = d.getFullYear();
|
||||
const m = String(d.getMonth() + 1).padStart(2, "0");
|
||||
const day = String(d.getDate()).padStart(2, "0");
|
||||
return `${y}-${m}-${day}`;
|
||||
};
|
||||
|
||||
form
|
||||
.transform((data) => ({
|
||||
...data,
|
||||
due_date: formatDateForSubmit(data.due_date),
|
||||
attachment_document_ids:
|
||||
templateAllowsAttachments.value && data.attach_documents
|
||||
? data.attachment_document_ids
|
||||
: [],
|
||||
}))
|
||||
.post(route("clientCase.activity.store", props.client_case), {
|
||||
onSuccess: () => {
|
||||
close();
|
||||
form.reset("due_date", "amount", "note");
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
const currentDecision = () => {
|
||||
if (!Array.isArray(decisions.value) || decisions.value.length === 0) {
|
||||
return null;
|
||||
}
|
||||
return (
|
||||
decisions.value.find((d) => d.id === form.decision_id) || decisions.value[0] || null
|
||||
);
|
||||
};
|
||||
const showSendAutoMail = () => {
|
||||
const d = currentDecision();
|
||||
return !!(d && d.auto_mail && d.email_template_id);
|
||||
};
|
||||
|
||||
// Determine if the selected template requires a contract
|
||||
const templateAllowsAttachments = computed(() => {
|
||||
const d = currentDecision();
|
||||
const tmpl = d?.email_template || d?.emailTemplate || null;
|
||||
return !!(tmpl && tmpl.allow_attachments);
|
||||
});
|
||||
|
||||
const autoMailRequiresContract = computed(() => {
|
||||
const d = currentDecision();
|
||||
if (!d) return false;
|
||||
@@ -136,16 +154,14 @@ const autoMailRequiresContract = computed(() => {
|
||||
return types.includes("contract");
|
||||
});
|
||||
|
||||
// Disable checkbox when contract is required but none is selected
|
||||
const autoMailDisabled = computed(() => {
|
||||
return showSendAutoMail() && autoMailRequiresContract.value && !form.contract_uuid;
|
||||
});
|
||||
|
||||
const autoMailDisabledHint = computed(() => {
|
||||
return autoMailDisabled.value ? "Ta e-poštna predloga zahteva pogodbo. Najprej izberite pogodbo." : "";
|
||||
return autoMailDisabled.value
|
||||
? "Ta e-poštna predloga zahteva pogodbo. Najprej izberite pogodbo."
|
||||
: "";
|
||||
});
|
||||
|
||||
// If disabled, force the flag off to avoid accidental queue attempts
|
||||
watch(
|
||||
() => autoMailDisabled.value,
|
||||
(disabled) => {
|
||||
@@ -154,7 +170,106 @@ watch(
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
const isToday = (val) => {
|
||||
try {
|
||||
if (!val) return false;
|
||||
let d;
|
||||
if (val instanceof Date) {
|
||||
d = val;
|
||||
} else if (typeof val === "string") {
|
||||
// Normalize common MySQL timestamp 'YYYY-MM-DD HH:mm:ss' for Safari/iOS
|
||||
const s = val.includes("T") ? val : val.replace(" ", "T");
|
||||
d = new Date(s);
|
||||
if (isNaN(d.getTime())) {
|
||||
// Fallback: parse manually as local date
|
||||
const m = val.match(/^(\d{4})-(\d{2})-(\d{2})[ T](\d{2}):(\d{2})(?::(\d{2}))?/);
|
||||
if (m) {
|
||||
const [_, yy, mm, dd, hh, mi, ss] = m;
|
||||
d = new Date(
|
||||
Number(yy),
|
||||
Number(mm) - 1,
|
||||
Number(dd),
|
||||
Number(hh),
|
||||
Number(mi),
|
||||
Number(ss || "0")
|
||||
);
|
||||
}
|
||||
}
|
||||
} else if (typeof val === "number") {
|
||||
d = new Date(val);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
if (isNaN(d.getTime())) return false;
|
||||
const today = new Date();
|
||||
return (
|
||||
d.getFullYear() === today.getFullYear() &&
|
||||
d.getMonth() === today.getMonth() &&
|
||||
d.getDate() === today.getDate()
|
||||
);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
const docsSource = computed(() => {
|
||||
if (Array.isArray(props.documents)) {
|
||||
return props.documents;
|
||||
}
|
||||
const propsVal = page?.props?.value || {};
|
||||
return Array.isArray(propsVal.documents) ? propsVal.documents : [];
|
||||
});
|
||||
|
||||
const availableContractDocs = computed(() => {
|
||||
if (!form.contract_uuid) return [];
|
||||
const docs = docsSource.value;
|
||||
const all = docs.filter((d) => d.contract_uuid === form.contract_uuid);
|
||||
if (!props.phoneMode) return all;
|
||||
return all.filter((d) => {
|
||||
const mime = (d.mime_type || "").toLowerCase();
|
||||
const isImage =
|
||||
mime.startsWith("image/") ||
|
||||
["jpg", "jpeg", "png", "gif", "webp", "heic", "heif"].includes(
|
||||
(d.extension || "").toLowerCase()
|
||||
);
|
||||
return isImage && isToday(d.created_at || d.createdAt);
|
||||
});
|
||||
});
|
||||
|
||||
const pageContracts = computed(() => {
|
||||
if (Array.isArray(props.contracts)) {
|
||||
return props.contracts;
|
||||
}
|
||||
const propsVal = page?.props?.value || {};
|
||||
return Array.isArray(propsVal.contracts) ? propsVal.contracts : [];
|
||||
});
|
||||
|
||||
watch(
|
||||
[
|
||||
() => props.phoneMode,
|
||||
() => templateAllowsAttachments.value,
|
||||
() => form.contract_uuid,
|
||||
() => form.decision_id,
|
||||
() => availableContractDocs.value.length,
|
||||
],
|
||||
() => {
|
||||
if (!props.phoneMode) return;
|
||||
if (!templateAllowsAttachments.value) return;
|
||||
if (!form.contract_uuid) return;
|
||||
const docs = availableContractDocs.value;
|
||||
if (docs.length === 0) return;
|
||||
form.attach_documents = true;
|
||||
if (
|
||||
!Array.isArray(form.attachment_document_ids) ||
|
||||
form.attachment_document_ids.length === 0
|
||||
) {
|
||||
form.attachment_document_ids = docs.map((d) => d.id);
|
||||
}
|
||||
}
|
||||
);
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<DialogModal :show="show" @close="close">
|
||||
<template #title>Dodaj aktivnost</template>
|
||||
@@ -167,9 +282,9 @@ watch(
|
||||
id="activityAction"
|
||||
ref="activityActionSelect"
|
||||
v-model="form.action_id"
|
||||
:disabled="!actions || !actions.length"
|
||||
>
|
||||
<option v-for="a in actions" :value="a.id">{{ a.name }}</option>
|
||||
<!-- ... -->
|
||||
<option v-for="a in actions" :key="a.id" :value="a.id">{{ a.name }}</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="col-span-6 sm:col-span-4">
|
||||
@@ -179,9 +294,9 @@ watch(
|
||||
id="activityDecision"
|
||||
ref="activityDecisionSelect"
|
||||
v-model="form.decision_id"
|
||||
:disabled="!decisions || !decisions.length"
|
||||
>
|
||||
<option v-for="d in decisions" :value="d.id">{{ d.name }}</option>
|
||||
<!-- ... -->
|
||||
<option v-for="d in decisions" :key="d.id" :value="d.id">{{ d.name }}</option>
|
||||
</select>
|
||||
</div>
|
||||
<div class="col-span-6 sm:col-span-4">
|
||||
@@ -225,13 +340,61 @@ watch(
|
||||
v-model="form.send_auto_mail"
|
||||
:disabled="autoMailDisabled"
|
||||
class="rounded border-gray-300 text-indigo-600 shadow-sm focus:ring-indigo-500 disabled:opacity-50 disabled:cursor-not-allowed"
|
||||
>
|
||||
/>
|
||||
<span>Send auto email</span>
|
||||
</label>
|
||||
</div>
|
||||
<p v-if="autoMailDisabled" class="mt-1 text-xs text-amber-600">
|
||||
{{ autoMailDisabledHint }}
|
||||
</p>
|
||||
|
||||
<div v-if="templateAllowsAttachments && form.contract_uuid" class="mt-3">
|
||||
<label class="inline-flex items-center gap-2">
|
||||
<input type="checkbox" v-model="form.attach_documents" />
|
||||
<span class="text-sm">Dodaj priponke iz izbrane pogodbe</span>
|
||||
</label>
|
||||
<div
|
||||
v-if="form.attach_documents"
|
||||
class="mt-2 border rounded p-2 max-h-48 overflow-auto"
|
||||
>
|
||||
<div class="text-xs text-gray-600 mb-2">
|
||||
Izberite dokumente, ki bodo poslani kot priponke:
|
||||
</div>
|
||||
<div class="space-y-1">
|
||||
<template v-for="c in pageContracts" :key="c.uuid || c.id">
|
||||
<div v-if="c.uuid === form.contract_uuid">
|
||||
<div class="font-medium text-sm text-gray-700 mb-1">
|
||||
Pogodba {{ c.reference }}
|
||||
</div>
|
||||
<div class="space-y-1">
|
||||
<div
|
||||
v-for="doc in availableContractDocs"
|
||||
:key="doc.uuid || doc.id"
|
||||
class="flex items-center gap-2 text-sm"
|
||||
>
|
||||
<input
|
||||
type="checkbox"
|
||||
:value="doc.id"
|
||||
v-model="form.attachment_document_ids"
|
||||
/>
|
||||
<span>{{ doc.original_name || doc.name }}</span>
|
||||
<span class="text-xs text-gray-400"
|
||||
>({{ doc.extension?.toUpperCase() || "" }},
|
||||
{{ (doc.size / 1024 / 1024).toFixed(2) }} MB)</span
|
||||
>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
<div
|
||||
v-if="availableContractDocs.length === 0"
|
||||
class="text-sm text-gray-500"
|
||||
>
|
||||
Ni dokumentov, povezanih s to pogodbo.
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="flex justify-end mt-4">
|
||||
<ActionMessage :on="form.recentlySuccessful" class="me-3">
|
||||
|
||||
@@ -9,6 +9,7 @@ import ContractTable from "./Partials/ContractTable.vue";
|
||||
import ActivityDrawer from "./Partials/ActivityDrawer.vue";
|
||||
import ActivityTable from "./Partials/ActivityTable.vue";
|
||||
import DocumentsTable from "@/Components/DocumentsTable.vue";
|
||||
import DocumentEditDialog from "@/Components/DocumentEditDialog.vue";
|
||||
import DocumentUploadDialog from "@/Components/DocumentUploadDialog.vue";
|
||||
import DocumentViewerDialog from "@/Components/DocumentViewerDialog.vue";
|
||||
import { classifyDocument } from "@/Services/documents";
|
||||
@@ -46,6 +47,21 @@ const onUploaded = () => {
|
||||
router.reload({ only: ["documents"] });
|
||||
};
|
||||
|
||||
// Document edit dialog state
|
||||
const showDocEdit = ref(false)
|
||||
const editingDoc = ref(null)
|
||||
const openDocEdit = (doc) => {
|
||||
editingDoc.value = doc
|
||||
showDocEdit.value = true
|
||||
}
|
||||
const closeDocEdit = () => {
|
||||
showDocEdit.value = false
|
||||
editingDoc.value = null
|
||||
}
|
||||
const onDocSaved = () => {
|
||||
router.reload({ only: ['documents'] })
|
||||
}
|
||||
|
||||
const viewer = ref({ open: false, src: "", title: "" });
|
||||
const openViewer = (doc) => {
|
||||
const kind = classifyDocument(doc);
|
||||
@@ -337,6 +353,7 @@ const submitAttachSegment = () => {
|
||||
<DocumentsTable
|
||||
:documents="documents"
|
||||
@view="openViewer"
|
||||
@edit="openDocEdit"
|
||||
:download-url-builder="
|
||||
(doc) => {
|
||||
const isContractDoc = (doc?.documentable_type || '')
|
||||
@@ -365,6 +382,14 @@ const submitAttachSegment = () => {
|
||||
:post-url="route('clientCase.document.store', client_case)"
|
||||
:contracts="contracts"
|
||||
/>
|
||||
<DocumentEditDialog
|
||||
:show="showDocEdit"
|
||||
:client_case_uuid="client_case.uuid"
|
||||
:document="editingDoc"
|
||||
:contracts="contracts"
|
||||
@close="closeDocEdit"
|
||||
@saved="onDocSaved"
|
||||
/>
|
||||
<DocumentViewerDialog
|
||||
:show="viewer.open"
|
||||
:src="viewer.src"
|
||||
@@ -386,6 +411,8 @@ const submitAttachSegment = () => {
|
||||
:client_case="client_case"
|
||||
:actions="actions"
|
||||
:contract-uuid="activityContractUuid"
|
||||
:documents="documents"
|
||||
:contracts="contracts"
|
||||
/>
|
||||
<ConfirmDialog
|
||||
:show="confirmDelete.show"
|
||||
|
||||
@@ -99,7 +99,13 @@ function activityActionLine(a) {
|
||||
const drawerAddActivity = ref(false);
|
||||
const activityContractUuid = ref(null);
|
||||
const openDrawerAddActivity = (c = null) => {
|
||||
activityContractUuid.value = c?.uuid ?? null;
|
||||
if (c && c.uuid) {
|
||||
activityContractUuid.value = c.uuid;
|
||||
} else if (Array.isArray(props.contracts) && props.contracts.length === 1) {
|
||||
activityContractUuid.value = props.contracts[0]?.uuid ?? null;
|
||||
} else {
|
||||
activityContractUuid.value = null;
|
||||
}
|
||||
drawerAddActivity.value = true;
|
||||
};
|
||||
const closeDrawer = () => {
|
||||
@@ -530,6 +536,9 @@ const clientSummary = computed(() => {
|
||||
:client_case="client_case"
|
||||
:actions="actions"
|
||||
:contract-uuid="activityContractUuid"
|
||||
:phone-mode="true"
|
||||
:documents="documents"
|
||||
:contracts="contracts"
|
||||
/>
|
||||
|
||||
<ConfirmationModal :show="confirmComplete" @close="confirmComplete = false">
|
||||
|
||||
Reference in New Issue
Block a user