74 lines
2.2 KiB
Dart
74 lines
2.2 KiB
Dart
import '../models/bauteil_model.dart';
|
|
import '../models/kauf_filament_model.dart';
|
|
import 'abfall_berechnung.dart';
|
|
|
|
class BauteilPreis {
|
|
const BauteilPreis({
|
|
required this.materialKosten,
|
|
required this.abfallKosten,
|
|
required this.aufschlagProzent,
|
|
required this.gesamtpreis,
|
|
});
|
|
final double materialKosten;
|
|
final double abfallKosten;
|
|
final double aufschlagProzent;
|
|
final double gesamtpreis;
|
|
double get aufschlagKosten => double.parse(
|
|
(gesamtpreis - materialKosten - abfallKosten).toStringAsFixed(2),
|
|
);
|
|
}
|
|
|
|
class BauteilPreisBerechnung {
|
|
static double _cent(double value) => double.parse(value.toStringAsFixed(2));
|
|
|
|
static BauteilPreis? berechne(
|
|
BauteilModel entry,
|
|
Iterable<KaufFilamentModel> rollen,
|
|
double aufschlag, {
|
|
BauteilModel? previous,
|
|
}) {
|
|
if (!entry.gewichtGramm.isFinite ||
|
|
entry.gewichtGramm <= 0 ||
|
|
!entry.abfallGramm.isFinite ||
|
|
entry.abfallGramm < 0 ||
|
|
!aufschlag.isFinite ||
|
|
aufschlag < 0) {
|
|
return null;
|
|
}
|
|
double material;
|
|
double abfall;
|
|
if (previous?.materialKosten != null &&
|
|
previous?.abfallKosten != null &&
|
|
AbfallBerechnung.gleicheBasis(entry, previous!)) {
|
|
material = previous.materialKosten!;
|
|
abfall = previous.abfallKosten!;
|
|
} else {
|
|
if (entry.filamente.length > 1) {
|
|
final summe = entry.filamente.fold<double>(
|
|
0,
|
|
(sum, item) => sum + item.verbrauchGramm,
|
|
);
|
|
if (!summe.isFinite ||
|
|
(summe - entry.gesamtverbrauchGramm).abs() > .01) {
|
|
return null;
|
|
}
|
|
}
|
|
final preis = AbfallBerechnung.preisProGramm(entry, rollen);
|
|
if (preis == null) return null;
|
|
final materialRoh = entry.gewichtGramm * preis;
|
|
final abfallRoh = entry.abfallGramm * preis;
|
|
if (!materialRoh.isFinite || !abfallRoh.isFinite) return null;
|
|
material = _cent(materialRoh);
|
|
abfall = _cent(abfallRoh);
|
|
}
|
|
final gesamt = (material + abfall) * (1 + aufschlag / 100);
|
|
if (!gesamt.isFinite) return null;
|
|
return BauteilPreis(
|
|
materialKosten: material,
|
|
abfallKosten: abfall,
|
|
aufschlagProzent: aufschlag,
|
|
gesamtpreis: _cent(gesamt),
|
|
);
|
|
}
|
|
}
|