package com.example.slflo import java.math.BigDecimal /** * What the connector needs from your ERP. Implement it with your ERP's own services; the * in-memory [InMemoryErp] lets the connector run end to end against the sandbox. */ interface ErpBooks { data class SalesLine(val lineNumber: Int, val item: String, val quantity: BigDecimal, val amount: BigDecimal) data class Customer(val account: String, val name: String, val priceList: String, val creditLimit: BigDecimal) data class Item(val number: String, val name: String, val unit: String, val price: BigDecimal) /** The sales order whose customer reference holds this Slflo order id (D365: `CustomersOrderReference`). */ fun findSalesOrder(customerReference: String): String? /** Creates one sales order and returns its number. */ fun createSalesOrder(customerReference: String, account: String, lines: List): String /** Accounts the credit department put on hold: their orders are refused. */ fun onHold(account: String): Boolean /** One journal per Slflo document version; posting the same version again returns the first. */ fun postJournal(kind: String, sourceId: String, sourceVersion: Long, amount: String): String fun customers(): List fun items(): List } class InMemoryErp( private val customers: List = emptyList(), private val items: List = emptyList(), private val hold: Set = emptySet(), ) : ErpBooks { private val orders = linkedMapOf() private val journals = linkedMapOf, String>() override fun findSalesOrder(customerReference: String) = orders[customerReference] override fun createSalesOrder(customerReference: String, account: String, lines: List): String = "SO-%07d".format(orders.size + 1).also { orders[customerReference] = it } override fun onHold(account: String) = account in hold override fun postJournal(kind: String, sourceId: String, sourceVersion: Long, amount: String): String = journals.getOrPut(Triple(kind, sourceId, sourceVersion)) { "JRN-%06d".format(journals.size + 1) } override fun customers() = customers override fun items() = items }