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
@@ -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(),
};
}