| 1 |
<?php |
| 2 |
namespace WeDevs\ERP; |
| 3 |
use WeDevs\ERP\Admin\Models\Company_Locations; |
| 4 |
|
| 5 |
/** |
| 6 |
* Company class |
| 7 |
*/ |
| 8 |
class Company { |
| 9 |
|
| 10 |
/** |
| 11 |
* Holds the company data array |
| 12 |
* |
| 13 |
* @var array |
| 14 |
*/ |
| 15 |
private $data; |
| 16 |
|
| 17 |
/** |
| 18 |
* Option name in wp_options table |
| 19 |
*/ |
| 20 |
const key = '_erp_company'; |
| 21 |
|
| 22 |
/** |
| 23 |
* Constructor |
| 24 |
*/ |
| 25 |
public function __construct() { |
| 26 |
$this->data = get_option( self::key, $this->defaults() ); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Get some defaults if no data found |
| 31 |
* |
| 32 |
* @return array |
| 33 |
*/ |
| 34 |
public function defaults() { |
| 35 |
$defaults = [ |
| 36 |
'logo' => 0, |
| 37 |
'name' => __( 'Untitled Company', 'erp' ), |
| 38 |
'address' => [ |
| 39 |
'address_1' => __( 'Street Address 1', 'erp' ), |
| 40 |
'address_2' => __( 'Address Line 2', 'erp' ), |
| 41 |
'city' => __( 'City', 'erp' ), |
| 42 |
'state' => __( 'State', 'erp' ), |
| 43 |
'postcode' => '', |
| 44 |
'country' => 'US' |
| 45 |
], |
| 46 |
'phone' => '', |
| 47 |
'fax' => '', |
| 48 |
'mobile' => '', |
| 49 |
'website' => '', |
| 50 |
'currency' => 'USD' |
| 51 |
]; |
| 52 |
|
| 53 |
return apply_filters( 'erp_company_defaults', $defaults ); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Magic method to get item data values |
| 58 |
* |
| 59 |
* @param string |
| 60 |
* |
| 61 |
* @return string |
| 62 |
*/ |
| 63 |
public function __get( $key ) { |
| 64 |
if ( isset( $this->data[ $key ] ) ) { |
| 65 |
if ( is_array( $this->data[ $key ] ) ) { |
| 66 |
return $this->data[ $key ]; |
| 67 |
} |
| 68 |
|
| 69 |
return stripslashes( $this->data[ $key ] ); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Update company data |
| 75 |
* |
| 76 |
* @param array $args |
| 77 |
* |
| 78 |
* @return void |
| 79 |
*/ |
| 80 |
public function update( $args = [] ) { |
| 81 |
$value = wp_parse_args( $args, $this->defaults() ); |
| 82 |
|
| 83 |
update_option( self::key, $value ); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Check if a company has logo |
| 88 |
* |
| 89 |
* @return boolean |
| 90 |
*/ |
| 91 |
public function has_logo() { |
| 92 |
return (int) $this->logo; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Get the company logo |
| 97 |
* |
| 98 |
* @return string the HTML image attribute |
| 99 |
*/ |
| 100 |
public function get_logo() { |
| 101 |
$logo_id = (int) $this->logo; |
| 102 |
|
| 103 |
if ( ! $logo_id ) { |
| 104 |
$url = $this->placeholder_logo(); |
| 105 |
} else { |
| 106 |
$image = wp_get_attachment_image_src( $logo_id, 'medium' ); |
| 107 |
$url = $image[0]; |
| 108 |
} |
| 109 |
|
| 110 |
$image = sprintf( '<img src="%s" alt="%s" />', esc_url( $url ), esc_attr( $this->name ) ); |
| 111 |
|
| 112 |
return $image; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* [placeholder_logo description] |
| 117 |
* |
| 118 |
* @return string placeholder image url |
| 119 |
*/ |
| 120 |
public function placeholder_logo() { |
| 121 |
$url = WPERP_ASSETS . '/images/placeholder.png'; |
| 122 |
|
| 123 |
return apply_filters( 'erp_placeholder_image', $url ); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Get formatted address of the company |
| 128 |
* |
| 129 |
* @return string address |
| 130 |
*/ |
| 131 |
public function get_formatted_address() { |
| 132 |
$country = Countries::instance(); |
| 133 |
|
| 134 |
return $country->get_formatted_address( array( |
| 135 |
'address_1' => isset( $this->address['address_1'] ) ? $this->address['address_1'] : '', |
| 136 |
'address_2' => isset( $this->address['address_2'] ) ? $this->address['address_2'] : '', |
| 137 |
'city' => isset( $this->address['city'] ) ? $this->address['city'] : '', |
| 138 |
'state' => isset( $this->address['state'] ) ? $this->address['state'] : '', |
| 139 |
'postcode' => isset( $this->address['zip'] ) ? $this->address['zip'] : '', |
| 140 |
'country' => isset( $this->address['country'] ) ? $this->address['country'] : '' |
| 141 |
) ); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* [get_edit_url description] |
| 146 |
* |
| 147 |
* @return string the url |
| 148 |
*/ |
| 149 |
public function get_edit_url() { |
| 150 |
$url = add_query_arg( |
| 151 |
array( 'action' => 'edit' ), |
| 152 |
admin_url( 'admin.php?page=erp-company' ) |
| 153 |
); |
| 154 |
|
| 155 |
return $url; |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Check if the employee belongs to the company |
| 160 |
* |
| 161 |
* @param int employee id |
| 162 |
* |
| 163 |
* @return boolean |
| 164 |
*/ |
| 165 |
public function has_employee( $employee_id ) { |
| 166 |
return true; |
| 167 |
} |
| 168 |
|
| 169 |
public function get_locations() { |
| 170 |
$locations = new Company_Locations(); |
| 171 |
|
| 172 |
return $locations::all()->toArray(); |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* @param array $args |
| 177 |
* |
| 178 |
* @return \WP_Error |
| 179 |
*/ |
| 180 |
public function create_location( $args = [] ) { |
| 181 |
$defaults = array( |
| 182 |
'id' => 0, |
| 183 |
'name' => '', |
| 184 |
'address_1' => '', |
| 185 |
'address_2' => '', |
| 186 |
'city' => '', |
| 187 |
'state' => '', |
| 188 |
'zip' => '', |
| 189 |
'country' => '', |
| 190 |
); |
| 191 |
$fields = wp_parse_args( $args, $defaults ); |
| 192 |
$location_id = intval( $fields['id'] ); |
| 193 |
unset( $fields['id'] ); |
| 194 |
|
| 195 |
// validation |
| 196 |
if ( empty( $fields['name'] ) ) { |
| 197 |
return new \WP_Error( 'no-name', __( 'No location name provided.', 'erp' ) ); |
| 198 |
} |
| 199 |
|
| 200 |
if ( empty( $fields['address_1'] ) ) { |
| 201 |
return new \WP_Error( 'no-address_1', __( 'No address provided.', 'erp' ) ); |
| 202 |
} |
| 203 |
|
| 204 |
if ( empty( $fields['country'] ) ) { |
| 205 |
return new \WP_Error( 'no-country', __( 'No country provided.', 'erp' ) ); |
| 206 |
} |
| 207 |
|
| 208 |
$location = new Company_Locations(); |
| 209 |
|
| 210 |
if ( ! $location_id ) { |
| 211 |
$new_location = $location->create( $fields ); |
| 212 |
|
| 213 |
do_action( 'erp_company_location_new', $location->id, $fields ); |
| 214 |
|
| 215 |
return $new_location->id; |
| 216 |
|
| 217 |
} else { |
| 218 |
$location->find( $location_id )->update( $fields ); |
| 219 |
|
| 220 |
do_action( 'erp_company_location_updated', $location_id, $fields ); |
| 221 |
|
| 222 |
return $location_id; |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
|