<?php

class ModelTotalPixPDV extends Model {
  public function getTotal(&$total_data, &$total){

    $paymentMethod = $this->session->data['payment_method'] ?? null;

    if (isset($paymentMethod) && ($paymentMethod['code'] == Type_pixpdvConfigs::Tp_NameModule)) {

        $this->language->load('total/pixpdv');
        $this->load->helper('ftr_geral');
        $desconto = false;

        if (!$paymentMethod['code_type']) {
            return false;
        }

        $pixpdv_pix = $this->config->get('pixpdv_pix');
        $desconto_numerico = (float) str_replace(['R$', ' '], '', str_replace(',', '.', $pixpdv_pix['desconto']));
        $pixpdv_pix['desconto'] = $desconto_numerico;
     
        if ($pixpdv_pix) {
            $desconto = ftr_geral::getPaymentDiscount([$pixpdv_pix], $total, $this->cart->getTotal());
            $title = $this->language->get('text_discount_pix');
        }
      
        if (!$desconto) {
            return false;
        }

        $total -= $desconto * -1;

        $total_data[] = [
            'code' => Type_pixpdvConfigs::Tp_NameModule,
            'title' => $title,
            'text' => $this->currency->format($desconto),
            'value' => $desconto,
            'sort_order' => 0//$this->config->get('pixpdv_discount_order')
        ];

        return; 
    } 

    return false;
  }

}
