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