StoreOrder.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. class StoreOrder extends BaseModel
  7. {
  8. use HasFactory;
  9. protected $table = 'store_order';
  10. public function user(){
  11. return $this->hasOne(User::class, 'id', 'user_id');
  12. }
  13. public function client(){
  14. return $this->hasOne(User::class, 'id', 'user_id');
  15. }
  16. public function paymentMethod(){
  17. return $this->hasOne(PaymentMethod::class, 'id', 'payment_method_id');
  18. }
  19. public function financialTransactions(){
  20. return $this->hasMany(FinancialTransaction::class, 'order_id', 'id');
  21. }
  22. public function finalFinancialTransaction(){
  23. return FinancialTransaction::where('order_id', $this->id)->orderBy('created_at', 'DESC')->first();
  24. }
  25. public function orderNumber() {
  26. if ($this->iid) return $this->iid;
  27. return getFirstSectionUID($this->uid);
  28. }
  29. public function tests(){
  30. $detail = json_decode($this->detail_json);
  31. $selectedOptions = json_decode(@$detail->selected_options);
  32. if(!$selectedOptions) return null;
  33. return (array) @$selectedOptions->tests;
  34. }
  35. public function total(){
  36. $detail = json_decode($this->detail_json);
  37. $selectedOptions = json_decode(@$detail->selected_options);
  38. if(!$selectedOptions) return 0;
  39. return floatval(@$selectedOptions->tests_total);
  40. }
  41. public function lab(){
  42. $detail = json_decode($this->detail_json);
  43. $selectedOptions = json_decode(@$detail->selected_options);
  44. $labID = @$selectedOptions->tests_lab_id;
  45. if(!$labID) return null;
  46. return Lab::where('id', $labID)->first();
  47. }
  48. public function createdByUser(){
  49. return $this->hasOne(User::class, 'id', 'created_by_user_id');
  50. }
  51. }