예제 #1
0
 /**
  * (non-PHPdoc)
  * @see DetailsPageAbstract::saveItem()
  */
 public function saveItem($sender, $param)
 {
     $results = $errors = array();
     try {
         Dao::beginTransaction();
         if (!isset($param->CallbackParameter->id)) {
             throw new Exception('Invalid supplier ID passed in!');
         }
         $supplier = ($id = trim($param->CallbackParameter->id)) === '' ? new Supplier() : Supplier::get($id);
         if (!$supplier instanceof Supplier) {
             throw new Exception('Invalid supplier passed in!');
         }
         $contactName = trim($param->CallbackParameter->address->contactName);
         $contactNo = trim($param->CallbackParameter->address->contactNo);
         $street = trim($param->CallbackParameter->address->street);
         $city = trim($param->CallbackParameter->address->city);
         $region = trim($param->CallbackParameter->address->region);
         $postCode = trim($param->CallbackParameter->address->postCode);
         $country = trim($param->CallbackParameter->address->country);
         $address = $supplier->getAddress();
         $supplier->setName(trim($param->CallbackParameter->name))->setDescription(trim($param->CallbackParameter->description))->setContactNo(trim($param->CallbackParameter->contactNo))->setEmail(trim($param->CallbackParameter->email))->setAddress(Address::create($street, $city, $region, $country, $postCode, $contactName, $contactNo, $address))->save();
         $results['url'] = '/supplier/' . $supplier->getId() . '.html' . (isset($_REQUEST['blanklayout']) ? '?blanklayout=' . $_REQUEST['blanklayout'] : '');
         $results['item'] = $supplier->getJson();
         Dao::commitTransaction();
     } catch (Exception $ex) {
         Dao::rollbackTransaction();
         $errors[] = $ex->getMessage() . $ex->getTraceAsString();
     }
     $param->ResponseData = StringUtilsAbstract::getJson($results, $errors);
 }
예제 #2
0
 /**
  * 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);
 }
예제 #3
0
    /**
     * Getting the items
     *
     * @param unknown $sender
     * @param unknown $param
     * @throws Exception
     *
     */
    public function getItems($sender, $param)
    {
        $results = $errors = array();
        try {
            if (!isset($param->CallbackParameter->searchCriteria->supplierId) || !($supplier = Supplier::get(trim($param->CallbackParameter->searchCriteria->supplierId))) instanceof Supplier) {
                throw new Exception('Invalid Supplier provided');
            }
            if (!isset($param->CallbackParameter->searchCriteria->invoiceNo) || ($invoiceNo = trim($param->CallbackParameter->searchCriteria->invoiceNo)) === '') {
                throw new Exception('Invalid Invoice number provided');
            }
            ReceivingItem::getQuery()->eagerLoad('ReceivingItem.purchaseOrder', 'inner join', 'rec_item_po', 'rec_item_po.id = rec_item.purchaseOrderId');
            if (ReceivingItem::countByCriteria('rec_item_po.supplierId = ? and rec_item.invoiceNo = ?', array($supplier->getId(), $invoiceNo)) === 0) {
                throw new Exception('There is no such a invoice(invoice No=' . $invoiceNo . ') for supplier:' . $supplier->getName());
            }
            $sql = 'select ri.productId,
						ri.unitPrice `unitPrice`,
						sum(ri.qty) `qty`,
						group_concat(distinct ri.id) `itemIds`,
						group_concat(distinct po.id) `poIds`
					from receivingitem ri
					inner join purchaseorder po on (po.id = ri.purchaseOrderId)
					where ri.active = 1 and ri.invoiceNo = ? and po.supplierId = ?
					group by ri.productId,  ri.unitPrice';
            $params = array($invoiceNo, $supplier->getId());
            $rows = Dao::getResultsNative($sql, $params);
            $results['supplier'] = $supplier->getJson();
            $results['items'] = array();
            foreach ($rows as $row) {
                $items = count($itemIds = explode(',', $row['itemIds'])) === 0 ? array() : ReceivingItem::getAllByCriteria('id in (' . implode(',', array_fill(0, count($itemIds), '?')) . ')', $itemIds);
                $pos = count($poIds = explode(',', $row['poIds'])) === 0 ? array() : PurchaseOrder::getAllByCriteria('id in (' . implode(',', array_fill(0, count($poIds), '?')) . ')', $poIds);
                $results['items'][] = array('product' => Product::get($row['productId'])->getJson(), 'totalQty' => $row['qty'], 'totalPrice' => $row['unitPrice'] * $row['qty'], 'items' => array_map(create_function('$a', 'return $a->getJson();'), $items), 'purchaseOrders' => array_map(create_function('$a', 'return $a->getJson();'), $pos));
            }
        } catch (Exception $ex) {
            $errors[] = $ex->getMessage();
        }
        $param->ResponseData = StringUtilsAbstract::getJson($results, $errors);
    }
예제 #4
0
 /**
  * updateInvoiceNo
  *
  * @param unknown $sender
  * @param unknown $param
  * @throws Exception
  *
  */
 public function updateInvoiceNo($sender, $param)
 {
     $results = $errors = array();
     try {
         if (!isset($param->CallbackParameter->supplierId) || !($supplier = Supplier::get(trim($param->CallbackParameter->supplierId))) instanceof Supplier) {
             throw new Exception('Invalid Supplier provided.');
         }
         if (!isset($param->CallbackParameter->newInoviceNo) || ($newInoviceNo = trim($param->CallbackParameter->newInoviceNo)) === '') {
             throw new Exception('Invalid newInoviceNo.');
         }
         if (!isset($param->CallbackParameter->oldInvoiceNo)) {
             throw new Exception('Invalid oldInvoiceNo.');
         }
         $oldInvoiceNo = trim($param->CallbackParameter->oldInvoiceNo);
         ReceivingItem::updateByCriteria('invoiceNo = :newInvoiceNo', 'invoiceNo = :oldInvoiceNo and purchaseOrderId in (select po.id from purchaseorder po where po.active = 1 and po.supplierId = :supplierId)', array('newInvoiceNo' => $newInoviceNo, 'oldInvoiceNo' => $oldInvoiceNo, 'supplierId' => $supplier->getId()));
     } catch (Exception $ex) {
         $errors[] = $ex->getMessage();
     }
     $param->ResponseData = StringUtilsAbstract::getJson($results, $errors);
 }
예제 #5
0
 private function _setSupplierCodes(Product &$product, $param)
 {
     if (isset($param->CallbackParameter->supplierCodes) && count($supplierCodes = $param->CallbackParameter->supplierCodes) > 0) {
         foreach ($supplierCodes as $code) {
             if (!($supplier = Supplier::get(trim($code->typeId))) instanceof Supplier) {
                 continue;
             }
             if (!isset($code->id) || ($id = trim($code->id)) === '') {
                 if (trim($code->active) === '1') {
                     SupplierCode::create($product, $supplier, trim($code->value));
                 }
                 //if it's deactivated one, ignore
             } else {
                 if (($supplierCode = SupplierCode::get($id)) instanceof SupplierCode) {
                     $supplierCode->setActive(trim($code->active) === '1')->setCode(trim($code->value))->setSupplier($supplier)->setProduct($product)->save();
                 }
             }
         }
     }
     return $this;
 }
예제 #6
0
 /**
  * (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);
 }