function run($request)
 {
     //reset time limit
     set_time_limit(1200);
     //file data
     $now = Date("d-m-Y-H-i");
     $fileName = "export-{$now}.csv";
     //data object variables
     $orderStatusSubmissionLog = EcommerceConfig::get("OrderStatusLog", "order_status_log_class_used_for_submitting_order");
     $fileData = "";
     $offset = 0;
     $count = 50;
     while ($orders = Order::get()->sort("\"Order\".\"ID\" ASC")->innerJoin("OrderStatusLog", "\"Order\".\"ID\" = \"OrderStatusLog\".\"OrderID\"")->innerJoin($orderStatusSubmissionLog, "\"{$orderStatusSubmissionLog}\".\"ID\" = \"OrderStatusLog\".\"ID\"")->leftJoin("Member", "\"Member\".\"ID\" = \"Order\".\"MemberID\"")->limit($count, $offset) && ($ordersCount = $orders->count())) {
         $offset = $offset + $count;
         foreach ($orders as $order) {
             if ($order->IsSubmitted()) {
                 $memberIsOK = false;
                 if (!$order->MemberID) {
                     $memberIsOK = true;
                 } elseif (!$order->Member()) {
                     $memberIsOK = true;
                 } elseif ($member = $order->Member()) {
                     $memberIsOK = true;
                     if ($member->IsShopAdmin()) {
                         $memberIsOK = false;
                     }
                 }
                 if ($memberIsOK) {
                     $items = OrderItem::get()->filter(array("OrderID" => $order->ID));
                     if ($items && $items->count()) {
                         $fileData .= $this->generateExportFileData($order->getOrderEmail(), $order->SubmissionLog()->Created, $items);
                     }
                 }
             }
         }
         unset($orders);
     }
     if ($fileData) {
         SS_HTTPRequest::send_file($fileData, $fileName, "text/csv");
     } else {
         user_error("No records found", E_USER_ERROR);
     }
 }
 public function changeIsOrdered($sender, $param)
 {
     $results = $errors = array();
     try {
         Dao::beginTransaction();
         if (!isset($param->CallbackParameter->item_id) || !($item = OrderItem::get($param->CallbackParameter->item_id)) instanceof OrderItem) {
             throw new Exception('System Error: invalid order item provided!');
         }
         if (!isset($param->CallbackParameter->isOrdered)) {
             throw new Exception('System Error: invalid order item: isOrdered needed!');
         }
         $setIsOrdered = intval($param->CallbackParameter->isOrdered);
         $item->setIsOrdered($setIsOrdered);
         $item->addComment('Changing the isOrdered to be : ' . ($setIsOrdered === 1 ? 'ORDERED' : 'NOT ORDERED'));
         $order = $item->getOrder();
         $sku = $item->getProduct()->getSku();
         $order->addComment('Changing the isOrdered for product (sku=' . $sku . ') to be : ' . ($setIsOrdered === 1 ? 'ORDERED' : 'NOT ORDERED'), Comments::TYPE_PURCHASING);
         $item->save();
         $results = $item->getJson();
         Dao::commitTransaction();
     } catch (Exception $ex) {
         Dao::rollbackTransaction();
         $errors[] = $ex->getMessage();
     }
     $param->ResponseData = StringUtilsAbstract::getJson($results, $errors);
 }
Esempio n. 3
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);
 }
 public function getTotalItems($recalculate = false)
 {
     if ($this->totalItems === null || $recalculate) {
         $this->totalItems = OrderItem::get()->where("\"OrderAttribute\".\"OrderID\" = " . $this->ID . " AND \"OrderItem\".\"Quantity\" > 0")->count();
     }
     return $this->totalItems;
 }
Esempio n. 5
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->type) || ($type = trim($param->CallbackParameter->type)) === '' || !in_array($type, Order::getAllTypes())) {
             throw new Exception('Invalid type passed in!');
         }
         $order = null;
         if (isset($param->CallbackParameter->orderId) && ($orderId = trim($param->CallbackParameter->orderId)) !== '') {
             if (!($order = Order::get($orderId)) instanceof Order) {
                 throw new Exception('Invalid Order to edit!');
             }
         }
         $orderCloneFrom = null;
         if (isset($param->CallbackParameter->orderCloneFromId) && ($orderCloneFromId = trim($param->CallbackParameter->orderCloneFromId)) !== '') {
             if (!($orderCloneFrom = Order::get($orderCloneFromId)) instanceof Order) {
                 throw new Exception('Invalid Order to clone from!');
             }
         }
         $shipped = isset($param->CallbackParameter->shipped) && intval($param->CallbackParameter->shipped) === 1;
         $poNo = isset($param->CallbackParameter->poNo) && trim($param->CallbackParameter->poNo) !== '' ? trim($param->CallbackParameter->poNo) : '';
         if (isset($param->CallbackParameter->shippingAddr)) {
             $shippAddress = $order instanceof Order ? $order->getShippingAddr() : null;
             $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, $param->CallbackParameter->shippingAddr->companyName, $shippAddress);
         } else {
             $shippAddress = $customer->getShippingAddress();
         }
         $printItAfterSave = false;
         if (isset($param->CallbackParameter->printIt)) {
             $printItAfterSave = intval($param->CallbackParameter->printIt) === 1 ? true : false;
         }
         if (!$order instanceof Order) {
             $order = Order::create($customer, $type, null, '', OrderStatus::get(OrderStatus::ID_NEW), new UDate(), false, $shippAddress, $customer->getBillingAddress(), false, $poNo, $orderCloneFrom);
         } else {
             $order->setType($type)->setPONo($poNo)->save();
         }
         $totalPaymentDue = 0;
         if (trim($param->CallbackParameter->paymentMethodId)) {
             $paymentMethod = PaymentMethod::get(trim($param->CallbackParameter->paymentMethodId));
             if (!$paymentMethod instanceof PaymentMethod) {
                 throw new Exception('Invalid PaymentMethod passed in!');
             }
             $totalPaidAmount = trim($param->CallbackParameter->totalPaidAmount);
             $order->addPayment($paymentMethod, $totalPaidAmount);
             $order = Order::get($order->getId());
             $order->addInfo(OrderInfoType::ID_MAGE_ORDER_PAYMENT_METHOD, $paymentMethod->getName(), true);
             if ($shipped === true) {
                 $order->setType(Order::TYPE_INVOICE);
             }
         } else {
             $paymentMethod = '';
             $totalPaidAmount = 0;
         }
         foreach ($param->CallbackParameter->items as $item) {
             $product = Product::get(trim($item->product->id));
             if (!$product instanceof Product) {
                 throw new Exception('Invalid Product passed in!');
             }
             if (isset($item->active) && intval($item->active) === 1 && intval($product->getActive()) !== 1 && $type === Order::TYPE_INVOICE) {
                 throw new Exception('Product(SKU:' . $product->getSku() . ') is DEACTIVATED, please change it to be proper product before converting it to a ' . $type . '!');
             }
             $unitPrice = trim($item->unitPrice);
             $qtyOrdered = trim($item->qtyOrdered);
             $totalPrice = trim($item->totalPrice);
             $itemDescription = trim($item->itemDescription);
             if (intval($item->active) === 1) {
                 $totalPaymentDue += $totalPrice;
                 if ($orderCloneFrom instanceof Order || !($orderItem = OrderItem::get($item->id)) instanceof OrderItem) {
                     $orderItem = OrderItem::create($order, $product, $unitPrice, $qtyOrdered, $totalPrice, 0, null, $itemDescription);
                 } else {
                     $orderItem->setActive(intval($item->active))->setProduct($product)->setUnitPrice($unitPrice)->setQtyOrdered($qtyOrdered)->setTotalPrice($totalPrice)->setItemDescription($itemDescription)->save();
                     $existingSellingItems = SellingItem::getAllByCriteria('orderItemId = ?', array($orderItem->getId()));
                     foreach ($existingSellingItems as $sellingItem) {
                         //DELETING ALL SERIAL NUMBER BEFORE ADDING
                         $sellingItem->setActive(false)->save();
                     }
                 }
                 $orderItem = OrderItem::get($orderItem->getId())->reCalMarginFromProduct()->resetCostNMarginFromKits()->save();
             } else {
                 if ($orderCloneFrom instanceof Order) {
                     continue;
                 } elseif (($orderItem = OrderItem::get($item->id)) instanceof OrderItem) {
                     $orderItem->setActive(false)->save();
                 }
             }
             if (isset($item->serials) && count($item->serials) > 0) {
                 foreach ($item->serials as $serialNo) {
                     $orderItem->addSellingItem($serialNo)->save();
                 }
             }
             if ($shipped === true) {
                 $orderItem->setIsPicked(true)->save();
             }
         }
         if (isset($param->CallbackParameter->courierId)) {
             $totalShippingCost = 0;
             $courier = null;
             if (is_numeric($courierId = trim($param->CallbackParameter->courierId))) {
                 $courier = Courier::get($courierId);
                 if (!$courier instanceof Courier) {
                     throw new Exception('Invalid Courier passed in!');
                 }
                 $order->addInfo(OrderInfoType::ID_MAGE_ORDER_SHIPPING_METHOD, $courier->getName(), true);
             } else {
                 $order->addInfo(OrderInfoType::ID_MAGE_ORDER_SHIPPING_METHOD, $courierId, true);
             }
             if (isset($param->CallbackParameter->totalShippingCost)) {
                 $totalShippingCost = StringUtilsAbstract::getValueFromCurrency(trim($param->CallbackParameter->totalShippingCost));
                 $order->addInfo(OrderInfoType::ID_MAGE_ORDER_SHIPPING_COST, StringUtilsAbstract::getCurrency($totalShippingCost), true);
             }
             if ($shipped === true) {
                 if (!$courier instanceof Courier) {
                     $courier = Courier::get(Courier::ID_LOCAL_PICKUP);
                 }
                 Shippment::create($shippAddress, $courier, '', new UDate(), $order, '');
             }
         } else {
             $courier = '';
             $totalShippingCost = 0;
         }
         $totalPaymentDue += $totalShippingCost;
         $comments = isset($param->CallbackParameter->comments) ? trim($param->CallbackParameter->comments) : '';
         $order = $order->addComment($comments, Comments::TYPE_SALES)->setTotalPaid($totalPaidAmount);
         if ($shipped === true) {
             $order->setStatus(OrderStatus::get(OrderStatus::ID_SHIPPED));
         }
         $order->setTotalAmount($totalPaymentDue)->save();
         if (isset($param->CallbackParameter->newMemo) && ($newMemo = trim($param->CallbackParameter->newMemo)) !== '') {
             $order->addComment($newMemo, Comments::TYPE_MEMO);
         }
         $results['item'] = $order->getJson();
         if ($printItAfterSave === true) {
             $results['printURL'] = '/print/order/' . $order->getId() . '.html?pdf=1';
         }
         $results['redirectURL'] = '/order/' . $order->getId() . '.html' . (trim($_SERVER['QUERY_STRING']) === '' ? '' : '?' . $_SERVER['QUERY_STRING']);
         Dao::commitTransaction();
     } catch (Exception $ex) {
         Dao::rollbackTransaction();
         $errors[] = $ex->getMessage();
     }
     $param->ResponseData = StringUtilsAbstract::getJson($results, $errors);
 }
 function updateForAjax(array $js)
 {
     parent::updateForAjax($js);
     self::apply_min_max();
     if (is_array(self::$ids_of_items_adjusted) && count(self::$ids_of_items_adjusted)) {
         $items = OrderItem::get()->filter(array('ID' => self::$ids_of_items_adjusted));
         if ($items->count()) {
             foreach ($items as $item) {
                 $item->updateForAjax($js);
             }
         }
     }
     return $js;
 }
 /**
  * retrieves order item from post / get variables.
  * @return OrderItem | Null
  */
 protected function getProductQuestionOrderItem()
 {
     $id = intval($this->owner->request->param("ID"));
     if (!$id) {
         $id = intval($this->owner->request->postVar("OrderItemID"));
     }
     if (!$id) {
         $id = intval($this->owner->request->getVar("OrderItemID"));
     }
     if ($id) {
         $this->productQuestionOrderItem = OrderItem::get()->byID($id);
     }
     if (!$this->productQuestionOrderItem) {
         user_error("NO this->productQuestionOrderItem specified");
     }
     return $this->productQuestionOrderItem;
 }
 /**
  * Gets an existing order item based on buyable and passed parameters
  *
  * @param DataObject $buyable
  * @param Array $parameters
  * @return OrderItem or null
  */
 protected function getExistingItem(BuyableModel $buyable, array $parameters = array())
 {
     $filterString = $this->parametersToSQL($parameters);
     if ($order = $this->currentOrder()) {
         $orderID = $order->ID;
         $obj = OrderItem::get()->where(" \"BuyableClassName\" = '" . $buyable->ClassName . "' AND\r\n\t\t\t\t\t\"BuyableID\" = " . $buyable->ID . " AND\r\n\t\t\t\t\t\"OrderID\" = " . $orderID . " " . $filterString)->First();
         return $obj;
     }
 }
 function ecommercetaskupgradepickupordeliverymodifier_320()
 {
     $explanation = "\r\n\t\t\t<h1>320. Removing empty Order Items</h1>\r\n\t\t\t<p>Removes all the order items without a buyable.</p>\r\n\t\t";
     if ($this->retrieveInfoOnly) {
         return $explanation;
     } else {
         echo $explanation;
     }
     $orderItems = OrderItem::get()->filter(array("BuyableID" => 0));
     $count = $orderItems->count();
     if ($count > 0) {
         $style = "deleted";
     } else {
         $style = "created";
     }
     $this->DBAlterationMessageNow("There are " . $count . " items that should be removed", $style);
     foreach ($orderItems as $orderItem) {
         $this->DBAlterationMessageNow("Deleting order item with ID: " . $orderItem->ID, "deleted");
         $orderItem->delete();
     }
     return 0;
 }
Esempio n. 10
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->status) || ($status = trim($param->CallbackParameter->status)) === '' || !in_array($status, RMA::getAllStatuses())) {
             throw new Exception('Invalid Status passed in!');
         }
         if (isset($param->CallbackParameter->RMA) && !($RMA = RMA::get(trim($param->CallbackParameter->RMA->id))) instanceof RMA) {
             throw new Exception('Invalid RMA To passed in!');
         }
         $RMA = isset($param->CallbackParameter->RMA) && ($RMA = RMA::get(trim($param->CallbackParameter->RMA->id))->setDescription(trim($param->CallbackParameter->description))) instanceof RMA ? $RMA : RMA::create($customer, trim($param->CallbackParameter->description));
         if (isset($param->CallbackParameter->order) && ($order = Order::get(trim($param->CallbackParameter->order->id))) instanceof Order) {
             $RMA->setOrder($order);
         }
         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, $param->CallbackParameter->shippingAddr->companyName);
             $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);
             $RMA->addComment($comments, Comments::TYPE_SALES);
         }
         $totalPaymentDue = 0;
         foreach ($param->CallbackParameter->items as $item) {
             $product = Product::get(trim($item->product->id));
             if (!$product instanceof Product) {
                 throw new Exception('Invalid Product passed in!');
             }
             $unitPrice = trim($item->unitPrice);
             $qtyOrdered = trim($item->qtyOrdered);
             $totalPrice = trim($item->totalPrice);
             $itemDescription = trim($item->itemDescription);
             $active = trim($item->valid);
             $totalPaymentDue += $totalPrice;
             if (is_numeric($item->RMAItemId) && !RMAItem::get(trim($item->RMAItemId)) instanceof RMAItem) {
                 throw new Exception('Invalid RMA Item passed in');
             }
             $RMAItem = is_numeric($item->RMAItemId) ? RMAItem::get(trim($item->RMAItemId))->setActive($active)->setProduct($product)->setQty($qtyOrdered)->setItemDescription($itemDescription) : RMAItem::create($RMA, $product, $qtyOrdered, $itemDescription);
             if (isset($item->orderItemId) && ($orderItem = OrderItem::get(trim($item->orderItemId))) instanceof OrderItem) {
                 $RMAItem->setOrderItem($orderItem)->setUnitCost($orderItem->getUnitCost());
             }
             if (isset($item->RMAItemId) && ($RMAItem = RMAItem::get(trim($item->RMAItemId))) instanceof RMAItem && $product->getUnitCost() != 0) {
                 $RMAItem->setUnitCost($RMAItem->getUnitCost())->save();
             } else {
                 $RMAItem->setUnitCost($product->getUnitCost())->save();
             }
             $RMAItem->save();
         }
         $RMA->setTotalValue($totalPaymentDue)->setStatus($status)->save();
         $results['item'] = $RMA->getJson();
         // 			if($printItAfterSave === true)
         // 				$results['printURL'] = '/print/rma/' . $RMA->getId() . '.html?pdf=1';
         Dao::commitTransaction();
     } catch (Exception $ex) {
         Dao::rollbackTransaction();
         // 			$errors[] = $ex->getMessage();
         $errors[] = $ex->getTraceAsString();
     }
     $param->ResponseData = StringUtilsAbstract::getJson($results, $errors);
 }
Esempio n. 11
0
 public function preSave()
 {
     $this->setProduct($this->getProductQtyLog()->getproduct())->setLastPurchaseTime($this->getProductQtyLog()->getCreated());
     switch ($this->getProductQtyLog()->getEntityName()) {
         case 'ReceivingItem':
             if (($receivingItem = ReceivingItem::get($this->getProductQtyLog()->getEntityId())) instanceof ReceivingItem) {
                 $this->setReceivingItem($receivingItem);
             }
             break;
         case 'PurchaseOrderItem':
             if (($purchaseOrderItem = PurchaseOrderItem::get($this->getProductQtyLog()->getEntityId())) instanceof PurchaseOrderItem) {
                 $this->setPurchaseOrderItem($purchaseOrderItem);
             }
             break;
         case 'OrderItem':
             if (($orderItem = OrderItem::get($this->getProductQtyLog()->getEntityId())) instanceof OrderItem) {
                 $this->setOrderItem($orderItem);
             }
             break;
         case 'CreditNoteItem':
             if (($creditNoteItem = CreditNoteItem::get($this->getProductQtyLog()->getEntityId())) instanceof CreditNoteItem) {
                 $this->setCreditNoteItem($creditNoteItem);
             }
             break;
     }
 }