Newer
Older
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
<?php
/* +***********************************************************************************
* The contents of this file are subject to the vtiger CRM Public License Version 1.0
* ("License"); You may not use this file except in compliance with the License
* The Original Code is: vtiger CRM Open Source
* The Initial Developer of the Original Code is vtiger.
* Portions created by vtiger are Copyright (C) vtiger.
* All Rights Reserved.
* *********************************************************************************** */
require_once 'include/Webservices/VtigerModuleOperation.php';
require_once 'include/Webservices/Utils.php';
class VtigerProductOperation extends VtigerModuleOperation {
private static $currencyWsId = null;
public function create($elementType, $element) {
$element = $this->sanitizeTaxes($element);
$element = $this->sanitizeCurrency($element, 'Create');
$element = parent::create($elementType, $element);
$createdId = $element['id'];
$currencyTaxElement = $this->retrieveTaxesAndCurrency($createdId, $elementType);
return array_merge($element, $currencyTaxElement);
}
public function update($element) {
$element = $this->sanitizeTaxes($element);
$element = $this->sanitizeCurrency($element);
$element = parent::update($element);
$entityName = $this->meta->getObjectEntityName($element['id']);
$updatedId = $element['id'];
$currencyTaxElement = $this->retrieveTaxesAndCurrency($updatedId, $entityName);
return array_merge($element, $currencyTaxElement);
}
public function revise($element) {
$element = $this->sanitizeTaxes($element);
$element = $this->sanitizeCurrency($element);
$element = parent::revise($element);
$entityName = $this->meta->getObjectEntityName($element['id']);
$revisedId = $element['id'];
$currencyTaxElement = $this->retrieveTaxesAndCurrency($revisedId, $entityName);
return array_merge($element, $currencyTaxElement);
}
public function retrieve($wsId) {
$element = parent::retrieve($wsId);
$entityName = $this->meta->getObjectEntityName($element['id']);
$currencyTaxElement = $this->retrieveTaxesAndCurrency($wsId, $entityName);
return array_merge($element, $currencyTaxElement);
}
public function describe($elementType) {
$describe = parent::describe($elementType);
$taxes = array();
$taxes = getAllTaxes('available');
foreach ($taxes as $index => $taxInfo) {
$taxField = array();
$taxField['name'] = $taxInfo['taxname'];
$taxField['label'] = $taxInfo['taxlabel'];
$taxField['type'] = array('name' => 'double');
$taxField['nullable'] = '1';
$taxField['editable'] = '1';
$taxField['mandatory'] = '';
$taxField['default'] = $taxInfo['percentage'];
$describe['fields'][] = $taxField;
}
$currency = array();
$currencyDetails = getPriceDetailsForProduct('', '', 'available', $elementType);
foreach ($currencyDetails as $currencyDetail) {
$currencyId = $currencyDetail['curid'];
$currency['name'] = "currency$currencyId";
$currency['label'] = $currencyDetail['currencylabel']." ".$currencyDetail['currencycode'];
$currency['type'] = array('name' => 'double');
$currency['nullable'] = '1';
$currency['editable'] = '1';
$currency['mandatory'] = '';
$currency['default'] = '';
$describe['fields'][] = $currency;
}
return $describe;
}
protected function sanitizeTaxes($element) {
$taxes = getAllTaxes('available');
foreach ($taxes as $taxInfo) {
$taxName = $taxInfo['taxname'];
unset($_REQUEST[$taxName.'_check']);
unset($_REQUEST[$taxName]);
if (array_key_exists($taxName, $element) && $element[$taxName] !== '') {
$_REQUEST[$taxName.'_check'] = 1;
$_REQUEST[$taxName] = $element[$taxName];
}
}
return $element;
}
protected function sanitizeCurrency($element, $mode = false) {
$currencies = getAllCurrencies('available');
$activeCurrencies = array();
foreach ($currencies as $currencyInfo) {
$curId = $currencyInfo['curid'];
$activeCurrencies[] = $curId;
$currencyName = "currency$curId";
unset($_REQUEST[$currencyName]);
unset($_REQUEST["cur_$curId"."_check"]);
if (isset($element[$currencyName]) && is_numeric($element[$currencyName])) {
$_REQUEST["cur_$curId"."_check"] = 1;
$_REQUEST["curname$curId"] = CurrencyField::convertToUserFormat($element[$currencyName], null, true);
}
}
unset($_REQUEST['base_currency']);
if (!empty($element['currency_id'])) {
$ids = vtws_getIdComponents($element['currency_id']);
//Import will be sending currency id not in webservice format .
//So we are taking 0th index as value
if (php7_count($ids) == 1) {
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
172
173
174
175
176
$curId = $ids[0];
} else {
$curId = $ids[1];
}
if ($curId && is_numeric($curId) && in_array($curId, $activeCurrencies)) {
$_REQUEST['base_currency'] = "curname$curId";
$_REQUEST["cur_$curId"."_check"] = 1;
$_REQUEST["curname$curId"] = CurrencyField::convertToUserFormat($element['unit_price'], null, true);
} else {
throw new WebServiceException(WebServiceErrorCode::$INACTIVECURRENCY, "Provided Curreny is Inactive");
}
} else if ($mode == 'Create') {
$curidArray = Vtiger_Util_Helper::getBaseCurrency();
$curid = $curidArray['id'];
$_REQUEST['base_currency'] = "curname$curid";
$_REQUEST["cur_$curid"."_check"] = 1;
$_REQUEST["curname$curid"] = CurrencyField::convertToUserFormat($element['unit_price'], null, true);
}
$_REQUEST['unit_price'] = $element['unit_price'];
return $element;
}
Public function retrieveTaxesAndCurrency($wsId, $entityName) {
$db = PearDatabase::getInstance();
$newElement = array();
if (!self::$currencyWsId) {
$currencyObject = VtigerWebserviceObject::fromName($db, "Currency");
self::$currencyWsId = $currencyObject->getEntityId();
}
$ids = vtws_getIdComponents($wsId);
$productId = $ids[1];
$taxDetails = getTaxDetailsForProduct($productId);
if ($taxDetails) {
foreach ($taxDetails as $tax) {
$taxName = $tax['taxname'];
$taxPercent = $tax['percentage'];
$newElement[$taxName] = $taxPercent;
}
}
$priceDetails = getPriceDetailsForProduct($productId, '', '', $entityName);
foreach ($priceDetails as $priceDetail) {
$currencyId = $priceDetail['curid'];
$newElement["currency$currencyId"] = CurrencyField::convertToDBFormat($priceDetail['curvalue']);
if ($priceDetail['is_basecurrency']) {
$newElement['currency_id'] = vtws_getId(self::$currencyWsId, $currencyId);
}
}
return $newElement;
}
}