From cf71853d2710e9e7c94f2de91f78e9c528a8ac67 Mon Sep 17 00:00:00 2001 From: Michael Jefferies Date: Mon, 9 Oct 2017 19:38:01 -0500 Subject: [PATCH] Improved handling for SortOrder field in certain tables --- QuickBooks/Callbacks/SQL/Callbacks.php | 108 +++++++++++-------------- QuickBooks/SQL/Schema.php | 100 +++++++++++++++++++++++ 2 files changed, 145 insertions(+), 63 deletions(-) diff --git a/QuickBooks/Callbacks/SQL/Callbacks.php b/QuickBooks/Callbacks/SQL/Callbacks.php index aa0e526a..15cd8245 100644 --- a/QuickBooks/Callbacks/SQL/Callbacks.php +++ b/QuickBooks/Callbacks/SQL/Callbacks.php @@ -8489,17 +8489,9 @@ protected static function _queryChildren($children, $keyID) foreach ($children as $child) { $sort = QUICKBOOKS_DRIVER_SQL_FIELD_ID . " ASC "; - switch (strtolower($child['table'])) + if (QuickBooks_SQL_Schema::hasSortOrder($child['table'])) { - case 'invoice_invoiceline': // @TODO There are a whole lot of other line item tables missing the SortOrder field still... - case 'salesreceipt_salesreceiptline': - case 'purchaseorder_purchaseorderline': - case 'estimate_estimateline': - case 'bill_itemline': - case 'bill_expenseline': - case 'iteminventoryassembly_iteminventoryassemblyline': - $sort = ' SortOrder ASC '; - break; + $sort = ' SortOrder ASC '; } $table = $child['table']; @@ -9575,14 +9567,13 @@ protected static function _deleteChildren($table, $user, $action, $ID, $object, $multipart = array( $value => $TxnID_or_ListID ); $order = array(); - if (substr($key, -4, 4) == 'line') + if (QuickBooks_SQL_Schema::hasSortOrder($key)) { - $order = array( 'SortOrder' => 'ASC', 'TxnLineID' => 'ASC' ); - - if ($key == 'iteminventoryassembly_iteminventoryassemblyline') - { - unset($order['TxnLineID']); - } + $order['SortOrder'] = 'ASC'; + } + if (QuickBooks_SQL_Schema::hasTxnLineID($key)) + { + $order['TxnLineID'] = 'ASC'; } $obj = new QuickBooks_SQL_Object($table, null); @@ -9615,16 +9606,15 @@ protected static function _deleteChildren($table, $user, $action, $ID, $object, } $multipart = array( $value => $TxnID_or_ListID ); - + $order = array(); - if (substr($key, -4, 4) == 'line') + if (QuickBooks_SQL_Schema::hasSortOrder($key)) { - $order = array( 'SortOrder' => 'ASC', 'TxnLineID' => 'ASC' ); - - if ($key == 'iteminventoryassembly_iteminventoryassemblyline') - { - unset($order['TxnLineID']); - } + $order['SortOrder'] = 'ASC'; + } + if (QuickBooks_SQL_Schema::hasTxnLineID($key)) + { + $order['TxnLineID'] = 'ASC'; } //print_r($multipart); @@ -10417,48 +10407,40 @@ protected static function _massageInsertRecord($table, &$object) } // Some types of objects need some special custom field handling - - $map = array( - 'invoice_invoiceline' => 'Invoice_TxnID', - 'purchaseorder_purchaseorderline' => 'PurchaseOrder_TxnID', - 'salesreceipt_salesreceiptline' => 'SalesReceipt_TxnID', - 'estimate_estimateline' => 'Estimate_TxnID', - 'bill_itemline' => 'Bill_TxnID', - 'bill_expenseline' => 'Bill_TxnID', - ); - - switch ($table) + $sortOrderColumns = QuickBooks_SQL_Schema::getSortOrderColumns($table); + if (count($sortOrderColumns) > 0) { - case 'invoice_invoiceline': - case 'purchaseorder_purchaseorderline': - case 'salesreceipt_salesreceiptline': - case 'estimate_estimateline': - case 'bill_itemline': - case 'bill_expenseline': - - // Set the SortOrder for the line items - if (isset($map[$table])) + $ids = array(); + foreach ($sortOrderColumns as $col) + { + $id = $object->get($col); + if ($id) { - $TxnID = $object->get($map[$table]); - - if ($TxnID) - { - $errnum = 0; - $errmsg = ''; - $res = $Driver->query(" - SELECT - MAX(SortOrder) AS max_sort_order - FROM - " . QUICKBOOKS_DRIVER_SQL_PREFIX_SQL . $table . " - WHERE - " . $map[$table] . " = '" . $TxnID . "' ", $errnum, $errmsg); - $arr = $Driver->fetch($res); - - $object->set('SortOrder', (int) $arr['max_sort_order'] + 1); - } + $ids[$col] = $id; + } + } + + if (count($ids) == count($sortOrderColumns)) + { + $sql = " + SELECT + MAX(SortOrder) AS max_sort_order + FROM + " . QUICKBOOKS_DRIVER_SQL_PREFIX_SQL . $table; + $prefix = " WHERE "; + foreach ($ids as $col => $id) + { + $sql .= $prefix . $col . " = '" . $id . "'"; + $prefix = " AND "; } + + $errnum = 0; + $errmsg = ''; + $res = $Driver->query($sql, $errnum, $errmsg); + $arr = $Driver->fetch($res); - break; + $object->set('SortOrder', (int) $arr['max_sort_order'] + 1); + } } return true; diff --git a/QuickBooks/SQL/Schema.php b/QuickBooks/SQL/Schema.php index b4955cd4..297e1f64 100755 --- a/QuickBooks/SQL/Schema.php +++ b/QuickBooks/SQL/Schema.php @@ -2828,4 +2828,104 @@ static public function mapFieldToSQLDefinition($object_type, $field, $qb_type) return array( $type, $length, $default ); } + + /** + * Get the columns that a table's SortOrder column is sorting within + * + * @param string $table + * @return array + */ + static public function getSortOrderColumns($table) + { + static $sortOrderColumns = array( + 'bill_expenseline' => array( 'Bill_TxnID' ), + 'bill_itemline' => array( 'Bill_TxnID' ), + 'bill_itemgroupline' => array( 'Bill_TxnID' ), + 'bill_itemgroupline_item_line' => array( 'Bill_TxnID', 'Bill_ItemGroupLine_TxnLineID' ), + 'check_expenseline' => array( 'Check_TxnID' ), + 'check_itemline' => array( 'Check_TxnID' ), + 'check_itemgroupline' => array( 'Check_TxnID' ), + 'check_itemgroupline_itemline' => array( 'Check_TxnID', 'Check_ItemGroupLine_TxnLineID' ), + 'creditcardcharge_expenseline' => array( 'CreditCardCharge_TxnID' ), + 'creditcardcharge_itemline' => array( 'CreditCardCharge_TxnID' ), + 'creditcardcharge_itemgroupline' => array( 'CreditCardCharge_TxnID' ), + 'creditcardcharge_itemgroupline_itemline' => array( 'CreditCardCharge_TxnID', 'CreditCardCharge_ItemGroupLine_TxnLineID' ), + 'creditcardcredit_expenseline' => array( 'CreditCardCredit_TxnID' ), + 'creditcardcredit_itemline' => array( 'CreditCardCredit_TxnID' ), + 'creditcardcredit_itemgroupline' => array( 'CreditCardCredit_TxnID' ), + 'creditcardcredit_itemgroupline_itemline' => array( 'CreditCardCredit_TxnID', 'CreditCardCredit_ItemGroupLine_TxnLineID' ), + 'creditmemo_creditmemoline' => array( 'CreditMemo_TxnID' ), + 'creditmemo_creditmemolinegroup' => array( 'CreditMemo_TxnID' ), + 'creditmemo_creditmemolinegroup_creditmemoline' => array( 'CreditMemo_TxnID', 'CreditMemo_CreditMemoLineGroup_TxnLineID' ), + 'deposit_depositline' => array( 'Deposit_TxnID' ), + 'estimate_estimateline' => array( 'Estimate_TxnID' ), + 'estimate_estimatelinegroup' => array( 'Estimate_TxnID' ), + 'estimate_estimatelinegroup_estimateline' => array( 'Estimate_TxnID', 'Estimate_EstimateLineGroup_TxnLineID' ), + 'inventoryadjustment_inventoryadjustmentline' => array( 'InventoryAdjustment_TxnID' ), + 'invoice_invoiceline' => array( 'Invoice_TxnID' ), + 'invoice_invoicelinegroup' => array( 'Invoice_TxnID' ), + 'invoice_invoicelinegroup_invoice_line' => array( 'Invoice_TxnID', 'Invoice_InvoiceLineGroup_TxnLineID' ), + 'itemgroup_itemgroupline' => array( 'ItemGroup_ListID' ), + 'iteminventoryassembly_iteminventoryassemblyline' => array( 'ItemInventoryAssembly_ListID' ), + 'itemreceipt_expenseline' => array( 'ItemReceipt_TxnID' ), + 'itemreceipt_itemline' => array( 'ItemReceipt_TxnID' ), + 'itemreceipt_itemgroupline' => array( 'ItemReceipt_TxnID' ), + 'itemreceipt_itemgroupline_itemline' => array( 'ItemReceipt_TxnID', 'ItemReceipt_ItemGroupLine_TxnLineID' ), + 'journalentry_journaldebitline' => array( 'JournalEntry_TxnID' ), + 'journalentry_journalcreditline' => array( 'JournalEntry_TxnID' ), + 'purchaseorder_purchaseorderline' => array( 'PurchaseOrder_TxnID' ), + 'purchaseorder_purchaseorderlinegroup' => array( 'PurchaseOrder_TxnID' ), + 'purchaseorder_purchaseorderlinegroup_purchaseorderline' => array( 'PurchaseOrder_TxnID', 'PurchaseOrder_PurchaseOrderLineGroup_TxnLineID' ), + 'salesorder_salesorderline' => array( 'SalesOrder_TxnID' ), + 'salesorder_salesorderlinegroup' => array( 'SalesOrder_TxnID' ), + 'salesorder_salesorderlinegroup_salesorderline' => array( 'SalesOrder_TxnID', 'SalesOrder_SalesOrderLineGroup_TxnLineID' ), + 'salesreceipt_salesreceiptline' => array( 'SalesReceipt_TxnID' ), + 'salesreceipt_salesreceiptlinegroup' => array( 'SalesReceipt_TxnID' ), + 'salesreceipt_salesreceiptlinegroup_salesreceiptline' => array( 'SalesReceipt_TxnID', 'SalesReceipt_SalesReceiptLineGroup_TxnLineID' ), + 'vendorcredit_expenseline' => array( 'VendorCredit_TxnID' ), + 'vendorcredit_itemline' => array( 'VendorCredit_TxnID' ), + 'vendorcredit_itemgroupline' => array( 'VendorCredit_TxnID' ), + 'vendorcredit_itemgroupline_itemline' => array( 'VendorCredit_TxnID', 'VendorCredit_ItemGroupLine_TxnLineID' ), + ); + + $key = strtolower($table); + if (array_key_exists($key, $sortOrderColumns)) { + return $sortOrderColumns[$key]; + } + + return array(); + } + + /** + * Check if the table has a SortOrder column + * + * @param string $table + * @return boolean + */ + static public function hasSortOrder($table) + { + $columns = self::getSortOrderColumns($table); + return count($columns) > 0; + } + + /** + * Check if the table has a TxnLineID column + * + * @param string $table + * @return boolean + */ + static public function hasTxnLineID($table) + { + $key = strtolower($table); + // all tables with sort order have a TxnLineID, except the following: + if ($key == 'itemgroup_itemgroupline' || $key == 'iteminventoryassembly_iteminventoryassemblyline') + { + return false; + } + else + { + return self::hasSortOrder($table); + } + } } + \ No newline at end of file