<?php
//++++++++++++++++++++++++++++++++++++++++
function ANULAR_GEN_SER_NUM($FEC_HOY){
	$HOY=$FEC_HOY;//YYYY-mm-dd = date("Y-m-d");
	$SER_YYYY_MM=date("Ymd");
	$CX=gn("count(*)","fe_bajas_control","bc_fec='$HOY'");
	if($CX==0){
		$ID=GenId("id_bc","fe_bajas_control");		
		$s="INSERT INTO fe_bajas_control VALUES ($ID,'$HOY','$SER_YYYY_MM',0)";
		ejt($s);
	}
	$CX=gn("bc_cx","fe_bajas_control","bc_fec='$HOY'")+1;
	$XX="RA-".$SER_YYYY_MM."-".vbr("00000".$CX,5);
	return $XX;
}
//
function ANULAR_ACT_SER_NUM($FEC_HOY,$VEN_ID,$FE_ANU_FILE){
	$s="UPDATE fe_bajas_control SET bc_cx=bc_cx + 1 WHERE bc_fec='$FEC_HOY'";
	ejt($s);
	//
	$DET_FEC=date("Y-m-d");
	$DET_HOR=GetHoraAP();
	$DET_TIM=time();
	$ID=GenId("id_bcd","fe_bajas_det");
	$s="INSERT INTO fe_bajas_det VALUES ($ID,$VEN_ID,'$DET_FEC','$DET_HOR',$DET_TIM,'$FE_ANU_FILE')";
	ejt($s);	
}
//
function ANULAR_GEN_JSON($VEN_ID,$FEC_HOY){
	$BAJA_SER_NUM=ANULAR_GEN_SER_NUM($FEC_HOY);
	//
	$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_FEC=$r['FEC'];
	//
	$COD_TIP_DOC="01";//FACTURA
	if($r['COM']=="BOL. ELEC."){
		$COD_TIP_DOC="03";
	}
	//
	if($COD_TIP_DOC=="01"){
		$DOC_SER="F".vbr("000".$r['SER'],3);
		$DOC_NUM=vbr("00000000".$r['NUM'],8);
	}else{
		$DOC_SER="B".vbr("000".$r['SER'],3);
		$DOC_NUM=vbr("00000000".$r['NUM'],8);
	}
	//
	$OBSERVACION="ANULACION DE COMPROBANTE : ".$DOC_SER."-".$DOC_NUM;
	//
	$FEC_HOY=$FEC_HOY;
	$JSON='{
	   "_D":"urn:sunat:names:specification:ubl:peru:schema:xsd:VoidedDocuments-1",
	   "_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",
	   "_SUNAT":"urn:sunat:names:specification:ubl:peru:schema:xsd:SunatAggregateComponents-1",
			"VoidedDocuments":[
				{
					"UBLVersionID":[
						{
							"_":"2.0"
						}
					],
					"CustomizationID":[
						{
							"_":"1.0"
						}
					],
					"ID":[
						{
							"_":"'.$BAJA_SER_NUM.'"
						}
					],
					"ReferenceDate":[
						{
							"_":"'.$DOC_FEC.'"
						}
					],
					"IssueDate":[
						{
							"_":"'.$FEC_HOY.'"
						}
					],
					"Signature":[
						{
							"ID":[
								{
									"_":"IDSignature"
								}
							],
							"SignatoryParty":[
								{
									"PartyIdentification":[
										{
											"ID" : [ 
												{
													"_":"'.$EMP_RUC.'"
												}
											]
										}
									],
									"PartyName":[
										{
											"Name":[
												{
													"_":"'.$EMP_NOM.'"
												}
											]
										}
									]
								}
							],
							"DigitalSignatureAttachment":[
								{
									"ExternalReference":[
										{
											"URI":[
												{
													"_":"IDSignature"
												}
											]
										}
									]
								}
							]
						}
					],
					"AccountingSupplierParty":[
						{
							"CustomerAssignedAccountID":[
								{
									"_":"'.$EMP_RUC.'"
								}
							],
							"AdditionalAccountID":[
								{
									"_":"6"
								}
							],
							"Party":[
								{
									"PostalAddress":[
										{
											"ID":[
												{
													"_":"'.$EMP_UBI.'"
												}
											],
											"StreetName":[
												{
													"_":"'.$EMP_DIR.'"
												}
											],
											"CityName":[
												{
													"_":"'.$EMP_DEP.'"
												}
											],
											"CountrySubentity":[
												{
													"_":"'.$EMP_PRV.'"
												}
											],
											"District":[
												{
													"_":"'.$EMP_DIS.'"
												}
											],
											"Country":[
												{
													"IdentificationCode":[
														{
															"_":"PE"
														}
													]
												}
											]
										}
									],
									"PartyLegalEntity":[
										{
											"RegistrationName":[
												{
													"_":"'.$EMP_NOM.'"
												}
											]
										}
									]
								}
							]
						}
					],
					"VoidedDocumentsLine":[
						{
							"LineID":[
								{
									"_":1
								}
							],
							"DocumentTypeCode":[
								{
									"_":"'.$COD_TIP_DOC.'"
								}
							],
							"DocumentSerialID":[
								{
									"_":"'.$DOC_SER.'"
								}
							],
							"DocumentNumberID":[
								{
									"_":"'.$DOC_NUM.'"
								}
							],
							"VoidReasonDescription":[
								{
									"_":"'.$OBSERVACION.'"
								}
							]
						}
					]
				}
			]
		}';
	$FILE_JSON=$EMP_RUC."-".$BAJA_SER_NUM.".json";
	if(file_exists($FILE_JSON)){
		unlink($FILE_JSON);
	}	
	CREATE_FILE($FILE_JSON,$JSON);	
	return $FILE_JSON;	
}
//
function ANULAR_GEN_JSON_NDC($VEN_ID,$FEC_HOY){
	$BAJA_SER_NUM=ANULAR_GEN_SER_NUM($FEC_HOY);
	//
	$r=gr("vis_fe_ventaz","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_FEC=$r['FEC'];
	//++
	$COD_TIP_DOC="07";
	$NDC_PRJ="X";	
	//++
	$DOC_MOD_TIP=$r['COM_M'];	
	if($DOC_MOD_TIP=="BOL. ELEC."){
		$DOC_SER="B".vbr("000".$r['SER'],3);
		$DOC_NUM=vbr("00000000".$r['NUM'],8);
	}
	if($DOC_MOD_TIP=="FACT. ELEC."){
		$DOC_SER="F".vbr("000".$r['SER'],3);
		//$DOC_SER="F000";
		$DOC_NUM=vbr("00000000".$r['NUM'],8);
	}
	//
	$OBSERVACION="ANULACION DE NOTA DE CREDITO : ".$DOC_SER."-".$DOC_NUM;
	//
	$FEC_HOY=$FEC_HOY;
	$JSON='{
	   "_D":"urn:sunat:names:specification:ubl:peru:schema:xsd:VoidedDocuments-1",
	   "_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",
	   "_SUNAT":"urn:sunat:names:specification:ubl:peru:schema:xsd:SunatAggregateComponents-1",
			"VoidedDocuments":[
				{
					"UBLVersionID":[
						{
							"_":"2.0"
						}
					],
					"CustomizationID":[
						{
							"_":"1.0"
						}
					],
					"ID":[
						{
							"_":"'.$BAJA_SER_NUM.'"
						}
					],
					"ReferenceDate":[
						{
							"_":"'.$DOC_FEC.'"
						}
					],
					"IssueDate":[
						{
							"_":"'.$FEC_HOY.'"
						}
					],
					"Signature":[
						{
							"ID":[
								{
									"_":"IDSignature"
								}
							],
							"SignatoryParty":[
								{
									"PartyIdentification":[
										{
											"ID" : [ 
												{
													"_":"'.$EMP_RUC.'"
												}
											]
										}
									],
									"PartyName":[
										{
											"Name":[
												{
													"_":"'.$EMP_NOM.'"
												}
											]
										}
									]
								}
							],
							"DigitalSignatureAttachment":[
								{
									"ExternalReference":[
										{
											"URI":[
												{
													"_":"IDSignature"
												}
											]
										}
									]
								}
							]
						}
					],
					"AccountingSupplierParty":[
						{
							"CustomerAssignedAccountID":[
								{
									"_":"'.$EMP_RUC.'"
								}
							],
							"AdditionalAccountID":[
								{
									"_":"6"
								}
							],
							"Party":[
								{
									"PostalAddress":[
										{
											"ID":[
												{
													"_":"'.$EMP_UBI.'"
												}
											],
											"StreetName":[
												{
													"_":"'.$EMP_DIR.'"
												}
											],
											"CityName":[
												{
													"_":"'.$EMP_DEP.'"
												}
											],
											"CountrySubentity":[
												{
													"_":"'.$EMP_PRV.'"
												}
											],
											"District":[
												{
													"_":"'.$EMP_DIS.'"
												}
											],
											"Country":[
												{
													"IdentificationCode":[
														{
															"_":"PE"
														}
													]
												}
											]
										}
									],
									"PartyLegalEntity":[
										{
											"RegistrationName":[
												{
													"_":"'.$EMP_NOM.'"
												}
											]
										}
									]
								}
							]
						}
					],
					"VoidedDocumentsLine":[
						{
							"LineID":[
								{
									"_":1
								}
							],
							"DocumentTypeCode":[
								{
									"_":"'.$COD_TIP_DOC.'"
								}
							],
							"DocumentSerialID":[
								{
									"_":"'.$DOC_SER.'"
								}
							],
							"DocumentNumberID":[
								{
									"_":"'.$DOC_NUM.'"
								}
							],
							"VoidReasonDescription":[
								{
									"_":"'.$OBSERVACION.'"
								}
							]
						}
					]
				}
			]
		}';
	$FILE_JSON=$EMP_RUC."-".$BAJA_SER_NUM.".json";
	if(file_exists($FILE_JSON)){
		unlink($FILE_JSON);
	}	
	CREATE_FILE($FILE_JSON,$JSON);	
	return $FILE_JSON;	
}
//
function ANULAR_GEN_JSON_NDD($VEN_ID,$FEC_HOY){
	$BAJA_SER_NUM=ANULAR_GEN_SER_NUM($FEC_HOY);
	//
	$r=gr("vis_fe_ventaz","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_FEC=$r['FEC'];
	//++
	$COD_TIP_DOC="08";
	$NDC_PRJ="X";	
	//++
	$DOC_MOD_TIP=$r['COM_M'];	
	if($DOC_MOD_TIP=="BOL. ELEC."){
		$DOC_SER="B".vbr("000".$r['SER'],3);
		$DOC_NUM=vbr("00000000".$r['NUM'],8);
	}
	if($DOC_MOD_TIP=="FACT. ELEC."){
		$DOC_SER="F".vbr("000".$r['SER'],3);
		$DOC_NUM=vbr("00000000".$r['NUM'],8);
	}
	//
	$OBSERVACION="ANULACION DE NOTA DE CREDITO : ".$DOC_SER."-".$DOC_NUM;
	//
	$FEC_HOY=$FEC_HOY;
	$JSON='{
	   "_D":"urn:sunat:names:specification:ubl:peru:schema:xsd:VoidedDocuments-1",
	   "_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",
	   "_SUNAT":"urn:sunat:names:specification:ubl:peru:schema:xsd:SunatAggregateComponents-1",
			"VoidedDocuments":[
				{
					"UBLVersionID":[
						{
							"_":"2.0"
						}
					],
					"CustomizationID":[
						{
							"_":"1.0"
						}
					],
					"ID":[
						{
							"_":"'.$BAJA_SER_NUM.'"
						}
					],
					"ReferenceDate":[
						{
							"_":"'.$DOC_FEC.'"
						}
					],
					"IssueDate":[
						{
							"_":"'.$FEC_HOY.'"
						}
					],
					"Signature":[
						{
							"ID":[
								{
									"_":"IDSignature"
								}
							],
							"SignatoryParty":[
								{
									"PartyIdentification":[
										{
											"ID" : [ 
												{
													"_":"'.$EMP_RUC.'"
												}
											]
										}
									],
									"PartyName":[
										{
											"Name":[
												{
													"_":"'.$EMP_NOM.'"
												}
											]
										}
									]
								}
							],
							"DigitalSignatureAttachment":[
								{
									"ExternalReference":[
										{
											"URI":[
												{
													"_":"IDSignature"
												}
											]
										}
									]
								}
							]
						}
					],
					"AccountingSupplierParty":[
						{
							"CustomerAssignedAccountID":[
								{
									"_":"'.$EMP_RUC.'"
								}
							],
							"AdditionalAccountID":[
								{
									"_":"6"
								}
							],
							"Party":[
								{
									"PostalAddress":[
										{
											"ID":[
												{
													"_":"'.$EMP_UBI.'"
												}
											],
											"StreetName":[
												{
													"_":"'.$EMP_DIR.'"
												}
											],
											"CityName":[
												{
													"_":"'.$EMP_DEP.'"
												}
											],
											"CountrySubentity":[
												{
													"_":"'.$EMP_PRV.'"
												}
											],
											"District":[
												{
													"_":"'.$EMP_DIS.'"
												}
											],
											"Country":[
												{
													"IdentificationCode":[
														{
															"_":"PE"
														}
													]
												}
											]
										}
									],
									"PartyLegalEntity":[
										{
											"RegistrationName":[
												{
													"_":"'.$EMP_NOM.'"
												}
											]
										}
									]
								}
							]
						}
					],
					"VoidedDocumentsLine":[
						{
							"LineID":[
								{
									"_":1
								}
							],
							"DocumentTypeCode":[
								{
									"_":"'.$COD_TIP_DOC.'"
								}
							],
							"DocumentSerialID":[
								{
									"_":"'.$DOC_SER.'"
								}
							],
							"DocumentNumberID":[
								{
									"_":"'.$DOC_NUM.'"
								}
							],
							"VoidReasonDescription":[
								{
									"_":"'.$OBSERVACION.'"
								}
							]
						}
					]
				}
			]
		}';
	$FILE_JSON=$EMP_RUC."-".$BAJA_SER_NUM.".json";
	if(file_exists($FILE_JSON)){
		unlink($FILE_JSON);
	}	
	CREATE_FILE($FILE_JSON,$JSON);	
	return $FILE_JSON;	
}
?>