📁 Showcase
Automation

Drug order book automation

AutomateDrugOrderBook

ensure form PrivateOrderInfo 
ensure section: Details 
ensure field Link kind: hyperlink 
ensure field TotalAmount kind: number 
ensure field OldOrderStatus kind: pickText 
  sourceVar: VarOrderStatus
ensure field Handle kind: handle 
ensure field OrderId kind: text 
ensure field NewOrderStatus kind: pickText 
  sourceVar: VarOrderStatus

ensure automation AutomateDrugOrderBook kind: spreadsheet 
  spreadsheet: DrugOrderBook

1. BeforeInsert

// event, BeforeInsert
ensure event BeforeInsert fire: beforeInsert

a. CopyFieldRowId

ensure var VarMappingDrugOrderToDrugOrder2 kind: mapping 
  deploy: fixedOnDeploy
  fromForm: EntityDrugOrder 
  toForm: EntityDrugOrder 
  fieldMappingMap: {
      'updateVariant': 'updateForced',
      'emptyVariant': 'ignoreEmptyField',
      'map': {
        '${f:SysRowId}': 'Details.RowId'
      }
    }

// step, copyField, CopyFieldRowId
ensure automation AutomateDrugOrderBook
ensure event BeforeInsert
ensure step CopyFieldRowId kind: copyField 
  mappingVar: VarMappingDrugOrderToDrugOrder2

2. AfterInsert

// event, AfterInsert
ensure event AfterInsert fire: afterInsert

// pipelineVar, PipelineOrderLink
ensure pipelineVar PipelineOrderLink form: PrivateOrderInfo

a. CopyFieldCustomer

ensure var VarMappingDrugOrderToDrugOrder kind: mapping 
  deploy: fixedOnDeploy
  fromForm: EntityDrugOrder 
  toForm: EntityDrugOrder 
  fieldMappingMap: {
      'updateVariant': 'updateForced',
      'emptyVariant': 'ignoreEmptyField',
      'map': {
        '${f:SysCreatedBy}': 'Details.Customer'
      }
    }

// step, copyField, CopyFieldCustomer
ensure automation AutomateDrugOrderBook
ensure event AfterInsert
ensure step CopyFieldCustomer kind: copyField 
  mappingVar: VarMappingDrugOrderToDrugOrder

b. CopyFields

ensure var VarMappingDrugOrderToPrivateForm kind: mapping 
  deploy: fixedOnDeploy
  fromForm: EntityDrugOrder 
  toForm: PrivateOrderInfo 
  fieldMappingMap: {
      'map': {
        '${f:Summary.TotalBill}': 'Details.TotalAmount',
        '${f:Details.Handle}': 'Details.Handle',
        '${f:Details.OrderNo}': 'Details.OrderId',
        '${f:Summary.OrderStatus}': 'Details.OldOrderStatus'
      }
    }
// step, copyField, CopyFields
ensure automation AutomateDrugOrderBook
ensure event AfterInsert
ensure step CopyFields kind: copyField 
  targetPipelineVar: PipelineOrderLink 
  mappingVar: VarMappingDrugOrderToPrivateForm

c. DeeplinkDrugOrder

// step, generateDeeplink, GenerateDeeplinkDrugOrder
ensure step GenerateDeeplinkDrugOrder kind: generateDeeplink 
  outputFormPipelineVar: PipelineOrderLink 
  deeplinkField: Details.Link
  deeplink: DeeplinkDrugOrder

d. MsgSendOnWhatsapp

ensure var VarTextPatternMsg1 kind: text 
  deploy: fixedOnDeploy 
  value: "Thank you for your repeat order!"

ensure var VarTextPatternMsg2 kind: text 
  deploy: fixedOnDeploy 
  value: "New order id: ${P1}, amount: ${P2}, status: ${P3}"

ensure var VarTextPatternMsg3 kind: text
  deploy: fixedOnDeploy
  value: "Access your order ${P}"

// step, messageSendOnWhatsapp, MessageSendOnWhatsapp
ensure automation AutomateDrugOrderBook
ensure event AfterInsert
ensure step SendWhatsappTemplateMessage kind: sendWhatsappTemplateMessage 
  inputFormPipelineVar: PipelineOrderLink 
  dataSourceField: Details.Handle 
  templateGroupId: "36ab8ba797dc5cba4ec7e0b040ee8591"
  messageVarMap: [
    "{'var':'VarTextPatternMsg1'}", 
    "{'var':'VarTextPatternMsg2', 
      'paramSet':[
        '${f:Details.OrderId}',
        '${f:Details.TotalAmount}',
        '${f:Details.OldOrderStatus}'
        ]}", 
    "{'var':'VarTextPatternMsg3','paramSet':['${f:Details.Link}']}"
  ]

e. MessageSendOnEmail

ensure var VarParagraphPatternBody kind: paragraph 
  deploy: fixedOnDeploy 
  value: "<b>Thank you for your repeat order.</b>

New Order Id : ${P}
Order amount: ${P1}
Order status: ${P2}

Access your order ${P3}"

// step, messageSendOnEmail, MessageSendOnEmail
ensure automation AutomateDrugOrderBook
ensure event AfterInsert
ensure step SendEmail kind: sendEmail
  inputFormPipelineVar: PipelineOrderLink 
  dataSourceField: Details.Handle
    messageVar: {
      'var': 'VarParagraphPatternBody',
      'paramSet': [
        '${f:Details.OrderId}',
        '${f:Details.TotalAmount}',
        '${f:Details.OldOrderStatus}',
        '${f:Details.Link}'
      ]
    } 
  subjectVar: {
      'var': 'VarTextPatternMsg1'
    }

f. ForwardToGroups

// step, forwardToGroups, ForwardToGroups
ensure step ForwardToGroups kind: forwardToGroups 
  senderRole: $Self
  groups: [DrugOrderBook]

3. BeforeUpdate

// event, BeforeUpdate
ensure event BeforeUpdate fire: beforeUpdate

// pipelineVar, PipelineOrderStatus
ensure pipelineVar PipelineOrderStatus form: PrivateOrderInfo

a. GetSpreadsheetRows

ensure var VarMappingDrugOrderToPrivateForm1 kind: mapping 
  deploy: fixedOnDeploy
  fromForm: EntityDrugOrder 
  toForm: PrivateOrderInfo 
  fieldMappingMap: {
      'map': {
        '${f:Summary.OrderStatus}': 'Details.OldOrderStatus',
        '${f:Details.Handle}': 'Details.Handle'
      }
    }

ensure var VarConditionGetLastRow kind: condition deploy: fixedOnDeploy
  value: "<root>
      <stmt>${f:Details.RowId} == ${in:Details.RowId}</stmt>
    </root>" 
  sourceForm: EntityDrugOrder 
  inputForm: EntityDrugOrder

// step, getSpreadsheetRows, GetSpreadsheetRows
ensure automation AutomateDrugOrderBook
ensure event BeforeUpdate
ensure step GetSpreadsheetRows kind: getSpreadsheetRows 
  targetSpreadsheet: DrugOrderBook 
  targetSpreadsheetFilterVar: VarConditionGetLastRow 
  outputFormPipelineVar: PipelineOrderStatus 
  targetToSourceMappingVar: VarMappingDrugOrderToPrivateForm1

b. CopyFieldStatus

ensure var VarMappingDrugOrderToPipeline kind: mapping 
  deploy: fixedOnDeploy
  fromForm: EntityDrugOrder 
  toForm: PrivateOrderInfo 
  fieldMappingMap: {
      'map': {
        '${f:Summary.OrderStatus}': 'Details.NewOrderStatus',
        '${f:Details.OrderNo}': 'Details.OrderId'
      }
    }

// step, copyField, CopyFieldStatus
ensure automation AutomateDrugOrderBook
ensure event BeforeUpdate
ensure step CopyFieldStatus kind: copyField 
  targetPipelineVar: PipelineOrderStatus 
  mappingVar: VarMappingDrugOrderToPipeline

4. AfterUpdate

// event, AfterUpdate
ensure event AfterUpdate fire: afterUpdate

a. DeeplinkDrugOrder

// step, generateDeeplink, GenerateDeeplinkDrugOrder
ensure step GenerateDeeplinkDrugOrder kind: generateDeeplink 
  outputFormPipelineVar: PipelineOrderStatus
  deeplinkField: Link
  deeplink: DeeplinkDrugOrder

b. MessageSendOnEmail

ensure var VarConditionIsOrderStatusChange kind: condition 
  deploy: fixedOnDeploy
  value: "<root>
      <stmt>{f:Summary.OrderStatus} != ${in:Details.OldOrderStatus}</stmt>
    </root>" 
  sourceForm: EntityDrugOrder 
  inputForm: PrivateOrderInfo

ensure var VarParagraphPatternBody1 kind: paragraph 
  deploy: fixedOnDeploy 
  value: "<b>We have update for you for</b> 
Order id ${P}
Status changed to ${P1}.

Access your order ${P2}"

ensure var VarTetPatternSubject kind: text 
  deploy: fixedOnDeploy 
  value: "Your order"

// step, messageSendOnEmail, MessageSendOnEmail
ensure automation AutomateDrugOrderBook
ensure event AfterUpdate
ensure step SendEmail kind: sendEmail 
  executionConditionVar: VarConditionIsOrderStatusChange 
  executionConditionInputPipelineVar: PipelineOrderStatus 
  inputFormPipelineVar: PipelineOrderStatus 
  dataSourceField: Details.Handle
  subjectVar: {
      'var': 'VarTetPatternSubject'
    }
  messageVar: {
    'var': 'VarParagraphPatternBody1',
    'paramSet': [
      '${f:Details.OrderId}',
      '${f:Details.TotalAmount}',
      '${f:Details.Link}'
    ]
  }

c. MsgSendOnWhatsapp

ensure var VarTextPatternUpdateMsg kind: text 
  deploy: fixedOnDeploy 
  value: "For order id ${P}, status changed to ${P1}"
  
// step, messageSendOnWhatsapp, MessageSendOnWhatsapp
ensure automation AutomateDrugOrderBook
ensure event AfterUpdate
ensure step SendWhatsappUpdateMessage kind: sendWhatsappTemplateMessage 
  templateGroupId: "eea7e4bfb365b7e6c77d3839a91ce279"
  executionConditionVar: VarConditionIsOrderStatusChange 
  executionConditionInputPipelineVar: PipelineOrderStatus 
  inputFormPipelineVar: PipelineOrderStatus 
  dataSourceField: Details.Handle 
  messageVarMap: [ 
    "{'var':'VarTextPatternUpdateMsg',
      'paramSet':[
        '${f:Details.OrderId}',
        '${f:Details.NewOrderStatus}'
        ]}", 
    "{'var':'VarTextPatternMsg3','paramSet':['${f:Details.Link}']}"
  ]

AutomateDrugOrderBook

ensure spreadsheet: RepeatDrugOrderSheet
  withFields: ["Order", "Customer"]
  ofTypes: [text, pickUser]
  readRoleSet: [Owner, Customer]
  insertRoleSet: [Owner, Customer]
  removeRoleSet: [Owner]
  updateRoleSet: [Owner]
  modules: ["Pharmacy"]

ensure form EntityRepeatDrugOrder
ensure section: Details
ensure field Customer 
  roleDataSource: [Customer]
  
ensure automation AutomateRepeatDrugOrderSheet kind: spreadsheet 
  spreadsheet: RepeatDrugOrderSheet

1. BeforeInsert

// event, BeforeInsert
ensure event BeforeInsert fire: beforeInsert

a. CopyFieldCustomer

ensure var VarMappingRepeatOrderToRepeatOrder kind: mapping 
  deploy: fixedOnDeploy
  fromForm: EntityRepeatDrugOrder 
  toForm: EntityRepeatDrugOrder 
  fieldMappingMap: {
      'updateVariant': 'updateForced',
      'emptyVariant': 'ignoreEmptyField',
      'map': {
        '${f:SysCreatedBy}': 'Details.Customer'
      }
    }

// step, copyField, CopyFieldCustomer
ensure automation AutomateRepeatDrugOrderSheet
ensure event BeforeInsert
ensure step CopyFieldCustomer kind: copyField
   mappingVar: VarMappingRepeatOrderToRepeatOrder

2. AfterInsert

// event, AfterInsert
ensure event AfterInsert fire: afterInsert

// pipelineVar, PipelineRepeatOrder
ensure pipelineVar PipelineRepeatOrder form: EntityDrugOrder

a. CallReport

i. ReportLastOrder
ensure report ReportLastOrderCustomer kind: query 
  inputForm: EntityRepeatDrugOrder 
  outputForm: EntityDrugOrder 
  fromSpreadsheets: [DrugOrderBook] 
  neoQL: "SELECT
    ${ss:DrugOrderBook.Details.CustomerName} as ${out:Details.CustomerName},
    ${ss:DrugOrderBook.Details.Address} as ${out:Details.Address},
    ${ss:DrugOrderBook.Details.Handle} as ${out:Details.Handle},
    ${ss:DrugOrderBook.Details.Customer} as ${out:Details.Customer},
    ${ss:DrugOrderBook.Summary.Orderlink} as ${out:Summary.Orderlink}
  FROM ${ss}
  WHERE ${ctx:row.type} = ${ss:DrugOrderBook}
      AND ${ss:DrugOrderBook.Details.Customer.value} = ${in:Customer}
  ORDER BY ${ctx:row.createdOn} DESC
  LIMIT 1"

ensure report ReportLastOrderGrid kind: query 
  inputForm: EntityRepeatDrugOrder 
  outputForm: EntityDrugOrder 
  fromSpreadsheets: [DrugOrderBook] 
  neoQL: "With q1 as
  (
      SELECT
          ${ctx:row.id} as `id`
      FROM ${ss}
      WHERE ${ctx:row.type} = ${ss:DrugOrderBook}
        AND ${ss:DrugOrderBook.Details.Customer.value} = ${in:Customer}
      ORDER BY ${ctx:row.createdOn} DESC
      LIMIT 1
  ),

  q2 as
  (
      SELECT 
          ${ctx:row.parentId} as `pid`,
          ${ss:DrugOrderBook.Items.Quantity} as `qty`,
          ${ss:DrugOrderBook.Items.ItemMasterRowId} as `itemRowId`,
          ${ss:DrugOrderBook.Items.Name} as `drugName`,
          ${ss:DrugOrderBook.Items.Ingredient} as `ingredient`,
          ${ss:DrugOrderBook.Items.Brand} as `brand`,
          ${ss:DrugOrderBook.Items.Dose} as `dose`,
          ${ss:DrugOrderBook.Items.Pack} as `pack`,
          ${ss:DrugOrderBook.Items.DiscountedPrice} as `dsicountedPrice`,
          ${ss:DrugOrderBook.Items.MRP} as `mrp`
      FROM ${ss}
      WHERE ${ctx:row.type} = ${ss:DrugOrderBook.Items}
  )

  SELECT 
    q2.qty as ${out:Items.Quantity},
    q2.itemRowId as ${out:Items.ItemMasterRowId},
    q2.drugName as ${out:Items.Name}, 
    q2.ingredient as ${out:Items.Ingredient}, 
    q2.brand as ${out:Items.Brand}, 
    q2.dose as ${out:Items.Dose}, 
    q2.pack as ${out:Items.Pack}, 
    q2.dsicountedPrice as ${out:Items.DiscountedPrice}, 
    q2.mrp as ${out:Items.MRP} 
  FROM q1
  JOIN q2 ON q1.id = q2.pid"

ensure report ReportRepeatLastOrder kind: composite 
  inputForm: EntityRepeatDrugOrder 
  outputForm: EntityDrugOrder
  mergeReports: [ReportLastOrderCustomer, ReportLastOrderGrid]
ii. Step
ensure var VarMappingDrugOrderToDrugOrder1 kind: mapping 
  deploy: fixedOnDeploy
  fromForm: EntityDrugOrder 
  toForm: EntityDrugOrder 
  fieldMappingMap: {
      'map': {
        '${f:Details.Date}': 'Details.Date',
        '${f:Details.Customer}': 'Details.Customer',
        '${f:Details.CustomerName}': 'Details.CustomerName',
        '${f:Details.Address}': 'Details.Address',
        '${f:Summary.OrderStatus}': 'Summary.OrderStatus',
        '${f:Summary.TotalBill}': 'Summary.TotalBill',
        '${f:Details.Handle}': 'Details.Handle'
      }
    } 
  gridMappingMap: {
      'Mapping2Dhsl4': {
        'fromGrid': 'Items',
        'toGrid': 'Items',
        'fromKey': '$RowId',
        'toKey': '$RowId',
        'insertVariant': 'insertForced',
        'updateVariant': 'updateForced',
        'removeVariant': 'removeForced',
        'emptyVariant': 'overrideEmptyField',
        'map': {
          '${f:Items.Quantity}': 'Items.Quantity',
          '${f:Items.ItemMasterRowId}': 'Items.ItemMasterRowId',
          '${f:Items.Name}': 'Items.Name',
          '${f:Items.Ingredient}': 'Items.Ingredient',
          '${f:Items.Brand}': 'Items.Brand',
          '${f:Items.Dose}': 'Items.Dose',
          '${f:Items.Pack}': 'Items.Pack',
          '${f:Items.DiscountedPrice}': 'Items.DiscountedPrice',
          '${f:Items.MRP}': 'Items.MRP',
          '${f:Items.Total}': 'Items.Total'
        }
      }
    }

// step, callReport, CallReportRepeatLastOrder
ensure automation AutomateRepeatDrugOrderSheet
ensure event AfterInsert
ensure step CallReportRepeatLastOrder kind: callReport 
  targetReport: ReportRepeatLastOrder 
  outputPipelineVar: PipelineRepeatOrder 
  reportOutputMappingVar: VarMappingDrugOrderToDrugOrder1

b. InsertIntoOrderSheet

// step, insertIntoSpreadsheet, InsertIntoDrugOrderSheet
ensure step InsertIntoDrugOrderSheet kind: insertIntoSpreadsheet 
  targetSpreadsheet: DrugOrderBook 
  inputFormPipelineVar: PipelineRepeatOrder 
  sourceToTargetMappingVar: VarMappingDrugOrderToDrugOrder1