/** * saveOrder * * @param unknown $sender * @param unknown $param * * @throws Exception * */ public function saveOrder($sender, $param) { $results = $errors = array(); $daoStart = false; try { Dao::beginTransaction(); $daoStart = true; $supplier = Supplier::get(trim($param->CallbackParameter->supplier->id)); if (!$supplier instanceof Supplier) { throw new Exception('Invalid Supplier passed in!'); } $supplierContactName = trim($param->CallbackParameter->supplier->contactName); $supplierContactNo = trim($param->CallbackParameter->supplier->contactNo); $supplierEmail = trim($param->CallbackParameter->supplier->email); if (!empty($supplierContactName) && $supplierContactName !== $supplier->getContactName()) { $supplier->setContactName($supplierContactName); } if (!empty($supplierContactNo) && $supplierContactNo !== $supplier->getContactNo()) { $supplier->setContactNo($supplierContactNo); } if (!empty($supplierEmail) && $supplierEmail !== $supplier->getEmail()) { $supplier->setEmail($supplierEmail); } $supplier->save(); $purchaseOrder = PurchaseOrder::create($supplier, trim($param->CallbackParameter->supplierRefNum), $supplierContactName, $supplierContactNo, StringUtilsAbstract::getValueFromCurrency(trim($param->CallbackParameter->shippingCost)), StringUtilsAbstract::getValueFromCurrency(trim($param->CallbackParameter->handlingCost)))->setTotalAmount(StringUtilsAbstract::getValueFromCurrency(trim($param->CallbackParameter->totalPaymentDue)))->setEta(trim($param->CallbackParameter->eta))->setStatus(PurchaseOrder::STATUS_NEW)->save()->addComment(trim($param->CallbackParameter->comments), Comments::TYPE_PURCHASING); foreach ($param->CallbackParameter->items as $item) { if (!($product = Product::get(trim($item->productId))) instanceof Product) { throw new Exception('Invalid Product passed in!'); } $purchaseOrder->addItem($product, StringUtilsAbstract::getValueFromCurrency(trim($item->unitPrice)), 0 - abs(intval(trim($item->qtyOrdered)))); } if ($param->CallbackParameter->submitToSupplier === true) { $purchaseOrder->setStatus(PurchaseOrder::STATUS_ORDERED); } // For credit PO if (isset($param->CallbackParameter->type) && trim($param->CallbackParameter->type) === 'CREDIT') { $purchaseOrder->setIsCredit(true); if (isset($param->CallbackParameter->po) && ($fromPO = PurchaseOrder::get(trim($param->CallbackParameter->po->id))) instanceof PurchaseOrder) { $purchaseOrder->setFromPO($fromPO); } } $purchaseOrder->save(); $daoStart = false; Dao::commitTransaction(); $results['item'] = $purchaseOrder->getJson(); if (isset($param->CallbackParameter->confirmEmail) && trim($confirmEmail = trim($param->CallbackParameter->confirmEmail)) !== '') { $pdfFile = EntityToPDF::getPDF($purchaseOrder); $asset = Asset::registerAsset($purchaseOrder->getPurchaseOrderNo() . '.pdf', file_get_contents($pdfFile), Asset::TYPE_TMP); EmailSender::addEmail('*****@*****.**', $confirmEmail, 'BudgetPC Purchase Order:' . $purchaseOrder->getPurchaseOrderNo(), 'Please Find the attached PurchaseOrder(' . $purchaseOrder->getPurchaseOrderNo() . ') from BudgetPC.', array($asset)); EmailSender::addEmail('*****@*****.**', '*****@*****.**', 'BudgetPC Purchase Order:' . $purchaseOrder->getPurchaseOrderNo(), 'Please Find the attached PurchaseOrder(' . $purchaseOrder->getPurchaseOrderNo() . ') from BudgetPC.', array($asset)); $purchaseOrder->addComment('An email sent to "' . $confirmEmail . '" with the attachment: ' . $asset->getAssetId(), Comments::TYPE_SYSTEM); } } catch (Exception $ex) { if ($daoStart === true) { Dao::rollbackTransaction(); } $errors[] = $ex->getMessage(); } $param->ResponseData = StringUtilsAbstract::getJson($results, $errors); }
/** * (non-PHPdoc) * @see BPCPageAbstract::onLoad() */ public function onLoad($param) { parent::onLoad($param); if (!$this->isPostBack) { $this->order = PurchaseOrder::get($this->Request['POId']); if (!$this->order instanceof PurchaseOrder) { die('Invalid Purchase Order!'); } if (isset($_REQUEST['pdf']) && intval($_REQUEST['pdf']) === 1) { $file = EntityToPDF::getPDF($this->order); header('Content-Type: application/pdf'); // The PDF source is in original.pdf readfile($file); die; } } }
/** * (non-PHPdoc) * @see CRUDPageAbstract::_getEndJs() */ protected function _getEndJs() { $js = parent::_getEndJs(); $js .= "pageJs.setCallbackId('deleteItem', '" . $this->deleteItemBtn->getUniqueID() . "');"; if (isset($_REQUEST['productid']) || isset($_REQUEST['purchaseorderid'])) { if (isset($_REQUEST['productid'])) { if (!($product = Product::get(trim($_REQUEST['productid']))) instanceof Product) { die('Invalid Product Provided'); } $js .= "\$('searchBtn').up('.panel').down('.panel-body').insert({'bottom': new Element('input', {'type': 'hidden', 'search_field': 'productid', 'value': '" . $product->getId() . "'}) });"; $js .= "\$('searchBtn').up('.panel').hide();"; } if (isset($_REQUEST['purchaseorderid'])) { if (!($purchaseOrder = PurchaseOrder::get(trim($_REQUEST['purchaseorderid']))) instanceof PurchaseOrder) { die('Invalid PurchaseOrder Provided'); } $js .= "\$('searchBtn').up('.panel').down('.panel-body').insert({'bottom': new Element('input', {'type': 'hidden', 'search_field': 'purchaseorderid', 'value': '" . $purchaseOrder->getId() . "'}) });"; $js .= "\$('searchBtn').up('.panel').hide();"; } $js .= "\$('searchBtn').click();"; } $js .= "pageJs._bindSearchKey();"; return $js; }
/** * 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); }
/** * save the items * * @param unknown $sender * @param unknown $param * @throws Exception * */ public function deactivateItems($sender, $param) { $results = $errors = array(); try { $class = trim($this->_focusEntity); $id = isset($param->CallbackParameter->item_id) ? $param->CallbackParameter->item_id : array(); $item = PurchaseOrder::get($id); if (!$item instanceof PurchaseOrder) { throw new Exception(); } $item->setActive(false)->save(); $results['item'] = $item->getJson(); } catch (Exception $ex) { $errors[] = $ex->getMessage() . $ex->getTraceAsString(); } $param->ResponseData = StringUtilsAbstract::getJson($results, $errors); }
/** * (non-PHPdoc) * @see DetailsPageAbstract::saveItem() */ public function saveItem($sender, $param) { $results = $errors = array(); try { Dao::beginTransaction(); $perchaseorder = !isset($param->CallbackParameter->id) ? new PurchaseOrder() : PurchaseOrder::get(trim($param->CallbackParameter->id)); if (!$perchaseorder instanceof PurchaseOrder) { throw new Exception('Invalid Purchase Order passed in!'); } $purchaseOrderNo = trim($param->CallbackParameter->purchaseOrderNo); $supplierId = trim($param->CallbackParameter->supplierId); $supplier = Supplier::get($supplierId); $orderDate = trim($param->CallbackParameter->orderDate); $totalAmount = trim($param->CallbackParameter->totalAmount); $totalPaid = trim($param->CallbackParameter->totalPaid); if (isset($param->CallbackParameter->id)) { $perchaseorder->setPurchaseOrderNo($purchaseOrderNo)->setSupplier($supplier)->setSupplierRefNo($supplierId)->setOrderDate($orderDate)->setTotalAmount($totalAmount)->setTotalPaid($totalPaid)->save(); } else { // PurchaseOrder:: } $results['url'] = '/purchase/' . $perchaseorder->getId() . '.html'; $results['item'] = $perchaseorder->getJson(); Dao::commitTransaction(); } catch (Exception $ex) { Dao::rollbackTransaction(); $errors[] = $ex->getMessage() . $ex->getTraceAsString(); } $param->ResponseData = StringUtilsAbstract::getJson($results, $errors); }