// Update a User bool QuasarDB::update(const User& orig, User& user) { if (orig.id() == INVALID_ID || user.id() == INVALID_ID) return false; if (!validate(user)) return false; // Update the users table QString cmd = updateCmd("users", "user_id", "name,passwd,store_id," "employee_id,security_id,screen"); Stmt stmt(_connection, cmd); updateData(orig, user, stmt); stmtSetString(stmt, user.name().lower()); stmtSetString(stmt, user.password()); stmtSetId(stmt, user.defaultStore()); stmtSetId(stmt, user.defaultEmployee()); stmtSetId(stmt, user.securityType()); stmtSetString(stmt, user.screen()); stmtSetId(stmt, orig.id()); stmtSetInt(stmt, orig.version()); if (!execute(stmt)) return false; if (stmt.getUpdateCount() != 1) return error("Data has been changed by another user"); commit(); dataSignal(DataEvent::Update, orig); return true; }
// Update a Reconcile bool QuasarDB::update(const Reconcile& orig, Reconcile& reconcile) { if (orig.id() == INVALID_ID || reconcile.id() == INVALID_ID) return false; if (!validate(reconcile)) return false; // Update the reconcile table QString cmd = updateCmd("reconcile", "reconcile_id", "account_id," "stmt_balance,stmt_date,end_date,reconciled"); Stmt stmt(_connection, cmd); updateData(orig, reconcile, stmt); stmtSetId(stmt, reconcile.accountId()); stmtSetFixed(stmt, reconcile.statementBalance()); stmtSetDate(stmt, reconcile.statementDate()); stmtSetDate(stmt, reconcile.endDate()); stmtSetBool(stmt, reconcile.isReconciled()); stmtSetId(stmt, orig.id()); stmtSetInt(stmt, orig.version()); if (!execute(stmt)) return false; if (stmt.getUpdateCount() != 1) return error("Data has been changed by another user"); if (!sqlDeleteLines(orig)) return false; if (!sqlCreateLines(reconcile)) return false; commit(); dataSignal(DataEvent::Update, orig); return true; }
// Update a Recurring bool QuasarDB::update(const Recurring& orig, Recurring& recurring) { if (orig.id() == INVALID_ID || recurring.id() == INVALID_ID) return false; if (!validate(recurring)) return false; // Update the recurring table QString cmd = updateCmd("recurring", "recurring_id", "gltx_id," "description,frequency,day1,day2,max_post," "last_posted,post_count,group_id"); Stmt stmt(_connection, cmd); updateData(orig, recurring, stmt); stmtSetId(stmt, recurring.gltxId()); stmtSetString(stmt, recurring.description()); stmtSetInt(stmt, recurring.frequency()); stmtSetInt(stmt, recurring.day1()); stmtSetInt(stmt, recurring.day2()); stmtSetInt(stmt, recurring.maxPostings()); stmtSetDate(stmt, recurring.lastPosted()); stmtSetInt(stmt, recurring.postingCount()); stmtSetId(stmt, recurring.cardGroup()); stmtSetId(stmt, orig.id()); stmtSetInt(stmt, orig.version()); if (!execute(stmt)) return false; if (stmt.getUpdateCount() != 1) return error("Data has been changed by another user"); commit(); dataSignal(DataEvent::Update, orig); return true; }
// Update a Group bool QuasarDB::update(const Group& orig, Group& group) { if (orig.id() == INVALID_ID || group.id() == INVALID_ID) return false; if (!validate(group)) return false; // TODO: should type change be allowed? // Update the group table QString cmd = updateCmd("groups", "group_id", "name,description," "group_type"); Stmt stmt(_connection, cmd); updateData(orig, group, stmt); stmtSetString(stmt, group.name()); stmtSetString(stmt, group.description()); stmtSetInt(stmt, group.type()); stmtSetId(stmt, orig.id()); stmtSetInt(stmt, orig.version()); if (!execute(stmt)) return false; if (stmt.getUpdateCount() != 1) return error("Data has been changed by another user"); commit(); dataSignal(DataEvent::Update, orig); return true; }
void threadReading(void) { /* This function allow reading command received from UART. * The command frame need to start with '#' caracter followed by * the command ID (see commandID[][] table) and then a 5 caracter which represent the optional * or need parameter (unsigned short). * * If a correct command is received from UART, it will be stored inside UARTCde[] table of structure * (see updateCmd() function) * This table store NB_CMD caracters(defined in communication.h) which the UC can get. * * This function need to be called at least every 2 ms to have a good answer time. * At each call, it read the caracter present in UARTxxx.p1RX cas of the UARTxxx.RX[] buffer and test * if a command in the good format have been received (it is like a FSM). */ static char cpt=-1; static char cmdValue[8]={0,0,0,0,0,0,0,0}; static char cmdName[6]={0,0,0,0,0,0}; char myChar = 0; if (UARTUSB.p1RX != UARTUSB.p2RX) { //data received from USB myChar = UARTUSB.RX[UARTUSB.p1RX]; UARTUSB.p1RX=(UARTUSB.p1RX+1)%RX_BUFFER_SIZE; //increment the UARTxxx.p1RX pointer /* else if (UARTBlu.p1RX != UARTBlu.p2RX) { //data received from bluetooth myChar = UARTBlu.RX[UARTBlu.p1RX]; UARTBlu.p1RX=(UARTBlu.p1RX+1)%RX_BUFFER_SIZE; //increment the UARTxxx.p1RX pointer }*/ if (cpt == -1) { //if cpt == -1 then no command is currently computed if (myChar == '#') { //detection of the key car '#' cpt = 0; //inform the soft of the beginning of the command test and leavec } } else if (cpt < 9) { //the next call if(cpt<3) { //for the 3 first next caracter cmdName[cpt++]=myChar; //clone the 3 next caracter inside a dummy buffer cmdName[] and increment cpt cmdName[cpt]=0; //clear the next case of the buffer (security) } else { cmdValue[(cpt++)-3]=myChar; //clone the 5 next caracter and store it inside cmdValue[] buffer. -3 allow to cmdValue[cpt-3]=0; //store myChar in [0]...[4] first case (instead of [3]..[8]) } } else { if (myChar != '#') cpt=-1; else cpt=0; updateCmd(cmdName,cmdValue); //if cpt>=9, update command } } }
// Update a Slip bool QuasarDB::update(const Slip& orig, Slip& slip) { if (orig.id() == INVALID_ID || slip.id() == INVALID_ID) return false; if (!validate(slip)) return false; if (slip.number().stripWhiteSpace() == "#") return error("Can't change number to '#'"); // Update the slip table QString cmd = updateCmd("slip", "slip_id", "vendor_id,number," "waybill,carrier,ship_date,store_id," "inv_num,num_pieces,status,post_date"); Stmt stmt(_connection, cmd); updateData(orig, slip, stmt); stmtSetId(stmt, slip.vendorId()); stmtSetString(stmt, slip.number()); stmtSetString(stmt, slip.waybill()); stmtSetString(stmt, slip.carrier()); stmtSetDate(stmt, slip.shipDate()); stmtSetId(stmt, slip.storeId()); stmtSetString(stmt, slip.invoiceNumber()); stmtSetInt(stmt, slip.numPieces()); stmtSetString(stmt, slip.status()); stmtSetDate(stmt, slip.postDate()); stmtSetId(stmt, orig.id()); stmtSetInt(stmt, orig.version()); if (!execute(stmt)) return false; if (stmt.getUpdateCount() != 1) return error("Data has been changed by another user"); if (!sqlDeleteLines(orig)) return false; if (!sqlCreateLines(slip)) return false; commit(); dataSignal(DataEvent::Update, orig); return true; }
// Update a Gltx bool QuasarDB::update(const Gltx& orig, Gltx& gltx) { if (gltx.number() == "#") return error("Can't change number to '#'"); if (orig.dataType() != gltx.dataType()) { bool allowed = false; switch (orig.dataType()) { case DataObject::INVOICE: if (gltx.dataType() == DataObject::RETURN) allowed = true; break; case DataObject::RETURN: if (gltx.dataType() == DataObject::INVOICE) allowed = true; break; case DataObject::RECEIVE: if (gltx.dataType() == DataObject::CLAIM) allowed = true; break; case DataObject::CLAIM: if (gltx.dataType() == DataObject::RECEIVE) allowed = true; break; default: break; } if (!allowed) return error("Can't change data type"); } // Can't void or unvoid reconciled or shift posted transaction if (orig.isActive() != gltx.isActive()) { for (unsigned int i = 0; i < gltx.accounts().size(); ++i) if (!gltx.accounts()[i].cleared.isNull()) return error("Can't change reconciled transaction"); if (gltx.shiftId() != INVALID_ID) { Shift shift; lookup(gltx.shiftId(), shift); if (shift.shiftId() != INVALID_ID) return error("Can't change transaction from a posted shift"); } } // If shift changed, neither can be posted if (orig.shiftId() != gltx.shiftId()) { if (orig.shiftId() != INVALID_ID) { Shift shift; lookup(orig.shiftId(), shift); if (shift.shiftId() != INVALID_ID) return error("Can't change from a posted shift"); } if (gltx.shiftId() != INVALID_ID) { Shift shift; lookup(gltx.shiftId(), shift); if (shift.shiftId() != INVALID_ID) return error("Can't insert into a posted shift"); } } // Can't change tenders if in a posted shift if (gltx.shiftId() != INVALID_ID && orig.tenders() != gltx.tenders()) { Shift shift; lookup(gltx.shiftId(), shift); if (shift.shiftId() != INVALID_ID) return error("Can't change tenders if posted"); } // Reconcile information must match. Also copy over cleared date // when needed since it can be lost on changed transactions. for (unsigned int i = 0; i < orig.accounts().size(); ++i) { const AccountLine& line = orig.accounts()[i]; if (line.cleared.isNull()) continue; if (i >= gltx.accounts().size()) return error("Invalid change to reconciled transaction"); if (gltx.accounts()[i].account_id != line.account_id) return error("Invalid change to reconciled transaction"); if (gltx.accounts()[i].amount != line.amount) return error("Invalid change to reconciled transaction"); gltx.accounts()[i].cleared = line.cleared; } // Can't change transaction before close date Company company; lookup(company); if (!company.closeDate().isNull()) { if (gltx.postDate() <= company.closeDate()) return error("Can't change date to before close date"); if (orig.postDate() <= company.closeDate()) return error("Can't change transaction before close date"); } if (!company.startOfYear().isNull()) { if (gltx.postDate() < company.startOfYear()) return error("Can't change date into last year"); if (orig.postDate() < company.startOfYear()) return error("Can't change transaction in last year"); } // Verify station/employee are specified if (company.shiftMethod() == Company::BY_STATION) { if (gltx.stationId() == INVALID_ID) { // Error if its a transaction type involving cashrec switch (gltx.dataType()) { case DataObject::INVOICE: case DataObject::RECEIPT: case DataObject::NOSALE: case DataObject::PAYOUT: case DataObject::RETURN: case DataObject::SHIFT: case DataObject::TEND_ADJUST: case DataObject::WITHDRAW: return error("A station is required"); default: break; } } } if (company.shiftMethod() == Company::BY_EMPLOYEE) { if (gltx.employeeId() == INVALID_ID) { // Error if its a transaction type involving cashrec switch (gltx.dataType()) { case DataObject::INVOICE: case DataObject::RECEIPT: case DataObject::NOSALE: case DataObject::PAYOUT: case DataObject::RETURN: case DataObject::SHIFT: case DataObject::TEND_ADJUST: case DataObject::WITHDRAW: return error("An employee is required"); default: break; } } } // Check that change data exists and create if needed checkAccount(gltx); checkCard(gltx); checkItem(gltx); // NOTE: this doesn't include paid and shouldn't QString cmd = updateCmd("gltx", "gltx_id", "number,reference_str," "post_date,post_time,memo,station_id," "employee_id,card_id,store_id,shift_id," "link_id,printed,amount,data_type"); Stmt stmt(_connection, cmd); updateData(orig, gltx, stmt); stmtSetString(stmt, gltx.number()); stmtSetString(stmt, gltx.reference()); stmtSetDate(stmt, gltx.postDate()); stmtSetTime(stmt, gltx.postTime()); stmtSetString(stmt, gltx.memo()); stmtSetId(stmt, gltx.stationId()); stmtSetId(stmt, gltx.employeeId()); stmtSetId(stmt, gltx.cardId()); stmtSetId(stmt, gltx.storeId()); stmtSetId(stmt, gltx.shiftId()); stmtSetId(stmt, gltx.linkId()); stmtSetBool(stmt, gltx.printed()); stmtSetFixed(stmt, gltx.total()); stmtSetInt(stmt, gltx.dataType()); stmtSetId(stmt, orig.id()); stmtSetInt(stmt, orig.version()); execute(stmt); if (stmt.getUpdateCount() != 1) return error("Data has been changed by another user"); sqlDeleteLines(orig); sqlCreateLines(gltx); return true; }