Can define relationship with additional condition?

I have 3 models: OrderTypeA(orderid), OrderTypeB(orderid), Invoice(invoiceid, invoiceordertype, invoiceorderid) . Are there any ways to define relationships between OrderTypeA and Invoice, OrderTypeB and Invoice via pair (orderid, invoiceorderid) with additional condition (ordertypeid)? Can anyone help me resolve this?