<?php

class Bids {
    public function getCartBids(){
        global $database, $user;
        $bids = [];
        $pricesHandler = new Prices();
        $interestRate = new InterestRate();
        $interestRateValue = $interestRate->getInterestRate()['interestRate'];
        $interestRateCustomers = $interestRate->getInterestRateForCustomers();

        $sql = "SELECT
                    pb.id AS idBid,
                    pb.bidNumber,
                    pb.idPackage,
                    p.name AS packageName,
                    pb.idCustomerInstance,
                    pb.fixedExtra,
                    pb.recurrentExtra,
                    pb.servicesExtra,
                    c.id AS idCustomer,
                    c.name AS customer,
                    cl.name AS commercialLead,
                    pb.idPaymentType,
                    pt.payType,
                    pt.packagePayPeriod,
                    pt.periodUnit,
                    DATE_FORMAT(pb.startDate, '%D %b, %Y') AS startDate,
                    DATE_FORMAT(pb.endDate, '%D %b, %Y') AS endDate,
                    pb.fixedPrice,
                    pb.principalAmount,
                    pb.servicesPrice
                FROM ".TABLES['packages_bids']." pb
                INNER JOIN (
                    SELECT wsc.idPackage, wsc.idCustomerInstance, plcl.idPaymentType
                        FROM ".TABLES['web_shop_cart']." wsc
                    INNER JOIN ".TABLES['price_list_commercial_lead']." plcl
                        ON plcl.id=wsc.idPrice
                    WHERE wsc.idUser=".$user->getUserId()."
                )  wsc
                    ON wsc.idPackage=pb.idPackage AND wsc.idCustomerInstance=pb.idCustomerInstance AND wsc.idPaymentType=pb.idPaymentType
                INNER JOIN ".TABLES['packages']." p
                    ON p.id=pb.idPackage
                INNER JOIN ".TABLES['rel_commercial_lead_customers']." rclc
                    ON rclc.id=pb.idCustomerInstance
                INNER JOIN ".TABLES['customers']." c
                    ON c.id=rclc.idCustomer
                INNER JOIN ".TABLES['commercial_leads']." cl
                    ON cl.id=rclc.idCommercialLead
                INNER JOIN ".TABLES['payment_types']." pt
                    ON pt.id=pb.idPaymentType
                WHERE pb.endDate > NOW()
                ORDER BY pb.bidNumber";

        $query = $database->query($sql);
        while($row = $database->fetchArray($query)){
            $interestRateCust = $row['idCustomer'] && isset($interestRateCustomers[$row['idCustomer']]) ?
                                floatval($interestRateCustomers[$row['idCustomer']]) :
                                $interestRateValue;
            $row['recurrentPrice'] = $row['packagePayPeriod'] > 0
                ? $pricesHandler->PMT($interestRateCust / 100, $row['packagePayPeriod'], $row['principalAmount'])
                : 0;

            $row['fixedPrice'] =  floatval($row['fixedPrice']) + floatval($row['fixedExtra']);
            $row['recurrentPrice'] =  floatval($row['recurrentPrice']) + floatval($row['recurrentExtra']);
            $row['servicesPrice'] =  floatval($row['servicesPrice']) + floatval($row['servicesExtra']);
            unset($row['fixedExtra']);
            unset($row['recurrentExtra']);
            unset($row['servicesExtra']);
            $bids[$row['idPackage']][] = $row;
        }

        return $bids;
    }

    public function setBidForCart($idBid, $idCart){
        global $database;
        $data = [];

        if(!$idBid){
            $idBid = 'NULL';
        }

        if(!$idCart){
            $data['messages'][] =[
                'code' => 'error',
                'message' => 'CART_REQUIRED'
            ];

            return $data;
        }

        $idBid = $database->escapeValue($idBid);
        $idCart = $database->escapeValue($idCart);

        $sql = "UPDATE ".TABLES['web_shop_cart']."
                SET idBid=$idBid
                WHERE id=$idCart";
        $query = $database->query($sql);

        if($database->affectedRows() !== 1){
            $data['messages'][] =[
                'code' => 'error',
                'message' => 'BID_NOT_USED'
            ];
        }else{
            $data['messages'][] = [
                'code' => 'success',
                'message' => 'BID_SET'
            ];
        }

        return $data;
    }
}
