1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- <?php
- namespace app\cxinv\model;
- use app\cxinv\model\Base;
- use think\Model;
- class TzProduct extends Base
- {
- protected $schema = [
- 'id' =>'bigint',//
- 'ktCode' =>'varchar',//调整id
- 'type' =>'tinyint',//1元数据 2调整数据
- 'product_id' =>'bigint',//商品id
- 'num' =>'decimal',//影响数量
- 'unit_price' =>'decimal',//单价
- 'create_time' =>'datetime',//
- 'update_time' =>'datetime',//
- ];
- protected $createTime = 'create_time';
- protected $updateTime = 'update_time';
- public function Product(){
- return $this->hasOne(FinancialProducts::class,'id','product_id');
- }
- public function Adjust(){
- return $this->hasOne(FinancialTz::class,'ktCode','ktCode');
- }
- public static function onAfterInsert(Model $model): void
- {
- if($model->Product->status!=1) throw new \Exception($model->Product->goodName.'商品已禁用');
- if($model['type']==2){
- try{
- $adjust=$model->Adjust;
- if($model->Product->is_combind==1){
- if($model->Product->ProductStock->combind_stock<$model['num'])throw new \Exception($model->Product->goodName.'商品库存不足');
- $id=ProductStock::SubCombindStock($model->Product->id,$model['num']);
- }else{
- if($model->Product->ProductStock->residue_stock<$model['num'])throw new \Exception($model->Product->goodName.'商品库存不足');
- $id[]=ProductStock::SubSingleStock($model->Product,$model['num']);
- }
- if(!empty($id)){
- foreach ($id as $v){
- $log[] = [
- "type" => 6,
- 'order_item_id' => $adjust->id,
- 'product_id' => $v['product_id'],
- 'num' => $v['num'],
- "fz_date" => $adjust->fz_date,
- 'unit_price' => $v['unit_price'],
- 'rate' => $v['rate'],
- 'apply_id' => $model->checkUid,
- 'apply_name' => $model->checkUname,
- ];
- }
- (new ProductOnlog)->saveAll($log);
- }
- }catch (\Exception $e){
- throw new \Exception($e->getMessage());
- }
- }
- }
- }
|