<?php
/**
 * Class represents records from table selfservice_type_product
 * {autogenerated}
 * @property int $type_product_id PK
 * @property int $type_id FK selfservice_type
 * @property int $product_id FK product
 * @property int $billing_plan_id
 * @property string $relation
 * @property int $value
 * @property int $value_second
 * @property int|null $rollover
 * @property string|null $expire
 * @property int|null $sort_order
 * {/autogenerated}
 */
class SelfserviceTypeProduct extends Am_Record
{
    const IN = 'in';
    const OUT = 'out';

    function getProduct()
    {
        $bp = $this->getDi()->billingPlanTable->load($this->billing_plan_id);
        return $bp->getProduct();
    }

    public function insert($reload = true)
    {
        $table_name = $this->getTable()->getName();
        $max = $this->getAdapter()->selectCell("SELECT MAX(sort_order) FROM {$table_name} WHERE type_id=?", $this->type_id);
        $this->sort_order = $max + 1;
        return parent::insert($reload);
    }
}

/**
 * @extends Am_Table<SelfserviceTypeProduct>
 * @method void deleteByProductId($value, $useObjects = true)
 */
class SelfserviceTypeProductTable extends Am_Table
{
    protected $_key = 'type_product_id';
    protected $_table = '?_selfservice_type_product';
}