2022-03-25 23:57:02 +00:00
|
|
|
import { Injectable } from '@angular/core';
|
2023-05-08 17:10:13 +00:00
|
|
|
import { BehaviorSubject, Observable } from 'rxjs';
|
2022-03-25 23:57:02 +00:00
|
|
|
import { HttpClient, HttpParams, HttpHeaders } from '@angular/common/http';
|
|
|
|
import { Order } from './order/order.model';
|
2022-04-08 23:23:13 +00:00
|
|
|
import { Owner } from './owner.model';
|
|
|
|
import { UserService } from './user.service';
|
2022-03-25 23:57:02 +00:00
|
|
|
|
2022-07-12 18:22:17 +00:00
|
|
|
import { ConfigData } from './configdata';
|
|
|
|
|
2022-05-18 20:51:39 +00:00
|
|
|
var Buffer = require('buffer/').Buffer;
|
|
|
|
|
2022-03-25 23:57:02 +00:00
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
|
|
|
export class ReceiptService {
|
2022-07-12 18:22:17 +00:00
|
|
|
beUrl = ConfigData.Be_URL;
|
2022-03-25 23:57:02 +00:00
|
|
|
private dataStore: {order: Order, owner: Owner } = {
|
|
|
|
owner: {
|
|
|
|
_id: '',
|
|
|
|
name: '',
|
|
|
|
address: '',
|
|
|
|
currency: 'usd',
|
|
|
|
tax: false,
|
|
|
|
taxValue: 0,
|
|
|
|
vat: false,
|
|
|
|
vatValue: 0,
|
|
|
|
paid: false,
|
2022-05-18 20:51:39 +00:00
|
|
|
zats: false,
|
|
|
|
invoices: false,
|
2022-07-14 16:11:04 +00:00
|
|
|
expiration: new Date(Date.now()).toISOString(),
|
2022-07-18 20:29:27 +00:00
|
|
|
payconf: false,
|
2022-08-31 13:56:06 +00:00
|
|
|
crmToken: ''
|
2022-03-25 23:57:02 +00:00
|
|
|
},
|
|
|
|
order: {
|
|
|
|
address: '',
|
|
|
|
session: '',
|
|
|
|
timestamp: '',
|
|
|
|
closed: false,
|
|
|
|
currency: '',
|
|
|
|
price: 0,
|
|
|
|
total: 0,
|
|
|
|
totalZec: 0,
|
2022-05-24 18:18:46 +00:00
|
|
|
paid: false,
|
2022-08-10 19:12:28 +00:00
|
|
|
externalInvoice: '',
|
|
|
|
shortCode: '',
|
2022-03-25 23:57:02 +00:00
|
|
|
lines: [
|
|
|
|
{
|
|
|
|
qty: 1,
|
|
|
|
name: '',
|
|
|
|
cost:0
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
private _orderUpdated: BehaviorSubject<Order> = new BehaviorSubject(this.dataStore.order);
|
|
|
|
public readonly orderUpdate: Observable<Order> = this._orderUpdated.asObservable();
|
2022-04-08 23:23:13 +00:00
|
|
|
public _nameUpdated: BehaviorSubject<string> = new BehaviorSubject(this.dataStore.owner.name);
|
|
|
|
public readonly nameUpdate: Observable<string>= this._nameUpdated.asObservable();
|
|
|
|
public readonly ownerUpdate;
|
2022-03-25 23:57:02 +00:00
|
|
|
private reqHeaders: HttpHeaders;
|
2023-05-08 17:10:13 +00:00
|
|
|
private session: null|string;
|
|
|
|
private params: HttpParams;
|
2022-03-25 23:57:02 +00:00
|
|
|
|
2023-05-08 17:10:13 +00:00
|
|
|
constructor(
|
2022-03-25 23:57:02 +00:00
|
|
|
private http: HttpClient,
|
|
|
|
public userService: UserService
|
2023-05-08 17:10:13 +00:00
|
|
|
) {
|
|
|
|
this.session = localStorage.getItem('s4z_token');
|
|
|
|
this.params = new HttpParams().append('session', this.session!);
|
2022-07-12 18:22:17 +00:00
|
|
|
var auth = 'Basic ' + Buffer.from(ConfigData.UsrPwd).toString('base64');
|
2022-05-18 20:51:39 +00:00
|
|
|
this.reqHeaders = new HttpHeaders().set('Authorization', auth);
|
2022-03-25 23:57:02 +00:00
|
|
|
this.ownerUpdate = userService.ownerUpdate;
|
2023-05-08 17:10:13 +00:00
|
|
|
}
|
2022-03-25 23:57:02 +00:00
|
|
|
|
|
|
|
getOrderById(id:string) {
|
2023-05-08 17:10:13 +00:00
|
|
|
let obs = this.http.get<{message: string, order: any}>(this.beUrl+'api/order/'+id, { headers:this.reqHeaders, params: this.params, observe: 'response'});
|
2022-03-25 23:57:02 +00:00
|
|
|
|
|
|
|
obs.subscribe((OrderDataResponse) => {
|
|
|
|
if (OrderDataResponse.status == 200) {
|
|
|
|
this.dataStore.order = OrderDataResponse.body!.order;
|
|
|
|
this._orderUpdated.next(Object.assign({}, this.dataStore).order);
|
2023-05-12 14:02:45 +00:00
|
|
|
this.userService.getOwner();
|
2022-04-08 23:23:13 +00:00
|
|
|
this.ownerUpdate.subscribe((owner) => {
|
|
|
|
this.dataStore.owner = owner;
|
|
|
|
this._nameUpdated.next(Object.assign({}, this.dataStore).owner.name);
|
|
|
|
});
|
2022-03-25 23:57:02 +00:00
|
|
|
} else {
|
2022-05-23 19:14:21 +00:00
|
|
|
this._orderUpdated.next(Object.assign({}, this.dataStore).order);
|
2022-03-25 23:57:02 +00:00
|
|
|
console.log('No order found');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return obs;
|
|
|
|
}
|
2022-04-08 23:23:13 +00:00
|
|
|
|
2022-03-25 23:57:02 +00:00
|
|
|
}
|