> ## Documentation Index
> Fetch the complete documentation index at: https://docs.abbyy.com/llms.txt
> Use this file to discover all available pages before exploring further.

# アセンブリ スクリプトのサンプル

> ABBYY FlexiCapture のドキュメント セット用アセンブリ スクリプトのサンプル。1 つは融資額に応じて必須文書を追加し、もう 1 つは不足している文書を確認します。

これらのサンプル スクリプトでは、Document Set Definition で定義されたドキュメントセットに文書を組み立て、`AssemblingErrors` オブジェクトを通じてアセンブリ時の問題を報告します。最初のスクリプトでは、融資額に応じて必要な文書が異なります。2 つ目は、セットに想定される本人確認書類が含まれていることを確認します。

<div id="require-documents-based-on-the-loan-sum">
  ## 融資額に応じて必要書類を設定する
</div>

このスクリプトは、Document Set Definition で定義されたドキュメント セットに書類をまとめます。Document Set Definition には、**借入人フォーム** という名前のセクションと、次の Document Definitions への参照が含まれています。

* パスポート
* 保証人の書類
* 共同借入人の書類
* 勤務先証明書

**借入人フォーム** セクションには、次の field が含まれます。

* 共同借入人名
* 融資額

ドキュメント セットに含める必要がある書類は、**融資額** field の値によって決まります。どの書類が必須かを判定するために、このスクリプトでは 2 つのしきい値を使用します。

| 融資額                           | 必須書類                |
| ----------------------------- | ------------------- |
| 1 つ目のしきい値未満                   | パスポート               |
| 1 つ目のしきい値より大きく、2 つ目のしきい値より小さい | パスポートと勤務先証明書        |
| 2 つ目のしきい値より大きい                | パスポート、勤務先証明書、保証人の書類 |

**共同借入人名** field に値が含まれている場合、ドキュメント セットには共同借入人の書類も含まれている必要があります。

次のスクリプトでは、このロジックを使用して文書セットを構成します。

```csharp theme={null}
using System.Collections.Generic;
int creditSum = 0;
bool creditSumFound = false;
string cocreditor = "";
int formCount = 0;
string formName = "Borrower's form";
// ドキュメント セットで使用されるDocument Definitions
int referenceFromWorkCount = 0;
string referenceFromWorkName = "勤務先証明書";
int guaranteeDocsCount = 0;
string guaranteeDocsName = "Guarantor's documents";
int cocreditorDocsCount = 0;
string cocreditorDocsName = "Co-borrower's documents";
int passportCount = 0;
string passportName = "Passport";
foreach( IBatchItem item in BatchItems ) {
    // フォームを検索してローンの合計金額を確認する
    if (item.Type == TBatchItemType.BIT_Page)
    {
        if (item.AsPage.SectionName != formName)
            AssemblingErrors.AddCustomError("Cannot contain the following sections: " + item.AsPage.SectionName, 1);
        else
        {
            cocreditor = item.AsPage.Document.IndexedItemValue("Borrower's name").ToString();
            string creditSumText = item.AsPage.Document.IndexedItemValue("融資額").ToString();
            if (!int.TryParse(creditSumText, out creditSum))
                AssemblingErrors.AddCustomError("融資額 was not recognized: " + creditSumText, 1);
            else
                creditSumFound = true;
            formCount++;
        }
    }
    // セット内のドキュメント数をカウントする
    else if (item.Type == TBatchItemType.BIT_Document) {
        if (item.AsDocument.TemplateName == referenceFromWorkName)
            referenceFromWorkCount++;
        else if (item.AsDocument.TemplateName == guaranteeDocsName)
            guaranteeDocsCount++;
        else if (item.AsDocument.TemplateName == passportName)
            passportCount++;
        else if (item.AsDocument.TemplateName == cocreditorDocsName)
            cocreditorDocsCount++;
        else
            AssemblingErrors.AddCustomError("The ドキュメント セット cannot contain the document " + item.AsDocument.TemplateName, 1);
    }
}
// セット内のセクション数とドキュメント数を確認する
if (formCount > 1)
    AssemblingErrors.AddCustomError("There are too many documents of the type: " + formName, formCount);
else if (formCount < 1)
    AssemblingErrors.AddCustomError("Missing the following documents: " + formName, 1);
if (passportCount > 1)
    AssemblingErrors.AddCustomError("There are too many documents of the type: " + passportName, passportCount);
else if (passportCount < 1)
    AssemblingErrors.AddCustomError("Missing the following documents: " + passportName, 1);
// フォームに共同借入人名が存在する場合、ドキュメント セットに共同借入人が含まれているか確認する
if (cocreditor != "" && cocreditorDocsCount < 1)
    AssemblingErrors.AddCustomError("Missing the following documents: " + cocreditorDocsName, 1);
else if (cocreditor != "" && cocreditorDocsCount > 1)
    AssemblingErrors.AddCustomError("There are too many documents of the type: " + cocreditorDocsName, cocreditorDocsCount);
else if (cocreditor == "" && cocreditorDocsCount > 0)
    AssemblingErrors.AddCustomError("There are too many documents of the type: " + cocreditorDocsName, cocreditorDocsCount);
// ローン金額がいずれかの閾値を超えた場合に必要なすべてのドキュメントがドキュメント セットに含まれているか確認する
if (creditSumFound)
{
    if (creditSum > 50000) // 雇用主の証明書が必要
    {
        if (referenceFromWorkCount > 1)
            AssemblingErrors.AddCustomError("There are too many documents of the type: " + referenceFromWorkName, referenceFromWorkCount);
        else if (referenceFromWorkCount < 1)
            AssemblingErrors.AddCustomError("Missing the following documents: " + referenceFromWorkName, 1);
    }
    if (creditSum > 500000) // 保証人の書類が必要
    {
        if (guaranteeDocsCount > 1)
            AssemblingErrors.AddCustomError("There are too many documents of the type: " + guaranteeDocsName, guaranteeDocsCount);
        else if (guaranteeDocsCount < 1)
            AssemblingErrors.AddCustomError("Missing the following documents: " + guaranteeDocsName, 1);
    }
}
```

<div id="check-for-missing-or-duplicate-documents">
  ## 不足している文書や重複文書を確認する
</div>

このスクリプトは **Identification Documents** Document Set Definition で動作し、次の Document Definitions を参照します。

* Passport
* Driver's license

1 つのドキュメント セットには、各種類の文書を最大 1 つまで含めることができ、また、これらの文書のうち少なくとも 1 つを含める必要があります。

次のスクリプトでは、このロジックを使用してドキュメント セットを組み立てます。

```csharp theme={null}
using System.Collections.Generic;
using System;
string docSetName = "Identification Documents";
int pagesCnt = 0;
// セットに含めることができるドキュメントの種類のリスト
List<string> allowed = new List<string> {"Passport", "Driver's license"};
// セット内の各種類のドキュメント数の上限
List<int> allowedCount = new List<int>{1,1};
// セット内で検出されたドキュメントの数
List<int> foundCount = new List<int>{0,0};
// セット内のドキュメント数とページ数をカウントする
foreach (IBatchItem item in BatchItems) {
    if (item.Type == TBatchItemType.BIT_Document) {
        if (item.AsDocument.TemplateName == docSetName)
            AssemblingErrors.AddCustomError("Cannot contain a nested set", 1);
        else if (allowed.Contains(item.AsDocument.TemplateName)) {
            int i = allowed.IndexOf(item.AsDocument.TemplateName);
            foundCount[i]++;
        }
    }
    else if (item.Type == TBatchItemType.BIT_Page) pagesCnt++;
}
// ドキュメント数とページ数を確認する
if (foundCount[0] + foundCount[1] == 0)
    AssemblingErrors.AddCustomError("At least one identification document is required", 1);
for (int i = 0; i < allowed.Count; i++) {
    if (foundCount[i] > allowedCount[i])
        AssemblingErrors.AddCustomError("Maximum number of documents: " + allowed[i] + ": " + allowedCount[i]);
}
if (pagesCnt > 0) AssemblingErrors.AddCustomError("The document set cannot contain pages that do not belong to a document", pagesCnt);
```
