diff --git a/html/ajax_misc.php b/html/ajax_misc.php index e79e3b0f8..22c4fb2c3 100644 --- a/html/ajax_misc.php +++ b/html/ajax_misc.php @@ -352,6 +352,7 @@ $path = array( 'list_filter_followup'=>"ajax_follow_up", //delete a filter for followup 'delete_filter_followup'=>"ajax_follow_up", + "check_vatnumber"=>"ajax_check_vatnumber" ) ; if (array_key_exists($op, $path)) { diff --git a/html/js/ajax_fiche.js b/html/js/ajax_fiche.js index a69972b4a..be7a7a4fa 100644 --- a/html/js/ajax_fiche.js +++ b/html/js/ajax_fiche.js @@ -309,5 +309,61 @@ category_card.remove_attribut=function (p_dossier,p_fiche_def_ref,p_object_name, } }); }; +/** + * @brief ajax call to check a VAT number via VIES + * @param p_domid string domid of the IText + * @see ivatnumber.class.php + */ +category_card.check_vatnumber=function(p_domid) { + try + { + var dgbox="info"+p_domid; + waiting_box(); + // For form , most of the parameters are in the FORM + // method is then POST + //var queryString=$(p_form_id).serialize(true); + + var queryString = { + op: 'check_vatnumber', + vatnr:$(p_domid).value, + boxid: dgbox, + p_domid:p_domid + }; + var action = new Ajax.Request( + "ajax_misc.php" , + { + method:'GET', + parameters:queryString, + onFailure:ajax_misc_failure, + onSuccess:function(req){ + remove_waiting_box(); + if (req.responseText == 'NOCONX') { + reconnect(); + return; + } + var answer=req.responseJSON; + + if ( answer.status == 'OK') + { + $(dgbox).update(answer.html); + $(p_domid).value=answer.vat; + $(p_domid).removeClassName("notice") + $(p_domid).addClassName("valid") + } else { + $(p_domid).addClassName("notice"); + $(p_domid).removeClassName("valid"); + $(dgbox).update(answer.html); + } + + } + } + ); + }catch( e) + { + remove_waiting_box(); + console.error(e.message); + } +} //--> + diff --git a/include/ajax/ajax_check_vatnumber.php b/include/ajax/ajax_check_vatnumber.php new file mode 100644 index 000000000..c97655630 --- /dev/null +++ b/include/ajax/ajax_check_vatnumber.php @@ -0,0 +1,97 @@ +get("vatnr"); + $p_domid = $http->get("p_domid"); +} catch (\Exception $e) { + echo $e->getMessage(); + return; +} +$vatnr = strtoupper($vatnr); +$country = substr($vatnr, 0, 2); +$vatnr = preg_replace("/[[:^digit:]]/", '', $vatnr); +$array = array( + "countryCode" => $country, + "vatNumber" => $vatnr, + "requesterMemberStateCode" => "", + "requesterNumber" => "", + "traderName" => "", + "traderStreet" => "", + "traderPostalCode" => "", + "traderCity" => "", + "traderCompanyType" => "" +); + +$curl = curl_init(VATCHECK_URL . "/check-vat-number"); +curl_setopt($curl, CURLOPT_HTTPHEADER, array( + "Content-Type: application/json", + "accept: application/json" + ) +); +curl_setopt($curl, CURLOPT_POST, true); +curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); +curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($array)); +$json = curl_exec($curl); +curl_close($curl); +if (empty($json)) { + $obj = new stdClass; + $obj->status = 'NOK'; + $obj->html=_('Erreur'); + echo json_response($obj); + return; +} + + +$obj = json_decode($json); +if (isset ($obj->actionSucceed) && $obj->actionSucceed == false) +{ + $obj = new stdClass; + $obj->status = 'NOK'; + $obj->html=_('Erreur'); + echo json_response($obj); + return; +} + +if ($obj->valid == true) { + $obj->status = 'OK'; + $obj->vat = $country . $vatnr; + $obj->name = str_replace("\n", ' ', $obj->name ?? ""); + $obj->address = str_replace("\n", ' ', $obj->address ?? ""); + + $obj->html = <<name} {$obj->address} +EOF; + +} else { + $obj->status = 'NOK'; +} + +echo json_response($obj); +?> \ No newline at end of file diff --git a/include/class/card_property.class.php b/include/class/card_property.class.php index 58c7f73d0..33205ea06 100644 --- a/include/class/card_property.class.php +++ b/include/class/card_property.class.php @@ -194,7 +194,15 @@ class Card_Property $result['label']=_("Poste comptable"); $result['class']=" highlight input_text"; return $result; - } elseif ($this->ad_id == ATTR_DEF_TVA) { + + } + elseif ($this->ad_id == ATTR_DEF_NUMTVA) { + /// Propose a button to check VAT + $result['input']=new IVATNumber( "av_text" . $this->ad_id,$this->av_text); + $result['label']=$this->ad_text; + return $result; + } + elseif ($this->ad_id == ATTR_DEF_TVA) { $result['input'] = new ITva_Popup('popup_tva'); $result['input']->table = 0; $result['input']->value = $this->av_text; diff --git a/include/constant.php b/include/constant.php index 94cb7c211..76eb832a4 100644 --- a/include/constant.php +++ b/include/constant.php @@ -364,6 +364,9 @@ if (!defined("NOALYSS_URL")) { if (!defined("DEFAULT_SERVER_VIDEO_CONF")) { define("DEFAULT_SERVER_VIDEO_CONF", "https://www.free-solutions.org/"); } + +define ("VATCHECK_URL","https://ec.europa.eu/taxation_customs/vies/rest-api/"); + /** * @brief load automatically class * diff --git a/include/lib/ivatnumber.class.php b/include/lib/ivatnumber.class.php new file mode 100644 index 000000000..105ea8815 --- /dev/null +++ b/include/lib/ivatnumber.class.php @@ -0,0 +1,86 @@ +itext = new IText($name,$value,$p_id); + $this->itext->title = _("Numéro TVA"); + $this->itext->placeholder = "CO9999999999"; + $this->itext->extra = ""; + $this->itext->style = ' class="input_text" '; + $this->autofocus = false; + } + + function input() + { + if ( $this->readOnly==true) return $this->display(); + + $return = $this->itext->input(); + $return .= $this->button_check_vat(); + $return.=sprintf('
',$this->itext->id); + return $return; + } + + public function getIText(): IText + { + return $this->itext; + } + + public function setIText(IText $itext): IVATNumber + { + $this->itext = $itext; + return $this; + } + + function button_check_vat() + { + $button=new \IButton(uniqid()); + $button->javascript=sprintf("category_card.check_vatnumber('%s')", + $this->itext->id); + $button->extra='style="padding-bottom:0px"'; + $button->label=_("Vérifie"); + return $button->input(); + + + } + function display() + { + return $this->itext->display(); + + } + + static function testme() + { + $ivatnumber=new IVATNumber("av_text13"); + + echo $ivatnumber->input(); + } +} \ No newline at end of file diff --git a/scenario/ivatnumber.test.php b/scenario/ivatnumber.test.php new file mode 100644 index 000000000..6e7f83917 --- /dev/null +++ b/scenario/ivatnumber.test.php @@ -0,0 +1,24 @@ +