ecshop物料库存管理
1、创建物流库存表。sql语句:
CREATE TABLE IF NOT EXISTS `emws_materials` (
`id` mediumint(8) unsigned NOT NULL auto_increment,
`name` varchar(60) NOT NULL,
`modulus` varchar(60) NOT NULL,
`stock_number` smallint(5) unsigned NOT NULL default '0',
`stock_in` smallint(5) unsigned NOT NULL default '0',
`stock_out` smallint(5) unsigned NOT NULL default '0',
`safe_day` smallint(5) unsigned NOT NULL default '0',
`intent_day` smallint(5) unsigned NOT NULL default '0',
`is_buy` tinyint(1) unsigned NOT NULL default '1',
`buy_url` varchar(60) NOT NULL,
`price` decimal(10,2) NOT NULL,
`weight` smallint(5) unsigned NOT NULL default '0',
`img` varchar(60) NOT NULL,
`desc_info` varchar(60) NOT NULL,
`remark` varchar(60) NOT NULL,
`admin_id` smallint(5) unsigned NOT NULL,
`update_time` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
2、php程序,materials.php:
<?php
define('IN_ECS', true);
require(dirname(__FILE__) . '/includes/init.php');
include_once(ROOT_PATH . 'includes/cls_image.php');
$image = new cls_image($_CFG['bgcolor']);
$exc = new exchange($ecs->table("materials"), $db, 'id', 'name');
$_REQUEST['act']=!empty($_REQUEST['act']) ? $_REQUEST['act']:'list';
admin_priv('stock_alert');//权限:库存数量修改
if($_REQUEST['act'] == 'list')
{
$stock_list=material_list();
$smarty->assign('ur_here', '物料库存列表');
$smarty->assign('stock_list', $stock_list['stock_list']);
$smarty->assign('filter', $stock_list['filter']);
$smarty->assign('record_count', $stock_list['record_count']);
$smarty->assign('page_count', $stock_list['page_count']); $smarty->assign('shelf_list', $shelf_list);
$smarty->assign('full_page', 1);
$smarty->assign('action_link', array('href' => 'goods_stock.php?act=list', 'text' => '商品库存列表'));
$smarty->assign('action_link2', array('href' => 'materials.php?act=export', 'text' => '导出采购单'));
$smarty->assign('action_link3', array('href' => 'materials.php?act=add', 'text' => '添加物料')); $smarty->display('material_list.htm');
}
elseif($_REQUEST['act'] == 'add')
{
$smarty->assign('ur_here', "添加物料");
$smarty->assign('action_link', array('href' => 'materials.php?act=list', 'text' => '返回物料库存列表'));
$smarty->assign('form_action', "insert"); assign_query_info();
$smarty->display('material_info.htm');
}
elseif($_REQUEST['act'] == 'insert')
{
$material['is_buy'] = isset($_REQUEST['is_buy']) ? intval($_REQUEST['is_buy']) : 1;
$material['name'] = isset($_REQUEST['name']) ? trim($_REQUEST['name']) : '';
$material['modulus'] = isset($_REQUEST['modulus']) ? trim($_REQUEST['modulus']) : '';
$material['safe_day'] = isset($_REQUEST['safe_day']) ? intval($_REQUEST['safe_day']) : 0;
$material['intent_day']= isset($_REQUEST['intent_day']) ? intval($_REQUEST['intent_day']) : 0;
$material['price'] = isset($_REQUEST['price']) ? floatval($_REQUEST['price']) : '0.00';
$material['weight'] = isset($_REQUEST['weight']) ? intval($_REQUEST['weight']) : 0;
$material['desc_info'] = isset($_REQUEST['desc_info']) ? trim($_REQUEST['desc_info']) : '';
$material['remark'] = isset($_REQUEST['remark']) ? trim($_REQUEST['remark']) : '';
$material['update_time']= gmtime();
$material['admin_id'] = $_SESSION['admin_id']; if(empty($material['name']) || empty($material['modulus']) || empty($material['safe_day']) || empty($material['intent_day']))
{
sys_msg('名称、系数、安全天数、目标天数,不能为空或者格式不正确', 1);
} $is_only = $exc->is_only('name', $material['name']);
if (!$is_only)
{
sys_msg($material['name'].',已存在', 1);
} /*处理图片*/
$material['img'] = basename($image->upload_image($_FILES['img'],'material'));
/*处理URL*/
$material['buy_url'] = sanitize_url($_POST['buy_url']);
/*插入数据*/
$db->autoExecute($ecs->table('materials'), $material, 'INSERT', '', 'SILENT'); $link[0]['text'] = '继续添加';
$link[0]['href'] = 'materials.php?act=add';
$link[1]['text'] = '返回列表';
$link[1]['href'] = 'materials.php?act=list';
sys_msg('添加成功', 0, $link);
}
elseif($_REQUEST['act'] == 'updata')
{
$id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : 0;
$material['is_buy'] = isset($_REQUEST['is_buy']) ? intval($_REQUEST['is_buy']) : 1;
$material['name'] = isset($_REQUEST['name']) ? trim($_REQUEST['name']) : '';
$material['modulus'] = isset($_REQUEST['modulus']) ? trim($_REQUEST['modulus']) : '';
$material['safe_day'] = isset($_REQUEST['safe_day']) ? intval($_REQUEST['safe_day']) : 0;
$material['intent_day']= isset($_REQUEST['intent_day']) ? intval($_REQUEST['intent_day']) : 0;
$material['price'] = isset($_REQUEST['price']) ? floatval($_REQUEST['price']) : '0.00';
$material['weight'] = isset($_REQUEST['weight']) ? intval($_REQUEST['weight']) : 0;
$material['desc_info'] = isset($_REQUEST['desc_info']) ? trim($_REQUEST['desc_info']) : '';
$material['remark'] = isset($_REQUEST['remark']) ? trim($_REQUEST['remark']) : '';
$material['update_time']= gmtime();
$material['admin_id'] = $_SESSION['admin_id']; if(empty($id))
{
sys_msg('ID不能为空', 1);
} if(empty($material['name']) || empty($material['modulus']) || empty($material['safe_day']) || empty($material['intent_day']))
{
sys_msg('名称、系数、安全天数、目标天数,不能为空或者格式不正确', 1);
} /*处理图片*/
if(!empty($_FILES['img']['name']))
{
$material['img'] = basename($image->upload_image($_FILES['img'],'material'));
}
/*处理URL*/
$material['buy_url'] = sanitize_url($_POST['buy_url']);
/*插入数据*/
$db->autoExecute($ecs->table('materials'), $material, 'UPDATE', "id = '$id'"); $link[0]['text'] = '继续编辑';
$link[0]['href'] = 'materials.php?act=edit&id='.$id;
$link[1]['text'] = '返回列表';
$link[1]['href'] = 'materials.php?act=list';
sys_msg('编辑成功', 0, $link);
}
elseif ($_REQUEST['act'] =='edit')
{
$sql = "SELECT * FROM " .$ecs->table('materials'). " WHERE id='$_REQUEST[id]'";
$material = $db->GetRow($sql);
$smarty->assign('ur_here', "编辑物料");
$smarty->assign('action_link', array('href' => 'materials.php?act=list', 'text' => '返回物料库存列表'));
$smarty->assign('material', $material);
$smarty->assign('form_action', 'updata');
assign_query_info();
$smarty->display('material_info.htm');
}
elseif ($_REQUEST['act'] == 'remove')
{
$id = intval($_GET['id']);
$exc->drop($id);
$url = 'materials.php?act=query&' . str_replace('act=remove', '', $_SERVER['QUERY_STRING']);
ecs_header("Location: $url\n");
exit;
}
elseif ($_REQUEST['act'] == 'drop_img')
{
$id = isset($_GET['id']) ? intval($_GET['id']) : 0; $sql = "SELECT img FROM " .$ecs->table('materials'). " WHERE id = '$id'";
$img_name = $db->getOne($sql); if (!empty($img_name))
{
@unlink(ROOT_PATH . DATA_DIR . '/material/' .$img_name);
$sql = "UPDATE " .$ecs->table('materials'). " SET img = '' WHERE id = '$id'";
$db->query($sql);
}
$link= array(array('text' => '继续编辑', 'href' => 'materials.php?act=edit&id=' . $id), array('text' => '返回物料库存列表', 'href' => 'materials.php?act=list'));
sys_msg('图片删除成功', 0, $link);
}
elseif ($_REQUEST['act'] == 'edit_stock_in') //更改入库
{
$id = intval($_POST['id']);
$val = json_str_iconv(trim($_POST['val']));
/* 检查格式 */
if(!is_numeric($val) || $val < 0)
{
make_json_error(sprintf("格式不正确!", $val));
} $exc->edit("stock_in='$val'", $id);
make_json_result(stripslashes($val));
}
elseif ($_REQUEST['act'] == 'edit_stock_out') //更改出库
{
$id = intval($_POST['id']);
$val = json_str_iconv(trim($_POST['val']));
/* 检查格式 */
if(!is_numeric($val) || $val < 0)
{
make_json_error(sprintf("格式不正确!", $val));
}
$sql="SELECT * FROM ".$GLOBALS['ecs']->table('materials')." where id = '".$id."'";
$material = $GLOBALS['db']->getRow($sql);
if($val > $material['stock_in'] + $material['stock_number'])
{
make_json_error(sprintf("出库数不能大于现有库存与入库总和!", $val));
} $exc->edit("stock_out='$val'", $id);
make_json_result(stripslashes($val));
}
elseif ($_REQUEST['act'] == 'operate') //批量入库/出库
{
$sql = "UPDATE " .$ecs->table('materials'). " SET stock_number = stock_number + stock_in - stock_out,stock_out = 0,stock_in = 0,admin_id=$_SESSION[admin_id],update_time = ".gmtime();
$db->query($sql);
$link= array(array('text' => '返回物料库存列表', 'href' => 'materials.php?act=list'));
sys_msg('成功批量入库/出库', 0, $link);
}
elseif ($_REQUEST['act'] == 'export') //导出采购单
{
include_once('includes/PHPExcel/PHPExcel.php');
include_once('corlor.php');
$objPHPExcel = new PHPExcel(); $filename = '物料采购表_'.date("YmdHi",gmtime());
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setTitle($filename);
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(15);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(10);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(10);
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(10);
$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(10);
$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(10);
$objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(10);
$objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(10);
$objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth(10);
$objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
$objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
$objPHPExcel->getActiveSheet()->getStyle('F1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
$objPHPExcel->getActiveSheet()->getStyle('G1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
$objPHPExcel->getActiveSheet()->getStyle('H1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
$objPHPExcel->getActiveSheet()->getStyle('I1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
$objPHPExcel->getActiveSheet()->getStyle('J1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
$objPHPExcel->getActiveSheet()->getColumnDimension('K')->setWidth(10);
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', '物料名称')
->setCellValue('B1', '图片')
->setCellValue('C1', '每天用量')
->setCellValue('D1', '现有库存')
->setCellValue('E1', '周转天数')
->setCellValue('F1', '安全库存')
->setCellValue('G1', '目标库存')
->setCellValue('H1', '建议购买')
->setCellValue('I1', '单价')
->setCellValue('J1', '实际单价')
->setCellValue('K1', '采购链接');
$i=2;
$stock_list = material_list(false);
$arr = $stock_list['stock_list'];
foreach($arr as $v)
{
if($v['img'])
{
$objPHPExcel->getActiveSheet()->getRowDimension($i)->setRowHeight(50);
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setName('goods thumb');
$objDrawing->setDescription('Pgoods thumb');
$img_path = file_exists('../data/material/'.$v['img']) ? '../data/material/'.$v['img'] : '../images/no_img.jpg';
$objDrawing->setPath($img_path);
$objDrawing->setWidth(100);
$objDrawing->setCoordinates('B'.$i);
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
}
else
{
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('B'.$i, '');
} $objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A'.$i, $v['name'])
->setCellValue('C'.$i, $v['day_use'])
->setCellValue('D'.$i, $v['stock_number'])
->setCellValue('E'.$i, $v['stock_day'])
->setCellValue('F'.$i, $v['stock_safe'])
->setCellValue('G'.$i, $v['stock_intent'])
->setCellValue('H'.$i, $v['proposal_buy'])
->setCellValue('I'.$i, $v['price'])
->setCellValue('J'.$i, '');
if($v['stock_safe'] >= $v['stock_number'])
{
$objPHPExcel->setActiveSheetIndex(0)->getStyle('D'.$i)->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_RED);
}
if($v['buy_url'] != 'http://')
{
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('K'.$i, '采购链接');
$objPHPExcel->setActiveSheetIndex(0)->getCell('K'.$i)->getHyperlink()->setUrl($v['buy_url']);
$objPHPExcel->setActiveSheetIndex(0)->getCell('K'.$i)->getHyperlink()->setTooltip('采购链接');
$objPHPExcel->setActiveSheetIndex(0)->getStyle('K'.$i)->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLUE);
$objPHPExcel->setActiveSheetIndex(0)->getStyle('K'.$i)->getFont()->setUnderline(PHPExcel_Style_Font::UNDERLINE_SINGLE);
}
else
{
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('K'.$i, '');
}
$objPHPExcel->getActiveSheet()->getStyle('A'.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('B'.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('C'.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('D'.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('E'.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('F'.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('G'.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('H'.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('I'.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('J'.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('K'.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$i++;
}
$file_name = $filename.'.xls';
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$file_name.'"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
}
elseif ($_REQUEST['act'] == 'query')
{
$stock_list = material_list();
$smarty->assign('stock_list', $stock_list['stock_list']);
$smarty->assign('filter', $stock_list['filter']);
$smarty->assign('record_count', $stock_list['record_count']);
$smarty->assign('page_count', $stock_list['page_count']);
make_json_result($smarty->fetch('material_list.htm'), '', array('filter' => $stock_list['filter'], 'page_count' => $stock_list['page_count']));
} function material_list($is_pagination = true)
{
GLOBAL $ecs,$db;
$result = get_filter();
if ($result === false)
{
$filter['sort_by'] = empty($_REQUEST['sort_by']) ? 'id' : trim($_REQUEST['sort_by']);
$filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'desc' : trim($_REQUEST['sort_order']);
$where = " WHERE 1 = 1 "; $sql = 'select count(t.id) from '.$ecs->table('materials'). ' as t '.$where; $filter['record_count'] = $db->getOne($sql); /* 分页大小 */
$filter = page_and_size($filter); $sql = 'select t.*, au.user_name from '.
$ecs->table('materials').' as t left join '.
$ecs->table('admin_user')." as au on t.admin_id=au.user_id ".$where.
' order by '.$filter['sort_by']." ".$filter['sort_order']; if ($is_pagination)
{
$sql .= " LIMIT " . $filter['start'] . ', ' . $filter['page_size'];
} $end_time = strtotime(date("Y-m-d",gmtime()));
$start_time = $end_time - 7 * 86400;
$query = "SELECT count(order_id) as total FROM ".$GLOBALS['ecs']->table('order_info')." WHERE synch_time < '".$end_time."' and synch_time >= '".$start_time."'";
$filter['orders'] = round($GLOBALS['db']->getOne($query) / 7);//7天平均订单数
$filter['orders'] = $filter['orders'] ? $filter['orders'] : 1400;
set_filter($filter, $sql);
}
else
{
$sql = $result['sql'];
$filter = $result['filter'];
}
$row = $GLOBALS['db']->getAll($sql); $orders = $filter['orders'];
foreach($row as $k=>$val)
{
if ($is_pagination == false && $val['is_buy'] == 0) //不购买,不导出
{
unset($row[$k]);
continue;
}
$row[$k]['update_time'] = local_date('Y-m-d H:i',$val['update_time']);
$row[$k]['day_use'] = $day_use = round($orders * $val['modulus'],1);//每日用量
$row[$k]['stock_day'] = $day_use ? round($val['stock_number'] / $day_use,1) : 0;//周转天数
$row[$k]['stock_safe'] = round($val['safe_day'] * $day_use,1);//安全库存
$row[$k]['stock_intent']= $stock_intent = round($val['intent_day'] * $day_use,1);//目标库存
$row[$k]['proposal_buy']= round($stock_intent - $val['stock_number'],1);//建议购买
} $stock_list = array('stock_list' => $row, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);
return $stock_list;
}
?>
<?php
define('IN_ECS', true);
require(dirname(__FILE__) . '/includes/init.php');
include_once(ROOT_PATH . 'includes/cls_image.php');
$image = new cls_image($_CFG['bgcolor']);
$exc = new exchange($ecs->table("materials"), $db, 'id', 'name');
$_REQUEST['act']=!empty($_REQUEST['act']) ? $_REQUEST['act']:'list';
admin_priv('stock_alert');//权限:库存数量修改
if($_REQUEST['act'] == 'list')
{
$stock_list=material_list();
$smarty->assign('ur_here', '物料库存列表');
$smarty->assign('stock_list', $stock_list['stock_list']);
$smarty->assign('filter', $stock_list['filter']);
$smarty->assign('record_count', $stock_list['record_count']);
$smarty->assign('page_count', $stock_list['page_count']);
$smarty->assign('shelf_list', $shelf_list);
$smarty->assign('full_page', 1);
$smarty->assign('action_link', array('href' => 'goods_stock.php?act=list', 'text' => '商品库存列表'));
$smarty->assign('action_link2', array('href' => 'materials.php?act=export', 'text' => '导出采购单'));
$smarty->assign('action_link3', array('href' => 'materials.php?act=add', 'text' => '添加物料'));
$smarty->display('material_list.htm');
}
elseif($_REQUEST['act'] == 'add')
{
$smarty->assign('ur_here', "添加物料");
$smarty->assign('action_link', array('href' => 'materials.php?act=list', 'text' => '返回物料库存列表'));
$smarty->assign('form_action', "insert");
assign_query_info();
$smarty->display('material_info.htm');
}
elseif($_REQUEST['act'] == 'insert')
{
$material['is_buy'] = isset($_REQUEST['is_buy']) ? intval($_REQUEST['is_buy']) : 1;
$material['name'] = isset($_REQUEST['name']) ? trim($_REQUEST['name']) : '';
$material['modulus'] = isset($_REQUEST['modulus']) ? trim($_REQUEST['modulus']) : '';
$material['safe_day'] = isset($_REQUEST['safe_day']) ? intval($_REQUEST['safe_day']) : 0;
$material['intent_day']= isset($_REQUEST['intent_day']) ? intval($_REQUEST['intent_day']) : 0;
$material['price'] = isset($_REQUEST['price']) ? floatval($_REQUEST['price']) : '0.00';
$material['weight'] = isset($_REQUEST['weight']) ? intval($_REQUEST['weight']) : 0;
$material['desc_info'] = isset($_REQUEST['desc_info']) ? trim($_REQUEST['desc_info']) : '';
$material['remark'] = isset($_REQUEST['remark']) ? trim($_REQUEST['remark']) : '';
$material['update_time']= gmtime();
$material['admin_id'] = $_SESSION['admin_id'];
if(empty($material['name']) || empty($material['modulus']) || empty($material['safe_day']) || empty($material['intent_day']))
{
sys_msg('名称、系数、安全天数、目标天数,不能为空或者格式不正确', 1);
}
$is_only = $exc->is_only('name', $material['name']);
if (!$is_only)
{
sys_msg($material['name'].',已存在', 1);
}
/*处理图片*/
$material['img'] = basename($image->upload_image($_FILES['img'],'material'));
/*处理URL*/
$material['buy_url'] = sanitize_url($_POST['buy_url']);
/*插入数据*/
$db->autoExecute($ecs->table('materials'), $material, 'INSERT', '', 'SILENT');
$link[0]['text'] = '继续添加';
$link[0]['href'] = 'materials.php?act=add';
$link[1]['text'] = '返回列表';
$link[1]['href'] = 'materials.php?act=list';
sys_msg('添加成功', 0, $link);
}
elseif($_REQUEST['act'] == 'updata')
{
$id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : 0;
$material['is_buy'] = isset($_REQUEST['is_buy']) ? intval($_REQUEST['is_buy']) : 1;
$material['name'] = isset($_REQUEST['name']) ? trim($_REQUEST['name']) : '';
$material['modulus'] = isset($_REQUEST['modulus']) ? trim($_REQUEST['modulus']) : '';
$material['safe_day'] = isset($_REQUEST['safe_day']) ? intval($_REQUEST['safe_day']) : 0;
$material['intent_day']= isset($_REQUEST['intent_day']) ? intval($_REQUEST['intent_day']) : 0;
$material['price'] = isset($_REQUEST['price']) ? floatval($_REQUEST['price']) : '0.00';
$material['weight'] = isset($_REQUEST['weight']) ? intval($_REQUEST['weight']) : 0;
$material['desc_info'] = isset($_REQUEST['desc_info']) ? trim($_REQUEST['desc_info']) : '';
$material['remark'] = isset($_REQUEST['remark']) ? trim($_REQUEST['remark']) : '';
$material['update_time']= gmtime();
$material['admin_id'] = $_SESSION['admin_id'];
if(empty($id))
{
sys_msg('ID不能为空', 1);
}
if(empty($material['name']) || empty($material['modulus']) || empty($material['safe_day']) || empty($material['intent_day']))
{
sys_msg('名称、系数、安全天数、目标天数,不能为空或者格式不正确', 1);
}
/*处理图片*/
if(!empty($_FILES['img']['name']))
{
$material['img'] = basename($image->upload_image($_FILES['img'],'material'));
}
/*处理URL*/
$material['buy_url'] = sanitize_url($_POST['buy_url']);
/*插入数据*/
$db->autoExecute($ecs->table('materials'), $material, 'UPDATE', "id = '$id'");
$link[0]['text'] = '继续编辑';
$link[0]['href'] = 'materials.php?act=edit&id='.$id;
$link[1]['text'] = '返回列表';
$link[1]['href'] = 'materials.php?act=list';
sys_msg('编辑成功', 0, $link);
}
elseif ($_REQUEST['act'] =='edit')
{
$sql = "SELECT * FROM " .$ecs->table('materials'). " WHERE id='$_REQUEST[id]'";
$material = $db->GetRow($sql);
$smarty->assign('ur_here', "编辑物料");
$smarty->assign('action_link', array('href' => 'materials.php?act=list', 'text' => '返回物料库存列表'));
$smarty->assign('material', $material);
$smarty->assign('form_action', 'updata');
assign_query_info();
$smarty->display('material_info.htm');
}
elseif ($_REQUEST['act'] == 'remove')
{
$id = intval($_GET['id']);
$exc->drop($id);
$url = 'materials.php?act=query&' . str_replace('act=remove', '', $_SERVER['QUERY_STRING']);
ecs_header("Location: $url\n");
exit;
}
elseif ($_REQUEST['act'] == 'drop_img')
{
$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
$sql = "SELECT img FROM " .$ecs->table('materials'). " WHERE id = '$id'";
$img_name = $db->getOne($sql);
if (!empty($img_name))
{
@unlink(ROOT_PATH . DATA_DIR . '/material/' .$img_name);
$sql = "UPDATE " .$ecs->table('materials'). " SET img = '' WHERE id = '$id'";
$db->query($sql);
}
$link= array(array('text' => '继续编辑', 'href' => 'materials.php?act=edit&id=' . $id), array('text' => '返回物料库存列表', 'href' => 'materials.php?act=list'));
sys_msg('图片删除成功', 0, $link);
}
elseif ($_REQUEST['act'] == 'edit_stock_in') //更改入库
{
$id = intval($_POST['id']);
$val = json_str_iconv(trim($_POST['val']));
/* 检查格式 */
if(!is_numeric($val) || $val < 0)
{
make_json_error(sprintf("格式不正确!", $val));
}
$exc->edit("stock_in='$val'", $id);
make_json_result(stripslashes($val));
}
elseif ($_REQUEST['act'] == 'edit_stock_out') //更改出库
{
$id = intval($_POST['id']);
$val = json_str_iconv(trim($_POST['val']));
/* 检查格式 */
if(!is_numeric($val) || $val < 0)
{
make_json_error(sprintf("格式不正确!", $val));
}
$sql="SELECT * FROM ".$GLOBALS['ecs']->table('materials')." where id = '".$id."'";
$material = $GLOBALS['db']->getRow($sql);
if($val > $material['stock_in'] + $material['stock_number'])
{
make_json_error(sprintf("出库数不能大于现有库存与入库总和!", $val));
}
$exc->edit("stock_out='$val'", $id);
make_json_result(stripslashes($val));
}
elseif ($_REQUEST['act'] == 'operate') //批量入库/出库
{
$sql = "UPDATE " .$ecs->table('materials'). " SET stock_number = stock_number + stock_in - stock_out,stock_out = 0,stock_in = 0,admin_id=$_SESSION[admin_id],update_time = ".gmtime();
$db->query($sql);
$link= array(array('text' => '返回物料库存列表', 'href' => 'materials.php?act=list'));
sys_msg('成功批量入库/出库', 0, $link);
}
elseif ($_REQUEST['act'] == 'export') //导出采购单
{
include_once('includes/PHPExcel/PHPExcel.php');
include_once('corlor.php');
$objPHPExcel = new PHPExcel();
$filename = '物料采购表_'.date("YmdHi",gmtime());
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setTitle($filename);
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(15);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(10);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(10);
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(10);
$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(10);
$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(10);
$objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(10);
$objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(10);
$objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth(10);
$objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
$objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
$objPHPExcel->getActiveSheet()->getStyle('F1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
$objPHPExcel->getActiveSheet()->getStyle('G1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
$objPHPExcel->getActiveSheet()->getStyle('H1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
$objPHPExcel->getActiveSheet()->getStyle('I1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
$objPHPExcel->getActiveSheet()->getStyle('J1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
$objPHPExcel->getActiveSheet()->getColumnDimension('K')->setWidth(10);
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', '物料名称')
->setCellValue('B1', '图片')
->setCellValue('C1', '每天用量')
->setCellValue('D1', '现有库存')
->setCellValue('E1', '周转天数')
->setCellValue('F1', '安全库存')
->setCellValue('G1', '目标库存')
->setCellValue('H1', '建议购买')
->setCellValue('I1', '单价')
->setCellValue('J1', '实际单价')
->setCellValue('K1', '采购链接');
$i=2;
$stock_list = material_list(false);
$arr = $stock_list['stock_list'];
foreach($arr as $v)
{
if($v['img'])
{
$objPHPExcel->getActiveSheet()->getRowDimension($i)->setRowHeight(50);
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setName('goods thumb');
$objDrawing->setDescription('Pgoods thumb');
$img_path = file_exists('../data/material/'.$v['img']) ? '../data/material/'.$v['img'] : '../images/no_img.jpg';
$objDrawing->setPath($img_path);
$objDrawing->setWidth(100);
$objDrawing->setCoordinates('B'.$i);
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
}
else
{
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('B'.$i, '');
}
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A'.$i, $v['name'])
->setCellValue('C'.$i, $v['day_use'])
->setCellValue('D'.$i, $v['stock_number'])
->setCellValue('E'.$i, $v['stock_day'])
->setCellValue('F'.$i, $v['stock_safe'])
->setCellValue('G'.$i, $v['stock_intent'])
->setCellValue('H'.$i, $v['proposal_buy'])
->setCellValue('I'.$i, $v['price'])
->setCellValue('J'.$i, '');
if($v['stock_safe'] >= $v['stock_number'])
{
$objPHPExcel->setActiveSheetIndex(0)->getStyle('D'.$i)->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_RED);
}
if($v['buy_url'] != 'http://')
{
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('K'.$i, '采购链接');
$objPHPExcel->setActiveSheetIndex(0)->getCell('K'.$i)->getHyperlink()->setUrl($v['buy_url']);
$objPHPExcel->setActiveSheetIndex(0)->getCell('K'.$i)->getHyperlink()->setTooltip('采购链接');
$objPHPExcel->setActiveSheetIndex(0)->getStyle('K'.$i)->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLUE);
$objPHPExcel->setActiveSheetIndex(0)->getStyle('K'.$i)->getFont()->setUnderline(PHPExcel_Style_Font::UNDERLINE_SINGLE);
}
else
{
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('K'.$i, '');
}
$objPHPExcel->getActiveSheet()->getStyle('A'.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('B'.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('C'.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('D'.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('E'.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('F'.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('G'.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('H'.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('I'.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('J'.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('K'.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$i++;
}
$file_name = $filename.'.xls';
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$file_name.'"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
}
elseif ($_REQUEST['act'] == 'query')
{
$stock_list = material_list();
$smarty->assign('stock_list', $stock_list['stock_list']);
$smarty->assign('filter', $stock_list['filter']);
$smarty->assign('record_count', $stock_list['record_count']);
$smarty->assign('page_count', $stock_list['page_count']);
make_json_result($smarty->fetch('material_list.htm'), '', array('filter' => $stock_list['filter'], 'page_count' => $stock_list['page_count']));
}
function material_list($is_pagination = true)
{
GLOBAL $ecs,$db;
$result = get_filter();
if ($result === false)
{
$filter['sort_by'] = empty($_REQUEST['sort_by']) ? 'id' : trim($_REQUEST['sort_by']);
$filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'desc' : trim($_REQUEST['sort_order']);
$where = " WHERE 1 = 1 ";
$sql = 'select count(t.id) from '.$ecs->table('materials'). ' as t '.$where;
$filter['record_count'] = $db->getOne($sql);
/* 分页大小 */
$filter = page_and_size($filter);
$sql = 'select t.*, au.user_name from '.
$ecs->table('materials').' as t left join '.
$ecs->table('admin_user')." as au on t.admin_id=au.user_id ".$where.
' order by '.$filter['sort_by']." ".$filter['sort_order'];
if ($is_pagination)
{
$sql .= " LIMIT " . $filter['start'] . ', ' . $filter['page_size'];
}
$end_time = strtotime(date("Y-m-d",gmtime()));
$start_time = $end_time - 7 * 86400;
$query = "SELECT count(order_id) as total FROM ".$GLOBALS['ecs']->table('order_info')." WHERE synch_time < '".$end_time."' and synch_time >= '".$start_time."'";
$filter['orders'] = round($GLOBALS['db']->getOne($query) / 7);//7天平均订单数
$filter['orders'] = $filter['orders'] ? $filter['orders'] : 1400;
set_filter($filter, $sql);
}
else
{
$sql = $result['sql'];
$filter = $result['filter'];
}
$row = $GLOBALS['db']->getAll($sql);
$orders = $filter['orders'];
foreach($row as $k=>$val)
{
if ($is_pagination == false && $val['is_buy'] == 0) //不购买,不导出
{
unset($row[$k]);
continue;
}
$row[$k]['update_time'] = local_date('Y-m-d H:i',$val['update_time']);
$row[$k]['day_use'] = $day_use = round($orders * $val['modulus'],1);//每日用量
$row[$k]['stock_day'] = $day_use ? round($val['stock_number'] / $day_use,1) : 0;//周转天数
$row[$k]['stock_safe'] = round($val['safe_day'] * $day_use,1);//安全库存
$row[$k]['stock_intent']= $stock_intent = round($val['intent_day'] * $day_use,1);//目标库存
$row[$k]['proposal_buy']= round($stock_intent - $val['stock_number'],1);//建议购买
}
$stock_list = array('stock_list' => $row, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);
return $stock_list;
}
?>
3、列表页模板文件,material_list.htm:
<?php
define('IN_ECS', true);
require(dirname(__FILE__) . '/includes/init.php');
include_once(ROOT_PATH . 'includes/cls_image.php');
$image = new cls_image($_CFG['bgcolor']);
$exc = new exchange($ecs->table("materials"), $db, 'id', 'name');
$_REQUEST['act']=!empty($_REQUEST['act']) ? $_REQUEST['act']:'list';
admin_priv('stock_alert');//权限:库存数量修改
if($_REQUEST['act'] == 'list')
{
$stock_list=material_list();
$smarty->assign('ur_here', '物料库存列表');
$smarty->assign('stock_list', $stock_list['stock_list']);
$smarty->assign('filter', $stock_list['filter']);
$smarty->assign('record_count', $stock_list['record_count']);
$smarty->assign('page_count', $stock_list['page_count']);
$smarty->assign('shelf_list', $shelf_list);
$smarty->assign('full_page', 1);
$smarty->assign('action_link', array('href' => 'goods_stock.php?act=list', 'text' => '商品库存列表'));
$smarty->assign('action_link2', array('href' => 'materials.php?act=export', 'text' => '导出采购单'));
$smarty->assign('action_link3', array('href' => 'materials.php?act=add', 'text' => '添加物料'));
$smarty->display('material_list.htm');
}
elseif($_REQUEST['act'] == 'add')
{
$smarty->assign('ur_here', "添加物料");
$smarty->assign('action_link', array('href' => 'materials.php?act=list', 'text' => '返回物料库存列表'));
$smarty->assign('form_action', "insert");
assign_query_info();
$smarty->display('material_info.htm');
}
elseif($_REQUEST['act'] == 'insert')
{
$material['is_buy'] = isset($_REQUEST['is_buy']) ? intval($_REQUEST['is_buy']) : 1;
$material['name'] = isset($_REQUEST['name']) ? trim($_REQUEST['name']) : '';
$material['modulus'] = isset($_REQUEST['modulus']) ? trim($_REQUEST['modulus']) : '';
$material['safe_day'] = isset($_REQUEST['safe_day']) ? intval($_REQUEST['safe_day']) : 0;
$material['intent_day']= isset($_REQUEST['intent_day']) ? intval($_REQUEST['intent_day']) : 0;
$material['price'] = isset($_REQUEST['price']) ? floatval($_REQUEST['price']) : '0.00';
$material['weight'] = isset($_REQUEST['weight']) ? intval($_REQUEST['weight']) : 0;
$material['desc_info'] = isset($_REQUEST['desc_info']) ? trim($_REQUEST['desc_info']) : '';
$material['remark'] = isset($_REQUEST['remark']) ? trim($_REQUEST['remark']) : '';
$material['update_time']= gmtime();
$material['admin_id'] = $_SESSION['admin_id'];
if(empty($material['name']) || empty($material['modulus']) || empty($material['safe_day']) || empty($material['intent_day']))
{
sys_msg('名称、系数、安全天数、目标天数,不能为空或者格式不正确', 1);
}
$is_only = $exc->is_only('name', $material['name']);
if (!$is_only)
{
sys_msg($material['name'].',已存在', 1);
}
/*处理图片*/
$material['img'] = basename($image->upload_image($_FILES['img'],'material'));
/*处理URL*/
$material['buy_url'] = sanitize_url($_POST['buy_url']);
/*插入数据*/
$db->autoExecute($ecs->table('materials'), $material, 'INSERT', '', 'SILENT');
$link[0]['text'] = '继续添加';
$link[0]['href'] = 'materials.php?act=add';
$link[1]['text'] = '返回列表';
$link[1]['href'] = 'materials.php?act=list';
sys_msg('添加成功', 0, $link);
}
elseif($_REQUEST['act'] == 'updata')
{
$id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : 0;
$material['is_buy'] = isset($_REQUEST['is_buy']) ? intval($_REQUEST['is_buy']) : 1;
$material['name'] = isset($_REQUEST['name']) ? trim($_REQUEST['name']) : '';
$material['modulus'] = isset($_REQUEST['modulus']) ? trim($_REQUEST['modulus']) : '';
$material['safe_day'] = isset($_REQUEST['safe_day']) ? intval($_REQUEST['safe_day']) : 0;
$material['intent_day']= isset($_REQUEST['intent_day']) ? intval($_REQUEST['intent_day']) : 0;
$material['price'] = isset($_REQUEST['price']) ? floatval($_REQUEST['price']) : '0.00';
$material['weight'] = isset($_REQUEST['weight']) ? intval($_REQUEST['weight']) : 0;
$material['desc_info'] = isset($_REQUEST['desc_info']) ? trim($_REQUEST['desc_info']) : '';
$material['remark'] = isset($_REQUEST['remark']) ? trim($_REQUEST['remark']) : '';
$material['update_time']= gmtime();
$material['admin_id'] = $_SESSION['admin_id'];
if(empty($id))
{
sys_msg('ID不能为空', 1);
}
if(empty($material['name']) || empty($material['modulus']) || empty($material['safe_day']) || empty($material['intent_day']))
{
sys_msg('名称、系数、安全天数、目标天数,不能为空或者格式不正确', 1);
}
/*处理图片*/
if(!empty($_FILES['img']['name']))
{
$material['img'] = basename($image->upload_image($_FILES['img'],'material'));
}
/*处理URL*/
$material['buy_url'] = sanitize_url($_POST['buy_url']);
/*插入数据*/
$db->autoExecute($ecs->table('materials'), $material, 'UPDATE', "id = '$id'");
$link[0]['text'] = '继续编辑';
$link[0]['href'] = 'materials.php?act=edit&id='.$id;
$link[1]['text'] = '返回列表';
$link[1]['href'] = 'materials.php?act=list';
sys_msg('编辑成功', 0, $link);
}
elseif ($_REQUEST['act'] =='edit')
{
$sql = "SELECT * FROM " .$ecs->table('materials'). " WHERE id='$_REQUEST[id]'";
$material = $db->GetRow($sql);
$smarty->assign('ur_here', "编辑物料");
$smarty->assign('action_link', array('href' => 'materials.php?act=list', 'text' => '返回物料库存列表'));
$smarty->assign('material', $material);
$smarty->assign('form_action', 'updata');
assign_query_info();
$smarty->display('material_info.htm');
}
elseif ($_REQUEST['act'] == 'remove')
{
$id = intval($_GET['id']);
$exc->drop($id);
$url = 'materials.php?act=query&' . str_replace('act=remove', '', $_SERVER['QUERY_STRING']);
ecs_header("Location: $url\n");
exit;
}
elseif ($_REQUEST['act'] == 'drop_img')
{
$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
$sql = "SELECT img FROM " .$ecs->table('materials'). " WHERE id = '$id'";
$img_name = $db->getOne($sql);
if (!empty($img_name))
{
@unlink(ROOT_PATH . DATA_DIR . '/material/' .$img_name);
$sql = "UPDATE " .$ecs->table('materials'). " SET img = '' WHERE id = '$id'";
$db->query($sql);
}
$link= array(array('text' => '继续编辑', 'href' => 'materials.php?act=edit&id=' . $id), array('text' => '返回物料库存列表', 'href' => 'materials.php?act=list'));
sys_msg('图片删除成功', 0, $link);
}
elseif ($_REQUEST['act'] == 'edit_stock_in') //更改入库
{
$id = intval($_POST['id']);
$val = json_str_iconv(trim($_POST['val']));
/* 检查格式 */
if(!is_numeric($val) || $val < 0)
{
make_json_error(sprintf("格式不正确!", $val));
}
$exc->edit("stock_in='$val'", $id);
make_json_result(stripslashes($val));
}
elseif ($_REQUEST['act'] == 'edit_stock_out') //更改出库
{
$id = intval($_POST['id']);
$val = json_str_iconv(trim($_POST['val']));
/* 检查格式 */
if(!is_numeric($val) || $val < 0)
{
make_json_error(sprintf("格式不正确!", $val));
}
$sql="SELECT * FROM ".$GLOBALS['ecs']->table('materials')." where id = '".$id."'";
$material = $GLOBALS['db']->getRow($sql);
if($val > $material['stock_in'] + $material['stock_number'])
{
make_json_error(sprintf("出库数不能大于现有库存与入库总和!", $val));
}
$exc->edit("stock_out='$val'", $id);
make_json_result(stripslashes($val));
}
elseif ($_REQUEST['act'] == 'operate') //批量入库/出库
{
$sql = "UPDATE " .$ecs->table('materials'). " SET stock_number = stock_number + stock_in - stock_out,stock_out = 0,stock_in = 0,admin_id=$_SESSION[admin_id],update_time = ".gmtime();
$db->query($sql);
$link= array(array('text' => '返回物料库存列表', 'href' => 'materials.php?act=list'));
sys_msg('成功批量入库/出库', 0, $link);
}
elseif ($_REQUEST['act'] == 'export') //导出采购单
{
include_once('includes/PHPExcel/PHPExcel.php');
include_once('corlor.php');
$objPHPExcel = new PHPExcel();
$filename = '物料采购表_'.date("YmdHi",gmtime());
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setTitle($filename);
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(15);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(10);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(10);
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(10);
$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(10);
$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(10);
$objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(10);
$objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(10);
$objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth(10);
$objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
$objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
$objPHPExcel->getActiveSheet()->getStyle('F1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
$objPHPExcel->getActiveSheet()->getStyle('G1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
$objPHPExcel->getActiveSheet()->getStyle('H1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
$objPHPExcel->getActiveSheet()->getStyle('I1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
$objPHPExcel->getActiveSheet()->getStyle('J1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
$objPHPExcel->getActiveSheet()->getColumnDimension('K')->setWidth(10);
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', '物料名称')
->setCellValue('B1', '图片')
->setCellValue('C1', '每天用量')
->setCellValue('D1', '现有库存')
->setCellValue('E1', '周转天数')
->setCellValue('F1', '安全库存')
->setCellValue('G1', '目标库存')
->setCellValue('H1', '建议购买')
->setCellValue('I1', '单价')
->setCellValue('J1', '实际单价')
->setCellValue('K1', '采购链接');
$i=2;
$stock_list = material_list(false);
$arr = $stock_list['stock_list'];
foreach($arr as $v)
{
if($v['img'])
{
$objPHPExcel->getActiveSheet()->getRowDimension($i)->setRowHeight(50);
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setName('goods thumb');
$objDrawing->setDescription('Pgoods thumb');
$img_path = file_exists('../data/material/'.$v['img']) ? '../data/material/'.$v['img'] : '../images/no_img.jpg';
$objDrawing->setPath($img_path);
$objDrawing->setWidth(100);
$objDrawing->setCoordinates('B'.$i);
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
}
else
{
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('B'.$i, '');
}
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A'.$i, $v['name'])
->setCellValue('C'.$i, $v['day_use'])
->setCellValue('D'.$i, $v['stock_number'])
->setCellValue('E'.$i, $v['stock_day'])
->setCellValue('F'.$i, $v['stock_safe'])
->setCellValue('G'.$i, $v['stock_intent'])
->setCellValue('H'.$i, $v['proposal_buy'])
->setCellValue('I'.$i, $v['price'])
->setCellValue('J'.$i, '');
if($v['stock_safe'] >= $v['stock_number'])
{
$objPHPExcel->setActiveSheetIndex(0)->getStyle('D'.$i)->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_RED);
}
if($v['buy_url'] != 'http://')
{
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('K'.$i, '采购链接');
$objPHPExcel->setActiveSheetIndex(0)->getCell('K'.$i)->getHyperlink()->setUrl($v['buy_url']);
$objPHPExcel->setActiveSheetIndex(0)->getCell('K'.$i)->getHyperlink()->setTooltip('采购链接');
$objPHPExcel->setActiveSheetIndex(0)->getStyle('K'.$i)->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_BLUE);
$objPHPExcel->setActiveSheetIndex(0)->getStyle('K'.$i)->getFont()->setUnderline(PHPExcel_Style_Font::UNDERLINE_SINGLE);
}
else
{
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('K'.$i, '');
}
$objPHPExcel->getActiveSheet()->getStyle('A'.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('B'.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('C'.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('D'.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('E'.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('F'.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('G'.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('H'.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('I'.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('J'.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('K'.$i)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
$i++;
}
$file_name = $filename.'.xls';
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$file_name.'"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
}
elseif ($_REQUEST['act'] == 'query')
{
$stock_list = material_list();
$smarty->assign('stock_list', $stock_list['stock_list']);
$smarty->assign('filter', $stock_list['filter']);
$smarty->assign('record_count', $stock_list['record_count']);
$smarty->assign('page_count', $stock_list['page_count']);
make_json_result($smarty->fetch('material_list.htm'), '', array('filter' => $stock_list['filter'], 'page_count' => $stock_list['page_count']));
}
function material_list($is_pagination = true)
{
GLOBAL $ecs,$db;
$result = get_filter();
if ($result === false)
{
$filter['sort_by'] = empty($_REQUEST['sort_by']) ? 'id' : trim($_REQUEST['sort_by']);
$filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'desc' : trim($_REQUEST['sort_order']);
$where = " WHERE 1 = 1 ";
$sql = 'select count(t.id) from '.$ecs->table('materials'). ' as t '.$where;
$filter['record_count'] = $db->getOne($sql);
/* 分页大小 */
$filter = page_and_size($filter);
$sql = 'select t.*, au.user_name from '.
$ecs->table('materials').' as t left join '.
$ecs->table('admin_user')." as au on t.admin_id=au.user_id ".$where.
' order by '.$filter['sort_by']." ".$filter['sort_order'];
if ($is_pagination)
{
$sql .= " LIMIT " . $filter['start'] . ', ' . $filter['page_size'];
}
$end_time = strtotime(date("Y-m-d",gmtime()));
$start_time = $end_time - 7 * 86400;
$query = "SELECT count(order_id) as total FROM ".$GLOBALS['ecs']->table('order_info')." WHERE synch_time < '".$end_time."' and synch_time >= '".$start_time."'";
$filter['orders'] = round($GLOBALS['db']->getOne($query) / 7);//7天平均订单数
$filter['orders'] = $filter['orders'] ? $filter['orders'] : 1400;
set_filter($filter, $sql);
}
else
{
$sql = $result['sql'];
$filter = $result['filter'];
}
$row = $GLOBALS['db']->getAll($sql);
$orders = $filter['orders'];
foreach($row as $k=>$val)
{
if ($is_pagination == false && $val['is_buy'] == 0) //不购买,不导出
{
unset($row[$k]);
continue;
}
$row[$k]['update_time'] = local_date('Y-m-d H:i',$val['update_time']);
$row[$k]['day_use'] = $day_use = round($orders * $val['modulus'],1);//每日用量
$row[$k]['stock_day'] = $day_use ? round($val['stock_number'] / $day_use,1) : 0;//周转天数
$row[$k]['stock_safe'] = round($val['safe_day'] * $day_use,1);//安全库存
$row[$k]['stock_intent']= $stock_intent = round($val['intent_day'] * $day_use,1);//目标库存
$row[$k]['proposal_buy']= round($stock_intent - $val['stock_number'],1);//建议购买
}
$stock_list = array('stock_list' => $row, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);
return $stock_list;
}
?>
4、添加物料模板页,material_info.htm:
{include file="pageheader.htm"}
{insert_scripts files="../js/utils.js,listtable.js,validator.js"}
<script type="text/javascript" src="../js/calendar.php?lang={$cfg_lang}"></script>
<link href="../js/calendar/calendar.css" rel="stylesheet" type="text/css" />
<div class="main-div">
<form method="post" action="materials.php" name="theForm" enctype="multipart/form-data" onsubmit="return check()">
<table cellspacing="1" cellpadding="3" width="100%">
<tr>
<td class="label">物料名称:</td>
<td><input type="text" name="name" value="{$material.name}" size="40" />{$lang.require_field}</td>
</tr>
<tr>
<td class="label">系数:</td>
<td><input type="text" name="modulus" value="{$material.modulus}" size="20" />{$lang.require_field}</td>
</tr>
<tr>
<td class="label">单价:</td>
<td><input type="text" name="price" value="{$material.price}" size="20" /></td>
</tr>
<tr>
<td class="label">安全库存天数:</td>
<td><input type="text" name="safe_day" value="{$material.safe_day}" size="20" />{$lang.require_field}</td>
</tr>
<tr>
<td class="label">目标库存天数:</td>
<td><input type="text" name="intent_day" value="{$material.intent_day}" size="20" />{$lang.require_field}</td>
</tr>
<tr>
<td class="label">是否参与购买:</td>
<td><select name="is_buy" id="is_buy">
<option value="1" {if $material.is_buy == 1}selected="selected"{/if}>是</option>
<option value="0" {if $material.is_buy == 0}selected="selected"{/if}>否</option>
</select><br /><span class="notice-span">否表示不参与导出采购单</span></td>
</tr>
<tr>
<td class="label">购买链接:</td>
<td><textarea name="buy_url" rows="4" cols="80">{$material.buy_url}</textarea></td>
</tr>
<tr>
<td class="label">图片:</td>
<td><input type="file" name="img" id="img" size="45">
{if $material.img neq ""}<input type="button" value="删除图片" onclick="if (confirm('确定删除图片?'))location.href='materials.php?act=drop_img&id={$material.id}'">
<br /><span class="notice-span">你已经上传过图片。再次上传时将覆盖原图片!</span>
<br /><img src="../data/material/{$material.img}" height="50" />{/if}</td>
</tr>
<tr>
<td class="label">重量:</td>
<td><input type="text" name="weight" value="{$material.weight}" size="20" /><span class="notice-span">单位:克</span></td>
</tr>
<tr>
<td class="label">物料描述:</td>
<td><textarea name="desc_info" rows="8" cols="80">{$material.desc_info}</textarea></td>
</tr>
<tr>
<td class="label">备注:</td>
<td><input type="text" name="remark" value="{$material.remark}" size="40" /></td>
</tr>
<tr>
<td class="label"></td>
<td>
<input type="submit" value="{$lang.button_submit}" class="button" />
<input type="reset" value="{$lang.button_reset}" class="button" />
<input type="hidden" name="act" value="{$form_action}" />
<input type="hidden" name="id" value="{$material.id}" />
</td>
</tr>
</table>
</form>
</div>
<script type="text/javascript" language="javascript">
document.forms['theForm'].elements['name'].focus();
function check()
{
validator = new Validator("theForm");
validator.required("name", '物料名称 不能为空');
validator.isNumber("modulus", '系数 不能为空或者格式不正确', true);
validator.isNumber("safe_day", '安全库存天数 不能为空或者格式不正确', true);
validator.isNumber("intent_day", '目标库存天数 不能为空或者格式不正确', true);
validator.isNumber("price", '价格 格式不正确', false);
validator.isNumber("weight", '重量 格式不正确', false);
return validator.passed();
}
</script>
{include file="pagefooter.htm"}
5、导出采购表,截图如下:
ecshop物料库存管理的更多相关文章
- 「SAP技术」为正常库存管理的物料做成本中心采购会是什么结果?
SAP 为正常库存管理的物料做成本中心采购会是什么结果? 这种采购可以正常进行,收货后SAP会更新采购订单里指定的费用类科目,而库存数量和库存价值都不会增加. 1, 如下物料号,是正常做库存管理的物料 ...
- SAP物料批次管理配置及操作手册(轉載)
这个有待学习 业务说明需要先熟悉基本的物料管理(MM).生产计划管理(PP).销售与分销管理(SD)的相关知识.在化工.制药.快消.汽车零部件等行业,为了进行质量的跟踪,往往需要使用批次管理来监控质量 ...
- 【MM系列】SAP的库存管理
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP的库存管理 前言部分 库存 ...
- Java生鲜电商平台-库存管理设计与架构
Java生鲜电商平台-库存管理设计与架构 WMS的功能: 1.业务批次管理 该功能提供完善的物料批次信息.批次管理设置.批号编码规则设置.日常业务处理.报表查询,以及库存管理等综合批次管理功能,使企业 ...
- Oracle ERP 库存管理(业务流程 核心流程)
库存核心业务 库存管理的核心是对货物本身的管理,是对货物的数量与相关属性的管理,目的是为销售与采购服务,确保合理的库存保有量,处理库存分类帐目与进出流水帐,以单据的形式基本涵盖仓库的各种进出库业务. ...
- 【MM系列】SAP 的库存管理
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP 的库存管理 前言部分 大 ...
- 疯狂C#~伴随着我的库存管理¥
每次的等待都是期待下一次的勃发!但激进的我非常想和大家学习一些东西,所以特地的分享了一个库存管理, 生活中容易运用的很多,但现在的学业希望能够得到各界人士的帮助!!! 首先:会有几个类来让它们协调 ( ...
- C#的库存管理之旅的别样意义
我不知道大家对C#的一些基础知识掌握得怎么样了? 但无论怎么样,都应该静心下来去慢慢品味我的总结以及“库存管理”项目需用到的一些知识和技巧.你将会得到你料想不到的收获哦. 知识梳理: 数据类型:boo ...
- Odoo 的库存管理与OpenERP之前的版本有了很大的不同,解读Odoo新的WMS模块中的新特性
原文来自:http://shine-it.net/index.php/topic,16409.0.html 库存移动(Stock Move)新玩法Odoo的库存移动不仅仅是存货在两个“存货地点”之间的 ...
随机推荐
- php的一些小笔记--数学函数
通常我们使用的数学函数不多,经常出现的有 floor 地板->舍去 ceil 天花板->进一 round 四舍五入 rand 随机数 mt_rand 产生更好的随机数 pow 指数表达 ...
- JVM启动参数手册——JVM之八
jdk1.4.2 JVM官方地址:http://java.sun.com/j2se/1.4.2/docs/guide/vm/index.html 标准和非标注参数(for windows):http: ...
- Delphi之通过代码示例学习XML解析、StringReplace的用法(异常控制 good)
*Delphi之通过代码示例学习XML解析.StringReplace的用法 这个程序可以用于解析任何合法的XML字符串. 首先是看一下程序的运行效果: 以解析这样一个XML的字符串为例: <? ...
- 学习 ExtJS 4 面板与布局
原文 http://www.cnblogs.com/codealone/archive/2013/06/04/3091325.html 面板Panel Ext.panel.Panel拓展自Ext.co ...
- 【转】飞凌嵌入式(Forlinx)TE/OK6410内核编译:“make: arm-none-linux-gnueabi-gcc:命令未找到”
原文网址:http://www.xuebuyuan.com/1104711.html Ubuntu10.04下编译飞凌嵌入式(Forlinx)TE/OK6410开发板提供的内核2.6.36 本以为按照 ...
- UESTC_秋实大哥搞算数 2015 UESTC Training for Data Structures<Problem N>
N - 秋实大哥搞算数 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Subm ...
- 【剑指offer】面试题22:栈的压入、弹出序列
题目: 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序.假设压入栈的所有数字均不相等.例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列 ...
- 为什么和其他语言相比C语言是快速的语言
初入门的我们经常听见别人说"真正的程序员用C语言编程,C是最快的语言因为它是最靠近及其底层的语言."那么和其他语言相比C语言到底有什么特别的呢? C语言没有什么特别,这就是它快速的 ...
- python刷取CSDN博文访问量之一
python刷取CSDN博文访问量之一 作者:vpoet 注:这个系列我只贴代码,代码不注释.有兴趣的自己读读就懂了,纯属娱乐,望管理员抬手 若有转载一定不要注明来源 #coding=utf-8 ...
- zoj3433(贪心+优先队列)
Gu Jian Qi Tan Time Limit: 2 Seconds Memory Limit: 65536 KB Gu Jian Qi Tan is a very hot Chines ...