StoreOrder.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. array_push($data, config('constants.tests.'.$test));
  51. }
  52. return $data;
  53. }
  54. public function total(){
  55. $detail = json_decode($this->detail_json);
  56. $selectedOptions = json_decode(@$detail->selected_options);
  57. if(!$selectedOptions) return 0;
  58. return floatval(@$selectedOptions->tests_total);
  59. }
  60. public function lab(){
  61. $detail = json_decode($this->detail_json);
  62. $selectedOptions = json_decode(@$detail->selected_options);
  63. $labID = @$selectedOptions->tests_lab_id;
  64. if(!$labID) return null;
  65. return Lab::where('id', $labID)->first();
  66. }
  67. public function createdByUser(){
  68. return $this->hasOne(User::class, 'id', 'created_by_user_id');
  69. }
  70. }