feat: inventory report
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
import 'dart:developer';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:open_file/open_file.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:pdf/widgets.dart';
|
||||
|
||||
class HelperPdfService {
|
||||
static Future<File> saveDocument({
|
||||
required String name,
|
||||
required Document pdf,
|
||||
}) async {
|
||||
try {
|
||||
log("Starting PDF save process for: $name");
|
||||
log("PDF document object: $pdf");
|
||||
|
||||
final bytes = await pdf.save();
|
||||
log("PDF bytes generated successfully, size: ${bytes.length} bytes");
|
||||
|
||||
if (bytes.isEmpty) {
|
||||
log("WARNING: PDF bytes are empty!");
|
||||
return Future.error("PDF bytes are empty");
|
||||
}
|
||||
|
||||
final dir = await getApplicationDocumentsDirectory();
|
||||
log("Documents directory: ${dir.path}");
|
||||
|
||||
final file = File('${dir.path}/$name');
|
||||
log("Saving PDF to: ${file.path}");
|
||||
|
||||
await file.writeAsBytes(bytes);
|
||||
log("PDF saved successfully to: ${file.path}");
|
||||
|
||||
// Verify file was created
|
||||
if (await file.exists()) {
|
||||
final fileSize = await file.length();
|
||||
log("File exists and size is: $fileSize bytes");
|
||||
} else {
|
||||
log("ERROR: File was not created!");
|
||||
return Future.error("File was not created");
|
||||
}
|
||||
|
||||
return file;
|
||||
} catch (e) {
|
||||
log("Failed to save document: $e");
|
||||
log("Error stack trace: ${StackTrace.current}");
|
||||
return Future.error("Failed to save document: $e");
|
||||
}
|
||||
}
|
||||
|
||||
static Future openFile(File file) async {
|
||||
try {
|
||||
final url = file.path;
|
||||
log("Attempting to open file: $url");
|
||||
|
||||
if (!await file.exists()) {
|
||||
log("ERROR: File does not exist: $url");
|
||||
return;
|
||||
}
|
||||
|
||||
final fileSize = await file.length();
|
||||
log("File exists and size is: $fileSize bytes");
|
||||
|
||||
log("Calling OpenFile.open...");
|
||||
final result = await OpenFile.open(url, type: "application/pdf");
|
||||
log("OpenFile result: $result");
|
||||
|
||||
if (result.type == ResultType.done) {
|
||||
log("File opened successfully");
|
||||
} else {
|
||||
log("File opening failed with result: ${result.type}");
|
||||
log("Error message: ${result.message}");
|
||||
}
|
||||
} catch (e) {
|
||||
log("Failed to open file: $e");
|
||||
log("Error stack trace: ${StackTrace.current}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
import 'dart:developer';
|
||||
|
||||
import 'package:device_info_plus/device_info_plus.dart';
|
||||
import 'package:permission_handler/permission_handler.dart';
|
||||
|
||||
class PermessionHelper {
|
||||
Future<bool> checkPermission() async {
|
||||
final deviceInfo = await DeviceInfoPlugin().androidInfo;
|
||||
bool permissionStatus;
|
||||
if (deviceInfo.version.sdkInt > 32) {
|
||||
permissionStatus = await Permission.photos.request().isGranted;
|
||||
} else {
|
||||
permissionStatus = await Permission.storage.request().isGranted;
|
||||
}
|
||||
|
||||
if (permissionStatus) {
|
||||
log('Izin penyimpanan sudah diberikan.');
|
||||
} else {
|
||||
if (deviceInfo.version.sdkInt > 32) {
|
||||
log('deviceInfo.version.sdkInt > 32.');
|
||||
permissionStatus = await Permission.photos.request().isGranted;
|
||||
} else {
|
||||
permissionStatus = await Permission.storage.request().isGranted;
|
||||
}
|
||||
// } else {
|
||||
// openAppSettings();
|
||||
// }
|
||||
}
|
||||
log('permissionStatus: $permissionStatus');
|
||||
return permissionStatus;
|
||||
}
|
||||
|
||||
void permessionPrinter() async {
|
||||
Map<Permission, PermissionStatus> statuses = await [
|
||||
Permission.bluetooth,
|
||||
Permission.bluetoothScan,
|
||||
Permission.bluetoothAdvertise,
|
||||
Permission.bluetoothConnect,
|
||||
].request();
|
||||
|
||||
log("statuses: $statuses");
|
||||
}
|
||||
}
|
||||
|
||||
// try {
|
||||
// final status =
|
||||
// await PermessionHelper().checkPermission();
|
||||
// if (status) {
|
||||
// final pdfFile = await InventoryReport.previewPdf(
|
||||
// searchDateFormatted: widget.searchDateFormatted,
|
||||
// inventory: widget.inventory,
|
||||
// );
|
||||
// log("pdfFile: $pdfFile");
|
||||
// await HelperPdfService.openFile(pdfFile);
|
||||
// } else {
|
||||
// ScaffoldMessenger.of(context).showSnackBar(
|
||||
// const SnackBar(
|
||||
// content: Text(
|
||||
// 'Storage permission is required to save PDF'),
|
||||
// backgroundColor: Colors.red,
|
||||
// ),
|
||||
// );
|
||||
// }
|
||||
// } catch (e) {
|
||||
// log("Error generating PDF: $e");
|
||||
// ScaffoldMessenger.of(context).showSnackBar(
|
||||
// SnackBar(
|
||||
// content: Text('Failed to generate PDF: $e'),
|
||||
// backgroundColor: Colors.red,
|
||||
// ),
|
||||
// );
|
||||
// }
|
||||
Reference in New Issue
Block a user