function createAndSendInvoice($id_client, $prix_ht, $quantity, $description, $delivery_method)
 {
     # No invoice if amount is zero
     if ($prix_ht * $quantity <= 0) {
         return true;
     }
     $Facture = new Facture();
     $invoice = array('client_id' => $id_client, 'rows' => array());
     $id_facture = $Facture->create($invoice);
     // Get invoice payment
     $res = mysql_query("SELECT payment_method\n      FROM webfinance_invoices i\n      WHERE id_client = {$id_client}\n        AND type_doc = 'facture'\n        AND is_envoye = 1\n        AND payment_method IS NOT NULL\n        AND is_abandoned = 0\n      ORDER BY id_facture DESC\n      LIMIT 1") or die(mysql_error());
     # Default values
     $payment_method = 'unknown';
     if (mysql_num_rows($res) > 0) {
         $type_payment_res = mysql_fetch_array($res);
         $payment_method = $type_payment_res['payment_method'];
     }
     // Get id_compte
     $result = mysql_query('SELECT id_pref,value ' . 'FROM webfinance_pref ' . "WHERE type_pref='rib' " . 'LIMIT 1') or die(mysql_error());
     $cpt = mysql_fetch_object($result);
     $id_compte = $cpt->id_pref;
     // Get id_type_presta
     $result = mysql_query("SELECT id_type_presta\n                  FROM webfinance_type_presta\n                  WHERE nom = 'Support mensuel'\n                  LIMIT 1") or die(mysql_error());
     list($id_type_presta) = mysql_fetch_row($result);
     // Input facture parameters
     mysql_query("UPDATE webfinance_invoices SET\n\t\t        is_paye = 0,\n\t\t        is_envoye = 0,\n\t\t        ref_contrat = 'Support professionnel',\n\t\t        payment_method = '{$payment_method}',\n\t\t        id_compte = {$id_compte},\n\t\t        id_type_presta = {$id_type_presta}\n\t\t      WHERE id_facture = {$id_facture}") or die(mysql_error());
     // Add service rows to invoice
     $q = sprintf("INSERT INTO webfinance_invoice_rows (id_facture,description,prix_ht,qtt,ordre) " . "SELECT %d, '%s', %s, %s, if(max(ordre) is null, 1, max(ordre + 1)) " . "FROM webfinance_invoice_rows " . "WHERE id_facture=%d", $id_facture, mysql_real_escape_string($description), $prix_ht, $quantity, $id_facture);
     $result = mysql_query($q) or die(mysql_error());
     mysql_query("UPDATE webfinance_invoices SET date_generated=NULL WHERE id_facture=" . $id_facture) or die(mysql_error());
     if ($payment_method == 'direct_debit') {
         // Plan the invoice to be debited
         mysql_query("INSERT INTO direct_debit_row " . "SET invoice_id = {$id_facture}, " . "    state='todo'") or die(mysql_error());
         // Flag invoice as paid
         $Facture->setPaid($id_facture);
     }
     // Manage invoice delivery and send by email to client
     switch ($delivery_method) {
         case 'email':
             $Facture->sendByEmail($id_facture) or die("Unable to send email for invoice ID {$id_facture}");
             break;
         case 'postal':
             $send_mail_print_invoice = true;
             $attachments[] = $Facture->generatePDF($id_facture, true);
             $Facture->setSent($id_facture);
             break;
     }
     return true;
 }
     // Update invoice date
     $query = 'UPDATE webfinance_invoice_rows ' . "SET description='{$invoice_row['description']}'" . "WHERE id_facture_ligne={$invoice_row['id_facture_ligne']}";
     mysql_query($query) or die("{$query}:" . mysql_error());
 }
 // Manage invoice delivery
 switch ($invoice->delivery) {
     // Send invoice by email to the client
     case 'email':
         $Invoice->sendByEmail($id_new_invoice) or die("Unable to send email for invoice ID {$id_facture}");
         break;
         // Send the invoice to me in order to print and send it to the client
     // Send the invoice to me in order to print and send it to the client
     case 'postal':
         $send_mail_print_invoice = true;
         $attachments[] = $Invoice->generatePDF($id_new_invoice, true);
         $Invoice->setSent($id_new_invoice);
         break;
 }
 // Process direct debit invoices
 if ($invoice->payment_method == 'direct_debit') {
     $new_invoice = $Invoice->getInfos($id_new_invoice);
     $send_mail_direct_debit = true;
     $url = "https://webfinance.isvtec.com/prospection/edit_facture.php?id_facture={$new_invoice->id_facture}";
     # Set invoice as paid
     $Invoice->setPaid($id_new_invoice);
     # Plan the invoice to be debited
     mysql_query('INSERT INTO direct_debit_row ' . "SET invoice_id = {$id_new_invoice}, " . "    state='todo'") or die(mysql_error());
 }
 // Process paypal invoices
 if ($invoice->payment_method == 'paypal') {
     $Invoice->SendPaymentRequest($id_new_invoice);
Beispiel #3
0
 function sendByEmail($id_invoice, array $emails = array(), $from = '', $fromname = '', $subject = '', $body = '', $introduction_letter = false, $docs = false)
 {
     // Fetch company information
     $result = mysql_query('SELECT value ' . 'FROM webfinance_pref ' . "WHERE type_pref='societe' AND owner=-1") or die('sendByEmail-950' . mysql_error());
     list($value) = mysql_fetch_array($result);
     mysql_free_result($result);
     $societe = unserialize(base64_decode($value));
     // Fetch invoice information
     $invoice = Facture::getInfos($id_invoice);
     // Fetch client information
     $Client = new Client($invoice->id_client);
     // Fetch bank account information
     $result = mysql_query('SELECT value ' . 'FROM webfinance_pref ' . "WHERE id_pref=" . $invoice->id_compte) or die('sendByEmail-965' . mysql_error());
     list($cpt) = mysql_fetch_array($result);
     mysql_free_result($result);
     $cpt = unserialize(base64_decode($cpt));
     if (!is_object($cpt)) {
         die("Impossible de generer la facture. Vous devez saisir au " . "moins un compte bancaire dans les options pour emettre des " . "factures");
     }
     foreach ($cpt as $n => $v) {
         $cpt->{$n} = utf8_decode($cpt->{$n});
     }
     // Fetch preference information
     $result = mysql_query("SELECT value FROM webfinance_pref WHERE type_pref='mail_invoice_" . $invoice->language . "'") or die('sendByEmail-980' . mysql_error());
     list($data) = mysql_fetch_array($result);
     $pref = unserialize(base64_decode($data));
     if (empty($from)) {
         $from = $societe->email;
     }
     if (empty($fromname)) {
         $fromname = $societe->raison_sociale;
     }
     if (empty($Client->email)) {
         echo _('Missing email address $Client->email!');
         return false;
     }
     if (empty($emails)) {
         $emails = explode(',', $Client->email);
     }
     if (empty($subject)) {
         $subject = ucfirst($invoice->type_doc) . " #" . $invoice->num_facture . " pour " . $invoice->nom_client;
     }
     if (empty($body)) {
         // Delay
         $delay = '';
         $result = mysql_query("SELECT date_format(date, '%d/%m/%Y') " . 'FROM webfinance_transactions ' . "WHERE id_invoice={$invoice->id_facture} " . 'ORDER BY date ' . 'DESC') or die('sendByEmail-1006' . mysql_error());
         if (mysql_num_rows($result) == 1) {
             list($tr_date) = mysql_fetch_array($result);
             $delay = _('payable avant le') . " {$tr_date}";
         }
         mysql_free_result($result);
         $patterns = array('/%%LOGIN%%/', '/%%PASSWORD%%/', '/%%URL_COMPANY%%/', '/%%NUM_INVOICE%%/', '/%%CLIENT_NAME%%/', '/%%DELAY%%/', '/%%AMOUNT%%/', '/%%BANK%%/', '/%%RIB%%/', '/%%COMPANY%%/', '/%%SEPA_MNDTID%%/');
         $replacements = array($Client->login, $Client->password, $societe->wf_url, $invoice->num_facture, $invoice->nom_client, $delay, $invoice->nice_total_ttc, $cpt->banque, $cpt->code_banque . " " . $cpt->code_guichet . " " . $cpt->compte . " " . $cpt->clef . " ", $societe->raison_sociale, $Client->sepa_mndtid);
         $body = stripslashes(preg_replace($patterns, $replacements, stripslashes(utf8_decode($pref->body))));
     }
     $mail = new PHPMailer();
     $mail->CharSet = 'UTF-8';
     foreach ($emails as $email) {
         $mail->AddAddress($email);
     }
     $mail->From = $from;
     $mail->FromName = $fromname;
     $mail->Subject = $subject;
     $mail->Body = $body;
     $mail->WordWrap = 80;
     //attach the invoice file
     $facture = new Facture();
     $filename = $facture->generatePDF($id_invoice, $introduction_letter, $target = 'file', $docs);
     $mail->AddAttachment($filename, basename($filename), 'base64', 'application/pdf');
     // Send mail
     $mail->Send();
     // Remove attachment
     unlink($filename);
     Facture::setSent($id_invoice);
     // Log invoice as sent
     logmessage(_("Sent invoice") . " #{$invoice->num_facture} " . "to " . implode(' ', $emails) . ' ' . "fa:{$id_invoice} " . "client:{$invoice->id_client}", $invoice->id_client, $invoice->id_facture);
     return true;
 }