Ajuste Convenios e Particular

This commit is contained in:
Leonardo
2026-03-13 21:09:34 -03:00
parent 06fb369beb
commit 587079e414
13 changed files with 971 additions and 277 deletions

View File

@@ -506,72 +506,36 @@
:options="editScopeOptions"
optionLabel="label"
optionValue="value"
:optionDisabled="o => !!o.disabled"
class="w-full"
size="small"
/>
<Message v-if="isFirstOccurrence" severity="info" :closable="false" class="mt-2">
<span class="text-sm">
Esta é a primeira sessão da série.
Para alterar todas as ocorrências, use <b>Todos</b> ou <b>Todos sem exceção</b>.
</span>
</Message>
</div>
<!-- CONVÊNIO ( sessão) -->
<!-- FINANCEIRO ( sessão) -->
<div v-if="isSessionEvent" class="side-card mb-3">
<div class="side-card__title mb-2">Convênio</div>
<div class="flex flex-col gap-2">
<Select
v-model="form.insurance_plan_id"
:options="insurancePlans"
optionLabel="name"
optionValue="id"
placeholder="Particular (sem convênio)"
showClear
class="w-full"
size="small"
/>
<template v-if="hasInsurance">
<div class="flex gap-2">
<div class="flex-1">
<label class="text-xs text-color-secondary mb-1 block"> da Guia</label>
<InputText
v-model="form.insurance_guide_number"
placeholder="Ex: 123456789"
class="w-full"
size="small"
/>
</div>
<div class="flex-1">
<label class="text-xs text-color-secondary mb-1 block">Valor do Convênio (R$)</label>
<InputNumber
v-model="form.insurance_value"
mode="currency"
currency="BRL"
locale="pt-BR"
class="w-full"
size="small"
/>
</div>
</div>
</template>
</div>
</div>
<!-- RECORRÊNCIA ( sessão) -->
<div v-if="isSessionEvent" class="side-card">
<!-- SelectButton: Gratuito / Particular / Convênio -->
<SelectButton
v-model="billingType"
:options="billingTypeOptions"
optionLabel="label"
optionValue="value"
class="w-full mb-3"
/>
<!-- Serviços / Valor da sessão -->
<div class="mb-3">
<!-- SelectButton Gratuito/Pago -->
<SelectButton
v-model="billingType"
:options="billingTypeOptions"
optionLabel="label"
optionValue="value"
class="mb-3 w-full"
/>
<!-- Seletor de serviço ( quando Pago e serviços cadastrados) -->
<div v-if="billingType === 'pago' && services.length" class="flex gap-2 mb-2">
<!-- PARTICULAR: seletor de serviço -->
<template v-if="billingType === 'particular'">
<div v-if="services.filter(s => s.active).length" class="flex gap-2 mb-2">
<Select
v-model="servicePickerSel"
:options="services"
:options="services.filter(s => s.active)"
optionLabel="name"
optionValue="id"
placeholder="Adicionar serviço..."
@@ -580,6 +544,87 @@
@update:modelValue="(id) => { addItem(services.find(s => s.id === id)); servicePickerSel = null }"
/>
</div>
</template>
<!-- CONVÊNIO: fluxo progressivo -->
<template v-if="billingType === 'convenio'">
<!-- PASSO 1 Select do plano (sempre visível) -->
<Select
v-model="form.insurance_plan_id"
:options="activePlans"
optionLabel="name"
optionValue="id"
placeholder="Selecionar convênio..."
showClear
class="w-full mb-2"
size="small"
/>
<!-- PASSO 2 Procedimento (visível quando plano selecionado) -->
<template v-if="hasInsurance">
<template v-if="planServices.length > 0">
<label class="text-xs text-color-secondary mb-1 block">Procedimento</label>
<Select
v-model="selectedPlanService"
:options="planServices"
optionLabel="name"
optionValue="id"
placeholder="Selecionar procedimento..."
showClear
class="w-full mb-2"
size="small"
@update:modelValue="onProcedureSelect"
/>
</template>
<div v-else class="flex items-center justify-between gap-2 mb-2 p-2 rounded-lg bg-surface-100">
<span class="text-xs text-color-secondary">Este convênio não tem procedimentos cadastrados.</span>
<Button
label="Cadastrar"
icon="pi pi-plus"
size="small"
severity="secondary"
outlined
class="rounded-full shrink-0 text-xs h-7"
@click="goToConveniosConfig"
/>
</div>
<!-- PASSO 3 da Guia + Valor (visível após selecionar procedimento ou sem procedimentos) -->
<template v-if="selectedPlanService != null || planServices.length === 0">
<div class="flex gap-2">
<div class="flex-1">
<label class="text-xs text-color-secondary mb-1 block"> da Guia</label>
<InputText
v-model="form.insurance_guide_number"
placeholder="Ex: 123456789"
class="w-full"
size="small"
/>
</div>
<div class="flex-1">
<label class="text-xs text-color-secondary mb-1 block">Valor (R$)</label>
<InputNumber
v-model="form.insurance_value"
mode="currency"
currency="BRL"
locale="pt-BR"
:readonly="planServices.length > 0 && selectedPlanService != null"
class="w-full"
size="small"
/>
</div>
</div>
</template>
</template>
</template>
</div>
<!-- RECORRÊNCIA ( sessão) -->
<div v-if="isSessionEvent" class="side-card">
<div class="mb-3">
<!-- Lista de itens adicionados -->
<div v-if="commitmentItems.length" class="commitment-items-list mb-2">
@@ -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,
}
}

View File

@@ -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
),

View File

@@ -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,
}
}

View File

@@ -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,

View File

@@ -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 }
}

View File

@@ -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 })
}
}

View File

@@ -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') ||

View File

@@ -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,

View File

@@ -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 () => {
<div>
<div class="text-900 font-semibold text-lg">Convênios</div>
<div class="text-600 text-sm">
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.
</div>
</div>
</div>
@@ -179,7 +267,7 @@ onMounted(async () => {
</Card>
<!-- Loading -->
<div v-if="pageLoading" class="flex justify-center py-10">
<div v-if="pageLoading || loading" class="flex justify-center py-10">
<ProgressSpinner style="width:40px;height:40px" />
</div>
@@ -195,28 +283,13 @@ onMounted(async () => {
</template>
<template #content>
<div class="grid grid-cols-12 gap-3">
<div class="col-span-12 sm:col-span-4">
<div class="col-span-12 sm:col-span-6">
<FloatLabel variant="on">
<InputText v-model="newForm.name" inputId="new-name" class="w-full" />
<label for="new-name">Nome *</label>
</FloatLabel>
</div>
<div class="col-span-12 sm:col-span-4">
<FloatLabel variant="on">
<InputNumber
v-model="newForm.default_value"
inputId="new-value"
mode="currency"
currency="BRL"
locale="pt-BR"
:min="0"
:minFractionDigits="2"
fluid
/>
<label for="new-value">Valor padrão da tabela (R$)</label>
</FloatLabel>
</div>
<div class="col-span-12 sm:col-span-4">
<div class="col-span-12 sm:col-span-6">
<FloatLabel variant="on">
<InputText v-model="newForm.notes" inputId="new-notes" class="w-full" />
<label for="new-notes">Observações (opcional)</label>
@@ -242,34 +315,19 @@ onMounted(async () => {
</Card>
<!-- Lista de convênios -->
<Card v-for="plan in plans" :key="plan.id">
<Card v-for="plan in plans" :key="plan.id" :class="{ 'opacity-60': !plan.active }">
<template #content>
<!-- Modo edição -->
<!-- Modo edição do plano -->
<template v-if="editingId === plan.id">
<div class="grid grid-cols-12 gap-3">
<div class="col-span-12 sm:col-span-4">
<div class="col-span-12 sm:col-span-6">
<FloatLabel variant="on">
<InputText v-model="editForm.name" :inputId="`edit-name-${plan.id}`" class="w-full" />
<label :for="`edit-name-${plan.id}`">Nome *</label>
</FloatLabel>
</div>
<div class="col-span-12 sm:col-span-4">
<FloatLabel variant="on">
<InputNumber
v-model="editForm.default_value"
:inputId="`edit-value-${plan.id}`"
mode="currency"
currency="BRL"
locale="pt-BR"
:min="0"
:minFractionDigits="2"
fluid
/>
<label :for="`edit-value-${plan.id}`">Valor padrão da tabela (R$)</label>
</FloatLabel>
</div>
<div class="col-span-12 sm:col-span-4">
<div class="col-span-12 sm:col-span-6">
<FloatLabel variant="on">
<InputText v-model="editForm.notes" :inputId="`edit-notes-${plan.id}`" class="w-full" />
<label :for="`edit-notes-${plan.id}`">Observações (opcional)</label>
@@ -284,25 +342,157 @@ onMounted(async () => {
<!-- Modo leitura -->
<template v-else>
<!-- Cabeçalho do plano -->
<div class="flex items-center justify-between gap-3 flex-wrap">
<div class="flex items-center gap-3">
<div class="cfg-icon-box-sm">
<div class="flex items-center gap-3 min-w-0">
<div class="cfg-icon-box-sm shrink-0">
<i class="pi pi-id-card" />
</div>
<div>
<div class="min-w-0">
<div class="font-semibold text-900">{{ plan.name }}</div>
<div class="text-sm text-color-secondary flex flex-wrap gap-x-3 gap-y-0.5 mt-0.5">
<span><b class="text-primary-500">{{ fmtBRL(plan.default_value) }}</b></span>
<span v-if="plan.notes" class="italic">{{ plan.notes }}</span>
</div>
<div v-if="plan.notes" class="text-sm text-color-secondary italic truncate">{{ plan.notes }}</div>
</div>
</div>
<div class="flex items-center gap-2">
<Tag value="Ativo" severity="success" />
<div class="flex items-center gap-2 shrink-0 flex-wrap">
<Tag :value="plan.active ? 'Ativo' : 'Inativo'" :severity="plan.active ? 'success' : 'secondary'" />
<Button
:label="`Procedimentos (${totalProcedimentos(plan)})`"
:icon="expandedPlanId === plan.id ? 'pi pi-chevron-up' : 'pi pi-chevron-down'"
severity="secondary"
outlined
size="small"
@click="expandedPlanId === plan.id ? (expandedPlanId = null, addingServicePlanId = null) : (expandedPlanId = plan.id, addingServicePlanId = null)"
/>
<Button
:icon="plan.active ? 'pi pi-eye-slash' : 'pi pi-eye'"
:severity="plan.active ? 'secondary' : 'success'"
outlined
size="small"
v-tooltip.top="plan.active ? 'Desativar' : 'Ativar'"
@click="togglePlan(plan)"
/>
<Button icon="pi pi-pencil" severity="secondary" outlined size="small" v-tooltip.top="'Editar'" @click="startEdit(plan)" />
<Button icon="pi pi-trash" severity="danger" outlined size="small" v-tooltip.top="'Desativar'" @click="removePlan(plan.id)" />
</div>
</div>
<!-- Painel expansível: procedimentos -->
<div v-if="expandedPlanId === plan.id" class="mt-4 border-t border-surface pt-4">
<!-- Lista de procedimentos (ativos e inativos) -->
<div v-if="plan.insurance_plan_services?.length" class="mb-3 flex flex-col gap-1">
<template v-for="ps in plan.insurance_plan_services" :key="ps.id">
<!-- Modo edição inline do procedimento -->
<div v-if="editingServiceId === ps.id" class="flex flex-wrap gap-2 items-end py-2 border-b border-surface">
<div class="flex-1 min-w-[140px]">
<label class="text-xs text-color-secondary mb-1 block">Nome</label>
<InputText v-model="editServiceForm.name" class="w-full" size="small" />
</div>
<div class="w-36">
<label class="text-xs text-color-secondary mb-1 block">Valor (R$)</label>
<InputNumber
v-model="editServiceForm.value"
mode="currency" currency="BRL" locale="pt-BR"
:min="0" :minFractionDigits="2"
class="w-full" size="small"
/>
</div>
<div class="flex gap-2">
<Button label="Cancelar" severity="secondary" outlined size="small" @click="cancelEditService" />
<Button label="Salvar" icon="pi pi-check" size="small" :loading="savingServiceEdit" @click="saveServiceEdit" />
</div>
</div>
<!-- Modo leitura do procedimento -->
<div
v-else
class="flex items-center justify-between gap-2 py-2 border-b border-surface last:border-0"
:class="{ 'opacity-60': !ps.active }"
>
<div class="flex items-center gap-2 min-w-0">
<Tag v-if="!ps.active" value="Inativo" severity="secondary" class="text-xs" />
<span class="text-sm font-medium text-900 truncate">{{ ps.name }}</span>
</div>
<div class="flex items-center gap-3 shrink-0">
<span class="text-sm font-semibold text-primary-500">{{ fmtBRL(ps.value) }}</span>
<Button
:icon="ps.active ? 'pi pi-eye-slash' : 'pi pi-eye'"
:severity="ps.active ? 'secondary' : 'success'"
text size="small"
v-tooltip.top="ps.active ? 'Desativar' : 'Ativar'"
@click="onToggleService(ps)"
/>
<Button
icon="pi pi-pencil"
severity="secondary" text size="small"
v-tooltip.top="'Editar'"
@click="startEditService(ps)"
/>
<Button
icon="pi pi-trash"
severity="danger" text size="small"
v-tooltip.top="'Remover definitivamente'"
@click="deleteService(ps.id)"
/>
</div>
</div>
</template>
</div>
<div v-else-if="addingServicePlanId !== plan.id" class="text-sm text-color-secondary mb-3 italic">
Nenhum procedimento cadastrado.
</div>
<!-- Formulário adicionar procedimento -->
<div v-if="addingServicePlanId === plan.id" class="mt-3">
<!-- Cards de serviços para auto-preencher -->
<div v-if="services.filter(s => s.active).length" class="mb-3">
<div class="text-xs text-color-secondary mb-2">Clique num serviço para pré-preencher:</div>
<div class="grid grid-cols-2 sm:grid-cols-3 gap-2">
<button
v-for="svc in services.filter(s => s.active)"
:key="svc.id"
class="svc-quick-card"
@click="fillFromService(svc)"
>
<span class="svc-quick-name">{{ svc.name }}</span>
<span class="svc-quick-price">{{ fmtBRL(svc.price) }}</span>
</button>
</div>
</div>
<div class="flex flex-wrap gap-2 items-end">
<div class="flex-1 min-w-[140px]">
<label class="text-xs text-color-secondary mb-1 block">Nome do procedimento *</label>
<InputText v-model="newServiceForm.name" placeholder="Ex: Consulta" class="w-full" size="small" />
</div>
<div class="w-36">
<label class="text-xs text-color-secondary mb-1 block">Valor (R$) *</label>
<InputNumber
v-model="newServiceForm.value"
mode="currency" currency="BRL" locale="pt-BR"
:min="0" :minFractionDigits="2"
class="w-full" size="small"
/>
</div>
<div class="flex gap-2">
<Button label="Cancelar" severity="secondary" outlined size="small" @click="cancelAddService" />
<Button label="Adicionar" icon="pi pi-check" size="small" :loading="savingService" @click="saveService(plan.id)" />
</div>
</div>
</div>
<Button
v-if="addingServicePlanId !== plan.id"
label="Adicionar procedimento"
icon="pi pi-plus"
severity="secondary"
outlined
size="small"
class="mt-2"
@click="startAddService(plan.id)"
/>
</div>
</template>
</template>
@@ -310,7 +500,7 @@ onMounted(async () => {
<Message severity="info" :closable="false">
<span class="text-sm">
O convênio selecionado em um evento preenche automaticamente o valor da sessão com o valor de tabela cadastrado aqui.
Os procedimentos ativos ficam disponíveis para seleção ao registrar uma sessão com convênio na agenda.
</span>
</Message>
@@ -340,4 +530,23 @@ onMounted(async () => {
color: var(--p-primary-500, #6366f1);
flex-shrink: 0;
}
.svc-quick-card {
display: flex;
flex-direction: column;
gap: 0.125rem;
padding: 0.375rem 0.625rem;
border-radius: 0.5rem;
border: 1px solid var(--p-surface-200, #e5e7eb);
background: var(--p-surface-50, #f9fafb);
text-align: left;
cursor: pointer;
transition: border-color 0.15s, background 0.15s;
}
.svc-quick-card:hover {
border-color: var(--p-primary-400, #818cf8);
background: color-mix(in srgb, var(--p-primary-500, #6366f1) 6%, transparent);
}
.svc-quick-name { font-size: 0.75rem; font-weight: 600; color: var(--p-text-color); }
.svc-quick-price { font-size: 0.7rem; color: var(--p-text-muted-color); }
</style>

View File

@@ -9,7 +9,7 @@ import { useServices } from '@/features/agenda/composables/useServices'
const toast = useToast()
const tenantStore = useTenantStore()
const { services, loading, error: servicesError, load, save, remove } = useServices()
const { services, loading, error: servicesError, load, save, toggle, remove } = useServices()
const ownerId = ref(null)
const tenantId = ref(null)
@@ -97,10 +97,19 @@ async function saveNew () {
}
}
async function toggleService (svc) {
try {
await toggle(svc.id, !svc.active)
toast.add({ severity: 'success', summary: svc.active ? 'Desativado' : 'Ativado', detail: `Serviço ${svc.active ? 'desativado' : 'ativado'}.`, life: 3000 })
} catch {
toast.add({ severity: 'error', summary: 'Erro', detail: servicesError.value || 'Falha ao atualizar.', life: 4000 })
}
}
async function confirmRemove (id) {
try {
await remove(id)
toast.add({ severity: 'success', summary: 'Removido', detail: 'Serviço removido.', life: 3000 })
toast.add({ severity: 'success', summary: 'Removido', detail: 'Serviço removido permanentemente.', life: 3000 })
} catch {
toast.add({ severity: 'error', summary: 'Erro', detail: servicesError.value || 'Falha ao remover.', life: 4000 })
}
@@ -242,7 +251,7 @@ onMounted(async () => {
</Card>
<!-- Lista de serviços -->
<Card v-for="svc in services" :key="svc.id">
<Card v-for="svc in services" :key="svc.id" :class="{ 'opacity-60': !svc.active }">
<template #content>
<!-- Modo edição -->
@@ -305,7 +314,15 @@ onMounted(async () => {
</div>
</div>
<div class="flex items-center gap-2">
<Tag value="Ativo" severity="success" />
<Tag :value="svc.active ? 'Ativo' : 'Inativo'" :severity="svc.active ? 'success' : 'secondary'" />
<Button
:icon="svc.active ? 'pi pi-eye-slash' : 'pi pi-eye'"
:severity="svc.active ? 'secondary' : 'success'"
outlined
size="small"
v-tooltip.top="svc.active ? 'Desativar' : 'Ativar'"
@click="toggleService(svc)"
/>
<Button icon="pi pi-pencil" severity="secondary" outlined size="small" v-tooltip.top="'Editar'" @click="startEdit(svc)" />
<Button icon="pi pi-trash" severity="danger" outlined size="small" v-tooltip.top="'Remover'" @click="confirmRemove(svc.id)" />
</div>

View File

@@ -0,0 +1,58 @@
-- Migration: 06_insurance_plan_services_v2
-- Reestrutura insurance_plan_services para ser autônomo (sem FK para services)
-- Adiciona campo name, active e remove service_id
-- Passo 1: remover a tabela antiga se existir e recriar do zero
DROP TABLE IF EXISTS public.insurance_plan_services;
-- Passo 2: criar tabela nova autônoma
CREATE TABLE public.insurance_plan_services (
id uuid DEFAULT gen_random_uuid() NOT NULL,
insurance_plan_id uuid NOT NULL,
name text NOT NULL,
value numeric(10,2) NOT NULL,
active boolean DEFAULT true NOT NULL,
created_at timestamptz DEFAULT now(),
updated_at timestamptz DEFAULT now(),
CONSTRAINT insurance_plan_services_pkey PRIMARY KEY (id),
CONSTRAINT insurance_plan_services_plan_fkey
FOREIGN KEY (insurance_plan_id) REFERENCES public.insurance_plans(id) ON DELETE CASCADE
);
-- Passo 3: RLS
ALTER TABLE public.insurance_plan_services ENABLE ROW LEVEL SECURITY;
DROP POLICY IF EXISTS "insurance_plan_services_owner" ON public.insurance_plan_services;
CREATE POLICY "insurance_plan_services_owner"
ON public.insurance_plan_services
FOR ALL
USING (
EXISTS (
SELECT 1 FROM public.insurance_plans ip
WHERE ip.id = insurance_plan_services.insurance_plan_id
AND ip.owner_id = auth.uid()
)
)
WITH CHECK (
EXISTS (
SELECT 1 FROM public.insurance_plans ip
WHERE ip.id = insurance_plan_services.insurance_plan_id
AND ip.owner_id = auth.uid()
)
);
-- Passo 4: grants
GRANT ALL ON TABLE public.insurance_plan_services TO postgres;
GRANT ALL ON TABLE public.insurance_plan_services TO anon;
GRANT ALL ON TABLE public.insurance_plan_services TO authenticated;
GRANT ALL ON TABLE public.insurance_plan_services TO service_role;
-- Passo 5: trigger updated_at
DROP TRIGGER IF EXISTS set_insurance_plan_services_updated_at ON public.insurance_plan_services;
CREATE TRIGGER set_insurance_plan_services_updated_at
BEFORE UPDATE ON public.insurance_plan_services
FOR EACH ROW EXECUTE FUNCTION public.set_updated_at();
-- Passo 6: adicionar campo active na tabela services (se ainda não existir)
ALTER TABLE public.services
ADD COLUMN IF NOT EXISTS active boolean DEFAULT true NOT NULL;

View File

@@ -0,0 +1,6 @@
-- Migration: 07_insurance_plan_service_id
-- Adiciona referência ao procedimento do convênio no evento
ALTER TABLE public.agenda_eventos
ADD COLUMN IF NOT EXISTS insurance_plan_service_id uuid
REFERENCES public.insurance_plan_services(id) ON DELETE SET NULL;

View File

@@ -0,0 +1,10 @@
-- Migration: 08_recurrence_rules_insurance
-- Adiciona campos de convênio na tabela recurrence_rules
ALTER TABLE public.recurrence_rules
ADD COLUMN IF NOT EXISTS insurance_plan_id uuid
REFERENCES public.insurance_plans(id) ON DELETE SET NULL,
ADD COLUMN IF NOT EXISTS insurance_guide_number text,
ADD COLUMN IF NOT EXISTS insurance_value numeric(10,2),
ADD COLUMN IF NOT EXISTS insurance_plan_service_id uuid
REFERENCES public.insurance_plan_services(id) ON DELETE SET NULL;