public function testFormatAttribute() { $qp = new QuoteProduct(); $qp->adjustmentType = 'percent'; $qp->adjustment = 5; $this->assertEquals('5%', $qp->formatAttribute('adjustment')); }
/** * Clear out records associated with this quote before deletion. */ public function beforeDelete() { QuoteProduct::model()->deleteAllByAttributes(array('quoteId' => $this->id)); // for old relationships generated with incorrect type name Relationships::model()->deleteAllByAttributes(array('firstType' => 'quotes', 'firstId' => $this->id)); // generate action record for history $contact = $this->contact; if (!empty($contact)) { $action = new Actions(); $action->associationType = 'contacts'; $action->type = 'quotesDeleted'; $action->associationId = $contact->id; $action->associationName = $contact->name; $action->assignedTo = Yii::app()->getSuModel()->username; $action->completedBy = Yii::app()->getSuModel()->username; $action->createDate = time(); $action->dueDate = time(); $action->completeDate = time(); $action->visibility = 1; $action->complete = 'Yes'; $action->actionDescription = "Deleted Quote: <span style=\"font-weight:bold;\">{$this->id}</span> {$this->name}"; // Save after deletion of the model so that this action itself doensn't get deleted $action->save(); } return parent::beforeDelete(); }
public function actionDeleteProduct($id) { $model = $this->getModel($id); if (isset($_GET['productId'])) { QuoteProduct::model()->deleteAllByAttributes(array('quoteId' => $id, 'productId' => $_GET['productId'])); } if ($_GET['contactId']) { Yii::app()->clientScript->scriptMap['*.js'] = false; $contact = X2Model::model('Contacts')->findByPk($_GET['contactId']); $this->renderPartial('quoteFormWrapper', array('contactId' => $contact->id, 'accountName' => $contact->company), false, true); } }
public function testSetLineItems() { $errMsg = 'Did not throw exception when attempting to set w/invalid value for lineItems property.'; $quote = $this->quotes('lineItems'); ob_start(); try { $quote->lineItems = 1; $this->assertTrue(false, $errMsg); } catch (Exception $e) { $this->assertTrue(true); } try { $quote->lineItems = array(1); $this->assertTrue(false, $errMsg); } catch (Exception $e) { $this->assertTrue(true); } try { $altQuote = new Quote(); $quote->lineItems = array($altQuote); $this->assertTrue(false, $errMsg); } catch (Exception $e) { $this->assertTrue(true); } ob_end_clean(); // These exceptions should not result in the array being changed $this->assertEquals(9, count($quote->lineItems), 'Exceptions thrown, but lineItems changed!'); $lineItems = array('preexist_keep_1' => array('id' => 1, 'lineNumber' => 1, 'adjustmentType' => 'linear', 'name' => 'test item #1 new name', 'currency' => 'USD', 'price' => 1, 'quantity' => 1), 'preexist_keep_2' => array('id' => 2, 'lineNumber' => 2, 'adjustmentType' => 'linear', 'name' => 'test item #2 new name', 'currency' => 'USD', 'price' => 2, 'quantity' => 2), 'preexist_keep_3' => array('id' => 5, 'lineNumber' => 5, 'adjustmentType' => 'percent', 'name' => 'test item #5 new name', 'currency' => 'USD', 'price' => 5, 'quantity' => 5), 'preexist_keep_4' => array('id' => 6, 'lineNumber' => 6, 'adjustmentType' => 'linear', 'name' => 'test item #6 new name', 'currency' => 'USD', 'price' => 6, 'quantity' => 6), 'preexist_keep_5' => array('id' => 7, 'lineNumber' => 7, 'adjustmentType' => 'totalPercent', 'name' => 'test item #7 new name', 'currency' => 'USD', 'price' => 7, 'quantity' => 7), 'preexist_keep_6' => array('id' => 9, 'lineNumber' => 9, 'adjustmentType' => 'totalPercent', 'name' => 'test item # new name', 'currency' => 'USD', 'price' => 9, 'quantity' => 9), 'new_1' => array('lineNumber' => 3, 'adjustmentType' => 'linear', 'name' => 'new test item #1 new name', 'currency' => 'USD', 'price' => 9, 'quantity' => 9), 'new_2' => array('lineNumber' => 4, 'adjustmentType' => 'linear', 'name' => 'new test item #2 new name', 'currency' => 'USD', 'price' => 9, 'quantity' => 9), 'new_3' => array('lineNumber' => 8, 'adjustmentType' => 'totalPercent', 'name' => 'new test item #3 new name', 'currency' => 'USD', 'price' => 9, 'quantity' => 9)); $lineItemsNoAlias = array_values($lineItems); // var_dump(array_map(function($li){return $li->attributes;},$quote->lineItems)); $quote->setLineItems($lineItemsNoAlias, true); // var_dump(array_map(function($li){return $li->attributes;},$quote->lineItems)); $this->assertFalse($quote->hasLineItemErrors, "Quote has line item errors: " . CJSON::encode($quote->lineItemErrors)); // echo "\nNew line item id:lineNumber:\n"; // foreach ($quote->lineItems as $item) // echo "{$item->id}:{$item->lineNumber} "; $newLineItemSet = $quote->lineItems; $itemsByLine = array(); $dbItemsByLine = array(); // Check that the array in the object was set properly: foreach ($newLineItemSet as $item) { $itemsByLine[$item->lineNumber] = $item; } foreach ($lineItems as $alias => $item) { if (!array_key_exists($item['lineNumber'], $itemsByLine)) { $this->assertTrue(false, "Item at line {$item['lineNumber']} missing from lineItems array for quote {$quote->id}."); } foreach ($item as $attr => $value) { if ($attr != 'id') { $this->assertEquals($value, $itemsByLine[$item['lineNumber']]->{$attr}, "Attribute value assertion failed on {$alias} ({$attr}), line {$item['lineNumber']}."); } } $this->assertEquals($quote->id, $itemsByLine[$item['lineNumber']]->quoteId, "Failed asserting that quoteId was set properly."); } // Prepare reference arrays for database comparisons $newLineItemSet = QuoteProduct::model()->findAllByAttributes(array('quoteId' => $quote->id)); $itemsByLine = array(); $dbItemsByLine = array(); foreach ($lineItems as $item) { $itemsByLine[$item['lineNumber']] = $item; } foreach ($newLineItemSet as $item) { $this->assertNotEmpty($item->lineNumber, "Item ID #{$item->id} was inserted with null line number."); $dbItemsByLine[$item->lineNumber] = $item; } $deletedIds = array(); $newLines = array(); foreach (range(1, 3) as $pos) { $deletedIds[] = (int) $this->lineItems["preexist_delete_{$pos}"]['id']; $line = (int) $lineItems["new_{$pos}"]['lineNumber']; $newLines[(int) $line] = "new_{$pos}"; $updateLines[(int) $line] = "preexist_keep_{$pos}"; } foreach (range(4, 6) as $pos) { $updateLines[(int) $line] = "preexist_keep_{$pos}"; } // Check that the deleted items were deleted: $deleted = QuoteProduct::model()->findAllByPk($deletedIds); $this->assertEmpty($deleted, "Failed asserting that line items were deleted properly."); // Check that new items were inserted: $new = QuoteProduct::model()->findAllBySql("SELECT * FROM x2_quotes_products WHERE quoteId={$quote->id} AND lineNumber IN (" . implode(',', array_keys($newLines)) . ')'); $this->assertNotEmpty($new, "Failed asserting that any new line items were inserted."); $this->assertEquals(3, count($new), "Failed asserting that all line items were inserted."); foreach ($new as $item) { $alias = $newLines[$item->lineNumber]; foreach ($lineItems[$alias] as $attr => $value) { $this->assertEquals($value, $item->{$attr}, "New line item at key {$alias} was not inserted with proper values."); } } // Check that existing items were updated: foreach ($lineItems as $alias => $item) { if (!array_key_exists($item['lineNumber'], $dbItemsByLine)) { $this->assertTrue(false, "Item at line {$item['lineNumber']} not found in the database for quote {$quote->id}." . (array_key_exists('id', $item) ? "ID: {$item['id']}" : '')); } foreach ($item as $attr => $value) { $this->assertEquals($value, $dbItemsByLine[$item['lineNumber']]->{$attr}, "Attribute value assertion failed on {$alias} ({$attr})."); } } }
public function run() { Yii::app()->clientScript->registerScriptFile(Yii::app()->theme->getBaseUrl() . '/css/gridview/jquery.yiigridview.js'); $relationships = Relationships::model()->findAllByAttributes(array('firstType' => 'quotes', 'secondType' => 'contacts', 'secondId' => $this->contactId)); echo '<div id="quotes-form">'; echo '<div id="wide-quote-form" class="wide form" style="overflow: visible;">'; echo '<span style="font-weight:bold; font-size: 1.5em;">' . Yii::t('quotes', "Quotes") . '</span>'; echo '<br /><br />'; // get a list of products for adding to quotes $products = Product::model()->findAll(array('select' => 'id, name')); $productNames = array(0 => ''); foreach ($products as $product) { $productNames[$product->id] = $product->name; } $quotes = Quote::model()->findAllByAttributes(array('associatedContacts' => $this->contactId)); foreach ($quotes as $quote) { $products = Product::model()->findAll(array('select' => 'id, name, price')); $quoteProducts = QuoteProduct::model()->findAllByAttributes(array('quoteId' => $quote->id)); // find associated products and their quantities $quotesProducts = QuoteProduct::model()->findAllByAttributes(array('quoteId' => $quote->id)); $orders = array(); // array of product-quantity pairs $total = 0; // total price for the quote foreach ($quotesProducts as $qp) { $price = $qp->price * $qp->quantity; if ($qp->adjustmentType == 'percent') { $price += $price * ($qp->adjustment / 100); $qp->adjustment = "{$qp->adjustment}%"; } else { $price += $qp->adjustment; } $orders[] = array('name' => $qp->name, 'id' => $qp->productId, 'unit' => $qp->price, 'quantity' => $qp->quantity, 'adjustment' => $qp->adjustment, 'price' => $price); $order = end($orders); $total += $order['price']; } $dataProvider = new CArrayDataProvider($orders, array('keyField' => 'name', 'sort' => array('attributes' => array('name', 'unit', 'quantity', 'price')), 'pagination' => array('pageSize' => false))); $newProductId = "new_product_" . $quote->id; $this->render('viewQuotes', array('quote' => $quote, 'contactId' => $this->contactId, 'dataProvider' => $dataProvider, 'products' => $products, 'productNames' => $productNames, 'orders' => $quoteProducts, 'total' => $total)); } // Mini Create Quote Form $model = new Quote(); $this->render('createQuote', array('model' => $model, 'contactId' => $this->contactId, 'productNames' => $productNames)); echo "</div>"; echo "</div>"; }
public function activeProducts() { $products = Product::model()->findAllByAttributes(array('status' => 'Active')); $inactive = Product::model()->findAllByAttributes(array('status' => 'Inactive')); $quoteProducts = QuoteProduct::model()->findAll(array('select' => 'productId', 'condition' => 'quoteId=:quoteId', 'params' => array(':quoteId' => $this->id))); foreach ($quoteProducts as $qp) { foreach ($inactive as $i) { if ($qp->productId == $i->id) { $products[] = $i; } } } return $products; }