/** Do whatever the service is supposed to do. Should override this. @param $args array of data @return an array of data */ public function run($args = array()) { $ret = array(); if (!property_exists($args, 'type')) { // missing required arguments $ret['error'] = array('code' => -32602, 'message' => 'Invalid parameters needs type'); return $ret; } // validate additional arguments switch (strtolower($args->type)) { case 'vendor': if (!property_exists($args, 'vendor_id')) { // vendor ID required $ret['error'] = array('code' => -32602, 'message' => 'Invalid parameters needs vendor_id'); return $ret; } elseif (!property_exists($args, 'sku') && !property_exists($args, 'upc')) { // either sku or upc is required $ret['error'] = array('code' => -32602, 'message' => 'Invalid parameters needs sku or upc'); return $ret; } break; default: // unknown type argument $ret['error'] = array('code' => -32602, 'message' => 'Invalid parameters'); return $ret; } // lookup results $dbc = \FannieDB::getReadOnly(\FannieConfig::factory()->get('OP_DB')); switch (strtolower($args->type)) { case 'vendor': $vendor = new \VendorItemsModel($dbc); $vendor->vendorID($args->vendor_id); if (property_exists($args, 'sku')) { $vendor->sku($args->sku); } elseif (property_exists($args, 'upc')) { $vendor->upc($args->upc); } foreach ($vendor->find() as $v) { $ret['sku'] = $v->sku(); $ret['upc'] = $v->upc(); $ret['size'] = $v->size(); $ret['units'] = $v->units(); $ret['brand'] = $v->brand(); $ret['description'] = $v->description(); $ret['cost'] = $v->cost(); break; } return $ret; } }
public function get_id_break_handler() { $dbc = FannieDB::get($this->config->get('OP_DB')); $model = new VendorBreakdownsModel($dbc); $model->vendorID($this->id); $original = new VendorItemsModel($dbc); $product = new ProductsModel($dbc); foreach ($model->find() as $obj) { $original->vendorID($this->id); $original->sku($obj->sku()); if (!$original->load()) { $this->addOnloadCommand("showBootstrapAlert('#alert-area', 'danger', 'Vendor SKU #" . $obj->sku() . " not found');\n"); continue; } $split_factor = false; $unit_size = ''; if (preg_match('/^\\d+$/', $original->size())) { $split_factor = $original->size(); } elseif (preg_match('/(\\d+)\\s*\\/\\s*(.+)/', $original->size(), $matches)) { $split_factor = $matches[1]; $unit_size = $matches[2]; } elseif (preg_match('/(\\d+)\\s*CT/', $original->size(), $matches)) { $split_factor = $matches[1]; } elseif (preg_match('/(\\d+)\\s*PKT/', $original->size(), $matches)) { $split_factor = $matches[1]; } if (!$split_factor) { $this->addOnloadCommand("showBootstrapAlert('#alert-area', 'danger', 'Vendor SKU #" . $original->size() . " cannot be broken down');\n"); continue; } // add an entry using the store UPC/PLU in place of the vendor SKU // since two records from the same vendor with same SKU are not // permitted in the table $original->sku($obj->upc()); $original->upc($obj->upc()); $original->units(1); $original->size($unit_size); $original->cost($original->cost() / $split_factor); $original->saleCost($original->saleCost() / $split_factor); if ($original->save()) { // update cost in products table, too $product->reset(); $product->upc($obj->upc()); foreach ($product->find('store_id') as $p) { if ($p->load() && $p->default_vendor_id() == $this->id) { $p->cost($original->cost()); $p->save(); $original->description($p->description()); $original->save(); } } } else { $this->addOnloadCommand("showBootstrapAlert('#alert-area', 'success', 'Error saving vendor SKU #" . $obj->sku() . "');\n"); } } return true; }
public function get_id_view() { global $FANNIE_OP_DB; $dbc = FannieDB::get($FANNIE_OP_DB); $items = new VendorItemsModel($dbc); $items->vendorID($this->id); $ret = '<div id="alert-area"></div> <table class="table tablesorter">'; $ret .= '<thead><tr> <th>Vendor SKU</th> <th>Our UPC</th> <th>Brand</th> <th>Description</th> <th>Unit Size</th> <th>Case Qty</th> <th>Unit Cost</th> </tr></thead>'; $ret .= '<tbody>'; foreach ($items->find() as $item) { $ret .= sprintf('<tr> <input type="hidden" class="original-sku" name="originalSKU" value="%s" /> <td><span class="collapse">%s</span> <input type="text" class="form-control input-sm sku-field" name="editSKU" value="%s" size="13" /></td> <td><a href="../ItemEditorPage.php?searchupc=%s" target="_edit%s">%s</a></td> <td><span class="collapse">%s</span> <input type="text" class="form-control input-sm editable" name="brand" value="%s" /></td> <td><span class="collapse">%s</span> <input type="text" class="form-control input-sm editable" name="description" value="%s" /></td> <td><span class="collapse">%s</span> <input type="text" class="form-control input-sm editable" name="unitSize" value="%s" size="5" /></td> <td><span class="collapse">%s</span> <input type="text" class="form-control input-sm editable" name="caseQty" value="%.2f" size="5" /></td> <td><span class="collapse">%s</span> <input type="text" class="form-control input-sm costing" name="unitCost" value="%.2f" size="5" /></td> </tr>', $item->sku(), $item->sku(), $item->sku(), $item->upc(), $item->upc(), $item->upc(), $item->brand(), $item->brand(), $item->description(), $item->description(), $item->size(), $item->size(), $item->units(), $item->units(), $item->cost(), $item->cost()); } $ret .= '</tbody></table>'; $ret .= '<input type="hidden" id="vendor-id" value="' . $this->id . '" />'; $ret .= '<p><a href="VendorIndexPage.php?vid=' . $this->id . '" class="btn btn-default">Home</a></p>'; $this->add_onload_command('itemEditing();'); $this->add_script('../../src/javascript/tablesorter/jquery.tablesorter.js'); $this->addCssFile('../../src/javascript/tablesorter/themes/blue/style.css'); $this->add_onload_command("\$('.tablesorter').tablesorter({sortList:[[0,0]], widgets:['zebra']});"); return $ret; }
public function post_vendorID_invoice_num_po_num_invoice_date_handler() { $dbc = FannieDB::get($this->config->get('OP_DB')); $skus = FormLib::get('sku', array()); $upcs = FormLib::get('upc', array()); $descriptions = FormLib::get('desc', array()); $cases = FormLib::get('qty', array()); $units = FormLib::get('units', array()); $sizes = FormLib::get('size', array()); $costs = FormLib::get('unitCost', array()); $totals = FormLib::get('totalCost', array()); $order = new PurchaseOrderModel($dbc); $order->vendorID($this->vendorID); $order->creationDate($this->invoice_date); $order->placed(1); $order->placedDate($this->invoice_date); $order->vendorOrderID($this->po_num); $order->vendorInvoiceID($this->invoice_num); $order->userID(0); $orderID = $order->save(); $checkP = $dbc->prepare(' SELECT v.sku FROM vendorItems AS v WHERE v.vendorID=? AND v.upc <> \'0000000000000\' AND v.upc <> \'\' AND v.upc IS NOT NULL AND v.sku=?'); $vendorItem = new VendorItemsModel($dbc); $item = new PurchaseOrderItemsModel($dbc); for ($i = 0; $i < count($skus); $i++) { $sku = $skus[$i]; $upc = BarcodeLib::padUPC(isset($upcs[$i]) ? $upcs[$i] : ''); $qty = isset($cases[$i]) ? $cases[$i] : 1; $caseSize = isset($units[$i]) ? $units[$i] : 1; $unitSize = isset($sizes[$i]) ? $sizes[$i] : ''; $unitCost = isset($costs[$i]) ? $costs[$i] : 0; $totalCost = isset($totals[$i]) ? $totals[$i] : 0; $desc = isset($descriptions[$i]) ? substr($descriptions[$i], 0, 50) : ''; $item->reset(); $item->orderID($orderID); $item->sku($sku); $item->quantity($qty); $item->unitCost($unitCost); $item->caseSize($caseSize); $item->receivedDate($this->invoice_date); $item->receivedQty($qty); $item->receivedTotalCost($totalCost); $item->unitSize($unitSize); $item->brand(''); $item->description($desc); $item->internalUPC($upc); $item->save(); /** Add entries to vendor catalog if they don't exist */ $checkR = $dbc->execute($checkP, array($this->vendorID, $sku)); if ($checkR && $dbc->numRows($checkR) == 0) { $vendorItem->vendorID($this->vendorID); $vendorItem->sku($sku); $vendorItem->upc($upc); $vendorItem->description($desc); $vendorItem->brand(''); $vendorItem->units($caseSize); $vendorItem->size($unitSize); $vendorItem->cost($unitCost); $vendorItem->vendorDept(0); $vendorItem->save(); } } header('Location: ' . filter_input(INPUT_SERVER, 'PHP_SELF') . '?complete=' . $orderID); return false; }
function ajax_response() { global $FANNIE_OP_DB; $dbc = FannieDB::get($FANNIE_OP_DB); switch (FormLib::get_form_value('ajax')) { case 'save': $upc = FormLib::get_form_value('upc'); $store_id = FormLib::get('store_id'); $upc = BarcodeLib::padUPC($upc); $values = array(); $model = new ProductsModel($dbc); $model->upc($upc); $model->store_id($store_id); $brand = FormLib::get('brand'); if ($brand !== '') { $model->brand($brand); } $desc = FormLib::get_form_value('desc'); if ($desc !== '') { $model->description($desc); } $dept = FormLib::get_form_value('dept'); if ($dept !== '') { $model->department($dept); } $price = rtrim(FormLib::get_form_value('price'), ' '); if ($price !== '') { $model->normal_price($price); } $cost = rtrim(FormLib::get_form_value('cost'), ' '); if ($cost !== '') { $model->cost($cost); } $tax = FormLib::get_form_value('tax'); if ($tax !== '') { $model->tax($tax); } $fsx = FormLib::get_form_value('fs'); if ($fsx !== '') { $model->foodstamp($fsx); } $disc = FormLib::get_form_value('disc'); if ($disc !== '') { $model->discount($disc); } $wgt = FormLib::get_form_value('wgt'); if ($wgt !== '') { $model->scale($wgt); } $loc = FormLib::get_form_value('local'); if ($loc !== '') { $model->local($loc); } $supplier = FormLib::get_form_value('supplier'); /** Normalize free-form supplier text Look up corresponding vendor ID */ $vendorID = ''; $vendors = new VendorsModel($dbc); $vendors->vendorName($supplier); foreach ($vendors->find() as $obj) { $vendorID = $obj->vendorID(); break; } if ($vendorID !== '') { $model->default_vendor_id($vendorID); } $model->save(); $chkP = $dbc->prepare('SELECT upc FROM prodExtra WHERE upc=?'); $chkR = $dbc->execute($chkP, array($upc)); if ($dbc->num_rows($chkR) > 0) { $extraP = $dbc->prepare_statement('UPDATE prodExtra SET manufacturer=?, distributor=? WHERE upc=?'); $dbc->exec_statement($extraP, array($brand, $supplier, $upc)); } else { $extraP = $dbc->prepare('INSERT INTO prodExtra (upc, variable_pricing, margin, manufacturer, distributor) VALUES (?, 0, 0, ?, ?)'); $dbc->execute($extraP, array($upc, $brand, $supplier)); } if ($vendorID !== '') { $item = new VendorItemsModel($dbc); $item->createIfMissing($upc, $vendorID); $item->updateCostByUPC($upc, $cost, $vendorID); } updateProductAllLanes($upc); break; case 'deleteCheck': $upc = FormLib::get_form_value('upc'); $upc = BarcodeLib::padUPC($upc); $encoded_desc = FormLib::get_form_value('desc'); $desc = base64_decode($encoded_desc); $fetchP = $dbc->prepare_statement("select normal_price,\n special_price,t.description,\n case when foodstamp = 1 then 'Yes' else 'No' end as fs,\n case when scale = 1 then 'Yes' else 'No' end as s\n from products as p left join taxrates as t\n on p.tax = t.id\n where upc=? and p.description=?"); $fetchR = $dbc->exec_statement($fetchP, array($upc, $desc)); $fetchW = $dbc->fetch_array($fetchR); $ret = "Delete item {$upc} - {$desc}?\n"; $ret .= "Normal price: " . rtrim($fetchW[0]) . "\n"; $ret .= "Sale price: " . rtrim($fetchW[1]) . "\n"; $ret .= "Tax: " . rtrim($fetchW[2]) . "\n"; $ret .= "Foodstamp: " . rtrim($fetchW[3]) . "\n"; $ret .= "Scale: " . rtrim($fetchW[4]) . "\n"; $json = array('alertBox' => $ret, 'upc' => ltrim($upc, '0'), 'enc_desc' => $encoded_desc); echo json_encode($json); break; case 'doDelete': $upc = FormLib::get_form_value('upc'); $upc = BarcodeLib::padUPC($upc); $desc = base64_decode(FormLib::get_form_value('desc')); $update = new ProdUpdateModel($dbc); $update->upc($upc); $update->logUpdate(ProdUpdateModel::UPDATE_DELETE); $model = new ProductsModel($dbc); $model->upc($upc); $model->delete(); $model = new ProductUserModel($dbc); $model->upc($upc); $model->delete(); $model = new ScaleItemsModel($dbc); $model->plu($upc); $model->delete(); $delP = $dbc->prepare_statement("delete from prodExtra where upc=?"); $delXR = $dbc->exec_statement($delP, array($upc)); $delP = $dbc->prepare_statement("DELETE FROM upcLike WHERE upc=?"); $delR = $dbc->exec_statement($delP, array($upc)); deleteProductAllLanes($upc); break; default: echo 'Unknown Action'; break; } }
private function autoPopulate($vendorID) { global $FANNIE_OP_DB; $dbc = FannieDB::get($FANNIE_OP_DB); $query = ' SELECT p.upc, p.upc AS sku, p.brand, p.description, p.size, p.unitofmeasure, p.cost, 0.00 AS saleCost, 0 AS vendorDept FROM products AS p INNER JOIN vendors AS v ON p.default_vendor_id=v.vendorID WHERE v.vendorID=? AND p.upc NOT IN ( SELECT upc FROM vendorItems WHERE vendorID=? ) AND p.upc NOT IN ( SELECT upc FROM vendorSKUtoPLU WHERE vendorID=? )'; $prep = $dbc->prepare($query); $args = array($vendorID, $vendorID, $vendorID); $result = $dbc->execute($prep, $args); $item = new VendorItemsModel($dbc); while ($row = $dbc->fetch_row($result)) { $item->vendorID($vendorID); $item->upc($row['upc']); $item->sku($row['sku']); $item->brand($row['brand']); $item->description($row['description']); $item->units(1); $item->size($row['size'] . $row['unitofmeasure']); $item->cost($row['cost']); $item->saleCost(0); $item->vendorDept(0); $item->save(); } }
function process_file($linedata) { global $FANNIE_OP_DB; $dbc = FannieDB::get($FANNIE_OP_DB); $skuCol = $this->get_column_index('sku'); $costCol = $this->get_column_index('cost'); $uQtyCol = $this->get_column_index('unitQty'); $cQtyCol = $this->get_column_index('caseQty'); $uSizeCol = $this->get_column_index('unitSize'); $cSizeCol = $this->get_column_index('caseSize'); $brandCol = $this->get_column_index('brand'); $descCol = $this->get_column_index('desc'); $upcCol = $this->get_column_index('upc'); $upccCol = $this->get_column_index('upcc'); $vendorID = FormLib::get('vendorID'); $inv = FormLib::get('identifier', ''); $orderDate = FormLib::get('orderDate', date('Y-m-d H:i:s')); $recvDate = FormLib::get('recvDate', ''); $order = new PurchaseOrderModel($dbc); $order->vendorID($vendorID); $order->creationDate($orderDate); $order->placedDate($orderDate); $order->placed(1); $order->userID(FannieAuth::getUID()); $order->vendorOrderID($inv); $order->vendorInvoiceID($inv); $orderID = $order->save(); $item = new PurchaseOrderItemsModel($dbc); $info = new VendorItemsModel($dbc); $ret = ''; foreach ($linedata as $line) { if (!isset($line[$skuCol])) { continue; } if (!isset($line[$costCol])) { continue; } $sku = $line[$skuCol]; $cost = $line[$costCol]; $cost = trim($cost, ' '); $cost = trim($cost, '$'); if (!is_numeric($cost)) { $ret .= "<i>Omitting item {$sku}. Cost {$cost} isn't a number</i><br />"; continue; } $unitQty = $uQtyCol !== false && isset($line[$uQtyCol]) ? $line[$uQtyCol] : 0; $caseQty = $cQtyCol !== false && isset($line[$cQtyCol]) ? $line[$cQtyCol] : 0; if ($unitQty == 0 && $caseQty == 0) { // no qty specified. continue; } $unitSize = $uSizeCol !== false && isset($line[$uSizeCol]) ? $line[$uSizeCol] : 0; $caseSize = $cSizeCol !== false && isset($line[$cSizeCol]) ? $line[$cSizeCol] : 0; $brand = $brandCol !== '' && isset($line[$brandCol]) ? $line[$brandCol] : ''; $desc = $descCol !== false && isset($line[$descCol]) ? $line[$descCol] : ''; $upc = ''; if ($upcCol !== false && isset($line[$upcCol])) { $upc = BarcodeLib::padUPC($line[$upcCol]); } elseif ($upccCol !== false && isset($line[$upccCol])) { $upc = BarcodeLib::padUPC($line[$upccCol]); $upc = '0' . substr($upc, 0, 12); } $info->reset(); $info->vendorID($vendorID); $info->sku($sku); if ($info->load()) { if ($brand === '') { $brand = $info->brand(); } if ($desc === '') { $desc = $info->description(); } if ($unitSize === 0) { $unitSize = $info->size(); } if ($caseSize === 0) { $caseSize = $info->units(); } $upc = $info->upc(); } if ($caseQty == 0 && $unitQty != 0) { if ($caseSize == 0) { $caseQty = $unitQty; $caseSize = 1; } else { $caseQty = $unitQty / $caseSize; } } elseif ($caseQty != 0 && $unitQty == 0) { if ($caseSize == 0) { $unitQty = $caseQty; $caseSize = 1; } else { $unitQty = $caseQty * $caseSize; } } elseif ($caseQty != 0 && $unitQty != 0) { if ($caseSize == 0) { $caseSize = $caseQty / $unitQty; } } $unitCost = $cost / $unitQty; $item->orderID($orderID); $item->sku($sku); if ($item->load()) { // multiple records for same item $item->quantity($caseQty + $item->quantity()); if ($recvDate !== '') { $item->receivedTotalCost($cost + $item->receivedTotalCost()); $item->receivedQty($caseQty + $item->receivedQty()); $item->receivedDate($recvDate); } } else { $item->quantity($caseQty); if ($recvDate !== '') { $item->receivedTotalCost($cost); $item->receivedQty($caseQty); $item->receivedDate($recvDate); } } $item->unitCost($unitCost); $item->caseSize($caseSize); $item->brand($brand); $item->description($desc); $item->internalUPC($upc); $item->save(); } $ret .= "<p>Import Complete"; $ret .= '<br />'; $ret .= '<a href="' . $this->config->get('URL') . 'purchasing/ViewPurchaseOrders.php?id=' . $orderID . '">View Order</a></p>'; $this->results = $ret; return true; }
public function preprocess() { global $FANNIE_PLUGIN_SETTINGS, $FANNIE_OP_DB, $FANNIE_URL, $FANNIE_TRANS_DB; //$this->add_script($FANNIE_URL.'src/javascript/jquery.js'); //$this->add_script($FANNIE_URL.'src/javascript/jquery-ui.js'); if (FormLib::get('upc_in') !== '') { $upc = BarcodeLib::padUPC(FormLib::get('upc_in')); $this->current_item_data['upc'] = $upc; $dbc = FannieDB::get($FANNIE_OP_DB); $model = new ProductsModel($dbc); $model->upc($upc); $vendorID = 0; if ($model->load()) { $this->current_item_data['desc'] = $model->brand() . ' ' . $model->description(); $this->current_item_data['par'] = $model->auto_par(); $vendorID = $model->default_vendor_id(); } $model = new VendorsModel($dbc); $model->vendorID($vendorID); if ($model->load()) { $this->current_item_data['vendor'] = $model->vendorName(); $schedule = new VendorDeliveriesModel($dbc); $schedule->vendorID($vendorID); if ($schedule->load() && $schedule->regular() == 1) { $this->current_item_data['nextDelivery'] = date('D, M jS', strtotime($schedule->nextDelivery())) . ' & ' . date('D, M jS', strtotime($schedule->nextNextDelivery())); $nd = new DateTime(date('Y-m-d', strtotime($schedule->nextDelivery()))); $nnd = new DateTime(date('Y-m-d', strtotime($schedule->nextNextDelivery()))); $this->current_item_data['deliverySpan'] = $nnd->diff($nd)->format('%a'); } $items = new VendorItemsModel($dbc); $items->vendorID($vendorID); $items->upc($upc); $this->current_item_data['cases'] = array(); foreach ($items->find('units') as $item) { $this->current_item_data['cases'][] = $item->units(); } } $saleNow = 'SELECT b.batchName, b.startDate, b.endDate FROM batchList AS l INNER JOIN batches AS b ON b.batchID=l.batchID WHERE l.upc=? AND b.discounttype <> 0 AND b.startDate <= ' . $dbc->now() . ' AND b.endDate >= ' . $dbc->now(); $saleNow = $dbc->prepare($saleNow); $saleNow = $dbc->execute($saleNow, array($upc)); if ($dbc->num_rows($saleNow) > 0) { $row = $dbc->fetch_row($saleNow); $this->current_item_data['onSale'] = $row['batchName'] . ' thru ' . date('D, M jS', strtotime($row['endDate'])); } $saleNext = 'SELECT b.batchName, b.startDate, b.endDate FROM batchList AS l INNER JOIN batches AS b ON b.batchID=l.batchID WHERE l.upc=? AND b.discounttype <> 0 AND b.startDate >= ' . $dbc->now() . ' AND b.endDate >= ' . $dbc->now(); $saleNext = $dbc->prepare($saleNext); $saleNext = $dbc->execute($saleNext, array($upc)); if ($dbc->num_rows($saleNext) > 0) { $row = $dbc->fetch_row($saleNext); $this->current_item_data['soonSale'] = $row['batchName'] . ' on ' . date('D, M jS', strtotime($row['startDate'])); } $ordersQ = 'SELECT v.vendorName, o.placedDate, i.quantity, i.caseSize FROM PurchaseOrderItems AS i INNER JOIN PurchaseOrder AS o ON i.orderID=o.orderID INNER JOIN vendors AS v ON o.vendorID=v.vendorID WHERE i.internalUPC = ? ORDER BY o.placedDate DESC'; $ordersQ = $dbc->add_select_limit($ordersQ, 10); $ordersP = $dbc->prepare($ordersQ); $ordersR = $dbc->execute($ordersP, array($upc)); $orders = array(); while ($w = $dbc->fetch_row($ordersR)) { $orders[] = $w; } $this->current_item_data['orders'] = $orders; $salesQ = 'SELECT ' . DTrans::sumQuantity('d') . ' AS qty, MIN(tdate) as day FROM ' . $FANNIE_TRANS_DB . $dbc->sep() . 'dlog_15 AS d WHERE upc = ? GROUP BY YEAR(tdate), MONTH(tdate), DAY(tdate) ORDER BY YEAR(tdate) DESC, MONTH(tdate) DESC, DAY(tdate) DESC'; $salesP = $dbc->prepare($salesQ); $salesR = $dbc->execute($salesP, array($upc)); $sales = array(); while ($w = $dbc->fetch_row($salesR)) { $sales[] = $w; } $this->current_item_data['sales'] = $sales; } $this->linea_ios_mode = $this->linea_support_available(); if ($this->linea_ios_mode) { $this->add_script($FANNIE_URL . 'src/javascript/linea/cordova-2.2.0.js'); $this->add_script($FANNIE_URL . 'src/javascript/linea/ScannerLib-Linea-2.0.0.js'); } $this->add_script($FANNIE_URL . 'src/javascript/tablesorter/jquery.tablesorter.js'); $this->add_css_file($FANNIE_URL . 'src/javascript/tablesorter/themes/blue/style.css'); //$this->add_css_file($FANNIE_URL.'src/javascript/jquery-ui.css'); return true; }
/** Helper: update vendor costs when updating a product cost if the product has a defined vendor */ public function updateCostByUPC($upc, $cost, $vendorID) { $updateP = $this->connection->prepare(' UPDATE vendorItems SET cost=?, modified=' . $this->connection->now() . ' WHERE vendorID=? AND sku=?'); $skuModel = new VendorSKUtoPLUModel($this->connection); $skuModel->vendorID($vendorID); $skuModel->upc($upc); foreach ($skuModel->find() as $obj) { $this->connection->execute($updateP, array($cost, $vendorID, $obj->sku())); } $vModel = new VendorItemsModel($this->connection); $vModel->vendorID($vendorID); $vModel->upc($upc); foreach ($vModel->find() as $obj) { $this->connection->execute($updateP, array($cost, $vendorID, $obj->sku())); } }
private static function parseItem($line, $vendorID) { global $FANNIE_OP_DB; $UPC = 3; $SKU = 4; $RECVQTY = 5; $CASESIZE = 6; $UNITSIZE = 7; $BRAND = 8; $DESCRIPTION = 9; $TOTALCOST = 13; $UNITCOST = 15; $ORDERQTY = 18; // remove non-digits and check digits // then pad to length $upc = str_replace('-', '', $line[$UPC]); $upc = str_replace(' ', '', $upc); $upc = substr($upc, 0, strlen($upc) - 1); $upc = BarcodeLib::padUPC($upc); $caseSize = $line[$CASESIZE]; // invoice does not include proper case size // try to find actual size in vendorItems table // via SKU or UPC if (strtoupper($caseSize) == 'CS') { $dbc = FannieDB::get($FANNIE_OP_DB); $vmodel = new VendorItemsModel($dbc); $vmodel->sku($line[$SKU]); $vmodel->vendorID($vendorID); $vmodel->load(); if ($vmodel->units() != '') { $caseSize = $vmodel->units(); } else { $vmodel->reset(); $vmodel->upc($upc); $vmodel->vendorID($vendorID); foreach ($vmodel->find() as $item) { if ($item->units() != '') { $caseSize = $item->units(); } } } } // never found a valid size if (!is_numeric($caseSize)) { $caseSize = 1; } return array('sku' => $line[$SKU], 'quantity' => $line[$ORDERQTY], 'unitCost' => $line[$UNITCOST], 'caseSize' => $caseSize, 'receivedQty' => $line[$RECVQTY], 'receivedTotalCost' => $line[$TOTALCOST], 'unitSize' => $line[$UNITSIZE], 'brand' => $line[$BRAND], 'description' => $line[$DESCRIPTION], 'upc' => $upc); }
public function post_id_handler() { $dbc = FannieDB::get($this->config->get('OP_DB')); $ret = array('error' => false); $date = FormLib::get('order-date', date('Y-m-d')); $po_num = FormLib::get('po-number'); $inv_num = FormLib::get('inv-number'); $sku = FormLib::get('sku', array()); $upc = FormLib::get('upc', array()); $cases = FormLib::get('cases', array()); $caseSize = FormLib::get('case-size', array()); $total = FormLib::get('total', array()); $brand = FormLib::get('brand', array()); $description = FormLib::get('description', array()); if (count($sku) == 0) { $ret['error'] = true; $ret['message'] = 'Order must have at least one item'; echo json_encode($ret); return false; } /** Queries to check for vendorItems entries */ $skuP = $dbc->prepare(' SELECT size FROM vendorItems WHERE vendorID=? AND sku=?'); $upcP = $dbc->prepare(' SELECT size FROM vendorItems WHERE vendorID=? AND upc=?'); $vitem = new VendorItemsModel($dbc); /** Create parent record for the order */ $po = new PurchaseOrderModel($dbc); $po->vendorID($this->id); $po->creationDate($date); $po->placed(1); $po->placedDate($date); $po->userID(FannieAuth::getUID()); $po->vendorOrderID($po_num); $po->vendorInvoiceID($inv_num); // if an orderID is supplied, update the existing order if (FormLib::get('order-id') !== '' && is_numeric(FormLib::get('order-id'))) { $orderID = FormLib::get('order-id'); $po->orderID($orderID); $po->save(); } else { $orderID = $po->save(); } if (!$orderID) { $ret['error'] = true; $ret['message'] = 'Could not create new order'; echo json_encode($ret); return false; } /** Create item records for the order */ $pitem = new PurchaseOrderItemsModel($dbc); for ($i = 0; $i < count($sku); $i++) { $pitem->reset(); $pitem->orderID($orderID); $pitem->sku($sku[$i]); $units = $caseSize[$i]; $qty = $cases[$i]; $unitCost = $total[$i] / $qty / $units; /** Multiple same-SKU records Sum the quantities and costs to merge into a single record */ if ($pitem->load()) { $qty += $pitem->receivedQty(); $total[$i] += $pitem->receivedTotalCost(); } $pitem->quantity($qty); $pitem->caseSize($units); $pitem->unitSize(''); $pitem->unitCost($unitCost); $pitem->receivedDate($date); $pitem->receivedQty($qty); $pitem->receivedTotalCost($total[$i]); $pitem->brand($brand[$i]); $pitem->description($description[$i]); $pitem->internalUPC($upc[$i]); /** Try to look up unit size using vendorID+sku or vendorID+upc. This avoids making unit size a required field *and* checks for an existing vendorItems record */ $size = false; $skuR = $dbc->execute($skuP, array($this->id, $sku[$i])); if ($skuR && $dbc->numRows($skuR)) { $size = true; $w = $dbc->fetchRow($skuR); $pitem->unitSize($w['size']); } if ($size === false) { $upcR = $dbc->execute($upcP, array($this->id, $upc[$i])); if ($upcR && $dbc->numRows($upcR)) { $size = true; $w = $dbc->fetchRow($upcR); $pitem->unitSize($w['size']); } } $pitem->save(); /** If no vendorItems record exists for this SKU or UPC then create one */ if ($size === false) { $vitem->reset(); $vitem->vendorID($this->id); $vitem->sku($sku[$i]); $vitem->upc($upc[$i]); $vitem->brand($brand[$i]); $vitem->description($description[$i]); $vitem->size(''); $vitem->units($qty); $vitem->cost($unitCost); $vitem->saleCost(0.0); $vitem->vendorDept(0); $vitem->save(); } } $ret['order_id'] = $orderID; echo json_encode($ret); return false; }
function SaveFormData($upc) { $FANNIE_PRODUCT_MODULES = FannieConfig::config('PRODUCT_MODULES', array()); $upc = BarcodeLib::padUPC($upc); $dbc = $this->db(); $model = new ProductsModel($dbc); $model->upc($upc); if (!$model->load()) { // fully init new record $model->special_price(0); $model->specialpricemethod(0); $model->specialquantity(0); $model->specialgroupprice(0); $model->advertised(0); $model->tareweight(0); $model->start_date('0000-00-00'); $model->end_date('0000-00-00'); $model->discounttype(0); $model->wicable(0); $model->scaleprice(0); $model->inUse(1); } $stores = FormLib::get('store_id', array()); for ($i = 0; $i < count($stores); $i++) { $model->store_id($stores[$i]); $taxes = FormLib::get('tax'); if (isset($taxes[$i])) { $model->tax($taxes[$i]); } $fs = FormLib::get('FS', array()); if (in_array($stores[$i], $fs)) { $model->foodstamp(1); } else { $model->foodstamp(0); } $scale = FormLib::get('Scale', array()); if (in_array($stores[$i], $scale)) { $model->scale(1); } else { $model->scale(0); } $qtyFrc = FormLib::get('QtyFrc', array()); if (in_array($stores[$i], $qtyFrc)) { $model->qttyEnforced(1); } else { $model->qttyEnforced(0); } $wic = FormLib::get('prod-wicable', array()); if (in_array($stores[$i], $wic)) { $model->wicable(1); } else { $model->wicable(0); } $discount_setting = FormLib::get('discount'); if (isset($discount_setting[$i])) { switch ($discount_setting[$i]) { case 0: $model->discount(0); $model->line_item_discountable(0); break; case 1: $model->discount(1); $model->line_item_discountable(1); break; case 2: $model->discount(1); $model->line_item_discountable(0); break; case 3: $model->discount(0); $model->line_item_discountable(1); break; } } $price = FormLib::get('price'); if (isset($price[$i])) { $model->normal_price($price[$i]); } $cost = FormLib::get('cost'); if (isset($cost[$i])) { $model->cost($cost[$i]); } $desc = FormLib::get('descript'); if (isset($desc[$i])) { $model->description(str_replace("'", '', $desc[$i])); } $brand = FormLib::get('manufacturer'); if (isset($brand[$i])) { $model->brand(str_replace("'", '', $brand[$i])); } $model->pricemethod(0); $model->groupprice(0.0); $model->quantity(0); $dept = FormLib::get('department'); if (isset($dept[$i])) { $model->department($dept[$i]); } $size = FormLib::get('size'); if (isset($size[$i])) { $model->size($size[$i]); } $model->modified(date('Y-m-d H:i:s')); $unit = FormLib::get('unitm'); if (isset($unit[$i])) { $model->unitofmeasure($unit[$i]); } $subdept = FormLib::get('subdept'); if (isset($subdept[$i])) { $model->subdept($subdept[$i]); } // lookup vendorID by name $vendorID = 0; $v_input = FormLib::get('distributor'); if (isset($v_input[$i])) { $vendor = new VendorsModel($dbc); $vendor->vendorName($v_input[$i]); foreach ($vendor->find('vendorID') as $obj) { $vendorID = $obj->vendorID(); break; } } $model->default_vendor_id($vendorID); $inUse = FormLib::get('prod-in-use', array()); if (in_array($stores[$i], $inUse)) { $model->inUse(1); } else { $model->inUse(0); } $idEnf = FormLib::get('id-enforced', array()); if (isset($idEnf[$i])) { $model->idEnforced($idEnf[$i]); } $local = FormLib::get('prod-local'); if (isset($local[$i])) { $model->local($local[$i]); } $deposit = FormLib::get('deposit-upc'); if (isset($deposit[$i])) { if ($deposit[$i] == '') { $deposit[$i] = 0; } $model->deposit($deposit[$i]); } /* products.formatted_name is intended to be maintained automatically. * Get all enabled plugins and standard modules of the base. * Run plugins first, then standard modules. */ $formatters = FannieAPI::ListModules('ProductNameFormatter'); $fmt_name = ""; $fn_params = array('index' => $i); foreach ($formatters as $formatter_name) { $formatter = new $formatter_name(); $fmt_name = $formatter->compose($fn_params); if (isset($formatter->this_mod_only) && $formatter->this_mod_only) { break; } } $model->formatted_name($fmt_name); $model->save(); } /** If a vendor is selected, intialize a vendorItems record */ if ($vendorID != 0) { $vitem = new VendorItemsModel($dbc); $vitem->vendorID($vendorID); $vitem->upc($upc); $sku = FormLib::get('vendorSKU'); if (empty($sku)) { $sku = $upc; } else { /** If a SKU is provided, update any old record that used the UPC as a placeholder SKU. */ $existsP = $dbc->prepare(' SELECT sku FROM vendorItems WHERE sku=? AND upc=? AND vendorID=?'); $existsR = $dbc->execute($existsP, array($sku, $upc, $vendorID)); if ($dbc->numRows($existsR) > 0 && $sku != $upc) { $delP = $dbc->prepare(' DELETE FROM vendorItems WHERE sku =? AND upc=? AND vendorID=?'); $dbc->execute($delP, array($upc, $upc, $vendorID)); } else { $fixSkuP = $dbc->prepare(' UPDATE vendorItems SET sku=? WHERE sku=? AND vendorID=?'); $dbc->execute($fixSkuP, array($sku, $upc, $vendorID)); } } $vitem->sku($sku); $vitem->size($model->size()); $vitem->description($model->description()); $vitem->brand($model->brand()); $vitem->units(FormLib::get('caseSize', 1)); $vitem->cost($model->cost()); $vitem->save(); } if ($dbc->table_exists('prodExtra')) { $extra = new ProdExtraModel($dbc); $extra->upc($upc); if (!$extra->load()) { $extra->variable_pricing(0); $extra->margin(0); $extra->case_quantity(''); $extra->case_cost(0.0); $extra->case_info(''); } $brand = FormLib::get('manufacturer'); if (isset($brand[0])) { $extra->manufacturer(str_replace("'", '', $brand[0])); } $dist = FormLib::get('distributor'); if (isset($dist[0])) { $extra->distributor(str_replace("'", '', $dist[0])); } $cost = FormLib::get('cost'); if (isset($cost[0])) { $extra->cost($cost[0]); } $extra->save(); } if (!isset($FANNIE_PRODUCT_MODULES['ProdUserModule'])) { if ($dbc->table_exists('productUser')) { $ldesc = FormLib::get_form_value('puser_description'); $model = new ProductUserModel($dbc); $model->upc($upc); $model->description($ldesc); $model->save(); } } }
/** Receiving AJAX callback. Lookup item in the order and display form fields to enter required info */ public function get_id_sku_handler() { $dbc = $this->connection; $model = new PurchaseOrderItemsModel($dbc); $model->orderID($this->id); $model->sku($this->sku); // lookup by SKU but if nothing is found // try using the value as a UPC instead $found = false; if ($model->load()) { $found = true; } else { $model->reset(); $model->orderID($this->id); $model->internalUPC(BarcodeLib::padUPC($this->sku)); $matches = $model->find(); if (count($matches) == 1) { $model = $matches[0]; $found = true; } } // item not in order. need all fields to add it. if (!$found) { echo '<div class="alert alert-danger">SKU not found in order</div>'; echo '<form onsubmit="saveReceive(); return false;">'; echo '<table class="table table-bordered">'; echo '<tr><th>SKU</th><th>UPC</th><th>Brand</th><th>Description</th> <th>Qty Ordered</th><th>Cost (est)</th><th>Qty Received</th><th>Cost Received</th></tr>'; $order = new PurchaseOrderModel($dbc); $order->orderID($this->id); $order->load(); $item = new VendorItemsModel($dbc); $item->vendorID($order->vendorID()); $item->sku($this->sku); $item->load(); printf('<tr> <td>%s<input type="hidden" name="sku" value="%s" /></td> <td><input type="text" class="form-control" name="upc" value="%s" /></td> <td><input type="text" class="form-control" name="brand" value="%s" /></td> <td><input type="text" class="form-control" name="description" value="%s" /></td> <td><input type="text" class="form-control" name="orderQty" value="%s" /></td> <td><input type="text" class="form-control" name="orderCost" value="%.2f" /></td> <td><input type="text" class="form-control" name="receiveQty" value="%s" /></td> <td><input type="text" class="form-control" name="receiveCost" value="%.2f" /></td> <td><button type="submit" class="btn btn-default">Add New Item</button><input type="hidden" name="id" value="%d" /></td> </tr>', $this->sku, $this->sku, $item->upc(), $item->brand(), $item->description(), 1, $item->cost() * $item->units(), 0, 0, $this->id); echo '</table>'; echo '</form>'; } else { // item in order. just need received qty and cost echo '<form onsubmit="saveReceive(); return false;">'; echo '<table class="table table-bordered">'; echo '<tr><th>SKU</th><th>UPC</th><th>Brand</th><th>Description</th> <th>Qty Ordered</th><th>Cost (est)</th><th>Qty Received</th><th>Cost Received</th></tr>'; if ($model->receivedQty() === null) { $model->receivedQty($model->quantity()); } if ($model->receivedTotalCost() === null) { $model->receivedTotalCost($model->quantity() * $model->unitCost() * $model->caseSize()); } printf('<tr> <td>%s<input type="hidden" name="sku" value="%s" /></td> <td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td> <td>%.2f</td> <td><input type="text" class="form-control" name="qty" value="%s" /></td> <td><input type="text" class="form-control" name="cost" value="%.2f" /></td> <td><button type="submit" class="btn btn-default">Save</button><input type="hidden" name="id" value="%d" /></td> </tr>', $this->sku, $this->sku, $model->internalUPC(), $model->brand(), $model->description(), $model->quantity(), $model->quantity() * $model->unitCost() * $model->caseSize(), $model->receivedQty(), $model->receivedTotalCost(), $this->id); echo '</table>'; echo '</form>'; } return false; }
public function showEditForm($upc, $display_mode = 1, $expand_mode = 1) { $db = $this->db(); $product = new ProductsModel($db); $product->upc($upc); if (!$product->load()) { /** Lookup vendor cost on new items */ $vendor = new VendorItemsModel($db); $vendor->upc($upc); foreach ($vendor->find('vendorID') as $v) { $product->cost($v->cost()); break; } } $ret = '<div id="ItemMarginFieldset" class="panel panel-default">'; $ret .= "<div class=\"panel-heading\">\n <a href=\"\" onclick=\"\$('#ItemMarginContents').toggle();return false;\">\n Margin\n </a></div>"; $css = $expand_mode == 1 ? '' : ' collapse'; $ret .= '<div id="ItemMarginContents" class="panel-body' . $css . '">'; $ret .= '<div class="col-sm-5">'; $ret .= '<div id="ItemMarginMeter">'; $ret .= $this->calculateMargin($product->normal_price(), $product->cost(), $product->department(), $upc); $ret .= '</div>'; $ret .= '</div>'; $ret .= '<div class="col-sm-6">'; $ret .= '<div class="form-group form-inline"> <label>Pricing Rule</label> <select name="price-rule-id" class="form-control input-sm" onchange="if(this.value>=0)$(\'#custom-pricing-fields :input\').prop(\'disabled\', true); else $(\'#custom-pricing-fields :input\').prop(\'disabled\', false);"> <option value="0" ' . ($product->price_rule_id() == 0 ? 'selected' : '') . '>Normal</option> <option value="1" ' . ($product->price_rule_id() == 1 ? 'selected' : '') . '>Variable</option> <option value="-1" ' . ($product->price_rule_id() > 1 ? 'selected' : '') . '>Custom</option> </select> <input type="hidden" name="current-price-rule-id" value="' . $product->price_rule_id() . '" /> <label>Avg. Daily Movement</label> ' . sprintf('%.2f', $this->avgSales($upc)) . ' </div>'; $rule = new PriceRulesModel($db); if ($product->price_rule_id() > 1) { $rule->priceRuleID($product->price_rule_id()); $rule->load(); } $disabled = $product->price_rule_id() <= 1 ? 'disabled' : ''; $ret .= '<div id="custom-pricing-fields" class="form-group form-inline"> <label>Custom</label> <select ' . $disabled . ' name="price-rule-type" class="form-control input-sm"> {{RULE_TYPES}} </select> <input type="text" class="form-control date-field input-sm" name="rule-review-date" ' . $disabled . ' placeholder="Review Date" title="Review Date" value="{{REVIEW_DATE}}" /> <input type="text" class="form-control input-sm" name="rule-details" ' . $disabled . ' placeholder="Details" title="Details" value="{{RULE_DETAILS}}" /> </div>'; $types = new PriceRuleTypesModel($db); $ret = str_replace('{{RULE_TYPES}}', $types->toOptions($rule->priceRuleTypeID()), $ret); $ret = str_replace('{{REVIEW_DATE}}', $rule->reviewDate(), $ret); $ret = str_replace('{{RULE_DETAILS}}', $rule->details(), $ret); $ret .= '</div>'; $ret .= '</div>'; $ret .= '</div>'; return $ret; }
/** AJAX call: ?id=<vendor ID>&sku=<vendor SKU>&qty=<# of cases> Add the given SKU & qty to the order */ function get_id_sku_qty_handler() { global $FANNIE_OP_DB; $dbc = FannieDB::get($FANNIE_OP_DB); $orderID = $this->getOrderID($this->id, FannieAuth::getUID($this->current_user)); $vitem = new VendorItemsModel($dbc); $vitem->vendorID($this->id); $vitem->sku($this->sku); $vitem->load(); $pitem = new PurchaseOrderItemsModel($dbc); $pitem->orderID($orderID); $pitem->sku($this->sku); $pitem->quantity($this->qty); $pitem->unitCost($vitem->cost()); $pitem->caseSize($vitem->units()); $pitem->unitSize($vitem->size()); $pitem->brand($vitem->brand()); $pitem->description($vitem->description()); $pitem->internalUPC($vitem->upc()); $pitem->save(); $ret = array(); $pitem->reset(); $pitem->orderID($orderID); $pitem->sku($this->sku); if (count($pitem->find()) == 0) { $ret['error'] = 'Error saving entry'; } else { $ret['sidebar'] = $this->calculate_sidebar(); } echo json_encode($ret); return False; }
/** AJAX call: ?id=<vendor ID>&sku=<vendor SKU>&qty=<# of cases> Add the given SKU & qty to the order */ function get_id_sku_qty_handler() { global $FANNIE_OP_DB; $dbc = FannieDB::get($FANNIE_OP_DB); $orderID = $this->getOrderID($this->id, FannieAuth::getUID($this->current_user)); $vitem = new VendorItemsModel($dbc); $vitem->vendorID($this->id); $vitem->sku($this->sku); $vitem->load(); $pitem = new PurchaseOrderItemsModel($dbc); $pitem->orderID($orderID); $pitem->sku($this->sku); if ($this->qty == 0) { $pitem->delete(); } else { $pitem->quantity($this->qty); $pitem->unitCost($vitem->cost()); $pitem->caseSize($vitem->units()); $pitem->unitSize($vitem->size()); $pitem->brand($vitem->brand()); $pitem->description($vitem->description()); $pitem->internalUPC($vitem->upc()); $pitem->save(); } $ret = array(); $pitem->reset(); $pitem->orderID($orderID); $pitem->sku($this->sku); if (count($pitem->find()) == 0 && $this->qty != 0) { $ret['error'] = 'Error saving entry'; } else { $q = 'SELECT count(*) as rows, SUM(unitCost*caseSize*quantity) as estimatedCost FROM PurchaseOrderItems WHERE orderID=?'; $p = $dbc->prepare_statement($q); $r = $dbc->exec_statement($p, array($orderID)); $w = $dbc->fetch_row($r); $ret['count'] = $w['rows']; $ret['cost'] = sprintf('%.2f', $w['estimatedCost']); } echo json_encode($ret); return False; }