25 require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobject.class.php';
35 public $element =
'intracommreport';
40 public $table_element =
'intracommreport';
45 public $fk_element =
'fk_intracommreport';
50 public $declaration_number;
56 public $ismultientitymanaged = 1;
68 public static $type = array(
69 'introduction'=>
'Introduction',
70 'expedition'=>
'Expédition'
81 $this->exporttype =
'deb';
90 public function create($user, $notrigger = 0)
112 public function delete($id, $user, $notrigger = 0)
125 public function getXML($mode =
'O', $type =
'introduction', $period_reference =
'')
128 global $conf, $mysoc;
131 $party_id = substr(strtr($mysoc->tva_intra, array(
' '=>
'')), 0, 4).$mysoc->idprof2;
132 $declarant = substr($mysoc->managers, 0, 14);
133 $id_declaration = self::getDeclarationNumber($this->numero_declaration);
137 $e =
new SimpleXMLElement(
'<?xml version="1.0" encoding="utf-8" standalone="yes"?><INSTAT></INSTAT>');
139 $enveloppe = $e->addChild(
'Envelope');
140 $enveloppe->addChild(
'envelopeId', $conf->global->INTRACOMMREPORT_NUM_AGREMENT);
141 $date_time = $enveloppe->addChild(
'DateTime');
142 $date_time->addChild(
'date', date(
'Y-m-d'));
143 $date_time->addChild(
'time', date(
'H:i:s'));
144 $party = $enveloppe->addChild(
'Party');
145 $party->addAttribute(
'partyType', $conf->global->INTRACOMMREPORT_TYPE_ACTEUR);
146 $party->addAttribute(
'partyRole', $conf->global->INTRACOMMREPORT_ROLE_ACTEUR);
147 $party->addChild(
'partyId', $party_id);
148 $party->addChild(
'partyName', $declarant);
149 $enveloppe->addChild(
'softwareUsed',
'Dolibarr');
150 $declaration = $enveloppe->addChild(
'Declaration');
151 $declaration->addChild(
'declarationId', $id_declaration);
152 $declaration->addChild(
'referencePeriod', $period_reference);
153 if ($conf->global->INTRACOMMREPORT_TYPE_ACTEUR ===
'PSI') {
158 $declaration->addChild(
'PSIId', $psiId);
159 $function = $declaration->addChild(
'Function');
160 $functionCode = $function->addChild(
'functionCode', $mode);
161 $declaration->addChild(
'declarationTypeCode', $conf->global->{
'INTRACOMMREPORT_NIV_OBLIGATION_'.strtoupper($type)});
162 $declaration->addChild(
'flowCode', ($type ==
'introduction' ?
'A' :
'D'));
163 $declaration->addChild(
'currencyCode', $conf->global->MAIN_MONNAIE);
167 $res = $this->
addItemsFact($declaration, $type, $period_reference);
170 $this->errors = array_unique($this->errors);
187 public function getXMLDes($period_year, $period_month, $type_declaration =
'expedition')
191 $e =
new SimpleXMLElement(
'<?xml version="1.0" encoding="utf-8" ?><fichier_des></fichier_des>');
193 $declaration_des = $e->addChild(
'declaration_des');
194 $declaration_des->addChild(
'num_des', self::getDeclarationNumber($this->numero_declaration));
195 $declaration_des->addChild(
'num_tvaFr', $mysoc->tva_intra);
196 $declaration_des->addChild(
'mois_des', $period_month);
197 $declaration_des->addChild(
'an_des', $period_year);
200 $res = $this->addItemsFact($declaration_des, $type_declaration, $period_year.
'-'.$period_month,
'des');
203 $this->errors = array_unique($this->errors);
221 public function addItemsFact(&$declaration, $type, $period_reference, $exporttype =
'deb')
225 require_once DOL_DOCUMENT_ROOT.
'/categories/class/categorie.class.php';
227 $sql = $this->getSQLFactLines($type, $period_reference, $exporttype);
234 if (empty(
$resql->num_rows)) {
235 $this->errors[] =
'No data for this period';
239 if ($exporttype ==
'deb' && $conf->global->INTRACOMMREPORT_CATEG_FRAISDEPORT > 0) {
241 $categ_fraisdeport->fetch($conf->global->INTRACOMMREPORT_CATEG_FRAISDEPORT);
242 $TLinesFraisDePort = array();
245 while ($res = $this->
db->fetch_object(
$resql)) {
246 if ($exporttype ==
'des') {
247 $this->addItemXMlDes($declaration, $res, $i);
249 if (empty($res->fk_pays)) {
251 $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>';
253 if ($conf->global->INTRACOMMREPORT_CATEG_FRAISDEPORT > 0 && $categ_fraisdeport->containsObject(
'product', $res->id_prod)) {
254 $TLinesFraisDePort[] = $res;
256 $this->addItemXMl($declaration, $res, $i,
'');
264 if (!empty($TLinesFraisDePort)) {
265 $this->addItemFraisDePort($declaration, $TLinesFraisDePort, $type, $categ_fraisdeport, $i);
268 if (count($this->errors) > 0) {
286 global $mysoc, $conf;
288 if ($type ==
'expedition' || $exporttype ==
'des') {
289 $sql =
'SELECT f.ref as refinvoice, f.total as total_ht';
291 $table_extraf =
'facture_extrafields';
292 $tabledet =
'facturedet';
293 $field_link =
'fk_facture';
295 $sql =
'SELECT f.ref_supplier as refinvoice, f.total_ht';
296 $table =
'facture_fourn';
297 $table_extraf =
'facture_fourn_extrafields';
298 $tabledet =
'facture_fourn_det';
299 $field_link =
'fk_facture_fourn';
301 $sql .=
', l.fk_product, l.qty
302 , p.weight, p.rowid as id_prod, p.customcode
303 , s.rowid as id_client, s.nom, s.zip, s.fk_pays, s.tva_intra
306 FROM '.MAIN_DB_PREFIX.$tabledet.
' l
307 INNER JOIN '.MAIN_DB_PREFIX.$table.
' f ON (f.rowid = l.'.$field_link.
')
308 LEFT JOIN '.MAIN_DB_PREFIX.$table_extraf.
' ext ON (ext.fk_object = f.rowid)
309 INNER JOIN '.MAIN_DB_PREFIX.
'product p ON (p.rowid = l.fk_product)
310 INNER JOIN '.MAIN_DB_PREFIX.
'societe s ON (s.rowid = f.fk_soc)
311 LEFT JOIN '.MAIN_DB_PREFIX.
'c_country c ON (c.rowid = s.fk_pays)
312 WHERE f.fk_statut > 0
313 AND l.product_type = '.($exporttype ==
'des' ? 1 : 0).
'
314 AND f.entity = '.$conf->entity.
'
315 AND (s.fk_pays <> '.$mysoc->country_id.
' OR s.fk_pays IS NULL)
316 AND f.datef BETWEEN "'.$period_reference.
'-01" AND "'.$period_reference.
'-'.date(
't').
'"';
330 public function addItemXMl(&$declaration, &$res, $i, $code_douane_spe =
'')
332 $item = $declaration->addChild(
'Item');
333 $item->addChild(
'itemNumber', $i);
334 $cn8 = $item->addChild(
'CN8');
335 if (empty($code_douane_spe)) {
336 $code_douane = $res->customcode;
338 $code_douane = $code_douane_spe;
340 $cn8->addChild(
'CN8Code', $code_douane);
341 $item->addChild(
'MSConsDestCode', $res->code);
342 $item->addChild(
'countryOfOriginCode', substr($res->zip, 0, 2));
343 $item->addChild(
'netMass', round($res->weight * $res->qty));
344 $item->addChild(
'quantityInSU', $res->qty);
345 $item->addChild(
'invoicedAmount', round($res->total_ht));
347 if (!empty($res->tva_intra)) {
348 $item->addChild(
'partnerId', $res->tva_intra);
350 $item->addChild(
'statisticalProcedureCode',
'11');
351 $nature_of_transaction = $item->addChild(
'NatureOfTransaction');
352 $nature_of_transaction->addChild(
'natureOfTransactionACode', 1);
353 $nature_of_transaction->addChild(
'natureOfTransactionBCode', 1);
354 $item->addChild(
'modeOfTransportCode', $res->mode_transport);
355 $item->addChild(
'regionCode', substr($res->zip, 0, 2));
368 $item = $declaration->addChild(
'ligne_des');
369 $item->addChild(
'numlin_des', $i);
370 $item->addChild(
'valeur', round($res->total_ht));
371 $item->addChild(
'partner_des', $res->tva_intra);
384 public function addItemFraisDePort(&$declaration, &$TLinesFraisDePort, $type, &$categ_fraisdeport, $i)
389 if ($type ==
'expedition') {
391 $tabledet =
'facturedet';
392 $field_link =
'fk_facture';
395 $table =
'facture_fourn';
396 $tabledet =
'facture_fourn_det';
397 $field_link =
'fk_facture_fourn';
398 $more_sql =
'f.ref_supplier';
401 foreach ($TLinesFraisDePort as $res) {
402 $sql =
'SELECT p.customcode
403 FROM '.MAIN_DB_PREFIX.$tabledet.
' d
404 INNER JOIN '.MAIN_DB_PREFIX.$table.
' f ON (f.rowid = d.'.$field_link.
')
405 INNER JOIN '.MAIN_DB_PREFIX.
'product p ON (p.rowid = d.fk_product)
406 WHERE d.fk_product IS NOT NULL
407 AND f.entity = '.$conf->entity.
'
408 AND '.$more_sql.
' = "'.$res->refinvoice.
'"
411 SELECT MAX(d.total_ht)
412 FROM '.MAIN_DB_PREFIX.$tabledet.
' d
413 INNER JOIN '.MAIN_DB_PREFIX.$table.
' f ON (f.rowid = d.'.$field_link.
')
414 WHERE d.fk_product IS NOT NULL
415 AND '.$more_sql.
' = "'.$res->refinvoice.
'"
416 AND d.fk_product NOT IN
419 FROM '.MAIN_DB_PREFIX.
'categorie_product
420 WHERE fk_categorie = '.$categ_fraisdeport->id.
'
425 $ress = $this->
db->fetch_object(
$resql);
427 $this->addItemXMl($declaration, $res, $i, $ress->customcode);
440 $resql = $this->
db->query(
'SELECT MAX(numero_declaration) as max_declaration_number FROM '.MAIN_DB_PREFIX.$this->table_element.
' WHERE exporttype="'.$this->exporttype.
'"');
442 $res = $this->
db->fetch_object(
$resql);
445 return ($res->max_declaration_number + 1);
456 return str_pad($number, 6, 0, STR_PAD_LEFT);
467 $name = $this->periode.
'.xml';
468 $fname = sys_get_temp_dir().
'/'.$name;
469 $f = fopen($fname,
'w+');
470 fwrite($f, $this->content_xml);
473 header(
'Content-Description: File Transfer');
474 header(
'Content-Type: application/xml');
475 header(
'Content-Disposition: attachment; filename="'.$name.
'"');
476 header(
'Expires: 0');
477 header(
'Cache-Control: must-revalidate');
478 header(
'Pragma: public');
479 header(
'Content-Length: '.filesize($fname));
getXMLDes($period_year, $period_month, $type_declaration= 'expedition')
Generate XMLDes file.
getSQLFactLines($type, $period_reference, $exporttype= 'deb')
Add invoice line.
Class to manage intracomm report.
static getDeclarationNumber($number)
Verify declaration number.
Class to manage Dolibarr database access.
addItemsFact(&$declaration, $type, $period_reference, $exporttype= 'deb')
Add line from invoice.
generateXMLFile()
Generate XML file.
getNextDeclarationNumber()
Return next reference of declaration not already used (or last reference)
$conf db
API class for accounts.
fetch($id)
Fonction fetch.
__construct(DoliDB $db)
Constructor.
Class to manage categories.
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...
const TYPE_DEB
DEB - Product.
addItemXMlDes($declaration, &$res, $i)
Add item for DES.
const TYPE_DES
DES - Service.
create($user, $notrigger=0)
Fonction create.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
getXML($mode= 'O', $type= 'introduction', $period_reference= '')
Generate XML file.
addItemXMl(&$declaration, &$res, $i, $code_douane_spe= '')
Add item for DEB.
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)