Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.jleaf.erp.fin.web.controller.handoverinvoicear;
- import org.jleaf.core.GeneralConstants;
- import org.jleaf.erp.fin.api.FinanceApi;
- import org.jleaf.erp.fin.web.PageInputConstants;
- import org.jleaf.erp.fin.web.model.handoverinvoicear.HandoverInvoiceArLazyModel;
- import org.jleaf.util.Calc;
- import org.jleaf.web.helper.MessageHelper;
- import org.jleaf.web.jsf.FacesUtils;
- import org.jleaf.web.jsf.template.AbstractBrowseWithBasketController;
- import org.jleaf.web.model.ComboItem;
- import org.json.simple.JSONObject;
- import org.primefaces.context.RequestContext;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.context.annotation.Scope;
- import org.springframework.stereotype.Controller;
- import javax.annotation.PostConstruct;
- import java.io.Serializable;
- import java.util.*;
- /**
- * @author H.Kevin, January 6, 2020
- *
- */
- @SuppressWarnings("serial")
- @Controller
- @Scope("view")
- public class CreateHandoverInvoiceArController extends AbstractBrowseWithBasketController implements FinanceApi, Serializable {
- private static final Logger log = LoggerFactory.getLogger(CreateHandoverInvoiceArController.class);
- //private List<JSONObject> selectedInvoiceArBalanceList;
- private Double totalAmount = 0d;
- private HandoverInvoiceArLazyModel model;
- @SuppressWarnings("unchecked")
- @PostConstruct
- private void init() {
- log.debug("run init()");
- filterBean.getData().put("ou", new ComboItem("id", 10));
- filterBean.getData().put("dueDate", new ComboItem("code", GeneralConstants.EMPTY_VALUE));
- log.debug("filterBean.getData(): "+filterBean.getData());
- }
- @SuppressWarnings({ "unchecked" })
- @Override
- public JSONObject getJsonForSearch() {
- log.debug("filterBean: " + filterBean.getData());
- String dueDateTypeCode = GeneralConstants.EMPTY_VALUE;
- JSONObject ou = (JSONObject) filterBean.getData().get("ou");
- String customerCodeName = filterBean.getData().get("customerCodeName").toString().trim();
- JSONObject dueDateType = (JSONObject) filterBean.getData().get("dueDate");
- if(dueDateType != null) {
- dueDateTypeCode = dueDateType.get("code").toString();
- }
- JSONObject jsonInput = new JSONObject();
- jsonInput.put("ouId", ou.get("id") == null ? GeneralConstants.EMPTY_VALUE : ou.get("id"));
- jsonInput.put("customerCodeName", customerCodeName == null ? GeneralConstants.EMPTY_VALUE : customerCodeName);
- jsonInput.put("dueDateTypeCode", dueDateTypeCode);
- log.debug("jsonInput: " + jsonInput);
- return jsonInput;
- }
- @Override
- public void afterSearch(JSONObject result, boolean isOk) {
- log.debug("result after search: "+result);
- if (isOk) {
- Long rowCount = Long.valueOf(result.get("count").toString());
- model = new HandoverInvoiceArLazyModel(serviceContext, getJsonForSearch());
- model.setRowCount(rowCount.intValue());
- model.setPageSize(25);
- setSelectedList(null);
- }else {
- MessageHelper.displayError(result);
- }
- }
- @Override
- public String searchTaskName() {
- return COUNT_GET_INVOICE_AR_LIST_FOR_HANDOVER_INVOICE_AR;
- }
- @Override
- public void afterBasketAdd(JSONObject arg0) {
- // Nothing TO DO
- }
- @SuppressWarnings("unchecked")
- @Override
- public String next() {
- if (getChosenList().isEmpty()) {
- MessageHelper.displayError("org.jleaf.erp.fin.web.pleaseChooseInvoiceFirst");
- return PageInputConstants.CREATE_HANDOVER_INVOICE_AR + "?faces-redirect=false";
- }
- //String dueDateTypeCode = GeneralConstants.EMPTY_VALUE;
- List<JSONObject> invoiceArList = new ArrayList<JSONObject>(getChosenList().size());
- for (JSONObject balance : getChosenList()) {
- JSONObject json = new JSONObject();
- json.put("id", balance.get("invoiceId"));
- json.put("tenantId", balance.get("tenantId"));
- json.put("ouId", balance.get("ouId"));
- json.put("docTypeId", balance.get("docTypeId"));
- json.put("docDesc", balance.get("docTypeDesc"));
- json.put("docNo", balance.get("docNo"));
- json.put("docDate", balance.get("docDate"));
- json.put("partnerId", balance.get("partnerId"));
- json.put("partnerCode", balance.get("partnerCode"));
- json.put("partnerName", balance.get("partnerName"));
- json.put("dueDate", balance.get("dueDate"));
- json.put("currCode", balance.get("currCode"));
- json.put("invoiceAmount", balance.get("invoiceAmount"));
- json.put("paymentAmount", balance.get("paymentAmount"));
- json.put("osAmount", balance.get("osAmount"));
- json.put("version", balance.get("version"));
- invoiceArList.add(json);
- }
- JSONObject ou = (JSONObject) filterBean.getData().get("ou");
- // String customerCodeName = filterBean.getData().get("customerCodeName").toString().trim();
- // JSONObject dueDateType = (JSONObject) filterBean.getData().get("dueDate");
- //
- // if(dueDateType != null) {
- // dueDateTypeCode = dueDateType.get("code").toString();
- // }
- Map<String, Object> params = new HashMap<String, Object>();
- params.put("ouId", ou.get("id"));
- params.put("ouCode", ou.get("code"));
- params.put("ouName", ou.get("name"));
- params.put("invoiceArList", invoiceArList);
- log.debug("PARAMS FOR CREATE: " + params);
- FacesUtils.storeForCreate(PageInputConstants.INPUT_HANDOVER_INVOICE_AR, params);
- RequestContext.getCurrentInstance().execute("document.location='inputHandoverInvoiceAr.jsf'");
- return PageInputConstants.INPUT_HANDOVER_INVOICE_AR;
- }
- public void onOuChanged() {
- log.debug("ou: "+filterBean.getData().get("ou"));
- model = null;
- if (getChosenList() != null) {
- getChosenList().clear();
- }
- checkOnChange();
- }
- public void onDueDateChanged() {
- log.debug("dueDate: "+filterBean.getData().get("dueDate"));
- //model = null;
- //getChosenList().clear();
- checkOnChange();
- }
- private void checkOnChange() {
- RequestContext context = RequestContext.getCurrentInstance();
- if (!getChosenList().isEmpty()) {
- context.addCallbackParam("show", true);
- } else {
- context.addCallbackParam("show", false);
- resetGrids();
- }
- }
- public void resetGrids() {
- gridItemList = null;
- setSelectedList(null);
- clearBasket();
- }
- @SuppressWarnings("unchecked")
- @Override
- public void addToBasket() {
- log.debug(">> addToBasket()");
- log.debug("selectedList >>: " + Arrays.toString(getSelectedList()));
- RequestContext context = RequestContext.getCurrentInstance();
- //check Empty List
- if ((getSelectedList() == null) || (getSelectedList().length == 0)) {
- MessageHelper.displayError("org.jleaf.erp.fin.web.noDataSelected");
- context.addCallbackParam("show", false);
- return;
- }
- //Check duplicate invoice
- if(getChosenList() != null) {
- for(JSONObject newList : getSelectedList()) {
- log.debug("newList: "+newList);
- for (JSONObject addedInvoice : getChosenList()) {
- log.debug("addedInvoice: "+addedInvoice);
- if(addedInvoice.containsValue(newList.get("id"))){
- MessageHelper.displayError("invoice.already.added.to.basket");
- return;
- }
- }
- }
- }
- super.addToBasket();
- log.debug("getChosenList >> : " +getChosenList());
- calculateTotalAmount();
- }
- public void calculateTotalAmount(){
- Calc calc = new Calc(0);
- if(getChosenList()!=null && !getChosenList().isEmpty()) {
- for(JSONObject data : getChosenList()){
- calc = calc.add(Double.valueOf(data.get("osAmount").toString()));
- }
- }
- totalAmount = calc.doubleValue();
- log.debug("totalAmount : >>"+totalAmount);
- }
- @Override
- public boolean isMultiple() {
- return true;
- }
- @Override
- public void removeFromBasket(){
- super.removeFromBasket();
- calculateTotalAmount();
- }
- @Override
- public void clearBasket(){
- super.clearBasket();
- calculateTotalAmount();
- }
- public Double getTotalAmount() {
- return totalAmount;
- }
- public void setTotalAmount(Double totalAmount) {
- this.totalAmount = totalAmount;
- }
- public HandoverInvoiceArLazyModel getModel() {
- return model;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement