<?php
namespace app\cxinv\model;

use think\Model;
class ProductCheck extends Base
{
//设置字段信息
    protected $schema = [
        'id'  =>'bigint',//
        'checkCode'  =>'varchar',//盘点编号
        'product_id'  =>'bigint',//财务商品id
        'check_num'  =>'decimal',//盘点数量
        'diff_num'  =>'decimal',//盈亏数量
        'stock_num'=>'decimal',//盘点库存数量'
        'unit_price'=>'decimal',//单价
        'is_diff'  =>'tinyint',//1盘盈0盘亏
        'check_type'  =>'tinyint',//1数据盘点2调整单
        'fz_date'=>'varchar',//盘点月份
        'status'  =>'tinyint',//0待处理1已盘点2已驳回
        'remark'  =>'varchar',//备注'
        'apply_id'  =>'int',//盘点人
        'apply_name'  =>'varchar',//
        'create_time'  =>'datetime',//
        'update_time'  =>'datetime',//
        'delete_time'  =>'datetime',//
       ];
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    protected $deleteTime='delete_time';
    public function product(){
        return $this->belongsTo('FinancialProducts','product_id','id');
    }

    public static function onAfterInsert(Model $model) : void{
        try{
            $product=FinancialProducts::with(['ProductStock'])->findOrEmpty($model->product_id);
            if($product->isEmpty()) throw new \think\Exception('商品不存在');
            $id = $product->id;
            if($model->is_diff==1 &&is_null($product->residue_stock)){
                    $create=ProductStock::create([
                        'product_id'=>$product->id,
                        'residue_stock'=>$model->check_num,
                        'total_stock'=>$model->check_num,
                    ]);
                    if($create->isEmpty()) throw new \think\Exception($product->goodName.'库存记录创建失败');

            }else {
                if ($product->residue_stock != $model->stock_num) throw new \think\Exception("盘点数量与库存数量不匹配");
                $up = $product->ProductStock->save(['residue_stock' => $model->check_num]);
                if ($up == false) throw new \think\Exception("更新库存失败");
            }
            $data=[
                'type'=>$model->is_diff==1?3:4,
                'order_item_id'=>$model->id,
                'product_id'=>$id,
                'fz_date'=>$model->fz_date,
                'num'=>abs($model->diff_num),
                'unit_price'=>$product->unit_price,
                'rate'=>$product->cat_tax,
                'apply_id'=>$model->apply_id,
                'apply_name'=>$model->apply_name,
            ];
            (new ProductOnlog)->create($data);
            if($model->check_type==2){
                $check=[
                    'checkCode'=>makeNo('CWTZ'),
                    'code'=>$model->checkCode,
                    'itemid'=>$id,
                    "type"=>2,
                    'goodNo'=>$product->skuCode,
                    'goodType'=>$product->good_type,
                    'goodName'=>$product->goodName,
                    'spec'=>$product->spec,
                    'unit'=>$product->unit,
                    'company_code'=>$product->buyer_code,
                    'company_name'=>$product->buyer_name,
                    'num'=>$model->diff_num, // 出库调整金额
                    'price'=>$product->unit_price,
                    'subprice'=>$product->subunit_price,
                    'check_fee'=>$model->diff_fee,
                    'check_uid'=>$model->apply_id,
                    'fz_date'=>$model->fz_date,
                    'check_uname'=>$model->apply_name,
                ];
                FinancialCheck::create($check);
            }
            $model->status=1;
            $model->remark="";
        }catch (\Exception $exception){
            $model->status=2;
            $model->remark=$exception->getMessage();
        }
        $model->save();
    }
}