add discount api
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import 'package:fcs/domain/entities/discount.dart';
|
||||
import 'package:fcs/domain/entities/payment_method.dart';
|
||||
import 'package:fcs/domain/vo/message.dart';
|
||||
import 'package:fcs/helpers/api_helper.dart';
|
||||
@@ -32,4 +33,19 @@ class CommonDataProvider {
|
||||
payload: {"owner_id": ownerID, "seen_by_owner": seenByOwner},
|
||||
token: await getToken());
|
||||
}
|
||||
|
||||
Future<void> createDiscount(Discount discount) async {
|
||||
return await requestAPI("/discounts", "POST",
|
||||
payload: discount.toMap(), token: await getToken());
|
||||
}
|
||||
|
||||
Future<void> updateDiscount(Discount discount) async {
|
||||
return await requestAPI("/discounts", "PUT",
|
||||
payload: discount.toMap(), token: await getToken());
|
||||
}
|
||||
|
||||
Future<void> deleteDiscount(String id) async {
|
||||
return await requestAPI("/discounts", "DELETE",
|
||||
payload: {"id": id}, token: await getToken());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import 'package:fcs/data/provider/common_data_provider.dart';
|
||||
import 'package:fcs/data/provider/user_data_provider.dart';
|
||||
import 'package:fcs/domain/entities/discount.dart';
|
||||
import 'package:fcs/domain/entities/payment_method.dart';
|
||||
import 'package:fcs/domain/vo/message.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
@@ -37,4 +37,19 @@ class CommonServiceImp implements CommonService {
|
||||
Future<void> seenMessage(String ownerID, bool seenByOwner) {
|
||||
return commonDataProvider.seenMessage(ownerID, seenByOwner);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> createDiscount(Discount discount) {
|
||||
return commonDataProvider.createDiscount(discount);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> deleteDiscount(String id) {
|
||||
return commonDataProvider.deleteDiscount(id);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> updateDiscount(Discount discount) {
|
||||
return commonDataProvider.updateDiscount(discount);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import 'package:fcs/domain/entities/discount.dart';
|
||||
import 'package:fcs/domain/entities/payment_method.dart';
|
||||
import 'package:fcs/domain/vo/message.dart';
|
||||
|
||||
@@ -10,4 +11,9 @@ abstract class CommonService {
|
||||
// Messaging
|
||||
Future<void> sendMessage(Message message);
|
||||
Future<void> seenMessage(String ownerID, bool seenByOwner);
|
||||
|
||||
// Payment
|
||||
Future<void> createDiscount(Discount discount);
|
||||
Future<void> updateDiscount(Discount discount);
|
||||
Future<void> deleteDiscount(String id);
|
||||
}
|
||||
|
||||
@@ -1,16 +1,38 @@
|
||||
class Discount {
|
||||
String id;
|
||||
String code;
|
||||
String customer;
|
||||
String customerId;
|
||||
String customerName;
|
||||
String status;
|
||||
double amount;
|
||||
int weight;
|
||||
double discountRate;
|
||||
|
||||
Discount(
|
||||
{this.code,
|
||||
this.customer,
|
||||
Discount({
|
||||
this.id,
|
||||
this.code,
|
||||
this.customerId,
|
||||
this.customerName,
|
||||
this.amount,
|
||||
this.status,
|
||||
this.weight,
|
||||
this.discountRate});
|
||||
});
|
||||
|
||||
Map<String, dynamic> toMap() {
|
||||
return {
|
||||
'id': id,
|
||||
'code': code,
|
||||
"customer_id": customerId,
|
||||
"customer_name": customerName,
|
||||
"amount": amount,
|
||||
};
|
||||
}
|
||||
|
||||
factory Discount.fromMap(Map<String, dynamic> map, String id) {
|
||||
return Discount(
|
||||
id: id,
|
||||
code: map['code'],
|
||||
customerId: map['customer_id'],
|
||||
customerName: map['customer_name'],
|
||||
amount: map['amount'],
|
||||
status: map['status'],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ import 'package:fcs/helpers/theme.dart';
|
||||
import 'package:fcs/localization/app_translations.dart';
|
||||
import 'package:fcs/pages/discount/model/discount_model.dart';
|
||||
import 'package:fcs/pages/main/util.dart';
|
||||
import 'package:fcs/pages/widgets/bottom_up_page_route.dart';
|
||||
import 'package:fcs/pages/widgets/progress.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
@@ -87,7 +86,7 @@ class _DiscountListState extends State<DiscountList> {
|
||||
Row(
|
||||
children: <Widget>[
|
||||
Text(
|
||||
discount.customer,
|
||||
discount.customerName,
|
||||
style: TextStyle(
|
||||
color: Colors.black,
|
||||
fontWeight: FontWeight.normal,
|
||||
|
||||
@@ -1,56 +1,56 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||
import 'package:fcs/data/services/services.dart';
|
||||
import 'package:fcs/domain/entities/discount.dart';
|
||||
import 'package:fcs/pages/main/model/base_model.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
|
||||
class DiscountModel extends BaseModel {
|
||||
List<Discount> get discounts {
|
||||
List<Discount> discountList = [
|
||||
Discount(
|
||||
code: 'XMQY01',
|
||||
customer: 'Ko Nyi',
|
||||
amount: 5000,
|
||||
status: 'Used',
|
||||
),
|
||||
Discount(
|
||||
code: 'XMQY02',
|
||||
customer: 'Ko Aung Myo',
|
||||
amount: 3000,
|
||||
status: 'Avaliable',
|
||||
),
|
||||
Discount(
|
||||
code: 'XMQY03',
|
||||
customer: 'Ko Zaw Thu',
|
||||
amount: 2000,
|
||||
status: 'Used',
|
||||
),
|
||||
Discount(
|
||||
code: 'XMQY04',
|
||||
customer: 'Ko Myo Min',
|
||||
amount: 3000,
|
||||
status: 'Avaliable',
|
||||
),
|
||||
Discount(
|
||||
code: 'XMQY05',
|
||||
customer: 'Ko Nyi',
|
||||
amount: 3000,
|
||||
status: 'Avaliable',
|
||||
),
|
||||
];
|
||||
return discountList;
|
||||
}
|
||||
final log = Logger('DiscountModel');
|
||||
|
||||
// List<Discount> get discountByCustomer {
|
||||
// return invoices.where((e) => e.status == "Avaliable").toList()
|
||||
// ..sort((e1, e2) {
|
||||
// return e2.invoiceNumber.compareTo(e1.invoiceNumber);
|
||||
// });
|
||||
// }
|
||||
StreamSubscription<QuerySnapshot> listener;
|
||||
|
||||
List<Discount> discounts = [];
|
||||
|
||||
void initUser(user) {
|
||||
super.initUser(user);
|
||||
_load();
|
||||
}
|
||||
|
||||
_load() {
|
||||
if (listener != null) listener.cancel();
|
||||
try {
|
||||
listener = Firestore.instance
|
||||
.collection("/discounts")
|
||||
.orderBy("code", descending: false)
|
||||
.snapshots()
|
||||
.listen((snaps) {
|
||||
discounts.clear();
|
||||
snaps.documents.forEach((d) {
|
||||
discounts.add(Discount.fromMap(d.data, d.documentID));
|
||||
});
|
||||
notifyListeners();
|
||||
});
|
||||
} catch (e) {
|
||||
log.warning("error:$e");
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
logout() async {
|
||||
// discounts = [];
|
||||
discounts = [];
|
||||
}
|
||||
|
||||
Future<void> addDiscount(Discount discount) async {
|
||||
return Services.instance.commonService.createDiscount(discount);
|
||||
}
|
||||
|
||||
Future<void> updateDiscount(Discount discount) async {
|
||||
return Services.instance.commonService.updateDiscount(discount);
|
||||
}
|
||||
|
||||
Future<void> deleteDiscount(Discount discount) async {
|
||||
return Services.instance.commonService.deleteDiscount(discount.id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import 'package:fcs/domain/entities/discount.dart';
|
||||
import 'package:fcs/domain/entities/discount_by_weight.dart';
|
||||
import 'package:fcs/helpers/theme.dart';
|
||||
import 'package:fcs/localization/app_translations.dart';
|
||||
import 'package:fcs/pages/main/util.dart';
|
||||
@@ -9,8 +9,8 @@ import 'package:flutter/material.dart';
|
||||
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
|
||||
|
||||
class DiscountByWeightEditor extends StatefulWidget {
|
||||
final Discount discount;
|
||||
DiscountByWeightEditor({this.discount});
|
||||
final DiscountByWeight discountByWeight;
|
||||
DiscountByWeightEditor({this.discountByWeight});
|
||||
|
||||
@override
|
||||
_DiscountByWeightEditorState createState() => _DiscountByWeightEditorState();
|
||||
@@ -21,15 +21,15 @@ class _DiscountByWeightEditorState extends State<DiscountByWeightEditor> {
|
||||
TextEditingController _rateController = new TextEditingController();
|
||||
|
||||
bool _isLoading = false;
|
||||
Discount _discount = new Discount();
|
||||
DiscountByWeight _discountByWeight = new DiscountByWeight();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
if (widget.discount != null) {
|
||||
_discount = widget.discount;
|
||||
_weightController.text = _discount.weight.toString();
|
||||
_rateController.text = _discount.discountRate.toString();
|
||||
if (widget.discountByWeight != null) {
|
||||
_discountByWeight = widget.discountByWeight;
|
||||
_weightController.text = _discountByWeight.weight.toString();
|
||||
_rateController.text = _discountByWeight.discount.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,7 +74,7 @@ class _DiscountByWeightEditorState extends State<DiscountByWeightEditor> {
|
||||
],
|
||||
),
|
||||
),
|
||||
widget.discount == null
|
||||
widget.discountByWeight == null
|
||||
? fcsButton(context, "Create", callack: () {})
|
||||
: fcsButton(context, "Save", callack: () {}),
|
||||
SizedBox(height: 10)
|
||||
|
||||
Reference in New Issue
Block a user