<?php 
$docs_id=$_POST['docs_id']; 
$can1=mysqli_query($con,"SELECT * FROM dian_documento_soporte where docs_id='$docs_id'") or die(mysqli_error($con)); 
$dato1=mysqli_fetch_array($can1);
$docs_ser_id=$dato1['docs_ser_id'];
$docs_proveedor=$dato1['docs_proveedor'];
$prefijo=$dato1['docs_prefijo'];
$numero=$dato1['docs_numero'];
$pro=mysqli_query($con,"SELECT * FROM proveedor where pro_id='$docs_proveedor'") or die(mysqli_error($con)); 
$prov=mysqli_fetch_array($pro);

$resol=mysqli_query($con,"SELECT * FROM serie_factura where ser_id='$docs_ser_id' and empresa='$empresa'") or die(mysqli_error($con)); 
$res=mysqli_fetch_array($resol);


$sumador_total=0;
$can7=mysqli_query($con,"SELECT * FROM dian_documento_detalle where documento_id='$docs_id'") or die(mysqli_error($con));  
while($row=mysqli_fetch_array($can7))
{
    $sopd_nombre=$row['sopd_nombre'];
    $sopd_cantidad=$row['sopd_cantidad'];
    $sopd_precio=$row['sopd_precio'];
    $total=$sopd_precio*$sopd_cantidad;
    $sumador_total+= $total;
} 

$json = array(
    "number"            => $numero,
    "type_document_id"  => 11,
    "date"              => $dato1['docs_fecha'],
    "time"              => $dato1['docs_hora'],
    "notes"             => $dato1['docs_observaciones'],
    "resolution_number" => $res['ser_resolucion'],
    "prefix"            => $prefijo,
    "sendmail"          => false,
    "seller" => array(
        "identification_number" => $prov['pro_nit'],
        "dv"                    => $prov['pro_dv'],
        "name"                  => $prov['pro_nombre'],
        "phone"                 => $prov['pro_telefono'],
        "address"               => $prov['pro_direccion'],
        "email"                 => $prov['pro_correo'],
        "merchant_registration" => "00000",
        "postal_zone_code"      => $prov['pro_codigo_postal'],
        "type_document_identification_id"=> $prov['pro_tipo_doc'],
        "municipality_id"       => $prov['pro_ciudad']
        //"type_organization_id"  => 1,
        // "type_regime_id"        => 1
    ),
    "payment_form" => array(
        "payment_form_id"       => $dato1['dosc_forma_pago'],
        "payment_method_id"     => $dato1['dosc_medio_pago'],
        "payment_due_date"      => $dato1['docs_fecha'],
        "duration_measure"      => "0"
    ),
    
    "legal_monetary_totals" =>  array(
        "line_extension_amount"     => $sumador_total,
        "tax_exclusive_amount"      => "0.00",
        "tax_inclusive_amount"      => $sumador_total,
        "allowance_total_amount"    => "0.00",
        "charge_total_amount"       => "0.00",
        "payable_amount"            => $sumador_total
    ),
    'invoice_lines'          =>  array(),
    'with_holding_tax_total' =>  array()

);


//retenciones
$ret=mysqli_query($con, "SELECT * from retencion_doc where retencion_ret_id='$docs_id'");
while ($rets=mysqli_fetch_array($ret))
{
    $codigo=$rets['codigo'];
    $ret_base=$rets['ret_base'];
    $ret_importe=$rets['ret_importe'];
    $ret_porcentaje=$rets['porcentaje'];
    //$total_retencion=(($ret_porcentaje/100)*$subtotal2);
    array_push($json['with_holding_tax_total'], [
                            "tax_id"        => $codigo,
                            "tax_amount"    => round($ret_importe,2),
                            "percent"       => $ret_porcentaje,
                            "taxable_amount"=> round($ret_base,2),
                        ]);
}




$can7=mysqli_query($con,"SELECT * FROM dian_documento_detalle where documento_id='$docs_id'") or die(mysqli_error($con));  
while($row=mysqli_fetch_array($can7))
{
    $sopd_nombre=$row['sopd_nombre'];
    $sopd_cantidad=$row['sopd_cantidad'];
    $sopd_precio=$row['sopd_precio'];
    $total=$sopd_precio*$sopd_cantidad;
    
    $sopd_tipo_envio=$row['sopd_tipo_envio'];
    if($sopd_tipo_envio==1)
    {
        $fecha_envio=$row['sopd_fecha_envio'];
    }
    else
    {
        $fecha_envio=$dato1['docs_fecha'];
    }

    array_push($json['invoice_lines'], 
    [ 
        "unit_measure_id" => 70,
        "invoiced_quantity" => $sopd_cantidad,
        "line_extension_amount" => $sopd_precio,
        "free_of_charge_indicator" => false,
        "description"                   =>  $sopd_nombre,
        "code"                          => "0001",
        "type_item_identification_id"   => 4,
        "price_amount"                  => $sopd_precio,
        "base_quantity"                 => $sopd_cantidad,
        "type_generation_transmition_id"=> $sopd_tipo_envio,
        "start_date"                    => $fecha_envio
    ]);
} 
?>

