Exemple #1
0
int ArchiveMan::archivePos( int archDocId, KraftDoc *doc )
{
    /*
      mysql> describe archdocpos;
      +-----------+--------------+------+-----+---------+----------------+
      | Field     | Type         | Null | Key | Default | Extra          |
      +-----------+--------------+------+-----+---------+----------------+
      | archPosID | int(11)      | NO   | PRI | NULL    | auto_increment |
      | archDocID | int(11)      | NO   | MUL |         |                |
      | ordNumber | int(11)      | NO   |     |         |                |
      | text      | text         | YES  |     | NULL    |                |
      | amount    | decimal(6,2) | YES  |     | NULL    |                |
      | unit      | varchar(64)  | YES  |     | NULL    |                |
      | price     | decimal(6,2) | YES  |     | NULL    |                |
      | vat       | decimal(3,1) | YES  |     | 0.0     |                |
      +-----------+--------------+------+-----+---------+----------------+
    */
    if( ! doc ) return -1;

    QSqlTableModel model;
    model.setTable("archdocpos");
    QSqlRecord record = model.record();

    int cnt = 0;

    DocPositionList posList = doc->positions();
    DocPositionListIterator it( posList );

    kDebug() << "Archiving pos for " << archDocId << endl;
    while ( it.hasNext() ) {
        DocPosition *dp = static_cast<DocPosition*>( it.next() );

        record.setValue( "archDocID", archDocId );
        record.setValue( "ordNumber", 1+cnt /* dp->position() */ );
        record.setValue( "kind", dp->attribute( DocPosition::Kind ) );
        record.setValue( "text", dp->text() ); // expandItemText( dp ) );
        record.setValue( "amount", dp->amount() );
        record.setValue( "unit", dp->unit().einheit( dp->amount() ) );
        record.setValue( "price", dp->unitPrice().toDouble() );
        record.setValue( "overallPrice", dp->overallPrice().toDouble() );
        record.setValue( "taxType", dp->taxTypeNumeric() );

        if(!model.insertRecord(-1, record)) {
            kDebug() << model.lastError();
        }
        dbID id = KraftDB::self()->getLastInsertID();
        // kDebug() << "Inserted for id " << id.toString() << endl;
        cnt++;

        // save the attributes of the positions in the attributes
        // table but with a new host type which reflects the arch state
        AttributeMap attribs = dp->attributes();
        attribs.setHost( "ArchPosition" );
        attribs.save( id );
    }
    return cnt;
}
Exemple #2
0
void KraftViewRO::setup( DocGuardedPtr doc )
{
    KraftViewBase::setup( doc );

    if ( !doc ) return;

    KLocale *locale = doc->locale();
    if ( !locale ) locale = KGlobal::locale();

    // do stuff like open a template and render values into it.
    KStandardDirs stdDirs;
    QString templFileName = QString( "kraftdoc_ro.trml" );
    QString findFile = "kraft/reports/" + templFileName;

    QString tmplFile = stdDirs.findResource( "data", findFile );


    QByteArray kraftHome = qgetenv("KRAFT_HOME");

    if( !kraftHome.isEmpty() ) {
        QString file = QString( "%1/reports/kraftdoc_ro.trml").arg(QString::fromLocal8Bit(kraftHome));
        QFileInfo fi(file);
        if( fi.exists() && fi.isReadable() ) {
            tmplFile = file;
        }
    }
    if( tmplFile.isEmpty() ) {
        kDebug() << "Could not find template to render ro view of document.";
        return;
    }


    TextTemplate tmpl( tmplFile );
    if( !tmpl.open() ) {
        return;
    }
    tmpl.setValue( DOC_RO_TAG( "HEADLINE" ), doc->docType() + " " + doc->ident() );
    tmpl.setValue( DOC_RO_TAG( "DATE" ), locale->formatDate( doc->date(), KLocale::ShortDate ) );
    tmpl.setValue( DOC_RO_TAG( "DOC_TYPE" ),  doc->docType() );
    QString address = doc->address();
    address.replace( '\n', "<br/>" );
    tmpl.setValue( DOC_RO_TAG( "ADDRESS" ), address );
    tmpl.setValue( DOC_RO_TAG( "DOCNO" ), doc->ident() );
    tmpl.setValue( DOC_RO_TAG( "PRETEXT" ), doc->preText() );
    tmpl.setValue( DOC_RO_TAG( "POSTTEXT" ), doc->postText() );
    tmpl.setValue( DOC_RO_TAG( "SALUT" ), doc->salut() );
    tmpl.setValue( DOC_RO_TAG( "GOODBYE" ), doc->goodbye() );


    DocPositionList positions = doc->positions();

    // check the tax settings: If all items have the same settings, its not individual.
    bool individualTax = false;
    int ttype = -1;
    foreach( DocPositionBase *dp, positions  ) {
        if( ttype == -1 ) {
            ttype = dp->taxType();
        } else {
            if( ttype != dp->taxType() ) { // different from previous one?
                individualTax = true;
                break;
            }
        }
    }

    int pos = 1;
    int taxFreeCnt = 0;
    int reducedTaxCnt = 0;
    int fullTaxCnt = 0;

    QString docType = doc->docType();
    DocType dt(docType);

    foreach( DocPositionBase *dpb, positions ) {
        DocPosition *dp = static_cast<DocPosition*>(dpb);
        tmpl.createDictionary( "ITEMS" );

        tmpl.setValue( "ITEMS", "NUMBER", QString::number( pos++ ) );
        tmpl.setValue( "ITEMS", "TEXT", dp->text() );
        tmpl.setValue( "ITEMS", "AMOUNT", locale->formatNumber( dp->amount() ) );
        tmpl.setValue( "ITEMS", "UNIT", dp->unit().einheit( dp->amount() ) );
        double singlePrice = dp->unitPrice().toDouble();

        if( dt.pricesVisible() ) {
            tmpl.createSubDictionary("ITEMS", "PRICE_DISPLAY");
            tmpl.setValue( "PRICE_DISPLAY", "SINGLE_PRICE", locale->formatMoney( singlePrice ) );
            QString style( "positive" );
            if ( singlePrice < 0 ) {
                style = "negative";
            }

            tmpl.setValue( "PRICE_DISPLAY", "PRICE_STYLE", style );

            tmpl.setValue( "PRICE_DISPLAY", "PRICE", locale->formatMoney( dp->overallPrice().toDouble() ) );
        }
#if 0
        QString taxType;
        if( individualTax ) {
            if( dp->taxType() == 1 ) {
                taxFreeCnt++;
                taxType = "TAX_FREE";
            } else if( dp->taxType() == 2 ) {
                taxType = "REDUCED_TAX";
                reducedTaxCnt++;
            } else {
                // ATTENTION: Default for all non known tax types is full tax.
                fullTaxCnt++;
                taxType = "FULL_TAX";
            }
        }
#endif
    }