TzProduct.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace app\cxinv\model;
  3. use app\cxinv\model\Base;
  4. use think\Model;
  5. class TzProduct extends Base
  6. {
  7. protected $schema = [
  8. 'id' =>'bigint',//
  9. 'ktCode' =>'varchar',//调整id
  10. 'type' =>'tinyint',//1元数据 2调整数据
  11. 'product_id' =>'bigint',//商品id
  12. 'num' =>'decimal',//影响数量
  13. 'unit_price' =>'decimal',//单价
  14. 'create_time' =>'datetime',//
  15. 'update_time' =>'datetime',//
  16. ];
  17. protected $createTime = 'create_time';
  18. protected $updateTime = 'update_time';
  19. public function Product(){
  20. return $this->hasOne(FinancialProducts::class,'id','product_id');
  21. }
  22. public function Adjust(){
  23. return $this->hasOne(FinancialTz::class,'ktCode','ktCode');
  24. }
  25. public static function onAfterInsert(Model $model): void
  26. {
  27. if($model->Product->status!=1) throw new \Exception($model->Product->goodName.'商品已禁用');
  28. if($model['type']==2){
  29. try{
  30. $adjust=$model->Adjust;
  31. if($model->Product->is_combind==1){
  32. if($model->Product->ProductStock->combind_stock<$model['num'])throw new \Exception($model->Product->goodName.'商品库存不足');
  33. $id=ProductStock::SubCombindStock($model->Product->id,$model['num']);
  34. }else{
  35. if($model->Product->ProductStock->residue_stock<$model['num'])throw new \Exception($model->Product->goodName.'商品库存不足');
  36. $id[]=ProductStock::SubSingleStock($model->Product,$model['num']);
  37. }
  38. if(!empty($id)){
  39. foreach ($id as $v){
  40. $log[] = [
  41. "type" => 6,
  42. 'order_item_id' => $adjust->id,
  43. 'product_id' => $v['product_id'],
  44. 'num' => $v['num'],
  45. "fz_date" => $adjust->fz_date,
  46. 'unit_price' => $v['unit_price'],
  47. 'rate' => $v['rate'],
  48. 'apply_id' => $model->checkUid,
  49. 'apply_name' => $model->checkUname,
  50. ];
  51. }
  52. (new ProductOnlog)->saveAll($log);
  53. }
  54. }catch (\Exception $e){
  55. throw new \Exception($e->getMessage());
  56. }
  57. }
  58. }
  59. }