Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
order.inc
1 <?php
17 require_once SQ_PACKAGES_PATH.'/cms/form/form_submission/form_submission.inc';
18 require_once SQ_PACKAGES_PATH.'/ecommerce/orders/order_line/order_line.inc';
19 
31 class Order extends Form_Submission
32 {
33 
34 
43  function Order($assetid=0)
44  {
45  $this->_ser_attrs = true;
46  $this->Asset($assetid);
47 
48  }//end constructor
49 
50 
58  function _getAllowedLinks()
59  {
60  $order_links = parent::_getAllowedLinks();
61  $order_links[SQ_LINK_TYPE_3]['order_line'] = Array('card' => 'M');
62  return $order_links;
63 
64  }//end _getAllowedLinks()
65 
66 
73  function total()
74  {
75  $total = 0.0;
76 
77  $lines = $this->getLines();
78  foreach($lines as $assetid) {
79  $line =& $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid);
80  $total += $line->attr('quantity') * $line->attr('price');
81  }
82 
83  return $total;
84 
85  }//end total()
86 
87 
94  function itemCount()
95  {
96  $count = 0.0;
97 
98  $lines = $this->getLines();
99  foreach($lines as $assetid) {
100  $line =& $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid);
101  $count += $line->attr('quantity');
102  }
103 
104  return $count;
105 
106  }//end itemCount()
107 
108 
115  function lineCount()
116  {
117  $lines = $this->getLines();
118  return count($lines);
119 
120  }//end lineCount()
121 
122 
132  function getLines()
133  {
134  $links = $GLOBALS['SQ_SYSTEM']->am->getLinks($this->id, SQ_LINK_TYPE_3, 'order_line', false);
135 
136  $assetids = Array();
137 
138  foreach($links as $link) {
139  $assetids[] = $link['minorid'];
140  }
141 
142  return $assetids;
143 
144  }//end getLines()
145 
146 
157  function createLine($product_assetid, $price, $quantity=1)
158  {
159  // create and populate a new order line
160  $new_order_line = new Order_Line();
161 
162  $new_order_line->setAttrValue('quantity', $quantity);
163  $new_order_line->setAttrValue('price', $price);
164  $new_order_line->setAttrValue('product_assetid', $product_assetid);
165 
166  // prepare type 3 link between order and orderline
167  $link = array('asset' => &$this, 'link_type' => SQ_LINK_TYPE_3, 'is_dependant' => 1, 'is_exclusive' => 1);
168 
169  // attempt to link orderline to order
170  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
171  $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
172  $GLOBALS['SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
173  $success = $new_order_line->create($link);
174  $GLOBALS['SQ_SYSTEM']->restoreRunLevel();
175 
176  if ($success) {
177  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
178  } else {
179  $GLOBALS['SQ_SYSTEM']->doTransaction('ROLLBACK');
180  }
181 
182  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
183  return $success;
184 
185  }//end createLine()
186 
187 
198  function buildLinesFromCart($cart_items, $clear_after=true)
199  {
200  $ecommerce_checkout_id = $cart_items->attr('ecommerce_checkout_id');
201  // cart items is passed in straight from $_SESSION['sq_cart_contents'][$ecommerce_checkout_id];
202  foreach($_SESSION['sq_cart_contents'][$ecommerce_checkout_id] as $product_assetid => $product_info) {
203  $this->createLine($product_assetid, $product_info['price'], $product_info['quantity']);
204  }
205 
206  // are we clearing the cart afterwards
207  if ($clear_after) {
208  unset($_SESSION['sq_cart_contents'][$ecommerce_checkout_id]);
209  }
210 
211  return true;
212 
213  }//end buildLinesFromCart()
214 
215 
216 }//end class
217 
218 ?>