/** Scan prodUpdate from cost changes and log them in ProdCostHistory @param $dbc [SQLManager] db connection @param $offset [optional int] start scanning from this prodUpdateID */ private function scanCostChanges($dbc, $offset = 0) { $prodUpdateQ = 'SELECT prodUpdateID FROM prodUpdate '; $args = array(); if ($offset > 0) { $prodUpdateQ .= ' WHERE prodUpdateID > ? '; $args[] = $offset; } $prodUpdateQ .= ' ORDER BY upc, modified'; $prodUpdateP = $dbc->prepare($prodUpdateQ); $prodUpdateR = $dbc->execute($prodUpdateP, $args); $chkP = $dbc->prepare("\n SELECT modified,\n cost \n FROM ProdCostHistory \n WHERE upc=?\n ORDER BY modified DESC"); $upc = null; $prevPrice = null; $update = new ProdUpdateModel($dbc); $history = new ProdCostHistoryModel($dbc); /** Go through changes to each UPC in order When encountering a new UPC, lookup previous price (if any) from prodPriceHistory Only create new entries when the prodUpdate record's price does not match the previous price. */ while ($prodUpdateW = $dbc->fetchRow($prodUpdateR)) { $update->prodUpdateID($prodUpdateW['prodUpdateID']); if (!$update->load()) { continue; } if ($upc === null || $upc != $update->upc()) { $upc = $update->upc(); $prevPrice = null; $chkR = $dbc->execute($chkP, array($upc)); if ($dbc->numRows($chkR) > 0) { $chkW = $dbc->fetch_row($chkR); $prevPrice = $chkW['cost']; } } if ($prevPrice != $update->cost()) { $history->reset(); $history->upc($upc); $history->storeID($update->storeID()); $history->modified($update->modified()); $history->cost($update->cost()); $history->uid($update->user()); $history->prodUpdateID($update->prodUpdateID()); $history->save(); $this->cronMsg('Add cost change #' . $update->prodUpdateID(), FannieLogger::INFO); } $prevPrice = $update->cost(); } }