Yeni Talep

Yeni Talep Script

const onClose = this.onClose;
this.onClose = (...args) => {
  if (parent) {
    parent.postMessage('window.close', '*');
  }
  return onClose.bind(this)(...args);
};

this.recordService._getResCountAndUpdateButtonText = async () => {
  let resCount = await angus.api
    .select({
      Object: 'QA_HOTEL_RESERVATION',
      Select: ['ID'],
      Where: [
        {
          Column: 'CHECKIN',
          Operator: '>=',
          Value: moment(angus.api.tenant.PROGDATE).format('YYYY-MM-DD'),
        },
        {
          Column: 'RESSTATEID',
          Operator: '=',
          Value: 2,
        },
        {
          Column: 'RESGUESTID',
          Operator: '=',
          Value: this.recordService.formGroup.controls.GUESTID.value,
        },
      ],
      Paging: {
        ItemsPerPage: 100,
        Current: 1,
      },
    })
    .then((response) => {
      
      return response.ResultSets[0].length;
    });
  this.recordService.elements.btnFutureRepeatHistory.api.innerComponent.text =
    '(' + (resCount || 0) + ')';
};

this.recordService._getUnfinishedCall = async (phoneNumber) => {
  return this.recordService.component.api
    .select({
      Object: 'QA_EXT_HOTEL_CONTACTREQUEST',
      Where: [
        {
          Column: 'PHONE',
          Operator: '=',
          Value: phoneNumber,
        },
        {
          Column: 'REQUESTCLOSED',
          Operator: '=',
          Value: 0,
        },
      ],
      Paging: {
        ItemsPerPage: 1,
        Current: 1,
      },
      Select: ['ID'],
    })
    .then(async (response) => {
      if (
        response &&
        response.ResultSets[0] &&
        response.ResultSets[0][0] &&
        response.ResultSets[0][0].ID
      ) {
        let id = response.ResultSets[0][0].ID;
        return id;
      }
    });
};

const guestControlOrCreate = async (phoneNumber) => {
  this.recordService.component.api
    .select({
      Object: 'HOTEL_GUEST',
      Where: [
        {
          Column: 'PHONE',
          Operator: '=',
          Value: phoneNumber,
        },
      ],
      Joins: [
        {
          Object: 'HOTEL_GUEST_VIP_TYPE',
          Key: 'ID',
          Fields: ['VIPTYPE'],
          Field: 'VIPTYPEID',
        },
      ],
      Paging: {
        ItemsPerPage: 1,
        Current: 1,
      },
      Select: [
        'ID',
        'NAME',
        'SURNAME',
        'EMAIL',
        'NATIONALIDNO',
        'BIRTHDATE',
        'REPEATCOUNT',
        'CALLCOUNT',
        'VIPTYPEID',
        'PREFERENCECOUNT',
        'TASKCOUNT',
        'COMMENTCOUNT',
      ],
    })
    .then((response) => {
      if (response.ResultSets[0].length > 0) {
        let guestId = response.ResultSets[0][0].ID;
        let guestName = response.ResultSets[0][0].NAME;
        let guestLastName = response.ResultSets[0][0].SURNAME;
        let guestEmail = response.ResultSets[0][0].EMAIL;
        let guestNationalIdNo = response.ResultSets[0][0].NATIONALIDNO;
        //let guestBirthDate = response.ResultSets[0][0].BIRTHDATE;
        let guestRepeat = response.ResultSets[0][0].REPEATCOUNT;
        let guestCall = response.ResultSets[0][0].CALLCOUNT;
        let guestPreference = response.ResultSets[0][0].PREFERENCECOUNT;
        let guestTask = response.ResultSets[0][0].TASKCOUNT;
        let guestComment = response.ResultSets[0][0].COMMENTCOUNT;
        let guestVipId = response.ResultSets[0][0].VIPTYPEID;
        let guestVipType = response.ResultSets[0][0].VIPTYPEID_VIPTYPE;
        this.recordService.formGroup.controls.GUESTID.setValue(guestId);
        if (guestNationalIdNo && !guestEmail) {
          this.recordService.formGroup.controls.GUESTID_NAME_ID_BIRTH.setValue(
            guestName + ' ' + guestLastName + ' ' + guestNationalIdNo
          );
        } else if (!guestNationalIdNo && guestEmail) {
          this.recordService.formGroup.controls.GUESTID_NAME_ID_BIRTH.setValue(
            guestName + ' ' + guestLastName + ' ' + guestEmail
          );
        } else if (!guestNationalIdNo && !guestEmail) {
          this.recordService.formGroup.controls.GUESTID_NAME_ID_BIRTH.setValue(
            guestName + ' ' + guestLastName
          );
        } else if (guestNationalIdNo && guestEmail) {
          this.recordService.formGroup.controls.GUESTID_NAME_ID_BIRTH.setValue(
            guestName +
              ' ' +
              guestLastName +
              ' ' +
              guestNationalIdNo +
              ' ' +
              guestEmail
          );
        }
        this.recordService.formGroup.controls.GUESTID_NAME.setValue(guestName);
        this.recordService.formGroup.controls.GUESTID_LNAME.setValue(
          guestLastName
        );
        this.recordService.formGroup.controls.EMAIL.setValue(guestEmail);
        this.recordService.formGroup.controls.GUESTID_REPEATCOUNT.setValue(
          guestRepeat
        );
        this.recordService.formGroup.controls.GUESTID_CALLCOUNT.setValue(
          guestCall
        );
        this.recordService.formGroup.controls.GUESTID_TASKCOUNT.setValue(
          guestTask
        );
        this.recordService.formGroup.controls.GUESTID_PREFERENCECOUNT.setValue(
          guestPreference
        );
        this.recordService.formGroup.controls.GUESTID_COMMENTCOUNT.setValue(
          guestComment
        );
        this.recordService.formGroup.controls.VIPTYPEID.setValue(guestVipId);
        this.recordService.formGroup.controls.VIPTYPEID_VIPTYPE.setValue(
          guestVipType
        );
      } else {
        this.recordService.formGroup.controls.GUESTID_NAME_ID_BIRTH.setValue(
          null
        );
        this.recordService.formGroup.controls.GUESTID_NAME.setValue(null);
        this.recordService.formGroup.controls.GUESTID_LNAME.setValue(null);
        this.recordService.formGroup.controls.EMAIL.setValue(null);
        this.recordService.formGroup.controls.GUESTID_REPEATCOUNT.setValue(
          null
        );
        this.recordService.elements.btnRepeatHistory.api.innerComponent.text =
          '(' + 0 + ')';
        this.recordService.formGroup.controls.GUESTID_CALLCOUNT.setValue(null);
        this.recordService.elements.btnPreferenceHistory.api.innerComponent.text =
          '(' + 0 + ')';
        this.recordService.elements.btnTaskHistory.api.innerComponent.text =
          '(' + 0 + ')';
        this.recordService.elements.btnCommentHistory.api.innerComponent.text =
          '(' + 0 + ')';
        this.recordService.formGroup.controls.VIPTYPEID.setValue(null);
        this.recordService.formGroup.controls.VIPTYPEID_VIPTYPE.setValue(null);

        (async () => {
          const newGuest = await openNewGuestRecord(phoneNumber);
          if (newGuest?.ID) {
            newGuest['ID_NAME_ID_BIRTH'] = await getNameIdBirth(newGuest.ID);
            setNewGuestToRecord(newGuest);
          }
        })();
      }
    });
};

const openNewGuestRecord = async (phoneNumber) => {
  return this.recordService.component.popupService
    .openRecord('guest-cards', null, { PHONE: phoneNumber }, undefined, 'full')
    .afterClosed()
    .toPromise();
};

const getNameIdBirth = async (guestId) => {
  return this.recordService.component.api
    .select({
      Object: 'QA_HOTEL_GUEST',
      Where: [
        {
          Column: 'ID',
          Operator: '=',
          Value: guestId,
        },
      ],
      Paging: {
        ItemsPerPage: 1,
        Current: 1,
      },
      Select: ['NAME_ID_BIRTH'],
    })
    .then((response) => {
      return response.ResultSets[0][0].NAME_ID_BIRTH;
    });
};

const setNewGuestToRecord = (guestData) => {
  const controls = this.recordService.formGroup.controls;
  controls.GUESTID_NAME.setValue(guestData.NAME);
  controls.GUESTID_LNAME.setValue(guestData.LNAME);
  controls.GUESTID_NAME_ID_BIRTH.setValue(guestData.ID_NAME_ID_BIRTH);
  controls.GUESTID.setValue(guestData.ID);
};



const awaitIf = async (fn, ms = 100) => {
  await new Promise(async (resolve, reject) => {
    let result = false;
    try {
      result = fn();
    } catch (e) {}

    if (result) {
      resolve(true);
    } else {
      await rxjs.timer(ms).toPromise();
      resolve(await awaitIf(fn));
    }
  });
};

this.recordService.formGroup.controls.GUESTID.valueChanges.subscribe(
  (guestid) => {
    if (guestid) {
      this.recordService._getResCountAndUpdateButtonText();
      if (
        document.querySelector('.ang-record-new-calllogs .ang-panel-P3').style
          .display !== 'none'
      ) {
        setTimeout(() => {
          let data = this.recordService.formGroup.getRawValue();
          if (data.GUESTID_NAME && data.GUESTID_LNAME) {
            let filtre = [
              { Column: 'NAME', Operator: '=', Value: data.GUESTID_NAME },
              { Column: 'SURNAME', Operator: '=', Value: data.GUESTID_LNAME },
            ];
            let dataGrid = this.recordService.grids['call-request-select'].grid;
            dataGrid.extraFilters = filtre;
            dataGrid.loadData();
          }
        }, 0);
      }
    }
  }
);

document.querySelector(
  `.ang-record-${this.recordID} .ang-panel-P3`
).style.display = 'none';
document.querySelector(
  `.ang-record-${this.recordID} .ang-panel-P3`
).parentElement.style.display = 'none';
document.querySelector(
  `.ang-record-${this.recordID} .dynamic-container`
).style.gridTemplateAreas = `"P1" "T1" "P2"`;
document.querySelector('#splitByQty .mat-icon').style.transform =
  'rotate(90deg)';

this.recordService.formGroup.controls.NOTES.valueChanges
  .pipe(rxjs.startWith(this.recordService.formGroup.controls.NOTES.value))
  .subscribe((notes) => {
    if (notes && notes.includes('[')) {
      let jsonData = JSON.parse(notes);
      let text =
        jsonData[0].Hotel +
        ' ' +
        jsonData[0].Checkin +
        ' / ' +
        jsonData[0].Checkout +
        ' Adult: ' +
        jsonData[0].Adult +
        ' ' +
        jsonData[0].TotalPrice;
      setTimeout(() => {
        this.recordService.formGroup.controls.GUESTID_FATHERSNAME.setValue(
          text
        );
      }, 1000);
      document.querySelector('div.group.ang-group-jsonNotes').style.display =
        'grid';
    } else {
      document.querySelector('div.group.ang-group-jsonNotes').style.display =
        'none';
    }
  });

const toDate = (v) =>
  moment(v).set({ hour: 0, minute: 0, second: 0, millisecond: 0 });
(async () => {
  await awaitIf(() => this.recordService.formGroup.controls['CHECKIN']);

  const checkIn = this.recordService.formGroup.controls['CHECKIN'];
  const checkOut = this.recordService.formGroup.controls['CHECKOUT'];

  checkIn.valueChanges.pipe(rxjs.debounceTime(200)).subscribe((x) => {
    if (moment(x).isAfter(checkOut.value) || !checkOut.value) {
      checkOut.setValue(toDate(moment(x).add(1,'days')));
    }
  });

  
  await awaitIf(() => this.recordService.formGroup.controls['PHONE']);
  let phoneNumber = new URL(location.href).searchParams.get('phone');
  if (phoneNumber !== null && phoneNumber !== '') {
    let direction = new URL(location.href).searchParams.get('direction');
    if (direction == 'outbound') {
      if (phoneNumber.match(/\+{1}?[1-9]0?[1-9]/g)) {
        phoneNumber = '+9' + phoneNumber.substring(2);
      }
      if (phoneNumber.match(/\+{1}?[1-9][0]{2}?[1-9]/g)) {
        phoneNumber = '+' + phoneNumber.substring(4);
      }
      this.recordService.formGroup.controls.CALLDIRECTION.setValue(1);
      this.recordService.formGroup.controls.CALLDIRECTION.markAsDirty();
    } else {
      this.recordService.formGroup.controls.CALLDIRECTION.setValue(0);
      this.recordService.formGroup.controls.CALLDIRECTION.markAsDirty();
    }
    this.recordService.formGroup.controls.PHONE.setValue(phoneNumber);

    let callId = await this.recordService._getUnfinishedCall(phoneNumber);
    if (callId) {
      await this.recordService.component.getData(callId);
      await this.recordService._getResCountAndUpdateButtonText();
    } else {
      guestControlOrCreate(phoneNumber);
      
      let url_stringg = location.href;
      let urll = new URL(url_stringg);
      let destination = urll.searchParams.get('destination');
      let createtypeid = urll.searchParams.get('createtypeid');
      if (createtypeid == 2) {
        this.recordService.formGroup.controls.CREATETYPEID.setValue(2);
        this.recordService.formGroup.controls.CONTACTTYPEID.setValue(0);
      }
      if (destination) {
        let resp = await angus.api.select({
          Object: 'QACCOUNTING_BRANCH',
          Select: ['NAME', 'SOURCE'],
          Where: [
            {
              Column: 'PHONENUMBER',
              Operator: '=',
              Value: destination,
            },
          ],
          Paging: {
            ItemsPerPage: 1,
            Current: 1,
          },
        });
        if (resp?.ResultSets?.[0]?.[0]) {
          this.recordService.formGroup.controls.HOTELNAME.setValue(
            resp.ResultSets[0][0].NAME
          );
          this.recordService.formGroup.controls.HOTELNAME.markAsDirty();
          if (resp.ResultSets[0][0].SOURCE) {
            this.recordService.formGroup.controls.REFGROUPID.setValue(
              parseInt(resp.ResultSets[0][0].SOURCE)
            );
            this.recordService.formGroup.controls.REFGROUPID.markAsDirty();
          }
        }
      }
      
      const resGuestControl = this.recordService.elements.selectGuest.api
        .innerComponent.innerComponent.formControl;
      const guestFc = this.recordService.formGroup.controls.GUESTID;
      guestFc.valueChanges
        .pipe(
          window.rxjs.takeUntil(this.isDestroy$),
          window.rxjs.filter((x) => typeof x === 'number'),
          window.rxjs.distinctUntilChanged(),
          window.rxjs.switchMap((x) => {
            return window.rxjs.from(
              angus.api.select({
                Object: 'QA_HOTEL_RESERVATION',
                Select: ['ID'],
                Where: [
                  {
                    Column: 'CHECKIN',
                    Operator: '>=',
                    Value: moment(angus.api.tenant.PROGDATE).format(
                      'YYYY-MM-DD'
                    ),
                  },
                  {
                    Column: 'RESSTATEID',
                    Operator: '=',
                    Value: 2,
                  },
                  {
                    Column: 'RESGUESTID',
                    Operator: '=',
                    Value: x,
                  },
                ],
                Paging: {
                  ItemsPerPage: 100,
                  Current: 1,
                },
              })
            );
          })
        )
        .subscribe((response) => {
          console.log('response mehmet', response);
          const count = response.ResultSets[0].length;
          this.recordService.elements.btnFutureRepeatHistory.api.innerComponent.text =
            '(' + (count || 0) + ')';
        });
    }
    

    const resultControl = this.recordService.elements.cancelResult.api
      .innerComponent.innerComponent.formControl;
    this.recordService.formGroup.controls.REQUESTTYPEID.valueChanges.subscribe(
      (x) => {
        if (x) {
          this.recordService.formGroup.controls.CONTACTRESULTID.setValue(null);
          resultControl.setValue('abc');
          resultControl.setValue('');
        }
      }
    );

    
  }
})();

Last updated