Ejemplo n.º 1
0
 /**
  *	Create a new invoice in database from current invoice
  *
  *	@param      User	$user    		Object user that ask creation
  *	@param		int		$invertdetail	Reverse sign of amounts for lines
  *	@return		int						<0 if KO, >0 if OK
  */
 function createFromCurrent($user, $invertdetail = 0)
 {
     global $conf;
     // Charge facture source
     $facture = new Facture($this->db);
     $facture->fk_facture_source = $this->fk_facture_source;
     $facture->type = $this->type;
     $facture->socid = $this->socid;
     $facture->date = $this->date;
     $facture->note_public = $this->note_public;
     $facture->note_private = $this->note_private;
     $facture->ref_client = $this->ref_client;
     $facture->modelpdf = $this->modelpdf;
     $facture->fk_project = $this->fk_project;
     $facture->cond_reglement_id = $this->cond_reglement_id;
     $facture->mode_reglement_id = $this->mode_reglement_id;
     $facture->remise_absolue = $this->remise_absolue;
     $facture->remise_percent = $this->remise_percent;
     $facture->origin = $this->origin;
     $facture->origin_id = $this->origin_id;
     $facture->lines = $this->lines;
     // Tableau des lignes de factures
     $facture->products = $this->lines;
     // Tant que products encore utilise
     $facture->situation_counter = $this->situation_counter;
     $facture->situation_cycle_ref = $this->situation_cycle_ref;
     $facture->situation_final = $this->situation_final;
     // Loop on each line of new invoice
     foreach ($facture->lines as $i => $line) {
         $facture->lines[$i]->fk_prev_id = $this->lines[$i]->rowid;
         if ($invertdetail) {
             $facture->lines[$i]->subprice = -$facture->lines[$i]->subprice;
             $facture->lines[$i]->total_ht = -$facture->lines[$i]->total_ht;
             $facture->lines[$i]->total_tva = -$facture->lines[$i]->total_tva;
             $facture->lines[$i]->total_localtax1 = -$facture->lines[$i]->total_localtax1;
             $facture->lines[$i]->total_localtax2 = -$facture->lines[$i]->total_localtax2;
             $facture->lines[$i]->total_ttc = -$facture->lines[$i]->total_ttc;
         }
     }
     dol_syslog(get_class($this) . "::createFromCurrent invertdetail=" . $invertdetail . " socid=" . $this->socid . " nboflines=" . count($facture->lines));
     $facid = $facture->create($user);
     if ($facid <= 0) {
         $this->error = $facture->error;
         $this->errors = $facture->errors;
     } elseif ($this->type == self::TYPE_SITUATION && !empty($conf->global->INVOICE_USE_SITUATION)) {
         $this->fetchObjectLinked('', '', $object->id, 'facture');
         foreach ($this->linkedObjectsIds as $typeObject => $Tfk_object) {
             foreach ($Tfk_object as $fk_object) {
                 $facture->add_object_linked($typeObject, $fk_object);
             }
         }
         $facture->add_object_linked('facture', $this->fk_facture_source);
     }
     return $facid;
 }
 function create_facture()
 {
     require_once DOL_DOCUMENT_ROOT . '/compta/facture/class/facture.class.php';
     global $db, $user, $langs;
     $langs->load('pos@pos');
     $facture = new Facture($db);
     $facture->socid = $this->socid;
     //! Objet societe client (to load with fetch_client method)
     $facture->client = $this->client;
     $facture->author = $this->author;
     $facture->fk_user_author = $this->fk_user_author;
     $facture->fk_user_valid = $this->fk_user_valid;
     //! Ticket date
     $now = dol_now();
     $facture->date = $now;
     // Ticket date
     $facture->date_creation = $now;
     // Creation date
     $facture->datem = $this->datem;
     $facture->ref = $this->ref;
     //! 0=Standard ticket, 1=Credit note ticket,2=Deposit ticket
     $facture->type = 0;
     $facture->remise_absolue = $this->remise_absolute;
     $facture->remise_percent = $this->remise_percent;
     $facture->total_ht = $this->total_ht;
     $facture->total_tva = $this->total_tva;
     $facture->total_ttc = $this->total_ttc;
     $facture->note = $this->note;
     $facture->note_public = $this->note_public;
     //! 0=draft,
     //! 1=to invoice
     //! 2=invoiced
     //! 3=No invoicable
     //! 4=return ticket
     //! 5=abandoned
     $facture->statut = $this->statut;
     //! Fermeture apres paiement partiel: discount_vat, badcustomer, abandon
     //! Fermeture alors que aucun paiement: replaced (si remplace), abandon
     $facture->close_code = $this->close_code;
     //! Commentaire si mis a paye sans paiement complet
     $facture->close_note = $this->close_note;
     //! 1 if ticket paid COMPLETELY, 0 otherwise (do not use it anymore, use statut and close_code
     $facture->paye = 0;
     $facture->mode_reglement_id = $this->mode_reglement_id;
     // Id in llx_c_paiement
     $facture->mode_reglement_code = $this->mode_reglement_code;
     // Code in llx_c_paiement
     $facture->modelpdf = $this->modelpdf;
     $facture->products = $this->products;
     // TODO deprecated
     $facture->line = $this->line;
     //! Pour board
     $facture->nbtodo = $this->nbtodo;
     $facture->nbtodolate = $this->nbtodolate;
     $facture->specimen = $this->specimen;
     for ($i = 0; $i < sizeof($this->lines); $i++) {
         $factline = new FactureLigne($db);
         $factline->fk_parent_line = $this->lines[$i]->fk_parent_line;
         //! Description ligne
         $factline->desc = $this->lines[$i]->desc;
         $factline->fk_product = $this->lines[$i]->fk_product;
         // Id of predefined product
         $factline->product_type = $this->lines[$i]->product_type;
         // Type 0 = product, 1 = Service
         $factline->qty = $this->lines[$i]->qty;
         // Quantity (example 2)
         $factline->tva_tx = $this->lines[$i]->tva_tx;
         // Taux tva produit/service (example 19.6)
         $factline->localtax1_tx = $this->lines[$i]->localtax1_tx;
         // Local tax 1
         $factline->localtax2_tx = $this->lines[$i]->localtax2_tx;
         // Local tax 2
         $factline->subprice = $this->lines[$i]->subprice;
         // P.U. HT (example 100)
         $factline->remise_percent = $this->lines[$i]->remise_percent;
         // % de la remise ligne (example 20%)
         $factline->fk_remise_except = $this->lines[$i]->fk_remise_except;
         // Link to line into llx_remise_except
         $factline->rang = $this->lines[$i]->rang;
         $factline->info_bits = $this->lines[$i]->info_bits;
         // Liste d'options cumulables:
         // Bit 0:	0 si TVA normal - 1 si TVA NPR
         // Bit 1:	0 si ligne normal - 1 si bit discount (link to line into llx_remise_except)
         $factline->special_code = $this->lines[$i]->special_code;
         // Liste d'options non cumulabels:
         // 1: frais de port
         // 2: ecotaxe
         // 3: ??
         $factline->origin = $this->lines[$i]->origin;
         $factline->origin_id = $this->lines[$i]->origin_id;
         //! Total HT  de la ligne toute quantite et incluant la remise ligne
         $factline->total_ht = $this->lines[$i]->total_ht;
         //! Total TVA  de la ligne toute quantite et incluant la remise ligne
         $factline->total_tva = $this->lines[$i]->total_tva;
         $factline->total_localtax1 = $this->lines[$i]->total_localtax1;
         //Total Local tax 1 de la ligne
         $factline->total_localtax2 = $this->lines[$i]->total_localtax2;
         //Total Local tax 2 de la ligne
         //! Total TTC de la ligne toute quantite et incluant la remise ligne
         $factline->total_ttc = $this->lines[$i]->total_ttc;
         $factline->fk_code_ventilation = $this->lines[$i]->fk_code_ventilation;
         $factline->fk_export_compta = $this->lines[$i]->fk_export_compta;
         $factline->date_start = $this->lines[$i]->date_start;
         $factline->date_end = $this->lines[$i]->date_end;
         // From llx_product
         $factline->ref = $this->lines[$i]->ref;
         // Product ref (deprecated)
         $factline->product_ref = $this->lines[$i]->product_ref;
         // Product ref
         $factline->libelle = $this->lines[$i]->libelle;
         // Product label (deprecated)
         $factline->product_label = $this->lines[$i]->product_label;
         // Product label
         $factline->product_desc = $this->lines[$i]->product_desc;
         // Description produit
         $factline->skip_update_total = $this->lines[$i]->skip_update_total;
         // Skip update price total for special lines
         $facture->lines[$i] = $factline;
     }
     $facture->create($user);
     if ($facture->statut == 1 || $facture->type == 1) {
         $facture->validate($user);
         if ($this->diff_payment <= 0) {
             $facture->set_paid($user);
         }
         if ($this->diff_payment > 0) {
             $facture->setStatut(1);
         }
     }
     $sql = 'UPDATE ' . MAIN_DB_PREFIX . "pos_ticket SET fk_facture='" . $facture->id . "' WHERE rowid=" . $this->id;
     dol_syslog("Ticket::update sql=" . $sql);
     $resql = $this->db->query($sql);
     if (!$resql) {
         $this->db->rollback();
         return -1;
     } else {
         $this->db->commit();
         $sql = 'INSERT INTO ' . MAIN_DB_PREFIX . 'pos_facture (fk_cash, fk_place,fk_facture) VALUES (' . $this->fk_cash . ',' . ($this->fk_place ? $this->fk_place : 'null') . ',' . $facture->id . ')';
         dol_syslog("pos_facture::update sql=" . $sql);
         $resql = $this->db->query($sql);
         if (!$resql) {
             $this->db->rollback();
             return -1;
         } else {
             $this->db->commit();
         }
         $sql = 'SELECT fk_paiement, amount FROM ' . MAIN_DB_PREFIX . "pos_paiement_ticket WHERE fk_ticket=" . $this->id;
         $resql = $this->db->query($sql);
         if ($resql) {
             $num = $db->num_rows($resql);
             $i = 0;
             $totalpaye = 0;
             while ($i < $num) {
                 $objp = $db->fetch_object($resql);
                 $paye[$i]['fk_paiement'] = $objp->fk_paiement;
                 $paye[$i]['amount'] = $objp->amount;
                 $i++;
             }
             $i = 0;
             while ($i < $num) {
                 $sql = 'INSERT INTO ' . MAIN_DB_PREFIX . 'paiement_facture (fk_paiement, fk_facture, amount) VALUES (' . $paye[$i]['fk_paiement'] . ',' . $facture->id . ',' . $paye[$i]['amount'] . ')';
                 $resql = $this->db->query($sql);
                 $i++;
             }
         } else {
             return -1;
         }
         $facture->add_object_linked('ticket', $this->id);
         return $facture->id;
     }
 }