forked from shinesoftware/shineisp
/
Customers.php
180 lines (148 loc) · 6.83 KB
/
Customers.php
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
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
<?
class Shineisp_Api_Customers extends Shineisp_Api_Abstract_Action {
public function insert( $params ) {
$this->authenticate();
$form = new Api_Form_CustomerForm ( array ('action' => '#', 'method' => 'post' ) );
#Add email validator
$mailValidator = new Shineisp_Validate_Email();
$form->getElement('email')->addValidator($mailValidator);
$form->addElement('password', 'password', array(
'filters' => array('StringTrim'),
'required' => true,
'decorators' => array('Composite'),
'validators' => array(
array('regex', false, '/^[a-zA-Z0-9\-\_\.\%\!\$]{6,20}$/')
),
'description' => 'Write here your password. (min.6 chars - max.20 chars)',
'label' => 'Password',
'class' => 'text-input large-input'
));
if( array_key_exists('countrycode', $params) ) {
$country_id = Countries::getIDbyCode($params['countrycode']);
if( $country_id == null ) {
throw new Shineisp_Api_Exceptions( 400005, ":: 'countrycode' not valid" );
exit();
}
unset($params['coutrycode']);
$params['country_id'] = $country_id;
}
if( array_key_exists('provincecode',$params) && $params['provincecode'] != "" ) {
$params['area'] = $params['provincecode'];
unset($params['provincecode']);
}
if ($form->isValid ( $params ) ) {
if( $params['status'] == false ) {
$params['status'] = 'disabled';
}
$idcustomers = Customers::Create($params);
$customer = Customers::find($idcustomers);
return $customer['uuid'];
} else {
$errors = $form->getMessages();
$message = "";
foreach( $errors as $field => $errorsField ) {
$message .= "Field '{$field}'<br/>";
foreach( $errorsField as $error => $describe ) {
$message .=" => {$error} ({$describe})";
}
}
throw new Shineisp_Api_Exceptions( 400004, ":\n{$message}" );
exit();
}
}
public function get( $uuid ) {
$this->authenticate();
$customer = Customers::findWithUuid($uuid);
if( empty($customer) ) {
return false;
}
$customerid = $customer['customer_id'];
$fields = ' c.company as company, c.firstname as firstname, c.lastname as lastname,c.sex,c.email, c.taxpayernumber, c.vat, l.legalform_id as legalformid,c.birthdate as birthdate,
c.sex as sex, a.address_id, a.address as address, a.city as city,a.code as code,a.country_id as countryid ,a.region_id as regionid,a.area as area, ct.code as countrycode
,cn.*,cnts.*';
$customerid = $customer['customer_id'];
$customer = Customers::getAllInfo( $customerid, $fields );
unset($customer['password']);
return $customer;
}
public function update( $uuid, $params ) {
$this->authenticate();
$form = new Api_Form_CustomerForm ( array ('action' => '#', 'method' => 'post' ) );
if( array_key_exists('countrycode', $params) ) {
$country_id = Countries::getIDbyCode($params['countrycode']);
if( $country_id == null ) {
throw new Shineisp_Api_Exceptions( 400005, ":: 'countrycode' not valid" );
exit();
}
unset($params['coutrycode']);
$params['country_id'] = $country_id;
}
if( array_key_exists('provincecode',$params) && $params['provincecode'] != "" ) {
$params['area'] = $params['provincecode'];
unset($params['provincecode']);
}
if ($form->isValid ( $params ) ) {
if( $params['status'] == false ) {
$params['status'] = 'disabled';
}
$customer = Customers::findWithUuid($uuid);
if( empty($customer) ) {
return false;
}
$customerid = $customer['customer_id'];
// $data = explode('/', $params['birthdate']);
// list( $gg, $mm, $yyyy ) = $data;
// $params['birthdate'] = $yyyy.'-'.$mm.'-'.$gg;
foreach( $customer as $name => &$value ) {
if( array_key_exists($name, $params) ) {
$value = $params[$name];
}
}
$fields = $params;
unset( $fields['address'] );
unset( $fields['contact'] );
Customers::saveAll( $customerid, $fields );
$address = array();
$address['address'] = $params['address'];
$address['city'] = $params['city'];
$address['code'] = $params['code'];
$address['area'] = $params['area'];
$address['region_id'] = $params['regionid'];
$address['country_id'] = $params['country_id'];
$address['customer_id'] = $customerid;
if( $params['addressid'] != 0 ) {
Addresses::AddNew($address, $params['addressid']);
} else {
Addresses::AddNew($address);
}
if( array_key_exists('contacts', $params) && !empty( $params['contacts']) ) {
foreach( $params['contacts'] as $contact ) {
if( $contact['contact'] == "" ) {
continue;
}
$c = array();
$c['contact'] = $contact['contact'];
$c['type_id'] = $contact['contacttypes'];
$c['customer_id'] = $customerid;
if( intval($contact['idcontact']) != 0 ){
Contacts::AddNew($c, intval($contact['idcontact']));
} else {
Contacts::AddNew($c);
}
}
}
return true;
} else {
$errors = $form->getMessages();
$message = "";
foreach( $errors as $field => $errorsField ) {
$message .= "Field '{$field}'<br/>";
foreach( $errorsField as $error => $describe ) {
$message .=" => {$error} ({$describe})";
}
}
throw new Shineisp_Api_Exceptions( 400004, ":\n{$message}" );
exit();
}
}
}