Untitled

 avatar
unknown
plain_text
3 years ago
16 kB
1
Indexable
private void get_inventory_list_data(LinearLayout ll_inventory_list) {
        try {
            if (!Common.isNetworkAvailable()) {
                Notifications.showNotification(getActivity(), "", getString(R.string.NoInternet), 1);
                getView().findViewById(R.id.inventory_items_scroll_view).setVisibility(View.GONE);
                getView().findViewById(R.id.cv_no_inventory_items).setVisibility(View.VISIBLE);
                return;
            }

            Observable.fromCallable(new Callable<JsonNode>() {
                @Override
                public JsonNode call() {
                    try {
                        JSONObject jsonObject;
                        jsonObject = new JSONObject();
                        jsonObject.put("count", 1000);
                        jsonObject.put("offset", 0);

                        return optimus.mapperTemp.readTree(new CustomHttpClient().post("http://test.web-api.optimus.ba/api/" + "Resources/GET_Resource_Assignments", jsonObject.toString()));

                    } catch (Exception e) {
                        optimus.AddError("HomeFragment get_inventory_list_data call", e.getMessage(), e);
                    }

                    return null;
                }
            })
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Consumer<JsonNode>() {
                        @Override
                        public void accept(JsonNode jsonNode) {
                            try {

                                if (!isAdded()) return;

                                if (jsonNode == null) {
                                    return;
                                }

                                if (jsonNode.get("status_code").asInt() != 200) {
                                    return;
                                }

                                JsonNode result = jsonNode.get("result").get(0);

                                if (result.get("total_count").asInt() > 0 && result.get("total_count").asInt() > ll_inventory_list.getChildCount()) {

                                    if (ll_inventory_list.getChildCount() > 0) ll_inventory_list.removeAllViews();

                                    Calendar start_time = Calendar.getInstance();
                                    List<Long> route_dates = new ArrayList<>();

                                    if (start_time.get(Calendar.DAY_OF_MONTH) != start_time.getActualMaximum(Calendar.DAY_OF_MONTH)) {
                                        start_time.set(start_time.get(Calendar.YEAR), start_time.get(Calendar.MONTH), start_time.get(Calendar.DAY_OF_MONTH) + 1, 0, 0, 0);
                                        Calendar end_time = Calendar.getInstance();
                                        end_time.set(end_time.get(Calendar.YEAR), end_time.get(Calendar.MONTH), end_time.getActualMaximum(Calendar.DAY_OF_MONTH), 23, 59, 59);

                                        route_dates = DL_Routes.GET_Route_Dates(start_time.getTimeInMillis(), end_time.getTimeInMillis(), optimus.getVisit().delivery_place.DeliveryPlaceID);
                                    }

                                    getView().findViewById(R.id.cv_no_inventory_items).setVisibility(View.GONE);
                                    getView().findViewById(R.id.inventory_items_scroll_view).setVisibility(View.VISIBLE);
                                    LayoutInflater layoutInflater = LayoutInflater.from(getActivity());

                                    ArrayNode inventory_items = (ArrayNode) result.get("data");
                                    int total_items = 0;

                                    boolean has_mandatory_inventory = false;

                                    for (JsonNode item : inventory_items) {
                                        if (item.get("item_id").asLong() != optimus.getVisit().delivery_place.DeliveryPlaceID) continue;

                                        View v = layoutInflater.inflate(R.layout.vii_list_row_inventory, ll_inventory_list, false);

                                        RelativeLayout status_indicator = v.findViewById(R.id.rl_status);
                                        ImageView barcode = v.findViewById(R.id.img);
                                        TextView resource_serial_number = v.findViewById(R.id.lit_sub_text_1);
                                        TextView resource_name = v.findViewById(R.id.lit_text);
                                        TextView resource_type = v.findViewById(R.id.lit_sup_text);
                                        TextView inventory_date = v.findViewById(R.id.lit_inventory_date);

                                        status_indicator.setBackgroundResource(R.color.optimus_primary_color);

                                        resource_name.setText(item.get("data").get("properties").get("name").asText().trim().toUpperCase());
                                        resource_type.setText(item.get("data").get("properties").get("type").asText().trim().toUpperCase());
                                        resource_serial_number.setText(item.get("data").get("properties").get("serial_number").asText().trim());

                                        inventory_date.setText(Common.converMillisecondstoDate(item.get("data").asLong()));
                                        String barcode_str = item.get("data").get("properties").get("serial_number").asText();
                                        if (!TextUtils.isEmpty(barcode_str)) {
                                            try {
                                                MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
                                                BitMatrix bitMatrix = multiFormatWriter.encode(barcode_str, BarcodeFormat.CODE_128, barcode.getWidth(), barcode.getHeight());
                                                BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
                                                Bitmap bitmap = barcodeEncoder.createBitmap(bitMatrix);
                                                barcode.setImageBitmap(bitmap);
                                            } catch (Exception e) {
                                                e.printStackTrace();
                                            }
                                        }

                                        String text = item.get("data").get("properties").get("inventory_number").asText(); // Whatever you need to encode in the QR code
                                        MultiFormatWriter multiFormatWriter = new MultiFormatWriter();

                                        Map<EncodeHintType, Object> hintMap = new HashMap<>();
                                        hintMap.put(EncodeHintType.MARGIN, 0);
                                        BitMatrix bitMatrix = multiFormatWriter.encode(text, BarcodeFormat.QR_CODE,200,200, hintMap);
                                        BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
                                        Bitmap bitmap = barcodeEncoder.createBitmap(bitMatrix);
                                        ((ImageView) v.findViewById(R.id.resource_icon)).setImageBitmap(bitmap);

                                        v.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View view) {
                                                try {
                                                    InventoryAssetViewDialogFragment inventoryDialogItem = new InventoryAssetViewDialogFragment();
                                                    inventoryDialogItem.asset = item;
                                                    inventoryDialogItem.show(getActivity().getSupportFragmentManager(), "INVENTORY ASSET VIEW");
                                                } catch (Exception e) {
                                                    optimus.AddError("HomeFragment load_inventory list_item", e.getMessage(), e);
                                                }
                                            }
                                        });

                                        ObjectNode last_confirmation = DL_Forms.GET_Last_Inventory_Confirmation(optimus.getVisit().delivery_place.DeliveryPlaceID, item.get("uid").asText());

                                        if (last_confirmation != null) {
                                                Calendar today = Calendar.getInstance();
                                                Calendar confirmation_date = Calendar.getInstance();
                                                confirmation_date.setTimeInMillis(last_confirmation.get("doe").asLong());
                                                boolean confirmationFinished = false;

                                                if (today.get(Calendar.YEAR) == confirmation_date.get(Calendar.YEAR) && today.get(Calendar.MONTH) == confirmation_date.get(Calendar.MONTH) && today.get(Calendar.DAY_OF_MONTH) == confirmation_date.get(Calendar.DAY_OF_MONTH)) {
                                                    confirmationFinished = true;
                                                }

                                                switch (item.get("data").get("properties").get("confirmation_period").asInt()) {

                                                    case 1:
                                                        Calendar firstDayOfMonth = Calendar.getInstance();
                                                        firstDayOfMonth.set(firstDayOfMonth.get(Calendar.YEAR), firstDayOfMonth.get(Calendar.MONTH), 1, 0, 0, 0);
                                                        if (route_dates.size() == 0 && (last_confirmation.size() == 0 || last_confirmation.get("doe").asLong() < firstDayOfMonth.getTimeInMillis())) {
                                                            has_mandatory_inventory = true;
                                                            status_indicator.setBackgroundResource(R.color.red_status);
                                                            ((ObjectNode) item).put("inventory_confirmation_status", -1);
                                                        } else if (confirmationFinished) {
                                                            status_indicator.setBackgroundResource(R.color.green_status);
                                                            ((ObjectNode) item).put("inventory_confirmation_status", 1);
                                                        } else {
                                                            status_indicator.setBackgroundResource(R.color.yellow_status);
                                                            ((ObjectNode) item).put("inventory_confirmation_status", 0);
                                                        }
                                                        break;
                                                    case 2:
                                                        float diff = Calendar.getInstance().getTimeInMillis() - last_confirmation.get("doe").asLong();
                                                        float days = (diff / (1000 * 60 * 60 * 24));

                                                        if (days > 365) {
                                                            has_mandatory_inventory = true;
                                                            status_indicator.setBackgroundResource(R.color.red_status);
                                                            ((ObjectNode) item).put("inventory_confirmation_status", -1);
                                                        } else if (confirmationFinished) {
                                                            status_indicator.setBackgroundResource(R.color.green_status);
                                                            ((ObjectNode) item).put("inventory_confirmation_status", 1);
                                                        } else {
                                                            status_indicator.setBackgroundResource(R.color.yellow_status);
                                                            ((ObjectNode) item).put("inventory_confirmation_status", 0);
                                                        }
                                                        break;
                                                    case 3:
                                                        if (confirmationFinished) {
                                                            status_indicator.setBackgroundResource(R.color.green_status);
                                                            ((ObjectNode) item).put("inventory_confirmation_status", 1);

                                                        } else {
                                                            has_mandatory_inventory = true;
                                                            status_indicator.setBackgroundResource(R.color.red_status);
                                                            ((ObjectNode) item).put("inventory_confirmation_status", -1);
                                                        }
                                                        break;
                                                }
                                            } else {
                                                has_mandatory_inventory = true;
                                                status_indicator.setBackgroundResource(R.color.red_status);

                                                ((ObjectNode) item).put("inventory_confirmation_status", -1);
                                            }

                                        ll_inventory_list.addView(v);
                                        total_items++;
                                    }

                                    optimus.getVisit().has_mandatory_inventory = has_mandatory_inventory;

                                    ((TextView) getView().findViewById(R.id.lit_inventory_list_count)).setText(String.valueOf(total_items));
                                    getView().findViewById(R.id.lit_inventory_list_count).setVisibility(View.VISIBLE);

                                } else if (result.get("total_count").asInt() <= 0) {
                                    getView().findViewById(R.id.cv_inventory_legend).setVisibility(View.GONE);
                                    getView().findViewById(R.id.cv_no_inventory_items).setVisibility(View.VISIBLE);
                                    if (ll_inventory_list.getChildCount() > 0) ll_inventory_list.removeAllViews();
                                    optimus.getVisit().has_mandatory_inventory = false;
                                }
                            } catch (Exception e) {
                                optimus.AddError("HomeFragment get_inventory_list_data onNext", e.getMessage(), e);
                            }
                        }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) {
                            Exception e = new Exception(throwable.getMessage());
                            optimus.AddError("HomeFragment get_inventory_list_data", e.getMessage(), e);
                        }
                    }, new Action() {
                        @Override
                        public void run() throws Exception {
                            try {

                            } catch (Exception e) {
                                e.getMessage();
                            }
                        }
                    });
        } catch (Exception e) {
            optimus.AddError("HomeFragment get_inventory_list_data", e.getMessage(), e);
        }
    }