PHP code example of khaledhajsalem / laravel-zatca-phase2
1. Go to this page and download the library: Download khaledhajsalem/laravel-zatca-phase2 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/ */
khaledhajsalem / laravel-zatca-phase2 example snippets
// config/zatca.php
'invoice_model' => App\Models\Invoice::class,
'invoice_table' => 'invoices',
'credit_note_model' => App\Models\CreditNote::class, // Can be the same as invoice model
'credit_note_table' => 'invoices', // Can be the same as invoice table
use KhaledHajSalem\ZatcaPhase2\Traits\HasZatcaSupport;
class Invoice extends Model
{
use HasZatcaSupport;
// Your existing model implementation
}
class CreditNote extends Model
{
use HasZatcaSupport;
// Your existing model implementation
}
// config/zatca.php
'credit_note' => [
// Determines how to identify a document as a credit note
'identification' => [
// Method can be 'model', 'type_field', or 'table'
'method' => 'type_field',
// If method is 'type_field', specify which field contains the type
'type_field' => 'type',
// If method is 'type_field', specify the value that indicates a credit note
'type_value' => 'credit_note',
],
// Determines how to link credit notes to their original invoices
'invoice_reference' => [
// Field in the credit note that references the original invoice
'field' => 'original_invoice_id',
// How to retrieve the original invoice number (field or relation.field)
'number_reference' => 'originalInvoice.number',
// How to retrieve the original invoice UUID (field or relation.field)
'uuid_reference' => 'originalInvoice.zatca_invoice_uuid',
],
],
use KhaledHajSalem\ZatcaPhase2\Facades\Zatca;
// Report a credit note
$creditNote = CreditNote::find(1);
// Or if using a type field: $creditNote = Invoice::where('type', 'credit_note')->find(1);
try {
$response = Zatca::reportCreditNote($creditNote);
// Handle successful reporting
} catch (ZatcaException $e) {
// Handle exception
}
// Clear a credit note
try {
$response = Zatca::clearCreditNote($creditNote);
// Handle successful clearance
} catch (ZatcaException $e) {
// Handle exception
}
// Check if a document is a credit note
if ($document->isZatcaCreditNote()) {
// This is a credit note, handle accordingly
}
// Get the original invoice for a credit note
$originalInvoice = $creditNote->getZatcaOriginalInvoice();
// In your invoice view
<div class="qr-code">
<img src="{{ $invoice->getZatcaQrCode() }}" alt="ZATCA QR Code">
</div>
// Or download QR code directly using built-in route
<a href="{{ route('zatca.documents.qr', $invoice->id) }}" target="_blank">Download QR Code</a>
// Generate PDF with default template
$pdfContent = Zatca::generatePdf($invoice);
// With custom options
$pdfContent = Zatca::generatePdf($invoice, [
'paper' => 'a4',
'orientation' => 'portrait',
'template' => 'custom.invoice.template', // Optional custom template
'custom_css' => '.header { background-color: #f5f5f5; }',
]);
// Save to file
file_put_contents('invoice.pdf', $pdfContent);
// Or return as download response in controller
return response($pdfContent)
->header('Content-Type', 'application/pdf')
->header('Content-Disposition', 'attachment; filename="invoice-' . $invoice->number . '.pdf"');
// Publish the default templates
php artisan vendor:publish --tag=zatca-views
// Then customize them in resources/views/vendor/zatca/
// Queue a single invoice for processing
Zatca::queue($invoice, 'report');
// Queue a credit note
Zatca::queue($creditNote, 'report');
// Queue for clearance with specific queue name
Zatca::queue($invoice, 'clear', 'zatca-high');
// Queue a batch of invoices
$invoices = Invoice::needZatcaReporting()->take(100)->get();
Zatca::queueBatch($invoices, 'report', 20, function($batch) {
// This callback is executed when the batch completes
Mail::to('[email protected]')->send(new ZatcaBatchCompleted($batch));
});
use KhaledHajSalem\ZatcaPhase2\Facades\Zatca;
// Process a batch of invoices that need reporting
$invoices = Invoice::needZatcaReporting()->take(50)->get();
Zatca::queueBatch($invoices, 'report', 10);
// Process credit notes that need reporting
$creditNotes = Invoice::needZatcaCreditNoteReporting()->take(50)->get();
Zatca::queueBatch($creditNotes, 'report', 10);
// With completion callback
Zatca::queueBatch($invoices, 'report', 10, function($batch, $exception = null) {
if ($exception) {
// Handle batch failure
Log::error('Batch failed', ['exception' => $exception->getMessage()]);
} else {
// Handle batch success
Log::info('Batch completed', [
'processed' => $batch->processedJobs(),
'failed' => $batch->failedJobs,
]);
}
});
use KhaledHajSalem\ZatcaPhase2\Support\XmlGenerator;
// Extend the XML generator
class CustomXmlGenerator extends XmlGenerator
{
// Override methods as needed
protected static function generateInvoiceLines(array $lineItems, $isCreditNote = false)
{
// Custom implementation
}
}
// Register your custom class in a service provider
public function register()
{
$this->app->bind(XmlGenerator::class, CustomXmlGenerator::class);
}
use KhaledHajSalem\ZatcaPhase2\Events\InvoiceReported;
use KhaledHajSalem\ZatcaPhase2\Events\InvoiceCleared;
use KhaledHajSalem\ZatcaPhase2\Events\CreditNoteReported;
use KhaledHajSalem\ZatcaPhase2\Events\CreditNoteCleared;
// In your EventServiceProvider
protected $listen = [
InvoiceReported::class => [
// Your listeners
SendInvoiceReportedNotification::class,
],
InvoiceCleared::class => [
// Your listeners
UpdateAccountingSystem::class,
],
CreditNoteReported::class => [
// Your listeners
],
CreditNoteCleared::class => [
// Your listeners
],
];
// Generate a QR code
$qrCode = Zatca::generateQrCode($invoice);
// Display in view
<img src="{{ $invoice->getZatcaQrCode() }}" alt="ZATCA QR Code">
// Download QR code directly
<a href="{{ route('zatca.documents.qr', $invoice->id) }}" target="_blank">Download QR Code</a>
// Generate PDF
$pdfContent = Zatca::generatePdf($invoice, [
'paper' => 'a4',
'orientation' => 'portrait',
'template' => 'custom.invoice.template', // Optional custom template
]);
// Save to file
file_put_contents('invoice.pdf', $pdfContent);
// Or download directly via route
<a href="{{ route('zatca.documents.pdf', $invoice->id) }}" target="_blank">Download PDF</a>
// Queue a single invoice for processing
Zatca::queue($invoice, 'report');
// Queue a credit note
Zatca::queue($creditNote, 'report');
// Queue for clearance
Zatca::queue($invoice, 'clear', 'zatca-high');
// Queue a batch of invoices
$invoices = Invoice::needZatcaReporting()->take(100)->get();
Zatca::queueBatch($invoices, 'report', 20, function($batch) {
// This callback is executed when the batch completes
Mail::to('[email protected]')->send(new ZatcaBatchCompleted($batch));
});
bash
php artisan zatca:install
bash
php artisan migrate
bash
php artisan zatca:generate-certificate
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.