SELECT  
        io.ref,
        c.socialName,
        sa.iban,   
        pm.name AS payMethod, 
        t.clientFk,
        t.shipped,
        t.nickname,
        s.ticketFk,
        s.itemFk,
        s.concept, 
        s.quantity, 
        s.price, 
        s.discount,
        i.tag5, 
        i.value5,
        i.tag6, 
        i.value6, 
        i.tag7, 
        i.value7,
		tc.code AS vatType,
        ib.ediBotanic botanical
	FROM vn.invoiceOut io
        JOIN vn.ticket t ON t.refFk = io.ref
        JOIN vn.supplier su ON su.id = io.companyFk
        JOIN vn.client c ON c.id = t.clientFk 
        JOIN vn.payMethod pm ON pm.id = c.payMethodFk
        JOIN vn.company co ON co.id = io.companyFk        
        JOIN vn.supplierAccount sa ON sa.id = co.supplierAccountFk
        JOIN vn.sale s ON s.ticketFk = t.id
        JOIN item i ON i.id = s.itemFk
        LEFT JOIN itemType it ON it.id = i.typeFk
        LEFT JOIN itemCategory ic ON ic.id = it.categoryFk
        LEFT JOIN itemBotanicalWithGenus ib ON ib.itemFk = i.id
            AND ic.code = 'plant'
            AND ib.ediBotanic IS NOT NULL
        JOIN vn.itemTaxCountry itc ON itc.countryFk = su.countryFk
            AND itc.itemFk = s.itemFk
        JOIN vn.taxClass tc ON tc.id = itc.taxClassFk
    WHERE io.id = ?
    UNION ALL	
SELECT  
        io.ref,
        c.socialName,
        sa.iban,   
        pm.name AS payMethod, 
        t.clientFk,
        t.shipped,
        t.nickname,
        t.id AS ticketFk,
        '',
        ts.description concept, 
        ts.quantity, 
        ts.price, 
        0 discount, 
        NULL AS tag5,
        NULL AS value5,
        NULL AS tag6,
        NULL AS value6,
        NULL AS tag7,
        NULL AS value7,
        tc.code AS vatType,
        NULL AS botanical
    FROM vn.invoiceOut io
        JOIN vn.ticket t ON t.refFk = io.ref
        JOIN vn.ticketService ts ON ts.ticketFk = t.id
        JOIN vn.client c ON c.id = t.clientFk 
        JOIN vn.payMethod pm ON pm.id = c.payMethodFk
        JOIN vn.company co ON co.id = io.companyFk        
        JOIN vn.supplierAccount sa ON sa.id = co.supplierAccountFk
        JOIN vn.taxClass tc ON tc.id = ts.taxClassFk
    WHERE io.id = ?