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

function check_credit_note_exists($reference)
{
    $cn_row = get_customer_transaction_record($reference, ST_CUSTCREDIT);
    if($cn_row == null){
        return 0;
    }
    else{
        return $cn_row['trans_no'];
    }
}

function check_credit_note_data($info, $cart) {
	global $Refs;

	if (!$info['customer_id']) {
		return array("passed" => false, "message" => _("There is no customer selected."));
	}

	if (!$info['branch_id']) {
		return array("passed" => false, "message" => _("This customer has no branch defined."));
	}
	
	if (!is_date($info['order_date'])) {
		return array("passed" => false, "message" => _("The entered date is invalid."));
	}


	if (count($cart->line_items) == 0)	{
		return array("passed" => false, "message" => _("You must enter at least one non empty item line."));
	}

	if (!$Refs->is_valid($info['reference'])) {
		return array("passed" => false, "message" => _("You must enter a reference."));
	}
/*/
	if (!db_has_currency_rates($cart->customer_currency, $info['order_date'])){
		return false;
	}
*/	
	if ($cart->get_items_total() < 0) {
		return array("passed" => false, "message" => _("Invoice total amount cannot be less than zero."));
	}

	return array('passed' => true);
}


function check_credit_note_item_data($item)
{
	if (!is_numeric($item['qty'])) {
		return array("passed" => false, "message" => _("The quantity entered is negative or invalid."));
	}
        
        if ($item['qty'] <= 0) {
		return array("passed" => false, "message" => _("The quantity entered should be greater than zero."));
	}
        
	if (!is_numeric($item['price'])) {
		return array("passed" => false, "message" => _("The price entered is negative or invalid."));
	}
        
        if ($item['price'] <= 0) {
		return array("passed" => false, "message" => _("The price entered should be greater than zero."));
	}
        
        if (!is_numeric($item['discount'])) {
		return array("passed" => false, "message" => _("The discount entered is negative or invalid."));
	}
        
        if ($item['discount'] < 0) {
		return array("passed" => false, "message" => _("The discount entered can not be less than zero."));
	}
        
        if ($item['discount'] > 100) {
		return array("passed" => false, "message" => _("The discount entered should not be greater than 100."));
	}

	return array("passed" => true, "message" => _(""));
}


function credit_note_add($info)
{
    global $path_to_root ;
    include_once($path_to_root . "/sales/includes/cart_class.inc");
    include_once($path_to_root . "/admin/db/company_db.inc");
    include_once($path_to_root . "/includes/date_functions.inc");
    include_once($path_to_root . "/sales/includes/sales_ui.inc");
    include_once($path_to_root . "/sales/includes/ui/sales_order_ui.inc");
    include_once($path_to_root . "/sales/includes/db/cust_trans_details_db.inc");
    include_once($path_to_root . "/sales/includes/sales_db.inc");
    include_once($path_to_root . "/sales/includes/db/sales_types_db.inc");
    
    
    $cart = null;
    $cart = new Cart(ST_CUSTCREDIT,0);
    $cart->reference = $info['reference'];
    
    if(check_credit_note_exists($cart->reference) == true){
        return  array("passed" => false, "message" => _("Credit note exists [".$cart->reference."]"));
    }

    $cart->Comments = $info['comments'];
    
    $formatted_date = change_date_format("YYYYMMDD", $info['order_date'], "MMDDYYYY");
    $info['order_date'] = $formatted_date;
    $cart->document_date = $info['order_date'];
    
    $cart->freight_cost	= input_num('freight_cost');
    $cart->Location = $info['location'];
    $cart->ship_via = $info['ship_via'];
    
    if($info['application_id'] == 2){
            $converted_delivery_date = convert_delivery_date($info['delivery_date'], "MMDDYYYY");
            $info['delivery_date'] = $converted_delivery_date;
    }
    else{
        $info['delivery_date'] = $info['order_date'];
    }
    
    $cart->dimension_id = $info['dimension_id'];
    $cart->dimension2_id = $info['dimension2_id'];
    
  
    if (isset($info['email'])) {
        $cart->email = $info['email'];
    }	
    else{
        $cart->email = '';
    }

    $cart->customer_id	= $info['customer_id'];
    $cart->Branch = $info['branch_id'];
    $cart->sales_type = $info['sales_type'];



    foreach($info['Items'] as $item) {
        
            if($item['price'] < 0){
                $item['price'] = -$item['price'];
            }
            
            if($item['price'] > 0){
                $item_discount_value = $item['line_discount_total_value'] / $item['qty'];
                $item_discount_percentage = $item_discount_value / $item['price'];
                $item['discount'] = $item_discount_percentage;
            }
            
            $item_check_msg = check_credit_note_item_data($item);
            if($item_check_msg["passed"] == false){
                return array("passed" => false, "message" => $item_check_msg["message"]." [".$item['stock_id']."]");
            }
        
            add_to_order($cart, $item['stock_id'], $item['qty'], $item['price'], $item['discount'], $item['description']);
                //write_log_array("sales_add_api[items]",0,$info['items']);
        }
        
    

    $can_process_msg = check_sale_data($info, $cart);

    if ($can_process_msg['passed'] == false) {
        return array("passed" => false, "message" => $can_process_msg['message']);
    }

    //return;
    
    //$result = $cart->write(1);
    
    $credit_no = $cart->write(0);
    if ($credit_no == -1) {
        return array("passed" => false, "message" => _("The entered reference is already in use."));
    } 
    else{
        return array("passed" => true, "message" => $credit_no);
    }
/*/
  $feeback_response = sprintf(_("Invoice # %d has been entered."), key($cart->trans_no));
  api_success_response($feeback_response);
  */
}


?>