Fix get_allowed and change to get_dispatchable quantity hook
authorMaxime Bourget <bmx007@gmail.com>
Fri, 21 Jun 2013 10:46:59 +0000 (11:46 +0100)
committerMaxime Bourget <bmx007@gmail.com>
Fri, 21 Jun 2013 10:46:59 +0000 (11:46 +0100)
hooks.php

index 3f146c71448c72ea5e15940d39022d2fa0b0eea7..480d503de71722d6b84e9965dd7c58272f379580 100644 (file)
--- a/hooks.php
+++ b/hooks.php
@@ -83,11 +83,14 @@ class hooks_order_line_extra extends hooks {
                                return $this->update_databases($company, $updates, $check_only);
                }
 
-               function get_allowed_quantity($detail_id, $opts) {
+               function get_dispatchable_quantity($line_item, $opts) {
                        $location = array_shift($opts);
                        $date = array_shift($opts);
                        $qoh = array_shift($opts);
 
+                       $detail_id = $line_item->id;
+                       $quantity = $line_item->qty_dispatched;
+
                        /* 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.
@@ -112,33 +115,47 @@ class hooks_order_line_extra extends hooks {
                                        $row = db_fetch($result);
                                        if($row) { 
                                                $available = $row['quantity'];
-                                               return array($available, 'picked limited');
+                                               if($available == 0)  {
+                                                       return array(0, $qoh ? 'limited' : 'stockmankobg');
+                                               }
+                                               else if($available < $quantity) {
+                                                       return array($available, 'picked');
+                                               }
+                                               else {
+                                                       return array($quantity, 'picked');
+                                               }
                                        }
                                        else {
-                                               return array(0, '');
+                                               // hasn't been picked
+                                               return array(0, 'stockmankobg soldout');
                                        }
                
                        }
                        else {
-                               return $this->get_allowed_quantity_from_priority($detail_id, $location, $date, $qoh);
+                               return $this->get_allowed_quantity_from_priority($detail_id, $quantity,  $location, $date, $qoh);
                        }
                }
 
-               function get_allowed_quantity_from_priority($detail_id, $location, $date, $qoh) {
+               function get_allowed_quantity_from_priority($detail_id, $quantity,  $location, $date, $qoh) {
+                       if($qoh == 0) return array(0, 'stockmankobg');
                        $sql = "SELECT quantity_before, hold_until_date
                                                        FROM ".TB_PREF."denorm_order_details_queue
                                                        JOIN ".TB_PREF."sales_order_details USING (id)
                                                        WHERE id = $detail_id";
                        $result = db_query($sql);
-                       $quantity_before = 0;
+
+                       $classes = array();
+                       $dispatchable = $quantity;
                        if($row=db_fetch($result)) {
                                $quantity_before = $row['quantity_before'];
+                               $qoh = max(0, $qoh-$quantity_before);
                                $hold_until = sql2date($row['hold_until_date']);
-                               if($qoh<=$quantity_before) return array(0, 'stockmankobg');
-                               if(OrderXtraConfig::early($hold_until, $date)) return array(0, 'early');
+                               $dispatchable = min($quantity, $qoh);
+                               if($dispatchable < $quantity) array_push($classes, 'limited');
+                               if(OrderXtraConfig::early($hold_until, $date)) array_push($classes, 'early');
                                }
 
-                       return array(max($qoh - $quantity_before, 0), 'limited');
+                       return array($dispatchable, implode(' ', $classes));
                }
 
                function db_postwrite($cart, $trans_type) {