Calculation for adjustment
Published: 2022-10-31 12:11:13

https://help.hcltechsw.com/commerce/9.1.0/calculationframework/refs/rcfdiscount_example.html
```
SELECT * FROM CALMETHOD WHERE CALUSAGE_ID = -1 AND NAME IN ('CalculationCodeCalculate (discount)', 'CalculationCodeApply (discount)', 'CalculationCodeQualify (discount)')
```
`https://help.hcltechsw.com/commerce/8.0.0/calculationframework/concepts/ccfcalculationcodes.html
https://help.hcltechsw.com/commerce/8.0.0/api/com/ibm/commerce/couponredemption/commands/CalculateDiscountAmtCmdImpl.html
https://help.hcltechsw.com/commerce/8.0.0/api/com/ibm/commerce/order/calculation/CalculationCodeApplyCmd.html
https://help.hcltechsw.com/commerce/8.0.0/api/com/ibm/commerce/order/calculation/CalculationCodeQualifyCmd.html
`

Define Calculation Code

```
INSERT INTO CALCODE (CALCODE_ID,CODE,CALUSAGE_ID,STOREENT_ID,GROUPBY,PUBLISHED,SEQUENCE,
CALMETHOD_ID,CALMETHOD_ID_APP,CALMETHOD_ID_QFY,DESCRIPTION,DISPLAYLEVEL,FLAGS,PRECEDENCE) 
values (<Calcode_ID>,'<Calcode_Code>',<CalUsage_ID>,10151,0,1,0,<CalMethod_ID>,<CalMethod_ID_APP>,<CalMethod_ID_QFY>,'Extra Save - Save $2',0,0,0);
 
INSERT INTO CALCODEDSC (CALCODE_ID,LANGUAGE_ID,DESCRIPTION,LONGDESCRIPTION) 
VALUES (<Calcode_ID>,-1,'Extra Save - Save $2','Extra Save - Save $2');
```
Insert catentries and code record in `CATENCALCD` to apply promotion

Find the values for the CALMETHOD_ID (<RuleCalMethod_ID>), CALMETHOD_ID_QFY (<RuleCalMethod_ID_QFY>) for CALRULE table using the following SQL
```
SELECT * FROM CALMETHOD WHERE CALUSAGE_ID = -1 AND NAME IN ('CalculationRuleCalculate (discount)', 'CalculationRuleQualify (discount)')
```


Create the calculation rule for the calculation code
```
INSERT INTO CALRULE (CALRULE_ID,CALCODE_ID,SEQUENCE,COMBINATION,CALMETHOD_ID,CALMETHOD_ID_QFY,FLAGS,IDENTIFIER)
 VALUES (<Max value of CALRULE_ID+1>,<CalCode_ID>,0,2,<RuleCalMethod_ID>,<RuleCalMethod_ID_QFY>,0,1);
```

Find the values for the CALMETHOD_ID (<ScaleCalMethod_ID>) for CALSCALE table using the following SQL
```
SELECT * FROM CALMETHOD WHERE CALUSAGE_ID = -1 AND NAME IN ('CalculationScaleLookup (unit price based discount)')

```
Create the calculation scale properties.
```
INSERT INTO CALSCALE (CALSCALE_ID,QTYUNIT_ID,CODE,STOREENT_ID,CALUSAGE_ID,CALMETHOD_ID) 
VALUES (<Max value of CALSCALE_ID+1>,'C62','scale-ExtraDiscount',-1,<CalUsage_ID>,<ScaleCalMethod_ID>);
```


Associate the calculation scale properties to the calculation rules.
```
INSERT INTO CRULESCALE (CALSCALE_ID,CALRULE_ID) VALUES (<CALSCALE_ID created above>,<CALRULE_ID created above>);

```

Find the values for the CALMETHOD_ID (<RangeCalMethod_ID>) for CALRANGE table using the following SQL
```
SELECT * FROM CALMETHOD WHERE CALUSAGE_ID = -1 AND NAME IN ('CalculationRange (per unit discount)')
```

Create the ranges for the calculations.
```
INSERT INTO CALRANGE (CALRANGE_ID,CALSCALE_ID,CALMETHOD_ID,RANGESTART) VALUES (<Max value of CALRANGE_ID+1>,<CALSCALE_ID created above>,<RangeCalMethod_ID>,0);

```

Create the Discount value and associate it with the calculations. (You can change the value of the surcharge with this SQL statement, but this should be overidden by the java code described below).
```
INSERT INTO CALRLOOKUP (CALRLOOKUP_ID,CALRANGE_ID,VALUE) VALUES (<Max value of CALRLOOKUP_ID+1>,<CALRANGE_ID created above>,2);

```

Override Class for Discount Calculation
This is an update of the CALMETHOD table to override the java class used for Applying discounts to the new java command described above.
```
UPDATE CALMETHOD SET TASKNAME = 'com.<comp-name>.commerce.order.ExtraDiscountApplyTaskCmd' WHERE CALUSAGE_ID = -1 and subclass = 4;

```