ProductCheck.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace app\cxinv\model;
  3. use think\Model;class ProductCheck extends Base
  4. {
  5. //设置字段信息
  6. protected $schema = [
  7. 'id' =>'bigint',//
  8. 'checkCode' =>'varchar',//盘点编号
  9. 'product_id' =>'bigint',//财务商品id
  10. 'check_num' =>'decimal',//盘点数量
  11. 'diff_num' =>'decimal',//盈亏数量
  12. 'stock_num'=>'decimal',//盘点库存数量'
  13. 'unit_price'=>'decimal',//单价
  14. 'is_diff' =>'tinyint',//1盘盈0盘亏
  15. 'check_type' =>'tinyint',//1数据盘点2调整单
  16. 'fz_date'=>'varchar',//盘点月份
  17. 'status' =>'tinyint',//0待处理1已盘点2已驳回
  18. 'remark' =>'varchar',//备注'
  19. 'apply_id' =>'int',//盘点人
  20. 'apply_name' =>'varchar',//
  21. 'create_time' =>'datetime',//
  22. 'update_time' =>'datetime',//
  23. 'delete_time' =>'datetime',//
  24. ];
  25. protected $createTime = 'create_time';
  26. protected $updateTime = 'update_time';
  27. protected $deleteTime='delete_time';
  28. public function product(){
  29. return $this->belongsTo('FinancialProducts','product_id','id');
  30. }
  31. public static function onAfterInsert(Model $model) : void{
  32. try{
  33. $product=FinancialProducts::with(['ProductStock'])->findOrEmpty($model->product_id);
  34. if($product->isEmpty()) throw new \think\Exception('商品不存在');
  35. $id = $product->id;
  36. if($model->is_diff==1){
  37. if(is_null($product->residue_stock)){
  38. $create=ProductStock::create([
  39. 'product_id'=>$product->id,
  40. 'residue_stock'=>$model->check_num,
  41. 'total_stock'=>$model->check_num,
  42. ]);
  43. if($create->isEmpty()) throw new \think\Exception($product->goodName.'库存记录创建失败');
  44. }else{
  45. if($product->residue_stock!= $model->stock_num) throw new \think\Exception($product->goodName.'盘点数量与库存数量不匹配');
  46. $data = $product->toArray();
  47. unset($data['id']);
  48. unset($data['create_time']);
  49. unset($data['update_time']);
  50. $data['apply_id'] = $model->apply_id;
  51. $data['apply_name'] = $model->apply_name;
  52. $new = FinancialProducts::create($data);
  53. if($new->id>0){
  54. $create=ProductStock::create([
  55. 'product_id'=>$new->id,
  56. 'residue_stock'=>$model->check_num,
  57. 'total_stock'=>$model->check_num,
  58. ]);
  59. if($create->isEmpty()) throw new \think\Exception($product->goodName.'库存记录创建失败');
  60. }else throw new \think\Exception('复制商品失败');
  61. $id = $new->id;
  62. }
  63. }else{
  64. if($product->residue_stock!=$model->stock_num) throw new \think\Exception("盘点数量与库存数量不匹配");
  65. $up=$product->ProductStock->save(['residue_stock'=>$model->check_num]);
  66. if($up==false) throw new \think\Exception("更新库存失败");
  67. }
  68. $data=[
  69. 'type'=>$model->is_diff==1?3:4,
  70. 'order_item_id'=>$model->id,
  71. 'product_id'=>$id,
  72. 'fz_date'=>$model->fz_date,
  73. 'num'=>abs($model->diff_num),
  74. 'unit_price'=>$product->unit_price,
  75. 'rate'=>$product->cat_tax,
  76. 'apply_id'=>$model->apply_id,
  77. 'apply_name'=>$model->apply_name,
  78. ];
  79. (new ProductOnlog)->create($data);
  80. if($model->check_type==2){
  81. $check=[
  82. 'checkCode'=>makeNo('CWTZ'),
  83. 'code'=>$model->checkCode,
  84. 'itemid'=>$id,
  85. "type"=>1,
  86. 'goodNo'=>$product->skuCode,
  87. 'goodType'=>$product->good_type,
  88. 'goodName'=>$product->goodName,
  89. 'spec'=>$product->spec,
  90. 'unit'=>$product->unit,
  91. 'company_code'=>$product->buyer_code,
  92. 'company_name'=>$product->buyer_name,
  93. 'num'=>$model->diff_num, // 出库调整金额
  94. 'price'=>$product->unit_price,
  95. 'subprice'=>$product->subunit_price,
  96. 'check_fee'=>$model->diff_fee,
  97. 'check_uid'=>$model->apply_id,
  98. 'fz_date'=>$model->fz_date,
  99. 'check_uname'=>$model->apply_name,
  100. ];
  101. FinancialCheck::create($check);
  102. }
  103. $model->status=1;
  104. $model->remark="";
  105. }catch (\Exception $exception){
  106. $model->status=2;
  107. $model->remark=$exception->getMessage();
  108. }
  109. $model->save();
  110. }
  111. }