2 // ----------------------------------------------------------------
4 // Creator: Maxime Bourge
6 // Title: Order Line Extra
7 // Free software under GNU GPL
8 // ----------------------------------------------------------------
9 define ('SS_ORDERLINEX', 100<<8);
11 include_once('includes/order_xtra_config.inc');
12 include_once('includes/db_order_lines.inc');
14 class hooks_order_line_extra extends hooks {
15 var $module_name = 'order_line_extra';
18 Install additonal menu options provided by module
20 function install_tabs($app) {
25 function install_options($app) {
29 $app->add_rapp_function(0, _('Order Extra'),
30 $path_to_root.'/modules/order_line_extra/order_lines_view.php', 'SA_SALESTRANSVIEW');
34 function install_access()
36 $security_sections[SS_ORDERLINEX] = _("Order Line Extra");
37 $security_areas['SA_ORDERLINEX_EDIT'] = array(SS_ORDERLINEX|1, _("Edit lines"));
39 return array($security_areas, $security_sections);
42 /* This method is called on extension activation for company. */
43 function activate_extension($company, $check_only=true)
45 global $db_connections;
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'),
55 return $this->update_databases($company, $updates, $check_only)
56 && update_queue_quantities()
57 && update_qoh_for_item();;
60 function deactivate_extension($company, $check_only=true)
62 global $db_connections;
65 'clean_sales_order_details.sql' => array('ugly_hack') // FIXME: just an ugly hack to clean database on deactivation
68 return $this->update_databases($company, $updates, $check_only);
71 function get_allowed_quantity($detail_id, $opts) {
72 $location = array_shift($opts);
73 $date = array_shift($opts);
74 $qoh = array_shift($opts);
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);
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');
89 return array(max($qoh - $quantity_before, 0), '');
92 function db_postwrite($cart, $trans_type) {
93 if(is_a($cart, "Cart")) {
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);
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);
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();
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();
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
135 $result = db_query($sql);
136 while($row=db_fetch($result)) {
137 array_push($stock_ids, $row['stk_code']);