Fatura BeforeSave Script

Fatura

return new Promise(async (resolve, reject) => {
  if (
    this.recordService.lastData &&
    this.recordService.lastData.ACCOUNTINGRECEIPTID
  ) {
    
    return reject(
      new Error(
        angus.languageService.staticTranslator.translate(
          'This invoice is integrated, you cannot make changes!'
        )
      )
    );
  }
 
  const awaitIf = async (fn, ms = 100) => {
    await new Promise(async (resolve, reject) => {
      let result = false;
      try {
        result = fn();
      } catch (e) {
        console.error(e);
      }

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

  let faturaRecGrid = this.allGrids['stock-invoice-line'];
  await awaitIf(
    () =>
      faturaRecGrid.grid &&
      faturaRecGrid.grid.agGrid &&
      faturaRecGrid.grid.agGridReady,
    0
  );

  if (
    this.recordService.lastData &&
    this.recordService.lastData.SENDINGSTATUS == 3 &&
    [1, 3, 5, 22, 24, 26].indexOf(this.recordService.lastData.TYPEID) !== -1
  ) 

  if (this.allGrids['stock-invoice-line'].api.getDisplayedRowCount() === 0)
    return reject(
      new Error(
        angus.languageService.staticTranslator.translate(
          'You cannot save the invoice without a line!'
        )
      )
    );

  const deleteAllRows = [];
  this.allGrids['stock-invoice-line'].grid.agGrid.api.forEachNode((x) => {
    if (x.toBeDeleted == true) deleteAllRows.push(x);
  });

  if (
    this.allGrids['stock-invoice-line'].api.getDisplayedRowCount() ==
    deleteAllRows.length
  ) {
    let msg = angus.languageService.dynamicTranslator.translate(
      'There are no valid lines'
    );
    return reject(new Error(msg));
  }
  const invoiceNoControl = await this.api
    .execSP({
      Object: 'SP_INVOICE_NO_CONTROL',
      Parameters: {
        INVOICENO: this.allFormControls['InvoiceId'].value,
        ADDRESSID: this.allFormControls['ADDRESSID'].value,
        INVOICEID: this.allFormControls['ID'].value,
        INVDATE: this.allFormControls['IssueDate'].value,
        TYPEID: this.recordService.formGroup.controls.TYPEID.value,
      },
    })
    .toPromise();
  let invResult = invoiceNoControl[0][0];

  if (!invResult.SUCCESS) {
    return reject(new Error(invResult.MESSAGE));
  }

  /*if(this.isUsedInvoice){
    return reject( new Error(this.languageService.staticTranslator.translate("This invoice number has been used before")));
  }*/

  let gridCount = 0;
  this.recordService.grids['stock-invoice-line'].grid.agGrid.api.forEachNode(
    (node) => {
      if (node.toBeDeleted == false) {
        gridCount++;
      }
    }
  );

  this.recordService.component.allFormControls.LineCountNumeric.setValue(
    gridCount
  );

  let uidControl = this.recordService.formGroup.controls.UUID.value;

  if (uidControl === null) {
    let newUID = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
      .replace(/[xy]/g, (c) => {
        var r = (Math.random() * 16) | 0,
          v = c == 'x' ? r : (r & 0x3) | 0x8;
        return v.toString(16);
      })
      .toUpperCase();
    this.recordService.formGroup.controls.UUID.setValue(newUID);
  }

  
  /*
  if (this.opener.route.snapshot.params.mode == 'mustahsil') {
    let errMsg = '';
    this.recordService.grids['stock-invoice-line'].grid.agGrid.api.forEachNode(
      (node) => {
        if (
          node.data.DISCAMOUNT ||
          node.data.DISCPERCENT1 ||
          node.data.DISCPERCENT2 ||
          node.data.TaxPercent
        ) {
          console.log(node, 'node');
          errMsg += node.data.STOCKID_NAME + ', ';
        }
      }
    );
    if (errMsg) {
      return reject(
        new Error(
          angus.languageService.staticTranslator.translate(
            errMsg + 'Stokları için KDV ve İndirim bilgileri sıfırlanmalıdır.'
          )
        )
      );
    }
  }
*/
  resolve();
});

Last updated