/** * Searching searchProduct * * @param unknown $sender * @param unknown $param * * @throws Exception * */ public function searchProduct($sender, $param) { $results = $errors = array(); try { $items = array(); $searchTxt = isset($param->CallbackParameter->searchTxt) ? trim($param->CallbackParameter->searchTxt) : ''; $pageNo = isset($param->CallbackParameter->pageNo) ? trim($param->CallbackParameter->pageNo) : '1'; $where = 'pro_pro_code.code = :searchExact or pro.name like :searchTxt OR sku like :searchTxt'; $params = array('searchExact' => $searchTxt, 'searchTxt' => '%' . $searchTxt . '%'); $searchTxtArray = StringUtilsAbstract::getAllPossibleCombo(StringUtilsAbstract::tokenize($searchTxt)); if (count($searchTxtArray) > 1) { foreach ($searchTxtArray as $index => $comboArray) { $key = 'combo' . $index; $where .= ' OR pro.name like :' . $key; $params[$key] = '%' . implode('%', $comboArray) . '%'; } } $stats = array(); $supplierID = isset($param->CallbackParameter->supplierID) ? trim($param->CallbackParameter->supplierID) : ''; Product::getQuery()->eagerLoad('Product.codes', 'left join'); $products = Product::getAllByCriteria($where, $params, true, $pageNo, DaoQuery::DEFAUTL_PAGE_SIZE, array('pro.sku' => 'asc'), $stats); foreach ($products as $product) { $array = $product->getJson(); $array['minProductPrice'] = 0; $array['lastSupplierPrice'] = 0; $array['minSupplierPrice'] = 0; $minProductPriceProduct = PurchaseOrderItem::getAllByCriteria('productId = ?', array($product->getId()), true, 1, 1, array('unitPrice' => 'asc')); $minProductPrice = sizeof($minProductPriceProduct) ? $minProductPriceProduct[0]->getUnitPrice() : 0; $minProductPriceId = sizeof($minProductPriceProduct) ? $minProductPriceProduct[0]->getPurchaseOrder()->getId() : ''; PurchaseOrderItem::getQuery()->eagerLoad('PurchaseOrderItem.purchaseOrder'); $lastSupplierPriceProduct = PurchaseOrderItem::getAllByCriteria('po_item.productId = ? and po_item_po.supplierId = ?', array($product->getId(), $supplierID), true, 1, 1, array('po_item.id' => 'desc')); $lastSupplierPrice = sizeof($lastSupplierPriceProduct) ? $lastSupplierPriceProduct[0]->getUnitPrice() : 0; $lastSupplierPriceId = sizeof($lastSupplierPriceProduct) ? $lastSupplierPriceProduct[0]->getPurchaseOrder()->getId() : ''; PurchaseOrderItem::getQuery()->eagerLoad('PurchaseOrderItem.purchaseOrder'); $minSupplierPriceProduct = PurchaseOrderItem::getAllByCriteria('po_item.productId = ? and po_item_po.supplierId = ?', array($product->getId(), $supplierID), true, 1, 1, array('po_item.unitPrice' => 'asc')); $minSupplierPrice = sizeof($minSupplierPriceProduct) ? $minSupplierPriceProduct[0]->getUnitPrice() : 0; $minSupplierPriceId = sizeof($minSupplierPriceProduct) ? $minSupplierPriceProduct[0]->getPurchaseOrder()->getId() : ''; $array['minProductPrice'] = $minProductPrice; $array['minProductPriceId'] = $minProductPriceId; $array['lastSupplierPrice'] = $lastSupplierPrice; $array['lastSupplierPriceId'] = $lastSupplierPriceId; $array['minSupplierPrice'] = $minSupplierPrice; $array['minSupplierPriceId'] = $minSupplierPriceId; $items[] = $array; } $results['items'] = $items; $results['pagination'] = $stats; } catch (Exception $ex) { $errors[] = $ex->getMessage(); } $param->ResponseData = StringUtilsAbstract::getJson($results, $errors); }