121 lines
4.1 KiB
Dart
121 lines
4.1 KiB
Dart
import 'package:cloud_firestore/cloud_firestore.dart';
|
|
import 'package:e_commerce/const/AppColors.dart';
|
|
import 'package:e_commerce/const/rupiah.dart';
|
|
import 'package:firebase_auth/firebase_auth.dart';
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:google_fonts/google_fonts.dart';
|
|
import 'package:url_launcher/url_launcher.dart';
|
|
|
|
void deleteAllItems(String collectionName) {
|
|
final userEmail = FirebaseAuth.instance.currentUser!.email;
|
|
|
|
FirebaseFirestore.instance
|
|
.collection(collectionName)
|
|
.doc(userEmail)
|
|
.collection("items")
|
|
.get() // Dapatkan semua dokumen dalam sub-koleksi 'items'
|
|
.then((snapshot) {
|
|
for (var doc in snapshot.docs) {
|
|
doc.reference.delete(); // Hapus setiap dokumen dalam sub-koleksi
|
|
}
|
|
}).catchError((error) {
|
|
print("Error deleting items: $error"); // Tangani error saat menghapus
|
|
});
|
|
}
|
|
|
|
String createOrderMessage(QuerySnapshot snapshot, int totalPrice, String note) {
|
|
StringBuffer messageBuffer = StringBuffer("Halo minTIers aku\n mau pesan nih\n");
|
|
messageBuffer.write("\n");
|
|
messageBuffer.write("Rincian pesanan:\n");
|
|
int itemIndex = 1; // Untuk menjaga urutan item
|
|
|
|
// Tambahkan setiap item ke pesan dengan format yang diinginkan
|
|
snapshot.docs.forEach((doc) {
|
|
String itemName = doc['name']; // Nama item
|
|
String itemPrice = (int.tryParse(doc['price']) ?? 0).toRupiah(); // Harga item
|
|
|
|
// Tambahkan item ke pesan
|
|
messageBuffer.write("${itemIndex}. $itemName : $itemPrice\n");
|
|
|
|
// Tingkatkan indeks untuk item berikutnya
|
|
itemIndex++;
|
|
});
|
|
messageBuffer.write("\n");
|
|
messageBuffer.write("total pesanan: ${totalPrice.toRupiah()}\n");
|
|
messageBuffer.write("Pembayaran: Tunai\n");
|
|
messageBuffer.write("\n");
|
|
messageBuffer.write("Note: $note\n");
|
|
messageBuffer.write("\n");
|
|
messageBuffer.write("Tolong segera disiapin yaa dan infokan estimasi bisa diambilnya juga\nTerimakasih minTIers:))\n");
|
|
|
|
return messageBuffer.toString();
|
|
}
|
|
|
|
Future<void> updateStock(QuerySnapshot snapshot) async {
|
|
for (QueryDocumentSnapshot element in snapshot.docs) {
|
|
final data = await FirebaseFirestore.instance.collection('products').where('product-name', isEqualTo: element['name']).get();
|
|
final docs = data.docs.firstOrNull;
|
|
if (docs != null) {
|
|
print(docs.id);
|
|
final data = docs.data();
|
|
final updatedStock = data['stock'] - 1;
|
|
await FirebaseFirestore.instance.collection('products').doc(docs.id).update({'stock': updatedStock});
|
|
}
|
|
}
|
|
}
|
|
|
|
Future<void> openWhatsApp(String phone, String message) async {
|
|
final whatsappUrl = Uri.parse("whatsapp://send?phone=$phone&text=$message");
|
|
|
|
if (!await launchUrl(whatsappUrl, mode: LaunchMode.externalApplication)) {
|
|
throw Exception("Could not launch WhatsApp");
|
|
}
|
|
}
|
|
|
|
void showOrderDialog(BuildContext context, QuerySnapshot snapshot, int totalPrice) {
|
|
TextEditingController noteController = TextEditingController();
|
|
|
|
showDialog(
|
|
context: context,
|
|
builder: (BuildContext context) {
|
|
return AlertDialog(
|
|
title: Text("Tambahkan Catatan"),
|
|
content: TextField(
|
|
controller: noteController,
|
|
decoration: InputDecoration(
|
|
hintText: "Masukkan catatan untuk pesanan",
|
|
),
|
|
),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () {
|
|
Navigator.of(context).pop();
|
|
},
|
|
child: Text("Batal"),
|
|
),
|
|
TextButton(
|
|
onPressed: () async {
|
|
// Ambil catatan yang dimasukkan pengguna
|
|
final note = noteController.text;
|
|
|
|
// Buat pesan dengan catatan yang dimasukkan
|
|
final orderMessage = createOrderMessage(snapshot, totalPrice, note);
|
|
|
|
await updateStock(snapshot);
|
|
|
|
// Buka WhatsApp dengan pesan yang telah disiapkan
|
|
openWhatsApp("+6289523392734", orderMessage);
|
|
deleteAllItems("users-cart-items");
|
|
|
|
// Tutup dialog setelah menekan OK
|
|
Navigator.of(context).pop();
|
|
},
|
|
child: Text("OK"),
|
|
),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
}
|