feat: syn product

This commit is contained in:
efrilm
2025-08-03 00:35:00 +07:00
parent 576f687d21
commit 7678b4791d
28 changed files with 1922 additions and 948 deletions
+49 -53
View File
@@ -60,12 +60,12 @@ class PrintDataoutputs {
bytes += generator.row([
PosColumn(
text:
'${product.product.price!.toIntegerFromText.currencyFormatRp} x ${product.quantity}',
'${product.product.price!.currencyFormatRp} x ${product.quantity}',
width: 8,
styles: const PosStyles(align: PosAlign.left),
),
PosColumn(
text: '${product.product.price!.toIntegerFromText * product.quantity}'
text: '${product.product.price! * product.quantity}'
.toIntegerFromText
.currencyFormatRp,
width: 4,
@@ -328,8 +328,7 @@ class PrintDataoutputs {
styles: const PosStyles(align: PosAlign.left),
),
PosColumn(
text: (product.product.price!.toIntegerFromText * product.quantity)
.currencyFormatRp,
text: (product.product.price! * product.quantity).currencyFormatRp,
width: 4,
styles: const PosStyles(align: PosAlign.right),
),
@@ -398,8 +397,7 @@ class PrintDataoutputs {
styles: const PosStyles(align: PosAlign.left),
),
PosColumn(
text: (products[0].product.price!.toIntegerFromText *
products[0].quantity)
text: (products[0].product.price! * products[0].quantity)
.currencyFormatRp,
width: 4,
styles: const PosStyles(align: PosAlign.right),
@@ -430,8 +428,7 @@ class PrintDataoutputs {
styles: const PosStyles(align: PosAlign.left),
),
PosColumn(
text: (products[0].product.price!.toIntegerFromText *
products[0].quantity)
text: (products[0].product.price! * products[0].quantity)
.currencyFormatRp,
width: 4,
styles: const PosStyles(align: PosAlign.right),
@@ -610,8 +607,8 @@ class PrintDataoutputs {
styles: const PosStyles(bold: true, align: PosAlign.left),
),
PosColumn(
text: '${product.product.price!.toIntegerFromText * product.quantity}'
.currencyFormatRpV2,
text:
'${product.product.price! * product.quantity}'.currencyFormatRpV2,
width: 4,
styles: const PosStyles(bold: true, align: PosAlign.right),
),
@@ -626,8 +623,7 @@ class PrintDataoutputs {
final subTotalPrice = products.fold<int>(
0,
(previousValue, element) =>
previousValue +
(element.product.price!.toIntegerFromText * element.quantity));
previousValue + (element.product.price! * element.quantity));
bytes += generator.row([
PosColumn(
text: 'Subtotal $totalQuantity Product',
@@ -995,27 +991,27 @@ class PrintDataoutputs {
: '--------------------------------',
styles: const PosStyles(bold: false, align: PosAlign.center));
bytes += generator.feed(1);
final kitchenProducts =
products.where((p) => p.product.printerType == 'kitchen');
for (final product in kitchenProducts) {
bytes += generator.text('${product.quantity} x ${product.product.name}',
styles: const PosStyles(
align: PosAlign.left,
bold: false,
height: PosTextSize.size2,
width: PosTextSize.size1,
));
if (product.notes.isNotEmpty) {
bytes += generator.text(' Notes: ${product.notes}',
styles: const PosStyles(
align: PosAlign.left,
bold: false,
height: PosTextSize.size1,
width: PosTextSize.size1,
fontType: PosFontType.fontA,
));
}
}
// final kitchenProducts =
// products.where((p) => p.product.printerType == 'kitchen');
// for (final product in kitchenProducts) {
// bytes += generator.text('${product.quantity} x ${product.product.name}',
// styles: const PosStyles(
// align: PosAlign.left,
// bold: false,
// height: PosTextSize.size2,
// width: PosTextSize.size1,
// ));
// if (product.notes.isNotEmpty) {
// bytes += generator.text(' Notes: ${product.notes}',
// styles: const PosStyles(
// align: PosAlign.left,
// bold: false,
// height: PosTextSize.size1,
// width: PosTextSize.size1,
// fontType: PosFontType.fontA,
// ));
// }
// }
bytes += generator.feed(1);
bytes += generator.text(
@@ -1109,26 +1105,26 @@ class PrintDataoutputs {
: '--------------------------------',
styles: const PosStyles(bold: false, align: PosAlign.center));
bytes += generator.feed(1);
final barProducts = products.where((p) => p.product.printerType == 'bar');
for (final product in barProducts) {
bytes += generator.text('${product.quantity} x ${product.product.name}',
styles: const PosStyles(
align: PosAlign.left,
bold: false,
height: PosTextSize.size2,
width: PosTextSize.size1,
));
if (product.notes.isNotEmpty) {
bytes += generator.text(' Notes: ${product.notes}',
styles: const PosStyles(
align: PosAlign.left,
bold: false,
height: PosTextSize.size1,
width: PosTextSize.size1,
fontType: PosFontType.fontA,
));
}
}
// final barProducts = products.where((p) => p.product.printerType == 'bar');
// for (final product in barProducts) {
// bytes += generator.text('${product.quantity} x ${product.product.name}',
// styles: const PosStyles(
// align: PosAlign.left,
// bold: false,
// height: PosTextSize.size2,
// width: PosTextSize.size1,
// ));
// if (product.notes.isNotEmpty) {
// bytes += generator.text(' Notes: ${product.notes}',
// styles: const PosStyles(
// align: PosAlign.left,
// bold: false,
// height: PosTextSize.size1,
// width: PosTextSize.size1,
// fontType: PosFontType.fontA,
// ));
// }
// }
bytes += generator.feed(1);
bytes += generator.text(
@@ -308,7 +308,7 @@ class ProductLocalDatasource {
tableProduct,
product.toLocalMap(),
where: 'product_id = ?',
whereArgs: [product.productId],
whereArgs: [product.id],
);
}
@@ -319,7 +319,7 @@ class ProductLocalDatasource {
for (var product in products) {
await db.insert(tableProduct, product.toLocalMap(),
conflictAlgorithm: ConflictAlgorithm.replace);
log('inserted success id: ${product.productId} | name: ${product.name} | price: ${product.price} | Printer Type ${product.printerType}');
log('inserted success id: ${product.id} | name: ${product.name} | price: ${product.price} ');
}
}
@@ -1,6 +1,8 @@
import 'dart:developer';
import 'package:dartz/dartz.dart';
import 'package:dio/dio.dart';
import 'package:enaklo_pos/core/network/dio_client.dart';
import 'package:enaklo_pos/data/models/request/product_request_model.dart';
import 'package:enaklo_pos/data/models/response/add_product_response_model.dart';
import 'package:enaklo_pos/data/models/response/product_response_model.dart';
@@ -10,19 +12,34 @@ import '../../core/constants/variables.dart';
import 'auth_local_datasource.dart';
class ProductRemoteDatasource {
final Dio dio = DioClient.instance;
Future<Either<String, ProductResponseModel>> getProducts() async {
final url = Uri.parse('${Variables.baseUrl}/api/products');
final authData = await AuthLocalDataSource().getAuthData();
final response = await http.get(url, headers: {
'Authorization': 'Bearer ${authData.token}',
'Accept': 'application/json',
});
log("Status Code: ${response.statusCode}");
log("Body: ${response.body}");
if (response.statusCode == 200) {
return Right(ProductResponseModel.fromJson(response.body));
} else {
return const Left('Failed to get products');
try {
final authData = await AuthLocalDataSource().getAuthData();
final url = '${Variables.baseUrl}/api/v1/products';
final response = await dio.get(
url,
options: Options(
headers: {
'Authorization': 'Bearer ${authData.token}',
'Accept': 'application/json',
},
),
);
if (response.statusCode == 200) {
return Right(ProductResponseModel.fromMap(response.data));
} else {
return const Left('Failed to get products');
}
} on DioException catch (e) {
log("Dio error: ${e.message}");
return Left(e.response?.data['message'] ?? 'Gagal mengambil produk');
} catch (e) {
log("Unexpected error: $e");
return const Left('Unexpected error occurred');
}
}
@@ -57,7 +74,7 @@ class ProductRemoteDatasource {
final Map<String, String> headers = {
'Authorization': 'Bearer ${authData.token}',
};
log("Update Product Request Data: ${productRequestModel.toMap()}");
log("Update Product ID: ${productRequestModel.id}");
log("Update Product Name: ${productRequestModel.name}");
@@ -67,7 +84,7 @@ class ProductRemoteDatasource {
log("Update Product Is Best Seller: ${productRequestModel.isBestSeller}");
log("Update Product Printer Type: ${productRequestModel.printerType}");
log("Update Product Has Image: ${productRequestModel.image != null}");
var request = http.MultipartRequest(
'POST', Uri.parse('${Variables.baseUrl}/api/products/edit'));
request.fields.addAll(productRequestModel.toMap());
@@ -3,7 +3,7 @@ import 'dart:developer';
import 'package:image_picker/image_picker.dart';
class ProductRequestModel {
final int? id;
final String? id;
final String name;
final int price;
final int stock;
@@ -32,11 +32,11 @@ class ProductRequestModel {
'is_best_seller': isBestSeller.toString(),
'printer_type': printerType ?? '',
};
if (id != null) {
map['id'] = id.toString();
}
return map;
}
}
@@ -1,17 +1,15 @@
// ignore_for_file: public_member_api_docs, sort_constructors_first
import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:enaklo_pos/presentation/home/pages/confirm_payment_page.dart';
class ProductResponseModel {
final String? status;
final List<Product>? data;
final bool? success;
final ProductData? data;
final dynamic errors;
ProductResponseModel({
this.status,
this.success,
this.data,
this.errors,
});
factory ProductResponseModel.fromJson(String str) =>
@@ -21,50 +19,86 @@ class ProductResponseModel {
factory ProductResponseModel.fromMap(Map<String, dynamic> json) =>
ProductResponseModel(
status: json["status"],
data: json["data"] == null
? []
: List<Product>.from(json["data"]!.map((x) => Product.fromMap(x))),
success: json["success"],
data: json["data"] == null ? null : ProductData.fromMap(json["data"]),
errors: json["errors"],
);
Map<String, dynamic> toMap() => {
"status": status,
"data":
data == null ? [] : List<dynamic>.from(data!.map((x) => x.toMap())),
"success": success,
"data": data?.toMap(),
"errors": errors,
};
}
class ProductData {
final List<Product>? products;
final int? totalCount;
final int? page;
final int? limit;
final int? totalPages;
ProductData({
this.products,
this.totalCount,
this.page,
this.limit,
this.totalPages,
});
factory ProductData.fromMap(Map<String, dynamic> json) => ProductData(
products: json["products"] == null
? []
: List<Product>.from(
json["products"].map((x) => Product.fromMap(x))),
totalCount: json["total_count"],
page: json["page"],
limit: json["limit"],
totalPages: json["total_pages"],
);
Map<String, dynamic> toMap() => {
"products": products == null
? []
: List<dynamic>.from(products!.map((x) => x.toMap())),
"total_count": totalCount,
"page": page,
"limit": limit,
"total_pages": totalPages,
};
}
class Product {
final int? id;
final int? productId;
final int? categoryId;
final String? id;
final String? organizationId;
final String? categoryId;
final String? sku;
final String? name;
final String? description;
final String? image;
final String? price;
final int? stock;
final int? status;
final int? isFavorite;
final int? price;
final int? cost;
final String? businessType;
final Map<String, dynamic>? metadata;
final bool? isActive;
final DateTime? createdAt;
final DateTime? updatedAt;
final Category? category;
final String? printerType;
final List<ProductVariant>? variants;
Product({
this.id,
this.productId,
this.organizationId,
this.categoryId,
this.sku,
this.name,
this.description,
this.image,
this.price,
this.stock,
this.status,
this.isFavorite,
this.cost,
this.businessType,
this.metadata,
this.isActive,
this.createdAt,
this.updatedAt,
this.category,
this.printerType,
this.variants,
});
factory Product.fromJson(String str) => Product.fromMap(json.decode(str));
@@ -72,91 +106,94 @@ class Product {
String toJson() => json.encode(toMap());
factory Product.fromMap(Map<String, dynamic> json) => Product(
id: json["id"] is String ? int.tryParse(json["id"]) : json["id"],
productId: json["product_id"] is String ? int.tryParse(json["product_id"]) : json["product_id"],
categoryId: json["category_id"] is String
? int.tryParse(json["category_id"])
: json["category_id"],
id: json["id"],
organizationId: json["organization_id"],
categoryId: json["category_id"],
sku: json["sku"],
name: json["name"],
description: json["description"],
image: json["image"],
// price: json["price"].substring(0, json["price"].length - 3),
price: json["price"].toString().replaceAll('.00', ''),
stock: json["stock"] is String ? int.tryParse(json["stock"]) : json["stock"],
status: json["status"] is String ? int.tryParse(json["status"]) : json["status"],
isFavorite: json["is_favorite"] is String ? int.tryParse(json["is_favorite"]) : json["is_favorite"],
price: json["price"],
cost: json["cost"],
businessType: json["business_type"],
metadata: json["metadata"] ?? {},
isActive: json["is_active"],
createdAt: json["created_at"] == null
? null
: DateTime.parse(json["created_at"]),
updatedAt: json["updated_at"] == null
? null
: DateTime.parse(json["updated_at"]),
category: json["category"] == null
? null
: Category.fromMap(json["category"]),
printerType: json["printer_type"] ?? 'bar',
variants: json["variants"] == null
? []
: List<ProductVariant>.from(
json["variants"].map((x) => ProductVariant.fromMap(x))),
);
factory Product.fromOrderMap(Map<String, dynamic> json) => Product(
id: json["id_product"],
price: json["price"].toString(),
price: json["price"],
);
factory Product.fromLocalMap(Map<String, dynamic> json) => Product(
id: json["id"],
productId: json["product_id"],
categoryId: json["categoryId"],
category: Category(
id: json["categoryId"],
name: json["categoryName"],
),
organizationId: json["organization_id"],
categoryId: json["category_id"],
sku: json["sku"],
name: json["name"],
description: json["description"],
image: json["image"],
price: json["price"],
stock: json["stock"],
status: json["status"],
isFavorite: json["isFavorite"],
createdAt: json["createdAt"] == null
cost: json["cost"],
businessType: json["business_type"],
metadata: json["metadata"] ?? {},
isActive: json["is_active"],
createdAt: json["created_at"] == null
? null
: DateTime.parse(json["createdAt"]),
updatedAt: json["updatedAt"] == null
: DateTime.parse(json["created_at"]),
updatedAt: json["updated_at"] == null
? null
: DateTime.parse(json["updatedAt"]),
printerType: json["printer_type"] ?? 'bar',
: DateTime.parse(json["updated_at"]),
variants: json["variants"] == null
? []
: List<ProductVariant>.from(
json["variants"].map((x) => ProductVariant.fromMap(x))),
);
Map<String, dynamic> toLocalMap() => {
"product_id": id,
"categoryId": categoryId,
"categoryName": category?.name,
"id": id,
"organization_id": organizationId,
"category_id": categoryId,
"sku": sku,
"name": name,
"description": description,
"image": image,
"price": price?.replaceAll(RegExp(r'\.0+$'), ''),
"stock": stock,
"status": status,
"isFavorite": isFavorite,
"createdAt": createdAt?.toIso8601String(),
"updatedAt": updatedAt?.toIso8601String(),
"printer_type": printerType,
"price": price,
"cost": cost,
"business_type": businessType,
"metadata": metadata,
"is_active": isActive,
"created_at": createdAt?.toIso8601String(),
"updated_at": updatedAt?.toIso8601String(),
"variants": variants == null
? []
: List<dynamic>.from(variants!.map((x) => x.toMap())),
};
Map<String, dynamic> toMap() => {
"id": id,
"product_id": productId,
"organization_id": organizationId,
"category_id": categoryId,
"sku": sku,
"name": name,
"description": description,
"image": image,
"price": price,
"stock": stock,
"status": status,
"is_favorite": isFavorite,
"cost": cost,
"business_type": businessType,
"metadata": metadata,
"is_active": isActive,
"created_at": createdAt?.toIso8601String(),
"updated_at": updatedAt?.toIso8601String(),
"category": category?.toMap(),
"printer_type": printerType,
"variants": variants == null
? []
: List<dynamic>.from(variants!.map((x) => x.toMap())),
};
@override
@@ -164,70 +201,80 @@ class Product {
if (identical(this, other)) return true;
return other.id == id &&
other.productId == productId &&
other.organizationId == organizationId &&
other.categoryId == categoryId &&
other.sku == sku &&
other.name == name &&
other.description == description &&
other.image == image &&
other.price == price &&
other.stock == stock &&
other.status == status &&
other.isFavorite == isFavorite &&
other.cost == cost &&
other.businessType == businessType &&
other.metadata == metadata &&
other.isActive == isActive &&
other.createdAt == createdAt &&
other.updatedAt == updatedAt &&
other.category == category &&
other.printerType == printerType;
_listEquals(other.variants, variants);
}
bool _listEquals(List<ProductVariant>? a, List<ProductVariant>? b) {
if (a == null && b == null) return true;
if (a == null || b == null) return false;
if (a.length != b.length) return false;
for (int i = 0; i < a.length; i++) {
if (a[i] != b[i]) return false;
}
return true;
}
@override
int get hashCode {
return id.hashCode ^
productId.hashCode ^
organizationId.hashCode ^
categoryId.hashCode ^
sku.hashCode ^
name.hashCode ^
description.hashCode ^
image.hashCode ^
price.hashCode ^
stock.hashCode ^
status.hashCode ^
isFavorite.hashCode ^
cost.hashCode ^
businessType.hashCode ^
metadata.hashCode ^
isActive.hashCode ^
createdAt.hashCode ^
updatedAt.hashCode ^
category.hashCode ^
printerType.hashCode;
variants.hashCode;
}
Product copyWith({
int? id,
int? productId,
int? categoryId,
String? id,
String? organizationId,
String? categoryId,
String? sku,
String? name,
String? description,
String? image,
String? price,
int? stock,
int? status,
int? isFavorite,
int? price,
int? cost,
String? businessType,
Map<String, dynamic>? metadata,
bool? isActive,
DateTime? createdAt,
DateTime? updatedAt,
Category? category,
String? printerType,
List<ProductVariant>? variants,
}) {
return Product(
id: id ?? this.id,
productId: productId ?? this.productId,
organizationId: organizationId ?? this.organizationId,
categoryId: categoryId ?? this.categoryId,
sku: sku ?? this.sku,
name: name ?? this.name,
description: description ?? this.description,
image: image ?? this.image,
price: price ?? this.price,
stock: stock ?? this.stock,
status: status ?? this.status,
isFavorite: isFavorite ?? this.isFavorite,
cost: cost ?? this.cost,
businessType: businessType ?? this.businessType,
metadata: metadata ?? this.metadata,
isActive: isActive ?? this.isActive,
createdAt: createdAt ?? this.createdAt,
updatedAt: updatedAt ?? this.updatedAt,
category: category ?? this.category,
printerType: printerType ?? this.printerType,
variants: variants ?? this.variants,
);
}
}
@@ -297,3 +344,51 @@ class Category {
updatedAt.hashCode;
}
}
class ProductVariant {
final String? id;
final String? productId;
final String? name;
final int? priceModifier;
final int? cost;
final Map<String, dynamic>? metadata;
final DateTime? createdAt;
final DateTime? updatedAt;
ProductVariant({
this.id,
this.productId,
this.name,
this.priceModifier,
this.cost,
this.metadata,
this.createdAt,
this.updatedAt,
});
factory ProductVariant.fromMap(Map<String, dynamic> json) => ProductVariant(
id: json["id"],
productId: json["product_id"],
name: json["name"],
priceModifier: json["price_modifier"],
cost: json["cost"],
metadata: json["metadata"] ?? {},
createdAt: json["created_at"] == null
? null
: DateTime.parse(json["created_at"]),
updatedAt: json["updated_at"] == null
? null
: DateTime.parse(json["updated_at"]),
);
Map<String, dynamic> toMap() => {
"id": id,
"product_id": productId,
"name": name,
"price_modifier": priceModifier,
"cost": cost,
"metadata": metadata,
"created_at": createdAt?.toIso8601String(),
"updated_at": updatedAt?.toIso8601String(),
};
}