<?php

class OrderDocuments{
    private function getProductDocuments($idOrder){
        global $database, $user;
        $whreSql = "";
        $data = [];

        if($user->getUserType() !== USER_TYPES['BROKER']){
            $whreSql = " AND visibleToCustomer=1";
        }

        $sql = "SELECT
                    d.id AS idDocument,
                    d.documentName,
                    d.documentPath,
                    d.extension,
                    dt.type AS documentType,
                    rpp.idPackage,
                    rpd.idProduct
                FROM ".TABLES['rel_product_documents']." rpd
                INNER JOIN ".TABLES['documents']." d
                    ON d.id=rpd.idDocument
                INNER JOIN ".TABLES['document_types']." dt
                    ON dt.id=d.idDocumentType
                INNER JOIN ".TABLES['rel_package_products']." rpp
                    ON rpp.idProduct=rpd.idProduct
                INNER JOIN ".TABLES['rel_order_packages']." rop
                    ON rop.idPackage=rpp.idPackage AND rop.packageInstance=rpp.packageInstance
                WHERE rop.idOrder=$idOrder $whreSql
                ";
        $query = $database->query($sql);
        while($row = $database->fetchArray($query)){
            $data[$row['idPackage']][] = $row;
        }

        return $data;
    }

    /**
     * get all docuemnts linked to an order
     * @param  INT $idOrder id for the order
     * @return Array        list of documetns for an order grouped by package id
     */
    public function getOrderDocuments($idOrder){
        global $database;
        $data = [];

        $sql = "SELECT
                    d.id AS idDocument,
                    d.documentName,
                    d.documentPath,
                    d.extension,
                    dt.type AS documentType,
                    dt.folderName AS documentTypeName,
                    IFNULL(rod.idPackage, 0) as idPackage
                FROM ".TABLES['rel_order_documents']." rod
                INNER JOIN ".TABLES['documents']." d
                    ON d.id=rod.idDocument
                INNER JOIN ".TABLES['document_types']." dt
                    ON dt.id=d.idDocumentType
                WHERE rod.idOrder=$idOrder
                UNION all
                SELECT
                        d.id AS idDocument,
                        d.documentName,
                        d.documentPath,
                        d.extension,
                        dt.type AS documentType,
                        dt.folderName AS documentTypeName,
                        rpd.idPackage
                    FROM ".TABLES['rel_package_documents']." rpd
                    INNER JOIN ".TABLES['rel_order_packages']." rop
                        ON rop.idPackage=rpd.idPackage
                    INNER JOIN ".TABLES['documents']." d
                        ON d.id=rpd.idDocument
                    INNER JOIN ".TABLES['document_types']." dt
                        ON dt.id=d.idDocumentType
                    WHERE rop.idOrder=$idOrder";
        $query = $database->query($sql);
        while($row = $database->fetchArray($query)){
            $data[$row['idPackage']][] = $row;
        }



        $productDocuments = $this->getProductDocuments($idOrder);
        if(!empty($productDocuments)){
            foreach ($productDocuments as $idPackage => &$documents) {
                if(isset($data[$idPackage])){
                    $data[$idPackage] = array_merge($data[$idPackage], $documents);
                }else{
                    $data[$idPackage] = $documents;
                }
            }
        }

        return $data;
    }
}
