squareetlabs /
LaravelVerifactu
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Squareetlabs\VeriFactu\Console\Commands; |
||
| 4 | |||
| 5 | use Illuminate\Console\Command; |
||
| 6 | |||
| 7 | class MakeAdapterCommand extends Command |
||
| 8 | { |
||
| 9 | protected $signature = 'verifactu:make-adapter {model : The model class name}'; |
||
| 10 | |||
| 11 | protected $description = 'Generate VeriFactu contract implementation for an existing model'; |
||
| 12 | |||
| 13 | public function handle() |
||
| 14 | { |
||
| 15 | $modelName = $this->argument('model'); |
||
| 16 | $modelClass = "App\\Models\\{$modelName}"; |
||
|
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
| 17 | |||
| 18 | // We don't strictly check if class exists because user might be creating it |
||
| 19 | // or it might be in a different namespace, but we assume App\Models for simplicity |
||
| 20 | // in this helper command. |
||
| 21 | |||
| 22 | $stub = $this->getStub(); |
||
| 23 | |||
| 24 | $this->info("Add the following methods to your {$modelName} model:"); |
||
| 25 | $this->line(''); |
||
| 26 | $this->line($stub); |
||
| 27 | $this->line(''); |
||
| 28 | $this->info("Don't forget to add imports:"); |
||
| 29 | $this->info("use Squareetlabs\\VeriFactu\\Contracts\\VeriFactuInvoice;"); |
||
| 30 | $this->info("use Squareetlabs\\VeriFactu\\Contracts\\VeriFactuBreakdown;"); |
||
| 31 | $this->info("use Squareetlabs\\VeriFactu\\Contracts\\VeriFactuRecipient;"); |
||
| 32 | $this->info("use Illuminate\\Support\\Collection;"); |
||
| 33 | $this->info("use Carbon\\Carbon;"); |
||
| 34 | $this->line(''); |
||
| 35 | $this->info("And implement the interface: class {$modelName} extends Model implements VeriFactuInvoice"); |
||
| 36 | |||
| 37 | return 0; |
||
| 38 | } |
||
| 39 | |||
| 40 | protected function getStub(): string |
||
| 41 | { |
||
| 42 | return <<<'PHP' |
||
| 43 | // VeriFactuInvoice Contract Implementation |
||
| 44 | |||
| 45 | public function getInvoiceNumber(): string |
||
| 46 | { |
||
| 47 | return $this->number; // Adjust to your field name |
||
| 48 | } |
||
| 49 | |||
| 50 | public function getIssueDate(): Carbon |
||
| 51 | { |
||
| 52 | return $this->date; // Adjust to your field name |
||
| 53 | } |
||
| 54 | |||
| 55 | public function getInvoiceType(): string |
||
| 56 | { |
||
| 57 | return $this->type->value ?? (string)$this->type; // Adjust if needed |
||
| 58 | } |
||
| 59 | |||
| 60 | public function getTotalAmount(): float |
||
| 61 | { |
||
| 62 | return (float)$this->total; // Adjust to your field name |
||
| 63 | } |
||
| 64 | |||
| 65 | public function getTaxAmount(): float |
||
| 66 | { |
||
| 67 | return (float)$this->tax; // Adjust to your field name |
||
| 68 | } |
||
| 69 | |||
| 70 | public function getCustomerName(): string |
||
| 71 | { |
||
| 72 | return $this->customer_name; // Adjust to your field name |
||
| 73 | } |
||
| 74 | |||
| 75 | public function getCustomerTaxId(): ?string |
||
| 76 | { |
||
| 77 | return $this->customer_tax_id; // Adjust to your field name |
||
| 78 | } |
||
| 79 | |||
| 80 | public function getBreakdowns(): Collection |
||
| 81 | { |
||
| 82 | // If you have a single tax rate, create a breakdown on the fly: |
||
| 83 | return collect([ |
||
| 84 | new class($this) implements VeriFactuBreakdown { |
||
| 85 | public function __construct(private $invoice) {} |
||
| 86 | public function getRegimeType(): string { return '01'; } |
||
| 87 | public function getOperationType(): string { return 'S1'; } |
||
| 88 | public function getTaxRate(): float { return 0.0; } // Adjust |
||
| 89 | public function getBaseAmount(): float { return $this->invoice->getTotalAmount(); } |
||
| 90 | public function getTaxAmount(): float { return $this->invoice->getTaxAmount(); } |
||
| 91 | } |
||
| 92 | ]); |
||
| 93 | |||
| 94 | // Or if you have a relationship: |
||
| 95 | // return $this->breakdowns; |
||
| 96 | } |
||
| 97 | |||
| 98 | public function getRecipients(): Collection |
||
| 99 | { |
||
| 100 | // Return empty collection if you don't have multiple recipients |
||
| 101 | return collect(); |
||
| 102 | } |
||
| 103 | |||
| 104 | public function getPreviousHash(): ?string |
||
| 105 | { |
||
| 106 | return $this->previous_hash; // Adjust to your field name |
||
| 107 | } |
||
| 108 | |||
| 109 | public function getOperationDescription(): string |
||
| 110 | { |
||
| 111 | return $this->description ?? 'Invoice issued'; // Adjust |
||
| 112 | } |
||
| 113 | PHP; |
||
| 114 | } |
||
| 115 | } |
||
| 116 |