https://blog.csdn.net/dancheng1/article/details/78886811
<?php
namespace app\cangku\controller;
use think\Controller;
class Chukuguanli extends Controller{
//==============================出库===========================
public function chuku(){
$data = db("chuku_chuku")->select();
foreach($data as $k=>$v){
$data[$k]["cktime"] = date("Y-m-d H:i:s", $data[$k]["cktime"]);
}
$this->assign('data',$data);
return view();
}
public function chuku_delete(){
$id = input("post.id");
$time = time();
$uid = session("uid");
//查出出库表里的出库的产品名
$shu = db("chuku_chuku")->where("id",$id)->select();
$pname = $shu[0]["pname"];
$ckamount = $shu[0]["ckamount"];
$price = $shu[0]["price"];
//在库存表里通过产品名查出该条库存记录并用库存量加上订单的量算出当前库存量
$ku = db("kucun")->where("pname",$pname)->select();
$ghamount = $ku[0]["amount"]+$shu[0]["ckamount"];
$ghtotalprice = $ku[0]["totalprice"]+ $shu[0]["totalprice"];
$rizhi = ["id"=>"","operator"=>"$uid","time"=>"$time","event"=>"删除出库单(".$pname." ".$ckamount."单价:".$price.")"];
db("rizhi")->insert($rizhi);
//在数据库中删除该条出库单记录同时更改库存量
if(db("chuku_chuku")->where("id",$id)->delete()){
db("kucun")->where("pname",$pname)->
update(["amount"=>"$ghamount","totalprice"=>"$ghtotalprice"]);
echo "ok";
};
}
public function chuku_xq(){
$id = input("param.id");
$data = db("chuku_chuku")->where('id',$id)->select();
//把时间戳改成日期
$data[0]["cktime"] = date("Y-m-d H:i:s", $data[0]["cktime"]);
$this->assign('data',$data);
return view();
}
public function chuku_addview(){
return view();
}
public function chuku_add(){
$pcode = input("post.pcode");
$pname = input("post.pname");
$ckunit = input("post.ckunit");
$amount = input("post.amount");
$price = input("post.price");
$time = time();
$uid = session("uid");
$totalprice = $amount*$price;
//计算出库存增加后的数量和总价
$ku = db("kucun")->where("pname",$pname)->select();
$ghamount = $ku[0]['amount']-$amount;
$ghtotalprice = $ku[0]['totalprice']-$totalprice;
//添加日志
$rizhi = ["id"=>"","operator"=>"$uid","time"=>"$time","event"=>"商品出库(".$pname." ".$amount."单价:".$price.")"];
db("rizhi")->insert($rizhi);
//添加出库数据
if(!empty($amount)&&!empty($price)){
$data = ["id"=>"","pcode"=>"$pcode","pname"=>"$pname","ckunit"=>"$ckunit",
"cktime"=>"$time","ckamount"=>"$amount","totalprice"=>"$totalprice",
"operator"=>"$uid","price"=>"$price"];
if(db("chuku_chuku")->insert($data)){
//订单量添加到库存
db("kucun")->where("pname",$pname)->
update(["amount"=>"$ghamount","totalprice"=>"$ghtotalprice"]);
echo "ok";
};
}else{
echo "no";
}
}
//===================商品名,编号,库存单价二级联动=========================
public function pname(){
$data = db("add_product")->field("pname")->select();
echo json_encode($data);
}
public function dqkucun(){
$pname = input("post.pname");
$data = db("kucun")->field("amount")->where("pname",$pname)->select();
echo json_encode($data);
}
public function pcode(){
$pname = input("post.pname");
$data = db("add_product")->field("pcode")->where("pname",$pname)->select();
echo json_encode($data);
}
public function price(){
$pname = input("post.pname");
$data = db("kucun")->field("price")->where("pname",$pname)->select();
echo json_encode($data);
}
//========================退库============================
public function tuiku(){
$data = db("chuku_tuiku")->select();
foreach($data as $k=>$v){
$data[$k]["tktime"] = date("Y-m-d H:i:s", $data[$k]["tktime"]);
}
$this->assign('data',$data);
return view();
}
public function tuiku_delete(){
$id = input("post.id");
$time = time();
$uid = session("uid");
//查出出库表里的出库的产品名
$shu = db("chuku_tuiku")->where("id",$id)->select();
$pname = $shu[0]["pname"];
$tkamount = $shu[0]["tkamount"];
$price = $shu[0]["price"];
//添加日志
$rizhi = ["id"=>"","operator"=>"$uid","time"=>"$time","event"=>"删除退库单(".$pname." ".$tkamount."单价:".$price.")"];
db("rizhi")->insert($rizhi);
//在库存表里通过产品名查出该条库存记录并用库存量加上订单的量算出当前库存量
$ku = db("kucun")->where("pname",$pname)->select();
$ghamount = $ku[0]["amount"]-$shu[0]["tkamount"];
$ghtotalprice = $ku[0]["totalprice"]- $shu[0]["totalprice"];
//在数据库中删除该条出库单记录同时更改库存量
if(db("chuku_tuiku")->where("id",$id)->delete()){
db("kucun")->where("pname",$pname)->
update(["amount"=>"$ghamount","totalprice"=>"$ghtotalprice"]);
echo "ok";
};
}
public function tuiku_xq(){
$id = input("param.id");
$data = db("chuku_tuiku")->where('id',$id)->select();
//把时间戳改成日期
$data[0]["tktime"] = date("Y-m-d H:i:s", $data[0]["tktime"]);
$this->assign('data',$data);
return view();
}
public function tuiku_addview(){
return view();
}
public function tuiku_add(){
$pcode = input("post.pcode");
$pname = input("post.pname");
$tkunit = input("post.tkunit");
$amount = input("post.amount");
$price = input("post.price");
$time = time();
$uid = session("uid");
$totalprice = $amount*$price;
//计算出库存增加后的数量和总价
$ku = db("kucun")->where("pname",$pname)->select();
$ghamount = $ku[0]['amount']+$amount;
$ghtotalprice = $ku[0]['totalprice']+$totalprice;
//添加日志
$rizhi = ["id"=>"","operator"=>"$uid","time"=>"$time","event"=>"商品退库(".$pname." ".$amount."单价:".$price.")"];
db("rizhi")->insert($rizhi);
//添加退货单数据
if(!empty($amount)&&!empty($price)){
$data = ["id"=>"","pcode"=>"$pcode","pname"=>"$pname","tkunit"=>"$tkunit",
"tktime"=>"$time","tkamount"=>"$amount","totalprice"=>"$totalprice",
"operator"=>"$uid","price"=>"$price"];
if(db("chuku_tuiku")->insert($data)){
//退单量添加到库存
db("kucun")->where("pname",$pname)->
update(["amount"=>"$ghamount","totalprice"=>"$ghtotalprice"]);
echo "ok";
};
}else{
echo "no";
}
}
}
?>
<!DOCTYPE html>
<!--
BeyondAdmin - Responsive Admin Dashboard Template build with Twitter Bootstrap 3.2.0
Version: 1.0.0
Purchase: http://wrapbootstrap.com
-->
<html xmlns="http://www.w3.org/1999/xhtml">
<!-- Head -->
<head>
<meta charset="utf-8" />
<title>Data Tables</title>
<meta name="description" content="data tables" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="shortcut icon" href="__STATIC__/cangku/img/favicon.png" type="image/x-icon">
<!--Basic Styles-->
<link href="__STATIC__/cangku/css/bootstrap.min.css" rel="stylesheet" />
<link id="bootstrap-rtl-link" href="" rel="stylesheet" />
<link href="__STATIC__/cangku/css/font-awesome.min.css" rel="stylesheet" />
<link href="__STATIC__/cangku/css/weather-icons.min.css" rel="stylesheet" />
<!--Fonts-->
<!-- <link href="http://fonts.useso.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,400,600,700,300" rel="stylesheet" type="text/css">
-->
<!--Beyond styles-->
<link href="__STATIC__/cangku/css/beyond.min.css" rel="stylesheet" />
<link href="__STATIC__/cangku/css/demo.min.css" rel="stylesheet" />
<link href="__STATIC__/cangku/css/typicons.min.css" rel="stylesheet" />
<link href="__STATIC__/cangku/css/animate.min.css" rel="stylesheet" />
<link id="skin-link" href="" rel="stylesheet" type="text/css" />
<!--Page Related styles-->
<link href="__STATIC__/cangku/css/dataTables.bootstrap.css" rel="stylesheet" />
<!--Skin Script: Place this script in head to load scripts for skins and rtl support-->
<script src="__STATIC__/public/offten/jquery-3.3.1.min.js" type="text/javascript" charset="utf-8"></script>
<script src="__STATIC__/cangku/js/skins.min.js"></script>
</head>
<!-- /Head -->
<!-- Body -->
<body>
<!-- Loading Container -->
<div class="loading-container">
<div class="loading-progress">
<div class="rotator">
<div class="rotator">
<div class="rotator colored">
<div class="rotator">
<div class="rotator colored">
<div class="rotator colored"></div>
<div class="rotator"></div>
</div>
<div class="rotator colored"></div>
</div>
<div class="rotator"></div>
</div>
<div class="rotator"></div>
</div>
<div class="rotator"></div>
</div>
<div class="rotator"></div>
</div>
</div>
<!-- /Loading Container -->
<!-- Navbar -->
<div class="navbar">
<div class="navbar-inner">
<div class="navbar-container">
<!-- Navbar Barnd -->
<div class="navbar-header pull-left">
<a href="#" class="navbar-brand"style="line-height:35px;">
<small >
<!-- <img src="__STATIC__/cangku/img/logo.png" alt="" />-->
仓库管理系统
</small>
</a>
</div>
<!-- /Navbar Barnd -->
<!-- Sidebar Collapse -->
<div class="sidebar-collapse" id="sidebar-collapse">
<i class="collapse-icon fa fa-bars"></i>
</div>
<!-- /Sidebar Collapse -->
<!-- Account Area and Settings --->
<div class="navbar-header pull-right">
<div class="navbar-account">
<ul class="account-area">
<li>
<a class="login-area dropdown-toggle" data-toggle="dropdown">
<div class="avatar" title="View your public profile">
<img src="__STATIC__/cangku/img/avatars/adam-jansen.jpg">
</div>
<section>
<h2><span class="profile">管理员</span></h2>
</section>
</a>
<!--Login Area Dropdown-->
<ul class="pull-right dropdown-menu dropdown-arrow dropdown-login-area">
<!--<!-- <li class="username"><a>David Stevenson</a></li>
<li class="email"><a>David.Stevenson@live.com</a></li>-->
<!--Avatar Area-->
<!-- <li>
<div class="avatar-area">
<img src="__STATIC__/cangku/img/avatars/adam-jansen.jpg" class="avatar">
<span class="caption">Change Photo</span>
</div>
</li>-->
<!--Avatar Area-->
<li class="edit">
<a href="profile.html" class="pull-left"></a>
<a href="#" class="pull-right">改变风格</a>
</li>
<!--Theme Selector Area-->
<li class="theme-area">
<ul class="colorpicker" id="skin-changer">
<li><a class="colorpick-btn" href="#" style="background-color:#5DB2FF;" rel="__STATIC__/cangku/css/skins/blue.min.css"></a></li>
<li><a class="colorpick-btn" href="#" style="background-color:#2dc3e8;" rel="__STATIC__/cangku/css/skins/azure.min.css"></a></li>
<li><a class="colorpick-btn" href="#" style="background-color:#03B3B2;" rel="__STATIC__/cangku/css/skins/teal.min.css"></a></li>
<li><a class="colorpick-btn" href="#" style="background-color:#53a93f;" rel="__STATIC__/cangku/css/skins/green.min.css"></a></li>
<li><a class="colorpick-btn" href="#" style="background-color:#FF8F32;" rel="__STATIC__/cangku/css/skins/orange.min.css"></a></li>
<li><a class="colorpick-btn" href="#" style="background-color:#cc324b;" rel="__STATIC__/cangku/css/skins/pink.min.css"></a></li>
<li><a class="colorpick-btn" href="#" style="background-color:#AC193D;" rel="__STATIC__/cangku/css/skins/darkred.min.css"></a></li>
<li><a class="colorpick-btn" href="#" style="background-color:#8C0095;" rel="__STATIC__/cangku/css/skins/purple.min.css"></a></li>
<li><a class="colorpick-btn" href="#" style="background-color:#0072C6;" rel="__STATIC__/cangku/css/skins/darkblue.min.css"></a></li>
<li><a class="colorpick-btn" href="#" style="background-color:#585858;" rel="__STATIC__/cangku/css/skins/gray.min.css"></a></li>
<li><a class="colorpick-btn" href="#" style="background-color:#474544;" rel="__STATIC__/cangku/css/skins/black.min.css"></a></li>
<li><a class="colorpick-btn" href="#" style="background-color:#001940;" rel="__STATIC__/cangku/css/skins/deepblue.min.css"></a></li>
</ul>
</li>
<!--/Theme Selector Area-->
<!--<li class="dropdown-footer">
<a href="login.html">
Sign out
</a>
</li>-->
</ul>
<!--/Login Area Dropdown-->
</li>
<!-- /Account Area -->
<!--Note: notice that setting div must start right after account area list.
no space must be between these elements-->
<!-- Settings -->
</div>
</div>
<!-- /Account Area and Settings -->
</div>
</div>
</div>
<!-- /Navbar -->
<!-- Main Container -->
<div class="main-container container-fluid">
<!-- Page Container -->
<div class="page-container">
<!-- Page Sidebar -->
<div class="page-sidebar" id="sidebar">
<!-- Page Sidebar Header-->
<div class="sidebar-header-wrapper">
<i class="searchicon fa fa-search"></i>
</div>
<!-- /Page Sidebar Header -->
<!-- 导航菜单打开状态class="active open"-->
<!-- 导航分菜单打开状态class="active "-->
<!-- 左侧导航 -->
{include file="../application/cangku/view/Chukuguanli/chuku_header/header.html"/}
<!-- /左侧导航 -->
</div>
<!-- /Page Sidebar -->
<!-- Page Content -->
<div class="page-content">
<!-- Page Breadcrumb -->
<div class="page-breadcrumbs">
<ul class="breadcrumb">
<li>
首页
</li>
<li>
出库管理
</li>
<li class="active">出库</li>
</ul>
</div>
<!-- /Page Breadcrumb -->
<!-- Page Header -->
<div class="page-header position-relative">
<div class="header-title">
<h1>
<small>
<!-- <i class="fa fa-angle-right"></i>-->
</small>
</h1>
</div>
<!--右侧第二行最右边按钮-->
<div class="header-buttons">
<a class="sidebar-toggler" href="#">
<i class="fa fa-arrows-h"></i>
</a>
<a class="refresh" id="refresh-toggler" href="">
<i class="glyphicon glyphicon-refresh"></i>
</a>
<a class="fullscreen" id="fullscreen-toggler" href="#">
<i class="glyphicon glyphicon-fullscreen"></i>
</a>
</div>
<!--右侧第二行最右边按钮-->
</div>
<!-- /Page Header -->
<!-- Page Body -->
<div class="page-body">
<div class="row">
<div class="col-xs-12 col-md-12">
<div class="widget">
<div class="widget-header ">
<span class="widget-caption"></span>
<div class="widget-buttons">
<a href="#" data-toggle="maximize">
<i class="fa fa-expand"></i>
</a>
<a href="#" data-toggle="collapse">
<i class="fa fa-minus"></i>
</a>
<a href="#" data-toggle="dispose">
<i class="fa fa-times"></i>
</a>
</div>
</div>
<div class="widget-body">
<div class="row">
<div class="col-xs-12 col-md-12">
<div class="well with-header with-footer">
<div class="header bg-blue">
添加出库单
</div>
<table class="table table-hover">
<thead class="bordered-darkorange">
<tr>
<th>
选项
</th>
<th>
</th>
<th>
</th>
<th>
</th>
<th>
</th>
<th>
数据
</th>
</tr>
</thead>
<tbody>
<tr>
<td>
商品编号
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
<select id="pcode" style="width:130px;height: 30px;">
</select>
<!--<input type="text" name="" id="pcode" value="">-->
</td>
</tr>
<tr>
<td>
商品名称
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
<select id="pname" style="width:130px;height: 30px;">
</select>
</td>
</tr>
<tr>
<td>
出库数量
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
<input type="text" id="amount" style="width:130px;height: 30px;">
<div>
当前库存量:<span id="kc"></span>
</div>
</td>
</tr>
<tr>
<td>
库存单价
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
<select id="price" style="width: 130px;height: 30px;">
</select>
</td>
</tr>
<tr>
<td>
出库对象
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
</td>
<td>
<input type="text" id="ckunit" style="width: 130px;height: 30px;">
</td>
</tr>
</tbody>
</table>
<div style="margin-top: 15px;">
<a href="#" class="btn btn-danger btn-xs" οnclick="add()">提交</a>
<a href="chuku.html" class="btn btn-palegreen btn-xs" >返回</a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- /Page Body -->
</div>
<!-- /Page Content -->
</div>
<!-- /Page Container -->
<!-- Main Container -->
</div>
<!--Basic Scripts-->
<script src="__STATIC__/cangku/js/jquery-2.0.3.min.js"></script>
<script src="__STATIC__/cangku/js/bootstrap.min.js"></script>
<!--Beyond Scripts-->
<script src="__STATIC__/cangku/js/beyond.min.js"></script>
<!--Page Related Scripts-->
<script src="__STATIC__/cangku/js/datatable/jquery.dataTables.min.js"></script>
<script src="__STATIC__/cangku/js/datatable/ZeroClipboard.js"></script>
<script src="__STATIC__/cangku/js/datatable/dataTables.tableTools.min.js"></script>
<script src="__STATIC__/cangku/js/datatable/dataTables.bootstrap.min.js"></script>
<script src="__STATIC__/cangku/js/datatable/datatables-init.js"></script>
<script>
InitiateSimpleDataTable.init();
InitiateEditableDataTable.init();
InitiateExpandableDataTable.init();
InitiateSearchableDataTable.init();
</script>
<!--Google Analytics::Demo Only-->
<script>
(function (i, s, o, g, r, a, m) {
i['GoogleAnalyticsObject'] = r; i[r] = i[r] || function () {
(i[r].q = i[r].q || []).push(arguments)
}, i[r].l = 1 * new Date(); a = s.createElement(o),
m = s.getElementsByTagName(o)[0]; a.async = 1; a.src = g; m.parentNode.insertBefore(a, m)
})(window, document, 'script', 'http://www.google-analytics.com/analytics.js', 'ga');
ga('create', 'UA-52103994-1', 'auto');
ga('send', 'pageview');
</script>
</body>
<!-- /Body -->
</html>
<script type="text/javascript">
$(function(){
pname();
})
function pname(){
$.ajax({
type:"post",
url:"{:url('pname')}",
async:false,
data:{
},
dataType:"json",
success:function(data){
var str = "";
for(var i=0;i<data.length;i++){
console.log(data[i]['pname']);
str += "<option value='"+data[i]['pname']+"'>"+data[i]['pname']+"</option>";
}
$("#pname").html(str);
pcode();
price();
dqkucun();
}
});
}
function dqkucun(){
var pname = $("#pname").val();
$.ajax({
type:"post",
url:"{:url('dqkucun')}",
async:false,
data:{
pname:pname
},
dataType:"json",
success:function(data){
for(var i=0;i<data.length;i++){
$("#kc").html(data[i]['amount']);
}
}
});
}
function pcode(){
var pname = $("#pname").val();
$.ajax({
type:"post",
url:"{:url('pcode')}",
async:false,
data:{
pname:pname
},
dataType:"json",
success:function(data){
var str = "";
for(var i=0;i<data.length;i++){
console.log(data[i]['pcode']);
str += "<option value='"+data[i]['pcode']+"'>"+data[i]['pcode']+"</option>";
}
$("#pcode").html(str);
}
});
}
function price(){
var pname = $("#pname").val();
$.ajax({
type:"post",
url:"{:url('price')}",
async:false,
data:{
pname:pname
},
dataType:"json",
success:function(data){
var str = "";
for(var i=0;i<data.length;i++){
console.log(data[i]['price']);
str += "<option value='"+data[i]['price']+"'>"+data[i]['price']+"</option>";
}
$("#price").html(str);
}
});
}
$("#pname").change(function(){
pcode();
price();
dqkucun();
})
function add(){
var pcode = $("#pcode").val();
var pname = $("#pname").val();
var ckunit = $("#ckunit").val();
var amount = $("#amount").val();
var price = $("#price").val();
$.ajax({
type:"post",
url:"{:url('chuku_add')}",
async:true,
data:{
pcode:pcode,
pname:pname,
ckunit:ckunit,
amount:amount,
price:price
},
dataType:"text",
success: function(data){
if(data=="ok"){
alert("添加成功");
window.location.href="chuku.html";
}else if(data=="no"){
alert("数据不能为空");
}
}
});
}
</script>