StoreOrder.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 allAssociatedOrders(){
  22. $orders = StoreOrder::where('parent_order_id', $this->id)->get();
  23. $orders->prepend($this);
  24. return $orders;
  25. }
  26. public function paymentMethod(){
  27. return $this->hasOne(PaymentMethod::class, 'id', 'payment_method_id');
  28. }
  29. public function financialTransactions(){
  30. return $this->hasMany(FinancialTransaction::class, 'order_id', 'id');
  31. }
  32. public function finalFinancialTransaction(){
  33. return FinancialTransaction::where('order_id', $this->id)->orderBy('created_at', 'DESC')->first();
  34. }
  35. public function orderNumber() {
  36. if ($this->iid) return $this->iid;
  37. return getFirstSectionUID($this->uid);
  38. }
  39. public function tests(){
  40. $detail = json_decode($this->detail_json);
  41. $selectedOptions = json_decode(@$detail->selected_options);
  42. if(!$selectedOptions) return [];
  43. return (array) @$selectedOptions->tests;
  44. }
  45. public function partnerTests(){
  46. $detail = json_decode($this->detail_json);
  47. $selectedOptions = json_decode(@$detail->selected_options);
  48. if(!$selectedOptions) return [];
  49. return (array) @$selectedOptions->tests;
  50. }
  51. public function testsRequested(){
  52. $tests = (array) $this->tests();
  53. $data = [];
  54. foreach($tests as $key=>$value){
  55. if($value){
  56. array_push($data, $key);
  57. }
  58. }
  59. return $data;
  60. }
  61. public function partnerTestsRequested(){
  62. $tests = (array) $this->partnerTests();
  63. $data = [];
  64. foreach($tests as $key=>$value){
  65. if($value){
  66. array_push($data, $key);
  67. }
  68. }
  69. return $data;
  70. }
  71. public function testsRequestedInHumanReadable(){
  72. $data = [];
  73. $tests = $this->testsRequested();
  74. foreach($tests as $test){
  75. $testName = config('constants.tests.'.$test);
  76. $testPrice = displayAmount('$', config('app.'.$test));
  77. $string = $testName . ' - '.$testPrice;
  78. array_push($data, $string);
  79. }
  80. return $data;
  81. }
  82. public function partnerTestsRequestedInHumanReadable(){
  83. $data = [];
  84. $tests = $this->partnerTestsRequested();
  85. $discount = $this->getPartnerTestDiscount();
  86. foreach($tests as $test){
  87. $testName = config('constants.tests.'.$test);
  88. $testPrice = floatval(config('app.'.$test));
  89. $testCost = $testPrice - $discount;
  90. $string = $testName . ' - '. displayAmount('$', $testCost);
  91. array_push($data, $string);
  92. }
  93. return $data;
  94. }
  95. public function getPartnerTestDiscount(){
  96. $detail = json_decode($this->detail_json);
  97. $selectedOptions = json_decode(@$detail->selected_options);
  98. if(!$selectedOptions) return 0;
  99. if(!@$selectedOptions->partner_test_discount_amount) return 0;
  100. return floatval(@$selectedOptions->partner_test_discount_amount);
  101. }
  102. public function getPartnerEmail(){
  103. $detail = json_decode($this->detail_json);
  104. $selectedOptions = json_decode(@$detail->selected_options);
  105. if(!$selectedOptions) return null;
  106. if(!@$selectedOptions->partner_email) return null;
  107. return @$selectedOptions->partner_email;
  108. }
  109. public function total(){
  110. $total = $this->order_total;
  111. $orders = $this->associatedOrders($this->id);
  112. foreach($orders as $order){
  113. $total = $total + $order->order_total;
  114. }
  115. return $total;
  116. }
  117. public function lab(){
  118. $detail = json_decode($this->detail_json);
  119. $selectedOptions = json_decode(@$detail->selected_options);
  120. $labID = @$selectedOptions->tests_lab_id;
  121. if(!$labID) return null;
  122. return Lab::where('id', $labID)->first();
  123. }
  124. public function createdByUser(){
  125. return $this->hasOne(User::class, 'id', 'created_by_user_id');
  126. }
  127. public function detailJson($toArray = false)
  128. {
  129. if($toArray){
  130. return json_decode($this->detail_json ?? '{}', true);
  131. }
  132. return json_decode($this->detail_json ?? '{}');
  133. }
  134. public function getDetailJsonValue($field)
  135. {
  136. $parsed = $this->detailJson(true);
  137. if (isset($parsed[$field])) {
  138. return $parsed[$field];
  139. }
  140. return null;
  141. }
  142. public function selectedOptions(){
  143. $orderDetailJson = $this->detailJson();
  144. return json_decode(@$orderDetailJson->selected_options ?? '{}', true);
  145. }
  146. public function getOrderTestsSummary(){
  147. $orderSelectedOptions = $this->selectedOptions();
  148. return @$orderSelectedOptions['selectedOptionsSummary'] ?? [];
  149. }
  150. public function getOrderAllTestsRequested(){
  151. $summary = [];
  152. $allTests = [];
  153. $hasPartner = false;
  154. $partnerDiscount = 0;
  155. $orderTotal = 0;
  156. $partnerTotal = 0;
  157. $orders = $this->allAssociatedOrders();
  158. foreach($orders as $order){
  159. $orderSelectedOptions = $this->selectedOptions();
  160. $hasPartner = @$orderSelectedOptions['partner'] ? true:false;
  161. $partnerDiscount = floatval(@$orderSelectedOptions['partner_test_discount_amount']);
  162. $allTests = array_merge($allTests, (array) $order->getOrderTestsSummary());
  163. $partnerTotal = $partnerTotal + floatval($orderSelectedOptions['partner_test_total']);
  164. $orderTotal = $orderTotal + floatval($order->order_total);
  165. }
  166. $summary['all_tests'] = $allTests;
  167. $summary['client_total'] = $orderTotal - $partnerTotal;
  168. $summary['partner_total'] = $partnerTotal;
  169. $summary['sub_total'] = $orderTotal + $partnerDiscount;
  170. $summary['final_total'] = $orderTotal;
  171. $summary['has_partner'] = $hasPartner;
  172. $summary['partner_total_discount'] = $partnerDiscount;
  173. return $summary;
  174. }
  175. }