Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 35.00 KB | None | 0 0
  1. package com.ims.rms;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import android.app.Activity;
  7. import android.app.AlertDialog;
  8. import android.app.Dialog;
  9. import android.content.Context;
  10. import android.content.DialogInterface;
  11. import android.graphics.Color;
  12. import android.os.AsyncTask;
  13. import android.util.Log;
  14. import android.view.DragEvent;
  15. import android.view.LayoutInflater;
  16. import android.view.View;
  17. import android.view.View.OnClickListener;
  18. import android.view.View.OnLongClickListener;
  19. import android.view.ViewGroup;
  20. import android.widget.AdapterView;
  21. import android.widget.AdapterView.OnItemClickListener;
  22. import android.widget.ArrayAdapter;
  23. import android.widget.Button;
  24. import android.widget.EditText;
  25. import android.widget.ImageButton;
  26. import android.widget.ImageView;
  27. import android.widget.LinearLayout;
  28. import android.widget.ListView;
  29. import android.widget.TextView;
  30. import android.widget.Toast;
  31.  
  32. import com.google.gson.Gson;
  33.  
  34. import com.woxthebox.draglistview.DragItemAdapter;
  35. import com.woxthebox.draglistview.DragListView;
  36.  
  37. import org.json.JSONArray;
  38. import org.json.JSONException;
  39. import org.json.JSONObject;
  40.  
  41. import de.keyboardsurfer.android.widget.crouton.Crouton;
  42. import de.keyboardsurfer.android.widget.crouton.Style;
  43.  
  44. /***
  45.  * <
  46.  * ListViewCustomAdapter is the custom adapter class to list the order item in
  47.  * list view. It contain Item description with quantity.
  48.  * <p/>
  49.  */
  50. public class ListViewCustomAdapter extends DragItemAdapter<KotModel, ListViewCustomAdapter.ViewHolder> {
  51.     //MainActivity activity;
  52.     private Context context;
  53.     private int mGrabHandleId = R.id.drag_vertical_image_view;
  54.     private boolean mDragOnLongPress = false;
  55.     View view;
  56.     DragItemAdapter mAdapter;
  57.     public static final int NO_POSITION = -1;
  58.     public static final long NO_ID = -1;
  59.  
  60.     static int count;
  61.  
  62.     static ArrayList<String> listItem = new ArrayList<String>();
  63.     static ArrayList<String> listBaseUnit = new ArrayList<String>();
  64.     static ArrayList<Integer> listCount = new ArrayList<Integer>();
  65.     static ArrayList<String> listMcode = new ArrayList<String>();
  66.     static ArrayList<String> listKitchenDispatch = new ArrayList<String>();
  67.     static ArrayList<String> listKOTTIME = new ArrayList<String>();
  68.     static ArrayList<String> listAddedRemarks = new ArrayList<String>();
  69.     static ArrayList<String> listIsBarItem = new ArrayList<String>();
  70.     static ArrayList<String> listSno = new ArrayList<String>();
  71.     static ArrayList<String> listKot = new ArrayList<String>(); // edited by Direndra Subba
  72.     static ArrayList<Double> listRate = new ArrayList<Double>(); // edited by Direndra Subba
  73.  
  74.     JSONArray jsonArray = new JSONArray();
  75.  
  76.     String mcode, ret;
  77.     String[] tmp;
  78.     int pos;
  79.     int mcase;
  80.     View rowView;
  81.     //ImageView dragVerticalImage;
  82.     TextView tvCount, tvDescA, tvBaseUnit;
  83.     String[] altUnit, confactor, rate;
  84.  
  85.     String username, password;
  86.  
  87.     ListView lvList;
  88.     List<String> listRemarks;
  89.  
  90.     Integer isUnknown = 0;
  91.     EditText etDESC, etRate;
  92.     Gson gson = new Gson();
  93.     static ArrayList<KotModel> orderlist;
  94.  
  95.  
  96.     public ListViewCustomAdapter(Context context, JSONArray newJArray) {
  97.         this.context = context;
  98.         jsonArray = newJArray;
  99.  
  100.         Log.i("listViewjsonArray", jsonArray.toString());
  101.  
  102.         listItem.clear();
  103.         listBaseUnit.clear();
  104.         listCount.clear();
  105.         listMcode.clear();
  106.         listKitchenDispatch.clear();
  107.         listKOTTIME.clear();
  108.         listAddedRemarks.clear();
  109.         listIsBarItem.clear();
  110.         listSno.clear();
  111.         listKot.clear();
  112.         listRate.clear();
  113.  
  114.         for (int i = 0; i < jsonArray.length(); i++) {
  115.             try {
  116.                 JSONObject newJObject = jsonArray.getJSONObject(i);
  117.                 listItem.add(newJObject.getString("DESCA"));
  118.                 listBaseUnit.add(newJObject.getString("BASEUNIT"));
  119.                 listCount.add(newJObject.getInt("QUANTITY"));
  120.                 listMcode.add(newJObject.getString("MCODE"));
  121.                 listKitchenDispatch.add(newJObject.getString("KITCHENDISPATCH"));
  122.                 listKOTTIME.add(newJObject.getString("KOTTIME"));
  123.                 listAddedRemarks.add(newJObject.getString("REMARKS"));
  124.                 listIsBarItem.add(newJObject.getString("IsBarItem"));
  125.                 listSno.add(newJObject.getString("SNO"));
  126.                 listKot.add(newJObject.getString("KOT"));
  127.                 listRate.add(newJObject.getDouble("RATE"));
  128.             } catch (JSONException e) {
  129.                 e.printStackTrace();
  130.             } catch (NullPointerException e) {
  131.                 e.printStackTrace();
  132.             }
  133.         }
  134.  
  135.         setItemList(jsonArray);
  136.  
  137.     }
  138.  
  139.     private void setItemList(JSONArray jsonArray) {
  140.         orderlist = new ArrayList<KotModel>();
  141.         orderlist.clear();
  142.  
  143.         for (int i = 0; i < jsonArray.length(); i++) {
  144.             try {
  145.                 JSONObject jsonObject = jsonArray.getJSONObject(i);
  146.  
  147.                 orderlist.add(gson.fromJson(jsonObject.toString(), KotModel.class));
  148.  
  149.             } catch (JSONException e) {
  150.                 e.printStackTrace();
  151.             }
  152.         }
  153.  
  154.         setHasStableIds(true);
  155.         setItemList(orderlist);
  156.     }
  157.  
  158.     @Override
  159.     public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  160.         view = LayoutInflater.from(parent.getContext()).inflate(R.layout.listlayout, parent, false);
  161.         return new ViewHolder(view);
  162.     }
  163.  
  164.     @Override
  165.     public void onBindViewHolder(ViewHolder holder, final int position) {
  166.         super.onBindViewHolder(holder, position);
  167.         String descA = orderlist.get(position).getDescA();
  168.         String bUnit = orderlist.get(position).getBaseunit();
  169.         String quantity = orderlist.get(position).getQuantity();
  170.         String kottime = orderlist.get(position).getKotTime();
  171.  
  172.         holder.mtvDescA.setText(descA);
  173.         holder.mtvBunit.setText(bUnit);
  174.         holder.mQty.setText(quantity);
  175.  
  176.         if (!orderlist.get(position).getKotTime().equals("not set")) {
  177.  
  178.             holder.dragVerticalImage.setVisibility(View.INVISIBLE);
  179.             holder.dragVerticalImage.setEnabled(false);
  180.             holder.itemView.setBackgroundResource(R.color.custom_edit);
  181.             // count++;
  182.  
  183.             MainActivity.lv.setDragListCallback(new DragListView.DragListCallbackAdapter() {
  184.                 @Override
  185.                 public boolean canDragItemAtPosition(int dragPosition) {
  186.                     // Can not drag item at position 5
  187.                     return dragPosition > position;
  188.                 }
  189.  
  190.                 @Override
  191.                 public boolean canDropItemAtPosition(int dropPosition) {
  192.                     // Can not drop item at position 2
  193.                     return dropPosition > position;
  194.                 }
  195.             });
  196.  
  197.  
  198.         }
  199.  
  200.  
  201. //            ListViewCustomAdapter listViewCustomAdapter = new ListViewCustomAdapter(context, jsonArray);
  202. //            listViewCustomAdapter.swapItems(position, position);
  203.  
  204.  
  205.         holder.itemView.setTag(orderlist.get(position));
  206.     }
  207.  
  208.     @Override
  209.     public long getItemId(int position) {
  210.         return orderlist.get(position).hashCode();
  211.     }
  212.  
  213.     class ViewHolder extends DragItemAdapter.ViewHolder {
  214.         TextView mtvDescA;
  215.         TextView mtvBunit;
  216.         TextView mQty;
  217.         ImageView dragVerticalImage;
  218.  
  219.         ViewHolder(final View itemView) {
  220.             super(itemView, mGrabHandleId, mDragOnLongPress);
  221.             dragVerticalImage = (ImageView) itemView.findViewById(R.id.drag_vertical_image_view);
  222.             mtvDescA = (TextView) itemView.findViewById(R.id.tvDescA);
  223.             mtvBunit = (TextView) itemView.findViewById(R.id.tvBunit);
  224.             mQty = (TextView) itemView.findViewById(R.id.tvQty);
  225.         }
  226.  
  227.         @Override
  228.         public void onItemClicked(View view) {
  229.             //Toast.makeText(view.getContext(), "Item clicked", Toast.LENGTH_SHORT).show();
  230.         }
  231.  
  232.         @Override
  233.         public boolean onItemLongClicked(View view) {
  234.             //Toast.mak=eText(view.getContext(), "item clicked", Toast.LENGTH_SHORT).show();
  235.             pos = getAdapterPosition();
  236.             Log.i("Position", String.valueOf(pos));
  237.             try { // edited by Diren
  238.                 if (GridCustomAdapter.listItemsObj.jsonArrayList.getJSONObject(pos).getInt("QUANTITY") <= 0)
  239.                     return false;
  240.             } catch (JSONException e) {
  241.                 e.printStackTrace();
  242.             }
  243.             try {
  244.                 mcode = GridCustomAdapter.listItemsObj.jsonArrayList.getJSONObject(pos).getString("MCODE");
  245.             } catch (JSONException e) {
  246.                 e.printStackTrace();
  247.             }
  248.             mcase = 0;
  249.             new AsyncCall().execute();
  250.  
  251.             return true;
  252.         }
  253.     }
  254.  
  255.  
  256. //        @Override
  257. //        public View getView(final int position, View convertView, ViewGroup parent) {
  258. //
  259. //            LayoutInflater inflater = (LayoutInflater) context
  260. //                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  261. //            rowView = inflater.inflate(R.layout.listlayout, parent, false);
  262. //
  263. //            tvDescA = (TextView) rowView.findViewById(R.id.tvDescA);
  264. //            tvCount = (TextView) rowView.findViewById(R.id.tvQty);
  265. //            tvBaseUnit = (TextView) rowView.findViewById(R.id.tvBunit);
  266. //            tvDescA.setText(listItem.get(position));
  267. //            tvDescA.setSelected(true);
  268. //            tvBaseUnit.setText(listBaseUnit.get(position));
  269. //            tvCount.setText("" + listCount.get(position));
  270. //
  271. //            rowView.setOnLongClickListener(new OnLongClickListener() {
  272. //
  273. //                @Override
  274. //                public boolean onLongClick(View arg0) {
  275. //                    // TODO Auto-generated method stub
  276. //                    pos = position;
  277. //                    try { // edited by Diren
  278. //                        if (GridCustomAdapter.listItemsObj.jsonArrayList.getJSONObject(pos).getInt("QUANTITY") <= 0)
  279. //                            return false;
  280. //                    } catch (JSONException e) {
  281. //                        e.printStackTrace();
  282. //                    }
  283. //                    try {
  284. //                        mcode = GridCustomAdapter.listItemsObj.jsonArrayList.getJSONObject(pos).getString("MCODE");
  285. //                    } catch (JSONException e) {
  286. //                        e.printStackTrace();
  287. //                    }
  288. //                    mcase = 0;
  289. //                    new AsyncCall().execute();
  290. //
  291. //                    return false;
  292. //
  293. //                }
  294. //            });
  295. //
  296. //            return rowView;
  297. //        }
  298.  
  299. //    @Override
  300. //    public int getCount() {
  301. //        int x = 0;
  302. //        try {
  303. //            x = listItem.size();
  304. //        } catch (Exception e) {
  305. //            e.printStackTrace();
  306. //        }
  307. //        return x;
  308. //    }
  309. //
  310. //    @Override
  311. //    public Object getItem(int position) {
  312. //        return null;
  313. //    }
  314.  
  315.  
  316.     class AsyncCall extends AsyncTask<String, Void, Void> {
  317.         @Override
  318.         protected Void doInBackground(String... params) {
  319.  
  320.             switch (mcase) {
  321.                 case 0:
  322.                     ret = "1";
  323.                     //if (listKitchenDispatch.get(pos).equals("0")) {
  324.                     ret = WebService.invoke(MainActivity.tablenumber + "<;>" + listMcode.get(pos) + "<;>" + listKOTTIME.get(pos), "command",
  325.                             "getKitchenDispatch");
  326.                     //}
  327.                     break;
  328.                 case 1:
  329.                     ret = WebService.invoke(mcode, "mcode", "getMultiAltUnit");
  330.                     break;
  331.                 case 2:
  332.                     ret = WebService.invoke3(username, password, "checkAccess");
  333.                     break;
  334.             }
  335.             return null;
  336.         }
  337.  
  338.         @Override
  339.         protected void onPostExecute(Void result) {
  340.             // TODO Auto-generated method stub
  341.             super.onPostExecute(result);
  342.             switch (mcase) {
  343.                 case 0:
  344.                     Log.i("Position", String.valueOf(pos));
  345.  
  346.                     try {
  347.                         isUnknown = jsonArray.getJSONObject(pos).getInt("IsUnknown");
  348.                     } catch (JSONException e) {
  349.                         e.printStackTrace();
  350.                     }
  351.  
  352.  
  353.                     if (getTotQuantity(mcode) == 0) {
  354.                         Crouton.makeText((Activity) context, "Total Remaining Qty : 0", Style.ALERT).show();
  355.                     } else {
  356.                         final Dialog dialog = new Dialog(context);
  357.                         dialog.setContentView(R.layout.listclickview);
  358.                         dialog.setCancelable(false);
  359.                         dialog.setTitle("Edit Order");
  360.                         etDESC = (EditText) dialog.findViewById(R.id.etDESC);
  361.                         etRate = (EditText) dialog.findViewById(R.id.etRate);
  362.                         TextView tvBASEUNIT = (TextView) dialog.findViewById(R.id.tvBASEUNT);
  363.                         final Button btnDecrease = (Button) dialog.findViewById(R.id.btnDecrease);
  364.  
  365.  
  366.                         etDESC.setEnabled(false);
  367.                         etDESC.setFocusableInTouchMode(false);
  368.                         etDESC.setFocusable(false);
  369.                         etDESC.setClickable(false);
  370.                         etDESC.setTextColor(Color.DKGRAY);
  371.  
  372.                         etRate.setEnabled(false);
  373.                         etRate.setFocusableInTouchMode(false);
  374.                         etRate.setFocusable(false);
  375.                         etRate.setClickable(false);
  376.                         etRate.setTextColor(Color.DKGRAY);
  377.  
  378.                         if (isUnknown == 1) {
  379.                             etDESC.setEnabled(true);
  380.                             etDESC.setFocusableInTouchMode(true);
  381.                             etDESC.setFocusable(true);
  382.                             etDESC.setClickable(true);
  383.  
  384.  
  385.                             etRate.setEnabled(true);
  386.                             etRate.setFocusableInTouchMode(true);
  387.                             etRate.setFocusable(true);
  388.                             etRate.setClickable(true);
  389.                         }
  390.  
  391.                         final Button btnAltUnit = (Button) dialog.findViewById(R.id.btnAltUnit);
  392.                         btnAltUnit.setVisibility(View.GONE);
  393.                         final Button btnOk = (Button) dialog.findViewById(R.id.btnOk);
  394.                         Button btnCancel = (Button) dialog.findViewById(R.id.btnCancel);
  395.  
  396.                         //final TextView tvQUANTITY = (TextView) dialog.findViewById(R.id.tvQUANTITY);
  397.                         final EditText etQUANTITY = (EditText) dialog.findViewById(R.id.etQUANTITY);
  398.                         final EditText etRemarks = (EditText) dialog.findViewById(R.id.etRemarks);
  399.                         ImageButton btnRemarks = (ImageButton) dialog.findViewById(R.id.btnSelectRemarks);
  400.  
  401.                         etDESC.setText(listItem.get(pos));
  402.  
  403.                         String strDESC = listItem.get(pos);
  404.  
  405.                         try {
  406.                             JSONObject jObj = jsonArray.getJSONObject(pos);
  407.  
  408.                             etRate.setText(String.valueOf(jObj.getDouble("RATE")));
  409.                         } catch (JSONException e) {
  410.                             e.printStackTrace();
  411.                         }
  412.  
  413.                         tvBASEUNIT.setText(listBaseUnit.get(pos));
  414.  
  415.                         etQUANTITY.setText(String.valueOf(getTotQuantity(mcode)));
  416.                         etQUANTITY.setText(etQUANTITY.getText().toString());
  417.  
  418.  
  419.                         if (getTotQuantity(mcode) <= 1) {
  420.                             btnDecrease.setVisibility(View.GONE);
  421.                         }
  422.                         listKitchenDispatch.set(pos, ret);
  423.                         if (ret.equals("1")) {
  424.                             btnCancel.setVisibility(View.GONE);
  425.                             btnDecrease.setVisibility(View.GONE);
  426.                             btnAltUnit.setVisibility(View.GONE);
  427.                             etRemarks.setVisibility(View.GONE);
  428.  
  429.                         }
  430.  
  431.                         btnAltUnit.setOnClickListener(new OnClickListener() {
  432.  
  433.                             @Override
  434.                             public void onClick(View arg0) {
  435.                                 // TODO Auto-generated method stub
  436.                                 mcase = 1;
  437.                                 new AsyncCall().execute();
  438.                                 dialog.dismiss();
  439.                             }
  440.                         });
  441.                         btnDecrease.setOnClickListener(new OnClickListener() {
  442.  
  443.                             @Override
  444.                             public void onClick(View arg0) {
  445.                                 // TODO Auto-generated method stub
  446.  
  447.                                 if (getTotQuantity(mcode) <= 1) {
  448.                                     btnDecrease.setVisibility(View.GONE);
  449.                                     return;
  450.                                 }
  451.  
  452.                                 int index = getIndex();// edited by Direndra Subba
  453.                                 try {
  454.                                     if (index >= 0 && GridCustomAdapter.listItemsObj.jsonArrayList.getJSONObject(index).getString("KOTTIME").equals("not set")) { // edited by Direndra Subba
  455.                                         JSONObject jsonObject = GridCustomAdapter.listItemsObj.jsonArrayList.getJSONObject(index);
  456.                                         jsonObject.put("QUANTITY", listCount.get(index) - 1); //edited by Direndra Subba
  457.                                     } else {
  458.                                         addCancelOrder(pos, -1);
  459.                                     }
  460.                                 } catch (JSONException e) {
  461.                                     e.printStackTrace();
  462.                                 }
  463.                                 etQUANTITY.setText(String.valueOf(getTotQuantity(mcode)));
  464.                                 etQUANTITY.setText(etQUANTITY.getText().toString());
  465. //                            }
  466.                                 setListViewAdapter();
  467.                             }
  468.                         });
  469.  
  470.                         btnCancel.setOnClickListener(new OnClickListener() {
  471.  
  472.                             @Override
  473.                             public void onClick(View v) {
  474.                                 // TODO Auto-generated method stub
  475.                                 try { //edited by Direndra Subba
  476.                                     if (GridCustomAdapter.listItemsObj.jsonArrayList.getJSONObject(pos).getString("KOTTIME").equals("not set")) {
  477.                                         cancelOrder();
  478.                                     } else {
  479.                                         checkCancelAccess();
  480.                                     }
  481.                                 } catch (JSONException e) {
  482.                                     e.printStackTrace();
  483.                                 }
  484.                                 dialog.dismiss();
  485.                             }
  486.                         });
  487.  
  488.                         btnRemarks.setOnClickListener(new OnClickListener() {
  489.                             @Override
  490.                             public void onClick(View v) {
  491.                                 ImsRmsDao db = new ImsRmsDao(context);
  492.                                 final Dialog listDialog = new Dialog(context);
  493.                                 listDialog.setContentView(R.layout.dialoglist);
  494.  
  495.                                 listDialog.setTitle("Remarks");
  496.                                 listRemarks = db.listRemarks();
  497.                                 lvList = (ListView) listDialog.findViewById(R.id.lvList);
  498.  
  499.                                 ArrayAdapter<String> dialogAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, listRemarks);
  500.  
  501.                                 lvList.setAdapter(dialogAdapter);
  502.  
  503.                                 lvList.setOnItemClickListener(new OnItemClickListener() {
  504.  
  505.                                     @Override
  506.                                     public void onItemClick(AdapterView<?> parent,
  507.                                                             View view, int pos, long arg3) {
  508.                                         // TODO Auto-generated method stub
  509.                                         etRemarks.setText(parent.getItemAtPosition(pos).toString());
  510.                                         listDialog.dismiss();
  511.                                     }
  512.                                 });
  513.                                 listDialog.show();
  514.  
  515.                             }
  516.                         });
  517.  
  518.                         etRemarks.setOnLongClickListener(new OnLongClickListener() {
  519.  
  520.                             @Override
  521.                             public boolean onLongClick(View v) {
  522.                                 // TODO Auto-generated method stub
  523.                                 ImsRmsDao db = new ImsRmsDao(context);
  524.                                 final Dialog listDialog = new Dialog(context);
  525.                                 listDialog.setContentView(R.layout.dialoglist);
  526.  
  527.                                 listDialog.setTitle("Remarks");
  528.                                 listRemarks = db.listRemarks();
  529.                                 lvList = (ListView) listDialog.findViewById(R.id.lvList);
  530.  
  531.                                 ArrayAdapter<String> dialogAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, listRemarks);
  532.  
  533.                                 lvList.setAdapter(dialogAdapter);
  534.  
  535.                                 lvList.setOnItemClickListener(new OnItemClickListener() {
  536.  
  537.                                     @Override
  538.                                     public void onItemClick(AdapterView<?> parent,
  539.                                                             View view, int pos, long arg3) {
  540.                                         // TODO Auto-generated method stub
  541.                                         etRemarks.setText(parent.getItemAtPosition(pos).toString());
  542.                                         listDialog.dismiss();
  543.                                     }
  544.                                 });
  545.                                 listDialog.show();
  546.  
  547.                                 return false;
  548.                             }
  549.                         });
  550.  
  551.                         btnOk.setOnClickListener(new OnClickListener() {
  552.  
  553.                             @Override
  554.                             public void onClick(View v) {
  555.                                 // TODO Auto-generated method stub
  556.  
  557.                                 Log.e("pos", pos + "");
  558.                                 try {
  559.                                     JSONObject jsonObject = GridCustomAdapter.listItemsObj.jsonArrayList.getJSONObject(pos);
  560.                                     if (!etRemarks.getText().toString().equals("")) {
  561.                                         jsonObject.put("REMARKS", etRemarks.getText().toString()); //edited by Direndra Subba
  562.                                     }
  563.                                     if (jsonObject.getInt("IsUnknown") == 1) {
  564.                                         jsonObject.put("DESCA", etDESC.getText().toString());
  565.                                         jsonObject.put("RATE", etRate.getText().toString());
  566.                                         MainActivity.lv.setAdapter(new ListViewCustomAdapter(context, GridCustomAdapter.listItemsObj.jsonArrayList), true);
  567.                                     }
  568.                                     if (!etQUANTITY.getText().toString().equals("")) {
  569.                                         String strQuantity = etQUANTITY.getText().toString();
  570.                                         Log.i("quantity", strQuantity);
  571.                                         jsonObject.put("QUANTITY", etQUANTITY.getText().toString()); //edited by Direndra Subba
  572.                                     }
  573.  
  574.                                 } catch (JSONException e) {
  575.                                     e.printStackTrace();
  576.                                 }
  577.  
  578.                                 for (int i = 0; i < GridCustomAdapter.listItemsObj.jsonArrayList.length(); i++) { // edited by Direndra Subba
  579.                                     try {
  580.                                         if (GridCustomAdapter.listItemsObj.jsonArrayList.getJSONObject(i).getInt("QUANTITY") == 0)
  581.                                             removeOrder(i);
  582.                                     } catch (JSONException e) {
  583.                                         e.printStackTrace();
  584.                                     }
  585.                                 }
  586.                                 setListViewAdapter();
  587.                                 dialog.dismiss();
  588.                             }
  589.                         });
  590.                         //   try{
  591.  
  592.                         dialog.show();
  593. //                        }catch (Exception e){
  594. //                            e.printStackTrace();
  595. //                        }
  596.  
  597.                     }
  598.                     break;
  599.                 case 1:
  600.                     if (ret.equals("no")) {
  601.                         Toast.makeText(context, "Error in loading unit", Toast.LENGTH_SHORT).show();
  602.                     } else {
  603.                         Log.e("unit", ret);
  604.                         tmp = ret.split("<;>");
  605.                         if (tmp.length > 0) {
  606.                             try {
  607.                                 altUnit = tmp[0].split("<>");
  608.                                 confactor = tmp[1].split("<>");
  609.                                 rate = tmp[2].split("<>");
  610.                                 loadAltUnit();
  611.                             } catch (Exception e) {
  612.                                 // TODO: handle exception
  613.                                 Toast.makeText(context, "Unit can't be changed", Toast.LENGTH_SHORT).show();
  614.                             }
  615.                         } else {
  616.                             Toast.makeText(context, "No alternate unit available", Toast.LENGTH_SHORT).show();
  617.                         }
  618.                     }
  619.                     break;
  620.                 case 2:
  621.                     if (ret.equals("1")) {
  622.                         cancelOrder();
  623.                     } else {
  624.                         Toast.makeText(context, "You do not have access to cancel order.", Toast.LENGTH_SHORT).show();
  625.                     }
  626.                     break;
  627.  
  628.                 default:
  629.                     break;
  630.             }
  631.         }
  632.     }
  633.  
  634.     public void setListViewAdapter() {
  635. //        String strValue = etDESC.getText().toString();
  636. //
  637. //        if (isUnknown ==0) {
  638.         MainActivity.lv.setAdapter(new ListViewCustomAdapter(context, GridCustomAdapter.listItemsObj.jsonArrayList), true);
  639. //        }
  640. //        else{
  641. //            MainActivity.lv.setAdapter(new ListViewAdapter(context,strValue));
  642. //        }
  643.     }
  644.  
  645.     private int getTotQuantity(String mcode) {
  646.         int oldQuantity = 0, newQuantity = 0;
  647.         for (int i = 0; i < GridCustomAdapter.listItemsObj.jsonArrayList.length(); i++) {
  648.             try {
  649.                 JSONObject jsonObject = GridCustomAdapter.listItemsObj.jsonArrayList.getJSONObject(i);
  650.                 if (jsonObject.getString("MCODE").equals(mcode)) {
  651.                     if (jsonObject.getString("KOTTIME").equals("not set"))
  652.                         newQuantity += jsonObject.getInt("QUANTITY");
  653.                     else
  654.                         oldQuantity += jsonObject.getInt("QUANTITY");
  655.                 }
  656.             } catch (JSONException e) {
  657.                 e.printStackTrace();
  658.             }
  659.         } // edited by Direndra Subba
  660.         return oldQuantity + newQuantity;
  661.     }
  662.  
  663.     private void removeOrder(int pos) {
  664.         GridCustomAdapter.listItemsObj.removeOrderListItems(pos); // edited by Direndra Subba
  665.     }
  666.  
  667.     private void addCancelOrder(int pos, int qty) {
  668.         GridCustomAdapter.listItemsObj.addCancelOrderListItems(pos, qty); //edited by Direndra Subba
  669.     }
  670.  
  671.     private void addChangeUnitOrder(int pos, String unit) {
  672.         GridCustomAdapter.listItemsObj.addChangeUnitOrderListItems(pos, unit);
  673.  
  674.     }
  675.  
  676.     private int getCancellableQty(int pos) {
  677.         int x = 0;
  678.         try {
  679.             if (getTotQuantity(mcode) > GridCustomAdapter.listItemsObj.jsonArrayList.getJSONObject(pos).getInt("QUANTITY"))
  680.                 x = GridCustomAdapter.listItemsObj.jsonArrayList.getJSONObject(pos).getInt("QUANTITY");
  681.             else
  682.                 x = getTotQuantity(mcode);
  683.         } catch (JSONException e) {
  684.             e.printStackTrace();
  685.         }
  686.         return x;
  687.     }
  688.  
  689.  
  690.     public void cancelOrder() {
  691.  
  692.         new AlertDialog.Builder(context)
  693.                 .setTitle("Cancel order?")
  694.                 .setMessage("Are you sure you want to cancel this order?")
  695.                 .setPositiveButton(android.R.string.yes,
  696.                         new DialogInterface.OnClickListener() {
  697.                             public void onClick(DialogInterface dialog,
  698.                                                 int which) {
  699.                                 try {
  700.                                     if (GridCustomAdapter.listItemsObj.jsonArrayList.getJSONObject(pos).getString("KOTTIME").equals("not set")) { // edited by Direndra Subba
  701.                                         removeOrder(pos);
  702.                                     } else {
  703.                                         int index = getIndex(); // edited by Direndra Subba
  704.                                         if (index > 0 && GridCustomAdapter.listItemsObj.jsonArrayList.getJSONObject(index).getString("KOTTIME").equals("not set")) { // edited by Direndra Subba
  705.                                             JSONObject jsonObject = GridCustomAdapter.listItemsObj.jsonArrayList.getJSONObject(index);
  706.                                             jsonObject.put("QUANTITY", listCount.get(index) - getCancellableQty(pos)); //edited by Direndra Subba
  707.                                         } else {
  708.                                             addCancelOrder(pos, 0 - getCancellableQty(pos));
  709.                                         }
  710.                                     }
  711.                                 } catch (JSONException e) {
  712.                                     e.printStackTrace();
  713.                                 }
  714.                                 setListViewAdapter();
  715.                             }
  716.                         }
  717.  
  718.                 )
  719.                 .setNegativeButton(android.R.string.no,
  720.                         new DialogInterface.OnClickListener() {
  721.                             public void onClick(DialogInterface dialog,
  722.                                                 int which) {
  723.                             }
  724.                         }
  725.                 ).setIcon(android.R.drawable.btn_star).show();
  726.     }
  727.  
  728.     public void checkCancelAccess() {
  729.         final Dialog caDialog = new Dialog(context);
  730.         caDialog.setContentView(R.layout.activity_user_login);
  731.         caDialog.setTitle("Log In");
  732.         final EditText etUserName = (EditText) caDialog.findViewById(R.id.etUserName);
  733.         final EditText etPassword = (EditText) caDialog.findViewById(R.id.etPassword);
  734.         LinearLayout llIp = (LinearLayout) caDialog.findViewById(R.id.llIp);
  735.         llIp.setVisibility(View.GONE);
  736.         Button btnLogin = (Button) caDialog.findViewById(R.id.btnLogin);
  737.         btnLogin.setOnClickListener(new OnClickListener() {
  738.  
  739.             @Override
  740.             public void onClick(View arg0) {
  741.                 // TODO Auto-generated method stub
  742.                 username = etUserName.getText().toString();
  743.                 password = etPassword.getText().toString();
  744.                 if (username.equals("") || password.equals("")) {
  745.                     Toast.makeText(context, "Username or Password can't be empty.", Toast.LENGTH_SHORT).show();
  746.                 } else {
  747.                     mcase = 2;
  748.                     new AsyncCall().execute();
  749.                 }
  750.                 caDialog.dismiss();
  751.             }
  752.         });
  753.         caDialog.show();
  754.     }
  755.  
  756.     public void loadAltUnit() {
  757.         final Dialog altDialog = new Dialog(context);
  758.         altDialog.setContentView(R.layout.dialoglist);
  759.         altDialog.setTitle("Alternate Unit");
  760.         ListView lvList = (ListView) altDialog.findViewById(R.id.lvList);
  761.         lvList.setAdapter(new NavListAdapter(context, altUnit, rate));
  762.         lvList.setOnItemClickListener(new OnItemClickListener() {
  763.  
  764.                                           @Override
  765.                                           public void onItemClick(AdapterView<?> parent, View view,
  766.                                                                   int position, long arg3) {
  767.                                               // TODO Auto-generated method stub
  768.                                               try {
  769.                                                   JSONObject jsonObject = GridCustomAdapter.listItemsObj.jsonArrayList.getJSONObject(pos);
  770.                                                   if (jsonObject.getString("KOTTIME").equals("not set")) { // edited by Direndra Subba
  771.                                                       jsonObject.put("BASEUNIT", altUnit[position]); //edited by Direndra Subba
  772.                                                   } else {
  773.                                                       try {
  774.                                                           addCancelOrder(pos, 0 - jsonObject.getInt("QUANTITY"));// edited by Direndra Subba
  775.                                                       } catch (JSONException e) {
  776.                                                           e.printStackTrace();
  777.                                                       }
  778.                                                       addChangeUnitOrder(pos, altUnit[position]);
  779.                                                   }
  780.                                               } catch (JSONException e) {
  781.                                                   e.printStackTrace();
  782.                                               }
  783.                                               MainActivity.lv.setAdapter(new ListViewCustomAdapter(context, GridCustomAdapter.listItemsObj.jsonArrayList), true); // edited by Direndra Subba
  784.                                               altDialog.dismiss();
  785.                                           }
  786.                                       }
  787.  
  788.         );
  789.         altDialog.show();
  790.     }
  791.  
  792.     public int getIndex() { // edited by Direndra Subba
  793.         int index = 0;
  794.         breakLoop:
  795.         for (int i = 0; i < jsonArray.length(); i++) {
  796.             try {
  797.                 if (jsonArray.getJSONObject(i).getString("MCODE").equals(mcode)) {
  798.                     index = i;
  799.                     break breakLoop;
  800.                 }
  801.             } catch (JSONException e) {
  802.                 e.printStackTrace();
  803.             }
  804.         }
  805.         return index;
  806.     }
  807. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement