· 4 years ago · Sep 12, 2021, 09:12 PM
1
2
3import 'dart:convert';
4// import 'dart:html';
5import 'package:date_time_picker/date_time_picker.dart';
6import 'package:flutter/cupertino.dart';
7import 'package:flutter/material.dart';
8import 'package:http/http.dart' as http;
9import 'package:medbo/Animation/showupAnimation.dart';
10import 'package:medbo/Booking/PathologicalMultipleBooking/testModel.dart';
11import 'package:shared_preferences/shared_preferences.dart';
12import 'AllPathLabTestModel.dart';
13import 'GetTestFeeModel.dart';
14
15
16class GetTestFeeMap {
17String? encPartnerId;
18String? encTestId;
19int? fee;
20int? discountedFee;
21int? bookingFee;
22
23GetTestFeeMap(
24 {this.encPartnerId,
25 this.encTestId,
26 this.fee,
27 this.discountedFee,
28 this.bookingFee});
29
30GetTestFeeMap.fromJson(Map<String, dynamic> json) {
31 encPartnerId = json['EncPartnerId'];
32 encTestId = json['EncTestId'];
33 //fee = json['Fee'];
34 fee = int.parse(json['Fee']);
35 // discountedFee = json['DiscountedFee'];
36 discountedFee = int.parse(json['DiscountedFee']);
37 //bookingFee = json['BookingFee'];
38 bookingFee = int.parse(json['BookingFee']);
39}
40
41Map<String, dynamic> toJson() {
42 final Map<String, dynamic> data = new Map<String, dynamic>();
43 data['EncPartnerId'] = this.encPartnerId;
44 data['EncTestId'] = this.encTestId;
45 data['Fee'] = this.fee;
46 data['DiscountedFee'] = this.discountedFee;
47 data['BookingFee'] = this.bookingFee;
48 return data;
49 }
50}
51
52
53
54class MultipleTestBooking extends StatefulWidget {
55 //const MultipleTestBooking({required Key key}) : super(key: key);
56
57 @override
58 _MultipleTestBookingState createState() => _MultipleTestBookingState();
59}
60
61class _MultipleTestBookingState extends State<MultipleTestBooking> {
62
63
64
65
66//=============================S H O W USER DETIALS IN APP DRAWER WITH SHARED PREFERENCES==================================
67
68String Name="";
69String EncUserId="";
70
71// void initState(){
72// super.initState();
73// getCred();
74// }
75
76void getCred() async{
77 //HERE WE FETCH OUR CREDENTIALS FROM SHARED PREF
78 SharedPreferences pref = await SharedPreferences.getInstance();
79 setState(() {
80 Name = pref.getString("userEmail");
81 EncUserId= pref.getString("encId");
82 });
83
84}
85//=====================S H O W USER DETIALS IN APP DRAWER WITH SHARED PREFERENCES=================================================
86
87
88
89
90
91
92
93
94
95
96
97
98
99 int delayAmount = 500;
100
101
102 List<GetTestFeeMap> reponseArray =[]; // Storing API response || later showing test fee in table format
103 int feeSum =0;
104 int discountSum =0;
105 int bookingSum =0 ;
106
107 String eNcTestIdInList='';
108 int? testFeeinLIST;
109 int? testDiscountFeeInLIST;
110 int? testBookingFeeiNlIST;
111
112 // String actualFee ='';
113 // String actualDiscountFee='';
114 // String actualBookingFee='';
115
116 GetTestFeeMap? getTestFeeObj;
117
118 Partner? _selectedLab;
119 Datum? _selectedTest;
120 Future? getAllPathLabResults;
121 Future? getTestByLabResult;
122
123 String encLabId = '';
124 String encTestId = '';
125 String testName ='';
126
127 void initState() {
128 getCred();
129 super.initState();
130 getAllPathLabResults = allPathLab();
131 getTestByLabResult = getTestByLab();
132 }
133
134 String _selectedDate = DateTime.now().toString();
135
136
137
138//==============================================================================================================================
139 @override
140 Widget build(BuildContext context) {
141 var screenWidth = MediaQuery.of(context).size.width;
142 var screenHeight = MediaQuery.of(context).size.height;
143 var blockSizeHorizontal = (screenWidth / 100);
144 var blockSizeVertical = (screenHeight / 100);
145
146 return Scaffold(
147 body: SafeArea(
148 child: SingleChildScrollView(
149 child: Container(
150 child: Column(
151 children: [
152 Padding(
153 padding: const EdgeInsets.all(8.0),
154 child: ShowUp(
155 delay: delayAmount,
156 child: ListTile(
157 title: Text("Booking Information",
158 style: TextStyle(
159 fontWeight: FontWeight.bold,
160 fontSize: blockSizeHorizontal * 5,
161 fontFamily: 'Poppins',
162 color: Theme.of(context).primaryColor,
163 )),
164 subtitle: Text("Preferred Visit Date"),
165 ),
166 ),
167 ),
168 ShowUp(
169 delay: delayAmount+450,
170 child: Container(
171 margin: EdgeInsets.only(left: 20),
172 padding: EdgeInsets.only(left: 0, right: 150),
173 decoration: BoxDecoration(
174 color: Colors.lightBlue[50],
175 borderRadius: BorderRadius.all(Radius.circular(12)),
176 ),
177 child: Padding(
178 padding: const EdgeInsets.all(8.0),
179 child: DateTimePicker(
180 initialValue: DateTime.now().toString(),
181 //initialValue:'', // initialValue or controller.text can be null, empty or a DateTime string otherwise it will throw an error.
182 type: DateTimePickerType.date,
183 dateLabelText: 'Select Date',
184 style: TextStyle(
185 fontWeight: FontWeight.bold,
186 fontSize: blockSizeHorizontal * 3.5,
187 fontFamily: 'Poppins',
188 color: Colors.green,
189 letterSpacing: 2.0,
190 ),
191 firstDate: DateTime.now(),
192 lastDate: DateTime.now().add(Duration(days: 30)),
193 // This will add one year from current date
194 validator: (value) {
195 return null;
196 },
197 onChanged: (value) {
198 if (value.isNotEmpty) {
199 setState(() {
200 _selectedDate = value;
201 });
202 }
203 },
204 onSaved: (value) {
205 if (value.isNotEmpty) {
206 _selectedDate = value;
207 }
208 },
209 ),
210 ),
211 ),
212 ),
213
214 //=========================================================== INITIAL drop down==========================
215
216 ShowUp(
217 delay: delayAmount+500,
218 child: ListTile(
219 title: Text(
220 "Select Pathological Lab",
221 style: TextStyle(
222 fontWeight: FontWeight.bold,
223 fontSize: blockSizeHorizontal * 4.0,
224 fontFamily: 'Poppins',
225 color: Theme.of(context).primaryColor,
226 ),
227 ),
228 ),
229 ),
230
231 ShowUp(
232 delay: 0,
233 child: Container(
234 child: FutureBuilder<List<Partner>>(
235 future: getAllPathLabResults as Future<List<Partner>>,
236 builder: (BuildContext context, AsyncSnapshot snapshot) {
237 if (snapshot.connectionState != ConnectionState.done) {
238 return CircularProgressIndicator();
239 }
240 if (snapshot.hasError) {
241 return Text("Somthing went wrong");
242 }
243
244 if (snapshot.hasData) {
245 List<Partner> data =
246 snapshot.hasData ? snapshot.data : [];
247 return Padding(
248 padding: const EdgeInsets.only(left:20.0, right: 150),
249 child: DropdownButton<Partner>(
250 value: _selectedLab,//USER SELECTED DROPDOWN ITEM VALUE
251 hint: Text("Select Lab"),
252 //underline: SizedBox(),
253 isExpanded: true,
254 items: data.map((Partner data) => DropdownMenuItem<Partner>(
255 child: Text("${data.partnerName}"),
256 value: data,
257 )).toList().cast<DropdownMenuItem<Partner>>(),
258 onChanged: (val) {
259 setState(() {
260 _selectedLab = val!;
261
262 encLabId = val.encPartnerId; //===== Passing encLabId to my next API function
263 getTestByLabResult = getTestByLab();
264 });
265 //GetTestByLab(value!.encPartnerId); // passing encid to my next API function
266 // GetTestByLab();
267 },
268 ),
269 );
270 }
271 return Text("Waiting for Internet Connection");
272 },
273 ),
274 ),
275 ),
276
277 //=========================================================== Dependent drop down=============================
278
279 ShowUp(
280 delay: delayAmount+600,
281 child: ListTile(
282 title: Text(
283 "Test Name",
284 style: TextStyle(
285 fontWeight: FontWeight.bold,
286 fontSize: blockSizeHorizontal * 4.0,
287 fontFamily: 'Poppins',
288 color: Theme.of(context).primaryColor,
289 ),
290 ),
291 ),
292 ),
293
294 ShowUp(
295 delay: 0,
296 child: Container(
297 child: FutureBuilder<List<Datum>>(
298 future: getTestByLabResult as Future<List<Datum>>,
299 builder: (BuildContext context, AsyncSnapshot snapshot) {
300 if (snapshot.connectionState != ConnectionState.done) {
301 return CircularProgressIndicator();
302 }
303 if (snapshot.hasError) {
304 return Text("Something wrong");
305 }
306
307 if (snapshot.hasData) {
308 List<Datum> data = snapshot.hasData ? snapshot.data : [];
309
310 return Padding(
311 padding: const EdgeInsets.only(left:20.0, right: 150),
312 child: DropdownButton<Datum>(
313 value: _selectedTest,
314 hint: Text(""),
315 //underline: SizedBox(),
316 isExpanded: true,
317 items: data
318 .map((Datum data) => DropdownMenuItem<Datum>(
319 child: Text("${data.testName}"),
320 value: data,
321 ))
322 .toList()
323 .cast<DropdownMenuItem<Datum>>(),
324 onChanged: (value) {
325 print("This is the TestName : ${value!.testName}");
326 print("This is the EncTestId which is need to get Test Fee : ${value.testId}");
327 setState(() {
328 encTestId = value.testId; // == SELCTED TEST from drop down 'encTestId' needed for to get Test Fee
329 testName = value.testName;
330 _selectedTest = value;
331 });
332 }),
333 );
334 }
335 return Text("Waiting for Internet Connection");
336 },
337 ),
338 ),
339 ),
340 SizedBox(height: 50,),
341
342 ShowUp(
343 delay: delayAmount+900,
344 child: OutlinedButton(
345 onPressed: (){
346 setState(() {
347 });
348 GetTestFee();
349 },
350 child:Text("Add")),
351 ),
352
353
354
355
356 ShowUp(
357 delay: delayAmount+1000,
358 child: DataTable(
359 columnSpacing: 13.0,
360 columns: <DataColumn>[
361 //DataColumn(label: Text("encPartnerId")),
362 //DataColumn(label: Text("encTestId")),
363 DataColumn(label: Text("TestName")),
364 DataColumn(label: Text("Fee")),
365 DataColumn(label: Text("Discounted Fee")),
366 DataColumn(label: Text("Booking Fee")),
367 ],
368
369 rows:reponseArray.map((testRowData){
370 return DataRow(
371 cells: [
372 DataCell(Text(testName)),
373 DataCell(Text (testRowData.fee!.toString() )),
374 DataCell(Text(testRowData.discountedFee !.toString() )),
375 DataCell(Text(testRowData.bookingFee!.toString() ))
376 ]
377 );
378 }).toList()
379 ),
380 ),
381
382
383
384 ShowUp(
385 delay: delayAmount+ 1050,
386 child: ListTile(
387 title: Text("Total Fee: ${feeSum}\nTotal Discounted Fee: ${discountSum}\nTotal Booking Fee: ${bookingSum}" ),
388 ),
389 ),
390
391
392
393
394
395
396 ShowUp(
397 delay: delayAmount+ 1100,
398 child: OutlinedButton(onPressed: (){
399 SaveMultipleTestBooking();
400 },
401 child: Text("Save Mutiple bookking")),
402 )
403
404
405 ],
406 ),
407 ),
408 ),
409 ),
410 );
411 }
412
413
414
415
416
417
418//============================================================================All Lab list API============================
419 Future<List<Partner>> allPathLab() async {
420 var jsonResponse;
421 var response = await http.post(Uri.parse("http://medbo.digitalicon.in/api/medboapi/AllPathLab"),
422 body: ({ }));
423 if (response.statusCode == 200) {
424 print("Correct");
425 jsonResponse = json.decode(response.body.toString());
426 print(jsonResponse);
427
428 AllPathLabTestModel dataModel = allPathLabTestModelFromJson(response.body);
429 print(dataModel.partner.length);
430 for (final item in dataModel.partner) {
431 print("This are the hte LAB name :${item.partnerName}");
432 }
433
434 List<Partner> arrData = dataModel.partner; // this "partner" is actual json array of data[]
435 return arrData;
436 } else {
437 print("Wrong URL");
438 throw Exception("Faild to fetch");
439 }
440 }
441//============================================================== Dependent Test dropdown list Api ==================
442
443 Future<List<Datum>> getTestByLab() async {
444 print("This is the LabId :$encLabId");
445 print("This is the EncTestId :$encTestId");
446 _selectedTest = null as Datum;
447 var response = await http.post(Uri.parse("http://medbo.digitalicon.in/api/medboapi/GetTestByLab"),
448 body: ({
449 "EncId": encLabId
450 }));
451
452 if (response.statusCode == 200) {
453 final dataModel = dataModelFromJson(response.body);
454 print(dataModel.data.length);
455 for (final item in dataModel.data) {
456 print("This are hte test names :${item.testName}");
457 print("This are hte test EncTestId :${item.encTestId}");
458 }
459
460
461 List<Datum> arrData = dataModel.data;
462 return arrData;
463 }
464
465 return [];
466 }
467//=====================================================================Get Test Fee Api===========================
468 Future<void> GetTestFee() async {
469 var jsonResponse;
470 if (encTestId.isNotEmpty) {
471 var response = await http.post(
472 Uri.parse("http://medbo.digitalicon.in/api/medboapi/GetTestFee"),
473 body: ({
474 'EncPartnerId': encLabId,
475 'EncTestId': encTestId,
476
477 }));
478 if (response.statusCode == 200) {
479 print("Correct");
480 print(response.body);
481 jsonResponse = json.decode(response.body.toString());
482
483
484
485 getTestFeeObj=GetTestFeeMap.fromJson(jsonResponse);
486
487 setState(() {
488 reponseArray.add(getTestFeeObj!); // Adding data to my Arraylist 'reponseArray'
489 feeSum=0;
490 discountSum=0;
491 bookingSum=0;
492
493 for (var i = 0; i < reponseArray.length; i++) {
494 eNcTestIdInList+=(reponseArray[i].encTestId!);
495
496 testFeeinLIST= (reponseArray[i].fee);
497 testDiscountFeeInLIST=(reponseArray[i].discountedFee)!;
498 testBookingFeeiNlIST=(reponseArray[i].bookingFee)!;
499 }
500
501 for(GetTestFeeMap elemInList in reponseArray){
502
503 feeSum += elemInList.fee! ;
504 discountSum += elemInList.discountedFee! ; // Doing calculation here
505 bookingSum += elemInList.bookingFee! ;
506//===============================================================================================================
507 print("Storing Test Id in list: ${elemInList.encTestId}"); //Test id stroring in list after hiting ADD button
508//===============================================================================================================
509 }
510 print(feeSum);
511 print(discountSum);
512 print(bookingSum);
513 });
514
515 } else {
516 throw Exception("Faild to fetch");
517 }
518 } else {
519 throw Exception("Faild to fetch");
520 }
521 //throw Exception("Faild to fetch");
522 //return GetTestFee();
523 }
524
525//================================================================== SaveMultipleTestBooking =======================
526
527
528 Future<void> SaveMultipleTestBooking() async {
529 var jsonResponse;
530 if (encTestId.isNotEmpty) {
531 var response = await http.post(
532 Uri.parse("http://medbo.digitalicon.in/api/medboapi/SaveMultipleTestBooking"),
533 body: ({
534 'EncPartnerId':encLabId,
535 'EncDoctorId': eNcTestIdInList, // .toString(),
536 'VisitDate': _selectedDate,
537 'EncUserId' : EncUserId,
538
539 "Fee": testFeeinLIST.toString(),
540 "DiscountedFee" : testDiscountFeeInLIST.toString(),
541 "BookingFee" : testBookingFeeiNlIST.toString(),
542 "TotalFee" :feeSum.toString(),
543 "TotalDiscountedFee": discountSum.toString(),
544 "TotalBookingFee" : bookingSum.toString(),
545
546 }));
547 if (response.statusCode == 200) {
548 print("Correct");
549 print(response.body);
550 jsonResponse = json.decode(response.body.toString());
551 print(jsonResponse);
552 } else {
553 ScaffoldMessenger.of(context)
554 .showSnackBar(SnackBar(content: Text("Please select from available dates")));
555 throw Exception("Faild to fetch");
556 }
557 } else {
558 throw Exception("Faild to fetch");
559 }
560 }
561
562}