InvoiceItem.php 4.2 KB

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