# `Sp_Receivables_AR` (procedure)


- **Type:** PROCEDURE
- **Deterministic:** NO
- **SQL data access:** CONTAINS SQL

## Parameters

| # | Mode | Name | Type |
|---|---|---|---|
| 1 | IN | `iType` | `int` |
| 2 | IN | `sStatistics` | `varchar(100)` |
| 3 | IN | `tStartDate` | `varchar(100)` |
| 4 | IN | `tEndDate` | `varchar(100)` |
| 5 | IN | `sZero` | `varchar(100)` |
| 6 | IN | `sLoginId` | `varchar(100)` |
| 7 | INOUT | `dNoReceiptMoney` | `decimal(18,6)` |
| 8 | INOUT | `dDueReceiptMoney` | `decimal(18,6)` |
| 9 | IN | `sBrId` | `varchar(100)` |
| 10 | IN | `sSuId` | `varchar(100)` |
| 11 | IN | `bFilter` | `varchar(5000)` |
| 12 | IN | `pageNum` | `int` |
| 13 | IN | `pageSize` | `int` |
| 14 | OUT | `totalCount` | `int` |
| 15 | IN | `countCloumn` | `varchar(5000)` |
| 16 | OUT | `countMapJson` | `longtext` |
| 17 | IN | `sFilterOrderBy` | `varchar(5000)` |
| 18 | IN | `sGroupby_select_sql` | `varchar(5000)` |
| 19 | IN | `sGroupby_group_sql` | `varchar(5000)` |

## Body

_Body is not pre-cached. To inspect: `mysql --defaults-file=~/.my.cnf -e 'SHOW CREATE PROCEDURE `Sp_Receivables_AR`'`._

## Narrative

**Business context:** 财务管理 → 应收账款 → 应收账款 — paged accounts-receivable report. Computes per-customer outstanding balance by combining 期初 (opening balance from `CahCustomerInit`), 销售本期 (period sales from `viw_salsalescheckingmaster`/`SalSalesCheckingSlave`/`saldelivergoodsslave`), 收款 (receipts from `CahReceiptMaster`), 应开票/已开票 invoice rows from `SalSalesInvoiceMaster`, plus payment offsets/adjustments from `CahPaymentMaster` and `CahFinancialAdjust`.

**What it does:** Resolves the report mode from `iType`/`sStatistics` (multiple variants — by date, by 销售员/客户分类/结算方式 filter, with/without zero-balance customers). Builds `TmpAll` by `UNION` of period sales (slave-level sums of `dMoney`/`dTaxMoney`), receipts, invoices, payments, adjustments, opening balances and quality-abnormal/reject deductions; then aggregates per `sCustomerName` from `EleCustomer`. Sets `dNoReceiptMoney`/`dDueReceiptMoney` as INOUT totals and hands the final SQL to `Sp_Outstanding_Query` for paging, `bFilter` splicing, `countMapJson` building.

**Invocation:** Bound as data-source of form 应收账款 (`gfm.sId=19211681015115285275044580`, module 应收账款 `19211681015115285273329980` under 财务管理 → 应收账款). Also called internally by `Sp_System_ReplaceField` / `Sp_System_ReplaceFieldNew` / `Sp_System_ReplaceField_NEW_ACT` (dynamic-field expansion), `Sp_Receivables_ARAccount`, `Sp_Sales_PerformanceCommission`, and `Fun_getInitColumnByProName`. Install: `script/标版/30100101/Sp_Receivables_AR.sql` (with the 20220119-2858 upgrade adding `订单金额` column).
