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

function get_debtor_trans_no(&$invoice, $trans_type)
{
    $in_row = get_customer_transaction_record($invoice["reference"], $trans_type);
    if($in_row == null){
        return false;
    }
    else{
        $invoice['trans_no'] = $in_row['trans_no'];
        return true;
    }
}

function check_sale_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 ($cart->payment_terms['cash_sale'] == 0) {
		if (strlen($info['deliver_to']) <= 1) {
			return array("passed" => false, "message" => _("You must enter the person or company to whom delivery should be made to."));
		}

		if ($cart->trans_type != ST_SALESQUOTE && strlen($info['delivery_address']) <= 1) {
			return array("passed" => false, "message" => _("You should enter the street address in the box provided. Orders cannot be accepted without a valid street address."));
		}

		if ($info['freight_cost'] == "") {
			$info['freight_cost'] = price_format(0);
		}

		/*if (!check_num('freight_cost', 0)) {
			return array("passed" => false, "message" => _("The shipping cost entered is expected to be numeric."));
		}*/

		if (!is_date($info['delivery_date'])) {
				return array("passed" => false, "message" => _("The delivery date is invalid."));
		}

		if (date1_greater_date2($info['order_date'], $info['delivery_date'])) {
				return array("passed" => false, "message" => _("The requested delivery date is before the date of the order."));
		}
	} else {
		if (!db_has_cash_accounts()) {
			return array("passed" => false, "message" => _("You need to define a cash account for your Sales Point."));
		}
	}

	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 sales_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_SALESINVOICE, 0);
    $cart->reference = $info['reference'];
    
    //if(check_invoice_exists($cart->reference) == true){
    if($info['trans_no'] > 0){
        return  array("passed" => false, "message" => _("Invoice record exists [".$cart->reference."]"));
    }

    $cart->Comments = $info['comments'];
    
    $formatted_date = change_date_format("YYYYMMDD", $info['order_date'], "MMDDYYYY");
    $info['order_date'] = $formatted_date;
    
    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->document_date = $info['order_date'];

    if (isset($info['payment']) && ($cart->payment != $info['payment'])){
            $cart->payment = $info['payment'];
            $cart->payment_terms = get_payment_terms($info['payment']);
            $newpayment	= true;
    }

    if ($cart->payment_terms['cash_sale']) {
        if ($newpayment){
            $cart->due_date = $cart->document_date;
            $cart->phone = $cart->cust_ref = $cart->delivery_address = '';
            $cart->ship_via = 0;
            $cart->deliver_to	= '';
        }
    }
    else{
        $cart->due_date = $info['delivery_date'];
        $cart->cust_ref = $info['cust_ref'];
        $cart->deliver_to = $info['deliver_to'];
        $cart->delivery_address = $info['delivery_address'];
        $cart->phone = $info['phone'];
        $cart->ship_via = $info['ship_via'];
    }
    $cart->Location = $info['location'];
    $cart->freight_cost	= input_num('freight_cost');

    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'];

    $cart->dimension_id = $info['dimension_id'];
    $cart->dimension2_id = $info['dimension2_id'];

    foreach($info['Items'] as $item) {
	if($item['line_status'] != "V"){
            if($item['line_status'] == "G"){
                $item['line_discount_total_value'] = $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;
            }
        
            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 = check_sale_data($info, $cart);

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

    //return;
    
    $result = $cart->write(1);

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


?>