StoreOrder.php 3.1 KB

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