StoreOrder.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 [];
  43. if($selectedOptions->partner != 1) return [];
  44. return (array) @$selectedOptions->tests;
  45. }
  46. public function partnerTests(){
  47. $detail = json_decode($this->detail_json);
  48. $selectedOptions = json_decode(@$detail->selected_options);
  49. if(!$selectedOptions) return [];
  50. return (array) @$selectedOptions->tests;
  51. }
  52. public function testsRequested(){
  53. $tests = (array) $this->tests();
  54. $data = [];
  55. foreach($tests as $key=>$value){
  56. if($value){
  57. array_push($data, $key);
  58. }
  59. }
  60. return $data;
  61. }
  62. public function partnerTestsRequested(){
  63. $tests = (array) $this->partnerTests();
  64. $data = [];
  65. foreach($tests as $key=>$value){
  66. if($value){
  67. array_push($data, $key);
  68. }
  69. }
  70. return $data;
  71. }
  72. public function testsRequestedInHumanReadable(){
  73. $data = [];
  74. $tests = $this->testsRequested();
  75. foreach($tests as $test){
  76. $testName = config('constants.tests.'.$test);
  77. $testPrice = displayAmount('$', config('app.'.$test));
  78. $string = $testName . ' - '.$testPrice;
  79. array_push($data, $string);
  80. }
  81. return $data;
  82. }
  83. public function partnerTestsRequestedInHumanReadable(){
  84. $data = [];
  85. $tests = $this->partnerTestsRequested();
  86. $discount = $this->getPartnerTestDiscount();
  87. foreach($tests as $test){
  88. $testName = config('constants.tests.'.$test);
  89. $testPrice = floatval(config('app.'.$test));
  90. $testCost = $testPrice - $discount;
  91. $string = $testName . ' - '. displayAmount('$', $testCost);
  92. array_push($data, $string);
  93. }
  94. return $data;
  95. }
  96. public function getPartnerTestDiscount(){
  97. $detail = json_decode($this->detail_json);
  98. $selectedOptions = json_decode(@$detail->selected_options);
  99. if(!$selectedOptions) return 0;
  100. if(!$selectedOptions->partner_test_discount_amount) return 0;
  101. return floatval($selectedOptions->partner_test_discount_amount);
  102. }
  103. public function getPartnerEmail(){
  104. $detail = json_decode($this->detail_json);
  105. $selectedOptions = json_decode(@$detail->selected_options);
  106. if(!$selectedOptions) return null;
  107. if(!$selectedOptions->partner_email) return null;
  108. return $selectedOptions->partner_email;
  109. }
  110. public function total(){
  111. $total = $this->order_total;
  112. $orders = $this->associatedOrders($this->id);
  113. foreach($orders as $order){
  114. $total = $total + $order->order_total;
  115. }
  116. return $total;
  117. }
  118. public function lab(){
  119. $detail = json_decode($this->detail_json);
  120. $selectedOptions = json_decode(@$detail->selected_options);
  121. $labID = @$selectedOptions->tests_lab_id;
  122. if(!$labID) return null;
  123. return Lab::where('id', $labID)->first();
  124. }
  125. public function createdByUser(){
  126. return $this->hasOne(User::class, 'id', 'created_by_user_id');
  127. }
  128. public function detailJson($toArray = false)
  129. {
  130. if($toArray){
  131. return json_decode($this->detail_json ?? '{}', true);
  132. }
  133. return json_decode($this->detail_json ?? '{}');
  134. }
  135. public function getDetailJsonValue($field)
  136. {
  137. $parsed = $this->detailJson(true);
  138. if (isset($parsed[$field])) {
  139. return $parsed[$field];
  140. }
  141. return null;
  142. }
  143. }