<?php

class OrderProcessHelper{

    /**
     * get all steps for an order
     * @param  INT $idOrder id of the order
     * @return Array        list of steps for an order grouped by process and package
     */
    public function getOrderSteps($idOrder){
        global $database, $user;
        $userType = $user->getUserType();
        $extraWhereClause = '';
        $extraJoin = '';

        $comments = $this->getStepComments($idOrder);

        if($userType === USER_TYPES['CUSTOMER']) {
            $extraWhereClause = " AND ps.isVisibleForCustomer = 1";
        }

        if($userType === USER_TYPES['SUPPLIER']) {
            $extraJoin = " INNER JOIN(
                            SELECT DISTINCT rop.idOrder, rop.idPackage
                                FROM ".TABLES['rel_order_packages']." rop
                            INNER JOIN ".TABLES['rel_package_products']." rpp
                                ON rpp.idPackage=rop.idPackage and rpp.packageInstance=rpp.packageInstance
                            INNER JOIN ".TABLES['suppliers_countries_products']." scp
                                ON scp.idProduct=rpp.idProduct
                            INNER JOIN ".TABLES['suppliers']." s
                                ON s.id=scp.idSupplier
                            WHERE s.idUSer=".$user->getUserId()." AND rop.idOrder=$idOrder
                            ) supplierPkg
                            ON rops.idOrder=supplierPkg.idOrder AND rops.idPackage=supplierPkg.idPackage";
        }

        $sql = "SELECT o.id AS idOrder,
                    ps.shortDesc,
                    ps.fullDesc,
                    ps.isVisibleForCustomer,
                    psa.actionCode,
                    psa.stepType,
                    rops.idProcessStep,
                    rops.status,
                    DATE_FORMAT(rops.actualDate, '%D %b, %y') as actualDate,
                    DATE_FORMAT(NOW(), '%D %b %y') as now,
                    rps.idProcess,
                    p.name AS processName
                FROM ".TABLES['rel_order_process_step']." rops
                INNER JOIN ".TABLES['orders']." o
                    ON o.id=rops.idOrder
                INNER JOIN ".TABLES['rel_process_steps']." rps
                    ON rps.id=rops.idProcessStep
                INNER JOIN ".TABLES['processes']." p
                    ON p.id=rps.idProcess
                INNER JOIN ".TABLES['process_step']." ps
                    ON ps.id=rps.idStep
                INNER JOIN ".TABLES['process_step_actions']." psa
                    ON psa.id=ps.idActionCode
                $extraJoin
                WHERE o.id=".$idOrder."
                    $extraWhereClause
                ORDER BY rps.idProcess, rops.idProcessStep DESC
                ";
        $query = $database->query($sql);
        $data = [];
        while($row = $database->fetchArray($query)){
            $idProcess = $row['idProcess'];
            $data[$idProcess]['idProcess'] = $row['idProcess'];
            $data[$idProcess]['processName'] = $row['processName'];
            unset($row['processName']);
            $row['comments'] = isset($comments[$row['idProcessStep']]) ? $comments[$row['idProcessStep']] : [];
            $row['isNewCommentVisible'] = $row['isVisibleForCustomer'];
            $data[$idProcess]['steps'][] = $row;
        }

        return $data;
    }

    /**
     * get comments from an order
     * @param  INT $idOrder id of the order
     * @return Array          list of comments for an order grouped by idPackage and idProcessStep
     */
    private function getStepComments($idOrder){
        global $database, $user;

        $extraFields = '';
        $whereSql = '';
        $userType = $user->getUserType();
        if($userType === USER_TYPES['BROKER']){
            $extraFields = ",rsc.isVisible ";
        }else{
            $whereSql = " AND rsc.isVisible=1";
        }

        $sql = "SELECT rsc.id, rsc.idProcessStep,
                    rsc.idPackage,
                    rsc.comment,
                    DATE_FORMAT(rsc.addDate, '%D %b, %y') as addDate,
                    u.username as user
                    $extraFields
                FROM ".TABLES['rel_step_comments']." rsc
                INNER JOIN ".TABLES['users']." u
                    ON u.id=rsc.idUser
                WHERE rsc.idOrder=$idOrder
                    AND rsc.type = 'stepComment'
                    $whereSql";
        $comments = [];
        $query = $database->query($sql);
        while($row = $database->fetchArray($query)){
            $idProcessStep = $row['idProcessStep'];
            unset($row['idProcessStep']);
            $idPackage = $row['idPackage'];
            unset( $row['idPackage']);
            $comments[$idPackage][$idProcessStep][] = $row;
        }

        return $comments;
    }

}
