order_article.php

Go to the documentation of this file.
00001 <?php
00002 
00008 class Order_Article extends oxAdminDetails
00009 {
00015     protected $_oSearchProduct = null;
00016 
00024     protected $_oSearchProductList = null;
00025 
00031     protected $_oMainSearchProduct = null;
00032 
00038     protected $_oEditObject = null;
00039 
00047     public function render()
00048     {
00049         parent::render();
00050 
00051         if ( $oOrder = $this->getEditObject() ) {
00052             $this->_aViewData["edit"] = $oOrder;
00053             $this->_aViewData["aProductVats"] = $oOrder->getProductVats(true);
00054         }
00055 
00056         return "order_article.tpl";
00057     }
00058 
00064     public function getEditObject()
00065     {
00066         $soxId = $this->getEditObjectId();
00067         if ( $this->_oEditObject === null && isset( $soxId ) && $soxId != "-1" ) {
00068             $this->_oEditObject = oxNew( "oxorder" );
00069             $this->_oEditObject->load( $soxId );
00070         }
00071         return $this->_oEditObject;
00072     }
00073 
00079     public function getSearchProductArtNr()
00080     {
00081         return oxConfig::getParameter( 'sSearchArtNum' );
00082     }
00083 
00089     public function getSearchProduct()
00090     {
00091         if ( $this->_oSearchProduct === null ) {
00092             $this->_oSearchProduct = false;
00093             $sSearchArtNum = $this->getSearchProductArtNr();
00094 
00095             foreach ( $this->getProductList() as $oProduct ) {
00096                 if ( $oProduct->oxarticles__oxartnum->value == $sSearchArtNum ) {
00097                     $this->_oSearchProduct = $oProduct;
00098                     break;
00099                 }
00100             }
00101         }
00102 
00103         return $this->_oSearchProduct;
00104     }
00105 
00111     public function getMainProduct()
00112     {
00113         if ( $this->_oMainSearchProduct === null && ( $sArtNum = $this->getSearchProductArtNr() ) ) {
00114             $this->_oMainSearchProduct = false;
00115             $sArtId = null;
00116 
00117             //get article id
00118             $oDb = oxDb::getDb(true);
00119             $sTable = getViewName( "oxarticles" );
00120             $sQ  = "select oxid, oxparentid from $sTable where oxartnum = ".$oDb->quote( $sArtNum )." limit 1";
00121 
00122             $rs  = $oDb->execute( $sQ );
00123             if ($rs != false && $rs->recordCount() > 0) {
00124                 $sArtId = $rs->fields['OXPARENTID'] ? $rs->fields['OXPARENTID'] : $rs->fields['OXID'];
00125 
00126                 $oProduct = oxNew( "oxarticle" );
00127                 if ( $oProduct->load( $sArtId ) ) {
00128                     $this->_oMainSearchProduct = $oProduct;
00129                 }
00130             }
00131         }
00132 
00133         return $this->_oMainSearchProduct;
00134     }
00135 
00141     public function getProductList()
00142     {
00143         if ( $this->_oSearchProductList === null ) {
00144             $this->_oSearchProductList = oxNew( "oxlist" );
00145 
00146             // main search product is found?
00147             if ( $oMainSearchProduct = $this->getMainProduct() ) {
00148                 // storing self to first list position
00149                 $this->_oSearchProductList->offsetSet( $oMainSearchProduct->getId(), $oMainSearchProduct );
00150 
00151                 // adding variants..
00152                 foreach ( $oMainSearchProduct->getVariants() as $oVariant ) {
00153                     $this->_oSearchProductList->offsetSet( $oVariant->getId(), $oVariant );
00154                 }
00155             }
00156         }
00157 
00158         return $this->_oSearchProductList;
00159     }
00160 
00166     public function addThisArticle()
00167     {
00168         $sOxid    = oxConfig::getParameter( 'aid' );
00169         $dAmount  = oxConfig::getParameter( 'am' );
00170         $oProduct = oxNew( "oxarticle" );
00171 
00172         if ( $sOxid && $dAmount && $oProduct->load( $sOxid ) ) {
00173 
00174             $sOrderId = $this->getEditObjectId();
00175             $oOrder   = oxNew( 'oxorder' );
00176             if ( $sOrderId && $oOrder->load( $sOrderId ) ) {
00177                 $oOrderArticle = oxNew( 'oxorderArticle' );
00178                 $oOrderArticle->oxorderarticles__oxartid  = new oxField( $oProduct->getId() );
00179                 $oOrderArticle->oxorderarticles__oxartnum = new oxField( $oProduct->oxarticles__oxartnum->value );
00180                 $oOrderArticle->oxorderarticles__oxamount = new oxField( $dAmount );
00181                 $oOrderArticle->oxorderarticles__oxselvariant = new oxField( oxConfig::getParameter( 'sel' ) );
00182                 $oOrder->recalculateOrder( array( $oOrderArticle ) );
00183             }
00184         }
00185     }
00186 
00192     public function deleteThisArticle()
00193     {
00194         // get article id
00195         $sOrderArtId = oxConfig::getParameter( 'sArtID' );
00196         $sOrderId = $this->getEditObjectId();
00197 
00198         $oOrderArticle = oxNew( 'oxorderarticle' );
00199         $oOrder = oxNew( 'oxorder' );
00200 
00201         // order and order article exits?
00202         if ( $oOrderArticle->load( $sOrderArtId ) && $oOrder->load( $sOrderId ) ) {
00203             $myConfig = $this->getConfig();
00204 
00205             // deleting record
00206             $oOrderArticle->delete();
00207 
00208             // recalculating order
00209             $oOrder->recalculateOrder();
00210         }
00211     }
00212 
00218     public function storno()
00219     {
00220         $myConfig = $this->getConfig();
00221 
00222         $sOrderArtId = oxConfig::getParameter( 'sArtID' );
00223         $oArticle = oxNew( 'oxorderarticle' );
00224         $oArticle->load( $sOrderArtId );
00225 
00226         if ( $oArticle->oxorderarticles__oxstorno->value == 1 ) {
00227             $oArticle->oxorderarticles__oxstorno->setValue( 0 );
00228             $sStockSign = -1;
00229         } else {
00230             $oArticle->oxorderarticles__oxstorno->setValue( 1 );
00231             $sStockSign = 1;
00232         }
00233 
00234         // stock information
00235         if ( $myConfig->getConfigParam( 'blUseStock' ) ) {
00236             $oArticle->updateArticleStock( $oArticle->oxorderarticles__oxamount->value * $sStockSign, $myConfig->getConfigParam('blAllowNegativeStock') );
00237         }
00238 
00239         $oDb = oxDb::getDb();
00240         $sQ = "update oxorderarticles set oxstorno = ".$oDb->quote( $oArticle->oxorderarticles__oxstorno->value )." where oxid = ".$oDb->quote( $sOrderArtId );
00241         $oDb->execute( $sQ );
00242 
00243         //get article id
00244         $sQ = "select oxartid from oxorderarticles where oxid = ".$oDb->quote( $sOrderArtId );
00245         if ( ( $sArtId = oxDb::getDb()->getOne( $sQ ) ) ) {
00246             $oOrder = oxNew( 'oxorder' );
00247             if ( $oOrder->load( $this->getEditObjectId() ) ) {
00248                 $oOrder->recalculateOrder();
00249             }
00250         }
00251     }
00252 
00258     public function updateOrder()
00259     {
00260         $aOrderArticles = oxConfig::getParameter( 'aOrderArticles' );
00261 
00262         $oOrder = oxNew( 'oxorder' );
00263         if ( is_array( $aOrderArticles ) && $oOrder->load( $this->getEditObjectId() ) ) {
00264 
00265             $myConfig = $this->getConfig();
00266             $oOrderArticles = $oOrder->getOrderArticles();
00267 
00268             $blUseStock = $myConfig->getConfigParam( 'blUseStock' );
00269             foreach ( $oOrderArticles as $oOrderArticle ) {
00270                 $sItemId = $oOrderArticle->getId();
00271                 if ( isset( $aOrderArticles[$sItemId] ) ) {
00272 
00273                     // update stock
00274                     if ( $blUseStock ) {
00275                         $oOrderArticle->setNewAmount( $aOrderArticles[$sItemId]['oxamount'] );
00276                     } else {
00277                         $oOrderArticle->assign( $aOrderArticles[$sItemId] );
00278                         $oOrderArticle->save();
00279                     }
00280                 }
00281             }
00282 
00283             // recalculating order
00284             $oOrder->recalculateOrder();
00285         }
00286     }
00287 }