У меня есть страница калитки, на которой будет опубликована транзакция. Принимает чистую стоимость и вычитает комиссию и налог. Класс контроллера используется для обновления других значений при их изменении. У меня это работает, но я чувствую, что дизайн не лучший по отношению к контроллеру, поскольку он имеет разные методы обновления, основанные на том, является ли транзакция брутто или нетто.
Моей первой мыслью было иметь nettController и grossController, которые наследуются от контроллера и используют фабрику для выбора правильного. У меня были проблемы с настройкой работы с калиткой из-за использования @ SpringBean.
public class PostUnderwriterTransactionPanel extends AbstractPolicyCurrentAdhocTransactionPanel<PostUnderwriterTransactionContext>{
@SpringBean
public PostUnderwriterTransactionPanelController controller;
@Override
protected void onInitialize() {
super.onInitialize();
transactionDetails.add(grossAmountQuestion = createNettAmountTextField(GROSS_AMOUNT_ID, GROSS_AMOUNT_KEY));
transactionDetails.add(nettAmountQuestion = createNettAmountTextField(NETT_AMOUNT_ID, NETT_AMOUNT_KEY));
transactionDetails.add(nettAmountIncTax = createCurrencyTextField(NETT_INC_TAX_ID, NETT_INC_TAX_KEY));
transactionDetails.add(commissionAmount = createCurrencyTextField(COMMISSION_AMOUNT_ID, COMMISSION_AMOUNT_KEY));
transactionDetails.add(commissionRate = createPercentageTextField(COMMISSION_RATE_ID, COMMISSION_RATE_KEY));
transactionDetails.add(taxAmount = createCurrencyTextField(TAX_AMOUNT_ID, TAX_AMOUNT_KEY));
transactionDetails.add(taxRate = createPercentageTextField(TAX_RATE_ID, TAX_RATE_KEY));
transactionDetails.add(totalAmount = createInfoPanel(TOTAL_AMOUNT_ID, TOTAL_AMOUNT_KEY));
checkOverride(commissionAmount, commissionRate, OVERRIDE_INSURER_COMMISSION.getCode());
checkOverride(taxAmount, taxRate, OVERRIDE_TAX.getCode());
add(content);
addEffectiveDateOnUpdate();
getModelObject().getTransactionPosting().setAmount(0L);
controller.setParameterValues(getModelObject());
if(getModelObject().isNetRated()){
grossAmountQuestion.setVisible(false);
}else{
nettAmountQuestion.setVisible(false);
nettAmountIncTax.setVisible(false);
}
}
public void checkOverride(TextField<Float> amount, TextField<Float> rate, String code){
if(!controller.getOverrideAllowed(code, getPscm())){
amount.setEnabled(false);
rate.setEnabled(false);
}
}
private void redrawValues(AjaxRequestTarget target){
if(getModelObject().isNetRated()) {
target.add(nettAmountQuestion, nettAmountIncTax, commissionAmount, commissionRate, taxAmount, taxRate, totalAmount, newPolicyBalance);
}
else{
target.add(grossAmountQuestion, commissionAmount, commissionRate, taxAmount, taxRate, totalAmount, newPolicyBalance);
}
}
private void callControllerUpdate(String type){
if(getModelObject().isNetRated()) {
switch (type) {
case NETT_AMOUNT_ID:
controller.updateNettAmount(getModelObject());
break;
case NETT_INC_TAX_ID:
controller.updateNettIncTax(getModelObject());
break;
case COMMISSION_AMOUNT_ID:
controller.updateCommissionAmount(getModelObject());
break;
case COMMISSION_RATE_ID:
controller.updateCommissionRate(getModelObject());
break;
case TAX_AMOUNT_ID:
controller.updateTaxAmount(getModelObject());
break;
case TAX_RATE_ID:
controller.updateTaxRateFromTextBox(getModelObject());
break;
}
}else{
switch (type) {
case GROSS_AMOUNT_ID:
controller.updateGrossAmount(getModelObject());
break;
case COMMISSION_AMOUNT_ID:
controller.updateGrossCommissionAmount(getModelObject());
break;
case COMMISSION_RATE_ID:
controller.updateGrossCommissionRate(getModelObject());
break;
case TAX_AMOUNT_ID:
controller.updateGrossTaxAmount(getModelObject());
break;
case TAX_RATE_ID:
controller.updateGrossTaxRate(getModelObject());
break;
}
}
}
}
@Component
public class PostUnderwriterTransactionPanelController extends AdhocTransactionViewControllerImpl {
@Autowired
private NettRatedCalculator nettRatedCalculator;
public void updateNettAmount(PostUnderwriterTransactionContext context){
context.setNettAmountIncTax(nettRatedCalculator.calculateNettIncTax(context.getNettAmount(), context.getParameterTaxRate()));
context.setCommissionAmount(nettRatedCalculator.calculateCommissionAmount(context.getCommissionFlatCharge(), context.getNettAmount(), context.getParameterCommissionRate()));
context.setCommissionRate(nettRatedCalculator.calculateCommissionRate(context.getCommissionAmount(), context.getNettAmount()));
context.setTaxRate(context.getParameterTaxRate());
context.setTaxAmount(nettRatedCalculator.calculateTaxAmount(context.getParameterTaxRate(), context.getNettAmount(), context.getCommissionAmount()));
context.setTotalAmount(nettRatedCalculator.calculateTotalAmount(context.getNettAmount(), context.getCommissionAmount(), context.getTaxAmount()));
}
public void updateCommissionAmount(PostUnderwriterTransactionContext context){
context.setCommissionRate(nettRatedCalculator.calculateCommissionRate(context.getCommissionAmount(), context.getNettAmount()));
updateCommission(context);
}
public void updateCommissionRate(PostUnderwriterTransactionContext context){
float equivalentCommission = nettRatedCalculator.calculateEquivalentRate(context.getCommissionRate());
context.setCommissionAmount(nettRatedCalculator.calculateCommissionAmount(0L, context.getNettAmount(), equivalentCommission));
updateCommission(context);
}
private void updateCommission(PostUnderwriterTransactionContext context){
context.setNettAmountIncTax(nettRatedCalculator.calculateNettIncTax(context.getNettAmount(), context.getTaxRate()));
context.setTaxAmount(nettRatedCalculator.calculateTaxAmount(context.getTaxRate(), context.getNettAmount(), context.getCommissionAmount()));
context.setTotalAmount(nettRatedCalculator.calculateTotalAmount(context.getNettAmount(), context.getCommissionAmount(), context.getTaxAmount()));
}
public void updateTaxAmount(PostUnderwriterTransactionContext context){
context.setTaxRate(nettRatedCalculator.calculateTaxRate(context.getTaxAmount(), context.getNettAmount(), context.getCommissionAmount()));
context.setNettAmountIncTax(nettRatedCalculator.calculateNettIncTax(context.getNettAmount(), context.getTaxRate()));
context.setTotalAmount(nettRatedCalculator.calculateTotalAmount(context.getNettAmount(), context.getCommissionAmount(), context.getTaxAmount()));
}
public void updateTaxRateFromTextBox(PostUnderwriterTransactionContext context) {
context.setTaxAmount(nettRatedCalculator.calculateTaxAmount(context.getTaxRate(), context.getNettAmount(), context.getCommissionAmount()));
context.setNettAmountIncTax(nettRatedCalculator.calculateNettIncTax(context.getNettAmount(), context.getTaxRate()));
context.setTotalAmount(nettRatedCalculator.calculateTotalAmount(context.getNettAmount(), context.getCommissionAmount(), context.getTaxAmount()));
}
public void updateTaxRateFromSystemParameter(PostUnderwriterTransactionContext context) {
context.setTaxRate(getTaxRateParameter(context.getPscm(), context));
context.setTaxAmount(nettRatedCalculator.calculateTaxAmount(context.getTaxRate(), context.getNettAmount(), context.getCommissionAmount()));
context.setNettAmountIncTax(nettRatedCalculator.calculateNettIncTax(context.getNettAmount(), context.getTaxRate()));
context.setTotalAmount(nettRatedCalculator.calculateTotalAmount(context.getNettAmount(), context.getCommissionAmount(), context.getTaxAmount()));
}
public void updateNettIncTax(PostUnderwriterTransactionContext context){
context.setNettAmount(nettRatedCalculator.calculateNettAmount(context.getNettAmountIncTax(), context.getParameterTaxRate()));
context.setCommissionAmount(nettRatedCalculator.calculateCommissionAmount(context.getCommissionFlatCharge(), context.getNettAmount(), context.getParameterCommissionRate()));
context.setCommissionRate(nettRatedCalculator.calculateCommissionRate(context.getCommissionAmount(), context.getNettAmount()));
context.setTaxAmount(nettRatedCalculator.calculateTaxAmount(context.getParameterTaxRate(), context.getNettAmount(), context.getCommissionAmount()));
context.setTaxRate(context.getParameterTaxRate());
context.setTotalAmount(nettRatedCalculator.calculateTotalAmount(context.getNettAmount(), context.getCommissionAmount(), context.getTaxAmount()));
}
public void updateGrossAmount(PostUnderwriterTransactionContext context) {
context.setTaxRate(context.getParameterTaxRate());
context.setTaxAmount(nettRatedCalculator.calculateTaxFromGross(context.getGrossAmount(), context.getTaxRate()));
context.setCommissionRate(context.getParameterCommissionRate());
context.setCommissionAmount(nettRatedCalculator.calculateCommissionGross(context.getCommissionRate(), context.getGrossAmount(), context.getTaxAmount()));
context.setTotalAmount(nettRatedCalculator.calculateTotalAmount(context.getGrossAmount(), context.getCommissionAmount(), context.getTaxAmount()));
}
public void updateGrossCommissionAmount(PostUnderwriterTransactionContext context){
context.setCommissionRate(nettRatedCalculator.calculateCommissionRateGross(context.getCommissionAmount(), context.getGrossAmount(), context.getTaxAmount()));
context.setTotalAmount(nettRatedCalculator.calculateTotalAmount(context.getGrossAmount(), context.getCommissionAmount(), context.getTaxAmount()));
}
public void updateGrossCommissionRate(PostUnderwriterTransactionContext context){
context.setCommissionAmount(nettRatedCalculator.calculateCommissionAmountGross(context.getCommissionRate(), context.getGrossAmount(), context.getTaxAmount()));
context.setTotalAmount(nettRatedCalculator.calculateTotalAmount(context.getGrossAmount(), context.getCommissionAmount(), context.getTaxAmount()));
}
public void updateGrossTaxAmount(PostUnderwriterTransactionContext context){
context.setTaxRate(nettRatedCalculator.calculateTaxRateGross(context.getTaxAmount(), context.getGrossAmount()));
context.setCommissionAmount(nettRatedCalculator.calculateCommissionAmountGross(context.getCommissionRate(), context.getGrossAmount(), context.getTaxAmount()));
context.setTotalAmount(nettRatedCalculator.calculateTotalAmount(context.getGrossAmount(), context.getCommissionAmount(), context.getTaxAmount()));
}
public void updateGrossTaxRate(PostUnderwriterTransactionContext context){
context.setTaxAmount(nettRatedCalculator.calculateTaxAmountFromTaxRateGross(context.getGrossAmount(), context.getTaxRate()));
context.setCommissionAmount(nettRatedCalculator.calculateCommissionAmountGross(context.getCommissionRate(), context.getGrossAmount(), context.getTaxAmount()));
context.setTotalAmount(nettRatedCalculator.calculateTotalAmount(context.getGrossAmount(), context.getCommissionAmount(), context.getTaxAmount()));
}
}
Я довольно новичок в калитке, но у меня все работает. Я думаю, что код может быть лучше. Есть идеи, что я могу сделать, чтобы улучшить дизайн?