BulkUpdater work.
[order_line_extra.git] / hooks.php
index 2425ba93d8822dd3995798b2b3fe5ffac3558bd7..6cb3ff82a94031d362d3eaae98b39ad03098adbf 100644 (file)
--- a/hooks.php
+++ b/hooks.php
@@ -8,6 +8,8 @@
 // ----------------------------------------------------------------
 define ('SS_ORDERLINEX', 100<<8);
 
+include_once('includes/db_order_lines.inc');
+
 class hooks_order_line_extra extends hooks {
        var $module_name = 'order_line_extra';
 
@@ -42,10 +44,16 @@ class hooks_order_line_extra extends hooks {
         global $db_connections;
 
         $updates = array(
-            'alter_sales_order_details.sql' => array('sales_order_details', 'required_date')
+                                               'alter_sales_order_details.sql' => array('sales_order_details','required_date'),
+                                               'alter_sales_order_details_2.sql' => array('sales_order_details','expiry_date'),
+                                               'create_denorm_order_details_queue.sql' => array('denorm_order_details_queue'),
+                                               'create_denorm_qoh.sql' => array('denorm_qoh'),
+                                               'create_order_summary_view.sql' => array('order_summary_view'),
         );
 
-        return $this->update_databases($company, $updates, $check_only);
+                               return $this->update_databases($company, $updates, $check_only)
+                                                               && update_queue_quantities()
+                                                               && update_qoh_for_item();;
     }
 
     function deactivate_extension($company, $check_only=true)
@@ -58,5 +66,70 @@ class hooks_order_line_extra extends hooks {
 
         return $this->update_databases($company, $updates, $check_only);
     }
+
+               function get_allowed_quantity($detail_id, $opts) {
+                       $location = array_shift($opts);
+                       $date = array_shift($opts);
+                       $qoh = array_shift($opts);
+
+                       $sql = "select quantity_before from ".TB_PREF."denorm_order_details_queue where id = $detail_id";
+                       $result = db_query($sql);
+                       $quantity_before = 0;
+                       if($row=db_fetch($result)) {
+                               $quantity_before = $row['quantity_before'];
+                               }
+
+                       return max($qoh - $quantity_before, 0);
+               }
+
+               function db_postwrite($cart, $trans_type) {
+                       if(is_a($cart, "Cart")) {
+
+                       // It's a cart. Find all the stock_id and update the cache table 
+                       foreach($cart->line_items as $line_no => $item) {
+                               $stock_id = $item->stock_id;
+                               update_queue_quantity_for_item($stock_id);
+                               update_qoh_for_item($stock_id);
+                               }
+                       }
+                       else if (isset($trans_type) && $trans_type == "order_xtra") {
+                               $stock_ids = stock_id_from_detail($cart);
+                               if($stock_ids) foreach($stock_ids as $stock_id) {
+                                       update_queue_quantity_for_item($stock_id);
+                                       update_qoh_for_item($stock_id);
+                                       }
+                       }
+
+                       // update null fields of new entered orders.
+               $types = array(ST_SALESORDER, 'order_xtra');
+               if(in_array($trans_type, $types)) update_order_detail_defaults();
+       
+               }
+
+
+               function db_prevoid($cart, $trans_type) {
+               // Simple version, rebuild everything
+               $types = array(ST_CUSTCREDIT, ST_CUSTDELIVERY, ST_INVADJUST, ST_PURCHORDER, ST_WORKORDER, ST_MANUISSUE,
+                               ST_SALESORDER, ST_SALESQUOTE, ST_MANURECEIVE);
+               if(in_array($trans_type, $types)) {
+                       update_queue_quantities();
+                       update_qoh_for_item();
+               }
+               }
+}
+
+function stock_id_from_detail($detail_ids) {
+       if(empty($detail_ids)) return null;
+       $stock_ids = array();
+       $ids = implode(', ', $detail_ids);
+       $sql = " SELECT DISTINCT stk_code
+                                       FROM ".TB_PREF."sales_order_details
+                                       WHERE id IN ($ids)";
+       $result = db_query($sql);
+       while($row=db_fetch($result)) {
+                       array_push($stock_ids, $row['stk_code']);
+       }
+
+       return $stock_ids;
 }
 ?>