#!/usr/bin/env php
<?php


/**
* In acest exemplu se face o cerere POST catre API-ul IceFact pentru a crea o proforma.
* In cazul in care comanda s-a executat cu succes, raspunsul va fi de forma:
*
* {
*     "status": "OK",
*     "proforma_id": 1
* }
* 
*/


$url = 'https://api.icefact.ro:10777/v1/proforma/create';

$data = [
    'proforma' => [
        'serie' => 'TEST',
        'numar' => 1,
        'data' => '2025-01-01',
        'moneda' => 'RON',
        'observatii' => 'Observatii',
        'anulat' => '0'
    ],
    'curs_valutar' => [
        'moneda' => 'EUR',
        'multiplicator' => 1,
        'curs' => 4.9774
    ],
    'prestator' => [
        'contact' => [
            'nume' => 'IceFact Cloud API'
        ]
    ],
    'cumparator' => [
        'denumire' => 'IceSoft SRL',
        'cif' => 'RO21394595',
        'nr_regcom' => 'J13/933/2007',
        'adresa' => 'Str. Principala, nr. 1',
        'localitate' => 'Bucuresti',
        'judet' => 'RO-B',
        'sector' => 1,
        'tara' => 'RO',
        'cod_postal' => '123456',
        'iban' => 'RO58BRDE140SV45289901400',
        'banca' => 'BRD Năvodari',
        'contact' => [
            'nume' => "Popescu Ion",
            "telefon" => "0777777777",
            "email" => "office@firma.ro"
        ]
    ],
    'articole' => [
        [
            'sku' => 'SKU123',
            'mpn' => 'MPN123',
            'ean' => '1234567890123',
            'tags' => 'tag1, tag2',
            'serii' => 'serie1, serie2',
            'cpv' => '45233220-3',
            'nc' => 'NC123',
            'denumire' => 'Produs',
            'descriere' => 'Descriere produs',
            'observatii' => 'Observatii produs',
            'tara_origine' => 'RO',
            'um' => 'H87',
            'pret_unitar_max_zecimale' => 8,
            'cantitate' => 1,
            'pret_unitar_cu_tva' => 125,
            'valoare_fara_tva' => 105.04,
            'valoare_tva' => 19.96,
            'cota_tva' => "21%"
        ]
    ],
    'total' => [
        'valoare_fara_tva' => 105.04,
        'valoare_tva' => 19.96,
        'valoare_cu_tva' => 125
    ]
];

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/json'
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

curl_setopt($ch, CURLOPT_SSLCERT, 'demo-api.icefact.ro.crt');
curl_setopt($ch, CURLOPT_SSLKEY, 'demo-api.icefact.ro.key');

$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

echo "Status code: $http_code\n";
echo "Response text: $response\n";
