· 5 years ago · Sep 29, 2020, 07:22 AM
1package com.example.achiepos.listActivity;
2
3import android.app.DatePickerDialog;
4import android.content.Intent;
5import android.os.Bundle;
6import android.view.View;
7import android.widget.Button;
8import android.widget.DatePicker;
9import android.widget.EditText;
10import android.widget.TextView;
11import android.widget.Toast;
12
13import com.example.achiepos.R;
14import com.example.achiepos.activity.PenjualanActivity;
15import com.example.achiepos.adapter.PenjualanAdapter;
16import com.example.achiepos.config.apiClient;
17import com.example.achiepos.config.apiInterface;
18import com.example.achiepos.model.Penjualan;
19
20import java.text.SimpleDateFormat;
21import java.util.Calendar;
22import java.util.List;
23import java.util.Locale;
24
25import androidx.appcompat.app.AppCompatActivity;
26import androidx.recyclerview.widget.LinearLayoutManager;
27import androidx.recyclerview.widget.RecyclerView;
28import retrofit2.Call;
29import retrofit2.Callback;
30import retrofit2.Response;
31
32public class ListPenjualanActivity extends AppCompatActivity {
33 private RecyclerView recyclerView;
34 private RecyclerView.LayoutManager layoutManager;
35 private PenjualanAdapter adapter;
36 private List<Penjualan> dbKeranjangList;
37 apiInterface api;
38 private EditText edTglAwal, edTglAkhir;
39 private Button btnCari;
40 private TextView tvHJ, tvHPP, tvLaba;
41 PenjualanAdapter.onClickListener listener;
42 Calendar myCalendar = Calendar.getInstance();
43 DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
44
45 @Override
46 public void onDateSet(DatePicker view, int year, int monthOfYear,
47 int dayOfMonth) {
48 // TODO Auto-generated method stub
49 myCalendar.set(Calendar.YEAR, year);
50 myCalendar.set(Calendar.MONTH, monthOfYear);
51 myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
52 setDate();
53 }
54
55 };
56
57 DatePickerDialog.OnDateSetListener date2 = new DatePickerDialog.OnDateSetListener() {
58
59 @Override
60 public void onDateSet(DatePicker view, int year, int monthOfYear,
61 int dayOfMonth) {
62 // TODO Auto-generated method stub
63 myCalendar.set(Calendar.YEAR, year);
64 myCalendar.set(Calendar.MONTH, monthOfYear);
65 myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
66 setDate2();
67 }
68
69 };
70
71 @Override
72 protected void onCreate(Bundle savedInstanceState) {
73 super.onCreate(savedInstanceState);
74 setContentView(R.layout.activity_list_penjualan);
75 if (getSupportActionBar() != null) {
76 getSupportActionBar().setTitle("Penjualan");
77 }
78 api = apiClient.getApiClient().create(apiInterface.class);
79 tvHJ = findViewById(R.id.tv_harga_HJ);
80 tvHPP = findViewById(R.id.tv_hargaHPP);
81 tvLaba = findViewById(R.id.tv_hargaLaba);
82 edTglAwal = findViewById(R.id.ed_tglAwal);
83 edTglAwal.setFocusableInTouchMode(false);
84 edTglAwal.setFocusable(false);
85 edTglAwal.setOnClickListener(new View.OnClickListener() {
86 @Override
87 public void onClick(View v) {
88 new DatePickerDialog(ListPenjualanActivity.this, date, myCalendar
89 .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
90 myCalendar.get(Calendar.DAY_OF_MONTH)).show();
91 }
92 });
93 edTglAkhir = findViewById(R.id.ed_tglAkhir);
94 edTglAkhir.setFocusableInTouchMode(false);
95 edTglAkhir.setFocusable(false);
96 edTglAkhir.setOnClickListener(new View.OnClickListener() {
97 @Override
98 public void onClick(View v) {
99 new DatePickerDialog(ListPenjualanActivity.this, date2, myCalendar
100 .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
101 myCalendar.get(Calendar.DAY_OF_MONTH)).show();
102 }
103 });
104 btnCari = findViewById(R.id.btn_cari);
105 btnCari.setOnClickListener(new View.OnClickListener() {
106 @Override
107 public void onClick(View v) {
108 getDataTanggal("readtanggal");
109 getDataLabaTanggal("readlabatanggal");
110 }
111 });
112 getData("read");
113 getDataLaba("readlaba");
114 recyclerView = findViewById(R.id.rv_penjualan);
115 layoutManager = new LinearLayoutManager(this);
116 recyclerView.setLayoutManager(layoutManager);
117
118 listener = new PenjualanAdapter.onClickListener() {
119 @Override
120 public void onRowClick(View view, int position) {
121 Intent intent = new Intent(ListPenjualanActivity.this, PenjualanActivity.class);
122 intent.putExtra("no_faktur_penjualan", dbKeranjangList.get(position).getNo_faktur_penjualan());
123 intent.putExtra("total_bayar", dbKeranjangList.get(position).getTotal_bayar());
124 intent.putExtra("nilai_penjualan", dbKeranjangList.get(position).getNilai_penjualan());
125
126 startActivity(intent);
127 }
128 };
129 }
130
131
132 private void setDate() {
133 String myFormat = "dd MMMM yyyy"; //In which you need put here
134 SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
135
136 edTglAwal.setText(sdf.format(myCalendar.getTime()));
137 }
138
139 private void setDate2() {
140 String myFormat = "dd MMMM yyyy"; //In which you need put here
141 SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
142
143 edTglAkhir.setText(sdf.format(myCalendar.getTime()));
144 }
145
146 public void getData(final String key){
147 Call<List<Penjualan>> call = api.bacaPenjualan(key);
148 call.enqueue(new Callback<List<Penjualan>>() {
149 @Override
150 public void onResponse(Call<List<Penjualan>> call, Response<List<Penjualan>> response) {
151 dbKeranjangList = response.body();
152 adapter = new PenjualanAdapter(dbKeranjangList, ListPenjualanActivity.this, listener);
153 recyclerView.setAdapter(adapter);
154 adapter.notifyDataSetChanged();
155 }
156
157 @Override
158 public void onFailure(Call<List<Penjualan>> call, Throwable t) {
159 Toast.makeText(ListPenjualanActivity.this, "rp :"+
160 t.getMessage().toString(),
161 Toast.LENGTH_SHORT).show();
162 }
163 });
164 }
165
166 public void getDataLaba(final String key){
167 Call<Penjualan> call = api.bacaLabaPenjualan(key);
168 call.enqueue(new Callback<Penjualan>() {
169 @Override
170 public void onResponse(Call<Penjualan> call, Response<Penjualan> response) {
171 Penjualan pj = response.body();
172 tvHJ.setText(String.format("Rp.%s", pj.getTotal_penjualan()));
173 tvHPP.setText(String.format("Rp.%s", pj.getTotal_hpp()));
174 tvLaba.setText(String.format("Rp.%s", pj.getTotal_pendapatan()));
175 }
176
177 @Override
178 public void onFailure(Call<Penjualan> call, Throwable t) {
179
180 }
181 });
182 }
183
184 public void getDataTanggal(final String key){
185 String tglawal = edTglAwal.getText().toString().trim();
186 String tglakhir = edTglAkhir.getText().toString().trim();
187 Call<List<Penjualan>> call = api.bacaTanggalPenjualan(key, tglawal, tglakhir);
188 call.enqueue(new Callback<List<Penjualan>>() {
189 @Override
190 public void onResponse(Call<List<Penjualan>> call, Response<List<Penjualan>> response) {
191 dbKeranjangList = response.body();
192 adapter = new PenjualanAdapter(dbKeranjangList, ListPenjualanActivity.this, listener);
193 recyclerView.setAdapter(adapter);
194 adapter.notifyDataSetChanged();
195 }
196
197 @Override
198 public void onFailure(Call<List<Penjualan>> call, Throwable t) {
199 Toast.makeText(ListPenjualanActivity.this, "rp :"+
200 t.getMessage().toString(),
201 Toast.LENGTH_SHORT).show();
202 }
203 });
204 }
205
206 public void getDataLabaTanggal(final String key){
207 String tglawal = edTglAwal.getText().toString().trim();
208 String tglakhir = edTglAkhir.getText().toString().trim();
209 Call<Penjualan> call = api.bacaTanggalLabaPenjualan(key, tglawal, tglakhir);
210 call.enqueue(new Callback<Penjualan>() {
211 @Override
212 public void onResponse(Call<Penjualan> call, Response<Penjualan> response) {
213 Penjualan pj = response.body();
214 tvHJ.setText(String.format("Rp.%s", pj.getTotal_penjualan()));
215 tvHPP.setText(String.format("Rp.%s", pj.getTotal_hpp()));
216 tvLaba.setText(String.format("Rp.%s", pj.getTotal_pendapatan()));
217 }
218
219 @Override
220 public void onFailure(Call<Penjualan> call, Throwable t) {
221
222 }
223 });
224 }
225}
226