Commit 3d17569a authored by Nicolas Svirchevsky's avatar Nicolas Svirchevsky

Invoice can be sent by mail

parent e86c8367
package fr.pasteque.client.activities;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
......@@ -12,14 +15,20 @@ import fr.pasteque.client.fragments.CustomerSelectDialog;
import fr.pasteque.client.models.Customer;
import fr.pasteque.client.models.Ticket;
import fr.pasteque.client.models.TicketLine;
import fr.pasteque.client.utils.CouldNotCreatePdfException;
import fr.pasteque.client.utils.file.ExternalFile;
import fr.pasteque.client.utils.file.File;
import fr.pasteque.client.utils.file.InternalFile;
import fr.pasteque.client.widgets.CompanyView;
import fr.pasteque.client.widgets.pdf.Pdf;
import fr.pasteque.client.widgets.pdf.PdfTaxeRow;
import fr.pasteque.client.widgets.pdf.PdfTicketRow;
import fr.pasteque.client.widgets.pdf.PdfTotalRow;
import java.io.*;
import java.util.List;
import java.util.Map;
import java.util.UUID;
/**
* Created by svirch_n on 25/04/16
......@@ -30,6 +39,7 @@ public class PdfCreatorActivity extends TrackedActivity implements CustomerSelec
public static final String TICKET_TAG = "TICKET_TAG";
private static final int HEADER_PADDING = 10;
private static final int EDIT = 1;
private static final java.lang.String PDF_DIRECTORY = "invoice";
private View.OnClickListener addCustomerListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
......@@ -50,6 +60,12 @@ public class PdfCreatorActivity extends TrackedActivity implements CustomerSelec
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.findItem(R.id.ab_menu_send).setEnabled(this.customer != null);
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
......@@ -62,10 +78,80 @@ public class PdfCreatorActivity extends TrackedActivity implements CustomerSelec
case R.id.ab_menu_customer_edit:
this.editCustomer();
break;
case R.id.ab_menu_send:
this.sendInvoice();
break;
}
return true;
}
private void sendInvoice() {
InternalFile file = createPdf();
if (file != null) {
sendMail(file);
}
finish();
}
private void sendMail(File file) {
File copy = copyInTempCache(file);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("message/rfc822");
intent.putExtra(Intent.EXTRA_EMAIL, this.customer.getMail());
intent.putExtra(Intent.EXTRA_SUBJECT, "Invoice " + copy.getName());
intent.putExtra(Intent.EXTRA_TEXT, "Thank you!");
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + copy.getAbsolutePath()));
try {
startActivity(Intent.createChooser(intent, getString(R.string.pick_email_client)));
} catch (android.content.ActivityNotFoundException ex) {
Pasteque.Toast.show(R.string.err_no_email_client);
}
}
private File copyInTempCache(File file) {
File result = new ExternalFile(getApplicationContext().getCacheDir().getPath(), file.getName());
InputStream in = null;
OutputStream out = null;
try {
in = file.openRead();
out = result.openWrite();
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
private InternalFile createPdf() {
String filename = "invoice_" + UUID.randomUUID() + ".pdf";
InternalFile internalFile = null;
try {
internalFile = new InternalFile(PDF_DIRECTORY, filename, Context.MODE_WORLD_READABLE);
FileOutputStream fileOutputStream = internalFile.openWrite();
getPdf().build(fileOutputStream);
} catch (FileNotFoundException | CouldNotCreatePdfException e) {
e.printStackTrace();
Pasteque.Toast.show(R.string.err_create_pdf);
}
return internalFile;
}
private void createCustomer() {
CustomerInfoDialog customerInfoDialog = CustomerInfoDialog.newInstance(true, null);
customerInfoDialog.setDialogCustomerListener(this);
......@@ -87,6 +173,7 @@ public class PdfCreatorActivity extends TrackedActivity implements CustomerSelec
if (bundle != null) {
Ticket ticket = (Ticket) bundle.getSerializable(PdfCreatorActivity.TICKET_TAG);
if (ticket != null) {
this.customer = ticket.getCustomer();
fillInvoice(ticket);
}
}
......
......@@ -33,12 +33,12 @@ public class ExternalFile extends File {
* @throws FileNotFoundException
*/
@Override
protected FileInputStream openRead() throws FileNotFoundException {
public FileInputStream openRead() throws FileNotFoundException {
return null;
}
@Override
protected FileOutputStream openWrite() throws FileNotFoundException {
public FileOutputStream openWrite() throws FileNotFoundException {
return new FileOutputStream(this);
}
}
\ No newline at end of file
......@@ -84,7 +84,7 @@ public abstract class File extends java.io.File {
return result;
}
protected abstract FileInputStream openRead() throws FileNotFoundException;
public abstract FileInputStream openRead() throws FileNotFoundException;
protected abstract FileOutputStream openWrite() throws FileNotFoundException;
public abstract FileOutputStream openWrite() throws FileNotFoundException;
}
......@@ -21,15 +21,19 @@ public class InternalFile extends File {
super(Pasteque.getAppContext().getDir(path, Context.MODE_PRIVATE), fileName);
}
public InternalFile(String path, String fileName, int mode) {
super(Pasteque.getAppContext().getDir(path, mode), fileName);
}
public InternalFile(String dir, int directory) {
super(dir, directory);
}
protected FileInputStream openRead() throws FileNotFoundException {
public FileInputStream openRead() throws FileNotFoundException {
return new FileInputStream(this);
}
protected FileOutputStream openWrite() throws FileNotFoundException {
public FileOutputStream openWrite() throws FileNotFoundException {
return new FileOutputStream(this);
}
}
......@@ -20,5 +20,10 @@
android:showAsAction="ifRoom|withText"
android:title="@string/menu_edit_customer" />
<item
android:id="@+id/ab_menu_send"
android:icon="@android:drawable/ic_menu_send"
android:showAsAction="ifRoom|withText"
android:title="@string/menu_send" />
</menu>
\ No newline at end of file
......@@ -47,4 +47,7 @@
<string name="err_local_data">Les données doivent être envoyées avant de se déconnecter</string>
<string name="err_cash_opened">La caisse est ouverte et doit être fermée</string>
<string name="err_wrong_ean13">Barcode erroné</string>
<string name="err_create_pdf">Impossible de créer le PDF</string>
</resources>
......@@ -331,5 +331,8 @@
<string name="config_mail_enabled_desc">Envoie des rapports d\'erreur par mail</string>
<string name="invoice">Facture</string>
<string name="menu_edit_customer">Modifier un client</string>
<string name="menu_send">Envoyer</string>
<string name="err_no_email_client">Il n\'y a pas de client mail d\'installé.</string>
<string name="pick_email_client">Choisir un client mail</string>
</resources>
......@@ -47,4 +47,7 @@
<string name="err_local_data">Local data must be sent to disconnect</string>
<string name="err_cash_opened">Cash is opened and must be closed</string>
<string name="err_wrong_ean13">Wrong barcode</string>
<string name="err_create_pdf">Could not create pdf</string>
9
</resources>
......@@ -345,5 +345,8 @@
<string name="config_mail_enabled_desc">Enable mail reporting</string>
<string name="invoice">Invoice</string>
<string name="menu_edit_customer">Edit customer</string>
<string name="menu_send">Send</string>
<string name="err_no_email_client">There are no email clients installed.</string>
<string name="pick_email_client">Pick an Email client</string>
</resources>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment