Add pre_hold_offset. hook quantity + item scheduler.
[order_line_extra.git] / hooks.php
1 <?php
2 // ----------------------------------------------------------------
3 // $ Revision:  1.0 $
4 // Creator: Maxime Bourge
5 // date_:   2013-05-04
6 // Title:   Order Line Extra
7 // Free software under GNU GPL
8 // ----------------------------------------------------------------
9 define ('SS_ORDERLINEX', 100<<8);
10
11 include_once('includes/order_xtra_config.inc');
12 include_once('includes/db_order_lines.inc');
13
14 class hooks_order_line_extra extends hooks {
15         var $module_name = 'order_line_extra';
16
17         /*
18                 Install additonal menu options provided by module
19         */
20     function install_tabs($app) {
21         global $path_to_root;
22
23     }
24
25     function install_options($app) {
26         global $path_to_root;
27                                 switch($app->id) {
28                                         case 'orders':
29                                                 $app->add_rapp_function(0, _('Order Extra'), 
30                                                         $path_to_root.'/modules/order_line_extra/order_lines_view.php', 'SA_SALESTRANSVIEW');
31                                 }
32     }
33
34     function install_access()
35         {
36         $security_sections[SS_ORDERLINEX] =  _("Order Line Extra");
37                 $security_areas['SA_ORDERLINEX_EDIT'] = array(SS_ORDERLINEX|1, _("Edit lines"));
38
39                 return array($security_areas, $security_sections);
40         }
41
42     /* This method is called on extension activation for company.   */
43     function activate_extension($company, $check_only=true)
44     {
45         global $db_connections;
46
47         $updates = array(
48                                                 'alter_sales_order_details.sql' => array('sales_order_details','required_date'),
49                                                 'alter_sales_order_details_2.sql' => array('sales_order_details','expiry_date'),
50                                                 'create_denorm_order_details_queue.sql' => array('denorm_order_details_queue'),
51                                                 'create_denorm_qoh.sql' => array('denorm_qoh'),
52                                                 'create_order_summary_view.sql' => array('order_summary_view'),
53         );
54
55                                 return $this->update_databases($company, $updates, $check_only)
56                                                                 && update_queue_quantities()
57                                                                 && update_qoh_for_item();;
58     }
59
60     function deactivate_extension($company, $check_only=true)
61     {
62         global $db_connections;
63
64         $updates = array(
65             'clean_sales_order_details.sql' => array('ugly_hack') // FIXME: just an ugly hack to clean database on deactivation
66         );
67
68         return $this->update_databases($company, $updates, $check_only);
69     }
70
71                 function get_allowed_quantity($detail_id, $opts) {
72                         $location = array_shift($opts);
73                         $date = array_shift($opts);
74                         $qoh = array_shift($opts);
75
76                         $sql = "SELECT quantity_before, hold_until_date
77                                                         FROM ".TB_PREF."denorm_order_details_queue
78                                                         NATURAL JOIN ".TB_PREF."sales_order_details
79                                                         WHERE id = $detail_id";
80                         $result = db_query($sql);
81                         $quantity_before = 0;
82                         if($row=db_fetch($result)) {
83                                 $quantity_before = $row['quantity_before'];
84                                 $hold_until = sql2date($row['hold_until_date']);
85                                 if($qoh<=$quantity_before) return array(0, 'stockmankobg');
86                                 if(OrderXtraConfig::early($hold_until, $date)) return array(0, 'early');
87                                 }
88
89                         return array(max($qoh - $quantity_before, 0), '');
90                 }
91
92                 function db_postwrite($cart, $trans_type) {
93                         if(is_a($cart, "Cart")) {
94
95                         // It's a cart. Find all the stock_id and update the cache table 
96                         foreach($cart->line_items as $line_no => $item) {
97                                 $stock_id = $item->stock_id;
98                                 update_queue_quantity_for_item($stock_id);
99                                 update_qoh_for_item($stock_id);
100                                 }
101                         }
102                         else if (isset($trans_type) && $trans_type == "order_xtra") {
103                                 $stock_ids = stock_id_from_detail($cart);
104                                 if($stock_ids) foreach($stock_ids as $stock_id) {
105                                         update_queue_quantity_for_item($stock_id);
106                                         update_qoh_for_item($stock_id);
107                                         }
108                         }
109
110                         // update null fields of new entered orders.
111                 $types = array(ST_SALESORDER, 'order_xtra');
112                 if(in_array($trans_type, $types)) update_order_detail_defaults();
113         
114                 }
115
116
117                 function db_prevoid($cart, $trans_type) {
118                 // Simple version, rebuild everything
119                 $types = array(ST_CUSTCREDIT, ST_CUSTDELIVERY, ST_INVADJUST, ST_PURCHORDER, ST_WORKORDER, ST_MANUISSUE,
120                                 ST_SALESORDER, ST_SALESQUOTE, ST_MANURECEIVE);
121                 if(in_array($trans_type, $types)) {
122                         update_queue_quantities();
123                         update_qoh_for_item();
124                 }
125                 }
126 }
127
128 function stock_id_from_detail($detail_ids) {
129         if(empty($detail_ids)) return null;
130         $stock_ids = array();
131         $ids = implode(', ', $detail_ids);
132         $sql = " SELECT DISTINCT stk_code
133                                         FROM ".TB_PREF."sales_order_details
134                                         WHERE id IN ($ids)";
135         $result = db_query($sql);
136         while($row=db_fetch($result)) {
137                         array_push($stock_ids, $row['stk_code']);
138         }
139
140         return $stock_ids;
141 }
142 ?>