<?php


function cs_get_pos_with_no_tax_details(){
    $sql = "SELECT DISTINCT po.order_no 
            FROM ".TB_PREF."purch_orders po 
                LEFT JOIN ".TB_PREF."purch_order_tax_details pot ON 
                        po.order_no = pot.order_no 
            WHERE pot.order_no IS NULL LIMIT 1";
    return db_query($sql,"Could not get_pos_with_no_tax_group");
}

function sc_read_po($trans_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"]);

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

}

function cs_add_po_tax_details($po_obj)
{
    $po_obj->taxes = $po_obj->get_taxes(0);
    
    begin_transaction();
    delete_po_tax_details($po_obj->order_no);
    foreach ($po_obj->taxes as $po_tax_line){
        if($po_tax_line['Net'] > 0){ // $po_tax_line['Value'] can be zero for excempted taxes
            add_po_tax_details($po_obj->order_no, $po_tax_line['tax_type_id'], $po_tax_line['rate'], $po_obj->tax_included,
                                   $po_tax_line['Value'], $po_tax_line['Net'], 1, $po_obj->orig_order_date, '');
        }
    } 
    commit_transaction();
}

function cs_update_purchase_order_tax_details()
{
    $pos = cs_get_pos_with_no_tax_details();
    while ($row = db_fetch($pos)) {
        $cart = new purch_order;
        sc_read_po($row['order_no'], $cart);           
        cs_add_po_tax_details($cart);    
        unset($cart);
    }
}

?>