예제 #1
0
 /**
  * Calculates pending order qty to count against available
  * inventory by searching for Requested or Awaiting Processing orders
  * Because we have our cart table and our documents table, we have to get both numbers
  */
 public function CalculateReservedInventory()
 {
     //Pending orders not yet converted to Invoice
     $intReservedA = $this->getDbConnection()->createCommand("SELECT SUM(qty) FROM " . CartItem::model()->tableName() . " AS a\n\t\t\t\t\tLEFT JOIN " . Cart::model()->tableName() . " AS b ON a.cart_id=b.id\n\t\t\t\t\tLEFT JOIN " . Document::model()->tableName() . " AS c ON b.document_id=c.id\n\t\t\t\t\tWHERE\n\t\t\t\t\ta.product_id=" . $this->id . " AND b.cart_type=" . CartType::order . "\n\t\t\t\t\tAND (b.status='" . OrderStatus::Requested . "' OR b.status='" . OrderStatus::AwaitingProcessing . "'\n\t\t\t\t\t\tOR b.status='" . OrderStatus::Downloaded . "')\n\t\t\t\t\tAND (c.status IS NULL OR c.status='" . OrderStatus::Requested . "');")->queryScalar();
     if (empty($intReservedA)) {
         $intReservedA = 0;
     }
     //Unattached orders (made independently in Lightspeed)
     $intReservedB = $this->getDbConnection()->createCommand("SELECT SUM(qty) from " . DocumentItem::model()->tableName() . " AS a\n\t\t\t\t\tLEFT JOIN " . Document::model()->tableName() . " AS b ON a.document_id=b.id\n\t\t\t\t\tWHERE\n\t\t\t\t\ta.product_id=" . $this->id . " AND b.order_type=" . CartType::order . "\n\t\t\t\t\tAND cart_id IS NULL AND left(order_str,3)='WO-' AND (b.status='" . OrderStatus::Requested . "');")->queryScalar();
     if (empty($intReservedB)) {
         $intReservedB = 0;
     }
     return $intReservedA + $intReservedB;
 }
 /**
  * Document Flush
  *
  * @param string $passkey
  * @return string
  */
 public function document_flush($passkey)
 {
     if (!$this->check_passkey($passkey)) {
         return self::FAIL_AUTH;
     }
     if (_xls_get_conf('DEBUG_RESET', 0) == 1) {
         Yii::log('Skipped document flush operation due to DEBUG mode', CLogger::LEVEL_WARNING, 'application.' . __CLASS__ . "." . __FUNCTION__);
         return self::OK;
     }
     try {
         SroRepair::model()->deleteAll();
         SroItem::model()->deleteAll();
         Sro::model()->deleteAll();
         Cart::model()->updateAll(array('document_id' => null));
         //We need to make Document items for anything not Invoiced manual to roll back
         $objCarts = Document::model()->findAll("order_type = :type AND (status=:status1 OR status=:status2 OR status=:status3)", array(':type' => CartType::order, ':status1' => OrderStatus::Requested, ':status2' => OrderStatus::Processed, ':status3' => OrderStatus::PartiallyReceived));
         foreach ($objCarts as $objCart) {
             foreach ($objCart->documentItems as $item) {
                 $item->qty = 0;
                 $item->save();
                 $item->product->SetAvailableInventory();
                 $item->delete();
             }
             $objCart->delete();
         }
         //Then delete everytihing else
         DocumentItem::model()->deleteAll();
         Document::model()->deleteAll();
         Yii::app()->db->createCommand("alter table " . Document::model()->tableName() . " auto_increment=1;")->execute();
         Yii::app()->db->createCommand("alter table " . DocumentItem::model()->tableName() . " auto_increment=1;")->execute();
         Yii::app()->db->createCommand("alter table " . Sro::model()->tableName() . " auto_increment=1;")->execute();
         Yii::app()->db->createCommand("alter table " . SroItem::model()->tableName() . " auto_increment=1;")->execute();
         Yii::app()->db->createCommand("alter table " . SroRepair::model()->tableName() . " auto_increment=1;")->execute();
         //We shouldn't have anything in the cart table that's not an original order, so remove the following if they exist
         //ToDo: this shouldn't be in production because we will have removed Quote lines from _cart during install
         $objCarts = Cart::model()->findAllByAttributes(array('cart_type' => CartType::quote));
         foreach ($objCarts as $objCart) {
             foreach ($objCart->cartItems as $item) {
                 $item->delete();
             }
             $objCart->delete();
         }
         //ToDo: this shouldn't be in production because we will have removed SRO lines from _cart during install
         $objCarts = Cart::model()->findAllByAttributes(array('cart_type' => CartType::sro));
         foreach ($objCarts as $objCart) {
             foreach ($objCart->cartItems as $item) {
                 $item->delete();
             }
             $objCart->delete();
         }
         //ToDo: this shouldn't be in production because we will have removed O- from _cart during install
         //Delete anything here that's just a pure Order from LS from our Cart Table
         $objCarts = Cart::model()->findAll("cart_type = :type and id_str LIKE 'O-%'", array(':type' => CartType::order));
         foreach ($objCarts as $objCart) {
             foreach ($objCart->cartItems as $item) {
                 $item->delete();
             }
             $objCart->delete();
         }
         //Delete any Web Orders that have been reuploaded from Lightspeed already
         $objCarts = Cart::model()->findAll("cart_type = :type AND status<>:status1 AND status<>:status2 AND status<>:status3", array(':type' => CartType::order, ':status1' => OrderStatus::AwaitingPayment, ':status2' => OrderStatus::AwaitingProcessing, ':status3' => OrderStatus::Downloaded));
         foreach ($objCarts as $objCart) {
             foreach ($objCart->cartItems as $item) {
                 $item->delete();
             }
             $objCart->delete();
         }
         //Delete any carts older than our timeout that don't have a customer ID attached (since those can always be restored)
         Cart::GarbageCollect();
     } catch (Exception $e) {
         Yii::log('SOAP ERROR : In flushing document tables ' . $e, 'error', 'application.' . __CLASS__ . "." . __FUNCTION__);
         return self::UNKNOWN_ERROR;
     }
     return self::OK;
 }