#include "enterprisedao.h"
#include "../mapplication.h"

bool EnterpriseDAO::create(const Data& data)
{
    dApp->Enterprise().open();
    QSqlQuery qry(dApp->Enterprise());
    qry.prepare("INSERT INTO ENTERPRISES (NAME, CIF, DBNAME, PATH) "
                "VALUES (:NAME, :CIF, :DBNAME, :PATH)");
    qry.bindValue(":NAME", data.name);
    qry.bindValue(":CIF", data.cif);
    qry.bindValue(":DBNAME", data.name);
    qry.bindValue(":PATH", "");

    bool ok = qry.exec();
    if (!ok) qDebug() << "EnterpriseDAO::create error:" << qry.lastError().text();

    if (ok) {
        // Insert info
        qry.prepare("INSERT INTO ENTERPRISESINFO (NAME, CIF, ADDRESS, POSTCODE, CITY, PROVINCE, "
                    "COUNTRY, PHONE, MOBILE, FAX, EMAIL, WEB, CIF_COMMUNITY, IAE, CNAE, DCNAE, "
                    "LOGO1, LOGO2, CREATEDBY) "
                    "VALUES (:NAME, :CIF, :ADDRESS, :POSTCODE, :CITY, :PROVINCE, "
                    ":COUNTRY, :PHONE, :MOBILE, :FAX, :EMAIL, :WEB, :CIF_COMMUNITY, :IAE, :CNAE, :DCNAE, "
                    ":LOGO1, :LOGO2, :CREATEDBY)");
        qry.bindValue(":NAME", data.name);
        qry.bindValue(":CIF", data.cif);
        qry.bindValue(":ADDRESS", data.address);
        qry.bindValue(":POSTCODE", data.postcode);
        qry.bindValue(":CITY", data.city);
        qry.bindValue(":PROVINCE", data.province);
        qry.bindValue(":COUNTRY", data.country);
        qry.bindValue(":PHONE", data.phone);
        qry.bindValue(":MOBILE", data.mobile);
        qry.bindValue(":FAX", data.fax);
        qry.bindValue(":EMAIL", data.email);
        qry.bindValue(":WEB", data.web);
        qry.bindValue(":CIF_COMMUNITY", data.cifCommunity);
        qry.bindValue(":IAE", data.iae);
        qry.bindValue(":CNAE", data.cnae);
        qry.bindValue(":DCNAE", data.dcnae);
        qry.bindValue(":LOGO1", data.logo1);
        qry.bindValue(":LOGO2", data.logo2);
        qry.bindValue(":CREATEDBY", data.createdBy);
        ok = qry.exec();
        if (!ok) qDebug() << "EnterpriseDAO::create info error:" << qry.lastError().text();
    }

    dApp->Enterprise().close();
    return ok;
}

bool EnterpriseDAO::read(const QString& name, Data& data)
{
    dApp->Enterprise().open();
    QSqlQuery qry(dApp->Enterprise());
    qry.prepare("SELECT NAME, CIF, ADDRESS, POSTCODE, CITY, PROVINCE, COUNTRY, "
                "PHONE, MOBILE, FAX, EMAIL, WEB, CIF_COMMUNITY, IAE, CNAE, DCNAE, "
                "LOGO1, LOGO2, CREATEDBY "
                "FROM ENTERPRISESINFO WHERE NAME = :NAME");
    qry.bindValue(":NAME", name);

    bool ok = qry.exec();
    if (ok && qry.next()) {
        data.name = qry.value(0).toString();
        data.cif = qry.value(1).toString();
        data.address = qry.value(2).toString();
        data.postcode = qry.value(3).toString();
        data.city = qry.value(4).toString();
        data.province = qry.value(5).toString();
        data.country = qry.value(6).toString();
        data.phone = qry.value(7).toString();
        data.mobile = qry.value(8).toString();
        data.fax = qry.value(9).toString();
        data.email = qry.value(10).toString();
        data.web = qry.value(11).toString();
        data.cifCommunity = qry.value(12).toString();
        data.iae = qry.value(13).toString();
        data.cnae = qry.value(14).toString();
        data.dcnae = qry.value(15).toString();
        data.logo1 = qry.value(16).toByteArray();
        data.logo2 = qry.value(17).toByteArray();
        data.createdBy = qry.value(18).toString();
    } else {
        ok = false;
        qDebug() << "EnterpriseDAO::read error:" << qry.lastError().text();
    }

    dApp->Enterprise().close();
    return ok;
}

bool EnterpriseDAO::update(const Data& data)
{
    dApp->Enterprise().open();
    QSqlQuery qry(dApp->Enterprise());
    qry.prepare("UPDATE ENTERPRISESINFO SET "
                "ADDRESS = :ADDRESS, POSTCODE = :POSTCODE, CITY = :CITY, PROVINCE = :PROVINCE, "
                "COUNTRY = :COUNTRY, PHONE = :PHONE, MOBILE = :MOBILE, FAX = :FAX, "
                "EMAIL = :EMAIL, WEB = :WEB, CIF_COMMUNITY = :CIF_COMMUNITY, IAE = :IAE, "
                "CNAE = :CNAE, DCNAE = :DCNAE, LOGO1 = :LOGO1, LOGO2 = :LOGO2 "
                "WHERE NAME = :NAME");
    qry.bindValue(":NAME", data.name);
    qry.bindValue(":ADDRESS", data.address);
    qry.bindValue(":POSTCODE", data.postcode);
    qry.bindValue(":CITY", data.city);
    qry.bindValue(":PROVINCE", data.province);
    qry.bindValue(":COUNTRY", data.country);
    qry.bindValue(":PHONE", data.phone);
    qry.bindValue(":MOBILE", data.mobile);
    qry.bindValue(":FAX", data.fax);
    qry.bindValue(":EMAIL", data.email);
    qry.bindValue(":WEB", data.web);
    qry.bindValue(":CIF_COMMUNITY", data.cifCommunity);
    qry.bindValue(":IAE", data.iae);
    qry.bindValue(":CNAE", data.cnae);
    qry.bindValue(":DCNAE", data.dcnae);
    qry.bindValue(":LOGO1", data.logo1);
    qry.bindValue(":LOGO2", data.logo2);

    bool ok = qry.exec();
    if (!ok) qDebug() << "EnterpriseDAO::update error:" << qry.lastError().text();
    dApp->Enterprise().close();
    return ok;
}

bool EnterpriseDAO::remove(const QString& name)
{
    dApp->Enterprise().open();
    QSqlQuery qry(dApp->Enterprise());
    qry.prepare("DELETE FROM ENTERPRISES WHERE NAME = :NAME");
    qry.bindValue(":NAME", name);
    bool ok = qry.exec();
    if (!ok) qDebug() << "EnterpriseDAO::remove error:" << qry.lastError().text();

    if (ok) {
        qry.prepare("DELETE FROM ENTERPRISESINFO WHERE NAME = :NAME");
        qry.bindValue(":NAME", name);
        ok = qry.exec();
        if (!ok) qDebug() << "EnterpriseDAO::remove info error:" << qry.lastError().text();
    }

    dApp->Enterprise().close();
    return ok;
}

bool EnterpriseDAO::exists(const QString& name, const QString& cif)
{
    dApp->Enterprise().open();
    QSqlQuery qry(dApp->Enterprise());
    qry.prepare("SELECT COUNT(*) FROM ENTERPRISES WHERE NAME = :NAME OR CIF = :CIF");
    qry.bindValue(":NAME", name);
    qry.bindValue(":CIF", cif);
    bool exists = false;
    if (qry.exec() && qry.next())
        exists = qry.value(0).toInt() > 0;
    dApp->Enterprise().close();
    return exists;
}

QVector<QString> EnterpriseDAO::getAllNames()
{
    QVector<QString> names;
    dApp->Enterprise().open();
    QSqlQuery qry(dApp->Enterprise());
    qry.prepare("SELECT NAME FROM ENTERPRISES ORDER BY NAME");
    if (qry.exec()) {
        while (qry.next())
            names.append(qry.value(0).toString());
    }
    dApp->Enterprise().close();
    return names;
}