593 lines
17 KiB
PHP
593 lines
17 KiB
PHP
<?php
|
|
|
|
/**
|
|
* @deprecated since 1.5.0 use \Polkurier\ApiClient
|
|
* Class PolkurierAPI
|
|
*/
|
|
class PolkurierAPI extends \Polkurier\ApiClient
|
|
{
|
|
/**
|
|
* @param $method
|
|
* @param array $params
|
|
* @return array|bool|mixed
|
|
*/
|
|
protected function call($method, $params = array())
|
|
{
|
|
try {
|
|
return parent::call($method, $params);
|
|
} catch (\Polkurier\Exception\ApiException $ex) {
|
|
Polkurier_Admin::api_error_notice($ex->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"] . "<br/>";
|
|
$msg .= $this->address["person"] . "<br/>";
|
|
$msg .= $this->address["street"] . " " . $this->address["housenumber"] . " " . $this->address["flatnumber"] . "<br/>";
|
|
$msg .= $this->address["postcode"] . " " . $this->address["city"] . " " . $this->address['country'] . "<br/>";
|
|
$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("<td>%s</td>", $this->address["company"]);
|
|
}
|
|
|
|
if (!in_array('person', $excludedRows)) {
|
|
$row .= sprintf("<td>%s</td>", $this->address["person"]);
|
|
}
|
|
|
|
if (!in_array('address', $excludedRows)) {
|
|
$row .= sprintf("<td>%s</td>", $this->address["street"] . " " . $this->address["housenumber"] . " " . $this->address["flatnumber"]);
|
|
}
|
|
|
|
if (!in_array('location', $excludedRows)) {
|
|
$row .= sprintf("<td>%s</td>", $this->address["postcode"] . " " . $this->address["city"] . " " . $this->address["country"]);
|
|
}
|
|
|
|
if (!in_array('phone', $excludedRows)) {
|
|
$row .= sprintf("<td>%s</td>", $this->address["phone"]);
|
|
}
|
|
|
|
if (!in_array('email', $excludedRows)) {
|
|
$row .= sprintf("<td>%s</td>", $this->address["email"]);
|
|
}
|
|
|
|
if (!in_array('account_id', $excludedRows)) {
|
|
$row .= sprintf("<td>%s</td>", $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 '<input type="hidden" name="polkurier_id" value="' . $this->id . '"/>';
|
|
}
|
|
?>
|
|
<table class="form-table">
|
|
<tbody>
|
|
<tr>
|
|
<th scope="row">
|
|
<label for="polkurier_company_name">Nazwa firmy</label></th>
|
|
<td><input required type="text" name="polkurier_company_name" id="polkurier_company_name"
|
|
value="<?php echo $this->address['company'] ?>"></td>
|
|
</tr>
|
|
<tr>
|
|
<th scope="row"><label for="polkurier_name_surname_cb">Imię i nazwisko</label></th>
|
|
<td><input required type="text" name="polkurier_name_surname" id="polkurier_name_surname"
|
|
value="<?php echo $this->address['person'] ?>"></td>
|
|
</tr>
|
|
<tr>
|
|
<th scope="row"><label for="polkurier_street_cb">Ulica</label></th>
|
|
<td><input required type="text" name="polkurier_street" id="polkurier_street"
|
|
value="<?php echo $this->address['street'] ?>"></td>
|
|
</tr>
|
|
<tr>
|
|
<th scope="row"><label for="polkurier_number_1_cb">Numer budynku</label></th>
|
|
<td><input required type="text" name="polkurier_number_1" id="polkurier_number_1"
|
|
value="<?php echo $this->address['housenumber'] ?>"></td>
|
|
</tr>
|
|
<tr>
|
|
<th scope="row"><label for="polkurier_number_2_cb">Numer lokalu</label></th>
|
|
<td><input type="text" name="polkurier_number_2" id="polkurier_number_2"
|
|
value="<?php echo $this->address['flatnumber'] ?>"></td>
|
|
</tr>
|
|
<tr>
|
|
<th scope="row"><label for="polkurier_postal_code_cb">Kod pocztowy</label></th>
|
|
<td><input required type="text" pattern="[0-9]{2}-[0-9]{3}" title="Kod pocztowy musi być w formie xx-xxx"
|
|
name="polkurier_postal_code" id="polkurier_postal_code"
|
|
value="<?php echo $this->address['postcode'] ?>"></td>
|
|
</tr>
|
|
<tr>
|
|
<th scope="row"><label for="polkurier_city_cb">Miasto</label></th>
|
|
<td><input required type="text" name="polkurier_city" id="polkurier_city"
|
|
value="<?php echo $this->address['city'] ?>"></td>
|
|
</tr>
|
|
<tr>
|
|
<th scope="row"><label for="polkurier_phone_cb">Numer telefonu</label></th>
|
|
<td><input required type="text" pattern="[0-9]{9}" title="Podaj dziewięciocyfrowy numer telefonu"
|
|
name="polkurier_phone" id="polkurier_phone" value="<?php echo $this->address['phone'] ?>"></td>
|
|
</tr>
|
|
<tr>
|
|
<th scope="row"><label for="polkurier_email_cb">Adres e-mail</label></th>
|
|
<td><input required type="email" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$"
|
|
title="Podaj poprawny adres e-mail" name="polkurier_email" id="polkurier_email"
|
|
value="<?php echo $this->address['email'] ?>"></td>
|
|
</tr>
|
|
<tr>
|
|
<th scope="row"><label for="polkurier_polkurier_account_id_cb">ID konta Polkurier</label></th>
|
|
<td><input type="text" name="polkurier_polkurier_account_id" id="polkurier_polkurier_account_id"
|
|
value="<?php echo $this->address['account_id'] ?>"> <br/><em>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.</em></td>
|
|
</tr>
|
|
</tbody></table><?php
|
|
}
|
|
}
|
|
|
|
class PolkurierBankAccount
|
|
{
|
|
public $account = null;
|
|
public $alias = null;
|
|
|
|
public function __toString()
|
|
{
|
|
return (string)$this->account;
|
|
}
|
|
|
|
public function asTableRow()
|
|
{
|
|
return sprintf("<td>%s</td><td>%s</td>",
|
|
$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;
|
|
}
|
|
|
|
}
|