1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of FacturaScripts |
4
|
|
|
* Copyright (C) 2019 Carlos Garcia Gomez <[email protected]> |
5
|
|
|
* |
6
|
|
|
* This program is free software: you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU Lesser General Public License as |
8
|
|
|
* published by the Free Software Foundation, either version 3 of the |
9
|
|
|
* License, or (at your option) any later version. |
10
|
|
|
* |
11
|
|
|
* This program is distributed in the hope that it will be useful, |
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
* GNU Lesser General Public License for more details. |
15
|
|
|
* |
16
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
17
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
18
|
|
|
*/ |
19
|
|
|
namespace FacturaScripts\Core\Lib; |
20
|
|
|
|
21
|
|
|
use FacturaScripts\Core\Base\DataBase\DataBaseWhere; |
22
|
|
|
use FacturaScripts\Core\Model\Base\SalesDocument; |
23
|
|
|
use FacturaScripts\Core\Model\Base\SalesDocumentLine; |
24
|
|
|
use FacturaScripts\Dinamic\Model\Comision; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Class for the calculation of sales commissions |
28
|
|
|
* |
29
|
|
|
* @author Artex Trading s.a. <[email protected]> |
30
|
|
|
* @author Carlos García Gómez <[email protected]> |
31
|
|
|
*/ |
32
|
|
|
class CommissionTools |
33
|
|
|
{ |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* |
37
|
|
|
* @var Comision[] |
38
|
|
|
*/ |
39
|
|
|
protected $commissions; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* |
43
|
|
|
* @param SalesDocument $doc |
44
|
|
|
* @param SalesDocumentLine[] $lines |
45
|
|
|
*/ |
46
|
|
|
public function recalculate(&$doc, &$lines) |
47
|
|
|
{ |
48
|
|
|
if (!property_exists($doc, 'totalcomision')) { |
49
|
|
|
return; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$totalcommission = 0.0; |
53
|
|
|
$this->loadCommissions($doc); |
54
|
|
|
foreach ($lines as $line) { |
55
|
|
|
$totalcommission += $this->recalculateLine($line); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$doc->totalcomision = round($totalcommission, (int) FS_NF0); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* |
63
|
|
|
* @param SalesDocumentLine $line |
64
|
|
|
* |
65
|
|
|
* @return float |
66
|
|
|
*/ |
67
|
|
|
protected function getCommission($line) |
68
|
|
|
{ |
69
|
|
|
$codfamilia = $line->getProducto()->codfamilia; |
70
|
|
|
foreach ($this->commissions as $commission) { |
71
|
|
|
if (!empty($commission->codfamilia) && $commission->codfamilia != $codfamilia) { |
72
|
|
|
continue; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if (!empty($commission->idproducto) && $commission->idproducto != $line->idproducto) { |
76
|
|
|
continue; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $commission->porcentaje; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return 0.0; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Charge applicable commissions. |
87
|
|
|
* |
88
|
|
|
* @param SalesDocument $doc |
89
|
|
|
*/ |
90
|
|
|
protected function loadCommissions(&$doc) |
91
|
|
|
{ |
92
|
|
|
$this->commissions = []; |
93
|
|
|
if (empty($doc->codagente)) { |
94
|
|
|
return; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$commission = new Comision(); |
98
|
|
|
$where = [new DataBaseWhere('idempresa', $doc->idempresa)]; |
99
|
|
|
foreach ($commission->all($where, ['prioridad' => 'DESC'], 0, 0) as $comm) { |
100
|
|
|
if (!empty($comm->codagente) && $comm->codagente != $doc->codagente) { |
101
|
|
|
continue; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if (!empty($comm->codcliente) && $comm->codcliente != $doc->codcliente) { |
105
|
|
|
continue; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$this->commissions[] = $comm; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Update commission sale of a document line |
114
|
|
|
* |
115
|
|
|
* @param SalesDocumentLine $line |
116
|
|
|
* |
117
|
|
|
* @return float |
118
|
|
|
*/ |
119
|
|
|
protected function recalculateLine(&$line) |
120
|
|
|
{ |
121
|
|
|
$newValue = $this->getCommission($line); |
122
|
|
|
if ($newValue != $line->porcomision) { |
123
|
|
|
$line->porcomision = $newValue; |
124
|
|
|
$line->save(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $line->porcomision * $line->pvptotal / 100; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|