BulkCollectActivity.java 14.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
package com.huaheng.wms.collectgoods;

import android.app.AlertDialog;
import android.content.Context;
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 android.widget.Toast;

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.SpeechUtil;
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, cn, barcode, receiptID, prefix;
    private boolean isSubmit = false;
    private boolean isEnterCN = false;
    private AlertDialog mDialog;
    private ReceiptInfo receiptInfo;
    private List<BillsInfo> billsInfos;
    private Context mContext;

    @Override
    protected void initActivityOnCreate(Bundle savedInstanceState) {
        super.initActivityOnCreate(savedInstanceState);
        setContentView(R.layout.activity_bulk_collect);
        ButterKnife.bind(this);
        setTitle(getString(R.string.bulk_collection));
        mContext = this;
        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 (WMSUtils.isNotEmpty(number)) {
                        wn = number;
                        enableButton(true);
                    } else {
                        enableButton(false);
                    }
                } else {
                    if (WMSUtils.isNotEmpty(number)) {
                        sanContainer(number);
                    }
                }
            }
        });
        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) {
                String number = editable.toString();
                if (!WMSUtils.isNotEmpty(number)) {
                    enableButton(false);
                }
            }
        });

        inputEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                String number = textView.getText().toString();
                if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
                   if(!WMSUtils.isNotEmpty(number)) {
                       return false;
                   }
                    prefix = number.substring(0, 1);
                    if(WMSUtils.containLetter(prefix)) {
                        sanContainer(number);
                    } else {
                        barcode = number;
                        if (getReceiptInfo()) {
                            getCollectInfo();
                        } else {
                            WMSUtils.showShort(mContext.getString(R.string.commodity_number_was_not_found));
                            WMSUtils.resetEdit(inputEdit);
                        }
                    }
                }
                return false;
            }
        });
    }

    private boolean getReceiptInfo() {
        boolean find = false;
        List<ReceiptInfo> receiptInfoList = DataBaseHelpter.queryAllReceiptInfo();
        ReceiptInfo receipt;
        for (int i = 0; i < receiptInfoList.size(); i++) {
            receipt = receiptInfoList.get(i);
            if (receipt.getMaterialCode().equals(barcode)) {
                receiptInfo = receipt;
                find = true;
                break;
            }
        }
        return find;
    }

    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() {
        WMSLog.d("getCollectInfo  receiptInfo:" + receiptInfo.toString());
        int already = (int) receiptInfo.getQty();
        if (already > receiptInfo.getSysQty()) {
            already = (int) receiptInfo.getSysQty();
        }
        int tobe = (int) (receiptInfo.getSysQty() - already);
        if (tobe < 0) {
            tobe = 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(receiptInfo.getName());
        unitLayout.setLineName(this.getString(R.string.unit));
        unitLayout.setLineContent(receiptInfo.getUnit());
        inventoryLayout.setLineName(this.getString(R.string.inventory_state));
        inventoryLayout.setLineContent(receiptInfo.getQuality());
        alreadyView.setText(String.valueOf(already));
        tobeView.setText(String.valueOf(tobe));
        WMSUtils.requestFocus(inputEdit);
    }

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


    @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 number = et.getText().toString();
                        showCollectsNumber(number);

                    }
                }).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 number = textView.getText().toString();
                    showCollectsNumber(number);
                }
                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) {
            WMSLog.d("scanBillListener  onNext:" + receiptInfos);
            if (WMSUtils.isNotEmptyList(receiptInfos)) {
                DataBaseHelpter.deleteAllReceiptInfo();
                insertOrUpdateReceiptInfo(receiptInfos);
                isEnterCN = true;
                WMSUtils.resetEdit(snEdit);
                snEdit.setHint(R.string.enter_container_number);
                enableButton(false);
            } else {
                WMSUtils.showShort(getString(R.string.toast_error));
            }
        }

        @Override
        public void onError(String str) {
            WMSUtils.resetEdit(snEdit);
        }
    };

    private void sanContainer(String number) {
        cn = number;
        WMSLog.d("sanContainer cn:" + cn);
        HttpInterface.getInsstance().sanContainer(new ProgressSubscriber<String>(this, sanContainerListener), number);
    }

    SubscriberOnNextListener sanContainerListener = new SubscriberOnNextListener<String>() {

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

        @Override
        public void onError(String str) {
            WMSUtils.resetEdit(snEdit);
        }
    };

    private void addBillList(String number) {
        int qry = Integer.parseInt(number);
        if(qry > 0) {
            WMSLog.d("addBillList  cn:" + cn + "  receiptInfo.getId():" + receiptInfo.getId());
            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) {
            WMSLog.d("goodsInfos:" + goodsInfos.toString());
            saveGoodsInfos(goodsInfos);
            SpeechUtil.getInstance(mContext).speech(mContext.getString(R.string.bulk_collection_success));
            finish();
            WMSUtils.startActivity(mContext, BulkCollectActivity.class);
        }

        @Override
        public void onError(String str) {
            WMSUtils.resetEdit(inputEdit);
        }
    };

    /*
    ** 保存商品信息到数据库
     */
    private void saveGoodsInfos(List<GoodsInfo> goodsInfos) {
        if (!WMSUtils.isNotEmptyList(goodsInfos)) {
            return;
        }
        for (GoodsInfo goodsInfo : goodsInfos) {
            List<ReceiptInfo> receiptInfoList = DataBaseHelpter.queryAllReceiptInfo();
            if (WMSUtils.isNotEmptyList(receiptInfoList)) {
                for (ReceiptInfo receipt : receiptInfoList) {
                    if (receipt.getId().equals(goodsInfo.getId())) {
                        receipt.setQty(goodsInfo.getQty());
                        DataBaseHelpter.updateReceiptInfoi(receiptInfo);
                    }
                }
            }
        }
    }

    private void insertOrUpdateReceiptInfo(ArrayList<ReceiptInfo> receiptInfos) {
        for (ReceiptInfo receiptInfo : receiptInfos) {
            List<ReceiptInfo> receiptInfoList = DataBaseHelpter.queryAllReceiptInfo();
            long id = 0;
            if (WMSUtils.isNotEmptyList(receiptInfoList)) {
                boolean insert = false;
                for (ReceiptInfo receipt : receiptInfoList) {
                    if (receipt.getId().equals(receiptInfo.getId())) {
                        insert = false;
                        id = receipt.get_id();
                        break;
                    }
                    insert = true;
                }
                if (insert) {
                    receiptInfo.set_id(receiptInfoList.size());
                    try {
                        DataBaseHelpter.insertReceiptInfo(receiptInfo);
                        continue;
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    receiptInfo.set_id(id);
                    DataBaseHelpter.updateReceiptInfoi(receiptInfo);
                }
            } else {
                receiptInfo.set_id(id);
                DataBaseHelpter.insertReceiptInfo(receiptInfo);
            }
        }
    }

    private void showCollectsNumber(String number) {
        if (WMSUtils.isNotEmpty(number)) {
            enableButton(true);
        }
        int ready = Integer.parseInt(number) + (int) receiptInfo.getQty();
        String already = String.valueOf(ready);
        int tobe = (int) (receiptInfo.getSysQty()) - Integer.parseInt(already);
        alreadyView.setText(already);
        tobeView.setText(String.valueOf(tobe));
        inputEdit.requestFocus();
        addBillList(number);
        mDialog.dismiss();
    }
}