@Override
  protected void customerSelected(ClientCustomer customer) {
    if (customer == null) return;

    // Job Tracking
    if (isTrackJob()) {
      jobListCombo.setValue("");
      jobListCombo.setEnabled(!isInViewMode());
      jobListCombo.setCustomer(customer);
    }
    ClientCurrency clientCurrency = getCurrency(customer.getCurrency());
    amountText.setCurrency(clientCurrency);
    bankBalText.setCurrency(clientCurrency);
    customerBalText.setCurrency(clientCurrency);

    this.setCustomer(customer);
    if (customerCombo != null) {
      customerCombo.setComboItem(customer);
    }
    this.addressListOfCustomer = customer.getAddress();
    initBillToCombo();
    customerBalText.setAmount(customer.getBalance());
    adjustBalance(amountText.getAmount());
    currencyWidget.setSelectedCurrencyFactorInWidget(
        clientCurrency, transactionDateItem.getDate().getDate());
  }
 public void resetElements() {
   this.setCustomer(null);
   this.addressListOfCustomer = null;
   this.depositInAccount = null;
   this.paymentMethod = UIUtils.getpaymentMethodCheckBy_CompanyType(messages.check());
   amountText.setAmount(0D);
   // endBalText.setAmount(getAmountInTransactionCurrency(0D));
   // customerBalText.setAmount(getAmountInTransactionCurrency(0D));
   memoTextAreaItem.setValue("");
 }
 @Override
 protected void accountSelected(ClientAccount account) {
   if (account == null) return;
   this.depositInAccount = account;
   depositInCombo.setValue(depositInAccount);
   bankBalText.setAmount(depositInAccount.getTotalBalance());
   // if (account != null && !(Boolean) printCheck.getValue()) {
   // setCheckNumber();
   // } else if (account == null)
   // checkNo.setValue("");
   adjustBalance(amountText.getAmount());
 }
  private void adjustBalance(double amount) {
    ClientCustomerPrePayment customerPrePayment = transaction;
    enteredBalance = amount;

    if (DecimalUtil.isLessThan(enteredBalance, 0)
        || DecimalUtil.isGreaterThan(enteredBalance, 1000000000000.00)) {
      amountText.setAmount(0D);
      enteredBalance = 0D;
    }
    if (getCustomer() != null) {
      if (isInViewMode()
          && getCustomer().getID() == (customerPrePayment.getCustomer())
          && !DecimalUtil.isEquals(enteredBalance, 0)) {
        double cusBal =
            DecimalUtil.isLessThan(getCustomer().getBalance(), 0)
                ? -1 * getCustomer().getBalance()
                : getCustomer().getBalance();
        toBeSetCustomerBalance = (cusBal - transaction.getTotal()) + enteredBalance;
      } else {
        toBeSetCustomerBalance = getCustomer().getBalance() - enteredBalance;
      }
      // customerBalText.setAmount(toBeSetCustomerBalance);

    }
    if (depositInAccount != null) {
      double balanceToBeUpdate;
      if (depositInAccount.getCurrency() == getPreferences().getPrimaryCurrency().getID()) {
        balanceToBeUpdate = enteredBalance;
      } else {
        balanceToBeUpdate = enteredBalance;
      }

      if (depositInAccount.isIncrease()) {
        toBeSetEndingBalance =
            depositInAccount.getTotalBalanceInAccountCurrency() - balanceToBeUpdate;
      } else {
        toBeSetEndingBalance =
            depositInAccount.getTotalBalanceInAccountCurrency() + balanceToBeUpdate;
      }
      if (isInViewMode()
          && depositInAccount.getID() == (customerPrePayment.getDepositIn())
          && !DecimalUtil.isEquals(balanceToBeUpdate, 0)) {
        toBeSetEndingBalance = toBeSetEndingBalance - transaction.getTotal();
      }
      // endBalText.setAmount(toBeSetEndingBalance);

    }
  }
  @Override
  protected void createControls() {
    Label lab1 = new Label(messages.payeePrePayment(Global.get().Customer()));
    lab1.setStyleName("label-title");
    // lab1.setHeight("35px");
    transactionDateItem = createTransactionDateItem();

    transactionNumber = createTransactionNumberItem();

    listforms = new ArrayList<DynamicForm>();
    locationCombo = createLocationCombo();
    DynamicForm dateNoForm = new DynamicForm("datenumber-panel");
    dateNoForm.add(transactionDateItem, transactionNumber);

    StyledPanel datepanel = new StyledPanel("datepanel");
    datepanel.add(dateNoForm);

    StyledPanel labeldateNoLayout = new StyledPanel("labeldateNoLayout");
    labeldateNoLayout.add(lab1);
    labeldateNoLayout.add(datepanel);
    // customer and address
    customerCombo = createCustomerComboItem(messages.payeeName(Global.get().Customer()));

    billToCombo = createBillToComboItem(messages.address());
    billToCombo.setEnabled(false);

    // Ending and Vendor Balance
    bankBalText = new AmountField(messages.bankBalance(), this, getBaseCurrency(), "bankBalText");
    bankBalText.setEnabled(false);

    customerBalText =
        new AmountField(
            messages.payeeBalance(Global.get().Customer()),
            this,
            getBaseCurrency(),
            "customerBalText");
    customerBalText.setEnabled(false);

    DynamicForm balForm = new DynamicForm("balForm");
    if (locationTrackingEnabled) balForm.add(locationCombo);
    classListCombo = createAccounterClassListCombo();
    if (isTrackClass()) {
      balForm.add(classListCombo);
    }
    jobListCombo = createJobListCombo();
    if (isTrackJob()) {
      jobListCombo.setEnabled(false);
      balForm.add(jobListCombo);
    }
    balForm.add(bankBalText, customerBalText);
    // balForm.getCellFormatter().setWidth(0, 0, "205px");

    // payment
    depositInCombo = createDepositInComboItem(bankBalText);
    // depositInCombo.setPopupWidth("500px");

    amountText = new AmountField(messages.amount(), this, getBaseCurrency(), "amountText");
    amountText.setRequired(true);
    amountText.addBlurHandler(getBlurHandler());

    paymentMethodCombo = createPaymentMethodSelectItem();
    paymentMethodCombo.setComboItem(UIUtils.getpaymentMethodCheckBy_CompanyType(messages.check()));
    // printCheck = new CheckboxItem(messages.toBePrinted());
    // printCheck.setValue(true);
    // printCheck.addChangeHandler(new ValueChangeHandler<Boolean>() {
    //
    // @Override
    // public void onValueChange(ValueChangeEvent<Boolean> event) {
    // isChecked = (Boolean) event.getValue();
    // if (isChecked) {
    // if (printCheck.getValue().toString()
    // .equalsIgnoreCase("true")) {
    // checkNo.setValue(messages.toBePrinted());
    // checkNo.setDisabled(true);
    // } else {
    // if (depositInAccount == null)
    // checkNo.setValue(messages
    // .toBePrinted());
    // else if (isInViewMode()) {
    // checkNo.setValue(((ClientCustomerPrePayment) transaction)
    // .getCheckNumber());
    // }
    // }
    // } else
    // // setCheckNumber();
    // checkNo.setValue("");
    // checkNo.setDisabled(false);
    //
    // }
    // });
    checkNo = createCheckNumberItm();
    // checkNo.setValue(messages.toBePrinted());
    // checkNo.setDisabled(true);
    checkNo.addChangeHandler(
        new ChangeHandler() {

          @Override
          public void onChange(ChangeEvent event) {
            checkNumber = checkNo.getValue().toString();
          }
        });
    checkNo.setEnabled(!isInViewMode());
    currencyWidget = createCurrencyFactorWidget();
    payForm = UIUtils.form(messages.payment());
    memoTextAreaItem = createMemoTextAreaItem();
    // refText = createRefereceText();
    // refText.setWidth(100);
    payForm.add(
        customerCombo,
        billToCombo,
        depositInCombo,
        amountText,
        paymentMethodCombo,
        checkNo,
        memoTextAreaItem);
    // memo and Reference
    ClientAccount selectedValue = depositInCombo.getSelectedValue();
    if (selectedValue != null) {
      bankBalText.setAmount(selectedValue.getTotalBalanceInAccountCurrency());
      bankBalText.setCurrency(getCompany().getCurrency(selectedValue.getCurrency()));
    }

    // payForm.getCellFormatter().setWidth(0, 0, "160px");

    StyledPanel leftPanel = new StyledPanel("leftPanel");
    leftPanel.add(payForm);
    // leftPanel.add(payForm);
    // leftPanel.add(memoForm);

    StyledPanel rightPanel = new StyledPanel("rightPanel");
    rightPanel.add(balForm);
    if (isMultiCurrencyEnabled()) {
      rightPanel.add(currencyWidget);
    }

    StyledPanel mainVLay = new StyledPanel("mainVLay");
    mainVLay.add(voidedPanel);
    mainVLay.add(labeldateNoLayout);
    StyledPanel hLay = getTopLayOut();
    if (hLay != null) {
      hLay.add(leftPanel);
      hLay.add(rightPanel);
      mainVLay.add(hLay);
    } else {
      mainVLay.add(leftPanel);
      mainVLay.add(rightPanel);
    }

    this.add(mainVLay);

    /* Adding dynamic forms in list */
    listforms.add(dateNoForm);
    listforms.add(balForm);
    listforms.add(payForm);
    // settabIndexes();
  }
  @Override
  protected void initTransactionViewData() {
    if (transaction == null) {
      setData(new ClientCustomerPrePayment());
      initDepositInAccounts();
    } else {

      if (currencyWidget != null) {
        this.currency = getCompany().getCurrency(transaction.getCurrency());
        this.currencyFactor = transaction.getCurrencyFactor();
        currencyWidget.setSelectedCurrency(this.currency);
        // currencyWidget.currencyChanged(this.currency);
        currencyWidget.setCurrencyFactor(transaction.getCurrencyFactor());
        currencyWidget.setEnabled(!isInViewMode());
      }
      ClientCompany comapny = getCompany();

      ClientCustomer customer = comapny.getCustomer(transaction.getCustomer());
      customerSelected(comapny.getCustomer(transaction.getCustomer()));
      this.billingAddress = transaction.getAddress();
      if (billingAddress != null) billToaddressSelected(billingAddress);
      amountText.setEnabled(!isInViewMode());
      amountText.setAmount(transaction.getTotal());
      if (customer != null) {
        customerBalText.setAmount(customer.getBalance());
      }
      // bankBalText.setAmount(getAmountInTransactionCurrency(transaction.g));
      paymentMethodSelected(transaction.getPaymentMethod());
      this.depositInAccount = comapny.getAccount(transaction.getDepositIn());
      if (depositInAccount != null) {
        depositInCombo.setComboItem(depositInAccount);
        bankBalText.setAmount(depositInAccount.getTotalBalanceInAccountCurrency());
        bankBalText.setCurrency(getCompany().getCurrency(depositInAccount.getCurrency()));
      }
      if (isTrackClass()) {
        classListCombo.setComboItem(
            getCompany().getAccounterClass(transaction.getAccounterClass()));
      }
      paymentMethodCombo.setComboItem(transaction.getPaymentMethod());
      checkNo.setValue(transaction.getCheckNumber());
      // if (transaction.getPaymentMethod().equals(constants.check())) {
      // printCheck.setDisabled(isInViewMode());
      // checkNo.setDisabled(isInViewMode());
      // } else {
      // printCheck.setDisabled(true);
      // checkNo.setDisabled(true);
      // }

      // if (transaction.getCheckNumber() != null) {
      // if (transaction.getCheckNumber().equals(
      // messages.toBePrinted())) {
      // checkNo.setValue(messages.toBePrinted());
      // printCheck.setValue(true);
      // } else {
      // checkNo.setValue(transaction.getCheckNumber());
      // printCheck.setValue(false);
      // }
      // }
    }
    if (locationTrackingEnabled)
      locationSelected(getCompany().getLocation(transaction.getLocation()));
    if (isTrackJob()) {
      if (customer != null) {
        jobListCombo.setCustomer(customer);
      }
      jobSelected(Accounter.getCompany().getjob(transaction.getJob()));
    }
    initMemoAndReference();
    initCustomers();
    if (isMultiCurrencyEnabled()) {
      updateAmountsFromGUI();
    }
  }