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
<?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';
/**
* Description of VtigerInventoryOperation
*/
class VtigerInventoryOperation extends VtigerModuleOperation {
public function create($elementType, $element) {
$element = $this->sanitizeInventoryForInsert($element);
$element = $this->sanitizeShippingTaxes($element);
$lineItems = $element['LineItems'];
if (!empty($lineItems)) {
$element = parent::create($elementType, $element);
$handler = vtws_getModuleHandlerFromName('LineItem', $this->user);
$handler->setLineItems('LineItem', $lineItems, $element);
$parent = $handler->getParentById($element['id']);
$handler->updateParent($lineItems, $parent);
$updatedParent = $handler->getParentById($element['id']);
//since subtotal and grand total is updated in the update parent api
$parent['hdnSubTotal'] = $updatedParent['hdnSubTotal'];
$parent['hdnGrandTotal'] = $updatedParent['hdnGrandTotal'];
$parent['pre_tax_total'] = $updatedParent['pre_tax_total'];
$components = vtws_getIdComponents($element['id']);
$parentId = $components[1];
$parent['LineItems'] = $handler->getAllLineItemForParent($parentId);
} else {
throw new WebServiceException(WebServiceErrorCode::$MANDFIELDSMISSING, "Mandatory Fields Missing..");
}
return array_merge($element,$parent);
}
public function update($element) {
$element = $this->sanitizeInventoryForInsert($element);
$element = $this->sanitizeShippingTaxes($element);
$lineItemList = $element['LineItems'];
$handler = vtws_getModuleHandlerFromName('LineItem', $this->user);
if (!empty($lineItemList)) {
$updatedElement = parent::update($element);
$handler->setLineItems('LineItem', $lineItemList, $updatedElement);
$parent = $handler->getParentById($element['id']);
$handler->updateParent($lineItemList, $parent);
$updatedParent = $handler->getParentById($element['id']);
//since subtotal and grand total is updated in the update parent api
$parent['hdnSubTotal'] = $updatedParent['hdnSubTotal'];
$parent['hdnGrandTotal'] = $updatedParent['hdnGrandTotal'];
$parent['pre_tax_total'] = $updatedParent['pre_tax_total'];
$updatedElement = array_merge($updatedElement,$parent);
} else {
$updatedElement = $this->revise($element);
}
return $updatedElement;
}
public function revise($element) {
$element = $this->sanitizeInventoryForInsert($element);
$element = $this->sanitizeShippingTaxes($element);
$handler = vtws_getModuleHandlerFromName('LineItem', $this->user);
$components = vtws_getIdComponents($element['id']);
$parentId = $components[1];
if (!empty($element['LineItems'])) {
$lineItemList = $element['LineItems'];
unset($element['LineItems']);
$updatedElement = parent::revise($element);
$handler->setLineItems('LineItem', $lineItemList, $updatedElement);
$parent = $handler->getParentById($element['id']);
$handler->updateParent($lineItemList, $parent);
$updatedParent = $handler->getParentById($element['id']);
//since subtotal and grand total is updated in the update parent api
$parent['hdnSubTotal'] = $updatedParent['hdnSubTotal'];
$parent['hdnGrandTotal'] = $updatedParent['hdnGrandTotal'];
$parent['pre_tax_total'] = $updatedParent['pre_tax_total'];
$parent['LineItems'] = $handler->getAllLineItemForParent($parentId);
} else {
$prevAction = $_REQUEST['action'];
// This is added as we are passing data in user format, so in the crmentity insertIntoEntity API
// should convert to database format, we have added a check based on the action name there. But
// while saving Invoice and Purchase Order we are also depending on the same action file names to
// not to update stock if its an ajax save. In this case also we do not want line items to change.
$_REQUEST['action'] = 'FROM_WS';
$parent = parent::revise($element);
$_REQUEST['action'] = $prevAction;
$parent['LineItems'] = $handler->getAllLineItemForParent($parentId);
}
return array_merge($element,$parent);
}
public function retrieve($id) {
$element = parent::retrieve($id);
$skipLineItemFields = getLineItemFields();
foreach ($skipLineItemFields as $key => $field) {
if (array_key_exists($field, $element)) {
unset($element[$field]);
}
}
$handler = vtws_getModuleHandlerFromName('LineItem', $this->user);
$idComponents = vtws_getIdComponents($id);
$lineItems = $handler->getAllLineItemForParent($idComponents[1]);
$element['LineItems'] = $lineItems;
$element['productid'] = $lineItems[0]['productid'];
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
return $element;
}
public function delete($id) {
$components = vtws_getIdComponents($id);
$parentId = $components[1];
$handler = vtws_getModuleHandlerFromName('LineItem', $this->user);
$handler->cleanLineItemList($id);
$result = parent::delete($id);
return $result;
}
/**
* function to display discounts,taxes and adjustments
* @param type $element
* @return type
*/
protected function sanitizeInventoryForInsert($element) {
$meta = $this->getMeta();
if (!empty($element['hdnTaxType'])) {
$_REQUEST['taxtype'] = $element['hdnTaxType'];
}
if (!empty($element['hdnSubTotal'])) {
$_REQUEST['subtotal'] = $element['hdnSubTotal'];
}
if (($element['hdnDiscountAmount'])) {
$_REQUEST['discount_type_final'] = 'amount';
$_REQUEST['discount_amount_final'] = $element['hdnDiscountAmount'];
} elseif (($element['hdnDiscountPercent'])) {
$_REQUEST['discount_type_final'] = 'percentage';
$_REQUEST['discount_percentage_final'] = $element['hdnDiscountPercent'];
} else {
$_REQUEST['discount_type_final'] = '';
$_REQUEST['discount_percentage_final'] = '';
}
if (($element['txtAdjustment'])) {
$_REQUEST['adjustmentType'] = ((int) $element['txtAdjustment'] < 0) ? '-' : '+';
$_REQUEST['adjustment'] = abs($element['txtAdjustment']);
}else {
$_REQUEST['adjustmentType'] = '';
$_REQUEST['adjustment'] = '';
}
if (!empty($element['hdnGrandTotal'])) {
$_REQUEST['total'] = $element['hdnGrandTotal'];
}
return $element;
}
public function sanitizeShippingTaxes($element){
$_REQUEST['shipping_handling_charge'] = $element['hdnS_H_Amount'];
$taxDetails = getAllTaxes('all', 'sh');
foreach ($taxDetails as $taxInfo) {
//removing previous taxes
unset($_REQUEST[$taxInfo['taxname'] . '_sh_percent']);
if ($taxInfo['deleted'] == '0' || $taxInfo['deleted'] === 0) {
if(isset($element['hdnS_H_Percent']) && $element['hdnS_H_Percent'] != 0){
$_REQUEST[$taxInfo['taxname'] . '_sh_percent'] = $element['hdnS_H_Percent'];
break;
} else {
if(isset($element[$taxInfo['taxname'] . '_sh_percent'])){
$_REQUEST[$taxInfo['taxname'] . '_sh_percent'] = $element[$taxInfo['taxname'] . '_sh_percent'];
}
//if there is Shipping Amount and shipping taxes is provided with 0
elseif($element['hdnS_H_Amount'] > 0 && $element['hdnS_H_Percent'] === 0){
$_REQUEST[$taxInfo['taxname'] . '_sh_percent'] = 0;
}else{
$_REQUEST[$taxInfo['taxname'] . '_sh_percent'] = $taxInfo['percentage'];
}
}
}
}
return $element;
}
/* NOTE: Special case to pull the default setting of TermsAndCondition */
public function describe($elementType) {
$describe = parent::describe($elementType);
$tandc = getTermsAndConditions();
foreach ($describe['fields'] as $key => $list){
if($list["name"] == 'terms_conditions'){
$describe['fields'][$key]['default'] = $tandc;
}
}
return $describe;
}
}
?>