Return original concept information from the audit trails

import module namespace sec = "http://xbrl.io/modules/bizql/profiles/sec/core";
import module namespace sec-fiscal = "http://xbrl.io/modules/bizql/profiles/sec/fiscal/core";
import module namespace entities = "http://xbrl.io/modules/bizql/entities";
import module namespace concept-maps = "http://xbrl.io/modules/bizql/concept-maps";
import module namespace hypercubes = "http://xbrl.io/modules/bizql/hypercubes";

import module namespace req = "http://www.28msec.com/modules/http-request";

variable $cik := let $cik := req:param-values("cik","0000104169")
                 return if (empty($cik))
                        then error(QName("local:INVALID-REQUEST"), "cik: mandatory parameter not found")
                        else $cik;
variable $periodFocus := let $periodFocus := req:param-values("fiscalPeriodFocus", "FY")
                         return if (empty($periodFocus))
                         then error(QName("local:INVALID-REQUEST"),"fiscalPeriodFocus: mandatory parameter not found")
                         else $periodFocus;
variable $yearFocus := let $yearFocus := req:param-values("fiscalYearFocus","2012")
                       return if (empty($yearFocus))
                              then error(QName("local:INVALID-REQUEST"), "fiscalYearFocus: mandatory parameter not found")
                              else $yearFocus cast as integer;

variable $entity := let $entity := entities:entities(sec:normalize-cik($cik))
                    return if (empty($entity))
                           then  error(QName("local:INVALID-REQUEST"), "Given CIK:"||$cik|| " not found")
                           else  $entity;

variable $archive := let $archive :=  sec-fiscal:filings-for-entities-and-fiscal-periods-and-years($entity, $periodFocus, $yearFocus)
                     return if (empty($archive))
                           then  error(QName("local:INVALID-REQUEST"), "Filing not found")
                           else  $archive;

variable $mapName := let $mapName := req:param-values("map","FundamentalAccountingConcepts")
                     return
                         if($mapName ne "None" and not(exists(concept-maps:concept-maps($mapName))))
                            then error(QName("local:INVALID-REQUEST"), "Given map:"||$mapName|| " not found")
                            else $mapName;

variable $conceptMap :=  if ($mapName eq "None")
                         then ()
                         else concept-maps:concept-maps($mapName);

variable $concept := let $concept := req:param-values("conceptName", "fac:Revenues")
                     return if (empty($concept))
                            then error(QName("local:INVALID-REQUEST"),"conceptName: mandatory parameter not found")
                            else $concept;

variable $fact := let $fact := if(exists($conceptMap))
                               then sec-fiscal:facts-for-entities-and-concepts-and-fiscal-periods-and-years($entity,$concept, $periodFocus, $yearFocus, { "concept-maps" : [ $conceptMap ]})
                 else sec-fiscal:facts-for-entities-and-concepts-and-fiscal-periods-and-years($entity,$concept, $periodFocus, $yearFocus)
                  return if (empty($fact))
                            then error(QName("local:INVALID-REQUEST"),"fact not found")
                            else $fact;
let $value := if ($fact.Value castable as decimal)
             then $fact.Value cast as decimal
             else $fact.Value

return {
    cik: $cik,
    periodFocus: $periodFocus,
    fiscalYearFocus: $yearFocus,
    conceptName: $concept,
    map: $mapName,
    originalConcept: $fact.AuditTrails[].Data.OriginalConcept,
    type: $fact.Type,
    unit: $fact.Aspects."xbrl:Unit",
    decimals: $fact.Decimals,
    value: $value
    (:entity: serialize($entity):)
    (:archive: serialize($archive):)
}

import module namespace sec = "http://xbrl.io/modules/bizql/profiles/sec/core";
import module namespace sec-fiscal = "http://xbrl.io/modules/bizql/profiles/sec/fiscal/core";
import module namespace entities = "http://xbrl.io/modules/bizql/entities";
import module namespace concept-maps = "http://xbrl.io/modules/bizql/concept-maps";
import module namespace hypercubes = "http://xbrl.io/modules/bizql/hypercubes";

import module namespace req = "http://www.28msec.com/modules/http-request";

variable $cik := let $cik := req:param-values("cik","0000104169")
                 return if (empty($cik))
                        then error(QName("local:INVALID-REQUEST"), "cik: mandatory parameter not found")
                        else $cik;
variable $periodFocus := let $periodFocus := req:param-values("fiscalPeriodFocus", "FY")
                         return if (empty($periodFocus))
                         then error(QName("local:INVALID-REQUEST"),"fiscalPeriodFocus: mandatory parameter not found")
                         else $periodFocus;
variable $yearFocus := let $yearFocus := req:param-values("fiscalYearFocus","2012")
                       return if (empty($yearFocus))
                              then error(QName("local:INVALID-REQUEST"), "fiscalYearFocus: mandatory parameter not found")
                              else $yearFocus cast as integer;

variable $entity := let $entity := entities:entities(sec:normalize-cik($cik))
                    return if (empty($entity))
                           then  error(QName("local:INVALID-REQUEST"), "Given CIK:"||$cik|| " not found")
                           else  $entity;

variable $archive := let $archive :=  sec-fiscal:filings-for-entities-and-fiscal-periods-and-years($entity, $periodFocus, $yearFocus)
                     return if (empty($archive))
                           then  error(QName("local:INVALID-REQUEST"), "Filing not found")
                           else  $archive;

variable $mapName := let $mapName := req:param-values("map","None")
                     return
                         if($mapName ne "None" and not(exists(concept-maps:concept-maps($mapName))))
                            then error(QName("local:INVALID-REQUEST"), "Given map:"||$mapName|| " not found")
                            else $mapName;

variable $conceptMap :=  if ($mapName eq "None")
                         then ()
                         else concept-maps:concept-maps($mapName);

variable $concept := let $concept := req:param-values("conceptName", "us-gaap:RevenueRecognitionPolicyTextBlock")
                     return if (empty($concept))
                            then error(QName("local:INVALID-REQUEST"),"conceptName: mandatory parameter not found")
                            else $concept;

variable $fact := let $fact := if(exists($conceptMap))
                               then sec-fiscal:facts-for-entities-and-concepts-and-fiscal-periods-and-years($entity,$concept, $periodFocus, $yearFocus, { "concept-maps" : [ $conceptMap ]})
                 else sec-fiscal:facts-for-entities-and-concepts-and-fiscal-periods-and-years($entity,$concept, $periodFocus, $yearFocus)
                  return if (empty($fact))
                            then error(QName("local:INVALID-REQUEST"),"fact not found")
                            else $fact;
let $value := if ($fact.Value castable as decimal)
             then $fact.Value cast as decimal
             else $fact.Value

return {
    cik: $cik,
    periodFocus: $periodFocus,
    fiscalYearFocus: $yearFocus,
    conceptName: $concept,
    map: $mapName,
    type: $fact.Type,
    unit: $fact.Aspects."xbrl:Unit",
    value: $value,
    decimals: $fact.Decimals
    (:entity: serialize($entity):)
    (:archive: serialize($archive):)

}

(: archives:archives-for-entities($entity)[secprofiles:fiscal-period($$)=$periodFocus][secprofiles:fiscal-year($$)=$yearFocus]

can be changed to fiscal:filings-for-entities-and-fiscal-periods-and-years($entity, $periodFocus, $yearFocus) :)

(: seccore:latest-main-fact-for-concept($archive,$concept)

will need to be changed to seccore:main-facts-for-archives-and-concepts($archive, $concept) :)


:white_check_mark: add test for entity-search-offset and entity-search-limit parameters.

import module namespace test = "http://apps.28.io/test";

declare variable $local:expected as object :=
{
  "all" : [ "E01225",
            "E01264",
            "E02166",
            "E02274",
            "E02513",
            "E02529",
            "E04147",
            "E04425",
            "E04430",
            "EDINET" ],
  "NIKKEI" : [ "E01225",
               "E01264",
               "E02166",
               "E02274",
               "E02513",
               "E02529",
               "E04147",
               "E04425",
               "E04430" ]
};

test:check-all-success({
    all: test:invoke-and-assert-deep-equal(
      "entities",
      {},
      function($b as item*) as item* { [ $b.Entities[].Profiles.FSA.EDINETCode ] },
      $local:expected.all,
      { NoArrayOrder: true }
    ),
    nikkei: test:invoke-and-assert-deep-equal(
      "entities",
      {tag:"NIKKEI"},
      function($b as item*) as item* { [ $b.Entities[].Profiles.FSA.EDINETCode ] },
      $local:expected.NIKKEI,
      { NoArrayOrder: true }
    ),
    example-json: test:invoke-and-assert-deep-equal(
      "entities",
      {edinetcode:"E01225"},
      function($b as item*) as item* { $b.Entities },
      test:get-expected-result("edinet/entities-expected1.jq")
    ),
    example-xml: test:invoke-and-assert-deep-equal(
      "entities",
      {edinetcode:"E01225", format: "xml"},
      function($b as item*) as item* { document { $b/Entities } },
      test:get-expected-result-xml("edinet/entities-expected1-xml.jq"),
      { Format: "xml" }
    ),
    example-search-industry: test:invoke-and-assert-deep-equal(
      "entities",
      {entity-search: "Transportation"},
      function($b as item*) as item* { size($b.Entities) ge 2 },
      true
    ),
    example-search-name: test:invoke-and-assert-deep-equal(
      "entities",
      {entity-search: "Nippon"},
      function($b as item*) as item* { size($b.Entities) ge 2},
      true
    ),
    example-offset-and-limit-search-name: test:invoke-and-assert-deep-equal(
      "entities",
      {entity-search: "Nippon", entity-search-offset: 1,entity-search-limit: 1},
      function($b as item*) as item* { size($b.Entities) eq 1},
      true
    )
})

import module namespace test = "http://apps.28.io/test";

declare variable $local:expected as object :=
{
  "all" : [ "E01225",
            "E01264",
            "E02166",
            "E02274",
            "E02513",
            "E02529",
            "E04147",
            "E04425",
            "E04430",
            "EDINET" ],
  "NIKKEI" : [ "E01225",
               "E01264",
               "E02166",
               "E02274",
               "E02513",
               "E02529",
               "E04147",
               "E04425",
               "E04430" ]
};

test:check-all-success({
    all: test:invoke-and-assert-deep-equal(
      "entities",
      {},
      function($b as item*) as item* { [ $b.Entities[].Profiles.FSA.EDINETCode ] },
      $local:expected.all,
      { NoArrayOrder: true }
    ),
    nikkei: test:invoke-and-assert-deep-equal(
      "entities",
      {tag:"NIKKEI"},
      function($b as item*) as item* { [ $b.Entities[].Profiles.FSA.EDINETCode ] },
      $local:expected.NIKKEI,
      { NoArrayOrder: true }
    ),
    example-json: test:invoke-and-assert-deep-equal(
      "entities",
      {edinetcode:"E01225"},
      function($b as item*) as item* { $b.Entities },
      test:get-expected-result("edinet/entities-expected1.jq")
    ),
    example-xml: test:invoke-and-assert-deep-equal(
      "entities",
      {edinetcode:"E01225", format: "xml"},
      function($b as item*) as item* { document { $b/Entities } },
      test:get-expected-result-xml("edinet/entities-expected1-xml.jq"),
      { Format: "xml" }
    ),
    example-search-industry: test:invoke-and-assert-deep-equal(
      "entities",
      {entity-search: "Transportation"},
      function($b as item*) as item* { size($b.Entities) ge 2 },
      true
    ),
    example-search-name: test:invoke-and-assert-deep-equal(
      "entities",
      {entity-search: "Nippon"},
      function($b as item*) as item* { size($b.Entities) ge 2},
      true
    )
})