-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcalc-parcelamento.php
171 lines (132 loc) · 3.65 KB
/
calc-parcelamento.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/**
*
* @since 1.0.0
* @author David Costa <contato@davidcosta.com.br>
* @version 1.0.0
*/
if(!defined('ABSPATH')){
exit;
}
if ( ! class_exists( 'CalcParcelamento' ) ) :
final class CalcParcelamento
{
/**
* 0 - Mercado Livre
* 1 - Marcedo password_get_info
* 2 - Sem Juros
*/
public $fator = 'mercado_livre';
public $valor_minimo = 10;
public $mercaro_livre = [
'2' => 5.24,
'3' => 6.99,
'4' => 8.74,
'5' => 10.28,
'6' => 11.99,
'10' => 14.85,
'12' => 15.99,
// '15' => 18.49,
// '18' => 21.49,
// '24' => 27.99
];
public $mercaro_pago = [
'2' => 2.39,
'3' => 4.78,
'4' => 7.17,
'5' => 9.15,
'6' => 10.72,
'10' => 17.17,
'12' => 20.48,
// '15' => 25.56,
// '18' => 30.77,
// '24' => 32.99
];
public $sem_juros = [
'1' => 0,
'2' => 0,
'3' => 0,
'4' => 0,
'5' => 0,
'6' => 0,
'7' => 12.308,
'8' => 13.920,
'9' => 15.542,
'10' => 17.170,
'11' => 18.822,
'12' => 20.480,
// '15' => 25.56,
// '18' => 30.77,
// '24' => 32.99
];
protected $type = 'sem_juros';
protected static $_instance = null;
// $Calc = self::instance();
public static function instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
}
public function setType($type) {
$this->type = $type;
return $this;
}
public function setMinimo($minimo) {
$this->valor_minimo = $minimo;
return $this;
}
public function calc($valor) {
if($valor == 0) {
return '';
}
$fators = $this->getFator();
$last = '';
foreach ($fators as $divisao => $fator) {
$fator_refinado = ($fator * 0.01) + 1;
$total_parcelado = $valor * $fator_refinado;
$parcela = $total_parcelado / $divisao;
if($parcela < $this->valor_minimo) break;
if($divisao > 6) break;
$semJurusText = $fator == 0 ? ' SEM JUROS' : '';
$last = 'em <strong>'. $divisao .'x de '.$this->floatToReal($parcela).'</strong>'.$semJurusText;
}
return $last;
}
public function parcela_table($valor) {
if($valor == 0) {
return '';
}
$fators = $this->getFator();
$html = '';
foreach ($fators as $divisao => $fator) {
$fator_refinado = ($fator * 0.01) + 1;
$total_parcelado = $valor * $fator_refinado;
$parcela = $total_parcelado / $divisao;
if($parcela < $this->valor_minimo) break;
$semJurusText = $fator == 0 ? ' SEM JUROS' : '';
$html .= '<div class="col-xs-12 col-sm-6"><strong>'. $divisao .'x de '.$this->floatToReal($parcela).'</strong>'.$semJurusText.'</div>';
}
$img = '<img src="'.plugins_url('dvd-woo-mercadopago-parcela/img/bandeiras-parcelamento.gif').'">';
return '<div id="table-parcelado"><div class="title">'.$img.' Parcelamento</div><div class="row">'.$html.'</div></div>';
}
private function getFator() {
switch ($this->type) {
case 'mercaro_livre':
$fators = $this->mercaro_livre;
break;
case 'sem_juros':
$fators = $this->sem_juros;
break;
case 'mercaro_pago':
$fators = $this->mercaro_pago;
break;
}
return $fators;
}
private function floatToReal($float) {
return 'R$ '. number_format($float, 2, ',', '.');
}
}
// new CalcParcelamento();
endif;