dolibarr 23.0.3
pricecyfield.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2025 Open-Dsi <support@open-dsi.fr>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
24require_once DOL_DOCUMENT_ROOT . '/core/class/fields/commonfield.class.php';
25
26
31{
35 public $emptyValues = array('');
36
37
50 public function printInputSearchField($fieldInfos, $key, $value, $keyPrefix = '', $keySuffix = '', $moreCss = '', $moreAttrib = '')
51 {
52 $moreCss = $this->getInputCss($fieldInfos, $moreCss);
53 $htmlName = $keyPrefix . $key . $keySuffix;
54 $tmp = $this->getPriceAndCurrencyFromValue($fieldInfos, $value);
55 $value = $tmp['price'];
56 $currency = $tmp['currency'];
57
58 $tmp = $this->getPriceAndCurrencyAliasAndField($fieldInfos, $key);
59 $aliasPrice = $tmp['aliasPrice'];
60 $fieldPrice = $tmp['fieldPrice'];
61 $aliasCurrency = $tmp['aliasCurrency'];
62 $fieldCurrency = $tmp['fieldCurrency'];
63
64 $out = self::$form->inputType('text', $htmlName, (string) $value, $htmlName, $moreCss, $moreAttrib);
65 if (!empty($fieldCurrency)) {
66 $out .= self::$form->selectCurrency($currency, $htmlName . 'currency_id');
67 }
68
69 return $out;
70 }
71
84 public function printInputField($fieldInfos, $key, $value, $keyPrefix = '', $keySuffix = '', $moreCss = '', $moreAttrib = '')
85 {
86 $moreCss = $this->getInputCss($fieldInfos, $moreCss);
87 $autoFocus = $fieldInfos->inputAutofocus ? ' autofocus' : '';
88 $htmlName = $keyPrefix . $key . $keySuffix;
89 $tmp = $this->getPriceAndCurrencyFromValue($fieldInfos, $value);
90 $value = $tmp['price'];
91 $currency = $tmp['currency'];
92 $value = !$this->isEmptyValue($fieldInfos, $value) ? price($value) : '';
93
94 $out = self::$form->inputType('text', $htmlName, (string) $value, $htmlName, $moreCss, $moreAttrib . $autoFocus);
95 $out .= self::$form->selectCurrency($currency, $htmlName . 'currency_id');
96
97 return '<span class="form-select-price-currency-container">'.$out.'</span>';
98 }
99
112 public function printOutputField($fieldInfos, $key, $value, $keyPrefix = '', $keySuffix = '', $moreCss = '', $moreAttrib = '')
113 {
114 global $langs;
115 $tmp = $this->getPriceAndCurrencyFromValue($fieldInfos, $value);
116 $value = $tmp['price'];
117 $currency = $tmp['currency'];
118
119 return !$this->isEmptyValue($fieldInfos, $value) ? price($value, 0, $langs, 0, getDolGlobalInt('MAIN_MAX_DECIMALS_TOT'), -1, $currency) : '';
120 }
121
131 public function getInputCss($fieldInfos, $moreCss = '', $defaultCss = '')
132 {
133 return parent::getInputCss($fieldInfos, $moreCss, $defaultCss ? $defaultCss : 'maxwidth75');
134 }
135
145 public function verifyFieldValue($fieldInfos, $key, $value)
146 {
147 $result = parent::verifyFieldValue($fieldInfos, $key, $value);
148 if ($result && !$this->isEmptyValue($fieldInfos, $value)) {
149 $tmp = $this->getPriceAndCurrencyFromValue($fieldInfos, $value);
150 $value = $tmp['price'];
151 $currency = $tmp['currency'];
152 if (!self::$validator->isNumeric($value)) {
153 return false;
154 }
155
156 $result = true;
157 }
158
159 return $result;
160 }
161
172 public function verifyPostFieldValue($fieldInfos, $key, $keyPrefix = '', $keySuffix = '')
173 {
174 $htmlName = $keyPrefix . $key . $keySuffix;
175 $value = GETPOST($htmlName, 'restricthtml') . ':' . GETPOST($htmlName . "currency_id", 'restricthtml');
176 $value = str_replace(',', '.', $value);
177
178 return $this->verifyFieldValue($fieldInfos, $key, $value);
179 }
180
192 public function getPostFieldValue($fieldInfos, $key, $defaultValue = null, $keyPrefix = '', $keySuffix = '')
193 {
194 $htmlName = $keyPrefix . $key . $keySuffix;
195
196 if (GETPOSTISSET($htmlName)) {
197 $value = price2num(GETPOST($htmlName, 'alphanohtml')) . ':' . GETPOST($htmlName . "currency_id", 'alpha');
198 } else {
199 $value = $defaultValue;
200 }
201
202 return $value;
203 }
204
216 public function getPostSearchFieldValue($fieldInfos, $key, $defaultValue = null, $keyPrefix = '', $keySuffix = '')
217 {
218 $htmlName = $keyPrefix . $key . $keySuffix;
219
220 if (GETPOSTISSET($htmlName)) {
221 $value = array(
222 'value' => GETPOST($htmlName, 'alphanohtml'),
223 'currency' => GETPOST($htmlName . "currency_id", 'alpha'),
224 );
225 } else {
226 $value = $defaultValue;
227 }
228
229 return $value;
230 }
231
241 public function sqlFilterSearchField($fieldInfos, $key, $value)
242 {
243 $filterValue = $value['value'] ?? '';
244 $filterCurrency = $value['currency'] ?? '';
245 if ($filterCurrency == '-1') $filterCurrency = '';
246
247 $tmp = $this->getPriceAndCurrencyAliasAndField($fieldInfos, $key);
248 $aliasPrice = $tmp['aliasPrice'];
249 $fieldPrice = $tmp['fieldPrice'];
250 $aliasCurrency = $tmp['aliasCurrency'];
251 $fieldCurrency = $tmp['fieldCurrency'];
252
253 if (!empty($filterValue)) {
254 return natural_search($aliasPrice . $fieldPrice, $filterValue, 1);
255 }
256 if (!empty($filterCurrency) && !empty($fieldCurrency)) {
257 return natural_search($aliasCurrency . $fieldCurrency, $filterCurrency, 0);
258 }
259
260 return '';
261 }
262
270 public function getPriceAndCurrencyFromValue($fieldInfos, $value)
271 {
272 global $conf;
273
274 if ($this->isEmptyValue($fieldInfos, $value)) {
275 $price = '';
276 $currency = $conf->currency;
277 } else {
278 // $value in memory is a php string like '10.01:USD'
279 $tmp = explode(':', $value);
280 $price = $this->isEmptyValue($fieldInfos, $tmp[0] ?? '') ? '' : $tmp[0];
281 $currency = !empty($tmp[1]) ? $tmp[1] : $conf->currency;
282 }
283
284 return array(
285 'price' => (double) $price,
286 'currency' => $currency
287 );
288 }
289
297 public function getPriceAndCurrencyAliasAndField($fieldInfos, $key)
298 {
299 $alias = $fieldInfos->sqlAlias ?? 't.';
300 $tmp = explode(':', $alias);
301 $aliasPrice = $tmp[0] ?? '';
302 $aliasCurrency = $tmp[1] ?? '';
303
304 $field = $fieldInfos->nameInTable ?? $key;
305 $tmp = explode(':', $field);
306 $fieldPrice = $tmp[0] ?? '';
307 $fieldCurrency = $tmp[1] ?? '';
308
309 return array(
310 'aliasPrice' => trim($aliasPrice),
311 'aliasCurrency' => trim($aliasCurrency),
312 'fieldPrice' => trim($fieldPrice),
313 'fieldCurrency' => trim($fieldCurrency),
314 );
315 }
316}
Class to common field.
isEmptyValue($fieldInfos, $value, $emptyValues=null)
Check if the value is deemed as empty.
Class to pricecy field (price with currency)
getPostFieldValue($fieldInfos, $key, $defaultValue=null, $keyPrefix='', $keySuffix='')
Get field value from GET/POST.
printInputField($fieldInfos, $key, $value, $keyPrefix='', $keySuffix='', $moreCss='', $moreAttrib='')
Return HTML string to put an input field into a page.
sqlFilterSearchField($fieldInfos, $key, $value)
Get sql filter for search field.
getPriceAndCurrencyFromValue($fieldInfos, $value)
Get price and currency from value.
verifyFieldValue($fieldInfos, $key, $value)
Verify if the field value is valid.
verifyPostFieldValue($fieldInfos, $key, $keyPrefix='', $keySuffix='')
Verify if the field value from GET/POST is valid.
getInputCss($fieldInfos, $moreCss='', $defaultCss='')
Get input CSS.
printInputSearchField($fieldInfos, $key, $value, $keyPrefix='', $keySuffix='', $moreCss='', $moreAttrib='')
Return HTML string to put an input search field into a page.
getPostSearchFieldValue($fieldInfos, $key, $defaultValue=null, $keyPrefix='', $keySuffix='')
Get search field value from GET/POST.
getPriceAndCurrencyAliasAndField($fieldInfos, $key)
Get alias and field name in table for price and currency.
printOutputField($fieldInfos, $key, $value, $keyPrefix='', $keySuffix='', $moreCss='', $moreAttrib='')
Return HTML string to show a field into a page.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages.
natural_search($fields, $value, $mode=0, $nofirstand=0, $sqltoadd='')
Generate natural SQL search string for a criteria (this criteria can be tested on one or several fiel...
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.