| 1 |
<?php |
| 2 |
/** |
| 3 |
* |
| 4 |
* Plugin Name: Moloni |
| 5 |
* Plugin URI: https://plugins.moloni.com/woocommerce |
| 6 |
* Description: Send your orders automatically to your Moloni invoice software |
| 7 |
* Version: 0.0.1 |
| 8 |
* Author: Moloni.pt |
| 9 |
* Author URI: https://moloni.pt |
| 10 |
* License: GPL2 |
| 11 |
* License URI: https://www.gnu.org/licenses/gpl-2.0.html |
| 12 |
* |
| 13 |
*/ |
| 14 |
|
| 15 |
namespace Moloni\Controllers; |
| 16 |
|
| 17 |
|
| 18 |
use Moloni\Curl; |
| 19 |
use Moloni\Exceptions\APIException; |
| 20 |
use Moloni\Exceptions\GenericException; |
| 21 |
|
| 22 |
class DeliveryMethod |
| 23 |
{ |
| 24 |
public $delivery_method_id; |
| 25 |
public $name; |
| 26 |
public $value = 0; |
| 27 |
|
| 28 |
/** |
| 29 |
* Payment constructor. |
| 30 |
* @param string $name |
| 31 |
*/ |
| 32 |
public function __construct($name) |
| 33 |
{ |
| 34 |
$this->name = trim($name); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* @throws APIException |
| 39 |
*/ |
| 40 |
public function loadByName() |
| 41 |
{ |
| 42 |
$deliveryMethods = Curl::simple('deliveryMethods/getAll', []); |
| 43 |
|
| 44 |
if (!empty($deliveryMethods) && is_array($deliveryMethods)) { |
| 45 |
foreach ($deliveryMethods as $deliveryMethod) { |
| 46 |
if ($deliveryMethod['name'] === $this->name) { |
| 47 |
$this->delivery_method_id = $deliveryMethod['delivery_method_id']; |
| 48 |
|
| 49 |
return $this; |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
return false; |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
/** |
| 59 |
* Create a Payment Methods based on the name |
| 60 |
* |
| 61 |
* @return DeliveryMethod |
| 62 |
* |
| 63 |
* @throws GenericException |
| 64 |
* @throws APIException |
| 65 |
*/ |
| 66 |
public function create(): DeliveryMethod |
| 67 |
{ |
| 68 |
$insert = Curl::simple('deliveryMethods/insert', $this->mapPropsToValues()); |
| 69 |
|
| 70 |
if (isset($insert['delivery_method_id'])) { |
| 71 |
$this->delivery_method_id = $insert['delivery_method_id']; |
| 72 |
|
| 73 |
return $this; |
| 74 |
} |
| 75 |
|
| 76 |
throw new GenericException(__('Erro ao inserir a método de transporte') . $this->name); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
/** |
| 81 |
* Map this object properties to an array to insert/update a moloni Payment Value |
| 82 |
* |
| 83 |
* @return array |
| 84 |
*/ |
| 85 |
private function mapPropsToValues(): array |
| 86 |
{ |
| 87 |
$values = []; |
| 88 |
|
| 89 |
$values['name'] = $this->name; |
| 90 |
|
| 91 |
return $values; |
| 92 |
} |
| 93 |
} |
| 94 |
|