getMessage()); $this->lastError = $ex->getMessage(); return false; } } } class PolkurierModel implements ArrayAccess, JsonSerializable { public $params = []; /** * @return array */ public function toArray() { $data = array(); foreach ($this->params as $field => $value) { if (is_array($value) || is_object($value)) { $data[$field] = json_encode($value); } else { $data[$field] = $value; } } return $data; } /** * @param $params * @return $this */ public function fromArray($params) { foreach ($params as $key => $val) { $this->__set($key, $val); } return $this; } /** * @return int */ public function getId() { return (int)$this->id; } /** * @param $name * @param $value */ public function __set($name, $value) { if (array_key_exists($name, $this->params)) { $this->params[$name] = $value; } else { $this->params['extra'][$name] = $value; } } /** * @param $name * @return mixed|null */ public function __get($name) { if (array_key_exists($name, $this->params)) { return $this->params[$name]; } else if (array_key_exists($name, $this->params['extra'])) { return $this->params['extra'][$name]; } return null; } #[\ReturnTypeWillChange] public function offsetExists($offset) { return array_key_exists($offset, $this->params) || array_key_exists($offset, $this->params['extra']); } #[\ReturnTypeWillChange] public function offsetGet($offset) { return $this->__get($offset); } #[\ReturnTypeWillChange] public function offsetSet($offset, $value) { return $this->__set($offset, $value); } #[\ReturnTypeWillChange] public function offsetUnset($offset) { } #[\ReturnTypeWillChange] public function jsonSerialize() { return $this->toArray(); } } class PolkurierAddress implements JsonSerializable { public $id; public $address = [ "company" => null, "person" => null, "street" => null, "housenumber" => null, "flatnumber" => null, "postcode" => null, "city" => null, "country" => null, "email" => null, "phone" => null, "account_id" => null, "machine_name" => null, "point_name" => null, ]; public function __toString() { $msg = ""; $msg .= $this->address["company"] . "
"; $msg .= $this->address["person"] . "
"; $msg .= $this->address["street"] . " " . $this->address["housenumber"] . " " . $this->address["flatnumber"] . "
"; $msg .= $this->address["postcode"] . " " . $this->address["city"] . " " . $this->address['country'] . "
"; $msg .= $this->address["phone"] . " " . $this->address["email"]; if (!empty($this->address['point_name'])) { $msg .= 'Punkt: ' . $this->address["point_name"]; } $msg .= ""; return $msg; } public function asTableRow(array $excludedRows = []) { $row = ''; if (!in_array('company', $excludedRows)) { $row .= sprintf("%s", $this->address["company"]); } if (!in_array('person', $excludedRows)) { $row .= sprintf("%s", $this->address["person"]); } if (!in_array('address', $excludedRows)) { $row .= sprintf("%s", $this->address["street"] . " " . $this->address["housenumber"] . " " . $this->address["flatnumber"]); } if (!in_array('location', $excludedRows)) { $row .= sprintf("%s", $this->address["postcode"] . " " . $this->address["city"] . " " . $this->address["country"]); } if (!in_array('phone', $excludedRows)) { $row .= sprintf("%s", $this->address["phone"]); } if (!in_array('email', $excludedRows)) { $row .= sprintf("%s", $this->address["email"]); } if (!in_array('account_id', $excludedRows)) { $row .= sprintf("%s", $this->address["account_id"]); } return $row; } public function toArray() { $data = $this->address; $data['id'] = $this->id; return $data; } #[\ReturnTypeWillChange] public function jsonSerialize() { return $this->toArray(); } public function fromArray($array) { $this->id = (int)$array['id']; foreach ($array as $key => $val) { if (array_key_exists($key, $this->address)) { $this->address[$key] = $val; } } } public static function getAddressById($id) { $opt = get_option('polkurier_addresses'); $opt[$id]->id = $id; return $opt[$id]; } public static function getCoverAddressById($id) { $opt = get_option('polkurier_cover_addresses'); if ($opt[$id] !== null) { $opt[$id]->id = $id; return $opt[$id]; } return null; } public function renderForm() { if ($this->id || $this->id == 0) { echo ''; } ?>

Opcja dostępna dla klientów nadających przesyłki za pośrednictwem przewoźnika Geis oraz posiadających aktywną opcje stałych odbiorów. ID konta należy pozyskać kontaktując się z biurem obsługi klienta serwisu polkurier.pl.
account; } public function asTableRow() { return sprintf("%s%s", $this->alias, $this->account ); } } class PolkurierOrder extends PolkurierModel { public $params = [ 'id' => null, 'wp_order_id' => null, "order_number" => '', "label" => null, "price_gross" => 0, "price_net" => 0, "carrier" => '', "address" => null, "address_to" => null, "pobranie" => 0, 'status_code' => '', "extra" => [], 'date_created' => null, ]; /** * @param $response * @param $carrier * @param $address * @param $address_to * @param $pobranie * @param $extra * @return PolkurierOrder */ public static function createFromApiResponse($response, $carrier, $address, $address_to, $pobranie, $extra) { $order = new PolkurierOrder(); $order->order_number = (string)$response['order_number']; $order->label = (array)$response['label']; $order->price_gross = (float)$response['price_gross']; $order->price_net = (float)$response['price_net']; $order->carrier = (string)$carrier; $order->address = $address; $order->address_to = $address_to; $order->pobranie = (float)$pobranie; $order->extra = (array)$extra; $order->date_created = new DateTime(); return $order; } /** * @return $this */ public function save() { global $wpdb; if (!empty($this->getId())) { $wpdb->update($wpdb->prefix . 'polkurier_orders', $this->toArray(), [ 'id' => $this->getId() ]); } else { $this->wp_order_id = $wpdb->insert($wpdb->prefix . 'polkurier_orders', $this->toArray()); } return $this; } /** * @param $params * @return $this */ public function fromArray($params) { $this->id = (int)$params['id']; $this->wp_order_id = (int)$params['wp_order_id']; $this->order_number = (string)$params['order_number']; $this->label = json_decode($params['label'], true); $this->price_gross = (float)$params['price_gross']; $this->price_net = (float)$params['price_net']; $this->carrier = $params['carrier']; $this->address = new PolkurierAddress(); $this->address->fromArray(json_decode($params['address'], true)); $this->address_to = new PolkurierAddress(); $this->address_to->fromArray(json_decode($params['address_to'], true)); $this->pobranie = (float)$params['pobranie']; $this->status_code = $params['status_code']; $this->extra = json_decode($params['extra'], true); $this->date_created = \Polkurier\Util\Dates::dateTimeOrNull($params['date_created']); return $this; } public function toArray() { $data = parent::toArray(); $data['date_created'] = $this->date_created ? $this->date_created->format(\Polkurier\Util\Dates::MYSQL_DATETIME) : date(\Polkurier\Util\Dates::MYSQL_DATETIME); return $data; } /** * @param $id * @return PolkurierOrder|null */ public static function getOrderByWCOrderId($id) { return PolkurierOrder::getOrderBy('wp_order_id', (int)$id); } /** * @param $id * @return PolkurierOrder|null */ public static function getOrderById($id) { return PolkurierOrder::getOrderBy('id', (int)$id); } /** * @param $orderNumber * @return PolkurierOrder|null */ public static function getOrderByNumber($orderNumber) { return PolkurierOrder::getOrderBy('order_number', $orderNumber); } /** * @param string $field * @param string $value * @return PolkurierOrder|null */ public static function getOrderBy($field, $value) { global $wpdb; $data = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->prefix}polkurier_orders WHERE `$field` = %s", $value), ARRAY_A); if (!empty($data)) { $order = new PolkurierOrder(); $order->fromArray($data); return $order; } return null; } /** * @param $id * @return bool */ public static function hasOrderDefined($id) { return self::getOrderByWCOrderId($id) !== null; } /** * @param array $where * @return string */ private static function createWhere(array $where) { $searchQuery = ''; if (isset($where['searchQuery'])) { $searchQuery = trim($where['searchQuery']); unset($where['searchQuery']); } $query = \Polkurier\Util\Database::createWhere($where); if (!empty($searchQuery)) { if (!empty($query)) { $query = "($query) AND "; } $query .= \Polkurier\Util\Database::createSearchWhere($searchQuery, array( 'order_number', 'label', 'address', 'address_to', )); } return $query; } /** * @param array $where * @param string|array $order * @param null $limit * @param null $offset * @return PolkurierOrder[] */ public static function getAll($where = array(), $order = null, $limit = null, $offset = null) { global $wpdb; $data = []; $query = "SELECT * FROM {$wpdb->prefix}polkurier_orders"; $whereString = self::createWhere($where); if (!empty($whereString)) { $query .= ' WHERE ' . $whereString; } if (!empty($order)) { $col = is_array($order) ? $order[0] : $order; $dir = strtoupper(is_array($order) ? $order[1] : 'ASC'); $dir = $dir === 'DESC' ? 'DESC' : 'ASC'; $query .= " ORDER BY `$col` $dir "; } $limit = (int)$limit; $offset = (int)$offset; if (!empty($limit)) { $query .= ' LIMIT ' . $limit; if (!empty($offset)) { $query .= ' OFFSET ' . $offset; } } foreach ($wpdb->get_results($query, ARRAY_A) as $row) { $polkurierOrder = new PolkurierOrder(); $polkurierOrder->fromArray($row); $data[] = $polkurierOrder; } return $data; } /** * @param array $where * @return int */ public static function countAll($where = array()) { global $wpdb; $query = "SELECT COUNT(*) as c FROM {$wpdb->prefix}polkurier_orders"; $whereString = self::createWhere($where); if (!empty($whereString)) { $query .= ' WHERE ' . $whereString; } $data = $wpdb->get_results($query, ARRAY_A); return (int)$data[0]['c']; } /** * @param $id */ public static function deleteByOrderNumber($orderNumber) { global $wpdb; $wpdb->delete($wpdb->prefix . 'polkurier_orders', ['order_number' => $orderNumber]); } public function getDateCreated() { if (!($this->date_created instanceof DateTime)) { $this->date_created = new DateTime('1970-01-01'); } return $this->date_created; } }