Skip to content

Commit

Permalink
Added rules attribute to Invoice resource
Browse files Browse the repository at this point in the history
  • Loading branch information
xavier-stark committed May 3, 2023
1 parent dcc71ce commit 2857de3
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Given a version number MAJOR.MINOR.PATCH, increment:

## [Unreleased]
### Added
- rules attribute to Invoice resource
- Invoice.Rule sub-resource
- schedule attribute to CorporateRules resource
- purposes attribute to CorporateRules resource
- description attribute to Corporate Purchase Log
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,14 @@ $invoices = [
"due" => ((new DateTime("now"))->add(new DateInterval("P2D")))
]
],
"rules" => [
new Invoice\Rule([
"key" => "allowedTaxIds", # Set TaxIds allowed to receive this Invoice
"value" => [
"012.345.678-90"
]
])
],
"tags" => [
'War supply',
'Invoice #1234'
Expand Down
4 changes: 4 additions & 0 deletions src/invoice/invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use StarkCore\Utils\StarkDate;
use StarkBank\Utils\Rest;
use StarkBank\Invoice\Payment;
use StarkBank\Invoice\Rule;


class Invoice extends Resource
Expand All @@ -20,6 +21,7 @@ class Invoice extends Resource
public $interest;
public $discounts;
public $tags;
public $rules;
public $descriptions;
public $pdf;
public $link;
Expand Down Expand Up @@ -54,6 +56,7 @@ class Invoice extends Resource
- fine [float, default 2.0]: Invoice fine for overdue payment in %. ex: 2.5
- interest [float, default 1.0]: Invoice monthly interest for overdue payment in %. ex: 5.2
- discounts [array of dictionaries, default null]: array of dictionaries with "percentage":float and "due":DateTime or string pairs
- rules [list of Invoice.Rules, default []]: list of Invoice.Rule objects for modifying invoice behavior. ex: [Invoice.Rule(key="allowedTaxIds", value=[ "012.345.678-90", "45.059.493/0001-73" ])]
- tags [array of strings, default null]: array of strings for tagging
- descriptions [array of dictionaries, default null]: array of dictionaries with "key":string and (optional) "value":string pairs
Expand Down Expand Up @@ -83,6 +86,7 @@ function __construct(array $params)
$this->expiration = Checks::checkDateInterval(Checks::checkParam($params, "expiration"));
$this->fine = Checks::checkParam($params, "fine");
$this->interest = Checks::checkParam($params, "interest");
$this->rules = Rule::parseRules(Checks::checkParam($params, "rules"));
$this->tags = Checks::checkParam($params, "tags");
$this->descriptions = Checks::checkParam($params, "descriptions");
$this->pdf = Checks::checkParam($params, "pdf");
Expand Down
52 changes: 52 additions & 0 deletions src/invoice/rule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace StarkBank\Invoice;
use StarkCore\Utils\API;
use StarkCore\Utils\Checks;
use StarkCore\Utils\SubResource;


class Rule extends SubResource
{

public $key;
public $value;

/**
# Invoice\Rule object
The Invoice\Rule object modifies the behavior of Invoice objects when passed as an argument upon their creation.
## Parameters (required):
- key [string]: Rule to be customized, describes what Invoice behavior will be altered. ex: "allowedTaxIds"
- value [list of string]: Value of the rule. ex: ["012.345.678-90", "45.059.493/0001-73"]
*/
function __construct(array $params)
{
$this->key = Checks::checkParam($params, "key");
$this->value = Checks::checkParam($params, "value");

Checks::checkParams($params);
}

public static function parseRules($rules) {
if (is_null($rules)){
return null;
}
$parsedRules = [];
foreach($rules as $rule) {
if($rule instanceof Rule) {
array_push($parsedRules, $rule);
continue;
}
$parsedRule = function ($array) {
$ruleMaker = function ($array) {
return new Rule($array);
};
return API::fromApiJson($ruleMaker, $array);
};
array_push($parsedRules, $parsedRule($rule));
}
return $parsedRules;
}
}
8 changes: 8 additions & 0 deletions tests/invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,14 @@ public static function examples()
"due" => ((new DateTime("now", new DateTimeZone('Europe/London')))->add(new DateInterval("P2D")))->setTime(0,0,0,0)
]
],
"rules" => [
new Invoice\Rule([
"key" => "allowedTaxIds",
"value" => [
"012.345.678-90"
]
])
],
"tags" => [
'War supply',
'Invoice #1234'
Expand Down

0 comments on commit 2857de3

Please sign in to comment.