123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- <?php
- namespace app\cxinv\model;
- use think\facade\Log;use think\Model;
- class PayInvoice extends Base
- {
- protected $schema = [
- 'id' =>'int',
- 'payNo' =>'varchar',
- 'hpNo' =>'varchar',
- 'apply_id' =>'int',
- 'apply_name' =>'varchar',
- 'invType' =>'tinyint',
- 'invoiceType' =>'varchar',
- 'invName' =>'varchar',
- 'inv_img' =>'varchar',
- 'inv_fee' =>'decimal',
- 'inv_subtotal_amount' =>'decimal',
- 'inv_amount' =>'decimal',
- 'invStatus' =>'tinyint',
- 'status' =>'tinyint',
- 'remark' =>'varchar',
- 'exam_remark' =>'varchar',
- 'check_remark' =>'varchar',
- 'tag_id' =>'int',
- 'check_time' =>'varchar',
- 'open_time' =>'varchar',
- 'invoiceNumber' =>'varchar',
- 'seller_check' =>'tinyint',
- 'buyer_check' =>'tinyint',
- 'invoiceCode' =>'varchar',
- 'checkNumber' =>'varchar',
- 'checkApi' =>'tinyint',
- 'is_del' =>'tinyint',
- 'addtime' =>'datetime',
- 'updatetime' =>'datetime',
- ];
- protected $createTime='addtime';
- protected $updateTime='updatetime';
- public function Items(){
- return $this->hasMany(InvoiceItem::class,'invoiceCode','hpNo');
- }
- public static function onAfterInsert(Model $model) : void{
- if ($model->invType==4){
- $ticket= InvoiceInfo::where("payNo",$model->payNo)->findOrEmpty();
- if (!$ticket->isEmpty() && !empty($ticket->item_list)){
- $bool = true;
- (new InvoiceItem)->saveAll(array_map(function ($item)use($model,&$bool) {
- if($item['amount']<0) $bool=false;
- return [
- 'invoiceCode'=>$model->hpNo,
- 'order_type'=>2,
- 'good_name'=>$item['name'],
- 'unit'=>$item['unit'],
- 'specinfo'=>$item['specification'],
- 'num'=>$item['quantity'],
- 'unit_price'=>$item['unit_price'],
- 'amount'=>$item['amount'],
- 'tax_amount'=>$item['tax'],
- 'tax'=>$item['tax_rate'],
- 'cat_code'=>$item['license_plate_number'],
- 'total_amount'=>bcadd($item['amount'],$item['tax'],2),
- 'balance_amount'=>bcadd($item['amount'],$item['tax'],2),
- ];
- },$ticket->item_list));
- $ticket->hpNo= $model->hpNo;
- $ticket->save();
- if($bool==false){
- $model->status=14;
- $model->save();
- }
- }
- }
- }
- public static function onAfterUpdate(Model $model) : void{
- Log::info('PayInv回票状态变更onAfterUpdate:model'.json_encode($model->toArray()));
- if ($model->invType==4){
- $change = $model->getChangedData();
- if (isset($change['status'])){
- Log::info("PayInv回票状态变更onAfterUpdate:change".json_encode($change));
- $invocie = Invoice::where("InvCode",$model->payNo)->findOrEmpty();
- if($change['status']==4){
- if (!$invocie->isEmpty()){
- $invocie->status=4;
- $invocie->save();
- }
- $itemid= InvoiceItem::where(['invoiceCode'=>$model->hpNo,'status'=>[1,2]])->column('id');
- if(!empty($itemid)){
- $invo= InvoiceOrder::whereIn('itemId',$itemid)->where(['status'=>[1,2]])->column('code,balance_amount,total_amount');
- if(!empty($invo)){
- foreach ($invo as $key=>$val){
- InvoiceOrder::orderFinish($val['code'],bcsub($val['total_amount'],$val['balance_amount'],2));
- }
- }
- }
- }
- if($change['status']==10){
- Log::info("回票终止,退款开始".$model->hpNo);
- InvoiceItem::refund($model->hpNo);
- if (!$invocie->isEmpty()){
- $invocie->status=2;
- $invocie->save();
- }
- }
- if (in_array($change['status'],[5,6,7,8,13])){
- InvoiceItem::rmInvoice($model->hpNo);
- if (!$invocie->isEmpty()){
- $invocie->status=2;
- $invocie->save();
- }
- }
- }
- }
- }
- }
|