gestion des formats de tickets!
This commit is contained in:
@@ -38,14 +38,19 @@ import android.widget.Toast;
|
||||
import com.anggastudio.printama.Printama;
|
||||
import com.example.quiz.data.model.Course;
|
||||
import com.example.quiz.data.model.Pari;
|
||||
import com.example.quiz.data.model.PariMise;
|
||||
import com.example.quiz.data.model.ParisResponse;
|
||||
import com.example.quiz.data.model.Reunion;
|
||||
import com.example.quiz.data.model.TypeOfBet;
|
||||
import com.example.quiz.data.model.dtos.PariCourseDto;
|
||||
import com.example.quiz.data.model.enums.PariStatut;
|
||||
import com.example.quiz.databinding.FragmentBetValidationBinding;
|
||||
import com.example.quiz.utils.BluetoothUtils;
|
||||
import com.example.quiz.utils.LoaderDialog;
|
||||
import com.example.quiz.utils.MessageDialog;
|
||||
import com.example.quiz.utils.Result;
|
||||
import com.example.quiz.utils.SharedPrefsHelper;
|
||||
import com.example.quiz.viewModel.LogsViewModel;
|
||||
import com.example.quiz.viewModel.PariViewModel;
|
||||
import com.example.quiz.viewModel.SharedViewModel;
|
||||
import com.google.android.material.appbar.MaterialToolbar;
|
||||
@@ -54,7 +59,10 @@ import com.google.zxing.MultiFormatWriter;
|
||||
import com.google.zxing.WriterException;
|
||||
import com.google.zxing.common.BitMatrix;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -80,6 +88,8 @@ public class BetValidation extends Fragment {
|
||||
|
||||
private int nombreX;
|
||||
|
||||
LogsViewModel logsViewModel;
|
||||
|
||||
|
||||
private boolean order;
|
||||
|
||||
@@ -90,10 +100,13 @@ public class BetValidation extends Fragment {
|
||||
|
||||
SharedPrefsHelper prefsHelper;
|
||||
|
||||
private Reunion reunion;
|
||||
|
||||
private Course course;
|
||||
|
||||
LoaderDialog loader;
|
||||
|
||||
|
||||
|
||||
private int coeff;
|
||||
|
||||
private ActivityResultLauncher<Intent> enableBluetoothLauncher;
|
||||
@@ -126,6 +139,8 @@ public class BetValidation extends Fragment {
|
||||
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
|
||||
Bundle savedInstanceState) {
|
||||
binding = FragmentBetValidationBinding.inflate(inflater, container, false);
|
||||
loader = new LoaderDialog(getContext());
|
||||
logsViewModel = new ViewModelProvider(this).get(LogsViewModel.class);
|
||||
binding.coeff.setText(String.valueOf(1));
|
||||
coeff = Integer.parseInt(binding.coeff.getText().toString());
|
||||
binding.coeff.addTextChangedListener(new TextWatcher(){
|
||||
@@ -162,7 +177,7 @@ public class BetValidation extends Fragment {
|
||||
int columns = 8;
|
||||
grid.setColumnCount(columns);
|
||||
if(shared.selectedCourse.getValue() != null){
|
||||
for (int i = 1; i <= shared.selectedCourse.getValue().getPartants(); i++) {
|
||||
for (int i = 1; i <= shared.selectedCourse.getValue().getNombrePartants(); i++) {
|
||||
createNumberItem(String.valueOf(i));
|
||||
grid.addView(createNumberItem(String.valueOf(i)));
|
||||
}
|
||||
@@ -183,8 +198,16 @@ public class BetValidation extends Fragment {
|
||||
textView.setHeight(100);
|
||||
textView.setGravity(Gravity.CENTER);
|
||||
textView.setBackgroundResource(Objects.equals(horse, "X") ?R.drawable.x_background: R.drawable.number_background);
|
||||
if(!Objects.equals(horse, "X") && shared.selectedCourse.getValue().getNonPartants() != null && shared.selectedCourse.getValue().getNonPartants().contains(Integer.valueOf(horse))){
|
||||
textView.setTextColor(getResources().getColor(R.color.white));
|
||||
textView.setBackgroundResource(R.drawable.number_background_grey);
|
||||
}
|
||||
textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
|
||||
textView.setOnClickListener(v -> {
|
||||
if(!Objects.equals(horse, "X") && shared.selectedCourse.getValue().getNonPartants() != null && shared.selectedCourse.getValue().getNonPartants().contains(Integer.valueOf(horse))){
|
||||
Toast.makeText(getContext(), "Ce cheval n'est pas partant", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
final List<String> horses = new ArrayList<>(selectedHorses.getValue());
|
||||
if(horses.size() >= shared.typeOfBet.getValue().getNumberOfHorse() && horse.equals("X")){
|
||||
Toast.makeText(getContext(), "Vous ne pouvez plus sélectionner X", Toast.LENGTH_SHORT).show();
|
||||
@@ -233,7 +256,6 @@ public class BetValidation extends Fragment {
|
||||
setSelectedTypeOfBetImage();
|
||||
pariViewModel = new ViewModelProvider(this).get(PariViewModel.class);
|
||||
typeOfBet = shared.typeOfBet.getValue();
|
||||
reunion = shared.selectedReunion.getValue();
|
||||
course = shared.selectedCourse.getValue();
|
||||
AppCompatActivity activity = (AppCompatActivity) getActivity();
|
||||
if(activity != null){
|
||||
@@ -306,7 +328,7 @@ public class BetValidation extends Fragment {
|
||||
return;
|
||||
}
|
||||
|
||||
if(shared.typeOfBet == null || shared.selectedReunion.getValue() == null || shared.selectedCourse == null){
|
||||
if(shared.typeOfBet == null || shared.selectedCourse == null){
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -322,20 +344,18 @@ public class BetValidation extends Fragment {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Pari pari = new Pari(
|
||||
generate12Digits(),
|
||||
shared.typeOfBet.getValue().getName(),
|
||||
mise,
|
||||
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss").format(LocalDateTime.now()),
|
||||
true,
|
||||
true,
|
||||
new PariCourseDto(shared.selectedCourse.getValue().getId()),
|
||||
selectedHorses.getValue(),
|
||||
order?new ArrayList<String>(): selectedHorses.getValue(),
|
||||
!order,
|
||||
"MULTI_4",
|
||||
prefsHelper.get("code")
|
||||
OffsetDateTime.now(ZoneOffset.UTC).toString(),
|
||||
Long.valueOf(course.getId()),
|
||||
Long.valueOf(prefsHelper.get("id")),
|
||||
Long.valueOf(prefsHelper.get("terminalId")),
|
||||
Long.valueOf(prefsHelper.get("pointDeVenteId")),
|
||||
List.of(new PariMise(String.valueOf(shared.typeOfBet.getValue().getName()), mise)),
|
||||
_getFormule(),
|
||||
_getCombinaison(),
|
||||
coeff,
|
||||
"XOF",
|
||||
"Pari"
|
||||
);
|
||||
_showPariDialog(pari);
|
||||
});
|
||||
@@ -349,64 +369,107 @@ public class BetValidation extends Fragment {
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
String _getCombinaison(){
|
||||
if(selectedHorses.getValue().contains("X") && shared.typeOfBet.getValue().getNumberOfHorse() < selectedHorses.getValue().size()){
|
||||
String first = selectedHorses.getValue().subList(0, shared.typeOfBet.getValue().getNumberOfHorse()).stream()
|
||||
.map(String::valueOf)
|
||||
.collect(Collectors.joining(","));
|
||||
String last = selectedHorses.getValue().subList(shared.typeOfBet.getValue().getNumberOfHorse(), selectedHorses.getValue().size()).stream()
|
||||
.map(String::valueOf)
|
||||
.collect(Collectors.joining(","));
|
||||
return first+",R,"+last;
|
||||
}else{
|
||||
return selectedHorses.getValue().stream().map(String::valueOf).collect(Collectors.joining(","));
|
||||
}
|
||||
}
|
||||
|
||||
List<String> _getFormule(){
|
||||
List<String> combinaison = selectedHorses.getValue();
|
||||
if(!combinaison.contains("X")){
|
||||
if(!order){
|
||||
return List.of("UNITAIRE");
|
||||
}else{
|
||||
return List.of("FORMULE_COMPLETE");
|
||||
}
|
||||
}else{
|
||||
if(shared.typeOfBet.getValue().getNumberOfHorse() < selectedHorses.getValue().size()){
|
||||
if(order){
|
||||
return List.of("CHAMP_X", "FORMULE_COMPLETE");
|
||||
}else{
|
||||
return List.of("CHAMP_X");
|
||||
}
|
||||
}else{
|
||||
if(order){
|
||||
return List.of("CHAMP_TOTAL", "FORMULE_COMPLETE");
|
||||
}else{
|
||||
return List.of("CHAMP_TOTAL");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void setSelectedTypeOfBetImage(){
|
||||
switch (shared.typeOfBet.getValue().getName()){
|
||||
case "COUPLE_PLACE":
|
||||
case COUPLE_PLACE:
|
||||
binding.icTypeOfBet.setImageResource(R.drawable.ic_couple_place);
|
||||
break;
|
||||
case "COUPLE_GAGNANT":
|
||||
case COUPLE_GAGNANT:
|
||||
binding.icTypeOfBet.setImageResource(R.drawable.ic_couple_gagnant);
|
||||
break;
|
||||
case "TIERCE":
|
||||
case TIERCE:
|
||||
binding.icTypeOfBet.setImageResource(R.drawable.ic_tierce);
|
||||
break;
|
||||
case "QUARTE":
|
||||
case QUARTE:
|
||||
binding.icTypeOfBet.setImageResource(R.drawable.ic_quarte);
|
||||
break;
|
||||
case "QUINTE":
|
||||
case QUINTE:
|
||||
binding.icTypeOfBet.setImageResource(R.drawable.ic_quinte_plus);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void printPari(Pari pari) throws WriterException {
|
||||
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pmu_logo);
|
||||
Bitmap barcode = generateBarcodeBitmap(pari.getNumeroTicket(), 384, 100);
|
||||
StringBuilder tspl = new StringBuilder();
|
||||
|
||||
tspl.append("Bamako").append("\n");
|
||||
tspl.append(shared.selectedCourse.getValue().getNom()).append("\n");
|
||||
|
||||
LocalDateTime dateTime = LocalDateTime.parse(shared.selectedCourse.getValue().getDateDepartCourse());
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss");
|
||||
String formattedDate = dateTime.format(formatter);
|
||||
tspl.append(formattedDate).append("\n");
|
||||
tspl.append("Course ").append(String.valueOf(shared.selectedCourse.getValue().getId())).append("\n");
|
||||
tspl.append(shared.selectedCourse.getValue().getType()).append("\n");
|
||||
tspl.append("--------------------------------\n");
|
||||
boolean isElargie = selectedHorses.getValue().size()>shared.typeOfBet.getValue().getNumberOfHorse();
|
||||
tspl.append(isElargie?pari.getTypePari()+"/Elargie":pari.getTypePari()).append("\n");
|
||||
tspl.append(order?"COMBINAISON COMPLETE"+"\n":"");
|
||||
String combinationText = selectedHorses.getValue().stream()
|
||||
.map(String::valueOf)
|
||||
.collect(Collectors.joining("-"));
|
||||
tspl.append(combinationText).append("\n");
|
||||
tspl.append("COEF: "+String.valueOf(coeff)).append(".0").append("\n");
|
||||
tspl.append("--------------------------------\n");
|
||||
tspl.append("MONTANT: ").append(pari.getMise()).append(" XOF\n");
|
||||
tspl.append("-------------------------------\n");
|
||||
tspl.append("AGENT: ").append(prefsHelper.get("code")).append("\n");
|
||||
tspl.append("DATE: ").append(DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss").format(LocalDateTime.now())).append("\n");
|
||||
if (BluetoothUtils.needsBluetoothPermissions()) {
|
||||
if (!BluetoothUtils.hasBluetoothPermission(requireContext())) {
|
||||
// Demande la permission si non accordée
|
||||
BluetoothUtils.requestBluetoothPermission(requireActivity());
|
||||
return; // arrête ici, la popup va apparaître
|
||||
}
|
||||
}
|
||||
|
||||
// 2️⃣ Permission OK, on peut afficher la liste
|
||||
public void printPari(ParisResponse pari) throws WriterException {
|
||||
try {
|
||||
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pmu_logo);
|
||||
Bitmap barcode = generateBarcodeBitmap(pari.getNumeroTicket(), 384, 100);
|
||||
StringBuilder tspl = new StringBuilder();
|
||||
Printama printama = Printama.with(getContext());
|
||||
|
||||
tspl.append("Bamako").append("\n");
|
||||
tspl.append(shared.selectedCourse.getValue().getNom()).append("\n");
|
||||
OffsetDateTime dateTime = OffsetDateTime.parse(shared.selectedCourse.getValue().getHeureDepartPrevue());
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");
|
||||
String formattedDate = dateTime.format(formatter);
|
||||
tspl.append(formattedDate).append("\n");
|
||||
tspl.append("Course ").append(String.valueOf(shared.selectedCourse.getValue().getId())).append("\n");
|
||||
tspl.append(shared.selectedCourse.getValue().getTypesParisOuverts().get(0)).append("\n");
|
||||
tspl.append(printama.lineSeparator()+"\n");
|
||||
boolean isElargie = selectedHorses.getValue().size()>shared.typeOfBet.getValue().getNumberOfHorse();
|
||||
String typePari = pari.getTypesParisMises().get(0).getTypePari();
|
||||
tspl.append(isElargie?typePari+"/Elargie":typePari).append("\n");
|
||||
tspl.append(order?"COMBINAISON COMPLETE"+"\n":"");
|
||||
String combinationText = selectedHorses.getValue().stream()
|
||||
.map(String::valueOf)
|
||||
.collect(Collectors.joining("-"));
|
||||
tspl.append(combinationText).append("\n");
|
||||
tspl.append("COEF: ").append(String.valueOf(coeff)).append(".0");
|
||||
tspl.append("\n").append(printama.lineSeparator()).append("\n");
|
||||
tspl.append("MONTANT: ").append(pari.getTypesParisMises().get(0).getMiseTotale()).append(" XOF");
|
||||
tspl.append("\n").append(printama.lineSeparator()).append("\n");
|
||||
tspl.append("AGENT: ").append(prefsHelper.get("code")).append("\n");
|
||||
tspl.append("DATE: ").append(DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss").format(LocalDateTime.now())).append("\n");
|
||||
if (BluetoothUtils.needsBluetoothPermissions()) {
|
||||
if (!BluetoothUtils.hasBluetoothPermission(requireContext())) {
|
||||
// Demande la permission si non accordée
|
||||
BluetoothUtils.requestBluetoothPermission(requireActivity());
|
||||
return; // arrête ici, la popup va apparaître
|
||||
}
|
||||
}
|
||||
|
||||
// 2️⃣ Permission OK, on peut afficher la liste
|
||||
|
||||
Printama.with(getContext()).printTextBuilder(tspl, bitmap, pari.getNumeroTicket(), barcode);
|
||||
_initializeToZero();
|
||||
} catch (SecurityException e) {
|
||||
@@ -423,7 +486,7 @@ public class BetValidation extends Fragment {
|
||||
TextView numero_course = (TextView) view.findViewById(R.id.alert_course_numero);
|
||||
numero_course.setText("Numero Course: "+String.valueOf(shared.selectedCourse.getValue().getId()));
|
||||
TextView alert_pari_type = (TextView) view.findViewById(R.id.alert_pari_type);
|
||||
alert_pari_type.setText(shared.typeOfBet.getValue().getName());
|
||||
alert_pari_type.setText(String.valueOf(shared.typeOfBet.getValue().getName()));
|
||||
TextView is_elargie = (TextView) view.findViewById(R.id.alert_is_elargie);
|
||||
is_elargie.setText(selectedHorses.getValue().size() > shared.typeOfBet.getValue().getNumberOfHorse()?"CE":"SI");
|
||||
TextView alert_combinaison = (TextView) view.findViewById(R.id.alert_combinaison);
|
||||
@@ -433,7 +496,7 @@ public class BetValidation extends Fragment {
|
||||
TextView aler_coeff = (TextView) view.findViewById(R.id.alert_coeff);
|
||||
aler_coeff.setText("Coef:"+String.valueOf(coeff));
|
||||
TextView alert_montant = (TextView) view.findViewById(R.id.alert_montant);
|
||||
alert_montant.setText("Montant: "+String.valueOf(pari.getMise())+" CFA");
|
||||
alert_montant.setText("Montant: "+String.valueOf(pari.getTypesParisMises().get(0).getMiseTotale())+" CFA");
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
|
||||
builder.setView(view);
|
||||
builder.setCancelable(false);
|
||||
@@ -442,21 +505,28 @@ public class BetValidation extends Fragment {
|
||||
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
|
||||
|
||||
view.findViewById(R.id.alert_validate).setOnClickListener(v->{
|
||||
pariViewModel.createPari(pari).observe(getViewLifecycleOwner(),new Observer<Result<Pari>>() {
|
||||
pariViewModel.createPari(pari).observe(getViewLifecycleOwner(),new Observer<Result<ParisResponse>>() {
|
||||
@Override
|
||||
public void onChanged(Result<Pari> pariResult) {
|
||||
public void onChanged(Result<ParisResponse> pariResult) {
|
||||
switch (pariResult.status){
|
||||
case ERROR:
|
||||
Toast.makeText(getContext(), pariResult.message, Toast.LENGTH_SHORT).show();
|
||||
loader.dismiss();
|
||||
dialog.dismiss();
|
||||
MessageDialog.showError(getContext(), pariResult.message);
|
||||
break;
|
||||
case LOADING:
|
||||
Toast.makeText(getContext(), "En cours", Toast.LENGTH_SHORT).show();
|
||||
loader.show("Prise de pari");
|
||||
break;
|
||||
case SUCCESS:
|
||||
try {
|
||||
printPari(pari);
|
||||
loader.dismiss();
|
||||
printPari(pariResult.data);
|
||||
logsViewModel.insertLog(prefsHelper.get("id"), "BET", "Création du pari "+pariResult.data.getNumeroTicket()+" type de paris: "+pariResult.data.getTypesParisMises().get(0).getTypePari(), System.currentTimeMillis());
|
||||
dialog.dismiss();
|
||||
MessageDialog.showSuccess(getContext(), "Pari créé avec succès");
|
||||
} catch (WriterException e) {
|
||||
loader.dismiss();
|
||||
MessageDialog.showError(getContext(), e.getMessage());
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
break;
|
||||
@@ -485,11 +555,15 @@ public class BetValidation extends Fragment {
|
||||
_notClickable(selectedHorses);
|
||||
int nombreChevauxSelectionnes = selectedHorses.size();
|
||||
long mise = 0;
|
||||
int nonPartants = 0;
|
||||
if(shared.typeOfBet.getValue() == null || shared.selectedCourse.getValue() == null){
|
||||
return;
|
||||
}
|
||||
if(shared.selectedCourse.getValue().getNonPartants() != null){
|
||||
nonPartants = shared.selectedCourse.getValue().getNonPartants().size();
|
||||
}
|
||||
int typeOfBetHorses = shared.typeOfBet.getValue().getNumberOfHorse();
|
||||
int partants = shared.selectedCourse.getValue().getPartants();
|
||||
int partants = shared.selectedCourse.getValue().getNombrePartants();
|
||||
if(shared.typeOfBet.getValue().getNumberOfHorse() > nombreChevauxSelectionnes){
|
||||
binding.mise.setText(mise+" CFA");
|
||||
return;
|
||||
@@ -505,15 +579,15 @@ public class BetValidation extends Fragment {
|
||||
}
|
||||
mise = mise * coeff;
|
||||
if(nombreX>0){
|
||||
if(nombreChevauxSelectionnes - typeOfBetHorses == 1){
|
||||
mise = 0;
|
||||
this.mise = mise;
|
||||
binding.mise.setText(mise+" CFA");
|
||||
return;
|
||||
}
|
||||
if(nombreChevauxSelectionnes == typeOfBetHorses){
|
||||
mise = mise * _calculateArrangement((partants - (typeOfBetHorses-nombreX)), nombreX);
|
||||
mise = mise * _calculateArrangement((partants - (typeOfBetHorses-nombreX) - nonPartants), nombreX);
|
||||
}else{
|
||||
if(nombreChevauxSelectionnes - typeOfBetHorses < nombreX || nombreChevauxSelectionnes - typeOfBetHorses==1){
|
||||
mise = 0;
|
||||
this.mise = mise;
|
||||
binding.mise.setText(mise+" CFA");
|
||||
return;
|
||||
}
|
||||
mise = mise * _calculateArrangement(nombreChevauxSelectionnes - typeOfBetHorses , nombreX);
|
||||
}
|
||||
if(order){
|
||||
|
||||
Reference in New Issue
Block a user