public function safeUp()
 {
     $table = craft()->db->schema->getTable('craft_market_lineitems');
     if (!isset($table->columns['salePrice'])) {
         $this->addColumnAfter('market_lineitems', 'salePrice', ['column' => ColumnType::Decimal, 'length' => '14', 'decimals' => '4'], 'saleAmount');
     }
     $lineItems = craft()->db->createCommand()->select('*')->from('market_lineitems')->where('purchasableId is not null')->queryAll();
     foreach ($lineItems as $lineItem) {
         $purchasable = craft()->db->createCommand()->select('*')->from('market_variants')->where('id = ' . $lineItem['purchasableId'])->queryRow();
         if ($purchasable) {
             $purchasable['isImplicit'] = $purchasable['isMaster'];
             $purchasable = Market_VariantModel::populateModel($purchasable);
             $onSale = false;
             if ($lineItem['saleAmount'] != 0) {
                 $onSale = true;
             }
             // ensure all snapshots from previous orders have the purchasable interface data in them.
             $snapshot = ['price' => $purchasable->getPrice(), 'sku' => $purchasable->getSku(), 'description' => $purchasable->getDescription(), 'purchasableId' => $purchasable->getPurchasableId(), 'cpEditUrl' => '#', 'onSale' => $onSale];
             // Add our purchasable data to the snapshot
             $snapshot = array_merge($purchasable->getSnapShot(), $snapshot);
             $snapshot_json = json_encode($snapshot);
             $salePrice = $lineItem['saleAmount'] + $lineItem['price'];
             craft()->db->createCommand()->insertOrUpdate('market_lineitems', ['id' => $lineItem['id'], 'purchasableId' => $lineItem['purchasableId'], 'orderId' => $lineItem['orderId']], ['snapshot' => $snapshot_json, 'salePrice' => $salePrice]);
         }
     }
     return true;
 }
 /**
  * @throws HttpException
  */
 public function actionSave()
 {
     $this->requireAdmin();
     $this->requirePostRequest();
     $variant = new Market_VariantModel();
     // Shared attributes
     $params = ['id', 'productId', 'sku', 'price', 'width', 'height', 'length', 'weight', 'stock', 'unlimitedStock', 'minQty', 'maxQty', 'isImplicit'];
     foreach ($params as $param) {
         $variant->{$param} = craft()->request->getPost($param);
     }
     $variant->setContentFromPost('fields');
     // Save it
     if (craft()->market_variant->save($variant)) {
         craft()->userSession->setNotice(Craft::t('Variant saved.'));
         $this->redirectToPostedUrl($variant);
     } else {
         craft()->userSession->setError(Craft::t('Couldn’t save variant.'));
     }
     // Send the model back to the template
     craft()->urlManager->setRouteVariables(['variant' => $variant]);
 }
 public function populateElementModel($row)
 {
     return Market_VariantModel::populateModel($row);
 }