|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* The file contains: Class to change of payment status on related records. |
|
4
|
|
|
* |
|
5
|
|
|
* @package Model |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright YetiForce S.A. |
|
8
|
|
|
* @license YetiForce Public License 5.0 (licenses/LicenseEN.txt or yetiforce.com) |
|
9
|
|
|
* @author Arkadiusz Adach <[email protected]> |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Change of payment status on related records. |
|
14
|
|
|
*/ |
|
15
|
|
|
abstract class PaymentsIn_PaymentStatus_Model |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* Module name. |
|
19
|
|
|
* |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected static $moduleName; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Field payment status name. |
|
26
|
|
|
* |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
protected static $fieldPaymentStatusName; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Field payment sum name. |
|
33
|
|
|
* |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
protected static $fieldPaymentSumName; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Related record ID name. |
|
40
|
|
|
* |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
protected static $relatedRecordIdName; |
|
44
|
|
|
|
|
45
|
1 |
|
/** |
|
46
|
|
|
* Update if possible. |
|
47
|
1 |
|
* |
|
48
|
1 |
|
* @param Vtiger_Record_Model $recordModel |
|
49
|
|
|
* |
|
50
|
1 |
|
* @return void |
|
51
|
|
|
*/ |
|
52
|
|
|
public static function updateIfPossible(Vtiger_Record_Model $recordModel) |
|
53
|
|
|
{ |
|
54
|
|
|
if (static::canUpdatePaymentStatus($recordModel)) { |
|
55
|
|
|
if ($currentRelatedId = $recordModel->get(static::$relatedRecordIdName)) { |
|
56
|
|
|
(new \App\BatchMethod(['method' => static::class . '::updatePaymentStatus', 'params' => [$currentRelatedId]]))->save(); |
|
57
|
|
|
} |
|
58
|
|
|
$previousRelatedId = $recordModel->getPreviousValue(static::$relatedRecordIdName); |
|
59
|
1 |
|
if (false !== $previousRelatedId && $previousRelatedId > 0) { |
|
60
|
|
|
(new \App\BatchMethod(['method' => static::class . '::updatePaymentStatus', 'params' => [$previousRelatedId]]))->save(); |
|
61
|
1 |
|
} |
|
62
|
1 |
|
} |
|
63
|
1 |
|
} |
|
64
|
1 |
|
|
|
65
|
|
|
/** |
|
66
|
1 |
|
* Update payment status. |
|
67
|
1 |
|
* |
|
68
|
|
|
* @param int $recordId |
|
69
|
|
|
* |
|
70
|
|
|
* @return void |
|
71
|
|
|
*/ |
|
72
|
|
|
public static function updatePaymentStatus(int $recordId) |
|
73
|
|
|
{ |
|
74
|
|
|
$changes = false; |
|
75
|
|
|
$recordModel = \Vtiger_Record_Model::getInstanceById($recordId, static::$moduleName); |
|
76
|
|
|
if (!empty(static::$fieldPaymentStatusName)) { |
|
77
|
1 |
|
$statusFieldModel = $recordModel->getField(static::$fieldPaymentStatusName); |
|
78
|
|
|
if ($statusFieldModel && $statusFieldModel->isActiveField()) { |
|
79
|
1 |
|
$recordModel->set( |
|
80
|
1 |
|
static::$fieldPaymentStatusName, |
|
81
|
|
|
static::calculatePaymentStatus((float) $recordModel->get('sum_gross'), static::getSumOfPaymentsByRecordId($recordId, static::$moduleName)) |
|
82
|
|
|
); |
|
83
|
1 |
|
$changes = true; |
|
84
|
1 |
|
} |
|
85
|
1 |
|
} |
|
86
|
|
|
if (!empty(static::$fieldPaymentSumName)) { |
|
87
|
1 |
|
$sumFieldModel = $recordModel->getField(static::$fieldPaymentSumName); |
|
88
|
1 |
|
if ($sumFieldModel && $sumFieldModel->isActiveField()) { |
|
89
|
1 |
|
$recordModel->set( |
|
90
|
1 |
|
static::$fieldPaymentSumName, |
|
91
|
1 |
|
static::getSumOfPaymentsByRecordId($recordId, static::$moduleName) |
|
92
|
1 |
|
); |
|
93
|
|
|
$changes = true; |
|
94
|
1 |
|
} |
|
95
|
|
|
} |
|
96
|
|
|
if ($changes) { |
|
97
|
|
|
$recordModel->save(); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Get the sum of all payments by record ID. |
|
103
|
|
|
* |
|
104
|
|
|
* @param int $recordId |
|
105
|
1 |
|
* @param string $moduleName |
|
106
|
|
|
* |
|
107
|
1 |
|
* @return float |
|
108
|
|
|
*/ |
|
109
|
1 |
|
public static function getSumOfPaymentsByRecordId(int $recordId, string $moduleName): float |
|
110
|
1 |
|
{ |
|
111
|
|
|
$cacheNamespace = "getSumOfPaymentsByRecordId.{$moduleName}"; |
|
112
|
|
|
if (\App\Cache::staticHas($cacheNamespace, $recordId)) { |
|
113
|
|
|
$sumOfPayments = (float) \App\Cache::staticGet($cacheNamespace, $recordId); |
|
114
|
|
|
} else { |
|
115
|
|
|
$relationModel = Vtiger_Relation_Model::getInstance( |
|
116
|
1 |
|
Vtiger_Module_Model::getInstance($moduleName), |
|
117
|
|
|
Vtiger_Module_Model::getInstance('PaymentsIn') |
|
118
|
|
|
); |
|
119
|
|
|
$relationModel->set('parentRecord', Vtiger_Record_Model::getInstanceById($recordId, $moduleName)); |
|
120
|
|
|
$queryGenerator = $relationModel->getQuery(); |
|
121
|
|
|
$queryGenerator->addNativeCondition(['vtiger_paymentsin.paymentsin_status' => 'PLL_PAID']); |
|
122
|
|
|
$sumOfPayments = (float) $queryGenerator->createQuery() |
|
123
|
|
|
->sum('vtiger_paymentsin.paymentsvalue'); |
|
124
|
|
|
\App\Cache::staticSave($cacheNamespace, $recordId, $sumOfPayments); |
|
125
|
|
|
} |
|
126
|
1 |
|
return $sumOfPayments; |
|
127
|
|
|
} |
|
128
|
1 |
|
|
|
129
|
1 |
|
/** |
|
130
|
1 |
|
* Calculate payment status. |
|
131
|
1 |
|
* |
|
132
|
|
|
* @param float $sumOfGross |
|
133
|
1 |
|
* @param float $sumOfPayments |
|
134
|
|
|
* |
|
135
|
|
|
* @return string |
|
136
|
|
|
*/ |
|
137
|
|
|
protected static function calculatePaymentStatus(float $sumOfGross, float $sumOfPayments): string |
|
138
|
|
|
{ |
|
139
|
|
|
if (\App\Validator::floatIsEqual($sumOfGross, $sumOfPayments, 3)) { |
|
140
|
|
|
$paymentStatus = 'PLL_PAID'; |
|
141
|
|
|
} elseif (\App\Validator::floatIsEqual(0.0, $sumOfPayments, 3)) { |
|
142
|
|
|
$paymentStatus = 'PLL_NOT_PAID'; |
|
143
|
|
|
} elseif ($sumOfGross > $sumOfPayments) { |
|
144
|
|
|
$paymentStatus = 'PLL_UNDERPAID'; |
|
145
|
|
|
} else { |
|
146
|
|
|
$paymentStatus = 'PLL_OVERPAID'; |
|
147
|
|
|
} |
|
148
|
|
|
return $paymentStatus; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Checking if you can update the payment status. |
|
153
|
|
|
* |
|
154
|
|
|
* @param Vtiger_Record_Model $recordModel |
|
155
|
|
|
* |
|
156
|
|
|
* @return bool |
|
157
|
|
|
*/ |
|
158
|
|
|
protected static function canUpdatePaymentStatus(Vtiger_Record_Model $recordModel): bool |
|
159
|
|
|
{ |
|
160
|
|
|
$returnValue = !$recordModel->isEmpty(static::$relatedRecordIdName) && ($recordModel->isNew() || false !== $recordModel->getPreviousValue('paymentsin_status')); |
|
161
|
|
|
if ($returnValue) { |
|
162
|
|
|
$fieldModel = \Vtiger_Module_Model::getInstance(static::$moduleName)->getFieldByName(static::$fieldPaymentStatusName); |
|
163
|
|
|
$returnValue = $fieldModel && $fieldModel->isActiveField(); |
|
164
|
|
|
} |
|
165
|
|
|
return $returnValue; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Check if payment fields for update changed. |
|
170
|
|
|
* |
|
171
|
|
|
* @param Vtiger_Record_Model $recordModel |
|
172
|
|
|
* |
|
173
|
|
|
* @return bool |
|
174
|
|
|
*/ |
|
175
|
|
|
protected static function checkIfPaymentFieldsChanged(Vtiger_Record_Model $recordModel): bool |
|
176
|
|
|
{ |
|
177
|
|
|
$result = false; |
|
178
|
|
|
$fieldsToCheck = ['paymentsin_status', 'finvoiceid', 'ssingleordersid', 'finvoiceproformaid']; |
|
179
|
|
|
foreach ($fieldsToCheck as $fieldName) { |
|
180
|
|
|
if (false !== $recordModel->getPreviousValue($fieldName)) { |
|
181
|
|
|
$result = true; |
|
182
|
|
|
break; |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
return $result; |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
|