Esempio n. 1
0
 /**
  * Import Orders
  *
  * @param string $lastUpdatedTime The datatime string
  *
  * @return B2BConnector
  */
 public function importOrders($lastUpdatedTime = '')
 {
     $totalItems = 0;
     $this->_log(0, get_class($this), 'starting ...', self::LOG_TYPE, 'start', __FUNCTION__);
     if (($lastUpdatedTime = trim($lastUpdatedTime)) === '') {
         $this->_log(0, get_class($this), 'Getting the last updated time', self::LOG_TYPE, '$lastUpdatedTime is blank', __FUNCTION__);
         // 			$lastImportTime = new UDate(SystemSettings::getSettings(SystemSettings::TYPE_B2B_SOAP_LAST_IMPORT_TIME), SystemSettings::getSettings(SystemSettings::TYPE_B2B_SOAP_TIMEZONE));
         $lastUpdatedTime = trim(SystemSettings::getSettings(SystemSettings::TYPE_B2B_SOAP_LAST_IMPORT_TIME));
     }
     //getting the lastest order since last updated time
     $orders = $this->getlastestOrders($lastUpdatedTime);
     $this->_log(0, get_class($this), 'Found ' . count($orders) . ' order(s) since "' . $lastUpdatedTime . '".', self::LOG_TYPE, '', __FUNCTION__);
     if (is_array($orders) && count($orders) > 0) {
         $transStarted = false;
         try {
             try {
                 Dao::beginTransaction();
             } catch (Exception $e) {
                 $transStarted = true;
             }
             foreach ($orders as $index => $order) {
                 $this->_log(0, get_class($this), 'Found order from Magento with orderNo = ' . trim($order->increment_id) . '.', self::LOG_TYPE, '', __FUNCTION__);
                 $order = $this->getOrderInfo(trim($order->increment_id));
                 if (!is_object($order)) {
                     $this->_log(0, get_class($this), 'Found no object from $order, next element!', self::LOG_TYPE, '$index = ' . $index, __FUNCTION__);
                     continue;
                 }
                 if (($status = trim($order->state)) === '') {
                     $this->_log(0, get_class($this), 'Found no state Elment from $order, next element!', self::LOG_TYPE, '$index = ' . $index, __FUNCTION__);
                     continue;
                 }
                 //saving the order
                 $orderDate = new UDate(trim($order->created_at), SystemSettings::getSettings(SystemSettings::TYPE_B2B_SOAP_TIMEZONE));
                 $orderDate->setTimeZone('UTC');
                 // 				$totalPaid = (!isset($order->total_paid) ? 0 : trim($order->total_paid));
                 $shippingAddr = $billingAddr = null;
                 if (($o = Order::getByOrderNo(trim($order->increment_id))) instanceof Order) {
                     //skip, if order exsits
                     $this->_log(0, get_class($this), 'Found order from DB, ID = ' . $o->getId(), self::LOG_TYPE, '$index = ' . $index, __FUNCTION__);
                     continue;
                     // 					$shippingAddr = $o->getShippingAddr();
                     // 					$billingAddr = $o->getBillingAddr();
                 }
                 $o = new Order();
                 $this->_log(0, get_class($this), 'Found no order from DB, create new', self::LOG_TYPE, '$index = ' . $index, __FUNCTION__);
                 $customer = Customer::create(isset($order->billing_address) && isset($order->billing_address->company) && trim($order->billing_address->company) !== '' ? trim($order->billing_address->company) : (isset($order->customer_firstname) ? trim($order->customer_firstname) . ' ' . trim($order->customer_lastname) : ''), '', trim($order->customer_email), $this->_createAddr($order->billing_address, $billingAddr), true, '', $this->_createAddr($order->shipping_address, $shippingAddr), isset($order->customer_id) ? trim($order->customer_id) : 0);
                 $o->setOrderNo(trim($order->increment_id))->setOrderDate(trim($orderDate))->setTotalAmount(trim($order->grand_total))->setStatus(strtolower($status) === 'canceled' ? OrderStatus::get(OrderStatus::ID_CANCELLED) : OrderStatus::get(OrderStatus::ID_NEW))->setIsFromB2B(true)->setShippingAddr($customer->getShippingAddress())->setBillingAddr($customer->getBillingAddress())->setCustomer($customer)->save();
                 $this->_log(0, get_class($this), 'Saved the order, ID = ' . $o->getId(), self::LOG_TYPE, '$index = ' . $index, __FUNCTION__);
                 $totalShippingCost = StringUtilsAbstract::getValueFromCurrency(trim($order->shipping_amount)) * 1.1;
                 //create order info
                 $this->_createOrderInfo($o, OrderInfoType::get(OrderInfoType::ID_CUS_NAME), trim($customer->getName()))->_createOrderInfo($o, OrderInfoType::get(OrderInfoType::ID_CUS_EMAIL), trim($customer->getEmail()))->_createOrderInfo($o, OrderInfoType::get(OrderInfoType::ID_QTY_ORDERED), intval(trim($order->total_qty_ordered)))->_createOrderInfo($o, OrderInfoType::get(OrderInfoType::ID_MAGE_ORDER_STATUS), trim($order->status))->_createOrderInfo($o, OrderInfoType::get(OrderInfoType::ID_MAGE_ORDER_STATE), trim($order->state))->_createOrderInfo($o, OrderInfoType::get(OrderInfoType::ID_MAGE_ORDER_TOTAL_AMOUNT), trim($order->grand_total))->_createOrderInfo($o, OrderInfoType::get(OrderInfoType::ID_MAGE_ORDER_SHIPPING_METHOD), trim($order->shipping_description))->_createOrderInfo($o, OrderInfoType::get(OrderInfoType::ID_MAGE_ORDER_SHIPPING_COST), $totalShippingCost)->_createOrderInfo($o, OrderInfoType::get(OrderInfoType::ID_MAGE_ORDER_PAYMENT_METHOD), !isset($order->payment) ? '' : (!isset($order->payment->method) ? '' : trim($order->payment->method)));
                 $this->_log(0, get_class($this), 'Updated order info', self::LOG_TYPE, '$index = ' . $index, __FUNCTION__);
                 //saving the order item
                 $totalItemCost = 0;
                 foreach ($order->items as $item) {
                     $this->_createItem($o, $item);
                     $totalItemCost = $totalItemCost * 1 + StringUtilsAbstract::getValueFromCurrency($item->row_total) * 1.1;
                 }
                 if (($possibleSurchargeAmount = $o->getTotalAmount() - $totalShippingCost - $totalItemCost) > 0 && ($product = Product::getBySku('surcharge')) instanceof Product) {
                     OrderItem::create($o, $product, $possibleSurchargeAmount, 1, $possibleSurchargeAmount);
                 }
                 //record the last imported time for this import process
                 SystemSettings::addSettings(SystemSettings::TYPE_B2B_SOAP_LAST_IMPORT_TIME, trim($order->created_at));
                 $this->_log(0, get_class($this), 'Updating the last updated time :' . trim($order->created_at), self::LOG_TYPE, '', __FUNCTION__);
                 $totalItems++;
             }
             if ($transStarted === false) {
                 Dao::commitTransaction();
             }
         } catch (Exception $e) {
             if ($transStarted === false) {
                 Dao::rollbackTransaction();
             }
             throw $e;
         }
     }
     $this->_log(0, get_class($this), $lastUpdatedTime . " => " . SystemSettings::getSettings(SystemSettings::TYPE_B2B_SOAP_LAST_IMPORT_TIME) . ' => ' . $totalItems, self::LOG_TYPE, '', __FUNCTION__);
     return $this;
 }
Esempio n. 2
0
 /**
  * (non-PHPdoc)
  * @see BaseEntityAbstract::preSave()
  */
 public function preSave()
 {
     if (trim($this->getInvDate()) === '') {
         $this->setInvDate(Udate::zeroDate());
     }
     if (trim($this->getId()) !== '') {
         //status changed
         $originalOrder = self::get($this->getId());
         if ($originalOrder instanceof Order && $originalOrder->getStatus()->getId() !== $this->getStatus()->getId()) {
             $infoType = OrderInfoType::get(OrderInfoType::ID_MAGE_ORDER_STATUS_BEFORE_CHANGE);
             $orderInfos = OrderInfo::find($this, $infoType, false, 1, 1);
             $orderInfo = count($orderInfos) === 0 ? null : $orderInfos[0];
             OrderInfo::create($this, $infoType, $originalOrder->getStatus()->getId(), $orderInfo);
             $this->addLog('Changed Status from [' . $originalOrder->getStatus()->getName() . '] to [' . $this->getStatus() . ']', Log::TYPE_SYSTEM, 'Auto Log', get_class($this) . '::' . __FUNCTION__);
             //get the required kits qty
             if (in_array(intval($this->getStatus()->getId()), array(OrderStatus::ID_PICKED, OrderStatus::ID_SHIPPED))) {
                 $sql = "select sum(ord_item.qtyOrdered) `requiredQty`, ord_item.productId `productId`, pro.sku `sku` from orderitem ord_item inner join product pro on (pro.id = ord_item.productId and pro.isKit = 1) where ord_item.orderId = ? and ord_item.active = 1 group by pro.id";
                 $result = Dao::getResultsNative($sql, array($this->getId()), PDO::FETCH_ASSOC);
                 if (count($result) > 0) {
                     $errMsg = array();
                     foreach ($result as $row) {
                         $requiredQty = $row['requiredQty'];
                         $sku = $row['sku'];
                         if (($kitsCount = count($kits = Kit::getAllByCriteria('soldOnOrderId = ? and productId =? ', array($this->getId(), $row['productId'])))) < $requiredQty) {
                             $errMsg[] = 'Product (SKU=' . $sku . ') needs to be sold as a kit(req Qty=' . $requiredQty . ', providedQty=' . $kitsCount . '): ' . implode(', ', array_map(create_function('$a', 'return $a->getBarcode();'), $kits));
                         }
                     }
                     if (count($errMsg) > 0) {
                         throw new EntityException(implode('; ', $errMsg));
                     }
                 }
             }
         }
     }
 }