2
* $Log: MpBizDoc.java,v $
3
* Revision 1.1 2004/01/21 17:11:35 m16e
6
* Revision 1.1.1.1 2003/07/23 00:33:50 carlos
9
* Revision 1.1.1.1 2003/07/22 14:04:37 carlos
12
* Revision 1.1.1.1 2003/07/17 14:56:39 carlos
15
* Revision 1.1.1.1 2003/07/15 17:03:30 carlos
18
* Revision 1.1.1.1 2003/06/19 02:23:17 carlos
19
* restarting point - v1r01
21
* Revision 1.3 2003/06/16 19:15:55 carlos
22
* ----------------------------------------------------------------------
24
* ----------------------------------------------------------------------
25
* - adicionado DbEntFin e suporte moeda
27
* Revision 1.2 2003/05/13 18:27:48 carlos
28
* ----------------------------------------------------------------------
31
* - addicionada 'int getMoedaId()'
34
* ----------------------------------------------------------------------
36
* Revision 1.1.1.1 2003/03/31 11:25:29 carlos
37
* Starting point (v. 1.1-pre1)
42
package com.m16e.mpbiz;
49
import com.m16e.free.tools.*;
50
import com.m16e.free.tools.db.*;
51
import com.m16e.free.tools.debug.*;
52
import com.m16e.free.tools.i18n.*;
53
import com.m16e.free.tools.xml.*;
55
import com.m16e.mpbiz.factory.*;
56
import com.m16e.mpbiz.i18n.*;
57
import com.m16e.mpbiz.tables.*;
59
////////////////////////////////////////////////////////////
62
Object[] docTuple = DbDoc.TDM.getEmptyTuple();
63
Object[][] docLinTuples = null;
64
Object[] entTuple = null;
65
Object[][] docTaxaTuples = null;
66
Object[] tabTipoDocTuple = null;
68
Database database = null;
69
DbFactory dbFactory = null;
72
MpBizEntByType mpBizEntByType = null;
73
DbDocLin dbDocLin = null;
74
DbDocTaxa dbDocTaxa = null;
76
DbEntFin dbEntFin = null;
78
MpBizDocTaxas docTaxas = new MpBizDocTaxas();
79
MpBizAppContext mpBizAppContext = null;
81
ResourceBundle bundle = MpBizBundle.getBundle();
83
////////////////////////////////////////////////////////////
84
public MpBizDoc( MpBizAppContext mpBizAppContext )
87
this.mpBizAppContext = mpBizAppContext;
88
dbFactory = mpBizAppContext.getDbFactory();
89
database = dbFactory.getDatabase();
90
dbDoc = new DbDoc( database );
91
mpBizEntByType = new MpBizEntByType( database, 0 );
92
setEntTuple( mpBizEntByType.getTuple() );
93
dbDocLin = new DbDocLin( database );
94
dbDocTaxa = new DbDocTaxa( database );
95
dbProd = new DbProd( database );
98
////////////////////////////////////////////////////////////
99
public int appendLineToDoc( Object[] line )
102
if( docLinTuples != null )
103
noLines = docLinTuples.length;
104
Object[][] newLines =
105
new Object[ noLines + 1 ][ DbDocLin.FIELDS.length ];
107
if( docLinTuples != null )
109
for( ; f < docLinTuples.length; f++ )
111
newLines[f] = docLinTuples[f];
115
docLinTuples = newLines;
119
////////////////////////////////////////////////////////////
120
public Object[] getNewDocLin()
123
Object[] tuple = dbDocLin.getNewTuple();
124
DbDocLin.setTipoEntId( tuple, getTipoEntId() );
125
DbDocLin.setTipoDocId( tuple, getTipoDocId() );
126
DbDocLin.setDocId( tuple, getDocId() );
127
DbDocLin.setEntId( tuple, getEntId() );
131
////////////////////////////////////////////////////////////
132
public int getDocId() { return DbDoc.getDocId( docTuple ); }
133
public DbEntFin getDbEntFin() { return dbEntFin; }
134
public int getEntId() { return DbDoc.getEntId( docTuple ); }
135
public int getLineCount() { return docLinTuples.length; }
136
public int getMoedaId() { return DbDoc.getMoedaId( docTuple ); }
137
public int getTipoEntId() { return DbDoc.getTipoEntId( docTuple ); }
138
public int getTipoDocId() { return DbDoc.getTipoDocId( docTuple ); }
139
public java.util.Date getDataDoc() { return DbDoc.getDataDoc( docTuple ); }
141
////////////////////////////////////////////////////////////
142
public void setEntId( int entId )
143
throws MpDocException
145
boolean found = false;
148
found = mpBizEntByType.getById( entId );
150
catch( SQLException sqle )
154
" EXCEPTION: " + sqle.getMessage() +
155
"\n code: " + sqle.getErrorCode() );
156
sqle.printStackTrace();
157
throw new MpDocException(
158
"<MpBizDoc.setEntId>\n " + sqle.getMessage() );
164
entTuple = mpBizEntByType.getTuple();
165
DbDoc.setEntId( docTuple, entId );
166
if ( docLinTuples != null )
168
for ( int f = 0; f < docLinTuples.length; f++ )
170
DbDocLin.setEntId( (Object[]) docLinTuples[f], entId );
171
DbDocLin.setOrgTipoDocId( (Object[]) docLinTuples[f], 0 );
172
DbDocLin.setOrgDocId( (Object[]) docLinTuples[f], 0 );
173
DbDocLin.setOrgDocLinId( (Object[]) docLinTuples[f], 0 );
176
dbEntFin = dbFactory.getDbEntFin();
177
dbEntFin.setTipoEntId( getTipoEntId() );
178
dbEntFin.setEntId( getEntId() );
179
if( dbEntFin.getById() )
181
dbDoc.setMoedaId( dbEntFin.getMoedaId() );
186
catch( SQLException sqle )
190
" EXCEPTION: " + sqle.getMessage() +
191
"\n code: " + sqle.getErrorCode() );
192
sqle.printStackTrace();
193
throw new MpDocException(
194
"<MpBizDoc.setEntId>\n " + sqle.getMessage() );
198
throw new MpDocException(
199
"<MpBizDoc.setEntId> not found: " + entId );
201
////////////////////////////////////////////////////////////
202
public DbDoc getDbDoc()
204
dbDoc.setTuple( docTuple );
208
////////////////////////////////////////////////////////////
209
public DbDocLin getDbDocLin()
214
////////////////////////////////////////////////////////////
215
public DbProd getDbProd()
220
////////////////////////////////////////////////////////////
221
public MpBizEntByType getBizEnt()
223
mpBizEntByType.setTipoEntId( getTipoEntId() );
224
mpBizEntByType.setTuple( entTuple );
225
return mpBizEntByType;
228
////////////////////////////////////////////////////////////
229
public void setDataDoc( java.util.Date dataDoc )
231
DbDoc.setDataDoc( docTuple, dataDoc );
234
////////////////////////////////////////////////////////////
235
public void setTipoEntId( int tipoEntId )
237
mpBizEntByType.setTipoEntId( tipoEntId );
238
DbDoc.setTipoEntId( docTuple, tipoEntId );
241
////////////////////////////////////////////////////////////
242
public void setTipoDocId( int tipoDocId )
245
DbDoc.setTipoDocId( docTuple, tipoDocId );
247
mpBizAppContext.getMpDocFactory().getTipoDoc( tipoDocId );
250
////////////////////////////////////////////////////////////
251
public void setDocId( int docId )
252
throws SQLException, MpDocException
256
mpBizAppContext.getMpDocFactory().getLastDocNumber(
257
dbFactory, getTipoEntId(), getTipoDocId() ) + 1;
259
DbDoc.setDocId( docTuple, docId );
262
////////////////////////////////////////////////////////////
263
public Object[] getDocTuple() { return docTuple; }
264
public void setDocTuple( Object[] o) { docTuple = o; }
266
public Object[] getEntTuple() { return entTuple; }
267
public void setEntTuple( Object[] o ) { entTuple = o; }
269
public Object[][] getDocLinTuples() { return docLinTuples; }
270
public void setDocLinTuples( Object[][] o ) { docLinTuples = o; }
272
public Object[][] getDocTaxaTuples() { return docTaxaTuples; }
273
public void setDocTaxaTuples( Object[][] o ) { docTaxaTuples = o; }
275
public Database getDatabase() { return database; }
277
////////////////////////////////////////////////////////////
278
public void setDoc( Object[] docTuple, Object[][] docLinTuples )
281
this.docTuple = docTuple;
282
this.docLinTuples = docLinTuples;
283
dbDoc.setTuple( docTuple );
284
if ( !mpBizEntByType.getById( dbDoc.getEntId() ) )
287
entTuple = mpBizEntByType.getTuple();
289
database.getSqlFieldName(
290
dbDocTaxa.getField( DbDocTaxa.TIPO_ENT_ID ).getFieldName() ) +
291
"=" + DbDoc.getTipoEntId( docTuple ) + " and " +
292
database.getSqlFieldName(
293
dbDocTaxa.getField( DbDocTaxa.TIPO_DOC_ID ).getFieldName() ) +
294
"=" + DbDoc.getTipoDocId( docTuple ) + " and " +
295
database.getSqlFieldName(
296
dbDocTaxa.getField( DbDocTaxa.DOC_ID ).getFieldName() ) +
297
"=" + DbDoc.getDocId( docTuple );
298
docTaxaTuples = dbDocTaxa.select( where, dbDocTaxa.getPKeyOrder() );
301
////////////////////////////////////////////////////////////
302
public boolean selectDoc( int tipoEntId, int tipoDocId, int docId )
305
//Object[] tuple = dbDoc.getTuple();
306
dbDoc.setTipoEntId( tipoEntId );
307
dbDoc.setTipoDocId( tipoDocId );
308
dbDoc.setDocId( docId );
309
if ( dbDoc.getById() )
312
dbDocLin.getSqlName( DbDocLin.TIPO_ENT_ID ) +
313
" = " + tipoEntId + " and " +
314
dbDocLin.getSqlName( DbDocLin.TIPO_DOC_ID ) +
315
" = " + tipoDocId + " and " +
316
dbDocLin.getSqlName( DbDocLin.DOC_ID ) +
320
dbDocLin.select( where, dbDocLin.getPKeyOrder() ) );
321
int entId = dbDoc.getEntId();
322
mpBizEntByType.setTipoEntId( tipoEntId );
323
mpBizEntByType.getById( entId );
324
entTuple = mpBizEntByType.getTuple();
330
////////////////////////////////////////////////////////////
331
public void insertDoc()
334
dbDoc.setTuple( docTuple );
336
for ( int f = 0; f < docLinTuples.length; f++ )
338
dbDocLin.setTuple( docTuple );
343
////////////////////////////////////////////////////////////
344
public void deleteDoc()
347
int tipoEntId = DbDoc.toInt( docTuple, DbDoc.TIPO_ENT_ID );
348
int tipoDocId = DbDoc.toInt( docTuple, DbDoc.TIPO_DOC_ID );
349
int docId = DbDoc.toInt( docTuple, DbDoc.DOC_ID );
350
Statement stmt = database.getStatement();
352
"delete from " + dbDocTaxa.getTableName() +
354
dbDocTaxa.getSqlName( DbDocTaxa.TIPO_ENT_ID ) +
355
"=" + tipoEntId + " and " +
356
dbDocTaxa.getSqlName( DbDocTaxa.TIPO_DOC_ID ) +
357
"=" + tipoDocId + " and " +
358
dbDocTaxa.getSqlName( DbDocTaxa.DOC_ID ) +
361
"<MpBizDoc.deleteDoc>\n exec:\n" + exec );
362
stmt.executeUpdate( exec );
365
"delete from " + dbDocLin.getTableName() +
367
dbDocLin.getSqlName( DbDocLin.TIPO_ENT_ID ) +
368
"=" + tipoEntId + " and " +
369
dbDocLin.getSqlName( DbDocLin.TIPO_DOC_ID ) +
370
"=" + tipoDocId + " and " +
371
dbDocLin.getSqlName( DbDocLin.DOC_ID ) +
374
"<MpBizDoc.deleteDoc>\n exec:\n" + exec );
375
stmt.executeUpdate( exec );
378
"delete from " + dbDoc.getTableName() +
380
dbDoc.getSqlName( DbDoc.TIPO_ENT_ID) +
381
"=" + tipoEntId + " and " +
382
dbDoc.getSqlName( DbDoc.TIPO_DOC_ID ) +
383
"=" + tipoDocId + " and " +
384
dbDoc.getSqlName( DbDoc.DOC_ID ) +
387
"<MpBizDoc.deleteDoc>\n exec:\n" + exec );
388
stmt.executeUpdate( exec );
392
////////////////////////////////////////////////////////////
393
public void updateDoc()
400
///////////////////////////////////////////////////////////
401
public double computeProdLineValorTaxa( int f )
402
throws MpDocException
404
double total = computeProdLineTotalSemTaxa( f );
405
Object o = docLinTuples[ f ][ DbDocLin.TAXA_ID ];;
408
int taxaId = ((Integer) o).intValue();
410
mpBizAppContext.getMpDocFactory().getValorTaxa(
411
taxaId, DbDoc.getDataDoc( docTuple ) );
416
///////////////////////////////////////////////////////////
417
public double computeProdLineTotalSemTaxa( int line )
418
throws MpDocException
420
Object o = docLinTuples[ line ][ DbDocLin.QUANT ];
423
double quant = ((Double) o).doubleValue();
424
o = docLinTuples[ line ][ DbDocLin.PRECO_UNI ];
427
double precoUni = ((Double) o).doubleValue();
428
o = docLinTuples[ line ][ DbDocLin.DESCONTO1 ];
431
double desc1 = ((Double) o).doubleValue();
432
double d = quant * precoUni;
433
double total = d - (d * desc1 / 100.0);
434
total = Tools.roundTo3( total );
435
docLinTuples[ line ][ DbDocLin.TOTAL_SEM_TAXA ] = new Double( total );
439
///////////////////////////////////////////////////////////
440
public double computeProdTotal( boolean finalComputation )
441
throws MpDocException
444
for ( int f = 0; f < docLinTuples.length; f++ )
446
Object o = docLinTuples[ f ][ DbDocLin.TAXA_ID ];
448
o = new Integer( 0 );
449
int taxaId = ((Integer) o).intValue();
450
double tot = computeProdLineTotalSemTaxa( f );
451
double tax = computeProdLineValorTaxa( f );
453
docTaxas.addTaxa( taxaId, tot, tax );
455
double desconto = DbDoc.getDesconto( docTuple );
456
total -= total * desconto / 100.0;
459
"<MpBizDoc.computeProdTotal>\n total: " + total );
460
return Tools.roundTo3( total );
463
////////////////////////////////////////////////////////////
464
private void insertProdDocLine( int lineNo, Object[] line )
465
throws SQLException, MpDocException, ParseException
468
"<MpBizDoc.insertDocLine>\n lineNo: " + lineNo );
469
DbFactory dbFactory = mpBizAppContext.getDbFactory();
470
MpDocFactory mpDocFactory = mpBizAppContext.getMpDocFactory();
471
DbTabTipoDoc dbTabTipoDoc = dbFactory.getDbTabTipoDoc();
474
int docId = getDocId();
476
double taxaValorPercent = 0.0;
478
int tipoDocId = getTipoDocId();
479
double pUni = Tools.roundTo3( DbDocLin.getPrecoUni( line ) );
480
double desc1 = Tools.roundTo3( DbDocLin.getDesconto1( line ) );
481
dbTabTipoDoc.setTipoDocId( tipoDocId );
482
if( !dbTabTipoDoc.getById() )
484
MessageFormat messageFormat = new MessageFormat("");
485
Object[] msgArgs = { new Integer( tipoDocId ) };
487
bundle.getString( MpBizBundle.UNKNOWN_DOC_TYPE );
488
messageFormat.applyPattern( pattern );
489
throw new MpDocException(
490
messageFormat.format( msgArgs ) );
492
String descricao = null;
493
int docLinType = dbTabTipoDoc.getDocLinType();
494
if ( docLinType == MpDocFactory.PROD_LINE )
496
DbProd dbProd = dbFactory.getDbProd();
497
itemId = DbDocLin.getItemId( line );
498
dbProd.setProdId( itemId );
499
if ( !dbProd.getById() )
501
MessageFormat messageFormat = new MessageFormat("");
502
Object[] msgArgs = { new Integer( itemId ) };
504
bundle.getString( MpBizBundle.UNKNOWN_PROD );
505
messageFormat.applyPattern( pattern );
506
throw new MpDocException(
507
messageFormat.format( msgArgs ) );
510
pUni = dbProd.getPvenda();
511
quant = DbDocLin.getQuant( line );
512
taxaId = DbDocLin.getTaxaId( line );
514
mpDocFactory.getValorTaxa( taxaId, getDataDoc() );
515
// test for empty description
516
descricao = DbDocLin.getDescricao( line ).trim();
517
if( descricao.length() == 0 )
518
descricao = dbProd.getNome();
522
MessageFormat messageFormat = new MessageFormat("");
523
Object[] msgArgs = { new Integer( dbTabTipoDoc.getDocLinType() ) };
525
bundle.getString( MpBizBundle.BAD_LINE_TYPE );
526
messageFormat.applyPattern( pattern );
527
throw new MpDocException( messageFormat.format( msgArgs ) );
530
DbDocLin.setDocId( line, docId );
531
DbDocLin.setDocLinId( line, lineNo );
532
DbDocLin.setItemId( line, itemId );
533
DbDocLin.setDescricao( line, descricao );
534
DbDocLin.setTaxaValorPercent( line, taxaValorPercent );
535
DbDocLin.setPrecoUni( line, pUni );
536
DbDocLin.setPrecoUniM0(
537
line, pUni * DbDoc.getMoedaCot( docTuple ) );
539
boolean qtChanged = false;
540
int orgTipoDocId = DbDocLin.getOrgTipoDocId( line );
541
if( orgTipoDocId != 0 )
543
DbDocLin orgDocLin = mpDocFactory.getLineOrg( dbDocLin );
544
if ( orgDocLin == null )
546
throw new MpDocException(
547
MpBizBundle.getBundle().getString( MpBizBundle.BAD_ORG_LINE ) );
549
double qt2 = orgDocLin.getQuant();
556
DbDocLin.setQuant( line, quant );
557
double totalSemTaxa = Tools.calcPercent( quant * pUni, desc1 );
558
DbDocLin.setTotalSemTaxa( line, totalSemTaxa );
559
DbDocLin.setValorTaxa( line, computeProdLineValorTaxa( lineNo - 1 ) );
560
dbDocLin.setTuple( line );
561
mpDocFactory.applyRulesToLine( getDbDoc(), dbDocLin );
563
DebugFactory.printTableRow(
564
"MpBizDoc.insertProdDocLine." + lineNo, dbDocLin );
567
if( dbDocLin.getLineType() == MpDocFactory.PROD_LINE &&
568
dbDocLin.getOrgTipoDocId() > 0 )
570
mpDocFactory.checkDocOrg(
571
dbDocLin.getTipoEntId(), dbDocLin.getOrgTipoDocId(),
572
dbDocLin.getOrgDocId() );
576
////////////////////////////////////////////////////////////
577
public void createProdDocument()
578
throws MpDocException
580
DbFactory dbFactory = mpBizAppContext.getDbFactory();
581
MpDocFactory mpDocFactory = mpBizAppContext.getMpDocFactory();
582
MpBizFactory mpBizFactory = mpBizAppContext.getMpBizFactory();
583
Connection con = mpBizAppContext.getDatabase().getConnection();
586
int tipoEntId = getTipoEntId();
587
int tipoDocId = getTipoDocId();
588
int orgDocId = getDocId();
594
mpDocFactory.getLastDocNumber(
595
dbFactory, tipoEntId, tipoDocId ) + 1;
596
DbDoc.setDocId( docTuple, docId );
599
case MpDocFactory.FACTURA:
602
mpDocFactory.getDataLimitePagamento(
603
tipoEntId, getEntId(), getDataDoc() ) );
607
double totalComTaxas = computeProdTotal( true );
608
DbDoc.setTotalComTaxas( docTuple, totalComTaxas );
609
DbDoc.setTotalComTaxasM0( docTuple, totalComTaxas * dbDoc.getMoedaCot() );
610
double totSemTaxas = 0.0;
612
for ( f = 0; f < docTaxas.getSize(); f++ )
615
totSemTaxas += docTaxas.getDocTaxa( f ).getIncidencia();
617
DbDoc.setTotalSemTaxas( docTuple, totSemTaxas );
618
DbDoc.setUserId( docTuple, mpBizAppContext.getMpBizUser().getUserId() );
620
DbDoc.setTs( docTuple, new Timestamp( new java.util.Date().getTime() ) );
622
//Statement stmt = null;
623
con.setAutoCommit( false );
626
DbEntTipo dbEntTipo = dbFactory.getDbEntTipo();
627
dbEntTipo.setTipoEntId( tipoEntId );
628
int entId = getEntId();
629
dbEntTipo.setEntId( entId );
630
if( !dbEntTipo.getById() )
632
dbEntTipo.setTuple( dbEntTipo.getDataModel().getEmptyTuple() );
633
dbEntTipo.setTipoEntId( tipoEntId );
634
dbEntTipo.setEntId( entId );
638
DbEntFin dbEntFin = dbFactory.getDbEntFin();
639
dbEntFin.setTipoEntId( tipoEntId );
640
dbEntFin.setEntId( entId );
641
if( !dbEntFin.getById() )
643
dbEntFin.setTuple( dbEntFin.getDataModel().getEmptyTuple() );
644
dbEntFin.setTipoEntId( tipoEntId );
645
dbEntFin.setEntId( entId );
648
dbDoc.setTuple( docTuple );
650
for ( f = 0; f < docLinTuples.length; f++ )
652
insertProdDocLine( f + 1, docLinTuples[f] );
654
DbDocTaxa dbDocTaxa = dbFactory.getDbDocTaxa();
655
dbDocTaxa.setTuple( dbDocTaxa.getNewTuple() );
656
dbDocTaxa.setTipoEntId( tipoEntId );
657
dbDocTaxa.setTipoDocId( tipoDocId );
658
dbDocTaxa.setDocId( docId );
659
for ( f = 0; f < docTaxas.getSize(); f++ )
661
MpBizDocTaxa docTaxa = docTaxas.getDocTaxa( f );
662
dbDocTaxa.setTaxaId( docTaxa.getTaxaId() );
663
dbDocTaxa.setIncidencia( docTaxa.getIncidencia() );
664
dbDocTaxa.setImposto( docTaxa.getValorTaxa() );
669
selectDoc( tipoEntId, tipoDocId, docId );
672
mpDocFactory.getTipoDoc( dbDoc.getTipoDocId() );
674
mpDocFactory.getDocProps(
675
dbDoc.getTipoEntId(), dbDoc.getTipoDocId() );
677
catch( ParseException pse )
680
"<MpBizDoc.createDocument>\n PARSE EXCEPTION: " +
682
pse.printStackTrace();
687
catch( SQLException sqle2 )
691
throw new MpDocException( pse.getMessage() );
694
catch( SQLException sqle )
700
catch( SQLException sqle2 )
704
sqle.printStackTrace();
705
throw new MpDocException(
707
MpDocException.SQL_ERROR,
708
sqle.getErrorCode() );
711
catch( MpDocException e )
718
catch( SQLException sqle2 )
727
///////////////////////////////////////////////////////////
728
public double computeDocTotal( boolean finalComputation )
731
for ( int f = 0; f < docLinTuples.length; f++ )
733
int tipoDocId = getTipoDocId();
734
double totAPagar = DbDocLin.getTotalSemTaxa( docLinTuples[ f ] );
735
if( tipoDocId != MpDocFactory.NOTA_CREDITO )
740
System.out.println( "<MpBizDoc.computeDocTotal>\n total: " + total );
744
////////////////////////////////////////////////////////////
745
private void insertDocDocLine( int lineNo, Object[] line )
746
throws SQLException, MpDocException, ParseException
748
DbFactory dbFactory = mpBizAppContext.getDbFactory();
749
MpDocFactory mpDocFactory = mpBizAppContext.getMpDocFactory();
750
DbTabTipoDoc dbTabTipoDoc = dbFactory.getDbTabTipoDoc();
752
int docId = getDocId();
753
int tipoDocId = getTipoDocId();
754
int tipoEntId = getTipoEntId();
755
dbTabTipoDoc.setTipoDocId( tipoDocId );
756
dbTabTipoDoc.getById();
757
if ( dbTabTipoDoc.getDocLinType() == MpDocFactory.DOC_LINE )
762
DbDocLin.setDocLinId( line, lineNo );
763
DbDocLin.setDocId( line, docId );
764
DbDocLin.setTipoDocId( line, tipoDocId );
765
DbDocLin.setTipoEntId( line, tipoEntId );
766
dbDoc.setTuple( docTuple );
767
dbDocLin.setTuple( line );
768
boolean qtChanged = false;
769
Database db = dbFactory.getDatabase();
772
mpDocFactory.applyRulesToLine( dbDoc, dbDocLin );
773
mpDocFactory.checkDocOrg(
774
dbDocLin.getTipoEntId(), dbDocLin.getOrgTipoDocId(), docId );
776
DebugFactory.printTableRow(
777
"MpBizDoc.insertDocLine", dbDocLin );
781
////////////////////////////////////////////////////////////
782
public void createDocDocument()
783
throws MpDocException
785
DbFactory dbFactory = mpBizAppContext.getDbFactory();
786
MpDocFactory mpDocFactory = mpBizAppContext.getMpDocFactory();
787
MpBizFactory mpBizFactory = mpBizAppContext.getMpBizFactory();
788
Connection con = mpBizAppContext.getDatabase().getConnection();
791
int tipoEntId = getTipoEntId();
792
int tipoDocId = getTipoDocId();
794
mpDocFactory.getLastDocNumber(
795
dbFactory, tipoEntId, tipoDocId ) + 1;
796
DbDoc.setDocId( docTuple, docId );
797
DbDoc.setUserId( docTuple, mpBizAppContext.getMpBizUser().getUserId() );
799
double totalComTaxas = computeDocTotal( true );
800
DbDoc.setTotalComTaxas( docTuple, totalComTaxas );
801
double moedaCot = DbDoc.getMoedaCot( docTuple );
802
DbDoc.setTotalComTaxasM0( docTuple, totalComTaxas * moedaCot );
804
DbDoc.setUserId( docTuple, mpBizAppContext.getMpBizUser().getUserId() );
805
DbDoc.setEstadoId( docTuple, 0 );
806
if( tipoDocId == MpDocFactory.RECIBO )
807
DbDoc.setEstadoFinal( docTuple, true );
809
DbDoc.setTs( docTuple, new Timestamp( new java.util.Date().getTime() ) );
810
dbDoc.setTuple( docTuple );
811
con.setAutoCommit( false );
813
for ( int f = 0; f < docLinTuples.length; f++ )
815
insertDocDocLine( f + 1, docLinTuples[f] );
820
catch( ParseException pse )
823
"<MpDocFromDocFrame.createDocument>\n PARSE EXCEPTION: " +
825
pse.printStackTrace();
830
catch( SQLException sqle2 )
834
throw new MpDocException( pse.getMessage() );
837
catch( SQLException sqle )
843
catch( SQLException sqle2 )
847
sqle.printStackTrace();
848
throw new MpDocException(
850
MpDocException.SQL_ERROR,
851
sqle.getErrorCode() );
854
catch( MpDocException e )
861
catch( SQLException sqle2 )
870
////////////////////////////////////////////////////////////
871
public void createDocument()
872
throws MpDocException
874
DbFactory dbFactory = mpBizAppContext.getDbFactory();
875
MpDocFactory mpDocFactory = mpBizAppContext.getMpDocFactory();
876
MpBizFactory mpBizFactory = mpBizAppContext.getMpBizFactory();
877
Connection con = mpBizAppContext.getDatabase().getConnection();
880
int tipoEntId = getTipoEntId();
881
int tipoDocId = getTipoDocId();
882
int orgDocId = getDocId();
883
DbTabTipoDoc dbTabTipoDoc = dbFactory.getDbTabTipoDoc();
884
dbTabTipoDoc.setTipoDocId( getTipoDocId() );
885
if( dbTabTipoDoc.getById() )
887
int docLinType = dbTabTipoDoc.getDocLinType();
888
if( docLinType == MpDocFactory.PROD_LINE )
890
createProdDocument();
892
else if( docLinType == MpDocFactory.DOC_LINE )
898
MessageFormat messageFormat = new MessageFormat("");
899
Object[] msgArgs = { new Integer( docLinType ) };
901
bundle.getString( MpBizBundle.BAD_LINE_TYPE );
902
messageFormat.applyPattern( pattern );
903
throw new MpDocException( messageFormat.format( msgArgs ) );
907
catch( SQLException sqle )
909
try { con.rollback(); }
910
catch( SQLException sqle2 ) {}
912
sqle.printStackTrace();
913
throw new MpDocException(
915
MpDocException.SQL_ERROR,
916
sqle.getErrorCode() );
921
////////////////////////////////////////////////////////////
922
private void insertTmpDocDocLine( int lineNo, Object[] line )
923
throws SQLException, MpDocException
925
DbFactory dbFactory = mpBizAppContext.getDbFactory();
926
dbDocLin.setTuple( line );
927
dbDocLin.setDocLinId( lineNo );
931
////////////////////////////////////////////////////////////
932
public int saveDocDocument()
933
throws MpDocException
936
DbFactory dbFactory = mpBizAppContext.getDbFactory();
937
MpDocFactory mpDocFactory = mpBizAppContext.getMpDocFactory();
938
MpBizFactory mpBizFactory = mpBizAppContext.getMpBizFactory();
939
Connection con = mpBizAppContext.getDatabase().getConnection();
942
int tipoEntId = getTipoEntId();
943
int tipoDocId = getTipoDocId();
944
int orgDocId = getDocId();
947
setDocId( orgDocId );
954
mpDocFactory.getLastTmpDocNumber(
955
dbFactory, tipoEntId, tipoDocId ) - 1;
957
double totalComTaxas = 0.0;
958
DbTabTipoDoc dbTabTipoDoc = dbFactory.getDbTabTipoDoc();
959
dbTabTipoDoc.setTipoDocId( getTipoDocId() );
961
if( dbTabTipoDoc.getById() )
963
docLinType = dbTabTipoDoc.getDocLinType();
964
if( docLinType == MpDocFactory.PROD_LINE )
966
totalComTaxas = computeProdTotal( true );
968
else if( docLinType == MpDocFactory.DOC_LINE )
970
totalComTaxas = computeProdTotal( true );
974
MessageFormat messageFormat = new MessageFormat("");
975
Object[] msgArgs = { new Integer( docLinType ) };
977
bundle.getString( MpBizBundle.BAD_LINE_TYPE );
978
messageFormat.applyPattern( pattern );
979
throw new MpDocException( messageFormat.format( msgArgs ) );
984
MessageFormat messageFormat = new MessageFormat("");
985
Object[] msgArgs = { new Integer( docLinType ) };
987
bundle.getString( MpBizBundle.BAD_LINE_TYPE );
988
messageFormat.applyPattern( pattern );
989
throw new MpDocException( messageFormat.format( msgArgs ) );
992
DbDoc.setDocId( docTuple, docId );
993
DbDoc.setUserId( docTuple, mpBizAppContext.getMpBizUser().getUserId() );
995
DbDoc.setTotalComTaxas( docTuple, totalComTaxas );
996
DbDoc.setTotalSemTaxas( docTuple, 0.0 );
997
DbDoc.setUserId( docTuple, mpBizAppContext.getMpBizUser().getUserId() );
998
DbDoc.setEstadoId( docTuple, 0 );
1000
DbDoc.setTs( docTuple, new Timestamp( new java.util.Date().getTime() ) );
1002
con.setAutoCommit( false );
1004
// test for ent_tipo
1005
int entId = getEntId();
1006
DbEntTipo dbEntTipo = dbFactory.getDbEntTipo();
1007
dbEntTipo.setTipoEntId( tipoEntId );
1008
dbEntTipo.setEntId( entId );
1009
if( !dbEntTipo.getById() )
1013
DbEntFin dbEntFin = dbFactory.getDbEntFin();
1014
dbEntFin.setTipoEntId( tipoEntId );
1015
dbEntFin.setEntId( entId );
1016
if( !dbEntFin.getById() )
1018
dbDoc.setTuple( docTuple );
1020
for( int f = 0; f < docLinTuples.length; f++ )
1022
insertTmpDocDocLine( f + 1, docLinTuples[f] );
1026
catch( SQLException sqle )
1032
catch( SQLException sqle2 )
1036
sqle.printStackTrace();
1037
throw new MpDocException(
1039
MpDocException.SQL_ERROR,
1040
sqle.getErrorCode() );
1043
catch( MpDocException e )
1045
e.printStackTrace();
1050
catch( SQLException sqle2 )
1059
////////////////////////////////////////////////////////////
1060
private void insertTmpProdDocLine( int lineNo, Object[] line )
1061
throws SQLException, MpDocException
1063
DbFactory dbFactory = mpBizAppContext.getDbFactory();
1064
MpDocFactory mpDocFactory = mpBizAppContext.getMpDocFactory();
1070
double taxaValorPercent = 0.0;
1072
int tipoDocId = getTipoDocId();
1073
String descricao = null;
1074
DbProd dbProd = dbFactory.getDbProd();
1075
itemId = DbDocLin.getItemId( line );
1076
dbProd.setProdId( itemId );
1077
if ( !dbProd.getById() )
1079
MessageFormat messageFormat = new MessageFormat("");
1080
Object[] msgArgs = { new Integer( itemId ) };
1082
bundle.getString( MpBizBundle.UNKNOWN_PROD );
1083
messageFormat.applyPattern( pattern );
1084
throw new MpDocException( messageFormat.format( msgArgs ) );
1087
quant = DbDocLin.getQuant( line );
1088
taxaId = DbDocLin.getTaxaId( line );
1090
mpDocFactory.getValorTaxa( taxaId, getDataDoc() );
1091
DbProdArm dbProdArm = dbFactory.getDbProdArm();
1092
dbProdArm.setArmId( auxItemId );
1093
dbProdArm.setProdId( itemId );
1094
if( !dbProdArm.getById() )
1098
// test for empty description
1099
descricao = DbDocLin.getDescricao( line ).trim();
1100
if( descricao.length() == 0 )
1102
descricao = dbProd.getNome();
1104
DbDocLin.setDocLinId( line, lineNo );
1105
DbDocLin.setDescricao( line, descricao );
1106
DbDocLin.setTaxaValorPercent( line, taxaValorPercent );
1107
DbDocLin.setOrgTipoDocId( line, 0 );
1108
DbDocLin.setOrgDocId( line, 0 );
1109
DbDocLin.setOrgDocLinId( line, 0 );
1110
double totalSemTaxa =
1112
quant * dbDocLin.getPrecoUni(), dbDocLin.getDesconto1() );
1113
DbDocLin.setTotalSemTaxa( line, totalSemTaxa );
1114
DbDocLin.setValorTaxa( line, computeProdLineValorTaxa( lineNo - 1 ) );
1115
dbDocLin.setTuple( line );
1119
////////////////////////////////////////////////////////////
1120
public int saveProdDocument()
1121
throws MpDocException
1124
DbFactory dbFactory = mpBizAppContext.getDbFactory();
1125
MpDocFactory mpDocFactory = mpBizAppContext.getMpDocFactory();
1126
MpBizFactory mpBizFactory = mpBizAppContext.getMpBizFactory();
1127
Connection con = mpBizAppContext.getDatabase().getConnection();
1130
int tipoEntId = getTipoEntId();
1131
int tipoDocId = getTipoDocId();
1132
int orgDocId = getDocId();
1135
setDocId( orgDocId );
1142
mpDocFactory.getLastTmpDocNumber(
1143
dbFactory, tipoEntId, tipoDocId ) - 1;
1145
DbDoc.setDocId( docTuple, docId );
1146
DbDoc.setUserId( docTuple, mpBizAppContext.getMpBizUser().getUserId() );
1147
double totalComTaxas = computeProdTotal( true );
1148
DbDoc.setTotalComTaxas( docTuple, totalComTaxas );
1149
double totSemTaxas = 0.0;
1151
for ( f = 0; f < docTaxas.getSize(); f++ )
1154
totSemTaxas += docTaxas.getDocTaxa( f ).getIncidencia();
1156
DbDoc.setTotalSemTaxas( docTuple, totSemTaxas );
1157
DbDoc.setUserId( docTuple, mpBizAppContext.getMpBizUser().getUserId() );
1158
DbDoc.setEstadoId( docTuple, 0 );
1160
DbDoc.setTs( docTuple, new Timestamp( new java.util.Date().getTime() ) );
1162
con.setAutoCommit( false );
1164
int entId = getEntId();
1165
// test for ent_tipo
1166
DbEntTipo dbEntTipo = dbFactory.getDbEntTipo();
1167
dbEntTipo.setTipoEntId( tipoEntId );
1168
dbEntTipo.setEntId( entId );
1169
if( !dbEntTipo.getById() )
1171
dbEntTipo.setTuple( dbEntTipo.getDataModel().getEmptyTuple() );
1172
dbEntTipo.setTipoEntId( tipoEntId );
1173
dbEntTipo.setEntId( entId );
1177
DbEntFin dbEntFin = dbFactory.getDbEntFin();
1178
dbEntFin.setTipoEntId( tipoEntId );
1179
dbEntFin.setEntId( entId );
1180
if( !dbEntFin.getById() )
1182
dbEntFin.setTuple( dbEntFin.getDataModel().getEmptyTuple() );
1183
dbEntFin.setTipoEntId( tipoEntId );
1184
dbEntFin.setEntId( entId );
1187
dbDoc.setTuple( docTuple );
1189
for ( f = 0; f < docLinTuples.length; f++ )
1191
insertTmpProdDocLine( f + 1, docLinTuples[f] );
1195
catch( SQLException sqle )
1201
catch( SQLException sqle2 )
1205
sqle.printStackTrace();
1206
throw new MpDocException(
1208
MpDocException.SQL_ERROR,
1209
sqle.getErrorCode() );
1212
catch( MpDocException e )
1214
e.printStackTrace();
1219
catch( SQLException sqle2 )
1229
////////////////////////////////////////////////////////////
1230
public void saveDocument()
1231
throws MpDocException
1233
DbFactory dbFactory = mpBizAppContext.getDbFactory();
1234
MpDocFactory mpDocFactory = mpBizAppContext.getMpDocFactory();
1235
MpBizFactory mpBizFactory = mpBizAppContext.getMpBizFactory();
1236
Connection con = mpBizAppContext.getDatabase().getConnection();
1239
int tipoDocId = getTipoDocId();
1240
DbTabTipoDoc dbTabTipoDoc = dbFactory.getDbTabTipoDoc();
1241
dbTabTipoDoc.setTipoDocId( getTipoDocId() );
1242
if( dbTabTipoDoc.getById() )
1244
int docLinType = dbTabTipoDoc.getDocLinType();
1245
if( docLinType == MpDocFactory.PROD_LINE )
1249
else if( docLinType == MpDocFactory.DOC_LINE )
1255
MessageFormat messageFormat = new MessageFormat("");
1256
Object[] msgArgs = { new Integer( docLinType ) };
1258
bundle.getString( MpBizBundle.BAD_LINE_TYPE );
1259
messageFormat.applyPattern( pattern );
1260
throw new MpDocException( messageFormat.format( msgArgs ) );
1264
catch( SQLException sqle )
1266
try { con.rollback(); }
1267
catch( SQLException sqle2 ) {}
1269
sqle.printStackTrace();
1270
throw new MpDocException(
1272
MpDocException.SQL_ERROR,
1273
sqle.getErrorCode() );
1278
////////////////////////////////////////////////////////////
1279
public void validateDocument()
1280
throws MpDocException
1282
DbFactory dbFactory = mpBizAppContext.getDbFactory();
1283
dbDoc.setTuple( docTuple );
1286
MpBizEntByType mpBizEntByType = getBizEnt();
1287
if ( !mpBizEntByType.getById() )
1289
MessageFormat messageFormat = new MessageFormat("");
1290
Object[] msgArgs = { new Integer( getEntId() ) };
1292
bundle.getString( MpBizBundle.UNKNOWN_ENT );
1293
messageFormat.applyPattern( pattern );
1294
throw new MpDocException( messageFormat.format( msgArgs ) );
1296
for ( int f = 0; f < docLinTuples.length; f++ )
1298
int tipoDocId = DbDoc.getTipoDocId( docTuple );
1299
int docId = DbDoc.getDocId( docTuple );
1300
int tipoDocLinId = DbDocLin.getTipoDocId( docLinTuples[f] );
1301
int docLinId = DbDocLin.getDocId( docLinTuples[f] );
1302
if( docId != docLinId )
1304
MessageFormat messageFormat = new MessageFormat("");
1306
{ DbDoc.FIELDS[ DbDoc.DOC_ID ],
1307
new Integer( docId ),
1308
new Integer( docLinId ) };
1310
bundle.getString( MpBizBundle.DOC_KEY_MISMATCH );
1311
messageFormat.applyPattern( pattern );
1312
throw new MpDocException( messageFormat.format( msgArgs ) );
1314
if( tipoDocId != tipoDocLinId )
1316
MessageFormat messageFormat = new MessageFormat("");
1318
{ DbDoc.FIELDS[ DbDoc.TIPO_DOC_ID ],
1319
new Integer( tipoDocId ),
1320
new Integer( tipoDocLinId ) };
1322
bundle.getString( MpBizBundle.DOC_KEY_MISMATCH );
1323
messageFormat.applyPattern( pattern );
1324
throw new MpDocException( messageFormat.format( msgArgs ) );
1328
catch( SQLException sqle )
1331
"<MpDocFromDocFrame.validateDocument>\n " + sqle.getMessage() );
1332
sqle.printStackTrace();
1333
throw new MpDocException(
1335
MpDocException.SQL_ERROR,
1336
sqle.getErrorCode() );