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
@@ -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,
}
}
@@ -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
),
@@ -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,
}
}
@@ -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,
+34 -26
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 }
}
+78 -12
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 })
}
}
@@ -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') ||
@@ -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,