1. Go to this page and download the library: Download vgspedro/moloniapi library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
vgspedro / moloniapi example snippets
namespace App\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use App\Service\InvoiceMoloni;
class InvoicingController extends AbstractController
{
public function index(InvoiceMoloni $moloni)
{
return $this->render('admin/payment/native.html', [
//Call the methods from the Service $moloni;
'moloni' => [
'moloni_get_product_categories' => $moloni->getProductCategories(0), //ok
'moloni_get_document_sets' => $moloni->getDocumentSets(),
'moloni_get_customers' => $moloni->getCustomers(),
'moloni_get_payments' => $moloni->getPaymentMethods(),
'moloni_get_products' => $moloni->getProducts(3137658), // ok
'moloni_get_taxes' => $moloni->getTaxes(),
'moloni_get_maturity_dates' => $moloni->getMaturityDates(), //ok
'moloni_doc' => $moloni->getInvoiceReceipt($this->ir_id()),
//'moloni_set_ir' => $moloni->setInvoiceReceipt($this->ir()), //ok
],
]);
}
}
private function ir(){
$products[] = [ // array
];
$payments[] = [ //array
namespace App\Service;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use \VgsPedro\MoloniApi\Authentication;
use \VgsPedro\MoloniApi\Classes\Customer;
use \VgsPedro\MoloniApi\Classes\DeliveryMethods;
use \VgsPedro\MoloniApi\Classes\GlobalData;
use \VgsPedro\MoloniApi\Classes\MaturityDates;
use \VgsPedro\MoloniApi\Classes\MeasurementUnits;
use \VgsPedro\MoloniApi\Classes\PaymentMethods;
use \VgsPedro\MoloniApi\Classes\Product;
use \VgsPedro\MoloniApi\Classes\ProductCategories;
use \VgsPedro\MoloniApi\Classes\Taxes;
use \VgsPedro\MoloniApi\Classes\Documents;
use \VgsPedro\MoloniApi\Classes\InvoiceReceipts;
use \VgsPedro\MoloniApi\Classes\Suppliers;
use \VgsPedro\MoloniApi\Classes\DocumentSets;
use \VgsPedro\MoloniApi\Classes\IdentificationTemplates;
class InvoiceMoloni
{
private $credencials;
private $session;
public function __construct(ParameterBagInterface $environment, SessionInterface $session){
$this->credencials = [];
$this->session = $session;
if($environment->get("kernel.environment") == 'prod'){
$this->credencials['company_id'] = 5 ; //Change according to specific user // Company ID, Provided by Moloni
$this->credencials['url'] = 'https://api.moloni.pt/v1'; // Url to make request, sandbox or live (sandbox APP_ENV=dev or test) (live APP_ENV=prod)
}
else{
$this->credencials['company_id'] = 5 ; //Change according to specific user // Company ID, Provided by Moloni
$this->credencials['url'] = 'https://api.moloni.pt/sandbox'; // Url to make request, sandbox or live (sandbox APP_ENV=dev or test) (live APP_ENV=prod)
}
$this->credencials['client_id'] = ''; // Client ID, Provided by Moloni
$this->credencials['client_secret'] = ''; // Client Secret, Provided by Moloni
$this->credencials['opendoc'] = true; // On generate invoice set to provisory or definitiv
$this->credencials['username'] = '[email protected]'; // Username, that allows access to Moloni (login page)
$this->credencials['password'] = 'pass23'; // Password, that allows access to Moloni (login page)
$this->credencials['token']['access_token'] = $this->session->get('access_token');
$this->credencials['token']['expires_in'] = $this->session->get('expires_in');
$this->credencials['token']['refresh_token'] = $this->session->get('refresh_token');
}
/**
* Create a new access token or use the existing one if valid
* @return boolean
* https://www.moloni.pt/dev
**/
public function start(){
$now = new \DateTime();
//Access token expired or not
if($this->credencials['token']['access_token'] && $this->credencials['token']['expires_in'] > $now->format('U'))
return true;
//Access token expired get a new one
$auth = new Authentication();
$auth->setClientId($this->credencials['client_id']);
$auth->setPassword($this->credencials['password']);
$auth->setUsername($this->credencials['username']);
$auth->setClientSecret($this->credencials['client_secret']);
$auth->setCompanyId($this->credencials['company_id']);
$auth->setUrl($this->credencials['url']);
$token = $auth->login();
if($token['status'] == 0)
return null;
$this->session->set('access_token', $token['data']->access_token);
//Removed 5 seconds from the current expire value ( 3600 - 5)
//The session expires_in in seconds
$this->session->set('expires_in', $now->format('U') + $token['data']->expires_in - 5 );
$this->session->set('refresh_token', $token['data']->refresh_token);
//Set the values on the array, on 1st request is neeeded
$this->credencials['token']['access_token'] = $this->session->get('access_token');
$this->credencials['token']['expires_in'] = $this->session->get('expires_in');
$this->credencials['token']['refresh_token'] = $this->session->get('refresh_token');
return true;
}
#####
## TAXES METHODS
#####
/**
* List Taxes of Company
* @return json
* https://www.moloni.pt/dev/index.php?action=getApiDocDetail&id=262
**/
public function getTaxes()
{
if($this->start()){
$t = new Taxes();
$t->setCompanyId($this->credencials['company_id']);
$t->setAccessToken($this->credencials['token']['access_token']);
$t->setUrl($this->credencials['url']);
return $t->getAll();
}
else
return false;
}
/**
* Create Tax in the Company
* @param array $t tax information
* @return json
* https://www.moloni.pt/dev/index.php?action=getApiDocDetail&id=263
**/
public function setTax(array $t = [])
{
if($this->start()){
$t = new Taxes();
$t->setCompanyId($this->credencials['company_id']);
$t->setAccessToken($this->credencials['token']['access_token']);
$t->setUrl($this->credencials['url']);
$t->setName($t['name']);
$t->setValue($t['value']);
$t->setType($t['type']);
$t->setSaftType($t['saft_type']);
$t->setVatType($t['vat_type']);
$t->setStampTax($t['stamp_tax']);
$t->setExemptionReason($t['exemption_reason']);
$t->setFiscalZone($t['fiscal_zone']);
$t->setActivByDefault($t['active_by_default']);
return $t->insert();
}
else
return false;
}
/**
* Update Tax by Id
* @param array $t Tax information // $this->getTaxes()
* @return json
* https://www.moloni.pt/dev/index.php?action=getApiDocDetail&id=264
**/
public function updateTax(array $t = [])
{
if($this->start()){
$t = new Taxes();
$t->setCompanyId($this->credencials['company_id']);
$t->setAccessToken($this->credencials['token']['access_token']);
$t->setUrl($this->credencials['url']);
$t->setId($t['tax_id']);
$t->setName($t['name']);
$t->setValue($t['value']);
$t->setType($t['type']);
$t->setSaftType($t['saft_type']);
$t->setVatType($t['vat_type']);
$t->setStampTax($t['stamp_tax']);
$t->setExemptionReason($t['exemption_reason']);
$t->setFiscalZone($t['fiscal_zone']);
$t->setActivByDefault($t['active_by_default']);
return $t->update();
}
else
return false;
}
/**
* Delete a Tax from the Company
* @param int $tax_id // $this->getTaxes()
* @return json
* https://www.moloni.pt/dev/index.php?action=getApiDocDetail&id=265
**/
public function delete(int $tax_id = 0)
{
if($this->start()){
$t = new Taxes();
$t->setCompanyId($this->credencials['company_id']);
$t->setAccessToken($this->credencials['token']['access_token']);
$t->setUrl($this->credencials['url']);
$t->setId($t['tax_id']);
return $t->delete();
}
else
return false;
}
#####
## GLOBALDATA METHODS
#####
/**
* List Countries available in Moloni
* @return json
* https://www.moloni.pt/dev/index.php?action=getApiDocSub&s_id=68
**/
public function getCountries()
{
if($this->start()){
$g = new GlobalData();
$g->setAccessToken($this->credencials['token']['access_token']);
$g->setUrl($this->credencials['url']);
return $g->getCountries();
}
else
return false;
}
/**
* List Languages available in Moloni
* @return json
* https://www.moloni.pt/dev/index.php?action=getApiDocSub&s_id=70
**/
public function getLanguages()
{
if($this->start()){
$g = new GlobalData();
$g->setAccessToken($this->credencials['token']['access_token']);
$g->setUrl($this->credencials['url']);
return $g->getLanguages();
}
else
return false;
}
/**
* List Currencies available in Moloni
* @return json
* https://www.moloni.pt/dev/index.php?action=getApiDocSub&s_id=101
**/
public function getCurrencies()
{
if($this->start()){
$g = new GlobalData();
$g->setAccessToken($this->credencials['token']['access_token']);
$g->setUrl($this->credencials['url']);
return $g->getCurrencies();
}
else
return false;
}
/**
* List of Fiscal Zones available in Moloni
* @param int $id country_id // $this->getCountries()
* @return json
* https://www.moloni.pt/dev/index.php?action=getApiDocSub&s_id=69
**/
public function getFiscalZones(int $id = 0)
{
if($this->start()){
$g = new GlobalData();
$g->setAccessToken($this->credencials['token']['access_token']);
$g->setUrl($this->credencials['url']);
$g->setCountryId($id);
return $g->getFiscalZones();
}
else
return false;
}
/**
* List Tax Exemptions available in Moloni
* @return json
* https://www.moloni.pt/dev/index.php?action=getApiDocSub&s_id=261
**/
public function getTaxExemptions()
{
if($this->start()){
$g = new GlobalData();
$g->setAccessToken($this->credencials['token']['access_token']);
$g->setUrl($this->credencials['url']);
return $g->getTaxExemptions();
}
else
return false;
}
#####
## CUSTOMERS METHODS
#####
/**
* Count Customers of the Company
* @return json
* https://www.moloni.pt/dev/index.php?action=getApiDocDetail&id=306
**/
public function getCustomerCount()
{
if($this->start()){
$c = new Customer();
$c->setAccessToken($this->credencials['token']['access_token']);
$c->setUrl($this->credencials['url']);
$c->setCompanyId($this->credencials['company_id']);
return $c->getCounter();
}
else
return false;
}
/**
* List Customers of the Company
* @return json
* https://www.moloni.pt/dev/index.php?action=getApiDocDetail&id=306
**/
public function getCustomers()
{
if($this->start()){
$c = new Customer();
$c->setAccessToken($this->credencials['token']['access_token']);
$c->setUrl($this->credencials['url']);
$c->setCompanyId($this->credencials['company_id']);
return $c->getAll();
}
else
return false;
}
/**
* Get Customer by Id
* @param int $id Customer Id
* @return json
* https://www.moloni.pt/dev/index.php?action=getApiDocDetail&id=199
**/
public function getCustomerById(int $id = 0)
{
if($this->start()){
$c = new Customer();
$c->setAccessToken($this->credencials['token']['access_token']);
$c->setUrl($this->credencials['url']);
$c->setCompanyId($this->credencials['company_id']);
$c->setId($id);
return $c->getById();
}
else
return false;
}
/**
* Get Customer by Vat
* @param string $vat Customer Vat // '123456789'
* @return json
* https://www.moloni.pt/dev/index.php?action=getApiDocDetail&id=201
**/
public function getCustomerByVat(string $vat = null)
{
if($this->start()){
$c = new Customer();
$c->setAccessToken($this->credencials['token']['access_token']);
$c->setUrl($this->credencials['url']);
$c->setCompanyId($this->credencials['company_id']);
$c->setVat($vat);
return $c->getByVat();
}
else
return false;
}
/**
* Update Customer by Id
* @param array $a Customer information
* @return json
* https://www.moloni.pt/dev/index.php?action=getApiDocDetail&id=205
**/
public function updateCustomerById(array $a = [])
{
if($this->start()){
$c = new Customer();
$c->setAccessToken($this->credencials['token']['access_token']);
$c->setUrl($this->credencials['url']);
$c->setCompanyId($this->credencials['company_id']);
$c->setId($a['id']);
$c->setVat($a['vat']);
$c->setNumber($a['number']);
$c->setName($a['name']);
$c->setLanguageId($a['language_id']);
$c->setAddress($a['address']);
$c->setZipCode($a['zip_code']);
$c->setCity($a['city']);
$c->setCountryId($a['country_id']);
$c->setEmail($a['email']);
$c->setWebsite($a['website']);
$c->setPhone($a['phone']);
$c->setFax($a['fax']);
$c->setContactName($a['contact_name']);
$c->setContactEmail($a['contact_email']);
$c->setContactPhone($a['contact_phone']);
$c->setNotes($a['notes']);
$c->setSalesmanId($a['salesman_id']);
$c->setPriceClassId($a['price_class_id']);
$c->setMaturityDateId($a['maturity_date_id']);
$c->setPaymentDay($a['payment_day']);
$c->setDiscount($a['discount']);
$c->setCreditLimit($a['credit_limit']);
$c->setCopiesDocumentTypeId($a['copies']['document_type_id']);
$c->setCopiesCopies($a['copies']['copies']);
$c->setPaymentMethodId($a['payment_method_id']);
$c->setDeliveryMethodId($a['delivery_method_id']);
$c->setFieldNotes($a['field_notes']);
return $c->update();
}
else
return false;
}
/**
* Create Customer in the Company
* @param array $a Customer information
* @return json
* https://www.moloni.pt/dev/index.php?action=getApiDocDetail&id=204
**/
public function setCustomer(array $a = []){
if($this->start()){
$c = new Customer();
$c->setAccessToken($this->credencials['token']['access_token']);
$c->setUrl($this->credencials['url']);
$c->setCompanyId($this->credencials['company_id']);
$c->setVat($a['vat']);
$c->setNumber($a['number']);
$c->setName($a['name']);
$c->setLanguageId($a['language_id']);
$c->setAddress($a['address']);
$c->setZipCode($a['zip_code']);
$c->setCity($a['city']);
$c->setCountryId($a['country_id']);
$c->setEmail($a['email']);
$c->setWebsite($a['website']);
$c->setPhone($a['phone']);
$c->setFax($a['fax']);
$c->setContactName($a['contact_name']);
$c->setContactEmail($a['contact_email']);
$c->setContactPhone($a['contact_phone']);
$c->setNotes($a['notes']);
$c->setSalesmanId($a['salesman_id']);
$c->setPriceClassId($a['price_class_id']);
$c->setMaturityDateId($a['maturity_date_id']);
$c->setPaymentDay($a['payment_day']);
$c->setDiscount($a['discount']);
$c->setCreditLimit($a['credit_limit']);
$c->setCopiesDocumentTypeId($a['copies']['document_type_id']);
$c->setCopiesCopies($a['copies']['copies']);
$c->setPaymentMethodId($a['payment_method_id']);
$c->setDeliveryMethodId($a['delivery_method_id']);
$c->setFieldNotes($a['field_notes']);
return $c->insert();
}
else
return false;
}
/**
* Delete Customer from the Company
* @param int $customer_id // $this->getCustomers()
* @return json
* https://www.moloni.pt/dev/index.php?action=getApiDocDetail&id=206
**/
public function deleteCustomer(int $id = 0)
{
if($this->start()){
$c = new Customer();
$c->setAccessToken($this->credencials['token']['access_token']);
$c->setUrl($this->credencials['url']);
$c->setCompanyId($this->credencials['company_id']);
$c->setId($id);
return $c->delete();
}
else
return false;
}
#####
## PAYMENTMETHODS METHODS
#####
/**
* List Payment Methods of Company
* @return json
* https://www.moloni.pt/dev/index.php?action=getApiDocDetail&id=236
**/
public function getPaymentMethods()
{
if($this->start()){
$pm = new PaymentMethods();
$pm->setCompanyId($this->credencials['company_id']);
$pm->setAccessToken($this->credencials['token']['access_token']);
$pm->setUrl($this->credencials['url']);
return $pm->getAll();
}
else
return false;
}
/**
* Delete Payment Methods from the Company
* @param int $payment_method_id // $this->getPaymentMethods()
* @return json
* https://www.moloni.pt/dev/index.php?action=getApiDocDetail&id=239
**/
public function deletePaymentMethods(int $id = 0)
{
if($this->start()){
$pm = new PaymentMethods();
$pm->setCompanyId($this->credencials['company_id']);
$pm->setAccessToken($this->credencials['token']['access_token']);
$pm->setUrl($this->credencials['url']);
$pm->setId($id);
return $pm->delete();
}
else
return false;
}
/**
* Create Payment Methods
* @param array $p Payment Methods
* @return json
* https://www.moloni.pt/dev/index.php?action=getApiDocDetail&id=237
**/
public function setPaymentMethods(array $p = [])
{
if($this->start()){
$pm = new PaymentMethods();
$pm->setCompanyId($this->credencials['company_id']);
$pm->setAccessToken($this->credencials['token']['access_token']);
$pm->setUrl($this->credencials['url']);
$pm->setName($p['name']);
$pm->setIsMumeric($p['is_numeric(var)']);
return $pm->insert();
}
else
return false;
}
/**
* Update PaymentMethods by Id
* @param array $pm PaymentMethods // $this->getPaymentMethods()
* @return json
* https://www.moloni.pt/dev/index.php?action=getApiDocDetail&id=237
**/
public function updatePaymentMethods(array $p = [])
{
if($this->start()){
$pm = new PaymentMethods();
$pm->setCompanyId($this->credencials['company_id']);
$pm->setAccessToken($this->credencials['token']['access_token']);
$pm->setUrl($this->credencials['url']);
$pm->setId($p['id']);
$pm->setName($p['name']);
$pm->setIsMumeric($p['is_numeric(var)']);
return $pm->update();
}
else
return false;
}
#####
## MATURITYDATES METHODS
#####
/**
* List MaturityDates in the Company
* @return json
* https://www.moloni.pt/dev/index.php?action=getApiDocDetail&id=240
**/
public function getMaturityDates()
{
if($this->start()){
$md = new MaturityDates();
$md->setCompanyId($this->credencials['company_id']);
$md->setAccessToken($this->credencials['token']['access_token']);
$md->setUrl($this->credencials['url']);
return $md->getAll();
}
else
return false;
}
/**
* Delete Maturity Dates from the Company
* @param int $maturity_dates_id // $this->getPaymentMethods()
* @return json
* https://www.moloni.pt/dev/index.php?action=getApiDocDetail&id=243
**/
public function deleteMaturityDates(int $id = 0)
{
if($this->start()){
$md = new MaturityDates();
$md->setCompanyId($this->credencials['company_id']);
$md->setAccessToken($this->credencials['token']['access_token']);
$md->setUrl($this->credencials['url']);
$md->setId($id);
return $md->delete();
}
else
return false;
}
/**
* Update PaymentMethods by Id
* @param array $p MaturityDates // $this->getMaturityDates()
* @return json
* https://www.moloni.pt/dev/index.php?action=getApiDocDetail&id=242
**/
public function updateMaturityDates(array $p = [])
{
if($this->start()){
$md = new MaturityDates();
$md->setCompanyId($this->credencials['company_id']);
$md->setAccessToken($this->credencials['token']['access_token']);
$md->setUrl($this->credencials['url']);
$md->setId($id);
$md->setName($p['name']); //string
html
<h2>Result</h2>
{{dump(moloni)}}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.