Ms Dynamics AX - 如何让 table 只借出一次记录?

Ms Dynamics AX - How to let the table lend record only once?

我是编程新手。我正在使用 MS Dynamics AX,但遇到了问题。

我正在做一个简单的项目:汽车租赁:

三个简单的表格:

我想知道如何限制只能将一辆车分配给一个客户。

现在我可以进行交易,例如:

先生琼斯 -> 奥迪 A3(ABC 1234A)
布莱恩先生 -> 奥迪 A3(ABC 1234A)

在同一时间,而且很糟糕。他们不应该同时租用同一辆车。

帮帮我。

与大多数时候一样,您可以通过多种不同的方式实现您想要的目标。

我的建议是将 transactions table 设为 valid time state table(假设您使用的是 2012 或更高版本)。但是对于刚接触编程的人来说,一开始这可能是一个困难的概念。

一个直接的解决方案是覆盖 transactions table 的 validateWrite 方法,并检查是否已经存在任何在所需时间段内租车的记录。这看起来类似于以下代码(假设 rentDate 是开始,backRentDate 是租期的结束):

public boolean validateWrite()
{
    Transactions alreadyExistingTransaction;
    boolean      ret;
    ;

    ret = super();
    select firstOnly alreadyExistingTransaction 
        where alreadyExistingTransaction.refToCars == this.refToCars
           && alreadyExistingTransaction.rentDate <= this.rentDate
           && (alreadyExistingTransaction.backRentDate >= this.rentDate
            || alreadyExistingTransaction.backRentDate == dateNull())
           && alreadyExistingTransaction.RecId != this.RecId;
    if (alreadyExistingTransaction.RecId != 0)
    {
        ret = checkFailed(strFmt("Car %1 is already rented to %2 from %3 till %4.",
                                 alreadyExistingTransaction.refToCars,
                                 alreadyExistingTransaction.refToCustomers,
                                 alreadyExistingTransaction.rentDate,
                                 alreadyExistingTransaction.backRentDate));
    }

    return ret;
}