web_site = $this->request->server('HTTP_ORIGIN'); $this->serial_status= Db::name('kuerp_sets')->where(['name'=>'serial'])->value('is_checked');//是否开启序列号 $this->batch_status= Db::name('kuerp_sets')->where(['name'=>'batch'])->value('is_checked');//是否开启批次 $this->auxiliary = Db::name('kuerp_sets')->where(['name'=>'auxiliary'])->value('is_checked');//是否启用属性 $this->taxes_status = Db::name('kuerp_sets')->where(['name'=>'taxes'])->value('is_checked');//是否启用税金 $this->price_taxes = Db::name('kuerp_sets')->where(['name'=>'price_taxes'])->value('is_checked');//商品价格是否含税 $this->taxes_rate = Db::name('kuerp_sets')->where(['name'=>'taxes'])->value('value');//税率 $this->price_decimal = Db::name('kuerp_sets')->where(['name'=>'price_decimal'])->value('value'); $this->number_decimal = Db::name('kuerp_sets')->where(['name'=>'number_decimal'])->value('value'); $this->examine_status = Db::name('kuerp_sets')->where(['name'=>'examine'])->value('is_checked'); } /** * 获取销货单列表 * @ApiMethod (POST) */ public function getSalesList() { $data = input(); $page = isset($data['page'])?$data['page']:1; $page = empty($page)?1:$page; $limit = isset($data['limit'])?$data['limit']:10; $limit = empty($limit)?10:$limit; $where = []; $keyword = isset($data['keyword'])?$data['keyword']:''; if(!empty($keyword)){ $where['s.purchase_code'] = ['like','%'.$keyword.'%']; } $order_num = isset($data['order_num'])?$data['order_num']:''; if(!empty($order_num)){ $where['g.order_num'] = ['like','%'.$order_num.'%']; } $third_account = $this->third_account; $where['g.third_id'] = $third_account['id']; $list = Db::name('kuerp_goods_sales')->alias('s') ->join('kuerp_goods_salethird g','s.id=g.purchase_id') ->join('kuerp_customers c','s.customers_id=c.id') ->where($where) ->page($page,$limit) ->field('s.purchase_code,s.purchase_time,s.customers_id,c.customer_code,c.customer_name,s.receiving_address as address, s.number,s.all_money,s.preferential_money,s.real_money,s.tax_money,s.money_tax_price,s.note,s.createtime') ->select(); foreach ($list as $list_k=>$list_v){ $list[$list_k]['purchase_time_text'] = date('Y-m-d',$list_v['purchase_time']); $list[$list_k]['createtime_text'] = date('Y-m-d H:i:s',$list_v['createtime']); } $count = Db::name('kuerp_goods_sales')->alias('s') ->join('kuerp_goods_salethird g','s.id=g.purchase_id') ->join('kuerp_customers c','s.customers_id=c.id') ->where($where)->count(); $result['list'] = $list;$result['count'] = $count; $this->success('查询成功',$result,200); } /** * 添加销货单 * @ApiMethod (POST) */ public function sales() { $detail = input(); $check = $this->checkParameter($detail,[ 'order_num|外部订单号'=>'require', 'mobile|客户'=>'require', 'goods|商品'=>'require', ]); if(isset($check['status'])){ $this->error($check['msg'],'',$check['status']); }else{ $purchase_code = $this->getCodes('',11); if(empty($purchase_code)){ $this->error('销货单单号获取失败','',27); } $purchase_time = strtotime(date('Y-m-d')); $checktime = Db::name('kuerp_checkout')->order(['checktime'=>'DESC'])->value('checktime'); $checkdate = Db::name('kuerp_checkout')->order(['checktime'=>'DESC'])->value('checkdate'); if($purchase_time<=$checktime){ $this->error('不可添加小于结账日期'.$checkdate.'的数据'); } //验证客户是否可用 $third_account = $this->third_account; $customer = Db::name('kuerp_customers') ->where(['customer_mobile'=>$detail['mobile'],'third_id'=>$third_account['id']]) ->find(); if(empty($customer)){ $this->error('此手机号未注册','',27); }elseif ($customer['status']!=1){ $this->error('客户状态异常','',27); } $salethird = Db::name('kuerp_goods_salethird') ->where(['order_num'=>$detail['order_num'],'third_id' => $third_account['id'],]) ->find(); if($salethird){ $this->error('此订单号已创建销货单','',27); } $account = Db::name('kuerp_accounts')->where(['is_default'=>'1'])->value('id'); if(empty($account)){ $account = Db::name('kuerp_accounts')->value('id'); if(empty($account)){ $this->error('未查询到可用账户','',27); } } $data = []; $number = 0;$all_money = 0;$tax_money=0;$money_tax_price=0; //验证商品数据 $goods_data = html_entity_decode($detail['goods']); $goods_data = json_decode($goods_data,true); if(empty($goods_data)){ $this->error('商品数据解析失败','',27); }else{ foreach ($goods_data as $goods_data_k=>$goods_data_v){ $check = $this->checkParameter($goods_data_v,[ 'goods_code|商品编号'=>'require', 'goods_unit|商品单位'=>'require', 'goods_attr|商品属性'=>'requireNone', 'number|数量'=>'require', 'price|商品价格'=>'require', ]); if(isset($check['status'])){ $this->error($check['msg'],'',$check['status']); } $status = 0; foreach ($goods_data as $goods_data_k2=>$goods_data_v2){ if($goods_data_v['goods_code']==$goods_data_v2['goods_code'] && $goods_data_v['goods_unit']==$goods_data_v2['goods_unit'] && $goods_data_v['goods_attr']==$goods_data_v2['goods_attr']){ $status += 1; } } if($status>1){ $this->error('同一笔销货单不可存在相同单位,属性的商品','',27); } $goods = Db::name('kuerp_goods')->where(['goods_code'=>$goods_data_v['goods_code']])->find(); if(empty($goods)){ $this->error('商品数据错误','',27); } if($goods['auxiliary']==1 && $this->auxiliary==1 && empty($goods_data_v['goods_attr'])){ $this->error($goods['goods_name'].'必须选择商品属性','',27); } } $data = []; $goods_data = $this->setGoodsData($goods_data); if($goods_data['status']!=1){ $this->error($goods_data['msg'],'',27); }else{ foreach ($goods_data['data'] as $goods_data_k=>$goods_data_v){ $goods_data_v['purchase_code'] = $purchase_code; $goods_data_v['purchase_type'] = 1; $goods_data_v['purchase_time'] = $purchase_time; $goods_data_v['customers_id'] = $customer['id']; $goods_data_v['sale_user'] = 0; $goods_data_v['receiving_address'] = isset($detail['address'])?$detail['address']:$customer['getgoods_address']; $data[] = $goods_data_v; $number += (float)$goods_data_v['number']; $all_money += (float)$goods_data_v['all_money']; $tax_money += (float)$goods_data_v['tax_money']; $money_tax_price += (float)$goods_data_v['money_tax_price']; } } } $preferential_rate = ((float)$detail['preferential_money'])/$money_tax_price; $row = [ 'purchase_code' => $purchase_code, 'purchase_type' => 1, 'purchase_time' => $purchase_time, 'customers_id' => $customer['id'],//客户 'sale_user' => 0,//销售员 'receiving_address' => isset($detail['address'])?$detail['address']:$customer['getgoods_address'], 'customers_money' => 0, 'number' => $number, 'all_money' => $all_money, 'tax_money' => $tax_money,//税 'money_tax_price' => $money_tax_price,//价税合计 'preferential_rate' => $preferential_rate, 'preferential_money'=> (float)$detail['preferential_money'], 'real_money' => (float)($money_tax_price-(float)$detail['preferential_money']), 'purchase_money' => 0, 'note' => isset($detail['note'])?$detail['note']:'', 'arrears' => 0, 'all_arrears' => 0, 'pay_status' => 1, 'pay_money' => (float)($money_tax_price-(float)$detail['preferential_money']), 'account' => $account, 'cost_data' => null, 'third_id' => $third_account['id'], 'createtime' => time(), 'updatetime' => time(), ]; $result = false; Db::startTrans(); try { if(!empty($row)){ $result = Db::name('kuerp_goods_sales')->insertGetId($row); } Db::name('kuerp_goods_salethird')->insert([ 'order_num' => $detail['order_num'], 'purchase_id' => $result, 'third_id' => $third_account['id'], 'createtime' => time(), 'updatetime' => time(), ]); Db::name('kuerp_goods_sales_info')->where(['purchase_code'=>$row['purchase_code']])->delete(); foreach ($data as $data_k=>$data_v){ $goods_serial = $data_v['serial_code']; unset($data_v['serial_code']); $goods_batch_info = $data_v['goods_batch_info']; unset($data_v['goods_batch_info']); $purchase_id = Db::name('kuerp_goods_sales_info')->insertGetId($data_v); //批次 if($goods_batch_info){ $goods_batch_info['purchase_info_id'] = $purchase_id; Db::name('kuerp_goods_batch_sale_info')->insert($goods_batch_info); } if(!empty($goods_serial)){ Db::name('kuerp_goods_serial')->where(['serial'=>['in',$goods_serial]])->update([ 'status'=>'3', 'sale_id' => $purchase_id, 'sale_code' => $data_v['purchase_code'] ]); } } Db::commit(); } catch (Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if ($result !== false) { if($this->examine_status!=1){ try { $exa = $this->examine($result,1); } catch (Exception $e) { } } $this->success('添加成功',['id'=>$result,'purchase_code'=>$row['purchase_code']],1); } else { $this->error('添加失败','',27); } } } /** * 循环解析商品信息 */ function setGoodsData($goods_data){ $serial_status = $this->serial_status;//是否开启序列号 $batch_status = $this->batch_status;//是否开启批次 $taxes_status = $this->taxes_status;//是否启用税金 $price_taxes = $this->price_taxes;//商品价格是否含税 $taxes_rate = $this->taxes_rate;//税率 $account = Db::name('kuerp_accounts')->where(['is_default'=>'1'])->value('id'); if(empty($account)){ $account = Db::name('kuerp_accounts')->value('id'); if(empty($account)){ return ['status'=>0,'msg'=>'未查询到可用账户']; } } $goods_arr = []; foreach ($goods_data as $goods_data_k=>$goods_data_v){ $goods = Db::name('kuerp_goods')->where(['goods_code'=>$goods_data_v['goods_code']])->find(); $number = (float)$goods_data_v['number']; if($taxes_status==1){//启用税金 $unit_price = $goods_data_v['price']; $tax_included = $unit_price;//默认接口提交的金额是价税合计,若不是则使用注释代码 $unit_price = $tax_included*100/(100+$taxes_rate);//单价 //if($price_taxes==1){//商品价格是否含税 //$tax_included = $unit_price; //$unit_price = $tax_included*100/(100+$taxes_rate);//单价 //}else{ //$tax_included = $unit_price*(100+$taxes_rate)/100; //} }else{ $unit_price = $goods_data_v['price']; $tax_included = $goods_data_v['price']; } $all_money = $unit_price*$number; $money_tax_price = $tax_included*$number; $tax_money = $money_tax_price-$all_money; //商品库存 $where = []; $where['goods_code'] = $goods['goods_code']; if($goods_data_v['goods_attr']>0){ $where['attribute_id'] = $goods_data_v['goods_attr']; } $stock = Db::name('kuerp_goods_stock')->where($where)->select();//通过库存去查询可用仓库 if(empty($stock)){ return ['status'=>0,'msg'=>$goods['goods_name'].'商品库存不足']; } $allow_stock = Db::name('kuerp_goods_stock')->where($where)->sum('stock'); $need_number = $number; $stock_arr = []; foreach ($stock as $stock_k=>$stock_v){ if($need_number<=0){ break; }else{ if($need_number<$stock_v['stock']){ $stock_arr[] = [ 'stores_id' => $stock_v['stores_id'], 'number' => $need_number, ]; $need_number = 0; }else{ $stock_arr[] = [ 'stores_id' => $stock_v['stores_id'], 'number' => $stock_v['stock'], ]; $need_number -= $stock_v['stock']; } } } if($goods['batch']==1 && $batch_status==1){//开启批次 $stock_batch_arr = []; foreach ($stock_arr as $stock_arr_k=>$stock_arr_v){ $where = []; $where['goods_id'] = $goods['id']; if($goods_data_v['goods_attr']>0){ $where['attribute_id'] = $goods_data_v['goods_attr']; } $where['stores_id'] = $stock_arr_v['stores_id']; $goods_batch_stock = Db::name('kuerp_goods_batch_stock')->where($where)->select(); if(empty($goods_batch_stock)){ return ['status'=>0,'msg'=>'批次信息查询失败']; } $need_number = $stock_arr_v['number']; foreach ($goods_batch_stock as $stock_k=>$stock_v){ if($need_number<=0){ break; }else{ $batch = Db::name('kuerp_goods_batch')->where(['id'=>$stock_v['batch_id']])->find(); if(empty($batch)){ return ['status'=>0,'msg'=>'批次信息查询失败']; } if($need_number<$stock_v['stock']){ $stock_batch_arr[] = [ 'stores_id' => $stock_v['stores_id'], 'batch_id' => $stock_v['batch_id'], 'number' => $need_number, 'batch_name' => $batch['batch_name'], 'manufacture' => $batch['manufacture'], 'guarantee' => $batch['guarantee'], 'validity' => $batch['validity'], ]; $need_number = 0; }else{ $stock_batch_arr[] = [ 'stores_id' => $stock_v['stores_id'], 'batch_id' => $stock_v['batch_id'], 'number' => $stock_v['stock'], 'batch_name' => $batch['batch_name'], 'manufacture' => $batch['manufacture'], 'guarantee' => $batch['guarantee'], 'validity' => $batch['validity'], ]; $need_number -= $stock_v['stock']; } } } } $stock_arr = $stock_batch_arr; } if($goods['serial']==1 && $serial_status==1){//开启序列号 foreach ($stock_arr as $stock_arr_k=>$stock_arr_v){ $where = []; $where['goods_id'] = $goods['id']; if($goods_data_v['goods_attr']>0){ $where['attribute_id'] = $goods_data_v['goods_attr']; } $where['stores_id'] = $stock_arr_v['stores_id']; $where['store_status'] = '1'; $serial_id = Db::name('kuerp_goods_serial')->where($where)->limit($stock_arr_v['number'])->column('id'); if(sizeof($serial_id)!=$stock_arr_v['number']){ return ['status'=>0,'msg'=>'商品可用序列号不足']; } $stock_arr[$stock_arr_k]['serial_id'] = $serial_id; $period_period = [];$serial_code = []; $serial = Db::name('kuerp_goods_serial')->where($where)->limit($stock_arr_v['number'])->select(); foreach ($serial as $serial_k=>$serial_v){ $period_period[] = [ 'serial' => $serial_v['serial'], 'remark' => $serial_v['remark'], ]; $serial_code[] = $serial_v['serial']; } $stock_arr[$stock_arr_k]['period_period'] = serialize($period_period); $stock_arr[$stock_arr_k]['serial_code'] = $serial_code; } } //多单位换算基础数量 $advance_rate = 1; if($goods['more_unit']==1){ $main_unit = $goods['more_unit_id']; $unit_list = Db::name('kuerp_unit_conversion') ->where(['main_unit'=>$main_unit]) ->order('advance_rate','ASC') ->field('vice_unit,advance_rate') ->select(); $unit_id = [$main_unit]; foreach ($unit_list as $unit_list_k=>$unit_list_v){ $unit_id[] = $unit_list_v['vice_unit']; } $unit_list_id = Db::name('kuerp_unit_conversion') ->where(['main_unit'=>$main_unit]) ->column('advance_rate','vice_unit'); $unit_list_id[$goods['unit_id']] = 1; $advance_rate = isset($unit_list_id[$goods_data_v['goods_unit']])?$unit_list_id[$goods_data_v['goods_unit']]:$advance_rate; } foreach ($stock_arr as $stock_arr_k=>$stock_arr_v){ $goods_batch_info = []; if(isset($stock_arr_v['batch_id'])){ $goods_batch_info = [ 'batch_id' => isset($stock_arr_v['batch_id'])?$stock_arr_v['batch_id']:0, 'goods_id' => $goods['id'], 'goods_code' => $goods['goods_code'], 'attribute_id'=> $goods_data_v['goods_attr'], 'goods_unit' => $goods_data_v['goods_unit'], 'stores_id' => $stock_arr_v['stores_id'], 'stock' => $stock_arr_v['number'], 'number' => $stock_arr_v['number'], 'batch_name' => isset($stock_arr_v['batch_name'])?$stock_arr_v['batch_name']:'', 'manufacture' => isset($stock_arr_v['manufacture'])?$stock_arr_v['manufacture']:null, 'guarantee' => isset($stock_arr_v['guarantee'])?$stock_arr_v['guarantee']:null, 'validity' => isset($stock_arr_v['validity'])?$stock_arr_v['validity']:null, 'status' => '0', 'type' => 2, 'createtime' => time(), 'updatetime' => time(), ]; } $goods_arr[] = [ 'goods_code' => $goods_data_v['goods_code'], 'goods_note' => $goods['note'], 'goods_unit' => $goods_data_v['goods_unit'], 'stores_id' => $stock_arr_v['stores_id'], 'allow_stock' => $allow_stock, 'number' => $stock_arr_v['number'], 'default_unit' => $goods['unit_id'], 'basic_nmber' => $stock_arr_v['number']*$advance_rate, 'unit_price' => $unit_price, 'cost_price' => 0, 'discount' => 0, 'dis_money' => 0, 'all_money' => $all_money, 'remark' => '', 'goods_attr' => $goods_data_v['goods_attr'], 'tax_included' => $tax_included, 'tax_rate' => $taxes_rate, 'tax_money' => $tax_money, 'money_tax_price'=> $money_tax_price, 'batch_id' => isset($stock_arr_v['batch_id'])?$stock_arr_v['batch_id']:0, 'batch_name' => isset($stock_arr_v['batch_name'])?$stock_arr_v['batch_name']:'', 'manufacture' => isset($stock_arr_v['manufacture'])?$stock_arr_v['manufacture']:null, 'guarantee' => isset($stock_arr_v['guarantee'])?$stock_arr_v['guarantee']:null, 'validity' => isset($stock_arr_v['validity'])?$stock_arr_v['validity']:null, 'preferential_rate' => 0, 'preferential_money' => 0, 'real_money' => $money_tax_price, 'purchase_money'=> 0, 'pay_status' => 1, 'pay_money' => $money_tax_price, 'account' => $account, 'createtime' => time(), 'updatetime' => time(), 'goods_batch_info' => $goods_batch_info, 'period' => isset($stock_arr_v['period_period'])?$stock_arr_v['period_period']:'', 'serial_code' => isset($stock_arr_v['serial_code'])?$stock_arr_v['serial_code']:[], ]; } } return ['status'=>1,'data'=>$goods_arr]; } //审核/反审核 function examine($ids = null,$examine=1){ $examine_set= Db::name('kuerp_sets')->where(['name'=>'examine'])->value('is_checked'); if(empty($ids)){ if($examine_set!=1){ return ['status'=>0,'msg'=>'系统错误']; } $this->error('请选择要操作的数据'); } if($examine==1){ $list = Db::name('kuerp_goods_sales')->where(['id'=>['in',$ids],'examine_status'=>'0'])->select(); $checktime = Db::name('kuerp_checkout')->order(['checktime'=>'DESC'])->value('checktime'); $checkdate = Db::name('kuerp_checkout')->order(['checktime'=>'DESC'])->value('checkdate'); foreach ($list as $list_k=>$list_v){ if($list_v['purchase_time']<=$checktime){ $this->error('不可操作小于结账日期'.$checkdate.'的数据'); } } $set_discount = \think\Db::name('kuerp_sets')->where(['name'=>'discount'])->value('is_checked'); $purchase_code = Db::name('kuerp_goods_sales')->where(['id'=>['in',$ids],'examine_status'=>'0'])->column('purchase_code'); if(empty($list)){ if($examine_set!=1){ return ['status'=>0,'msg'=>'系统错误']; } $this->error('请选择需要审核的数据'); } $account_list_obj = Db::name('kuerp_accounts')->field('id,account_coding,account_name,balance')->select(); $account_list = []; foreach ($account_list_obj as $account_list_obj_k=>$account_list_obj_v){ $account_list[$account_list_obj_v['id']] = $account_list_obj_v; } $examine_ids = []; $accountData = []; $arrears = []; $log = []; $payment_log = [];$invoicing = []; foreach ($list as $list_k=>$list_v){ $examine_ids[] = $list_v['id']; if($list_v['arrears']>0){ $suppliers = Db::name('kuerp_customers')->where(['id'=>$list_v['customers_id']])->field('id,need_receive_account')->find(); if($suppliers){ $status = false; foreach ($arrears as $arrears_k=>$arrears_v){ if($arrears_v['id']==$suppliers['id']){ $arrears[$arrears_k]['need_receive_account'] = $list_v['arrears']+$arrears_v['need_receive_account']; $status = true; } } if(!$status){ $arrears[] = [ 'id' => $suppliers['id'], 'need_receive_account' => $list_v['arrears']+$suppliers['need_receive_account'], ]; } } } if($list_v['pay_money']>0){ $accountData[] = [ 'balance' => $list_v['pay_money'], 'account' => $list_v['account'], ]; $log[] = [ 'account_coding' => $account_list[$list_v['account']]['account_coding'], 'account_name' => $account_list[$list_v['account']]['account_name'], 'balance' => $account_list[$list_v['account']]['balance']-$list_v['pay_money'], 'expenditure' => $list_v['pay_money'], 'pay_time' => $list_v['purchase_time'], 'bill_code' => $list_v['purchase_code'], 'business_type' => 5, 'customers_id' => $list_v['customers_id'], 'remark' => $list_v['note'], 'createtime' => time(), 'updatetime' => time(), ]; } $payment_log[] = [ 'customers_id' => $list_v['customers_id'], 'sale_user' => $list_v['sale_user'], 'business_type' => 1, 'bill_time' => $list_v['purchase_time'], 'bill_code' => $list_v['purchase_code'], 'bill_codes' => $list_v['purchase_code'], 'purchase_money' => $list_v['all_money'], 'discount_money' => $list_v['preferential_money'], 'real_money' => $list_v['real_money'], 'money_tax_price'=> $list_v['money_tax_price'], 'pay_money' => $list_v['pay_money'], 'give_money' => $list_v['arrears'], 'reamrk' => $list_v['note'], 'createtime' => time(), ]; if($set_discount==1){ $money = $list_v['real_money']; }else{ $money = $list_v['money_tax_price']; } $invoicing[] = [ 'customers_id' => $list_v['customers_id'], 'status' => $list_v['pay_status'], 'bill_time' => $list_v['purchase_time'], 'bill_code' => $list_v['purchase_code'], 'bill_codes' => $list_v['purchase_code'], 'money' => $list_v['real_money'], 'tax' => $list_v['tax_money'], 'real_money' => $money, 'pay_money' => 0, 'give_money' => $money, 'createtime' => time(), ]; } $editstock = [];$addstock = [];$goods_stock = []; $info_list = Db::name('kuerp_goods_sales_info')->where(['purchase_code'=>['in',$purchase_code]])->select(); foreach ($info_list as $info_list_k=>$info_list_v){ try { unset($info_list[$info_list_k]['period']); } catch (Exception $e) { } } $purchase_info_id = Db::name('kuerp_goods_sales_info')->where(['purchase_code'=>['in',$purchase_code]])->column('id'); $purchase_info = []; $goods_stock_log = [];//收发详情 $serial_log = [];//序列号跟踪 foreach ($info_list as $list_k=>$list_v){ //序列号跟踪 $goods_serial = Db::name('kuerp_goods_serial')->where(['sale_id'=>$list_v['id']])->field('goods_id,serial,stores_id,attribute_id')->select(); foreach ($goods_serial as $goods_serial_k=>$goods_serial_v){ $serial_log[] = [ 'goods_id'=>$goods_serial_v['goods_id'], 'stores_id'=>$goods_serial_v['stores_id'], 'attribute_id'=>$goods_serial_v['attribute_id'], 'serial'=>$goods_serial_v['serial'], 'business_type'=>'4', 'store_status'=>'2', 'remarks' => $list_v['remark'], 'purchase_id'=>$list_v['id'], 'customer_id'=>$list_v['customers_id'], 'purchase_code'=>$list_v['purchase_code'], 'purchase_time' => $list_v['purchase_time'], 'createtime' => time(), 'updatetime' => time(), ]; } //序列号跟踪 $info_list[$list_k]['sales_order_id'] = $list_v['id']; $info_list[$list_k]['createtime'] = time(); $info_list[$list_k]['updatetime'] = time(); $stock_number = $list_v['basic_nmber']; $goods = Db::name('kuerp_goods')->where(['goods_code'=>$list_v['goods_code']])->field('id,attribute_id,inventory,unit_id')->find(); $status = false; foreach ($goods_stock as $goods_stock_k=>$goods_stock_v){ if($goods_stock_v['id']==$goods['id']){ $goods_stock[$goods_stock_k]['inventory'] = $goods_stock_v['inventory']-$stock_number; $status = true; } } if(!$status){ $goods_stock[] = [ 'id'=>$goods['id'], 'inventory'=>$goods['inventory']-$stock_number, ]; } //商品库存 $where = []; $where['goods_code'] = $list_v['goods_code']; $where['stores_id'] = $list_v['stores_id']; if($list_v['goods_attr']>0){ $goods_attr = $list_v['goods_attr']; if($goods_attr>0){ $where['attribute_id'] = $goods_attr; } } //获取成本单价 $balance_cost=0;$balance_unit=0; $goodsInfo = json_decode($this->getUnit($list_v['goods_code'],$list_v['stores_id'],$list_v['goods_attr']),true); $unitData = isset($goodsInfo['unit'])?$goodsInfo['unit']:[]; if(empty($unitData)){ if($examine_set!=1){ return ['status'=>0,'msg'=>'商品信息获取错误']; } $this->error('商品信息获取错误'); }else{ foreach ($unitData as $unitData_k=>$unitData_v){ if($unitData_v['id']==$goods['unit_id']){ $balance_unit = $unitData_v['purchase']; } } } //获取成本单价结束 $stock = Db::name('kuerp_goods_stock')->where($where)->field('stock,id')->find(); if($stock){ $status = false; foreach ($editstock as $editstock_k=>$editstock_v){ if($editstock_v['id']==$stock['id']){ $editstock[$editstock_k]['stock'] -= $stock_number; $status = true; $balance_cost = (round($balance_unit*$stock_number,4)); } } if(!$status){ $editstock[] = [ 'id'=>$stock['id'], 'stock'=>(round($stock['stock']-$stock_number,4)) ]; $balance_cost = (round($balance_unit*$stock_number,4)); } }else{ $store = Db::name('kuerp_stores')->where(['id'=>$list_v['stores_id']])->value('name'); if($examine!=1){ if($examine_set!=1){ return ['status'=>0,'msg'=>$goods['goods_name'].'库存不足,操作失败']; } return ['status'=>0,'msg'=>$goods['goods_name'].'库存不足,操作失败']; } if($examine_set!=1){ return ['status'=>0,'msg'=>'商品'.$store.'库存不足']; } $this->error('商品'.$store.'库存不足'); } $purchase_info[] = $list_v['id']; //收发详情 if($goods){ $goods_stock_log[] = [ 'purchase_code' => $list_v['purchase_code'], 'purchase_id' => $list_v['id'], 'purchase_time' => $list_v['purchase_time'], 'business_type' => '4', 'customer_id' => $list_v['customers_id'], 'goods_id' => $goods['id'], 'goods_code' => $list_v['goods_code'], 'attribute_id' => (float)$list_v['goods_attr'], 'goods_unit' => (float)$list_v['goods_unit'], 'stores_id' => (float)$list_v['stores_id'], 'remark' => $list_v['remark'], 'out_number' => (float)$list_v['number'], 'out_basics' => (float)$list_v['basic_nmber'], 'out_unit' => $balance_unit, 'out_cost' => $balance_cost, 'createtime' => time(), 'updatetime' => time(), ]; } //收发详情结束 } foreach ($editstock as $editstock_k=>$editstock_v){ if($editstock_v['stock']<0){ $store = Db::name('kuerp_stores')->where(['id'=>$list_v['stores_id']])->value('name'); if($examine_set!=1){ return ['status'=>0,'msg'=>$goods['goods_name'].'库存不足,操作失败']; } $this->error($store.'库存不足,操作失败'); } } $order_count = []; $code_id = Db::name('kuerp_goods_saleord_info')->where(['purchase_id'=>['in',$purchase_info]])->column('order_id'); $purchase_order_info = Db::name('kuerp_goods_sales_order_info') ->field('id,purchase_code') ->where(['id'=>['in',$code_id]]) ->select(); foreach ($purchase_order_info as $purchase_order_info_k=>$purchase_order_info_v){ $purord_info = Db::name('kuerp_goods_saleord_info') ->where(['order_id'=>$purchase_order_info_v['id'],'purchase_id'=>['in',$purchase_info]])->field('sum(number) as number,sum(money) as money,sum(all_money) as all_money')->find(); $purchase_order_info[$purchase_order_info_k]['pay_count'] = isset($purord_info['number'])?$purord_info['number']:0; $purchase_order_info[$purchase_order_info_k]['pay_money'] = isset($purord_info['money'])?$purord_info['money']:0; $purchase_order_info[$purchase_order_info_k]['all_money'] = isset($purord_info['all_money'])?$purord_info['all_money']:0; } foreach ($purchase_order_info as $purchase_order_info_k=>$purchase_order_info_v){ $status = false; foreach ($order_count as $order_count_k=>$order_count_v){ if($order_count_v['purchase_code']==$purchase_order_info_v['purchase_code']){ $order_count[$order_count_k]['store_number'] += $purchase_order_info_v['pay_count']; $status = true; } } if(!$status){ $order_count[] = [ 'purchase_code' => $purchase_order_info_v['purchase_code'], 'store_number' => $purchase_order_info_v['pay_count'], ]; } } //修改状态 if(!empty($order_count)){ foreach ($order_count as $order_count_k=>$order_count_v){ $number = Db::name('kuerp_goods_sales_order')->where(['purchase_code'=>$order_count_v['purchase_code']])->field('id,number,store_number')->find(); if(($number['number']-$number['store_number'])<=$order_count_v['store_number']){ $order_count[$order_count_k]['status'] = 1; }else{ $order_count[$order_count_k]['status'] = 2; } $order_count[$order_count_k]['id'] = $number['id']; } } //批次库存数据 //获取设置 $set= \think\Db::name('kuerp_sets')->field('type,name,value,is_checked,set')->select(); $setList = []; foreach ($set as $set_k=>$set_v){ if(!empty($set_v['set'])){ $set_v['set'] = unserialize($set_v['set']); } $setList[$set_v['name']] = $set_v; } //数量价格格式化 $number_decimal = $setList['number_decimal']['value']; $goods_serial = Db::name('kuerp_goods_sales_info')->where(['id'=>['in',$purchase_info_id]])->select(); $goods_batch_add = [];$goods_batch_edit = [];$goods_batch_log = []; foreach ($goods_serial as $k=>$v){ $goods = Db::name('kuerp_goods')->where(['goods_code'=>$v['goods_code']])->field('id,goods_name,attribute_id,inventory,unit_id,batch')->find(); if($goods['batch']==1){ //先通过单位计算需修改多单位商品的库存数量 if($goods['unit_id']==$v['goods_unit']){ $stock_number = $v['number']; }else{ $unit_conversion = Db::name('kuerp_unit_conversion')->where(['main_unit'=>$goods['unit_id'],'vice_unit'=>$v['goods_unit']])->value('advance_rate'); if(empty($unit_conversion)){ if($examine_set!=1){ return ['status'=>0,'msg'=>'商品单位信息查询失败']; } $this->error('商品单位信息查询失败'); }else{ $stock_number = round(($v['number']*$unit_conversion),$number_decimal); } } $where = []; $where['goods_id'] = $goods['id']; if($v['goods_attr']){ $where['attribute_id'] = $v['goods_attr']; } $where['stores_id'] = $v['stores_id']; $where['batch_id'] = $v['batch_id']; $goods_batch_stock = Db::name('kuerp_goods_batch_stock')->where($where)->find(); if(empty($goods_batch_stock)){ $status = false; foreach ($goods_batch_add as $goods_batch_add_k=>$goods_batch_add_v){ if($goods_batch_add_v['batch_id']==$v['batch_id'] && $goods_batch_add_v['goods_id']==$v['goods_id'] && $goods_batch_add_v['attribute_id']==$v['attribute_id'] && $goods_batch_add_v['stores_id']==$v['stores_id']){ $goods_batch_add[$goods_batch_add_k]['stock'] -= $stock_number; $status = true; } } if(!$status){ $goods_batch_add[] = [ 'batch_id' => $v['batch_id'], 'goods_id' => $goods['id'], 'goods_code' => $v['goods_code'], 'attribute_id' => $v['goods_attr'], 'stores_id' => $v['stores_id'], 'stock' => $stock_number, 'createtime' => time(), 'updatetime' => time(), ]; } }else{ if(isset($goods_batch_edit[$goods_batch_stock['id']])){ $goods_batch_edit[$goods_batch_stock['id']] -= $stock_number; }else{ $goods_batch_edit[$goods_batch_stock['id']] = $goods_batch_stock['stock']-$stock_number; } } foreach ($goods_batch_edit as $k2=>$v2){ if($v2<0){ if($examine_set!=1){ return ['status'=>0,'msg'=>'商品'.$goods['goods_name'].'库存不足']; } $this->error('商品'.$goods['goods_name'].'库存不足'); } } //商品批次跟踪 $goods_batch_log[] = [ 'goods_id' => $goods['id'], 'goods_unit' => $v['goods_unit'], 'stores_id' => $v['stores_id'], 'batch_id' => (float)$v['batch_id'], 'number' => (float)$v['number'], 'purchase_code' => $v['purchase_code'], 'purchase_time' => $v['purchase_time'], 'business_type' => '4', 'remarks' => $v['remark'], 'order_id' => $v['id'], 'unit_price' => $v['unit_price'], 'tax_included' => $v['tax_included'], 'all_money' => $v['all_money'], 'tax_money' => $v['tax_money'], 'money_tax_price' => $v['money_tax_price'], 'customer_id' => $v['customers_id'], 'createtime' => time(), 'updatetime' => time(), ]; } //商品批次跟踪结束 } //批次库存数据结束 //应收账款明细表 $ex_list = Db::name('kuerp_goods_sales')->where(['id'=>['in',$ids],'examine_status'=>'0','arrears'=>['>','0']])->select(); $accounts_receivable = []; foreach ($ex_list as $ex_list_k=>$ex_list_v){ $accounts_receivable[] = [ 'customers_id' => $ex_list_v['customers_id'], 'purchase_time' => $ex_list_v['purchase_time'], 'purchase_code' => $ex_list_v['purchase_code'], 'business_type' => '6', 'receivable' => $ex_list_v['arrears'], 'staff_member_id' => $ex_list_v['sale_user'], 'remark' => $ex_list_v['note'], 'sale_money' => $ex_list_v['money_tax_price'], 'receivable_money' => round(($ex_list_v['real_money']+$ex_list_v['customers_money']),4), 'amount' => $ex_list_v['preferential_money'], 'cost' => $ex_list_v['customers_money'], 'real_money' => $ex_list_v['pay_money'], 'createtime' => time(), 'updatetime' => time(), ]; } //采购销售费用 $cost_data = []; $suppliers = Db::name('kuerp_suppliers')->column('id','supplyer_code'); foreach ($list as $list_k=>$list_v){ if(!empty($list_v['cost_data'])){ $cost_datas = unserialize($list_v['cost_data']); foreach ($cost_datas as $cost_datas_k=>$cost_datas_v){ $list_v['supplyer_code'] = isset($list_v['supplyer_code'])?$list_v['supplyer_code']:''; $cost_data[] = [ 'purchase_time' => time(), 'purchase_code' => (new Goods())->getCodes('',27), 'business_type' => '2', 'suppliers_ids' => isset($suppliers[$list_v['supplyer_code']])?$suppliers[$list_v['supplyer_code']]:null, 'suppliers_id' => $cost_datas_v['suppliers_id'], 'expend_type_id' => $cost_datas_v['expend_type_id'], 'purchase_number' => $list_v['purchase_code'], 'purchase_times' => $list_v['purchase_time'], 'money' => $cost_datas_v['money'], 'pay_money' => $cost_datas_v['money'], 'remark' => $cost_datas_v['remark'], 'preparation_id' => 0, 'operation_id' => 0, 'createtime' => time(), 'updatetime' => time(), ]; } } } //销售利润表 $profit = []; foreach ($list as $list_k=>$list_v){ $amount = null; $amounts = Db::name('kuerp_goods_sales_info')->where(['purchase_code'=>$list_v['purchase_code']])->sum('dis_money'); if($amounts>0){ $amount = $amounts; } $profit[] = [ 'purchase_type' => '1', 'purchase_code' => $list_v['purchase_code'], 'purchase_time' => $list_v['purchase_time'], 'customers_id' => $list_v['customers_id'], 'sale_user' => $list_v['sale_user'], 'preparation_id'=> $list_v['preparation_id'], 'examine_id' => 0, 'number' => $list_v['number'], 'amount' => $amount, 'all_money' => $list_v['all_money'], 'tax_money' => $list_v['tax_money'], 'money_tax_price'=> $list_v['money_tax_price'], 'preferential_money'=> $list_v['preferential_money'], 'purchase_money'=> $list_v['purchase_money'], 'real_money' => $list_v['real_money'], 'customers_money'=> $list_v['customers_money'], 'need_money' => $list_v['real_money']+$list_v['customers_money'], 'pay_money' => $list_v['pay_money'], 'remark' => $list_v['note'], ]; } //核销单源单 $writeoff_source = []; foreach ($list as $list_k=>$list_v){ if($list_v['arrears']>0){ $writeoff_source[] = [ 'purchase_type' => 3, 'type' => 1, 'customers_id' => $list_v['customers_id'], 'purchase_code' => $list_v['purchase_code'], 'purchase_time' => $list_v['purchase_time'], 'price' => $list_v['real_money'], 'money' => $list_v['pay_money'], 'write_off' => $list_v['arrears'], 'remark' => $list_v['note'], ]; } } Db::startTrans(); try { $orderinfo_id = Db::name('kuerp_user_ordersaleinfo')->where(['sales_id'=>['in',$ids]])->column('orderinfo_id'); $orderinfo = Db::name('kuerp_user_orderinfo')->where(['id'=>['in',$orderinfo_id]])->field('goods_id,number')->select(); foreach ($orderinfo as $orderinfo_k=>$orderinfo_v){ Db::name('kuerp_goods')->where(['id'=>$orderinfo_v['goods_id']])->inc('sale_number',$orderinfo_v['number'])->update(); } if(!empty($writeoff_source)){ Db::name('kuerp_writeoff_source')->insertAll($writeoff_source); } if(!empty($profit)){ Db::name('kuerp_goods_sales_profit')->insertAll($profit); } if(!empty($cost_data)){ Db::name('kuerp_cost')->insertAll($cost_data); } if(!empty($accounts_receivable)){ Db::name('kuerp_accounts_receivable')->insertAll($accounts_receivable); } //序列号跟踪 if(!empty($serial_log)){ Db::name('kuerp_goods_serial_log')->insertAll($serial_log); } if(!empty($goods_stock_log)){ Db::name('kuerp_goods_log')->insertAll($goods_stock_log); } //批次库存数据 if(!empty($goods_batch_log)){ Db::name('kuerp_goods_batch_log')->insertAll($goods_batch_log); } if(!empty($goods_batch_add)){ Db::name('kuerp_goods_batch_stock')->insertAll($goods_batch_add); } if(!empty($goods_batch_edit)){ foreach ($goods_batch_edit as $k=>$v){ Db::name('kuerp_goods_batch_stock')->where(['id'=>$k])->update(['stock'=>$v]); } } //批次库存数据结束 if(!empty($purchase_info_id)){ Db::name('kuerp_goods_serial')->where(['sale_id'=>['in',$purchase_info_id]])->update(['store_status'=>'2']); Db::name('kuerp_goods_batch_sale_info')->where(['purchase_info_id'=>['in',$purchase_info_id]])->update(['status'=>'1']); } if(!empty($order_count)){ $goodsSalesOrder = (new \app\admin\model\kuerp\GoodsSalesOrder()); foreach ($order_count as $order_count_k=>$order_count_v){ $goodsSalesOrder->where(['id'=>$order_count_v['id']])->inc('store_number',$order_count_v['store_number'])->update(); unset($order_count[$order_count_k]['store_number']); } $goodsSalesOrder->saveAll($order_count); } if(!empty($payment_log)){ (new \app\admin\model\kuerp\SalesPaymentLog())->saveAll($payment_log); } if(!empty($invoicing)){ (new \app\admin\model\kuerp\SalesInvoicing())->saveAll($invoicing); } $result = Db::name('kuerp_goods_sales')->where(['id'=>['in',$ids],'examine_status'=>'0'])->update(['examine_status'=>1,'examine_id'=>0,'examine_time'=>time(),'operation_id'=>0]); if(!empty($log)){ Db::name('kuerp_accounts_log')->insertAll($log); } if(!empty($editstock)){ (new \app\admin\model\kuerp\GoodsStock())->saveAll($editstock); } if(!empty($goods_stock)){ (new \app\admin\model\kuerp\Goods())->saveAll($goods_stock); } if(!empty($arrears)){ (new \app\admin\model\kuerp\Customers())->saveAll($arrears); } Db::name('kuerp_goods_saleord_info')->where(['purchase_id'=>['in',$purchase_info]])->update(['status'=>'1','times'=>time()]); foreach ($info_list as $info_list_k=>$info_list_v){ if(isset($info_list_v['id'])){ unset($info_list[$info_list_k]['id']); } } Db::name('kuerp_goods_sales_summary')->insertAll($info_list); if(!empty($accountData)){ foreach ($accountData as $accountData_k=>$accountData_v){ Db::name('kuerp_accounts')->where(['id'=>$accountData_v['account']])->inc('balance',$accountData_v['balance'])->update(); } } Db::commit(); } catch (Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if(isset($result) && $result !== false){ if($examine_set!=1){ return ['status'=>1,'msg'=>'操作成功']; } $this->success(sizeof($list).'条数据审核成功'); }else{ if($examine_set!=1){ return ['status'=>0,'msg'=>'操作失败']; } $this->error('审核失败'); } } } //获取单位价格信息 public function getUnit($goods_code='',$store='',$attr='',$supplyer_code=''){ $set= Db::name('kuerp_sets')->where(['name'=>'taxes'])->field('value,is_checked')->find(); $taxes= Db::name('kuerp_sets')->where(['name'=>'price_taxes'])->value('is_checked'); if($goods_code){ $row = Db::name('kuerp_goods')->where(['goods_code'=>$goods_code])->field('batch,serial,id,auxiliary,unit_id,more_unit,more_unit_id,purchase,goods_code,manager_warehouse_id,batch,shelf_life')->find(); $goods_id = isset($row['id'])?$row['id']:0; }else{ $goods_id = isset($_POST['goods_id'])?$_POST['goods_id']:0; $row = Db::name('kuerp_goods')->where(['id'=>$goods_id])->field('batch,serial,auxiliary,unit_id,more_unit,more_unit_id,purchase,goods_code,manager_warehouse_id,batch,shelf_life')->find(); } if(empty($row)){ return []; } if($supplyer_code){ $suppliers = Db::name('kuerp_suppliers')->where(['supplyer_code'=>$supplyer_code])->value('vat_rate'); } $auxiliary = Db::name('kuerp_auxiliary')->where(['status'=>1])->column('name','id'); $goods_attr = Db::name('kuerp_goods_attr')->where(['goods_id'=>$goods_id])->select(); foreach ($goods_attr as $goods_attr_k=>$goods_attr_v){ $aux_combination = Db::name('kuerp_aux_combination')->where(['id'=>$goods_attr_v['attribute_id']])->value('attr_ids'); $goods_attr[$goods_attr_k]['attr_ids'] = []; $goods_attr[$goods_attr_k]['attr_name'] = ''; if($aux_combination){ $goods_attr[$goods_attr_k]['attr_id'] = $aux_combination; $aux_combination = explode('&',$aux_combination); $goods_attr[$goods_attr_k]['attr_ids'] = $aux_combination; foreach ($aux_combination as $aux_combination_k=>$aux_combination_v){ $aux_combination[$aux_combination_k] = $auxiliary[$aux_combination_v]; } $aux_combination = implode('/',$aux_combination); $goods_attr[$goods_attr_k]['attr_name'] = $aux_combination; } } $where = []; $where['goods_id'] = $goods_id; if($store){ $where['stores_id'] = $store; }else{ $where['stores_id'] = $row['manager_warehouse_id']; } if($attr){ //$attribute_id = Db::name('kuerp_goods_attr')->where(['goods_id'=>$goods_id,'id'=>$attr])->value('attribute_id'); $where['attribute_id'] = $attr; } $stock = Db::name('kuerp_goods_stock')->where($where)->value('stock'); if($row['more_unit']==1){ $unit = Db::name('kuerp_unit_conversion')->alias('uc') ->join('kuerp_unit u','u.id=uc.vice_unit') ->where(['uc.main_unit'=>$row['unit_id']]) ->order('uc.advance_rate','ASC') ->field('uc.vice_unit,u.name,u.id,uc.advance_rate') ->select(); $unit_ids = [$row['unit_id']]; foreach ($unit as $unit_k=>$unit_v){ $unit_ids[] = $unit_v['id']; } $goods_price = Db::name('kuerp_goods_price')->where(['unit'=>['in',$unit_ids],'goods_id'=>$goods_id])->select(); foreach ($goods_price as $goods_price_k=>$goods_price_v){ foreach ($unit as $unit_k=>$unit_v){ if($goods_price_v['unit']==$row['unit_id']){ $goods_price[$goods_price_k]['advance_rate'] = 1; }else if($goods_price_v['unit']==$unit_v['id']){ $goods_price[$goods_price_k]['advance_rate'] = $unit_v['advance_rate']; } } } }else{ $unit_ids = [$row['unit_id']]; } $unit = Db::name('kuerp_unit')->where(['id'=>['in',$unit_ids]])->field('id,name')->select(); foreach ($unit as $unit_k=>$unit_v){ if(isset($goods_price)){ foreach ($goods_price as $goods_price_k=>$goods_price_v){ if($unit_v['id']==$goods_price_v['unit']){ $unit[$unit_k]['is_default'] = $goods_price_v['is_default']; //$unit[$unit_k]['purchase'] = $goods_price_v['purchase']*$goods_price_v['advance_rate']; $unit[$unit_k]['purchase'] = $goods_price_v['purchase']; if($goods_price_v['is_default']!=1){ $unit[$unit_k]['basic_nmber'] = $goods_price_v['advance_rate']; }else{ $unit[$unit_k]['basic_nmber'] = 1; } } } }else{ if($row['auxiliary']==1 && !empty($attr)){ $purchase = Db::name('kuerp_goods_attr')->where(['goods_id'=>$goods_id,'id'=>$attr])->value('purchase'); }else{ $purchase = $row['purchase']; } if($unit_v['id']==$row['unit_id']){ $unit[$unit_k]['is_default'] = 1; $unit[$unit_k]['basic_nmber'] = 1; $unit[$unit_k]['purchase'] = $purchase; } } } foreach ($unit as $unit_k=>$unit_v){ if(isset($suppliers) && $suppliers!==''){ $tax = $suppliers; }else{ $tax = $set['value']; } if(isset($set['is_checked']) && isset($set['value']) && $set['is_checked']==1){ $unit[$unit_k]['tax'] = 1; $unit[$unit_k]['tax_rate'] = $tax; }else{ $unit[$unit_k]['tax'] = 0; $unit[$unit_k]['tax_rate'] = 0; } if(isset($set['is_checked']) && isset($set['value']) && $set['is_checked']==1 && $taxes==1){ $unit[$unit_k]['purchase'] = round(($unit_v['purchase']/(100+$tax)*100),4); $unit[$unit_k]['tax_included'] = $unit_v['purchase']; }elseif(isset($set['is_checked']) && isset($set['value']) && $set['is_checked']==1 && $taxes!=1){ $unit[$unit_k]['purchase'] = $unit_v['purchase']; $unit[$unit_k]['tax_included'] = round(($unit_v['purchase']*(100+$tax)/100),4); }else{ $unit[$unit_k]['purchase'] = $unit_v['purchase']; $unit[$unit_k]['tax_included'] = $unit_v['purchase']; } } $data = []; foreach ($unit_ids as $unit_ids_k=>$unit_ids_v){ foreach ($unit as $unit_k=>$unit_v){ if($unit_ids_v==$unit_v['id']){ if($unit_v['is_default']==1){ $unit_v['stock'] = is_null($stock)?0:$stock; }else{ $unit_v['stock'] = round($stock/$unit_v['basic_nmber'],2); } $unit_v['tax_money'] = round(($unit_v['tax_included']-$unit_v['purchase']),4);; $data[] = $unit_v; } } } return json_encode(['unit'=>$data,'attr'=>$goods_attr,'goods'=>$row]); } /** * 销货单详情 * @ApiMethod (POST) */ public function salesInfo() { $detail = input(); $check = $this->checkParameter($detail,[ 'purchase_code|销货单编号'=>'require', ]); if(isset($check['status'])){ $this->error($check['msg'],'',$check['status']); }else{ $where = []; $third_account = $this->third_account; $where['g.third_id'] = $third_account['id']; $where['s.purchase_code'] = $detail['purchase_code']; $list = Db::name('kuerp_goods_sales')->alias('s') ->join('kuerp_goods_salethird g','s.id=g.purchase_id') ->join('kuerp_customers c','s.customers_id=c.id') ->where($where) ->field('s.purchase_code,s.purchase_time,s.customers_id,c.customer_code,c.customer_name,s.receiving_address as address, s.number,s.all_money,s.preferential_money,s.real_money,s.tax_money,s.money_tax_price,s.note,s.createtime') ->find(); if(empty($list)){ $this->error('未查询到相关数据','',27); } $list['purchase_time_text'] = date('Y-m-d',$list['purchase_time']); $list['createtime_text'] = date('Y-m-d H:i:s',$list['createtime']); $purchase_info = Db::name('kuerp_goods_sales_info')->alias('gs') ->join('kuerp_goods g','gs.goods_code=g.goods_code') ->join('kuerp_unit u','u.id=gs.goods_unit') ->where(['gs.purchase_code'=>$list['purchase_code']]) ->field('gs.goods_code,gs.goods_note,gs.goods_attr,gs.goods_unit,u.name as unit_name,gs.number,gs.unit_price,gs.tax_included,gs.all_money ,gs.tax_money,gs.money_tax_price') ->select(); foreach ($purchase_info as $purchase_info_k=>$purchase_info_v){ $attribute_id = Db::name('kuerp_goods_attr')->where(['id'=>$purchase_info_v['goods_attr']])->value('attribute_id'); $attribute_ids = Db::name('kuerp_aux_combination')->where(['id'=>$attribute_id])->value('attr_ids'); $attribute_name = ''; if($attribute_ids){ $attribute_ids = explode('&',$attribute_ids); $attribute = Db::name('kuerp_auxiliary')->where(['id'=>['in',$attribute_ids]])->column('name'); $attribute_name = implode('/',$attribute); } $purchase_info[$purchase_info_k]['attr_name'] = $attribute_name; } $list['info_list'] = $purchase_info; $this->success('查询成功',$list,1); } } /** * 添加退货单 * @ApiMethod (POST) */ public function returnSales() { $detail = input(); $check = $this->checkParameter($detail,[ 'order_num|外部订单号'=>'require', 'mobile|客户'=>'require', 'goods|商品'=>'require', ]); if(isset($check['status'])){ $this->error($check['msg'],'',$check['status']); }else{ $serial_status = $this->serial_status;//是否开启序列号 $batch_status = $this->batch_status;//是否开启批次 $taxes_status = $this->taxes_status;//是否启用税金 $price_taxes = $this->price_taxes;//商品价格是否含税 $taxes_rate = $this->taxes_rate;//税率 $where = []; $third_account = $this->third_account; $where['g.third_id'] = $third_account['id']; $where['g.order_num'] = $detail['order_num']; $list = Db::name('kuerp_goods_sales')->alias('s') ->join('kuerp_goods_salethird g','s.id=g.purchase_id') ->join('kuerp_customers c','s.customers_id=c.id') ->where($where) ->field('s.id,s.purchase_code,s.purchase_time,s.customers_id,c.customer_code,c.customer_name,s.receiving_address as address, s.number,s.all_money,s.preferential_money,s.real_money,s.tax_money,s.money_tax_price,s.note,s.createtime,s.examine_status') ->find(); if(empty($list)){ $this->error('未查询到相关数据','',27); }elseif ($list['examine_status']!=1){ $this->error('此销货单未审核,不可退货','',27); } $purchase_info = Db::name('kuerp_goods_sales_info')->alias('gs') ->where(['gs.purchase_code'=>$list['purchase_code']]) ->select(); $purchase_code = $this->getCodes('',12); if(empty($purchase_code)){ $this->error('销货退货单单号获取失败','',27); } $purchase_time = strtotime(date('Y-m-d')); $checktime = Db::name('kuerp_checkout')->order(['checktime'=>'DESC'])->value('checktime'); $checkdate = Db::name('kuerp_checkout')->order(['checktime'=>'DESC'])->value('checkdate'); if($purchase_time<=$checktime){ $this->error('不可添加小于结账日期'.$checkdate.'的数据'); } //验证客户是否可用 $third_account = $this->third_account; $customer = Db::name('kuerp_customers') ->where(['customer_mobile'=>$detail['mobile'],'third_id'=>$third_account['id']]) ->find(); if(empty($customer)){ $this->error('此手机号未注册','',27); }elseif ($customer['status']!=1){ $this->error('客户状态异常','',27); } $salethird = Db::name('kuerp_goods_exchangethird') ->where(['order_num'=>$detail['order_num'],'third_id' => $third_account['id'],]) ->find(); if($salethird){ $this->error('此订单号已创建销货退货单','',27); } $account = Db::name('kuerp_accounts')->where(['is_default'=>'1'])->value('id'); if(empty($account)){ $account = Db::name('kuerp_accounts')->value('id'); if(empty($account)){ $this->error('未查询到可用账户','',27); } } $data = []; //验证商品数据 $goods_data = html_entity_decode($detail['goods']); $goods_data = json_decode($goods_data,true); if(empty($goods_data)){ $this->error('商品数据解析失败','',27); }else{ $data = []; foreach ($goods_data as $goods_data_k=>$goods_data_v){ $check = $this->checkParameter($goods_data_v,[ 'goods_code|商品编号'=>'require', 'goods_unit|商品单位'=>'require', 'goods_attr|商品属性'=>'requireNone', 'number|数量'=>'require', 'price|商品价格'=>'require', ]); if(isset($check['status'])){ $this->error($check['msg'],'',$check['status']); } $status = 0; foreach ($goods_data as $goods_data_k2=>$goods_data_v2){ if($goods_data_v['goods_code']==$goods_data_v2['goods_code'] && $goods_data_v['goods_unit']==$goods_data_v2['goods_unit'] && $goods_data_v['goods_attr']==$goods_data_v2['goods_attr']){ $status += 1; } } if($status>1){ $this->error('同一笔销货单不可存在相同单位,属性的商品','',27); } $goods = Db::name('kuerp_goods')->where(['goods_code'=>$goods_data_v['goods_code']])->find(); if($goods['auxiliary']==1 && $this->auxiliary==1 && empty($goods_data_v['goods_attr'])){ $this->error($goods['goods_name'].'必须选择商品属性','',27); } $status = false; foreach ($purchase_info as $k=>$v){ if($v['goods_code']==$goods_data_v['goods_code'] && $v['goods_unit']==$goods_data_v['goods_unit']){ if(!empty($v['goods_attr']) && $v['goods_attr']==$goods_data_v['goods_attr']){ $status = true; }elseif (empty($v['goods_attr'])){ $status = true; } if($status){ $goods_batch_info = []; if($goods['batch']==1 && $batch_status==1){ if(empty($v['batch_id'])){ $this->error('销货单批次信息错误,不可退货','',27); } $batch = Db::name('kuerp_goods_batch')->where(['id'=>$v['batch_id']])->find(); if(empty($batch)){ $this->error('批次信息查询失败','',27); } $goods_batch_info = [ 'batch_id' => isset($v['batch_id'])?$v['batch_id']:'', 'goods_id' => $goods['id'], 'goods_code' => $goods['goods_code'], 'attribute_id'=> isset($goods_data_v['goods_attr'])?$goods_data_v['goods_attr']:0, 'goods_unit' => (int)$goods_data_v['goods_unit'], 'stores_id' => $v['stores_id'], 'stock' => (float)$goods_data_v['number'], 'number' => (float)$goods_data_v['number'], 'batch_name' => isset($v['batch_name'])?$v['batch_name']:'', 'manufacture' => $v['manufacture'], 'guarantee' => $v['guarantee'], 'validity' => $v['validity'], 'status' => '0', 'type' => 2, 'createtime' => time(), 'updatetime' => time(), ]; $v['goods_batch_info'] = $goods_batch_info; } if($goods['serial']==1 && $serial_status==1){//开启序列号 $serial = unserialize($v['period']); $serial_code = []; foreach ($serial as $serial_k=>$serial_v){ $serial_code[] = $serial_v['serial']; } $goods_serials = Db::name('kuerp_goods_serial')->where(['store_status'=>'1','serial'=>['in',$serial_code]])->limit($goods_data_v['number'])->field('serial,remark')->select(); if(sizeof($goods_serials)!=$goods_data_v['number']){ $this->error('商品可用序列号不足,退货失败','',27); } $period_period = [];$serial_code = []; foreach ($goods_serials as $serial_k=>$serial_v){ $period_period[] = [ 'serial' => $serial_v['serial'], 'remark' => $serial_v['remark'], ]; $serial_code[] = $serial_v['serial']; } $v['period_period'] = serialize($period_period); $v['serial_code'] = $serial_code; } //多单位换算基础数量 $advance_rate = 1; if($goods['more_unit']==1){ $main_unit = $goods['more_unit_id']; $unit_list = Db::name('kuerp_unit_conversion') ->where(['main_unit'=>$main_unit]) ->order('advance_rate','ASC') ->field('vice_unit,advance_rate') ->select(); $unit_id = [$main_unit]; foreach ($unit_list as $unit_list_k=>$unit_list_v){ $unit_id[] = $unit_list_v['vice_unit']; } $unit_list_id = Db::name('kuerp_unit_conversion') ->where(['main_unit'=>$main_unit]) ->column('advance_rate','vice_unit'); $unit_list_id[$goods['unit_id']] = 1; $advance_rate = isset($unit_list_id[$goods_data_v['goods_unit']])?$unit_list_id[$goods_data_v['goods_unit']]:$advance_rate; } $number = (float)$goods_data_v['number']; if($taxes_status==1){//启用税金 $unit_price = $goods_data_v['price']; $tax_included = $unit_price;//默认接口提交的金额是价税合计,若不是则使用注释代码 $unit_price = $tax_included*100/(100+$taxes_rate);//单价 //if($price_taxes==1){//商品价格是否含税 //$tax_included = $unit_price; //$unit_price = $tax_included*100/(100+$taxes_rate);//单价 //}else{ //$tax_included = $unit_price*(100+$taxes_rate)/100; //} }else{ $unit_price = $goods_data_v['price']; $tax_included = $goods_data_v['price']; } $all_money = $unit_price*$number; $money_tax_price = $tax_included*$number; $tax_money = $money_tax_price-$all_money; $data[] = [ 'purchase_code' => $purchase_code, 'purchase_type' => 2, 'purchase_time' => $purchase_time, 'customers_id' => $customer['id'], 'sale_user' => 0, 'receiving_address' => isset($detail['address'])?$detail['address']:$customer['getgoods_address'], 'goods_code' => $v['goods_code'], 'goods_note' => $v['remark'], 'goods_unit' => $v['goods_unit'], 'stores_id' => $v['stores_id'], 'allow_stock' => $v['number'], 'number' => $goods_data_v['number'], 'default_unit' => $v['default_unit'], 'basic_nmber' => $goods_data_v['number']*$advance_rate, 'unit_price' => $unit_price, 'cost_price' => 0, 'discount' => 0, 'dis_money' => 0, 'all_money' => $all_money, 'remark' => '', 'goods_attr' => $goods_data_v['goods_attr'], 'tax_included' => $tax_included, 'tax_rate' => $taxes_rate, 'tax_money' => $tax_money, 'money_tax_price'=> $money_tax_price, 'batch_id' => $v['batch_id'], 'batch_name' => $v['batch_name'], 'manufacture' => $v['manufacture'], 'guarantee' => $v['guarantee'], 'validity' => $v['validity'], 'preferential_rate' => 0, 'preferential_money' => 0, 'real_money' => $money_tax_price, 'purchase_money'=> 0, 'purchase_info_id' => $v['id'], 'purchase_id' => $list['purchase_code'], 'retreat_number' => (float)$goods_data_v['number'], 'retreat_status' => 1, 'retreat_money' => $money_tax_price, 'account' => $account, 'createtime' => time(), 'updatetime' => time(), 'goods_batch_info' => $goods_batch_info, 'period' => isset($v['period_period'])?$v['period_period']:'', 'serial_code' => isset($v['serial_code'])?$v['serial_code']:[], ]; } } } if(!$status){ $this->error('退货单商品数据与购货单商品数据不符,退货单创建失败','',1); } } } $number = 0;$all_money = 0;$tax_money=0;$money_tax_price=0; foreach ($data as $goods_data_k=>$goods_data_v){ $number += (float)$goods_data_v['number']; $all_money += (float)$goods_data_v['all_money']; $tax_money += (float)$goods_data_v['tax_money']; $money_tax_price += (float)$goods_data_v['money_tax_price']; } $preferential_rate = ((float)$detail['preferential_money'])/$money_tax_price; $row = [ 'purchase_code' => $purchase_code, 'purchase_type' => 2, 'purchase_time' => $purchase_time, 'customers_id' => $customer['id'],//客户 'sale_user' => 0,//销售员 'receiving_address' => isset($detail['address'])?$detail['address']:$customer['getgoods_address'], 'customers_money' => 0, 'number' => $list['number'], 'all_money' => $all_money, 'tax_money' => $tax_money,//税 'money_tax_price' => $money_tax_price,//价税合计 'preferential_rate' => $preferential_rate, 'preferential_money'=> (float)$detail['preferential_money'], 'real_money' => (float)($money_tax_price-(float)$detail['preferential_money']), 'purchase_money' => 0, 'note' => isset($detail['note'])?$detail['note']:'', 'arrears' => 0, 'all_arrears' => 0, 'pay_status' => 1, 'pay_money' => (float)($money_tax_price-(float)$detail['preferential_money']), 'purchase_id' => $list['purchase_code'], 'retreat_number' => $number, 'retreat_status' => 1, 'retreat_money' => (float)($money_tax_price-(float)$detail['preferential_money']), 'account' => $account, 'cost_data' => null, 'third_id' => $third_account['id'], 'createtime' => time(), 'updatetime' => time(), ]; $result = false; Db::startTrans(); try { if(!empty($row)){ $result = Db::name('kuerp_goods_sales_exchange')->insertGetId($row); } if($result){ Db::name('kuerp_goods_saleexc')->insert(['purchase_id'=>$list['id'],'exchange_id'=>$result]); } Db::name('kuerp_goods_exchangethird')->insert([ 'order_num' => $detail['order_num'], 'purchase_id' => $result, 'third_id' => $third_account['id'], 'createtime' => time(), 'updatetime' => time(), ]); Db::name('kuerp_goods_sales_exchange_info')->where(['purchase_code'=>$row['purchase_code']])->delete(); foreach ($data as $data_k=>$data_v){ $goods_serial = $data_v['serial_code']; unset($data_v['serial_code']); $goods_batch_info = $data_v['goods_batch_info']; unset($data_v['goods_batch_info']); $purchase_id = Db::name('kuerp_goods_sales_exchange_info')->insertGetId($data_v); //批次 if($goods_batch_info){ $goods_batch_info['purchase_info_id'] = $purchase_id; Db::name('kuerp_goods_batch_salee_info')->insert($goods_batch_info); } if(!empty($goods_serial)){ Db::name('kuerp_goods_serial')->where(['serial'=>['in',$goods_serial]])->update([ 'status'=>'4', 'purchase_exchange_id' => $purchase_id, 'purchase_exchange_code' => $data_v['purchase_code'] ]); } if(!empty($data_v['purchase_info_id'])){ Db::name('kuerp_goods_saleexc_info')->insert([ 'purchase_id'=>$data_v['purchase_info_id'], 'exchange_id'=>$purchase_id, 'type' => $data_v['purchase_type'], 'number'=>$data_v['retreat_number'], 'money'=>$data_v['retreat_money'], 'all_money'=>$data_v['all_money'], 'tax_rate'=>$data_v['tax_rate'], 'tax_money'=>$data_v['tax_money'], 'money_tax_price'=>$data_v['money_tax_price'] ]); } } Db::commit(); } catch (Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if ($result !== false) { if($this->examine_status!=1){ try { $exa = $this->examiner($result,1); } catch (Exception $e) { } } $this->success('添加成功',['id'=>$result,'purchase_code'=>$row['purchase_code']],1); } else { $this->error('添加失败','',27); } } } /** * 退货单详情 * @ApiMethod (POST) */ public function returnSalesInfo() { $detail = input(); $check = $this->checkParameter($detail,[ 'purchase_code|销货退货单编号'=>'require', ]); if(isset($check['status'])){ $this->error($check['msg'],'',$check['status']); }else{ $where = []; $third_account = $this->third_account; $where['g.third_id'] = $third_account['id']; $where['s.purchase_code'] = $detail['purchase_code']; $list = Db::name('kuerp_goods_sales_exchange')->alias('s') ->join('kuerp_goods_exchangethird g','s.id=g.purchase_id') ->join('kuerp_customers c','s.customers_id=c.id') ->where($where) ->field('s.purchase_code,s.purchase_time,s.customers_id,c.customer_code,c.customer_name,s.receiving_address as address, s.number,s.all_money,s.preferential_money,s.real_money,s.tax_money,s.money_tax_price,s.note,s.createtime') ->find(); if(empty($list)){ $this->error('未查询到相关数据','',27); } $list['purchase_time_text'] = date('Y-m-d',$list['purchase_time']); $list['createtime_text'] = date('Y-m-d H:i:s',$list['createtime']); $purchase_info = Db::name('kuerp_goods_sales_exchange_info')->alias('gs') ->join('kuerp_goods g','gs.goods_code=g.goods_code') ->join('kuerp_unit u','u.id=gs.goods_unit') ->where(['gs.purchase_code'=>$list['purchase_code']]) ->field('gs.goods_code,gs.goods_note,gs.goods_attr,gs.goods_unit,u.name as unit_name,gs.number,gs.unit_price,gs.tax_included,gs.all_money ,gs.tax_money,gs.money_tax_price') ->select(); foreach ($purchase_info as $purchase_info_k=>$purchase_info_v){ $attribute_id = Db::name('kuerp_goods_attr')->where(['id'=>$purchase_info_v['goods_attr']])->value('attribute_id'); $attribute_ids = Db::name('kuerp_aux_combination')->where(['id'=>$attribute_id])->value('attr_ids'); $attribute_name = ''; if($attribute_ids){ $attribute_ids = explode('&',$attribute_ids); $attribute = Db::name('kuerp_auxiliary')->where(['id'=>['in',$attribute_ids]])->column('name'); $attribute_name = implode('/',$attribute); } $purchase_info[$purchase_info_k]['attr_name'] = $attribute_name; } $list['info_list'] = $purchase_info; $this->success('查询成功',$list,1); } } //审核/反审核 function examiner($ids = null,$examine=1){ $examine_set= Db::name('kuerp_sets')->where(['name'=>'examine'])->value('is_checked'); if(empty($ids)){ if($examine_set!=1){ return ['status'=>0,'msg'=>'系统错误']; } $this->error('请选择要操作的数据'); } if($examine==1){ $list = Db::name('kuerp_goods_sales_exchange')->where(['id'=>['in',$ids],'examine_status'=>'0'])->select(); $checktime = Db::name('kuerp_checkout')->order(['checktime'=>'DESC'])->value('checktime'); $checkdate = Db::name('kuerp_checkout')->order(['checktime'=>'DESC'])->value('checkdate'); foreach ($list as $list_k=>$list_v){ if($list_v['purchase_time']<=$checktime){ $this->error('不可操作小于结账日期'.$checkdate.'的数据'); } } $set_discount = Db::name('kuerp_sets')->where(['name'=>'discount'])->value('is_checked'); $purchase_code = Db::name('kuerp_goods_sales_exchange')->where(['id'=>['in',$ids],'examine_status'=>'0'])->column('purchase_code'); if(empty($list)){ if($examine_set!=1){ return ['status'=>0,'msg'=>'系统错误']; } $this->error('请选择需要审核的数据'); } $account_list_obj = Db::name('kuerp_accounts')->field('id,account_coding,account_name,balance')->select(); $account_list = []; foreach ($account_list_obj as $account_list_obj_k=>$account_list_obj_v){ $account_list[$account_list_obj_v['id']] = $account_list_obj_v; } $examine_ids = []; $accountData = []; $log = []; $suppliers = []; $payment_log = [];$invoicing = []; $editstock = [];$goods_stock = [];$goods_exchange_info = [];$exchange_info = []; $goods_stock_log = [];//收发详情 $serial_log = [];//序列号跟踪 $order_count = []; foreach ($list as $list_k=>$list_v){ $info_list = Db::name('kuerp_goods_sales_exchange_info')->where(['purchase_code'=>$list_v['purchase_code']])->select(); foreach ($info_list as $info_list_k=>$info_list_v){ if($list_v['purchase_type']==1 && !empty($info_list_v['purchase_order_id'])){ $status = false; foreach ($order_count as $order_count_k=>$order_count_v){ if($order_count_v['purchase_code']==$info_list_v['purchase_order_id']){ $order_count[$order_count_k]['store_number'] += $info_list_v['retreat_number']; $status = true; } } if(!$status){ $order_count[] = [ 'purchase_code' => $info_list_v['purchase_order_id'], 'store_number' => $info_list_v['retreat_number'], ]; } //修改状态 if(!empty($order_count)){ foreach ($order_count as $order_count_k=>$order_count_v){ $number = Db::name('kuerp_goods_sales_order')->where(['purchase_code'=>$order_count_v['purchase_code']])->field('id,number,store_number')->find(); if(($number['number']-$number['store_number'])<=$order_count_v['store_number']){ $order_count[$order_count_k]['status'] = 1; }else{ $order_count[$order_count_k]['status'] = 2; } $order_count[$order_count_k]['id'] = $number['id']; } } } //序列号跟踪 $goods_serial = Db::name('kuerp_goods_serial')->where(['sale_id'=>$info_list_v['id']])->field('goods_id,serial,stores_id,attribute_id')->select(); foreach ($goods_serial as $goods_serial_k=>$goods_serial_v){ $serial_log[] = [ 'goods_id'=>$goods_serial_v['goods_id'], 'stores_id'=>$goods_serial_v['stores_id'], 'attribute_id'=>$goods_serial_v['attribute_id'], 'serial'=>$goods_serial_v['serial'], 'business_type'=>'5', 'store_status'=>'1', 'remarks' => $info_list_v['remark'], 'purchase_id'=>$info_list_v['id'], 'customer_id'=>$info_list_v['customers_id'], 'purchase_code'=>$info_list_v['purchase_code'], 'purchase_time' => $info_list_v['purchase_time'], 'createtime' => time(), 'updatetime' => time(), ]; } //序列号跟踪 $goods_exchange_info[] = $info_list_v; //商品总库存 $goods = Db::name('kuerp_goods')->where(['goods_code'=>$info_list_v['goods_code']])->field('id,attribute_id,inventory,unit_id')->find(); //换算单位为基础单位 $now_unit = Db::name('kuerp_unit')->where(['id'=>$info_list_v['goods_unit']])->find(); if(empty($now_unit)){ if($examine_set!=1){ return ['status'=>0,'msg'=>'商品单位错误']; } $this->error('商品单位错误'); }else if($now_unit['type']==2 && $now_unit['is_main']!=1){ $unit_conversion = Db::name('kuerp_unit_conversion')->where(['main_unit'=>$goods['unit_id'],'vice_unit'=>$now_unit['id']])->find(); if(empty($unit_conversion)){ if($examine_set!=1){ return ['status'=>0,'msg'=>'商品单位信息获取错误错误']; } $this->error('商品单位信息获取错误错误'); }else{ $info_list_v['retreat_number'] = $info_list_v['retreat_number']*$unit_conversion['advance_rate']; $goods_exchange[] = $info_list_v; } } $status = false; foreach ($goods_stock as $goods_stock_k=>$goods_stock_v){ if($goods_stock_v['id']==$goods['id']){ $goods_stock[$goods_stock_k]['inventory'] = $goods_stock_v['inventory']+$info_list_v['retreat_number']; $status = true; } } if(!$status){ $goods_stock[] = [ 'id'=>$goods['id'], 'inventory'=>$goods['inventory']+$info_list_v['retreat_number'], ]; } //获取成本单价 $balance_cost=0;$balance_unit=0; $goodsInfo = json_decode($this->getUnit($info_list_v['goods_code'],$info_list_v['stores_id'],$info_list_v['goods_attr']),true); $unitData = isset($goodsInfo['unit'])?$goodsInfo['unit']:[]; if(empty($unitData)){ if($examine_set!=1){ return ['status'=>0,'msg'=>'商品信息获取错误']; } $this->error('商品信息获取错误'); }else{ foreach ($unitData as $unitData_k=>$unitData_v){ if($unitData_v['id']==$goods['unit_id']){ $balance_unit = $unitData_v['purchase']; } } } //获取成本单价结束 //商品库存 $where = []; $where['goods_code'] = $info_list_v['goods_code']; $where['stores_id'] = $info_list_v['stores_id']; if($info_list_v['goods_attr']>0){ $goods_attr = Db::name('kuerp_goods_attr')->where(['id'=>$info_list_v['goods_attr']])->value('attribute_id'); if($goods_attr>0){ $where['attribute_id'] = $goods_attr; } } $stock = Db::name('kuerp_goods_stock')->where($where)->field('stock,id')->find(); if(empty($stock)){ if($examine_set!=1){ return ['status'=>0,'msg'=>'商品'.$info_list_v['goods_code'].'库存不足']; } $this->error('商品'.$info_list_v['goods_code'].'库存不足'); } $status = false; foreach ($editstock as $editstock_k=>$editstock_v){ if($editstock_v['id']==$stock['id']){ $editstock[$editstock_k]['stock'] += $info_list_v['retreat_number']; $status = true; $balance_cost = (round($balance_unit*$info_list_v['retreat_number'],4)); } } if(!$status){ $editstock[] = [ 'id'=>$stock['id'], 'stock'=>(round($stock['stock']+$info_list_v['retreat_number'],2)) ]; $balance_cost = (round($balance_unit*$info_list_v['retreat_number'],4)); } //收发详情 if($goods){ $goods_stock_log[] = [ 'purchase_code' => $info_list_v['purchase_code'], 'purchase_id' => $info_list_v['id'], 'purchase_time' => $info_list_v['purchase_time'], 'business_type' => '3', 'customer_id' => $info_list_v['customers_id'], 'goods_id' => $goods['id'], 'goods_code' => $info_list_v['goods_code'], 'attribute_id' => (float)$info_list_v['goods_attr'], 'goods_unit' => (float)$info_list_v['goods_unit'], 'stores_id' => (float)$info_list_v['stores_id'], 'remark' => $info_list_v['remark'], 'out_number' => (float)$info_list_v['number'], 'out_basics' => (float)$info_list_v['basic_nmber'], 'out_unit' => $balance_unit, 'out_cost' => $balance_cost, 'createtime' => time(), 'updatetime' => time(), ]; } //收发详情结束 } if ($list_v['retreat_money']>0){ $status = false; foreach ($accountData as $accountData_k=>$accountData_v){ if($accountData_v['id']==$list_v['account']){ $accountData[$accountData_k]['balance'] -= $list_v['retreat_money']; $status = true; } } if(!$status){ $accountData[] = [ 'balance' => $list_v['retreat_money'], 'id' => $list_v['account'], ]; } } $need_give_account = $list_v['real_money']+$list_v['customers_money']-$list_v['retreat_money']; if ($need_give_account>0){ $status = false; foreach ($suppliers as $suppliers_k=>$suppliers_v){ if($suppliers_v['id']==$list_v['customers_id']){ $suppliers[$suppliers_k]['need_receive_account'] -= $need_give_account; $status = true; } } if(!$status){ $suppliers[] = [ 'need_receive_account' => $need_give_account, 'id' => $list_v['customers_id'], ]; } } $examine_ids[] = $list_v['id']; if($list_v['pay_money']>0){ $log[] = [ 'account_coding' => $account_list[$list_v['account']]['account_coding'], 'account_name' => $account_list[$list_v['account']]['account_name'], 'balance' => $account_list[$list_v['account']]['balance']-$list_v['pay_money'], 'expenditure' => $list_v['pay_money'], 'pay_time' => $list_v['purchase_time'], 'bill_code' => $list_v['purchase_code'], 'business_type' => 6, 'customers_id' => $list_v['customers_id'], 'remark' => $list_v['note'], 'createtime' => time(), 'updatetime' => time(), ]; } $payment_log[] = [ 'customers_id' => $list_v['customers_id'], 'sale_user' => $list_v['sale_user'], 'business_type' => 2, 'bill_time' => $list_v['purchase_time'], 'bill_code' => $list_v['purchase_code'], 'bill_codes' => $list_v['purchase_code'], 'purchase_money' => -$list_v['all_money'], 'discount_money' => $list_v['preferential_money'], 'real_money' => -$list_v['real_money'], 'money_tax_price'=> -$list_v['money_tax_price'], 'pay_money' => -$list_v['retreat_money'], 'give_money' => -$list_v['arrears'], 'reamrk' => $list_v['note'], 'createtime' => time(), ]; if($set_discount==1){ $money = $list_v['real_money']; }else{ $money = $list_v['money_tax_price']; } $invoicing[] = [ 'customers_id' => $list_v['customers_id'], 'status' => $list_v['retreat_status']+3, 'bill_time' => $list_v['purchase_time'], 'bill_code' => $list_v['purchase_code'], 'bill_codes' => $list_v['purchase_code'], 'money' => $list_v['real_money'], 'tax' => $list_v['tax_money'], 'real_money' => $money, 'pay_money' => 0, 'give_money' => $money, 'createtime' => time(), ]; } $info_list_sum = Db::name('kuerp_goods_sales_exchange_info')->where(['purchase_code'=>['in',$purchase_code]])->select(); foreach ($info_list_sum as $list_k=>$list_v){ $info_list_sum[$list_k]['returns_order_id'] = $list_v['id']; unset($info_list_sum[$list_k]['id']); $info_list_sum[$list_k]['createtime'] = time(); $info_list_sum[$list_k]['updatetime'] = time(); } $info_list_id = Db::name('kuerp_goods_sales_exchange_info')->where(['purchase_code'=>['in',$purchase_code]])->column('id'); //批次库存数据 //获取设置 $set= \think\Db::name('kuerp_sets')->field('type,name,value,is_checked,set')->select(); $setList = []; foreach ($set as $set_k=>$set_v){ if(!empty($set_v['set'])){ $set_v['set'] = unserialize($set_v['set']); } $setList[$set_v['name']] = $set_v; } //数量价格格式化 $number_decimal = $setList['number_decimal']['value']; $goods_serial = Db::name('kuerp_goods_sales_exchange_info')->where(['id'=>['in',$info_list_id]])->select(); $goods_batch_add = [];$goods_batch_edit = [];$goods_batch_log = []; foreach ($goods_serial as $k=>$v){ $goods = Db::name('kuerp_goods')->where(['goods_code'=>$v['goods_code']])->field('id,goods_name,attribute_id,inventory,unit_id,batch')->find(); if($goods['batch']==1){ //先通过单位计算需修改多单位商品的库存数量 if($goods['unit_id']==$v['goods_unit']){ $stock_number = $v['number']; }else{ $unit_conversion = Db::name('kuerp_unit_conversion')->where(['main_unit'=>$goods['unit_id'],'vice_unit'=>$v['goods_unit']])->value('advance_rate'); if(empty($unit_conversion)){ if($examine_set!=1){ return ['status'=>0,'msg'=>'商品单位信息查询失败']; } $this->error('商品单位信息查询失败'); }else{ $stock_number = round(($v['number']*$unit_conversion),$number_decimal); } } $where = []; $where['goods_id'] = $goods['id']; if($v['goods_attr']){ $where['attribute_id'] = $v['goods_attr']; } $where['stores_id'] = $v['stores_id']; $where['batch_id'] = $v['batch_id']; $goods_batch_stock = Db::name('kuerp_goods_batch_stock')->where($where)->find(); if(empty($goods_batch_stock)){ $status = false; foreach ($goods_batch_add as $goods_batch_add_k=>$goods_batch_add_v){ if($goods_batch_add_v['batch_id']==$v['batch_id'] && $goods_batch_add_v['goods_code']==$v['goods_code'] && $goods_batch_add_v['attribute_id']==$v['goods_attr'] && $goods_batch_add_v['stores_id']==$v['stores_id']){ $goods_batch_add[$goods_batch_add_k]['stock'] += $stock_number; $status = true; } } if(!$status){ $goods_batch_add[] = [ 'batch_id' => $v['batch_id'], 'goods_id' => $goods['id'], 'goods_code' => $v['goods_code'], 'attribute_id' => $v['goods_attr'], 'stores_id' => $v['stores_id'], 'stock' => $stock_number, 'createtime' => time(), 'updatetime' => time(), ]; } }else{ if(isset($goods_batch_edit[$goods_batch_stock['id']])){ $goods_batch_edit[$goods_batch_stock['id']] += $stock_number; }else{ $goods_batch_edit[$goods_batch_stock['id']] = $goods_batch_stock['stock']+$stock_number; } } foreach ($goods_batch_edit as $k2=>$v2){ if($v2<0){ if($examine_set!=1){ return ['status'=>0,'msg'=>'商品'.$goods['goods_name'].'库存不足']; } $this->error('商品'.$goods['goods_name'].'库存不足'); } } //商品批次跟踪 $goods_batch_log[] = [ 'goods_id' => $goods['id'], 'goods_unit' => $v['goods_unit'], 'stores_id' => $v['stores_id'], 'batch_id' => (float)$v['batch_id'], 'number' => (float)$v['number'], 'purchase_code' => $v['purchase_code'], 'purchase_time' => $v['purchase_time'], 'business_type' => '5', 'remarks' => $v['remark'], 'order_id' => $v['id'], 'unit_price' => $v['unit_price'], 'tax_included' => $v['tax_included'], 'all_money' => $v['all_money'], 'tax_money' => $v['tax_money'], 'money_tax_price' => $v['money_tax_price'], 'customer_id' => $v['customers_id'], 'createtime' => time(), 'updatetime' => time(), ]; //商品批次跟踪结束 } } //批次库存数据结束 //应收账款明细表 $ex_list = Db::name('kuerp_goods_sales_exchange')->where(['id'=>['in',$ids],'examine_status'=>'0','arrears'=>['>','0']])->select(); $accounts_receivable = []; foreach ($ex_list as $ex_list_k=>$ex_list_v){ $accounts_receivable[] = [ 'customers_id' => $ex_list_v['customers_id'], 'purchase_time' => $ex_list_v['purchase_time'], 'purchase_code' => $ex_list_v['purchase_code'], 'business_type' => '7', 'receivable' => -$ex_list_v['arrears'], 'remark' => $ex_list_v['note'], 'sale_money' => $ex_list_v['money_tax_price'], 'receivable_money' => round(($ex_list_v['real_money']+$ex_list_v['customers_money']),4), 'amount' => $ex_list_v['preferential_money'], 'cost' => $ex_list_v['customers_money'], 'real_money' => $ex_list_v['pay_money'], 'createtime' => time(), 'updatetime' => time(), ]; } //采购销售费用 $cost_data = []; $now_suppliers = Db::name('kuerp_suppliers')->column('id','supplyer_code'); foreach ($list as $list_k=>$list_v){ if(!empty($list_v['cost_data'])){ $cost_datas = unserialize($list_v['cost_data']); foreach ($cost_datas as $cost_datas_k=>$cost_datas_v){ $list_v['supplyer_code'] = isset($list_v['supplyer_code'])?$list_v['supplyer_code']:''; $cost_data[] = [ 'purchase_time' => time(), 'purchase_code' => (new Goods())->getCodes('',27), 'business_type' => '4', 'suppliers_ids' => isset($now_suppliers[$list_v['supplyer_code']])?$now_suppliers[$list_v['supplyer_code']]:null, 'suppliers_id' => $cost_datas_v['suppliers_id'], 'expend_type_id' => $cost_datas_v['expend_type_id'], 'purchase_number' => $list_v['purchase_code'], 'purchase_times' => $list_v['purchase_time'], 'money' => $cost_datas_v['money'], 'pay_money' => $cost_datas_v['money'], 'remark' => $cost_datas_v['remark'], 'preparation_id' => 0, 'operation_id' => 0, 'createtime' => time(), 'updatetime' => time(), ]; } } } //销售利润表 $profit = []; foreach ($list as $list_k=>$list_v){ $amount = null; $amounts = Db::name('kuerp_goods_sales_exchange_info')->where(['purchase_code'=>$list_v['purchase_code']])->sum('dis_money'); if($amounts>0){ $amount = $amounts; } $profit[] = [ 'purchase_type' => '2', 'purchase_code' => $list_v['purchase_code'], 'purchase_time' => $list_v['purchase_time'], 'customers_id' => $list_v['customers_id'], 'sale_user' => $list_v['sale_user'], 'preparation_id'=> $list_v['preparation_id'], 'examine_id' => 0, 'number' => -$list_v['number'], 'amount' => -$amount, 'all_money' => -$list_v['all_money'], 'tax_money' => -$list_v['tax_money'], 'money_tax_price'=> -$list_v['money_tax_price'], 'preferential_money'=> -$list_v['preferential_money'], 'purchase_money'=> -$list_v['purchase_money'], 'real_money' => -$list_v['real_money'], 'need_money' => -$list_v['real_money']-$list_v['customers_money'], 'pay_money' => -$list_v['pay_money'], 'remark' => $list_v['note'], ]; } //核销单源单 $writeoff_source = []; foreach ($list as $list_k=>$list_v){ if($list_v['arrears']>0){ $writeoff_source[] = [ 'purchase_type' => 3, 'type' => 2, 'customers_id' => $list_v['customers_id'], 'purchase_code' => $list_v['purchase_code'], 'purchase_time' => $list_v['purchase_time'], 'price' => -$list_v['real_money'], 'money' => -$list_v['pay_money'], 'write_off' => -$list_v['arrears'], 'remark' => $list_v['note'], ]; } } Db::startTrans(); try { if(!empty($writeoff_source)){ Db::name('kuerp_writeoff_source')->insertAll($writeoff_source); } if(!empty($profit)){ Db::name('kuerp_goods_sales_profit')->insertAll($profit); } if(!empty($cost_data)){ Db::name('kuerp_cost')->insertAll($cost_data); } if(!empty($accounts_receivable)){ Db::name('kuerp_accounts_receivable')->insertAll($accounts_receivable); } //序列号跟踪 if(!empty($serial_log)){ Db::name('kuerp_goods_serial_log')->insertAll($serial_log); } if(!empty($goods_stock_log)){ Db::name('kuerp_goods_log')->insertAll($goods_stock_log); } //批次库存数据 if(!empty($goods_batch_log)){ Db::name('kuerp_goods_batch_log')->insertAll($goods_batch_log); } if(!empty($goods_batch_add)){ Db::name('kuerp_goods_batch_stock')->insertAll($goods_batch_add); } if(!empty($goods_batch_edit)){ foreach ($goods_batch_edit as $k=>$v){ Db::name('kuerp_goods_batch_stock')->where(['id'=>$k])->update(['stock'=>$v]); } } //批次库存数据结束 if(!empty($info_list_id)){ Db::name('kuerp_goods_batch_salee_info')->where(['purchase_info_id'=>['in',$info_list_id]])->update(['status'=>'1']); Db::name('kuerp_goods_serial')->where(['sale_exchange_id'=>['in',$info_list_id]])->update(['store_status'=>'1']); Db::name('kuerp_goods_saleexc_info')->where(['exchange_id'=>['in',$info_list_id]])->update(['status'=>'1','times'=>time()]); } //销售记录 $info_list = $goods_exchange_info; foreach ($info_list as $info_list_k=>$info_list_v){ $info_list[$info_list_k]['number'] = $info_list_v['retreat_number']; unset($info_list[$info_list_k]['id']); unset($info_list[$info_list_k]['purchase_id']); unset($info_list[$info_list_k]['purchase_info_id']); unset($info_list[$info_list_k]['retreat_status']); unset($info_list[$info_list_k]['retreat_number']); unset($info_list[$info_list_k]['retreat_money']); $info_list[$info_list_k]['createtime'] = time(); $info_list[$info_list_k]['updatetime'] = time(); } foreach ($info_list as $info_list_k=>$info_list_v){ $info_list[$info_list_k]['purchase_type'] = 2; } Db::name('kuerp_goods_sales_info')->insertAll($info_list); if(!empty($info_list_sum)){ foreach ($info_list_sum as $info_list_k=>$info_list_v){ unset($info_list_sum[$info_list_k]['purchase_id']); unset($info_list_sum[$info_list_k]['purchase_info_id']); unset($info_list_sum[$info_list_k]['retreat_status']); unset($info_list_sum[$info_list_k]['retreat_number']); unset($info_list_sum[$info_list_k]['retreat_money']); $info_list_sum[$info_list_k]['purchase_type'] = 2; $info_list_sum[$info_list_k]['number'] = -$info_list_v['retreat_number']; $info_list_sum[$info_list_k]['basic_nmber'] = -$info_list_v['basic_nmber']; $info_list_sum[$info_list_k]['all_money'] = -$info_list_v['all_money']; $info_list_sum[$info_list_k]['tax_money'] = -$info_list_v['tax_money']; $info_list_sum[$info_list_k]['money_tax_price'] = -$info_list_v['money_tax_price']; $info_list_sum[$info_list_k]['real_money'] = -$info_list_v['real_money']; $info_list_sum[$info_list_k]['pay_money'] = -$info_list_v['pay_money']; } Db::name('kuerp_goods_sales_summary')->insertAll($info_list_sum); } //销售记录 foreach ($exchange_info as $exchange_info_k=>$exchange_info_v){ $info_ids = Db::name('kuerp_goods_saleexc_info')->where(['exchange_id'=>$exchange_info_v['id']])->value('purchase_id'); if($info_ids){ $where = []; $where['id'] = $info_ids; $purchase_summary = Db::name('kuerp_goods_sales_summary')->where($where)->find(); if($purchase_summary){ Db::name('kuerp_goods_sales_summary')->where($where)->update([ 'number' => ($purchase_summary['number']-$exchange_info_v['retreat_number']), 'basic_nmber' => ($purchase_summary['basic_nmber']-$exchange_info_v['retreat_number']), 'all_money' => ($purchase_summary['all_money']-$exchange_info_v['all_money']), 'money_tax_price' => ($purchase_summary['money_tax_price']-$exchange_info_v['money_tax_price']), 'tax_money' => ($purchase_summary['tax_money']-$exchange_info_v['tax_money']), 'real_money' => ($purchase_summary['real_money']-$exchange_info_v['real_money']), ]); } } } Db::name('kuerp_goods_excpur_info')->where(['exchange_id'=>['in',$info_list_id]])->update(['status'=>'1','times'=>time()]); if(!empty($editstock)){ (new \app\admin\model\kuerp\GoodsStock())->saveAll($editstock); } if(!empty($goods_stock)){ (new \app\admin\model\kuerp\Goods())->saveAll($goods_stock); } $result = Db::name('kuerp_goods_sales_exchange')->where(['id'=>['in',$ids],'examine_status'=>'0'])->update(['examine_status'=>1,'examine_id'=>0,'examine_time'=>time(),'operation_id'=>0]); if(!empty($log)){ Db::name('kuerp_accounts_log')->insertAll($log); } if(!empty($accountData)){ foreach ($accountData as $accountData_k=>$accountData_v){ Db::name('kuerp_accounts')->where(['id'=>$accountData_v['id']])->dec('balance',$accountData_v['balance'])->update(); } } if(!empty($suppliers)){ foreach ($suppliers as $suppliers_k=>$suppliers_v){ Db::name('kuerp_customers')->where(['id'=>$suppliers_v['id']])->dec('need_receive_account',$suppliers_v['need_receive_account'])->update(); } } if(!empty($payment_log)){ (new \app\admin\model\kuerp\SalesPaymentLog())->saveAll($payment_log); } if(!empty($invoicing)){ (new \app\admin\model\kuerp\SalesInvoicing())->saveAll($invoicing); } Db::commit(); } catch (Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if(isset($result) && $result !== false){ if($examine_set!=1){ return ['status'=>1,'msg'=>'操作成功']; } $this->success(sizeof($list).'条数据审核成功'); }else{ if($examine_set!=1){ return ['status'=>0,'msg'=>'操作失败']; } $this->error('审核失败'); } } } }