|
本帖最后由 consatan 于 2010-3-2 15:00 编辑
下面是我的数据库
SQL复制代码
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
CREATE TABLE IF NOT EXISTS `goods_barcode` (
`barcode` INT(11) NOT NULL AUTO_INCREMENT COMMENT '条形码',
`in_id` INT(6) NOT NULL DEFAULT '0' COMMENT '收货单号',
`out_id` INT(6) NOT NULL DEFAULT '0' COMMENT '发货单号',
`in_item_no` INT(4) NOT NULL DEFAULT '1' COMMENT '收货组序号',
`out_item_no` INT(4) NOT NULL DEFAULT '1' COMMENT '发货组序号',
`status` INT(1) NOT NULL DEFAULT '1' COMMENT '状态(1:就绪|2:收货|3:发货|4:完成)',
PRIMARY KEY (`barcode`),
KEY `in_id` (`in_id`),
KEY `out_id` (`out_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='条形码货物' AUTO_INCREMENT=1011 ;
INSERT INTO `goods_barcode` (`barcode`, `in_id`, `out_id`, `in_item_no`, `out_item_no`, `status`) VALUES
(1000, 0, 0, 1, 1, 1),
(1001, 0, 0, 1, 1, 1),
(1002, 0, 0, 1, 1, 1),
(1003, 0, 0, 1, 1, 1),
(1004, 0, 0, 1, 1, 1),
(1005, 0, 0, 1, 1, 1),
(1006, 0, 0, 1, 1, 1),
(1007, 0, 0, 1, 1, 1),
(1008, 0, 0, 1, 1, 1),
(1009, 0, 0, 1, 1, 1),
(1010, 0, 0, 1, 1, 1),
CREATE TABLE IF NOT EXISTS `goods_in` (
`in_bill_id` INT(6) NOT NULL AUTO_INCREMENT COMMENT '收货单号',
`in_date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '收货日期',
PRIMARY KEY (`in_bill_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='收货单' AUTO_INCREMENT=2 ;
INSERT INTO `goods_in` (`in_bill_id`, `in_date`) VALUES
(0, '2009-01-01 00:00:00');
CREATE TABLE IF NOT EXISTS `goods_out` (
`out_bill_id` INT(6) NOT NULL AUTO_INCREMENT COMMENT '发货单号',
`out_date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '发货日期',
PRIMARY KEY (`out_bill_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='发货单' AUTO_INCREMENT=2 ;
INSERT INTO `goods_out` (`out_bill_id`, `out_date`) VALUES
(0, '2009-02-01 00:00:00');
ALTER TABLE `goods_barcode`
ADD CONSTRAINT `goods_barcode_ibfk_2` FOREIGN KEY (`in_id`) REFERENCES `goods_in` (`in_bill_id`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `goods_barcode_ibfk_3` FOREIGN KEY (`out_id`) REFERENCES `goods_out` (`out_bill_id`) ON DELETE CASCADE ON UPDATE CASCADE;
复制代码
下面是Controller
PHP复制代码
<?php
class Goods extends Controller {
function Goods () {
parent ::Controller();
}
function barcode () {
$data = array('info' => "0",
'over' => 0,
'bill_id' => -1,
'item_no' => 1,
'status' => 0
);
$this->load->view('barcode',$data);
}
function get_barcode () {
$action = $this->input->post('inout');
$barcode = $this->input->post('barcode');
$bill_id = $this->input->post('bill_id');
$item_no = $this->input->post('item_no');
$status = $this->input->post('status');
$data = array('info' => "0",
'over' => 0,
'barcode' => $barcode,
'bill_id' => $bill_id,
'item_no' => $item_no,
'status' => $status
);
$this->load->model('Goods_Model','Goods');
$ise = $this->Goods->isExist($barcode);
if(count($ise) > 0) {
$data['status'] = $ise[0]['status'];
switch ($ise[0]['status']) {
case 1:
$this->process_goods($data,$action);
break;
case 2:
if($action == "in_bill") {
$data = $this->init_data($data);
$data['info'] = "此条形码的货物已经入库!";
return $this->load->view('barcode',$data);
} else if($action == "out_bill") {
$this->process_goods($data,$action);
}
break;
case 3:
$data = $this->init_data($data);
if($action == "out_bill") {
$data['info'] = "此条形码的货物已经出货!";
} else if($action == "in_bill") {
$data['info'] = "此条形码的货物未经入库就已经出货!";
}
return $this->load->view('barcode',$data);
break;
case 4:
$data = $this->init_data($data);
$data['info'] = "此条形码的货物曾经入库,并已经发货!";
return $this->load->view('barcode',$data);
break;
}
} else {
$data = $this->init_data($data);
$data['info'] = "不存在此条形码!";
return $this->load->view('barcode',$data);
}
}
function process_goods ($data,$action) {
$this->load->model('Goods_Model','Goods');
if($data['bill_id'] == -1) { //bill_id = -1 则建立收货单号
$bill = $this->Goods->insert_bill($action);
if($bill != 0) {
$data['bill_id'] = $bill[0]['liid'];
}
}
$this->Goods->update_bill($data,$action);
$data['info'] = "0";
$data['over'] = 2;
$data['item_no'] = number_format($data['item_no']) + 1;
$data['status'] = 0;
$this->load->view('barcode',$data);
}
function init_data ($data) {
$data['info'] = "0";
$data['over'] = 0;
$data['status'] = 0;
return $data;
}
}
?>
复制代码
下面是Model
PHP复制代码
<?php
class Goods_Model extends Model {
function Goods_Model () {
parent ::Model();
}
function isExist ($barcode) {
$sql = "select status from goods_barcode where barcode = ?";
return $this->db->query($sql,array($barcode))->result_array();
}
function insert_bill ($action) {
$tab = "";
$data = array();
if($action == "in_bill") {
$tab = "goods_in";
$data['in_date'] = date('Y-m-d');
} else {
$tab = "goods_out";
$data['out_date'] = date('Y-m-d');
}
$this->db->insert($tab,$data);
return $this->db->query("select last_insert_id() as liid")->result_array();
}
function update_bill ($data,$action) {
$row = array();
if($action == "in_bill") {
$row['id'] = "in_id";
$row['item_no'] = "in_item_no";
if($data['status'] == 1) {
$data['status'] = 2;
}
} else if($action == "out_bill") {
$row['id'] = "out_id";
$row['item_no'] = "out_item_no";
if($data['status'] == 2) {
$data['status'] =4;
} else if($data['status'] == 1) {
$data['status'] =3;
}
}
$sql = "UPDATE `goods_barcode` SET `".$row['id']."` = ?, `".$row['item_no']."` = ?, `status` = ? WHERE `barcode` = ?";
$this->db->query($sql,array($data['bill_id'],$data['item_no'],$data['status'],$data['barcode']));
}
}
?>
复制代码
最后是View
HTML复制代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8" />
<title>条形码 </title>
</head>
<body>
<script type="text/javascript">
function _submit(obj) {
document.getElementById('barcode').value = parseInt(document.getElementById('barcode').value,10);
obj.submit();
}
</script>
<?php
$URL_SENDBARCODE = array('goods','get_barcode');
?>
<form id"sendBC" name="sendBC" method="POST" action="<?php echo site_url($URL_SENDBARCODE) ?>" autocomplete="off" >
<input type="hidden" id="bill_id" name="bill_id" value="-1" />
<input type="hidden" id="item_no" name="item_no" value="1" />
<input type="hidden" id="status" name="status" value="0" />
<input type="hidden" id="inout" name="inout" value="">
<div style="text-align:center;">
<table border="0" style="width:310px;">
<tr>
<td>
<input type="text" id="barcode" name="barcode" value="" maxlength="20" />
</td>
</tr>
<tr>
<td style="text-align:center;">
<input type="submit" id="msbc" name="msbc" value="手动提交条形码" />
</td>
</tr>
</table>
</div>
<script type="text/javascript">
document.getElementById('inout').value = "in_bill";
if((parseInt( <?php echo $over ?>,10)) !== 0) { //$over: 0:默认|1太轻|2:标准|3:过重
sound((parseInt( <?php echo $over ?>,10)));
}
if(" <?php echo $info ?>" != "0") {
alert(" <?php echo $info ?>");
document.getElementById('bill_id').value = parseInt( <?php echo $bill_id ?>,10);
document.getElementById('item_no').value = <?php echo $item_no ?>;
document.getElementById('status').value = 0;
}
function sound(num) {
if(num === 2) {
alert("入库成功!");
document.getElementById('bill_id').value = parseInt( <?php echo $bill_id ?>,10);
document.getElementById('item_no').value = <?php echo $item_no ?>;
document.getElementById('status').value = 0;
}
}
if(document.getElementById('bill_id').value == -1) {
document.getElementById('bill_id').value = parseInt( <?php echo $bill_id ?>,10);
}
document.getElementById('barcode').focus();
</script>
</form>
</body>
</html>
复制代码
数据库中默认有1000~1010的条形码,我打开http://servername/goods/barcode页面
然后输入1000,提示“入库成功”
然后再输入1001,则有2种可能...一种是入库成功,一种是已经入库...但数据库都是初始化建立的...根本不可能存在已经入库的可能啊(但提示完再去查数据库,状态却确实已经更新为已经入库了...)
今天在弄一个项目的流程...遇到这问题...上面的代码是我把很多其余的信息都删除后,最直接的业务流程了...实在看不出哪里导致我说的这问题...
当局者迷...请路过的帮忙看一下吧...看了3、4个小时了,实在找不出哪里有问题... |
|