@prefix oc: <https://ontology.inferal.com/profiles/order-contribution-evidence/> .
@prefix ctx: <https://ontology.inferal.com/modules/commerce-transaction/> .
@prefix adm: <https://ontology.inferal.com/modules/advertising-measurement/> .
@prefix cme: <https://ontology.inferal.com/modules/commerce-margin-evidence/> .
@prefix pc: <https://ontology.inferal.com/modules/commerce-purchase-classification/> .
@prefix prov: <http://www.w3.org/ns/prov#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix sh: <http://www.w3.org/ns/shacl#> .
oc:StatementShape a sh:NodeShape ; sh:targetClass oc:Statement ; sh:nodeKind sh:IRI ;
 sh:property [ sh:path oc:order ; sh:minCount 1 ; sh:maxCount 1 ; sh:class ctx:Order ; sh:nodeKind sh:IRI ] ;
 sh:property [ sh:path oc:merchant ; sh:minCount 1 ; sh:maxCount 1 ; sh:nodeKind sh:IRI ] ;
 sh:property [ sh:path oc:policy ; sh:minCount 1 ; sh:maxCount 1 ; sh:nodeKind sh:IRI ; sh:class prov:Entity ; sh:node oc:PolicyShape ] ;
 sh:property [ sh:path oc:asOf ; sh:minCount 1 ; sh:maxCount 1 ; sh:datatype xsd:dateTimeStamp ] ;
 sh:property [ sh:path oc:currency ; sh:minCount 1 ; sh:maxCount 1 ; sh:datatype xsd:string ; sh:pattern "^[A-Z]{3}$" ] ;
 sh:property [ sh:path oc:selectedLine ; sh:minCount 1 ; sh:node oc:LineShape ; sh:nodeKind sh:IRI ; sh:class ctx:LineItem ] ;
 sh:property [ sh:path oc:lineCoverage ; sh:minCount 1 ; sh:maxCount 1 ; sh:in (cme:DeclaredComplete cme:DeclaredPartial) ] ;
 sh:property [ sh:path oc:adjustmentCoverage ; sh:minCount 1 ; sh:maxCount 1 ; sh:in (cme:DeclaredComplete cme:DeclaredPartial) ] ;
 sh:property [ sh:path oc:adjustment ; sh:class oc:Adjustment ; sh:nodeKind sh:IRI ] ;
 sh:property [ sh:path prov:wasDerivedFrom ; sh:minCount 1 ; sh:nodeKind sh:IRI ] ;
 sh:sparql [ sh:message "Selected lines must belong only to the selected order, in the declared merchant scope." ; sh:select """
 PREFIX oc: <https://ontology.inferal.com/profiles/order-contribution-evidence/>
 PREFIX ctx: <https://ontology.inferal.com/modules/commerce-transaction/>
 PREFIX pc: <https://ontology.inferal.com/modules/commerce-purchase-classification/>
 SELECT $this WHERE { $this oc:order ?order ; oc:merchant ?merchant .
 FILTER (NOT EXISTS { ?order pc:merchant ?merchant } || EXISTS { ?order pc:merchant ?other . FILTER (?other != ?merchant) } || EXISTS { $this oc:selectedLine ?line . FILTER NOT EXISTS { ?order ctx:orderContains ?line } } || EXISTS { $this oc:selectedLine ?line . ?other ctx:orderContains ?line . FILTER (?other != ?order) }) }
 """ ] ;
 sh:sparql [ sh:message "Every selected monetary value must share the statement currency." ; sh:select """
 PREFIX oc: <https://ontology.inferal.com/profiles/order-contribution-evidence/>
 PREFIX ctx: <https://ontology.inferal.com/modules/commerce-transaction/>
 PREFIX adm: <https://ontology.inferal.com/modules/advertising-measurement/>
 SELECT $this WHERE { $this oc:currency ?currency .
 $this (oc:selectedLine|oc:adjustment) ?record .
 ?record (ctx:lineItemRevenue|ctx:costOfGoods|oc:money)/adm:currencyCode ?actual .
 FILTER (?currency != ?actual) }
 """ ] ;
 sh:sparql [ sh:message "Selected records and the retained policy version must be captured by the statement cutoff." ; sh:select """
 PREFIX oc: <https://ontology.inferal.com/profiles/order-contribution-evidence/>
 PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
 SELECT $this WHERE { $this oc:asOf ?cutoff . $this (oc:selectedLine|oc:adjustment|oc:policy) ?record .
 ?record oc:capturedAt ?capture . FILTER (xsd:dateTime(STR(?capture)) > xsd:dateTime(STR(?cutoff))) }
 """ ] ;
 sh:sparql [ sh:message "Distinct adjustment handles with the same selected key require resolution before arithmetic." ; sh:select """
 PREFIX oc: <https://ontology.inferal.com/profiles/order-contribution-evidence/>
 SELECT $this WHERE { $this oc:adjustment ?left, ?right . ?left oc:adjustmentKey ?key . ?right oc:adjustmentKey ?key . FILTER (?left != ?right) }
 """ ] .
oc:PolicyShape a sh:NodeShape ; sh:targetObjectsOf oc:policy ; sh:class prov:Entity ; sh:nodeKind sh:IRI ;
 sh:property [ sh:path owl:versionInfo ; sh:minCount 1 ; sh:maxCount 1 ; sh:datatype xsd:string ; sh:minLength 1 ] ;
 sh:property [ sh:path oc:capturedAt ; sh:minCount 1 ; sh:maxCount 1 ; sh:datatype xsd:dateTimeStamp ] ;
 sh:property [ sh:path prov:wasDerivedFrom ; sh:minCount 1 ; sh:nodeKind sh:IRI ] .
oc:LineShape a sh:NodeShape ;
 sh:property [ sh:path ctx:lineItemRevenue ; sh:minCount 1 ; sh:maxCount 1 ; sh:node oc:MoneyShape ; sh:class adm:MonetaryAmount ] ;
 sh:property [ sh:path ctx:costOfGoods ; sh:maxCount 1 ; sh:node oc:MoneyShape ; sh:class adm:MonetaryAmount ] ;
 sh:property [ sh:path oc:capturedAt ; sh:minCount 1 ; sh:maxCount 1 ; sh:datatype xsd:dateTimeStamp ] ;
 sh:property [ sh:path prov:wasDerivedFrom ; sh:minCount 1 ; sh:nodeKind sh:IRI ] .
oc:MoneyShape a sh:NodeShape ; sh:nodeKind sh:IRI ;
 sh:property [ sh:path adm:amountValue ; sh:minCount 1 ; sh:maxCount 1 ; sh:datatype xsd:decimal ] ;
 sh:property [ sh:path adm:currencyCode ; sh:minCount 1 ; sh:maxCount 1 ; sh:datatype xsd:string ; sh:pattern "^[A-Z]{3}$" ] .
oc:AdjustmentShape a sh:NodeShape ; sh:targetClass oc:Adjustment ; sh:nodeKind sh:IRI ;
 sh:property [ sh:path oc:adjustmentKey ; sh:minCount 1 ; sh:maxCount 1 ; sh:datatype xsd:string ; sh:minLength 1 ] ;
 sh:property [ sh:path oc:kind ; sh:minCount 1 ; sh:maxCount 1 ; sh:in (oc:Refund oc:Fulfilment oc:PaymentFees oc:ShippingRevenue oc:Tax oc:Discount oc:Other) ] ;
 sh:property [ sh:path oc:treatment ; sh:minCount 1 ; sh:maxCount 1 ; sh:in (oc:ExcludedFromBase oc:AlreadyIncludedInBase) ] ;
 sh:property [ sh:path oc:money ; sh:maxCount 1 ; sh:node oc:MoneyShape ; sh:class adm:MonetaryAmount ] ;
 sh:property [ sh:path oc:capturedAt ; sh:minCount 1 ; sh:maxCount 1 ; sh:datatype xsd:dateTimeStamp ] ;
 sh:property [ sh:path prov:wasDerivedFrom ; sh:minCount 1 ; sh:nodeKind sh:IRI ] ;
 sh:sparql [ sh:message "Line revenue is post-discount. A discount cannot be deducted again." ; sh:select """
 PREFIX oc: <https://ontology.inferal.com/profiles/order-contribution-evidence/>
 SELECT $this WHERE { $this oc:kind oc:Discount ; oc:treatment oc:ExcludedFromBase }
 """ ] ;
 sh:sparql [ sh:message "Line revenue is pre-refund. Refund effects must be applied outside that base." ; sh:select """
 PREFIX oc: <https://ontology.inferal.com/profiles/order-contribution-evidence/>
 SELECT $this WHERE { $this oc:kind oc:Refund ; oc:treatment oc:AlreadyIncludedInBase }
 """ ] .
oc:TreatmentShape a sh:NodeShape ; sh:targetClass oc:Treatment ; sh:in (oc:ExcludedFromBase oc:AlreadyIncludedInBase) .
oc:KindShape a sh:NodeShape ; sh:targetClass oc:AdjustmentKind ; sh:in (oc:Refund oc:Fulfilment oc:PaymentFees oc:ShippingRevenue oc:Tax oc:Discount oc:Other) .
