/*
 * Copyright 2026, Jaime Terrats.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { workflow } from '../workflow-generator.js';
export function generateManufacturingWorkflows(version) {
    return {
        'sales-agreement-setup.md': workflow(version, 'Sales Agreement Setup', `
## 1. Gather Requirements

Ask:
- **Agreement scope:** Which accounts require Sales Agreements? Named accounts only or all?
- **Product model:** Are agreements at the product family level or individual SKU level?
- **Forecast integration:** Should agreement actuals feed into Account-Based Forecasting?
- **Metrics:** Which KPIs drive agreement performance? (Revenue, Volume, Units)
- **Schedule granularity:** Monthly, quarterly, or annual planned quantities?
- **Approval workflow:** Does agreement creation or modification require approval?

Check \`force-app/main/default/objects/SalesAgreement/\` for existing field customizations before adding new fields.

## 2. Configure Sales Agreements

### Object Setup
- \`SalesAgreement\` is the header object — one per customer agreement.
- Key fields: \`Account\`, \`StartDate\`, \`EndDate\`, \`Status\`, \`Description\`.
- Use Record Types to distinguish agreement categories (e.g., Volume Commitment, Revenue Target).

### Agreement Products
- \`SalesAgreementProduct\` links products to the agreement with planned quantities and schedules.
- Key fields: \`Product2Id\`, \`PlannedQuantity\`, \`ActualQuantity\`, \`StartDate\`, \`EndDate\`.
- Define schedules at the line-item level — each product line can have independent date ranges.

### Actuals Calculation
- Configure the Actuals Calculation method in Manufacturing Cloud Setup:
  - **Order-based:** actuals derived from Order Products linked to the agreement.
  - **Custom:** use a custom Apex class implementing the \`ActualsCalculation\` interface.
- Map \`OrderItem\` fields to agreement product lines for automatic actual aggregation.

## 3. Forecast Integration

### Link Agreements to Account Forecasts
1. Enable Account-Based Forecasting in Setup → Manufacturing Cloud → Forecasting.
2. Create an Account Forecast Set that includes agreement accounts.
3. Map \`SalesAgreementProduct\` planned quantities as the baseline forecast input.
4. Configure adjustment rules to allow sales reps to override forecast values within a tolerance band.

### Forecast Sharing
- Set sharing rules on \`AccountForecast\` to grant partner or distributor visibility.
- Use \`AccountForecastAdjustment\` records for collaborative forecast editing with audit trail.

## 4. Deploy Sales Agreement Metadata

\`\`\`bash
sf project deploy start \\
  --source-dir force-app/main/default/objects/SalesAgreement \\
  --source-dir force-app/main/default/objects/SalesAgreementProduct \\
  --target-org <alias>
\`\`\`

Verify in the target org:
- Sales Agreement tab is visible and Record Types are assigned to profiles
- Agreement Product related list renders on the agreement detail page
- Actuals calculation populates after Order fulfillment
`),
        'rebate-program-setup.md': workflow(version, 'Rebate Program Setup', `
## 1. Gather Requirements

Ask:
- **Program structure:** Volume-based tiers, flat percentage, or growth incentive?
- **Rebate types:** Which rebate categories apply? (Volume, Growth, Mix, Loyalty)
- **Calculation basis:** Revenue, units shipped, or order count?
- **Payout frequency:** Monthly, quarterly, annually, or on-demand?
- **Accrual method:** Should rebate accruals be tracked for financial reporting?
- **Beneficiary:** Is the rebate paid to the distributor, end customer, or both?
- **ERP integration:** Does payout data need to sync to an external finance system?

## 2. Configure Manufacturing Programs

### MfgProgram Setup
- \`MfgProgram\` is the container for rebate definitions.
- Key fields: \`Name\`, \`Status\`, \`StartDate\`, \`EndDate\`, \`Account\` (beneficiary).
- Use \`Status\` picklist to manage lifecycle: Draft → Active → Closed → Settled.

### Rebate Types
- \`MfgProgramRebateType\` defines the calculation rules for each rebate category.
- Key fields: \`RebateType\`, \`CalculationMethod\`, \`Percentage\`, \`TierStructure\`.
- Configure tiers as child records when volume-based breakpoints apply.

### Payout Schedules
- Define payout periods aligned with the program date range.
- Each payout period calculates:
  - **Qualified amount:** actual performance vs. target threshold.
  - **Rebate earned:** qualified amount × rebate percentage (or tier rate).
  - **Payout status:** Pending Approval → Approved → Paid.

## 3. Configure Accruals

### Accrual Tracking
- Enable rebate accruals in Manufacturing Cloud Setup → Rebate Management.
- Accruals post estimated rebate liability at each period close.
- On payout, accruals reverse and actual expense is recognized.

### Integration with Finance
- Use Platform Events (\`RebatePayoutEvent__e\`) to notify ERP of approved payouts.
- Alternatively, configure MuleSoft integration to push payout records to the GL system.
- Include: Program ID, Account, Amount, Period, Currency, Payout Date.

## 4. Deploy Rebate Program Metadata

\`\`\`bash
sf project deploy start \\
  --source-dir force-app/main/default/objects/MfgProgram \\
  --source-dir force-app/main/default/objects/MfgProgramRebateType \\
  --source-dir force-app/main/default/customMetadata \\
  --target-org <alias>
\`\`\`

Verify in the target org:
- MfgProgram records can be created with correct Record Types
- Rebate Type child records link to the program
- Payout calculation triggers correctly on period close
- Accrual records generate for active programs
`),
    };
}
//# sourceMappingURL=manufacturing.js.map