1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { Injectable } from '@angular/core';
import { AngularFirestore, AngularFirestoreDocument, AngularFirestoreCollection } from '@angular/fire/firestore';
import { Router, ActivatedRoute } from '@angular/router';
import { Order } from './services/order';
import { Products } from './services/product';
import { ServiceService } from './service.service';
import * as firebase from 'firebase';
import { CartItem } from './services/cart';
import { BehaviorSubject } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class OrdersService {
order: Order;
size: string;
color: string;
custId: string;
shopperId: any;
checkout = new BehaviorSubject(false);
constructor(
public afs: AngularFirestore,
public router: Router,
public service: ServiceService
) {
this.size = 'small';
this.color = 'Blue';
this.custId = '';
const users = this.service.get('user').then((data) => {
if (data) {
data = JSON.parse(data);
this.custId = data.uid;
console.log(this.custId);
} else {
this.custId = 'WwHnLICVY2dvZGUHuKqasiTB91a2';
}
});
}
public async checkOut(cart: CartItem[]) {
console.log(cart);
let currProcess = 0;
const cartCount = cart.length;
cart.forEach((product) => {
this.afs.collection('orders').add({
bookDate: firebase.firestore.FieldValue.serverTimestamp()
}).then((docRef) => {
const neworderId = docRef.id;
const delivery = new firebase.firestore.GeoPoint(10.0237, 76.3116);
const pickup = new firebase.firestore.GeoPoint(10.7231, 76.1234);
const orderItem: Order = {
amount: 'A$ 175',
customer: firebase.firestore().doc('/customer/' + this.custId),
shopper: product.shopperId,
deliveryAddress: 'Techware Software solution, Carnival Infopark, Kochi',
deliveryCharge: 'A$ 0.5',
deliveryLocation: delivery,
bookDate: firebase.firestore.FieldValue.serverTimestamp(),
discount: 'A$ 12.00',
orderCode: this.orderCode(),
orderId: neworderId,
orderStatus: 1,
pickupAddress: 'GetMi, Canberra, AUS',
pickupLocation: pickup,
price: 'A$ ' + product.price,
product: firebase.firestore().doc('/product/' + product.prodId),
promoId: null,
qty: product.qty,
rider: firebase.firestore().doc('/riders/qbTKza18mWVzYG9NLIbmjMbrYjG2'),
status: 1,
tax: 'A$ 7.5',
size: product.size,
color: product.color,
custId: this.custId,
image: product.image,
prodId: product.prodId,
prodName: product.prodName,
riderId: 'qbTKza18mWVzYG9NLIbmjMbrYjG2',
shopperId: 'qbTKza18mWVzYG9NLIbmjMbrYjG2'
};
this.afs.collection('orders').doc(neworderId).set(orderItem).then(() => {
currProcess += 1;
console.log('Booking Successfully');
this.afs.doc(`carts/${product.cartId}`).delete();
// this.router.navigateByUrl('cart');
console.log(currProcess, cartCount);
document.body.scrollTop = document.documentElement.scrollTop = 0;
if (currProcess === cartCount) {
// this.router.navigateByUrl('cart');
return true;
}
});
}).catch((error) => {
console.error('Error adding document: ', error);
});
});
}
public async buyNow(product: Products) {
//
}
orderCode() {
const newpin = Math.round(Math.random() * 1000000);
const orderCode = 'GM' + this.pad(newpin, 6, null);
return orderCode;
}
pad(n, width, z) {
z = z || '0';
n = n + '';
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}
choose_size(size: string) {
this.size = size;
}
}