<?php

class KataoNode extends BaseKataoNode {
    const ID_LAFRANCAISE = 2;
    const ID_MONTAUBAN = 3;

    public function __toString() {
        return $this->getCity();
    }

    public function getResponsibleLink($with_link = false) {
        $result = '';
        if (null != $katao_user = $this->getKataoUser()) {
            if (null != $katao_member = $katao_user->getKataoMember()) {
                if ($with_link) {
                    $result = link_to($katao_member->getFullName(), '@katao_member_show?id=' . $katao_member->getId());
                } else {
                    $result = $katao_member->getFullName();
                }
            }
        }
        return $result;
    }

    public function updateAvailableProducts($katao_period_id, $serialized_suppliers_data = '', $product_ids = array()) {
        $criteria = new Criteria();
        $criteria->add(KataoNodeSupplierPeer::KATAO_NODE_ID, $this->getId());
        $criteria->add(KataoNodeSupplierPeer::KATAO_PERIOD_ID, $katao_period_id);
        KataoNodeSupplierPeer::doDelete($criteria);

        $suppliers_data = Utils::parseOrderedData($serialized_suppliers_data);
        if (!empty($suppliers_data['middle_col'])) {
            foreach ($suppliers_data['middle_col'] as $supplier_id) {
                $katao_node_supplier = new KataoNodeSupplier();
                $katao_node_supplier->setKataoNode($this);
                $katao_node_supplier->setKataoPeriodId($katao_period_id);
                $katao_node_supplier->setKataoSupplierId(Utils::sanitizeSupplierId($supplier_id));
                $katao_node_supplier->setIncludeAllProducts(true);
                $katao_node_supplier->save();
            }
        }
        if (!empty($suppliers_data['right_col'])) {
            foreach ($suppliers_data['right_col'] as $supplier_id) {
                $katao_node_supplier = new KataoNodeSupplier();
                $katao_node_supplier->setKataoNode($this);
                $katao_node_supplier->setKataoPeriodId($katao_period_id);
                $katao_node_supplier->setKataoSupplierId(Utils::sanitizeSupplierId($supplier_id));
                $katao_node_supplier->setIncludeAllProducts(false);
                $katao_node_supplier->save();
            }
        }

        $criteria = new Criteria();
        $criteria->add(KataoNodeProductPeer::KATAO_NODE_ID, $this->getId());
        $criteria->add(KataoNodeProductPeer::KATAO_PERIOD_ID, $katao_period_id);
        KataoNodeProductPeer::doDelete($criteria);

        if (0 < count($product_ids)) {
            foreach ($product_ids as $product_id) {
                $katao_node_product = new KataoNodeProduct();
                $katao_node_product->setKataoNode($this);
                $katao_node_product->setKataoPeriodId($katao_period_id);
                $katao_node_product->setKataoProductId((int)$product_id);
                $katao_node_product->save();
            }
        }
    }

    public function getBeginAtStr() {
        return CatalyzDate::formatShort($this->getBeginAt(null));
    }

    public function isDeletable() {
        return (0 == $this->countKataoCarts()) && (0 == $this->countKataoOrders()) && (0 == $this->countKataoInvoices()) && (0 == $this->countKataoMembersRelatedByKataoNodeId()) && (0 == $this->countKataoMembersRelatedByNextKataoNodeId());
    }
}