StoreOrder.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 paymentMethod(){
  19. return $this->hasOne(PaymentMethod::class, 'id', 'payment_method_id');
  20. }
  21. public function financialTransactions(){
  22. return $this->hasMany(FinancialTransaction::class, 'order_id', 'id');
  23. }
  24. public function finalFinancialTransaction(){
  25. return FinancialTransaction::where('order_id', $this->id)->orderBy('created_at', 'DESC')->first();
  26. }
  27. public function orderNumber() {
  28. if ($this->iid) return $this->iid;
  29. return getFirstSectionUID($this->uid);
  30. }
  31. public function tests(){
  32. $detail = json_decode($this->detail_json);
  33. $selectedOptions = json_decode(@$detail->selected_options);
  34. if(!$selectedOptions) return null;
  35. return (array) @$selectedOptions->tests;
  36. }
  37. public function testsRequested(){
  38. $tests = $this->tests();
  39. $data = [];
  40. foreach($tests as $key=>$value){
  41. if($value){
  42. array_push($data, $key);
  43. }
  44. }
  45. return $data;
  46. }
  47. public function testsRequestedInHumanReadable(){
  48. $data = [];
  49. $tests = $this->testsRequested();
  50. foreach($tests as $test){
  51. $testName = config('constants.tests.'.$test);
  52. $testPrice = displayAmount('$', config('app.'.$test));
  53. $string = $testName . ' - '.$testPrice;
  54. array_push($data, $string);
  55. }
  56. return $data;
  57. }
  58. public function total(){
  59. $detail = json_decode($this->detail_json);
  60. $selectedOptions = json_decode(@$detail->selected_options);
  61. if(!$selectedOptions) return 0;
  62. return floatval(@$selectedOptions->tests_total);
  63. }
  64. public function lab(){
  65. $detail = json_decode($this->detail_json);
  66. $selectedOptions = json_decode(@$detail->selected_options);
  67. $labID = @$selectedOptions->tests_lab_id;
  68. if(!$labID) return null;
  69. return Lab::where('id', $labID)->first();
  70. }
  71. public function createdByUser(){
  72. return $this->hasOne(User::class, 'id', 'created_by_user_id');
  73. }
  74. public function detailJson($toArray = false)
  75. {
  76. if($toArray){
  77. return json_decode($this->detail_json ?? '{}', true);
  78. }
  79. return json_decode($this->detail_json ?? '{}');
  80. }
  81. public function getDetailJsonValue($field)
  82. {
  83. $parsed = $this->detailJson(true);
  84. if (isset($parsed[$field])) {
  85. return $parsed[$field];
  86. }
  87. return null;
  88. }
  89. }