StoreOrder.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Factories\HasFactory;
  4. use Illuminate\Database\Eloquent\Model;
  5. use App\Models\BaseModel;
  6. use Illuminate\Support\Arr;
  7. class StoreOrder extends BaseModel
  8. {
  9. use HasFactory;
  10. protected $table = 'store_order';
  11. public $timestamps = false;
  12. public function user(){
  13. return $this->hasOne(User::class, 'id', 'user_id');
  14. }
  15. public function client(){
  16. return $this->hasOne(User::class, 'id', 'user_id');
  17. }
  18. public function associatedOrders($parentOrderId){
  19. return StoreOrder::where('parent_order_id', $parentOrderId)->get();
  20. }
  21. public function allAssociatedOrders(){
  22. $orders = StoreOrder::where('parent_order_id', $this->id)->get();
  23. $orders->prepend($this);
  24. return $orders;
  25. }
  26. public function paymentMethod(){
  27. return $this->hasOne(PaymentMethod::class, 'id', 'payment_method_id');
  28. }
  29. public function financialTransactions(){
  30. return $this->hasMany(FinancialTransaction::class, 'order_id', 'id');
  31. }
  32. public function finalFinancialTransaction(){
  33. return FinancialTransaction::where('order_id', $this->id)->orderBy('created_at', 'DESC')->first();
  34. }
  35. public function orderNumber() {
  36. if ($this->iid) return $this->iid;
  37. return getFirstSectionUID($this->uid);
  38. }
  39. public function tests(){
  40. $detail = json_decode($this->detail_json);
  41. $selectedOptions = json_decode(@$detail->selected_options);
  42. if(!$selectedOptions) return null;
  43. return (array) @$selectedOptions->tests;
  44. }
  45. public function testsRequested(){
  46. $tests = $this->tests();
  47. $data = [];
  48. foreach($tests as $key=>$value){
  49. if($value){
  50. array_push($data, $key);
  51. }
  52. }
  53. return $data;
  54. }
  55. public function testsRequestedInHumanReadable(){
  56. $data = [];
  57. $tests = $this->testsRequested();
  58. foreach($tests as $test){
  59. $testName = config('constants.tests.'.$test);
  60. $testPrice = displayAmount('$', config('app.'.$test));
  61. $string = $testName . ' - '.$testPrice;
  62. array_push($data, $string);
  63. }
  64. return $data;
  65. }
  66. public function total(){
  67. $total = $this->order_total;
  68. $orders = $this->associatedOrders($this->id);
  69. foreach($orders as $order){
  70. $total = $total + $order->order_total;
  71. }
  72. return $total;
  73. }
  74. public function lab(){
  75. $detail = json_decode($this->detail_json);
  76. $selectedOptions = json_decode(@$detail->selected_options);
  77. $labID = @$selectedOptions->tests_lab_id;
  78. if(!$labID) return null;
  79. return Lab::where('id', $labID)->first();
  80. }
  81. public function createdByUser(){
  82. return $this->hasOne(User::class, 'id', 'created_by_user_id');
  83. }
  84. public function detailJson($toArray = false)
  85. {
  86. if($toArray){
  87. return json_decode($this->detail_json ?? '{}', true);
  88. }
  89. return json_decode($this->detail_json ?? '{}');
  90. }
  91. public function getDetailJsonValue($field)
  92. {
  93. $parsed = $this->detailJson(true);
  94. if (isset($parsed[$field])) {
  95. return $parsed[$field];
  96. }
  97. return null;
  98. }
  99. }