Update: 2026-05-15 04:35:25
This commit is contained in:
109
musadaq-app/lib/core/services/thermal_printer_service.dart
Normal file
109
musadaq-app/lib/core/services/thermal_printer_service.dart
Normal file
@@ -0,0 +1,109 @@
|
||||
import 'package:esc_pos_printer/esc_pos_printer.dart';
|
||||
import 'package:esc_pos_utils/esc_pos_utils.dart';
|
||||
import '../utils/logger.dart';
|
||||
|
||||
class ThermalPrinterService {
|
||||
/// Prints an invoice to a WiFi thermal printer
|
||||
Future<bool> printInvoice({
|
||||
required String ip,
|
||||
required Map<String, dynamic> invoice,
|
||||
int port = 9100,
|
||||
PaperSize paperSize = PaperSize.mm80,
|
||||
}) async {
|
||||
try {
|
||||
final profile = await CapabilityProfile.load();
|
||||
final printer = NetworkPrinter(paperSize, profile);
|
||||
|
||||
final PosPrintResult res = await printer.connect(ip, port: port);
|
||||
|
||||
if (res != PosPrintResult.success) {
|
||||
AppLogger.error('Could not connect to printer at $ip:$port');
|
||||
return false;
|
||||
}
|
||||
|
||||
AppLogger.print('Connected to printer, generating ticket...');
|
||||
|
||||
// Generate Ticket
|
||||
_buildInvoiceTicket(printer, invoice);
|
||||
|
||||
printer.feed(2);
|
||||
printer.cut();
|
||||
printer.disconnect();
|
||||
|
||||
return true;
|
||||
} catch (e) {
|
||||
AppLogger.error('Thermal printing failed', e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void _buildInvoiceTicket(NetworkPrinter printer, Map<String, dynamic> invoice) {
|
||||
// Header
|
||||
printer.text(
|
||||
'M U S A D A Q',
|
||||
styles: const PosStyles(
|
||||
align: PosAlign.center,
|
||||
height: PosTextSize.size2,
|
||||
width: PosTextSize.size2,
|
||||
bold: true,
|
||||
),
|
||||
);
|
||||
printer.text(
|
||||
'نظام الفوترة الإلكترونية',
|
||||
styles: const PosStyles(align: PosAlign.center),
|
||||
);
|
||||
printer.hr();
|
||||
|
||||
// Invoice Info
|
||||
printer.text('رقم الفاتورة: ${invoice['invoice_number'] ?? '-'}');
|
||||
printer.text('التاريخ: ${invoice['invoice_date'] ?? '-'}');
|
||||
printer.text('المورد: ${invoice['supplier_name'] ?? '-'}');
|
||||
if (invoice['supplier_tin'] != null) {
|
||||
printer.text('الرقم الضريبي: ${invoice['supplier_tin']}');
|
||||
}
|
||||
printer.hr();
|
||||
|
||||
// Table Header
|
||||
printer.row([
|
||||
PosColumn(text: 'البند', width: 6),
|
||||
PosColumn(text: 'الكمية', width: 2, styles: const PosStyles(align: PosAlign.center)),
|
||||
PosColumn(text: 'الإجمالي', width: 4, styles: const PosStyles(align: PosAlign.right)),
|
||||
]);
|
||||
printer.hr();
|
||||
|
||||
// Items
|
||||
final items = invoice['items'] as List? ?? [];
|
||||
for (var item in items) {
|
||||
printer.row([
|
||||
PosColumn(text: item['description'] ?? '-', width: 6),
|
||||
PosColumn(text: item['quantity']?.toString() ?? '1', width: 2, styles: const PosStyles(align: PosAlign.center)),
|
||||
PosColumn(text: item['line_total']?.toString() ?? '0', width: 4, styles: const PosStyles(align: PosAlign.right)),
|
||||
]);
|
||||
}
|
||||
printer.hr();
|
||||
|
||||
// Totals
|
||||
printer.row([
|
||||
PosColumn(text: 'المجموع الجزئي:', width: 8),
|
||||
PosColumn(text: '${invoice['subtotal'] ?? 0}', width: 4, styles: const PosStyles(align: PosAlign.right)),
|
||||
]);
|
||||
printer.row([
|
||||
PosColumn(text: 'الضريبة:', width: 8),
|
||||
PosColumn(text: '${invoice['tax_amount'] ?? 0}', width: 4, styles: const PosStyles(align: PosAlign.right)),
|
||||
]);
|
||||
printer.row([
|
||||
PosColumn(text: 'الإجمالي الكلي:', width: 8, styles: const PosStyles(bold: true)),
|
||||
PosColumn(text: '${invoice['grand_total'] ?? 0} JOD', width: 4, styles: const PosStyles(align: PosAlign.right, bold: true)),
|
||||
]);
|
||||
|
||||
printer.hr();
|
||||
printer.text(
|
||||
'شكراً لاستخدامكم مُصادَق',
|
||||
styles: const PosStyles(align: PosAlign.center, italic: true),
|
||||
);
|
||||
printer.text(
|
||||
'www.musadaq.com',
|
||||
styles: const PosStyles(align: PosAlign.center),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import 'package:share_plus/share_plus.dart';
|
||||
import '../../../core/network/dio_client.dart';
|
||||
import '../../../core/utils/app_snackbar.dart';
|
||||
import '../../../core/utils/logger.dart';
|
||||
import '../../../core/services/thermal_printer_service.dart';
|
||||
|
||||
class InvoiceDetailController extends GetxController {
|
||||
var invoice = {}.obs;
|
||||
@@ -269,4 +270,57 @@ class InvoiceDetailController extends GetxController {
|
||||
AppSnackbar.showError('خطأ', 'فشل إرسال الفاتورة لجوفوترا');
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> printThermalInvoice() async {
|
||||
final ipController = TextEditingController(text: '192.168.1.100'); // Default IP
|
||||
|
||||
final proceed = await Get.dialog<bool>(
|
||||
AlertDialog(
|
||||
title: const Text('إعدادات الطابعة الحرارية'),
|
||||
content: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
const Text('أدخل عنوان IP للطابعة (WiFi)'),
|
||||
const SizedBox(height: 12),
|
||||
TextField(
|
||||
controller: ipController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'IP Address',
|
||||
border: OutlineInputBorder(),
|
||||
hintText: 'e.g. 192.168.1.100',
|
||||
),
|
||||
keyboardType: TextInputType.number,
|
||||
),
|
||||
],
|
||||
),
|
||||
actions: [
|
||||
TextButton(onPressed: () => Get.back(result: false), child: const Text('إلغاء')),
|
||||
ElevatedButton(
|
||||
onPressed: () => Get.back(result: true),
|
||||
style: ElevatedButton.styleFrom(backgroundColor: const Color(0xFF0F4C81)),
|
||||
child: const Text('طباعة', style: TextStyle(color: Colors.white)),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
if (proceed == true && ipController.text.isNotEmpty) {
|
||||
try {
|
||||
AppSnackbar.showInfo('جاري الاتصال', 'يتم الاتصال بالطابعة...');
|
||||
final success = await ThermalPrinterService().printInvoice(
|
||||
ip: ipController.text.trim(),
|
||||
invoice: Map<String, dynamic>.from(invoice),
|
||||
);
|
||||
|
||||
if (success) {
|
||||
AppSnackbar.showSuccess('تمت الطباعة', 'تم إرسال الفاتورة للطابعة بنجاح');
|
||||
} else {
|
||||
AppSnackbar.showError('خطأ', 'فشل الاتصال بالطابعة. تأكد من عنوان IP والشبكة.');
|
||||
}
|
||||
} catch (e) {
|
||||
AppLogger.error('Print error', e);
|
||||
AppSnackbar.showError('خطأ', 'حدث خطأ أثناء الطباعة');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -109,6 +109,21 @@ class InvoiceDetailView extends StatelessWidget {
|
||||
const SizedBox(height: 12),
|
||||
],
|
||||
|
||||
SizedBox(
|
||||
height: 52,
|
||||
child: ElevatedButton.icon(
|
||||
onPressed: () => controller.printThermalInvoice(),
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: const Color(0xFF4F46E5),
|
||||
foregroundColor: Colors.white,
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
|
||||
),
|
||||
icon: const Icon(Icons.print_rounded),
|
||||
label: const Text('طباعة حرارية (WiFi)', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16)),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
|
||||
SizedBox(
|
||||
height: 52,
|
||||
child: OutlinedButton.icon(
|
||||
|
||||
@@ -6,6 +6,7 @@ import 'package:firebase_messaging/firebase_messaging.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:path/path.dart' as path;
|
||||
import 'package:file_picker/file_picker.dart';
|
||||
import 'package:image_picker/image_picker.dart';
|
||||
import '../../../core/services/upload_progress_service.dart';
|
||||
import '../../../core/utils/logger.dart';
|
||||
import '../../../core/utils/app_snackbar.dart';
|
||||
@@ -123,11 +124,92 @@ class ScannerController extends GetxController {
|
||||
addImage(file.path!);
|
||||
}
|
||||
}
|
||||
AppSnackbar.showSuccess('تمت الإضافة', 'تم استيراد ملفات الفواتير بنجاح');
|
||||
AppSnackbar.showSuccess('تمت الإضافة', 'تم استيراد ملفات PDF بنجاح');
|
||||
}
|
||||
} catch (e) {
|
||||
AppLogger.error('Failed to pick PDF', e);
|
||||
AppSnackbar.showError('خطأ', 'تعذر استيراد الملفات');
|
||||
AppSnackbar.showError('خطأ', 'تعذر استيراد ملفات PDF');
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> pickFromGallery() async {
|
||||
try {
|
||||
final ImagePicker picker = ImagePicker();
|
||||
final List<XFile> images = await picker.pickMultiImage();
|
||||
|
||||
if (images.isNotEmpty) {
|
||||
for (var image in images) {
|
||||
addImage(image.path);
|
||||
}
|
||||
AppSnackbar.showSuccess('تمت الإضافة', 'تم استيراد الصور من المعرض بنجاح');
|
||||
}
|
||||
} catch (e) {
|
||||
AppLogger.error('Failed to pick from gallery', e);
|
||||
AppSnackbar.showError('خطأ', 'تعذر استيراد الصور من المعرض');
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> pickExcelFile() async {
|
||||
if (selectedCompanyId.isEmpty) {
|
||||
AppSnackbar.showWarning('تنبيه', 'الرجاء اختيار الشركة أولاً');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
FilePickerResult? result = await FilePicker.platform.pickFiles(
|
||||
type: FileType.custom,
|
||||
allowedExtensions: ['xlsx', 'xls', 'csv'],
|
||||
allowMultiple: false,
|
||||
);
|
||||
|
||||
if (result != null && result.files.single.path != null) {
|
||||
final filePath = result.files.single.path!;
|
||||
await uploadExcel(filePath);
|
||||
}
|
||||
} catch (e) {
|
||||
AppLogger.error('Failed to pick Excel', e);
|
||||
AppSnackbar.showError('خطأ', 'تعذر استيراد ملف الإكسل');
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> uploadExcel(String filePath) async {
|
||||
try {
|
||||
isProcessing.value = true;
|
||||
uploadProgress.value = 0.0;
|
||||
|
||||
_progressService.startUpload(selectedCompanyName.value, 1);
|
||||
|
||||
final file = File(filePath);
|
||||
final fileName = file.path.split('/').last;
|
||||
|
||||
FormData formData = FormData.fromMap({
|
||||
'company_id': selectedCompanyId.value,
|
||||
'file': await MultipartFile.fromFile(file.path, filename: fileName),
|
||||
});
|
||||
|
||||
final response = await DioClient().client.post(
|
||||
'excel/import',
|
||||
data: formData,
|
||||
onSendProgress: (sent, total) {
|
||||
uploadProgress.value = sent / total;
|
||||
_progressService.updateProgress(uploadProgress.value, 1);
|
||||
},
|
||||
);
|
||||
|
||||
if (response.data['success'] == true) {
|
||||
_progressService.complete();
|
||||
AppSnackbar.showSuccess('تم بنجاح', response.data['message'] ?? 'تم استيراد البيانات بنجاح');
|
||||
Get.back();
|
||||
} else {
|
||||
_progressService.fail();
|
||||
AppSnackbar.showError('خطأ', response.data['message'] ?? 'فشل استيراد ملف الإكسل');
|
||||
}
|
||||
} catch (e) {
|
||||
_progressService.fail();
|
||||
AppLogger.error('Excel upload failed', e);
|
||||
AppSnackbar.showError('خطأ', 'حدث خطأ أثناء رفع ملف الإكسل');
|
||||
} finally {
|
||||
isProcessing.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -131,6 +131,30 @@ class ScannerView extends GetView<ScannerController> {
|
||||
tooltip: 'استيراد PDF',
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Container(
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.black45,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: IconButton(
|
||||
onPressed: () => controller.pickFromGallery(),
|
||||
icon: const Icon(Icons.photo_library, color: Colors.white),
|
||||
tooltip: 'من المعرض',
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Container(
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.black45,
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: IconButton(
|
||||
onPressed: () => controller.pickExcelFile(),
|
||||
icon: const Icon(Icons.table_chart, color: Colors.white),
|
||||
tooltip: 'استيراد Excel',
|
||||
),
|
||||
),
|
||||
const Spacer(),
|
||||
TextButton.icon(
|
||||
onPressed: () => Get.back(),
|
||||
|
||||
Reference in New Issue
Block a user