Skip to content

Commit

Permalink
replace addresses table with vaddresses view
Browse files Browse the repository at this point in the history
  • Loading branch information
Łukasz Kosicki authored and chilek committed Mar 8, 2017
1 parent 2ba3f1f commit e861f78
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 97 deletions.
49 changes: 14 additions & 35 deletions lib/LMSManagers/LMSCustomerManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -1270,43 +1270,25 @@ public function getCustomerAddresses($id, $hide_deleted = false ) {
addr.street as location_street_name, addr.street_id as location_street,
addr.house as location_house, addr.zip as location_zip,
addr.country_id as location_country_id, addr.flat as location_flat,
ca.type as location_address_type
ca.type as location_address_type, addr.location,
(CASE WHEN
addr.city_id is not null AND addr.street_id is not null
THEN 1 ELSE 0
END) as teryt
FROM
customerview cv
LEFT JOIN customer_addresses ca ON ca.customer_id = cv.id
LEFT JOIN addresses addr ON addr.id = ca.address_id
LEFT JOIN vaddresses addr ON addr.id = ca.address_id
WHERE
cv.id = ?' .
(($hide_deleted) ? ' AND cv.deleted != 1' : ''), 'address_id',
array( $id ));


if ( !$data ) {
return array();
}

foreach ( $data as $k=>$v ) {
$tmp = array(
'city_name' => $v['location_city_name'],
'street_name' => $v['location_street_name'],
'location_house' => $v['location_house'],
'location_flat' => $v['location_flat']
);

// generate address as single string
$location = location_str($tmp);

if ( strlen($location) > 0 ) {
$data[$k]['location'] = $location;
} else {
$data[$k]['location'] = trans('undefined');
}

// check if teryt is set
if ( $v['location_city'] && $v['location_street'] ) {
$data[$k]['teryt'] = 1;
}
}

return $data;
}

Expand All @@ -1321,23 +1303,20 @@ public function getCustomerAddresses($id, $hide_deleted = false ) {
*/
public function getAddressForCustomerStuff( $customer_id ) {
$addresses = $this->db->GetAllByKey('SELECT
addr.city as city_name, addr.flat as location_flat,
addr.house as location_house, addr.street as street_name, ca.type
ca.type, addr.location
FROM customer_addresses ca
LEFT JOIN addresses addr ON ca.address_id = addr.id
LEFT JOIN vaddresses addr ON ca.address_id = addr.id
WHERE
ca.customer_id = ?', 'type', array($customer_id));

$location = null;

if ( isset($addresses[ DEFAULT_LOCATION_ADDRESS ]) ) {
$location = location_str( $addresses[ DEFAULT_LOCATION_ADDRESS ] );
if ( isset($addresses[DEFAULT_LOCATION_ADDRESS]) ) {
return $addresses[DEFAULT_LOCATION_ADDRESS]['location'];
}

if ( !$location && isset($addresses[ BILLING_ADDRESS ]) ) {
return location_str( $addresses[ BILLING_ADDRESS ] );
if ( isset($addresses[BILLING_ADDRESS]) ) {
return $addresses[BILLING_ADDRESS]['location'];
}

return $location;
return null;
}
}
41 changes: 10 additions & 31 deletions lib/LMSManagers/LMSNetDevManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -486,9 +486,9 @@ public function GetNetDevList($order = 'name,asc', $search = array())
addr.zip as location_zip, addr.country_id as location_country,
addr.city as location_city_name, addr.street as location_street_name,
addr.city_id as location_city, addr.street_id as location_street,
addr.house as location_house, addr.flat as location_flat
addr.house as location_house, addr.flat as location_flat, addr.location
FROM netdevices d
LEFT JOIN addresses addr ON d.address_id = addr.id
LEFT JOIN vaddresses addr ON d.address_id = addr.id
LEFT JOIN invprojects p ON p.id = d.invprojectid
LEFT JOIN netnodes n ON n.id = d.netnodeid
LEFT JOIN location_cities lc ON lc.id = addr.city_id
Expand All @@ -498,20 +498,11 @@ public function GetNetDevList($order = 'name,asc', $search = array())
. (!empty($where) ? ' WHERE ' . implode(' AND ', $where) : '')
. ($sqlord != '' ? $sqlord . ' ' . $direction : ''));

if ($netdevlist) {
if ( $netdevlist ) {
global $LMS;

foreach ($netdevlist as $k=>$acc) {
$tmp = array('city_name' => $acc['location_city_name'],
'location_house' => $acc['location_house'],
'location_flat' => $acc['location_flat'],
'street_name' => $acc['location_street_name']);

$location = location_str( $tmp );

if ( $location ) {
$netdevlist[$k]['location'] = $location;
} else if ( $acc['ownerid'] ) {
if ( !$acc['location'] && $acc['ownerid'] ) {
$netdevlist[$k]['location'] = $LMS->getAddressForCustomerStuff( $acc['ownerid'] );
}
}
Expand All @@ -530,18 +521,15 @@ public function GetNetDevNames()

$netdevs = $this->db->GetAll('SELECT nd.id, nd.name, nd.producer, nd.ownerid,
addr.city as city_name, addr.flat as location_flat,
addr.house as location_house, addr.street as street_name
addr.house as location_house, addr.street as street_name,
addr.location
FROM netdevices nd
LEFT JOIN addresses addr ON nd.address_id = addr.id
LEFT JOIN vaddresses addr ON nd.address_id = addr.id
ORDER BY name');

if ( $netdevs ) {
foreach ( $netdevs as $k=>$v ) {
$location = location_str( $v );

if ( $location ) {
$netdevs[$k]['location'] = $location;
} else if ( $v['ownerid'] ) {
if ( !$v['location'] && $v['ownerid'] ) {
$netdevs[$k]['location'] = $LMS->getAddressForCustomerStuff( $v['ownerid'] );
}
}
Expand Down Expand Up @@ -575,9 +563,9 @@ public function GetNetDev($id)
addr.zip as location_zip, addr.country_id as location_country,
addr.city as location_city_name, addr.street as location_street_name,
addr.city_id as location_city, addr.street_id as location_street,
addr.house as location_house, addr.flat as location_flat
addr.house as location_house, addr.flat as location_flat, addr.location
FROM netdevices d
LEFT JOIN addresses addr ON addr.id = d.address_id
LEFT JOIN vaddresses addr ON addr.id = d.address_id
LEFT JOIN nastypes t ON (t.id = d.nastype)
LEFT JOIN ewx_channels c ON (d.channelid = c.id)
LEFT JOIN location_cities lc ON (lc.id = addr.city_id)
Expand All @@ -588,15 +576,6 @@ public function GetNetDev($id)
LEFT JOIN location_states ls ON (ls.id = ld.stateid)
WHERE d.id = ?', array($id));

if ($result) {
$tmp = array('city_name' => $result['location_city_name'],
'location_house' => $result['location_house'],
'location_flat' => $result['location_flat'],
'street_name' => $result['location_street_name']);

$result['location'] = location_str( $tmp );
}

// if location is empty and owner is set then heirdom address from owner
if ( !$result['location'] && $result['ownerid'] ) {
global $LMS;
Expand Down
40 changes: 9 additions & 31 deletions lib/LMSManagers/LMSVoipAccountManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ public function getVoipAccountList($order = 'login,asc', $search = null, $sqlske
addr.name as location_name,
addr.city as location_city_name, addr.street as location_street_name,
addr.city_id as location_city, addr.street_id as location_street,
addr.house as location_house, addr.flat as location_flat
addr.house as location_house, addr.flat as location_flat, addr.location
FROM voipaccounts v '
. (isset($search['phone']) ? 'JOIN voip_numbers n ON n.voip_account_id = v.id' : '')
. ' JOIN customerview c ON (v.ownerid = c.id)
LEFT JOIN addresses addr ON addr.id = v.address_id
LEFT JOIN vaddresses addr ON addr.id = v.address_id
LEFT JOIN location_cities lc ON lc.id = addr.city_id
LEFT JOIN location_streets ls ON ls.id = addr.street_id
LEFT JOIN location_street_types lt ON lt.id = ls.typeid
Expand All @@ -111,18 +111,9 @@ public function getVoipAccountList($order = 'login,asc', $search = null, $sqlske
if ( $voipaccountlist ) {
global $LMS;

foreach ($voipaccountlist as $k=>$acc) {
$tmp = array('city_name' => $acc['location_city_name'],
'location_house' => $acc['location_house'],
'location_flat' => $acc['location_flat'],
'street_name' => $acc['location_street_name']);

$location = location_str( $tmp );

if ( $location ) {
$voipaccountlist[$k]['location'] = $location;
} else if ( $acc['ownerid'] ) {
$voipaccountlist[$k]['location'] = $LMS->getAddressForCustomerStuff( $acc['ownerid'] );
foreach ($voipaccountlist as $k=>$v) {
if ( !$v['location'] && $v['ownerid'] ) {
$voipaccountlist[$k]['location'] = $LMS->getAddressForCustomerStuff( $v['ownerid'] );
}
}
}
Expand Down Expand Up @@ -342,9 +333,9 @@ public function getVoipAccount($id) {
v.cost_limit, v.address_id, addr.name as location_name,
addr.city as location_city_name, addr.street as location_street_name,
addr.city_id as location_city, addr.street_id as location_street,
addr.house as location_house, addr.flat as location_flat
addr.house as location_house, addr.flat as location_flat, addr.location
FROM voipaccounts v
LEFT JOIN addresses addr ON addr.id = v.address_id
LEFT JOIN vaddresses addr ON addr.id = v.address_id
LEFT JOIN location_cities lc ON lc.id = addr.city_id
LEFT JOIN location_streets ls ON ls.id = addr.street_id
LEFT JOIN location_street_types lt ON lt.id = ls.typeid
Expand All @@ -356,13 +347,6 @@ public function getVoipAccount($id) {
);

if ( $result ) {
$tmp = array('city_name' => $result['location_city_name'],
'location_house' => $result['location_house'],
'location_flat' => $result['location_flat'],
'street_name' => $result['location_street_name']);

$result['location'] = location_str( $tmp );

$customer_manager = new LMSCustomerManager($this->db, $this->auth, $this->cache, $this->syslog);
$user_manager = new LMSUserManager($this->db, $this->auth, $this->cache, $this->syslog);
$result['createdby'] = $user_manager->getUserName($result['creatorid']);
Expand Down Expand Up @@ -502,9 +486,9 @@ public function getCustomerVoipAccounts($id) {
lt.name AS street_type, addr.name as location_name,
addr.city as location_city_name, addr.street as location_street_name,
addr.city_id as location_city, addr.street_id as location_street,
addr.house as location_house, addr.flat as location_flat
addr.house as location_house, addr.flat as location_flat, addr.location
FROM voipaccounts v
LEFT JOIN addresses addr ON addr.id = v.address_id
LEFT JOIN vaddresses addr ON addr.id = v.address_id
LEFT JOIN location_cities lc ON lc.id = addr.city_id
LEFT JOIN location_streets ls ON ls.id = addr.street_id
LEFT JOIN location_street_types lt ON lt.id = ls.typeid
Expand All @@ -517,12 +501,6 @@ public function getCustomerVoipAccounts($id) {

if ( $result['accounts'] ) {
foreach ($result['accounts'] as $k=>$v) {
$tmp = array('city_name' => $v['location_city_name'],
'location_house' => $v['location_house'],
'location_flat' => $v['location_flat'],
'street_name' => $v['location_street_name']);

$result['accounts'][$k]['location'] = location_str( $tmp );
$result['accounts'][$k]['phones'] = $this->db->GetAll('SELECT * FROM voip_numbers WHERE voip_account_id = ?', array($v['id']) );
}

Expand Down

0 comments on commit e861f78

Please sign in to comment.