|
|
@@ -14,19 +14,47 @@ router.get('/:paymentId', (req, res) => {
|
|
|
l.rent_amount, l.charges_amount, l.deposit_amount,
|
|
|
pr.name as property_name, pr.address as property_address, pr.city as property_city, pr.zip_code as property_zip,
|
|
|
t.first_name as tenant_first_name, t.last_name as tenant_last_name, t.email as tenant_email,
|
|
|
- u.name as owner_name, u.email as owner_email,
|
|
|
- u.first_name as owner_first_name, u.last_name as owner_last_name,
|
|
|
- u.address as owner_address, u.phone as owner_phone
|
|
|
+ -- Propriétaire bailleur (owners table en priorité, fallback profil utilisateur)
|
|
|
+ o.type as o_type,
|
|
|
+ o.first_name as o_first_name, o.last_name as o_last_name,
|
|
|
+ o.company_name as o_company_name, o.siret as o_siret,
|
|
|
+ o.address as o_address, o.zip_code as o_zip, o.city as o_city,
|
|
|
+ o.email as o_email, o.phone as o_phone,
|
|
|
+ -- Fallback gestionnaire
|
|
|
+ u.name as u_name, u.email as u_email,
|
|
|
+ u.first_name as u_first_name, u.last_name as u_last_name,
|
|
|
+ u.address as u_address, u.phone as u_phone
|
|
|
FROM payments p
|
|
|
JOIN leases l ON p.lease_id = l.id
|
|
|
JOIN properties pr ON l.property_id = pr.id
|
|
|
JOIN tenants t ON l.tenant_id = t.id
|
|
|
JOIN users u ON p.user_id = u.id
|
|
|
+ LEFT JOIN owners o ON l.owner_id = o.id
|
|
|
WHERE p.id = ? AND p.user_id = ?
|
|
|
`).get(req.params.paymentId, req.userId);
|
|
|
|
|
|
if (!payment) return res.status(404).json({ error: 'Paiement non trouvé' });
|
|
|
|
|
|
+ // Résoudre le nom du bailleur : owner en priorité, sinon profil gestionnaire
|
|
|
+ let ownerFullName, ownerAddress, ownerEmail, ownerPhone, ownerSiret;
|
|
|
+ if (payment.o_type) {
|
|
|
+ ownerFullName = payment.o_type === 'morale'
|
|
|
+ ? payment.o_company_name
|
|
|
+ : `${payment.o_first_name || ''} ${payment.o_last_name || ''}`.trim();
|
|
|
+ ownerSiret = payment.o_siret;
|
|
|
+ ownerAddress = [payment.o_address, payment.o_zip && payment.o_city ? `${payment.o_zip} ${payment.o_city}` : (payment.o_zip || payment.o_city)].filter(Boolean).join(', ');
|
|
|
+ ownerEmail = payment.o_email;
|
|
|
+ ownerPhone = payment.o_phone;
|
|
|
+ } else {
|
|
|
+ ownerFullName = (payment.u_first_name && payment.u_last_name)
|
|
|
+ ? `${payment.u_first_name} ${payment.u_last_name}`
|
|
|
+ : payment.u_name;
|
|
|
+ ownerSiret = null;
|
|
|
+ ownerAddress = payment.u_address;
|
|
|
+ ownerEmail = payment.u_email;
|
|
|
+ ownerPhone = payment.u_phone;
|
|
|
+ }
|
|
|
+
|
|
|
const doc = new PDFDocument({ size: 'A4', margin: 50 });
|
|
|
|
|
|
res.setHeader('Content-Type', 'application/pdf');
|
|
|
@@ -47,14 +75,12 @@ router.get('/:paymentId', (req, res) => {
|
|
|
doc.moveDown(1);
|
|
|
|
|
|
// Bailleur
|
|
|
- const ownerFullName = (payment.owner_first_name && payment.owner_last_name)
|
|
|
- ? `${payment.owner_first_name} ${payment.owner_last_name}`
|
|
|
- : payment.owner_name;
|
|
|
doc.fontSize(12).font('Helvetica-Bold').text('BAILLEUR');
|
|
|
doc.font('Helvetica').fontSize(11).text(ownerFullName);
|
|
|
- if (payment.owner_address) doc.text(payment.owner_address);
|
|
|
- doc.text(payment.owner_email);
|
|
|
- if (payment.owner_phone) doc.text(payment.owner_phone);
|
|
|
+ if (ownerSiret) doc.text(`SIRET : ${ownerSiret}`);
|
|
|
+ if (ownerAddress) doc.text(ownerAddress);
|
|
|
+ if (ownerEmail) doc.text(ownerEmail);
|
|
|
+ if (ownerPhone) doc.text(ownerPhone);
|
|
|
doc.moveDown(1);
|
|
|
|
|
|
// Locataire
|