You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
229 lines
7.9 KiB
TypeScript
229 lines
7.9 KiB
TypeScript
import { Injectable, Input } from '@angular/core';
|
|
import { Router } from '@angular/router';
|
|
|
|
import * as firebase from 'firebase/app';
|
|
import 'firebase/storage';
|
|
import { AngularFireAuth } from 'angularfire2/auth';
|
|
import { AngularFireDatabase, FirebaseListObservable } from 'angularfire2/database';
|
|
|
|
import { Parrain } from '../../../models/Parrain';
|
|
import { Commercant } from '../../../models/Commercant';
|
|
import { OperationCommerciale } from '../../../models/OperationCommerciale';
|
|
import { CarteParrainage } from '../../../models/CarteParrainage';
|
|
|
|
@Injectable()
|
|
export class OpCommercialService {
|
|
databaseRef: any;
|
|
storageRef: any;
|
|
parrain: Parrain;
|
|
commercant: Commercant;
|
|
test: string;
|
|
|
|
@Input() folder: string;
|
|
|
|
constructor(public afAuth: AngularFireAuth, public afDb: AngularFireDatabase, public router: Router) {
|
|
this.databaseRef = firebase.database().ref();
|
|
this.storageRef = firebase.storage().ref();
|
|
}
|
|
|
|
getParrains(): FirebaseListObservable<any> {
|
|
return this.afDb.list('/Parrain/', {
|
|
query: {
|
|
orderByChild: 'uid'
|
|
}
|
|
}
|
|
);
|
|
}
|
|
|
|
getCommercants(): FirebaseListObservable<any> {
|
|
return this.afDb.list('/Commercant/', {
|
|
query: {
|
|
orderByChild: 'commercialName'
|
|
}
|
|
}
|
|
);
|
|
}
|
|
|
|
getOpComs(): FirebaseListObservable<any> {
|
|
return this.afDb.list('/OperationCommercial/', {
|
|
query: {
|
|
orderByChild: 'uid'
|
|
}
|
|
}
|
|
);
|
|
}
|
|
getCartes(): FirebaseListObservable<any> {
|
|
return this.afDb.list('/CarteParrainage/', {
|
|
query: {
|
|
orderByChild: 'uid'
|
|
}
|
|
}
|
|
);
|
|
}
|
|
getCartesOP(opCommUid): FirebaseListObservable<any> {
|
|
return this.afDb.list('/CarteParrainage/', {
|
|
query: {
|
|
orderByChild: 'operationCommercialCarte/uid',
|
|
equalTo: opCommUid
|
|
}
|
|
}
|
|
);
|
|
}
|
|
|
|
getFilleuls(): FirebaseListObservable<any> {
|
|
return this.afDb.list('/Filleul/', {
|
|
query: {
|
|
orderByChild: 'uid'
|
|
}
|
|
}
|
|
);
|
|
}
|
|
|
|
updateOpCommStatus() {
|
|
const opComAll = this.getOpComs();
|
|
opComAll.subscribe(tabOpComs => {
|
|
if (tabOpComs.length > 0) {
|
|
for (let i = 0; i < tabOpComs.length ; ++i){
|
|
console.log('tabOpComs[i].name' + tabOpComs[i].name);
|
|
if(tabOpComs[i].name !== ''){
|
|
let monOpCom = tabOpComs[i];
|
|
console.log('tabOpComs[i].name not vide' + tabOpComs[i].name);
|
|
|
|
|
|
|
|
|
|
if (monOpCom !== undefined){
|
|
console.log(monOpCom);
|
|
var todayTemp = new Date(Date.now());
|
|
var debutTemp = new Date(monOpCom.dateDebut);
|
|
var finTemp = new Date(monOpCom.dateFin);
|
|
if (monOpCom.status !== 'Bloqué') {
|
|
if (todayTemp.getTime() >= debutTemp.getTime() && todayTemp.getTime() <= finTemp.getTime()) {
|
|
if (monOpCom.uid !== undefined && monOpCom.status !== undefined && monOpCom.name !== undefined
|
|
&& monOpCom.uid !== '' && monOpCom.status !== '' && monOpCom.name !== '') {
|
|
console.log(monOpCom.uid);
|
|
console.log(monOpCom.status);
|
|
monOpCom.status = 'Active';
|
|
}
|
|
} else {
|
|
if (monOpCom.uid !== undefined && monOpCom.status !== undefined && monOpCom.name !== undefined
|
|
&& monOpCom.uid !== '' && monOpCom.status !== '' && monOpCom.name !== ''){
|
|
console.log(monOpCom.uid);
|
|
console.log(monOpCom.status);
|
|
monOpCom.status = 'Inactive';
|
|
}
|
|
}
|
|
if (monOpCom.uid !== undefined && monOpCom.status !== undefined && monOpCom.name !== undefined
|
|
&& monOpCom.uid !== '' && monOpCom.status !== '' && monOpCom.name !== '') {
|
|
console.log('monOpCom.uid: ' + monOpCom.uid);
|
|
console.log('monOpCom.status: ' + monOpCom.status);
|
|
const carteStatusRef = firebase.database().ref().child('OperationCommercial').child(monOpCom.uid).child('status');
|
|
carteStatusRef.set(monOpCom.status);
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
});
|
|
opComAll.forEach(utilisateur => {
|
|
utilisateur.forEach(monOpCom => {
|
|
|
|
});
|
|
});
|
|
}
|
|
|
|
updateCarteEtOpCom(){
|
|
this.updateOpCommStatus();
|
|
this.updateCarteStatus();
|
|
}
|
|
|
|
updateCarteStatus(){
|
|
const carteAll = this.getCartes();
|
|
|
|
carteAll.subscribe(tabCarte => {
|
|
if (tabCarte.length > 0) {
|
|
for (let i = 0; i < tabCarte.length ; ++i){
|
|
console.log('tabCarte[i].parrainCarte.firstname' + tabCarte[i].parrainCarte.firstname);
|
|
if (tabCarte[i].parrainCarte.firstname !== '') {
|
|
let maCarte = tabCarte[i];
|
|
|
|
if(maCarte !== undefined) {
|
|
var todayTemp = new Date(Date.now());
|
|
var debutTemp = new Date(maCarte.dateDebut);
|
|
var finTemp = new Date(maCarte.dateFin);
|
|
if (todayTemp.getTime() >= debutTemp.getTime() && todayTemp.getTime() <= finTemp.getTime()) {
|
|
if(maCarte.status !== undefined && maCarte.uid !== undefined && maCarte.parrainCarte !== undefined
|
|
&& maCarte.status !== '' && maCarte.uid !== '' && maCarte.parrainCarte.firstname !== '') {
|
|
console.log('maCarte.uid: ' + maCarte.uid);
|
|
console.log('maCarte.status: ' + maCarte.status);
|
|
maCarte.status = 'Active';
|
|
}
|
|
} else {
|
|
if (maCarte.status !== undefined && maCarte.uid !== undefined && maCarte.parrainCarte !== undefined
|
|
&& maCarte.status !== '' && maCarte.uid !== '' && maCarte.parrainCarte.firstname !== '') {
|
|
console.log('maCarte.uid: ' + maCarte.uid);
|
|
console.log('maCarte.status: ' + maCarte.status);
|
|
maCarte.status = 'Inactive';
|
|
}
|
|
}
|
|
if (maCarte.status !== undefined && maCarte.uid !== undefined && maCarte.parrainCarte !== undefined
|
|
&& maCarte.status !== '' && maCarte.uid !== '' && maCarte.parrainCarte.firstname !== '') {
|
|
console.log('maCarte.uid: ' + maCarte.uid);
|
|
console.log('maCarte.status: ' + maCarte.status);
|
|
const carteStatusRef = firebase.database().ref().child('CarteParrainage').child(maCarte.uid).child('status');
|
|
carteStatusRef.set(maCarte.status);
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
});
|
|
carteAll.forEach(utilisateur => {
|
|
utilisateur.forEach(maCarte => {
|
|
|
|
});
|
|
});
|
|
}
|
|
|
|
saveOpComm(user: firebase.User, infoOpCom: OperationCommerciale) {
|
|
|
|
infoOpCom.uid = firebase.database().ref().child('OperationCommercial').push().key;
|
|
let updates = {};
|
|
updates['/OperationCommercial/' + infoOpCom.uid] = infoOpCom;
|
|
firebase.database().ref().update(updates);
|
|
this.upload(infoOpCom);
|
|
}
|
|
|
|
|
|
upload(info: any) {
|
|
// Create a root reference
|
|
let storageRef = firebase.storage().ref();
|
|
this.folder = '/OperationCommercial/' + info.uid + '/carteTemplate';
|
|
|
|
if ((<HTMLInputElement>document.getElementById('orangeForm-File')) !== null) {
|
|
for (let selectedFile of [(<HTMLInputElement>document.getElementById('orangeForm-File')).files[0]]) {
|
|
// Make local copies of services because "this" will be clobbered
|
|
let router = this.router;
|
|
|
|
let folder = this.folder;
|
|
|
|
let path = `${this.folder}`;
|
|
|
|
let iRef = storageRef.child(path);
|
|
|
|
iRef.put(selectedFile).then((snapshot) => {
|
|
let updates = {};
|
|
info.carteTemplate.path = path;
|
|
info.carteTemplate.filename = info.name;
|
|
info.carteTemplate.downloadURL = snapshot.downloadURL;
|
|
updates[folder] = info.carteTemplate;
|
|
firebase.database().ref().update(updates);
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|