<?php
//+++++++++++++++++++++++++++++
function NDC_GEN_JSON($ID_NDC){
	$r=gr("vis_fe_ventaz","ID=$ID_NDC");
	//++
	$EMP_NOM=$r['EMP_FE_NOM'];
	$EMP_RUC=$r['EMP_FE_RUC'];
	$EMP_DEP=$r['EMP_FE_DEP'];
	$EMP_PRV=$r['EMP_FE_PRV'];
	$EMP_DIS=$r['EMP_FE_DIS'];
	$EMP_UBI=$r['EMP_FE_UBI'];
	$EMP_DIR=$r['EMP_FE_DIR'];
	$EMP_EMA=$r['EMP_FE_EMA'];
	$NDC_OBS=gs("fv_obs","fe_ventaz","id_ven=$ID_NDC");
	//++
	$NDC_PRJ="X";
	//++
	$DOC_MOD_TIP=$r['COM_M'];	
	if($DOC_MOD_TIP=="BOL. ELEC."){
		$NDC_PRJ="B";
		$DOC_MOD_SER="B".vbr("0000".$r['SER_M_'],3);
		$DOC_MOD_TIP_COD="03";
	}
	if($DOC_MOD_TIP=="FACT. ELEC."){
		$NDC_PRJ="F";
		$DOC_MOD_SER="F".vbr("0000".$r['SER_M_'],3);
		$DOC_MOD_TIP_COD="01";
	}
	$DOC_MOD_NUM=vbr("00000000".$r['NUM_M_'],8);
	$DOC_MOD_SER_NUM=$DOC_MOD_SER."-".$DOC_MOD_NUM;
	//
	$NDC_SER_NUM=$NDC_PRJ.vbr("000".$r['SER_'],3)."-".vbr("00000000".$r['NUM_'],8);
	$NDC_FEC_EMI=$r['FEC'];
	$NDC_TOT_EN_LETRAS=n2l($r['TOT']);
	$NDC_MOT_COD=$r['CAT_TIP'];
	$NDC_MOT_NOM=$r['CAT_DES'];
	//
	$DOC_CLI_KOD=$r['ID_CLI'];
	if($DOC_MOD_TIP_COD=="03"){
		$DOC_CLI_NRO=$r['CLI_DNI'];
		$DOC_CLI_DOC_COD=gs("SUNAT_COD","vclientes","ID=$DOC_CLI_KOD");
	}
	if($DOC_MOD_TIP_COD=="01"){
		$DOC_CLI_NRO=$r['CLI_RUC'];
		$DOC_CLI_DOC_COD="6";//gs("SUNAT_COD","vclientes","ID=$DOC_CLI_KOD");
	}
	//$DOC_CLI_DOC_COD=gs("SUNAT_COD","vclientes","ID=$DOC_CLI_KOD");
	$DOC_CLI_NOM=$r['CLI_NOM'];
	//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	$DOC_CLI_UBI_COD=$r['NDC_UBI_COD'];
		if(strlen($DOC_CLI_UBI_COD)==0)$DOC_CLI_UBI_COD=$r['CLI_UBI'];
	$DOC_CLI_DIR=$r['DIR'];
		if(strlen($DOC_CLI_DIR)==0)$DOC_CLI_DIR=$r['CLI_DIR'];
	$DOC_CLI_DEP=$r['NDC_UBI_DEP'];
		if(strlen($DOC_CLI_DEP)==0)$DOC_CLI_DEP=$r['CLI_DEP'];
	$DOC_CLI_PRO=$r['NDC_UBI_PRO'];
		if(strlen($DOC_CLI_PRO)==0)$DOC_CLI_PRO=$r['CLI_PRV'];
	$DOC_CLI_DIS=$r['NDC_UBI_DIS'];
		if(strlen($DOC_CLI_DIS)==0)$DOC_CLI_DIS=$r['CLI_DIS'];
	$DOC_CLI_PAI=$r['NDC_UBI_PAI'];
		if(strlen($DOC_CLI_PAI)==0)$DOC_CLI_PAI=$r['CLI_PAI'];	
	//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	$DOC_CLI_PAI_COD=gs("pai_cod","lista_paises","pai_nom='$DOC_CLI_PAI'");
	$DOC_CLI_EMA=$r['CLI_EMAIL'];
	//
	$DOC_IGV=round($r['IGV'],2);
	$DOC_STO=round($r['ST'],2);
	$DOC_TOT=round($r['TOT'],2);
	//++++
	if(strlen($NDC_OBS)==0){
		$NDC_OBS=$NDC_MOT_NOM;
	}
	//++++
	$JSON='{
	   "_D":"urn:oasis:names:specification:ubl:schema:xsd:CreditNote-2",
	   "_A":"urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2",
	   "_B":"urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2",
	   "_E" : "urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2",
	   "CreditNote":[
		  {
			 "UBLVersionID":[
				{
				   "_":"2.1"
				}
			 ],
			 
			 "CustomizationID":[
				{
				   "_":"2.0"
				}
			 ],
			 
			 "ID":[
				{
				   "_":"'.$NDC_SER_NUM.'"
				}
			 ],
			 
			 "IssueDate":[
				{
				   "_":"'.$NDC_FEC_EMI.'"
				}
			 ],
			 
			 "Note":[
				{
				   "_":"'.$NDC_TOT_EN_LETRAS.'",
				   "languageID":"en",
				   "languageLocaleID":"1000"
				}
			 ],
			 
			 "DocumentCurrencyCode":[
				{
				   "_":"PEN",
				   "listID":"ISO 4217 Alpha",
				   "listName":"Currency",
				   "listAgencyName":"United Nations Economic Commission for Europe"
				}
			 ],
			 
			 "DiscrepancyResponse":[
				{
				  "ResponseCode":[
						{	
							"_":"'.$NDC_MOT_COD.'",
							"listAgencyName":"PE:SUNAT",
							"listName":"Tipo de nota de credito",
							"listURI":"urn:pe:gob:sunat:cpe:see:gem:catalogos:catalogo09"
						}
				  ],
				  "Description":[
						{
							"_":"'.$NDC_OBS.'"
						}
					]
				 }
			 ],
			 
			 "BillingReference":[
				 {
						"InvoiceDocumentReference":[
							{
							   "ID":[
								   {
										"_":"'.$DOC_MOD_SER_NUM.'"
								   }
							   ],
							   "DocumentTypeCode":[
								   {
										"_":"'.$DOC_MOD_TIP_COD.'",
										"listAgencyName":"PE:SUNAT",
										"listName":"Tipo de Documento",
										"listURI":"urn:pe:gob:sunat:cpe:see:gem:catalogos:catalogo01"
								   }
							   ]
							}
					  ]
				 }
			 ],
			 
			 "Signature":[
				{
				   "ID":[
					  {
						 "_":"IDSignature"
					  }
				   ],
				   "SignatoryParty":[
					  {
						 "PartyIdentification":[
							{
							   "ID":[
								  {
									 "_":"'.$EMP_RUC.'"
								  }
							   ]
							}
						 ],
						 "PartyName":[
							{
							   "Name":[
								  {
									 "_":"'.$EMP_NOM.'"
								  }
							   ]
							}
						 ]
					  }
				   ],
				   "DigitalSignatureAttachment":[
					  {
						 "ExternalReference":[
							{
							   "URI":[
								  {
									 "_":"IDSignature"
								  }
							   ]
							}
						 ]
					  }
				   ]
				}
			 ],
			 
			 "AccountingSupplierParty":[
				{
				   "Party":[
					  {
						 "PartyIdentification":[
							{
							   "ID":[
								  {
									 "_":"'.$EMP_RUC.'",
									 "schemeID":"6",
									 "schemeName":"Documento de Identidad",
									 "schemeAgencyName":"PE:SUNAT",
									 "schemeURI":"urn:pe:gob:sunat:cpe:see:gem:catalogos:catalogo06"
								  }
							   ]
							}
						 ],
						 "PartyName":[
							{
							   "Name":[
								  {
									 "_":"'.$EMP_RUC.'"
								  }
							   ]
							}
						 ],
						 "PostalAddress":[
							{
							   "ID":[
								  {
									 "_":"'.$EMP_UBI.'"
								  }
							   ],
							   "StreetName":[
								  {
									 "_":"'.$EMP_DIR.'"
								  }
							   ],
							   "CityName":[
								  {
									 "_":"'.$EMP_DEP.'"
								  }
							   ],
							   "CountrySubentity":[
								  {
									 "_":"'.$EMP_PRV.'"
								  }
							   ],
							   "District":[
								  {
									 "_":"'.$EMP_DIS.'"
								  }
							   ],
							   "Country":[
								  {
									 "IdentificationCode":[
										{
										   "_":"PE"
										}
									 ]
								  }
							   ]
							}
						 ],
						 
						 "PartyLegalEntity":[
							{
							   "RegistrationName":[
								  {
									 "_":"'.$EMP_NOM.'"
								  }
							   ],
							   "RegistrationAddress":[
								  {
					                 "AddressTypeCode":[
										{
										   "_":"0000",
										   "listAgencyName":"PE:SUNAT",
										   "listName":"Establecimientos anexos"
										}
									 ],
									 "CityName":[
										{
										   "_":"'.$EMP_DEP.'"
										}
									 ],
									 "CountrySubentity":[
										{
										   "_":"'.$EMP_PRV.'"
										}
									 ],
									 "District":[
										{
										   "_":"'.$EMP_DIS.'"
										}
									 ],
									 
									 "AddressLine" : [ 
										{
											"Line" : [ 
												{
													"_" : "'.$EMP_DIR.'"
												}
											]
										}
									],
									 
									 
									 "Country":[
										{
										   "IdentificationCode":[
											  {
												 "_":"PE"
											  }
										   ]
										}
									 ]
								  }
							   ]
							}
						 ],
						 
						 "Contact":[
							{
							   "ElectronicMail":[
								  {
									 "_":"'.$EMP_EMA.'"
								  }
							   ]
							}
						 ]
					  }
				   ]
				}
			 ],
			 
			 
			 "AccountingCustomerParty":[
				{
				   "Party":[
					  {
						 "PartyIdentification":[
							{
							   "ID":[
								  {
									 "_":"'.$DOC_CLI_NRO.'",
									 "schemeID":"'.$DOC_CLI_DOC_COD.'",
									 "schemeName":"Documento de Identidad",
									 "schemeAgencyName":"PE:SUNAT",
									 "schemeURI":"urn:pe:gob:sunat:cpe:see:gem:catalogos:catalogo06"
								  }
							   ]
							}
						 ],
						 "PartyName":[
							{
							   "Name":[
								  {
									 "_":"'.$DOC_CLI_NOM.'"
								  }
							   ]
							}
						 ],
						 "PostalAddress":[
							{
							   "ID":[
								  {
									 "_":"'.$DOC_CLI_UBI_COD.'"
								  }
							   ],
							   "StreetName":[
								  {
									 "_":"'.$DOC_CLI_DIR.'"
								  }
							   ],
							   "CityName":[
								  {
									 "_":"'.$DOC_CLI_DEP.'"
								  }
							   ],
							   "CountrySubentity":[
								  {
									 "_":"'.$DOC_CLI_PRO.'"
								  }
							   ],
							   "District":[
								  {
									 "_":"'.$DOC_CLI_DIS.'"
								  }
							   ],
							   "Country":[
								  {
									 "IdentificationCode":[
										{
										   "_":"'.$DOC_CLI_PAI_COD.'"
										}
									 ]
								  }
							   ]
							}
						 ],
						 
						 
						 "PartyLegalEntity":[
							{
							   "RegistrationName":[
								  {
									 "_":"'.$DOC_CLI_NOM.'"
								  }
							   ],
							   "RegistrationAddress":[
								  {
									 "CityName":[
										{
										   "_":"'.$DOC_CLI_PRO.'"
										}
									 ],
									 
									 "CountrySubentity":[
										{
										   "_":"'.$DOC_CLI_DEP.'"
										}
									 ],
									 
									 "District" : [ 
												{
													"_" : "'.$DOC_CLI_DIS.'"
										}
									 ],
									 
									 "AddressLine":[
										{
										   "Line":[
											  {
												 "_":"'.$DOC_CLI_DIR.'"
											  }
										   ]
										}
									 ],										 
									 
									 "Country":[
										{
										   "IdentificationCode":[
											  {
												 "_":"'.$DOC_CLI_PAI_COD.'"
											  }
										   ]
										}
									 ]
								  }
							   ]
							}
						 ],
						 
						 "Contact":[
							{
							   "ElectronicMail":[
								  {
									 "_":"'.$DOC_CLI_EMA.'"
								  }
							   ]
							}
						 ]
					  }
				   ]
				}
			 ],
			 
			 
			 "TaxTotal":[
				{
				   "TaxAmount":[
					  {
						 "_":'.$DOC_IGV.',
						 "currencyID":"PEN"
					  }
				   ],
				   
				   "TaxSubtotal":[
				  	{
						 "TaxableAmount":[
							{
							   "_":'.$DOC_STO.',
							   "currencyID":"PEN"
							}
						 ],					 
						 "TaxAmount":[
							{
							   "_":'.$DOC_IGV.',
							   "currencyID":"PEN"
							}
						 ],
						 
						 "TaxCategory":[
							{
							   "ID":[
								  {
									 "_":"S",
									 "schemeID":"UN/ECE 5305",
									 "schemeName":"Tax Category Identifier",
									 "schemeAgencyName":"United Nations Economic Commission for Europe"
								  }
							   ],
							   
							   "Percent":[
								  {
									 "_":18
								  }
							   ],
							   
							   "TaxScheme":[
								  {
									 "ID":[
										{
										   "_":"1000",
										   "schemeID":"UN/ECE 5153",
										   "schemeAgencyID":"6"
										}
									 ],
									 "Name":[
										{
										   "_":"IGV"
										}
									 ],
									 "TaxTypeCode":[
										{
										   "_":"VAT"
										}
									 ]
								  }
							   ]
							}
						 ]
					  }
				   ]
				}
			 ],
			 
			 
			 "LegalMonetaryTotal":[
				{
				   "LineExtensionAmount":[
					  {
						 "_":'.$DOC_STO.',
						 "currencyID":"PEN"
					  }
				   ],
				   "TaxInclusiveAmount":[
					  {
						 "_":'.$DOC_TOT.',
						 "currencyID":"PEN"
					  }
				   ],
				   "PayableAmount":[
					  {
						 "_":'.$DOC_TOT.',
						 "currencyID":"PEN"
					  }
				   ]
				}
			 ],';
			 
	//++++++++++++++++++++++++++++++++++++++++++++
	$DETALLE="";
	$DETALLE_TOT=gn("count(*)","fe_ventaz_det","id_fe=$ID_NDC");
	$AGREGAR_COMA=0;
	$CX=0;
	$rs="SELECT * FROM fe_ventaz_det WHERE id_fe=$ID_NDC ORDER BY cx";
	$rs = grs($rs);			
	while ($r = $rs->fetch_assoc()){
		$CX++;
		//
		$PRE_UNI_C_IGV=NF(round($r['pre_']*$r['pie_'],2),2);
		$PRE_UNI_S_IGV=NF(round((($PRE_UNI_C_IGV/118)*100),2),2);
		$IMP_SIN_IGV=NF(round($r['can_']* $PRE_UNI_S_IGV,2),2);
		//
		$ITE_IGV_X_CAN=round(($PRE_UNI_C_IGV - $PRE_UNI_S_IGV) * $r['can_'],2);
		//
		$ITE_DES=$r['cod']."x".$r['pie_']." ".str_replace('"',"''",$r['des']);
		$ITE_COD=$r['cod'];
		$DET_CAN=$r['can_'];	
		//
		$ITEM='
		"CreditNoteLine":[
            {
               "ID":[
                  {
                     "_":'.$CX.'
                  }
               ],
			   "Note":[
                  {
                     "_":"CAJAS"
                  }
               ],            
               "CreditedQuantity":[
                  {
                     "_":"'.$DET_CAN.'",
                     "unitCode":"ZZ",
                     "unitCodeListID":"UN/ECE rec 20",
                     "unitCodeListAgencyName":"United Nations Economic Commission for Europe"
                  }
               ],
               "LineExtensionAmount":[
                  {
                     "_":'.$IMP_SIN_IGV.',
                     "currencyID":"PEN"
                  }
               ],
			   
               "PricingReference":[
                  {
                     "AlternativeConditionPrice":[
                        {
                           "PriceAmount":[
                              {
                                 "_":'.$PRE_UNI_C_IGV.',
                                 "currencyID":"PEN"
                              }
                           ],
                           "PriceTypeCode":[
                              {
                                 "_":"01",
                                 "listName":"Tipo de Precio",
                                 "listAgencyName":"PE:SUNAT",
                                 "listURI":"urn:pe:gob:sunat:cpe:see:gem:catalogos:catalogo16"
                              }
                           ]
                        }
                     ]
                  }
               ],
			   
               "TaxTotal":[
                  {
                     "TaxAmount":[
                        {
                           "_":'.$ITE_IGV_X_CAN.',
                           "currencyID":"PEN"
                        }
                     ],
                     "TaxSubtotal":[
                        {
                           "TaxableAmount":[
                              {
                                 "_":'.$IMP_SIN_IGV.',
                                 "currencyID":"PEN"
                              }
                           ],
                           "TaxAmount":[
                              {
                                 "_":'.$ITE_IGV_X_CAN.',
                                 "currencyID":"PEN"
                              }
                           ],
						   
                           "TaxCategory":[
                              {
				 				"Percent":[
                                    {
                                       "_":"18"
                                    }
                                 ],
                                 "TaxExemptionReasonCode":[
                                    {
                                       "_":"10",
                                       "listAgencyName":"PE:SUNAT",
                                       "listName":"Afectacion del IGV",
                                       "listURI":"urn:pe:gob:sunat:cpe:see:gem:catalogos:catalogo07"
                                    }
                                 ],
                                 "TaxScheme":[
                                    {
                                       "ID":[
                                          {
                                             "_":"1000",
                                             "schemeAgencyName":"PE:SUNAT",
                                             "schemeName":"Codigo de tributos",
											 "schemeURI":"urn:pe:gob:sunat:cpe:see:gem:catalogos:catalogo05"
                                          }
                                       ],
                                       "Name":[
                                          {
                                             "_":"IGV"
                                          }
                                       ],
                                       "TaxTypeCode":[
                                          {
                                             "_":"VAT"
                                          }
                                       ]
                                    }
                                 ]
                              }
                           ]
                        }
                     ]
                  }
               ],
			   
               "Item":[
                  {
                     "Description":[
                        {
                           "_":"'.$ITE_DES.'"
                        }
                     ],
					 "SellersItemIdentification":[
                        {
                           "ID":[
							  {
								 "_":"'.$ITE_COD.'"								 
							  }
						   ]
                        }
                     ]
                  }
               ],
			   
               "Price":[
                  {
                     "PriceAmount":[
                        {
                           "_":'.$PRE_UNI_S_IGV.',
                           "currencyID":"PEN"
                        }
                     ]
                  }
               ]
            }
         ]
		';
		if($CX<$DETALLE_TOT){
			$ITEM=$ITEM.",";
		}
		$DETALLE=$DETALLE."\n".$ITEM;			
	}
	//++++++++++++++++++++++++++++++++++++++++++++
	$JSON=$JSON.$DETALLE;
	$JSON=$JSON."
				}
			   ]
			}
	";
	$FILE_JSON=$EMP_RUC."-07-".$NDC_SER_NUM.".json";
	if(file_exists($FILE_JSON)){
		unlink($FILE_JSON);
	}	
	CREATE_FILE($FILE_JSON,$JSON);	
	return $FILE_JSON;		
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
function NDD_GEN_JSON($ID_NDC){
	$r=gr("vis_fe_ventaz","ID=$ID_NDC");
	$EMP_NOM=$r['EMP_FE_NOM'];
	$EMP_RUC=$r['EMP_FE_RUC'];
	$EMP_DEP=$r['EMP_FE_DEP'];
	$EMP_PRV=$r['EMP_FE_PRV'];
	$EMP_DIS=$r['EMP_FE_DIS'];
	$EMP_UBI=$r['EMP_FE_UBI'];
	$EMP_DIR=$r['EMP_FE_DIR'];
	$EMP_EMA=$r['EMP_FE_EMA'];
	$NDC_OBS=gs("fv_obs","fe_ventaz","id_ven=$ID_NDC");
	//++
	$NDC_PRJ="X";
	//++
	$DOC_MOD_TIP=$r['COM_M'];	
	if($DOC_MOD_TIP=="BOL. ELEC."){
		$NDC_PRJ="B";
		$DOC_MOD_SER="B".vbr("0000".$r['SER_M_'],3);
		$DOC_MOD_TIP_COD="03";
	}
	if($DOC_MOD_TIP=="FACT. ELEC."){
		$NDC_PRJ="F";
		$DOC_MOD_SER="F".vbr("0000".$r['SER_M_'],3);
		$DOC_MOD_TIP_COD="01";
	}
	$DOC_MOD_NUM=vbr("00000000".$r['NUM_M_'],8);
	$DOC_MOD_SER_NUM=$DOC_MOD_SER."-".$DOC_MOD_NUM;
	//
	$NDC_SER_NUM=$NDC_PRJ.vbr("000".$r['SER_'],3)."-".vbr("00000000".$r['NUM_'],8);
	$NDC_FEC_EMI=$r['FEC'];
	$NDC_TOT_EN_LETRAS=n2l($r['TOT']);
	$NDC_MOT_COD=$r['CAT_TIP'];
	$NDC_MOT_NOM=$r['CAT_DES'];
	//
	$DOC_CLI_KOD=$r['ID_CLI'];
	if($DOC_MOD_TIP_COD=="03"){
		$DOC_CLI_NRO=$r['CLI_DNI'];
		$DOC_CLI_DOC_COD=gs("SUNAT_COD","vclientes","ID=$DOC_CLI_KOD");
	}
	if($DOC_MOD_TIP_COD=="01"){
		$DOC_CLI_NRO=$r['CLI_RUC'];
		$DOC_CLI_DOC_COD="6";//gs("SUNAT_COD","vclientes","ID=$DOC_CLI_KOD");
	}
	
	//$DOC_CLI_DOC_COD=gs("SUNAT_COD","vclientes","ID=$DOC_CLI_KOD");
	$DOC_CLI_NOM=trim($r['CLI_NOM']);
	$DOC_CLI_UBI_COD=$r['CLI_UBI'];
	$DOC_CLI_DIR=$r['CLI_DIR'];
	$DOC_CLI_DEP=$r['CLI_DEP'];
	$DOC_CLI_PRO=$r['CLI_PRV'];
	$DOC_CLI_DIS=$r['CLI_DIS'];
	$DOC_CLI_PAI=$r['CLI_PAI'];
	$DOC_CLI_PAI_COD=gs("pai_cod","lista_paises","pai_nom='$DOC_CLI_PAI'");
	$DOC_CLI_EMA=$r['CLI_EMAIL'];
	//
	$DOC_IGV=round($r['IGV'],2);
	$DOC_STO=round($r['ST'],2);
	$DOC_TOT=round($r['TOT'],2);
	//++++
	if(strlen($NDC_OBS)==0){
		$NDC_OBS=$NDC_MOT_NOM;
	}
	//++++
	$JSON='{
	   "_D":"urn:oasis:names:specification:ubl:schema:xsd:DebitNote-2",
	   "_A":"urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2",
	   "_B":"urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2",
	   "_E" : "urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2",
	   "DebitNote":[
		  {
			 "UBLVersionID":[
				{
				   "_":"2.1"
				}
			 ],
			 
			 "CustomizationID":[
				{
				   "_":"2.0"
				}
			 ],
			 
			 "ID":[
				{
				   "_":"'.$NDC_SER_NUM.'"
				}
			 ],
			 
			 "IssueDate":[
				{
				   "_":"'.$NDC_FEC_EMI.'"
				}
			 ],
			 
			 "Note":[
				{
				   "_":"'.$NDC_TOT_EN_LETRAS.'",
				   "languageID":"en",
				   "languageLocaleID":"1000"
				}
			 ],
			 
			 "DocumentCurrencyCode":[
				{
				   "_":"PEN",
				   "listID":"ISO 4217 Alpha",
				   "listName":"Currency",
				   "listAgencyName":"United Nations Economic Commission for Europe"
				}
			 ],
			 
			 "DiscrepancyResponse":[
				{
				  "ReferenceID":[
			  		{
			  			"_":"'.$NDC_SER_NUM.'"
			  		}
			  	  ],
				  "ResponseCode":[
						{	
							"_":"'.$NDC_MOT_COD.'",
							"listAgencyName":"PE:SUNAT",
							"listName":"Tipo de nota de debito",
							"listURI":"urn:pe:gob:sunat:cpe:see:gem:catalogos:catalogo10"
						}
				  ],
				  "Description":[
						{
							"_":"'.$NDC_OBS.'"
						}
					]
				 }
			 ],
			 
			 "BillingReference":[
				 {
						"InvoiceDocumentReference":[
							{
							   "ID":[
								   {
										"_":"'.$DOC_MOD_SER_NUM.'"
								   }
							   ],
							   "DocumentTypeCode":[
								   {
										"_":"'.$DOC_MOD_TIP_COD.'",
										"listAgencyName":"PE:SUNAT",
										"listName":"Tipo de Documento",
										"listURI":"urn:pe:gob:sunat:cpe:see:gem:catalogos:catalogo01"
								   }
							   ]
							}
					  ]
				 }
			 ],
			 
			 "Signature":[
				{
				   "ID":[
					  {
						 "_":"IDSignature"
					  }
				   ],
				   "SignatoryParty":[
					  {
						 "PartyIdentification":[
							{
							   "ID":[
								  {
									 "_":"'.$EMP_RUC.'"
								  }
							   ]
							}
						 ],
						 "PartyName":[
							{
							   "Name":[
								  {
									 "_":"'.$EMP_NOM.'"
								  }
							   ]
							}
						 ]
					  }
				   ],
				   "DigitalSignatureAttachment":[
					  {
						 "ExternalReference":[
							{
							   "URI":[
								  {
									 "_":"IDSignature"
								  }
							   ]
							}
						 ]
					  }
				   ]
				}
			 ],
			 
			 "AccountingSupplierParty":[
				{
				   "Party":[
					  {
						 "PartyIdentification":[
							{
							   "ID":[
								  {
									 "_":"'.$EMP_RUC.'",
									 "schemeID":"6",
									 "schemeName":"Documento de Identidad",
									 "schemeAgencyName":"PE:SUNAT",
									 "schemeURI":"urn:pe:gob:sunat:cpe:see:gem:catalogos:catalogo06"
								  }
							   ]
							}
						 ],
						 "PartyName":[
							{
							   "Name":[
								  {
									 "_":"'.$EMP_RUC.'"
								  }
							   ]
							}
						 ],
						 "PostalAddress":[
							{
							   "ID":[
								  {
									 "_":"'.$EMP_UBI.'"
								  }
							   ],
							   "StreetName":[
								  {
									 "_":"'.$EMP_DIR.'"
								  }
							   ],
							   "CityName":[
								  {
									 "_":"'.$EMP_DEP.'"
								  }
							   ],
							   "CountrySubentity":[
								  {
									 "_":"'.$EMP_PRV.'"
								  }
							   ],
							   "District":[
								  {
									 "_":"'.$EMP_DIS.'"
								  }
							   ],
							   "Country":[
								  {
									 "IdentificationCode":[
										{
										   "_":"PE"
										}
									 ]
								  }
							   ]
							}
						 ],
						 
						 "PartyLegalEntity":[
							{
							   "RegistrationName":[
								  {
									 "_":"'.$EMP_NOM.'"
								  }
							   ],
							   "RegistrationAddress":[
								  {
					                 "AddressTypeCode":[
										{
										   "_":"0000",
										   "listAgencyName":"PE:SUNAT",
										   "listName":"Establecimientos anexos"
										}
									 ],
									 "CityName":[
										{
										   "_":"'.$EMP_DEP.'"
										}
									 ],
									 "CountrySubentity":[
										{
										   "_":"'.$EMP_PRV.'"
										}
									 ],
									 
									 "District":[
										{
										   "_":"'.$EMP_DIS.'"
										}
									 ],
									 
									 "AddressLine" : [ 
										{
											"Line" : [ 
												{
													"_" : "'.$EMP_DIR.'"
												}
											]
										}
									],
									 "Country":[
										{
										   "IdentificationCode":[
											  {
												 "_":"PE"
											  }
										   ]
										}
									 ]
								  }
							   ]
							}
						 ],
						 
						 "Contact":[
							{
							   "ElectronicMail":[
								  {
									 "_":"'.$EMP_EMA.'"
								  }
							   ]
							}
						 ]
					  }
				   ]
				}
			 ],
			 
			 
			 "AccountingCustomerParty":[
				{
				   "Party":[
					  {
						 "PartyIdentification":[
							{

							   "ID":[
								  {
									 "_":"'.$DOC_CLI_NRO.'",
									 "schemeID":"'.$DOC_CLI_DOC_COD.'",
									 "schemeName":"Documento de Identidad",
									 "schemeAgencyName":"PE:SUNAT",
									 "schemeURI":"urn:pe:gob:sunat:cpe:see:gem:catalogos:catalogo06"
								  }
							   ]
							}
						 ],
						 "PartyName":[
							{
							   "Name":[
								  {
									 "_":"'.$DOC_CLI_NOM.'"
								  }
							   ]
							}
						 ],
						 "PostalAddress":[
							{
							   "ID":[
								  {
									 "_":"'.$DOC_CLI_UBI_COD.'"
								  }
							   ],
							   "StreetName":[
								  {
									 "_":"'.$DOC_CLI_DIR.'"
								  }
							   ],
							   "CityName":[
								  {
									 "_":"'.$DOC_CLI_DEP.'"
								  }
							   ],
							   "CountrySubentity":[
								  {
									 "_":"'.$DOC_CLI_PRO.'"
								  }
							   ],
							   "District":[
								  {
									 "_":"'.$DOC_CLI_DIS.'"
								  }
							   ],
							   "Country":[
								  {
									 "IdentificationCode":[
										{
										   "_":"'.$DOC_CLI_PAI_COD.'"
										}
									 ]
								  }
							   ]
							}
						 ],
						 
						 
						 "PartyLegalEntity":[
							{
							   "RegistrationName":[
								  {
									 "_":"'.$DOC_CLI_NOM.'"
								  }
							   ],

							   "RegistrationAddress":[
								  {
									 "CityName":[
										{
										   "_":"'.$DOC_CLI_PRO.'"
										}
									 ],
									 
									 "CountrySubentity":[
										{
										   "_":"'.$DOC_CLI_DEP.'"
										}
									 ],
									 "District" : [ 
										{
											"_" : "'.$DOC_CLI_DIS.'"
										}
									],	
									"AddressLine" : [ 
                                                    {
                                                        "Line" : [ 
                                                            {
                                                                "_" : "'.$DOC_CLI_DIR.'"
                                                            }
                                                        ]
                                                    }
                                                ],										 								 
									 "Country":[
										{
										   "IdentificationCode":[
											  {
												 "_":"'.$DOC_CLI_PAI_COD.'"
											  }
										   ]
										}
									 ]
								  }
							   ]
							}
						 ],
						 
						 "Contact":[
							{
							   "ElectronicMail":[
								  {
									 "_":"'.$DOC_CLI_EMA.'"
								  }
							   ]
							}
						 ]
					  }
				   ]
				}
			 ],
			 
			 
			 "TaxTotal":[
				{
				   "TaxAmount":[
					  {
						 "_":'.$DOC_IGV.',
						 "currencyID":"PEN"
					  }
				   ],
				   
				   "TaxSubtotal":[
				  	{
						 "TaxableAmount":[
							{
							   "_":'.$DOC_STO.',
							   "currencyID":"PEN"
							}
						 ],					 
						 "TaxAmount":[
							{
							   "_":'.$DOC_IGV.',
							   "currencyID":"PEN"
							}
						 ],
						 
						 "TaxCategory":[
							{
							   "ID":[
								  {
									 "_":"S",
									 "schemeID":"UN/ECE 5305",
									 "schemeName":"Tax Category Identifier",
									 "schemeAgencyName":"United Nations Economic Commission for Europe"
								  }
							   ],
							   
							   "Percent":[
								  {
									 "_":18
								  }
							   ],
							   
							   "TaxScheme":[
								  {
									 "ID":[
										{
										   "_":"1000",
										   "schemeID":"UN/ECE 5153",
										   "schemeAgencyID":"6"
										}
									 ],
									 "Name":[
										{
										   "_":"IGV"
										}
									 ],
									 "TaxTypeCode":[
										{
										   "_":"VAT"
										}
									 ]
								  }
							   ]
							}
						 ]
					  }
				   ]
				}
			 ],
			 
			 
			 "RequestedMonetaryTotal":[
				{
				   "LineExtensionAmount":[
					  {
						 "_":'.$DOC_STO.',
						 "currencyID":"PEN"
					  }
				   ],
				   "TaxInclusiveAmount":[
					  {
						 "_":'.$DOC_TOT.',
						 "currencyID":"PEN"
					  }
				   ],
				   "PayableAmount":[
					  {
						 "_":'.$DOC_TOT.',
						 "currencyID":"PEN"
					  }
				   ]
				}
			 ],';
			 
	//++++++++++++++++++++++++++++++++++++++++++++
	$DETALLE="";
	$DETALLE_TOT=gn("count(*)","fe_ventaz_det","id_fe=$ID_NDC");
	$AGREGAR_COMA=0;
	$CX=0;
	$rs="SELECT * FROM fe_ventaz_det WHERE id_fe=$ID_NDC ORDER BY cx";
	$rs = grs($rs);			
	while ($r = $rs->fetch_assoc()){
		$CX++;
		//
		$PRE_UNI_C_IGV=NF(round($r['pre_']*$r['pie_'],2),2);
		$PRE_UNI_S_IGV=NF(round((($PRE_UNI_C_IGV/118)*100),2),2);
		$IMP_SIN_IGV=NF(round($r['can_']* $PRE_UNI_S_IGV,2),2);
		//
		$ITE_IGV_X_CAN=round(($PRE_UNI_C_IGV - $PRE_UNI_S_IGV) * $r['can_'],2);
		//
		$ITE_DES=$r['cod']."x".$r['pie_']." ".str_replace('"',"''",$r['des']);
		$ITE_COD=$r['cod'];
		$DET_CAN=$r['can_'];	
		//
		$ITEM='
		"DebitNoteLine":[
            {
               "ID":[
                  {
                     "_":'.$CX.'
                  }
               ],
			   "Note":[
                  {
                     "_":"CAJAS"
                  }
               ],            
               "DebitedQuantity":[
                  {
                     "_":"'.$DET_CAN.'",
                     "unitCode":"ZZ",
                     "unitCodeListID":"UN/ECE rec 20",
                     "unitCodeListAgencyName":"United Nations Economic Commission for Europe"
                  }
               ],
               "LineExtensionAmount":[
                  {
                     "_":'.$IMP_SIN_IGV.',
                     "currencyID":"PEN"
                  }
               ],
			   
               "PricingReference":[
                  {
                     "AlternativeConditionPrice":[
                        {
                           "PriceAmount":[
                              {
                                 "_":'.$PRE_UNI_C_IGV.',
                                 "currencyID":"PEN"
                              }
                           ],
                           "PriceTypeCode":[
                              {
                                 "_":"01",
                                 "listName":"Tipo de Precio",
                                 "listAgencyName":"PE:SUNAT",
                                 "listURI":"urn:pe:gob:sunat:cpe:see:gem:catalogos:catalogo16"
                              }
                           ]
                        }
                     ]
                  }
               ],
			   
               "TaxTotal":[
                  {
                     "TaxAmount":[
                        {
                           "_":'.$ITE_IGV_X_CAN.',
                           "currencyID":"PEN"
                        }
                     ],
                     "TaxSubtotal":[
                        {
                           "TaxableAmount":[
                              {
                                 "_":'.$IMP_SIN_IGV.',
                                 "currencyID":"PEN"
                              }
                           ],
                           "TaxAmount":[
                              {
                                 "_":'.$ITE_IGV_X_CAN.',
                                 "currencyID":"PEN"
                              }
                           ],
						   
                           "TaxCategory":[
                              {
				 				"Percent":[
                                    {
                                       "_":"18"
                                    }
                                 ],
                                 "TaxExemptionReasonCode":[
                                    {
                                       "_":"10",
                                       "listAgencyName":"PE:SUNAT",
                                       "listName":"Afectacion del IGV",
                                       "listURI":"urn:pe:gob:sunat:cpe:see:gem:catalogos:catalogo07"
                                    }
                                 ],
                                 "TaxScheme":[
                                    {
                                       "ID":[
                                          {
                                             "_":"1000",
                                             "schemeAgencyName":"PE:SUNAT",
                                             "schemeName":"Codigo de tributos",
											 "schemeURI":"urn:pe:gob:sunat:cpe:see:gem:catalogos:catalogo05"
                                          }
                                       ],
                                       "Name":[
                                          {
                                             "_":"IGV"
                                          }
                                       ],
                                       "TaxTypeCode":[
                                          {
                                             "_":"VAT"
                                          }
                                       ]
                                    }
                                 ]
                              }
                           ]
                        }
                     ]
                  }
               ],
			   
               "Item":[
                  {
                     "Description":[
                        {
                           "_":"'.$ITE_DES.'"
                        }
                     ],
					 "SellersItemIdentification":[
                        {
                           "ID":[
							  {
								 "_":"'.$ITE_COD.'"								 
							  }
						   ]
                        }
                     ]
                  }
               ],
			   
               "Price":[
                  {
                     "PriceAmount":[
                        {
                           "_":'.$PRE_UNI_S_IGV.',
                           "currencyID":"PEN"
                        }
                     ]
                  }
               ]
            }
         ]
		';
		if($CX<$DETALLE_TOT){
			$ITEM=$ITEM.",";
		}
		$DETALLE=$DETALLE."\n".$ITEM;			
	}
	//++++++++++++++++++++++++++++++++++++++++++++
	$JSON=$JSON.$DETALLE;
	$JSON=$JSON."
				}
			   ]
			}
	";
	$FILE_JSON=$EMP_RUC."-08-".$NDC_SER_NUM.".json";
	if(file_exists($FILE_JSON)){
		unlink($FILE_JSON);
	}	
	CREATE_FILE($FILE_JSON,$JSON);	
	return $FILE_JSON;		
}

?>