InvoiceItem.php 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. public static function rmInvoice($code,$orderType=2){
  54. $items = self::where(['invoiceCode'=>$code,'order_type'=>$orderType])->select();
  55. if(!empty($items)){
  56. $orderItems=InvoiceOrder::whereIn('itemId',$items->column('id'))->select();
  57. if(!empty($orderItems)){
  58. InvoiceOrder::whereIn('itemId',$items->column('id'))->save(["status"=>3]);
  59. }
  60. self::where(['invoiceCode'=>$code,'order_type'=>$orderType])->save(["status"=>3]);
  61. }
  62. }
  63. public static function CopyItem($invNo,$hpNo){
  64. $info = self::where(['invoiceCode'=>$invNo,'order_type'=>1])->select();
  65. if(!$info->isEmpty()){
  66. foreach($info as $item){
  67. $temp = $item->toArray();
  68. $temp['invoiceCode']=$hpNo;
  69. $temp['order_type']=2;
  70. unset($temp['id']);
  71. $up = self::create($temp);
  72. if($up->id>0){
  73. $orders = InvoiceOrder::where(['itemId'=>$item->id,"status"=>[1,2]])->select();
  74. if(!$orders->isEmpty()){
  75. InvoiceOrder::create(array_map(function ($items)use ($up){
  76. $items['itemId']=$up->id;
  77. unset($items['id']);
  78. return $items;
  79. },$orders->toArray()));
  80. }
  81. }
  82. }
  83. }
  84. }
  85. }