update invoice section show download option
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
class ApiConfig {
|
||||
static const String baseUrl = "http://10.119.0.74:8000/api";
|
||||
static const String fileBaseUrl = "http://10.119.0.74:8000/";
|
||||
}
|
||||
|
||||
@@ -176,17 +176,17 @@ class InstallmentCard extends StatelessWidget {
|
||||
Divider(color: Colors.grey.shade300, thickness: 1),
|
||||
SizedBox(height: isTablet ? 10 : 6),
|
||||
|
||||
Align(
|
||||
alignment: Alignment.centerRight,
|
||||
child: Text(
|
||||
"Installment #${inst['id'] ?? ''}",
|
||||
style: TextStyle(
|
||||
fontSize: isTablet ? 15 : 13,
|
||||
color: Colors.grey.shade600,
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
),
|
||||
// Align(
|
||||
// alignment: Alignment.centerRight,
|
||||
// child: Text(
|
||||
// "Installment #${inst['id'] ?? ''}",
|
||||
// style: TextStyle(
|
||||
// fontSize: isTablet ? 15 : 13,
|
||||
// color: Colors.grey.shade600,
|
||||
// fontWeight: FontWeight.w500,
|
||||
// ),
|
||||
// ),
|
||||
// ),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
import 'dart:io';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:pdf/widgets.dart' as pw;
|
||||
import 'package:share_plus/share_plus.dart';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:permission_handler/permission_handler.dart';
|
||||
import 'package:share_plus/share_plus.dart';
|
||||
import '../config/api_config.dart';
|
||||
import '../services/dio_client.dart';
|
||||
import '../services/invoice_service.dart';
|
||||
import '../widgets/invoice_detail_view.dart';
|
||||
@@ -26,9 +29,6 @@ class _InvoiceDetailScreenState extends State<InvoiceDetailScreen> {
|
||||
load();
|
||||
}
|
||||
|
||||
// -------------------------------------------------------
|
||||
// ⭐ LOAD INVOICE FROM API
|
||||
// -------------------------------------------------------
|
||||
Future<void> load() async {
|
||||
final service = InvoiceService(DioClient.getInstance(context));
|
||||
try {
|
||||
@@ -40,93 +40,116 @@ class _InvoiceDetailScreenState extends State<InvoiceDetailScreen> {
|
||||
invoice = {};
|
||||
}
|
||||
} catch (e) {
|
||||
// handle error gracefully
|
||||
invoice = {};
|
||||
} finally {
|
||||
if (mounted) setState(() => loading = false);
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------------------------
|
||||
// ⭐ GENERATE + SAVE PDF TO DOWNLOADS FOLDER
|
||||
// (No Permission Needed)
|
||||
// -------------------------------------------------------
|
||||
Future<File> generatePDF() async {
|
||||
final pdf = pw.Document();
|
||||
String? get pdfUrl {
|
||||
final path = invoice['pdf_path'];
|
||||
if (path == null || path.toString().isEmpty) return null;
|
||||
|
||||
pdf.addPage(
|
||||
pw.Page(
|
||||
build: (context) => pw.Column(
|
||||
crossAxisAlignment: pw.CrossAxisAlignment.start,
|
||||
children: [
|
||||
pw.Text(
|
||||
"INVOICE DETAILS",
|
||||
style: pw.TextStyle(fontSize: 26, fontWeight: pw.FontWeight.bold),
|
||||
),
|
||||
pw.SizedBox(height: 20),
|
||||
return ApiConfig.fileBaseUrl + path;
|
||||
}
|
||||
|
||||
pw.Text("Invoice ID: ${invoice['id'] ?? '-'}"),
|
||||
pw.Text("Amount: ₹${invoice['amount'] ?? '-'}"),
|
||||
pw.Text("Status: ${invoice['status'] ?? '-'}"),
|
||||
pw.Text("Date: ${invoice['date'] ?? '-'}"),
|
||||
pw.Text("Customer: ${invoice['customer_name'] ?? '-'}"),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
static const MethodChannel _mediaScanner =
|
||||
MethodChannel('media_scanner');
|
||||
|
||||
// ⭐ SAFEST WAY (Android 10–14)
|
||||
final downloadsDir = await getDownloadsDirectory();
|
||||
|
||||
final filePath = "${downloadsDir!.path}/invoice_${invoice['id']}.pdf";
|
||||
final file = File(filePath);
|
||||
|
||||
await file.writeAsBytes(await pdf.save());
|
||||
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text("PDF saved to Downloads:\n$filePath")),
|
||||
);
|
||||
Future<void> _scanFile(String path) async {
|
||||
try {
|
||||
await _mediaScanner.invokeMethod('scanFile', {'path': path});
|
||||
} catch (e) {
|
||||
debugPrint("❌ MediaScanner error: $e");
|
||||
}
|
||||
|
||||
return file;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------
|
||||
// ⭐ SHARE THE SAVED PDF FILE
|
||||
// -------------------------------------------------------
|
||||
Future<void> sharePDF() async {
|
||||
final file = await generatePDF();
|
||||
|
||||
await Share.shareXFiles(
|
||||
[XFile(file.path)],
|
||||
text: "Invoice Details",
|
||||
);
|
||||
|
||||
Future<File?> _downloadPdf(String url) async {
|
||||
try {
|
||||
debugPrint("📥 PDF URL: $url");
|
||||
|
||||
final fileName = url.split('/').last;
|
||||
|
||||
// ✅ SAME FOLDER AS CHAT FILES
|
||||
final baseDir = Directory('/storage/emulated/0/Download/KentChat');
|
||||
|
||||
if (!await baseDir.exists()) {
|
||||
await baseDir.create(recursive: true);
|
||||
debugPrint("📁 Created directory: ${baseDir.path}");
|
||||
}
|
||||
|
||||
final filePath = "${baseDir.path}/$fileName";
|
||||
debugPrint("📄 Saving PDF to: $filePath");
|
||||
|
||||
await Dio().download(
|
||||
url,
|
||||
filePath,
|
||||
onReceiveProgress: (received, total) {
|
||||
if (total > 0) {
|
||||
debugPrint(
|
||||
"⬇ Downloading: ${(received / total * 100).toStringAsFixed(1)}%",
|
||||
);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
// 🔔 VERY IMPORTANT: Notify Android system (same as chat)
|
||||
await _scanFile(filePath);
|
||||
|
||||
debugPrint("✅ PDF Downloaded & Scanned Successfully");
|
||||
|
||||
return File(filePath);
|
||||
} catch (e) {
|
||||
debugPrint("❌ PDF download error: $e");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final width = MediaQuery.of(context).size.width;
|
||||
|
||||
// ⭐ COMPACT RESPONSIVE SCALE
|
||||
final scale = (width / 430).clamp(0.88, 1.08);
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(
|
||||
"Invoice Details",
|
||||
style: TextStyle(
|
||||
fontSize: 18 * scale,
|
||||
fontWeight: FontWeight.w600,
|
||||
title: const Text("Invoice Details"),
|
||||
actions: pdfUrl == null
|
||||
? []
|
||||
: [
|
||||
// DOWNLOAD
|
||||
IconButton(
|
||||
icon: const Icon(Icons.download_rounded),
|
||||
onPressed: () async {
|
||||
final file = await _downloadPdf(pdfUrl!);
|
||||
if (file != null && mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text("Invoice downloaded")),
|
||||
);
|
||||
}
|
||||
},
|
||||
),
|
||||
),
|
||||
|
||||
// ⭐ PDF + SHARE BUTTONS
|
||||
actions: [
|
||||
// SHARE
|
||||
IconButton(
|
||||
icon: const Icon(Icons.picture_as_pdf),
|
||||
onPressed: generatePDF,
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.share),
|
||||
onPressed: sharePDF,
|
||||
icon: const Icon(Icons.share_rounded),
|
||||
onPressed: () async {
|
||||
final file = await _downloadPdf(pdfUrl!);
|
||||
if (file != null) {
|
||||
await Share.shareXFiles(
|
||||
[XFile(file.path)],
|
||||
text: "Invoice ${invoice['invoice_number']}",
|
||||
);
|
||||
}
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
@@ -146,6 +169,8 @@ class _InvoiceDetailScreenState extends State<InvoiceDetailScreen> {
|
||||
)
|
||||
: Padding(
|
||||
padding: EdgeInsets.all(12 * scale),
|
||||
// NOTE: InvoiceDetailView should handle its own responsiveness.
|
||||
// If you want it to follow the same `scale`, I can update that widget to accept `scale`.
|
||||
child: InvoiceDetailView(invoice: invoice),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -92,9 +92,9 @@ class _OrderInvoiceScreenState extends State<OrderInvoiceScreen>
|
||||
_detailRow(Icons.numbers, "Invoice No", invoice['invoice_number'], scale),
|
||||
_detailRow(Icons.calendar_month, "Invoice Date", invoice['invoice_date'], scale),
|
||||
_detailRow(Icons.date_range, "Due Date", invoice['due_date'], scale),
|
||||
_detailRow(Icons.payment, "Payment Method", invoice['payment_method'], scale),
|
||||
_detailRow(Icons.confirmation_number, "Reference No",
|
||||
invoice['reference_no'], scale),
|
||||
// _detailRow(Icons.payment, "Payment Method", invoice['payment_method'], scale),
|
||||
// _detailRow(Icons.confirmation_number, "Reference No",
|
||||
// invoice['reference_no'], scale),
|
||||
], scale),
|
||||
|
||||
// AMOUNT SECTION
|
||||
@@ -103,6 +103,7 @@ class _OrderInvoiceScreenState extends State<OrderInvoiceScreen>
|
||||
}, scale),
|
||||
_sectionBody(s2, [
|
||||
_detailRow(Icons.money, "Amount", invoice['final_amount'], scale),
|
||||
_detailRow(Icons.percent, "GST percent", invoice['gst_percent'], scale),
|
||||
_detailRow(Icons.percent, "GST Amount", invoice['gst_amount'], scale),
|
||||
_detailRow(Icons.summarize, "Final With GST",
|
||||
invoice['final_amount_with_gst'], scale),
|
||||
|
||||
@@ -4,55 +4,30 @@ class InvoiceService {
|
||||
final Dio dio;
|
||||
InvoiceService(this.dio);
|
||||
|
||||
// -------------------------------------------------------
|
||||
// ⭐ GET ALL INVOICES
|
||||
// -------------------------------------------------------
|
||||
Future<Map<String, dynamic>> getAllInvoices() async {
|
||||
try {
|
||||
final res = await dio.get("/user/invoices");
|
||||
|
||||
print("🔵 ALL INVOICES RESPONSE:");
|
||||
print(res.data);
|
||||
|
||||
return Map<String, dynamic>.from(res.data);
|
||||
} catch (e) {
|
||||
print("❌ ERROR (All Invoices): $e");
|
||||
return {"success": false, "message": e.toString()};
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------------------------
|
||||
// ⭐ GET INSTALLMENTS
|
||||
// -------------------------------------------------------
|
||||
Future<Map<String, dynamic>> getInstallments(int invoiceId) async {
|
||||
try {
|
||||
final res =
|
||||
await dio.get("/user/invoice/$invoiceId/installments");
|
||||
|
||||
print("🔵 INSTALLMENTS RESPONSE:");
|
||||
print(res.data);
|
||||
|
||||
final res = await dio.get("/user/invoice/$invoiceId/installments");
|
||||
return Map<String, dynamic>.from(res.data);
|
||||
} catch (e) {
|
||||
print("❌ ERROR (Installments): $e");
|
||||
return {"success": false, "message": e.toString()};
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------------------------
|
||||
// ⭐ GET FULL INVOICE DETAILS (PRINT JSON HERE)
|
||||
// -------------------------------------------------------
|
||||
/// 🔵 NEW FUNCTION — Fetch Full Invoice Details
|
||||
Future<Map<String, dynamic>> getInvoiceDetails(int invoiceId) async {
|
||||
try {
|
||||
final res = await dio.get("/user/invoice/$invoiceId/details");
|
||||
|
||||
print("👇👇👇 INVOICE API RESPONSE START 👇👇👇");
|
||||
print(res.data); // <-- THIS IS WHAT YOU NEED
|
||||
print("👆👆👆 INVOICE API RESPONSE END 👆👆👆");
|
||||
|
||||
return Map<String, dynamic>.from(res.data);
|
||||
} catch (e) {
|
||||
print("❌ ERROR (Invoice Details): $e");
|
||||
return {"success": false, "message": e.toString()};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -298,17 +298,18 @@ class _InvoiceDetailViewState extends State<InvoiceDetailView>
|
||||
() => setState(() => s3 = !s3), scale),
|
||||
sectionBody(s3, [
|
||||
detailRow(Icons.money, "Amount", invoice['final_amount'], scale),
|
||||
detailRow(Icons.percent, "GST %", invoice['gst_percent'], scale),
|
||||
detailRow(Icons.percent, "GST percent %", invoice['gst_percent'], scale),
|
||||
detailRow(Icons.percent, "GST amount %", invoice['gst_amount'], scale),
|
||||
detailRow(Icons.summarize, "Total", invoice['final_amount_with_gst'], scale),
|
||||
], scale),
|
||||
|
||||
sectionHeader("Payment Details", Icons.payment, s4,
|
||||
() => setState(() => s4 = !s4), scale),
|
||||
sectionBody(s4, [
|
||||
detailRow(Icons.credit_card, "Method", invoice['payment_method'], scale),
|
||||
detailRow(Icons.confirmation_number, "Reference",
|
||||
invoice['reference_no'], scale),
|
||||
], scale),
|
||||
// sectionHeader("Payment Details", Icons.payment, s4,
|
||||
// () => setState(() => s4 = !s4), scale),
|
||||
// sectionBody(s4, [
|
||||
// detailRow(Icons.credit_card, "Method", invoice['payment_method'], scale),
|
||||
// detailRow(Icons.confirmation_number, "Reference",
|
||||
// invoice['reference_no'], scale),
|
||||
// ], scale),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user