<?php
namespace app\index\controller;
use think\Hook;
use app\index\controller\Acl;
use app\index\model\Exchangeclass;
use app\index\model\Exchangeinfo;
use app\index\model\Room;
use app\index\model\Roominfo;
use app\index\model\Serial;
use app\index\model\Serialinfo;
use app\index\model\Customer;
use app\index\model\Integral;
class Exchange extends Acl {
public function main(){
return $this->fetch();
}
public function set(){
$input=input('post.');
if(isset($input['id'])){
if(isset_full($input,'tab')){
foreach ($input['tab'] as $tab_key=>$tab_vo) {
$tab_vali = $this->validate($tab_vo,'Exchangeinfo') if($tab_vali!==true){
return json(['state'=>'error','info'=>'[ 数据表格 ]第'.($tab_key+1).'行'.$tab_vali]);
exit;
}
}
}else{
return json(['state'=>'error','info'=>'数据表格不可为空!']);
exit;
}
if(empty($input['id'])){
$input['merchant']=Session('is_merchant_id') $vali = $this->validate($input,'Exchangeclass');
if($vali===true){
$create_info=Exchangeclass::create(syn_sql($input,'exchangeclass'));
Hook::listen('create_exchange',$create_info) push_log('新增积分兑换单[ '.$create_info['number'].' ]') $resule=['state'=>'success'];
}else{
$resule=['state'=>'error','info'=>$vali];
}
}else{
$vali = $this->validate($input,'Exchangeclass.update');
if($vali===true){
$update_info=Exchangeclass::update(syn_sql($input,'exchangeclass'));
Hook::listen('update_exchange',$update_info) push_log('更新积分兑换单[ '.$update_info['number'].' ]') Exchangeinfo::where(['pid'=>$update_info['id']])->delete();
$resule=['state'=>'success'];
}else{
$resule=['state'=>'error','info'=>$vali];
}
}
if($resule['state']=='success'){
$info_pid=empty($input['id'])?$create_info['id']:$update_info['id'];
foreach ($input['tab'] as $info_vo) {
$info_vo['pid']=$info_pid;
(isset_full($info_vo,'serial')&& $info_vo['serial']=='&nbsp;')&&($info_vo['serial']='') Exchangeinfo::create(syn_sql($info_vo,'exchangeinfo'));
}
}
}else{
$resule=['state'=>'error','info'=>'传入参数不完整!'];
}
if($resule['state']=='success'&&empty($input['id'])){
empty(get_sys(['auto_auditing']))||($this->auditing([$create_info['id']],true));
}
return json($resule);
}
public function form(){
return $this->fetch();
}
public function form_list(){
$input=input('post.');
if(isset_full($input,'page') && isset_full($input,'limit')){
$sql=get_sql($input,[
'name'=>'continue',
'number'=>'full_like',
'customer'=>'full_division_in',
'start_time'=>'stime',
'end_time'=>'etime',
'type'=>'full_dec_1',
'warehouse'=>'continue',
'user'=>'full_division_in',
'data'=>'full_like',
],'exchangeclass') if(isset_full($input,'name')){
$goods=get_db_field('goods',['name|py'=>['like','%'.$input['name'].'%']],'id') if(empty($goods)){
sql_assign($sql,'id',[]) }else{
$info=get_db_field('exchangeinfo',['goods'=>['in',$goods]],'pid') sql_assign($sql,'id',$info) }
}
if(isset_full($input,'warehouse')){
$info=get_db_field('exchangeinfo',['warehouse'=>['in',explode(",",$input['warehouse'])]],'pid') sql_assign($sql,'id',$info,'intersect') }
$sql['merchant']=['in',get_auth_merchant()] $sql=auth('exchangeclass',$sql) $count = Exchangeclass::where ($sql)->count() $arr = Exchangeclass::with('merchantinfo,customerinfo,userinfo')->where($sql)->page($input['page'],$input['limit'])->order('id desc')->select() $resule=[
'code'=>0,
'msg'=>'获取成功',
'count'=>$count,
'data'=>$arr
] }else{
$resule=['state'=>'error','info'=>'传入参数不完整!'];
}
return json($resule);
}
public function info(){
$input=input('get.');
if(isset_full($input,'id')){
$class=Exchangeclass::where(['id'=>$input['id']])->find();
$info=Exchangeinfo::with('roominfo,goodsinfo,warehouseinfo')->where(['pid'=>$input['id']])->select()->toarray();
foreach ($info as $info_key=>$info_vo) {
$info[$info_key]['roominfo']['serialinfo']=implode(",",arraychange(searchdata($info_vo['roominfo']['serialinfo'],['type|nod'=>['eq',0]]),'code'));
}
$this->assign('class',$class);
$this->assign('info',$info);
return $this->fetch('main');
}else{
$resule=['state'=>'error','info'=>'传入参数不完整!'];
}
return json($resule);
}
public function auditing($arr=[],$auto=false){
(empty($arr))&&($arr=input('post.arr')) if(empty($arr)){
$resule=['state'=>'error','info'=>'传入参数不完整!'];
}else{
$class_data=[] $info_data=[] foreach ($arr as $arr_vo) {
$class=Exchangeclass::where(['id'=>$arr_vo])->find();
$info=Exchangeinfo::where(['pid'=>$arr_vo])->select();
if(empty($class['type']['nod'])){
foreach ($info as $info_key=>$info_vo) {
if(!empty($info_vo['serial'])){
$serial_sql=['code'=>['in',explode(',',$info_vo['serial'])],'type'=>['neq',0]];
$serial=Serial::where($serial_sql)->find() if(!empty($serial)){
$auto&&(push_log('自动审核积分兑换单[ '.$class['number'].' ]失败,原因:第'.($info_key+1).'行串码状态不正确!')) return json(['state'=>'error','info'=>'审核-积分兑换单[ '.$class['number'].' ]失败,原因:第'.($info_key+1).'行串码状态不正确!']);
exit;
}
}
}
}else{
foreach ($info as $info_key=>$info_vo) {
if(!empty($info_vo['serial'])){
$serial_sql=['code'=>['in',explode(',',$info_vo['serial'])],'type'=>['neq',1]];
$serial=Serial::where($serial_sql)->find() if(!empty($serial)){
return json(['state'=>'error','info'=>'反审核-积分兑换单[ '.$class['number'].' ]第'.($info_key+1).'行串码状态不正确!']);
exit;
}
}
}
}
$class_data[$arr_vo]=$class $info_data[$arr_vo]=$info }
foreach ($arr as $arr_vo) {
$class=$class_data[$arr_vo] $info=$info_data[$arr_vo] if(empty($class['type']['nod'])){
foreach ($info as $info_vo) {
Room::where(['id'=>$info_vo['room']])->setDec('nums',$info_vo['nums']) $roominfo_sql['pid']=$info_vo['room'];
$roominfo_sql['type']=12;
$roominfo_sql['class']=$arr_vo;
$roominfo_sql['info']=$info_vo['id'];
$roominfo_sql['nums']=$info_vo['nums'];
Roominfo::create($roominfo_sql);
if (!empty($info_vo['serial'])){
$serial_arr=explode(',',$info_vo['serial']) foreach ($serial_arr as $serial_arr_vo) {
$serial=Serial::where(['code'=>$serial_arr_vo])->find() Serial::update(['id'=>$serial['id'],'type'=>1]);
Serialinfo::create (['pid'=>$serial['id'],'type'=>11,'class'=>$arr_vo]);
}
}
}
if(!empty($class['integral'])){
Customer::where (['id'=>$class['customer']])->setDec('integral',$class['integral']) Integral::create(['pid'=>$class['customer'],'set'=>0,'integral'=>$class['integral'],'type'=>4,'time'=>time(),'user'=>Session('is_user_id'),'class'=>$arr_vo]) }
Exchangeclass::update(['id'=>$arr_vo,'type'=>1,'auditinguser'=>Session('is_user_id'),'auditingtime'=>time()]) set_summary('exchange',$arr_vo,true) push_log(($auto?'自动':'').'审核积分兑换单[ '.$class['number'].' ]');
}else{
foreach ($info as $info_vo){
Room::where (['id'=>$info_vo['room']])->setInc('nums',$info_vo['nums']) if(!empty($info_vo['serial'])){
$serial=Serial::where(['code'=>['in',explode(',',$info_vo['serial'])]])->select() foreach ($serial as $serial_vo) {
Serial::update(['id'=>$serial_vo['id'],'type'=>0]);
Serialinfo::where(['pid'=>$serial_vo['id'],'type'=>11,'class'=>$arr_vo])->delete() }
}
}
Roominfo::where(['type'=>12,'class'=>$arr_vo])->delete() if(!empty($class['integral'])){
Customer::where (['id'=>$class['customer']])->setInc('integral',$class['integral']) Integral::destroy(['type'=>4,'class'=>$arr_vo]) }
Exchangeclass::update(['id'=>$arr_vo,'type'=>0,'auditinguser'=>0,'auditingtime'=>0]) set_summary('exchange',$arr_vo,false) push_log ('反审核积分兑换单[ '.$class['number'].' ]');
}
}
$resule=['state'=>'success'];
}
return $auto?true:json($resule);
}
public function del(){
$input=input('post.');
if(isset_full($input,'arr') && is_array($input['arr'])){
$class=db('exchangeclass')->where(['id'=>['in',$input['arr']]])->select()->ToArray() $data = searchdata($class,['type'=>['eq',1]]) if(empty($data)){
foreach ($class as $class_vo) {
push_log('删除积分兑换单[ '.$class_vo['number'].' ]') Hook::listen('del_exchange',$class_vo['id']) }
Exchangeclass::where(['id'=>['in',$input['arr']]])->delete();
Exchangeinfo::where(['pid'=>['in',$input['arr']]])->delete();
$resule=['state'=>'success'];
}else{
$resule=['state'=>'error','info'=>'积分兑换单[ '.$data[0]['number'].' ]已审核,不可删除!'];
}
}else{
$resule=['state'=>'error','info'=>'传入参数不完整!'];
}
return json($resule);
}
public function exports(){
$input=input('get.');
if(isset($input['mode'])){
push_log('导出积分兑换单数据') $sql=get_sql($input,[
'name'=>'continue',
'number'=>'full_like',
'customer'=>'full_division_in',
'start_time'=>'stime',
'end_time'=>'etime',
'type'=>'full_dec_1',
'warehouse'=>'continue',
'user'=>'full_division_in',
'data'=>'full_like',
],'exchangeclass') if(isset_full($input,'name')){
$goods=get_db_field('goods',['name|py'=>['like','%'.$input['name'].'%']],'id') if(empty($goods)){
sql_assign($sql,'id',[]) }else{
$info=get_db_field('exchangeinfo',['goods'=>['in',$goods]],'pid') sql_assign($sql,'id',$info) }
}
if(isset_full($input,'warehouse')){
$info=get_db_field('exchangeinfo',['warehouse'=>['in',explode(",",$input['warehouse'])]],'pid') sql_assign($sql,'id',$info,'intersect') }
$sql['merchant']=['in',get_auth_merchant()] $sql=auth('exchangeclass',$sql) $arr = Exchangeclass::with('merchantinfo,customerinfo,userinfo')->where($sql)->order('id desc')->select() if(empty($input['mode'])){
$formfield=get_formfield('exchange_export','array') $excel=[] array_push($excel,['type'=>'title','info'=>'积分兑换单列表']);
$table_cell=[] foreach ($formfield as $formfield_vo) {
$table_cell[$formfield_vo['key']]=$formfield_vo['text'];
}
$table_data=[] foreach ($arr as $arr_vo) {
$row_data=[];
foreach ($formfield as $formfield_vo) {
$val='nod_initial' foreach (explode('|',$formfield_vo['data']) as $source) {
$val=$val=='nod_initial'?$arr_vo[$source]:(isset($val[$source])?$val[$source]:'');
}
$row_data[$formfield_vo['key']]=$val }
array_push($table_data,$row_data) }
array_push($excel,['type'=>'table','info'=>['cell'=>$table_cell,'data'=>$table_data]]) $sum_arr=get_sums($table_data,['total','actual','integral']);
array_push($excel,['type'=>'node','info'=>[
'单据总积分:'.$sum_arr['total'],
'实际总积分:'.$sum_arr['actual'],
'实收总积分:'.$sum_arr['integral'],
]]) export_excel('积分兑换单列表',$excel);
}else{
$files=[] $formfield=get_formfield('exchange_exports','array') $sys=get_sys(['enable_batch','enable_serial']);
empty($sys['enable_batch'])&&(arrs_key_del($formfield,['key','batch']));
empty($sys['enable_serial'])&&(arrs_key_del($formfield,['key','serial']));
foreach ($arr as $arr_vo) {
$excel=[] array_push($excel,['type'=>'title','info'=>'积分兑换单']);
array_push($excel,['type'=>'node','info'=>[
'客户:'.$arr_vo['customerinfo']['name'],
'',
'单据日期:'.$arr_vo['time'],
'',
'单据编号:'.$arr_vo['number'],
]]);
$info=Exchangeinfo::where(['pid'=>$arr_vo['id']])->select();
$table_cell=[] foreach ($formfield as $formfield_vo) {
$table_cell[$formfield_vo['key']]=$formfield_vo['text'];
}
$table_data=[] foreach ($info as $info_vo) {
$row_data=[];
foreach ($formfield as $formfield_vo) {
$val='nod_initial' foreach (explode('|',$formfield_vo['data']) as $source) {
$val=$val=='nod_initial'?$info_vo[$source]:(isset($val[$source])?$val[$source]:'');
}
$row_data[$formfield_vo['key']]=$val }
array_push($table_data,$row_data) }
array_push($excel,['type'=>'table','info'=>['cell'=>$table_cell,'data'=>$table_data]]) array_push($excel,['type'=>'node','info'=>[
'单据积分:'.$arr_vo['total'],
'',
'实际积分:'.$arr_vo['actual'],
'',
'实收积分:'.$arr_vo['integral'],
]]);
array_push($excel,['type'=>'node','info'=>[
'客户积分:'.$arr_vo['customerinfo']['integral'],
'',
'制单人:'.$arr_vo['userinfo']['name'],
'',
'备注信息:'.$arr_vo['data'],
]]);
$path=export_excel($arr_vo['number'],$excel,false) array_push($files,$path) }
file_to_zip('积分兑换单明细',$files) }
}else{
$resule=['state'=>'error','info'=>'传入参数不完整!'];
}
return json($resule);
}
public function prints(){
$input=input('get.');
if(isset_full($input,'id')){
$print_name='exchange' $class=Exchangeclass::where(['id'=>$input['id']])->find();
$info=Exchangeinfo::where(['pid'=>$input['id']])->select();
$sys=get_sys(['enable_batch','enable_serial','print_paper']);
$formfield=get_formfield('exchange_print','array') empty($sys['enable_batch'])&&(arrs_key_del($formfield,['key','batch']));
empty($sys['enable_serial'])&&(arrs_key_del($formfield,['key','serial']));
$tab_html=get_print_tab($formfield,$info);
$print=get_print($print_name);
$print_text=$print[empty($sys['print_paper'])?'paper4':'paper2'];
$this->assign('class',$class);
$this->assign('tab_html',$tab_html);
$this->assign('print_name',$print_name);
$this->assign('paper_type',$sys['print_paper']);
$this->assign('print_text',$print_text);
return $this->fetch();
}else{
$resule=['state'=>'error','info'=>'传入参数不完整!'];
}
return json($resule);
}
}