ProductCheck.php 3.9 KB

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