identifiers()+['unit_id'=>$c->unitId()];$actor=$c->userId();if(!$s['unit_id']||!app(Permissions::class)->has($actor,$s['tenant_id'],$s['company_id'],$module==='finance'?'service_receivables.view':'general_ledger.view',$s['unit_id']))return$legacy; $owned=DB::table('a_billing_drafts');ModuleRecords::constrain($owned,'a_billing_drafts','billing_drafts','commercial');$owned->where($s); return DB::table('si_invoices')->where($s)->where(function($q)use($legacy,$s,$owned){$q->whereIn('id',$legacy->select('id'))->orWhere(function($q)use($s,$owned){$q->whereRaw("si_invoices.snapshot::jsonb->>'calculation_policy'=?",[MultiLineInvoiceCalculation::POLICY])->whereExists(function($m)use($s,$owned){$m->selectRaw('1')->from('mi_drafts as md')->whereColumn('md.draft_id','si_invoices.draft_id')->whereColumn('md.source_id','si_invoices.source_id')->where(['md.tenant_id'=>$s['tenant_id'],'md.company_id'=>$s['company_id'],'md.unit_id'=>$s['unit_id']])->whereIn('md.draft_id',$owned->select('id'));});});}); } }