Exemplo n.º 1
0
 /**
  *	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;
     }
 }
Exemplo n.º 2
0
    if (!$error) {
        $object->titre = GETPOST('titre', 'alpha');
        $object->note_private = GETPOST('note_private');
        $object->usenewprice = GETPOST('usenewprice');
        if ($object->create($user, $id) > 0) {
            $id = $object->id;
            $action = '';
        } else {
            setEventMessages($object->error, $object->errors, 'errors');
            $action = "create";
        }
    }
}
// Delete
if ($action == 'delete' && $user->rights->facture->supprimer) {
    $object->fetch($id);
    $object->delete();
    $id = 0;
}
/*
 *	View
 */
llxHeader('', $langs->trans("RepeatableInvoices"), 'ch-facture.html#s-fac-facture-rec');
$form = new Form($db);
$companystatic = new Societe($db);
/*
 * Create mode
 */
if ($action == 'create') {
    print load_fiche_titre($langs->trans("CreateRepeatableInvoice"), '', 'title_accountancy.png');
    $object = new Facture($db);
Exemplo n.º 3
0
 /**
  *	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++;
             }
         }
         /*
          *  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;
     }
 }
Exemplo n.º 4
0
            print '</select>';
            print '</td></tr>';
        }
        print '<tr><td colspan="3" align="center"><br><input type="submit" class="button" value="' . $langs->trans("Create") . '"></td></tr>';
        print "</form>\n";
        print "</table>\n";
    } else {
        print "Erreur facture {$facture->id} inexistante";
    }
} else {
    /*
     * View mode
     */
    if ($facid > 0) {
        $fac = new FactureRec($db);
        if ($fac->fetch($facid, $user->societe_id) > 0) {
            $soc = new Societe($db);
            $soc->fetch($fac->socid);
            $author = new User($db);
            $author->fetch($fac->user_author);
            dol_fiche_head($head, 'compta', $langs->trans("PredefinedInvoices"), 0, 'company');
            // Add a div
            print '<table class="border" width="100%">';
            print '<tr><td>' . $langs->trans("Ref") . '</td>';
            print '<td colspan="4">' . $fac->titre . '</td>';
            print '<tr><td>' . $langs->trans("Customer") . '</td>';
            print '<td colspan="3">' . $soc->getNomUrl(1) . '</td>';
            print "<td>" . $langs->trans("PaymentConditions") . " : ";
            $form->form_conditions_reglement($_SERVER['PHP_SELF'] . '?facid=' . $fac->id, $fac->cond_reglement_id, 'none');
            print "</td></tr>";
            print "<tr><td>" . $langs->trans("Author") . "</td><td colspan=\"3\">" . $author->getFullName($langs) . "</td>";
function _create_and_send($PDOdb, $db, $user, $conf, $langs)
{
    unset($_SESSION['SENDTOINVOICETOADHERENT_TERROR']);
    unset($_SESSION['SENDTOINVOICETOADHERENT_TERRORFAC']);
    unset($_SESSION['SENDTOINVOICETOADHERENT_TERRORMAIL']);
    $error = 0;
    $nb_mail_sent = 0;
    if (!$user->rights->sendinvoicetoadherent->create) {
        $error++;
        setEventMessages($langs->trans('sendinvoicetoadherentErrorCreateNotPermitted'), false, 'errors');
        header('Location: ' . dol_buildpath('/sendinvoicetoadherent/sendinvoicetoadherent.php?action=list', 2));
        exit;
    }
    dol_include_once('/compta/facture/class/facture-rec.class.php');
    dol_include_once('/compta/facture/class/adherent.class.php');
    dol_include_once('/adherents/class/adherent.class.php');
    dol_include_once('/societe/class/societe.class.php');
    dol_include_once('/core/class/CMailFile.class.php');
    dol_include_once('/core/lib/files.lib.php');
    $date_fac = GETPOST('date_fac', 'alpha');
    $create_cotisation = GETPOST('create_cotisation', 'int');
    $date_start = GETPOST('date_start', 'alpha');
    $date_end = GETPOST('date_end', 'alpha');
    $amount = price2num(GETPOST('amount_cotisation', 'alpha'), 2);
    $label = GETPOST('label', 'alpha');
    $TDate_fac = explode('/', $date_fac);
    if (!checkdate($TDate_fac[1], $TDate_fac[0], $TDate_fac[2])) {
        $error++;
        setEventMessages($langs->trans('sendinvoicetoadherentErrorDateFac'), false, 'errors');
    }
    if ($create_cotisation) {
        $TDate_start = explode('/', $date_start);
        $TDate_end = explode('/', $date_end);
        if (!checkdate($TDate_start[1], $TDate_start[0], $TDate_start[2]) || !checkdate($TDate_end[1], $TDate_end[0], $TDate_end[2])) {
            $error++;
            setEventMessages($langs->trans('sendinvoicetoadherentErrorDate'), false, 'errors');
        }
    }
    $fk_facture_rec = (int) $conf->global->SENDINVOICETOADHERENT_FK_FACTURE;
    if (!$fk_facture_rec) {
        $error++;
        setEventMessages($langs->trans('sendinvoicetoadherent_fk_facture'), false, 'errors');
    }
    if (!$error) {
        $sql = _getSql();
        if ($PDOdb->Execute($sql)) {
            $TError = $TErrorFac = $TErrorMail = array();
            while ($row = $PDOdb->Get_line()) {
                $ad = new Adherent($db);
                $ad->fetch($row->rowid);
                if (!$ad->fk_soc && $ad->societe) {
                    $societe = _createTiers($db, $user, $ad);
                } else {
                    $ad->fetch_thirdparty();
                    $societe = $ad->thirdparty;
                }
                if ($societe && $societe->id > 0) {
                    if ($create_cotisation) {
                        if ($ad->cotisation(dol_mktime(0, 0, 0, $TDate_start[1], $TDate_start[0], $TDate_start[2]), $amount, 0, '', $label, '', '', '', dol_mktime(0, 0, 0, $TDate_end[1], $TDate_end[0], $TDate_end[2])) <= 0) {
                            $error++;
                            setEventMessages($object->error, $object->errors, 'errors');
                        }
                    }
                    $factureRec = new FactureRec($db);
                    $factureRec->fetch($fk_facture_rec);
                    $facture = new Facture($db);
                    $facture->brouillon = 1;
                    $facture->socid = $societe->id;
                    $facture->type = Facture::TYPE_STANDARD;
                    $facture->fk_project = $factureRec->fk_project;
                    $facture->cond_reglement_id = $factureRec->cond_reglement_id;
                    $facture->mode_reglement_id = $factureRec->mode_reglement_id;
                    $facture->remise_absolue = $factureRec->remise_absolue;
                    $facture->remise_percent = $factureRec->remise_percent;
                    $facture->date = dol_mktime(12, 0, 0, $TDate_fac[1], $TDate_fac[0], $TDate_fac[2]);
                    $facture->note_private = $factureRec->note_private;
                    $facture->note_public = $factureRec->note_public;
                    $facture->lines = $factureRec->lines;
                    if ($facture->create($user) > 0) {
                        $facture->validate($user);
                        if (!_sendByMail($db, $conf, $user, $langs, $facture, $societe, $label)) {
                            $TErrorMail[] = $societe->id;
                        }
                    } else {
                        $TErrorFac[] = $societe->id;
                    }
                } else {
                    $TError[] = $ad->id;
                }
            }
            if (count($TError) > 0) {
                setEventMessages($langs->trans('sendinvoicetoadherentErrorCreateTiers', count($TError)), null, 'errors');
            }
            if (count($TErrorFac) > 0) {
                setEventMessages($langs->trans('sendinvoicetoadherentErrorCreateFacture', count($TErrorFac)), null, 'errors');
            }
            if (count($TErrorMail) > 0) {
                setEventMessages($langs->trans('sendinvoicetoadherentErrorMailSend', count($TErrorMail)), null, 'errors');
            } else {
                setEventMessages($langs->trans('sendinvoicetoadherentConfirmCreate', $PDOdb->Get_Recordcount()), null);
            }
            $_SESSION['SENDTOINVOICETOADHERENT_ERRORS'] = array('TError' => $TError, 'TErrorFac' => $TErrorFac, '');
            $_SESSION['SENDTOINVOICETOADHERENT_TERROR'] = $TError;
            $_SESSION['SENDTOINVOICETOADHERENT_TERRORFAC'] = $TErrorFac;
            $_SESSION['SENDTOINVOICETOADHERENT_TERRORMAIL'] = $TErrorMail;
            header('Location: ' . dol_buildpath('/sendinvoicetoadherent/sendinvoicetoadherent.php?action=list', 2));
            exit;
        }
    } else {
        _create($PDOdb, $db, $user, $conf, $langs, $date_fac, $date_start, $date_end, $amount, $label);
    }
}
Exemplo n.º 6
0
			$(document).ready(function() {
				$("#socid").change(function() {
					var socid = $(this).val();
					// reload page
					window.location.href = "' . $_SERVER["PHP_SELF"] . '?action=create&socid="+socid;
				});
			});
			</script>';
        }
        print '</td>';
    }
    print '</tr>' . "\n";
    // Predefined invoices
    if (empty($origin) && empty($originid) && GETPOST('fac_rec', 'int') > 0) {
        $invoice_predefined = new FactureRec($db);
        $invoice_predefined->fetch(GETPOST('fac_rec', 'int'));
        $sql = 'SELECT r.rowid, r.titre, r.total_ttc';
        $sql .= ' FROM ' . MAIN_DB_PREFIX . 'facture_rec as r';
        $sql .= ' WHERE r.fk_soc = ' . $invoice_predefined->socid;
        $resql = $db->query($sql);
        if ($resql) {
            $num = $db->num_rows($resql);
            $i = 0;
            if ($num > 0) {
                print '<tr><td>' . $langs->trans('CreateFromRepeatableInvoice') . '</td><td>';
                print '<select class="flat" name="fac_rec">';
                print '<option value="0" selected></option>';
                while ($i < $num) {
                    $objp = $db->fetch_object($resql);
                    print '<option value="' . $objp->rowid . '"';
                    if (GETPOST('fac_rec') == $objp->rowid) {