@@ -1063,6 +1108,7 @@ const props = defineProps({
initialEndISO: { type: String, default: '' },
ownerId: { type: String, default: '' },
+ planOwnerId: { type: String, default: '' }, // owner dos convênios (clínica); fallback: ownerId
allowOwnerEdit: { type: Boolean, default: false },
ownerOptions: { type: Array, default: () => [] },
@@ -1110,12 +1156,27 @@ const currentRecurrenceDate = computed(() =>
)
const editScope = ref('somente_este')
-const editScopeOptions = [
+
+const isFirstOccurrence = computed(() => {
+ if (!hasSerie.value) return false
+ const rDate = props.eventRow?.recurrence_date || props.eventRow?.original_date
+ if (!rDate) return false
+ if (serieEvents.value?.length) {
+ const dates = serieEvents.value
+ .map(e => e.recurrence_date || e.original_date)
+ .filter(Boolean)
+ .sort()
+ return dates[0] === rDate
+ }
+ return false
+})
+
+const editScopeOptions = computed(() => [
{ value: 'somente_este', label: 'Somente esta sessão' },
- { value: 'este_e_seguintes', label: 'Esta e as seguintes' },
+ { value: 'este_e_seguintes', label: 'Esta e as seguintes', disabled: isFirstOccurrence.value },
{ value: 'todos', label: 'Todas da série' },
{ value: 'todos_sem_excecao', label: 'Todas sem exceção' },
-]
+])
// ── recorrência (criação / sessão avulsa) ──────────────────
// 'avulsa' | 'semanal' | 'quinzenal' | 'diasEspecificos'
@@ -1288,6 +1349,17 @@ const { services, getDefaultPrice, load: loadServices } = useServices()
const { loadItems: _csLoadItems, saveItems: saveCommitmentItems, loadItemsOrTemplate: _csLoadItemsOrTemplate } = useCommitmentServices()
const { loadActive: loadActiveDiscount } = usePatientDiscounts()
const { plans: insurancePlans, load: loadInsurancePlans } = useInsurancePlans()
+const selectedPlanService = ref(null)
+
+const activePlans = computed(() => insurancePlans.value.filter(p => p.active !== false))
+
+const planServices = computed(() => {
+ if (!form.value.insurance_plan_id) return []
+ return (insurancePlans.value
+ .find(p => p.id === form.value.insurance_plan_id)
+ ?.insurance_plan_services || [])
+ .filter(s => s.active)
+})
let _servicesLoaded = false
async function ensureServicesLoaded () {
@@ -1298,7 +1370,7 @@ async function ensureServicesLoaded () {
function applyDefaultPrice () {
// Pula quando pago: o preço vem dos commitmentItems, não de um default
- if (billingType.value === 'pago') return
+ if (billingType.value === 'particular') return
// Só auto-preenche se price ainda não foi definido manualmente (ou é novo evento)
if (!isEdit.value) {
const suggested = getDefaultPrice()
@@ -1311,16 +1383,32 @@ const commitmentItems = ref([])
const servicePickerSel = ref(null)
const serieValorMode = ref('multiplicar') // 'multiplicar' | 'dividir'
-const billingType = ref('pago') // 'gratuito' | 'pago'
+const billingType = ref('particular') // 'gratuito' | 'particular' | 'convenio'
+const _restoringConvenio = ref(false) // flag para ignorar watch durante restauração
const billingTypeOptions = [
- { label: 'Gratuito', value: 'gratuito' },
- { label: 'Pago', value: 'pago' },
+ { label: 'Gratuito', value: 'gratuito' },
+ { label: 'Particular', value: 'particular' },
+ { label: 'Convênio', value: 'convenio' },
]
watch(billingType, (val) => {
if (val === 'gratuito') {
- commitmentItems.value = []
- form.value.price = 0
+ commitmentItems.value = []
+ form.value.price = 0
+ form.value.insurance_plan_id = null
+ form.value.insurance_guide_number = null
+ form.value.insurance_value = null
+ selectedPlanService.value = null
+ }
+ if (val === 'particular') {
+ form.value.insurance_plan_id = null
+ form.value.insurance_guide_number = null
+ form.value.insurance_value = null
+ selectedPlanService.value = null
+ }
+ if (val === 'convenio') {
+ commitmentItems.value = []
+ servicePickerSel.value = null
}
})
@@ -1426,16 +1514,46 @@ function onItemChange (item) {
async function _loadCommitmentItemsForEvent (eventId) {
const ruleId = props.eventRow?.recurrence_id ?? null
const isCustomized = props.eventRow?.services_customized ?? false
- if (!eventId && !ruleId) { commitmentItems.value = []; billingType.value = 'gratuito'; return }
+ // Lê os dados de convênio diretamente do eventRow (form.value pode ter sido limpo por watches)
+ const origPlanId = props.eventRow?.insurance_plan_id ?? null
+ const origGuide = props.eventRow?.insurance_guide_number ?? null
+ const origInsValue = props.eventRow?.insurance_value != null ? Number(props.eventRow.insurance_value) : null
+
+ function applyConvenio () {
+ const origPsId = props.eventRow?.insurance_plan_service_id ?? null
+ _restoringConvenio.value = true
+ form.value.insurance_plan_id = origPlanId
+ form.value.insurance_guide_number = origGuide
+ form.value.insurance_value = origInsValue
+ form.value.insurance_plan_service_id = origPsId
+ billingType.value = 'convenio'
+ nextTick(() => {
+ if (origPsId && planServices.value.find(s => s.id === origPsId)) {
+ selectedPlanService.value = origPsId
+ } else {
+ selectedPlanService.value = null
+ }
+ _restoringConvenio.value = false
+ })
+ }
+
+ if (!eventId && !ruleId) {
+ commitmentItems.value = []
+ if (origPlanId) applyConvenio()
+ else billingType.value = 'particular'
+ return
+ }
try {
commitmentItems.value = ruleId
? await _csLoadItemsOrTemplate(eventId, ruleId, { allowEmpty: isCustomized })
: await _csLoadItems(eventId)
- billingType.value = commitmentItems.value.length > 0 ? 'pago' : 'gratuito'
+ if (origPlanId) applyConvenio()
+ else billingType.value = commitmentItems.value.length > 0 ? 'particular' : 'gratuito'
} catch (e) {
console.warn('[AgendaEventDialog] commitment_services load error:', e?.message)
commitmentItems.value = []
- billingType.value = 'gratuito'
+ if (origPlanId) applyConvenio()
+ else billingType.value = 'gratuito'
}
}
@@ -1675,23 +1793,25 @@ watch(
// Pré-carrega serviços para auto-fill de preço
ensureServicesLoaded()
- if (props.ownerId) {
- await loadInsurancePlans(props.ownerId)
- // Se já tem convênio selecionado (edição), aplica o valor padrão agora que os planos estão carregados
- const planId = form.value.insurance_plan_id
- if (planId && !form.value.insurance_value) {
- const plan = insurancePlans.value.find(p => p.id === planId)
- if (plan?.default_value) form.value.insurance_value = plan.default_value
- }
+ const insuranceOwner = props.planOwnerId || props.ownerId
+ if (insuranceOwner) {
+ await loadInsurancePlans(insuranceOwner)
+ // planServices é computed — atualiza automaticamente após insurancePlans carregar
}
+ // Reset convênio (será restaurado por _loadCommitmentItemsForEvent se necessário)
+ selectedPlanService.value = null
+ _restoringConvenio.value = false
+
// Reset e carrega itens de serviço do evento (commitment_services)
commitmentItems.value = []
servicePickerSel.value = null
- billingType.value = 'pago'
// Carrega serviços para eventos reais (form.value.id) ou template para ocorrências virtuais (só recurrence_id)
+ // _loadCommitmentItemsForEvent determina o billingType correto (incluindo 'convenio')
if (isEdit.value && (form.value.id || props.eventRow?.recurrence_id)) {
_loadCommitmentItemsForEvent(form.value.id)
+ } else {
+ billingType.value = 'particular'
}
}
)
@@ -1718,12 +1838,28 @@ watch(
watch(
() => form.value.insurance_plan_id,
(planId) => {
- if (!planId) { form.value.insurance_value = null; return }
- const plan = insurancePlans.value.find(p => p.id === planId)
- if (plan?.default_value) form.value.insurance_value = plan.default_value
+ if (_restoringConvenio.value) return
+ selectedPlanService.value = null
+ form.value.insurance_plan_service_id = null
+ if (!planId) {
+ // Limpa campos de convênio ao desmarcar
+ form.value.insurance_value = null
+ form.value.insurance_guide_number = null
+ return
+ }
+ // Ao selecionar convênio: exclusividade — remove serviços do caminho A
+ commitmentItems.value = []
+ servicePickerSel.value = null
}
)
+function onProcedureSelect (psId) {
+ form.value.insurance_plan_service_id = psId ?? null
+ if (!psId) { form.value.insurance_value = null; return }
+ const ps = planServices.value.find(s => s.id === psId)
+ form.value.insurance_value = ps?.value != null ? Number(ps.value) : null
+}
+
watch(
() => [form.value.paciente_id, form.value.dia?.toString()],
async () => {
@@ -1779,6 +1915,12 @@ function goToAgendamentosRecebidos () {
router.push(`${prefix}/agendamentos-recebidos`)
}
+function goToConveniosConfig () {
+ visible.value = false
+ const prefix = route.path.startsWith('/admin') ? '/admin' : '/therapist'
+ router.push(`${prefix}/configuracoes/convenios`)
+}
+
// ── duração / slots ────────────────────────────────────────
// grouped duration options: default preset first, then com pausa, then sem pausa
const duracaoOptions = computed(() => {
@@ -2260,7 +2402,7 @@ const canSave = computed(() => {
if (!form.value.startTime) return false
if (!form.value.commitment_id) return false
if (requiresPatient.value && !form.value.paciente_id) return false
- if (isSessionEvent.value && billingType.value === 'pago' && commitmentItems.value.length === 0) return false
+ if (isSessionEvent.value && billingType.value === 'particular' && commitmentItems.value.length === 0) return false
return true
})
@@ -2294,9 +2436,10 @@ function onSave () {
titulo_custom: form.value.titulo_custom || null,
extra_fields: Object.keys(form.value.extra_fields || {}).length ? form.value.extra_fields : null,
price: isSessionEvent.value ? (form.value.price ?? null) : null,
- insurance_plan_id: isSessionEvent.value ? (form.value.insurance_plan_id ?? null) : null,
- insurance_guide_number: isSessionEvent.value ? (form.value.insurance_guide_number ?? null) : null,
- insurance_value: isSessionEvent.value ? (form.value.insurance_value ?? null) : null,
+ insurance_plan_id: isSessionEvent.value ? (form.value.insurance_plan_id ?? null) : null,
+ insurance_guide_number: isSessionEvent.value ? (form.value.insurance_guide_number ?? null) : null,
+ insurance_value: isSessionEvent.value ? (form.value.insurance_value ?? null) : null,
+ insurance_plan_service_id: isSessionEvent.value ? (form.value.insurance_plan_service_id ?? null) : null,
}
// recorrência — só quando é sessão e não avulsa
@@ -2375,7 +2518,7 @@ function onDelete () {
: 'Esta sessão faz parte de uma série. O que deseja remover?',
icon: isTodos ? 'pi pi-trash' : 'pi pi-exclamation-triangle',
acceptClass: 'p-button-danger',
- acceptLabel: isTodos ? 'Sim, encerrar série' : (editScopeOptions.find(o => o.value === editScope.value)?.label || 'Excluir'),
+ acceptLabel: isTodos ? 'Sim, encerrar série' : (editScopeOptions.value.find(o => o.value === editScope.value)?.label || 'Excluir'),
rejectLabel: 'Cancelar',
accept: () => emit('delete', {
id: form.value.id,
@@ -2478,9 +2621,10 @@ function resetForm () {
conflito: null,
extra_fields: r?.extra_fields && typeof r.extra_fields === 'object' ? { ...r.extra_fields } : {},
price: r?.price != null ? Number(r.price) : null,
- insurance_plan_id: r?.insurance_plan_id ?? null,
- insurance_guide_number: r?.insurance_guide_number ?? null,
- insurance_value: r?.insurance_value != null ? Number(r.insurance_value) : null,
+ insurance_plan_id: r?.insurance_plan_id ?? null,
+ insurance_guide_number: r?.insurance_guide_number ?? null,
+ insurance_value: r?.insurance_value != null ? Number(r.insurance_value) : null,
+ insurance_plan_service_id: r?.insurance_plan_service_id ?? null,
}
}
diff --git a/src/features/agenda/composables/useAgendaEvents.js b/src/features/agenda/composables/useAgendaEvents.js
index d9f58ac..b8829d6 100644
--- a/src/features/agenda/composables/useAgendaEvents.js
+++ b/src/features/agenda/composables/useAgendaEvents.js
@@ -33,7 +33,7 @@ const BASE_SELECT = `
determined_commitment_id, link_online, extra_fields, modalidade,
recurrence_id, recurrence_date,
mirror_of_event_id, price,
- insurance_plan_id, insurance_guide_number, insurance_value,
+ insurance_plan_id, insurance_guide_number, insurance_value, insurance_plan_service_id,
patients!agenda_eventos_patient_id_fkey (
id, nome_completo, avatar_url
),
diff --git a/src/features/agenda/composables/useInsurancePlans.js b/src/features/agenda/composables/useInsurancePlans.js
index 111339f..4c5c871 100644
--- a/src/features/agenda/composables/useInsurancePlans.js
+++ b/src/features/agenda/composables/useInsurancePlans.js
@@ -1,15 +1,17 @@
// src/features/agenda/composables/useInsurancePlans.js
//
-// CRUD sobre a tabela public.insurance_plans.
-//
// Interface pública:
-// plans – ref([]) lista de planos ativos do owner
-// loading – ref(false)
-// error – ref('')
+// plans – ref([]) todos os planos do owner (ativos e inativos)
+// loading – ref(false)
+// error – ref(null)
//
-// load(ownerId) – carrega todos os planos ativos
-// save(payload) – cria ou atualiza (id presente = update)
-// remove(id) – soft-delete (active = false)
+// load(ownerId) – carrega planos com seus procedimentos
+// save(payload) – insert ou update do plano (name, notes)
+// toggle(id, active) – alterna active do plano
+// remove(id) – soft-delete do plano
+// savePlanService(payload) – insert ou update de procedimento { id?, insurance_plan_id, name, value }
+// togglePlanService(id, active) – alterna active do procedimento
+// removePlanService(id) – DELETE definitivo do procedimento
import { ref } from 'vue'
import { supabase } from '@/lib/supabase/client'
@@ -17,24 +19,27 @@ import { supabase } from '@/lib/supabase/client'
export function useInsurancePlans () {
const plans = ref([])
const loading = ref(false)
- const error = ref('')
+ const error = ref(null)
async function load (ownerId) {
if (!ownerId) return
loading.value = true
- error.value = ''
+ error.value = null
try {
const { data, error: err } = await supabase
.from('insurance_plans')
- .select('id, name, notes, default_value, active')
+ .select(`
+ *,
+ insurance_plan_services (
+ id, name, value, active
+ )
+ `)
.eq('owner_id', ownerId)
- .eq('active', true)
- .order('name', { ascending: true })
-
+ .order('name')
if (err) throw err
plans.value = data || []
} catch (e) {
- error.value = e?.message || 'Falha ao carregar convênios.'
+ error.value = e?.message || 'Erro ao carregar convênios'
plans.value = []
} finally {
loading.value = false
@@ -42,42 +47,141 @@ export function useInsurancePlans () {
}
async function save (payload) {
- error.value = ''
+ error.value = null
try {
if (payload.id) {
- const { id, owner_id, tenant_id, ...fields } = payload
const { error: err } = await supabase
.from('insurance_plans')
- .update(fields)
- .eq('id', id)
- .eq('owner_id', owner_id)
+ .update({
+ name: payload.name,
+ notes: payload.notes || null,
+ updated_at: new Date().toISOString(),
+ })
+ .eq('id', payload.id)
if (err) throw err
} else {
const { error: err } = await supabase
.from('insurance_plans')
- .insert(payload)
+ .insert({
+ owner_id: payload.owner_id,
+ tenant_id: payload.tenant_id,
+ name: payload.name,
+ notes: payload.notes || null,
+ })
if (err) throw err
}
} catch (e) {
- error.value = e?.message || 'Falha ao salvar convênio.'
+ error.value = e?.message || 'Erro ao salvar convênio'
+ throw e
+ }
+ }
+
+ async function toggle (id, active) {
+ error.value = null
+ try {
+ const { error: err } = await supabase
+ .from('insurance_plans')
+ .update({ active })
+ .eq('id', id)
+ if (err) throw err
+ const plan = plans.value.find(p => p.id === id)
+ if (plan) plan.active = active
+ } catch (e) {
+ error.value = e?.message || 'Erro ao atualizar convênio'
throw e
}
}
async function remove (id) {
- error.value = ''
+ error.value = null
try {
const { error: err } = await supabase
.from('insurance_plans')
.update({ active: false })
.eq('id', id)
if (err) throw err
- plans.value = plans.value.filter(p => p.id !== id)
+ const plan = plans.value.find(p => p.id === id)
+ if (plan) plan.active = false
} catch (e) {
- error.value = e?.message || 'Falha ao remover convênio.'
+ error.value = e?.message || 'Erro ao remover convênio'
throw e
}
}
- return { plans, loading, error, load, save, remove }
+ async function savePlanService (payload) {
+ error.value = null
+ try {
+ if (payload.id) {
+ const { error: err } = await supabase
+ .from('insurance_plan_services')
+ .update({
+ name: payload.name,
+ value: payload.value,
+ })
+ .eq('id', payload.id)
+ if (err) throw err
+ } else {
+ const { error: err } = await supabase
+ .from('insurance_plan_services')
+ .insert({
+ insurance_plan_id: payload.insurance_plan_id,
+ name: payload.name,
+ value: payload.value,
+ })
+ if (err) throw err
+ }
+ } catch (e) {
+ error.value = e?.message || 'Erro ao salvar procedimento'
+ throw e
+ }
+ }
+
+ async function togglePlanService (id, active) {
+ error.value = null
+ try {
+ const { error: err } = await supabase
+ .from('insurance_plan_services')
+ .update({ active })
+ .eq('id', id)
+ if (err) throw err
+ } catch (e) {
+ error.value = e?.message || 'Erro ao atualizar procedimento'
+ throw e
+ }
+ }
+
+ async function removeDefinitivo (id) {
+ error.value = null
+ try {
+ const { error: err } = await supabase
+ .from('insurance_plans')
+ .delete()
+ .eq('id', id)
+ if (err) throw err
+ plans.value = plans.value.filter(p => p.id !== id)
+ } catch (e) {
+ error.value = e?.message || 'Erro ao remover convênio'
+ throw e
+ }
+ }
+
+ async function removePlanService (id) {
+ error.value = null
+ try {
+ const { error: err } = await supabase
+ .from('insurance_plan_services')
+ .delete()
+ .eq('id', id)
+ if (err) throw err
+ } catch (e) {
+ error.value = e?.message || 'Erro ao remover procedimento'
+ throw e
+ }
+ }
+
+ return {
+ plans, loading, error,
+ load, save, toggle, remove, removeDefinitivo,
+ savePlanService, togglePlanService, removePlanService,
+ }
}
diff --git a/src/features/agenda/composables/useRecurrence.js b/src/features/agenda/composables/useRecurrence.js
index e75652f..245e122 100644
--- a/src/features/agenda/composables/useRecurrence.js
+++ b/src/features/agenda/composables/useRecurrence.js
@@ -308,6 +308,12 @@ function buildOccurrence (rule, date, originalIso, exception) {
extra_fields: exception?.extra_fields || rule.extra_fields || null,
price: rule.price ?? null,
+ // convênio — herdado da regra de recorrência
+ insurance_plan_id: rule.insurance_plan_id ?? null,
+ insurance_guide_number: rule.insurance_guide_number ?? null,
+ insurance_value: rule.insurance_value ?? null,
+ insurance_plan_service_id: rule.insurance_plan_service_id ?? null,
+
// estado da exceção
exception_type: exType,
exception_id: exception?.id || null,
diff --git a/src/features/agenda/composables/useServices.js b/src/features/agenda/composables/useServices.js
index b203930..a076181 100644
--- a/src/features/agenda/composables/useServices.js
+++ b/src/features/agenda/composables/useServices.js
@@ -1,17 +1,16 @@
// src/features/agenda/composables/useServices.js
//
-// CRUD completo sobre a tabela public.services.
-//
// Interface pública:
-// services – ref([]) lista de serviços ativos do owner
-// loading – ref(false)
-// error – ref('')
+// services – ref([]) todos os serviços do owner (ativos e inativos)
+// loading – ref(false)
+// error – ref('')
//
-// load(ownerId) – carrega todos os serviços ativos
-// save(payload) – cria ou atualiza (id presente = update)
-// remove(id) – soft-delete (active = false)
-// getDefaultPrice() – preço do primeiro serviço ativo, ou null
-// getPriceFor(serviceId) – preço de um serviço específico, ou null
+// load(ownerId) – carrega todos os serviços (ativos e inativos)
+// save(payload) – cria ou atualiza
+// toggle(id, active) – alterna active
+// remove(id) – DELETE definitivo
+// getDefaultPrice() – preço do primeiro serviço ativo, ou null
+// getPriceFor(id) – preço de um serviço específico, ou null
import { ref } from 'vue'
import { supabase } from '@/lib/supabase/client'
@@ -21,7 +20,6 @@ export function useServices () {
const loading = ref(false)
const error = ref('')
- // ── Carregar serviços ativos do owner ───────────────────────────────
async function load (ownerId) {
if (!ownerId) return
loading.value = true
@@ -31,7 +29,6 @@ export function useServices () {
.from('services')
.select('id, name, description, price, duration_min, active')
.eq('owner_id', ownerId)
- .eq('active', true)
.order('created_at', { ascending: true })
if (err) throw err
@@ -44,9 +41,6 @@ export function useServices () {
}
}
- // ── Criar ou atualizar um serviço ───────────────────────────────────
- // payload deve conter: { owner_id, tenant_id, name, price, description?, duration_min? }
- // Se payload.id estiver presente, faz UPDATE; caso contrário, INSERT.
async function save (payload) {
error.value = ''
try {
@@ -70,40 +64,54 @@ export function useServices () {
}
}
- // ── Soft-delete: marca active = false ───────────────────────────────
+ async function toggle (id, active) {
+ error.value = ''
+ try {
+ const { error: err } = await supabase
+ .from('services')
+ .update({ active })
+ .eq('id', id)
+ if (err) throw err
+ const svc = services.value.find(s => s.id === id)
+ if (svc) svc.active = active
+ } catch (e) {
+ error.value = e?.message || 'Falha ao atualizar serviço.'
+ throw e
+ }
+ }
+
async function remove (id) {
error.value = ''
try {
const { error: err } = await supabase
.from('services')
- .update({ active: false })
+ .delete()
.eq('id', id)
if (err) throw err
services.value = services.value.filter(s => s.id !== id)
} catch (e) {
- error.value = e?.message || 'Falha ao remover serviço.'
+ const msg = String(e?.message || '')
+ if (msg.includes('commitment_services_service_id_fkey') || msg.includes('violates foreign key constraint')) {
+ error.value = 'Este serviço está vinculado a sessões e não pode ser removido. Use Desativar para ocultá-lo.'
+ } else {
+ error.value = e?.message || 'Falha ao remover serviço.'
+ }
throw e
}
}
- // ── Helpers de preço ────────────────────────────────────────────────
-
- // Retorna o preço de um serviço específico (serviceId fornecido) ou
- // o preço do primeiro serviço ativo da lista (serviceId omitido).
- // Retorna null se não houver serviços ou o id não for encontrado.
function getDefaultPrice (serviceId) {
if (serviceId) {
const svc = services.value.find(s => s.id === serviceId)
return svc?.price != null ? Number(svc.price) : null
}
- const first = services.value[0]
+ const first = services.value.find(s => s.active)
return first?.price != null ? Number(first.price) : null
}
- // Alias explícito para clareza nos chamadores que conhecem o id
function getPriceFor (serviceId) {
return getDefaultPrice(serviceId)
}
- return { services, loading, error, load, save, remove, getDefaultPrice, getPriceFor }
+ return { services, loading, error, load, save, toggle, remove, getDefaultPrice, getPriceFor }
}
diff --git a/src/features/agenda/pages/AgendaClinicaPage.vue b/src/features/agenda/pages/AgendaClinicaPage.vue
index 3e2880e..637039d 100644
--- a/src/features/agenda/pages/AgendaClinicaPage.vue
+++ b/src/features/agenda/pages/AgendaClinicaPage.vue
@@ -403,6 +403,7 @@
:initialStartISO="dialogStartISO"
:initialEndISO="dialogEndISO"
:ownerId="dialogOwnerId || clinicOwnerId"
+ :planOwnerId="clinicOwnerId"
:tenantId="tenantId || ''"
:allowOwnerEdit="false"
:ownerOptions="ownerOptions"
@@ -1071,7 +1072,7 @@ async function loadMonthSearchRows () {
try {
const { data, error } = await supabase
.from('agenda_eventos')
- .select('id, owner_id, tenant_id, tipo, status, titulo, inicio_em, fim_em, observacoes, modalidade, determined_commitment_id, insurance_plan_id, insurance_guide_number, insurance_value, patients!agenda_eventos_patient_id_fkey(nome_completo)')
+ .select('id, owner_id, tenant_id, tipo, status, titulo, inicio_em, fim_em, observacoes, modalidade, determined_commitment_id, insurance_plan_id, insurance_guide_number, insurance_value, insurance_plan_service_id, patients!agenda_eventos_patient_id_fkey(nome_completo)')
.eq('tenant_id', tid)
.in('owner_id', ids)
.is('mirror_of_event_id', null)
@@ -1386,9 +1387,10 @@ async function onEventClick (info) {
titulo_custom: ep.titulo_custom ?? null,
extra_fields: ep.extra_fields ?? null,
price: ep.price != null ? Number(ep.price) : null,
- insurance_plan_id: ep.insurance_plan_id ?? null,
- insurance_guide_number: ep.insurance_guide_number ?? null,
- insurance_value: ep.insurance_value != null ? Number(ep.insurance_value) : null,
+ insurance_plan_id: ep.insurance_plan_id ?? null,
+ insurance_guide_number: ep.insurance_guide_number ?? null,
+ insurance_value: ep.insurance_value != null ? Number(ep.insurance_value) : null,
+ insurance_plan_service_id: ep.insurance_plan_service_id ?? null,
// ── recorrência (nova arquitetura) ──────────────────────────
recurrence_id: ep.recurrenceId ?? ep.recurrence_id ?? ep.serie_id ?? null,
original_date: ep.originalDate ?? ep.original_date ?? ep.recurrence_date ?? null,
@@ -1524,6 +1526,7 @@ function pickDbFields (obj) {
'insurance_plan_id',
'insurance_guide_number',
'insurance_value',
+ 'insurance_plan_service_id',
]
const out = {}
for (const k of allowed) {
@@ -1673,7 +1676,8 @@ async function onDialogSave (arg) {
}
// ── CASO C / C2: criação RECORRENTE (novo ou evento existente) ─────────
- if (recorrencia?.tipo === 'recorrente') {
+ // Só cria nova regra se NÃO há série existente — se houver recurrenceId, cai para F/G/E
+ if (recorrencia?.tipo === 'recorrente' && !recurrenceId) {
const startDate = new Date(basePayload.inicio_em)
const tipoFreq = recorrencia.tipoFreq ?? 'semanal'
const dow = recorrencia.diaSemana ?? startDate.getDay()
@@ -1709,6 +1713,11 @@ async function onDialogSave (arg) {
titulo_custom: basePayload.titulo_custom ?? null,
observacoes: basePayload.observacoes ?? null,
extra_fields: basePayload.extra_fields ?? null,
+ price: basePayload.price ?? null,
+ insurance_plan_id: basePayload.insurance_plan_id ?? null,
+ insurance_guide_number: basePayload.insurance_guide_number ?? null,
+ insurance_value: basePayload.insurance_value ?? null,
+ insurance_plan_service_id: basePayload.insurance_plan_service_id ?? null,
status: 'ativo',
}
const createdRule = await createRule(rule)
@@ -1797,6 +1806,11 @@ async function onDialogSave (arg) {
modalidade: basePayload.modalidade ?? 'presencial',
observacoes: basePayload.observacoes ?? null,
extra_fields: basePayload.extra_fields ?? null,
+ price: basePayload.price ?? null,
+ insurance_plan_id: basePayload.insurance_plan_id ?? null,
+ insurance_guide_number: basePayload.insurance_guide_number ?? null,
+ insurance_value: basePayload.insurance_value ?? null,
+ insurance_plan_service_id: basePayload.insurance_plan_service_id ?? null,
}, { tenantId: tid })
eventId = mat.id
}
@@ -1825,7 +1839,12 @@ async function onDialogSave (arg) {
titulo_custom: basePayload.titulo_custom ?? null,
observacoes: basePayload.observacoes ?? null,
extra_fields: basePayload.extra_fields ?? null,
- })
+ price: basePayload.price ?? null,
+ insurance_plan_id: basePayload.insurance_plan_id ?? null,
+ insurance_guide_number: basePayload.insurance_guide_number ?? null,
+ insurance_value: basePayload.insurance_value ?? null,
+ insurance_plan_service_id: basePayload.insurance_plan_service_id ?? null,
+ }
// Opção C — atualizar template e propagar para a nova sub-série
const serviceItemsE = arg.serviceItems
@@ -1853,8 +1872,29 @@ async function onDialogSave (arg) {
titulo_custom: basePayload.titulo_custom ?? null,
observacoes: basePayload.observacoes ?? null,
extra_fields: basePayload.extra_fields ?? null,
+ price: basePayload.price ?? null,
+ insurance_plan_id: basePayload.insurance_plan_id ?? null,
+ insurance_guide_number: basePayload.insurance_guide_number ?? null,
+ insurance_value: basePayload.insurance_value ?? null,
+ insurance_plan_service_id: basePayload.insurance_plan_service_id ?? null,
})
+ // Propaga campos não-serviço para sessões já materializadas da série
+ await supabase
+ .from('agenda_eventos')
+ .update({
+ modalidade: basePayload.modalidade ?? 'presencial',
+ titulo_custom: basePayload.titulo_custom ?? null,
+ observacoes: basePayload.observacoes ?? null,
+ extra_fields: basePayload.extra_fields ?? null,
+ price: basePayload.price ?? null,
+ insurance_plan_id: basePayload.insurance_plan_id ?? null,
+ insurance_guide_number: basePayload.insurance_guide_number ?? null,
+ insurance_value: basePayload.insurance_value ?? null,
+ insurance_plan_service_id: basePayload.insurance_plan_service_id ?? null,
+ })
+ .eq('recurrence_id', recurrenceId)
+
// Opção C — atualizar template e propagar para toda a série
const serviceItemsF = arg.serviceItems
if (recurrenceId && serviceItemsF?.length) {
@@ -1881,8 +1921,30 @@ async function onDialogSave (arg) {
titulo_custom: basePayload.titulo_custom ?? null,
observacoes: basePayload.observacoes ?? null,
extra_fields: basePayload.extra_fields ?? null,
+ price: basePayload.price ?? null,
+ insurance_plan_id: basePayload.insurance_plan_id ?? null,
+ insurance_guide_number: basePayload.insurance_guide_number ?? null,
+ insurance_value: basePayload.insurance_value ?? null,
+ insurance_plan_service_id: basePayload.insurance_plan_service_id ?? null,
})
+ // Propaga TODOS os campos para TODAS as sessões materializadas (sem exceção)
+ await supabase
+ .from('agenda_eventos')
+ .update({
+ modalidade: basePayload.modalidade ?? 'presencial',
+ titulo_custom: basePayload.titulo_custom ?? null,
+ observacoes: basePayload.observacoes ?? null,
+ extra_fields: basePayload.extra_fields ?? null,
+ price: basePayload.price ?? null,
+ insurance_plan_id: basePayload.insurance_plan_id ?? null,
+ insurance_guide_number: basePayload.insurance_guide_number ?? null,
+ insurance_value: basePayload.insurance_value ?? null,
+ insurance_plan_service_id: basePayload.insurance_plan_service_id ?? null,
+ services_customized: false,
+ })
+ .eq('recurrence_id', recurrenceId)
+
// Propaga para todos — incluindo services_customized=true — e reseta o flag
const serviceItemsG = arg.serviceItems
if (recurrenceId && serviceItemsG?.length) {
@@ -1890,12 +1952,6 @@ async function onDialogSave (arg) {
await propagateToSerie(recurrenceId, serviceItemsG, { ignoreCustomized: true })
}
- // Reseta services_customized para false em todos os eventos da série
- await supabase
- .from('agenda_eventos')
- .update({ services_customized: false })
- .eq('recurrence_id', recurrenceId)
-
if (id) await arg.onSaved?.(id)
toast.add({ severity: 'success', summary: 'Salvo', detail: 'Todas as sessões da série foram atualizadas (sem exceção).', life: 2500 })
@@ -1922,6 +1978,16 @@ async function onDialogSave (arg) {
}
} catch (e) {
+ const msg = String(e?.message || '')
+ if (msg.includes('recurrence_rules_dates_chk') || (msg.includes('violates check constraint') && msg.includes('recurrence_rules'))) {
+ toast.add({
+ severity: 'warn',
+ summary: 'Não foi possível dividir a série',
+ detail: 'Esta é a primeira sessão da série. Para alterar todas as ocorrências, selecione "Todos" ou "Todos sem exceção".',
+ life: 6000
+ })
+ return
+ }
toast.add({ severity: 'warn', summary: 'Erro', detail: eventsError.value || e?.message || 'Falha ao salvar.', life: 4500 })
}
}
diff --git a/src/features/agenda/pages/AgendaTerapeutaPage.vue b/src/features/agenda/pages/AgendaTerapeutaPage.vue
index 4e362c9..35911ae 100644
--- a/src/features/agenda/pages/AgendaTerapeutaPage.vue
+++ b/src/features/agenda/pages/AgendaTerapeutaPage.vue
@@ -950,7 +950,7 @@ async function loadMonthSearchRows () {
try {
const { data, error } = await supabase
.from('agenda_eventos')
- .select('id, owner_id, tipo, status, titulo, inicio_em, fim_em, observacoes, modalidade, determined_commitment_id, insurance_plan_id, insurance_guide_number, insurance_value, patients!agenda_eventos_patient_id_fkey(nome_completo)')
+ .select('id, owner_id, tipo, status, titulo, inicio_em, fim_em, observacoes, modalidade, determined_commitment_id, insurance_plan_id, insurance_guide_number, insurance_value, insurance_plan_service_id, patients!agenda_eventos_patient_id_fkey(nome_completo)')
.eq('owner_id', uid)
.is('mirror_of_event_id', null)
.gte('inicio_em', start)
@@ -1575,9 +1575,10 @@ function onEventClick (info) {
titulo_custom: ep.titulo_custom ?? null,
extra_fields: ep.extra_fields ?? null,
price: ep.price != null ? Number(ep.price) : null,
- insurance_plan_id: ep.insurance_plan_id ?? null,
- insurance_guide_number: ep.insurance_guide_number ?? null,
- insurance_value: ep.insurance_value != null ? Number(ep.insurance_value) : null,
+ insurance_plan_id: ep.insurance_plan_id ?? null,
+ insurance_guide_number: ep.insurance_guide_number ?? null,
+ insurance_value: ep.insurance_value != null ? Number(ep.insurance_value) : null,
+ insurance_plan_service_id: ep.insurance_plan_service_id ?? null,
// ── recorrência (nova arquitetura) ──────────────────────────
recurrence_id: ep.recurrence_id ?? ep.recurrenceId ?? ep.serie_id ?? null,
original_date: ep.original_date ?? ep.originalDate ?? ep.recurrence_date ?? null,
@@ -1672,6 +1673,7 @@ function pickDbFields (obj) {
'insurance_plan_id',
'insurance_guide_number',
'insurance_value',
+ 'insurance_plan_service_id',
]
const out = {}
for (const k of allowed) {
@@ -1828,7 +1830,8 @@ async function onDialogSave (arg) {
}
// ── CASO C / C2: criação RECORRENTE (novo ou evento existente) ─────────
- if (recorrencia?.tipo === 'recorrente') {
+ // Só cria nova regra se NÃO há série existente — se houver recurrenceId, cai para F/G/E
+ if (recorrencia?.tipo === 'recorrente' && !recurrenceId) {
const startDate = new Date(normalized.inicio_em)
const tipoFreq = recorrencia.tipoFreq ?? 'semanal'
const dow = recorrencia.diaSemana ?? startDate.getDay()
@@ -1867,6 +1870,10 @@ async function onDialogSave (arg) {
observacoes: normalized.observacoes ?? null,
extra_fields: normalized.extra_fields ?? null,
price: normalized.price ?? null,
+ insurance_plan_id: normalized.insurance_plan_id ?? null,
+ insurance_guide_number: normalized.insurance_guide_number ?? null,
+ insurance_value: normalized.insurance_value ?? null,
+ insurance_plan_service_id: normalized.insurance_plan_service_id ?? null,
status: 'ativo',
}
const createdRule = await createRule(rule)
@@ -1974,6 +1981,11 @@ async function onDialogSave (arg) {
modalidade: normalized.modalidade ?? 'presencial',
observacoes: normalized.observacoes ?? null,
extra_fields: normalized.extra_fields ?? null,
+ price: normalized.price ?? null,
+ insurance_plan_id: normalized.insurance_plan_id ?? null,
+ insurance_guide_number: normalized.insurance_guide_number ?? null,
+ insurance_value: normalized.insurance_value ?? null,
+ insurance_plan_service_id: normalized.insurance_plan_service_id ?? null,
})
eventId = mat.id
}
@@ -2002,6 +2014,11 @@ async function onDialogSave (arg) {
titulo_custom: normalized.titulo_custom ?? null,
observacoes: normalized.observacoes ?? null,
extra_fields: normalized.extra_fields ?? null,
+ price: normalized.price ?? null,
+ insurance_plan_id: normalized.insurance_plan_id ?? null,
+ insurance_guide_number: normalized.insurance_guide_number ?? null,
+ insurance_value: normalized.insurance_value ?? null,
+ insurance_plan_service_id: normalized.insurance_plan_service_id ?? null,
})
// Opção C — atualizar template e propagar para a nova sub-série
@@ -2030,8 +2047,29 @@ async function onDialogSave (arg) {
titulo_custom: normalized.titulo_custom ?? null,
observacoes: normalized.observacoes ?? null,
extra_fields: normalized.extra_fields ?? null,
+ price: normalized.price ?? null,
+ insurance_plan_id: normalized.insurance_plan_id ?? null,
+ insurance_guide_number: normalized.insurance_guide_number ?? null,
+ insurance_value: normalized.insurance_value ?? null,
+ insurance_plan_service_id: normalized.insurance_plan_service_id ?? null,
})
+ // Propaga campos não-serviço para sessões já materializadas da série
+ await supabase
+ .from('agenda_eventos')
+ .update({
+ modalidade: normalized.modalidade ?? 'presencial',
+ titulo_custom: normalized.titulo_custom ?? null,
+ observacoes: normalized.observacoes ?? null,
+ extra_fields: normalized.extra_fields ?? null,
+ price: normalized.price ?? null,
+ insurance_plan_id: normalized.insurance_plan_id ?? null,
+ insurance_guide_number: normalized.insurance_guide_number ?? null,
+ insurance_value: normalized.insurance_value ?? null,
+ insurance_plan_service_id: normalized.insurance_plan_service_id ?? null,
+ })
+ .eq('recurrence_id', recurrenceId)
+
// Opção C — atualizar template e propagar para toda a série
const serviceItemsF = arg.serviceItems
if (recurrenceId && serviceItemsF?.length) {
@@ -2058,8 +2096,30 @@ async function onDialogSave (arg) {
titulo_custom: normalized.titulo_custom ?? null,
observacoes: normalized.observacoes ?? null,
extra_fields: normalized.extra_fields ?? null,
+ price: normalized.price ?? null,
+ insurance_plan_id: normalized.insurance_plan_id ?? null,
+ insurance_guide_number: normalized.insurance_guide_number ?? null,
+ insurance_value: normalized.insurance_value ?? null,
+ insurance_plan_service_id: normalized.insurance_plan_service_id ?? null,
})
+ // Propaga TODOS os campos para TODAS as sessões materializadas (sem exceção)
+ await supabase
+ .from('agenda_eventos')
+ .update({
+ modalidade: normalized.modalidade ?? 'presencial',
+ titulo_custom: normalized.titulo_custom ?? null,
+ observacoes: normalized.observacoes ?? null,
+ extra_fields: normalized.extra_fields ?? null,
+ price: normalized.price ?? null,
+ insurance_plan_id: normalized.insurance_plan_id ?? null,
+ insurance_guide_number: normalized.insurance_guide_number ?? null,
+ insurance_value: normalized.insurance_value ?? null,
+ insurance_plan_service_id: normalized.insurance_plan_service_id ?? null,
+ services_customized: false,
+ })
+ .eq('recurrence_id', recurrenceId)
+
// Propaga para todos — incluindo services_customized=true — e reseta o flag
const serviceItemsG = arg.serviceItems
if (recurrenceId && serviceItemsG?.length) {
@@ -2067,12 +2127,6 @@ async function onDialogSave (arg) {
await propagateToSerie(recurrenceId, serviceItemsG, { ignoreCustomized: true })
}
- // Reseta services_customized para false em todos os eventos da série
- await supabase
- .from('agenda_eventos')
- .update({ services_customized: false })
- .eq('recurrence_id', recurrenceId)
-
if (id) await arg.onSaved?.(id)
toast.add({ severity: 'success', summary: 'Salvo', detail: 'Todas as sessões da série foram atualizadas (sem exceção).', life: 2500 })
@@ -2099,6 +2153,17 @@ async function onDialogSave (arg) {
} catch (e) {
const msg = String(e?.message || '')
+
+ if (msg.includes('recurrence_rules_dates_chk') || (msg.includes('violates check constraint') && msg.includes('recurrence_rules'))) {
+ toast.add({
+ severity: 'warn',
+ summary: 'Não foi possível dividir a série',
+ detail: 'Esta é a primeira sessão da série. Para alterar todas as ocorrências, selecione "Todos" ou "Todos sem exceção".',
+ life: 6000
+ })
+ return
+ }
+
const isOverlap =
e?.code === '23P01' ||
msg.includes('agenda_eventos_sem_sobreposicao') ||
diff --git a/src/features/agenda/services/agendaMappers.js b/src/features/agenda/services/agendaMappers.js
index cbd48f3..c86964c 100644
--- a/src/features/agenda/services/agendaMappers.js
+++ b/src/features/agenda/services/agendaMappers.js
@@ -117,9 +117,10 @@ function _mapRow (r) {
// financeiro
price: r.price ?? null,
- insurance_plan_id: r.insurance_plan_id ?? null,
- insurance_guide_number: r.insurance_guide_number ?? null,
- insurance_value: r.insurance_value != null ? Number(r.insurance_value) : null,
+ insurance_plan_id: r.insurance_plan_id ?? null,
+ insurance_guide_number: r.insurance_guide_number ?? null,
+ insurance_value: r.insurance_value != null ? Number(r.insurance_value) : null,
+ insurance_plan_service_id: r.insurance_plan_service_id ?? null,
// timestamps
inicio_em: r.inicio_em,
diff --git a/src/layout/configuracoes/ConfiguracoesConveniosPage.vue b/src/layout/configuracoes/ConfiguracoesConveniosPage.vue
index 5fa1474..6015ddc 100644
--- a/src/layout/configuracoes/ConfiguracoesConveniosPage.vue
+++ b/src/layout/configuracoes/ConfiguracoesConveniosPage.vue
@@ -4,85 +4,150 @@ import { ref, onMounted } from 'vue'
import { supabase } from '@/lib/supabase/client'
import { useTenantStore } from '@/stores/tenantStore'
import { useToast } from 'primevue/usetoast'
+import { useInsurancePlans } from '@/features/agenda/composables/useInsurancePlans'
+import { useServices } from '@/features/agenda/composables/useServices'
const toast = useToast()
const tenantStore = useTenantStore()
+const {
+ plans, loading, error: plansError,
+ load, save, toggle, remove,
+ savePlanService, togglePlanService, removePlanService,
+ removeDefinitivo,
+} = useInsurancePlans()
+
+const { services, load: loadServices } = useServices()
+
const ownerId = ref(null)
const tenantId = ref(null)
const pageLoading = ref(true)
-const plans = ref([])
-
-// ── Formulário ───────────────────────────────────────────────────────
-const emptyForm = () => ({ name: '', notes: '', default_value: null })
+// ── Formulário novo plano ─────────────────────────────────────────────
+const emptyForm = () => ({ name: '', notes: '' })
const newForm = ref(emptyForm())
const addingNew = ref(false)
const savingNew = ref(false)
+// ── Edição inline do plano ────────────────────────────────────────────
const editingId = ref(null)
const editForm = ref({})
const savingEdit = ref(false)
-// ── Load ─────────────────────────────────────────────────────────────
-async function load (uid) {
- const { data, error } = await supabase
- .from('insurance_plans')
- .select('*')
- .eq('owner_id', uid)
- .eq('active', true)
- .order('name')
- if (error) throw error
- plans.value = data || []
-}
+// ── Expansão de planos ────────────────────────────────────────────────
+const expandedPlanId = ref(null)
-// ── Save ─────────────────────────────────────────────────────────────
-async function savePlan (payload) {
- if (payload.id) {
- const { error } = await supabase
- .from('insurance_plans')
- .update({
- name: payload.name.trim(),
- notes: payload.notes?.trim() || null,
- default_value: payload.default_value ?? null,
- updated_at: new Date().toISOString(),
- })
- .eq('id', payload.id)
- if (error) throw error
+function togglePanel (planId) {
+ if (expandedPlanId.value === planId) {
+ expandedPlanId.value = null
+ addingServicePlanId.value = null
} else {
- const { error } = await supabase
- .from('insurance_plans')
- .insert({
- owner_id: ownerId.value,
- tenant_id: tenantId.value,
- name: payload.name.trim(),
- notes: payload.notes?.trim() || null,
- default_value: payload.default_value ?? null,
- })
- if (error) throw error
+ expandedPlanId.value = planId
+ addingServicePlanId.value = null
}
}
-// ── Remove (soft-delete) ─────────────────────────────────────────────
-async function removePlan (id) {
- const { error } = await supabase
- .from('insurance_plans')
- .update({ active: false })
- .eq('id', id)
- if (error) throw error
- plans.value = plans.value.filter(p => p.id !== id)
- toast.add({ severity: 'success', summary: 'Removido', detail: 'Convênio desativado.', life: 3000 })
+// ── Procedimentos ─────────────────────────────────────────────────────
+const addingServicePlanId = ref(null)
+const newServiceForm = ref({ name: '', value: null })
+const savingService = ref(false)
+const editingServiceId = ref(null)
+const editServiceForm = ref({})
+const savingServiceEdit = ref(false)
+
+function startAddService (planId) {
+ addingServicePlanId.value = planId
+ newServiceForm.value = { name: '', value: null }
}
-// ── Edit helpers ──────────────────────────────────────────────────────
+function cancelAddService () {
+ addingServicePlanId.value = null
+ newServiceForm.value = { name: '', value: null }
+}
+
+function fillFromService (svc) {
+ newServiceForm.value.name = svc.name
+ newServiceForm.value.value = Number(svc.price)
+}
+
+async function saveService (planId) {
+ if (!newServiceForm.value.name?.trim() || newServiceForm.value.value == null) {
+ toast.add({ severity: 'warn', summary: 'Campos obrigatórios', detail: 'Nome e valor são obrigatórios.', life: 3000 })
+ return
+ }
+ savingService.value = true
+ try {
+ await savePlanService({
+ insurance_plan_id: planId,
+ name: newServiceForm.value.name.trim(),
+ value: newServiceForm.value.value,
+ })
+ await load(ownerId.value)
+ cancelAddService()
+ toast.add({ severity: 'success', summary: 'Adicionado', detail: 'Procedimento adicionado.', life: 3000 })
+ } catch (e) {
+ toast.add({ severity: 'error', summary: 'Erro', detail: e?.message || 'Falha ao salvar.', life: 4000 })
+ } finally {
+ savingService.value = false
+ }
+}
+
+function startEditService (ps) {
+ editingServiceId.value = ps.id
+ editServiceForm.value = { id: ps.id, name: ps.name, value: Number(ps.value) }
+}
+
+function cancelEditService () {
+ editingServiceId.value = null
+ editServiceForm.value = {}
+}
+
+async function saveServiceEdit () {
+ if (!editServiceForm.value.name?.trim() || editServiceForm.value.value == null) {
+ toast.add({ severity: 'warn', summary: 'Campos obrigatórios', detail: 'Nome e valor são obrigatórios.', life: 3000 })
+ return
+ }
+ savingServiceEdit.value = true
+ try {
+ await savePlanService({
+ id: editServiceForm.value.id,
+ name: editServiceForm.value.name.trim(),
+ value: editServiceForm.value.value,
+ })
+ await load(ownerId.value)
+ cancelEditService()
+ toast.add({ severity: 'success', summary: 'Salvo', detail: 'Procedimento atualizado.', life: 3000 })
+ } catch (e) {
+ toast.add({ severity: 'error', summary: 'Erro', detail: e?.message || 'Falha ao salvar.', life: 4000 })
+ } finally {
+ savingServiceEdit.value = false
+ }
+}
+
+async function onToggleService (ps) {
+ try {
+ await togglePlanService(ps.id, !ps.active)
+ await load(ownerId.value)
+ toast.add({ severity: 'success', summary: ps.active ? 'Desativado' : 'Ativado', detail: `Procedimento ${ps.active ? 'desativado' : 'ativado'}.`, life: 3000 })
+ } catch (e) {
+ toast.add({ severity: 'error', summary: 'Erro', detail: e?.message || 'Falha ao atualizar.', life: 4000 })
+ }
+}
+
+async function deleteService (id) {
+ try {
+ await removePlanService(id)
+ await load(ownerId.value)
+ toast.add({ severity: 'success', summary: 'Removido', detail: 'Procedimento removido.', life: 3000 })
+ } catch (e) {
+ toast.add({ severity: 'error', summary: 'Erro', detail: e?.message || 'Falha ao remover.', life: 4000 })
+ }
+}
+
+// ── Edit helpers plano ────────────────────────────────────────────────
function startEdit (plan) {
editingId.value = plan.id
- editForm.value = {
- id: plan.id,
- name: plan.name,
- notes: plan.notes ?? '',
- default_value: plan.default_value != null ? Number(plan.default_value) : null,
- }
+ editForm.value = { id: plan.id, name: plan.name, notes: plan.notes ?? '' }
}
function cancelEdit () {
@@ -97,7 +162,7 @@ async function saveEdit () {
}
savingEdit.value = true
try {
- await savePlan(editForm.value)
+ await save({ ...editForm.value, name: editForm.value.name.trim(), notes: editForm.value.notes?.trim() || null })
await load(ownerId.value)
cancelEdit()
toast.add({ severity: 'success', summary: 'Salvo', detail: 'Convênio atualizado.', life: 3000 })
@@ -115,11 +180,11 @@ async function saveNew () {
}
savingNew.value = true
try {
- await savePlan(newForm.value)
+ await save({ owner_id: ownerId.value, tenant_id: tenantId.value, name: newForm.value.name.trim(), notes: newForm.value.notes?.trim() || null })
await load(ownerId.value)
newForm.value = emptyForm()
addingNew.value = false
- toast.add({ severity: 'success', summary: 'Adicionado', detail: 'Convênio criado com sucesso.', life: 3000 })
+ toast.add({ severity: 'success', summary: 'Adicionado', detail: 'Convênio criado.', life: 3000 })
} catch (e) {
toast.add({ severity: 'error', summary: 'Erro', detail: e?.message || 'Falha ao criar.', life: 4000 })
} finally {
@@ -127,11 +192,34 @@ async function saveNew () {
}
}
+async function togglePlan (plan) {
+ try {
+ await toggle(plan.id, !plan.active)
+ toast.add({ severity: 'success', summary: plan.active ? 'Desativado' : 'Ativado', detail: `Convênio ${plan.active ? 'desativado' : 'ativado'}.`, life: 3000 })
+ } catch (e) {
+ toast.add({ severity: 'error', summary: 'Erro', detail: e?.message || 'Falha ao atualizar.', life: 4000 })
+ }
+}
+
+async function removePlan (id) {
+ try {
+ await removeDefinitivo(id)
+ if (expandedPlanId.value === id) expandedPlanId.value = null
+ toast.add({ severity: 'success', summary: 'Removido', detail: 'Convênio removido.', life: 3000 })
+ } catch (e) {
+ toast.add({ severity: 'error', summary: 'Erro', detail: e?.message || 'Falha ao remover.', life: 4000 })
+ }
+}
+
function fmtBRL (v) {
if (v == null || v === '') return '—'
return Number(v).toLocaleString('pt-BR', { style: 'currency', currency: 'BRL' })
}
+function totalProcedimentos (plan) {
+ return plan.insurance_plan_services?.length || 0
+}
+
// ── Mount ─────────────────────────────────────────────────────────────
onMounted(async () => {
try {
@@ -139,7 +227,7 @@ onMounted(async () => {
if (!uid) return
ownerId.value = uid
tenantId.value = tenantStore.activeTenantId || null
- await load(uid)
+ await Promise.all([load(uid), loadServices(uid)])
} catch (e) {
toast.add({ severity: 'error', summary: 'Erro', detail: e?.message || 'Falha ao carregar.', life: 4000 })
} finally {
@@ -164,7 +252,7 @@ onMounted(async () => {
Convênios
- Cadastre os convênios que você atende e seus valores de tabela.
+ Cadastre os convênios que você atende e seus procedimentos com valores de tabela.