<?php

global $path_to_root;
include_once($path_to_root . "/sales/includes/save_sales_order_class.inc");
include_once($path_to_root . "/purchasing/includes/db/suppliers_db.inc");
            
function cs_get_sos_with_no_tax_details(){
    $sql = "SELECT DISTINCT so.order_no 
            FROM ".TB_PREF."sales_orders so 
                LEFT JOIN ".TB_PREF."sales_order_tax_details sot 
                    ON so.order_no = sot.order_no                     
                    AND sot.trans_type = 30 
            WHERE so.trans_type = 30 
                AND so.order_no > 0 
                AND sot.order_no IS NULL 
            ORDER BY so.order_no 
            LIMIT 100";
    return db_query($sql,"cs_get_sos_with_no_tax_details");
}

function sc_read_so($so_no, &$cart)
{
    

//    $cart->trans_type = ST_PURCHORDER;
//    $cart->order_no = $trans_no;

//    read_po($trans_no, $cart, false);
//    $cart->order_no = $trans_no;
//
//    $poh = get_po_record($cart->order_no);
    
//    get_supplier_details_to_order($cart, $poh['supplier_id'], $poh['tax_group_id']); 
//    $supplier = get_supplier($poh['supplier_id']);
//    $cart->set_supplier($poh['supplier_id'], $supplier["supp_name"], $supplier["curr_code"], 
//		$poh['tax_group_id'], $poh["tax_included"]);
    
    $soh = get_sales_order_header_record($so_no, ST_SALESORDER);
    $cart->tax_included = $soh['tax_included'];
    $cart->document_date = sql2date($soh['ord_date']);
    $cart->set_branch($soh['branch_code'], $soh['tax_group_id'], '');

//    $cart->tax_group_array = get_tax_group_items_as_array($cart->tax_group_id);
            

}

function cs_add_so_tax_details($so)
{
    $taxes = $so->calculate_save_sales_order_taxes(0);
    
    begin_transaction();
    delete_sales_order_tax_details($so->order_no, $so->trans_type);
    foreach ($taxes as $tax_line){
        if($tax_line['Net'] > 0){ // $po_tax_line['Value'] can be zero for excempted taxes
            add_sales_order_tax_details($so->order_no, $so->trans_type,
                                        $tax_line['tax_type_id'], $tax_line['rate'], $so->tax_included,
                                        $tax_line['Value'], $tax_line['Net'], 1, $so->document_date, '');
            
            $message = "SO ".$so->order_no." updated - tax_included: ".$so->tax_included.", tax_group_id: ".$so->tax_group_id.", 
                        tax type: ".$tax_line['name'].", Rate: ".$tax_line['rate'].", Tax Amount: ".$tax_line['Value'].", Line Value: ".$tax_line['Net'];
            write_custom_script_log("cs_update_sales_order_tax_details", $message);
        }
    } 
    commit_transaction();
}

function cs_update_sales_order_tax_details()
{
    $message = "==================Script Sarted==========================";
    write_custom_script_log("cs_update_sales_order_tax_details", $message);
    
    
    $pos = cs_get_sos_with_no_tax_details();    
    $recs = db_num_rows($pos);
    write_custom_script_log("cs_update_sales_order_tax_details", $recs." Records selected");
    
    while ($row = db_fetch($pos)) {
        $cart = new Save_Sales_Order_Cart(ST_SALESORDER, $row['order_no']);
        sc_read_so($row['order_no'], $cart);           
        cs_add_so_tax_details($cart);    
        unset($cart);        
    }
    
    
    $message = "------------------Script Completed----------------------------";
    write_custom_script_log("cs_update_sales_order_tax_details", $message);
}

?>