Created
August 23, 2021 13:11
-
-
Save rephlex/223efb449c9b4624732211b0f392c5f7 to your computer and use it in GitHub Desktop.
Export unpaid invoices from invoiceninja 4 and upload them to the izettle product catalog
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// database config | |
$dbhost="localhost"; | |
$dbname="invoiceninja"; | |
$dbuser="invoiceninja"; | |
$dbpass="changeme"; | |
// obtain this by creating an app on https://developer.zettle.com/ | |
// lol | |
$clientid="changeme-izettle-clientid"; | |
$clientsecret="changeme-izettle-clientsecret"; | |
// obtain this from your izettle account | |
$userclientid="changeme-izettle-userclientid"; | |
$userapikey="changeme-userapikey"; | |
// install all dependencies by running | |
// composer require laulamanapps/izettle-api | |
// from the command line | |
error_reporting(E_ERROR | E_PARSE); | |
require 'vendor/autoload.php'; | |
use GuzzleHttp\Client; | |
use LauLamanApps\IzettleApi\GuzzleIzettleClient; | |
use LauLamanApps\IzettleApi\IzettleClientFactory; | |
use LauLamanApps\IzettleApi\API\Product\Product; | |
use LauLamanApps\IzettleApi\API\Product\Variant; | |
use LauLamanApps\IzettleApi\API\Product\CategoryCollection; | |
use LauLamanApps\IzettleApi\API\Product\VariantCollection; | |
use LauLamanApps\IzettleApi\API\ImageCollection; | |
use Money\Money; | |
// mysql connection | |
$link = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname); | |
$res = mysqli_query($link, "select invoice_number,balance from invoiceninja.invoices where invoice_status_id=2 and invoice_type_id=1 and is_deleted = 0 and balance != CAST(0 AS DECIMAL(13,2));"); | |
//create zettle client | |
$iZettleClient = new GuzzleIzettleClient(new Client(), $clientid, $clientsecret); | |
$accessToken = $iZettleClient->getAccessTokenFromApiTokenAssertion($userapikey); | |
// create product client | |
$productClient = IzettleClientFactory::getProductClient($iZettleClient); | |
//delete all products | |
//yes, this deletes all your izettle products. | |
$library = $productClient->getProducts(); | |
foreach ($library as $product) { | |
$response = $productClient->deleteProduct($product); | |
} | |
//add all products | |
while ($row = mysqli_fetch_array($res)) { | |
$variants[0]=Variant::new(null,null,null,null,1,null,Money::EUR(str_replace(".", "",$row['balance'])),null,19); | |
$prod = Product::new($row['invoice_number'], $row['invoice_number'], new CategoryCollection(null), new ImageCollection(null), new VariantCollection($variants),null); | |
$response = $productClient->createProduct($prod); | |
} | |
mysqli_close($link); | |
// hint: i can be run by nagios/naemon instead of your crontab ;-) | |
echo "OK: unpaid invoices imported to izettle."; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment