Barcode Printing Utility Done
This commit is contained in:
@@ -0,0 +1,122 @@
|
||||
import 'dart:convert';
|
||||
import 'label_document.dart';
|
||||
|
||||
class BarcodeTemplate {
|
||||
final int? id;
|
||||
final int? userId;
|
||||
final String templateName;
|
||||
final String? description;
|
||||
final String category;
|
||||
final int version;
|
||||
final double labelWidthMm;
|
||||
final double labelHeightMm;
|
||||
final String measurementUnit;
|
||||
final String printerLanguage;
|
||||
final int dpi;
|
||||
final String configurationJson;
|
||||
final String templateJson;
|
||||
final bool isActive;
|
||||
final DateTime? createdAt;
|
||||
final DateTime? updatedAt;
|
||||
|
||||
BarcodeTemplate({
|
||||
this.id,
|
||||
this.userId,
|
||||
required this.templateName,
|
||||
this.description,
|
||||
this.category = 'PRODUCT',
|
||||
this.version = 1,
|
||||
this.labelWidthMm = 50.0,
|
||||
this.labelHeightMm = 25.0,
|
||||
this.measurementUnit = 'mm',
|
||||
this.printerLanguage = 'ZPL',
|
||||
this.dpi = 203,
|
||||
this.configurationJson = '{}',
|
||||
required this.templateJson,
|
||||
this.isActive = true,
|
||||
this.createdAt,
|
||||
this.updatedAt,
|
||||
});
|
||||
|
||||
factory BarcodeTemplate.fromJson(Map<String, dynamic> json) {
|
||||
return BarcodeTemplate(
|
||||
id: json['id'] as int?,
|
||||
userId: json['userId'] as int?,
|
||||
templateName: json['templateName'] as String? ?? 'Untitled Template',
|
||||
description: json['description'] as String?,
|
||||
category: json['category'] as String? ?? 'PRODUCT',
|
||||
version: json['version'] as int? ?? 1,
|
||||
labelWidthMm: (json['labelWidthMm'] as num?)?.toDouble() ?? 50.0,
|
||||
labelHeightMm: (json['labelHeightMm'] as num?)?.toDouble() ?? 25.0,
|
||||
measurementUnit: json['measurementUnit'] as String? ?? 'mm',
|
||||
printerLanguage: json['printerLanguage'] as String? ?? 'ZPL',
|
||||
dpi: json['dpi'] as int? ?? 203,
|
||||
configurationJson: json['configurationJson'] as String? ?? '{}',
|
||||
templateJson: json['templateJson'] as String? ?? '{}',
|
||||
isActive: json['isActive'] as bool? ?? true,
|
||||
createdAt: json['createdAt'] != null ? DateTime.tryParse(json['createdAt'].toString()) : null,
|
||||
updatedAt: json['updatedAt'] != null ? DateTime.tryParse(json['updatedAt'].toString()) : null,
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() => {
|
||||
if (id != null) 'id': id,
|
||||
if (userId != null) 'userId': userId,
|
||||
'templateName': templateName,
|
||||
'description': description,
|
||||
'category': category,
|
||||
'version': version,
|
||||
'labelWidthMm': labelWidthMm,
|
||||
'labelHeightMm': labelHeightMm,
|
||||
'measurementUnit': measurementUnit,
|
||||
'printerLanguage': printerLanguage,
|
||||
'dpi': dpi,
|
||||
'configurationJson': configurationJson,
|
||||
'templateJson': templateJson,
|
||||
'isActive': isActive,
|
||||
};
|
||||
|
||||
LabelDocument toLabelDocument() {
|
||||
try {
|
||||
final parsed = jsonDecode(templateJson) as Map<String, dynamic>;
|
||||
return LabelDocument.fromJson(
|
||||
parsed,
|
||||
id: id?.toString(),
|
||||
name: templateName,
|
||||
description: description,
|
||||
);
|
||||
} catch (_) {
|
||||
return LabelDocument(
|
||||
id: id?.toString() ?? 'temp',
|
||||
name: templateName,
|
||||
description: description,
|
||||
version: version,
|
||||
config: LabelConfiguration(
|
||||
widthMm: labelWidthMm,
|
||||
heightMm: labelHeightMm,
|
||||
dpi: dpi,
|
||||
printerLanguage: printerLanguage,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
static BarcodeTemplate fromLabelDocument({
|
||||
int? id,
|
||||
required LabelDocument doc,
|
||||
}) {
|
||||
return BarcodeTemplate(
|
||||
id: id,
|
||||
templateName: doc.name,
|
||||
description: doc.description,
|
||||
version: doc.version,
|
||||
labelWidthMm: doc.config.widthMm,
|
||||
labelHeightMm: doc.config.heightMm,
|
||||
measurementUnit: 'mm',
|
||||
printerLanguage: doc.config.printerLanguage,
|
||||
dpi: doc.config.dpi,
|
||||
configurationJson: jsonEncode(doc.config.toJson()),
|
||||
templateJson: doc.toJsonString(),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user