Skip to content

Commit

Permalink
Merge pull request #3 from vehrlich/master
Browse files Browse the repository at this point in the history
Invoice Split
  • Loading branch information
ViktorStiskala authored May 31, 2018
2 parents 247979a + fdb9f80 commit c2bd322
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 2 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@
"require": {
"php": ">=5.3.0"
}
}
}
1 change: 1 addition & 0 deletions src/Twisto.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
require_once TWISTO_PATH . 'Invoice.php';
require_once TWISTO_PATH . 'Item.php';
require_once TWISTO_PATH . 'ItemReturn.php';
require_once TWISTO_PATH . 'ItemSplit.php';
require_once TWISTO_PATH . 'ItemDiscountReturn.php';
require_once TWISTO_PATH . 'Order.php';
require_once TWISTO_PATH . 'ShortAddress.php';
Expand Down
29 changes: 29 additions & 0 deletions src/Twisto/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ class Invoice
/** @var string */
public $invoice_id;

/** @var string **/
public $parent_invoice_id;

/** @var string */
public $eshop_invoice_id;

Expand Down Expand Up @@ -68,6 +71,7 @@ public function get()
$this->deserialize($data);
}


/**
* Perform cancel invoice API request
*/
Expand Down Expand Up @@ -118,6 +122,7 @@ public static function create(Twisto $twisto, $transaction_id, $eshop_invoice_id
return $invoice;
}


/**
* Perform invoice return API request
* @param ItemReturn[] $items
Expand Down Expand Up @@ -150,11 +155,35 @@ public function returnAll()
$this->deserialize($data);
}


/**
* Split invoice to new one
* @param ItemSplit[] $items
* @return JSON response with new invoice
*/
public function splitItems($items)
{
$data = array(
'items' => array_map(function(ItemSplit $item) {
return $item->serialize();
}, $items)
);

$data = $this->twisto->requestJson('POST', 'invoice/' . urlencode($this->invoice_id) . '/split/', $data);
$split_invoice = new Invoice($this->twisto, null);
$split_invoice->deserialize($data);
$this->get();

return $split_invoice;
}


private function deserialize($data)
{
$this->invoice_id = $data['invoice_id'];
$this->eshop_invoice_id = $data['eshop_invoice_id'];
$this->customer_email = $data['customer_email'];
$this->parent_invoice_id = $data['parent_invoice_id'];

if ($data['billing_address']['type'] == BaseAddress::TYPE_SHORT) {
$this->billing_address = ShortAddress::deserialize($data['billing_address']);
Expand Down
32 changes: 32 additions & 0 deletions src/Twisto/ItemSplit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
namespace Twisto;

class ItemSplit
{
/** @var string */
public $product_id;

/** @var int */
public $quantity;

/**
* @param string $product_id
* @param int $quantity
*/
function __construct($product_id, $quantity)
{
$this->product_id = $product_id;
$this->quantity = (int)$quantity;
}

/**
* @return array
*/
public function serialize()
{
return array(
'product_id' => $this->product_id,
'quantity' => $this->quantity
);
}
}
1 change: 0 additions & 1 deletion src/Twisto/Twisto.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ public function request($method, $url, $data = null)
if (curl_errno($curl)) {
throw new Error('Curl error: ' . curl_error($curl));
}

$info = curl_getinfo($curl);
if ($info['http_code'] != 200) {
throw new Error('API responded with wrong status code (' . $info['http_code'] . ')', json_decode($response));
Expand Down

0 comments on commit c2bd322

Please sign in to comment.