<?php

/**
 * Subclass for performing query and update operations on the 'katao_invoice_product' table.
 *
 * @package lib.model
 */
class KataoInvoiceProductPeer extends BaseKataoInvoiceProductPeer {
    public static function deliverQuantity($katao_invoice_product_id, $quantity) {
        if (null != $katao_invoice_product = self::retrieveByPK($katao_invoice_product_id)) {
            $katao_invoice_product->setQuantityDelivered($quantity);
            $katao_invoice_product->save();

            if (null != $katao_order_product = $katao_invoice_product->getKataoOrderProduct()) {
                $quantity_delivered = 0;
                foreach ($katao_order_product->getKataoInvoiceProducts() as/*(KataoInvoiceProduct)*/ $katao_order_product_invoice_product) {
                    $quantity_delivered += $katao_order_product_invoice_product->getQuantityDelivered();
                }

                $katao_order_product->setHasBeenDelivered(true);
                $katao_order_product->setQuantityDelivered($quantity_delivered);
                $katao_order_product->save();
            }
            if (null != $katao_cart_product = $katao_invoice_product->getKataoCartProduct()) {
                $katao_cart_product->setQuantityDelivered($quantity);
                $katao_cart_product->save();
            }
        }
    }
}