1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967
|
/*
* 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 "recurrencewidget.h"
#include <QDebug>
#include <QMessageBox>
#include <QSqlError>
#include <QtScript>
#include "xsqlquery.h"
#include "storedProcErrorLookup.h"
#define DEBUG false
/**
\class RecurrenceWidget
\brief The RecurrenceWidget gives the %user a unified interface for telling
the system how often certain %events occur.
In general recurrences are stored in the recur table, but this is not
required.
Two basic values describe a recurrence:
<UL>
<LI>The period is the time unit of measure (hour, day, month).
</LI>
<LI>The frequency is the number of periods between recurrences.</LI>
</UL>
A recurrence with the period set to W (= week) and frequency of 3 will repeat
once every three weeks.
To add a new kind of recurring event or %item, you need to change data in the
database, stored procedures, triggers, and application code.
We'll use Invoice in the examples here, with 'I' as the internal code value,
even though %invoices already use this mechanism (xTuple ERP 3.5.1 and later).
Add a column to the parent table to track the recurrence parent/child
relationship. The column name must follow this pattern:
\c [tablename]_recurring_[tablename]_id ,
e.g.
\c invchead_recurring_invchead_id :
\code
ALTER TABLE invchead ADD COLUMN invchead_recurring_invchead_id INTEGER;
COMMENT ON COLUMN invchead.invchead_recurring_invchead_id IS 'The first invchead record in the series if this is a recurring Invoice. If the invchead_recurring_invchead_id is the same as the invchead_id, this record is the first in the series.';
\endcode
Add a RecurrenceWidget to the .ui for the window that will maintain data of
this type (e.g. invoice.ui).
Initialize the widget in the window's constructor:
\code
_recur->setParent(-1, 'I');
\endcode
Update the widget again when the window gets an id for the object.
This usually happens in either the
%set(), %save(), or %sSave() method for new objects, depending on the class,
and in %populate() when editing existing objects:
\code
// cNew case in invoice::set()
_recur->setParent(_invcheadid, 'I');
...
// invoice::populate()
_recur->setParent(q.value("invchead_recurring_invchead_id").toInt(), "I");
\endcode
Ask the %user how to handle existing recurrences before saving the data
and before starting a transaction. getChangePolicy will return
RecurrenceWidget::NoPolicy) if the %user chooses to cancel.
Then when preparing to insert or update the
main record, make sure to set the recurrence parentage.
Finally, after the insert/update, save the recurrence and check for errors:
\code
bool %save()
{
// error checking
RecurrenceWidget::ChangePolicy cp = _recur->getChangePolicy();
if (cp == RecurrenceWidget::NoPolicy)
return false;
XSqlQuery beginq("BEGIN;");
XSqlQuery qry;
...
// set up qry to insert or update
if (_recur->isRecurring())
qry.bindValue(":invchead_recurring_invchead_id", _recur->parentId());
...
// execute the insert/update
...
QString errmsg;
if (! _recur->save(true, cp, &errmsg))
{
rollbackq.exec();
systemError(this, errmsg, __FILE__, __LINE__);
return false;
}
...
// finish processing
XSqlQuery commitq("COMMIT;");
return true;
}
\endcode
To maintain the recurrence relationships,
there must be a function that copies an existing record
based on its id and gives the copy a different timestamp or date.
It must have a function signature like one of these:
<OL>
<LI>copy[tablename](INTEGER, TIMESTAMP WITH TIME ZONE)</LI>
<LI>copy[tablename](INTEGER, TIMESTAMP WITHOUT TIME ZONE)</LI>
<LI>copy[tablename](INTEGER, DATE)</LI>
</OL>
The copy function can take additional arguments as well, but they will be
ignored by the recurrence maintenance functions. The data type of each
argument must be listed in
the recurtype table's recurtype_copyargs column (see below) so the appropriate
casting can be done for the date or timestamp and an appropriate number
of NULL arguments can be passed.
The copy function must copy the
\c [tablename]_recurring_[tablename]_id
column.
There can be a function to delete records of this type as well. If there is
one, it must accept a single integer id of the record to delete.
Add a row to the recurtype table to
describe how the recurrence stored procedures interact with the
events/items of this type ('I' == Invoice).
\code
INSERT INTO recurtype (recurtype_type, recurtype_table, recurtype_donecheck,
recurtype_schedcol, recurtype_limit,
recurtype_copyfunc, recurtype_copyargs, recurtype_delfunc
) VALUES ('I', 'invchead', 'invchead_posted',
'invchead_invcdate', NULL,
'copyinvoice', '{integer,date}', 'deleteinvoice');
\endcode
If there isn't a delete function, set the recurtype_delfunc to
NULL. Existing records will be deleted when necessary with an
SQL DELETE statement.
The DELETE trigger on the table should clean up the recurrence information:
\code
CREATE OR REPLACE FUNCTION _invcheadBeforeTrigger() RETURNS "trigger" AS $$
DECLARE
_recurid INTEGER;
_newparentid INTEGER;
BEGIN
IF (TG_OP = 'DELETE') THEN
-- after other stuff not having to do with recurrence
SELECT recur_id INTO _recurid
FROM recur
WHERE ((recur_parent_id=OLD.invchead_id)
AND (recur_parent_type='I'));
IF (_recurid IS NOT NULL) THEN
SELECT invchead_id INTO _newparentid
FROM invchead
WHERE ((invchead_recurring_invchead_id=OLD.invchead_id)
AND (invchead_id!=OLD.invchead_id))
ORDER BY invchead_invcdate
LIMIT 1;
IF (_newparentid IS NULL) THEN
DELETE FROM recur WHERE recur_id=_recurid;
ELSE
UPDATE recur SET recur_parent_id=_newparentid
WHERE recur_id=_recurid;
UPDATE invchead SET invchead_recurring_invchead_id=_newparentid
WHERE invchead_recurring_invchead_id=OLD.invchead_id
AND invchead_id!=OLD.invchead_id;
END IF;
END IF;
RETURN OLD;
END IF;
RETURN NEW;
END;
$$ LANGUAGE 'plpgsql';
\endcode
\todo Simplify this so the developer of a new recurring item/event doesn't
have to work so hard. There should be a way to (1) have the recurrence
widget update the [tablename]_recurring_[tablename]_id column
when saving the recurrence (this would save a couple of lines)
and (2) there should be a way to simplify or eliminate the code
in the DELETE triggers. Some of this stuff really belongs in an
AFTER trigger instead of a before trigger.
\see _invcheadBeforeTrigger
\see copyInvoice
\see createRecurringItems
\see deleteInvoice
\see deleteOpenRecurringItems
\see invoice
\see openRecurringItems
\see recur
\see recurtype
\see splitRecurrence
*/
/* TODO: Add properties to set minimum/maximum periods?
(e.g. it doesn't make sense for invoicing to recur hourly).
The enum value Custom is intended for use by cases like
Recur every Tuesday and Thursday
Recur 1st of every month
Recur every January and July
It would be nice to have drag-n-drop using iCalendar (IETF RFC 5545)
but this will have to be associated with the To-do or calendar item,
not just the recurrence widget.
*/
RecurrenceWidget::RecurrenceWidget(QWidget *parent, const char *pName) :
QWidget(parent)
{
setupUi(this);
if(pName)
setObjectName(pName);
setMinPeriod(Minutely);
if (minPeriod() < Weekly)
_period->setCode("W");
_dates->setStartCaption(tr("From:"));
_dates->setEndCaption(tr("Until:"));
_dates->setStartNull(tr("Today"), QDate::currentDate(), true);
setStartDateVisible(false);
setEndTimeVisible(false);
setStartTimeVisible(false);
setMaxVisible(true);
if (_x_preferences)
{
XSqlQuery eotq("SELECT endOfTime() AS eot;");
if (eotq.first())
{
_eot = QDateTime(eotq.value("eot").toDate(), QTime(23, 59, 59, 999));
_dates->setEndNull(tr("Forever"), _eot.date(), true);
}
else
qWarning("RecurrenceWidget could not get endOfTime()");
}
_dates->setEndNull(tr("Forever"), _eot.date(), true);
if (! _eot.isValid())
_eot = QDateTime(QDate(2100,12,31), QTime(23, 59, 59, 999));
clear();
}
RecurrenceWidget::~RecurrenceWidget()
{
}
void RecurrenceWidget::languageChange()
{
}
/** \brief Set the RecurrenceWidget to its default state.
*/
void RecurrenceWidget::clear()
{
set(false, 1, "W", QDateTime::currentDateTime(), _eot, 1);
_id = -1;
_prevParentId = -1;
_prevParentType = "";
_parentId = -1;
_parentType = "";
}
/** \brief Convert a string representation of the period to a RecurrencePeriod.
This is used to convert between the values stored in the recur table
and the enumerated values used internally by the RecurrenceWidget.
It also accepts human-readable values, such as "Minutes" and "Hours".
\param p The recur_period or human-readable period name to convert.
This is case-sensitive. Currently accepted values are
m or Minutes, H or Hours, D or Days, W or Weeks, M or Months,
Y or Years, C or Custom. If current translation includes
the period names, the translations are also accepted.
\return The RecurrencePeriod that matches the input or Never
if no match was found.
*/
RecurrenceWidget::RecurrencePeriod RecurrenceWidget::stringToPeriod(QString p) const
{
if (p == "m" || p == "Minutes" || p == tr("Minutes")) return Minutely;
else if (p == "H" || p == "Hours" || p == tr("Hours") ) return Hourly;
else if (p == "D" || p == "Days" || p == tr("Days") ) return Daily;
else if (p == "W" || p == "Weeks" || p == tr("Weeks") ) return Weekly;
else if (p == "M" || p == "Months" || p == tr("Months") ) return Monthly;
else if (p == "Y" || p == "Years" || p == tr("Years") ) return Yearly;
else if (p == "C" || p == "Custom" || p == tr("Custom") ) return Custom;
else return Never;
}
/** \brief Return the date the recurrence is set to end. */
QDate RecurrenceWidget::endDate() const
{
return _dates->endDate();
}
/** \brief Return the date and time the recurrence is set to end. */
QDateTime RecurrenceWidget::endDateTime() const
{
return QDateTime(_dates->endDate(), _endTime->time());
}
/** \brief Return the time of day the recurrence is set to end. */
QTime RecurrenceWidget::endTime() const
{
return _endTime->time();
}
/** \brief Return whether the end date field is visible. */
bool RecurrenceWidget::endDateVisible() const
{
return _dates->endVisible();
}
/** \brief Return whether the end time field is visible. */
bool RecurrenceWidget::endTimeVisible() const
{
return _endTime->isVisible();
}
/** \brief Return the frequency (number of periods) the recurrence is set
to run.
*/
int RecurrenceWidget::frequency() const
{
return _frequency->value();
}
RecurrenceWidget::RecurrenceChangePolicy RecurrenceWidget::getChangePolicy()
{
if (DEBUG)
qDebug("%s::getChangePolicy() entered with id %d type %s",
qPrintable(objectName()), _parentId, qPrintable(_parentType));
if (! modified())
return IgnoreFuture;
RecurrenceChangePolicy ans;
XSqlQuery futureq;
futureq.prepare("SELECT openRecurringItems(:parent_id, :parent_type,"
" NULL) AS open;");
futureq.bindValue(":parent_id", _parentId);
futureq.bindValue(":parent_type", _parentType);
futureq.exec();
if (futureq.first())
{
int open = futureq.value("open").toInt();
if (open < 0)
{
QMessageBox::critical(this, tr("Processing Error"),
storedProcErrorLookup("openRecurringItems", open));
return NoPolicy;
}
else if (open == 0)
return ChangeFuture;
}
else if (futureq.lastError().type() != QSqlError::NoError)
{
QMessageBox::critical(this, tr("Database Error"),
futureq.lastError().text());
return NoPolicy;
}
switch (QMessageBox::question(this, tr("Change Open Events?"),
tr("<p>This event is part of a recurring "
"series. Do you want to change all open "
"events in this series?"),
QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel,
QMessageBox::Yes))
{
case QMessageBox::Yes: ans = ChangeFuture; break;
case QMessageBox::No: ans = IgnoreFuture; break;
case QMessageBox::Cancel:
default: ans = NoPolicy; break;
};
return ans;
}
bool RecurrenceWidget::isRecurring() const
{
return _recurring->isChecked();
}
int RecurrenceWidget::max() const
{
return _max->value();
}
bool RecurrenceWidget::maxVisible() const
{
return _max->isVisible();
}
RecurrenceWidget::RecurrencePeriod RecurrenceWidget::minPeriod() const
{
return (RecurrencePeriod)_period->id(0);
}
bool RecurrenceWidget::modified() const
{
if (DEBUG)
qDebug() << "recurring:" << isRecurring() << _prevRecurring << "\n"
<< "period:" << period() << _prevPeriod << "\n"
<< "frequency:" << frequency() << _prevFrequency << "\n"
<< "startDateTime:" << startDateTime()<< _prevStartDateTime << "\n"
<< "endDateTime:" << endDateTime() << _prevEndDateTime << "\n"
<< "max:" << max() << _prevMax << "\n"
<< "_parentId:" << _parentId << _prevParentId << "\n"
<< "_parentType:" << _parentType << _prevParentType
;
bool returnVal = (isRecurring() != _prevRecurring ||
period() != _prevPeriod ||
frequency() != _prevFrequency ||
startDateTime() != _prevStartDateTime||
endDateTime() != _prevEndDateTime ||
max() != _prevMax ||
_parentId != _prevParentId ||
_parentType != _prevParentType);
return returnVal;
}
int RecurrenceWidget::parentId() const
{
return _parentId;
}
QString RecurrenceWidget::parentType() const
{
return _parentType;
}
RecurrenceWidget::RecurrencePeriod RecurrenceWidget::period() const
{
return (RecurrencePeriod)(_period->id());
}
QString RecurrenceWidget::periodCode() const
{
return _period->code();
}
bool RecurrenceWidget::save(bool externaltxn, RecurrenceChangePolicy cp, QString *message)
{
if (! message)
message = new QString();
if (DEBUG)
qDebug("%s::save(%d, %d, %p) entered with id %d type %s",
qPrintable(objectName()), externaltxn, cp, message, _parentId,
qPrintable(_parentType));
if (! modified())
return true;
if (_parentId < 0 || _parentType.isEmpty())
{
*message = tr("Could not save Recurrence information. The "
"parent object/event has not been set.");
if (! externaltxn)
QMessageBox::warning(this, tr("Missing Data"), *message);
else
qWarning("%s", qPrintable(*message));
return false;
}
if (! externaltxn && cp == NoPolicy)
{
cp = getChangePolicy();
if (cp == NoPolicy)
return false;
}
else if (externaltxn && cp == NoPolicy)
{
*message = tr("You must choose how open events are to be handled");
qWarning("%s", qPrintable(*message));
return false;
}
XSqlQuery rollbackq;
if (! externaltxn)
{
XSqlQuery beginq("BEGIN;");
rollbackq.prepare("ROLLBACK;");
}
XSqlQuery recurq;
if (isRecurring())
{
if (_id > 0)
{
if (cp == ChangeFuture)
{
XSqlQuery futureq;
futureq.prepare("SELECT splitRecurrence(:parent_id, :parent_type,"
" :splitdate) AS newrecurid;");
futureq.bindValue(":parent_id", _parentId);
futureq.bindValue(":parent_type", _parentType);
futureq.bindValue(":splitdate", startDate());
futureq.exec();
if (futureq.first())
{
int result = futureq.value("newrecurid").toInt();
if (result > 0)
{
_id = result;
futureq.prepare("SELECT recur_parent_id"
" FROM recur"
" WHERE recur_id=:recur_id;");
futureq.bindValue(":recur_id", _id);
futureq.exec();
if (futureq.first())
_parentId = futureq.value("recur_parent_id").toInt();
}
else if (result < 0)
{
*message = storedProcErrorLookup("splitRecurrence", result);
if (! externaltxn)
{
rollbackq.exec();
QMessageBox::warning(this, tr("Processing Error"), *message);
}
else
qWarning("%s", qPrintable(*message));
return false;
}
}
// one check for potentially 2 queries
if (futureq.lastError().type() != QSqlError::NoError)
{
*message = futureq.lastError().text();
if (! externaltxn)
{
rollbackq.exec();
QMessageBox::warning(this, tr("Database Error"), *message);
}
else
qWarning("%s", qPrintable(*message));
return false;
}
}
recurq.prepare("UPDATE recur SET"
" recur_parent_id=:recur_parent_id,"
" recur_parent_type=UPPER(:recur_parent_type),"
" recur_period=:recur_period,"
" recur_freq=:recur_freq,"
" recur_start=:recur_start,"
" recur_end=:recur_end,"
" recur_max=:recur_max"
" WHERE (recur_id=:recurid)"
" RETURNING recur_id;");
recurq.bindValue(":recurid", _id);
}
else
{
recurq.prepare("INSERT INTO recur ("
" recur_parent_id, recur_parent_type,"
" recur_period, recur_freq,"
" recur_start, recur_end,"
" recur_max"
") VALUES ("
" :recur_parent_id, UPPER(:recur_parent_type),"
" :recur_period, :recur_freq,"
" :recur_start, :recur_end,"
" :recur_max"
") RETURNING recur_id;");
}
recurq.bindValue(":recur_parent_id", _parentId);
recurq.bindValue(":recur_parent_type", _parentType);
recurq.bindValue(":recur_period", periodCode());
recurq.bindValue(":recur_freq", frequency());
recurq.bindValue(":recur_start", startDateTime());
if (endDate() < _eot.date())
recurq.bindValue(":recur_end", endDateTime());
recurq.bindValue(":recur_max", max());
recurq.exec();
if (recurq.first())
{
_id = recurq.value("recur_id").toInt();
_prevParentId = _parentId;
_prevParentType = _parentType;
_prevEndDateTime = endDateTime();
_prevFrequency = frequency();
_prevMax = max();
_prevPeriod = period();
_prevRecurring = isRecurring();
_prevStartDateTime = startDateTime();
}
}
else // ! isRecurring()
{
recurq.prepare("DELETE FROM recur"
" WHERE ((recur_parent_id=:recur_parent_id)"
" AND (UPPER(recur_parent_type)=UPPER(:recur_parent_type)));");
recurq.bindValue(":recur_parent_id", _parentId);
recurq.bindValue(":recur_parent_type", _parentType);
recurq.exec();
}
if (recurq.lastError().type() != QSqlError::NoError)
{
*message = recurq.lastError().text();
if (! externaltxn)
{
rollbackq.exec();
QMessageBox::warning(this, tr("Database Error"), *message);
}
else
qWarning("%s", qPrintable(*message));
return false;
}
if (cp == ChangeFuture)
{
int procresult = -1;
QString procname = "deleteOpenRecurringItems";
XSqlQuery cfq;
cfq.prepare("SELECT deleteOpenRecurringItems(:parentId, :parentType,"
" :splitdate, false) AS result;");
cfq.bindValue(":parentId", _parentId);
cfq.bindValue(":parentType", _parentType);
cfq.bindValue(":splitdate", startDate());
cfq.exec();
if (cfq.first())
{
procresult = cfq.value("result").toInt();
if (procresult >= 0)
{
QString procname = "createOpenRecurringItems";
cfq.prepare("SELECT createRecurringItems(:parentId, :parentType)"
" AS result;");
cfq.bindValue(":parentId", _parentId);
cfq.bindValue(":parentType", _parentType);
cfq.exec();
if (cfq.first())
procresult = cfq.value("result").toInt();
}
}
// error handling for either 1 or 2 queries so not elseif
// check cfq.lastError() first to avoid misreporting db errs as -1
if (cfq.lastError().type() != QSqlError::NoError)
{
*message = cfq.lastError().text();
if (! externaltxn)
{
rollbackq.exec();
QMessageBox::critical(this, tr("Database Error"), *message);
}
else
qWarning("%s", qPrintable(*message));
return false;
}
else if (procresult < 0)
{
*message = storedProcErrorLookup(procname, procresult);
if (! externaltxn)
{
rollbackq.exec();
QMessageBox::critical(this, tr("Processing Error"), *message);
}
else
qWarning("%s", qPrintable(*message));
return false;
}
}
if (! externaltxn)
XSqlQuery commitq("COMMIT;");
return true;
}
void RecurrenceWidget::set(bool recurring, int frequency, QString period, QDate startDate, QDate endDate, int max)
{
if (DEBUG)
qDebug() << objectName() << "::set(" << recurring << ", "
<< frequency << ", " << period << ", "
<< startDate << ", " << endDate << ", "
<< max << ") entered";
// run from the beginning of the start date to the end of the end date
QDateTime startDateTime(startDate);
QDateTime endDateTime(endDate.addDays(1));
endDateTime = endDateTime.addMSecs(-1);
set(recurring, frequency, period, startDateTime, endDateTime, max);
}
void RecurrenceWidget::set(bool recurring, int frequency, QString period, QDateTime start, QDateTime end, int max)
{
if (DEBUG)
qDebug() << objectName() << "::set(" << recurring << ", "
<< frequency << ", " << period << ", "
<< start << ", " << end << ", "
<< max << ") entered";
setRecurring(recurring);
setPeriod(period);
setFrequency(frequency);
setStartDateTime(start);
setEndDateTime(end);
setMax(max);
_prevEndDateTime = end.isValid() ? end : _eot ;
_prevFrequency = frequency;
_prevPeriod = stringToPeriod(period);
_prevRecurring = recurring;
_prevStartDateTime = start;
_prevMax = max;
}
void RecurrenceWidget::setEndDate(QDate p)
{
_dates->setEndDate(p.isValid() ? p : _eot.date());
_endTime->setTime(QTime(23, 59, 59, 999));
}
void RecurrenceWidget::setEndDateTime(QDateTime p)
{
if (DEBUG)
qDebug("setEndDateTime(%s) entered, valid = %d",
qPrintable(p.toString()), p.isValid());
if (p.isValid())
{
_dates->setEndDate(p.date());
_endTime->setTime(p.time());
}
else
setEndDateTime(_eot);
}
void RecurrenceWidget::setEndDateVisible(bool p)
{
_dates->setEndVisible(p);
}
void RecurrenceWidget::setEndTime(QTime p)
{
_endTime->setTime(p);
}
void RecurrenceWidget::setEndTimeVisible(bool p)
{
_endTime->setVisible(p);
}
void RecurrenceWidget::setFrequency(int p)
{
_frequency->setValue(p);
}
void RecurrenceWidget::setMax(int p)
{
_max->setValue(p);
}
void RecurrenceWidget::setMaxVisible(bool p)
{
_max->setVisible(p);
_maxLit->setVisible(p);
}
void RecurrenceWidget::setMinPeriod(RecurrencePeriod min)
{
_period->clear();
// fall through all cases in the switch
switch (min)
{
default:
// case Never: _period->append(Never, tr("Never"), "");
case Minutely: _period->append(Minutely, tr("Minutes"), "m");
case Hourly: _period->append(Hourly, tr("Hours"), "H");
case Daily: _period->append(Daily, tr("Days"), "D");
case Weekly: _period->append(Weekly, tr("Weeks"), "W");
case Monthly: _period->append(Monthly, tr("Months"), "M");
case Yearly: _period->append(Yearly, tr("Years"), "Y");
//case Custom: _period->append(Custom, tr("Custom"), "C");
}
}
bool RecurrenceWidget::setParent(int pid, QString ptype)
{
_parentId = pid;
_parentType = ptype;
XSqlQuery recurq;
recurq.prepare("SELECT *"
" FROM recur"
" WHERE ((recur_parent_id=:parentid)"
" AND (recur_parent_type=UPPER(:parenttype)));");
recurq.bindValue(":parentid", pid);
recurq.bindValue(":parenttype", ptype);
recurq.exec();
if (recurq.first())
{
set(true,
recurq.value("recur_freq").toInt(),
recurq.value("recur_period").toString(),
recurq.value("recur_start").toDateTime(),
recurq.value("recur_end").toDateTime(),
recurq.value("recur_max").toInt());
_id = recurq.value("recur_id").toInt();
_prevParentId = _parentId;
_prevParentType = _parentType;
return true;
}
else if (recurq.lastError().type() != QSqlError::NoError)
QMessageBox::warning(this, tr("Database Error"),
recurq.lastError().text());
else
{
clear();
_parentId = pid;
_parentType = ptype;
}
return false;
}
void RecurrenceWidget::setPeriod(RecurrencePeriod p)
{
_period->setId((int)p);
}
void RecurrenceWidget::setPeriod(QString p)
{
_period->setCode(p);
}
void RecurrenceWidget::setRecurring(bool p)
{
_recurring->setChecked(p);
}
void RecurrenceWidget::setStartDate(QDate p)
{
_dates->setStartDate(p.isValid() ? p : QDate::currentDate());
_startTime->setTime(QTime(0, 0));
}
void RecurrenceWidget::setStartDateTime(QDateTime p)
{
if (p.isValid())
{
_dates->setStartDate(p.date());
_startTime->setTime(p.time());
}
else
setStartDate(QDate::currentDate());
}
void RecurrenceWidget::setStartDateVisible(bool p)
{
_dates->setStartVisible(p);
}
void RecurrenceWidget::setStartTime(QTime p)
{
_startTime->setTime(p);
}
void RecurrenceWidget::setStartTimeVisible(bool p)
{
_startTime->setVisible(p);
}
QDate RecurrenceWidget::startDate() const
{
return _dates->startDate();
}
QDateTime RecurrenceWidget::startDateTime() const
{
return QDateTime(_dates->startDate(), _startTime->time());
}
bool RecurrenceWidget::startDateVisible() const
{
return _dates->startVisible();
}
QTime RecurrenceWidget::startTime() const
{
return _startTime->time();
}
bool RecurrenceWidget::startTimeVisible() const
{
return _startTime->isVisible();
}
// scripting exposure /////////////////////////////////////////////////////////
QScriptValue RecurrenceWidgettoScriptValue(QScriptEngine *engine, RecurrenceWidget* const &item)
{
return engine->newQObject(item);
}
void RecurrenceWidgetfromScriptValue(const QScriptValue &obj, RecurrenceWidget* &item)
{
item = qobject_cast<RecurrenceWidget*>(obj.toQObject());
}
QScriptValue constructRecurrenceWidget(QScriptContext *context,
QScriptEngine *engine)
{
QWidget *parent = (qscriptvalue_cast<QWidget*>(context->argument(0)));
const char *objname = "_recurrenceWidget";
if (context->argumentCount() > 1)
objname = context->argument(1).toString().toLatin1().data();
return engine->toScriptValue(new RecurrenceWidget(parent, objname));
}
void setupRecurrenceWidget(QScriptEngine *engine)
{
QScriptValue::PropertyFlags stdflags = QScriptValue::ReadOnly |
QScriptValue::Undeletable;
qScriptRegisterMetaType(engine, RecurrenceWidgettoScriptValue,
RecurrenceWidgetfromScriptValue);
QScriptValue constructor = engine->newFunction(constructRecurrenceWidget);
engine->globalObject().setProperty("RecurrenceWidget", constructor, stdflags);
constructor.setProperty("Never", QScriptValue(engine, RecurrenceWidget::Never), stdflags);
constructor.setProperty("Minutely",QScriptValue(engine, RecurrenceWidget::Minutely), stdflags);
constructor.setProperty("Hourly", QScriptValue(engine, RecurrenceWidget::Hourly), stdflags);
constructor.setProperty("Daily", QScriptValue(engine, RecurrenceWidget::Daily), stdflags);
constructor.setProperty("Weekly", QScriptValue(engine, RecurrenceWidget::Weekly), stdflags);
constructor.setProperty("Monthly", QScriptValue(engine, RecurrenceWidget::Monthly), stdflags);
constructor.setProperty("Yearly", QScriptValue(engine, RecurrenceWidget::Yearly), stdflags);
constructor.setProperty("Custom", QScriptValue(engine, RecurrenceWidget::Custom), stdflags);
constructor.setProperty("NoPolicy", QScriptValue(engine, RecurrenceWidget::NoPolicy), stdflags);
constructor.setProperty("IgnoreFuture", QScriptValue(engine, RecurrenceWidget::IgnoreFuture), stdflags);
constructor.setProperty("ChangeFuture", QScriptValue(engine, RecurrenceWidget::ChangeFuture), stdflags);
}
|