Skip to content

Instantly share code, notes, and snippets.

@Rene-Roscher
Created May 29, 2023 00:38
Show Gist options
  • Select an option

  • Save Rene-Roscher/dbb11598057eca3b35b55316ba6f6b76 to your computer and use it in GitHub Desktop.

Select an option

Save Rene-Roscher/dbb11598057eca3b35b55316ba6f6b76 to your computer and use it in GitHub Desktop.
Accounting Helper Class (connectivity sales & accounting)
<?php
namespace RServices\Helpers;
use GuzzleHttp\RequestOptions;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Storage;
use Ramsey\Uuid\Uuid;
class Rsacc
{
/**
* @return \Illuminate\Http\Client\PendingRequest
*/
static function client()
{
return \Http::withOptions([
'base_uri' => config('app.accounting.uri'),
])->withToken(config('app.accounting.token'))->withHeaders([
'Accept' => 'application/json',
]);
}
public static function createContact($firstname, $lastname, $email, $street, $number, $postcode, $city, $region, $sex, $country = 'Germany', $organisation = null)
{
return self::client()->post('contact/create', get_defined_vars())->json();
}
public static function updateContact($contact, $firstname, $lastname, $email, $street, $number, $postcode, $city, $region, $sex, $country = 'Germany', $organisation = null)
{
return self::client()->post("contact/update/$contact", \Arr::except(get_defined_vars(), 'contact'))->json();
}
public static function downloadTransaction($transaction, $disposition = 'attachment')
{
$res = self::client()->get("transaction/download/$transaction");
if ($res->serverError() || $res->status() == 404)
return false;
return response($res->body(), 200, [
'Title' => 'Test.pdf',
'Content-Type' => 'application/pdf',
'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
'Content-Disposition' => $disposition.'; filename="' . $res->header('Title') . '"',
'Pragma' => 'public',
]);
}
public function setContentDisposition(string $disposition, string $filename = '', string $filenameFallback = '')
{
if ('' === $filenameFallback && (!preg_match('/^[\x20-\x7e]*$/', $filename) || false !== strpos($filename, '%'))) {
$encoding = mb_detect_encoding($filename, null, true) ?: '8bit';
for ($i = 0, $filenameLength = mb_strlen($filename, $encoding); $i < $filenameLength; ++$i) {
$char = mb_substr($filename, $i, 1, $encoding);
if ('%' === $char || \ord($char) < 32 || \ord($char) > 126) {
$filenameFallback .= '_';
} else {
$filenameFallback .= $char;
}
}
}
$dispositionHeader = $this->headers->makeDisposition($disposition, $filename, $filenameFallback);
$this->headers->set('Content-Disposition', $dispositionHeader);
return $this;
}
public static function createTransaction($offer_contact_id, array $items, $type = 'INVOICE', $state = 'DONE')
{
return self::client()->post('transaction/create', get_defined_vars())->json();
}
public static function transactionItem(float $amount, string $description, float $unit_value = 1, string $unit = 'ITEM')
{
return get_defined_vars();
}
public static function contactList()
{
return self::client()->get('contact/list')->json();
}
public static function transactionList()
{
return self::client()->get('transaction/list')->json();
}
static function call($method, $uri, $data = null)
{
return self::client()->{$method}($uri, $data)->json();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment