/** * Create invoice in database * Note: this->ref can be set or empty. If empty, we will use "(PROV)" * * @param User $user Object user that create * @param int $notrigger 1=Does not execute triggers, 0 otherwise * @param int $forceduedate 1=Do not recalculate due date from payment condition but force it with value * @return int <0 if KO, >0 if OK */ function create($user, $notrigger = 0, $forceduedate = 0) { global $langs, $conf, $mysoc, $hookmanager; $error = 0; // Clean parameters if (empty($this->type)) { $this->type = self::TYPE_STANDARD; } $this->ref_client = trim($this->ref_client); $this->note = isset($this->note) ? trim($this->note) : trim($this->note_private); // deprecated $this->note_private = isset($this->note_private) ? trim($this->note_private) : trim($this->note_private); $this->note_public = trim($this->note_public); if (!$this->cond_reglement_id) { $this->cond_reglement_id = 0; } if (!$this->mode_reglement_id) { $this->mode_reglement_id = 0; } $this->brouillon = 1; dol_syslog(get_class($this) . "::create user="******"ErrorBadParameter"; dol_syslog(get_class($this) . "::create Try to create an invoice with an empty parameter (user, date, ...)", LOG_ERR); return -3; } $soc = new Societe($this->db); $result = $soc->fetch($this->socid); if ($result < 0) { $this->error = "Failed to fetch company"; dol_syslog(get_class($this) . "::create " . $this->error, LOG_ERR); return -2; } $now = dol_now(); $this->db->begin(); // Create invoice from a predefined invoice if ($this->fac_rec > 0) { require_once DOL_DOCUMENT_ROOT . '/compta/facture/class/facture-rec.class.php'; $_facrec = new FactureRec($this->db); $result = $_facrec->fetch($this->fac_rec); $this->fk_project = $_facrec->fk_project; $this->cond_reglement_id = $_facrec->cond_reglement_id; $this->mode_reglement_id = $_facrec->mode_reglement_id; $this->remise_absolue = $_facrec->remise_absolue; $this->remise_percent = $_facrec->remise_percent; $this->fk_incoterms = $_facrec->fk_incoterms; $this->location_incoterms = $_facrec->location_incoterms; // Clean parameters if (!$this->type) { $this->type = self::TYPE_STANDARD; } $this->ref_client = trim($this->ref_client); $this->note_private = trim($this->note_private); $this->note_public = trim($this->note_public); //if (! $this->remise) $this->remise = 0; if (!$this->mode_reglement_id) { $this->mode_reglement_id = 0; } $this->brouillon = 1; } // Define due date if not already defined $datelim = empty($forceduedate) ? $this->calculate_date_lim_reglement() : $forceduedate; // Insert into database $socid = $this->socid; $sql = "INSERT INTO " . MAIN_DB_PREFIX . "facture ("; $sql .= " facnumber"; $sql .= ", entity"; $sql .= ", ref_ext"; $sql .= ", type"; $sql .= ", fk_soc"; $sql .= ", datec"; $sql .= ", remise_absolue"; $sql .= ", remise_percent"; $sql .= ", datef"; $sql .= ", note_private"; $sql .= ", note_public"; $sql .= ", ref_client, ref_int"; $sql .= ", fk_account"; $sql .= ", fk_facture_source, fk_user_author, fk_projet"; $sql .= ", fk_cond_reglement, fk_mode_reglement, date_lim_reglement, model_pdf"; $sql .= ", situation_cycle_ref, situation_counter, situation_final"; $sql .= ", fk_incoterms, location_incoterms"; $sql .= ")"; $sql .= " VALUES ("; $sql .= "'(PROV)'"; $sql .= ", " . $conf->entity; $sql .= ", " . ($this->ref_ext ? "'" . $this->db->escape($this->ref_ext) . "'" : "null"); $sql .= ", '" . $this->db->escape($this->type) . "'"; $sql .= ", '" . $socid . "'"; $sql .= ", '" . $this->db->idate($now) . "'"; $sql .= ", " . ($this->remise_absolue > 0 ? $this->remise_absolue : 'NULL'); $sql .= ", " . ($this->remise_percent > 0 ? $this->remise_percent : 'NULL'); $sql .= ", '" . $this->db->idate($this->date) . "'"; $sql .= ", " . ($this->note_private ? "'" . $this->db->escape($this->note_private) . "'" : "null"); $sql .= ", " . ($this->note_public ? "'" . $this->db->escape($this->note_public) . "'" : "null"); $sql .= ", " . ($this->ref_client ? "'" . $this->db->escape($this->ref_client) . "'" : "null"); $sql .= ", " . ($this->ref_int ? "'" . $this->db->escape($this->ref_int) . "'" : "null"); $sql .= ", " . ($this->fk_account > 0 ? $this->fk_account : 'NULL'); $sql .= ", " . ($this->fk_facture_source ? "'" . $this->db->escape($this->fk_facture_source) . "'" : "null"); $sql .= ", " . ($user->id > 0 ? "'" . $user->id . "'" : "null"); $sql .= ", " . ($this->fk_project ? $this->fk_project : "null"); $sql .= ", " . $this->cond_reglement_id; $sql .= ", " . $this->mode_reglement_id; $sql .= ", '" . $this->db->idate($datelim) . "', '" . $this->db->escape($this->modelpdf) . "'"; $sql .= ", " . ($this->situation_cycle_ref ? "'" . $this->db->escape($this->situation_cycle_ref) . "'" : "null"); $sql .= ", " . ($this->situation_counter ? "'" . $this->db->escape($this->situation_counter) . "'" : "null"); $sql .= ", " . ($this->situation_final ? $this->situation_final : 0); $sql .= ", " . (int) $this->fk_incoterms; $sql .= ", '" . $this->db->escape($this->location_incoterms) . "'"; $sql .= ")"; dol_syslog(get_class($this) . "::create", LOG_DEBUG); $resql = $this->db->query($sql); if ($resql) { $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . 'facture'); // Update ref with new one $this->ref = '(PROV' . $this->id . ')'; $sql = 'UPDATE ' . MAIN_DB_PREFIX . "facture SET facnumber='" . $this->ref . "' WHERE rowid=" . $this->id; dol_syslog(get_class($this) . "::create", LOG_DEBUG); $resql = $this->db->query($sql); if (!$resql) { $error++; } // Add object linked if (!$error && $this->id && is_array($this->linked_objects) && !empty($this->linked_objects)) { foreach ($this->linked_objects as $origin => $origin_id) { $ret = $this->add_object_linked($origin, $origin_id); if (!$ret) { dol_print_error($this->db); $error++; } // TODO mutualiser if ($origin == 'commande') { // On recupere les differents contact interne et externe $order = new Commande($this->db); $order->id = $origin_id; // On recupere le commercial suivi propale $this->userid = $order->getIdcontact('internal', 'SALESREPFOLL'); if ($this->userid) { //On passe le commercial suivi commande en commercial suivi paiement $this->add_contact($this->userid[0], 'SALESREPFOLL', 'internal'); } // On recupere le contact client facturation commande $this->contactid = $order->getIdcontact('external', 'BILLING'); if ($this->contactid) { //On passe le contact client facturation commande en contact client facturation $this->add_contact($this->contactid[0], 'BILLING', 'external'); } } } } /* * Insert lines of invoices into database */ if (count($this->lines) && is_object($this->lines[0])) { $fk_parent_line = 0; dol_syslog("There is " . count($this->lines) . " lines that are invoice lines objects"); foreach ($this->lines as $i => $val) { $newinvoiceline = $this->lines[$i]; $newinvoiceline->fk_facture = $this->id; $newinvoiceline->origin = $this->element; $newinvoiceline->origin_id = $this->lines[$i]->id; if ($result >= 0 && ($newinvoiceline->info_bits & 0x1) == 0) { // Reset fk_parent_line for no child products and special product if ($newinvoiceline->product_type != 9 && empty($newinvoiceline->fk_parent_line) || $newinvoiceline->product_type == 9) { $fk_parent_line = 0; } $newinvoiceline->fk_parent_line = $fk_parent_line; $result = $newinvoiceline->insert(); // Defined the new fk_parent_line if ($result > 0 && $newinvoiceline->product_type == 9) { $fk_parent_line = $result; } } if ($result < 0) { $this->error = $newinvoiceline->error; $error++; break; } } } else { $fk_parent_line = 0; dol_syslog("There is " . count($this->lines) . " lines that are array lines"); foreach ($this->lines as $i => $val) { if (($this->lines[$i]->info_bits & 0x1) == 0) { // Reset fk_parent_line for no child products and special product if ($this->lines[$i]->product_type != 9 && empty($this->lines[$i]->fk_parent_line) || $this->lines[$i]->product_type == 9) { $fk_parent_line = 0; } $result = $this->addline($this->lines[$i]->desc, $this->lines[$i]->subprice, $this->lines[$i]->qty, $this->lines[$i]->tva_tx, $this->lines[$i]->localtax1_tx, $this->lines[$i]->localtax2_tx, $this->lines[$i]->fk_product, $this->lines[$i]->remise_percent, $this->lines[$i]->date_start, $this->lines[$i]->date_end, $this->lines[$i]->fk_code_ventilation, $this->lines[$i]->info_bits, $this->lines[$i]->fk_remise_except, 'HT', 0, $this->lines[$i]->product_type, $this->lines[$i]->rang, $this->lines[$i]->special_code, $this->element, $this->lines[$i]->id, $fk_parent_line, $this->lines[$i]->fk_fournprice, $this->lines[$i]->pa_ht, $this->lines[$i]->label, $this->lines[$i]->array_options, $this->lines[$i]->situation_percent, $this->lines[$i]->fk_prev_id, $this->lines[$i]->fk_unit); if ($result < 0) { $this->error = $this->db->lasterror(); dol_print_error($this->db); $this->db->rollback(); return -1; } // Defined the new fk_parent_line if ($result > 0 && $this->lines[$i]->product_type == 9) { $fk_parent_line = $result; } } } } /* * Insert lines of predefined invoices */ if (!$error && $this->fac_rec > 0) { foreach ($_facrec->lines as $i => $val) { if ($_facrec->lines[$i]->fk_product) { $prod = new Product($this->db); $res = $prod->fetch($_facrec->lines[$i]->fk_product); } $tva_tx = get_default_tva($mysoc, $soc, $prod->id); $localtax1_tx = get_localtax($tva_tx, 1, $soc); $localtax2_tx = get_localtax($tva_tx, 2, $soc); $result_insert = $this->addline($_facrec->lines[$i]->desc, $_facrec->lines[$i]->subprice, $_facrec->lines[$i]->qty, $tva_tx, $localtax1_tx, $localtax2_tx, $_facrec->lines[$i]->fk_product, $_facrec->lines[$i]->remise_percent, '', '', 0, 0, '', 'HT', 0, $_facrec->lines[$i]->product_type, $_facrec->lines[$i]->rang, $_facrec->lines[$i]->special_code, '', 0, 0, null, 0, $_facrec->lines[$i]->label, null, $_facrec->lines[$i]->situation_percent, '', $_facrec->lines[$i]->fk_unit); if ($result_insert < 0) { $error++; $this->error = $this->db->error(); break; } } } if (!$error) { $result = $this->update_price(1); if ($result > 0) { $action = 'create'; // Actions on extra fields (by external module or standard code) // TODO le hook fait double emploi avec le trigger !! $hookmanager->initHooks(array('invoicedao')); $parameters = array('invoiceid' => $this->id); $reshook = $hookmanager->executeHooks('insertExtraFields', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks if (empty($reshook)) { if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) { $result = $this->insertExtraFields(); if ($result < 0) { $error++; } } } else { if ($reshook < 0) { $error++; } } // Call trigger $result = $this->call_trigger('BILL_CREATE', $user); if ($result < 0) { $error++; } // End call triggers if (!$error) { $this->db->commit(); return $this->id; } else { $this->db->rollback(); return -4; } } else { $this->error = $langs->trans('FailedToUpdatePrice'); $this->db->rollback(); return -3; } } else { dol_syslog(get_class($this) . "::create error " . $this->error, LOG_ERR); $this->db->rollback(); return -2; } } else { $this->error = $this->db->error(); $this->db->rollback(); return -1; } }
/** * Create invoice in database * Note: this->ref can be set or empty. If empty, we will use "(PROV)" * @param user Object user that create * @param notrigger 1=Does not execute triggers, 0 otherwise * @param forceduedate 1=Do not recalculate due date from payment condition but force it with value * @return int <0 if KO, >0 if OK */ function create($user, $notrigger = 0, $forceduedate = 0) { global $langs, $conf, $mysoc; $error = 0; // Clean parameters if (!$this->type) { $this->type = 0; } $this->ref_client = trim($this->ref_client); $this->note = trim($this->note); $this->note_public = trim($this->note_public); if (!$this->remise) { $this->remise = 0; } if (!$this->cond_reglement_id) { $this->cond_reglement_id = 0; } if (!$this->mode_reglement_id) { $this->mode_reglement_id = 0; } $this->brouillon = 1; dol_syslog("Facture::Create user="******"ErrorBadParameter"; dol_syslog("Facture::create Try to create an invoice with an empty parameter (user, date, ...)", LOG_ERR); return -3; } $soc = new Societe($this->db); $result = $soc->fetch($this->socid); if ($result < 0) { $this->error = "Failed to fetch company"; dol_syslog("Facture::create " . $this->error, LOG_ERR); return -2; } $now = dol_now(); $this->db->begin(); // Create invoice from a predefined invoice if ($this->fac_rec > 0) { require_once DOL_DOCUMENT_ROOT . '/compta/facture/class/facture-rec.class.php'; $_facrec = new FactureRec($this->db); $result = $_facrec->fetch($this->fac_rec); $this->fk_project = $_facrec->fk_project; $this->cond_reglement = $_facrec->cond_reglement_id; $this->cond_reglement_id = $_facrec->cond_reglement_id; $this->mode_reglement = $_facrec->mode_reglement_id; $this->mode_reglement_id = $_facrec->mode_reglement_id; $this->amount = $_facrec->amount; $this->remise_absolue = $_facrec->remise_absolue; $this->remise_percent = $_facrec->remise_percent; $this->remise = $_facrec->remise; // Clean parametres if (!$this->type) { $this->type = 0; } $this->ref_client = trim($this->ref_client); $this->note = trim($this->note); $this->note_public = trim($this->note_public); if (!$this->remise) { $this->remise = 0; } if (!$this->mode_reglement_id) { $this->mode_reglement_id = 0; } $this->brouillon = 1; } // Define due date if not already defined $datelim = empty($forceduedate) ? $this->calculate_date_lim_reglement() : $forceduedate; // Insert into database $socid = $this->socid; $amount = $this->amount; $remise = $this->remise; $totalht = $amount - $remise; $sql = "INSERT INTO " . MAIN_DB_PREFIX . "facture ("; $sql .= " facnumber"; $sql .= ", entity"; $sql .= ", type"; $sql .= ", fk_soc"; $sql .= ", datec"; $sql .= ", amount"; $sql .= ", remise_absolue"; $sql .= ", remise_percent"; $sql .= ", datef"; $sql .= ", note"; $sql .= ", note_public"; $sql .= ", ref_client, ref_int"; $sql .= ", fk_facture_source, fk_user_author, fk_projet"; $sql .= ", fk_cond_reglement, fk_mode_reglement, date_lim_reglement, model_pdf"; $sql .= ")"; $sql .= " VALUES ("; $sql .= "'(PROV)'"; $sql .= ", " . $conf->entity; $sql .= ", '" . $this->type . "'"; $sql .= ", '" . $socid . "'"; $sql .= ", '" . $this->db->idate($now) . "'"; $sql .= ", '" . $totalht . "'"; $sql .= "," . ($this->remise_absolue > 0 ? $this->remise_absolue : 'NULL'); $sql .= "," . ($this->remise_percent > 0 ? $this->remise_percent : 'NULL'); $sql .= ", '" . $this->db->idate($this->date) . "'"; $sql .= "," . ($this->note ? "'" . $this->db->escape($this->note) . "'" : "null"); $sql .= "," . ($this->note_public ? "'" . $this->db->escape($this->note_public) . "'" : "null"); $sql .= "," . ($this->ref_client ? "'" . $this->db->escape($this->ref_client) . "'" : "null"); $sql .= "," . ($this->ref_int ? "'" . $this->db->escape($this->ref_int) . "'" : "null"); $sql .= "," . ($this->fk_facture_source ? "'" . $this->db->escape($this->fk_facture_source) . "'" : "null"); $sql .= "," . ($user->id > 0 ? "'" . $user->id . "'" : "null"); $sql .= "," . ($this->fk_project ? $this->fk_project : "null"); $sql .= ',' . $this->cond_reglement_id; $sql .= "," . $this->mode_reglement_id; $sql .= ", '" . $this->db->idate($datelim) . "', '" . $this->modelpdf . "')"; dol_syslog("Facture::Create sql=" . $sql); $resql = $this->db->query($sql); if ($resql) { $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . 'facture'); // Update ref with new one $this->ref = '(PROV' . $this->id . ')'; $sql = 'UPDATE ' . MAIN_DB_PREFIX . "facture SET facnumber='" . $this->ref . "' WHERE rowid=" . $this->id; dol_syslog("Facture::create sql=" . $sql); $resql = $this->db->query($sql); if (!$resql) { $error++; } // Add object linked if (!$error && $this->id && $this->origin && $this->origin_id) { $ret = $this->add_object_linked(); if (!$ret) { dol_print_error($this->db); $error++; } // TODO mutualiser if ($this->origin == 'commande') { // On recupere les differents contact interne et externe $order = new Commande($this->db); $order->id = $this->origin_id; // On recupere le commercial suivi propale $this->userid = $order->getIdcontact('internal', 'SALESREPFOLL'); if ($this->userid) { //On passe le commercial suivi commande en commercial suivi paiement $this->add_contact($this->userid[0], 'SALESREPFOLL', 'internal'); } // On recupere le contact client facturation commande $this->contactid = $order->getIdcontact('external', 'BILLING'); if ($this->contactid) { //On passe le contact client facturation commande en contact client facturation $this->add_contact($this->contactid[0], 'BILLING', 'external'); } } } /* * Insert lines of invoices into database */ if (sizeof($this->lines) && is_object($this->lines[0])) { $fk_parent_line = 0; dol_syslog("There is " . sizeof($this->lines) . " lines that are invoice lines objects"); foreach ($this->lines as $i => $val) { $newinvoiceline = new FactureLigne($this->db); $newinvoiceline = $this->lines[$i]; $newinvoiceline->fk_facture = $this->id; if ($result >= 0 && ($newinvoiceline->info_bits & 0x1) == 0) { // Reset fk_parent_line for no child products and special product if ($newinvoiceline->product_type != 9 && empty($newinvoiceline->fk_parent_line) || $newinvoiceline->product_type == 9) { $fk_parent_line = 0; } $newinvoiceline->fk_parent_line = $fk_parent_line; $result = $newinvoiceline->insert(); // Defined the new fk_parent_line if ($result > 0 && $newinvoiceline->product_type == 9) { $fk_parent_line = $result; } } if ($result < 0) { $this->error = $newinvoiceline->error; $error++; break; } } } else { $fk_parent_line = 0; dol_syslog("There is " . sizeof($this->lines) . " lines that are array lines"); foreach ($this->lines as $i => $val) { if (($this->lines[$i]->info_bits & 0x1) == 0) { // Reset fk_parent_line for no child products and special product if ($this->lines[$i]->product_type != 9 && empty($this->lines[$i]->fk_parent_line) || $this->lines[$i]->product_type == 9) { $fk_parent_line = 0; } $result = $this->addline($this->id, $this->lines[$i]->desc, $this->lines[$i]->subprice, $this->lines[$i]->qty, $this->lines[$i]->tva_tx, $this->lines[$i]->localtax1_tx, $this->lines[$i]->localtax2_tx, $this->lines[$i]->fk_product, $this->lines[$i]->remise_percent, $this->lines[$i]->date_start, $this->lines[$i]->date_end, $this->lines[$i]->fk_code_ventilation, $this->lines[$i]->info_bits, $this->lines[$i]->fk_remise_except, 'HT', 0, $this->lines[$i]->product_type, $this->lines[$i]->rang, $this->lines[$i]->special_code, '', 0, $fk_parent_line); if ($result < 0) { $this->error = $this->db->lasterror(); dol_print_error($this->db); $this->db->rollback(); return -1; } // Defined the new fk_parent_line if ($result > 0 && $this->lines[$i]->product_type == 9) { $fk_parent_line = $result; } } } } /* * Insert lines of predefined invoices */ if (!$error && $this->fac_rec > 0) { foreach ($_facrec->lines as $i => $val) { if ($_facrec->lines[$i]->fk_product) { $prod = new Product($this->db, $_facrec->lines[$i]->fk_product); $res = $prod->fetch($_facrec->lines[$i]->fk_product); } $tva_tx = get_default_tva($mysoc, $soc, $prod->id); $localtax1_tx = get_localtax($tva_tx, 1, $soc); $localtax2_tx = get_localtax($tva_tx, 2, $soc); $result_insert = $this->addline($this->id, $_facrec->lines[$i]->desc, $_facrec->lines[$i]->subprice, $_facrec->lines[$i]->qty, $tva_tx, $localtax1_tx, $localtax2_tx, $_facrec->lines[$i]->fk_product, $_facrec->lines[$i]->remise_percent, '', '', 0, 0, '', 'HT', 0, $_facrec->lines[$i]->product_type, $_facrec->lines[$i]->rang, $_facrec->lines[$i]->special_code); if ($result_insert < 0) { $error++; $this->error = $this->db->error(); break; } } } if (!$error) { $result = $this->update_price(1); if ($result > 0) { // Appel des triggers include_once DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php"; $interface = new Interfaces($this->db); $result = $interface->run_triggers('BILL_CREATE', $this, $user, $langs, $conf); if ($result < 0) { $error++; $this->errors = $interface->errors; } // Fin appel triggers if (!$error) { $this->db->commit(); return $this->id; } else { $this->db->rollback(); return -4; } } else { $this->error = $langs->trans('FailedToUpdatePrice'); $this->db->rollback(); return -3; } } else { dol_syslog("Facture::create error " . $this->error, LOG_ERR); $this->db->rollback(); return -2; } } else { $this->error = $this->db->error(); dol_syslog("Facture::create error " . $this->error . " sql=" . $sql, LOG_ERR); $this->db->rollback(); return -1; } }