예제 #1
0
 /**
  * Convert a QuickBooks_XML_Node object to a QuickBooks_Object_* object instance 
  * 
  * @param QuickBooks_XML_Node $XML
  * @param string $action_or_object
  * @return QuickBooks_Object
  */
 public static function fromXML($XML, $action_or_object = null)
 {
     if (!$action_or_object or $action_or_object == QUICKBOOKS_QUERY_ITEM) {
         $action_or_object = $XML->name();
     }
     $type = QuickBooks_Utilities::actionToObject($action_or_object);
     $exceptions = array(QUICKBOOKS_OBJECT_SERVICEITEM => 'ServiceItem', QUICKBOOKS_OBJECT_INVENTORYITEM => 'InventoryItem', QUICKBOOKS_OBJECT_NONINVENTORYITEM => 'NonInventoryItem', QUICKBOOKS_OBJECT_DISCOUNTITEM => 'DiscountItem', QUICKBOOKS_OBJECT_FIXEDASSETITEM => 'FixedAssetItem', QUICKBOOKS_OBJECT_GROUPITEM => 'GroupItem', QUICKBOOKS_OBJECT_OTHERCHARGEITEM => 'OtherChargeItem', QUICKBOOKS_OBJECT_SALESTAXITEM => 'SalesTaxItem', QUICKBOOKS_OBJECT_SALESTAXGROUPITEM => 'SalesTaxGroupItem', QUICKBOOKS_OBJECT_SUBTOTALITEM => 'SubtotalItem', QUICKBOOKS_OBJECT_INVENTORYASSEMBLYITEM => 'InventoryAssemblyItem');
     if (isset($exceptions[$type])) {
         $type = $exceptions[$type];
     }
     //print('trying to create type: {' . $type . '}' . "\n");
     $class = 'QuickBooks_QBXML_Object_' . ucfirst(strtolower($type));
     if (true) {
         $Object = QuickBooks_QBXML_Object::_fromXMLHelper($class, $XML);
         if (!is_object($Object)) {
             return false;
         }
         $children = array();
         switch ($Object->object()) {
             case QUICKBOOKS_OBJECT_RECEIVEPAYMENT:
                 $children = array('AppliedToTxnRet' => array('QuickBooks_QBXML_Object_ReceivePayment_AppliedToTxn', 'addAppliedToTxn'));
                 break;
             case QUICKBOOKS_OBJECT_BILL:
                 $children = array('ItemLineRet' => array('QuickBooks_QBXML_Object_Bill_ItemLine', 'addItemLine'), 'ExpenseLineRet' => array('QuickBooks_QBXML_Object_Bill_ExpenseLine', 'addExpenseLine'));
                 break;
             case QUICKBOOKS_OBJECT_PURCHASEORDER:
                 $children = array('PurchaseOrderLineRet' => array('QuickBooks_QBXML_Object_PurchaseOrder_PurchaseOrderLine', 'addPurchaseOrderLine'));
                 break;
             case QUICKBOOKS_OBJECT_INVOICE:
                 $children = array('InvoiceLineRet' => array('QuickBooks_QBXML_Object_Invoice_InvoiceLine', 'addInvoiceLine'));
                 break;
             case QUICKBOOKS_OBJECT_ESTIMATE:
                 $children = array('EstimateLineRet' => array('QuickBooks_QBXML_Object_Estimate_EstimateLine', 'addEstimateLine'));
                 break;
             case QUICKBOOKS_OBJECT_SALESRECEIPT:
                 $children = array('SalesReceiptLineRet' => array('QuickBooks_QBXML_Object_SalesReceipt_SalesReceiptLine', 'addSalesReceiptLine'));
                 break;
             case QUICKBOOKS_OBJECT_JOURNALENTRY:
                 $children = array('JournalCreditLineRet' => array('QuickBooks_QBXML_Object_JournalEntry_JournalCreditLine', 'addCreditLine'), 'JournalDebitLineRet' => array('QuickBooks_QBXML_Object_JournalEntry_JournalDebitLine', 'addDebitLine'));
                 break;
             case QUICKBOOKS_OBJECT_SALESTAXGROUPITEM:
                 $children = array('ItemSalesTaxRef' => array('QuickBooks_QBXML_Object_SalesTaxGroupItem_ItemSalesTaxRef', 'addItemSalesTaxRef'));
                 break;
             case QUICKBOOKS_OBJECT_UNITOFMEASURESET:
                 $children = array('RelatedUnit' => array('QuickBooks_QBXML_Object_UnitOfMeasureSet_RelatedUnit', 'addRelatedUnit'), 'DefaultUnit' => array('QuickBooks_QBXML_Object_UnitOfMeasureSet_DefaultUnit', 'addDefaultUnit'));
                 break;
         }
         foreach ($children as $node => $tmp) {
             $childclass = $tmp[0];
             $childmethod = $tmp[1];
             if (class_exists($childclass)) {
                 foreach ($XML->children() as $ChildXML) {
                     if ($ChildXML->name() == $node) {
                         $ChildObject = QuickBooks_QBXML_Object::_fromXMLHelper($childclass, $ChildXML);
                         $Object->{$childmethod}($ChildObject);
                     }
                 }
             } else {
                 print 'Missing class: ' . $childclass . "\n";
             }
         }
         return $Object;
     }
     return false;
 }