123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412 |
- <?php
- namespace app\cxinv\model;
- use think\facade\Log;use think\Model;use think\model\concern\SoftDelete;
- class FinancialManager extends Base{
- use SoftDelete;
- protected $schema=[
- 'id'=>'int',//主键,自动递增
- 'code'=>'string',//订单或记录的唯一标识符
- 'invoiceCode'=>'string',//订单或记录的唯一标识符
- 'type'=>'int',//记录类型 1入库 2出库 3入库红冲 4 出库红冲
- 'source'=>'int',//数据来源,1结算 2 线下订单
- 'channel'=>'int',//渠道 1订单导入 2 非订单商品导入 3 c端无发票导入
- 'seller_code'=>'string',//卖方代码
- 'seller_name'=>'string',//卖方名称
- 'buyer_code'=>'string',//买方代码
- 'buyer_name'=>'string',//买方名称
- 'orderCode'=>'string',//订单编号
- 'cxCode'=>'string',//客户编号
- 'poCode'=>'string',//采购订单编号
- 'platform_type'=>'int',//平台类型,1ToB 2ToC
- 'goodType'=>'int',//商品类型 1库存 2非库存 3采返
- 'goodNo'=>'string',//商品编号
- 'goodName'=>'string',//商品名称
- 'unit'=>'string',//商品单位
- 'num'=>'int',//商品数量
- 'goodPrice'=>'decimal',//商品单价
- 'totalPrice'=>'decimal',//商品总价
- 'cat_code'=>'string',//商品分类代码
- 'cat_name'=>'string',//商品分类名称
- 'tax'=>'decimal',//税率或税费
- 'inv_fee'=>'decimal',//发票费用
- 'inv_seller_code'=>'string',//发票卖方代码
- 'inv_seller_name'=>'string',//发票卖方名称
- 'inv_buyer_code'=>'string',//发票买方代码
- 'inv_buyer_name'=>'string',//发票买方名称
- 'inv_number'=>'string',//发票编号
- 'inv_type'=>'string',//发票类型
- 'inv_item_id'=>'int',//发票项目ID
- 'inv_good_name'=>'string',//发票商品名称
- 'inv_cat_code'=>'string',//发票商品分类代码
- 'inv_spec'=>'string',//发票商品规格
- 'inv_unit'=>'string',//发票商品单位
- 'inv_num'=>'decimal',//发票商品数量
- 'inv_subprice'=>'decimal',//发票商品子项单价
- 'inv_subtotal'=>'decimal',//发票商品子项总价
- 'inv_tax'=>'decimal',//发票税额
- 'inv_tax_total'=>'decimal',//发票总税额
- 'inv_price'=>'decimal',//发票单价
- 'inv_total'=>'decimal',//发票总价
- 'inv_open_date'=>'datetime',
- 'cat_diff'=>'int',//分类差异
- 'tax_diff'=>'int',//税费差异
- 'remark'=>'string',//备注或说明
- 'error_remark'=>'string',//备注或说明
- 'status'=>'int',//1 待处理 2 正常 3计提 4 异常 5 取消数据
- 'balance_num'=>'decimal',//库存数量
- 'total_num'=>'decimal',//总库存数量,
- 'fz_date'=>'varchar',
- 'apply_id'=>'int',
- 'apply_name'=>'string',
- 'create_time'=>'datetime',
- 'update_time'=>'datetime',
- 'delete_time'=>'datetime',
- ];
- protected $createTime='create_time';
- protected $updateTime='update_time';
- protected $deleteTime='delete_time';
- public static $ManagerType=[1=>'入库',2=>'出库',3=>'入库红冲',4=>'出库红冲'];
- public static $ManagerSource=[1=>'结算',2=>'线下订单'];
- public static $ManagerGoodType=[1=>'库存',2=>'非库存',3=>'采返'];
- public static $PlatformType=[1=>'ToB',2=>'ToC'];
- public static $StatusCn=[1=>'待处理',2=>'正常',3=>'计提',4=>'异常' ,5=>'取消数据'];
- public function ProductRela(){
- return $this->hasMany(ManagerProduct::class,'manager_id','id');
- }
- public function FinancialTz(){
- return $this->hasOne(FinancialTz::class,'manager_id','id');
- }
- public function setErrorRemarkAttr($value,$data){
- return ($data['status']==4|| $data['status']==1)?$value:'';
- }
- public function CreateData($data){
- if(!empty($data)){
- switch ($data['channel']){
- case 1:
- $this->OrderImport($data);
- break;
- case 2:
- $this->OfflineImport($data);
- break;
- case 3:
- $this->CImport($data);
- break;
- default:
- throw new \Exception('导入渠道不存在');
- break;
- }
- }
- return true;
- }
- // 订单导入
- public function OrderImport($data){
- $data['total_num'] = $data['balance_num'] =$data['inv_num'];
- if(!empty($data['idArr'])) $product=array_map(function ($item) use($data){
- $temp=[];
- $temp['product_id']=$item['id'];
- $temp['type']=$data['type'];
- $temp['num']=$item['num'];
- $temp['apply_id'] = $data['apply_id'];
- $temp['apply_name'] = $data['apply_name'];
- $temp['status'] = 0;
- return $temp;
- },$data['idArr']);
- $info=$this->create($data);
- if($info->isEmpty()) throw new \Exception('添加失败');
- if (isset($product)) ManagerProduct::AddProduct($info->id,$product);
- }
- // 线下订单导入入库
- public function inProduct(&$data){
- $productID=[];
- try{
- $product = FinancialProducts::with(['productStock','ProductsCombind'])->where($this->getCondition($data))->findOrEmpty();
- if($product->isEmpty()){
- if($data['type']==4) throw new \Exception('红冲未找到财务商品信息');
- if($data['channel']==2 || $data['channel']==3) throw new \Exception('入库未找到财务商品信息');
- else{
- $product_data=[
- 'skuCode'=>$data['goodNo'],
- 'goodName'=>$data['goodName'],
- 'buyer_name'=>$data['inv_buyer_name'],
- 'buyer_code'=>$data['inv_buyer_code'],
- 'seller_name'=>$data['inv_seller_name'],
- 'seller_code'=>$data['inv_seller_code'],
- 'good_source'=>1,
- 'good_type'=>$data['goodType'],
- 'inv_good_name'=>$data['inv_good_name'],
- 'unit'=>$data['inv_unit'],
- 'unit_price'=>$data['inv_price'],
- 'subunit_price'=>$data['inv_subprice'],
- 'unit_weight'=>$data['unit_weight']??0,
- 'cat_code'=>$data['inv_cat_code'],
- 'cat_tax'=>$data['inv_tax'],
- "spec"=>$data['inv_spec'],
- 'inv_type'=>$data['inv_type'],
- "is_combind"=>0,
- 'basic_status'=>1,
- 'spectral'=>'',
- 'apply_id'=>$data['apply_id'],
- 'apply_name'=>$data['apply_name'],
- 'status'=>1
- ];
- $product = FinancialProducts::create($product_data);
- }
- }
- if($product->status!=1) throw new \Exception('商品未启用');
- if($product->is_combind==1){
- $rednum=$data['balance_num'];
- if($product->ProductsCombind->isEmpty()) throw new \Exception('组合商品未找到明细');
- $product->ProductsCombind->each(function ($item) use ($rednum, &$productID,$data) {
- ProductStock::AddStock($item->child_id, $rednum * $item->child_num);
- $productID[] = ['product_id' => $item->child_id, 'type'=>1,'status'=>1,'num' => $rednum * $item->child_num,'apply_id'=>$data['apply_id'], 'apply_name'=>$data['apply_name']];
- });
- }else{
- ProductStock::AddStock($product->id,$data['balance_num']);
- $productID[]=["product_id"=>$product->id,"type"=>1,'status'=>1,"num"=>$data['balance_num'],'apply_id'=>$data['apply_id'], 'apply_name'=>$data['apply_name']];
- }
- $data['balance_num']="0";
- $data['status']=2;
- return $productID;
- }catch (\Exception $e){
- throw new \Exception($e->getMessage());
- }
- }
- // 出库
- public function outProduct(&$data){
- $productID=[];
- try{
- if(($data['channel'] == 1 || $data['channel']==3) && empty($data['relaArr'])) {
- $product = FinancialProducts::with(['productStock','ProductsCombind'])->where($this->getCondition($data))->findOrEmpty();
- if($product->isEmpty()) throw new \Exception('出库未找到财务商品信息');
- if($product->status!=1) throw new \Exception('商品未启用');
- if($data['balance_num']>$product->residue_stock) throw new \Exception('出库数量大于库存');
- $item=['id'=>$product->id,'num'=> $data['balance_num']];
- $this->processSingleProduct($item, $productID,$data);
- } else {
- if (isset($data['relaArr']) && is_array($data['relaArr'])&& !empty($data['relaArr'])) {
- if(floatval($data['balance_num'])!= floatval(array_sum(array_column($data['relaArr'], 'num')))){
- throw new \Exception('出库商品数量不正确');
- }else{
- foreach ($data['relaArr'] as $item) {
- if (!isset($item['id']) || !isset($item['num'])) {
- throw new \Exception('relaArr 中的元素缺少 id 或 num');
- }
- $this->processSingleProduct($item, $productID, $data);
- }
- }
- } else {
- throw new \Exception('relaArr 为空或格式不正确');
- }
- }
- if($data['balance_num']=='0')$data['status']=2;
- }catch (\Exception $e){
- throw new \Exception($e->getMessage());
- }
- return $productID;
- }
- // 单商品处理
- private function processSingleProduct($data, &$productID, &$mainData = null) {
- $product = FinancialProducts::with(['productStock',"ProductsCombind"])->findOrEmpty($data['id']);
- if (!$product->isEmpty()) {
- if($product->status!=1) throw new \Exception('商品未启用');
- if($mainData['type']!=2 && $product->basic_status==2) throw new \Exception($product->skuCode.'商品不可为预估成本商品');
- $rednum = $data['num'];
- if($mainData['balance_num']< $data['num']) throw new \Exception('出库数量大于订单数量');
- if ($product->residue_stock < $data['num']) {
- if($mainData['type']==3) throw new \Exception($product->skuCode.'商品库存不足');
- $rednum = $product->residue_stock;
- if ($mainData !== null) {
- $mainData['balance_num'] = bcsub($mainData['balance_num'], $product->residue_stock, 8);
- }
- } else {
- if ($mainData !== null) {
- $mainData['balance_num'] = bcsub($mainData['balance_num'], $data['num'], 8);
- }
- }
- if($product->is_combind==1 ){
- if($product->ProductsCombind->isEmpty()) throw new \Exception('组合商品未找到明细');
- $productID=ProductsCombind::CombindSubStock($product->id,$rednum);
- array_map(function (&$item) use ($mainData){
- ProductStock::OutStock($item['product_id'], $item['num']);
- $item['status']=1;
- $item['apply_id']=$mainData['apply_id'];
- $item['apply_name']=$mainData['apply_name'];
- },$productID);
- }else{
- ProductStock::OutStock($product->id, $rednum);
- $productID[] = ['product_id' => $product->id, 'type'=>2,'status'=>1,'num' => $rednum,'apply_id'=>$mainData['apply_id'], 'apply_name'=>$mainData['apply_name']];
- }
- } else {
- throw new \Exception('出库未找到财务商品信息');
- }
- }
- public function getCondition($data){
- $where=[];
- if(($data['channel']==1|| $data['channel']==3) && (empty($data['relaArr'])|| !isset($data['relaArr']))) {
- $where=[
- 'skuCode'=>$data['goodNo'],
- 'goodName'=>$data['goodName'],
- 'good_type'=>$data['goodType'],
- 'seller_code'=>$data['inv_seller_code'],
- 'buyer_code'=>$data['inv_buyer_code'],
- 'inv_good_name'=>$data['inv_good_name'],
- 'unit'=>$data['inv_unit'],
- 'unit_price'=>$data['inv_price'],
- 'subunit_price'=>$data['inv_subprice'],
- 'inv_type'=>$data['inv_type'],
- 'cat_tax'=>$data['inv_tax'],
- 'cat_code'=>$data['inv_cat_code'],
- "basic_status"=>1
- ];
- if($data['type']==2){
- $good_source = $data['channel']==2?2:1;
- $where=[
- ['skuCode',"=",$data['goodNo']],
- ['good_type',"=",$data['goodType']],
- ['good_source',"=",$good_source],
- ['buyer_code',"=",$data['inv_seller_code']],
- ['basic_status',"=",1],
- ];
- }
- }
- else $where[]=[
- 'id','in',array_column($data['relaArr'],"id")
- ];
- return $where;
- }
- // 线下订单导入
- public function OfflineImport($data){
- $data['total_num'] = $data['balance_num'] =$data['relaArr'][0]['num'];
- if($data['orderCode']=="")$data['orderCode']=makeNo("PQR");
- if(!empty($data['idArr'])) $product=array_map(function ($item) use($data){
- $temp=[];
- $temp['product_id']=$item['id'];
- $temp['type']=$data['type'];
- $temp['num']=$item['num'];
- $temp['apply_id'] = $data['apply_id'];
- $temp['apply_name'] = $data['apply_name'];
- $temp['status'] = 0;
- return $temp;
- },$data['idArr']);
- $info=$this->create($data);
- if($info->isEmpty()) throw new \Exception('添加失败');
- if(isset($product) && !empty($product))ManagerProduct::AddProduct($info->id,$product);
- }
- // c端无发票导入
- public function CImport($data){
- $data['total_num'] = $data['balance_num'] =$data['num'];
- if(!empty($data['idArr'])) $product=array_map(function ($item) use($data){
- $temp=[];
- $temp['product_id']=$item['id'];
- $temp['type']=$data['type'];
- $temp['num']=$item['num'];
- $temp['apply_id'] = $data['apply_id'];
- $temp['apply_name'] = $data['apply_name'];
- $temp['status'] = 0;
- return $temp;
- },$data['idArr']);
- $info=$this->create($data);
- if($info->isEmpty()) throw new \Exception('添加失败');
- if(isset($product) && !empty($product))ManagerProduct::AddProduct($info->id,$product);
- }
- // 校验数据
- public function CheckDatas($data){
- $error=[];
- $invoice = PayInvoice::where('hpNo',$data['invoiceCode'])->findOrEmpty();
- if($invoice->isEmpty()){
- $invoice = InvoicePool::where("invNo",$data['invoiceCode'])->findOrEmpty();
- if($invoice->isEmpty()){
- $error[] = ['code'=>$data['invoiceCode'],'msg'=>'发票号未找到数据'];
- }
- }
- if(!$invoice->isEmpty() && $invoice->status!=4) $error[] = ['code'=>$data['invoiceCode'],'msg'=>'发票状态不正确'];
- $order = CgdInfo::where("sequenceNo",$data['orderCode'])->findOrEmpty();
- if($order->isEmpty()){
- $order = QrdInfo::where("sequenceNo",$data['orderCode'])->findOrEmpty();
- if($order->isEmpty())$error[] = ['code'=>$data['orderCode'],'msg'=>'订单未找到数据'];
- }
- $good = Good::where("spuCode",$data['goodNo'])->findOrEmpty();
- if($good->isEmpty()) $error[] = ['code'=>$data['goodNo'],'msg'=>'采销商品未找到数据'];
- if(!$order->isEmpty() && $order->cxCode!=$data['cxCode']) $error[] = ['code'=>$data['orderCode'],'msg'=>'订单主单号不一致'];
- return $error;
- }
- public function OutKet(&$data)
- {
- $productID=[];
- try{
- if (isset($data['relaArr']) && is_array($data['relaArr'])) {
- $ketArr=[];
- foreach ($data['relaArr'] as $item) {
- if (!isset($item['id']) || !isset($item['num'])) {
- throw new \Exception('relaArr 中的元素缺少 id 或 num');
- }
- $ket=$this->processKtProduct($item, $productID, $data);
- if(!empty($ket)) $ketArr[]=$ket;
- }
- if(!empty($ketArr)){
- $create=[
- 'manager_id'=>$data['id'],
- 'ktCode'=>makeNo('CL'),
- 'apply_id'=>$data['cl_uid'],
- 'apply_name'=>$data['cl_uname'],
- 'num'=>"0",
- 'out_fee'=>"0",
- 'check_fee'=>0,
- 'status'=>1,
- ];
- FinancialTz::MakeInfo($ketArr,$create);
- }
- if($data['balance_num']==0) $data['status']=empty($ketArr)?2:3; //计提
- } else {
- throw new \Exception('relaArr 为空或格式不正确');
- }
- }catch (\Exception $e){
- throw new \Exception($e->getMessage());
- }
- return $productID;
- }
- public function processKtProduct($item, &$productID, &$data)
- {
- $kt=[];
- $product = FinancialProducts::with(['productStock',"ProductsCombind"])->findOrEmpty($item['id']);
- if (!$product->isEmpty()) {
- if($product->status!=1) throw new \Exception('商品未启用');
- if($product->is_combind==0){
- if($product->residue_stock<$item['num']){
- $kt=["product_id"=>$product->id,"type"=>1,'num'=>bcsub($item['num'],$product->residue_stock,8),"subunit_price"=>$product->subunit_price,"unit_price"=>$product->unit_price];
- }
- ProductStock::OutStock($product->id,$item['num'],2);
- $productID[]=["product_id"=>$product->id,'num'=>$item['num'],'status'=>1,'type'=>2,'apply_id'=>$data['cl_uid'],'apply_name'=>$data['cl_uname']];
- $data['balance_num']=bcsub($data['balance_num'],$item['num'],8);
- }else{
- $productID=ProductsCombind::CombindSubStock($product->id,$item['num'],2);
- if($product->combind_stock<$item['num']) $kt =['product_id'=>$product->id,'type'=>1,'num'=>bcsub($item['num'],$product->combind_stock,8),'subunit_price'=>$product->subunit_price,'unit_price'=>$product->unit_price];
- array_map(function (&$item)use(&$kt,$data){
- ProductStock::OutStock($item['product_id'], $item['num'],2);
- $item['status']=1;
- $item['apply_id']=$data['cl_uid'];
- $item['apply_name']=$data['cl_uname'];
- },$productID);
- $data['balance_num']=bcsub($data['balance_num'],$item['num'],8);
- }
- }else{
- throw new \Exception('商品未找到');
- }
- return $kt;
- }
- }
|