NewReceiptDetailActivity.java 9.89 KB
package com.huaheng.mobilewms.refactor;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.huaheng.mobilewms.R;
import com.huaheng.mobilewms.WMSApplication;
import com.huaheng.mobilewms.activity.model.CommonActivity;
import com.huaheng.mobilewms.bean.Receipt;
import com.huaheng.mobilewms.bean.ReceiptBill;
import com.huaheng.mobilewms.bean.ReceiptDetail;
import com.huaheng.mobilewms.bean.ReceiptHeader;
import com.huaheng.mobilewms.https.HttpInterface;
import com.huaheng.mobilewms.https.Subscribers.ProgressSubscriber;
import com.huaheng.mobilewms.https.Subscribers.SubscriberOnNextListener;
import com.huaheng.mobilewms.util.SpeechUtil;
import com.huaheng.mobilewms.util.WMSUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;


public class NewReceiptDetailActivity extends CommonActivity implements View.OnClickListener {
    private String receiptCode, label;
    public Context mContext;

    ListView receiptListView;
    ImageView backImage;
    LinearLayout contentLayout;
    TextView receiptNoView, containerCode_label;
    EditText containerEdit;
    Button sortBt, ensureBtn;

    ReceiptHeader receiptHeader;
    List<ReceiptDetail> receiptDetailList;
    ReceiptDetailAdapter adapter;

    //收货容器
    HashMap<String, List<ReceiptBill>> containerMap = new HashMap<>();
    //当前容器编码
    String currentContainerCode;

    Bundle bundle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_receipt_detail);
        bundle = getIntent().getExtras();
        receiptCode = bundle.getString("receiptCode");
        label = bundle.getString("label");
        mContext = this;

        findReceipt(receiptCode);
        initView();
        switchButton();
    }

    private void initView() {
        backImage = findViewById(R.id.backImage);
        backImage.setOnClickListener(this);
        contentLayout = findViewById(R.id.contentLayout);
        receiptListView = findViewById(R.id.receiptListView);
        receiptNoView = findViewById(R.id.receipt_no);
        receiptNoView.setText(receiptCode);
        containerEdit = findViewById(R.id.containerEdit);
        sortBt = findViewById(R.id.sortBt);
        containerCode_label = findViewById(R.id.containerCode_label);

        ensureBtn = findViewById(R.id.ensureBtn);

        sortBt.setOnClickListener(this);
        ensureBtn.setOnClickListener(this);
        containerCode_label.setOnClickListener(this);

        containerEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                String text = textView.getText().toString();
                text = text.trim().toUpperCase();
                textView.setText(text);
                if (WMSUtils.isNotEmpty(text)) {
                    isContainer(text);
                }
                return false;
            }
        });
    }

    private void initList() {
        adapter = new ReceiptDetailAdapter(mContext, receiptDetailList);
        receiptListView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        sortListByQtyZero();
    }

    public void sortListByQtyZero(){
        receiptDetailList.sort(new Comparator<ReceiptDetail>() {
            @Override
            public int compare(ReceiptDetail o1, ReceiptDetail o2) {
                return o2.tobeCollectQty() - o1.tobeCollectQty();
            }
        });

        adapter.updateList(receiptDetailList);
        adapter.notifyDataSetChanged();
    }

    /**
     * 按用户是否选中做排序
     */
    public void sortListBySelection(){
        receiptDetailList.sort(new Comparator<ReceiptDetail>() {
            @Override
            public int compare(ReceiptDetail o1, ReceiptDetail o2) {
                if(o1.isSelected() && o2.isSelected())
                    return 0;
                if(o1.isSelected() && !o2.isSelected())
                    return -1;
                if(!o1.isSelected() && o2.isSelected())
                    return 1;
//                return o2.getQtySelected()  - o1.getQtySelected();
                return 0;
            }
        });

        adapter.updateList(receiptDetailList);
        adapter.notifyDataSetChanged();
    }


    private void findReceipt(String receiptCode) {
        HttpInterface.getInsstance().findReceipt(new ProgressSubscriber<Receipt>(mContext, receiptListener), receiptCode);
    }

    SubscriberOnNextListener receiptListener = new SubscriberOnNextListener<Receipt>() {
        @Override
        public void onNext(Receipt receipt) {
            if (receipt != null) {
                receiptHeader = receipt.getReceiptHeader();
                receiptDetailList = receipt.getReceiptDetails();
                initList();
            } else {
                WMSUtils.showShort(getString(R.string.toast_error));
            }
        }

        @Override
        public void onError(String str) {

        }
    };

    public void isContainer(String containerCode) {
        HttpInterface.getInsstance().isContainer(new ProgressSubscriber<String>(this, isContainerListener), containerCode);
    }

    SubscriberOnNextListener isContainerListener = new SubscriberOnNextListener<String>() {

        @Override
        public void onNext(String str) {
            updateContainerCode(str);
        }

        @Override
        public void onError(String str) {
        }
    };

    private void listReceipt(List<ReceiptBill> billList) {
        HttpInterface.getInsstance().listReceipt(new ProgressSubscriber <String>(this, quickReceiptListener), billList);
    }

    SubscriberOnNextListener quickReceiptListener = new SubscriberOnNextListener <String>() {

        @Override
        public void onNext(String result) {
            SpeechUtil.getInstance(mContext).speech(mContext.getString(R.string.receipt_success));
            if(isAllReceiptDone()) {
                finish();
            }else{
                finish();
                Intent intent = new Intent();
                intent.setClass(mContext, NewReceiptDetailActivity.class);
                intent.putExtras(bundle);// 发送数据
                WMSApplication.getContext().startActivity(intent);
            }
        }

        @Override
        public void onError(String str) {

        }
    };

    /**
     * 根据用户的选择生成需要提交到后台的ReceiptBill
     **/
    private List<ReceiptBill> getReceiptSelected() {
        List<ReceiptBill> billList = new ArrayList<>();
        for (ReceiptDetail detail : receiptDetailList) {
            if (detail.getQtySelected() > 0) {
                ReceiptBill bill = new ReceiptBill();
                bill.setBatch(detail.getBatch());
                BigDecimal qty = new BigDecimal(detail.getQtySelected());
                bill.setQty(qty);
                bill.setMaterialCode(detail.getMaterialName());
                bill.setMaterialCode(detail.getMaterialCode());
                bill.setProject(detail.getProjectNo());
                bill.setReceiptDetailId(String.valueOf(detail.getId()));
                bill.setReceiptContainerCode(currentContainerCode);
                billList.add(bill);
            }
        }
        return billList;
    }

    /**检查是否还有未收的物料**/
    private boolean isAllReceiptDone(){
        int tobeCollectSum = 0;
        int userSelectQtySum = 0;
        for (ReceiptDetail detail : receiptDetailList) {
            tobeCollectSum += detail.tobeCollectQty();
            if (detail.getQtySelected() > 0) {
                userSelectQtySum += detail.getQtySelected();
            }
        }

        if(userSelectQtySum < tobeCollectSum){
            //未收完
            return false;
        }else{
            //已收完
            return true;
        }
    }

    /**
     * 提交收货
     **/
    private void save() {
        if (TextUtils.isEmpty(currentContainerCode)) {
            WMSUtils.showShort("还没有选择容器");
            return;
        }

        List<ReceiptBill> billList = getReceiptSelected();
        if (billList.size() == 0) {
            WMSUtils.showShort("还没有选择收货的物料");
            return;
        }
        listReceipt(billList);
    }

    /**
     * 检查并且切换是否可以提交
     */
    public void switchButton() {
        boolean enable = !TextUtils.isEmpty(currentContainerCode) && getReceiptSelected().size()>0;
        if (enable) {
            ensureBtn.setBackgroundResource(R.drawable.blue_button_bg);
            ensureBtn.setClickable(true);
            ensureBtn.setClickable(true);
        } else {
            ensureBtn.setBackgroundResource(R.drawable.gray_button_bg);
            ensureBtn.setClickable(false);
            ensureBtn.setClickable(false);
        }
    }

    /**更新所选容器**/
    private void updateContainerCode(String code){
        currentContainerCode = code;
        containerEdit.setText(code);
        if(TextUtils.isEmpty(code)) {
            containerEdit.requestFocus();
        }
        switchButton();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.sortBt:
                sortListBySelection();
                break;
            case R.id.containerCode_label:
                updateContainerCode("");
                break;
            case R.id.backImage:
                finish();
                break;
            case R.id.ensureBtn:
                save();
                break;
            default:
                break;
        }
    }
}