Use to pick table on dispatch.
authorMaxime Bourget <bmx007@gmail.com>
Sat, 15 Jun 2013 14:40:36 +0000 (15:40 +0100)
committerMaxime Bourget <bmx007@gmail.com>
Sat, 15 Jun 2013 15:11:57 +0000 (16:11 +0100)
hooks.php
includes/picking.inc

index 5b2624929ff188e7a14e3a2a66a747d1f5ef911d..026f2d2997a710d8c1dd5d84283bad7f9d4e50f3 100644 (file)
--- a/hooks.php
+++ b/hooks.php
@@ -8,8 +8,9 @@
 // ----------------------------------------------------------------
 define ('SS_ORDERLINEX', 100<<8);
 
-include_once('includes/order_xtra_config.inc');
-include_once('includes/db_order_lines.inc');
+require_once('includes/order_xtra_config.inc');
+require_once('includes/db_order_lines.inc');
+require_once('includes/picking.inc');
 
 class hooks_order_line_extra extends hooks {
        var $module_name = 'order_line_extra';
@@ -87,6 +88,38 @@ class hooks_order_line_extra extends hooks {
                        $date = array_shift($opts);
                        $qoh = array_shift($opts);
 
+                       /* We have 2 methods to estimate the allowed quantity.
+                        * the first use the different priority to estimate what's available.
+                        * This is the same algorithm used to auto pick items.
+                        * The second rely on what's being  choose to be picked up.
+                        * If an order has been selected to be picked up, only the items and the quantity picked up
+                        * needs to be available.
+                        */
+                       $sql = "SELECT count(*)
+                                                       FROM 0_sales_order_details
+                                                       WHERE order_no IN (SELECT distinct order_no
+                                                                                                                                       FROM 0_sales_order_details 
+                                                                                                                                       WHERE id = $detail_id)";
+
+                       $result = db_query($sql);
+                       if(db_num_rows($result) > 0) {
+                                       $result = db_query(pick_query($detail_id));
+                                       $row = db_fetch($result);
+                                       if($row) { 
+                                               $available = $row['quantity'];
+                                               return array($available, 'picked');
+                                       }
+                                       else {
+                                               return array(0, '');
+                                       }
+               
+                       }
+                       else {
+                               return get_allowed_quantity_from_priority($detail_id, $location, $date, $qoh);
+                       }
+               }
+
+               function get_allowed_quantity_from_priority($detail_id, $location, $date, $qoh) {
                        $sql = "SELECT quantity_before, hold_until_date
                                                        FROM ".TB_PREF."denorm_order_details_queue
                                                        NATURAL JOIN ".TB_PREF."sales_order_details
@@ -125,6 +158,9 @@ class hooks_order_line_extra extends hooks {
                        $types = array(ST_SALESORDER, 'order_xtra');
                        if(in_array($trans_type, $types)) update_order_detail_defaults();
 
+                       // Clean to_pick table on dispatch
+                       if($trans_type == ST_CUSTDELIVERY) clean_pickup_for_dispatch($cart);
+
                }
 
 
index d4141dc233e9972544361c683acabd69fe7e5e13..3591a7744c201eb398d23978d904440a67718e3e 100644 (file)
@@ -78,11 +78,14 @@ class Picker {
 
 }
 
-function pick_query() {
-       return "SELECT  detail_id, -sum(quantity) as quantity
+function pick_query($detail_id=null) {
+       $sql =  "SELECT  detail_id, -sum(quantity) as quantity
                                        FROM ".TB_PREF."topick
-                                       WHERE type IN ('order', 'booked')
-                                       GROUP BY detail_id ";
+                                       WHERE type IN ('order', 'booked')";
+       if($detail_id) $sql .= " AND detail_id = $detail_id";
+       else $sql .= " GROUP BY detail_id ";
+
+       return $sql;
 
 }
 
@@ -140,12 +143,16 @@ function insert_pick($sku, $order_id, $detail_id, $debtor_no, $branch_code, $to_
 
 }
 
-function update_pick($detail_id, $to_pick) {
-       // delete existing
+function delete_pick($detail_id) {
                $sql = "DELETE FROM ".TB_PREF."topick
                                                WHERE  detail_id = $detail_id
                                                ";
-               db_query($sql);
+               return db_query($sql);
+}
+
+function update_pick($detail_id, $to_pick) {
+       // delete existing
+               delete_pick($detail_id);
 
                if($to_pick ==0) return;
 
@@ -166,4 +173,10 @@ function update_pick($detail_id, $to_pick) {
        insert_pick($stock_id, $order_id, $detail_id, $debtor_no, $branch_code, $to_pick, $quantity);
 }
 
+function clean_pickup_for_dispatch($cart) {
+       foreach($cart->line_items as $line) {
+               delete_pick($line->src_id);
+       }
+}
+