InvoiceItem.php 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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");
  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(in_array($change["status"],[0,1,2])){
  26. $num = self::where(['invoiceCode'=>$code,'order_type'=>$orderType,'status'=>0])->count();
  27. if($orderType==1){
  28. $info= InvoicePool::where(['invNo'=>$code,'is_del'=>0])->findOrEmpty();
  29. if(!$info->isEmpty()){
  30. if($num==0 && $info->status==10){
  31. $info->status=11;
  32. }
  33. if($num>0 && $info->status==11){
  34. $info->status=10;
  35. }
  36. $info->save();
  37. }
  38. }
  39. if($orderType==2){
  40. $info= PayInvoice::where(['hpNo'=>$code,'is_del'=>0])->findOrEmpty();
  41. if(!$info->isEmpty()){
  42. $pay = Pay::where(['payNo'=>$info->payNo,'is_del'=>0])->findOrEmpty();
  43. if(!$pay->isEmpty() && $pay->status==2){
  44. if($num==0 && $info->status==11){
  45. $info->status=12;
  46. }
  47. if($num>0 && $info->status==12){
  48. $info->status=11;
  49. }
  50. $info->save();
  51. }
  52. }
  53. }
  54. }
  55. }
  56. public static function rmInvoice($code,$orderType){
  57. $items = self::where(['invoiceCode'=>$code,'order_type'=>$orderType])->select();
  58. if(!empty($items)){
  59. $orderItems=InvoiceOrder::whereIn('itemId',$items->column('id'))->select();
  60. if(!empty($orderItems)){
  61. InvoiceOrder::whereIn('itemId',$items->column('id'))->save(["status"=>3]);
  62. }
  63. self::where(['invoiceCode'=>$code,'order_type'=>$orderType])->save(["status"=>3]);
  64. }
  65. }
  66. public static function CopyItem($invNo,$hpNo){
  67. $info = self::where(['invoiceCode'=>$invNo,'order_type'=>1])->select();
  68. if(!$info->isEmpty()){
  69. foreach($info as $item){
  70. $temp = $item->toArray();
  71. $temp['invoiceCode']=$hpNo;
  72. $temp['order_type']=2;
  73. unset($temp['id']);
  74. $up = self::create($temp);
  75. if($up->id>0){
  76. $orders = InvoiceOrder::where(['itemId'=>$item->id])->select();
  77. if(!$orders->isEmpty()){
  78. InvoiceOrder::create(array_map(function ($items)use ($up){
  79. $items['itemId']=$up->id;
  80. unset($items['id']);
  81. return $item;
  82. },$orders->toArray()));
  83. }
  84. }
  85. }
  86. }
  87. }
  88. }