123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- <?php
- namespace app\admin\model;
- use think\facade\Log;use think\model\concern\SoftDelete;
- class InvoiceItem extends \think\Model {
- use SoftDelete;
- protected $createTime="createTime";
- protected $updateTime="updateTime";
- protected $autoWriteTimestamp=true;
- protected $deleteTime="delete_time";
- public function OrderInfo(){
- return $this->hasMany(InvoiceOrder::class,"itemId","id")->whereIn('status', [1, 2]);
- }
- public static function getNumAttr($val){
- return strval(floatval($val));
- }
- public static function setNumAttr($val){
- return floatval($val);
- }
- public static function onAfterUpdate($model){
- $code=$model->invoiceCode;
- $orderType=$model->order_type;
- $change = $model->getChangedData();
- Log::info("修改发票明细状态:".json_encode($change,JSON_UNESCAPED_UNICODE));
- Log::info("跟后数据:".json_encode($model->toArray(),JSON_UNESCAPED_UNICODE));
- if(isset($change['status'])&&in_array($change["status"],[0,1,2])){
- Log::info('跟后数据:'.json_encode($model->toArray(),JSON_UNESCAPED_UNICODE));
- $num = self::where(['invoiceCode'=>$code,'order_type'=>$orderType,'status'=>0])->count();
- // if($orderType==1){
- // $info= InvoicePool::where(['invNo'=>$code,'is_del'=>0])->findOrEmpty();
- // if(!$info->isEmpty()){
- // if($num==0 && $info->status==10){
- // $info->status=11;
- // }
- // if($num>0 && $info->status==11){
- // $info->status=10;
- // }
- // $info->save();
- // }
- // }
- if($orderType==2){
- $info= PayInvoice::where(['hpNo'=>$code,'is_del'=>0])->findOrEmpty();
- Log::info('跟后数据:'.json_encode( $info->toArray(),JSON_UNESCAPED_UNICODE));
- if(!$info->isEmpty()){
- $pay = Pay::where(['payNo'=>$info->payNo,'is_del'=>0])->findOrEmpty();
- Log::info('跟后数据:'.json_encode( $pay->toArray(),JSON_UNESCAPED_UNICODE));
- if(!$pay->isEmpty() && $pay->status==2){
- if($num==0 && $info->status==11){
- $info->status=12;
- }
- if($num>0 && $info->status==12){
- $info->status=11;
- }
- $info->save();
- }
- }
- }
- }
- }
- public static function rmInvoice($code,$orderType=2){
- $items = self::where(['invoiceCode'=>$code,'order_type'=>$orderType])->select();
- if(!empty($items)){
- $orderItems=InvoiceOrder::whereIn('itemId',$items->column('id'))->select();
- if(!empty($orderItems)){
- InvoiceOrder::whereIn('itemId',$items->column('id'))->save(["status"=>3]);
- }
- self::where(['invoiceCode'=>$code,'order_type'=>$orderType])->save(["status"=>3]);
- }
- }
- public static function CopyItem($invNo,$hpNo){
- $info = self::where(['invoiceCode'=>$invNo,'order_type'=>1])->select();
- if(!$info->isEmpty()){
- foreach($info as $item){
- $temp = $item->toArray();
- $temp['invoiceCode']=$hpNo;
- $temp['order_type']=2;
- unset($temp['id']);
- $up = self::create($temp);
- if($up->id>0){
- $orders = InvoiceOrder::where(['itemId'=>$item->id,"status"=>[1,2]])->select();
- if(!$orders->isEmpty()){
- InvoiceOrder::create(array_map(function ($items)use ($up){
- $items['itemId']=$up->id;
- unset($items['id']);
- return $items;
- },$orders->toArray()));
- }
- }
- }
- }
- }
- }
|