feat(email/pdf): include partner company name, partner user name and email in emails and PDF confirmations
This commit is contained in:
@@ -94,7 +94,14 @@ const styles = StyleSheet.create({
|
||||
}
|
||||
});
|
||||
|
||||
export const InvoicePDF = ({ order, orderSnapshot, customer }: any) => {
|
||||
export const InvoicePDF = ({
|
||||
order,
|
||||
orderSnapshot,
|
||||
customer,
|
||||
partnerCompanyName,
|
||||
partnerUserName,
|
||||
partnerUserEmail,
|
||||
}: any) => {
|
||||
const items = orderSnapshot?.items ?? [];
|
||||
const taxRate = orderSnapshot?.tax_rate ?? 19;
|
||||
const isSubscription = order?.type === 'subscription' || orderSnapshot?.billing_cycle === 'monthly';
|
||||
@@ -112,7 +119,6 @@ export const InvoicePDF = ({ order, orderSnapshot, customer }: any) => {
|
||||
item.selected_modules?.forEach((mod: any) => {
|
||||
const qty = mod.quantity || 1;
|
||||
const price = mod.total_price ?? (mod.price * qty);
|
||||
// Module sind monatliche wiederkehrende Beträge (Abo/Miete)
|
||||
monthlyNet += price;
|
||||
});
|
||||
});
|
||||
@@ -135,6 +141,10 @@ export const InvoicePDF = ({ order, orderSnapshot, customer }: any) => {
|
||||
groupedItems[devName].push(item);
|
||||
});
|
||||
|
||||
const displayPartnerName = partnerCompanyName || order?.partner_company_name;
|
||||
const displayUserName = partnerUserName || order?.partner_user_name;
|
||||
const displayUserEmail = partnerUserEmail || order?.partner_user_email;
|
||||
|
||||
return (
|
||||
<Document>
|
||||
<Page size="A4" style={styles.page}>
|
||||
@@ -155,20 +165,31 @@ export const InvoicePDF = ({ order, orderSnapshot, customer }: any) => {
|
||||
Anfragebestätigung: {order.order_number || order.id}
|
||||
</Text>
|
||||
|
||||
<View style={styles.section}>
|
||||
<Text style={styles.label}>Kunde</Text>
|
||||
<Text>{customer.company_name}</Text>
|
||||
<Text>{customer.first_name} {customer.last_name}</Text>
|
||||
<Text>{customer.address}</Text>
|
||||
<Text>{customer.zip_code} {customer.city}</Text>
|
||||
<Text>USt-IdNr: {customer.vat_id}</Text>
|
||||
</View>
|
||||
<View style={{ flexDirection: 'row', justify: 'space-between', marginBottom: 15 }}>
|
||||
<View style={{ width: '48%' }}>
|
||||
<Text style={styles.label}>Endkunde</Text>
|
||||
<Text style={{ fontWeight: 'bold' }}>{customer.company_name}</Text>
|
||||
{customer.first_name || customer.last_name ? <Text>{customer.first_name} {customer.last_name}</Text> : null}
|
||||
{customer.address ? <Text>{customer.address}</Text> : null}
|
||||
{customer.zip_code || customer.city ? <Text>{customer.zip_code} {customer.city}</Text> : null}
|
||||
{customer.vat_id ? <Text>USt-IdNr: {customer.vat_id}</Text> : null}
|
||||
</View>
|
||||
|
||||
<View style={styles.section}>
|
||||
<Text style={styles.label}>Bestelldetails</Text>
|
||||
<Text>Bestellnummer: {order.order_number || order.id}</Text>
|
||||
<Text>Zahlungsart: {isSubscription ? 'SEPA-Lastschrift' : 'Rechnung'}</Text>
|
||||
<Text>Datum: {new Date(order.created_at || Date.now()).toLocaleDateString('de-DE')}</Text>
|
||||
<View style={{ width: '48%' }}>
|
||||
<Text style={styles.label}>Bestelldetails</Text>
|
||||
<Text>Bestellnummer: {order.order_number || order.id}</Text>
|
||||
<Text>Zahlungsart: {isSubscription ? 'SEPA-Lastschrift' : 'Rechnung'}</Text>
|
||||
<Text>Datum: {new Date(order.created_at || Date.now()).toLocaleDateString('de-DE')}</Text>
|
||||
|
||||
{(displayPartnerName || displayUserName || displayUserEmail) && (
|
||||
<View style={{ marginTop: 6, paddingTop: 6, borderTopWidth: 1, borderColor: '#eee' }}>
|
||||
<Text style={styles.label}>Erstellt durch Partner / User</Text>
|
||||
{displayPartnerName ? <Text style={{ fontWeight: 'bold' }}>Partner: {displayPartnerName}</Text> : null}
|
||||
{displayUserName ? <Text>Erstellt von: {displayUserName}</Text> : null}
|
||||
{displayUserEmail ? <Text>E-Mail: {displayUserEmail}</Text> : null}
|
||||
</View>
|
||||
)}
|
||||
</View>
|
||||
</View>
|
||||
|
||||
<View style={{ marginTop: 10, marginBottom: 15 }}>
|
||||
|
||||
@@ -16,6 +16,30 @@ import { getOrderEmailTemplate, getStatusEmailTemplate, buildEmailItemsSection }
|
||||
|
||||
// ─── Hilfsfunktionen ─────────────────────────────────────────────────────────
|
||||
|
||||
async function fetchOrderPartnerInfo(adminClient: any, companyId: string | null, userId: string | null) {
|
||||
let partnerCompanyName = ''
|
||||
let partnerUserName = ''
|
||||
let partnerUserEmail = ''
|
||||
|
||||
if (companyId) {
|
||||
const { data: comp } = await adminClient.from('companies').select('name').eq('id', companyId).single()
|
||||
if (comp) partnerCompanyName = comp.name
|
||||
}
|
||||
|
||||
if (userId) {
|
||||
const { data: dbUser } = await adminClient.from('users').select('first_name, last_name, email, role').eq('id', userId).single()
|
||||
if (dbUser) {
|
||||
partnerUserName = [dbUser.first_name, dbUser.last_name].filter(Boolean).join(' ')
|
||||
partnerUserEmail = dbUser.email || ''
|
||||
if (!partnerCompanyName && (dbUser.role === 'admin' || dbUser.role === 'verwaltung')) {
|
||||
partnerCompanyName = 'Administrator / Direktbestellung'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { partnerCompanyName, partnerUserName, partnerUserEmail }
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
@@ -164,6 +188,9 @@ export async function submitOrder(params: {
|
||||
order,
|
||||
customer: customerSnapshot,
|
||||
orderSnapshot,
|
||||
partnerCompanyName,
|
||||
partnerUserName,
|
||||
partnerUserEmail,
|
||||
})
|
||||
)
|
||||
|
||||
@@ -382,6 +409,8 @@ export async function updateOrderStatus(
|
||||
throw new Error(`Fehler beim Aktualisieren des Status: ${updateError?.message || 'Unbekannt'}`)
|
||||
}
|
||||
|
||||
const partnerInfo = await fetchOrderPartnerInfo(admin, updatedOrder.company_id, updatedOrder.user_id)
|
||||
|
||||
// 3. Wenn Status auf 'active' oder 'approved' wechselt, generiere PDF und lade es hoch (pdf-invoice-generator)
|
||||
let attachmentBuffer: Buffer | null = null
|
||||
if (newStatus === 'active' || newStatus === 'approved') {
|
||||
@@ -394,6 +423,9 @@ export async function updateOrderStatus(
|
||||
order: updatedOrder,
|
||||
customer: customerSnapshot,
|
||||
orderSnapshot,
|
||||
partnerCompanyName: partnerInfo.partnerCompanyName,
|
||||
partnerUserName: partnerInfo.partnerUserName,
|
||||
partnerUserEmail: partnerInfo.partnerUserEmail,
|
||||
})
|
||||
)
|
||||
attachmentBuffer = buffer
|
||||
@@ -751,6 +783,8 @@ export async function updateOrder(
|
||||
|
||||
if (orderError || !order) throw orderError || new Error('Fehler beim Aktualisieren der Bestellung.')
|
||||
|
||||
const partnerInfo = await fetchOrderPartnerInfo(admin, order.company_id, order.user_id)
|
||||
|
||||
// 3. PDF generieren und überschreiben
|
||||
try {
|
||||
const buffer = await renderToBuffer(
|
||||
@@ -758,6 +792,9 @@ export async function updateOrder(
|
||||
order,
|
||||
customer: customerSnapshot,
|
||||
orderSnapshot,
|
||||
partnerCompanyName: partnerInfo.partnerCompanyName,
|
||||
partnerUserName: partnerInfo.partnerUserName,
|
||||
partnerUserEmail: partnerInfo.partnerUserEmail,
|
||||
})
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user