<?php

class KataoSupplier extends BaseKataoSupplier {
    public function save($con = null) {
        if ('' == $this->getUrlIdentifier()) {
            $this->setUrlIdentifier(Catalyz::normalizeUrlIdentifier($this->getName()));
        }
        $this->initGmapCoordinates();
        $this->initAccountingCode();
        parent::save($con);
    }

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

    public function getCompleteAdress() {
        $result = '';
        if ($this->getAddress1()) {
            $result .= ' ' . $this->getAddress1() ;
        }
        if ($this->getAddress2()) {
            $result .= ' ' . $this->getAddress2() ;
        }
        $result .= ' ' . $this->getZip() ;
        $result .= ' ' . $this->getCity() ;
        return $result;
    }

    public function getFullAdress() {
        $return = $this->getAddress1();
        if ('' != $this->getAddress2()) {
            $return .= "\n" . $this->getAddress2();
        }
        $return .= "\n";
        $return .= $this->getZip() ;
        $return .= ' ' . $this->getCity() ;
        return $return;
    }

    public function getMemberLink($with_link = false) {
        $result = '';
        if (null != $katao_member = $this->getKataoMemberRelatedByMemberId()) {
            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 getRefererLink($with_link = false) {
        $result = '';
        if (null != $katao_member = $this->getKataoMemberRelatedByRefererId()) {
            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 initGmapCoordinates($force = false) {
        if ($this->isNew() || $force) {
            $lat_lng = Utils::getGmapLatLng($this->getCompleteAdress(), $this->getZip() . ' ' . $this->getCity());
            $this->setGmapLat($lat_lng['lat']);
            $this->setGmapLng($lat_lng['lng']);
        }
    }

    public function initAccountingCode() {
        if ('' == $this->getAccountingCode()) {
            $index = 0;
            $accounting_code = sprintf('401%s', strtoupper(substr($this->getName(), 0, 3)));
            $accounting_code_sol = sprintf('402%s', strtoupper(substr($this->getName(), 0, 3)));
            while (0 < KataoSupplierPeer::countByAccountingCode($accounting_code)) {
                $index++;
                $accounting_code = sprintf('401%s%d', strtoupper(substr($this->getName(), 0, 2)), $index);
                $accounting_code_sol = sprintf('402%s%d', strtoupper(substr($this->getName(), 0, 2)), $index);
            }
            $this->setAccountingCode($accounting_code);
            $this->setAccountingCodeSol($accounting_code_sol);
        }
    }

    public function getNodesStr() {
        $return = array();
        foreach ($this->getKataoSupplierNodesJoinKataoNode() as/*(KataoSupplierNode)*/ $katao_supplier_node) {
            $katao_node = $katao_supplier_node->getKataoNode();
            $return[] = $katao_node->getCity();
        }
        return implode(', ', $return);
    }

    public function getTypeStr($display_only_number = false) {
        $return = 'National';
        if (!$this->getIsGlobal()) {
            $return = 'Local';
            if (!$display_only_number && '' != $nodes = $this->getNodesStr()) {
                $return .= sprintf(' (%s)', $nodes);
            } elseif ($display_only_number && 0 < $nodes_count = $this->countKataoSupplierNodes()) {
                $return .= sprintf(' (%d noeud%s)', $nodes_count, (1 < $nodes_count)?'s':'');
            }
        }
        return $return;
    }

    /**
     * KataoSupplier::isVisible()
     *
     * @return boolean
     */
    public function isVisible() {
        $visible_suppliers = KataoSupplierPeer::getAllVisible();
        return !empty($visible_suppliers[$this->getId()]);
    }

    /**
     * KataoMember::getFirstKataoUser()
     *
     * @return KataoUser
     */
    public function getFirstKataoUser() {
        $katao_users = $this->getKataoUsers();
        return array_shift($katao_users);
    }

    public function getStatusStr() {
        return $this->getFirstKataoUser()->getStatusStr();
    }

    /**
     * KataoSupplier::isLocalToNode()
     *
     * @param int $katao_node_id
     * @return boolean
     */
    public function isLocalToNode($katao_node_id) {
        $criteria = new Criteria();
        $criteria->add(KataoSupplierNodePeer::KATAO_SUPPLIER_ID, $this->getId());
        $criteria->add(KataoSupplierNodePeer::KATAO_NODE_ID, $katao_node_id);
        return 1 == KataoSupplierNodePeer::doCount($criteria);
    }

    public function isDeletable() {
        return (0 == $this->countKataoProducts()) && (0 == $this->countKataoOrders());
    }

    public function getCAEuroStr() {
        $return = 0;

        $criteria = new Criteria();
        $criteria->addSelectColumn(KataoSupplierInvoicePeer::ID);
        $criteria->addAsColumn('sum_euro', sprintf('SUM(%s)', KataoSupplierInvoicePeer::TOTAL_HT));
        $criteria->add(KataoSupplierInvoicePeer::KATAO_SUPPLIER_ID, $this->getId());
        $criteria->add(KataoSupplierInvoicePeer::PAYMENT_DATE, null, Criteria::NOT_EQUAL);
        $criteria->addGroupByColumn(KataoSupplierInvoicePeer::ID);

        $rs = KataoSupplierInvoicePeer::doSelectRS($criteria);
        if ($rs->next()) {
            $return = $rs->getFloat(2);
        }

        return Utils::formatCurrencyEuro($return);
    }

    public function getCASolStr() {
        $return = 0;

        $criteria = new Criteria();
        $criteria->addSelectColumn(KataoSupplierInvoicePeer::ID);
        $criteria->addAsColumn('sum_sol', sprintf('SUM(%s)', KataoSupplierInvoicePeer::PAYMENT_AMOUNT_SOL));
        $criteria->add(KataoSupplierInvoicePeer::KATAO_SUPPLIER_ID, $this->getId());
        $criteria->add(KataoSupplierInvoicePeer::PAYMENT_DATE, null, Criteria::NOT_EQUAL);
        $criteria->addGroupByColumn(KataoSupplierInvoicePeer::ID);

        $rs = KataoSupplierInvoicePeer::doSelectRS($criteria);
        if ($rs->next()) {
            $return = $rs->getInt(2);
        }

        return Utils::formatCurrencySol($return);
    }
}