<?php
class Procurement{

    /**
     * get documents grouped by suppliers
     * @param  INT $idOrder         id for the order
     * @param  String $documentType the type of the document
     * @return Array                list of documents
     */
    private function getSupplierDocuments($idOrder, $documentType = ''){
        global $database;
        $data = [];
        $extraWhere = '';

        if($documentType) {
            $extraWhere = "
                AND d.idDocumentType =
                  (SELECT
                    dt.id
                  FROM
                    document_types dt
                  WHERE dt.folderName = '$documentType')";
        }

        $sql = "SELECT d.id as idDocument,
                    d.documentName,
                    d.extension,
                    rod.idPackage,
                    s.name AS supplierName
                FROM ".TABLES['documents']." d
                INNER JOIN ".TABLES['rel_order_documents']." rod
                    ON rod.idDocument=d.id
                INNER JOIN ".TABLES['suppliers']." s
                    ON s.idUser=d.idOwner
                WHERE rod.idOrder=$idOrder $extraWhere";
        $query = $database->query($sql);
        while($row = $database->fetchArray($query)){
            $data[$row['supplierName']][] = $row;
        }

        return $data;
    }

    /**
     * get suppliers products
     * @param  INT $idOrder          id for the order
     * @param  INT $idPackage        id for the order
     * @param  String $documentType  the type of the document
     * @return Array                 Array of products estimations
     */
    public function getSuppliersByPackageOrder($idOrder, $idPackage, $documentType){
        global $database, $user;
        $supplierBidsHandler = new SupplierBids();

        $idOrder = $database->escapeValue($idOrder);
        $idPackage = $database->escapeValue($idPackage);
        $documentType = $database->escapeValue($documentType);
        $data = [];
        $whereSql = "";

        $documents = $this->getSupplierDocuments($idOrder, $documentType);
        $supplierBids = $supplierBidsHandler->getOrderSupplierBids($idOrder);

        if($user->getUserType() === USER_TYPES['SUPPLIER']){
            $whereSql .= " AND s.idUser=".$user->getUserId();
        }

        if($idPackage != 0){
            $whereSql .= " AND idPackage=$idPackage";
        }

        $sql = "SELECT s.id AS idSupplier,
                       s.name AS supplierName
                FROM ".TABLES['rel_order_supplier_estimations']." rose
                INNER JOIN ".TABLES['suppliers']." s
                    ON s.id=rose.idSupplier
                WHERE idOrder=$idOrder $whereSql
                ORDER BY s.id";
        $query = $database->query($sql);
        while($row = $database->fetchArray($query)){
            $data[$row['supplierName']]['idSupplier'] = $row['idSupplier'];
            $data[$row['supplierName']]['documents'] = isset($documents[$row['supplierName']]) ? $documents[$row['supplierName']] : [];
            $data[$row['supplierName']]['supplierBids'] = isset($supplierBids[$row['idSupplier']]) ? $supplierBids[$row['idSupplier']] : [];
        }

        return $data;
    }
}
