BulkCollectActivity.java 12.3 KB
package com.huaheng.wms.collectgoods;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.huaheng.wms.R;
import com.huaheng.wms.WMSLog;
import com.huaheng.wms.https.HttpInterface;
import com.huaheng.wms.https.Subscribers.ProgressSubscriber;
import com.huaheng.wms.https.Subscribers.SubscriberOnNextListener;
import com.huaheng.wms.login.UserBean;
import com.huaheng.wms.login.WareHouseActivity;
import com.huaheng.wms.model.CommonActivity;
import com.huaheng.wms.util.DataBaseHelpter;
import com.huaheng.wms.util.SoundUtils;
import com.huaheng.wms.util.WMSUtils;
import com.huaheng.wms.views.LineLayout;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 ** 批量收货
 */
public class BulkCollectActivity extends CommonActivity {

    @BindView(R.id.snEdit)
    EditText snEdit;
    @BindView(R.id.scanLayout)
    LinearLayout scanLayout;
    @BindView(R.id.inputEdit)
    EditText inputEdit;
    @BindView(R.id.inputLayout)
    FrameLayout inputLayout;
    @BindView(R.id.wnLayout)
    LineLayout wnLayout;
    @BindView(R.id.cnLayout)
    LineLayout cnLayout;
    @BindView(R.id.barcodeLayout)
    LineLayout barcodeLayout;
    @BindView(R.id.commondityLayout)
    LineLayout commondityLayout;
    @BindView(R.id.unitLayout)
    LineLayout unitLayout;
    @BindView(R.id.inventoryLayout)
    LineLayout inventoryLayout;
    @BindView(R.id.alreadyView)
    TextView alreadyView;
    @BindView(R.id.tobeView)
    TextView tobeView;
    @BindView(R.id.ensureBtn)
    Button ensureBtn;
    @BindView(R.id.contentLayout)
    LinearLayout contentLayout;
    @BindView(R.id.ensureLayout)
    LinearLayout ensureLayout;
    private String wn;
    private String tobeNumber = "100";
    private String barcode;
    private String cn;
    private String receiptID;
    private boolean isSubmit = false;
    private boolean isEnterCN = false;
    private AlertDialog mDialog;
    private ReceiptInfo receiptInfo;
    private List<BillsInfo> billsInfos;

    @Override
    protected void initActivityOnCreate(Bundle savedInstanceState) {
        super.initActivityOnCreate(savedInstanceState);
        setContentView(R.layout.activity_bulk_collect);
        ButterKnife.bind(this);
        setTitle(getString(R.string.bulk_collection));
        initView();
        enableButton(false);
    }

    private void initView() {
        billsInfos = new ArrayList<>();
        snEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String number = editable.toString();
                if(!isEnterCN) {
                    if (number != null && number.length() > 0) {
                        wn = number;
                        enableButton(true);
                    } else {
                        enableButton(false);
                    }
                } else {
                    if (number != null && number.length() > 0) {
                        cn = number;
                        sanContainer(cn);
                    }
                }
            }
        });
        snEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
                    if(isEnterCN) {

                    }
                }
                return  false;
            }
        });
        inputEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                    barcode = editable.toString();
                    if (barcode != null && barcode.length() > 0) {
                        getCollectInfo();
                    } else {
                        enableButton(false);
                    }
            }
        });
    }

    private void enableButton(boolean enable) {
        if (enable) {
            ensureBtn.setBackgroundResource(R.drawable.blue_button_bg);
            ensureLayout.setClickable(true);
        } else {
            ensureBtn.setBackgroundResource(R.drawable.gray_button_bg);
            ensureLayout.setClickable(false);
        }
    }


    private void getCollectInfo() {
        String name = "MYR930S-X 立式饮水机 白色 S2平台网销机";
        String unit = "个";
        String status = "良品";
        String already_received = "0";
        contentLayout.setVisibility(View.VISIBLE);
        wnLayout.setLineName(this.getString(R.string.warehouse_number));
        wnLayout.setLineContent(receiptID);
        cnLayout.setLineName(this.getString(R.string.container_number));
        cnLayout.setLineContent(cn);
        barcodeLayout.setLineName(this.getString(R.string.commodity_barcode));
        barcodeLayout.setLineContent(barcode);
        commondityLayout.setLineName(this.getString(R.string.commodity_name));
        commondityLayout.setLineContent(name);
        unitLayout.setLineName(this.getString(R.string.unit));
        unitLayout.setLineContent(unit);
        inventoryLayout.setLineName(this.getString(R.string.inventory_state));
        inventoryLayout.setLineContent(status);
        alreadyView.setText(already_received);
        tobeView.setText(tobeNumber);
    }

    private void showInputLayout() {
        WMSLog.d("showInputLayout");
        scanLayout.setVisibility(View.GONE);
        inputLayout.setVisibility(View.VISIBLE);
        inputEdit.requestFocus();
    }


    @OnClick({R.id.inputBtn, R.id.ensureLayout})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.inputBtn:
                showInputDialog();
                break;
            case R.id.ensureLayout:
                WMSLog.d("isSubmit:" + isSubmit);
                if (isSubmit) {
                    submit();
                } else {
                    scanBill(wn);
                }
                break;
        }
    }

    private void showInputDialog() {
        final EditText et = new EditText(this);
        et.setInputType(InputType.TYPE_CLASS_NUMBER);
        AlertDialog.Builder builder = new AlertDialog.Builder(this).setTitle(getString(R.string.input_quantity_goods))
                .setView(et)
                .setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        //按下确定键后的事件
                        String already = et.getText().toString();
                        int number = Integer.parseInt(already);
                        if (number > 0) {
                            enableButton(true);
                        }
                        int tobe = Integer.parseInt(tobeNumber) - Integer.parseInt(already);
                        alreadyView.setText(already);
                        tobeView.setText(String.valueOf(tobe));
                        inputEdit.requestFocus();
                        addBillList(already);
                    }
                }).setNegativeButton(this.getString(R.string.cancel), null);
        mDialog = builder.create();
        mDialog.show();
        et.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
                    String already = textView.getText().toString();
                    int number = Integer.parseInt(already);
                    if (number > 0) {
                        enableButton(true);
                    }
                    int tobe = Integer.parseInt(tobeNumber) - Integer.parseInt(already);
                    alreadyView.setText(already);
                    tobeView.setText(String.valueOf(tobe));
                    mDialog.dismiss();
                }
                return false;
            }
        });
    }

    private void submit() {
        saveBillByBulk();
    }

    private void scanBill(String code) {
        receiptID = code;
        HttpInterface.getInsstance().scanBill(new ProgressSubscriber<ArrayList<ReceiptInfo>>(this, scanBillListener), code);
    }

    SubscriberOnNextListener scanBillListener = new SubscriberOnNextListener<ArrayList<ReceiptInfo>>() {
        @Override
        public void onNext(ArrayList<ReceiptInfo> receiptInfos) {
            if(receiptInfos != null && receiptInfos.size() > 0) {
                for(int i=0; i< receiptInfos.size(); i++) {
                    List<ReceiptInfo>  receiptInfoList = DataBaseHelpter.queryAllReceiptInfo();
                    receiptInfo = receiptInfos.get(i);
                    if(receiptInfoList != null && receiptInfoList.size() >0) {
                        boolean insert = false;
                        for(int j=0; j< receiptInfoList.size(); j++) {
                            WMSLog.d("receiptInfoList.get(j).getId():" + receiptInfoList.get(j).getId() + "  receiptInfo.getId():" + receiptInfo.getId());
                            if(receiptInfoList.get(j).getId().equals(receiptInfo.getId())) {
                                insert = false;
                                break;
                            }
                            insert = true;
                        }
                        if(insert) {
                            WMSLog.d("receiptInfoList.size():" + receiptInfoList.size());
                            receiptInfo.set_id(receiptInfoList.size());
                            try {
                                DataBaseHelpter.insertReceiptInfo(receiptInfo);
                                continue;
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    } else {
                        receiptInfo.set_id(0);
                        DataBaseHelpter.insertReceiptInfo(receiptInfo);
                    }
                }
                isEnterCN = true;
                snEdit.requestFocus();
                snEdit.setText("");
                snEdit.setHint(R.string.enter_container_number);
                enableButton(false);
            }
        }
    };

    private void sanContainer(String code) {
        HttpInterface.getInsstance().sanContainer(new ProgressSubscriber<String>(this, sanContainerListener), code);
    }

    SubscriberOnNextListener sanContainerListener = new SubscriberOnNextListener<String>() {

        @Override
        public void onNext(String s) {
            isSubmit = true;
            showInputLayout();
        }
    };

    private void addBillList(String number) {
        BillsInfo billsInfo = new BillsInfo(cn, receiptInfo.getId(), number);
        billsInfos.add(billsInfo);
    }

    private void saveBillByBulk() {
        HttpInterface.getInsstance().saveBillByBulk(new ProgressSubscriber<List<GoodsInfo>>(this, saveBillByBulkListener), billsInfos);
    }

    SubscriberOnNextListener saveBillByBulkListener = new SubscriberOnNextListener<List<GoodsInfo>>() {

        @Override
        public void onNext(List<GoodsInfo> goodsInfos) {
            SoundUtils.getInstance(BulkCollectActivity.this).dingSound();
            finish();
            WMSUtils.startActivity(BulkCollectActivity.this, BulkCollectActivity.class);
        }
    };
}