-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrderEditorFragment.java
More file actions
1704 lines (1441 loc) · 79.4 KB
/
OrderEditorFragment.java
File metadata and controls
1704 lines (1441 loc) · 79.4 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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package com.wosaku.android.ordercontrol;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.CursorLoader;
import android.content.DialogInterface;
import android.content.Loader;
import android.database.Cursor;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import com.wosaku.android.ordercontrol.data.OrdersContract;
import com.wosaku.android.ordercontrol.my_util.FileUtils;
import java.io.File;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
/**
* Created by Wilian on 2016-12-12.
* Order editor
*/
public class OrderEditorFragment extends Fragment implements android.app.LoaderManager.LoaderCallbacks<Cursor>, AdapterView.OnItemSelectedListener {
public final static String SUPPLIER_SEPARATOR = " | ";
private final static int PRODUCT_ITEM_ID_INITIAL = 0;
private final static int PRODUCT_ITEM_ADD = 10;
private final static int PRODUCT_ITEM_NUMBER_IDS = 1;
private final static int PRODUCT_SPINNER_IDS = 2;
private final static int PRODUCT_PRICE_IDS = 3;
private final static int PRODUCT_QUANTITY_IDS = 4;
private final static int PRODUCT_TOTALS_IDS = 5;
private final static int PRODUCT_ID = 6;
private final static int PRODUCT_ORDER_ID = 7;
private final static String TIME_SUFFIX = " 23:59:59";
private final static int PDF_TOTAL_QUANTITY_ID = 1234;
private final static int PDF_TOTAL_PRICE_ID = 5678;
private final static int ORDER_LOADER = 0;
public ImageView mImageView;
private final ArrayList<Long> productOrdersRemoved = new ArrayList<>();
private ArrayAdapter<String> supplierArrayAdapter;
private boolean addProductClicked = false;
private boolean orientationChanged = false;
private boolean replicateOrderSelector = false;
private final Calendar myCalendar = Calendar.getInstance();
private Cursor deliveryList;
private Cursor orderProductCursor;
private Cursor paymentList;
private Cursor productList;
private final DateFormat dateFormat = DateFormat.getDateInstance();
private double productCostTotalDouble;
private EditText commentsEditText;
private EditText deliveryDateEditText;
private EditText orderDateEditText;
private EditText paymentDateEditText;
private TextView totalPriceEditText;
private TextView totalQuantityEditText;
private long productCostInt;
private long productCostTotalInt;
private int productCount = 1;
private int productItemId = 0;
private long productQuantityInt;
private int productSaved;
private long totalQuantity = 0;
private LinearLayout btCallSupplier;
private LinearLayout btSendEmail;
private LinearLayout productsContainer;
private Long loadedOrderId;
private long newOrderId;
private Long orderProductId;
private final NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance();
private final NumberFormat numberFormatter = NumberFormat.getNumberInstance();
private OnCreateNewProductSelectedListener mCallback;
private SimpleCursorAdapter spinnerDeliveryAdapter;
private SimpleCursorAdapter spinnerPaymentAdapter;
private SimpleCursorAdapter spinnerProductAdapter;
private final SimpleDateFormat dateFormatSql = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
private Spinner spinnerDeliveryStatus;
private Spinner spinnerPaymentStatus;
private Spinner spinnerProductItem;
private Spinner spinnerSupplier;
private String commentsString;
private String deliveryDateString;
private String mCurrentImagePath = null;
private String orderDateString;
private String paymentDateString;
private String selectedDelivery;
private String selectedPayment;
private String selectedSupplier;
private String selectedSupplierWithName;
private String supplierEmail;
private String supplierPhone;
private Uri currentOrderUri;
private TextView pdfOrderNumberTv;
private TextView pdfSupplierNameTv;
private TextView pdfOrderDateTv;
private TextView pdfDeliveryDateTv;
private TextView pdfPaymentDateTv;
private TextView pdfComments;
private String pdfSupName;
private TableLayout pdfTable;
private int pdfProductItemId = 1000;
private String pdfOrdNum;
public View pdfContent;
private boolean isNewProduct = true;
private int newProductItemCount = 0;
// Check if a field was touched
private final View.OnTouchListener mTouchListener = new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
HomeActivity.mFragmentEditorHasChanged = true;
return false;
}
};
public OrderEditorFragment() {
// required constructor
}
@TargetApi(23)
@Override public void onAttach(Context context) {
super.onAttach(context);
try {
mCallback = (OnCreateNewProductSelectedListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString()
+ " must implement OnCreateNewProductSelectedListener");
}
}
/*
* Deprecated on API 23
* Use onAttachToContext instead
*/
@SuppressWarnings("deprecation")
@Override public void onAttach(Activity activity) {
super.onAttach(activity);
if (Build.VERSION.SDK_INT < 23) {
try {
mCallback = (OnCreateNewProductSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnCreateNewProductSelectedListener");
}
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_order_editor, container, false);
// Find all elements
orderDateEditText = (EditText) rootView.findViewById(R.id.et_order_date);
spinnerSupplier = (Spinner) rootView.findViewById(R.id.spinner_order_supplier);
productsContainer = (LinearLayout) rootView.findViewById(R.id.ll_products_container);
Button btAddProduct = (Button) rootView.findViewById(R.id.bt_addItem);
Button newProduct = (Button) rootView.findViewById(R.id.bt_addNewProduct);
deliveryDateEditText = (EditText) rootView.findViewById(R.id.et_order_delivery_date);
paymentDateEditText = (EditText) rootView.findViewById(R.id.et_order_payment_date);
commentsEditText = (EditText) rootView.findViewById(R.id.et_order_comments);
spinnerDeliveryStatus = (Spinner) rootView.findViewById(R.id.spinner_delivery_status);
spinnerPaymentStatus = (Spinner) rootView.findViewById(R.id.spinner_payment_status);
totalPriceEditText = (TextView) rootView.findViewById(R.id.et_total_price);
totalQuantityEditText = (TextView) rootView.findViewById(R.id.et_total_quantity);
mImageView = (ImageView) rootView.findViewById(R.id.order_image);
Button btAddImage = (Button) rootView.findViewById(R.id.bt_addImage);
Button btRemoveImage = (Button) rootView.findViewById(R.id.bt_removeImage);
ImageView clearDeliveryDate = (ImageView) rootView.findViewById(R.id.delivery_date_clear);
ImageView clearPaymentDate = (ImageView) rootView.findViewById(R.id.payment_date_clear);
btCallSupplier = (LinearLayout) rootView.findViewById(R.id.bt_call_supplier);
btSendEmail = (LinearLayout) rootView.findViewById(R.id.bt_sendEmail);
// Views for print
LinearLayout printPdfBt = (LinearLayout) rootView.findViewById(R.id.bt_printPdf);
pdfSupplierNameTv = (TextView) rootView.findViewById(R.id.pdf_supplier_name);
pdfOrderNumberTv = (TextView) rootView.findViewById(R.id.pdf_order_number);
pdfOrderDateTv = (TextView) rootView.findViewById(R.id.pdf_order_date);
pdfDeliveryDateTv = (TextView) rootView.findViewById(R.id.pdf_delivery_date);
pdfPaymentDateTv = (TextView) rootView.findViewById(R.id.pdf_payment_date);
pdfComments = (TextView) rootView.findViewById(R.id.pdf_comments);
pdfTable = (TableLayout) rootView.findViewById(R.id.product_table);
pdfContent = rootView.findViewById(R.id.pdf_container);
// Get args from ProductCatalogFragment
Bundle bundle = getArguments();
if (bundle != null) {
currentOrderUri = Uri.parse(getArguments().getString("order_id"));
loadedOrderId = ContentUris.parseId(currentOrderUri);
loadOrderProductCursor();
isNewProduct = false;
}
// Check if the Activity started from Main (edit product) or from add button (new product)
if (currentOrderUri == null) {
getActivity().setTitle(getString(R.string.order_editor_title_new_product));
printPdfBt.setVisibility(View.GONE);
} else {
getActivity().setTitle(getString(R.string.order_editor_title_edit_product));
if (savedInstanceState == null) {
getLoaderManager().initLoader(ORDER_LOADER, null, this);
}
}
// Check if PDF should be enable
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
printPdfBt.setVisibility(View.GONE);
pdfContent.setVisibility(View.GONE);
}
// Loading spinners data from database
loadSpinnerSupplierData();
loadSpinnerPayment();
loadSpinnerDelivery();
// Set order date to current date
dateFormatSql.setTimeZone(TimeZone.getDefault());
orderDateEditText.setText(dateFormat.format(myCalendar.getTime()));
orderDateString = dateFormatSql.format(myCalendar.getTime()) + TIME_SUFFIX;
// Set spinners listeners
spinnerSupplier.setOnItemSelectedListener(this);
spinnerPaymentStatus.setOnItemSelectedListener(this);
spinnerDeliveryStatus.setOnItemSelectedListener(this);
// Set click listeners
btAddImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
HomeActivity.addImageRequest = HomeActivity.ACTION_REQUEST_IMAGE_ORDER;
((HomeActivity) getActivity()).checkImagePermission();
}
});
mImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (mImageView.getDrawable() != null) {
FileUtils.openImageInGallery(mImageView, mCurrentImagePath, getActivity());
}
}
});
btRemoveImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
removeImage();
}
});
btAddProduct.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
newProductItemCount = newProductItemCount + 1;
addProductClicked = true;
loadProductData();
}
});
clearDeliveryDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
deliveryDateEditText.getText().clear();
deliveryDateString = null;
}
});
clearPaymentDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
paymentDateEditText.getText().clear();
paymentDateString = null;
}
});
btCallSupplier.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FileUtils.callSupplier(supplierPhone, getActivity());
}
});
btSendEmail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FileUtils.sendEmail(supplierEmail, getActivity());
}
});
newProduct.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(getString(R.string.save_order));
builder.setPositiveButton(getString(R.string.action_save), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
saveOrder();
mCallback.onNewProductSelected(selectedSupplierWithName, newOrderId);
}
});
builder.setNegativeButton(getString(R.string.do_not_save), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
if (dialog != null) {
dialog.dismiss();
mCallback.onNewProductSelected(selectedSupplierWithName, newOrderId);
}
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
});
printPdfBt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
HomeActivity.orderNumToPdf = pdfOrdNum;
pdfLoadProductItems();
((HomeActivity) getActivity()).checkPdfPermission();
}
});
final DatePickerDialog.OnDateSetListener orderDateDialog, deliveryDateDialog, paymentDateDialog;
orderDateDialog = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
orderDateEditText.setText(dateFormat.format(myCalendar.getTime()));
orderDateString = dateFormatSql.format(myCalendar.getTime()) + TIME_SUFFIX;
}
};
deliveryDateDialog = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
deliveryDateEditText.setText(dateFormat.format(myCalendar.getTime()));
deliveryDateString = dateFormatSql.format(myCalendar.getTime()) + TIME_SUFFIX;
}
};
paymentDateDialog = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
paymentDateEditText.setText(dateFormat.format(myCalendar.getTime()));
paymentDateString = dateFormatSql.format(myCalendar.getTime()) + TIME_SUFFIX;
}
};
orderDateEditText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new DatePickerDialog(getActivity(), orderDateDialog, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
});
deliveryDateEditText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new DatePickerDialog(getActivity(), deliveryDateDialog, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
});
paymentDateEditText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new DatePickerDialog(getActivity(), paymentDateDialog, myCalendar
.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
});
// Set Touch listener to watch for possible edits and ask for confirmation when pressing back button
orderDateEditText.setOnTouchListener(mTouchListener);
spinnerSupplier.setOnTouchListener(mTouchListener);
btAddProduct.setOnTouchListener(mTouchListener);
deliveryDateEditText.setOnTouchListener(mTouchListener);
spinnerDeliveryStatus.setOnTouchListener(mTouchListener);
paymentDateEditText.setOnTouchListener(mTouchListener);
spinnerPaymentStatus.setOnTouchListener(mTouchListener);
commentsEditText.setOnTouchListener(mTouchListener);
btAddImage.setOnTouchListener(mTouchListener);
btRemoveImage.setOnTouchListener(mTouchListener);
// Indicate that has options
setHasOptionsMenu(true);
// Set drawer indicator
HomeActivity.mDrawerToggle.setDrawerIndicatorEnabled(false);
return rootView;
}
private void loadOrderProductCursor(){
String[] projection = {
OrdersContract.OrderProductsEntry._ID,
OrdersContract.OrderProductsEntry.COLUMN_ORDERPRODUCTS_ORD_ID,
OrdersContract.OrderProductsEntry.COLUMN_ORDERPRODUCTS_ORD_ITEM_NUMBER,
OrdersContract.OrderProductsEntry.COLUMN_ORDERPRODUCTS_PRODUCT_ID,
OrdersContract.OrderProductsEntry.COLUMN_ORDERPRODUCTS_PRODUCT_PRICE,
OrdersContract.OrderProductsEntry.COLUMN_ORDERPRODUCTS_PRODUCT_QUANTITY
};
String whereClause = OrdersContract.OrderProductsEntry.COLUMN_ORDERPRODUCTS_ORD_ID + "=?";
String[] whereArgs = {loadedOrderId.toString()};
orderProductCursor = getActivity().getContentResolver().query(OrdersContract.OrderProductsEntry.CONTENT_URI, projection, whereClause, whereArgs, null);
if (orderProductCursor != null && orderProductCursor.moveToFirst()) {
productSaved = orderProductCursor.getCount();
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// Bundle handle automatically all text but not image
mImageView = (ImageView) getActivity().findViewById(R.id.order_image);
if (mImageView.getDrawable() != null && mImageView.getTag() != null) {
mCurrentImagePath = mImageView.getTag().toString();
}
String savedImage = mCurrentImagePath;
outState.putString("savedImage", savedImage);
boolean savedMFragmentEditorHasChanged = HomeActivity.mFragmentEditorHasChanged;
outState.putBoolean("savedMFragmentEditorHasChanged", savedMFragmentEditorHasChanged);
// Used to save call supplier and send email visibility status
int callSupplierVisible = btCallSupplier.getVisibility();
outState.putInt("callSupplierVisible", callSupplierVisible);
int sendEmailVisible = btSendEmail.getVisibility();
outState.putInt("sendEmailVisible", sendEmailVisible);
// Used to reload items
String savedSupplier = selectedSupplier;
outState.putString("savedSupplier", savedSupplier);
String savedOrderNumber = pdfOrdNum;
outState.putString("savedOrderNumber", savedOrderNumber);
String savedSupplierName = pdfSupName;
outState.putString("savedSupplierName", savedSupplierName);
outState.putBoolean("orientationChanged", true);
outState.putBoolean("isNewProduct", isNewProduct);
outState.putInt("newProductItemCount", newProductItemCount);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState != null) {
String savedImage = savedInstanceState.getString("savedImage");
if (savedImage != null) {
mImageView.setImageDrawable(new BitmapDrawable(getResources(),
FileUtils.getResizedBitmap(savedImage, 512, 512)));
mImageView.setTag(savedImage);
}
HomeActivity.mFragmentEditorHasChanged = savedInstanceState.getBoolean("savedMFragmentEditorHasChanged");
// Reshow call supplier and send email
int callSupplierVisible = savedInstanceState.getInt("callSupplierVisible");
switch (callSupplierVisible) {
case View.VISIBLE:
btCallSupplier.setVisibility(View.VISIBLE);
break;
case View.GONE:
btCallSupplier.setVisibility(View.GONE);
break;
}
int sendEmailVisible = savedInstanceState.getInt("sendEmailVisible");
switch (sendEmailVisible) {
case View.VISIBLE:
btSendEmail.setVisibility(View.VISIBLE);
break;
case View.GONE:
btSendEmail.setVisibility(View.GONE);
break;
}
// Reload product items
pdfOrdNum = savedInstanceState.getString("savedOrderNumber");
pdfSupName = savedInstanceState.getString("savedSupplierName");
selectedSupplier = savedInstanceState.getString("savedSupplier");
orientationChanged = savedInstanceState.getBoolean("orientationChanged");
isNewProduct = savedInstanceState.getBoolean("isNewProduct");
newProductItemCount = savedInstanceState.getInt("newProductItemCount");
if (!isNewProduct){
loadOrderProductCursor();
for (int i = 1; i <= productSaved; i++) {
loadProductData();
pdfCreateProductLines();
spinnerProductItem.setEnabled(false);
}
loadProductItem();
pdfCreateTotalLine();
pdfLoadProductItems();
}
if (isNewProduct && newProductItemCount > 0){
for (int i = 1; i <= newProductItemCount; i++) {
loadProductData();
}
}
}
}
@Override
public void onDestroy() {
super.onDestroy();
if (productList != null) {
productList.close();
}
if (paymentList != null) {
paymentList.close();
}
if (deliveryList != null) {
deliveryList.close();
}
if (orderProductCursor != null) {
orderProductCursor.close();
}
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.menu_editor, menu);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_action_delete:
showDeleteConfirmationDialog();
return true;
case R.id.menu_action_replicate:
replicateOrder();
return true;
case R.id.menu_action_save:
saveOrder();
return true;
case android.R.id.home:
getActivity().onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
if (currentOrderUri == null) {
MenuItem actionDelete = menu.findItem(R.id.menu_action_delete);
actionDelete.setVisible(false);
MenuItem actionReplicate = menu.findItem(R.id.menu_action_replicate);
actionReplicate.setVisible(false);
}
}
private void removeImage() {
mCurrentImagePath = null;
mImageView.setTag(null);
mImageView.setImageDrawable(null);
}
private void loadSpinnerSupplierData() {
String[] projection = {
OrdersContract.SupplierEntry._ID,
OrdersContract.SupplierEntry.COLUMN_SUPPLIER_STATUS,
OrdersContract.SupplierEntry.COLUMN_SUPPLIER_NAME
};
String whereClause = OrdersContract.SupplierEntry.COLUMN_SUPPLIER_STATUS + "=?";
String[] whereArgs = {Integer.toString(OrdersContract.SupplierEntry.STATUS_ACTIVE)};
Cursor supplierList;
if (currentOrderUri != null) {
supplierList = getActivity().getContentResolver().query(OrdersContract.SupplierEntry.CONTENT_URI, projection, null, null, null);
} else {
supplierList = getActivity().getContentResolver().query(OrdersContract.SupplierEntry.CONTENT_URI, projection, whereClause, whereArgs, null);
}
final ArrayList<String> supplierIdAndNameArray = new ArrayList<>();
if (supplierList != null && supplierList.moveToFirst()) {
while (!supplierList.isAfterLast()) {
String supId = supplierList.getString(supplierList.getColumnIndexOrThrow(OrdersContract.SupplierEntry._ID));
String supName = supplierList.getString(supplierList.getColumnIndexOrThrow(OrdersContract.SupplierEntry.COLUMN_SUPPLIER_NAME));
String nameAndId = supName + SUPPLIER_SEPARATOR + supId;
supplierIdAndNameArray.add(nameAndId);
supplierList.moveToNext();
}
supplierList.close();
}
Collections.sort(supplierIdAndNameArray, String.CASE_INSENSITIVE_ORDER);
supplierArrayAdapter = new ArrayAdapter<>(getActivity(), R.layout.list_spinner, R.id.tvDBViewRow, supplierIdAndNameArray);
spinnerSupplier.setAdapter(supplierArrayAdapter);
}
private void loadSpinnerPayment() {
String[] projection = {
OrdersContract.PaymentEntry._ID,
OrdersContract.PaymentEntry.COLUMN_PAYMENT_STATUS,
OrdersContract.PaymentEntry.COLUMN_PAYMENT_DELETED
};
String whereClause = OrdersContract.PaymentEntry.COLUMN_PAYMENT_DELETED + "=?";
String[] whereArgs = {Integer.toString(OrdersContract.PaymentEntry.STATUS_ACTIVE)};
paymentList = getActivity().getContentResolver().query(OrdersContract.PaymentEntry.CONTENT_URI, projection, whereClause, whereArgs, null);
String[] from = new String[]{OrdersContract.PaymentEntry.COLUMN_PAYMENT_STATUS};
int[] to = new int[]{R.id.tvDBViewRow};
spinnerPaymentAdapter = new SimpleCursorAdapter(getActivity(), R.layout.list_spinner, paymentList, from, to, 0);
spinnerPaymentStatus.setAdapter(spinnerPaymentAdapter);
}
private void loadSpinnerDelivery() {
String[] projection = {
OrdersContract.DeliveryEntry._ID,
OrdersContract.DeliveryEntry.COLUMN_DELIVERY_STATUS,
OrdersContract.DeliveryEntry.COLUMN_DELIVERY_DELETED
};
String whereClause = OrdersContract.DeliveryEntry.COLUMN_DELIVERY_DELETED + "=?";
String[] whereArgs = {Integer.toString(OrdersContract.DeliveryEntry.STATUS_ACTIVE)};
deliveryList = getActivity().getContentResolver().query(OrdersContract.DeliveryEntry.CONTENT_URI, projection, whereClause, whereArgs, null);
String[] from = new String[]{OrdersContract.DeliveryEntry.COLUMN_DELIVERY_STATUS};
int[] to = new int[]{R.id.tvDBViewRow};
spinnerDeliveryAdapter = new SimpleCursorAdapter(getActivity(), R.layout.list_spinner, deliveryList, from, to, 0);
spinnerDeliveryStatus.setAdapter(spinnerDeliveryAdapter);
}
private void loadProductData() {
spinnerSupplier.setEnabled(false);
String[] projection = {
OrdersContract.ProductEntry.TABLE_NAME + "." + OrdersContract.ProductEntry._ID,
OrdersContract.ProductEntry.COLUMN_PRODUCT_NAME,
OrdersContract.ProductEntry.COLUMN_PRODUCT_STATUS,
OrdersContract.ProductEntry.COLUMN_SUPPLIER_ID,
OrdersContract.ProductEntry.COLUMN_PRODUCT_PRICE
};
String orderBy = OrdersContract.ProductEntry.TABLE_NAME + "." + OrdersContract.ProductEntry.COLUMN_PRODUCT_NAME + " ASC";
if (currentOrderUri != null) {
String whereClause = OrdersContract.ProductEntry.COLUMN_SUPPLIER_ID + "=?";
String[] whereArgs = {selectedSupplier};
productList = getActivity().getContentResolver().query(OrdersContract.ProductEntry.CONTENT_URI, projection, whereClause, whereArgs, orderBy);
}
if (currentOrderUri == null || addProductClicked) {
String whereClause = OrdersContract.ProductEntry.COLUMN_SUPPLIER_ID + "=?" + " AND " + OrdersContract.ProductEntry.COLUMN_PRODUCT_STATUS + "=?";
String[] whereArgs = {selectedSupplier, "1"};
productList = getActivity().getContentResolver().query(OrdersContract.ProductEntry.CONTENT_URI, projection, whereClause, whereArgs, orderBy);
}
if (productList != null && !productList.moveToFirst()) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.no_product_found);
builder.setPositiveButton(R.string.create_product, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
saveOrder();
mCallback.onNewProductSelected(selectedSupplierWithName, newOrderId);
}
});
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
if (dialog != null) {
dialog.dismiss();
}
}
});
// Create and show the AlertDialog
AlertDialog alertDialog = builder.create();
alertDialog.show();
return;
}
createProductLine();
}
private void createProductLine() {
String[] from = new String[]{OrdersContract.ProductEntry.COLUMN_PRODUCT_NAME};
int[] to = new int[]{R.id.tvDBViewRow};
spinnerProductAdapter = new SimpleCursorAdapter(getActivity(), R.layout.list_spinner, productList, from, to, 0);
spinnerProductItem = new Spinner(getActivity());
spinnerProductItem.setOnItemSelectedListener(this);
spinnerProductItem.setAdapter(spinnerProductAdapter);
productItemId = productItemId + PRODUCT_ITEM_ADD;
LinearLayout productItemLine = new LinearLayout(getActivity());
productItemLine.setPadding(8,8,8,8);
productItemLine.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams productItemLineParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
productItemLine.setId(+productItemId);
productItemLine.setLayoutParams(productItemLineParams);
if (productItemId / PRODUCT_ITEM_ADD % 2 == 1) {
productItemLine.setBackgroundResource(R.color.item1);
} else {
productItemLine.setBackgroundResource(R.color.item2);
}
productsContainer.addView(productItemLine);
LinearLayout spinnerLine = new LinearLayout(getActivity());
LinearLayout.LayoutParams spinnerLineParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
spinnerLine.setOrientation(LinearLayout.HORIZONTAL);
spinnerLine.setLayoutParams(spinnerLineParams);
productItemLine.addView(spinnerLine);
LinearLayout itemNumberContainer = new LinearLayout(getActivity());
LinearLayout.LayoutParams itemNumberContainerParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT, 2.0f);
itemNumberContainer.setLayoutParams(itemNumberContainerParams);
spinnerLine.addView(itemNumberContainer);
TextView productItemNumber = new TextView(getActivity());
LinearLayout.LayoutParams productItemNumberParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
productItemNumber.setLines(1);
productItemNumber.setGravity(Gravity.CENTER_VERTICAL);
final float scale = getResources().getDisplayMetrics().density;
int pixels = (int) (getResources().getDimension(R.dimen.item_number_padding_order_editor) * scale + 0.5f);
productItemNumber.setMinWidth(pixels);
productItemNumber.setLayoutParams(productItemNumberParams);
productItemNumber.setText(String.format(Locale.ENGLISH, "%d)", productCount));
productItemNumber.setId(+productItemId + PRODUCT_ITEM_NUMBER_IDS);
productCount++;
itemNumberContainer.addView(productItemNumber);
LinearLayout spinnerContainer = new LinearLayout(getActivity());
LinearLayout.LayoutParams spinnerContainerParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT, 5.0f);
spinnerContainer.setLayoutParams(spinnerContainerParams);
spinnerLine.addView(spinnerContainer);
LinearLayout.LayoutParams spinnerParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
spinnerProductItem.setLayoutParams(spinnerParams);
spinnerContainer.addView(spinnerProductItem);
spinnerProductItem.setId(+productItemId + PRODUCT_SPINNER_IDS);
LinearLayout invisibleContainer = new LinearLayout(getActivity());
LinearLayout.LayoutParams invisibleContainerContainerParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT, 0.5f);
invisibleContainer.setLayoutParams(invisibleContainerContainerParams);
spinnerLine.addView(invisibleContainer);
TextView productIdTextView = new TextView(getActivity());
LinearLayout.LayoutParams invisibleParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
productIdTextView.setTextSize(4);
productIdTextView.setLayoutParams(invisibleParams);
invisibleContainer.addView(productIdTextView);
productIdTextView.setVisibility(View.INVISIBLE);
productIdTextView.setId(+productItemId + PRODUCT_ID);
TextView productOrderIdTextView = new TextView(getActivity());
productOrderIdTextView.setTextSize(4);
productOrderIdTextView.setLayoutParams(invisibleParams);
invisibleContainer.addView(productOrderIdTextView);
productOrderIdTextView.setVisibility(View.INVISIBLE);
productOrderIdTextView.setId(+productItemId + PRODUCT_ORDER_ID);
LinearLayout removeItemContainer = new LinearLayout(getActivity());
LinearLayout.LayoutParams removeItemContainerParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
removeItemContainer.setLayoutParams(removeItemContainerParams);
spinnerLine.addView(removeItemContainer);
final ImageView removeItem = new ImageView(getActivity());
LinearLayout.LayoutParams removeImageParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
removeImageParams.setMargins(10, 4, 10, 4);
removeItem.setLayoutParams(removeImageParams);
removeItem.setImageResource(R.drawable.ic_remove_circle_outline_black_24dp);
removeItemContainer.addView(removeItem);
// Add a new horizontal line, will host price and quantity
final LinearLayout priceLine = new LinearLayout(getActivity());
priceLine.setOrientation(LinearLayout.HORIZONTAL);
priceLine.setLayoutParams(productItemLineParams);
productItemLine.addView(priceLine);
final LinearLayout priceContainer = new LinearLayout(getActivity());
priceContainer.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams labelContainers = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1f);
priceContainer.setLayoutParams(labelContainers);
labelContainers.gravity = Gravity.CENTER_VERTICAL;
labelContainers.setMargins(0, 0, 0, 0);
priceLine.addView(priceContainer);
TextView unitPriceLabel = new TextView(getActivity());
LinearLayout.LayoutParams labelParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
unitPriceLabel.setLayoutParams(labelParams);
unitPriceLabel.setText(getString(R.string.order_editor_product_symbol));
priceContainer.addView(unitPriceLabel);
TextView productPriceTextView = new TextView(getActivity());
ViewGroup.LayoutParams productCostParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
productPriceTextView.setLayoutParams(productCostParams);
productPriceTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.font_size_for_order_editor));
productPriceTextView.setPadding(0,0,0,20);
priceContainer.addView(productPriceTextView);
productPriceTextView.setId(+productItemId + PRODUCT_PRICE_IDS);
final LinearLayout quantityContainer = new LinearLayout(getActivity());
LinearLayout.LayoutParams quantityContainerParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 0.5f);
quantityContainer.setOrientation(LinearLayout.VERTICAL);
quantityContainer.setLayoutParams(quantityContainerParams);
priceLine.addView(quantityContainer);
TextView qtyLabel = new TextView(getActivity());
labelParams.gravity = Gravity.CENTER_VERTICAL;
qtyLabel.setLayoutParams(labelParams);
qtyLabel.setText(getString(R.string.order_editor_quantity_symbol));
quantityContainer.addView(qtyLabel);
EditText productQuantityEditText = new EditText(getActivity());
ViewGroup.LayoutParams productQuantityTextViewParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
productQuantityEditText.setLayoutParams(productQuantityTextViewParams);
productQuantityEditText.setInputType(InputType.TYPE_CLASS_NUMBER);
productQuantityEditText.setPadding(0,0,0,20);
productQuantityEditText.setText("1");
productQuantityEditText.setGravity(Gravity.CENTER);
productQuantityEditText.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.font_size_for_order_editor));
quantityContainer.addView(productQuantityEditText);
productQuantityEditText.setId(+productItemId + PRODUCT_QUANTITY_IDS);
LinearLayout totalContainer = new LinearLayout(getActivity());
LinearLayout.LayoutParams totalContainerParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1f);
totalContainer.setOrientation(LinearLayout.VERTICAL);
totalContainer.setLayoutParams(totalContainerParams);
priceLine.addView(totalContainer);
TextView totalLabel = new TextView(getActivity());
totalLabel.setLayoutParams(labelParams);
totalLabel.setText(getString(R.string.order_editor_total_symbol));
totalContainer.addView(totalLabel);
TextView productPriceTotalTextView = new TextView(getActivity());
ViewGroup.LayoutParams productCostTotalTextViewParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
productPriceTotalTextView.setLayoutParams(productCostTotalTextViewParams);
productPriceTotalTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.font_size_for_order_editor));
productPriceTotalTextView.setPadding(0,0,0,20);
totalContainer.addView(productPriceTotalTextView);
productPriceTotalTextView.setId(+productItemId + PRODUCT_TOTALS_IDS);
removeItem.setOnClickListener(new View.OnClickListener() {
final View getParent = (View) removeItem.getParent().getParent().getParent();
@Override
public void onClick(View view) {
int currentProductItem = getParent.getId();
int currentProductOrderId = currentProductItem + PRODUCT_ORDER_ID;
TextView currentProductOrderTv = (TextView) getActivity().findViewById(currentProductOrderId);
if (!currentProductOrderTv.getText().toString().equals("")) {
Long currentProductOrderLong = Long.parseLong(currentProductOrderTv.getText().toString());
productOrdersRemoved.add(currentProductOrderLong);
}
LinearLayout itemToRemove = (LinearLayout) getActivity().findViewById(currentProductItem);
productsContainer.removeView(itemToRemove);
updateTotalPrice();
updateTotalQuantity();
updateProductItemNumber();
if (productCount == 1) {
totalPriceEditText.setText("0");
totalQuantityEditText.setText("0");
spinnerSupplier.setEnabled(true);
productItemId = 0;
}
newProductItemCount = newProductItemCount - 1;
}
});
}
@Override
public void onItemSelected(final AdapterView<?> parent, View view, int pos, long l) {
switch (parent.getId()) {
case R.id.spinner_order_supplier:
selectedSupplierWithName = parent.getItemAtPosition(pos).toString();
selectedSupplier = selectedSupplierWithName.substring(selectedSupplierWithName.indexOf(SUPPLIER_SEPARATOR.trim()) + 1).trim();
break;
case R.id.spinner_delivery_status:
deliveryList = (Cursor) parent.getSelectedItem();
selectedDelivery = deliveryList.getString(deliveryList.getColumnIndexOrThrow(OrdersContract.DeliveryEntry._ID));
break;
case R.id.spinner_payment_status:
paymentList = (Cursor) parent.getSelectedItem();
selectedPayment = paymentList.getString(paymentList.getColumnIndexOrThrow(OrdersContract.PaymentEntry._ID));
break;
default:
productList = (Cursor) parent.getSelectedItem();
int selectedSpinner = parent.getId();
int selectedProduct = selectedSpinner - PRODUCT_SPINNER_IDS;
int productPriceId = selectedProduct + PRODUCT_PRICE_IDS;
int productQuantityId = selectedProduct + PRODUCT_QUANTITY_IDS;
int productTotalId = selectedProduct + PRODUCT_TOTALS_IDS;
int productId = selectedProduct + PRODUCT_ID;
TextView productCostItem = (TextView) getActivity().findViewById(productPriceId);
final EditText productQuantityItem = (EditText) getActivity().findViewById(productQuantityId);
final TextView productCostTotalItem = (TextView) getActivity().findViewById(productTotalId);
TextView productIdTextView = (TextView) getActivity().findViewById(+productId);
int selectedProductId = productList.getInt(productList.getColumnIndexOrThrow(OrdersContract.ProductEntry._ID));
// If it is a new order OR if it is a loaded order but a new item has been added
if (orderProductCursor == null || addProductClicked) {
String selectedProductPriceString = productList.getString(productList.getColumnIndexOrThrow(OrdersContract.ProductEntry.COLUMN_PRODUCT_PRICE));
long selectedProductPriceInt = Long.parseLong(selectedProductPriceString);
double selectedProductPriceDouble = selectedProductPriceInt / 100.00;
String currentProductPriceFormatted = currencyFormatter.format(selectedProductPriceDouble);
productCostItem.setText(currentProductPriceFormatted);