28require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
39 public $element =
'intracommreport';
44 public $table_element =
'intracommreport';
49 public $fk_element =
'fk_intracommreport';
51 public $picto =
'intracommreport';
65 public $declaration_number;
75 public $type_declaration;
76 public $numero_declaration;
89 public static $type = array(
90 'introduction' =>
'Introduction',
91 'expedition' =>
'Expédition'
104 $this->ismultientitymanaged = 1;
105 $this->exporttype =
'deb';
115 public function create($user, $notrigger = 0)
138 public function delete($user, $notrigger = 0)
151 public function getXML($mode =
'O', $type =
'introduction', $period_reference =
'')
153 global $conf, $mysoc;
156 $party_id = substr(strtr($mysoc->tva_intra, array(
' ' =>
'')), 0, 4).$mysoc->idprof2;
157 $declarant = substr($mysoc->managers, 0, 14);
162 $e =
new SimpleXMLElement(
'<?xml version="1.0" encoding="utf-8" standalone="yes"?><INSTAT></INSTAT>');
164 $envelope = $e->addChild(
'Envelope');
165 $envelope->addChild(
'envelopeId', $conf->global->INTRACOMMREPORT_NUM_AGREMENT);
166 $date_time = $envelope->addChild(
'DateTime');
167 $date_time->addChild(
'date', date(
'Y-m-d'));
168 $date_time->addChild(
'time', date(
'H:i:s'));
169 $party = $envelope->addChild(
'Party');
170 $party->addAttribute(
'partyType', $conf->global->INTRACOMMREPORT_TYPE_ACTEUR);
171 $party->addAttribute(
'partyRole', $conf->global->INTRACOMMREPORT_ROLE_ACTEUR);
172 $party->addChild(
'partyId', $party_id);
173 $party->addChild(
'partyName', $declarant);
174 $envelope->addChild(
'softwareUsed',
'Dolibarr');
175 $declaration = $envelope->addChild(
'Declaration');
176 $declaration->addChild(
'declarationId', $id_declaration);
177 $declaration->addChild(
'referencePeriod', $period_reference);
183 $declaration->addChild(
'PSIId', $psiId);
184 $function = $declaration->addChild(
'Function');
185 $functionCode = $function->addChild(
'functionCode', $mode);
186 $declaration->addChild(
'declarationTypeCode',
getDolGlobalString(
'INTRACOMMREPORT_NIV_OBLIGATION_'.strtoupper($type)));
187 $declaration->addChild(
'flowCode', ($type ==
'introduction' ?
'A' :
'D'));
188 $declaration->addChild(
'currencyCode', $conf->global->MAIN_MONNAIE);
192 $res = $this->
addItemsFact($declaration, $type, $period_reference);
195 $this->errors = array_unique($this->errors);
212 public function getXMLDes($period_year, $period_month, $type_declaration =
'expedition')
216 $e =
new SimpleXMLElement(
'<?xml version="1.0" encoding="utf-8" ?><fichier_des></fichier_des>');
218 $declaration_des = $e->addChild(
'declaration_des');
219 $declaration_des->addChild(
'num_des', self::getDeclarationNumber($this->numero_declaration));
220 $declaration_des->addChild(
'num_tvaFr', $mysoc->tva_intra);
221 $declaration_des->addChild(
'mois_des', (
string) $period_month);
222 $declaration_des->addChild(
'an_des', (
string) $period_year);
225 $res = $this->
addItemsFact($declaration_des, $type_declaration, $period_year.
'-'.$period_month,
'des');
227 $this->errors = array_unique($this->errors);
245 public function addItemsFact(&$declaration, $type, $period_reference, $exporttype =
'deb')
249 require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
253 $resql = $this->db->query($sql);
258 if ($this->db->num_rows($resql) <= 0) {
259 $this->errors[] =
'No data for this period';
263 if ($exporttype ==
'deb' &&
getDolGlobalInt(
'INTRACOMMREPORT_CATEG_FRAISDEPORT') > 0) {
264 $categ_fraisdeport =
new Categorie($this->db);
266 $TLinesFraisDePort = array();
269 while ($res = $this->db->fetch_object($resql)) {
270 if ($exporttype ==
'des') {
273 if (empty($res->fk_pays)) {
275 $this->errors[] =
'Country not filled in for the third party <a href="'.dol_buildpath(
'/societe/soc.php', 1).
'?socid='.$res->id_client.
'">'.$res->nom.
'</a>';
277 if (
getDolGlobalInt(
'INTRACOMMREPORT_CATEG_FRAISDEPORT') > 0 && $categ_fraisdeport->containsObject(
'product', $res->id_prod)) {
278 $TLinesFraisDePort[] = $res;
280 $this->
addItemXMl($declaration, $res, $i,
'');
288 if (!empty($TLinesFraisDePort)) {
289 $this->
addItemFraisDePort($declaration, $TLinesFraisDePort, $type, $categ_fraisdeport, $i);
292 if (count($this->errors) > 0) {
310 global $mysoc, $conf;
312 if ($type ==
'expedition' || $exporttype ==
'des') {
313 $sql =
"SELECT f.ref as refinvoice, f.total_ht";
315 $table_extraf =
'facture_extrafields';
316 $tabledet =
'facturedet';
317 $field_link =
'fk_facture';
319 $sql =
"SELECT f.ref_supplier as refinvoice, f.total_ht";
320 $table =
'facture_fourn';
321 $table_extraf =
'facture_fourn_extrafields';
322 $tabledet =
'facture_fourn_det';
323 $field_link =
'fk_facture_fourn';
325 $sql .=
", l.fk_product, l.qty
326 , p.weight, p.rowid as id_prod, p.customcode
327 , s.rowid as id_client, s.nom, s.zip, s.fk_pays, s.tva_intra
330 FROM ".MAIN_DB_PREFIX.$tabledet.
" l
331 INNER JOIN ".MAIN_DB_PREFIX.$table.
" f ON (f.rowid = l.".$this->db->escape($field_link).
")
332 LEFT JOIN ".MAIN_DB_PREFIX.$table_extraf.
" ext ON (ext.fk_object = f.rowid)
333 INNER JOIN ".MAIN_DB_PREFIX.
"product p ON (p.rowid = l.fk_product)
334 INNER JOIN ".MAIN_DB_PREFIX.
"societe s ON (s.rowid = f.fk_soc)
335 LEFT JOIN ".MAIN_DB_PREFIX.
"c_country c ON (c.rowid = s.fk_pays)
336 WHERE f.fk_statut > 0
337 AND l.product_type = ".($exporttype ==
"des" ? 1 : 0).
"
338 AND f.entity = ".((
int) $conf->entity).
"
339 AND (s.fk_pays <> ".((
int) $mysoc->country_id).
" OR s.fk_pays IS NULL)
340 AND f.datef BETWEEN '".$this->db->escape($period_reference).
"-01' AND '".$this->db->escape($period_reference).
"-".date(
't').
"'";
354 public function addItemXMl(&$declaration, &$res, $i, $code_douane_spe =
'')
356 $item = $declaration->addChild(
'Item');
357 $item->addChild(
'itemNumber', (
string) $i);
358 $cn8 = $item->addChild(
'CN8');
359 if (empty($code_douane_spe)) {
360 $code_douane = $res->customcode;
362 $code_douane = $code_douane_spe;
364 $cn8->addChild(
'CN8Code', $code_douane);
365 $item->addChild(
'MSConsDestCode', $res->code);
366 $item->addChild(
'countryOfOriginCode', substr($res->zip, 0, 2));
367 $item->addChild(
'netMass', (
string) round($res->weight * $res->qty));
368 $item->addChild(
'quantityInSU', (
string) $res->qty);
369 $item->addChild(
'invoicedAmount', (
string) round($res->total_ht));
371 if (!empty($res->tva_intra)) {
372 $item->addChild(
'partnerId', $res->tva_intra);
374 $item->addChild(
'statisticalProcedureCode',
'11');
375 $nature_of_transaction = $item->addChild(
'NatureOfTransaction');
376 $nature_of_transaction->addChild(
'natureOfTransactionACode',
'1');
377 $nature_of_transaction->addChild(
'natureOfTransactionBCode',
'1');
378 $item->addChild(
'modeOfTransportCode', $res->mode_transport);
379 $item->addChild(
'regionCode', substr($res->zip, 0, 2));
392 $item = $declaration->addChild(
'ligne_des');
393 $item->addChild(
'numlin_des', (
string) $i);
394 $item->addChild(
'valeur', (
string) round($res->total_ht));
395 $item->addChild(
'partner_des', $res->tva_intra);
408 public function addItemFraisDePort(&$declaration, &$TLinesFraisDePort, $type, &$categ_fraisdeport, $i)
412 if ($type ==
'expedition') {
414 $tabledet =
'facturedet';
415 $field_link =
'fk_facture';
418 $table =
'facture_fourn';
419 $tabledet =
'facture_fourn_det';
420 $field_link =
'fk_facture_fourn';
421 $more_sql =
'f.ref_supplier';
424 foreach ($TLinesFraisDePort as $res) {
425 $sql =
"SELECT p.customcode
426 FROM ".MAIN_DB_PREFIX.$tabledet.
" d
427 INNER JOIN ".MAIN_DB_PREFIX.$table.
" f ON (f.rowid = d.".$this->db->escape($field_link).
")
428 INNER JOIN ".MAIN_DB_PREFIX.
"product p ON (p.rowid = d.fk_product)
429 WHERE d.fk_product IS NOT NULL
430 AND f.entity = ".((int) $conf->entity).
"
431 AND ".$more_sql.
" = '".$this->db->escape($res->refinvoice).
"'
434 SELECT MAX(d.total_ht)
435 FROM ".MAIN_DB_PREFIX.$tabledet.
" d
436 INNER JOIN ".MAIN_DB_PREFIX.$table.
" f ON (f.rowid = d.".$this->db->escape($field_link).
")
437 WHERE d.fk_product IS NOT NULL
438 AND ".$more_sql.
" = '".$this->db->escape($res->refinvoice).
"'
439 AND d.fk_product NOT IN
442 FROM ".MAIN_DB_PREFIX.
"categorie_product
443 WHERE fk_categorie = ".((int) $categ_fraisdeport->id).
"
447 $resql = $this->db->query($sql);
448 $ress = $this->db->fetch_object($resql);
450 $this->
addItemXMl($declaration, $res, $i, $ress->customcode);
463 $sql =
"SELECT MAX(numero_declaration) as max_declaration_number";
464 $sql .=
" FROM ".MAIN_DB_PREFIX.$this->table_element;
465 $sql .=
" WHERE exporttype = '".$this->db->escape($this->exporttype).
"'";
466 $resql = $this->db->query($sql);
468 $res = $this->db->fetch_object($resql);
471 return (
string) ($res->max_declaration_number + 1);
482 return str_pad($number, 6,
'0', STR_PAD_LEFT);
493 $name = $this->period.
'.xml';
496 $fname = sys_get_temp_dir().
'/'.$name;
498 $f = fopen($fname,
'w+');
499 fwrite($f, $content_xml);
502 header(
'Content-Description: File Transfer');
503 header(
'Content-Type: application/xml');
504 header(
'Content-Disposition: attachment; filename="'.$name.
'"');
505 header(
'Expires: 0');
506 header(
'Cache-Control: must-revalidate');
507 header(
'Pragma: public');
508 header(
'Content-Length: '.filesize($fname));
Class to manage categories.
Parent class of all other business classes (invoices, contracts, proposals, orders,...
Class to manage Dolibarr database access.
Class to manage intracomm report.
static getDeclarationNumber($number)
Verify declaration number.
addItemsFact(&$declaration, $type, $period_reference, $exporttype='deb')
Add line from invoice.
fetch($id)
Function fetch.
const TYPE_DEB
DEB - Product.
const TYPE_DES
DES - Service.
__construct(DoliDB $db)
Constructor.
addItemXMl(&$declaration, &$res, $i, $code_douane_spe='')
Add item for DEB.
getXML($mode='O', $type='introduction', $period_reference='')
Generate XML file.
addItemXMlDes($declaration, &$res, $i)
Add item for DES.
addItemFraisDePort(&$declaration, &$TLinesFraisDePort, $type, &$categ_fraisdeport, $i)
This function adds an item by retrieving the customs code of the product with the highest amount in t...
getNextDeclarationNumber()
Return next reference of declaration not already used (or last reference)
getSQLFactLines($type, $period_reference, $exporttype='deb')
Add invoice line.
generateXMLFile($content_xml)
Generate XML file.
create($user, $notrigger=0)
Function create.
getXMLDes($period_year, $period_month, $type_declaration='expedition')
Generate XMLDes file.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.