<?php
define ('SS_EXTERNAL', 252<<8);
class hooks_fifo extends hooks {
    
    function __construct() {
        $this->module_name = 'fifo'; 
    }

    /*
    * Install additonal menu options provided by module
    */

    function install_options($app) {
	global $path_to_root;

	switch($app->id) {
            case 'system':
                $app->add_lapp_function(0, _("FIFO Setup"),
                    $path_to_root."/modules/".$this->module_name."/manage/external_setup.php?", 'SA_SETUPCOMPANY', MENU_MAINTENANCE);
		break;
	}
    }
    
    function db_prewrite(&$cart, $trans_type){
        global $path_to_root;
        if (get_company_pref('fifo_cost_calculations')){
            include_once( $path_to_root . "/modules/fifo/includes/db/sales_db.inc");
            if($trans_type == ST_CUSTCREDIT && !check_credit_note($trans_type, $cart)){
                display_error("Credit Note of type Return not allowed here. Please create credit notes against Invoice");
                exit();
            }
            
            if($trans_type == ST_CUSTDELIVERY || $trans_type == ST_SUPPCREDIT){
                void_fifo_allocs($trans_type, is_array($cart->trans_no) ? key($cart->trans_no) : $cart->trans_no, TRUE);
            }
        }
    }
    
    function db_postwrite(&$cart, $trans_type){
        global $SysPrefs,$path_to_root;
        if (get_company_pref('fifo_cost_calculations')){
                include_once( $path_to_root . "/modules/fifo/includes/db/sales_db.inc");
            if($trans_type == ST_CUSTDELIVERY){
                update_fifo_costs($cart);
            }

            if($trans_type == ST_SUPPRECEIVE || $trans_type == ST_INVADJUST){
                commit_transaction();

                begin_transaction();
                $result = get_stock_moves($trans_type, $cart->trans_no);
                while ($myrow = db_fetch($result)) {
                    db_query("UPDATE " . TB_PREF . "stock_moves set not_cleared=" . $myrow['qty'] . " WHERE qty>0 AND trans_id=" . db_escape($myrow['trans_id'] ), "FIFO updating qty not_cleared on GRN  failed.");
                }
                commit_transaction();
            }
            
            if($trans_type == ST_CUSTCREDIT){
                cust_credit_note_fifo_costing($trans_type, $cart);
            }
            
            if($trans_type == ST_INVADJUST || $trans_type == ST_SUPPCREDIT){
                stock_move_fifo_costing($trans_type, $cart->trans_no);
            }
        }
    }
    
    function db_prevoid($type, $trans_no){
        global $path_to_root;
        if (get_company_pref('fifo_cost_calculations')){
                include_once( $path_to_root . "/modules/fifo/includes/db/sales_db.inc");
            if($type == ST_CUSTDELIVERY || $type == ST_SUPPCREDIT){
                void_fifo_allocs($type, $trans_no);
            }

            if($type == ST_SUPPRECEIVE || $type == ST_CUSTCREDIT || $type == ST_INVADJUST || $type == ST_WORKORDER){
                if(db_has_fifo_allocs($type, $trans_no)){
                    display_error("This transaction cannot be voided becaused inventory costing transactions have been attached to it ");
                    exit();
                }
            }
        }
    }
    
    function pre_header($fun_args){
        
    }
    
    function activate_extension($company, $check_only=true) {
        global $db_connections;
        
        $updates = array( 'update.sql' => array($this->module_name));
 
        return $this->update_databases($company, $updates, $check_only);
    }
    
    function deactivate_extension($company, $check_only=true) {
        global $db_connections;

        $updates = array('remove.sql' => array($this->module_name));

        return $this->update_databases($company, $updates, $check_only);
    }

}
