/*
 * This file is part of the xTuple ERP: PostBooks Edition, a free and
 * open source Enterprise Resource Planning software suite,
 * Copyright (c) 1999-2014 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 "printPurchaseOrdersByAgent.h"

#include <QMessageBox>
#include <QSqlError>
#include <QVariant>

#include <openreports.h>
#include <parameter.h>

#include "guiclient.h"
#include "errorReporter.h"

printPurchaseOrdersByAgent::printPurchaseOrdersByAgent(QWidget* parent, const char* name, bool modal, Qt::WindowFlags fl)
    : XDialog(parent, name, modal, fl)
{
  setupUi(this);

  connect(_print, SIGNAL(clicked()), this, SLOT(sPrint()));

  _agent->setType(XComboBox::Agent);
  _agent->setText(omfgThis->username());

  _vendorCopy->setChecked(_metrics->boolean("POVendor"));

  if (_metrics->value("POInternal").toInt() > 0)
  {
    _internalCopy->setChecked(true);
    _numOfCopies->setValue(_metrics->value("POInternal").toInt());
  }
  else
  {
    _internalCopy->setChecked(false);
    _numOfCopies->setEnabled(false);
  }
}

printPurchaseOrdersByAgent::~printPurchaseOrdersByAgent()
{
  // no need to delete child widgets, Qt does it all for us
}

void printPurchaseOrdersByAgent::languageChange()
{
  retranslateUi(this);
}

void printPurchaseOrdersByAgent::sPrint()
{
  XSqlQuery markprinted;
  markprinted.prepare("UPDATE pohead"
                      "   SET pohead_printed=true"
                      " WHERE (pohead_id=:pohead_id);");
  XSqlQuery pohead;
  pohead.prepare( "SELECT pohead_id "
                  "FROM pohead "
                  "WHERE ( (pohead_agent_username=:username)"
                  " AND (pohead_status='O')"
                  " AND (NOT pohead_printed)"
                  " AND (pohead_saved) );" );
  pohead.bindValue(":username", _agent->currentText());
  pohead.exec();
  if (pohead.first())
  {
    QPrinter  *printer = new QPrinter(QPrinter::HighResolution);
    bool      setupPrinter = true;

    bool userCanceled = false;
    if (orReport::beginMultiPrint(printer, userCanceled) == false)
    {
      if(!userCanceled)
        ErrorReporter::error(QtCriticalMsg, this, tr("Error Occurred"),
                           tr("%1: Could not initialize printing system "
                              "for multiple reports. ").arg(windowTitle()),__FILE__,__LINE__);
      return;
    }

    do
    {
      if (_vendorCopy->isChecked())
      {
        ParameterList params;
        params.append("pohead_id", pohead.value("pohead_id").toInt());
        params.append("title", "Vendor Copy");

        orReport report("PurchaseOrder", params);
        if (report.isValid() && report.print(printer, setupPrinter))
	  setupPrinter = false;
	else
	{
          report.reportError(this);
	  orReport::endMultiPrint(printer);
	  return;
	}
      }

      if (_internalCopy->isChecked())
      {
        for (int counter = _numOfCopies->value(); counter; counter--)
        {
          ParameterList params;
          params.append("pohead_id", pohead.value("pohead_id"));
          params.append("title", QString("Internal Copy #%1").arg(counter));

          orReport report("PurchaseOrder", params);
          if (report.isValid() && report.print(printer, setupPrinter))
	    setupPrinter = false;
	  else
	  {
            report.reportError(this);
	    orReport::endMultiPrint(printer);
	    return;
	  }
        }
      }
      markprinted.bindValue(":pohead_id", pohead.value("pohead_id").toInt());
      markprinted.exec();
      if (ErrorReporter::error(QtCriticalMsg, this, tr("Error Printing Purchase Order"),
                                    markprinted, __FILE__, __LINE__))
      {
        orReport::endMultiPrint(printer);
        return;
      }

      emit finishedPrinting(pohead.value("pohead_id").toInt());
    }
    while (pohead.next());
    orReport::endMultiPrint(printer);
  }
  else if (ErrorReporter::error(QtCriticalMsg, this, tr("Error Printing Purchase Order"),
                                pohead, __FILE__, __LINE__))
  {
    return;
  }
  else
    QMessageBox::information( this, tr("No Purchase Orders to Print"),
                              tr("There are no posted, unprinted Purchase Orders entered by the selected Purchasing Agent to print.") );

  accept();
}

