<?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'];
$docs_motivo=$dato1['docs_motivo'];
$docs_id_ref=$dato1['docs_id_ref'];

$can2=mysqli_query($con,"SELECT * FROM dian_documento_soporte where docs_id='$docs_id_ref'") or die(mysqli_error($con)); 
$dato2=mysqli_fetch_array($can2);
$prefijo_ref=$dato2['docs_prefijo'];
$numero_ref=$dato2['docs_numero'];
$uuid=$dato2['dosc_cuds'];
$issue_date=$dato2['docs_fecha'];


$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(
    "billing_reference" => array(
        "number"     => $prefijo_ref.$numero_ref,
        "uuid"       => $uuid,
        "issue_date" => $issue_date
    ),

    "discrepancyresponsecode"        => $docs_motivo,
    "discrepancyresponsedescription" => "",
    "notes"             => $dato1['docs_observaciones'],
    "prefix"            => $prefijo,
    "number"            => $numero,
    "type_document_id"  => 13,
    "date"              => $dato1['docs_fecha'],
    "time"              => $dato1['docs_hora'],
    "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"  => 2,
        "type_regime_id"       => 1
    ),
   "tax_totals" => array(
        array( 
            "tax_id"          => 1,
            "tax_amount"      => "0.00",
            "taxable_amount"  => round($sumador_total,2),
            "percent"         => "0.00"
        )
    ),

    "allowance_charges" => array(
        array( 
            "discount_id"             =>  1,
            "charge_indicator"        =>  false,
            "allowance_charge_reason" =>  "DESCUENTO GENERAL",
            "amount"                  =>  "0.00",
            "base_amount"             =>  round($sumador_total,2)
        )
    ),
 
    "legal_monetary_totals" =>  array(
        "line_extension_amount"     => round($sumador_total,2),
        "tax_exclusive_amount"      => round($sumador_total,2),
        "tax_inclusive_amount"      => round($sumador_total,2),
        "allowance_total_amount"    => "0.00",
        "charge_total_amount"       => "0.00",
        "payable_amount"            => round($sumador_total,2)
    ),
    'credit_note_lines'          =>  array()

);






$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['credit_note_lines'], 
    [ 
        "unit_measure_id" => 70,
        "invoiced_quantity" => $sopd_cantidad,
        "line_extension_amount" => round($sopd_precio,2),
        "free_of_charge_indicator" => false,

         "allowance_charges" =>  array(
                                array(
                                    "charge_indicator"        => false,
                                    "allowance_charge_reason" => "DESCUENTO GENERAL",
                                    "amount"                  => "0.00",  //descuento item
                                    "base_amount"             => round($sopd_precio,2)      //precio final
                                    ) 
                                ),

        "tax_totals"        =>  array(
                                array(
                                     "tax_id"         => 1,                  //codigo table taxes 
                                     "tax_amount"     => "0.00",  //iva total
                                     "taxable_amount" => round($total,2), //total sin iva
                                     "percent"        => "0"                // % iva
                                  ),
                                ),


        "description"                   =>  $sopd_nombre,
        "code"                          => "0001",
        "brandname"                     => "Sin marca",
        "modelname"                     => "Sin modelo",
        "type_item_identification_id"   => 4,
        "price_amount"                  => round($sopd_precio,2),
        "base_quantity"                 => $sopd_cantidad
    ]);
} 




?>




