protected static function _getData() { $return = array(); $myobCodeType = ProductCodeType::get(ProductCodeType::ID_MYOB); foreach (Product::getAllByCriteria('updated <= 2015-12-06 04:00:00', array(), false) as $product) { $logs = ProductQtyLog::getAllByCriteria('productId = ? and created <= ?', array($product->getId(), trim($toDate)), true, 1, 1, array('id' => 'desc')); $log = count($logs) > 0 ? $logs[0] : null; $myobCodes = ProductCode::getCodes($product, $myobCodeType, true, 1, 1); $return[] = array('sku' => $product->getSku(), 'name' => $product->getName(), 'short description' => $product->getShortDescription(), 'category' => join(', ', array_map(create_function('$a', 'return $a->getCategory()->getName();'), $product->getCategories())), 'assetAccNo' => $product->getAssetAccNo(), 'revenueAccNo' => $product->getRevenueAccNo(), 'costAccNo' => $product->getCostAccNo(), 'Stock On PO' => $log instanceof ProductQtyLog ? $log->getStockOnPO() : $product->getStockOnPO(), 'Stock On Order' => $log instanceof ProductQtyLog ? $log->getStockOnOrder() : $product->getStockOnOrder(), 'Stock On Hand' => $log instanceof ProductQtyLog ? $log->getStockOnHand() : $product->getStockOnHand(), 'Total On Hand Value' => $log instanceof ProductQtyLog ? $log->getTotalOnHandValue() : $product->getTotalOnHandValue(), 'Stock In Parts' => $log instanceof ProductQtyLog ? $log->getStockInParts() : $product->getStockInParts(), 'Total In Parts Value' => $log instanceof ProductQtyLog ? $log->getTotalInPartsValue() : $product->getTotalInPartsValue(), 'Stock In RMA' => $log instanceof ProductQtyLog ? $log->getStockInRMA() : $product->getStockInRMA(), 'Total RMA Value' => $log instanceof ProductQtyLog ? $log->getTotalRMAValue() : $product->getTotalRMAValue(), 'active' => intval($product->getActive()) === 1 ? 'Y' : 'N', 'MYOB' => count($myobCodes) > 0 ? $myobCodes[0]->getCode() : ''); } return $return; }
public function searchProduct($sender, $param) { $results = $errors = array(); try { $items = array(); $searchTxt = isset($param->CallbackParameter->searchTxt) ? trim($param->CallbackParameter->searchTxt) : ''; $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) . '%'; } } Product::getQuery()->eagerLoad('Product.codes', 'left join'); $products = Product::getAllByCriteria($where, $params, true, 1, DaoQuery::DEFAUTL_PAGE_SIZE, array('pro.sku' => 'asc')); foreach ($products as $product) { if (!$product instanceof Product) { throw new Exception('Invalid Product passed in!'); } $EANcodes = ProductCode::getAllByCriteria('pro_code.productId = :productId and pro_code.typeId = :typeId', array('productId' => $product->getId(), 'typeId' => ProductCodeType::ID_EAN), true, 1, 1); $EANcodes = count($EANcodes) > 0 ? $EANcodes[0]->getCode() : ''; $UPCcodes = ProductCode::getAllByCriteria('pro_code.productId = :productId and pro_code.typeId = :typeId', array('productId' => $product->getId(), 'typeId' => ProductCodeType::ID_UPC), true, 1, 1); $UPCcodes = count($UPCcodes) > 0 ? $UPCcodes[0]->getCode() : ''; $array = $product->getJson(); $array['codes'] = array('EAN' => $EANcodes, 'UPC' => $UPCcodes); $items[] = $array; } $results['items'] = $items; } catch (Exception $ex) { $errors[] = $ex->getMessage(); } $param->ResponseData = StringUtilsAbstract::getJson($results, $errors); }
/** * Update product code * * @param Product $product * @param unknown $myobCode * @param ProductCodeType $productCodeType * @param string $assetAccNo * @param string $revenueAccNo * @param string $costAccNo * * @return Product */ private function updateProductCode(Product $product, $myobCode, ProductCodeType $productCodeType, $assetAccNo = '', $revenueAccNo = '', $costAccNo = '') { try { Dao::beginTransaction(); // only take the myobCode (myob item#) after the first dash $position = strpos($myobCode, '-'); if ($position) { $myobCodeAfter = substr($myobCode, $position + 1); // get everything after first dash $myobCodeAfter = str_replace(' ', '', $myobCodeAfter); // remove all whitespace } else { $myobCodeAfter = $myobCode; } $result = array(); $result['product'] = $product->getJson(); $result['code'] = $myobCodeAfter; $result['MYOBcode'] = $myobCode; $result['assetAccNo'] = $assetAccNo; $result['revenueAccNo'] = $revenueAccNo; $result['costAccNo'] = $costAccNo; // if such code type for such product exist, update it to the new one if (!empty($myobCode)) { if (count($productCodes = ProductCode::getAllByCriteria('pro_code.typeId = ? and pro_code.productId = ?', array($productCodeType->getId(), $product->getId()), true, 1, 1)) > 0) { $productCodes[0]->setCode($myobCodeAfter)->save(); $result['codeNew'] = false; } else { $newCode = ProductCode::create($product, $productCodeType, trim($myobCodeAfter)); $result['codeNew'] = true; } } // do the same for MYOB code (NOTE: have to have MYOB code in code type !!!) if (!empty($myobCode)) { if (count($productCodes = ProductCode::getAllByCriteria('pro_code.typeId = ? and pro_code.productId = ?', array(ProductCodeType::ID_MYOB, $product->getId()), true, 1, 1)) > 0) { $productCodes[0]->setCode($myobCode)->save(); $result['MYOBcodeNew'] = false; } else { ProductCode::create($product, ProductCodeType::get(ProductCodeType::ID_MYOB), trim($myobCode)); $result['MYOBcodeNew'] = true; } } if (!empty($assetAccNo)) { $product->setAssetAccNo($assetAccNo)->save(); } if (!empty($revenueAccNo)) { $product->setRevenueAccNo($revenueAccNo)->save(); } if (!empty($costAccNo)) { $product->setCostAccNo($costAccNo)->save(); } Dao::commitTransaction(); return $product; } catch (Exception $e) { Dao::rollbackTransaction(); echo $e; exit; } }
private function _setBarcodes(Product &$product, $param) { if (isset($param->CallbackParameter->productCodes) && count($productCodes = $param->CallbackParameter->productCodes) > 0) { foreach ($productCodes as $code) { if (!($type = ProductCodeType::get(trim($code->typeId))) instanceof ProductCodeType) { continue; } if (!isset($code->id) || ($id = trim($code->id)) === '') { if (trim($code->active) === '1') { ProductCode::create($product, $type, trim($code->value)); } //if it's deactivated one, ignore } else { if (($productCode = ProductCode::get($id)) instanceof ProductCode) { $productCode->setActive(trim($code->active) === '1')->setCode(trim($code->value))->setType($type)->setProduct($product)->save(); } } } } return $this; }
/** * saveOrder * * @param unknown $sender * @param unknown $param * * @throws Exception * */ public function saveOrder($sender, $param) { $results = $errors = array(); try { Dao::beginTransaction(); $items = array(); $purchaseOrder = PurchaseOrder::get(trim($param->CallbackParameter->purchaseOrder->id)); if (!$purchaseOrder instanceof PurchaseOrder) { throw new Exception('Invalid PurchaseOrder passed in!'); } $comment = trim($param->CallbackParameter->comments); $purchaseOrder->addComment(Comments::TYPE_WAREHOUSE, $comment); $products = $param->CallbackParameter->products; $outStandingOrders = array(); $invoiceNos = array(); foreach ($products->matched as $item) { $product = Product::get(trim($item->product->id)); if (!$product instanceof Product) { throw new Exception('Invalid Product passed in!'); } if (isset($item->product->EANcode)) { $EANcode = trim($item->product->EANcode); $productcodes = ProductCode::getAllByCriteria('pro_code.productId = :productId and pro_code.typeId = :typeId', array('productId' => $product->getId(), 'typeId' => ProductCodeType::ID_EAN), true, 1, 1); if (count($productcodes) > 0) { $productcodes[0]->setCode($EANcode)->save(); } else { ProductCode::create($product, ProductCodeType::get(ProductCodeType::ID_EAN), $EANcode); } } if (isset($item->product->UPCcode)) { $UPCcode = trim($item->product->UPCcode); $productcodes = ProductCode::getAllByCriteria('pro_code.productId = :productId and pro_code.typeId = :typeId', array('productId' => $product->getId(), 'typeId' => ProductCodeType::ID_UPC), true, 1, 1); if (sizeof($productcodes)) { $productcodes[0]->setCode($UPCcode)->save(); } else { ProductCode::create($product, ProductCodeType::get(ProductCodeType::ID_UPC), $UPCcode); } } if (isset($item->product->warehouseLocation) && ($locationName = trim($item->product->warehouseLocation)) !== '') { $locs = Location::getAllByCriteria('name = ?', array($locationName), true, 1, 1); $loc = count($locs) > 0 ? $locs[0] : Location::create($locationName, $locationName); $product->addLocation(PreferredLocationType::get(PreferredLocationType::ID_WAREHOUSE), $loc); } $serials = $item->serial; $totalQty = 0; foreach ($serials as $serial) { $qty = trim($serial->qty); $totalQty += intval($qty); $serialNo = trim($serial->serialNo); $unitPrice = trim($serial->unitPrice); $invoiceNo = trim($serial->invoiceNo); $invoiceNos[] = $invoiceNo; $comments = trim($serial->comments); ReceivingItem::create($purchaseOrder, $product, $unitPrice, $qty, $serialNo, $invoiceNo, $comments); } OrderItem::getQuery()->eagerLoad('OrderItem.order', 'inner join', 'ord', 'ord.id = ord_item.orderId and ord.active = 1 and ord.type = :ordType and ord_item.productId = :productId and ord.statusId in ( :statusId1, :statusId2, :statusId3)'); $orderItems = OrderItem::getAllByCriteria('ord_item.active = 1', array('ordType' => Order::TYPE_INVOICE, 'productId' => $product->getId(), 'statusId1' => OrderStatus::ID_INSUFFICIENT_STOCK, 'statusId2' => OrderStatus::ID_ETA, 'statusId3' => OrderStatus::ID_STOCK_CHECKED_BY_PURCHASING)); if (count($orderItems) > 0) { $orders = array(); foreach ($orderItems as $orderItem) { if (!array_key_exists($orderItem->getOrder()->getId(), $orders)) { $orders[$orderItem->getOrder()->getId()] = $orderItem->getOrder()->getJson(); } } $outStandingOrders[$product->getId()] = array('product' => $product->getJson(), 'recievedQty' => $totalQty, 'outStandingOrders' => array_values($orders)); } } $results['outStandingOrders'] = count($outStandingOrders) > 0 ? array_values($outStandingOrders) : array(); $results['item'] = PurchaseOrder::get($purchaseOrder->getId())->getJson(); $invoiceNos = array_unique($invoiceNos); $results['invoiceNos'] = $invoiceNos; Dao::commitTransaction(); } catch (Exception $ex) { Dao::rollbackTransaction(); $errors[] = $ex->getMessage(); } $param->ResponseData = StringUtilsAbstract::getJson($results, $errors); }
/** * (non-PHPdoc) * @see BaseEntityAbstract::getJson() */ public function getJson($extra = array(), $reset = false) { try { $array = $extra; if (!$this->isJsonLoaded($reset)) { $array['prices'] = array_map(create_function('$a', 'return $a->getJson();'), $this->getPrices()); $array['manufacturer'] = $this->getManufacturer() instanceof Manufacturer ? $this->getManufacturer()->getJson() : null; $array['supplierCodes'] = array_map(create_function('$a', 'return $a->getJson();'), SupplierCode::getAllByCriteria('productId = ?', array($this->getId()))); $array['productCodes'] = array_map(create_function('$a', 'return $a->getJson();'), ProductCode::getAllByCriteria('productId = ?', array($this->getId()))); $array['images'] = array_map(create_function('$a', 'return $a->getJson();'), $this->getImages()); $array['categories'] = array_map(create_function('$a', '$json = $a->getJson(); return $json["category"];'), Product_Category::getCategories($this)); $array['fullDescriptionAsset'] = ($asset = Asset::getAsset($this->getFullDescAssetId())) instanceof Asset ? $asset->getJson() : null; $array['locations'] = array_map(create_function('$a', 'return $a->getJson();'), PreferredLocation::getPreferredLocations($this)); $array['unitCost'] = $this->getUnitCost(); $array['priceMatchRule'] = ($i = ProductPriceMatchRule::getByProduct($this)) instanceof ProductPriceMatchRule ? $i->getJson() : null; $array['attributeSet'] = ($i = $this->getAttributeSet()) instanceof ProductAttributeSet ? $i->getJson() : null; $array['status'] = ($i = $this->getStatus()) instanceof ProductStatus ? $i->getJson() : null; } } catch (Exception $ex) { throw new Exception(' ********** getJson exception :' . $ex->getMessage()); } return parent::getJson($array, $reset); }