File: /var/www/dtw.bradford/app/Models/InventoryPurchaseOrders.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class InventoryPurchaseOrders extends Model
{
use HasFactory;
protected $table = 'inventory_purchase_orders_view';
public $timestamps = false;
protected $fillable = [
'id',
'type_request',
'responsible',
'provider_social_reason',
'provider_rut',
'observation',
'quantity',
'subtotal',
'tax',
'total',
'tax_type',
'status_purchase_order',
'status_integration',
'odoo_document_id',
'odoo_service_name',
'odoo_account_analytic',
'accounting_account',
'date_purchase_order',
'creation_date',
'branch',
'purchase_order_details' // JSON con los detalles de productos o servicios
];
// Método para obtener detalles de líneas de la orden de compra
protected function details()
{
return collect(json_decode($this->purchase_order_details, true))->map(function ($line) {
return [
'detail_id' => $line['detail_id'],
'product_id' => $line['product_id'],
'service' => $line['service'],
'quantity' => $line['quantity'],
'price' => $line['price'],
'subtotal' => $line['subtotal'],
'bar_code' => $line['bar_code'],
'product' => $line['product'],
'product_odoo_id' => $line['product_odoo_id'],
'presentation' => $line['presentation'],
];
});
}
// Sobrescribir el método toArray
public function toArray()
{
$array = parent::toArray();
$array['purchase_order_details'] = $this->details();
return $array;
}
}