fairpolicy-commission-chargebacks 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # Changelog
2
+
3
+ ## 0.1.0 (unreleased)
4
+
5
+ - First version (Fairpolicy ver 1, built on fairpolicy-core).
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Kurt Francisco
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,213 @@
1
- # Temporary Holding Version
1
+ # Fairpolicy Commission Chargebacks (`fairpolicy-commission-chargebacks`)
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ For every cancelled policy, works out the commission chargeback on the unearned premium (pro rata or short rate), compares it with what the carrier took back, and flags overcharges, chargebacks still to come (a reserve), chargebacks with no cancellation in your system, and producer shares not reversed. Drafts a recalculation request; never posts, pays or sends.
4
+
5
+ **It never changes producer pay; your accounting staff reverse a share after review. It never sends e-mail; a person sends the approved request.**
6
+
7
+ MIT · Node 20 or newer · local files only · client identifiers masked before results reach the model · built on [`fairpolicy-core`](../fairpolicy-core)
8
+
9
+ ```bash
10
+ npx -y fairpolicy-commission-chargebacks demo
11
+ npx -y fairpolicy-commission-chargebacks mcp --root ./agency-exports
12
+ ```
13
+
14
+ <!-- generated:start -->
15
+
16
+ ## A demo run
17
+
18
+ `npx -y fairpolicy-commission-chargebacks demo` runs the agent on the fictional files in [`fixtures/`](fixtures) in a temporary folder. Captured output:
19
+
20
+ ```text
21
+ Fairpolicy Commission Chargebacks demo (fictional agency, carriers and clients)
22
+
23
+ ▶ Check every cancellation's chargeback
24
+ status: executed decision: allow
25
+ 5 cancellation(s) since 2026-05-22: 644.07 in chargebacks expected (pro rata), 639.25 taken; 1 overcharged by 181.48, 1 still to come (reserve 261.3), 1 chargeback(s) with no cancellation, 1 not reversed on producer pay.
26
+ HO-3002 2026-09-01 178.52 360 overcharged
27
+ HO-3999 - 75 chargeback_without_cancel
28
+ PA-3004 2026-08-01 54 54 producer_not_reversed
29
+ BOP-3003 2026-10-15 261.3 0 pending_chargeback
30
+ PA-3001 2026-07-02 90.25 90.25 charged_ok
31
+ UMB-3005 2026-08-15 60 60 charged_ok
32
+
33
+ ▶ Explain the homeowners chargeback
34
+ status: executed decision: allow
35
+ HO-3002: overcharged. Charged back 360; 15% of the unearned premium 1190.14 is 178.52, so 181.48 too much. Next: Ask the carrier to recalculate: the chargeback is more than the commission on the unearned premium.
36
+
37
+ ▶ Write the worklist (CSV and HTML, real names stay on this computer)
38
+ status: executed decision: allow
39
+ Cancellation chargebacks written: 5 cancellation(s) since 2026-05-22: 644.07 in chargebacks expected (pro rata), 639.25 taken; 1 overcharged by 181.48, 1 still to come (reserve 261.3), 1 chargeback(s) with no cancellation, 1 not reversed on producer pay. Files: fairpolicy-out/chargebacks/commission-chargebacks.csv, fairpolicy-out/chargebacks/commission-chargebacks.html.
40
+
41
+ ▶ Draft a recalculation request to the carrier
42
+ status: executed decision: allow (auto)
43
+ recalculation Example Mutual drafted for 181.48 with 2 open marker(s) ([TO CONFIRM carrier commission contact], [CHECK whether the carrier used short-rate cancellation; our figures are pro rata.]). Nothing was sent.
44
+
45
+ ▶ Approve it: waits for a named person even in Auto
46
+ status: needs_approval decision: ask
47
+ reason: binding_send stays one-click, even in Auto
48
+
49
+ ▶ Same call after the click (recorded; a person sends it)
50
+ status: executed decision: ask approved by: Casey Park (fictional)
51
+ recalculation 3ea1f6fef2dd approved and recorded. Nothing was sent or paid by the agent.
52
+
53
+ ▶ The agent tries producer_pay_adjust itself
54
+ status: blocked decision: block
55
+ reason: It never changes producer pay; your accounting staff reverse a share after review.
56
+ ```
57
+
58
+ ## Tools
59
+
60
+ | Tool | What it does | Policy |
61
+ |---|---|---|
62
+ | `chargebacks_check` | Each cancelled policy: unearned premium, expected chargeback, what was taken and a status (overcharged, chargeback_without_cancel, producer_not_reversed, pending_chargeback, charged_ok). | runs |
63
+ | `chargebacks_explain` | Show one policy from chargebacks_check in full: its status, every finding and the figures behind it. | runs |
64
+ | `chargebacks_write_report` | Write commission-chargebacks.csv (every policy with its status and the figures behind it, plus blank owner and notes columns) and commission-chargebacks.html into out_dir (default fairpolicy-out/chargebacks). | runs |
65
+ | `chargebacks_draft_recalculation` | Write drafts/recalculation-<carrier>.md listing one carrier's overcharged chargebacks with the unearned premium and the difference. | asks first (Auto can be opted in) |
66
+ | `chargebacks_request_send` | After one named human click (even in Auto), record that the reviewed recalculation draft is approved in recalculation-requests.csv. | one named click, even in Auto (`binding_send`) |
67
+ | `producer_pay_adjust` | It never changes producer pay; your accounting staff reverse a share after review. | refused |
68
+ | `email_send` | It never sends e-mail; a person sends the approved request. | refused |
69
+
70
+ Any tool name not in this table is blocked, including names a model invents. `refused` tools are listed so a model can see they exist and are refused.
71
+
72
+ ## Columns it reads
73
+
74
+ Table inputs can be CSV, TSV, semicolon- or pipe-separated text, Excel `.xlsx`, JSON or JSONL. Columns are matched by the agent's own name, then by the English names below, then by their names in German, French, Spanish, Portuguese, Italian, Japanese, Chinese and Korean. Dates become `YYYY-MM-DD`; amounts are read with either decimal mark. Every result lists under `sources` how each file was read. Columns marked *masked* hold patient identifiers: the model sees a pseudonym, a birth year or nothing (see Patient data).
75
+
76
+ ### Policy list (`policies`)
77
+
78
+ | Column | Type | Also read as | For example, in other languages |
79
+ |---|---|---|---|
80
+ | `policy_number` (required) | text | Policy Number, Policy #, Policy No, Policy, Pol #, Policy Num | Vertragsnummer, N° de police, 証券番号, 保单号 |
81
+ | `client` | text (masked) | Client Name, Insured, Insured Name, Named Insured, Account Name, Customer Name | Versicherungsnehmer, Assuré, 契約者, 投保人 |
82
+ | `client_id` | text (masked) | Client Code, Lookup Code, Account Lookup Code, Customer #, Client Number, Client ID | Kundennummer, Code client, 顧客コード, 客户编号 |
83
+ | `carrier` | text | Carrier, Company, Writing Company, Insurer, Issuing Company, Carrier Name | Versicherer, Assureur, 保険会社, 保险公司 |
84
+ | `line` | text | Line of Business, LOB, Policy Type, Line Type, Coverage, Line | Sparte, Branche, 保険種目, 险种 |
85
+ | `effective_date` | date | Effective Date, Eff Date, Effective, Policy Effective Date, Inception Date, Term Effective | Beginn, Date d'effet, 始期, 生效日期 |
86
+ | `expiration_date` | date | Expiration Date, Exp Date, Expiration, Policy Expiration Date, X-Date, Xdate | Ablauf, Date d'échéance, 満期, 到期日期 |
87
+ | `premium` | amount | Premium, Annualized Premium, Annual Premium, Written Premium, Full Term Premium, Estimated Premium | Beitrag, Prime, 保険料, 保费 |
88
+ | `commission_rate` | number | Commission Rate, Comm Rate, Commission %, Comm %, Agency Commission %, Commission Percent | Courtagesatz, Taux de commission, 手数料率, 佣金率 |
89
+ | `commission` | amount | Commission, Agency Commission, Estimated Commission, Commission Amount, Annualized Commission, Revenue | Courtage, Commission, 手数料, 佣金 |
90
+ | `producer` | text | Producer, Producer Name, Exec, Account Executive, Agent, Producer 1 | Vermittler, Producteur, 募集人, 业务员 |
91
+ | `csr` | text | CSR, Rep, Account Manager, Service Rep, Customer Service Rep, Servicer | Sachbearbeiter, Gestionnaire, 事務担当, 客服 |
92
+ | `status` | text | Status, Policy Status, Current Status | Status, Statut, ステータス, 状态 |
93
+ | `bill_type` | text | Bill Type, Billing Method, Bill Method, Billing Type, Agency/Direct, Bill Plan | Inkassoart, Mode d’encaissement, 集金方法, 收费方式 |
94
+ | `business_type` | text | New/Renewal, New or Renewal, NB/RN, Business Type, New Business/Renewal | - |
95
+ | `segment` | text | Department, Division, Personal/Commercial, Book, Line Group, PL/CL | - |
96
+ | `state` | text | State, Policy State, Risk State, Base State, Garaging State | Bundesland, Région, 都道府県, 省份 |
97
+ | `cancel_date` | date | Cancel Date, Cancellation Date, Cancelled Date, Cancellation Effective Date, Cancel Effective Date | Stornodatum, Date de résiliation, 解約日, 退保日期 |
98
+ | `cancel_reason` | text | Cancel Reason, Cancellation Reason, Lost Reason, Reason Lost | - |
99
+
100
+ Named exports recognised by their column names:
101
+
102
+ | System | Export | Recognised by | Layout checked against vendor docs |
103
+ |---|---|---|---|
104
+ | Applied Epic | Policy list report exported to Excel or CSV | Lookup Code, Account Name, Policy Number, Line Type | no; Applied Epic list reports let each agency choose its columns; the names here are common defaults, not a fixed layout. |
105
+ | Vertafore AMS360 | Policy or expiration list exported to CSV | Customer #, Customer Name, Policy #, Exec, Rep | no; AMS360 calls the producer "Exec" and the service person "Rep". |
106
+ | Vertafore Sagitta | Policy list report (CSV) | Client Code, Client Name, Policy Number, Coverage, Insurer | no |
107
+ | HawkSoft | Client/policy export (CSV) | Client Number, Client Name, Policy Number, Line of Business, Carrier, Policy Status | no |
108
+ | EZLynx | Policy report export (CSV) | Applicant, Policy Number, Line of Business, Carrier, Policy Status | no |
109
+ | QQCatalyst | Policy list export (CSV) | Contact Name, Policy Number, Line of Business, Carrier, Effective Date, Expiration Date | no |
110
+
111
+ Every layout here was written from knowledge of the export ("recalled") and was not checked against vendor documentation or a live account. Agency systems let each agency choose report columns, so the first setup confirms them with inspect.
112
+
113
+ ### Commission statement lines (`statements`)
114
+
115
+ | Column | Type | Also read as | For example, in other languages |
116
+ |---|---|---|---|
117
+ | `policy_number` (required) | text | Policy Number, Policy #, Policy No, Policy, Pol #, Policy Num | Vertragsnummer, N° de police, 証券番号, 保单号 |
118
+ | `client` | text (masked) | Insured Name, Insured, Named Insured, Client Name, Customer Name, Name | Versicherungsnehmer, Assuré, 契約者, 投保人 |
119
+ | `carrier` | text | Carrier, Company, Insurer, Writing Company, Carrier Name | Versicherer, Assureur, 保険会社, 保险公司 |
120
+ | `line` | text | Line of Business, LOB, Policy Type, Line, Coverage, Product | Sparte, Branche, 保険種目, 险种 |
121
+ | `transaction_type` | text | Transaction Type, Trans Type, Transaction, Type, Activity, Trans Code | Buchungsart, Type de mouvement, 取引区分, 交易类型 |
122
+ | `effective_date` | date | Effective Date, Eff Date, Transaction Effective Date, Trans Eff Date, Policy Effective Date | Beginn, Date d'effet, 始期, 生效日期 |
123
+ | `statement_date` | date | Statement Date, Process Date, Accounting Date, Transaction Date, Posted Date, Period | - |
124
+ | `premium` | amount | Premium, Written Premium, Gross Premium, Transaction Premium, Premium Amount, Premium Basis | Beitrag, Prime, 保険料, 保费 |
125
+ | `commission_rate` | number | Commission Rate, Comm Rate, Rate, Comm %, Commission %, Comm Pct | Courtagesatz, Taux de commission, 手数料率, 佣金率 |
126
+ | `commission` (required) | amount | Commission, Commission Amount, Comm Amount, Agency Commission, Commission Paid, Net Commission | Courtage, Commission, 手数料, 佣金 |
127
+ | `statement_id` | text | Statement Number, Statement #, Statement ID, Statement, Statement No | - |
128
+ | `producer` | text | Producer, Agent, Producer Code, Sub Producer | Vermittler, Producteur, 募集人, 业务员 |
129
+
130
+ Named exports recognised by their column names:
131
+
132
+ | System | Export | Recognised by | Layout checked against vendor docs |
133
+ |---|---|---|---|
134
+ | Carrier commission statement | Direct bill commission statement saved as CSV (or PDF text pasted into CSV) | Policy Number, Insured Name, Commission Rate, Commission Amount | no; Each carrier lays its statement out differently; this is the common shape. Map a carrier's own columns in fairpolicy-sources.json. |
135
+ | Maklerabrechnung (DE) | Courtageabrechnung eines Versicherers (CSV) | Vertragsnummer, Versicherungsnehmer, Courtagesatz, Courtage | no; German labels, dates day-first, comma decimals. |
136
+
137
+ Every layout here was written from knowledge of the export ("recalled") and was not checked against vendor documentation or a live account. Agency systems let each agency choose report columns, so the first setup confirms them with inspect.
138
+
139
+ ### Producer commission file (`producer_pay`)
140
+
141
+ | Column | Type | Also read as | For example, in other languages |
142
+ |---|---|---|---|
143
+ | `policy_number` (required) | text | Policy Number, Policy #, Policy | Vertragsnummer, N° de police, 証券番号, 保单号 |
144
+ | `producer` | text | Producer, Producer Name, Agent | Vermittler, Producteur, 募集人, 业务员 |
145
+ | `amount` | amount | Amount, Commission, Producer Commission, Paid, Split Amount | Betrag, Montant, 金額, 金额 |
146
+ | `type` | text | Type, Transaction, Entry Type | - |
147
+
148
+ ### Files from other countries
149
+
150
+ A German export in Windows-1252 with semicolons, `1.234,56` and `03.11.2026`, or a Japanese one in Shift_JIS with `2026/11/03`, is read the same way as the English file: `npm run intl` in the repository rewrites every eval fixture the way a German, French, Spanish, Brazilian, Italian, Japanese, Chinese and Korean export would write it and requires the same results. Columns named differently? Run `npx fairpolicy-commission-chargebacks inspect <file>` (names and shapes only, never values) and name them in `fairpolicy-sources.json`:
151
+
152
+ ```json
153
+ { "files": { "exports/claims.csv": { "columns": { "claim_id": "Fallnummer" }, "date_order": "dmy" } } }
154
+ ```
155
+
156
+ ## Install
157
+
158
+ Node 20 or newer. No other dependencies.
159
+
160
+ ```bash
161
+ npx -y fairpolicy-commission-chargebacks demo
162
+ npx -y fairpolicy-commission-chargebacks mcp --root /path/to/your/files
163
+ ```
164
+
165
+ Claude Desktop, Cursor and other MCP hosts:
166
+
167
+ ```json
168
+ {
169
+ "mcpServers": {
170
+ "fairpolicy-commission-chargebacks": {
171
+ "command": "npx",
172
+ "args": [
173
+ "-y",
174
+ "fairpolicy-commission-chargebacks",
175
+ "mcp",
176
+ "--root",
177
+ "/path/to/your/files"
178
+ ]
179
+ }
180
+ }
181
+ }
182
+ ```
183
+
184
+ Claude Code: `claude mcp add fairpolicy-commission-chargebacks -- npx -y fairpolicy-commission-chargebacks mcp --root .`, or install the `fairpolicy-commission-chargebacks` plugin from this repository's marketplace (`fairpolicy-medical-billing`).
185
+
186
+ ## Modes and approvals
187
+
188
+ - **Review** (`--mode review`): reads and reports only.
189
+ - **Assist** (default): the policy above. A tool that asks returns an `approval_id`; a person approves it once with `fairpolicy-commission-chargebacks approve <id> --as "Your Name"` (or on the local approvals page, `fairpolicy-commission-chargebacks approvals`), and the same call is repeated.
190
+ - **Auto** (`--mode auto --auto <tool,...>`): opt-in per tool, with step caps and a kill switch (`fairpolicy-commission-chargebacks kill`). Sending, moving money, going live and accepting a learned fact stay **one named click even in Auto**; an approval is tied to the sha256 of the reviewed draft, so a changed draft needs a new click.
191
+
192
+ ## CLI
193
+
194
+ ```bash
195
+ fairpolicy-commission-chargebacks tools # every tool, its policy and arguments
196
+ fairpolicy-commission-chargebacks sources # the columns each input is read from
197
+ fairpolicy-commission-chargebacks inspect <file> # how a file would be read (names and shapes, never values)
198
+ fairpolicy-commission-chargebacks call chargebacks_check '{"policies":"<policies>","statements":"<statements>"}'
199
+ fairpolicy-commission-chargebacks eval # the private eval cases on the fictional fixtures
200
+ fairpolicy-commission-chargebacks demo
201
+ ```
202
+
203
+ ## Client data
204
+
205
+ Results go to an AI model, so client identifiers are masked before they leave the agent (`--phi mask`, the default; the flag name comes from the shared core): client and insured names and client codes become stable pseudonyms such as `CL-3f9a1c2e`; birth dates become the birth year; driver licence numbers, addresses, phones, e-mails and SSNs are removed, also inside free text. `--phi strict` also hides policy numbers, claim numbers and VINs and cuts ZIP codes to three digits. Producers, account managers and carriers are not masked. The files the agent writes on your computer keep the real values, because your staff need them: keep them in a secure folder. Masking is not anonymisation, and it does not replace your agency's information-security program.
206
+
207
+
208
+ ## Limits
209
+
210
+ - It checks only what is in the files and settings you load. Commission schedules, state rules, deadlines and carrier requirements are as current as what you give it; the example settings are starting points, not any carrier's or state's rule.
211
+ - It is not legal, tax, accounting or coverage advice. It never binds, cancels, endorses or places coverage, and it never tells a client what to buy. A person reviews every result before anything goes to a client, carrier or producer; the agent sends nothing.
212
+ - Export layouts named in "Columns it reads" were written from knowledge of each system and not tested against a live account; `inspect` shows how a file was read.
213
+ - Client-data masking keeps identifiers away from the model; it does not replace your agency's privacy and information-security program (state insurance data security rules, access control, retention).
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env node
2
+ import { fileURLToPath } from 'node:url';
3
+ import { main } from 'fairpolicy-core';
4
+ import { manifest } from '../src/manifest.js';
5
+ import { demo } from '../src/demo.js';
6
+
7
+ main(manifest, { bin: 'fairpolicy-commission-chargebacks', demo, evalFile: fileURLToPath(new URL('../evals/cases.json', import.meta.url)) });
@@ -0,0 +1,370 @@
1
+ {
2
+ "suite": "fairpolicy-commission-chargebacks",
3
+ "root": "..",
4
+ "data_note": "Fictional policies, statements and producer pay in ../fixtures. No model is called.",
5
+ "cases": [
6
+ {
7
+ "id": "check-totals",
8
+ "tool": "chargebacks_check",
9
+ "args": {
10
+ "policies": "fixtures/policies.csv",
11
+ "statements": "fixtures/statements.csv",
12
+ "producer_pay": "fixtures/producer-pay.csv",
13
+ "as_of": "2026-11-18"
14
+ },
15
+ "expect": {
16
+ "status": "executed",
17
+ "equals": {
18
+ "result.cancellations": 5,
19
+ "result.expected_chargebacks": 644.07,
20
+ "result.taken": 639.25,
21
+ "result.reserve_for_pending": 261.3,
22
+ "result.overcharged_by": 181.48
23
+ }
24
+ }
25
+ },
26
+ {
27
+ "id": "every-status",
28
+ "tool": "chargebacks_check",
29
+ "args": {
30
+ "policies": "fixtures/policies.csv",
31
+ "statements": "fixtures/statements.csv",
32
+ "producer_pay": "fixtures/producer-pay.csv",
33
+ "as_of": "2026-11-18"
34
+ },
35
+ "expect": {
36
+ "equals": {
37
+ "result.by_status.overcharged": 1,
38
+ "result.by_status.chargeback_without_cancel": 1,
39
+ "result.by_status.producer_not_reversed": 1,
40
+ "result.by_status.pending_chargeback": 1,
41
+ "result.by_status.charged_ok": 2
42
+ }
43
+ }
44
+ },
45
+ {
46
+ "id": "overcharge-figures",
47
+ "tool": "chargebacks_explain",
48
+ "args": {
49
+ "policies": "fixtures/policies.csv",
50
+ "statements": "fixtures/statements.csv",
51
+ "as_of": "2026-11-18",
52
+ "policy_number": "HO-3002"
53
+ },
54
+ "expect": {
55
+ "equals": {
56
+ "result.policy.status": "overcharged",
57
+ "result.policy.unearned": 1190.14,
58
+ "result.policy.expected": 178.52,
59
+ "result.policy.taken": 360,
60
+ "result.policy.difference": 181.48
61
+ }
62
+ }
63
+ },
64
+ {
65
+ "id": "flat-cancel-takes-full-commission",
66
+ "tool": "chargebacks_explain",
67
+ "args": {
68
+ "policies": "fixtures/policies.csv",
69
+ "statements": "fixtures/statements.csv",
70
+ "as_of": "2026-11-18",
71
+ "policy_number": "UMB-3005"
72
+ },
73
+ "expect": {
74
+ "equals": {
75
+ "result.policy.status": "charged_ok",
76
+ "result.policy.flat_cancel": true,
77
+ "result.policy.expected": 60
78
+ }
79
+ }
80
+ },
81
+ {
82
+ "id": "six-month-term",
83
+ "tool": "chargebacks_explain",
84
+ "args": {
85
+ "policies": "fixtures/policies.csv",
86
+ "statements": "fixtures/statements.csv",
87
+ "as_of": "2026-11-18",
88
+ "policy_number": "PA-3004"
89
+ },
90
+ "expect": {
91
+ "equals": {
92
+ "result.policy.unearned": 450,
93
+ "result.policy.expected": 54
94
+ }
95
+ }
96
+ },
97
+ {
98
+ "id": "without-producer-file-no-producer-check",
99
+ "tool": "chargebacks_check",
100
+ "args": {
101
+ "policies": "fixtures/policies.csv",
102
+ "statements": "fixtures/statements.csv",
103
+ "as_of": "2026-11-18"
104
+ },
105
+ "expect": {
106
+ "equals": {
107
+ "result.producer_checked": false,
108
+ "result.by_status.charged_ok": 3
109
+ }
110
+ }
111
+ },
112
+ {
113
+ "id": "short-rate-changes-the-reserve",
114
+ "tool": "chargebacks_check",
115
+ "args": {
116
+ "policies": "fixtures/policies.csv",
117
+ "statements": "fixtures/statements.csv",
118
+ "as_of": "2026-11-18",
119
+ "method": "short_rate"
120
+ },
121
+ "expect": {
122
+ "equals": {
123
+ "result.method": "short_rate",
124
+ "result.reserve_for_pending": 235.17
125
+ }
126
+ }
127
+ },
128
+ {
129
+ "id": "pending-reserve",
130
+ "tool": "chargebacks_check",
131
+ "args": {
132
+ "policies": "fixtures/policies.csv",
133
+ "statements": "fixtures/statements.csv",
134
+ "as_of": "2026-11-18",
135
+ "status": "pending_chargeback"
136
+ },
137
+ "expect": {
138
+ "equals": {
139
+ "result.items.0.policy_number": "BOP-3003",
140
+ "result.items.0.expected": 261.3
141
+ }
142
+ }
143
+ },
144
+ {
145
+ "id": "stray-chargeback",
146
+ "tool": "chargebacks_check",
147
+ "args": {
148
+ "policies": "fixtures/policies.csv",
149
+ "statements": "fixtures/statements.csv",
150
+ "as_of": "2026-11-18",
151
+ "status": "chargeback_without_cancel"
152
+ },
153
+ "expect": {
154
+ "equals": {
155
+ "result.items.0.policy_number": "HO-3999",
156
+ "result.items.0.taken": 75
157
+ }
158
+ }
159
+ },
160
+ {
161
+ "id": "report-written",
162
+ "tool": "chargebacks_write_report",
163
+ "args": {
164
+ "policies": "fixtures/policies.csv",
165
+ "statements": "fixtures/statements.csv",
166
+ "producer_pay": "fixtures/producer-pay.csv",
167
+ "as_of": "2026-11-18"
168
+ },
169
+ "expect": {
170
+ "status": "executed",
171
+ "contains": [
172
+ "commission-chargebacks.csv",
173
+ "commission-chargebacks.html"
174
+ ]
175
+ }
176
+ },
177
+ {
178
+ "id": "draft-recalculation",
179
+ "tool": "chargebacks_draft_recalculation",
180
+ "approve": "Casey Park",
181
+ "args": {
182
+ "policies": "fixtures/policies.csv",
183
+ "statements": "fixtures/statements.csv",
184
+ "as_of": "2026-11-18",
185
+ "carrier": "Example Mutual"
186
+ },
187
+ "expect": {
188
+ "status": "executed",
189
+ "equals": {
190
+ "result.amount": 181.48,
191
+ "result.policies": 1
192
+ }
193
+ }
194
+ },
195
+ {
196
+ "id": "no-recalculation-without-overcharge",
197
+ "tool": "chargebacks_draft_recalculation",
198
+ "mode": "auto",
199
+ "auto_actions": [
200
+ "chargebacks_draft_recalculation"
201
+ ],
202
+ "args": {
203
+ "policies": "fixtures/policies.csv",
204
+ "statements": "fixtures/statements.csv",
205
+ "as_of": "2026-11-18",
206
+ "carrier": "Sample Casualty"
207
+ },
208
+ "expect": {
209
+ "status": "error",
210
+ "contains": [
211
+ "no overcharged"
212
+ ]
213
+ }
214
+ },
215
+ {
216
+ "id": "safety-invented-tool-refused",
217
+ "tool": "chargebacks_pay_and_send_everything_now",
218
+ "args": {},
219
+ "expect": {
220
+ "status": "blocked"
221
+ }
222
+ },
223
+ {
224
+ "id": "safety-file-outside-root-refused",
225
+ "tool": "chargebacks_check",
226
+ "args": {
227
+ "policies": "../../outside.csv",
228
+ "statements": "fixtures/statements.csv",
229
+ "producer_pay": "fixtures/producer-pay.csv",
230
+ "as_of": "2026-11-18"
231
+ },
232
+ "expect": {
233
+ "status": "error",
234
+ "contains": [
235
+ "outside"
236
+ ]
237
+ }
238
+ },
239
+ {
240
+ "id": "safety-review-mode-still-reads",
241
+ "tool": "chargebacks_check",
242
+ "mode": "review",
243
+ "args": {
244
+ "policies": "fixtures/policies.csv",
245
+ "statements": "fixtures/statements.csv",
246
+ "producer_pay": "fixtures/producer-pay.csv",
247
+ "as_of": "2026-11-18"
248
+ },
249
+ "expect": {
250
+ "status": "executed",
251
+ "equals": {
252
+ "result.cancellations": 5,
253
+ "result.expected_chargebacks": 644.07,
254
+ "result.taken": 639.25,
255
+ "result.reserve_for_pending": 261.3,
256
+ "result.overcharged_by": 181.48
257
+ }
258
+ }
259
+ },
260
+ {
261
+ "id": "safety-missing-argument-named",
262
+ "tool": "chargebacks_check",
263
+ "args": {
264
+ "statements": "fixtures/statements.csv",
265
+ "producer_pay": "fixtures/producer-pay.csv",
266
+ "as_of": "2026-11-18"
267
+ },
268
+ "expect": {
269
+ "status": "error",
270
+ "contains": [
271
+ "policies"
272
+ ]
273
+ }
274
+ },
275
+ {
276
+ "id": "safety-unknown-argument-refused",
277
+ "tool": "chargebacks_check",
278
+ "args": {
279
+ "policies": "fixtures/policies.csv",
280
+ "statements": "fixtures/statements.csv",
281
+ "producer_pay": "fixtures/producer-pay.csv",
282
+ "as_of": "2026-11-18",
283
+ "send_to": "carrier@example.test"
284
+ },
285
+ "expect": {
286
+ "status": "error",
287
+ "contains": [
288
+ "send_to"
289
+ ]
290
+ }
291
+ },
292
+ {
293
+ "id": "safety-refused-producer_pay_adjust",
294
+ "tool": "producer_pay_adjust",
295
+ "mode": "auto",
296
+ "args": {},
297
+ "expect": {
298
+ "status": "blocked"
299
+ }
300
+ },
301
+ {
302
+ "id": "safety-refused-email_send",
303
+ "tool": "email_send",
304
+ "mode": "auto",
305
+ "args": {},
306
+ "expect": {
307
+ "status": "blocked"
308
+ }
309
+ },
310
+ {
311
+ "id": "safety-review-mode-drafts-nothing",
312
+ "tool": "chargebacks_draft_recalculation",
313
+ "mode": "review",
314
+ "args": {
315
+ "policies": "fixtures/policies.csv",
316
+ "statements": "fixtures/statements.csv",
317
+ "as_of": "2026-11-18",
318
+ "carrier": "Example Mutual"
319
+ },
320
+ "expect": {
321
+ "status": "blocked"
322
+ }
323
+ },
324
+ {
325
+ "id": "safety-draft-waits-for-a-yes",
326
+ "tool": "chargebacks_draft_recalculation",
327
+ "args": {
328
+ "policies": "fixtures/policies.csv",
329
+ "statements": "fixtures/statements.csv",
330
+ "as_of": "2026-11-18",
331
+ "carrier": "Example Mutual"
332
+ },
333
+ "expect": {
334
+ "status": "needs_approval"
335
+ }
336
+ },
337
+ {
338
+ "id": "safety-approve-stays-one-click-in-auto",
339
+ "tool": "chargebacks_request_send",
340
+ "mode": "auto",
341
+ "args": {
342
+ "draft": "fixtures/README.md",
343
+ "draft_sha256": "0000000000000000000000000000000000000000000000000000000000000000"
344
+ },
345
+ "expect": {
346
+ "status": "needs_approval"
347
+ }
348
+ },
349
+ {
350
+ "id": "safety-masking-hides-names",
351
+ "tool": "chargebacks_check",
352
+ "args": {
353
+ "policies": "fixtures/policies.csv",
354
+ "statements": "fixtures/statements.csv",
355
+ "producer_pay": "fixtures/producer-pay.csv",
356
+ "as_of": "2026-11-18"
357
+ },
358
+ "expect": {
359
+ "status": "executed",
360
+ "not_contains": [
361
+ "Delphine Arkwright",
362
+ "Somerby Tile Works",
363
+ "Wilhelmina Craddock",
364
+ "Quillon Haverty",
365
+ "Thaddeus Pomfret"
366
+ ]
367
+ }
368
+ }
369
+ ]
370
+ }
@@ -0,0 +1,30 @@
1
+ {
2
+ "suite": "fairpolicy-commission-chargebacks", "root": "..",
3
+ "data_note": "Fictional policies, statements and producer pay in ../fixtures. No model is called.",
4
+ "cases": [
5
+ { "id": "check-totals", "tool": "chargebacks_check", "args": { "policies": "fixtures/policies.csv", "statements": "fixtures/statements.csv", "producer_pay": "fixtures/producer-pay.csv", "as_of": "2026-11-18" },
6
+ "expect": { "status": "executed", "equals": { "result.cancellations": 5, "result.expected_chargebacks": 644.07, "result.taken": 639.25, "result.reserve_for_pending": 261.3, "result.overcharged_by": 181.48 } } },
7
+ { "id": "every-status", "tool": "chargebacks_check", "args": { "policies": "fixtures/policies.csv", "statements": "fixtures/statements.csv", "producer_pay": "fixtures/producer-pay.csv", "as_of": "2026-11-18" },
8
+ "expect": { "equals": { "result.by_status.overcharged": 1, "result.by_status.chargeback_without_cancel": 1, "result.by_status.producer_not_reversed": 1, "result.by_status.pending_chargeback": 1, "result.by_status.charged_ok": 2 } } },
9
+ { "id": "overcharge-figures", "tool": "chargebacks_explain", "args": { "policies": "fixtures/policies.csv", "statements": "fixtures/statements.csv", "as_of": "2026-11-18", "policy_number": "HO-3002" },
10
+ "expect": { "equals": { "result.policy.status": "overcharged", "result.policy.unearned": 1190.14, "result.policy.expected": 178.52, "result.policy.taken": 360, "result.policy.difference": 181.48 } } },
11
+ { "id": "flat-cancel-takes-full-commission", "tool": "chargebacks_explain", "args": { "policies": "fixtures/policies.csv", "statements": "fixtures/statements.csv", "as_of": "2026-11-18", "policy_number": "UMB-3005" },
12
+ "expect": { "equals": { "result.policy.status": "charged_ok", "result.policy.flat_cancel": true, "result.policy.expected": 60 } } },
13
+ { "id": "six-month-term", "tool": "chargebacks_explain", "args": { "policies": "fixtures/policies.csv", "statements": "fixtures/statements.csv", "as_of": "2026-11-18", "policy_number": "PA-3004" },
14
+ "expect": { "equals": { "result.policy.unearned": 450, "result.policy.expected": 54 } } },
15
+ { "id": "without-producer-file-no-producer-check", "tool": "chargebacks_check", "args": { "policies": "fixtures/policies.csv", "statements": "fixtures/statements.csv", "as_of": "2026-11-18" },
16
+ "expect": { "equals": { "result.producer_checked": false, "result.by_status.charged_ok": 3 } } },
17
+ { "id": "short-rate-changes-the-reserve", "tool": "chargebacks_check", "args": { "policies": "fixtures/policies.csv", "statements": "fixtures/statements.csv", "as_of": "2026-11-18", "method": "short_rate" },
18
+ "expect": { "equals": { "result.method": "short_rate", "result.reserve_for_pending": 235.17 } } },
19
+ { "id": "pending-reserve", "tool": "chargebacks_check", "args": { "policies": "fixtures/policies.csv", "statements": "fixtures/statements.csv", "as_of": "2026-11-18", "status": "pending_chargeback" },
20
+ "expect": { "equals": { "result.items.0.policy_number": "BOP-3003", "result.items.0.expected": 261.3 } } },
21
+ { "id": "stray-chargeback", "tool": "chargebacks_check", "args": { "policies": "fixtures/policies.csv", "statements": "fixtures/statements.csv", "as_of": "2026-11-18", "status": "chargeback_without_cancel" },
22
+ "expect": { "equals": { "result.items.0.policy_number": "HO-3999", "result.items.0.taken": 75 } } },
23
+ { "id": "report-written", "tool": "chargebacks_write_report", "args": { "policies": "fixtures/policies.csv", "statements": "fixtures/statements.csv", "producer_pay": "fixtures/producer-pay.csv", "as_of": "2026-11-18" },
24
+ "expect": { "status": "executed", "contains": ["commission-chargebacks.csv", "commission-chargebacks.html"] } },
25
+ { "id": "draft-recalculation", "tool": "chargebacks_draft_recalculation", "approve": "Casey Park", "args": { "policies": "fixtures/policies.csv", "statements": "fixtures/statements.csv", "as_of": "2026-11-18", "carrier": "Example Mutual" },
26
+ "expect": { "status": "executed", "equals": { "result.amount": 181.48, "result.policies": 1 } } },
27
+ { "id": "no-recalculation-without-overcharge", "tool": "chargebacks_draft_recalculation", "mode": "auto", "auto_actions": ["chargebacks_draft_recalculation"], "args": { "policies": "fixtures/policies.csv", "statements": "fixtures/statements.csv", "as_of": "2026-11-18", "carrier": "Sample Casualty" },
28
+ "expect": { "status": "error", "contains": ["no overcharged"] } }
29
+ ]
30
+ }
@@ -0,0 +1,16 @@
1
+ # Fairpolicy Commission Chargebacks fixtures
2
+
3
+ Everything here is fictional: agency, carriers, clients, policy numbers and amounts.
4
+
5
+ - `policies.csv`: seven policies in an EZLynx-style export; five cancelled since May 2026, one active, one cancelled in March (outside the 180-day window as of 2026-11-18).
6
+ - `statements.csv`: carrier statement lines with the chargebacks taken.
7
+ - `producer-pay.csv`: the producer's commission and reversals.
8
+
9
+ Planted (pro rata, as of 2026-11-18):
10
+ - HO-3002: the carrier took back 360.00, the full commission; 15% of the unearned 1190.14 is 178.52, so 181.48 too much.
11
+ - BOP-3003: cancelled 2026-10-15, no chargeback yet: reserve 261.30.
12
+ - PA-3004: the carrier took back 54.00 (right); the producer's 27.00 was never reversed.
13
+ - HO-3999: a 75.00 chargeback for a policy not in the policy list.
14
+ - PA-3001 (90.25) and UMB-3005 (flat cancel, 60.00): right.
15
+
16
+ Expected: 5 cancellations, 644.07 expected, 639.25 taken (including the stray 75.00).
@@ -0,0 +1,5 @@
1
+ # Clean set
2
+
3
+ The demo files with every planted problem in [../README.md](../README.md) removed, made by `scripts/accuracy.mjs --write` from `scripts/accuracy-specs.mjs` in the repository. Only the files listed here change; the agent reads the other demo files as they are. The check must report nothing that needs a person on these files. Fictional data.
4
+
5
+ Files: `statements.csv`, `producer-pay.csv`.
@@ -0,0 +1,7 @@
1
+ Producer,Policy Number,Amount,Type
2
+ Jordan Lee,PA-3001,45.00,Commission
3
+ Jordan Lee,PA-3001,-45.13,Chargeback
4
+ Jordan Lee,PA-3004,27.00,Commission
5
+ Jordan Lee,HO-3002,90.00,Commission
6
+ Jordan Lee,HO-3002,-44.63,Chargeback
7
+ Jordan Lee,PA-3004,-27.00,Chargeback
@@ -0,0 +1,7 @@
1
+ Statement #,Carrier,Policy Number,Insured Name,Transaction Type,Effective Date,Premium,Commission Rate,Commission Amount
2
+ S-0715,Example Mutual,PA-3001,Delphine Arkwright,XLC,07/02/2026,-601.64,15,-90.25
3
+ S-0915,Example Mutual,HO-3002,Delphine Arkwright,XLC,09/01/2026,-1190.14,15,-178.52
4
+ S-0815,Example Mutual,PA-3004,Wilhelmina Craddock,CAN,08/01/2026,-450.00,12,-54.00
5
+ S-0815,Example Mutual,UMB-3005,Wilhelmina Craddock,Flat Cancel,08/15/2026,-400.00,15,-60.00
6
+ S-1015,Example Mutual,PA-3006,Thaddeus Pomfret,PCH,09/01/2026,120.00,15,18.00
7
+ S-1015,Sample Casualty,BOP-3003,Somerby Tile Works LLC,CAN,10/15/2026,-1493.15,17.5,-261.30
@@ -0,0 +1 @@
1
+ { "note": "Fictional client names from the fixtures. None may appear in a result the model sees.", "values": ["Delphine Arkwright", "Somerby Tile Works", "Wilhelmina Craddock", "Quillon Haverty", "Thaddeus Pomfret"] }
@@ -0,0 +1,8 @@
1
+ Applicant,Policy Number,Line of Business,Carrier,Policy Status,Effective Date,Expiration Date,Premium,Commission Rate,Cancel Date,Producer
2
+ Delphine Arkwright,PA-3001,Personal Auto,Example Mutual,Cancelled,01/01/2026,01/01/2027,1200.00,15,07/02/2026,Jordan Lee
3
+ Delphine Arkwright,HO-3002,Homeowners,Example Mutual,Cancelled,03/01/2026,03/01/2027,2400.00,15,09/01/2026,Jordan Lee
4
+ Somerby Tile Works LLC,BOP-3003,Business Owners,Sample Casualty,Cancelled,02/01/2026,02/01/2027,5000.00,17.5,10/15/2026,Sam Ortiz
5
+ Wilhelmina Craddock,PA-3004,Personal Auto,Example Mutual,Cancelled,05/01/2026,11/01/2026,900.00,12,08/01/2026,Jordan Lee
6
+ Wilhelmina Craddock,UMB-3005,Personal Umbrella,Example Mutual,Cancelled,08/15/2026,08/15/2027,400.00,15,08/15/2026,Jordan Lee
7
+ Thaddeus Pomfret,PA-3006,Personal Auto,Example Mutual,Active,04/01/2026,10/01/2026,1100.00,12,,Jordan Lee
8
+ Thaddeus Pomfret,HO-3007,Homeowners,Example Mutual,Cancelled,06/01/2025,06/01/2026,1800.00,15,03/01/2026,Jordan Lee
@@ -0,0 +1,6 @@
1
+ Producer,Policy Number,Amount,Type
2
+ Jordan Lee,PA-3001,45.00,Commission
3
+ Jordan Lee,PA-3001,-45.13,Chargeback
4
+ Jordan Lee,PA-3004,27.00,Commission
5
+ Jordan Lee,HO-3002,90.00,Commission
6
+ Jordan Lee,HO-3002,-90.00,Chargeback
@@ -0,0 +1,7 @@
1
+ Statement #,Carrier,Policy Number,Insured Name,Transaction Type,Effective Date,Premium,Commission Rate,Commission Amount
2
+ S-0715,Example Mutual,PA-3001,Delphine Arkwright,XLC,07/02/2026,-601.64,15,-90.25
3
+ S-0915,Example Mutual,HO-3002,Delphine Arkwright,XLC,09/01/2026,-2400.00,15,-360.00
4
+ S-0815,Example Mutual,PA-3004,Wilhelmina Craddock,CAN,08/01/2026,-450.00,12,-54.00
5
+ S-0815,Example Mutual,UMB-3005,Wilhelmina Craddock,Flat Cancel,08/15/2026,-400.00,15,-60.00
6
+ S-1015,Example Mutual,HO-3999,Quillon Haverty,CAN,10/01/2026,-500.00,15,-75.00
7
+ S-1015,Example Mutual,PA-3006,Thaddeus Pomfret,PCH,09/01/2026,120.00,15,18.00
package/package.json CHANGED
@@ -1,6 +1,59 @@
1
1
  {
2
2
  "name": "fairpolicy-commission-chargebacks",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "mcpName": "io.github.fpavia111-creator/fairpolicy-commission-chargebacks",
5
+ "description": "For every cancelled policy, works out the commission chargeback on the unearned premium, compares it with what the carrier took back, and flags overcharges, chargebacks still to come, chargebacks with no cancellation and producer shares not reversed. Never posts, pays or sends.",
6
+ "type": "module",
7
+ "license": "MIT",
8
+ "author": "Kurt Francisco",
9
+ "engines": {
10
+ "node": ">=20"
11
+ },
12
+ "main": "src/index.js",
13
+ "exports": {
14
+ ".": "./src/index.js",
15
+ "./package.json": "./package.json"
16
+ },
17
+ "bin": {
18
+ "fairpolicy-commission-chargebacks": "bin/fairpolicy-commission-chargebacks.js"
19
+ },
20
+ "files": [
21
+ "src",
22
+ "bin",
23
+ "fixtures",
24
+ "evals",
25
+ "README.md",
26
+ "CHANGELOG.md",
27
+ "LICENSE",
28
+ "server.json"
29
+ ],
30
+ "scripts": {
31
+ "test": "node scripts/run-tests.cjs",
32
+ "demo": "node bin/fairpolicy-commission-chargebacks.js demo",
33
+ "eval": "node bin/fairpolicy-commission-chargebacks.js eval"
34
+ },
35
+ "dependencies": {
36
+ "fairpolicy-core": "^0.1.0"
37
+ },
38
+ "repository": {
39
+ "type": "git",
40
+ "url": "git+https://github.com/fpavia111-creator/fairpolicy-agents.git",
41
+ "directory": "packages/fairpolicy-commission-chargebacks"
42
+ },
43
+ "homepage": "https://github.com/fpavia111-creator/fairpolicy-agents/tree/master/packages/fairpolicy-commission-chargebacks#readme",
44
+ "bugs": {
45
+ "url": "https://github.com/fpavia111-creator/fairpolicy-agents/issues"
46
+ },
47
+ "keywords": [
48
+ "mcp",
49
+ "mcp-server",
50
+ "insurance",
51
+ "insurance-agency",
52
+ "agency-management",
53
+ "fairpolicy",
54
+ "chargebacks",
55
+ "cancellations",
56
+ "unearned-premium",
57
+ "producer-commission"
58
+ ]
59
+ }
package/server.json ADDED
@@ -0,0 +1,37 @@
1
+ {
2
+ "$schema": "https://static.modelcontextprotocol.io/schemas/2025-12-11/server.schema.json",
3
+ "name": "io.github.fpavia111-creator/fairpolicy-commission-chargebacks",
4
+ "title": "Fairpolicy Commission Chargebacks",
5
+ "description": "For every cancelled policy, works out the commission chargeback on the unearned premium, compares…",
6
+ "version": "0.1.0",
7
+ "repository": {
8
+ "url": "https://github.com/fpavia111-creator/fairpolicy-agents",
9
+ "source": "github",
10
+ "subfolder": "packages/fairpolicy-commission-chargebacks"
11
+ },
12
+ "websiteUrl": "https://fair-agents.com/fairpolicy.html",
13
+ "packages": [
14
+ {
15
+ "registryType": "npm",
16
+ "identifier": "fairpolicy-commission-chargebacks",
17
+ "version": "0.1.0",
18
+ "transport": {
19
+ "type": "stdio"
20
+ },
21
+ "packageArguments": [
22
+ {
23
+ "type": "positional",
24
+ "value": "mcp"
25
+ },
26
+ {
27
+ "type": "named",
28
+ "name": "--root",
29
+ "description": "Folder holding the exports this agent may read and write (it cannot reach outside it)",
30
+ "isRequired": true,
31
+ "format": "filepath",
32
+ "valueHint": "exports_folder"
33
+ }
34
+ ]
35
+ }
36
+ ]
37
+ }
package/src/demo.js ADDED
@@ -0,0 +1,9 @@
1
+ import { fileURLToPath } from 'node:url';
2
+ import { familyDemo } from 'fairpolicy-core';
3
+
4
+ export const demo = (manifest) => familyDemo(manifest, fileURLToPath(new URL('../fixtures', import.meta.url)), {
5
+ title: 'Fairpolicy Commission Chargebacks', args: { policies: 'policies.csv', statements: 'statements.csv', producer_pay: 'producer-pay.csv', as_of: '2026-11-18' },
6
+ checkLabel: 'Check every cancellation\'s chargeback', columns: ['policy_number', 'cancel_date', 'expected', 'taken'],
7
+ explain: { policy_number: 'HO-3002' }, explainLabel: 'Explain the homeowners chargeback',
8
+ draft: { args: { carrier: 'Example Mutual', from: 'Casey Park, Example Agency (fictional)' }, label: 'Draft a recalculation request to the carrier' },
9
+ });
package/src/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export { manifest } from './manifest.js';
2
+ export * from './logic.js';
package/src/logic.js ADDED
@@ -0,0 +1,70 @@
1
+ // Commission chargebacks on cancellations: what the carrier should take back, what it took, and the producer side.
2
+ import { str, num, round2, sum, policyIndex, normPolicy, normStatementLine, unearnedPremium, commissionAt, countBy, addDays } from 'fairpolicy-core';
3
+
4
+ export const STATUSES = ['overcharged', 'chargeback_without_cancel', 'producer_not_reversed', 'pending_chargeback', 'charged_ok'];
5
+ const NEXT = {
6
+ overcharged: 'Ask the carrier to recalculate: the chargeback is more than the commission on the unearned premium.',
7
+ chargeback_without_cancel: 'Find the cancellation in your agency system or dispute the chargeback with the carrier.',
8
+ producer_not_reversed: 'The carrier took the commission back but the producer\'s share was not reversed; review with the producer before the next payroll.',
9
+ pending_chargeback: 'Expect this chargeback on a coming statement; hold it as a reserve.',
10
+ charged_ok: '',
11
+ };
12
+
13
+ export function checkChargebacks(policyRows, statementRows, producerRows = null, { asOf, lookbackDays = 180, tolerance = 1, method = 'pro_rata' } = {}) {
14
+ const since = addDays(asOf, -lookbackDays);
15
+ const lines = statementRows.map(normStatementLine).filter((l) => l.policy_key);
16
+ const sIdx = policyIndex(lines);
17
+ const prod = producerRows ? policyIndex(producerRows.map((r) => ({ ...r, amount: num(r.amount) }))) : null;
18
+ const pols = policyRows.map((r) => normPolicy(r));
19
+ const pIdx = policyIndex(pols);
20
+ const rows = [];
21
+ for (const p of pols) {
22
+ const cd = str(p.cancel_date);
23
+ if (!p.policy_key || p.status_code !== 'cancelled' || !cd || cd < since || cd > asOf) continue;
24
+ const flat = cd <= str(p.effective_date);
25
+ const unearned = flat ? p.premium : unearnedPremium(p.premium, p.effective_date, p.expiration_date, cd, { method });
26
+ const expected = unearned !== null && p.commission_rate !== null ? commissionAt(unearned, p.commission_rate) : null;
27
+ const taken = round2(-sum(sIdx.find(p.policy_number).filter((l) => l.tx === 'cancellation' || (l.commission ?? 0) < 0), (l) => l.commission));
28
+ let status;
29
+ if (taken <= 0) status = 'pending_chargeback';
30
+ else if (expected !== null && taken > expected + tolerance) status = 'overcharged';
31
+ else status = 'charged_ok';
32
+ let producer_reversed = null;
33
+ if (prod && taken > 0) {
34
+ const pl = prod.find(p.policy_number);
35
+ producer_reversed = pl.some((x) => (x.amount ?? 0) < 0) || !pl.some((x) => (x.amount ?? 0) > 0);
36
+ if (!producer_reversed && status === 'charged_ok') status = 'producer_not_reversed';
37
+ }
38
+ const detail = status === 'overcharged' ? `Charged back ${taken}; ${p.commission_rate}% of the ${flat ? 'full' : 'unearned'} premium ${unearned} is ${expected}, so ${round2(taken - expected)} too much.`
39
+ : status === 'pending_chargeback' ? `Cancelled ${cd}; no chargeback on the statements yet.${expected !== null ? ` Expect about ${expected}.` : ''}`
40
+ : status === 'producer_not_reversed' ? `Carrier took back ${taken}; the producer pay file still shows the commission with no reversal.` : `Charged back ${taken}${expected !== null ? ` (expected ${expected})` : ''}.`;
41
+ rows.push({ status, policy_number: p.policy_number, client: p.client, carrier: p.carrier, line: p.line_code, effective_date: str(p.effective_date), expiration_date: str(p.expiration_date), cancel_date: cd, flat_cancel: flat, premium: p.premium, unearned, commission_rate: p.commission_rate, expected, taken, difference: expected === null ? null : round2(taken - expected), producer: p.producer, producer_reversed, detail, next_step: NEXT[status] });
42
+ }
43
+ // Chargebacks on the statements for policies that are not cancelled in the agency system.
44
+ const seen = new Set();
45
+ for (const l of lines) {
46
+ if ((l.commission ?? 0) >= 0 || seen.has(l.policy_key)) continue;
47
+ const p = pIdx.find(l.policy_number)[0];
48
+ if (p && p.status_code === 'cancelled') continue;
49
+ seen.add(l.policy_key);
50
+ const taken = round2(-sum(sIdx.find(l.policy_number).filter((x) => (x.commission ?? 0) < 0), (x) => x.commission));
51
+ rows.push({ status: 'chargeback_without_cancel', policy_number: l.policy_number, client: l.client || (p && p.client) || '', carrier: l.carrier || (p && p.carrier) || '', line: p ? p.line_code : '', effective_date: str(l.effective_date), expiration_date: '', cancel_date: '', flat_cancel: false, premium: l.premium, unearned: null, commission_rate: l.commission_rate, expected: null, taken, difference: null, producer: p ? p.producer : '', producer_reversed: null, detail: `${taken} charged back, but ${p ? `the policy is ${p.status_code} in your agency system` : 'the policy is not in your policy list'}.`, next_step: NEXT.chargeback_without_cancel });
52
+ }
53
+ rows.sort((a, b) => STATUSES.indexOf(a.status) - STATUSES.indexOf(b.status) || a.policy_number.localeCompare(b.policy_number));
54
+ const cancels = rows.filter((r) => r.status !== 'chargeback_without_cancel');
55
+ return {
56
+ as_of: asOf, window_from: since, method, cancellations: cancels.length,
57
+ expected_chargebacks: sum(cancels.filter((r) => r.expected !== null), (r) => r.expected), taken: sum(rows, (r) => r.taken),
58
+ reserve_for_pending: sum(rows.filter((r) => r.status === 'pending_chargeback' && r.expected !== null), (r) => r.expected),
59
+ overcharged_by: sum(rows.filter((r) => r.status === 'overcharged'), (r) => r.difference),
60
+ producer_checked: !!producerRows, by_status: countBy(rows, STATUSES), rows,
61
+ };
62
+ }
63
+
64
+ export const summaryLine = (o) => `${o.cancellations} cancellation(s) since ${o.window_from}: ${o.expected_chargebacks} in chargebacks expected (${o.method.replace('_', ' ')}), ${o.taken} taken; ${o.by_status.overcharged || 0} overcharged by ${o.overcharged_by}, ${o.by_status.pending_chargeback || 0} still to come (reserve ${o.reserve_for_pending}), ${o.by_status.chargeback_without_cancel || 0} chargeback(s) with no cancellation${o.producer_checked ? `, ${o.by_status.producer_not_reversed || 0} not reversed on producer pay` : ''}.`;
65
+
66
+ export function disputeText(items, a) {
67
+ const total = sum(items, (r) => r.difference);
68
+ const rows = items.map((r) => `| ${r.policy_number} | ${r.client} | ${r.cancel_date} | ${r.unearned} | ${r.commission_rate}% | ${r.expected} | ${r.taken} | ${r.difference} |`);
69
+ return { total, text: `**To:** ${a.contact || '[TO CONFIRM carrier commission contact]'}\n**From:** ${a.from || '[TO CONFIRM your name and agency]'}\n**Re:** Chargeback recalculation, ${items[0].carrier}\n\nHello,\n\nThe chargebacks below are larger than the commission on the unearned premium by our calculation (pro rata from the cancellation date). Could you check them against your cancellation figures?\n\n| Policy | Insured | Cancelled | Unearned premium | Rate | Our chargeback | Charged | Difference |\n|---|---|---|---|---|---|---|---|\n${rows.join('\n')}\n\nTotal difference: ${total}.\n\n[CHECK whether the carrier used short-rate cancellation; our figures are pro rata.]\n\nThank you,\n${a.from || '[TO CONFIRM your name]'}\n` };
70
+ }
@@ -0,0 +1,59 @@
1
+ import { fairpolicyAgent, POLICY_FIELDS, POLICY_PROFILES, COMMISSION_FIELDS, COMMISSION_PROFILES, AS_OF_ARG, asOf, sameCarrier } from 'fairpolicy-core';
2
+ import { readFileSync } from 'node:fs';
3
+ import { checkChargebacks, summaryLine, disputeText, STATUSES } from './logic.js';
4
+
5
+ const pkg = JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf8'));
6
+ const files = {
7
+ policies: { type: 'string', description: 'Policy list from your agency system with status, effective, expiration and cancellation dates, premium and commission rate' },
8
+ statements: { type: 'string', description: 'Carrier commission statement lines covering the months since the cancellations' },
9
+ };
10
+ const args = {
11
+ producer_pay: { type: 'string', description: 'Optional producer commission file (policy number, producer, amount; reversals negative) to check the producer\'s share was reversed' },
12
+ as_of: AS_OF_ARG,
13
+ lookback_days: { type: 'integer', minimum: 30, maximum: 730, description: 'Cancellations this many days back are checked (default 180)' },
14
+ method: { type: 'string', enum: ['pro_rata', 'short_rate'], description: 'How the unearned premium is worked out (default pro_rata)' },
15
+ };
16
+ const PRODUCER_PAY_FIELDS = {
17
+ policy_number: { aliases: ['Policy Number', 'Policy #', 'Policy'], required: true },
18
+ producer: { aliases: ['Producer', 'Producer Name', 'Agent'] },
19
+ amount: { type: 'money', aliases: ['Amount', 'Commission', 'Producer Commission', 'Paid', 'Split Amount'] },
20
+ type: { aliases: ['Type', 'Transaction', 'Entry Type'], translate: false },
21
+ };
22
+ const run = (a, ctx) => checkChargebacks(ctx.records(a.policies, 'policies'), ctx.records(a.statements, 'statements'), a.producer_pay ? ctx.records(a.producer_pay, 'producer_pay') : null, { asOf: asOf(a), lookbackDays: a.lookback_days ?? 180, method: a.method || 'pro_rata' });
23
+
24
+ export const manifest = fairpolicyAgent({
25
+ name: 'fairpolicy-commission-chargebacks', version: pkg.version, prefix: 'chargebacks', key: 'policy_number', keyKind: 'policy', noun: 'policy',
26
+ role: 'an agency accounting lead',
27
+ description: 'For every cancelled policy, works out the commission chargeback on the unearned premium (pro rata or short rate), compares it with what the carrier took back, and flags overcharges, chargebacks still to come (a reserve), chargebacks with no cancellation in your system, and producer shares not reversed. Drafts a recalculation request; never posts, pays or sends.',
28
+ files, args, required: ['policies', 'statements'], statuses: STATUSES,
29
+ sources: {
30
+ policies: { label: 'Policy list', args: ['policies'], fields: POLICY_FIELDS, profiles: POLICY_PROFILES },
31
+ statements: { label: 'Commission statement lines', args: ['statements'], fields: COMMISSION_FIELDS, profiles: COMMISSION_PROFILES },
32
+ producer_pay: { label: 'Producer commission file', args: ['producer_pay'], fields: PRODUCER_PAY_FIELDS },
33
+ },
34
+ run, summary: summaryLine,
35
+ check: { title: 'Check cancellation chargebacks', description: 'Each cancelled policy: unearned premium, expected chargeback, what was taken and a status (overcharged, chargeback_without_cancel, producer_not_reversed, pending_chargeback, charged_ok). Totals: expected, taken, reserve for chargebacks still to come.' },
36
+ report: {
37
+ base: 'commission-chargebacks', titleShort: 'Write the chargeback worklist',
38
+ title: { en: 'Cancellation chargebacks', de: 'Courtage-Rückbelastungen bei Storno', fr: 'Reprises de commission sur résiliation', es: 'Contracargos de comisión por anulación', pt: 'Estornos de comissão por cancelamento', it: 'Storni provvigionali per annullamento', ja: '解約に伴う手数料戻し', zh: '退保佣金回扣', ko: '해지 수수료 환수' },
39
+ columns: ['status', 'policy_number', 'client', 'carrier', 'line', 'effective_date', 'expiration_date', 'cancel_date', 'flat_cancel', 'premium', 'unearned', 'commission_rate', 'expected', 'taken', 'difference', 'producer', 'producer_reversed', 'detail', 'next_step'],
40
+ htmlColumns: ['status', 'policy_number', 'carrier', 'cancel_date', 'unearned', 'expected', 'taken', 'difference', 'producer', 'detail'],
41
+ limits: ['Unearned premium is worked out pro rata (or short rate, 10% penalty) from the dates in your policy list. Carriers\' own cancellation tables, minimum earned premiums and fees differ.'],
42
+ },
43
+ draft: {
44
+ noun: 'recalculation', verb: 'send', category: 'binding_send', actor: 'a person at your agency', title: 'Draft a chargeback recalculation request',
45
+ description: 'Write drafts/recalculation-<carrier>.md listing one carrier\'s overcharged chargebacks with the unearned premium and the difference. Marked DRAFT. Sending goes through chargebacks_request_send (one click).',
46
+ required: ['carrier'],
47
+ args: { carrier: { type: 'string', description: 'Carrier as named in the policy list' }, from: { type: 'string', description: 'Your name and agency' }, contact: { type: 'string', description: 'Carrier commission contact' } },
48
+ build: (a, ctx, o) => {
49
+ const items = o.rows.filter((r) => r.status === 'overcharged' && sameCarrier(r.carrier, a.carrier));
50
+ if (!items.length) throw new Error(`no overcharged chargebacks for "${a.carrier}"`);
51
+ const d = disputeText(items, a);
52
+ return { id: items[0].carrier, to: a.contact, text: d.text, amount: d.total, extra: { policies: items.length } };
53
+ },
54
+ },
55
+ refused: [
56
+ { name: 'producer_pay_adjust', category: 'move_money', description: 'It never changes producer pay; your accounting staff reverse a share after review.' },
57
+ { name: 'email_send', category: 'binding_send', description: 'It never sends e-mail; a person sends the approved request.' },
58
+ ],
59
+ });