CreateFz.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\cxinv\command;
  4. use app\cxinv\model\FinancialManager;
  5. use app\cxinv\model\FinancialProducts;
  6. use app\cxinv\model\ManagerProduct;
  7. use app\cxinv\model\ProductFz;
  8. use app\cxinv\model\ProductsCombind;
  9. use app\cxinv\model\ProductStock;
  10. use app\user\model\Business;use think\console\Command;
  11. use think\console\Input;
  12. use think\console\input\Argument;
  13. use think\console\input\Option;
  14. use think\console\Output;
  15. use think\facade\Cache;
  16. class CreateFz extends Command
  17. {
  18. protected function configure()
  19. {
  20. // 指令配置
  21. $this->setName('createfz')
  22. ->setDescription('the createfz command');
  23. }
  24. protected function execute(Input $input, Output $output)
  25. {
  26. if(Cache::get('createfz')==1){
  27. $output->writeln('正在执行中');
  28. return;
  29. }
  30. Cache::set('createfz',1);
  31. $info = $this->getManager();
  32. while ($info->valid()){
  33. $output->writeln($info->current()->id.'正在执行中');
  34. #$output->writeln($info->current()->id);
  35. $item=$info->current();
  36. $this->active($item);
  37. $info->next();
  38. }
  39. Cache::set('createfz',0);
  40. }
  41. public function getManager()
  42. {
  43. $list = FinancialManager::with(["ProductRela"=>["Product"]])->where(["status"=>0])->cursor();
  44. foreach ($list as $manager){
  45. yield $manager;
  46. }
  47. }
  48. public function active($info){
  49. if($info->type==1){
  50. $this->inManager($info);
  51. }
  52. if($info->type==2){
  53. $this->outManager($info);
  54. }
  55. }
  56. public function inManager($info){
  57. FinancialManager::startTrans();
  58. try{
  59. $product = FinancialProducts::with(['productStock','ProductsCombind'])->where($this->getCondition($info))->findOrEmpty();
  60. if($product->isEmpty()){
  61. if($info['type']==4) throw new \Exception('红冲未找到财务商品信息');
  62. if($info['channel']==2 || $info['channel']==3) throw new \Exception('入库未找到财务商品信息');
  63. else{
  64. $product_data=[
  65. 'skuCode'=>$info['goodNo'],
  66. 'goodName'=>$info['goodName'],
  67. 'buyer_name'=>$info['inv_buyer_name'],
  68. 'buyer_code'=>$info['inv_buyer_code'],
  69. 'seller_name'=>$info['inv_seller_name'],
  70. 'seller_code'=>$info['inv_seller_code'],
  71. 'good_source'=>1,
  72. 'good_type'=>$info['goodType'],
  73. 'inv_good_name'=>$info['inv_good_name'],
  74. 'unit'=>$info['inv_unit'],
  75. 'unit_price'=>$info['inv_price'],
  76. 'subunit_price'=>$info['inv_subprice'],
  77. 'unit_weight'=>$info['unit_weight']??0,
  78. 'cat_code'=>$info['inv_cat_code'],
  79. 'cat_tax'=>$info['inv_tax'],
  80. "spec"=>$info['inv_spec'],
  81. 'inv_type'=>$info['inv_type'],
  82. "is_combind"=>0,
  83. 'basic_status'=>1,
  84. 'spectral'=>'',
  85. 'apply_id'=>$info['apply_id'],
  86. 'apply_name'=>$info['apply_name'],
  87. 'status'=>1
  88. ];
  89. $product = FinancialProducts::create($product_data);
  90. }
  91. }
  92. if($product->status!=1) throw new \Exception('商品未启用');
  93. if($product->is_combind==1){
  94. if($info['type']==1) throw new \Exception('入库商品不能为组合商品');
  95. $productID=ProductStock::AddCombindStock($product->id,$info['balance_num']);
  96. }else{
  97. $productID[]= ProductStock::AddSingleStock($product,$info['balance_num']);
  98. }
  99. if(!empty($productID)){
  100. array_map(function ($item)use ($info){
  101. $item['id'] = ManagerProduct::where(['manager_id'=>$info['id'],'product_id'=>$item['product_id']])->value('id');
  102. $item['status']=1;
  103. $item['manager_id'] = $info["id"];
  104. $item['apply_id']=$info['apply_id'];
  105. $item['apply_name']=$info['apply_name'];
  106. (new \app\cxinv\model\ManagerProduct)->save($item);
  107. },$productID);
  108. }
  109. FinancialManager::commit();
  110. $info['balance_num']='0';
  111. $info->status=2;
  112. }catch (\Exception $e){
  113. FinancialManager::rollback();
  114. $info->status=4;
  115. $info->error_remark = $e->getMessage();
  116. }
  117. $info->save();
  118. }
  119. private function getCondition($data){
  120. $where=[];
  121. $childArr= $data->ProductRela->toArray();
  122. if(($data['channel']==1|| $data['channel']==3) && empty($childArr)) {
  123. $where=[
  124. 'skuCode'=>$data['goodNo'],
  125. 'goodName'=>$data['goodName'],
  126. 'good_type'=>$data['goodType'],
  127. 'seller_code'=>$data['inv_seller_code'],
  128. 'buyer_code'=>$data['inv_buyer_code'],
  129. 'inv_good_name'=>$data['inv_good_name'],
  130. 'unit'=>$data['inv_unit'],
  131. 'unit_price'=>$data['inv_price'],
  132. 'subunit_price'=>$data['inv_subprice'],
  133. 'inv_type'=>$data['inv_type'],
  134. 'cat_tax'=>$data['inv_tax'],
  135. 'cat_code'=>$data['inv_cat_code'],
  136. "basic_status"=>1
  137. ];
  138. if($data['type']==2){
  139. $good_source = $data['channel']==2?2:1;
  140. $where=[
  141. ['skuCode',"=",$data['goodNo']],
  142. ['good_type',"=",$data['goodType']],
  143. ['good_source',"=",$good_source],
  144. ['buyer_code',"=",$data['inv_seller_code']],
  145. ['basic_status',"=",1],
  146. ];
  147. }
  148. }
  149. else $where[]=[
  150. 'id','in',array_column($childArr,"product_id")
  151. ];
  152. return $where;
  153. }
  154. public function outManager($data){
  155. FinancialManager::startTrans();
  156. $productID=[];
  157. try{
  158. if(($data['channel'] == 1 || $data['channel']==3) && empty($data->ProductRela->toArray())) {
  159. $product = FinancialProducts::with(['productStock','ProductsCombind'])->where($this->getCondition($data))->findOrEmpty();
  160. if($product->isEmpty()) throw new \Exception('出库未找到财务商品信息');
  161. if($product->status!=1) throw new \Exception('商品未启用');
  162. if($data['balance_num']>$product->residue_stock) throw new \Exception('出库数量大于库存');
  163. $item=['product_id'=>$product->id,'num'=> $data['balance_num']];
  164. $this->processSingleProduct($item, $productID,$data);
  165. } else {
  166. if(floatval($data['balance_num'])!= floatval(array_sum(array_column($data->ProductRela->toArray(), 'num')))) throw new \Exception('出库商品数量不正确');
  167. foreach ($data->ProductRela->toArray() as $item) {
  168. $this->processSingleProduct($item, $productID, $data);
  169. }
  170. }
  171. if(!empty($productID)) {
  172. array_map(function ($item) use ($data) {
  173. $item['id'] = ManagerProduct::where(['manager_id' => $data['id'], 'product_id' => $item['product_id']])->value('id');
  174. $item['status'] = 1;
  175. $item['manager_id'] = $data["id"];
  176. $item['apply_id']=$data['apply_id'];
  177. $item['apply_name']=$data['apply_name'];
  178. (new \app\cxinv\model\ManagerProduct)->save($item);
  179. }, $productID);
  180. }
  181. if($data['balance_num']!=0) throw new \Exception('出库数量不正确');
  182. FinancialManager::commit();
  183. $data['status']=2;
  184. }catch (\Exception $e){
  185. FinancialManager::rollback();
  186. $data['status']=4;
  187. $data['error_remark'] = $e->getMessage();
  188. }
  189. $data->save();
  190. }
  191. private function processSingleProduct($data, &$productID, &$mainData = null) {
  192. $product = FinancialProducts::with(['productStock',"ProductsCombind"])->findOrEmpty($data['product_id']);
  193. if (!$product->isEmpty()) {
  194. if($product->status!=1) throw new \Exception('商品未启用');
  195. if($mainData['type']!=2 && $product->basic_status==2) throw new \Exception($product->skuCode.'商品不可为预估成本商品');
  196. $rednum = $data['num'];
  197. if($mainData['balance_num']< $data['num']) throw new \Exception('出库数量大于订单数量');
  198. $stock = $product->is_combind==1?$product->combind_stock:$product->residue_stock;
  199. if ($stock< $data['num'])throw new \Exception($product->skuCode.'商品库存不足');
  200. $mainData['balance_num'] = bcsub($mainData['balance_num'],$rednum, 8);
  201. if($product->is_combind==1 ){
  202. if($product->ProductsCombind->isEmpty()) throw new \Exception('组合商品未找到明细');
  203. $producttemp=ProductStock::SubCombindStock($product->id, $rednum);
  204. $productID= array_merge($productID,$producttemp);
  205. }else{
  206. $productID[]=ProductStock::SubSingleStock($product, $rednum);
  207. }
  208. } else throw new \Exception('出库未找到财务商品信息');
  209. }
  210. }