cost import
This commit is contained in:
@@ -357,6 +357,93 @@ class Cost extends HD_Controller{
|
||||
|
||||
}
|
||||
|
||||
public function add_excel()
|
||||
{
|
||||
require_once COMMPATH . '/third_party/PHPExcel/IOFactory.php';
|
||||
$res = $this->upload();
|
||||
if (!$res['code']) {
|
||||
return $this->show_json(0, $res['message']);
|
||||
}
|
||||
$file = $res['path'];
|
||||
if ($res['file_ext'] == '.xls') {
|
||||
$reader = \PHPExcel_IOFactory::createReader('Excel5'); // 读取 excel 文档
|
||||
} elseif ($res['file_ext'] == '.xlsx') {
|
||||
$reader = \PHPExcel_IOFactory::createReader('Excel2007'); // 读取 excel 文档
|
||||
}
|
||||
$PHPExcel = $reader->load($file); // 文档名称
|
||||
$objWorksheet = $PHPExcel->getActiveSheet();
|
||||
$rowCnt = $objWorksheet->getHighestRow(); //获取总行数
|
||||
if ($rowCnt > 10000) {
|
||||
@unlink($file);
|
||||
return $this->show_json(0, '数据大于一万请拆分多个表格导入');
|
||||
}
|
||||
$s = $f = $ignore_vin = $ignore_item = $ignore_cost = 0;
|
||||
for ($_row = 2; $_row <= $rowCnt; $_row++) { //读取内容
|
||||
$vin = $objWorksheet->getCell('A' . $_row)->getValue(); //vin码
|
||||
if (!$vin){
|
||||
$ignore_vin++;
|
||||
continue;
|
||||
}
|
||||
$item = $this->items_model->get(array('vin' => $vin));
|
||||
if (!$item){
|
||||
$ignore_item++;
|
||||
continue;
|
||||
}
|
||||
$cost = $this->items_cost_model->get(array('item_id' => $item['id']));
|
||||
if (!$cost){
|
||||
$ignore_cost++;
|
||||
continue;
|
||||
}
|
||||
|
||||
$id = $cost['id'];
|
||||
$promotion_json = json_decode($cost['promotion_json'],true);
|
||||
$cost_json = json_decode($cost['cost_json'],true);
|
||||
$srv_json = json_decode($cost['srv_json'],true);
|
||||
# 读取需要更新的列,生成需更新的数据
|
||||
# 参考 edit* 更新
|
||||
$buy_price = floatval($objWorksheet->getCell('B' . $_row)->getValue()); //采购成本
|
||||
$select_price = floatval($objWorksheet->getCell('C' . $_row)->getValue()); //选装成本
|
||||
$labor_price = floatval($objWorksheet->getCell('D' . $_row)->getValue()); //选装工时费
|
||||
$sale_price = floatval($objWorksheet->getCell('E' . $_row)->getValue()); //销售佣金
|
||||
$other = floatval($objWorksheet->getCell('F' . $_row)->getValue()); //其他促销成本
|
||||
$factory_price = floatval($objWorksheet->getCell('G' . $_row)->getValue()); //厂家补贴
|
||||
$other_in_price = floatval($objWorksheet->getCell('H' . $_row)->getValue()); //其他收入
|
||||
$other_out_price = floatval($objWorksheet->getCell('I' . $_row)->getValue()); //其他支出
|
||||
$res_item = 0;
|
||||
if ($item['buy_price'] != $buy_price){
|
||||
$res_item = $this->items_model->update(array('buy_price'=>$buy_price),['id'=>$item['id']]);
|
||||
}
|
||||
$cost_json['select_price'] = $select_price ? $select_price : 0;
|
||||
$cost_json['labor_price'] = $labor_price ? $labor_price : 0;
|
||||
|
||||
$promotion_json['sale_price'] = $sale_price ? $sale_price : 0;
|
||||
$promotion_json['other'] = $other ? $other : 0;
|
||||
$promotion_json['factory_price'] = $factory_price ? $factory_price : 0;
|
||||
|
||||
$srv_json['other_in_price'] = $other_in_price ? $other_in_price : 0;
|
||||
$srv_json['other_out_price'] = $other_out_price ? $other_out_price : 0;
|
||||
|
||||
$data = [
|
||||
'cost_json' => json_encode($cost_json,JSON_UNESCAPED_UNICODE),
|
||||
'promotion_json' => json_encode($promotion_json,JSON_UNESCAPED_UNICODE),
|
||||
'srv_json' => json_encode($srv_json,JSON_UNESCAPED_UNICODE),
|
||||
];
|
||||
$res = $this->items_cost_model->update($data,['id'=>$cost['id']]);
|
||||
if(is_numeric($res_item) || is_numeric($res)){
|
||||
$this->items_cost_model->update_total($cost['id']);
|
||||
}else{
|
||||
$f++;
|
||||
}
|
||||
$s++;
|
||||
}
|
||||
@unlink($file);
|
||||
$ignore = $ignore_vin + $ignore_item + $ignore_cost;
|
||||
$msg = "导入完成,导入总条数为:" . $s;
|
||||
$f && $msg = $msg . '; 失败条数:'. $f;
|
||||
$ignore && $msg = $msg . '; 忽略条数:'. $ignore;
|
||||
return $this->show_json(SYS_CODE_SUCCESS, $msg);
|
||||
}
|
||||
|
||||
public function export(){
|
||||
$params = $this->input->get();
|
||||
$params['page'] = 1;
|
||||
@@ -697,4 +784,25 @@ class Cost extends HD_Controller{
|
||||
$this->load->library('excel');
|
||||
$this->excel->out_csv($data, $indexs, $fileName . "_" . date('YmdHis'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Notes:上传excel
|
||||
* Created on: 2021/7/14 15:06
|
||||
* Created by: dengbw
|
||||
* @return array
|
||||
*/
|
||||
private function upload()
|
||||
{
|
||||
$config['upload_path'] = $_SERVER['DOCUMENT_ROOT'] . '/temp/';
|
||||
$config['allowed_types'] = '*';//xls|xlsx
|
||||
$config['max_size'] = 5 * 1024;
|
||||
$config['file_name'] = 'items_' . time() . rand(1, 99999);
|
||||
$this->load->library('upload', $config);
|
||||
if (!$this->upload->do_upload('file')) {
|
||||
return array('code' => SYS_CODE_FAIL, 'message' => $this->upload->display_errors('', ''));
|
||||
} else {
|
||||
$data = $this->upload->data();
|
||||
return array('code' => SYS_CODE_SUCCESS, 'path' => $data['full_path'], 'file_ext' => $data['file_ext']);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -115,6 +115,9 @@
|
||||
<div class="am-form-group fl ml10">
|
||||
<button type="button" id="export" class="am-btn am-btn-success am-btn-sm w100">导出</button>
|
||||
</div>
|
||||
<div class="am-form-group fl ml10">
|
||||
<button type="button" id="import" class="am-btn am-btn-success am-btn-sm w100">导入</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
@@ -175,6 +178,24 @@
|
||||
<?php page_view($pager) ?>
|
||||
</div>
|
||||
</div>
|
||||
<div id="cost-modal" style="display: none;padding: 10px">
|
||||
<div class="modal-body">
|
||||
<div class="text-center">
|
||||
<h2><i class="fa fa-info-circle"></i> 请上传Excel文件</h2>
|
||||
<p>Excel文件格式必须与模板保持一致,否则无法导入</p>
|
||||
<div class="form-group-action">
|
||||
<a href="/temp/cost.xlsx" target="_blank" class="btn btn-default">查看模板</a>
|
||||
<label href="javascript:" class="btn btn-primary" style="margin-left: 10px;">
|
||||
<form id="import-form">
|
||||
<input type="file" id="cost-file" name="file" accept=".xls,.xlsx"
|
||||
style="left:-9999px;position:absolute;" onchange="load_cost()">
|
||||
</form>
|
||||
<span>上传Excel文件</span>
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
@@ -200,6 +221,42 @@
|
||||
}
|
||||
},'json')
|
||||
}
|
||||
|
||||
function load_cost() {
|
||||
var file = $("#cost-file");
|
||||
if (file.val() == '') {
|
||||
layer.msg('文件是空的');
|
||||
return 0;
|
||||
}
|
||||
var loading = layer.load(1, {
|
||||
shade: [0.1, '#fff'] //0.1透明度的白色背景
|
||||
});
|
||||
|
||||
var options = {
|
||||
url: "/items/cost/add_excel",
|
||||
type: "post",
|
||||
dataType: "json",
|
||||
data: {},
|
||||
success: function (res) {
|
||||
if (res['code']) {
|
||||
layer.msg(res.msg, {
|
||||
icon: 1,
|
||||
time: 2000
|
||||
}, function () {
|
||||
location.reload();
|
||||
});
|
||||
} else {
|
||||
layer.msg(res.msg, {icon: 2});
|
||||
}
|
||||
},
|
||||
complete: function () {
|
||||
file.val('');
|
||||
layer.closeAll('loading');
|
||||
}
|
||||
};
|
||||
$("#import-form").ajaxSubmit(options);
|
||||
}
|
||||
|
||||
</script>
|
||||
|
||||
<script>
|
||||
@@ -425,6 +482,22 @@
|
||||
window.location.href = href;
|
||||
});
|
||||
|
||||
$("#import").click(function () {
|
||||
$("#cost-file").val('');
|
||||
layer.open({
|
||||
type: 1,
|
||||
area: ['40%'], //宽高
|
||||
content: $('#cost-modal'),
|
||||
title: '导入整车成本',
|
||||
shade: false,
|
||||
btn: ['完成'],
|
||||
yes: function (index) {
|
||||
layer.close(index);
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
$('#bd-auto1-id').change(function () {
|
||||
$('#bd-auto2-id').empty();
|
||||
$('#bd-auto3-id').empty();
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user