StoreOrder.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 function user(){
  12. return $this->hasOne(User::class, 'id', 'user_id');
  13. }
  14. public function client(){
  15. return $this->hasOne(User::class, 'id', 'user_id');
  16. }
  17. public function paymentMethod(){
  18. return $this->hasOne(PaymentMethod::class, 'id', 'payment_method_id');
  19. }
  20. public function financialTransactions(){
  21. return $this->hasMany(FinancialTransaction::class, 'order_id', 'id');
  22. }
  23. public function finalFinancialTransaction(){
  24. return FinancialTransaction::where('order_id', $this->id)->orderBy('created_at', 'DESC')->first();
  25. }
  26. public function orderNumber() {
  27. if ($this->iid) return $this->iid;
  28. return getFirstSectionUID($this->uid);
  29. }
  30. public function tests(){
  31. $detail = json_decode($this->detail_json);
  32. $selectedOptions = json_decode(@$detail->selected_options);
  33. if(!$selectedOptions) return null;
  34. return (array) @$selectedOptions->tests;
  35. }
  36. public function testsRequested(){
  37. $tests = $this->tests();
  38. $data = [];
  39. foreach($tests as $key=>$value){
  40. if($value){
  41. array_push($data, $key);
  42. }
  43. }
  44. return $data;
  45. }
  46. public function testsRequestedInHumanReadable(){
  47. $data = [];
  48. $tests = $this->testsRequested();
  49. foreach($tests as $test){
  50. $testName = config('constants.tests.'.$test);
  51. $testPrice = displayAmount('$', config('app.'.$test));
  52. $string = $testName . ' - '.$testPrice;
  53. array_push($data, $string);
  54. }
  55. return $data;
  56. }
  57. public function total(){
  58. $detail = json_decode($this->detail_json);
  59. $selectedOptions = json_decode(@$detail->selected_options);
  60. if(!$selectedOptions) return 0;
  61. return floatval(@$selectedOptions->tests_total);
  62. }
  63. public function lab(){
  64. $detail = json_decode($this->detail_json);
  65. $selectedOptions = json_decode(@$detail->selected_options);
  66. $labID = @$selectedOptions->tests_lab_id;
  67. if(!$labID) return null;
  68. return Lab::where('id', $labID)->first();
  69. }
  70. public function createdByUser(){
  71. return $this->hasOne(User::class, 'id', 'created_by_user_id');
  72. }
  73. }