Add Ajax onclick radio. With big hack
[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_ORDERLINEX_EDIT');
31                                                 break;
32                                         case 'stock':
33                                                 $app->add_rapp_function(1, _('Item Schedule'), 
34                                                         $path_to_root.'/modules/order_line_extra/item_schedule.php', 'SA_ORDERLINEX_SCHEDULE');
35                                                 break;
36                                         case 'system':
37                                                 $app->add_rapp_function(1, _('Reset Order Xtra cache'), 
38                                                         $path_to_root.'/modules/order_line_extra/refresh_cache.php', 'SA_ORDERLINEX_REFRESH');
39                                                 break;
40                                 }
41
42                 }
43
44                 function install_access()
45         {
46                                 $security_sections[SS_ORDERLINEX] =  _("Order Line Extra");
47                 $security_areas['SA_ORDERLINEX_EDIT'] = array(SS_ORDERLINEX|1, _("Edit lines"));
48                 $security_areas['SA_ORDERLINEX_SCHEDULE'] = array(SS_ORDERLINEX|1, _("Item Schedule"));
49                 $security_areas['SA_ORDERLINEX_REFRESH'] = array(SS_ORDERLINEX|1, _("Reset Cache"));
50
51                 return array($security_areas, $security_sections);
52         }
53
54                 /* This method is called on extension activation for company.   */
55                 function activate_extension($company, $check_only=true)
56                 {
57                                 global $db_connections;
58
59                                 $updates = array(
60                                                 'alter_sales_order_details.sql' => array('sales_order_details','required_date'),
61                                                 'alter_sales_order_details_2.sql' => array('sales_order_details','expiry_date'),
62                                                 'create_denorm_order_details_queue.sql' => array('denorm_order_details_queue'),
63                                                 'create_denorm_qoh.sql' => array('denorm_qoh'),
64                                                 'create_order_summary_view.sql' => array('order_summary_view'),
65                                 );
66
67                                 return $this->update_databases($company, $updates, $check_only)
68                                                                 && update_order_detail_defaults(true)
69                                                                 && update_queue_quantities()
70                                                                 && update_qoh_for_item()
71                                                                 ;
72                 }
73
74                 function deactivate_extension($company, $check_only=true)
75                 {
76                                 global $db_connections;
77
78                                 $updates = array(
79                                                 'clean_sales_order_details.sql' => array('ugly_hack') // FIXME: just an ugly hack to clean database on deactivation
80                                 );
81
82                                 return $this->update_databases($company, $updates, $check_only);
83                 }
84
85                 function get_allowed_quantity($detail_id, $opts) {
86                         $location = array_shift($opts);
87                         $date = array_shift($opts);
88                         $qoh = array_shift($opts);
89
90                         $sql = "SELECT quantity_before, hold_until_date
91                                                         FROM ".TB_PREF."denorm_order_details_queue
92                                                         NATURAL JOIN ".TB_PREF."sales_order_details
93                                                         WHERE id = $detail_id";
94                         $result = db_query($sql);
95                         $quantity_before = 0;
96                         if($row=db_fetch($result)) {
97                                 $quantity_before = $row['quantity_before'];
98                                 $hold_until = sql2date($row['hold_until_date']);
99                                 if($qoh<=$quantity_before) return array(0, 'stockmankobg');
100                                 if(OrderXtraConfig::early($hold_until, $date)) return array(0, 'early');
101                                 }
102
103                         return array(max($qoh - $quantity_before, 0), '');
104                 }
105
106                 function db_postwrite($cart, $trans_type) {
107                         if(is_a($cart, "Cart")) {
108
109                         // It's a cart. Find all the stock_id and update the cache table 
110                         foreach($cart->line_items as $line_no => $item) {
111                                 $stock_id = $item->stock_id;
112                                 update_queue_quantity_for_item($stock_id);
113                                 update_qoh_for_item($stock_id);
114                                 }
115                         }
116                         else if (isset($trans_type) && $trans_type == "order_xtra") {
117                                 $stock_ids = stock_id_from_detail($cart);
118                                 if($stock_ids) foreach($stock_ids as $stock_id) {
119                                         update_queue_quantity_for_item($stock_id);
120                                         update_qoh_for_item($stock_id);
121                                         }
122                         }
123
124                         // update null fields of new entered orders.
125                         $types = array(ST_SALESORDER, 'order_xtra');
126                         if(in_array($trans_type, $types)) update_order_detail_defaults();
127
128                 }
129
130
131                 function db_prevoid($cart, $trans_type) {
132                 // Simple version, rebuild everything
133                 $types = array(ST_CUSTCREDIT, ST_CUSTDELIVERY, ST_INVADJUST, ST_PURCHORDER, ST_WORKORDER, ST_MANUISSUE,
134                                 ST_SALESORDER, ST_SALESQUOTE, ST_MANURECEIVE);
135                 if(in_array($trans_type, $types)) {
136                                 update_queue_quantities();
137                                 update_qoh_for_item();
138                         }
139                 }
140 }
141
142 function stock_id_from_detail($detail_ids) {
143         if(empty($detail_ids)) return null;
144         $stock_ids = array();
145         $ids = implode(', ', $detail_ids);
146         $sql = " SELECT DISTINCT stk_code
147                                         FROM ".TB_PREF."sales_order_details
148                                         WHERE id IN ($ids)";
149         $result = db_query($sql);
150         while($row=db_fetch($result)) {
151                         array_push($stock_ids, $row['stk_code']);
152         }
153
154         return $stock_ids;
155 }
156 ?>