/*
 * This file is part of the xTuple ERP: PostBooks Edition, a free and
 * open source Enterprise Resource Planning software suite,
 * Copyright (c) 1999-2009 by OpenMFG LLC, d/b/a xTuple.
 * It is licensed to you under the Common Public Attribution License
 * version 1.0, the full text of which (including xTuple-specific Exhibits)
 * is available at www.xtuple.com/CPAL.  By using this software, you agree
 * to be bound by its terms.
 */

#include "dspMaterialUsageVarianceByWorkOrder.h"

#include <QVariant>
//#include <QStatusBar>
#include <QMenu>
#include <openreports.h>
#include <parameter.h>
#include "inputManager.h"

/*
 *  Constructs a dspMaterialUsageVarianceByWorkOrder as a child of 'parent', with the
 *  name 'name' and widget flags set to 'f'.
 *
 */
dspMaterialUsageVarianceByWorkOrder::dspMaterialUsageVarianceByWorkOrder(QWidget* parent, const char* name, Qt::WFlags fl)
    : XWidget(parent, name, fl)
{
  setupUi(this);

//  (void)statusBar();

  // signals and slots connections
  connect(_print, SIGNAL(clicked()), this, SLOT(sPrint()));
  connect(_womatlvar, SIGNAL(populateMenu(QMenu*,QTreeWidgetItem*,int)), this, SLOT(sPopulateMenu(QMenu*)));
  connect(_close, SIGNAL(clicked()), this, SLOT(close()));
  connect(_wo, SIGNAL(newId(int)), this, SLOT(sFillList()));
  connect(_wo, SIGNAL(valid(bool)), _print, SLOT(setEnabled(bool)));

  _wo->setType(cWoClosed);

  omfgThis->inputManager()->notify(cBCWorkOrder, this, _wo, SLOT(setId(int)));

  _womatlvar->addColumn(tr("Post Date"),          _dateColumn,  Qt::AlignCenter, true,  "posted" );
  _womatlvar->addColumn(tr("Component Item"),     -1,           Qt::AlignLeft,   true,  "item_number"   );
  _womatlvar->addColumn(tr("Description"),        -1,           Qt::AlignLeft,   true,  "descrip");
  _womatlvar->addColumn(tr("Ordered"),            _qtyColumn,   Qt::AlignRight,  true,  "ordered"  );
  _womatlvar->addColumn(tr("Produced"),           _qtyColumn,   Qt::AlignRight,  true,  "received"  );
  _womatlvar->addColumn(tr("Proj. Req."),         _qtyColumn,   Qt::AlignRight,  true,  "projreq"  );
  _womatlvar->addColumn(tr("Proj. Qty. per"),     _qtyColumn,   Qt::AlignRight,  true,  "projqtyper"  );
  _womatlvar->addColumn(tr("Act. Iss."),          _qtyColumn,   Qt::AlignRight,  true,  "actiss"  );
  _womatlvar->addColumn(tr("Act. Qty. per"),      _qtyColumn,   Qt::AlignRight,  true,  "actqtyper"  );
  _womatlvar->addColumn(tr("Qty. per Var."),      _qtyColumn,   Qt::AlignRight,  true,  "qtypervar"  );
  _womatlvar->addColumn(tr("%"),                  _prcntColumn, Qt::AlignRight,  true,  "qtypervarpercent");
  _womatlvar->addColumn(tr("Notes"),              -1,           Qt::AlignLeft,   false, "womatlvar_notes");
  _womatlvar->addColumn(tr("Ref. Designator(s)"), -1,           Qt::AlignLeft,   false, "womatlvar_ref");
}

/*
 *  Destroys the object and frees any allocated resources
 */
dspMaterialUsageVarianceByWorkOrder::~dspMaterialUsageVarianceByWorkOrder()
{
  // no need to delete child widgets, Qt does it all for us
}

/*
 *  Sets the strings of the subwidgets using the current
 *  language.
 */
void dspMaterialUsageVarianceByWorkOrder::languageChange()
{
  retranslateUi(this);
}

void dspMaterialUsageVarianceByWorkOrder::sPrint()
{
  ParameterList params;
  params.append("wo_id", _wo->id());

  orReport report("MaterialUsageVarianceByWorkOrder", params);
  if (report.isValid())
    report.print();
  else
    report.reportError(this);
}

void dspMaterialUsageVarianceByWorkOrder::sPopulateMenu(QMenu *)
{
}

void dspMaterialUsageVarianceByWorkOrder::sFillList()
{
  if (_wo->isValid())
  {
    q.prepare( "SELECT womatlvar_id, posted, item_number,"
               "       descrip, ordered, received,"
               "       projreq, projqtyper,"
               "       actiss, actqtyper,"
               "       (actqtyper - projqtyper) AS qtypervar,"
               "       CASE WHEN (actqtyper=projqtyper) THEN 0"
               "            WHEN (projqtyper=0) THEN actqtyper"
               "            ELSE ((1 - (actqtyper / projqtyper)) * -1)"
               "       END AS qtypervarpercent,"
               "       womatlvar_notes, womatlvar_ref,"
               "       'qty' AS ordered_xtnumericrole,"
               "       'qty' AS received_xtnumericrole,"
               "       'qty' AS projreq_xtnumericrole,"
               "       'qtyper' AS projqtyper_xtnumericrole,"
               "       'qty' AS actiss_xtnumericrole,"
               "       'qtyper' AS actqtyper_xtnumericrole,"
               "       'qtyper' AS qtypervar_xtnumericrole,"
               "       'percent' AS qtypervarpercent_xtnumericrole "
               "FROM ( SELECT womatlvar_id, womatlvar_posted AS posted, item_number,"
               "              item_descrip1 || ' ' || item_descrip2 as descrip,"
               "              womatlvar_notes, womatlvar_ref,"
               "              womatlvar_qtyord AS ordered, womatlvar_qtyrcv AS received,"
               "              (womatlvar_qtyrcv * (womatlvar_qtyper * (1 + womatlvar_scrap))) AS projreq,"
               "              womatlvar_qtyper AS projqtyper,"
               "              (womatlvar_qtyiss) AS actiss, (womatlvar_qtyiss / (womatlvar_qtyrcv * (1 + womatlvar_scrap))) AS actqtyper "
               "       FROM womatlvar, itemsite, item, wo "
               "       WHERE ( (womatlvar_component_itemsite_id=itemsite_id)"
               "        AND (itemsite_item_id=item_id)"
               "        AND (wo_number=womatlvar_number)"
               "        AND (wo_subnumber=womatlvar_subnumber)"
               "        AND (wo_id=:wo_id) ) ) AS data "
               "ORDER BY item_number;" );
    q.bindValue(":wo_id", _wo->id());
    q.exec();
    _womatlvar->populate(q);
  }
  else
    _womatlvar->clear();
}

