2019-05-10 11:43:45 +00:00
|
|
|
import 'dart:async';
|
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:http/http.dart' as http;
|
2019-05-21 14:30:14 +00:00
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
2019-05-10 11:43:45 +00:00
|
|
|
import 'package:local_spend/common/functions/show_dialog_single_button.dart';
|
|
|
|
import 'package:local_spend/model/json/login_model.dart';
|
|
|
|
|
2019-07-05 14:34:39 +00:00
|
|
|
class Receipt {
|
|
|
|
var amount = "";
|
|
|
|
var time = "";
|
|
|
|
var street = "";
|
|
|
|
var category = "";
|
|
|
|
var organisationName = "";
|
|
|
|
var postcode = "";
|
|
|
|
var recurring = "";
|
|
|
|
var town = "";
|
|
|
|
|
|
|
|
var essential = "false";
|
|
|
|
}
|
|
|
|
|
2019-05-10 11:43:45 +00:00
|
|
|
Future<LoginModel> submitReceiptAPI(
|
2019-07-05 14:34:39 +00:00
|
|
|
BuildContext context, Receipt receipt) async {
|
2019-05-10 11:43:45 +00:00
|
|
|
//var apiUrl = ConfigWrapper.of(context).apiKey;
|
2019-05-21 14:30:14 +00:00
|
|
|
final url = "https://dev.peartrade.org/api/upload";
|
|
|
|
|
|
|
|
SharedPreferences preferences = await SharedPreferences.getInstance();
|
2019-05-10 11:43:45 +00:00
|
|
|
|
|
|
|
Map<String, String> body = {
|
2019-07-05 21:56:15 +00:00
|
|
|
'transaction_type' : "3",
|
2019-07-05 14:34:39 +00:00
|
|
|
'transaction_value': receipt.amount,
|
|
|
|
'purchase_time': receipt.time,
|
2019-08-12 11:09:04 +00:00
|
|
|
'category': receipt.category,
|
2019-07-05 14:34:39 +00:00
|
|
|
'essential': receipt.essential,
|
|
|
|
'organisation_name': receipt.organisationName,
|
|
|
|
'recurring': receipt.recurring,
|
|
|
|
'street_name': receipt.street,
|
|
|
|
'postcode': receipt.postcode,
|
2019-07-05 21:56:15 +00:00
|
|
|
'town': receipt.town,
|
|
|
|
|
2019-05-21 14:30:14 +00:00
|
|
|
'session_key': preferences.get('LastToken'),
|
2019-05-10 11:43:45 +00:00
|
|
|
};
|
|
|
|
|
2019-07-16 11:09:10 +00:00
|
|
|
// debugPrint('$body');
|
2019-08-12 10:37:59 +00:00
|
|
|
debugPrint(json.encode(body));
|
2019-05-10 11:43:45 +00:00
|
|
|
|
|
|
|
final response = await http.post(
|
|
|
|
url,
|
|
|
|
body: json.encode(body),
|
|
|
|
);
|
|
|
|
|
2019-08-12 15:03:00 +00:00
|
|
|
// debugPrint(response.body);
|
2019-05-10 11:43:45 +00:00
|
|
|
|
|
|
|
if (response.statusCode == 200) {
|
|
|
|
final responseJson = json.decode(response.body);
|
|
|
|
|
2019-08-12 15:03:00 +00:00
|
|
|
// print(responseJson[0]);
|
2019-07-16 10:28:36 +00:00
|
|
|
|
|
|
|
showDialogSingleButton(
|
2019-07-16 10:42:34 +00:00
|
|
|
context,
|
|
|
|
responseJson[0] == "" ? responseJson[0] : "Upload Successful",
|
|
|
|
"Transaction successfully submitted to server",
|
|
|
|
"OK"
|
2019-07-16 10:28:36 +00:00
|
|
|
);
|
2019-05-10 11:43:45 +00:00
|
|
|
return LoginModel.fromJson(responseJson);
|
|
|
|
} else {
|
|
|
|
final responseJson = json.decode(response.body);
|
|
|
|
|
2019-05-10 11:51:11 +00:00
|
|
|
|
2019-05-10 11:43:45 +00:00
|
|
|
showDialogSingleButton(
|
|
|
|
context,
|
|
|
|
"Unable to Submit Receipt",
|
2019-07-05 21:56:15 +00:00
|
|
|
// "You may have supplied an invalid 'Email' / 'Password' combination. Please try again or email an administrator.",
|
2019-07-16 10:28:36 +00:00
|
|
|
"Message from server: " + responseJson[1],
|
2019-05-10 11:43:45 +00:00
|
|
|
"OK");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|