785 lines
32 KiB
TypeScript
785 lines
32 KiB
TypeScript
import { HttpClient, HttpParams } from '@angular/common/http';
|
|
import { Injectable } from '@angular/core';
|
|
import { Observable, Observer, forkJoin, map, tap } from 'rxjs';
|
|
import { environment } from 'src/environments/environment';
|
|
import { IonicStorageService } from 'src/providers/ionic-storage/ionic-storage.service';
|
|
import { Storage } from '@ionic/storage-angular'
|
|
import { Capacitor } from '@capacitor/core';
|
|
import { ConnectionStatus, NetworkDetectionService } from 'src/providers/network-detection/network-detection.service';
|
|
import { OfflineManagerService } from '../offline-manager/offline-manager.service';
|
|
import { CameraService } from '../camera/camera.service';
|
|
|
|
const pd_key ='local_sales_PDs'
|
|
@Injectable({
|
|
providedIn: 'root'
|
|
})
|
|
export class SalesPdService {
|
|
private apiUrl = environment.url
|
|
original_rawData:any;
|
|
available_sections:any;
|
|
current_section:any;
|
|
constructor(public http: HttpClient, private ionicStorageProvider:IonicStorageService,private storage: Storage,private networkProvider:NetworkDetectionService,private offlineManagerProvider:OfflineManagerService,private cameraService:CameraService) { }
|
|
|
|
getData(product_id: any, sales_pd_type?: any): Observable<any> {
|
|
const users:any = localStorage.getItem('user_details');
|
|
console.log(users)
|
|
const parsedUsers = JSON.parse(users);
|
|
console.log(parsedUsers)
|
|
const params = {
|
|
records: {
|
|
lender_id: parsedUsers.fk_entity_id,
|
|
product_id: product_id,
|
|
sales_pd_type: sales_pd_type,
|
|
},
|
|
};
|
|
console.log(params)
|
|
return new Observable((observer: any) => {
|
|
if (this.networkProvider.getCurrentNetworkStatus() == ConnectionStatus.online) {
|
|
console.log('Capacitor.isNative',Capacitor.isNative);
|
|
let api_properties = {
|
|
method: 'post',
|
|
api_name: this.apiUrl + 'loadSalesPDTemplate',
|
|
params: params,
|
|
};
|
|
|
|
// START OF GETTING LOCAL TEMPLATE ONLY ON BROWSER
|
|
if (!Capacitor.isNative) {
|
|
const fileName = product_id + (sales_pd_type ? '-' + sales_pd_type : '');
|
|
// api_properties = {method:'get', api_name:`assets/data/sales_pd_templates/${fileName}.json`,params : params}
|
|
}
|
|
// END OF GETTING LOCAL TEMPLATE
|
|
// this.http.request(api_properties.method, api_properties.api_name, { body: api_properties.params }).subscribe(
|
|
this.http.post(api_properties.api_name, api_properties.params).subscribe(
|
|
(result:any) => {
|
|
console.log(result)
|
|
if (!Capacitor.isNative && !result.hasOwnProperty('dataStatus')) {
|
|
result = {
|
|
dataStatus: true,
|
|
status: 200,
|
|
records: { template: JSON.stringify(result) },
|
|
};
|
|
}
|
|
|
|
if (result['dataStatus']) {
|
|
this.ionicStorageProvider.checkAndInsertApiDataLocally(result, params, 'loadSalesPDTemplate', true).then((res) => {
|
|
observer.next(result);
|
|
observer.complete();
|
|
});
|
|
} else {
|
|
observer.next(result);
|
|
observer.complete();
|
|
}
|
|
},
|
|
(error) => {
|
|
// Handle error
|
|
console.error(error);
|
|
observer.error(error);
|
|
}
|
|
);
|
|
} else {
|
|
this.ionicStorageProvider.getApiDataFromLocally(params, 'loadSalesPDTemplate').subscribe((result) => {
|
|
observer.next(result);
|
|
observer.complete();
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
getSalesPDList(isSalesLogin: boolean): Observable<any> {
|
|
let userDetails:any = localStorage.getItem('user_details')
|
|
const det = JSON.parse(userDetails);
|
|
const params = { fk_entity_id: det.fk_entity_id, userid: det.userid };
|
|
|
|
if (this.networkProvider.getCurrentNetworkStatus() === ConnectionStatus.online) {
|
|
let complete_url = this.apiUrl + 'listSalesPD/' + det.fk_entity_id;
|
|
if (isSalesLogin) {
|
|
complete_url = complete_url+'/'+det.userid+'/'+det.user_role;
|
|
}
|
|
const options = { params: new HttpParams({ fromObject: params }) };
|
|
|
|
return this.http.get(complete_url, options).pipe(
|
|
tap((res: any) => {
|
|
this.ionicStorageProvider.checkAndInsertApiDataLocally(res, params, 'listSalesPD');
|
|
})
|
|
);
|
|
} else {
|
|
return this.ionicStorageProvider.getApiDataFromLocally(params, 'listSalesPD');
|
|
}
|
|
}
|
|
|
|
insertData_Replace(key:any,data:any){
|
|
// console.log(key)
|
|
return this.storage.set(key,data);
|
|
}
|
|
|
|
getSalesPdpdf(pdfdetail:any): Observable<any> {
|
|
return this.http.get<any[]>(this.apiUrl + "downloadSalesPDReport/" + pdfdetail)
|
|
}
|
|
|
|
getSalesPDSectionData(params:any){
|
|
console.log(params)
|
|
return Observable.create((observer:Observer<any>)=>{
|
|
this.offlineManagerProvider.getSalesPDSectionLocally(params).then(result => {
|
|
if(result !== null && result != undefined) {
|
|
let modified_result:any={};
|
|
if(params.section_id == 'all'){
|
|
modified_result = result
|
|
}
|
|
else {
|
|
modified_result.records = [result]
|
|
modified_result.dataStatus = true
|
|
modified_result.status = 200
|
|
console.clear()
|
|
}
|
|
console.log(modified_result)
|
|
observer.next(modified_result);
|
|
observer.complete()
|
|
|
|
}
|
|
else if(this.networkProvider.getCurrentNetworkStatus() == ConnectionStatus.online) {
|
|
let values={sales_pd_id:params.sales_pd_id,dataStatus:true}
|
|
return this.http.post(this.apiUrl+'getSalesPDSectionData',{records:params}).subscribe((result:any) =>{
|
|
let modified_values:any =result
|
|
modified_values.section_id = params.section_id
|
|
// let resultDetails = modified_values['dataStatus']
|
|
if(result['dataStatus']) {
|
|
if(params.section_id != 'all') {
|
|
modified_values = result['records'][0]
|
|
modified_values.is_synced = true
|
|
}
|
|
if(this.networkProvider.getCurrentNetworkStatus() == ConnectionStatus.online) {
|
|
this.checkAndCreatePDLocally(values,'','online','',modified_values).then((res:any)=>{
|
|
observer.next(result);
|
|
observer.complete()
|
|
})
|
|
}
|
|
else {
|
|
observer.next(result);
|
|
observer.complete()
|
|
}
|
|
}
|
|
else{
|
|
observer.next(result);
|
|
observer.complete()
|
|
}
|
|
},err=>{
|
|
observer.next(err);
|
|
observer.complete();
|
|
})
|
|
}
|
|
else {
|
|
console.log("No Data Found#########");
|
|
// observer.error({err_msg:'No Data Found'})
|
|
// Observable.throw(new Error('No Data Found'))
|
|
observer.next({dataStatus:false})
|
|
observer.complete();
|
|
}
|
|
return null;
|
|
})
|
|
})
|
|
}
|
|
|
|
checkAndCreatePDLocally(modified_values:any,params:any,connection_status:any,request_items?:any,section_data?:any) {
|
|
let paramsCompare;
|
|
if(modified_values.sales_pd_id != null && modified_values.sales_pd_id != undefined && typeof(modified_values.sales_pd_id) == 'string') {
|
|
console.log(modified_values.sales_pd_id);
|
|
console.log("######### local pd id ",modified_values.sales_pd_id.includes('local'))
|
|
if(modified_values.sales_pd_id.includes('local')) {
|
|
modified_values.local_pd_id=modified_values.sales_pd_id
|
|
modified_values.sales_pd_id =null
|
|
}
|
|
}
|
|
if(modified_values.sales_pd_id != null) {
|
|
return this.offlineManagerProvider.checkTheExistingPDLocally(modified_values.sales_pd_id,'sales_pd_id').then(result=>{
|
|
console.log("offline result",result);
|
|
// debugger;
|
|
if(result || result != undefined) {
|
|
paramsCompare = {sales_pd_id : modified_values.sales_pd_id}
|
|
modified_values = result
|
|
if(params.hasOwnProperty('status') && params.stautus == 'COMPLETED') {
|
|
modified_values = result
|
|
modified_values.dataStatus = true
|
|
}
|
|
if(section_data) {
|
|
if(section_data.section_id == '1' && section_data.hasOwnProperty('questions') && section_data.questions.filter((val:any)=>val.question_key == 'company_name').length > 0) {
|
|
let company_ques_index = section_data.questions.findIndex((val:any)=>val.question_key == "company_name")
|
|
modified_values.applicant_name = section_data.questions[company_ques_index].answer_value
|
|
}
|
|
|
|
let update_section_datas =this.offlineManagerProvider.updateSectionData(section_data,result)
|
|
modified_values.section_datas = update_section_datas
|
|
if(modified_values.section_datas.length > 0 && modified_values.section_datas.filter((val:any)=>val.is_synced == false).length > 0){
|
|
modified_values.is_all_section_synced = false
|
|
}
|
|
}
|
|
if(params.hasOwnProperty('status') && params.status == 'COMPLETED') {
|
|
if(connection_status == 'offline'){
|
|
modified_values.is_pd_completed = true;
|
|
modified_values.is_pd_completed_sync = false
|
|
modified_values.status = '_COMPLETED'
|
|
modified_values.pd_complete_request_items = request_items
|
|
}
|
|
else {
|
|
modified_values.is_pd_completed = true;
|
|
modified_values.is_pd_completed_sync = true
|
|
}
|
|
}
|
|
// this.offlineManagerProvider.getlocalPDData(modified_values,connection_status,request_items)
|
|
}
|
|
else{
|
|
// console.clear();
|
|
// console.log("check c",modified_values)
|
|
paramsCompare = {sales_pd_id:String(modified_values.sales_pd_id)}
|
|
if(section_data) {
|
|
if(section_data.section_id == '1' && section_data.hasOwnProperty('questions') && section_data.questions.filter((val:any)=>val.question_key == 'company_name').length > 0) {
|
|
let company_ques_index = section_data.questions.findIndex((val:any)=>val.question_key == "company_name")
|
|
modified_values.applicant_name = section_data.questions[company_ques_index].answer_value
|
|
}
|
|
|
|
let update_section_datas =this.offlineManagerProvider.updateSectionData(section_data,result)
|
|
modified_values.section_datas = update_section_datas
|
|
if(modified_values.section_datas.length > 0 && modified_values.section_datas.filter((val:any)=>val.is_synced == false).length > 0){
|
|
modified_values.is_all_section_synced = false
|
|
}
|
|
}
|
|
this.offlineManagerProvider.getlocalPDData(modified_values,connection_status,request_items)
|
|
}
|
|
return this.ionicStorageProvider.checkAndInsertApiDataLocally(modified_values,paramsCompare,pd_key,true).then(res=>{
|
|
return modified_values
|
|
})
|
|
})
|
|
}
|
|
else {
|
|
return this.offlineManagerProvider.getTheLengthOfLocalPDs().then(Res=>{
|
|
return this.offlineManagerProvider.checkTheExistingPDLocally(modified_values.local_pd_id,'local_pd_id').then(result=>{
|
|
console.log("local pd id",result,params.local_pd_id);
|
|
if((result || result != undefined) && modified_values.local_pd_id != undefined) {
|
|
paramsCompare = {local_pd_id:modified_values.local_pd_id}
|
|
modified_values = result
|
|
if(section_data) {
|
|
if(section_data.section_id == '1' && section_data.hasOwnProperty('questions') && section_data.questions.filter((val:any)=>val.question_key == 'company_name').length > 0) {
|
|
let company_ques_index = section_data.questions.findIndex((val:any)=>val.question_key == "company_name")
|
|
modified_values.applicant_name = section_data.questions[company_ques_index].answer_value
|
|
}
|
|
|
|
let update_section_datas =this.offlineManagerProvider.updateSectionData(section_data,result)
|
|
modified_values.section_datas = update_section_datas
|
|
if(modified_values.section_datas.length > 0 && modified_values.section_datas.filter((val:any)=>val.is_synced == false).length > 0){
|
|
modified_values.is_all_section_synced = false
|
|
}
|
|
}
|
|
if(params.hasOwnProperty('status') && params.status == 'COMPLETED') {
|
|
if(connection_status == 'offline'){
|
|
modified_values.is_pd_completed = true;
|
|
modified_values.is_pd_completed_sync = false
|
|
modified_values.pd_complete_request_items = request_items
|
|
modified_values.status = '_COMPLETED'
|
|
}
|
|
else {
|
|
modified_values.is_pd_completed = true;
|
|
modified_values.is_pd_completed_sync = true
|
|
}
|
|
}
|
|
// this.offlineManagerProvider.getlocalPDData(modified_values,connection_status,request_items)
|
|
}
|
|
else {
|
|
console.log("Else##########",Res)
|
|
let pd_id:string= '1'
|
|
if(Res){
|
|
pd_id =Res
|
|
}
|
|
paramsCompare = {local_pd_id:pd_id}
|
|
modified_values.local_pd_id = pd_id
|
|
if(section_data) {
|
|
if(section_data.section_id == '1' && section_data.hasOwnProperty('questions') && section_data.questions.filter((val:any)=>val.question_key == 'company_name').length > 0) {
|
|
let company_ques_index = section_data.questions.findIndex((val:any)=>val.question_key == "company_name")
|
|
modified_values.applicant_name = section_data.questions[company_ques_index].answer_value
|
|
}
|
|
|
|
let update_section_datas =this.offlineManagerProvider.updateSectionData(section_data,result)
|
|
modified_values.section_datas = update_section_datas
|
|
if(modified_values.section_datas.length > 0 && modified_values.section_datas.filter((val:any)=>val.is_synced == false).length > 0){
|
|
modified_values.is_all_section_synced = false
|
|
}
|
|
}
|
|
this.offlineManagerProvider.getlocalPDData(modified_values,connection_status,request_items)
|
|
|
|
}
|
|
return this.ionicStorageProvider.checkAndInsertApiDataLocally(modified_values,paramsCompare,pd_key,true).then(res=>{
|
|
return modified_values
|
|
})
|
|
})
|
|
})
|
|
}
|
|
}
|
|
|
|
getAllMasterDatas(TableName: string): Observable<any> {
|
|
let params = { "master_name": TableName }
|
|
return new Observable((observer:any)=>{
|
|
if(this.networkProvider.getCurrentNetworkStatus() == ConnectionStatus.online) {
|
|
return this.http.post(this.apiUrl + 'getListOfMaster', { "master_name": TableName }).subscribe(result=>{
|
|
return this.ionicStorageProvider.checkAndInsertApiDataLocally(result,params,'getListOfMaster',true).then(rr=>{
|
|
observer.next(result);
|
|
observer.complete();
|
|
})
|
|
})
|
|
}
|
|
else {
|
|
return this.ionicStorageProvider.getApiDataFromLocally(params,'getListOfMaster').subscribe(result=>{
|
|
observer.next(result);
|
|
observer.complete();
|
|
})
|
|
}
|
|
})
|
|
}
|
|
|
|
getCityPincode(value:any){
|
|
let params={records:{state_id:value}}
|
|
return new Observable((observer:any)=>{
|
|
if(this.networkProvider.getCurrentNetworkStatus() == ConnectionStatus.online) {
|
|
return this.http.post(this.apiUrl+"getListOfStatesAndCities",params).subscribe(result=>{
|
|
return this.ionicStorageProvider.checkAndInsertApiDataLocally(result,params,'getListOfStatesAndCities',true).then(res=>{
|
|
observer.next(result);
|
|
observer.complete();
|
|
})
|
|
})
|
|
}
|
|
else {
|
|
return this.ionicStorageProvider.getApiDataFromLocally(params,'getListOfStatesAndCities').subscribe(result=>{
|
|
observer.next(result);
|
|
observer.complete();
|
|
})
|
|
}
|
|
})
|
|
}
|
|
|
|
callMultipleApisForTemplate(product_arr:any) {
|
|
let users:any=localStorage.getItem('user_details')
|
|
users=JSON.parse(users)
|
|
let arr_call = []
|
|
for(let val of product_arr) {
|
|
let params1 = {records:{lender_id:users.fk_entity_id,product_id:val.product_id,sales_pd_type:'1'}}
|
|
let params2 = {records:{lender_id:users.fk_entity_id,product_id:val.product_id,sales_pd_type:'2'}}
|
|
let api1= this.http.post(this.apiUrl+'loadSalesPDTemplate',params1)
|
|
let api2= this.http.post(this.apiUrl+'loadSalesPDTemplate',params2)
|
|
arr_call.push(api1)
|
|
arr_call.push(api2)
|
|
}
|
|
forkJoin(arr_call).subscribe((res:any)=>{
|
|
console.log("rrsadfsadf",res)
|
|
res.forEach((val:any)=>{
|
|
if(val['dataStatus']) {
|
|
let params = {records:{lender_id:val.records.lender_id,product_id:val.records.product_id,sales_pd_type:val.records.sales_pd_type}}
|
|
// this.ionicStorageProvider.checkAndInsertApiDataLocally(val,params,'loadSalesPDTemplate',true).then(rr=>)
|
|
val.params = params
|
|
}
|
|
})
|
|
console.log("final lksdjfl asdf", res);
|
|
this.ionicStorageProvider.insertDataWithReplace('loadSalesPDTemplate',res).then(rr=>{})
|
|
})
|
|
|
|
}
|
|
|
|
// getData_fromLocal(key:any){
|
|
// // console.log("get data key",this.getKey(key));
|
|
// // console.log("get data provider",this.storage.get(key))
|
|
// // console.log("all key in storage",this.storage.keys())
|
|
// return this.storage.get(key);
|
|
// }
|
|
|
|
getData_fromLocal(key: any): Promise<any> {
|
|
return this.storage.get(key);
|
|
}
|
|
|
|
clear_local_key(key:any){
|
|
return this.storage.remove(key)
|
|
}
|
|
|
|
clearLocalData(){
|
|
this.storage.clear().then(res=>{
|
|
console.log(res)
|
|
})
|
|
}
|
|
|
|
api_post_method(apiName: string, params: any, apiMethod?: string): Observable<any> {
|
|
if (this.networkProvider.getCurrentNetworkStatus() === ConnectionStatus.online) {
|
|
return new Observable((observer: Observer<any>) => {
|
|
if (apiMethod?.toUpperCase() === 'POST') {
|
|
this.http.post(this.apiUrl + apiName, params).subscribe(result => {
|
|
if ((result as any)['dataStatus']) {
|
|
this.ionicStorageProvider.checkAndInsertApiDataLocally(result, params, apiName, true).then(
|
|
r => {
|
|
observer.next(result);
|
|
observer.complete();
|
|
},
|
|
err => console.log("Ionic storage error", err)
|
|
);
|
|
} else {
|
|
observer.next(result);
|
|
observer.complete();
|
|
}
|
|
});
|
|
} else {
|
|
this.http.get(this.apiUrl + apiName).subscribe(result => {
|
|
this.ionicStorageProvider.checkAndInsertApiDataLocally(result, params, apiName).then(
|
|
rr => {
|
|
observer.next(result);
|
|
observer.complete();
|
|
},
|
|
err => console.log("Ionic storage error", err)
|
|
);
|
|
});
|
|
}
|
|
});
|
|
}
|
|
// else if (apiName === 'getStakeHoldersInfo') {
|
|
// return this.creditPDService.getStakeholderInfo().map(res => {
|
|
// console.log(res);
|
|
// if (res['dataStatus']) {
|
|
// let sectionData = res['records'][0].sectionData; // Adjust the property name based on the actual response structure
|
|
// sectionData = JSON.parse(sectionData);
|
|
// let stakeholderQues = sectionData.questions.filter((vv: any) => vv.isRepeatable === '1')[0]; // Adjust property names
|
|
|
|
// console.log(stakeholderQues);
|
|
|
|
// if (stakeholderQues?.questionsGroup?.length > 0) {
|
|
// let applicants = stakeholderQues.questionsGroup.map((vv: any) => vv.questions[0].answerValue); // Adjust property names
|
|
// console.log(applicants);
|
|
// return { dataStatus: true, records: applicants };
|
|
// }
|
|
// }
|
|
|
|
// return res;
|
|
// });
|
|
// }
|
|
else {
|
|
return this.ionicStorageProvider.getApiDataFromLocally(params, apiName);
|
|
}
|
|
}
|
|
|
|
generateSatelliteImg(params:any,form_structure:any):Observable<Response> {
|
|
return Observable.create((observer:Observer<any>)=>{
|
|
this.getData_fromLocal('sales_pd_id_PRIMARYKEY').then(key_value=>{
|
|
params.sales_pd_id = key_value
|
|
let values={sales_pd_id:key_value,dataStatus:true}
|
|
let section_data=JSON.stringify({section_id:form_structure.section_id,section_name:form_structure.section_name,onsubmit:null,questions:form_structure.questions})
|
|
let modified_params = form_structure
|
|
if(this.networkProvider.getCurrentNetworkStatus() == ConnectionStatus.online && (key_value != null ? (typeof(key_value) == 'string' ? !key_value.includes('local') : true) : true)) {
|
|
this.http.post(this.apiUrl+'generateSatelliteImg',params).pipe()
|
|
.subscribe(result =>{
|
|
observer.next(result);
|
|
observer.complete();
|
|
}
|
|
,err=>{
|
|
console.clear();
|
|
console.log("err>>>>",err)
|
|
observer.next(err);
|
|
observer.complete();
|
|
})
|
|
}
|
|
else {
|
|
console.log("#######params",modified_params)
|
|
let offline_params = modified_params
|
|
offline_params.request_items=[{url:this.apiUrl+'generateSatelliteImg',
|
|
params:JSON.stringify(params),method:'POST'}]
|
|
offline_params.is_synced = false
|
|
offline_params.section_data = section_data
|
|
console.log(offline_params.request_items.params)
|
|
this.checkAndCreatePDLocally(values,'','offline','',offline_params).then(res=>{
|
|
observer.next({dataStatus:true});
|
|
observer.complete()
|
|
})
|
|
}
|
|
})
|
|
})
|
|
}
|
|
|
|
getSalesPDImgDataCusLink(params:any) {
|
|
return this.http.post(this.apiUrl+'getSalesPDImgDataCusLink',params)
|
|
}
|
|
|
|
saveSalesPDImage(params:any,form_structure?:any): Observable<Response>{
|
|
return Observable.create((observer:Observer<any>) => {
|
|
this.getData_fromLocal('sales_pd_id_PRIMARYKEY').then(key_value=>{
|
|
// this.cameraService.getGeoTag().then((geoCords:any)=>{
|
|
// params.location=geoCords
|
|
params.fk_sales_pd_id=key_value
|
|
if(this.networkProvider.getCurrentNetworkStatus() == ConnectionStatus.online && (key_value != null ? (typeof(key_value) == 'string' ? !key_value.includes('local') : true) : true)) {
|
|
this.http.post(this.apiUrl+'saveSalesPDImage',{records:params},{
|
|
reportProgress: true,
|
|
}).pipe().subscribe(result => {
|
|
observer.next(result);
|
|
observer.complete();
|
|
},err=>{
|
|
console.clear();
|
|
// console.log("err>>>>",err)
|
|
observer.next(err);
|
|
observer.complete();
|
|
})
|
|
}
|
|
else {
|
|
observer.next({dataStatus:true});
|
|
observer.complete()
|
|
// })
|
|
}
|
|
// })
|
|
})
|
|
})
|
|
}
|
|
|
|
|
|
updateSectionDataLocally(params:any,form_structure:any){
|
|
return Observable.create((observer:Observer<any>) => {
|
|
this.getData_fromLocal('sales_pd_id_PRIMARYKEY').then(key_value=>{
|
|
// params.location=geoCords
|
|
params.fk_sales_pd_id=key_value
|
|
let values={sales_pd_id:key_value,dataStatus:true}
|
|
let section_data=JSON.stringify({section_id:form_structure.section_id,section_name:form_structure.section_name,onsubmit:null,questions:form_structure.questions})
|
|
let modified_params = form_structure
|
|
if(this.networkProvider.getCurrentNetworkStatus() == ConnectionStatus.online && (key_value != null ? (typeof(key_value) == 'string' ? !key_value.includes('local') : true) : true)) {
|
|
modified_params.is_synced = true
|
|
modified_params.section_data = section_data
|
|
this.checkAndCreatePDLocally(values,'','online','',modified_params).then(res=>{
|
|
observer.next('');
|
|
observer.complete();
|
|
})
|
|
// },err=>{
|
|
// console.clear();
|
|
// // console.log("err>>>>",err)
|
|
// observer.next(err);
|
|
// observer.complete();
|
|
// })
|
|
}
|
|
else {
|
|
// console.log("#######params",modified_params)
|
|
let offline_params = modified_params
|
|
offline_params.request_items=[{url:this.apiUrl+'saveSalesPDImage',
|
|
params:JSON.stringify({records:params}),method:'POST'}]
|
|
offline_params.is_synced = false
|
|
offline_params.section_data = section_data
|
|
console.log(offline_params)
|
|
this.checkAndCreatePDLocally(values,'','offline','',offline_params).then(res=>{
|
|
observer.next({dataStatus:true});
|
|
observer.complete()
|
|
})
|
|
}
|
|
})
|
|
})
|
|
}
|
|
|
|
saveSalesPDSection(params: any): Observable<any> {
|
|
let users:any=localStorage.getItem('user_details')
|
|
users=JSON.parse(users)
|
|
params.fk_createdby=users.userid
|
|
return Observable.create((observer: Observer<any>) => {
|
|
this.getData_fromLocal('sales_pd_id_PRIMARYKEY').then((key_value) => {
|
|
params.sales_pd_id = key_value;
|
|
let values = { sales_pd_id: key_value, dataStatus: true };
|
|
let section_data = JSON.stringify({ section_id: params.section_id, section_name: params.section_name, onsubmit: null, questions: params.questions });
|
|
let modified_params = params;
|
|
|
|
if (this.networkProvider.getCurrentNetworkStatus() == ConnectionStatus.online && (key_value != null ? (typeof (key_value) == 'string' ? !key_value.includes('local') : true) : true)) {
|
|
this.http.post(this.apiUrl + 'saveSalesPDsection', { records: params }).pipe().subscribe(response => {
|
|
modified_params.is_synced = true;
|
|
modified_params.section_data = section_data;
|
|
if (modified_params.section_id != '9') {
|
|
this.checkAndCreatePDLocally(values, '', 'online', '', modified_params).then(res => {
|
|
observer.next(response);
|
|
observer.complete();
|
|
});
|
|
} else {
|
|
observer.next(response);
|
|
observer.complete();
|
|
}
|
|
});
|
|
} else if (modified_params.section_id != '9') {
|
|
let offline_params = modified_params;
|
|
offline_params.request_items = {
|
|
url: this.apiUrl + 'saveSalesPDsection',
|
|
params: JSON.stringify({ records: params }),
|
|
method: 'POST'
|
|
};
|
|
offline_params.is_synced = false;
|
|
offline_params.section_data = section_data;
|
|
|
|
this.checkAndCreatePDLocally(values, '', 'offline', '', offline_params).then(res => {
|
|
observer.next({ dataStatus: true });
|
|
observer.complete();
|
|
});
|
|
} else {
|
|
observer.next({ dataStatus: true });
|
|
observer.complete();
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
saveQuestions(params: any): Observable<Response> {
|
|
let result_from_api: any;
|
|
let userid: any = localStorage.getItem("user_details");
|
|
userid = JSON.parse(userid).userid;
|
|
params.createdby = userid;
|
|
let product_id = localStorage.getItem('product_id_salesPD');
|
|
let sales_pd_type = localStorage.getItem('sales_pd_type');
|
|
let users: any = localStorage.getItem('user_details');
|
|
users = JSON.parse(users);
|
|
|
|
return new Observable((observer: Observer<any>) => {
|
|
this.getData_fromLocal('sales_pd_id_PRIMARYKEY').then(key_value => {
|
|
params.sales_pd_id = key_value;
|
|
params.lender_id = users.fk_entity_id;
|
|
params.product_id = product_id;
|
|
params.sales_pd_type = sales_pd_type;
|
|
let modified_values = params;
|
|
|
|
if (this.networkProvider.getCurrentNetworkStatus() == ConnectionStatus.online &&
|
|
(key_value != null ? (typeof (key_value) == 'string' ? !key_value.includes('local') : true) : true)) {
|
|
this.http.post(this.apiUrl + 'saveSalesPD', { records: params }).pipe().subscribe((result: any) => {
|
|
result_from_api = result;
|
|
|
|
if (!params.hasOwnProperty('status') && params.status != 'COMPLETED') {
|
|
if (params.sales_pd_id != null) {
|
|
modified_values.sales_pd_id = params.sales_pd_id;
|
|
} else {
|
|
modified_values.sales_pd_id = result['records'];
|
|
}
|
|
}
|
|
|
|
if (params.hasOwnProperty('status') && params.status == 'COMPLETED') {
|
|
if (!result_from_api.hasOwnProperty('pd_status') && result_from_api['pd_status'] != 'DRAFT') {
|
|
this.offlineManagerProvider.deleteCompletedPD(params.sales_pd_id).then(rd => {
|
|
observer.next(result_from_api);
|
|
observer.complete();
|
|
});
|
|
} else {
|
|
observer.next(result_from_api);
|
|
observer.complete();
|
|
}
|
|
} else {
|
|
this.checkAndCreatePDLocally(modified_values, params, 'online').then(lc => {
|
|
observer.next(result_from_api);
|
|
observer.complete();
|
|
});
|
|
}
|
|
});
|
|
} else if (params.hasOwnProperty('status') && params.status == 'COMPLETED') {
|
|
let pd_id_key = this.offlineManagerProvider.getThePDType(modified_values.sales_pd_id);
|
|
this.offlineManagerProvider.checkTheExistingPDLocally(modified_values.sales_pd_id, pd_id_key).then(result => {
|
|
if (result && result.hasOwnProperty('section_datas') && result.section_datas.length > 0) {
|
|
let pd_section_datas = result.section_datas.filter((val: any) => val.section_id != 'all').map((val1: any) => val1.section_name);
|
|
let template_sections: any = localStorage.getItem('question_temp_salespd');
|
|
template_sections = JSON.parse(template_sections).sections.map((val1: any) => val1.section_name);
|
|
|
|
if (template_sections.length != pd_section_datas.length) {
|
|
let unsavedSections = template_sections.filter(function (item: any) {
|
|
return !pd_section_datas.includes(item);
|
|
});
|
|
|
|
let unsavedObj: any = [];
|
|
unsavedSections.forEach((val: any, index: any) => {
|
|
unsavedObj.push({ id: index, sectin_name: val });
|
|
});
|
|
|
|
if (unsavedSections.length > 0) {
|
|
let local_result = {
|
|
'pd_status': 'DRAFT',
|
|
'records': unsavedObj,
|
|
'dataStatus': true
|
|
};
|
|
|
|
observer.next(local_result);
|
|
observer.complete();
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
let req_params = { url: this.apiUrl + 'saveSalesPD', params: JSON.stringify({ records: params }), method: 'POST' };
|
|
this.checkAndCreatePDLocally(modified_values, params, 'offline', req_params).then(lc => {
|
|
if (params.hasOwnProperty('status') && params.status != 'COMPLETED') {
|
|
result_from_api = { dataStatus: true, records: lc.local_pd_id };
|
|
} else {
|
|
result_from_api = { dataStatus: true, records: true };
|
|
}
|
|
|
|
observer.next(result_from_api);
|
|
observer.complete();
|
|
});
|
|
});
|
|
} else {
|
|
if (params.hasOwnProperty('status') && params.status != 'COMPLETED') {
|
|
modified_values.sales_pd_id = null;
|
|
}
|
|
|
|
let req_params = { url: this.apiUrl + 'saveSalesPD', params: JSON.stringify({ records: params }), method: 'POST' };
|
|
let dd = { product_id: modified_values.product_id, lender_id: modified_values.lender_id };
|
|
|
|
this.getProductAbbrbyId(dd).then((product_abbr: any) => {
|
|
modified_values.abbr = product_abbr;
|
|
this.checkAndCreatePDLocally(modified_values, params, 'offline', req_params).then(lc => {
|
|
if (params.hasOwnProperty('status') && params.status != 'COMPLETED') {
|
|
result_from_api = { dataStatus: true, records: lc.local_pd_id };
|
|
} else {
|
|
result_from_api = { dataStatus: true, records: true };
|
|
}
|
|
|
|
observer.next(result_from_api);
|
|
observer.complete();
|
|
});
|
|
});
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
|
|
getProductAbbrbyId(obj: any): Promise<string | undefined> {
|
|
if (obj) {
|
|
return new Promise((resolve, reject) => {
|
|
return this.getEntityPreferences(obj.lender_id).subscribe((result: any) => {
|
|
if (result && result['dataStatus']) {
|
|
const productforLender = result['records'].salespd_products;
|
|
if (productforLender && productforLender.length > 0) {
|
|
const product_abbr = productforLender
|
|
.filter((val: any) => val.product_id == obj.product_id)
|
|
.map((va: any) => va.product_abbr)[0];
|
|
|
|
resolve(product_abbr);
|
|
} else {
|
|
resolve(undefined); // Handle the case where product_abbr is undefined
|
|
}
|
|
} else {
|
|
resolve(undefined); // Handle the case where dataStatus is falsy
|
|
}
|
|
});
|
|
});
|
|
} else {
|
|
return Promise.resolve(undefined); // Handle the case where obj is falsy
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
// ...
|
|
|
|
getEntityPreferences(entity_id: any) {
|
|
let params = { entity_id: entity_id };
|
|
|
|
if (this.networkProvider.getCurrentNetworkStatus() == ConnectionStatus.online) {
|
|
return this.http.get(this.apiUrl + 'getEntityPreferences/' + entity_id)
|
|
.pipe(
|
|
map((result: any) => {
|
|
this.ionicStorageProvider.checkAndInsertApiDataLocally(result, params, 'getEntityPreferences');
|
|
return result;
|
|
})
|
|
);
|
|
} else {
|
|
return this.ionicStorageProvider.getApiDataFromLocally(params, 'getEntityPreferences');
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|