Exemple #1
0
void
OpenBalances::slotPost()
{
    Id store_id = _store->getId();
    Id station_id = _station->getId();
    Id employee_id = _employee->getId();
    Id history_id = _account->getId();

    if (store_id == INVALID_ID) {
	QString message = tr("A store is required");
	QMessageBox::critical(this, tr("Error"), message);
	_store->setFocus();
	return;
    }

    if (history_id == INVALID_ID) {
	QString message = tr("An account is required");
	QMessageBox::critical(this, tr("Error"), message);
	_account->setFocus();
	return;
    }

    QApplication::setOverrideCursor(waitCursor);
    qApp->processEvents();

    for (int row = 0; row < _customers->rows(); ++row) {
	Id customer_id = _customers->cellValue(row, 0).toId();
	QDate date = _customers->cellValue(row, 1).toDate();
	fixed amount = _customers->cellValue(row, 2).toFixed();
	if (customer_id == INVALID_ID || amount == 0.0) continue;

	Customer customer;
	_quasar->db()->lookup(customer_id, customer);
	Id customer_acct = customer.accountId();

	CardAdjust adjustment;
	adjustment.setPostDate(date);
	adjustment.setPostTime(QTime(0, 0, 0));
	adjustment.setMemo(tr("Open Balance"));
	adjustment.setStationId(station_id);
	adjustment.setEmployeeId(employee_id);
	adjustment.setCardId(customer_id);
	adjustment.setStoreId(store_id);

	adjustment.accounts().push_back(AccountLine(customer_acct, amount));
	adjustment.accounts().push_back(AccountLine(history_id, -amount));
	adjustment.cards().push_back(CardLine(customer_id, amount));

	if (!_quasar->db()->create(adjustment)) {
	    QString message = tr("Customer '%1' open balance failed")
		.arg(customer.name());
	    QMessageBox::critical(this, tr("Error"), message);
	}
    }

    for (int row = 0; row < _vendors->rows(); ++row) {
	Id vendor_id = _vendors->cellValue(row, 0).toId();
	QDate date = _vendors->cellValue(row, 1).toDate();
	fixed amount = _vendors->cellValue(row, 2).toFixed();
	if (vendor_id == INVALID_ID || amount == 0.0) continue;

	Vendor vendor;
	_quasar->db()->lookup(vendor_id, vendor);
	Id vendor_acct = vendor.accountId();

	CardAdjust adjustment;
	adjustment.setPostDate(date);
	adjustment.setPostTime(QTime(0, 0, 0));
	adjustment.setMemo(tr("Open Balance"));
	adjustment.setStationId(station_id);
	adjustment.setEmployeeId(employee_id);
	adjustment.setCardId(vendor_id);
	adjustment.setStoreId(store_id);

	adjustment.accounts().push_back(AccountLine(vendor_acct, -amount));
	adjustment.accounts().push_back(AccountLine(history_id, amount));
	adjustment.cards().push_back(CardLine(vendor_id, amount));

	if (!_quasar->db()->create(adjustment)) {
	    QString message = tr("Vendor '%1' open balance failed")
		.arg(vendor.name());
	    QMessageBox::critical(this, tr("Error"), message);
	}
    }

    for (int row = 0; row < _items->rows(); ++row) {
	Id item_id = _items->cellValue(row, 0).toId();
	QString number = _items->cellValue(row, 0).toPlu().number();
	QString size = _items->cellValue(row, 2).toString();
	fixed qty = _items->cellValue(row, 3).toFixed();
	fixed cost = _items->cellValue(row, 4).toFixed();
	if (item_id == INVALID_ID || (qty == 0.0 && cost == 0.0)) continue;

	Item item;
	_quasar->db()->lookup(item_id, item);
	Id item_acct = item.assetAccount();

	ItemAdjust adjustment;
	adjustment.setPostDate(QDate::currentDate());
	adjustment.setPostTime(QTime::currentTime());
	adjustment.setMemo(tr("Open Balance"));
	adjustment.setStationId(station_id);
	adjustment.setEmployeeId(employee_id);
	adjustment.setStoreId(store_id);
	adjustment.setAccountId(history_id);

	adjustment.accounts().push_back(AccountLine(item_acct, cost));
	adjustment.accounts().push_back(AccountLine(history_id, -cost));

	ItemLine line;
	line.item_id = item.id();
	line.number = number;
	line.size = size;
	line.size_qty = item.sizeQty(size);
	line.quantity = qty;
	line.inv_cost = cost;
	adjustment.items().push_back(line);

	if (!_quasar->db()->create(adjustment)) {
	    QString message = tr("Item '%1' open balance failed")
		.arg(item.number());
	    QMessageBox::critical(this, tr("Error"), message);
	}
    }

    for (int row = 0; row < _accounts->rows(); ++row) {
	Id account_id = _accounts->cellValue(row, 0).toId();
	fixed debit = _accounts->cellValue(row, 1).toFixed();
	fixed credit = _accounts->cellValue(row, 2).toFixed();
	if (account_id == INVALID_ID || (debit == 0.0 && credit == 0))
		continue;

	Account account;
	_quasar->db()->lookup(account_id, account);

	General general;
	general.setPostDate(QDate::currentDate());
	general.setPostTime(QTime::currentTime());
	general.setMemo(tr("Open Balance"));
	general.setStationId(station_id);
	general.setEmployeeId(employee_id);
	general.setStoreId(store_id);

	if (debit != 0.0) {
	    general.accounts().push_back(AccountLine(account_id, debit));
	    general.accounts().push_back(AccountLine(history_id, -debit));
	}
	if (credit != 0.0) {
	    general.accounts().push_back(AccountLine(account_id, -credit));
	    general.accounts().push_back(AccountLine(history_id, credit));
	}

	if (!_quasar->db()->create(general)) {
	    QString message = tr("Account '%1' open balance failed")
		.arg(account.name());
	    QMessageBox::critical(this, tr("Error"), message);
	}
    }

    QApplication::restoreOverrideCursor();

    close();
}