def order_detail_flush_delete(a_row, a_session: session):
    order = a_row.OrderHeader
    old_order = ObjectView(
        row2dict(order))  # hmm... key ShippedDate vs. "ShippedDate"
    order.AmountTotal -= a_row.Amount
    order_update(order, old_order, a_session)
    row_prt(order,
            "order_detail_flush_delete adjusted to: " + str(order.AmountTotal))
def order_detail_flush_dirty(a_row: models.OrderDetail, a_session: session):
    old_row = get_old_row(a_row)  # type: models.OrderDetail
    if a_row.OrderId == old_row.OrderId:
        if a_row.ProductId != old_row.ProductId:
            product = a_session.query(models.Product). \
                filter(models.Product.Id == a_row.ProductId).one()
            a_row.UnitPrice = product.UnitPrice
        a_row.Amount = a_row.UnitPrice * a_row.Quantity
        if a_row.Amount != old_row.Amount:
            order = a_row.OrderHeader
            order.AmountTotal += a_row.Amount - old_row.Amount
            old_order = ObjectView(row2dict(order))
            order_update(order, old_order, a_session)
            row_prt(
                order, "order_detail_flush_dirty adjusted to: " +
                str(order.AmountTotal))
    else:  # moved item to different order
        order = a_row.OrderHeader  # reduce the old one
        order.AmountTotal -= old_row.Amount
        old_order = ObjectView(row2dict(order))
        order_update(order, old_order, a_session)
        row_prt(
            order,
            "order_detail_flush_dirty adjusted to: " + str(order.AmountTotal))

        if a_row.ProductId != old_row.ProductId:
            product = a_session.query(models.Product). \
                filter(models.Product.Id == old_row.ProductId).one()
            a_row.UnitPrice = product.UnitPrice
        a_row.Amount = a_row.UnitPrice * a_row.Quantity
        order = a_session.query(models.Order). \
            filter(models.Order.Id == a_row.OrderId).one()
        old_order = ObjectView(row2dict(order))
        order.AmountTotal += a_row.Amount
        order_update(order, old_order, a_session)
        row_prt(
            order,
            "order_detail_flush_dirty adjusted to: " + str(order.AmountTotal))
Ejemplo n.º 3
0
def order_update(a_row, an_old_row, a_session):
    """
    called either by order_flush_dirty, *or* by order_detail_code. to adjust order
    see order_detail_code.order_detail_flush_new
    """
    row_prt(a_row, "\norder_flush_dirty")

    if a_row.ShippedDate != an_old_row.ShippedDate:
        is_unshipped = (a_row.ShippedDate is None) or (a_row.ShippedDate == "")
        delta = -a_row.AmountTotal  # assume not changed!!
        if is_unshipped:
            delta = a_row.AmountTotal
        customer = a_row.Customer
        customer.Balance += delta  # attach, update not req'd
        row_prt(customer, "order_upd adjusted per shipped change")

    if a_row.CustomerId != an_old_row.CustomerId:
        is_unshipped = (a_row.ShippedDate is None) or (a_row.ShippedDate == "")
        if is_unshipped:
            delta = a_row.AmountTotal
            customer = a_row.Customer
            customer.Balance += delta  # attach, update not req'd
            row_prt(customer, "order_upd adjusted Customer per re-assignment")
            old_customer = a_session.query(models.Customer). \
                filter(models.Customer.Id == an_old_row.CustomerId).one()
            # old_customer = ObjectView(row2dict(customer))
            old_customer.Balance -= delta
            a_session.add(old_customer)
            customer_update(old_customer, old_customer, a_session)
            row_prt(customer,
                    "order_upd adjusted Customer, per AmountTotal change")

    if a_row.AmountTotal != an_old_row.AmountTotal:
        # nice try: customer = a_row.Customer  -- fails, since this is *adding* order
        customer = a_session.query(models.Customer). \
            filter(models.Customer.Id == a_row.CustomerId).one()
        old_customer = ObjectView(row2dict(customer))
        delta = a_row.AmountTotal - an_old_row.AmountTotal
        customer.Balance += delta
        #  a_session.add(customer)
        customer_update(customer, old_customer, a_session)
        row_prt(customer,
                "order_upd adjusted Customer, per AmountTotal change")
def order_detail_flush_new(a_row: models.OrderDetail, a_session: session):
    """
    OrderDetail before_flush, new rows
    compute amount, adjust Order.AmountTotal
    .. which adjusts Customer.balance)
    """
    # no "old" in inserts...  old_row = get_old_row(a_row)
    row_prt(a_row, "\norder_detail_flush_new")  # readable log: curr/old values
    # nice try.. product = row.Product
    product = a_session.query(models.Product).\
        filter(models.Product.Id == a_row.ProductId).one()
    a_row.UnitPrice = product.UnitPrice
    a_row.Amount = a_row.Quantity * a_row.UnitPrice
    order = a_row.OrderHeader
    """
        2 issues make this a little more complicated than expected:
            1. can't just alter AmountTotal - does not trigger Order's before_flush
            2. can't just call Order's before_flush - old values not available
    """
    old_order = ObjectView(row2dict(order))
    order.AmountTotal += a_row.Amount
    order_update(order, old_order, a_session)
    row_prt(order,
            "order_detail_flush_new adjusted to: " + str(order.AmountTotal))