NewReceiptDetailActivity.java 14 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
package com.huaheng.mobilewms.refactor;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
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.Collections;
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, containerCode_value;
    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);
//        ButterKnife.bind(this);
        setContentView(R.layout.activity_new_receipt_detail);
        bundle = getIntent().getExtras();
        receiptCode = bundle.getString("receiptCode");
        label = bundle.getString("label");
        mContext = this;
        setTitle(mContext.getString(R.string.bulk_collection));
        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);
        containerCode_value = findViewById(R.id.containerCode_value);
        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();
                text = WMSUtils.filterUTF8StringBomHeader(text);
                textView.setText(text);
                if (WMSUtils.isNotEmpty(text)) {
                    if (text.length() >= 11) {
                        focusReceiptByMaterialCode(text);
                    } else {
                        isContainer(text);
                    }
                }
                return false;
            }
        });
    }

    public void setTitle(String message) {
        commonTitle = findViewById(R.id.commonTitle);
        if (commonTitle != null) {
            commonTitle.setText(message);
        }
    }

    private void focusReceiptByMaterialCode(String text) {
        String materialCode;
        int qty;
        try {
            String[] array = text.split(";");
            if (array.length == 2) {
                //条码
                materialCode = array[0];
                qty = Integer.parseInt(array[1]);
            } else {
                //二维码, -1表示保留空值
                array = text.split("/", -1);
                materialCode = array[0];
                qty = Integer.parseInt(array[array.length - 2]);
            }
            autoSelectReceipt(materialCode, qty);
        } catch (Exception e) {
            WMSUtils.showShort("解析条码出错:" + text + "\n" + e.getMessage());
        } finally {
            resetContainerEdit();
        }
    }

    private void resetContainerEdit() {
        //延时聚集
        WMSUtils.requestFocus(containerEdit);
        containerEdit.setText("");
    }

    /**
     * 根据物料编码和收货数量将找到的单据都移动到首行,如果找到了多条,默认默认选中第1条
     *
     * @param material
     * @param qty
     */
    private void autoSelectReceipt(String material, int qty) {
        ReceiptDetailView view;
        int position = -1;
        ReceiptDetail detailSelect = null;
        for (int i = 0; i < receiptDetailList.size(); i++) {
            ReceiptDetail detail = receiptDetailList.get(i);
            //找物料编码、收货数量一致的单据
            if (detail.getMaterialCode().equals(material) && detail.getQty() == qty) {
                if (detail.tobeCollectQty() <= 0 || detail.isSelected())
                    continue;
                ReceiptDetail detailFind = receiptDetailList.remove(i);
                receiptDetailList.add(0, detailFind);
                //找未选中的单据, detailSelect将会得到最后一条未选中的
                if (!detailFind.isSelected()) {
                    detailSelect = detailFind;
                }
            }
        }

        if (detailSelect == null) {
            WMSUtils.showShort("没有了待收的物料:" + material);
            SpeechUtil.getInstance(mContext).speech("没有了");
            return;
        }else{
            SpeechUtil.getInstance(mContext).speech(detailSelect.getMaterialName() + " " + detailSelect.tobeCollectQty() + detailSelect.getUnit());
        }

        detailSelect.selectAll();
        //回到第一条
        //adk > 24 可用
        adapter.notifyDataSetInvalidated();
        receiptListView.setSelection(0);

//        receiptListView.smoothScrollToPosition(0);  //sdk 23可用
        adapter.notifyDataSetChanged();
//        sortListBySelection();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                ReceiptDetailView view = (ReceiptDetailView) receiptListView.getChildAt(0);
                view.setAutoSelectColor();
                if(view.getReceiptDetail().tobeCollectQty() > 1){
                    view.openQtyDialog();
                }
            }
        }, 500);
    }

    private void initList() {
        if (TextUtils.isEmpty(receiptHeader.getSourceCode()))
            receiptNoView.setText(receiptHeader.getCode());
        else
            receiptNoView.setText(receiptHeader.getSourceCode());
        setTitle(receiptHeader.getSupplierName());
        adapter = new ReceiptDetailAdapter(mContext, receiptDetailList);
        receiptListView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        sortListByQtyZero();
    }

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

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

    /**
     * 按用户是否选中做排序
     */
    public void sortListBySelection() {
        Collections.sort(receiptDetailList, 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) {
            SpeechUtil.getInstance(mContext).speech("没有这个托盘");
            resetContainerEdit();
        }
    };

    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);
        } else {
            ensureBtn.setBackgroundResource(R.drawable.gray_button_bg);
            ensureBtn.setClickable(false);
        }
    }

    /**
     * 更新所选容器
     **/
    private void updateContainerCode(String code) {
        currentContainerCode = code;
        containerCode_value.setText(code);
        resetContainerEdit();
        if(!TextUtils.isEmpty(code)){
            SpeechUtil.getInstance(mContext).speech("已选择托盘");
        }
        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;
        }
    }
}