# `Sp_Sales_SalesAnalysiseOfProfit_Product` (procedure)

> @author:杨恒林
							@date:20210616 封存
							@describe: 销售利润分析-按产品，从对帐，送货，入库 三个层面分析 三个层面没有关联，只与时间段有关


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

## Parameters

| # | Mode | Name | Type |
|---|---|---|---|
| 1 | IN | `tStartDate` | `datetime` |
| 2 | IN | `tEndDate` | `datetime` |
| 3 | IN | `sBrId` | `varchar(100)` |
| 4 | IN | `sSuId` | `varchar(100)` |
| 5 | IN | `sLoginId` | `varchar(100)` |
| 6 | IN | `bFilter` | `varchar(5000)` |
| 7 | IN | `pageNum` | `int` |
| 8 | IN | `pageSize` | `int` |
| 9 | OUT | `totalCount` | `int` |
| 10 | IN | `countCloumn` | `varchar(5000)` |
| 11 | OUT | `countMapJson` | `longtext` |
| 12 | IN | `sFilterOrderBy` | `varchar(5000)` |
| 13 | IN | `sGroupby_select_sql` | `varchar(5000)` |
| 14 | IN | `sGroupby_group_sql` | `varchar(5000)` |

## Body

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

## Narrative

**Business context:** 成本管理 → 利润分析 → 销售利润分析-按产品 — paged customer×product profit report. Twin of `Sp_Sales_SalesAnalysiseOfProfit` but with an extra `sProductId` axis: one row per (`sCustomerId`,`sProductId`) pair. Per the embedded `@author:杨恒林 @date:20210616 封存` comment, it cross-checks profit from three independent angles (入库 / 送货 / 对账); the three layers are not joined, only sharing the same period.

**What it does:** Seeds `SAS(sCustomerId,sProductId)` by `UNION`ing distinct (customer,product) pairs from `viw_pitproductinstore`, `viw_saldelivergoods` and `viw_salsaleschecking` for the period. Three parallel `UPDATE ... INNER JOIN`s pull `(SUM(dProductNoTaxMoney), SUM(dCostMoney), profit = sales − cost, rate)` grouped by `(sProductId,sCustomerId)` from each source view into the `dInStore*` / `dDelivery*` / `dReconciliation*` column groups. Joins back `eleCustomer.sCustomerName` and `eleproduct.sProductName`, drops rows where all six money columns are zero, then pages via `Sp_Outstanding_Query`. Same `dInStoreProfitRate`/`dDeliveryProfitRate` target-swap bug as the customer-only variant.

**Invocation:** Bound as the data-source of the 销售利润分析-按产品 form `gfm.sId=101251240115016230550817050` via `gdsconfigformmaster.sSqlStr`; module path 成本管理 → 利润分析 → 销售利润分析-按产品. The cost-side install lives at `script/标版/30100101/cost/Sp_Sales_SalesAnalysiseOfProfit_Product.sql`; 优化版 at `script/标版/optimize/`. Marked `封存` (archived) by the author but still wired to the form.
