trade_history

<?php

function trade_history_menu()
{
	$items = array();

	$items['stock/trade_history/balance/%'] = array(
		'title' => 'Balance',
		'page callback' => 'shopping_car',
		'page arguments' => array(3),
		'access callback' => TRUE,
		'type' => MENU_LOCAL_TASK
	);
	$items['admin/example/import'] = array(
		'title' => 'Import',
		'page callback' => 'drupal_get_form',
		'page arguments' => array('trade_history_import_form'),
		'access callback' => TRUE,
		'type' => MENU_LOCAL_TASK,
	);
	$items['admin/example/balance'] = array(
		'title' => 'Balance_test',
		'page callback' => 'drupal_get_form',
		'page arguments' => array('trade_history_balance_form'),
		'access callback' => TRUE,
		'type' => MENU_LOCAL_TASK,
	);


	return $items;
}

function trade_history_balance_form($node, &$form_state, $params)
{
	foreach ($params['buy_list'] as $buy_detail) {
		$forms[$buy_detail->id]['num'] = array(
			'#type' => 'textfield',
			'#default_value' => $buy_detail->num,
			'#size' => 10,
		);
		$forms[$buy_detail->id]['sale_num[]'] = array(
			//'#title' => t('num'),
			'#type' => 'textfield',
			'#size' => 10,
		);
	}

	return $forms;
}

function formexample_nameform() {
	$form['name'] = array(
		'#title' => t('Your Name'),
		'#type' => 'fieldset',
		'#description' => t('What people call you.')
	);
	$form['name']['user_name'] = array(
		'#title' => t('Your Name'),
		'#type' => 'textfield',
		'#description' => t('Please enter your name.')
	);
	$form['color'] = array(
		'#title' => t('Color'),
		'#type' => 'fieldset',
		'#description' => t('This fieldset contains the Color field.'),
		'#collapsible' => TRUE,
		'#collapsed' => FALSE
	);
	$form['color_options'] = array(
		'#type' => 'value',
		'#value' => array(t('red'), t('green'), t('blue'))
	);
		$form['color']['favorite_color'] = array(
			'#title' => t('Favorite Color'),
			'#type' => 'select',
			'#description' => t('Please select your favorite color.'),
			'#options' => $form['color_options']['#value']
		);
	$form['submit'] = array(
		'#type' => 'submit',
		'#value' => t('Submit')
	);
	return $form;
	}

function theme_trade_history_balance_form(&$variables)
{
	$variables['formexample_nameform'] = array();
	$hidden = array();

	$output = " 
<table>
	<tr>
		<th></th>
		<th>ID</th>
		<th>trade_day</th>
		<th>own num</th>
		<th>sale num</th>
	</tr>";


	foreach (element_children($variables['form']) as $key) {
		if (isset($variables['form'][$key]['#type'])) {
			$hidden[] = drupal_render($variables['form'][$key]);
		} else {
			//$variables['formexample_nameform'][$key] = drupal_render($variables['form'][$key]);
			$output .= '
				<tr>
				<td><input type="checkbox" /></td>
				<td>1</td>
				<td>2012-04-16</td>
				<td>' . drupal_render($variables['form'][$key]['num']) . '</td>
				<td>{$a}</td>
				</tr>';

		}
	}
	$variables['formexample_nameform']['hidden'] = implode($hidden);
	$variables['formexample_nameform_form'] = implode($variables['formexample_nameform']);

	$output .= '</table>';

	return $output;
}

function shopping_car($stock_trade_history_id)
{
	drupal_add_js('jquery-ui-1.8.18.custom/js/jquery-1.7.1.min.js');
	drupal_add_js('jquery-ui-1.8.18.custom/js/jquery-ui-1.8.18.custom.min.js');

	$result = trade_history_load($stock_trade_history_id);
	$params['sale_detail'] = $result->fetchObject(); 

	$result = db_query("
		SELECT * 
		FROM {stock_trade_history} 
		WHERE trade_type = :trade_type AND trade_day <= :trade_day", 
		array(
			':trade_type' => 'B',
			':trade_day' => $params['sale_detail']->trade_day
		)
	);
	foreach ($result as $object) { 
		$params['buy_list'][] = $object;
	}

	$balance = drupal_get_form('trade_history_balance_form', $params);
	$balance = drupal_render($balance);

	$html = <<<HTML
<style type="text/css">
</style>


<script type="text/javascript">
</script>

HTML;
	return $balance . $html;
}

function trade_history_import_form()
{
	$form = array();

	$form['browser'] = array(
		'#type' => 'fieldset',
		'#title' => t('History Import'),
		'#collapsible' => TRUE,
		'#description' => t("Import trade history from a CSV file."),
	);

	$file_size = t('Maximum file size: !size MB.', array('!size' => file_upload_max_size()));

	$form['browser']['file_upload'] = array(
		'#type' => 'file',
		'#title' => t('CSV File'),
		'#size' => 40,
		'#description' => t('Select the CSV file to be imported. ') . $file_size,
	);

	$form['submit'] = array(
		'#type' => 'submit',
		'#value' => t('Import'),
	);

	// set the form encoding type
	$form['#attributes']['enctype'] = "multipart/form-data";

	return $form;
}

function trade_history_theme()
{
	return array(
		'trade_history_balance_form' => array(
			'render element' => 'form',
		),
		'formexample_nameform' => array(
			'render element' => 'form',
			'template' => 'formexample-nameform',
		),
	);
}

function template_preprocess_formexample_nameform(&$variables) {
	$variables['formexample_nameform'] = array();
	$hidden = array();
	// Provide variables named after form keys so themers can print each element independently.
	foreach (element_children($variables['form']) as $key) {
		$type = $variables['form'][$key]['#type'];
		if ($type == 'hidden' || $type == 'token') {
			$hidden[] = drupal_render($variables['form'][$key]);
				}
else {
	$variables['formexample_nameform'][$key] = drupal_render($variables['form'][$key]);
	}
}
// Hidden form elements have no value to themers. No need for separation.
$variables['formexample_nameform']['hidden'] = implode($hidden);
// Collect all form elements to make it easier to print the whole form.
$variables['formexample_nameform_form'] = implode($variables['formexample_nameform']);
}



function trade_history_import_form_validate($form, &$form_state)
{
	// attempt to save the uploaded file
	$file = file_save_upload('file_upload', 
		array(
			'file_validate_extensions' => array('csv')
		)
	);

	// check file uploaded OK
	if (!$file) {
		form_set_error('file_upload', t('A file must be uploaded or selected from FTP updates.'));
	} else if($file->filemime != 'text/csv') {
		form_set_error('file_upload', t('The file must be of CSV type only.'));
	} else {
		// set files to form_state, to process when form is submitted
		$form_state['values']['file_upload'] = $file;
	}
}


function trade_history_import_form_submit($form, &$form_state)
{
	$line_max = variable_get('user_import_line_max', 1000);
	ini_set('auto_detect_line_endings', true);

	//$filepath = $form_state['values']['file_upload']->filepath;
	$filepath = $form_state['values']['file_upload']->uri;
	$handle = @fopen($filepath, "r");

	// start count of imports for this upload
	$send_counter = 0;

	while ($row = fgetcsv($handle, $line_max, ',')) {
		$row[1] = time();
		$row[3] = (int) $row[3];
		$row[1] = $row[10] = $row[11] = date('Y-m-d H:i:s');
		var_dump($row);
		db_query("INSERT INTO {stock_trade_history} VALUES(NULL, 
			:stock_id, 
			:trade_day, 
			:trade_type,
			:num,
		    :price,
		    :total,
			:stamp_tax,
			:poundage,
			:transfer_fee,
			:status,
			:created_at,
			:modified_at)", 
			array(
				':stock_id' => $row[0],
				':trade_day' => $row[1],
				':trade_type' => $row[2],
				':num' => $row[3],
				':price' => $row[4],
				':total' => $row[5],
				':stamp_tax' => $row[6],
				':poundage' => $row[7],
				':transfer_fee' => $row[8],
				':status' => $row[9],
				':created_at' => $row[10],
				':modified_at' => $row[11]

			)
		);
	}


	drupal_set_message('Import sucess.');
}

function trade_history_load($id)
{
	return db_query('SELECT * FROM {stock_trade_history} WHERE id=:id', array(':id' => $id));
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值