<?php
/**********************************************************************
***********************************************************************/


function write_project_credit_note_header($pcn_no, $project_id, $debtor_no, $customer_branch, $reference,
                                            $order_date, $sales_person,
                                            $total, $taxes, $rate_include_taxes,  
                                            $tax_group_id, $pi_taxes, $project_invoice_id, 
                                            $comment, $staxes_total)
{
//    begin_transaction();
    global $Refs;
    
    if($pcn_no > 0){        
        update_project_credit_note_header($pcn_no, $debtor_no, $customer_branch, $reference,
                                                $order_date, $sales_person,
                                                $total, $taxes, $rate_include_taxes, 
                                                $tax_group_id, $project_invoice_id, $comment, $staxes_total);
    }else{
        
        $pcn_no = add_project_credit_note_header($project_id, $debtor_no, $customer_branch, $reference,
                                                    $order_date, $sales_person,
                                                    $total, $taxes, $rate_include_taxes, 
                                                    $tax_group_id, $project_invoice_id, $comment, $staxes_total);
        
        $Refs->save(ST_PROJECT_CREDIT_NOTE, $pcn_no, $reference);
    }
    
    update_project_credit_note_tax_details($pcn_no, $pi_taxes);
    
//    commit_transaction();
    
    return $pcn_no;
}


function write_project_credit_note_detail($pcn_no, $pcn_detail_row_id, $type, 
                                            $sequence, $text, $completed_date, $unit, 
                                            $quantity, $rate, $amount, $line_comment,
                                            $category_id, $debit_account, $credit_account)
{                
    if($pcn_detail_row_id > 0){         
        update_project_credit_note_details($pcn_detail_row_id, $type, 
                                                $sequence, $text, $completed_date, $unit, 
                                                $quantity, $rate, $amount, $line_comment,
                                                $category_id, $debit_account, $credit_account);     
    }else{

        $pcn_detail_row_id = add_project_credit_note_details($pcn_no, $type, 
                                                                $sequence, $text, $completed_date, $unit, 
                                                                $quantity, $rate, $amount, $line_comment,
                                                                $category_id, $debit_account, $credit_account);
    }
          
}


function update_project_credit_note_tax_details($pcn_no, $pi_taxes)
{   
    delete_project_credit_note_tax_details($pcn_no);
    foreach ($pi_taxes as $tax_detail) {
        add_project_credit_note_tax_detail($pcn_no, $tax_detail['tax_type_id'], 
                                            $tax_detail['tax_amount'], $tax_detail['tax_type_name'], 
                                            $tax_detail['tax_rate'], $tax_detail['suspended']);
    }
}


function delete_project_credit_note_tax_details($pcn_no)
{     
     $sql = "DELETE FROM ".TB_PREF."project_credit_note_item_taxes  
             WHERE pcn_id = ".db_escape($pcn_no);     
     
    db_query($sql, "Could not delete_project_credit_note_tax_details.");
 
}


function add_project_credit_note_tax_detail($pcn_no, $tax_id, $tax_value, $tax_type_name, $tax_rate, $suspended)
{     
     $sql = "INSERT INTO ".TB_PREF."project_credit_note_item_taxes 
                   (pcn_id, tax_type_id, amount, tax_type_name, rate, suspended)
             VALUES ("
                    .db_escape($pcn_no).","
                    .db_escape($tax_id).","
                    .db_escape($tax_value).","
                    .db_escape($tax_type_name).","
                    .db_escape($tax_rate).","           
                    .db_escape($suspended).")";              
                           
    db_query($sql, "Could not add_project_credit_note_tax_detail.");
 
}

function update_project_credit_note_header($pcn_no, $debtor_no, $customer_branch, $reference,
                                                $order_date, $sales_person,
                                                $total, $taxes, $rate_include_taxes, 
                                                $tax_group_id, $project_invoice_id, $comment, $staxes)
{
    $updated_by = $_SESSION['wa_current_user']->user;
    $updated_on = date('Y-m-d H:i:s');
    
    $sql = "UPDATE ".TB_PREF."project_credit_notes
            SET 
                debtor_no=".db_escape($debtor_no).", 
                branch_code=".db_escape($customer_branch).", 
                reference=".db_escape($reference).", 
                ord_date=".db_escape(date2sql($order_date)).", 
                salesman_code =".db_escape($sales_person).",
                rate_include_taxes=".db_escape($rate_include_taxes).", 
                tax_group_id=".db_escape($tax_group_id).", 
                total=".db_escape($total).", 
                taxes =".db_escape($taxes).",              
                staxes =".db_escape($staxes).",              
                project_invoice_id=".db_escape($project_invoice_id).",     
                comment =".db_escape($comment).",                    
                updated_by=".db_escape($updated_by).",
                updated_on=".db_escape($updated_on)." 
                    
            WHERE order_no=".db_escape($pcn_no);
    
    db_query($sql);
    return db_num_affected_rows();  
    
}


function add_project_credit_note_header($project_id, $debtor_no, $customer_branch, $reference,
                                            $order_date, $sales_person,
                                            $total, $taxes, $rate_include_taxes,  
                                            $tax_group_id, $project_invoice_id, $comment, $staxes)
{
    $created_by = $_SESSION['wa_current_user']->user;
    $created_on = date('Y-m-d H:i:s');
    
    $next_no = get_next_trans_no(ST_PROJECT_CREDIT_NOTE);
    
    $sql = "INSERT INTO ".TB_PREF."project_credit_notes
                  ( order_no, project_id, debtor_no, 
                    branch_code, reference, 
                    ord_date, 
                    salesman_code, rate_include_taxes,
                    tax_group_id, total, 
                    taxes, staxes,        
                    project_invoice_id, comment,
                    created_by, created_on )         
                VALUES 
                (".db_escape($next_no).", ".db_escape($project_id).", ".db_escape($debtor_no).", 
                    ".db_escape($customer_branch).", ".db_escape($reference).",
                    ".db_escape(date2sql($order_date)).",
                    ".db_escape($sales_person).",".db_escape($rate_include_taxes).",
                    ".db_escape($tax_group_id).", ".db_escape($total).", 
                    ".db_escape($taxes).", ".db_escape($staxes).",                    
                    ".db_escape($project_invoice_id).", ".db_escape($comment).",
                    ".db_escape($created_by).", ".db_escape($created_on).")";
     
    db_query($sql);
    return db_insert_id();        
}


function add_project_credit_note_details($pcn_no, $type, $sequence, $text, $completed_date, $unit, 
                                            $quantity, $rate, $amount, $line_comment,
                                            $category_id, $debit_account, $credit_account)
{
    $created_by = $_SESSION['wa_current_user']->user;
    $created_on = date('Y-m-d H:i:s');
    
     $sql = "INSERT INTO ".TB_PREF."project_credit_note_details (
                        order_no, line_type, 
                        sequence, description, 
                        completed_date, unit, quantity, 
                        rate, amount, line_comment,
                        category_id, debit_account, credit_account,
                        created_by, created_on )                
            VALUES (".db_escape($pcn_no).", ".db_escape($type).","
                        .db_escape($sequence).", ".db_escape($text).",".db_escape($completed_date).","
                        .db_escape($unit).", ".db_escape($quantity).","
                        .db_escape($rate).", ".db_escape($amount).",".db_escape($line_comment).","
                        .db_escape($category_id).", ".db_escape($debit_account).",".db_escape($credit_account).","
                        .db_escape($created_by).", ".db_escape($created_on).")";
              
    db_query($sql);
    return db_insert_id();    
}


function update_project_credit_note_details($pcn_detail_row_id, $type, $sequence, $text, $completed_date, $unit, 
                                                $quantity, $rate, $amount, $lne_comment,
                                                $category_id, $debit_account, $credit_account)
{
    $updated_by = $_SESSION['wa_current_user']->user;
    $updated_on = date('Y-m-d H:i:s');
    
     $sql = "UPDATE ".TB_PREF."project_credit_note_details
                SET 
                    line_type=".db_escape($type).",
                    sequence=".db_escape($sequence).",
                    description=".db_escape($text).", 
                    completed_date=".db_escape($completed_date).", 
                    unit=".db_escape($unit).",
                    quantity=".db_escape($quantity).",
                    rate=".db_escape($rate).",
                    amount=".db_escape($amount).",
                    line_comment=".db_escape($lne_comment).",
                    category_id=".db_escape($category_id).",
                    debit_account=".db_escape($debit_account).",
                    credit_account=".db_escape($credit_account).",
                    updated_by=".db_escape($updated_by).",
                    updated_on=".db_escape($updated_on)."                  
                        
                WHERE id=".db_escape($pcn_detail_row_id);
     
    db_query($sql);
    return db_num_affected_rows();  
    
}

function get_project_credit_note_header($pcn_no)
{
    $sql = "SELECT pcn.*,
                    pm.description project_description    

            FROM ".TB_PREF."project_credit_notes pcn
                LEFT JOIN ".TB_PREF."projects_master pm ON 
                    pcn.project_id = pm.project_id

            WHERE pcn.order_no=".db_escape($pcn_no);
    
    $result = db_query($sql);
    return db_fetch($result);     
}


function get_project_credit_note_details($order_no)
{
    $sql = "SELECT pid.*, 
                    u.name unit_name,
                    cn.value_name category_name,
                    da.account_name debit_account_name,
                    ca.account_name credit_account_name 
            FROM ".TB_PREF."project_credit_note_details pid 
                    LEFT JOIN ".TB_PREF."item_units u ON 
                            pid.unit = u.abbr 
                    LEFT JOIN ".TB_PREF."list_values cn ON 
                            pid.category_id = cn.id 
                    LEFT JOIN ".TB_PREF."chart_master da ON 
                            pid.debit_account = da.account_code  
                    LEFT JOIN ".TB_PREF."chart_master ca ON 
                            pid.credit_account = ca.account_code  
                    
            WHERE order_no=".db_escape($order_no)." 
            ORDER BY sequence";
    
    return db_query($sql, "Could not get manual project invoices");
    
}

function get_project_credit_note_line($selected_id)
{
     $sql = "SELECT pid.*                     
         
             FROM ".TB_PREF."project_credit_note_details pid 
                      
             WHERE pid.id=".db_escape($selected_id);
     
     $result = db_query($sql);
     return db_fetch($result);
    
}

function delete_project_credit_note_item($selected_id)
{
   $sql = "DELETE FROM ".TB_PREF."project_credit_note_details WHERE id=".db_escape($selected_id);
   
   db_query($sql);
   return db_num_affected_rows();
    
}


function get_project_credit_note_tax_type_amount($pcn_no, $tax_type_id)
{
    $sql = "SELECT amount 
            FROM ".TB_PREF."project_credit_note_item_taxes 
            WHERE pcn_id =".db_escape($pcn_no)." AND 
                tax_type_id = ".db_escape($tax_type_id);
    
    $result = db_query($sql);
    $amt =  db_fetch($result);   
    $amount = 0;
    if(isset($amt['amount']) && $amt['amount'] > 0){
        $amount = $amt['amount'];
    }
    
    return $amount;
}


function get_project_credit_note_line_count($mpi_id)
{
     $sql = "SELECT COUNT(pcnd.id) num_rows                    
         
             FROM ".TB_PREF."project_credit_note_details pcnd
                      
             WHERE pcnd.order_no=".db_escape($mpi_id);
     
    $result = db_query($sql);
    $row = db_fetch($result);
    if(isset($row['num_rows']) && $row['num_rows'] > 0){
        return $row['num_rows'];
    }else{
        return 0;
    }
    
}



?>