bool printMulticopyDocument::sPostOneDoc(XSqlQuery *docq)
{
  if (! _postQuery.isEmpty()    &&
      _data->_post->isChecked() &&
      isOnPrintedList(docq->value("docid").toInt()) &&
      ! docq->value("posted").toBool())
  {
    QString docnumber = docq->value("docnumber").toString();
    message(tr("Posting %1 #%2").arg(_data->_doctypefull, docnumber));

    ParameterList postp;
    postp.append("docid",     docq->value("docid"));
    postp.append("docnumber", docq->value("docnumber"));

    if (! _askBeforePostingQry.isEmpty())
    {
      MetaSQLQuery askm(_askBeforePostingQry);
      XSqlQuery askq = askm.toQuery(postp);
      if (ErrorReporter::error(QtCriticalMsg, this,
                               tr("Cannot Post %1").arg(docnumber),
                               askq, __FILE__, __LINE__))
        return false;
      else if (askq.value("ask").toBool() &&
               QMessageBox::question(this, tr("Post Anyway?"),
                                     _askBeforePostingMsg.arg(docnumber),
                                      QMessageBox::Yes,
                                      QMessageBox::No | QMessageBox::Default)
                  == QMessageBox::No)
        return false;
    }

    if (! _errCheckBeforePostingQry.isEmpty())
    {
      MetaSQLQuery errm(_errCheckBeforePostingQry);
      XSqlQuery errq = errm.toQuery(postp);
      if (ErrorReporter::error(QtCriticalMsg, this,
                               tr("Cannot Post %1").arg(docnumber),
                               errq, __FILE__, __LINE__))
        return false;
      else if (! errq.first() || ! errq.value("ok").toBool())
      {
        ErrorReporter::error(QtCriticalMsg, this,
                             tr("Cannot Post %1").arg(docnumber),
                             _errCheckBeforePostingMsg, __FILE__, __LINE__);
        return false;
      }
    }

    //TODO: find a way to do this without holding locks during user input
    XSqlQuery("BEGIN;");

    XSqlQuery rollback;
    rollback.prepare("ROLLBACK;");

    MetaSQLQuery postm(_postQuery);
    XSqlQuery postq = postm.toQuery(postp);
    if (postq.first())
    {
      int result = postq.value("result").toInt();
      if (result < 0)
      {
        rollback.exec();
        ErrorReporter::error(QtCriticalMsg, this,
                             tr("Cannot Post %1").arg(docnumber),
                             storedProcErrorLookup(_postFunction, result),
                             __FILE__, __LINE__);
        return false;
      }
      if (_distributeInventory &&
          (distributeInventory::SeriesAdjust(result, this) == XDialog::Rejected))
      {
        rollback.exec();
        QMessageBox::information(this, tr("Posting Canceled"),
                                 tr("Transaction Canceled") );
        return false;
      }
    }
    else if (postq.lastError().type() != QSqlError::NoError)
    {
      rollback.exec();
      ErrorReporter::error(QtCriticalMsg, this,
                           tr("Cannot Post %1").arg(docnumber),
                           postq, __FILE__, __LINE__);
      return false;
    }

    XSqlQuery("COMMIT;");

    emit posted(_data->_docid);
  }

  return true;
}
Exemple #2
0
void enterPoReceipt::sPost()
{
  ParameterList params;	// shared by several queries
  setParams(params);

  QString checks = "SELECT SUM(qtyToReceive(recv_order_type,"
		   "                        recv_orderitem_id)) AS qtyToRecv "
		   "FROM recv, orderitem "
		   "WHERE ((recv_order_type=<? value(\"ordertype\") ?>)"
		   "  AND  (recv_orderitem_id=orderitem_id)"
		   "  AND  (orderitem_orderhead_id=<? value(\"orderid\") ?>));";
  MetaSQLQuery checkm(checks);
  q = checkm.toQuery(params);
  if (q.first())
  {
    if (q.value("qtyToRecv").toDouble() == 0)
    {
      QMessageBox::critical(this, tr("Nothing selected for Receipt"),
			    tr("<p>No Line Items have been selected "
			       "for receipt. Select at least one Line Item and "
			       "enter a Receipt before trying to Post."));
      return;
    }
  }
  else if (q.lastError().type() != QSqlError::NoError)
  {
    systemError(this, q.lastError().databaseText(), __FILE__, __LINE__);
    return;
  }

  XSqlQuery rollback;
  rollback.prepare("ROLLBACK;");

  q.exec("BEGIN;");	// because of possible insertgltransaction failures
  QString posts = "SELECT postReceipts(<? value (\"ordertype\") ?>,"
		  "                    <? value(\"orderid\") ?>,0) AS result;" ;
  MetaSQLQuery postm(posts);
  q = postm.toQuery(params);
  if (q.first())
  {
    int result = q.value("result").toInt();
    if (result < 0)
    {
      rollback.exec();
      systemError(this, storedProcErrorLookup("postReceipts", result),
		  __FILE__, __LINE__);
      return;
    }

    QString lotnum = QString::null;
    QDate expdate = omfgThis->startOfTime();
    QDate warrdate;
    if(result > 0 && _singleLot->isChecked())
    {
      // first find out if we have any lot controlled items that need distribution
      q.prepare("SELECT count(*) AS result"
                "  FROM itemlocdist, itemsite"
                " WHERE ((itemlocdist_itemsite_id=itemsite_id)"
                "   AND  (itemlocdist_reqlotserial)"
                "   AND  (itemsite_controlmethod='L')"
                "   AND  (itemlocdist_series=:itemlocdist_series) ); ");
      q.bindValue(":itemlocdist_series", result);
      q.exec();
      // if we have any then ask for a lot# and optionally expiration date.
      if(q.first() && (q.value("result").toInt() > 0) )
      {
        getLotInfo newdlg(this, "", TRUE);

        // find out if any itemsites that are lot controlled are perishable
        q.prepare("SELECT itemsite_perishable,itemsite_warrpurc AS result"
            "  FROM itemlocdist, itemsite"
            " WHERE ((itemlocdist_itemsite_id=itemsite_id)"
            "   AND  (itemlocdist_series=:itemlocdist_series) ); ");
        q.bindValue(":itemlocdist_series", result);
        q.exec();
        if(q.first())
          newdlg.enableExpiration(q.value("itemsite_perishable").toBool());       
          newdlg.enableWarranty(q.value("itemsite_warrpurc").toBool());

          if(newdlg.exec() == XDialog::Accepted)
          {
            lotnum = newdlg.lot();
            expdate = newdlg.expiration();
            warrdate = newdlg.warranty();
          }
        }
        else if (q.lastError().type() != QSqlError::None)
        {
          systemError(this, q.lastError().databaseText(), __FILE__, __LINE__);
          rollback.exec();
          return;
        }
      }

      if (distributeInventory::SeriesAdjust(result, this, lotnum, expdate, warrdate) == XDialog::Rejected)
      {
        QMessageBox::information( this, tr("Enter Receipts"), tr("Post Canceled") );
        rollback.exec();
        return;
      }
    
      q.exec("COMMIT;");
      
      // TODO: update this to sReceiptsUpdated?
      omfgThis->sPurchaseOrderReceiptsUpdated();
      
      if (_captive)
      {
        _orderitem->clear();
        close();
      }
      else
      {
        _order->setId(-1);
        _close->setText(tr("&Close"));
      }
    }
    else if (q.lastError().type() != QSqlError::None)
    {
      rollback.exec();
      systemError(this, q.lastError().databaseText(), __FILE__, __LINE__);
      return;
    }
    else // select succeeded but returned no rows
      q.exec("COMMIT;");
}