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; }
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; }
/** 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; }
/** 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; }
/** 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; }