Skip to content

Instantly share code, notes, and snippets.

@saeed-khalafinejad
Created January 8, 2020 12:07
Show Gist options
  • Save saeed-khalafinejad/d1ca60aca007474aa7b23f036113020d to your computer and use it in GitHub Desktop.
Save saeed-khalafinejad/d1ca60aca007474aa7b23f036113020d to your computer and use it in GitHub Desktop.
Print Invoice and Person's Transaction from Android to Bluetooth Thermal Printers
package ilia.anrdAcunt.export;
/*
1- Printing an invoice to Blutooth printers via Android.
2- Please visit the corresponding printing library at https://github.com/saeed-khalafinejad/Android-Thermal-Bluetooth-Print.
3- This code is part of Kasabeh Pocket Accounting Software (نرم افزار حسابداری جیبی کسبه). You can download the software
directly from our website or from the Cafebazaar Iranian Anroid market:
- http://mobile.kasabeh.org
- https://cafebazaar.ir/app/ilia.anrdAcunt.ui?l=en
*/
import java.util.ArrayList;
import org.kasabeh.anrdlib.dateUtil.DateFactory;
import org.kasabeh.anrdlib.dateUtil.DateMng;
import org.kasabeh.anrdlib.logical.AccDoc;
import org.kasabeh.anrdlib.logical.Article;
import org.kasabeh.anrdlib.logical.Person;
import org.kasabeh.anrdlib.util.StrPross;
import android.content.Context;
import com.woosim.printer.WoosimCmd;
import ilia.anrdAcunt.ui.ActPref;
import ilia.anrdAcunt.ui.ActPrefAdvancePrn;
import ilia.anrdAcunt.ui.R;
import ilia.anrdAcunt.util.PrefMng;
import ilia.anrdAcunt.util.StrProssPrv;
import ilia.anrdAcunt.woosim.prnLib.BidiStringBreaker;
import ilia.anrdAcunt.woosim.prnLib.IPrintToWoosim;
import ilia.anrdAcunt.woosim.prnLib.IWoosimCanvasConst;
import ilia.anrdAcunt.woosim.prnLib.WoosimWordMng;
import ilia.anrdAcunt.woosim.prnLib.WoosimPrnMng;
public class WoosimPrnInvoice6Cm extends RepGenerator implements IPrintToWoosim{
private static final int CMaxPaperChars = IWoosimCanvasConst.CMaxChars_2Inch;
protected int maxPaperChars;
protected AccDoc ad;
protected ArrayList<Article> articles;
private ArrayList<AccDoc> payments;
private String strInvoiceKind;
protected Person person;
protected WoosimPrnMng mPrnMng;
private double oldInvRemain;
protected WoosimWordMng paperMng;
protected WoosimPrnInvoice6Cm(Context c, AccDoc ad, ArrayList<Article> articles,
String strInvoiceKind, double oldInvRemain, ArrayList<AccDoc> payments,
int maxPaperChars) {
super(c);
this.maxPaperChars = maxPaperChars;
paperMng = new WoosimWordMng(maxPaperChars);
this.ad = ad;
this.articles = articles;
this.strInvoiceKind = strInvoiceKind;
this.oldInvRemain = correctRemain(oldInvRemain);
this.payments = payments;
try {
person = Person.createPerson(ad.getPersonId());
} catch (Exception e) {
person = null;
}
}
public WoosimPrnInvoice6Cm(Context c, AccDoc ad, ArrayList<Article> articles,
String strInvoiceKind, double oldInvRemain, ArrayList<AccDoc> payments) {
this(c, ad, articles, strInvoiceKind, oldInvRemain, payments, CMaxPaperChars);
}
private double correctRemain(double remain) {
if (ad.getDocKind().equals(AccDoc.CCredit)||
ad.getDocKind().equals(AccDoc.CRetSell)) return remain * -1;
return remain;
}
@Override
public void printContent(WoosimPrnMng prnMng) {
mPrnMng = prnMng;
runRepGeneration();
}
@Override
public void printEnded(WoosimPrnMng prnMng) {
if (prnMng.printSucc()){
PrefMng.saveDeviceAddr(contx, prnMng.getDeviceAddr());
/*ActInvoice inv = (ActInvoice) contx;
inv.storeInvoice();*/
}
}
@Override
protected void initializeRepCreation() throws Exception {
}
@Override
protected void generateHeader() throws Exception {
String logoPath = ActPref.getLogoFile(contx);
if (logoPath.length()!=0){
mPrnMng.printBitmap(logoPath, paperMng.getWidth());
}
String invoiceHeader = ActPref.getInvoiceHeader(contx);
if(invoiceHeader.length()!=0){
mPrnMng.printStr(paperMng.autoWrap(invoiceHeader), 2, WoosimCmd.ALIGN_CENTER);
}
mPrnMng.printStr(strInvoiceKind, 1, WoosimCmd.ALIGN_CENTER);
mPrnMng.printStr(contx.getString(R.string.strNumInvoice)+C_Collon+ad.getDocNum());
DateMng persianDate = DateFactory.createDate(ad.getInDate(), contx);
mPrnMng.printStr(contx.getString(R.string.html_date)+C_Collon+persianDate.toAlphaForm());
//String theTime = StdTime.getReadableTime(ad.getInTime());
String theTime = ad.getInTime().substring(0, 2)+C_Collon+ad.getInTime().substring(2, 4);
mPrnMng.printStr(contx.getString(R.string.wTime)+C_Collon+theTime);
}
@Override
protected void generateBody() throws Exception {
if (articles.size()==0) noArticleRep(); else hasArticleRep();
}
protected void hasArticleRep() {
boolean isRowDiscountActive = ActPref.isRowDiscountActive(contx);
boolean isRowDiscountPercent = PrefMng.getRowDiscountType(contx)==PrefMng.ROW_DISCOUNT_PERCENT;
String str = contx.getString(R.string.wPersonName)+C_Collon+person.getFullName();
mPrnMng.printStr(paperMng.autoTrim(str));
printAddrTels();
mPrnMng.printStr(paperMng.getHorizontalUnderline(), 1, WoosimCmd.ALIGN_CENTER);
double totalAmount = 0;
int rowNO = 1;
for (Article ar : articles) {
/*mPrnMng.printStr(paperMng.autoTrim(Integer.toString(rowNO++)+"-"+
BidiStringBreaker.getForceBreaker()+ar.getName()));*/
mPrnMng.printStr(paperMng.autoWordWrap(Integer.toString(rowNO++)+"-"+
BidiStringBreaker.getForceBreaker()+ar.getName()));
/*str = StrPross.readableNO(ar.getAmount())+ar.getUnit()+
StrPross.addSeparators(ar.getTotalSum())+" "+
StrPross.addSeparators(ar.getUnitPrice());
if (paperMng.isFittable(str)){
mPrnMng.printStr(str);
}else{*/
totalAmount += ar.getAmount();
str = StrPross.readableNO(ar.getAmount())+ar.getUnit()+
StrPross.addSeparators(ar.getUnitPrice());
mPrnMng.printStr(str);
if (isRowDiscountActive){
if (isRowDiscountPercent){
str = contx.getString(R.string.discount)+C_Collon+
StrPross.readableNO(ar.getDiscountPercent())+"%";
} else {
str = contx.getString(R.string.discount)+C_Collon+
StrPross.addSeparators(ar.getDiscountAmount());
}
mPrnMng.printStr(str);
}
str = StrPross.addSeparators(ar.getTotalSum());
mPrnMng.printStr(str, 1, WoosimCmd.ALIGN_LEFT);
//}
mPrnMng.printStr(paperMng.getHorizontalUnderline(), 1, WoosimCmd.ALIGN_CENTER);
}
//mPrnMng.printStr(paperMng.getHorizontalUnderline(), 1, WoosimCmd.ALIGN_CENTER);
mPrnMng.printStr(contx.getString(R.string.strTotalAmount)+C_Collon+StrPross.readableNO(totalAmount),
1, WoosimCmd.ALIGN_LEFT);
double calcSumArticles = Article.calcSum(articles);
double totalRowDiscount = Article.calcDiscountSum(articles);
if (totalRowDiscount!=0){
double withoutDiscount = calcSumArticles + totalRowDiscount;
str = contx.getString(R.string.strWNoDiscount)+C_Collon+
StrPross.addSeparators(withoutDiscount);
mPrnMng.printStr(str, 1, WoosimCmd.ALIGN_LEFT);
str = contx.getString(R.string.strWRowDiscount)+C_Collon+
StrPross.addSeparators(totalRowDiscount);
mPrnMng.printStr(str, 1, WoosimCmd.ALIGN_LEFT);
}
mPrnMng.printStr(contx.getString(R.string.total) + C_Collon +
StrPross.addSeparators(calcSumArticles),
1, WoosimCmd.ALIGN_LEFT);
if (ad.getDocDesc().length()!=0){
str = contx.getString(R.string.sharh)+C_Collon+ad.getDocDesc();
mPrnMng.printStr(paperMng.autoWrap(str));
}
if (ad.getDocDesc2().length()!=0){
str = contx.getString(R.string.desc)+C_Collon+ad.getDocDesc2();
mPrnMng.printStr(paperMng.autoWrap(str));
}
}
protected void printAddrTels() {
if (ActPrefAdvancePrn.getPrintAddr(contx)){
String str = "";
if (person.getAddr()!=null && person.getAddr().length()>0){
str = contx.getString(R.string.addr)+C_Collon+person.getAddr()+" ";
}
if ((person.getMobilePhone()!=null && person.getMobilePhone().length()>0)||
(person.getTels()!=null && person.getTels().length()>0)){
str += contx.getString(R.string.tel)+C_Collon+person.getMobilePhone()+
" "+person.getTels();
}
if (str.length()>0)
mPrnMng.printStr(paperMng.autoWordWrap(str));
}
}
private void noArticleRep() {
mPrnMng.printStr(paperMng.getHorizontalUnderline(), 1, WoosimCmd.ALIGN_CENTER);
String str = contx.getString(R.string.wPersonName)+C_Collon+
person.getFullName();
mPrnMng.printStr(paperMng.autoTrim(str));
printAddrTels();
str = contx.getString(R.string.sharh)+C_Collon+ad.getDocDesc();
mPrnMng.printStr(paperMng.autoWrap(str));
str = contx.getString(R.string.html_invoiceTotal)+C_Collon+
ad.getDocAmount()+" "+ActPref.getCurrency(contx);
if (paperMng.isFittable(str)){
mPrnMng.printStr(str);
}else{
str = contx.getString(R.string.html_invoiceTotal)+C_Collon;
mPrnMng.printStr(str);
str = ad.getDocAmount()+" "+ActPref.getCurrency(contx);
mPrnMng.printStr(str, 1, WoosimCmd.ALIGN_LEFT);
}
str = contx.getString(R.string.desc)+C_Collon+ad.getDocDesc2();
mPrnMng.printStr(paperMng.autoWrap(str));
mPrnMng.printStr(paperMng.getHorizontalUnderline(), 1, WoosimCmd.ALIGN_CENTER);
}
private double getTotalBalance() {
double paid = AccDoc.calcSum(payments);
if (ad.getDocKind().equals(AccDoc.CDebit)||
ad.getDocKind().equals(AccDoc.CRetBuy)){
paid*= -1;
}
double invRemain = StrPross.parseDouble(ad.getDocAmount()) - paid;
invRemain = correctRemain(invRemain);
return person.getBalance() - oldInvRemain + invRemain;
}
private double getOldBalance() {
return person.getBalance() - oldInvRemain;
}
@Override
protected void generateFooter() throws Exception {
generatePayments();
mPrnMng.printNewLine();
String s;
if (ActPrefAdvancePrn.getPrintOldBalance(contx)){
s = contx.getString(R.string.pastBalance)+C_Collon;
mPrnMng.printStr(s);
s = StrProssPrv.crdDebStr(getOldBalance(), contx, false);
mPrnMng.printStr(s, 1, WoosimCmd.ALIGN_LEFT);
s = contx.getString(R.string.personTotalBalance)+C_Collon;
mPrnMng.printStr(s);
s = StrProssPrv.crdDebStr(getTotalBalance(), contx, false);
mPrnMng.printStr(s, 1, WoosimCmd.ALIGN_LEFT);
mPrnMng.printNewLine();
}
s = ActPref.getInvoiceFooter(contx);
if (s.length()!=0){
mPrnMng.printStr(paperMng.autoWrap(s), 1, WoosimCmd.ALIGN_CENTER);
}
mPrnMng.printStr(contx.getString(R.string.app_name), true, false,
1, WoosimCmd.ALIGN_CENTER);
mPrnMng.printNewLine();
}
private void generatePayments() throws Exception {
String s;
String[] strKinds = contx.getResources().getStringArray(R.array.kindsLstBrief);
double total = StrPross.parseDouble(ad.getDocAmount());
double discount = AccDoc.calcSum(payments, AccDoc.CDiscountGive) * -1;
discount += AccDoc.calcSum(payments, AccDoc.CDiscountGet);
if (discount!=0){
s = strKinds[AccDoc.CDiscountGive_int]+" "+StrPross.addSeparators(discount);
mPrnMng.printStr(paperMng.autoWordWrap(s));
s = contx.getString(R.string.strPayable)+" "+StrPross.addSeparators(total - discount);
mPrnMng.printStr(paperMng.autoWordWrap(s));
}
double paid = 0;
for (AccDoc pay : payments) {
int kind = Integer.parseInt(pay.getDocKind());
if (kind!=AccDoc.CDiscountGet_int&&kind!=AccDoc.CDiscountGive_int){
if (kind==AccDoc.CCashIn_int||kind==AccDoc.CCashOut_int){
s = pay.getDocAmount() + " " + strKinds[kind];
} else if (kind==AccDoc.CChequeIn_int||kind==AccDoc.CChequeOut_int||
kind==AccDoc.CBankTranIn_int||kind==AccDoc.CBankTranOut_int){
DateMng pd = DateFactory.createDate(pay.getDocDate(), contx);
s = pay.getDocAmount() + " " + strKinds[kind]+" "+
pd.getCompact()+" "+pay.getDocDesc();
} else {
s = pay.getDocAmount() + " " + strKinds[kind]+" "+
pay.getDocDesc()+" "+pay.getDocDesc2();
}
mPrnMng.printStr(paperMng.autoTrim(s));
paid+=StrPross.parseDouble(pay.getDocAmount());
}
}
double remain = total - discount - paid;
if (remain!=0){
remain = correctRemain(remain);
s = StrProssPrv.crdDebStr(remain, contx, false);
mPrnMng.printStr(paperMng.autoTrim(s));
}
}
@Override
protected void finalizeRepCration() throws Exception {
}
@Override
protected void doOnPostAction() {
printEnded(mPrnMng);
}
}
package ilia.anrdAcunt.export;
/*
1- Printing the person's transaction to Blutooth printers via Android.
2- Please visit the corresponding printing library at https://github.com/saeed-khalafinejad/Android-Thermal-Bluetooth-Print.
3- This code is part of Kasabeh Pocket Accounting Software (نرم افزار حسابداری جیبی کسبه). You can download the software
directly from our website or from the Cafebazaar Iranian Anroid market:
- http://mobile.kasabeh.org
- https://cafebazaar.ir/app/ilia.anrdAcunt.ui?l=en
*/
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import ilia.anrdAcunt.ui.ActPref;
import ilia.anrdAcunt.ui.R;
import ilia.anrdAcunt.util.PrefMng;
import ilia.anrdAcunt.woosim.prnLib.BidiStringBreaker;
import ilia.anrdAcunt.woosim.prnLib.IPrintToWoosim;
import ilia.anrdAcunt.woosim.prnLib.IWoosimCanvasConst;
import ilia.anrdAcunt.woosim.prnLib.WoosimWordMng;
import ilia.anrdAcunt.woosim.prnLib.WoosimPrnMng;
import org.kasabeh.anrdlib.logical.AccDoc;
import org.kasabeh.anrdlib.logical.Person;
import org.kasabeh.anrdlib.util.StrPross;
import android.content.Context;
import android.database.Cursor;
import android.widget.Adapter;
import com.woosim.printer.WoosimCmd;
public class WoosimPrnPersonTran6Cm extends RepGenerator implements IPrintToWoosim{
private static final int CMaxPaperChars = IWoosimCanvasConst.CMaxChars_2Inch;
protected int maxPaperChars;
private Adapter adap;
private double total = 0;
private double sumDebit = 0;
private double sumCredit = 0;
private Person person;
private WoosimPrnMng mPrnMng;
private WoosimWordMng paperMng;
private boolean mDetailTran;
public WoosimPrnPersonTran6Cm(Context c, String personId,
Adapter adap, boolean mDetailTran) throws Exception{
this(c, personId, adap, mDetailTran, CMaxPaperChars);
}
protected WoosimPrnPersonTran6Cm(Context c, String personId,
Adapter adap, boolean mDetailTran, int maxPaperChars) throws Exception{
super(c);
this.maxPaperChars = maxPaperChars;
paperMng = new WoosimWordMng(maxPaperChars);
this.adap = adap;
this.mDetailTran = mDetailTran;
person = Person.createPerson(personId);
}
@Override
public void printContent(WoosimPrnMng prnMng) {
mPrnMng = prnMng;
runRepGeneration();
}
@Override
public void printEnded(WoosimPrnMng prnMng) {
if (prnMng.printSucc()){
PrefMng.saveDeviceAddr(contx, prnMng.getDeviceAddr());
}
}
@Override
protected void initializeRepCreation() throws Exception {
}
@Override
protected void generateHeader() throws Exception {
String str = ActPref.getRepHeader(contx);
if(str.length()!=0){
mPrnMng.printStr(paperMng.autoWrap(str), 2, WoosimCmd.ALIGN_CENTER);
}
mPrnMng.printStr(contx.getString(R.string.personTrans)+C_Collon);
mPrnMng.printStr(paperMng.autoTrim(person.getFullName()), 1, WoosimCmd.ALIGN_CENTER);
}
@Override
protected void generateBody() throws Exception {
total = 0;
sumDebit = 0;
sumCredit = 0;
mPrnMng.printStr(paperMng.getHorizontalUnderline(), 1, WoosimCmd.ALIGN_CENTER);
for(int i=adap.getCount()-1;i>=0;i--){
Cursor c = (Cursor) adap.getItem(i);
String docKind = Integer.toString(c.getInt(AccDoc.CKindIxCol));
String desc = c.getString(AccDoc.CSummaryIxCol);
desc = simpleYear(desc);
double amount = c.getDouble(AccDoc.CMoneyIxCol);
total+= amount;
if (mDetailTran){
if (docKind.equals(AccDoc.CCredit)||docKind.equals(AccDoc.CDebit)||
docKind.equals(AccDoc.CRetSell)||docKind.equals(AccDoc.CRetBuy)){
desc = simpleNO(desc);
}
}
mPrnMng.printStr(paperMng.autoWordWrap(desc));
if (amount>0){
mPrnMng.printStr("+"+StrPross.addSeparators(amount), 1, WoosimCmd.ALIGN_LEFT);
sumDebit+=amount;
}else{
mPrnMng.printStr(StrPross.addSeparators(amount), 1, WoosimCmd.ALIGN_LEFT);
sumCredit+=amount;
}
//mPrnMng.printStr(briefBalance(total), 1, WoosimCmd.ALIGN_LEFT);
mPrnMng.printStr(paperMng.getHorizontalUnderline(), 1, WoosimCmd.ALIGN_CENTER);
}
//mPrnMng.printStr(paperMng.getHorizontalUnderline(), 1, WoosimCmd.ALIGN_CENTER);
mPrnMng.printStr(StrPross.addSeparators(sumDebit)+" "+
contx.getString(R.string.briefDebit),
1, WoosimCmd.ALIGN_LEFT);
mPrnMng.printStr(StrPross.addSeparators(-sumCredit)+" "+
contx.getString(R.string.briefCredit),
1, WoosimCmd.ALIGN_LEFT);
mPrnMng.printStr(briefBalance(total, contx), 1, WoosimCmd.ALIGN_LEFT);
}
private String simpleNO(String desc) {
try {
String date = desc.substring(0, 5);
String theCount = desc.substring(6, desc.indexOf(" ", 6));
theCount = StrPross.readableNO(theCount);
return date + " " + BidiStringBreaker.getForceBreaker() +
theCount + " " + desc.substring(desc.indexOf(" ", 6) + 1);
} catch (Exception e) {
return desc;
}
}
public static String simpleYear(String desc) {
StringBuffer sb = new StringBuffer();
Pattern patt = Pattern.compile("([0-9]{4})/([0-9]{2})/([0-9]{2})");
Matcher m = patt.matcher(desc);
while (m.find()){
String g0 = m.group(0);
m.appendReplacement(sb, g0.substring(2, 10));
}
m.appendTail(sb);
return sb.toString();
}
public static String briefBalance(double total, Context contx) {
if (total>0){
return contx.getString(R.string.personBalance)+
StrPross.addSeparators(total)+" "+
contx.getString(R.string.briefDebit);
}
if (total<0){
return contx.getString(R.string.personBalance)+
StrPross.addSeparators(-total)+" "+
contx.getString(R.string.briefCredit);
}
return contx.getString(R.string.personBalance)+" "+"0";
}
@Override
protected void generateFooter() throws Exception {
String repFooter = ActPref.getRepFooter(contx);
if (repFooter.length()!=0){
mPrnMng.printStr(paperMng.autoWrap(repFooter), 1, WoosimCmd.ALIGN_CENTER);
}
mPrnMng.printStr(contx.getString(R.string.app_name), true, false,
1, WoosimCmd.ALIGN_CENTER);
mPrnMng.printNewLine();
}
@Override
protected void finalizeRepCration() throws Exception {
}
@Override
protected void doOnPostAction() {
printEnded(mPrnMng);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment