~vcs-imports/evaristo/trunk

« back to all changes in this revision

Viewing changes to com/m16e/mpbiz/MpBizDoc.java

  • Committer: m16e
  • Date: 2004-01-21 17:10:58 UTC
  • Revision ID: vcs-imports@canonical.com-20040121171058-00w8f93ynnk2xwve
Initial revision

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * $Log: MpBizDoc.java,v $
 
3
 * Revision 1.1  2004/01/21 17:11:35  m16e
 
4
 * Initial revision
 
5
 *
 
6
 * Revision 1.1.1.1  2003/07/23 00:33:50  carlos
 
7
 * Reload v1r2
 
8
 *
 
9
 * Revision 1.1.1.1  2003/07/22 14:04:37  carlos
 
10
 * CVS reload: v1r2
 
11
 *
 
12
 * Revision 1.1.1.1  2003/07/17 14:56:39  carlos
 
13
 * v1r2
 
14
 *
 
15
 * Revision 1.1.1.1  2003/07/15 17:03:30  carlos
 
16
 * Start of v1r2
 
17
 *
 
18
 * Revision 1.1.1.1  2003/06/19 02:23:17  carlos
 
19
 * restarting point - v1r01
 
20
 *
 
21
 * Revision 1.3  2003/06/16 19:15:55  carlos
 
22
 * ----------------------------------------------------------------------
 
23
 * versao 1.01
 
24
 * ----------------------------------------------------------------------
 
25
 * - adicionado DbEntFin e suporte moeda
 
26
 *
 
27
 * Revision 1.2  2003/05/13 18:27:48  carlos
 
28
 * ----------------------------------------------------------------------
 
29
 * - Version 1.1-beta2
 
30
 *
 
31
 * - addicionada 'int getMoedaId()'
 
32
 *
 
33
 *
 
34
 * ----------------------------------------------------------------------
 
35
 *
 
36
 * Revision 1.1.1.1  2003/03/31 11:25:29  carlos
 
37
 * Starting point (v. 1.1-pre1)
 
38
 *
 
39
 *
 
40
 */
 
41
 
 
42
package com.m16e.mpbiz;
 
43
 
 
44
import java.sql.*;
 
45
import java.io.*;
 
46
import java.text.*;
 
47
import java.util.*;
 
48
 
 
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.*;
 
54
 
 
55
import com.m16e.mpbiz.factory.*;
 
56
import com.m16e.mpbiz.i18n.*;
 
57
import com.m16e.mpbiz.tables.*;
 
58
 
 
59
////////////////////////////////////////////////////////////
 
60
public class MpBizDoc
 
61
{
 
62
        Object[] docTuple = DbDoc.TDM.getEmptyTuple();
 
63
        Object[][] docLinTuples = null;
 
64
        Object[] entTuple = null;
 
65
        Object[][] docTaxaTuples = null;
 
66
        Object[] tabTipoDocTuple = null;
 
67
 
 
68
        Database database = null;
 
69
        DbFactory dbFactory = null;
 
70
        
 
71
        DbDoc dbDoc = null;
 
72
        MpBizEntByType mpBizEntByType = null;
 
73
        DbDocLin dbDocLin = null;
 
74
        DbDocTaxa dbDocTaxa = null;
 
75
        DbProd dbProd = null;
 
76
        DbEntFin dbEntFin = null;
 
77
        
 
78
        MpBizDocTaxas docTaxas = new MpBizDocTaxas();
 
79
        MpBizAppContext mpBizAppContext = null;
 
80
        
 
81
        ResourceBundle bundle = MpBizBundle.getBundle();
 
82
                
 
83
        ////////////////////////////////////////////////////////////
 
84
        public MpBizDoc( MpBizAppContext mpBizAppContext )
 
85
                throws SQLException
 
86
        {
 
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 );
 
96
        }
 
97
        
 
98
        ////////////////////////////////////////////////////////////
 
99
        public int appendLineToDoc( Object[] line )
 
100
        {
 
101
                int noLines = 0;
 
102
                if( docLinTuples != null )
 
103
                        noLines = docLinTuples.length;
 
104
                Object[][] newLines = 
 
105
                        new Object[ noLines + 1 ][ DbDocLin.FIELDS.length ];
 
106
                int f = 0;
 
107
                if( docLinTuples != null )
 
108
                {
 
109
                        for( ; f < docLinTuples.length; f++ )
 
110
                        {
 
111
                                newLines[f] = docLinTuples[f];
 
112
                        }
 
113
                }
 
114
                newLines[f] = line;
 
115
                docLinTuples = newLines;
 
116
                return f;
 
117
        }
 
118
        
 
119
        ////////////////////////////////////////////////////////////
 
120
        public Object[] getNewDocLin()
 
121
                throws SQLException
 
122
        {
 
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() );
 
128
                return tuple;
 
129
        }
 
130
        
 
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 ); }
 
140
 
 
141
        ////////////////////////////////////////////////////////////
 
142
        public void setEntId( int entId ) 
 
143
                throws MpDocException
 
144
        { 
 
145
                boolean found = false;
 
146
                try
 
147
                {
 
148
                        found = mpBizEntByType.getById( entId );
 
149
                }
 
150
                catch( SQLException sqle )
 
151
                {
 
152
                        DebugFactory.print( 
 
153
                                "MpBizDoc.setEntId",
 
154
                                "  EXCEPTION: " + sqle.getMessage() +
 
155
                                "\n  code: " + sqle.getErrorCode() );
 
156
                        sqle.printStackTrace();
 
157
                        throw new MpDocException( 
 
158
                                "<MpBizDoc.setEntId>\n  " + sqle.getMessage() );
 
159
                }
 
160
                if ( found )
 
161
                {
 
162
                        try
 
163
                        {
 
164
                                entTuple = mpBizEntByType.getTuple();
 
165
                                DbDoc.setEntId( docTuple, entId ); 
 
166
                                if ( docLinTuples != null )
 
167
                                {
 
168
                                        for ( int f = 0; f < docLinTuples.length; f++ )
 
169
                                        {
 
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 );
 
174
                                        }
 
175
                                }
 
176
                                dbEntFin = dbFactory.getDbEntFin();
 
177
                                dbEntFin.setTipoEntId( getTipoEntId() );
 
178
                                dbEntFin.setEntId( getEntId() );
 
179
                                if( dbEntFin.getById() )
 
180
                                {
 
181
                                        dbDoc.setMoedaId( dbEntFin.getMoedaId() );
 
182
                                }
 
183
                                else
 
184
                                        dbEntFin = null;
 
185
                        }
 
186
                        catch( SQLException sqle )
 
187
                        {
 
188
                                DebugFactory.print( 
 
189
                                        "MpBizDoc.setEntId",
 
190
                                        "  EXCEPTION: " + sqle.getMessage() +
 
191
                                        "\n  code: " + sqle.getErrorCode() );
 
192
                                sqle.printStackTrace();
 
193
                                throw new MpDocException( 
 
194
                                        "<MpBizDoc.setEntId>\n  " + sqle.getMessage() );
 
195
                        }
 
196
                }
 
197
                else
 
198
                        throw new MpDocException( 
 
199
                                "<MpBizDoc.setEntId> not found: " + entId );
 
200
        }
 
201
        ////////////////////////////////////////////////////////////
 
202
        public DbDoc getDbDoc() 
 
203
        { 
 
204
                dbDoc.setTuple( docTuple );
 
205
                return dbDoc;
 
206
        }
 
207
        
 
208
        ////////////////////////////////////////////////////////////
 
209
        public DbDocLin getDbDocLin() 
 
210
        { 
 
211
                return dbDocLin;
 
212
        }
 
213
        
 
214
        ////////////////////////////////////////////////////////////
 
215
        public DbProd getDbProd() 
 
216
        { 
 
217
                return dbProd;
 
218
        }
 
219
        
 
220
        ////////////////////////////////////////////////////////////
 
221
        public MpBizEntByType getBizEnt() 
 
222
        { 
 
223
                mpBizEntByType.setTipoEntId( getTipoEntId() );
 
224
                mpBizEntByType.setTuple( entTuple );
 
225
                return mpBizEntByType;
 
226
        }
 
227
        
 
228
        ////////////////////////////////////////////////////////////
 
229
        public void setDataDoc( java.util.Date dataDoc ) 
 
230
        { 
 
231
                DbDoc.setDataDoc( docTuple, dataDoc ); 
 
232
        }
 
233
        
 
234
        ////////////////////////////////////////////////////////////
 
235
        public void setTipoEntId( int tipoEntId ) 
 
236
        { 
 
237
                mpBizEntByType.setTipoEntId( tipoEntId );
 
238
                DbDoc.setTipoEntId( docTuple, tipoEntId ); 
 
239
        }
 
240
        
 
241
        ////////////////////////////////////////////////////////////
 
242
        public void setTipoDocId( int tipoDocId ) 
 
243
                throws SQLException
 
244
        { 
 
245
                DbDoc.setTipoDocId( docTuple, tipoDocId ); 
 
246
                tabTipoDocTuple = 
 
247
                        mpBizAppContext.getMpDocFactory().getTipoDoc( tipoDocId );
 
248
        }
 
249
        
 
250
        ////////////////////////////////////////////////////////////
 
251
        public void setDocId( int docId )
 
252
                throws SQLException, MpDocException
 
253
        { 
 
254
                if ( docId == 0 )
 
255
                        docId = 
 
256
                                mpBizAppContext.getMpDocFactory().getLastDocNumber( 
 
257
                                        dbFactory, getTipoEntId(), getTipoDocId() ) + 1;
 
258
 
 
259
                DbDoc.setDocId( docTuple, docId ); 
 
260
        }
 
261
        
 
262
        ////////////////////////////////////////////////////////////
 
263
        public Object[] getDocTuple() { return docTuple; }
 
264
        public void setDocTuple( Object[] o) { docTuple = o; }
 
265
        
 
266
        public Object[] getEntTuple() { return entTuple; }
 
267
        public void setEntTuple( Object[] o ) { entTuple = o; }
 
268
 
 
269
        public Object[][] getDocLinTuples() { return docLinTuples; }
 
270
        public void setDocLinTuples( Object[][] o ) { docLinTuples = o; }
 
271
 
 
272
        public Object[][] getDocTaxaTuples() { return docTaxaTuples; }
 
273
        public void setDocTaxaTuples( Object[][] o ) { docTaxaTuples = o; }
 
274
 
 
275
        public Database getDatabase() { return database; }
 
276
        
 
277
        ////////////////////////////////////////////////////////////
 
278
        public void setDoc( Object[] docTuple, Object[][] docLinTuples )
 
279
                throws SQLException
 
280
        {
 
281
                this.docTuple = docTuple;
 
282
                this.docLinTuples = docLinTuples;
 
283
                dbDoc.setTuple( docTuple );
 
284
                if ( !mpBizEntByType.getById( dbDoc.getEntId() ) )
 
285
                        entTuple = null;
 
286
                else
 
287
                        entTuple = mpBizEntByType.getTuple();
 
288
                String where =
 
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() );
 
299
        }
 
300
        
 
301
        ////////////////////////////////////////////////////////////
 
302
        public boolean selectDoc( int tipoEntId, int tipoDocId, int docId )
 
303
                throws SQLException
 
304
        {
 
305
                //Object[] tuple = dbDoc.getTuple();
 
306
                dbDoc.setTipoEntId( tipoEntId );
 
307
                dbDoc.setTipoDocId( tipoDocId );
 
308
                dbDoc.setDocId( docId );
 
309
                if ( dbDoc.getById() )
 
310
                {
 
311
                        String where =
 
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 ) +
 
317
                                " = " + docId;
 
318
                        setDoc( 
 
319
                                dbDoc.getTuple(), 
 
320
                                dbDocLin.select( where, dbDocLin.getPKeyOrder() ) );
 
321
                        int entId = dbDoc.getEntId();
 
322
                        mpBizEntByType.setTipoEntId( tipoEntId );
 
323
                        mpBizEntByType.getById( entId );
 
324
                        entTuple = mpBizEntByType.getTuple();
 
325
                        return true;
 
326
                }
 
327
                return false;
 
328
        }
 
329
 
 
330
        ////////////////////////////////////////////////////////////
 
331
        public void insertDoc()
 
332
                throws SQLException
 
333
        {
 
334
                dbDoc.setTuple( docTuple );
 
335
                dbDoc.insert();
 
336
                for ( int f = 0; f < docLinTuples.length; f++ )
 
337
                {
 
338
                        dbDocLin.setTuple( docTuple );
 
339
                        dbDocLin.insert();
 
340
                }
 
341
        }
 
342
 
 
343
        ////////////////////////////////////////////////////////////
 
344
        public void deleteDoc()
 
345
                throws SQLException
 
346
        {
 
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();
 
351
                String exec =
 
352
                        "delete from " + dbDocTaxa.getTableName() + 
 
353
                        " where " + 
 
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 ) +
 
359
                        "=" + docId;
 
360
                System.out.println(
 
361
                        "<MpBizDoc.deleteDoc>\n  exec:\n" + exec );
 
362
                stmt.executeUpdate( exec );
 
363
                
 
364
                exec =
 
365
                        "delete from " + dbDocLin.getTableName() + 
 
366
                        " where " + 
 
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 ) +
 
372
                        "=" + docId;
 
373
                System.out.println(
 
374
                        "<MpBizDoc.deleteDoc>\n  exec:\n" + exec );
 
375
                stmt.executeUpdate( exec );
 
376
                
 
377
                exec =
 
378
                        "delete from " + dbDoc.getTableName() + 
 
379
                        " where " + 
 
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  ) +
 
385
                        "=" + docId;
 
386
                System.out.println(
 
387
                        "<MpBizDoc.deleteDoc>\n  exec:\n" + exec );
 
388
                stmt.executeUpdate( exec );
 
389
                
 
390
        }
 
391
        
 
392
        ////////////////////////////////////////////////////////////
 
393
        public void updateDoc()
 
394
                throws SQLException
 
395
        {
 
396
                deleteDoc();
 
397
                insertDoc();
 
398
        }
 
399
 
 
400
  ///////////////////////////////////////////////////////////
 
401
        public double computeProdLineValorTaxa( int f )
 
402
                throws MpDocException
 
403
        {
 
404
                double total = computeProdLineTotalSemTaxa( f );
 
405
                Object o = docLinTuples[ f ][ DbDocLin.TAXA_ID ];;
 
406
                if( o == null )
 
407
                        return 0.0;
 
408
                int taxaId = ((Integer) o).intValue();
 
409
                double valorTaxa = 
 
410
                        mpBizAppContext.getMpDocFactory().getValorTaxa( 
 
411
                                taxaId, DbDoc.getDataDoc( docTuple ) );
 
412
                total *= valorTaxa;
 
413
                return total;
 
414
        }
 
415
        
 
416
  ///////////////////////////////////////////////////////////
 
417
        public double computeProdLineTotalSemTaxa( int line )
 
418
                throws MpDocException
 
419
        {
 
420
                Object o = docLinTuples[ line ][ DbDocLin.QUANT ];
 
421
                if( o == null )
 
422
                        return 0.0;
 
423
                double quant = ((Double) o).doubleValue();
 
424
                o = docLinTuples[ line ][ DbDocLin.PRECO_UNI ];
 
425
                if( o == null )
 
426
                        return 0.0;
 
427
                double precoUni = ((Double) o).doubleValue();
 
428
                o = docLinTuples[ line ][ DbDocLin.DESCONTO1 ];
 
429
                if( o == null )
 
430
                        return 0.0;
 
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 );
 
436
                return total;
 
437
        }
 
438
        
 
439
  ///////////////////////////////////////////////////////////
 
440
        public double computeProdTotal( boolean finalComputation )
 
441
                throws MpDocException
 
442
        {
 
443
                double total = 0.0;
 
444
                for ( int f = 0; f < docLinTuples.length; f++ )
 
445
                {
 
446
                        Object o = docLinTuples[ f ][ DbDocLin.TAXA_ID ];
 
447
                        if( o == null )
 
448
                                o = new Integer( 0 );
 
449
                        int taxaId = ((Integer) o).intValue();
 
450
                        double tot = computeProdLineTotalSemTaxa( f );
 
451
                        double tax = computeProdLineValorTaxa( f );
 
452
                        total += tot + tax;
 
453
                        docTaxas.addTaxa( taxaId, tot, tax );
 
454
                }
 
455
                double desconto = DbDoc.getDesconto( docTuple );
 
456
                total -= total * desconto / 100.0;
 
457
                
 
458
                System.out.println( 
 
459
                        "<MpBizDoc.computeProdTotal>\n  total: " + total );
 
460
                return Tools.roundTo3( total );
 
461
        }
 
462
 
 
463
        ////////////////////////////////////////////////////////////
 
464
        private void insertProdDocLine( int lineNo, Object[] line )
 
465
                throws SQLException, MpDocException, ParseException
 
466
        {
 
467
                System.out.println(
 
468
                        "<MpBizDoc.insertDocLine>\n  lineNo: " + lineNo );
 
469
                DbFactory dbFactory = mpBizAppContext.getDbFactory();
 
470
                MpDocFactory mpDocFactory = mpBizAppContext.getMpDocFactory();
 
471
                DbTabTipoDoc dbTabTipoDoc = dbFactory.getDbTabTipoDoc();
 
472
                // item_id
 
473
                int itemId = 0;
 
474
                int docId = getDocId();
 
475
                double quant = 1.0;
 
476
                double taxaValorPercent = 0.0;
 
477
                int taxaId = 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() )
 
483
                {
 
484
                        MessageFormat messageFormat = new MessageFormat("");
 
485
                        Object[] msgArgs = { new Integer( tipoDocId ) };
 
486
                        String pattern = 
 
487
                                bundle.getString( MpBizBundle.UNKNOWN_DOC_TYPE );
 
488
                        messageFormat.applyPattern( pattern );
 
489
                        throw new MpDocException(
 
490
                                messageFormat.format( msgArgs ) );
 
491
                }
 
492
                String descricao = null;
 
493
                int docLinType = dbTabTipoDoc.getDocLinType();
 
494
                if ( docLinType == MpDocFactory.PROD_LINE )
 
495
                {
 
496
                        DbProd dbProd = dbFactory.getDbProd();
 
497
                        itemId = DbDocLin.getItemId( line );
 
498
                        dbProd.setProdId( itemId );
 
499
                        if ( !dbProd.getById() )
 
500
                        {
 
501
                                MessageFormat messageFormat = new MessageFormat("");
 
502
                                Object[] msgArgs = { new Integer( itemId ) };
 
503
                                String pattern = 
 
504
                                        bundle.getString( MpBizBundle.UNKNOWN_PROD );
 
505
                                messageFormat.applyPattern( pattern );
 
506
                                throw new MpDocException(
 
507
                                        messageFormat.format( msgArgs ) );
 
508
                        }
 
509
                        if( pUni <= 0.0 )
 
510
                                pUni = dbProd.getPvenda();
 
511
                        quant = DbDocLin.getQuant( line );
 
512
                        taxaId = DbDocLin.getTaxaId( line );
 
513
                        taxaValorPercent = 
 
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();
 
519
                }
 
520
                else 
 
521
                {
 
522
                        MessageFormat messageFormat = new MessageFormat("");
 
523
                        Object[] msgArgs = { new Integer(  dbTabTipoDoc.getDocLinType() ) };
 
524
                        String pattern = 
 
525
                                bundle.getString( MpBizBundle.BAD_LINE_TYPE );
 
526
                        messageFormat.applyPattern( pattern );
 
527
                        throw new MpDocException( messageFormat.format( msgArgs ) );
 
528
                }
 
529
                
 
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 ) );
 
538
                
 
539
                boolean qtChanged = false;
 
540
                int orgTipoDocId = DbDocLin.getOrgTipoDocId( line );
 
541
                if( orgTipoDocId != 0 )
 
542
                {
 
543
                        DbDocLin orgDocLin = mpDocFactory.getLineOrg( dbDocLin );
 
544
                        if ( orgDocLin == null )
 
545
                        {
 
546
                                throw new MpDocException( 
 
547
                                        MpBizBundle.getBundle().getString( MpBizBundle.BAD_ORG_LINE ) );
 
548
                        }
 
549
                        double qt2 = orgDocLin.getQuant();
 
550
                        if ( quant > qt2 )
 
551
                        {
 
552
                                quant = qt2;
 
553
                                qtChanged = true;
 
554
                        }
 
555
                }
 
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 );
 
562
 
 
563
                DebugFactory.printTableRow( 
 
564
                        "MpBizDoc.insertProdDocLine." + lineNo, dbDocLin );
 
565
                dbDocLin.insert();
 
566
                
 
567
                if( dbDocLin.getLineType() == MpDocFactory.PROD_LINE &&
 
568
                                dbDocLin.getOrgTipoDocId() > 0 )
 
569
                {
 
570
                        mpDocFactory.checkDocOrg( 
 
571
                                dbDocLin.getTipoEntId(), dbDocLin.getOrgTipoDocId(),
 
572
                                dbDocLin.getOrgDocId() );               
 
573
                }
 
574
        }
 
575
        
 
576
        ////////////////////////////////////////////////////////////
 
577
        public void createProdDocument()
 
578
                throws MpDocException
 
579
        {
 
580
                DbFactory dbFactory = mpBizAppContext.getDbFactory();
 
581
                MpDocFactory mpDocFactory = mpBizAppContext.getMpDocFactory();
 
582
                MpBizFactory mpBizFactory = mpBizAppContext.getMpBizFactory();
 
583
                Connection con = mpBizAppContext.getDatabase().getConnection();
 
584
                try
 
585
                {
 
586
                        int tipoEntId = getTipoEntId();
 
587
                        int tipoDocId = getTipoDocId();
 
588
                        int orgDocId = getDocId();
 
589
                        if( orgDocId < 0 )
 
590
                        {
 
591
                                deleteDoc();
 
592
                        }
 
593
                        int docId = 
 
594
                                mpDocFactory.getLastDocNumber( 
 
595
                                        dbFactory, tipoEntId, tipoDocId ) + 1;
 
596
                        DbDoc.setDocId( docTuple, docId );
 
597
                        switch( tipoDocId )
 
598
                        {
 
599
                                case MpDocFactory.FACTURA:
 
600
                                        DbDoc.setAuxData1( 
 
601
                                                docTuple, 
 
602
                                                mpDocFactory.getDataLimitePagamento( 
 
603
                                                        tipoEntId, getEntId(), getDataDoc() ) );
 
604
                                        break;
 
605
                        }
 
606
                        
 
607
                        double totalComTaxas = computeProdTotal( true );
 
608
                        DbDoc.setTotalComTaxas( docTuple, totalComTaxas );
 
609
                        DbDoc.setTotalComTaxasM0( docTuple, totalComTaxas * dbDoc.getMoedaCot() );
 
610
                        double totSemTaxas = 0.0;
 
611
                        int f;
 
612
                        for ( f = 0; f < docTaxas.getSize(); f++ )
 
613
                        {
 
614
                                // incidencia
 
615
                                totSemTaxas += docTaxas.getDocTaxa( f ).getIncidencia();
 
616
                        }
 
617
                        DbDoc.setTotalSemTaxas( docTuple, totSemTaxas );
 
618
                        DbDoc.setUserId( docTuple, mpBizAppContext.getMpBizUser().getUserId() );
 
619
                        
 
620
                        DbDoc.setTs( docTuple, new Timestamp( new java.util.Date().getTime() ) );
 
621
                        
 
622
                        //Statement stmt = null;
 
623
                        con.setAutoCommit( false );
 
624
                        
 
625
                        // test for ent_tipo
 
626
                        DbEntTipo dbEntTipo = dbFactory.getDbEntTipo();
 
627
                        dbEntTipo.setTipoEntId( tipoEntId );
 
628
                        int entId = getEntId();
 
629
                        dbEntTipo.setEntId( entId );
 
630
                        if( !dbEntTipo.getById() )
 
631
                        {
 
632
                                dbEntTipo.setTuple( dbEntTipo.getDataModel().getEmptyTuple() );
 
633
                                dbEntTipo.setTipoEntId( tipoEntId );
 
634
                                dbEntTipo.setEntId( entId );
 
635
                                dbEntTipo.insert();
 
636
                        }
 
637
                        // test for ent_fin
 
638
                        DbEntFin dbEntFin = dbFactory.getDbEntFin();
 
639
                        dbEntFin.setTipoEntId( tipoEntId );
 
640
                        dbEntFin.setEntId( entId );
 
641
                        if( !dbEntFin.getById() )
 
642
                        {
 
643
                                dbEntFin.setTuple( dbEntFin.getDataModel().getEmptyTuple() );
 
644
                                dbEntFin.setTipoEntId( tipoEntId );
 
645
                                dbEntFin.setEntId( entId );
 
646
                                dbEntFin.insert();
 
647
                        }
 
648
                        dbDoc.setTuple( docTuple );
 
649
                        dbDoc.insert();
 
650
                        for ( f = 0; f < docLinTuples.length; f++ )
 
651
                        {
 
652
                                insertProdDocLine( f + 1, docLinTuples[f] );
 
653
                        }
 
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++ )
 
660
                        {
 
661
                                MpBizDocTaxa docTaxa = docTaxas.getDocTaxa( f );
 
662
                                dbDocTaxa.setTaxaId( docTaxa.getTaxaId() );
 
663
                                dbDocTaxa.setIncidencia( docTaxa.getIncidencia() );
 
664
                                dbDocTaxa.setImposto( docTaxa.getValorTaxa() );
 
665
                                dbDocTaxa.insert();
 
666
                        }
 
667
                        
 
668
                        con.commit();
 
669
                        selectDoc( tipoEntId, tipoDocId, docId );
 
670
                        
 
671
                        Object[] tDoc = 
 
672
                                mpDocFactory.getTipoDoc( dbDoc.getTipoDocId() );
 
673
                        Object[] docProps =
 
674
                                mpDocFactory.getDocProps( 
 
675
                                        dbDoc.getTipoEntId(), dbDoc.getTipoDocId() );
 
676
                }
 
677
                catch( ParseException pse )
 
678
                {
 
679
                        System.out.println(
 
680
                                "<MpBizDoc.createDocument>\n  PARSE EXCEPTION: " + 
 
681
                                pse.getMessage() );
 
682
                        pse.printStackTrace();
 
683
                        try
 
684
                        {
 
685
                                con.rollback();
 
686
                        }
 
687
                        catch( SQLException sqle2 )
 
688
                        {
 
689
                                
 
690
                        }
 
691
                        throw new MpDocException( pse.getMessage() );
 
692
                        
 
693
                }
 
694
                catch( SQLException sqle )
 
695
                {
 
696
                        try
 
697
                        {
 
698
                                con.rollback();
 
699
                        }
 
700
                        catch( SQLException sqle2 )
 
701
                        {
 
702
                                
 
703
                        }
 
704
                        sqle.printStackTrace();
 
705
                        throw new MpDocException( 
 
706
                                sqle.getMessage(), 
 
707
                                MpDocException.SQL_ERROR,
 
708
                                sqle.getErrorCode() );
 
709
                        
 
710
                }
 
711
                catch( MpDocException e )
 
712
                {
 
713
                        e.printStackTrace();
 
714
                        try
 
715
                        {
 
716
                                con.rollback();
 
717
                        }
 
718
                        catch( SQLException sqle2 )
 
719
                        {
 
720
                                
 
721
                        }
 
722
                        throw e;
 
723
                        
 
724
                }
 
725
        }
 
726
        
 
727
  ///////////////////////////////////////////////////////////
 
728
        public double computeDocTotal( boolean finalComputation )
 
729
        {
 
730
                double total = 0.0;
 
731
                for ( int f = 0; f < docLinTuples.length; f++ )
 
732
                {
 
733
                        int tipoDocId = getTipoDocId();
 
734
                        double totAPagar = DbDocLin.getTotalSemTaxa( docLinTuples[ f ] );
 
735
                        if( tipoDocId != MpDocFactory.NOTA_CREDITO )
 
736
                                total += totAPagar;
 
737
                        else
 
738
                                total -= totAPagar;
 
739
                }
 
740
                System.out.println( "<MpBizDoc.computeDocTotal>\n  total: " + total );
 
741
                return total;
 
742
        }
 
743
        
 
744
        ////////////////////////////////////////////////////////////
 
745
        private void insertDocDocLine( int lineNo, Object[] line )
 
746
                throws SQLException, MpDocException, ParseException
 
747
        {
 
748
                DbFactory dbFactory = mpBizAppContext.getDbFactory();
 
749
                MpDocFactory mpDocFactory = mpBizAppContext.getMpDocFactory();
 
750
                DbTabTipoDoc dbTabTipoDoc = dbFactory.getDbTabTipoDoc();
 
751
                // item_id
 
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 )
 
758
                {
 
759
                        if( docId == 0 )
 
760
                                tipoDocId = 0;
 
761
                }
 
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();
 
770
                if( docId > 0 )
 
771
                {
 
772
                        mpDocFactory.applyRulesToLine( dbDoc, dbDocLin );
 
773
                        mpDocFactory.checkDocOrg( 
 
774
                                dbDocLin.getTipoEntId(), dbDocLin.getOrgTipoDocId(), docId );
 
775
                }
 
776
                DebugFactory.printTableRow( 
 
777
                        "MpBizDoc.insertDocLine", dbDocLin );
 
778
                dbDocLin.insert();
 
779
        }
 
780
        
 
781
        ////////////////////////////////////////////////////////////
 
782
        public void createDocDocument()
 
783
                throws MpDocException
 
784
        {
 
785
                DbFactory dbFactory = mpBizAppContext.getDbFactory();
 
786
                MpDocFactory mpDocFactory = mpBizAppContext.getMpDocFactory();
 
787
                MpBizFactory mpBizFactory = mpBizAppContext.getMpBizFactory();
 
788
                Connection con = mpBizAppContext.getDatabase().getConnection();
 
789
                try
 
790
                {
 
791
                        int tipoEntId = getTipoEntId();
 
792
                        int tipoDocId = getTipoDocId();
 
793
                        int docId = 
 
794
                                mpDocFactory.getLastDocNumber( 
 
795
                                        dbFactory, tipoEntId, tipoDocId ) + 1;
 
796
                        DbDoc.setDocId( docTuple, docId );
 
797
                        DbDoc.setUserId( docTuple, mpBizAppContext.getMpBizUser().getUserId() );
 
798
                        
 
799
                        double totalComTaxas = computeDocTotal( true );
 
800
                        DbDoc.setTotalComTaxas( docTuple, totalComTaxas );
 
801
                        double moedaCot = DbDoc.getMoedaCot( docTuple );
 
802
                        DbDoc.setTotalComTaxasM0( docTuple, totalComTaxas * moedaCot );
 
803
                        
 
804
                        DbDoc.setUserId( docTuple, mpBizAppContext.getMpBizUser().getUserId() );
 
805
                        DbDoc.setEstadoId( docTuple, 0 );
 
806
                        if( tipoDocId == MpDocFactory.RECIBO )
 
807
                                DbDoc.setEstadoFinal( docTuple, true );
 
808
                        
 
809
                        DbDoc.setTs( docTuple, new Timestamp( new java.util.Date().getTime() ) );
 
810
                        dbDoc.setTuple( docTuple );
 
811
                        con.setAutoCommit( false );
 
812
                        dbDoc.insert();
 
813
                        for ( int f = 0; f < docLinTuples.length; f++ )
 
814
                        {
 
815
                                insertDocDocLine( f + 1, docLinTuples[f] );
 
816
                        }
 
817
 
 
818
                        con.commit();
 
819
                }
 
820
                catch( ParseException pse )
 
821
                {
 
822
                        System.out.println(
 
823
                                "<MpDocFromDocFrame.createDocument>\n  PARSE EXCEPTION: " + 
 
824
                                pse.getMessage() );
 
825
                        pse.printStackTrace();
 
826
                        try
 
827
                        {
 
828
                                con.rollback();
 
829
                        }
 
830
                        catch( SQLException sqle2 )
 
831
                        {
 
832
                                
 
833
                        }
 
834
                        throw new MpDocException( pse.getMessage() );
 
835
                        
 
836
                }
 
837
                catch( SQLException sqle )
 
838
                {
 
839
                        try
 
840
                        {
 
841
                                con.rollback();
 
842
                        }
 
843
                        catch( SQLException sqle2 )
 
844
                        {
 
845
                                
 
846
                        }
 
847
                        sqle.printStackTrace();
 
848
                        throw new MpDocException( 
 
849
                                sqle.getMessage(), 
 
850
                                MpDocException.SQL_ERROR,
 
851
                                sqle.getErrorCode() );
 
852
                        
 
853
                }
 
854
                catch( MpDocException e )
 
855
                {
 
856
                        e.printStackTrace();
 
857
                        try
 
858
                        {
 
859
                                con.rollback();
 
860
                        }
 
861
                        catch( SQLException sqle2 )
 
862
                        {
 
863
                                
 
864
                        }
 
865
                        throw e;
 
866
                        
 
867
                }
 
868
        }
 
869
        
 
870
        ////////////////////////////////////////////////////////////
 
871
        public void createDocument()
 
872
                throws MpDocException
 
873
        {
 
874
                DbFactory dbFactory = mpBizAppContext.getDbFactory();
 
875
                MpDocFactory mpDocFactory = mpBizAppContext.getMpDocFactory();
 
876
                MpBizFactory mpBizFactory = mpBizAppContext.getMpBizFactory();
 
877
                Connection con = mpBizAppContext.getDatabase().getConnection();
 
878
                try
 
879
                {
 
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() )
 
886
                        {
 
887
                                int docLinType = dbTabTipoDoc.getDocLinType();
 
888
                                if( docLinType == MpDocFactory.PROD_LINE )
 
889
                                {
 
890
                                        createProdDocument();
 
891
                                }
 
892
                                else if( docLinType == MpDocFactory.DOC_LINE )
 
893
                                {
 
894
                                        createDocDocument();
 
895
                                }
 
896
                                else
 
897
                                {
 
898
                                        MessageFormat messageFormat = new MessageFormat("");
 
899
                                        Object[] msgArgs = { new Integer(  docLinType ) };
 
900
                                        String pattern = 
 
901
                                                bundle.getString( MpBizBundle.BAD_LINE_TYPE );
 
902
                                        messageFormat.applyPattern( pattern );
 
903
                                        throw new MpDocException( messageFormat.format( msgArgs ) );
 
904
                                }
 
905
                        }
 
906
                }
 
907
                catch( SQLException sqle )
 
908
                {
 
909
                        try { con.rollback(); }
 
910
                        catch( SQLException sqle2 ) {}
 
911
                        
 
912
                        sqle.printStackTrace();
 
913
                        throw new MpDocException( 
 
914
                                sqle.getMessage(), 
 
915
                                MpDocException.SQL_ERROR,
 
916
                                sqle.getErrorCode() );
 
917
                        
 
918
                }
 
919
        }
 
920
        
 
921
        ////////////////////////////////////////////////////////////
 
922
        private void insertTmpDocDocLine( int lineNo, Object[] line )
 
923
                throws SQLException, MpDocException
 
924
        {
 
925
                DbFactory dbFactory = mpBizAppContext.getDbFactory();
 
926
                dbDocLin.setTuple( line );
 
927
                dbDocLin.setDocLinId( lineNo );
 
928
                dbDocLin.insert();
 
929
        }
 
930
        
 
931
        ////////////////////////////////////////////////////////////
 
932
        public int saveDocDocument() 
 
933
                throws MpDocException
 
934
        {
 
935
                int docId = 0;
 
936
                DbFactory dbFactory = mpBizAppContext.getDbFactory();
 
937
                MpDocFactory mpDocFactory = mpBizAppContext.getMpDocFactory();
 
938
                MpBizFactory mpBizFactory = mpBizAppContext.getMpBizFactory();
 
939
                Connection con = mpBizAppContext.getDatabase().getConnection();
 
940
                try
 
941
                {
 
942
                        int tipoEntId = getTipoEntId();
 
943
                        int tipoDocId = getTipoDocId();
 
944
                        int orgDocId = getDocId();
 
945
                        if( orgDocId < 0 )
 
946
                        {
 
947
                                setDocId( orgDocId );
 
948
                                deleteDoc();
 
949
                        }
 
950
                        docId = orgDocId;
 
951
                        if( docId >= 0 )
 
952
                        {
 
953
                                docId =
 
954
                                        mpDocFactory.getLastTmpDocNumber( 
 
955
                                                dbFactory, tipoEntId, tipoDocId ) - 1;
 
956
                        }
 
957
                        double totalComTaxas = 0.0;
 
958
                        DbTabTipoDoc dbTabTipoDoc = dbFactory.getDbTabTipoDoc();
 
959
                        dbTabTipoDoc.setTipoDocId( getTipoDocId() );
 
960
                        int docLinType = 0;
 
961
                        if( dbTabTipoDoc.getById() )
 
962
                        {
 
963
                                docLinType = dbTabTipoDoc.getDocLinType();
 
964
                                if( docLinType == MpDocFactory.PROD_LINE )
 
965
                                {
 
966
                                        totalComTaxas = computeProdTotal( true );
 
967
                                }
 
968
                                else if( docLinType == MpDocFactory.DOC_LINE )
 
969
                                {
 
970
                                        totalComTaxas = computeProdTotal( true );
 
971
                                }
 
972
                                else
 
973
                                {
 
974
                                        MessageFormat messageFormat = new MessageFormat("");
 
975
                                        Object[] msgArgs = { new Integer(  docLinType ) };
 
976
                                        String pattern = 
 
977
                                                bundle.getString( MpBizBundle.BAD_LINE_TYPE );
 
978
                                        messageFormat.applyPattern( pattern );
 
979
                                        throw new MpDocException( messageFormat.format( msgArgs ) );
 
980
                                }
 
981
                        }
 
982
                        else
 
983
                        {
 
984
                                MessageFormat messageFormat = new MessageFormat("");
 
985
                                Object[] msgArgs = { new Integer(  docLinType ) };
 
986
                                String pattern = 
 
987
                                        bundle.getString( MpBizBundle.BAD_LINE_TYPE );
 
988
                                messageFormat.applyPattern( pattern );
 
989
                                throw new MpDocException( messageFormat.format( msgArgs ) );
 
990
                        }
 
991
 
 
992
                        DbDoc.setDocId( docTuple, docId );
 
993
                        DbDoc.setUserId( docTuple, mpBizAppContext.getMpBizUser().getUserId() );
 
994
                        
 
995
                        DbDoc.setTotalComTaxas( docTuple, totalComTaxas );
 
996
                        DbDoc.setTotalSemTaxas( docTuple, 0.0 );
 
997
                        DbDoc.setUserId( docTuple, mpBizAppContext.getMpBizUser().getUserId() );
 
998
                        DbDoc.setEstadoId( docTuple, 0 );
 
999
                        
 
1000
                        DbDoc.setTs( docTuple, new Timestamp( new java.util.Date().getTime() ) );
 
1001
                        
 
1002
                        con.setAutoCommit( false );
 
1003
                        
 
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() )
 
1010
                                dbEntTipo.insert();
 
1011
                        
 
1012
                        // test for ent_fin
 
1013
                        DbEntFin dbEntFin = dbFactory.getDbEntFin();
 
1014
                        dbEntFin.setTipoEntId( tipoEntId );
 
1015
                        dbEntFin.setEntId( entId );
 
1016
                        if( !dbEntFin.getById() )
 
1017
                                dbEntFin.insert();
 
1018
                        dbDoc.setTuple( docTuple );
 
1019
                        dbDoc.insert();
 
1020
                        for( int f = 0; f < docLinTuples.length; f++ )
 
1021
                        {
 
1022
                                insertTmpDocDocLine( f + 1, docLinTuples[f] );
 
1023
                        }
 
1024
                        con.commit();
 
1025
                }
 
1026
                catch( SQLException sqle )
 
1027
                {
 
1028
                        try
 
1029
                        {
 
1030
                                con.rollback();
 
1031
                        }
 
1032
                        catch( SQLException sqle2 )
 
1033
                        {
 
1034
                                
 
1035
                        }
 
1036
                        sqle.printStackTrace();
 
1037
                        throw new MpDocException( 
 
1038
                                sqle.getMessage(), 
 
1039
                                MpDocException.SQL_ERROR,
 
1040
                                sqle.getErrorCode() );
 
1041
                        
 
1042
                }
 
1043
                catch( MpDocException e )
 
1044
                {
 
1045
                        e.printStackTrace();
 
1046
                        try
 
1047
                        {
 
1048
                                con.rollback();
 
1049
                        }
 
1050
                        catch( SQLException sqle2 )
 
1051
                        {
 
1052
                                
 
1053
                        }
 
1054
                        throw e;
 
1055
                }
 
1056
                return docId;
 
1057
        }
 
1058
        
 
1059
        ////////////////////////////////////////////////////////////
 
1060
        private void insertTmpProdDocLine( int lineNo, Object[] line )
 
1061
                throws SQLException, MpDocException
 
1062
        {
 
1063
                DbFactory dbFactory = mpBizAppContext.getDbFactory();
 
1064
                MpDocFactory mpDocFactory = mpBizAppContext.getMpDocFactory();
 
1065
                // item_id
 
1066
                int itemId = 0;
 
1067
                int auxItemId = 1;
 
1068
                int auxItem2Id = 0;
 
1069
                double quant = 1.0;
 
1070
                double taxaValorPercent = 0.0;
 
1071
                int taxaId = 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() )
 
1078
                {
 
1079
                        MessageFormat messageFormat = new MessageFormat("");
 
1080
                        Object[] msgArgs = { new Integer(  itemId ) };
 
1081
                        String pattern = 
 
1082
                                bundle.getString( MpBizBundle.UNKNOWN_PROD );
 
1083
                        messageFormat.applyPattern( pattern );
 
1084
                        throw new MpDocException( messageFormat.format( msgArgs ) );
 
1085
                }
 
1086
 
 
1087
                quant = DbDocLin.getQuant( line );
 
1088
                taxaId = DbDocLin.getTaxaId( line );
 
1089
                taxaValorPercent = 
 
1090
                        mpDocFactory.getValorTaxa( taxaId, getDataDoc() );
 
1091
                DbProdArm dbProdArm = dbFactory.getDbProdArm();
 
1092
                dbProdArm.setArmId( auxItemId );
 
1093
                dbProdArm.setProdId( itemId );
 
1094
                if( !dbProdArm.getById() )
 
1095
                {
 
1096
                        dbProdArm.insert();
 
1097
                }
 
1098
                // test for empty description
 
1099
                descricao = DbDocLin.getDescricao( line ).trim();
 
1100
                if( descricao.length() == 0 )
 
1101
                {
 
1102
                        descricao = dbProd.getNome();
 
1103
                }
 
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 = 
 
1111
                        Tools.calcPercent( 
 
1112
                                quant * dbDocLin.getPrecoUni(), dbDocLin.getDesconto1() );
 
1113
                DbDocLin.setTotalSemTaxa( line, totalSemTaxa );
 
1114
                DbDocLin.setValorTaxa( line, computeProdLineValorTaxa( lineNo - 1 ) );
 
1115
                dbDocLin.setTuple( line );
 
1116
                dbDocLin.insert();
 
1117
        }
 
1118
        
 
1119
        ////////////////////////////////////////////////////////////
 
1120
        public int saveProdDocument()
 
1121
                throws MpDocException
 
1122
        {
 
1123
                int docId = 0;
 
1124
                DbFactory dbFactory = mpBizAppContext.getDbFactory();
 
1125
                MpDocFactory mpDocFactory = mpBizAppContext.getMpDocFactory();
 
1126
                MpBizFactory mpBizFactory = mpBizAppContext.getMpBizFactory();
 
1127
                Connection con = mpBizAppContext.getDatabase().getConnection();
 
1128
                try
 
1129
                {
 
1130
                        int tipoEntId = getTipoEntId();
 
1131
                        int tipoDocId = getTipoDocId();
 
1132
                        int orgDocId = getDocId();
 
1133
                        if( orgDocId < 0 )
 
1134
                        {
 
1135
                                setDocId( orgDocId );
 
1136
                                deleteDoc();
 
1137
                        }
 
1138
                        docId = orgDocId;
 
1139
                        if( docId >= 0 )
 
1140
                        {
 
1141
                                docId =
 
1142
                                        mpDocFactory.getLastTmpDocNumber( 
 
1143
                                                dbFactory, tipoEntId, tipoDocId ) - 1;
 
1144
                        }
 
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;
 
1150
                        int f;
 
1151
                        for ( f = 0; f < docTaxas.getSize(); f++ )
 
1152
                        {
 
1153
                                // incidencia
 
1154
                                totSemTaxas += docTaxas.getDocTaxa( f ).getIncidencia();
 
1155
                        }
 
1156
                        DbDoc.setTotalSemTaxas( docTuple, totSemTaxas );
 
1157
                        DbDoc.setUserId( docTuple, mpBizAppContext.getMpBizUser().getUserId() );
 
1158
                        DbDoc.setEstadoId( docTuple, 0 );
 
1159
                        
 
1160
                        DbDoc.setTs( docTuple, new Timestamp( new java.util.Date().getTime() ) );
 
1161
                        
 
1162
                        con.setAutoCommit( false );
 
1163
                        
 
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() )
 
1170
                        {
 
1171
                                dbEntTipo.setTuple( dbEntTipo.getDataModel().getEmptyTuple() );
 
1172
                                dbEntTipo.setTipoEntId( tipoEntId );
 
1173
                                dbEntTipo.setEntId( entId );
 
1174
                                dbEntTipo.insert();
 
1175
                        }
 
1176
                        // test for ent_fin
 
1177
                        DbEntFin dbEntFin = dbFactory.getDbEntFin();
 
1178
                        dbEntFin.setTipoEntId( tipoEntId );
 
1179
                        dbEntFin.setEntId( entId );
 
1180
                        if( !dbEntFin.getById() )
 
1181
                        {
 
1182
                                dbEntFin.setTuple( dbEntFin.getDataModel().getEmptyTuple() );
 
1183
                                dbEntFin.setTipoEntId( tipoEntId );
 
1184
                                dbEntFin.setEntId( entId );
 
1185
                                dbEntFin.insert();
 
1186
                        }
 
1187
                        dbDoc.setTuple( docTuple );
 
1188
                        dbDoc.insert();
 
1189
                        for ( f = 0; f < docLinTuples.length; f++ )
 
1190
                        {
 
1191
                                insertTmpProdDocLine( f + 1, docLinTuples[f] );
 
1192
                        }
 
1193
                        con.commit();
 
1194
                }
 
1195
                catch( SQLException sqle )
 
1196
                {
 
1197
                        try
 
1198
                        {
 
1199
                                con.rollback();
 
1200
                        }
 
1201
                        catch( SQLException sqle2 )
 
1202
                        {
 
1203
                                
 
1204
                        }
 
1205
                        sqle.printStackTrace();
 
1206
                        throw new MpDocException( 
 
1207
                                sqle.getMessage(), 
 
1208
                                MpDocException.SQL_ERROR,
 
1209
                                sqle.getErrorCode() );
 
1210
                        
 
1211
                }
 
1212
                catch( MpDocException e )
 
1213
                {
 
1214
                        e.printStackTrace();
 
1215
                        try
 
1216
                        {
 
1217
                                con.rollback();
 
1218
                        }
 
1219
                        catch( SQLException sqle2 )
 
1220
                        {
 
1221
                                
 
1222
                        }
 
1223
                        throw e;
 
1224
                        
 
1225
                }
 
1226
                return docId;
 
1227
        }
 
1228
        
 
1229
        ////////////////////////////////////////////////////////////
 
1230
        public void saveDocument()
 
1231
                throws MpDocException
 
1232
        {
 
1233
                DbFactory dbFactory = mpBizAppContext.getDbFactory();
 
1234
                MpDocFactory mpDocFactory = mpBizAppContext.getMpDocFactory();
 
1235
                MpBizFactory mpBizFactory = mpBizAppContext.getMpBizFactory();
 
1236
                Connection con = mpBizAppContext.getDatabase().getConnection();
 
1237
                try
 
1238
                {
 
1239
                        int tipoDocId = getTipoDocId();
 
1240
                        DbTabTipoDoc dbTabTipoDoc = dbFactory.getDbTabTipoDoc();
 
1241
                        dbTabTipoDoc.setTipoDocId( getTipoDocId() );
 
1242
                        if( dbTabTipoDoc.getById() )
 
1243
                        {
 
1244
                                int docLinType = dbTabTipoDoc.getDocLinType();
 
1245
                                if( docLinType == MpDocFactory.PROD_LINE )
 
1246
                                {
 
1247
                                        saveProdDocument();
 
1248
                                }
 
1249
                                else if( docLinType == MpDocFactory.DOC_LINE )
 
1250
                                {
 
1251
                                        saveDocDocument();
 
1252
                                }
 
1253
                                else
 
1254
                                {
 
1255
                                        MessageFormat messageFormat = new MessageFormat("");
 
1256
                                        Object[] msgArgs = { new Integer(  docLinType ) };
 
1257
                                        String pattern = 
 
1258
                                                bundle.getString( MpBizBundle.BAD_LINE_TYPE );
 
1259
                                        messageFormat.applyPattern( pattern );
 
1260
                                        throw new MpDocException( messageFormat.format( msgArgs ) );
 
1261
                                }
 
1262
                        }
 
1263
                }
 
1264
                catch( SQLException sqle )
 
1265
                {
 
1266
                        try { con.rollback(); }
 
1267
                        catch( SQLException sqle2 ) {}
 
1268
                        
 
1269
                        sqle.printStackTrace();
 
1270
                        throw new MpDocException( 
 
1271
                                sqle.getMessage(), 
 
1272
                                MpDocException.SQL_ERROR,
 
1273
                                sqle.getErrorCode() );
 
1274
                        
 
1275
                }
 
1276
        }
 
1277
        
 
1278
        ////////////////////////////////////////////////////////////
 
1279
        public void validateDocument()
 
1280
                throws MpDocException
 
1281
        {
 
1282
                DbFactory dbFactory = mpBizAppContext.getDbFactory();
 
1283
                dbDoc.setTuple( docTuple );
 
1284
                try
 
1285
                {
 
1286
                        MpBizEntByType mpBizEntByType = getBizEnt();
 
1287
                        if ( !mpBizEntByType.getById() )
 
1288
                        {
 
1289
                                MessageFormat messageFormat = new MessageFormat("");
 
1290
                                Object[] msgArgs = { new Integer(  getEntId() ) };
 
1291
                                String pattern = 
 
1292
                                        bundle.getString( MpBizBundle.UNKNOWN_ENT );
 
1293
                                messageFormat.applyPattern( pattern );
 
1294
                                throw new MpDocException( messageFormat.format( msgArgs ) );
 
1295
                        }
 
1296
                        for ( int f = 0; f < docLinTuples.length; f++ )
 
1297
                        {
 
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 )
 
1303
                                {
 
1304
                                        MessageFormat messageFormat = new MessageFormat("");
 
1305
                                        Object[] msgArgs = 
 
1306
                                                { DbDoc.FIELDS[ DbDoc.DOC_ID ],
 
1307
                                                        new Integer(  docId ),
 
1308
                                                        new Integer(  docLinId ) };
 
1309
                                        String pattern = 
 
1310
                                                bundle.getString( MpBizBundle.DOC_KEY_MISMATCH );
 
1311
                                        messageFormat.applyPattern( pattern );
 
1312
                                        throw new MpDocException( messageFormat.format( msgArgs ) );
 
1313
                                }
 
1314
                                if( tipoDocId != tipoDocLinId )
 
1315
                                {
 
1316
                                        MessageFormat messageFormat = new MessageFormat("");
 
1317
                                        Object[] msgArgs = 
 
1318
                                                { DbDoc.FIELDS[ DbDoc.TIPO_DOC_ID ],
 
1319
                                                        new Integer(  tipoDocId ),
 
1320
                                                        new Integer(  tipoDocLinId ) };
 
1321
                                        String pattern = 
 
1322
                                                bundle.getString( MpBizBundle.DOC_KEY_MISMATCH );
 
1323
                                        messageFormat.applyPattern( pattern );
 
1324
                                        throw new MpDocException( messageFormat.format( msgArgs ) );
 
1325
                                }
 
1326
                        }
 
1327
                }
 
1328
                catch( SQLException sqle )
 
1329
                {
 
1330
                        System.out.println( 
 
1331
                                "<MpDocFromDocFrame.validateDocument>\n  " + sqle.getMessage() );
 
1332
                        sqle.printStackTrace();
 
1333
                        throw new MpDocException( 
 
1334
                                sqle.getMessage(), 
 
1335
                                MpDocException.SQL_ERROR,
 
1336
                                sqle.getErrorCode() );
 
1337
                }
 
1338
        }
 
1339
        
 
1340
 
 
1341
}