<?php
//+++++
function FACTURA_GEN_JSON($VEN_ID){
	$r=gr("vventas_fe","ID=$VEN_ID");
	$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'];
	//
	$DOC_SER="F".vbr("000".$r['SER'],3);
	$DOC_NUM=vbr("00000000".$r['NUM'],8);
		$DOC_SER_NUM=$DOC_SER."-".$DOC_NUM;
	$DOC_FEC=$r['FEC'];
	$DOC_HOR="12:00:00";
	$DOC_TOT_EN_LETRAS=n2l(gn("tot","ventas","id_venta=".$VEN_ID));
	//
	$DOC_CLI_RUC=$r['RUC'];
	$DOC_CLI_NOM=$r['CLI'];
	$DOC_CLI_DIR=$r['DIR'];
	$DOC_CLI_EMA=$r['EMAIL'];
	//
	$DOC_CLI_PAI=$r['UBI_PAI'];
	$DOC_CLI_PAI_COD=gs("pai_cod","lista_paises","pai_nom='$DOC_CLI_PAI'");
	$DOC_CLI_DEP=$r['UBI_DEP'];
	$DOC_CLI_PRO=$r['UBI_PRO'];
	$DOC_CLI_DIS=$r['UBI_DIS'];
	$DOC_CLI_UBI_COD=$r['UBI_COD'];
	//
	$DOC_IGV=round($r['IGV'],2);
	$DOC_STO=round($r['ST'],2);
	$DOC_TOT=round($r['TOT'],2);
	//
	$JSON='{
		
		   "_D":"urn:oasis:names:specification:ubl:schema:xsd:Invoice-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",
		   
		   "Invoice":[
			{
				"UBLVersionID":[
					{
					   "_":"2.1"
					}
				 ],
				 
				 "CustomizationID":[
					{
					   "_":"2.0"
					}
				 ],
				 
				 "ID":[
					{
					   "_":"'.$DOC_SER_NUM.'"
					}
         		 ],
				 
				 "IssueDate":[
					{
					   "_":"'.$DOC_FEC.'"
					}
				 ],
				 
				 "IssueTime":[
					{
					   "_":"'.$DOC_HOR.'"
					}
        		 ],
				 
				 "DueDate" : [ 
					{
						"_" : "'.$DOC_FEC.'"
					}
				 ],
				 
				 "InvoiceTypeCode":[
					{
					   "_":"01",
				   	   "listID":"0101",
					   "name":"Tipo de Operacion",
					   "listSchemeURI":"urn:pe:gob:sunat:cpe:see:gem:catalogos:catalogo51"
					}
				 ],
				 
				 "Note":[
					{
					   "_":"'.$DOC_TOT_EN_LETRAS.'",
					   "languageID":"en",
					   "languageLocaleID":"1000"
					}
				 ],
				 
				 "DocumentCurrencyCode":[
					{
					   "_":"PEN",
					   "listID":"ISO 4217 Alpha",
					   "listName":"Currency",
					   "listAgencyName":"United Nations Economic Commission for Europe"
					}
				 ],		 
				 
				 
				 
				 "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_NOM.'"
									  }
								   ]
								}
							 ],
							 "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"
											}
										 ],
										  "CitySubdivisionName":[
                                                  {
                                                      "_":"'.$EMP_DIS.'"
                                                  }
                                              ],
										 "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_RUC.'",
										 "schemeID":"6",
										 "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.'"
									  }
								   ]
								}
							 ]
						  }
					   ]
					}
				 ],

				 
				 				 
				 "PaymentTerms":[
					{
						"ID":[
							{
								"_":"FormaPago"
							}
						],
						"PaymentMeansID":[
							{
								"_":"Contado"
							}
						]
					}
				],
				 
				 
				 
				 
				 "TaxTotal":[
					{
					   "TaxAmount":[
						  {
							 "_":'.$DOC_IGV.',
							 "currencyID":"PEN"
						  }
					   ],
					   
					   "TaxSubtotal":[
						  {
							 "TaxableAmount":[
								{
								   "_":'.$DOC_STO.',
								   "currencyID":"PEN"
								}
							 ],
							 "TaxAmount":[
								{
								   "_":'.$DOC_IGV.',
								   "currencyID":"PEN"
								}
							 ],
							 "TaxCategory":[
								{
								   "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(*)","pri_ven_det","ID=$VEN_ID");
	$AGREGAR_COMA=0;
	$CX=0;
	$rs="SELECT * FROM pri_ven_det WHERE ID=$VEN_ID 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='
		"InvoiceLine":[
            {
               "ID":[
                  {
                     "_":'.$CX.'
                  }
               ],
			   "Note":[
                  {
                     "_":"CAJAS"
                  }
               ],                           
               "InvoicedQuantity":[
                  {
                     "_":"'.$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."-01-".$DOC_SER."-".$DOC_NUM.".json";
	if(file_exists($FILE_JSON)){
		unlink($FILE_JSON);
	}	
	CREATE_FILE($FILE_JSON,$JSON);	
	return $FILE_JSON;	
}
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
function FACTURA_GEN_JSON_CREDITO($VEN_ID){
	$r=gr("vventas_fe","ID=$VEN_ID");
	$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'];
	//
	$DOC_SER="F".vbr("000".$r['SER'],3);
	$DOC_NUM=vbr("00000000".$r['NUM'],8);
		$DOC_SER_NUM=$DOC_SER."-".$DOC_NUM;
	$DOC_FEC=$r['FEC'];
	$DOC_HOR="12:00:00";
	$DOC_TOT_EN_LETRAS=n2l(gn("tot","ventas","id_venta=".$VEN_ID));
	//
	$DOC_CLI_RUC=$r['RUC'];
	$DOC_CLI_NOM=$r['CLI'];
	$DOC_CLI_DIR=$r['DIR'];
	$DOC_CLI_EMA=$r['EMAIL'];
	//
	$DOC_CLI_PAI=$r['UBI_PAI'];
	$DOC_CLI_PAI_COD=gs("pai_cod","lista_paises","pai_nom='$DOC_CLI_PAI'");
	$DOC_CLI_DEP=$r['UBI_DEP'];
	$DOC_CLI_PRO=$r['UBI_PRO'];
	$DOC_CLI_DIS=$r['UBI_DIS'];
	$DOC_CLI_UBI_COD=$r['UBI_COD'];
	//
	$DOC_IGV=round($r['IGV'],2);
	$DOC_STO=round($r['ST'],2);
	$DOC_TOT=round($r['TOT'],2);
	//
	$DOC_FEC_VEN=gs("MAX(fec)","ventas_cuotas","id_venta=$VEN_ID");
	//
	$JSON='{
		
		   "_D":"urn:oasis:names:specification:ubl:schema:xsd:Invoice-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",
		   
		   "Invoice":[
			{
				"UBLVersionID":[
					{
					   "_":"2.1"
					}
				 ],
				 
				 "CustomizationID":[
					{
					   "_":"2.0"
					}
				 ],
				 
				 "ID":[
					{
					   "_":"'.$DOC_SER_NUM.'"
					}
         		 ],
				 
				 "IssueDate":[
					{
					   "_":"'.$DOC_FEC.'"
					}
				 ],
				 
				 "IssueTime":[
					{
					   "_":"'.$DOC_HOR.'"
					}
        		 ],
				 
				 "DueDate" : [ 
					{
						"_" : "'.$DOC_FEC_VEN.'"
					}
				 ],
				 
				 "InvoiceTypeCode":[
					{
					   "_":"01",
				   	   "listID":"0101",
					   "name":"Tipo de Operacion",
					   "listSchemeURI":"urn:pe:gob:sunat:cpe:see:gem:catalogos:catalogo51"
					}
				 ],
				 
				 "Note":[
					{
					   "_":"'.$DOC_TOT_EN_LETRAS.'",
					   "languageID":"en",
					   "languageLocaleID":"1000"
					}
				 ],
				 
				 "DocumentCurrencyCode":[
					{
					   "_":"PEN",
					   "listID":"ISO 4217 Alpha",
					   "listName":"Currency",
					   "listAgencyName":"United Nations Economic Commission for Europe"
					}
				 ],		 
				 
				 
				 
				 "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_NOM.'"
									  }
								   ]
								}
							 ],
							 "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"
											}
										 ],
										  "CitySubdivisionName":[
                                                  {
                                                      "_":"'.$EMP_DIS.'"
                                                  }
                                              ],
										 "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_RUC.'",
										 "schemeID":"6",
										 "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.'"
									  }
								   ]
								}
							 ]
						  }
					   ]
					}
				 ],

				 
				 				 
				 "PaymentTerms":[
					{
						"ID":[
							{
								"_":"FormaPago"
							}
						],
						"PaymentMeansID":[
							{
								"_":"Credito"
							}
						],
                        "Amount" : [ 
                            {
                                "_" : "'.$DOC_TOT.'",
                                "currencyID" : "PEN"
                            }
                        ]
					},';
	//++			 
	$CUOTAS="";			
	$k_cx=0;
	$k_tx=gn("count(*)","ventas_cuotas","id_venta=$VEN_ID"); 
	$k="SELECT * FROM ventas_cuotas WHERE id_venta=$VEN_ID ORDER BY fec";
	$rk = grs($k);
	while($k=$rk->fetch_assoc()){
		$k_cx++;
		$CUOTAS_ITEM="";
		$CUOTA_NRO="Cuota".vbr("000".$k_cx,3);
		if($k_tx>$k_cx){
			$CUOTAS_ITEM='{
							"ID" : [ 
								{
									"_" : "FormaPago"
								}
							],
							"PaymentMeansID" : [ 
								{
									"_" : "'.$CUOTA_NRO.'"
								}
							],
							"Amount" : [ 
								{
									"_" : "'.$k['tot'].'",
									"currencyID" : "PEN"
								}
							],
							"PaymentDueDate" : [ 
								{
									"_" : "'.$k['fec'].'"
								}
							]
						},';
		}else{
			$CUOTAS_ITEM='{
							"ID" : [ 
								{
									"_" : "FormaPago"
								}
							],
							"PaymentMeansID" : [ 
								{
									"_" : "'.$CUOTA_NRO.'"
								}
							],
							"Amount" : [ 
								{
									"_" : "'.$k['tot'].'",
									"currencyID" : "PEN"
								}
							],
							"PaymentDueDate" : [ 
								{
									"_" : "'.$k['fec'].'"
								}
							]
						}';
			
		}
		$CUOTAS=$CUOTAS.$CUOTAS_ITEM;
	}
	//++			 
	$JSON=$JSON.$CUOTAS.'],
	
				"TaxTotal":[
					{
					   "TaxAmount":[
						  {
							 "_":'.$DOC_IGV.',
							 "currencyID":"PEN"
						  }
					   ],
					   
					   "TaxSubtotal":[
						  {
							 "TaxableAmount":[
								{
								   "_":'.$DOC_STO.',
								   "currencyID":"PEN"
								}
							 ],
							 "TaxAmount":[
								{
								   "_":'.$DOC_IGV.',
								   "currencyID":"PEN"
								}
							 ],
							 "TaxCategory":[
								{
								   "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(*)","pri_ven_det","ID=$VEN_ID");
	$AGREGAR_COMA=0;
	$CX=0;
	$rs="SELECT * FROM pri_ven_det WHERE ID=$VEN_ID 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_IGV_X_CAN=round($IMP_SIN_IGV * 0.18 ,2);
		//echo $IMP_SIN_IGV."<br>";
		//echo $ITE_IGV_X_CAN."<br>";
		//
		$ITE_DES=$r['COD']."x".$r['PIE']." ".str_replace('"',"''",$r['DES']);
		$ITE_COD=$r['COD'];
		$DET_CAN=$r['CAN'];	
		//
		$ITEM='
		"InvoiceLine":[
            {
               "ID":[
                  {
                     "_":'.$CX.'
                  }
               ],
			   "Note":[
                  {
                     "_":"CAJAS"
                  }
               ],                           
               "InvoicedQuantity":[
                  {
                     "_":"'.$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."-01-".$DOC_SER."-".$DOC_NUM.".json";
	if(file_exists($FILE_JSON)){
		unlink($FILE_JSON);
	}	
	CREATE_FILE($FILE_JSON,$JSON);	
	return $FILE_JSON;	
}
?>