Source for file SHOP_ORDER.phpclass

Documentation is available at SHOP_ORDER.phpclass

  1. <?php
  2. /**
  3.   * openCSP class file SHOP_ORDER.phpclass
  4.   *
  5.   * @project Open CSP-Management
  6.   * @package shop
  7.   *
  8.   * @author Peter Krebs <pitlinz@users.sourceforge.net>
  9.   * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  10.   *
  11.   * @since pk-15.10.2008
  12.   * @version $Id: SHOP_ORDER.phpclass,v 1.1 2008/10/16 02:21:48 pitlinz Exp $
  13.   */
  14.  
  15.     // ---------------------------------------------------------
  16.     // requirements
  17.     // ---------------------------------------------------------
  18.  
  19.     pcf_require_interface('INFA_SHOP_ORDER',dirname(__FILE__"/");
  20.     pcf_require_class('DBMS_TABLEOBJ',__OCSP_PHPINCPATH__ "db/");
  21.     
  22.     pcf_require_class('CLIENT',"client/");
  23.     pcf_require_class('ADDRESS',"client/");
  24.     pcf_require_class('OCSP_USER',"user/");
  25.     pcf_require_class('SHOP_ORDERITEM',"shop/");
  26.  
  27. /**
  28.   * openCSP class SHOP_ORDER
  29.   *
  30.   * @project Open CSP-Management
  31.   * @package shop
  32.   *
  33.   * @author Peter Krebs <pitlinz@users.sourceforge.net>
  34.   * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  35.   *
  36.   * @since pk-15.10.2008
  37.   * @version $Id: SHOP_ORDER.phpclass,v 1.1 2008/10/16 02:21:48 pitlinz Exp $
  38.   */
  39. class SHOP_ORDER extends DBMS_TABLEOBJ implements INFA_SHOP_ORDER
  40. {
  41.     // ---------------------------------------------------------------------------
  42.     // constants
  43.     // ---------------------------------------------------------------------------
  44.     
  45.     /**
  46.      * @constant string CLASS_SRC_FILE
  47.      */
  48.     const CLASS_SRC_FILE = __FILE__;
  49.  
  50.     // ---------------------------------------------------------------------------
  51.     // class (static)
  52.     // ---------------------------------------------------------------------------
  53.     
  54.     /*** class vars ------------------------------------------------------ */
  55.     
  56.     protected static $loadedOrders array();
  57.     
  58.     /*** class methods --------------------------------------------------- */
  59.     
  60.     /**
  61.      * returns an instance of the unit of work
  62.      *
  63.      * @param int $aId 
  64.      * 
  65.      * @return SHOP_ORDER 
  66.      */
  67.     public static function &getInstance($aId)
  68.     {
  69.         if (!isset(self::$loadedOrders[$aId]|| pcf_class_implements(self::$loadedOrders[$aId],'INFA_SHOP_ORDER'))
  70.         {
  71.             self::$loadedOrders[$aIdSHOP_ORDER::factoryFromId(intval($aId));
  72.         }
  73.         
  74.         return self::$loadedOrders[$aId];
  75.     }
  76.     
  77.     /**
  78.      * returns if we already have an instance of $aId
  79.      *
  80.      * @param int $aId 
  81.      * @return boolean 
  82.      */
  83.     public static function isInstanced($aId)
  84.     {
  85.         if (isset(self::$loadedOrders[$aId]))
  86.         {
  87.             return pcf_class_implements(self::$loadedOrders[$aId],'INFA_SHOP_ORDER');
  88.         }
  89.         return False;
  90.     }
  91.     
  92.     /**
  93.      * adds a newsletter to the unit of work
  94.      * 
  95.      * @param SHOP_ORDER $aOrder 
  96.      */
  97.     public static function setInstance(&$aOrder)
  98.     {
  99.         if (pcf_class_implements($aOrder,'INFA_SHOP_ORDER'))
  100.         {
  101.             if (intval($aOrder->getId()))
  102.             {
  103.                 self::$loadedOrders[$aOrder->getId()$aOrder;
  104.             }
  105.         }
  106.     }    
  107.     
  108.     // ---------------------------------------------------------------------------
  109.     // object vars
  110.     // ---------------------------------------------------------------------------
  111.     
  112.     /*** compostion --------------------------------------------------- */
  113.     
  114.     protected $myItems = array();
  115.     
  116.     /*** attributes  -------------------------------------------------- */
  117.     
  118.     /**
  119.      * the database table
  120.      *
  121.      * @var string 
  122.      */
  123.     protected $myTable = "T_SHOP_ORDER";    
  124.     
  125.     /**
  126.      * populate timestamp of $this->myItems
  127.      *
  128.      * @var double $myItemsPopulateTS (timestamp)
  129.      */    
  130.     protected $myItemsPopulateTS = 0;
  131.     
  132.     // ---------------------------------------------------------------------------
  133.     // factory / construct
  134.     // ---------------------------------------------------------------------------
  135.     
  136.     /**
  137.      * returns a new order object
  138.      *
  139.      * @param int $aId 
  140.      * 
  141.      * @return SHOP_ORDER 
  142.      */
  143.     public static function factoryFromId($aId)
  144.     {
  145.         $obj_ret new SHOP_ORDER();
  146.         $obj_ret->populateFromKeys(array('ORD_ID' => intval($aId)));
  147.         return $obj_ret;
  148.     }
  149.     
  150.     
  151.     // ---------------------------------------------------------------------------
  152.     // getter / setter
  153.     // ---------------------------------------------------------------------------    
  154.  
  155.     /**
  156.      * returns the order id
  157.      *
  158.      * @return int 
  159.      */
  160.     public function getId()
  161.     {
  162.         return $this->getDBField('ORD_ID');
  163.     }
  164.     
  165.     // ---------------------------------------------------------------------------
  166.     // DB methods
  167.     // ---------------------------------------------------------------------------    
  168.     
  169.     /**
  170.       * inserts a new row to the table
  171.       *
  172.       * @param boolean   $debug  show debug info
  173.       *
  174.       * @returns int   returns the autoIncFld ID if exists or 1 on success
  175.       *
  176.       */
  177.     public function dbInsert($debug=False)
  178.     {
  179.         if ($debugechoDebugMethod(__FILE__,get_class($this),"NWL_LETTER::dbInsert()");
  180.  
  181.         $this->setDBField('ORD_CREATEDON','CURRENT_TIMESTAMP');
  182.         $this->setDBField('ORD_CREATEDBY',OCSP_OBJ::currentUser(True)->getId());
  183.         $this->setDBField('ORD_CREATEDIP',$_SERVER['REMOTE_ADDR']);
  184.         $this->setDBField('ORD_CREATEDSRV',$_SERVER['SERVER_NAME']);        
  185.         
  186.         if (!($int_iAddr intval($this->getDBField('ORD_INV_ADDR'))))
  187.         {
  188.             if ($int_iAddr $this->getInvoiceAddrId())
  189.             {
  190.                 $this->setDBField('ORD_INV_ADDR',$int_iAddr);
  191.             }
  192.         }
  193.         
  194.         if (intval($int_iAddr&& ($obj_addr ADDRESS::getInstance($int_iAddr)))
  195.         {
  196.             if ($obj_client $this->getClient())
  197.             {
  198.                 $arr_data $obj_client->getDBVal();
  199.             else {
  200.                 $arr_data array();
  201.             }
  202.             
  203.             $arr_data array_merge($obj_addr->getDBVal(),$arr_data);            
  204.             $this->setDBField('ORD_INV_ADDR_DATA',base64_encode(serialize($arr_data)));
  205.         }
  206.                             
  207.         if ($int_id parent::dbInsert($debug))
  208.         {
  209.             if (sizeof($this->myItems))
  210.             {
  211.                 foreach($this->myItems as &$obj_item)
  212.                 {
  213.                     $obj_item->setOrderId($int_id);
  214.                     $obj_item->dbInsert($debug);
  215.                 }
  216.             }
  217.         }
  218.         
  219.         return $int_id;
  220.     }    
  221.     
  222.     // ---------------------------------------------------------------------------
  223.     // Client Methods
  224.     // ---------------------------------------------------------------------------    
  225.     
  226.     /**
  227.      * returns the client id
  228.      * 
  229.      * @return int 
  230.      */
  231.     public function getCliId()
  232.     {
  233.         if ($int_id intval($this->getDBField('CLI_ID')))
  234.         {
  235.             return $int_id;
  236.         }
  237.         
  238.         if ($int_usrId intval($this->getDBField('USR_ID')))
  239.         {
  240.             if ($obj_user OCSP_USER::factoryFromId($int_usrId))
  241.             {
  242.                 return $obj_user->getCliId();
  243.             }
  244.         }
  245.         
  246.         return 0;
  247.         
  248.     }
  249.     
  250.     /**
  251.      * sets the client id
  252.      *
  253.      * @param unknown_type $aId 
  254.      */
  255.     public function setCliId($aId)
  256.     {
  257.         $this->setDBFieldIfDiff('CLI_ID',intval($aId));
  258.     }
  259.     
  260.     /**
  261.      * returns the client
  262.      *
  263.      * @return CLIENT 
  264.      */
  265.     public function &getClient()
  266.     {
  267.         return CLIENT::getInstance(intval($this->getCliId()));
  268.     }
  269.     
  270.     /**
  271.      * returns the invoice address id
  272.      *
  273.      * @return int 
  274.      */
  275.     public function getInvoiceAddrId()
  276.     {
  277.         if ($int_id $this->getDBField('ORD_INV_ADDR'))
  278.         {
  279.             return $int_id;
  280.         }
  281.         
  282.         if ($obj_cli $this->getClient())
  283.         {
  284.             return $obj_cli->getPriAddrId();
  285.         }        
  286.     }
  287.     
  288.     /**
  289.      * returns the invoice address DB values
  290.      * 
  291.      * @return array 
  292.      */
  293.     public function getInvoiceAddrDBVals()
  294.     {
  295.         $str_val $this->getDBField('ORD_INV_ADDR_DATA');
  296.         if (!empty($str_val))
  297.         {
  298.             return unserialize(base64_decode($str_val));
  299.         }
  300.         return array('ADR_ID'=>0,'CLI_ID'=>$this->getClientId());
  301.     }
  302.  
  303.     // ---------------------------------------------------------------------------
  304.     // item Methods
  305.     // ---------------------------------------------------------------------------    
  306.     
  307.     /**
  308.      * returns if $this->myItems is populated
  309.      *
  310.      * @return int 
  311.      */
  312.     public function itemsArePopulated()
  313.     {
  314.         return ($this->myItemsPopulateTS >= (time(ini_get('max_execution_time')))
  315.     }
  316.     
  317.     /**
  318.      * populates the order items into $this->myItems
  319.      * 
  320.      * @return int (number of items loaded);
  321.      */
  322.     public function populateItems()
  323.     {
  324.         $this->myItems = array();
  325.         $arr_filter array('ORD_ID' => $this->getId());
  326.         if ($arr_items OCSP_OBJ::defaultReadDBObj()->getArray('T_SHOP_ORDER_ITEM',$arr_filter,0,0,"PRO_ID,PAR_ID"))        
  327.         {
  328.             foreach($arr_items as $arr_row)
  329.             {
  330.                 $this->myItems[SHOP_ORDERITEM::factoryFromRow($arr_row);    
  331.             }
  332.         }
  333.         
  334.         $this->myItemsPopulateTS = time();
  335.         return sizeof($this->myItems);
  336.     }
  337.     
  338.     /**
  339.      * adds a part as an item
  340.      *
  341.      * @param PRO_PART $aPart 
  342.      * @param float $quant 
  343.      * @param array $arr_attribs 
  344.      * 
  345.      * @todo check if part and attribs alread exists
  346.      * 
  347.      */
  348.     public function &addItem($aPart,$quant,$arr_attribs=Null)
  349.     {
  350.         $obj_item new SHOP_ORDERITEM();
  351.         try {
  352.             $obj_item->setPart($aPart);
  353.             $obj_item->setQuant(floatval($quant));
  354.             $obj_item->setAttributes($arr_attribs);
  355.             if (!$this->itemsArePopulated()) 
  356.             {
  357.                 $this->populateItems();
  358.             }
  359.             
  360.             $int_idx sizeof($this->myItems);
  361.             $this->myItems[$int_idx$obj_item;
  362.             return $this->myItems[$int_idx]
  363.             
  364.         catch (Exception $e{
  365.             return Null;
  366.         }
  367.         
  368.     }
  369.  
  370. }
  371.  
  372. ?>

Documentation generated on Thu, 08 Jan 2009 17:48:31 +0100 by phpDocumentor 1.4.0a2