Created
February 19, 2024 04:27
-
-
Save ANNASBlackHat/e2a7875cb655691c60629d40c3188cee to your computer and use it in GitHub Desktop.
Operational Cost Print Format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:core'; | |
import 'package:intl/intl.dart'; | |
void main() { | |
Outlet outlet = dummyOutlet(); | |
Employee employee = Employee( | |
name: 'Jane Doe', | |
); | |
OperationalCostEntity opCost = generateDummyOperationalCosts()[0]; | |
String report = getOperationalCostFormat(opCost, outlet, employee); | |
print(report); | |
} | |
String getOperationalCostFormat( | |
OperationalCostEntity operationalCost, | |
Outlet outlet, | |
Employee employee, | |
{int paperSize = 58} | |
) { | |
StringBuffer teks = StringBuffer(); | |
StringBuffer header = StringBuffer(Constant.PRINTER_CODE_LOGO); | |
final int MAX = getMaxCharacter(paperSize); | |
header.writeln(outlet.name.center(MAX)); | |
header.writeln(outlet.receiptAddress?.center(MAX)); | |
if(outlet.receiptPhone != null) { | |
header.writeln(outlet.receiptPhone!.center(MAX)); | |
} | |
header.writeln("=".loop(MAX)); | |
String bankName = operationalCost.bankName ?? ""; | |
teks.writeln("Supplier : ${operationalCost.supplierName}".width(MAX)); | |
teks.writeln("Tanggal : ${operationalCost.timeCreated?.dateTimeFormat()}".width(MAX)); | |
teks.writeln("Kasir : ${employee.name}".width(MAX)); | |
teks.writeln("Pembayaran : ${operationalCost.payment} $bankName".width(MAX)); | |
teks.writeln(); | |
teks.write(operationalCost.opcostName?.width(MAX - 4 - 8)); | |
teks.write(operationalCost.qty.toCurrency().width(4)); | |
teks.writeln(operationalCost.total.toCurrency().width(8)); | |
teks.write("\npowered by www.uniq.id\n\n"); | |
return header.toString() + teks.toString(); | |
} | |
List<OperationalCostEntity> generateDummyOperationalCosts() { | |
return List.generate(5, (index) { | |
return OperationalCostEntity( | |
id: index + 1, | |
opcostName: 'Operational Cost ${index + 1}', | |
total: (1000 * (index + 1)).toInt(), | |
payment: 'Payment Method ${index + 1}', | |
qty: (index + 1) * 2, | |
supplierName: 'Supplier ${index + 1}', | |
timeCreated: DateTime.now().millisecondsSinceEpoch, | |
bankName: 'Bank ${index + 1}', | |
); | |
}); | |
} | |
int getMaxCharacter(int? paperSize) { | |
switch (paperSize) { | |
case 75: | |
return 40; | |
case 80: | |
return 48; | |
default: | |
return 32; | |
} | |
} | |
extension IntExtension on int? { | |
String toCurrency({String prefix = ''}) { | |
var num = NumberFormat.decimalPattern(); | |
try { | |
return prefix + num.format(this ?? 0); | |
} catch (e) { | |
return '${prefix}0'; | |
} | |
} | |
String dateTimeFormat([String? format]) { | |
final DateFormat sdf = DateFormat(format ?? 'dd-MM-yyyy HH:mm'); | |
return sdf.format(DateTime.fromMillisecondsSinceEpoch(this ?? 0)); | |
} | |
} | |
extension StringExtensions on String { | |
String safeRepeat(int? count) { | |
return count != null && count > 0 ? this * count : ''; | |
} | |
String loop(int w) { | |
var newVal = ''; | |
for (var i = 0; i < w; i++) { | |
newVal += this; | |
} | |
return newVal; | |
} | |
String width(int width, {int max = 32, int before = 0}) { | |
try { | |
var cursor = 0; | |
var wordSplit = StringBuffer(); | |
do { | |
var suffix = ''; | |
var start = cursor; | |
var end = (start + width) < length ? start + width : length; | |
var cutWord = substring(start, end); | |
var lastSpace = cutWord.contains(' ') && end != length | |
? cutWord.lastIndexOf(' ') | |
: cutWord.length; | |
if (lastSpace < (width ~/ 2) && (width - 1) < cutWord.length) { | |
lastSpace = width - 1; | |
cursor--; | |
suffix = '-'; | |
} | |
cursor += lastSpace + 1; | |
suffix += cursor < length | |
? ' '.padRight(max - (before + lastSpace)) + '\n' | |
: ' '.padRight(width - (before + lastSpace)); | |
wordSplit.write(cutWord.substring(0, lastSpace) + suffix); | |
} while (cursor < length); | |
return wordSplit.toString(); | |
} catch (e) { | |
// Handle error | |
} | |
// Fallback logic | |
return padRight(width); | |
} | |
String widthRight(int w) { | |
return padLeft(w); | |
} | |
String center(int width, [String flanks = ' ']) { | |
if (length == width) { | |
return this; | |
} else if (length < width) { | |
var center = width ~/ 2; | |
var flank = center - (length ~/ 2); | |
var newValue = StringBuffer(); | |
for (var i = 0; i < flank; i++) { | |
newValue.write(flanks); | |
} | |
newValue.write(this); | |
for (var i = newValue.length; i < width; i++) { | |
newValue.write(flanks); | |
} | |
return newValue.toString(); | |
} else { | |
// Handle case where length > width | |
} | |
return ''; | |
} | |
} | |
class Constant { | |
static const String PRINTER_CODE_LOGO = "[CUT]"; | |
} | |
class OperationalCostEntity { | |
final int id; | |
final String opcostName; | |
final int total; | |
final String? supplierName; | |
final int? timeCreated; | |
final String payment; | |
final String? bankName; | |
final int qty; | |
OperationalCostEntity({ | |
required this.id, | |
required this.opcostName, | |
required this.total, | |
required this.payment, | |
required this.qty, | |
this.supplierName, | |
this.timeCreated, | |
this.bankName, | |
}); | |
factory OperationalCostEntity.fromJson(Map<String, dynamic> json) { | |
return OperationalCostEntity( | |
id: json['id'] as int, | |
opcostName: json['opcostName'] as String, | |
total: json['total'] as int, | |
payment: json['payment'] as String, | |
qty: json['qty'] as int, | |
supplierName: json['supplierName'] as String?, | |
timeCreated: json['timeCreated'] as int?, | |
bankName: json['bankName'] as String?, | |
); | |
} | |
Map<String, dynamic> toJson() { | |
final Map<String, dynamic> data = <String, dynamic>{}; | |
data['id'] = id; | |
data['opcostName'] = opcostName; | |
data['total'] = total; | |
data['payment'] = payment; | |
data['qty'] = qty; | |
data['supplierName'] = supplierName; | |
data['timeCreated'] = timeCreated; | |
data['bankName'] = bankName; | |
return data; | |
} | |
} | |
class Outlet { | |
String name; | |
String? receiptAddress; | |
String? receiptPhone; | |
String? receiptNote; | |
String? receiptSocialmedia; | |
Outlet({ | |
required this.name, | |
this.receiptAddress, | |
this.receiptPhone, | |
this.receiptNote, | |
this.receiptSocialmedia, | |
}); | |
} | |
// Employee model | |
class Employee { | |
String name; | |
Employee({ | |
required this.name, | |
}); | |
} | |
Outlet dummyOutlet() { | |
return Outlet( | |
name: 'Outlet ABC', | |
receiptAddress: '123 Main Street', | |
receiptPhone: '123-456-7890', | |
receiptNote: 'Thank you for your visit!', | |
receiptSocialmedia: '@OutletABC', | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment