/***************************************************************************
 *   Copyright (C) 2008 by S. MANKOWSKI / G. DE BURE support@mankowski.fr  *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>  *
 ***************************************************************************/
/** @file
 * This file implements classes SKGSubOperationObject.
 *
 * @author Stephane MANKOWSKI / Guillaume DE BURE
 */
#include "skgsuboperationobject.h"

#include <KLocale>

#include "skgoperationobject.h"
#include "skgcategoryobject.h"
#include "skgtrackerobject.h"
#include "skgservices.h"
#include "skgdocument.h"

SKGSubOperationObject::SKGSubOperationObject(SKGDocument* iDocument, int iID): SKGObjectBase(iDocument, "v_suboperation", iID)
{
}

SKGSubOperationObject::~SKGSubOperationObject()
{
}

SKGSubOperationObject::SKGSubOperationObject(const SKGSubOperationObject& iObject)
    : SKGObjectBase(iObject)
{
}

SKGSubOperationObject::SKGSubOperationObject(const SKGObjectBase& iObject)
{
    if (iObject.getRealTable() == "suboperation") {
        copyFrom(iObject);
    } else {
        *this = SKGObjectBase(iObject.getDocument(), "v_suboperation", iObject.getID());
    }
}

const SKGSubOperationObject& SKGSubOperationObject::operator= (const SKGObjectBase& iObject)
{
    copyFrom(iObject);
    return *this;
}

SKGError SKGSubOperationObject::setDate(const QDate& iDate)
{
    return setAttribute("d_date", iDate.isValid() ? SKGServices::dateToSqlString(QDateTime(iDate)) : "");
}

QDate SKGSubOperationObject::getDate() const
{
    return SKGServices::stringToTime(getAttribute("d_date")).date();
}

SKGError SKGSubOperationObject::setOrder(int iOrder)
{
    return setAttribute("i_order", SKGServices::intToString(iOrder));
}

int SKGSubOperationObject::getOrder() const
{
    return SKGServices::stringToInt(getAttribute("i_order"));
}

SKGError SKGSubOperationObject::setComment(const QString& iComment)
{
    return setAttribute("t_comment", iComment);
}

QString SKGSubOperationObject::getComment() const
{
    return getAttribute("t_comment");
}

QString SKGSubOperationObject::getFormula() const
{
    return getAttribute("t_formula");
}

SKGError SKGSubOperationObject::setFormula(const QString& iFormula)
{
    return setAttribute("t_formula", iFormula);
}

SKGError SKGSubOperationObject::getParentOperation(SKGOperationObject& oOperation) const
{
    SKGError err = getDocument()->getObject("v_operation", "id=" % getAttribute("rd_operation_id"), oOperation);
    return err;
}

SKGError SKGSubOperationObject::setParentOperation(const SKGOperationObject& iOperation)
{
    SKGError err;
    if (!getDate().isValid()) {
        err = setDate(iOperation.getDate());
    }
    IFOKDO(err, setAttribute("rd_operation_id", SKGServices::intToString(iOperation.getID())))
    return err;
}


SKGError SKGSubOperationObject::getCategory(SKGCategoryObject& oCategory) const
{
    SKGError err = getDocument()->getObject("v_category", "id=" % getAttribute("r_category_id"), oCategory);
    return err;
}

SKGError SKGSubOperationObject::setCategory(const SKGCategoryObject& iCategory)
{
    return setAttribute("r_category_id", SKGServices::intToString(iCategory.getID()));
}

SKGError SKGSubOperationObject::setQuantity(double iValue)
{
    return setAttribute("f_value", SKGServices::doubleToString(iValue));
}

double SKGSubOperationObject::getQuantity() const
{
    return SKGServices::stringToDouble(getAttribute("f_value"));
}


SKGError SKGSubOperationObject::setTracker(const SKGTrackerObject& iTracker, bool iForce)
{
    SKGError err;
    SKGTrackerObject previous;
    getTracker(previous);
    if (iTracker != previous) {
        if (!iForce && previous.isClosed()) {
            err = SKGError(ERR_FAIL, i18nc("Error message", "Impossible to remove an operation from a closed tracker"));
        } else if (!iForce && iTracker.isClosed()) {
            err = SKGError(ERR_FAIL, i18nc("Error message", "Impossible to add an operation in a closed tracker"));
        } else {
            err = setAttribute("r_refund_id", SKGServices::intToString(iTracker.getID()));
        }
    }
    return err;
}

SKGError SKGSubOperationObject::getTracker(SKGTrackerObject& oTracker) const
{
    QString idS = getAttribute("r_refund_id");
    if (idS.isEmpty()) {
        idS = '0';
    }
    SKGError err;
    if (getDocument() && idS != "0") {
        err = getDocument()->getObject("v_refund", "id=" % idS, oTracker);
    }
    return err;
}

#include "skgsuboperationobject.moc"
