ProductCheck.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. 'apply_id' =>'int',//盘点人
  18. 'apply_name' =>'varchar',//
  19. 'create_time' =>'datetime',//
  20. 'update_time' =>'datetime',//
  21. 'delete_time' =>'datetime',//
  22. ];
  23. protected $createTime = 'create_time';
  24. protected $updateTime = 'update_time';
  25. protected $deleteTime='delete_time';
  26. public function product(){
  27. return $this->belongsTo('FinancialProducts','product_id','id');
  28. }
  29. public static function onAfterInsert(Model $model) : void{
  30. $product=FinancialProducts::with(['ProductStock'])->findOrEmpty($model->product_id);
  31. if($product->isEmpty()) throw new \think\Exception('商品不存在');
  32. $id = $product->id;
  33. if($model->is_diff==1){
  34. if(is_null($product->residue_stock)){
  35. ProductStock::create([
  36. 'product_id'=>$product->id,
  37. 'residue_stock'=>$model->diff_num,
  38. 'total_stock'=>$model->diff_num,
  39. ]);
  40. }else{
  41. if($product->residue_stock!= bcsub($model->check_num,$model->diff_num,8)) throw new \think\Exception('盘点数量与库存数量不匹配');
  42. $data = $product->toArray();
  43. unset($data['id']);
  44. unset($data['create_time']);
  45. unset($data['update_time']);
  46. $data['apply_id'] = $model->apply_id;
  47. $data['apply_name'] = $model->apply_name;
  48. $new = FinancialProducts::create($data);
  49. if($new->id>0){
  50. ProductStock::create([
  51. 'product_id'=>$new->id,
  52. 'residue_stock'=>$model->diff_num,
  53. 'total_stock'=>$model->diff_num,
  54. ]);
  55. }else throw new \think\Exception('新增商品失败');
  56. $id = $new->id;
  57. }
  58. }else{
  59. if($product->residue_stock!=bcsub($model->check_num,$model->diff_num,8)) throw new \think\Exception("盘点数量与库存数量不匹配");
  60. $up=$product->ProductStock->save(['residue_stock'=>bcadd($product->residue_stock,$model->diff_num,8)]);
  61. if($up==false) throw new \think\Exception("更新库存失败");
  62. }
  63. $data=[
  64. 'type'=>$model->is_diff==1?3:4,
  65. 'order_item_id'=>$model->id,
  66. 'product_id'=>$id,
  67. 'fz_date'=>$model->fz_date,
  68. 'num'=>abs($model->diff_num),
  69. 'unit_price'=>$product->unit_price,
  70. 'rate'=>$product->cat_tax,
  71. 'apply_id'=>$model->apply_id,
  72. 'apply_name'=>$model->apply_name,
  73. ];
  74. (new ProductOnlog)->save($data);
  75. if($model->check_type==2){
  76. $check=[
  77. 'checkCode'=>makeNo('CWTZ'),
  78. 'code'=>$model->checkCode,
  79. 'itemid'=>$id,
  80. "type"=>1,
  81. 'goodNo'=>$product->skuCode,
  82. 'goodType'=>$product->good_type,
  83. 'goodName'=>$product->goodName,
  84. 'spec'=>$product->spec,
  85. 'unit'=>$product->unit,
  86. 'company_code'=>$product->buyer_code,
  87. 'company_name'=>$product->buyer_name,
  88. 'num'=>$model->diff_num, // 出库调整金额
  89. 'price'=>$product->unit_price,
  90. 'subprice'=>$product->subunit_price,
  91. 'check_fee'=>$model->diff_fee,
  92. 'check_uid'=>$model->apply_id,
  93. 'fz_date'=>$model->fz_date,
  94. 'check_uname'=>$model->apply_name,
  95. ];
  96. FinancialCheck::create($check);
  97. }
  98. }
  99. }