feat: sync order
This commit is contained in:
@@ -6,7 +6,7 @@ import 'package:dio/dio.dart';
|
||||
import 'package:enaklo_pos/core/constants/variables.dart';
|
||||
import 'package:enaklo_pos/core/network/dio_client.dart';
|
||||
import 'package:enaklo_pos/data/datasources/auth_local_datasource.dart';
|
||||
import 'package:enaklo_pos/data/models/response/order_remote_datasource.dart';
|
||||
import 'package:enaklo_pos/data/models/response/order_response_model.dart';
|
||||
import 'package:enaklo_pos/data/models/response/payment_method_response_model.dart';
|
||||
import 'package:enaklo_pos/data/models/response/summary_response_model.dart';
|
||||
import 'package:enaklo_pos/presentation/home/models/order_model.dart';
|
||||
@@ -223,4 +223,48 @@ class OrderRemoteDatasource {
|
||||
return const Left('Terjadi kesalahan tak terduga');
|
||||
}
|
||||
}
|
||||
|
||||
Future<Either<String, OrderResponseModel>> getOrder(
|
||||
{int page = 1,
|
||||
int limit = Variables.defaultLimit,
|
||||
String status = 'completed'}) async {
|
||||
try {
|
||||
final authData = await AuthLocalDataSource().getAuthData();
|
||||
final response = await dio.get(
|
||||
'${Variables.baseUrl}/api/v1/orders',
|
||||
queryParameters: {
|
||||
'page': page,
|
||||
'limit': limit,
|
||||
'status': status,
|
||||
'outlet_id': authData.user?.outletId,
|
||||
},
|
||||
options: Options(
|
||||
headers: {
|
||||
'Authorization': 'Bearer ${authData.token}',
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
),
|
||||
);
|
||||
|
||||
log("📥 HTTP Status Code: ${response.statusCode}");
|
||||
log("📥 Response Body: ${response.data}");
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
log("✅ getOrderByRangeDate API call successful");
|
||||
return Right(OrderResponseModel.fromMap(response.data));
|
||||
} else {
|
||||
log("❌ getOrderByRangeDate API call failed - Status: ${response.statusCode}");
|
||||
return const Left("Failed Load Data");
|
||||
}
|
||||
} on DioException catch (e) {
|
||||
final errorMessage = 'Something went wrong';
|
||||
log("💥 Dio error: ${e.message}");
|
||||
log("💥 Dio response: ${e.response?.data}");
|
||||
return Left(errorMessage);
|
||||
} catch (e) {
|
||||
log("💥 Unexpected error: $e");
|
||||
return Left("Unexpected Error: $e");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,113 +0,0 @@
|
||||
import 'dart:convert';
|
||||
|
||||
class OrderResponseModel {
|
||||
String? status;
|
||||
List<ItemOrder>? data;
|
||||
|
||||
OrderResponseModel({
|
||||
this.status,
|
||||
this.data,
|
||||
});
|
||||
|
||||
factory OrderResponseModel.fromJson(String str) =>
|
||||
OrderResponseModel.fromMap(json.decode(str));
|
||||
|
||||
String toJson() => json.encode(toMap());
|
||||
|
||||
factory OrderResponseModel.fromMap(Map<String, dynamic> json) =>
|
||||
OrderResponseModel(
|
||||
status: json["status"],
|
||||
data: json["data"] == null
|
||||
? []
|
||||
: List<ItemOrder>.from(
|
||||
json["data"]!.map((x) => ItemOrder.fromMap(x))),
|
||||
);
|
||||
|
||||
Map<String, dynamic> toMap() => {
|
||||
"status": status,
|
||||
"data":
|
||||
data == null ? [] : List<dynamic>.from(data!.map((x) => x.toMap())),
|
||||
};
|
||||
}
|
||||
|
||||
class ItemOrder {
|
||||
int? id;
|
||||
int? paymentAmount;
|
||||
int? subTotal;
|
||||
int? tax;
|
||||
int? discount;
|
||||
String? discountAmount;
|
||||
int? serviceCharge;
|
||||
int? total;
|
||||
String? paymentMethod;
|
||||
int? totalItem;
|
||||
int? idKasir;
|
||||
String? namaKasir;
|
||||
DateTime? transactionTime;
|
||||
DateTime? createdAt;
|
||||
DateTime? updatedAt;
|
||||
|
||||
ItemOrder({
|
||||
this.id,
|
||||
this.paymentAmount,
|
||||
this.subTotal,
|
||||
this.tax,
|
||||
this.discount,
|
||||
this.discountAmount,
|
||||
this.serviceCharge,
|
||||
this.total,
|
||||
this.paymentMethod,
|
||||
this.totalItem,
|
||||
this.idKasir,
|
||||
this.namaKasir,
|
||||
this.transactionTime,
|
||||
this.createdAt,
|
||||
this.updatedAt,
|
||||
});
|
||||
|
||||
factory ItemOrder.fromJson(String str) => ItemOrder.fromMap(json.decode(str));
|
||||
|
||||
String toJson() => json.encode(toMap());
|
||||
|
||||
factory ItemOrder.fromMap(Map<String, dynamic> json) => ItemOrder(
|
||||
id: json["id"],
|
||||
paymentAmount: json["payment_amount"],
|
||||
subTotal: json["sub_total"],
|
||||
tax: json["tax"],
|
||||
discount: json["discount"],
|
||||
discountAmount: json["discount_amount"],
|
||||
serviceCharge: json["service_charge"],
|
||||
total: json["total"],
|
||||
paymentMethod: json["payment_method"]!,
|
||||
totalItem: json["total_item"],
|
||||
idKasir: json["id_kasir"],
|
||||
namaKasir: json["nama_kasir"],
|
||||
transactionTime: json["transaction_time"] == null
|
||||
? null
|
||||
: DateTime.parse(json["transaction_time"]),
|
||||
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,
|
||||
"payment_amount": paymentAmount,
|
||||
"sub_total": subTotal,
|
||||
"tax": tax,
|
||||
"discount": discount,
|
||||
"discount_amount": discountAmount,
|
||||
"service_charge": serviceCharge,
|
||||
"total": total,
|
||||
"payment_method": paymentMethod,
|
||||
"total_item": totalItem,
|
||||
"id_kasir": idKasir,
|
||||
"nama_kasir": namaKasir,
|
||||
"transaction_time": transactionTime?.toIso8601String(),
|
||||
"created_at": createdAt?.toIso8601String(),
|
||||
"updated_at": updatedAt?.toIso8601String(),
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,220 @@
|
||||
import 'dart:convert';
|
||||
|
||||
class OrderResponseModel {
|
||||
final bool? success;
|
||||
final OrderData? data;
|
||||
final dynamic errors;
|
||||
|
||||
OrderResponseModel({
|
||||
this.success,
|
||||
this.data,
|
||||
this.errors,
|
||||
});
|
||||
|
||||
factory OrderResponseModel.fromJson(String str) =>
|
||||
OrderResponseModel.fromMap(json.decode(str));
|
||||
|
||||
String toJson() => json.encode(toMap());
|
||||
|
||||
factory OrderResponseModel.fromMap(Map<String, dynamic> json) =>
|
||||
OrderResponseModel(
|
||||
success: json["success"],
|
||||
data: json["data"] == null ? null : OrderData.fromMap(json["data"]),
|
||||
errors: json["errors"],
|
||||
);
|
||||
|
||||
Map<String, dynamic> toMap() => {
|
||||
"success": success,
|
||||
"data": data?.toMap(),
|
||||
"errors": errors,
|
||||
};
|
||||
}
|
||||
|
||||
class OrderData {
|
||||
final List<Order>? orders;
|
||||
final int? totalCount;
|
||||
final int? page;
|
||||
final int? limit;
|
||||
final int? totalPages;
|
||||
|
||||
OrderData({
|
||||
this.orders,
|
||||
this.totalCount,
|
||||
this.page,
|
||||
this.limit,
|
||||
this.totalPages,
|
||||
});
|
||||
|
||||
factory OrderData.fromMap(Map<String, dynamic> json) => OrderData(
|
||||
orders: json["orders"] == null
|
||||
? []
|
||||
: List<Order>.from(json["orders"].map((x) => Order.fromMap(x))),
|
||||
totalCount: json["total_count"],
|
||||
page: json["page"],
|
||||
limit: json["limit"],
|
||||
totalPages: json["total_pages"],
|
||||
);
|
||||
|
||||
Map<String, dynamic> toMap() => {
|
||||
"orders": orders == null
|
||||
? []
|
||||
: List<dynamic>.from(orders!.map((x) => x.toMap())),
|
||||
"total_count": totalCount,
|
||||
"page": page,
|
||||
"limit": limit,
|
||||
"total_pages": totalPages,
|
||||
};
|
||||
}
|
||||
|
||||
class Order {
|
||||
final String? id;
|
||||
final String? orderNumber;
|
||||
final String? outletId;
|
||||
final String? userId;
|
||||
final String? tableNumber;
|
||||
final String? orderType;
|
||||
final String? status;
|
||||
final int? subtotal;
|
||||
final int? taxAmount;
|
||||
final int? discountAmount;
|
||||
final int? totalAmount;
|
||||
final String? notes;
|
||||
final Map<String, dynamic>? metadata;
|
||||
final DateTime? createdAt;
|
||||
final DateTime? updatedAt;
|
||||
final List<OrderItem>? orderItems;
|
||||
|
||||
Order({
|
||||
this.id,
|
||||
this.orderNumber,
|
||||
this.outletId,
|
||||
this.userId,
|
||||
this.tableNumber,
|
||||
this.orderType,
|
||||
this.status,
|
||||
this.subtotal,
|
||||
this.taxAmount,
|
||||
this.discountAmount,
|
||||
this.totalAmount,
|
||||
this.notes,
|
||||
this.metadata,
|
||||
this.createdAt,
|
||||
this.updatedAt,
|
||||
this.orderItems,
|
||||
});
|
||||
|
||||
factory Order.fromMap(Map<String, dynamic> json) => Order(
|
||||
id: json["id"],
|
||||
orderNumber: json["order_number"],
|
||||
outletId: json["outlet_id"],
|
||||
userId: json["user_id"],
|
||||
tableNumber: json["table_number"],
|
||||
orderType: json["order_type"],
|
||||
status: json["status"],
|
||||
subtotal: json["subtotal"],
|
||||
taxAmount: json["tax_amount"],
|
||||
discountAmount: json["discount_amount"],
|
||||
totalAmount: json["total_amount"],
|
||||
notes: json["notes"],
|
||||
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"]),
|
||||
orderItems: json["order_items"] == null
|
||||
? []
|
||||
: List<OrderItem>.from(
|
||||
json["order_items"].map((x) => OrderItem.fromMap(x))),
|
||||
);
|
||||
|
||||
Map<String, dynamic> toMap() => {
|
||||
"id": id,
|
||||
"order_number": orderNumber,
|
||||
"outlet_id": outletId,
|
||||
"user_id": userId,
|
||||
"table_number": tableNumber,
|
||||
"order_type": orderType,
|
||||
"status": status,
|
||||
"subtotal": subtotal,
|
||||
"tax_amount": taxAmount,
|
||||
"discount_amount": discountAmount,
|
||||
"total_amount": totalAmount,
|
||||
"notes": notes,
|
||||
"metadata": metadata,
|
||||
"created_at": createdAt?.toIso8601String(),
|
||||
"updated_at": updatedAt?.toIso8601String(),
|
||||
"order_items": orderItems == null
|
||||
? []
|
||||
: List<dynamic>.from(orderItems!.map((x) => x.toMap())),
|
||||
};
|
||||
}
|
||||
|
||||
class OrderItem {
|
||||
final String? id;
|
||||
final String? orderId;
|
||||
final String? productId;
|
||||
final String? productName;
|
||||
final String? productVariantId;
|
||||
final int? quantity;
|
||||
final int? unitPrice;
|
||||
final int? totalPrice;
|
||||
final List<dynamic>? modifiers;
|
||||
final String? notes;
|
||||
final String? status;
|
||||
final DateTime? createdAt;
|
||||
final DateTime? updatedAt;
|
||||
|
||||
OrderItem({
|
||||
this.id,
|
||||
this.orderId,
|
||||
this.productId,
|
||||
this.productName,
|
||||
this.productVariantId,
|
||||
this.quantity,
|
||||
this.unitPrice,
|
||||
this.totalPrice,
|
||||
this.modifiers,
|
||||
this.notes,
|
||||
this.status,
|
||||
this.createdAt,
|
||||
this.updatedAt,
|
||||
});
|
||||
|
||||
factory OrderItem.fromMap(Map<String, dynamic> json) => OrderItem(
|
||||
id: json["id"],
|
||||
orderId: json["order_id"],
|
||||
productId: json["product_id"],
|
||||
productName: json["product_name"],
|
||||
productVariantId: json["product_variant_id"],
|
||||
quantity: json["quantity"],
|
||||
unitPrice: json["unit_price"],
|
||||
totalPrice: json["total_price"],
|
||||
modifiers: json["modifiers"] ?? [],
|
||||
notes: json["notes"],
|
||||
status: json["status"],
|
||||
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,
|
||||
"order_id": orderId,
|
||||
"product_id": productId,
|
||||
"product_name": productName,
|
||||
"product_variant_id": productVariantId,
|
||||
"quantity": quantity,
|
||||
"unit_price": unitPrice,
|
||||
"total_price": totalPrice,
|
||||
"modifiers": modifiers,
|
||||
"notes": notes,
|
||||
"status": status,
|
||||
"created_at": createdAt?.toIso8601String(),
|
||||
"updated_at": updatedAt?.toIso8601String(),
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user