/** 
 | 
 * @file         ADSO-10110 
 | 
 * @description  Edit single unit cost - change account selection and update cost 
 | 
 * @testcategory Web - Financials - Units 
 | 
 * @author       Gay Er Xuan (erxuan.gay@3ds.com) 
 | 
 * @copyright    Dassault Systemes 
 | 
 */ 
 | 
import { qCustomMatcher } from '../../../e2elib/lib/src/main/qmatchers.const'; 
 | 
import { ListRow } from '../../../e2elib/lib/src/pageobjects/list/listrow.component'; 
 | 
import { ColumnIDValueMap } from '../../../libappbase/listbase'; 
 | 
import { AppMP, Demo, Scenario } from '../../../libmp/appmp'; 
 | 
import { DataFoodBaseUnitCostCost, DataFoodBaseUnitCostLengOfTime, DataFoodBaseUnitCostTimeUnit } from '../../../libmp/data/data.unitcost'; 
 | 
import { DataFoodBaseAccountName, DataFoodBaseAccountCostDriver, DataFoodBaseAccountUoM } from '../../../libmp/data/data.account'; 
 | 
import { DataFoodBaseEntityName } from '../../../libmp/data/data.navigation'; 
 | 
import { ListUnitCostColumn } from '../../../libmp/forms/form.unitcost'; 
 | 
import { startOfPlanningYear } from '../../../libmp/data/data.period'; 
 | 
  
 | 
describe('ADSO-10110 - Edit single unit cost, change account selection and update cost', () => { 
 | 
  const appMP = AppMP.getInstance(); 
 | 
  const listUnitCost = appMP.viewUnitCost.frmUnitCost.listUnitCost; 
 | 
  let dlgUnitCost = listUnitCost.dlgUnitCost; 
 | 
  let sopYear: string; 
 | 
  let eopString: string; 
 | 
  let newStartString = '1-Feb-'; // Year to be added after getting sopYear 
 | 
  const janSOPMinus1 = `1-Jan-${startOfPlanningYear - 1}`; 
 | 
  
 | 
  beforeAll(async () => { 
 | 
    jasmine.addMatchers(qCustomMatcher); 
 | 
    await appMP.login(); 
 | 
  }); 
 | 
  
 | 
  afterAll(async () => { 
 | 
    await appMP.cleanupAndLogout(); 
 | 
  }); 
 | 
  
 | 
  afterEach(async () => { 
 | 
    await appMP.checkToastMessage(); 
 | 
  }); 
 | 
  
 | 
  it(`Setup - ${AppMP.getDemoDataPath(Demo.Food, Scenario.Base)}`, async () => { 
 | 
    await appMP.createDemoDataset(Demo.Food, Scenario.Base, false); 
 | 
  }); 
 | 
  
 | 
  it(`Setup - Open view ${appMP.viewPeriod.viewPath} to get the planning periods via Periods form.`, async () => { 
 | 
    // Open Periods view 
 | 
    await appMP.viewPeriod.switchTo(); 
 | 
    const formPeriod = appMP.viewPeriod.frmPeriod; 
 | 
    ({ sopYear, eopString } = await formPeriod.getStartAndEndOfPlanning()); 
 | 
    newStartString += sopYear; 
 | 
  }); 
 | 
  
 | 
  it(`Setup - Open view ${appMP.viewAccount.viewPath}`, async () => { 
 | 
    await appMP.viewAccount.switchTo(); 
 | 
    expect(await appMP.viewAccount.frmAccount.isVisible()).toBe(true, '"Account" form should be visible'); 
 | 
  }); 
 | 
  
 | 
  it(`Setup - Verify "${DataFoodBaseAccountName.GeneralFixedCosts}" account exists with cost driver "${DataFoodBaseAccountCostDriver.Fixed}" in "Accounts" list`, async () => { 
 | 
    // Verify General fixed costs exists with cost driver Fixed 
 | 
    const listAccount = appMP.viewAccount.frmAccount.listAccount; 
 | 
    const accRow = await listAccount.getRow({ Name: DataFoodBaseAccountName.GeneralFixedCosts }, [{ Name: DataFoodBaseAccountName.CostOfSales }]); 
 | 
    await listAccount.verifyRowValues(accRow, { 'Default cost driver': DataFoodBaseAccountCostDriver.Fixed }); 
 | 
  }); 
 | 
  
 | 
  it(`Setup - Open view ${appMP.viewUnitCost.viewPath}`, async () => { 
 | 
    await appMP.viewUnitCost.switchTo(); 
 | 
  }); 
 | 
  
 | 
  it(`Setup - Verify "${DataFoodBaseAccountName.GeneralFixedCosts}" account exists with UoM as "${DataFoodBaseAccountUoM.EuroPerMonth}" in "Unit Accounts" list.`, async () => { 
 | 
    const listUnitAccount = appMP.viewUnitCost.frmUnitAccount.listAccount; 
 | 
    const account = await listUnitAccount.getRow({ Account: DataFoodBaseAccountName.GeneralFixedCosts }); 
 | 
    await listUnitAccount.verifyRowValues(account, { 'Default UoM': DataFoodBaseAccountUoM.EuroPerMonth }); 
 | 
  }); 
 | 
  
 | 
  it(`Setup - In navigation panel, filter unit cost by "${DataFoodBaseEntityName.DEFermenter}" and verify exactly 1 cost with "start" value ${janSOPMinus1} exists in "Unit costs" list`, async () => { 
 | 
    // Filter by DE Fermenter 
 | 
    await appMP.formNavigation.filterByEntity(DataFoodBaseEntityName.DEFermenter, [DataFoodBaseEntityName.Europe, DataFoodBaseEntityName.Plants, DataFoodBaseEntityName.DenmarkPlant]); 
 | 
    // Verify 1 DE Fermenter Unit Cost exist in the list 
 | 
    await listUnitCost.waitForScreenUpdate(); 
 | 
  
 | 
    expect(await listUnitCost.getRowCount()).toBe(1, `Exactly one ${DataFoodBaseEntityName.DEFermenter} Unit Cost should exist in the list`); 
 | 
    const unitCost = await listUnitCost.getRowByIndex(0); 
 | 
    expect(await listUnitCost.getCellValueFromRow(ListUnitCostColumn.Start, unitCost)).toBe(janSOPMinus1, `Start of the ${DataFoodBaseEntityName.DEFermenter} unit cost should be ${janSOPMinus1}`); 
 | 
  }); 
 | 
  
 | 
  it(`Step 1 - Select "${DataFoodBaseAccountName.GeneralFixedCosts}" in "Unit Cost" list and edit it via action bar button to verify the default values`, async () => { 
 | 
    const unitCost = await listUnitCost.getRowByIndex(0); 
 | 
    dlgUnitCost = await listUnitCost.editRowByActionBarPage(unitCost); 
 | 
  
 | 
    // Verify default value in dialog 
 | 
    const feedback = await dlgUnitCost.verifyDialogValue(DataFoodBaseAccountName.GeneralFixedCosts, DataFoodBaseAccountCostDriver.Fixed, DataFoodBaseEntityName.DEFermenter, new Date(janSOPMinus1), DataFoodBaseUnitCostTimeUnit.Month, DataFoodBaseUnitCostLengOfTime._1, DataFoodBaseUnitCostCost._1000); 
 | 
    expect(feedback.length).toBe(0, feedback.join('. \n')); 
 | 
  }); 
 | 
  
 | 
  it(`Step 2 - In the dialog, change account to "${DataFoodBaseAccountName.ChangeoverCost}" and verify UI hidden for \ 
 | 
  "Time unit" and "Length of time".`, async () => { 
 | 
    // Change account to Changeover cost and verify time unit and length of time field hidden 
 | 
    await dlgUnitCost.updateUnitCostValue(DataFoodBaseAccountName.ChangeoverCost); 
 | 
    expect(await dlgUnitCost.ddlTimeUnit.isVisible()).toBe(false, 'Time unit drop down list should be hidden'); 
 | 
    expect(await dlgUnitCost.efLengthOfTime.isVisible()).toBe(false, 'Length of time field should be hidden'); 
 | 
    // Update other fields and confirm the dialog 
 | 
  }); 
 | 
  
 | 
  it(`Step 3 - Change start value to "${newStartString}(SOP year)" and cost to "${DataFoodBaseUnitCostCost._2000}". Then click ok button`, async () => { 
 | 
    await dlgUnitCost.updateUnitCostValue(undefined, undefined, undefined, new Date(newStartString), undefined, undefined, DataFoodBaseUnitCostCost._2000); 
 | 
    const isOkBtnClickable = await dlgUnitCost.clickOK(); 
 | 
    expect(isOkBtnClickable).toBe(true, 'Ok button should be clickable'); 
 | 
  }); 
 | 
  
 | 
  it('Step 4 - Verify the unit cost row is updated with correct values', async () => { 
 | 
    let unitCostRow: ListRow | undefined; 
 | 
    const columnValueMap: ColumnIDValueMap[] = [ 
 | 
      { columnID: ListUnitCostColumn.Account, value: DataFoodBaseAccountName.ChangeoverCost }, 
 | 
      { columnID: ListUnitCostColumn.CostDriver, value: DataFoodBaseAccountCostDriver.Changeover }, 
 | 
      { columnID: ListUnitCostColumn.Cost, value: `${DataFoodBaseUnitCostCost._2000.toLocaleString('en')}` }, 
 | 
      { columnID: ListUnitCostColumn.Start, value: newStartString }, 
 | 
      { columnID: ListUnitCostColumn.End, value: eopString }, 
 | 
    ]; 
 | 
  
 | 
    try { 
 | 
      unitCostRow = await listUnitCost.getRowByValue(columnValueMap); 
 | 
    } catch { 
 | 
      const valuesToUpdate = columnValueMap.map((el: ColumnIDValueMap) => el.value); 
 | 
      expect(unitCostRow).toBeDefined(`Unit cost row with values: "${valuesToUpdate.join(', ')}" should exists in "Unit Cost" list`); 
 | 
    } 
 | 
  }); 
 | 
}); 
 |