OXID eShop CE  4.8.12
 All Classes Files Functions Variables Pages
order_article.php
Go to the documentation of this file.
1 <?php
2 
9 {
15  protected $_oSearchProduct = null;
16 
24  protected $_oSearchProductList = null;
25 
31  protected $_oMainSearchProduct = null;
32 
38  protected $_oEditObject = null;
39 
47  public function render()
48  {
50 
51  if ( $oOrder = $this->getEditObject() ) {
52  $this->_aViewData["edit"] = $oOrder;
53  $this->_aViewData["aProductVats"] = $oOrder->getProductVats(true);
54  }
55 
56  return "order_article.tpl";
57  }
58 
64  public function getEditObject()
65  {
66  $soxId = $this->getEditObjectId();
67  if ( $this->_oEditObject === null && isset( $soxId ) && $soxId != "-1" ) {
68  $this->_oEditObject = oxNew( "oxorder" );
69  $this->_oEditObject->load( $soxId );
70  }
71  return $this->_oEditObject;
72  }
73 
79  public function getSearchProductArtNr()
80  {
81  return oxConfig::getParameter( 'sSearchArtNum' );
82  }
83 
89  public function getSearchProduct()
90  {
91  if ( $this->_oSearchProduct === null ) {
92  $this->_oSearchProduct = false;
93  $sSearchArtNum = $this->getSearchProductArtNr();
94 
95  foreach ( $this->getProductList() as $oProduct ) {
96  if ( $oProduct->oxarticles__oxartnum->value == $sSearchArtNum ) {
97  $this->_oSearchProduct = $oProduct;
98  break;
99  }
100  }
101  }
102 
103  return $this->_oSearchProduct;
104  }
105 
111  public function getMainProduct()
112  {
113  if ( $this->_oMainSearchProduct === null && ( $sArtNum = $this->getSearchProductArtNr() ) ) {
114  $this->_oMainSearchProduct = false;
115  $sArtId = null;
116 
117  //get article id
118  $oDb = oxDb::getDb( oxDB::FETCH_MODE_ASSOC );
119  $sTable = getViewName( "oxarticles" );
120  $sQ = "select oxid, oxparentid from $sTable where oxartnum = ".$oDb->quote( $sArtNum )." limit 1";
121 
122  $rs = $oDb->execute( $sQ );
123  if ($rs != false && $rs->recordCount() > 0) {
124  $sArtId = $rs->fields['OXPARENTID'] ? $rs->fields['OXPARENTID'] : $rs->fields['OXID'];
125 
126  $oProduct = oxNew( "oxarticle" );
127  if ( $oProduct->load( $sArtId ) ) {
128  $this->_oMainSearchProduct = $oProduct;
129  }
130  }
131  }
132 
134  }
135 
141  public function getProductList()
142  {
143  if ( $this->_oSearchProductList === null ) {
144  $this->_oSearchProductList = oxNew( "oxlist" );
145 
146  // main search product is found?
147  if ( $oMainSearchProduct = $this->getMainProduct() ) {
148  // storing self to first list position
149  $this->_oSearchProductList->offsetSet( $oMainSearchProduct->getId(), $oMainSearchProduct );
150 
151  // adding variants..
152  foreach ( $oMainSearchProduct->getVariants() as $oVariant ) {
153  $this->_oSearchProductList->offsetSet( $oVariant->getId(), $oVariant );
154  }
155  }
156  }
157 
159  }
160 
166  public function addThisArticle()
167  {
168  $sOxid = oxConfig::getParameter( 'aid' );
169  $dAmount = oxConfig::getParameter( 'am' );
170  $oProduct = oxNew( "oxarticle" );
171 
172  if ( $sOxid && $dAmount && $oProduct->load( $sOxid ) ) {
173 
174  $sOrderId = $this->getEditObjectId();
175  $oOrder = oxNew( 'oxorder' );
176  if ( $sOrderId && $oOrder->load( $sOrderId ) ) {
177  $oOrderArticle = oxNew( 'oxorderArticle' );
178  $oOrderArticle->oxorderarticles__oxartid = new oxField( $oProduct->getId() );
179  $oOrderArticle->oxorderarticles__oxartnum = new oxField( $oProduct->oxarticles__oxartnum->value );
180  $oOrderArticle->oxorderarticles__oxamount = new oxField( $dAmount );
181  $oOrderArticle->oxorderarticles__oxselvariant = new oxField( oxConfig::getParameter( 'sel' ) );
182  $oOrder->recalculateOrder( array( $oOrderArticle ) );
183  }
184  }
185  }
186 
192  public function deleteThisArticle()
193  {
194  // get article id
195  $sOrderArtId = oxConfig::getParameter( 'sArtID' );
196  $sOrderId = $this->getEditObjectId();
197 
198  $oOrderArticle = oxNew( 'oxorderarticle' );
199  $oOrder = oxNew( 'oxorder' );
200 
201  // order and order article exits?
202  if ( $oOrderArticle->load( $sOrderArtId ) && $oOrder->load( $sOrderId ) ) {
203  $myConfig = $this->getConfig();
204 
205  // deleting record
206  $oOrderArticle->delete();
207 
208  // recalculating order
209  $oOrder->recalculateOrder();
210  }
211  }
212 
218  public function storno()
219  {
220  $myConfig = $this->getConfig();
221 
222  $sOrderArtId = oxConfig::getParameter( 'sArtID' );
223  $oArticle = oxNew( 'oxorderarticle' );
224  $oArticle->load( $sOrderArtId );
225 
226  if ( $oArticle->oxorderarticles__oxstorno->value == 1 ) {
227  $oArticle->oxorderarticles__oxstorno->setValue( 0 );
228  $sStockSign = -1;
229  } else {
230  $oArticle->oxorderarticles__oxstorno->setValue( 1 );
231  $sStockSign = 1;
232  }
233 
234  // stock information
235  if ( $myConfig->getConfigParam( 'blUseStock' ) ) {
236  $oArticle->updateArticleStock( $oArticle->oxorderarticles__oxamount->value * $sStockSign, $myConfig->getConfigParam('blAllowNegativeStock') );
237  }
238 
239  $oDb = oxDb::getDb();
240  $sQ = "update oxorderarticles set oxstorno = ".$oDb->quote( $oArticle->oxorderarticles__oxstorno->value )." where oxid = ".$oDb->quote( $sOrderArtId );
241  $oDb->execute( $sQ );
242 
243  //get article id
244  $sQ = "select oxartid from oxorderarticles where oxid = ".$oDb->quote( $sOrderArtId );
245  if ( ( $sArtId = oxDb::getDb()->getOne( $sQ, false, false ) ) ) {
246  $oOrder = oxNew( 'oxorder' );
247  if ( $oOrder->load( $this->getEditObjectId() ) ) {
248  $oOrder->recalculateOrder();
249  }
250  }
251  }
252 
258  public function updateOrder()
259  {
260  $aOrderArticles = oxConfig::getParameter( 'aOrderArticles' );
261 
262  $oOrder = oxNew( 'oxorder' );
263  if ( is_array( $aOrderArticles ) && $oOrder->load( $this->getEditObjectId() ) ) {
264 
265  $myConfig = $this->getConfig();
266  $oOrderArticles = $oOrder->getOrderArticles( true );
267 
268  $blUseStock = $myConfig->getConfigParam( 'blUseStock' );
269  foreach ( $oOrderArticles as $oOrderArticle ) {
270  $sItemId = $oOrderArticle->getId();
271  if ( isset( $aOrderArticles[$sItemId] ) ) {
272 
273  // update stock
274  if ( $blUseStock ) {
275  $oOrderArticle->setNewAmount( $aOrderArticles[$sItemId]['oxamount'] );
276  } else {
277  $oOrderArticle->assign( $aOrderArticles[$sItemId] );
278  $oOrderArticle->save();
279  }
280  }
281  }
282 
283  // recalculating order
284  $oOrder->recalculateOrder();
285  }
286  }
287 }