123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- <?php
- declare (strict_types = 1);
- namespace app\cxinv\command;
- use app\cxinv\model\FinancialManager;
- use app\cxinv\model\FinancialProducts;
- use app\cxinv\model\ManagerProduct;
- use app\cxinv\model\ProductFz;
- use app\cxinv\model\ProductsCombind;
- use app\cxinv\model\ProductStock;
- use app\user\model\Business;use think\console\Command;
- use think\console\Input;
- use think\console\input\Argument;
- use think\console\input\Option;
- use think\console\Output;
- use think\facade\Cache;
- class CreateFz extends Command
- {
- protected function configure()
- {
- // 指令配置
- $this->setName('createfz')
- ->setDescription('the createfz command');
- }
- protected function execute(Input $input, Output $output)
- {
- if(Cache::get('createfz')==1){
- $output->writeln('正在执行中');
- return;
- }
- Cache::set('createfz',1);
- $info = $this->getManager();
- while ($info->valid()){
- $output->writeln($info->current()->id.'正在执行中');
- #$output->writeln($info->current()->id);
- $item=$info->current();
- $this->active($item);
- $info->next();
- }
- Cache::set('createfz',0);
- }
- public function getManager()
- {
- $list = FinancialManager::with(["ProductRela"=>["Product"]])->where(["status"=>0])->cursor();
- foreach ($list as $manager){
- yield $manager;
- }
- }
- public function active($info){
- if($info->type==1){
- $this->inManager($info);
- }
- if($info->type==2){
- $this->outManager($info);
- }
- }
- public function inManager($info){
- FinancialManager::startTrans();
- try{
- $product = FinancialProducts::with(['productStock','ProductsCombind'])->where($this->getCondition($info))->findOrEmpty();
- if($product->isEmpty()){
- if($info['type']==4) throw new \Exception('红冲未找到财务商品信息');
- if($info['channel']==2 || $info['channel']==3) throw new \Exception('入库未找到财务商品信息');
- else{
- $product_data=[
- 'skuCode'=>$info['goodNo'],
- 'goodName'=>$info['goodName'],
- 'buyer_name'=>$info['inv_buyer_name'],
- 'buyer_code'=>$info['inv_buyer_code'],
- 'seller_name'=>$info['inv_seller_name'],
- 'seller_code'=>$info['inv_seller_code'],
- 'good_source'=>1,
- 'good_type'=>$info['goodType'],
- 'inv_good_name'=>$info['inv_good_name'],
- 'unit'=>$info['inv_unit'],
- 'unit_price'=>$info['inv_price'],
- 'subunit_price'=>$info['inv_subprice'],
- 'unit_weight'=>$info['unit_weight']??0,
- 'cat_code'=>$info['inv_cat_code'],
- 'cat_tax'=>$info['inv_tax'],
- "spec"=>$info['inv_spec'],
- 'inv_type'=>$info['inv_type'],
- "is_combind"=>0,
- 'basic_status'=>1,
- 'spectral'=>'',
- 'apply_id'=>$info['apply_id'],
- 'apply_name'=>$info['apply_name'],
- 'status'=>1
- ];
- $product = FinancialProducts::create($product_data);
- }
- }
- if($product->status!=1) throw new \Exception('商品未启用');
- if($product->is_combind==1){
- if($info['type']==1) throw new \Exception('入库商品不能为组合商品');
- $productID=ProductStock::AddCombindStock($product->id,$info['balance_num']);
- }else{
- $productID[]= ProductStock::AddSingleStock($product,$info['balance_num']);
- }
- if(!empty($productID)){
- array_map(function ($item)use ($info){
- $item['id'] = ManagerProduct::where(['manager_id'=>$info['id'],'product_id'=>$item['product_id']])->value('id');
- $item['status']=1;
- $item['manager_id'] = $info["id"];
- $item['apply_id']=$info['apply_id'];
- $item['apply_name']=$info['apply_name'];
- (new \app\cxinv\model\ManagerProduct)->save($item);
- },$productID);
- }
- FinancialManager::commit();
- $info['balance_num']='0';
- $info->status=2;
- }catch (\Exception $e){
- FinancialManager::rollback();
- $info->status=4;
- $info->error_remark = $e->getMessage();
- }
- $info->save();
- }
- private function getCondition($data){
- $where=[];
- $childArr= $data->ProductRela->toArray();
- if(($data['channel']==1|| $data['channel']==3) && empty($childArr)) {
- $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($childArr,"product_id")
- ];
- return $where;
- }
- public function outManager($data){
- FinancialManager::startTrans();
- $productID=[];
- try{
- if(($data['channel'] == 1 || $data['channel']==3) && empty($data->ProductRela->toArray())) {
- $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=['product_id'=>$product->id,'num'=> $data['balance_num']];
- $this->processSingleProduct($item, $productID,$data);
- } else {
- if(floatval($data['balance_num'])!= floatval(array_sum(array_column($data->ProductRela->toArray(), 'num')))) throw new \Exception('出库商品数量不正确');
- foreach ($data->ProductRela->toArray() as $item) {
- $this->processSingleProduct($item, $productID, $data);
- }
- }
- if(!empty($productID)) {
- array_map(function ($item) use ($data) {
- $item['id'] = ManagerProduct::where(['manager_id' => $data['id'], 'product_id' => $item['product_id']])->value('id');
- $item['status'] = 1;
- $item['manager_id'] = $data["id"];
- $item['apply_id']=$data['apply_id'];
- $item['apply_name']=$data['apply_name'];
- (new \app\cxinv\model\ManagerProduct)->save($item);
- }, $productID);
- }
- if($data['balance_num']!=0) throw new \Exception('出库数量不正确');
- FinancialManager::commit();
- $data['status']=2;
- }catch (\Exception $e){
- FinancialManager::rollback();
- $data['status']=4;
- $data['error_remark'] = $e->getMessage();
- }
- $data->save();
- }
- private function processSingleProduct($data, &$productID, &$mainData = null) {
- $product = FinancialProducts::with(['productStock',"ProductsCombind"])->findOrEmpty($data['product_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('出库数量大于订单数量');
- $stock = $product->is_combind==1?$product->combind_stock:$product->residue_stock;
- if ($stock< $data['num'])throw new \Exception($product->skuCode.'商品库存不足');
- $mainData['balance_num'] = bcsub($mainData['balance_num'],$rednum, 8);
- if($product->is_combind==1 ){
- if($product->ProductsCombind->isEmpty()) throw new \Exception('组合商品未找到明细');
- $producttemp=ProductStock::SubCombindStock($product->id, $rednum);
- $productID= array_merge($productID,$producttemp);
- }else{
- $productID[]=ProductStock::SubSingleStock($product, $rednum);
- }
- } else throw new \Exception('出库未找到财务商品信息');
- }
- }
|