apskel-pos-flutter/lib/data/models/response/product_response_model.dart
2025-09-03 12:15:43 +07:00

311 lines
8.5 KiB
Dart

// ignore_for_file: public_member_api_docs, sort_constructors_first
import 'dart:convert';
int? parseInt(dynamic value) {
if (value == null) return null;
if (value is int) return value;
if (value is double) return value.toInt();
if (value is String) return int.tryParse(value);
return null;
}
class ProductResponseModel {
final bool? success;
final ProductData? data;
final dynamic errors;
ProductResponseModel({
this.success,
this.data,
this.errors,
});
factory ProductResponseModel.fromJson(String str) =>
ProductResponseModel.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory ProductResponseModel.fromMap(Map<String, dynamic> json) =>
ProductResponseModel(
success: json["success"],
data: json["data"] == null ? null : ProductData.fromMap(json["data"]),
errors: json["errors"],
);
Map<String, dynamic> 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: parseInt(json["total_count"]),
page: parseInt(json["page"]),
limit: parseInt(json["limit"]),
totalPages: parseInt(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 String? id;
final String? organizationId;
final String? categoryId;
final String? sku;
final String? name;
final String? description;
final int? price;
final int? cost;
final String? businessType;
final String? imageUrl;
final String? printerType;
final Map<String, dynamic>? metadata;
final bool? isActive;
final DateTime? createdAt;
final DateTime? updatedAt;
final List<ProductVariant>? variants;
Product({
this.id,
this.organizationId,
this.categoryId,
this.sku,
this.name,
this.description,
this.price,
this.cost,
this.businessType,
this.imageUrl,
this.printerType,
this.metadata,
this.isActive,
this.createdAt,
this.updatedAt,
this.variants,
});
factory Product.fromJson(String str) => Product.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory Product.fromMap(Map<String, dynamic> json) => Product(
id: json["id"],
organizationId: json["organization_id"],
categoryId: json["category_id"],
sku: json["sku"],
name: json["name"],
description: json["description"],
price: parseInt(json["price"]),
cost: parseInt(json["cost"]),
businessType: json["business_type"],
imageUrl: json["image_url"],
printerType: json["printer_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"]),
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: parseInt(json["price"]),
);
factory Product.fromLocalMap(Map<String, dynamic> json) => Product(
id: json["id"],
organizationId: json["organization_id"],
categoryId: json["category_id"],
sku: json["sku"],
name: json["name"],
description: json["description"],
price: parseInt(json["price"]),
cost: parseInt(json["cost"]),
businessType: json["business_type"],
imageUrl: json["image_url"],
printerType: json["printer_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"]),
variants: json["variants"] == null
? []
: List<ProductVariant>.from(
json["variants"].map((x) => ProductVariant.fromMap(x))),
);
Map<String, dynamic> toLocalMap() => {
"id": id,
"organization_id": organizationId,
"category_id": categoryId,
"sku": sku,
"name": name,
"description": description,
"price": price,
"cost": cost,
"business_type": businessType,
"image_url": imageUrl,
"printer_type": printerType,
"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,
"organization_id": organizationId,
"category_id": categoryId,
"sku": sku,
"name": name,
"description": description,
"price": price,
"cost": cost,
"business_type": businessType,
"image_url": imageUrl,
"printer_type": printerType,
"metadata": metadata,
"is_active": isActive,
"created_at": createdAt?.toIso8601String(),
"updated_at": updatedAt?.toIso8601String(),
"variants": variants == null
? []
: List<dynamic>.from(variants!.map((x) => x.toMap())),
};
}
class Category {
final int? id;
final String? name;
final String? description;
final String? image;
final DateTime? createdAt;
final DateTime? updatedAt;
Category({
this.id,
this.name,
this.description,
this.image,
this.createdAt,
this.updatedAt,
});
factory Category.fromJson(String str) => Category.fromMap(json.decode(str));
String toJson() => json.encode(toMap());
factory Category.fromMap(Map<String, dynamic> json) => Category(
id: json["id"] is String ? int.tryParse(json["id"]) : json["id"],
name: json["name"],
description: json["description"],
image: json["image"],
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,
"name": name,
"description": description,
"image": image,
"created_at": createdAt?.toIso8601String(),
"updated_at": updatedAt?.toIso8601String(),
};
}
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: parseInt(json["price_modifier"]),
cost: parseInt(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(),
};
}