InvoiceItem.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace app\admin\model;
  3. use think\facade\Log;use think\model\concern\SoftDelete;
  4. class InvoiceItem extends \think\Model {
  5. use SoftDelete;
  6. protected $createTime="createTime";
  7. protected $updateTime="updateTime";
  8. protected $autoWriteTimestamp=true;
  9. protected $deleteTime="delete_time";
  10. public function OrderInfo(){
  11. return $this->hasMany(InvoiceOrder::class,"itemId","id")->whereIn('status', [1, 2]);
  12. }
  13. public static function getNumAttr($val){
  14. return strval(floatval($val));
  15. }
  16. public static function setNumAttr($val){
  17. return floatval($val);
  18. }
  19. public static function onAfterUpdate($model){
  20. $code=$model->invoiceCode;
  21. $orderType=$model->order_type;
  22. $change = $model->getChangedData();
  23. Log::info("修改发票明细状态:".json_encode($change,JSON_UNESCAPED_UNICODE));
  24. Log::info("跟后数据:".json_encode($model->toArray(),JSON_UNESCAPED_UNICODE));
  25. if(isset($change['status'])&&in_array($change["status"],[0,1,2])){
  26. Log::info('跟后数据:'.json_encode($model->toArray(),JSON_UNESCAPED_UNICODE));
  27. $num = self::where(['invoiceCode'=>$code,'order_type'=>$orderType,'status'=>0])->count();
  28. // if($orderType==1){
  29. // $info= InvoicePool::where(['invNo'=>$code,'is_del'=>0])->findOrEmpty();
  30. // if(!$info->isEmpty()){
  31. // if($num==0 && $info->status==10){
  32. // $info->status=11;
  33. // }
  34. // if($num>0 && $info->status==11){
  35. // $info->status=10;
  36. // }
  37. // $info->save();
  38. // }
  39. // }
  40. if($orderType==2){
  41. $info= PayInvoice::where(['hpNo'=>$code,'is_del'=>0])->findOrEmpty();
  42. Log::info('跟后数据:'.json_encode( $info->toArray(),JSON_UNESCAPED_UNICODE));
  43. if(!$info->isEmpty()){
  44. $pay = Pay::where(['payNo'=>$info->payNo,'is_del'=>0])->findOrEmpty();
  45. Log::info('跟后数据:'.json_encode( $pay->toArray(),JSON_UNESCAPED_UNICODE));
  46. if(!$pay->isEmpty() && $pay->status==2){
  47. if($num==0 && $info->status==11){
  48. $info->status=12;
  49. }
  50. if($num>0 && $info->status==12){
  51. $info->status=11;
  52. }
  53. $info->save();
  54. }
  55. }
  56. }
  57. }
  58. }
  59. public static function rmInvoice($code,$orderType=2){
  60. $items = self::where(['invoiceCode'=>$code,'order_type'=>$orderType])->select();
  61. if(!empty($items)){
  62. $orderItems=InvoiceOrder::whereIn('itemId',$items->column('id'))->select();
  63. if(!empty($orderItems)){
  64. InvoiceOrder::whereIn('itemId',$items->column('id'))->save(["status"=>3]);
  65. }
  66. self::where(['invoiceCode'=>$code,'order_type'=>$orderType])->save(["status"=>3]);
  67. }
  68. }
  69. public static function CopyItem($invNo,$hpNo){
  70. $info = self::where(['invoiceCode'=>$invNo,'order_type'=>1])->select();
  71. if(!$info->isEmpty()){
  72. foreach($info as $item){
  73. $temp = $item->toArray();
  74. $temp['invoiceCode']=$hpNo;
  75. $temp['order_type']=2;
  76. unset($temp['id']);
  77. $up = self::create($temp);
  78. if($up->id>0){
  79. $orders = InvoiceOrder::where(['itemId'=>$item->id,"status"=>[1,2]])->select();
  80. if(!$orders->isEmpty()){
  81. InvoiceOrder::create(array_map(function ($items)use ($up){
  82. $items['itemId']=$up->id;
  83. unset($items['id']);
  84. return $items;
  85. },$orders->toArray()));
  86. }
  87. }
  88. }
  89. }
  90. }
  91. }