예제 #1
0
 /**
  * saveOrder
  *
  * @param unknown $sender
  * @param unknown $param
  *
  * @throws Exception
  *
  */
 public function saveOrder($sender, $param)
 {
     $results = $errors = array();
     try {
         Dao::beginTransaction();
         $customer = Customer::get(trim($param->CallbackParameter->customer->id));
         if (!$customer instanceof Customer) {
             throw new Exception('Invalid Customer passed in!');
         }
         if (!isset($param->CallbackParameter->applyTo) || ($applyTo = trim($param->CallbackParameter->applyTo)) === '' || !in_array($applyTo, CreditNote::getApplyToTypes())) {
             throw new Exception('Invalid Apply To passed in!');
         }
         if (isset($param->CallbackParameter->creditNoteId) && ($creditNote = CreditNote::get(trim($param->CallbackParameter->creditNoteId))) instanceof CreditNote) {
             $creditNote = $creditNote;
         } else {
             if (isset($param->CallbackParameter->orderId) && ($order = Order::get(trim($param->CallbackParameter->orderId))) instanceof Order) {
                 $creditNote = CreditNote::createFromOrder($order, $customer, trim($param->CallbackParameter->description));
             } else {
                 $creditNote = CreditNote::create($customer, trim($param->CallbackParameter->description));
             }
         }
         $creditNote->setShippingValue(isset($param->CallbackParameter->totalShippingCost) ? StringUtilsAbstract::getValueFromCurrency($param->CallbackParameter->totalShippingCost) : 0);
         if (isset($param->CallbackParameter->shippingAddr)) {
             $shippAddress = Address::create($param->CallbackParameter->shippingAddr->street, $param->CallbackParameter->shippingAddr->city, $param->CallbackParameter->shippingAddr->region, $param->CallbackParameter->shippingAddr->country, $param->CallbackParameter->shippingAddr->postCode, $param->CallbackParameter->shippingAddr->contactName, $param->CallbackParameter->shippingAddr->contactNo);
             $customer->setShippingAddress($shippAddress);
         }
         $printItAfterSave = false;
         if (isset($param->CallbackParameter->printIt)) {
             $printItAfterSave = intval($param->CallbackParameter->printIt) === 1 ? true : false;
         }
         if (isset($param->CallbackParameter->comments)) {
             $comments = trim($param->CallbackParameter->comments);
             $creditNote->addComment($comments, Comments::TYPE_SALES);
         }
         $totalPaymentDue = $creditNote->getShippingValue();
         $hasShipped = $creditNote->getOrder() instanceof Order && Shippment::countByCriteria('orderId = ?', array($creditNote->getOrder()->getId())) > 0;
         $creditNoteItemsMap = array();
         foreach ($param->CallbackParameter->items as $item) {
             if (!($product = Product::get(trim($item->product->id))) instanceof Product) {
                 throw new Exception('Invalid Product passed in!');
             }
             $unitPrice = StringUtilsAbstract::getValueFromCurrency(trim($item->unitPrice));
             $qtyOrdered = trim($item->qtyOrdered);
             $totalPrice = StringUtilsAbstract::getValueFromCurrency(trim($item->totalPrice));
             $itemDescription = trim($item->itemDescription);
             $active = trim($item->valid);
             $totalPaymentDue += $totalPrice;
             if (is_numeric($item->creditNoteItemId) && !CreditNoteItem::get(trim($item->creditNoteItemId)) instanceof CreditNoteItem) {
                 throw new Exception('Invalid Credit Note Item passed in');
             }
             $unitCost = $product->getUnitCost();
             $orderItem = null;
             if (isset($item->orderItemId) && ($orderItem = OrderItem::get(trim($item->orderItemId))) instanceof OrderItem) {
                 $unitCost = $orderItem->getUnitCost();
             }
             $creditNoteItem = is_numeric($item->creditNoteItemId) ? CreditNoteItem::get(trim($item->creditNoteItemId))->setActive($active)->setProduct($product)->setQty($qtyOrdered)->setUnitPrice($unitPrice)->setItemDescription($itemDescription)->setUnitCost($unitCost)->setTotalPrice($totalPrice)->save() : ($orderItem instanceof OrderItem ? CreditNoteItem::createFromOrderItem($creditNote, $orderItem, $qtyOrdered, $unitPrice, $itemDescription, $unitCost, $totalPrice) : CreditNoteItem::create($creditNote, $product, $qtyOrdered, $unitPrice, $itemDescription, $unitCost, $totalPrice));
             if (intval($creditNoteItem->getActive()) === 1) {
                 if (!isset($creditNoteItemsMap[$product->getId()])) {
                     $creditNoteItemsMap[$product->getId()] = 0;
                 }
                 $creditNoteItemsMap[$product->getId()] += $qtyOrdered;
             }
             //if we are not creating from a order, or there are shippments for this order then
             if (!$creditNote->getOrder() instanceof Order || $hasShipped === true) {
                 switch (trim($item->stockData)) {
                     case 'StockOnHand':
                         $product->returnedIntoSOH($qtyOrdered, $creditNoteItem->getUnitCost(), '', $creditNoteItem);
                         break;
                     case 'StockOnRMA':
                         $product->returnedIntoRMA($qtyOrdered, $creditNoteItem->getUnitCost(), '', $creditNoteItem);
                         break;
                     default:
                         throw new Exception('System Error: NO where to transfer the stock: ' . trim($item->stockData) . ' for product(SKU=' . $product->getSku() . ').');
                 }
             } else {
                 //revert all the shipped stock
                 foreach (OrderItem::getAllByCriteria('ord_item.orderId = ? and ord_item.isShipped = 1', array($creditNote->getOrder()->getId())) as $orderItem) {
                     $orderItem->setIsShipped(false)->save();
                 }
                 //revert all the picked stock
                 foreach (OrderItem::getAllByCriteria('ord_item.orderId = ? and ord_item.isPicked = 1', array($creditNote->getOrder()->getId())) as $orderItem) {
                     $orderItem->setIsPicked(false)->save();
                 }
             }
         }
         if (($paymentMethod = PaymentMethod::get(trim($param->CallbackParameter->paymentMethodId))) instanceof PaymentMethod) {
             $creditNote->setTotalPaid($totalPaidAmount = $param->CallbackParameter->totalPaidAmount)->addPayment($paymentMethod, $totalPaidAmount);
         }
         $creditNote->setTotalValue($totalPaymentDue)->setApplyTo($applyTo)->save();
         //if need to check half credited orders
         if ($creditNote->getOrder() instanceof Order && $hasShipped === false) {
             $orderItemMap = array();
             foreach ($order->getOrderItems() as $orderItem) {
                 $productId = $orderItem->getProduct()->getId();
                 if (!isset($orderItemMap[$productId])) {
                     $orderItemMap[$productId] = 0;
                 }
                 $orderItemMap[$productId] += $orderItem->getQtyOrdered();
             }
             //figure out the difference
             foreach ($creditNoteItemsMap as $productId => $qty) {
                 if (isset($orderItemMap[$productId])) {
                     if ($orderItemMap[$productId] <= $qty) {
                         //credited more than orderred
                         unset($orderItemMap[$productId]);
                     } else {
                         //credited less then ordered
                         $orderItemMap[$productId] = $orderItemMap[$productId] - $qty;
                     }
                 }
             }
             $orderItemMap = array_filter($orderItemMap);
             if (count($orderItemMap) > 0) {
                 //there are difference
                 $creditNote->creditFullOrder();
                 $newOrder = Order::create($creditNote->getOrder()->getCustomer(), $creditNote->getOrder()->getType() === 'INVOICE' ? Order::TYPE_ORDER : $creditNote->getOrder()->getType(), null, 'Spliting order because of partial credit note(CreditNoteNo:.' . $creditNote->getCreditNoteNo() . ') created', null, $creditNote->getOrder()->getOrderDate(), false, $creditNote->getOrder()->getShippingAddr(), $creditNote->getOrder()->getBillingAddr(), $creditNote->getOrder()->getPassPaymentCheck(), $creditNote->getOrder()->getPONo(), $creditNote->getOrder())->addComment('Created because of partial credit from ');
                 $totalAmount = 0;
                 foreach ($orderItemMap as $productId => $qty) {
                     $orderItems = OrderItem::getAllByCriteria('productId = ? and orderId = ?', array($productId, $creditNote->getOrder()->getId()), true, 1, 1);
                     if (count($orderItems) > 0) {
                         $totalAmount += $orderItems[0]->getUnitPrice() * $qty;
                     }
                     $newOrder->addItem($orderItems[0]->getProduct(), $orderItems[0]->getUnitPrice(), $qty, $orderItems[0]->getTotalPrice(), $orderItems[0]->getMageOrderId(), $orderItems[0]->getEta(), $orderItems[0]->getItemDescription());
                 }
                 if (count($shippingMethods = $creditNote->getOrder()->getInfo(OrderInfoType::ID_MAGE_ORDER_SHIPPING_METHOD)) > 0) {
                     $newOrder->addInfo(OrderInfoType::ID_MAGE_ORDER_SHIPPING_METHOD, $shippingMethods[0], true);
                 }
                 if (count($shippingCost = $creditNote->getOrder()->getInfo(OrderInfoType::ID_MAGE_ORDER_SHIPPING_COST)) > 0) {
                     $newOrder->addInfo(OrderInfoType::ID_MAGE_ORDER_SHIPPING_COST, $shippingCost[0], true);
                     $totalAmount += StringUtilsAbstract::getValueFromCurrency($shippingCost[0]);
                 }
                 $results['newOrder'] = $newOrder->setTotalAmount($totalAmount)->save()->getJson();
                 $creditNote->getOrder()->setStatus(OrderStatus::get(OrderStatus::ID_CANCELLED))->save()->addComment('This ' . $creditNote->getOrder()->getType() . ' is CANCELED, because of partial credit (CreditNoteNo:<a href="/creditnote/' . $creditNote->getId() . '.html" target="_BLANK">' . $creditNote->getCreditNoteNo() . '</a>) is created and a new ' . $newOrder->getType() . ' (<a href="/orderdetails/' . $newOrder->getId() . '.html?blanklayout=1">' . $newOrder->getOrderNo() . '</a>) is created for the diference.', Comments::TYPE_MEMO);
             } else {
                 $creditNote->getOrder()->setStatus(OrderStatus::get(OrderStatus::ID_CANCELLED))->save()->addComment('This ' . $creditNote->getOrder()->getType() . ' is CANCELED, because of full credit (CreditNoteNo:<a href="/creditnote/' . $creditNote->getId() . '.html" target="_BLANK">' . $creditNote->getCreditNoteNo() . '</a>) is created.', Comments::TYPE_MEMO);
             }
         }
         $results['item'] = $creditNote->getJson();
         $results['redirectURL'] = '/creditnote/' . $creditNote->getId() . '.html?' . $_SERVER['QUERY_STRING'];
         if ($printItAfterSave === true) {
             $results['printURL'] = '/print/creditnote/' . $creditNote->getId() . '.html?pdf=1';
         }
         Dao::commitTransaction();
     } catch (Exception $ex) {
         Dao::rollbackTransaction();
         $errors[] = $ex->getMessage() . '<pre>' . $ex->getTraceAsString() . '</pre>';
     }
     $param->ResponseData = StringUtilsAbstract::getJson($results, $errors);
 }