Skip to content

Commit

Permalink
Added customer endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
J. Marcelo Aviles Paco authored and J. Marcelo Aviles Paco committed Feb 3, 2023
1 parent a16c276 commit d538b8c
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 3 deletions.
53 changes: 53 additions & 0 deletions src/Classes/Cliente.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
namespace SinticBolivia\MonoInvoicesApi\Classes;

class Cliente extends SBObject
{
public $customer_id;
public $code;
public $first_name;
public $last_name;
public $identity_document;
public $phone;
public $email;
public $address_1;
public $meta = [];

public function __construct()
{

}
public function setNitRucNif($nit_ruc_nif)
{
$this->setMeta('_nit_ruc_nif', $nit_ruc_nif);
}
public function setNombreFacturacion($nombre)
{
$this->setMeta('_billing_name', $nombre);
}
public function getNitRucNif()
{
return $this->getMeta('_nit_ruc_nif');
}
public function getNombreFacturacion()
{
return $this->getMeta('_billing_name');
}
/**
* Asigna datos adicionales para el cliente
*
* @param string $key nombre del dato
* @param mixed $value valor del dato
*/
public function setMeta($key, $value)
{
$this->meta[$key] = $value;
}
public function getMeta($key, $defVal = null)
{
if( !isset($this->meta[$key]) )
return $defVal;

return $this->meta[$key];
}
}
24 changes: 23 additions & 1 deletion src/Classes/Factura.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?php
namespace SinticBolivia\MonoInvoicesApi\Classes;

class Factura
class Factura extends SBObject
{
const FACTURA_CON_CREDITO_FISCAL = 1;
const FACTURA_SIN_CREDITO_FISCAL = 2;

public $invoice_id;
public $customer_id;
public $customer;
public $nit_ruc_nif;
Expand All @@ -21,11 +22,32 @@ class Factura
public $complemento;
public $numero_tarjeta;
public $tipo_factura_documento = 1;
public $subtotal;
public $total;
public $invoice_number;
public $control_code;
public $invoice_date_time;
public $status;
public $cufd;
public $cuf;
public $evento_id;
public $tipo_emision;
public $nit_emisor;
public $siat_id;
public $leyenda;
public $siat_url;
public $data;

/**
* @var FacturaItem[]
*/
public $items = [];

public function __construct()
{

}

}


7 changes: 6 additions & 1 deletion src/Classes/FacturaItem.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
namespace SinticBolivia\MonoInvoicesApi\Classes;

class FacturaItem
class FacturaItem extends SBObject
{
public $product_id = 0;
public $product_code;
Expand All @@ -13,4 +13,9 @@ class FacturaItem
public $codigo_producto_sin;
public $codigo_actividad;
public $total;

public function __construct()
{

}
}
58 changes: 57 additions & 1 deletion src/Classes/MonoInvoicesApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
class MonoInvoicesApi
{
public $server;
public $version = '1.0.0';
public $version = '1.0.3';
public $baseUrl;
public $token;

Expand Down Expand Up @@ -158,4 +158,60 @@ public function cerrarEvento(int $id)
throw new ExceptionApi('Error anulando factura', $res);
return $res->json();
}
/**
*
* @param int $id
* @param string $tpl Plantilla de la factura pagina|rollo
* @throws ExceptionApi
* @return mixed
*/
public function obtenerPdf(int $id, $tpl = null)
{
$this->validateToken();
$endpoint = '/invoices/'. $id .'/pdf';
if( $tpl )
$endpoint .= '?tpl=' . $tpl;

$res = $this->getRequest()->get($endpoint);
if( $res->statusCode != 200 )
throw new ExceptionApi('Error obtiendo el PDF de la factura', $res);
return $res->json();
}
/**
*
* @param Cliente $cliente
* @throws ExceptionApi
* @return \SinticBolivia\MonoInvoicesApi\Classes\Cliente
*/
public function crearCliente(Cliente $cliente)
{
$this->validateToken();
$endpoint = '/customers';
$data = json_encode($client);
$res = $this->getRequest()->post($endpoint, $data);
if( $res->statusCode != 200 )
throw new ExceptionApi('Error creando el cliente', $res);

$cliente->bind($res->json());

return $cliente;
}
/**
*
* @param int $id
* @throws ExceptionApi
* @return \SinticBolivia\MonoInvoicesApi\Classes\Cliente
*/
public function obtenerCliente(int $id)
{
$this->validateToken();
$endpoint = '/customers/' . $id;
$res = $this->getRequest()->get($endpoint);
if( $res->statusCode != 200 )
throw new ExceptionApi('Error obtiendo el PDF de la factura', $res);
$cliente = new Cliente();
$cliente->bind($res->json());

return $cliente;
}
}
17 changes: 17 additions & 0 deletions src/Classes/SBObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
namespace SinticBolivia\MonoInvoicesApi\Classes;

class SBObject
{
public function bind($data)
{
if( !is_array($data) && !is_object($data) )
return false;
foreach($data as $prop => $val)
{
if( !property_exists($this, $prop) )
continue;
$this->$prop = $val;
}
}
}

0 comments on commit d538b8c

Please sign in to comment.