StoreOrder.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. $allTests = getAllTests();
  75. foreach($tests as $test){
  76. $testName = @$allTests[$test]['label'];
  77. $testPrice = displayAmount('$', config('app.'.$test));
  78. $string = $testName . ' - '.$testPrice;
  79. array_push($data, $string);
  80. }
  81. return $data;
  82. }
  83. public function partnerTestsRequestedInHumanReadable(){
  84. $data = [];
  85. $tests = $this->partnerTestsRequested();
  86. $discount = $this->getPartnerTestDiscount();
  87. $allTests = getAllTests();
  88. foreach($tests as $test){
  89. $testName = $allTests[$test]['label'];
  90. $testPrice = floatval(config('app.'.$test));
  91. $testCost = $testPrice - $discount;
  92. $string = $testName . ' - '. displayAmount('$', $testCost);
  93. array_push($data, $string);
  94. }
  95. return $data;
  96. }
  97. public function getPartnerTestDiscount(){
  98. $detail = json_decode($this->detail_json);
  99. $selectedOptions = json_decode(@$detail->selected_options);
  100. if(!$selectedOptions) return 0;
  101. if(!@$selectedOptions->partner_test_discount_amount) return 0;
  102. return floatval(@$selectedOptions->partner_test_discount_amount);
  103. }
  104. public function getPartnerEmail(){
  105. $detail = json_decode($this->detail_json);
  106. $selectedOptions = json_decode(@$detail->selected_options);
  107. if(!$selectedOptions) return null;
  108. if(!@$selectedOptions->partner_email) return null;
  109. return @$selectedOptions->partner_email;
  110. }
  111. public function total(){
  112. $total = $this->order_total;
  113. $orders = $this->associatedOrders($this->id);
  114. foreach($orders as $order){
  115. $total = $total + $order->order_total;
  116. }
  117. return $total;
  118. }
  119. public function lab(){
  120. $detail = json_decode($this->detail_json);
  121. $selectedOptions = json_decode(@$detail->selected_options);
  122. $labID = @$selectedOptions->tests_lab_id;
  123. if(!$labID) return null;
  124. return Lab::where('id', $labID)->first();
  125. }
  126. public function createdByUser(){
  127. return $this->hasOne(User::class, 'id', 'created_by_user_id');
  128. }
  129. public function detailJson($toArray = false)
  130. {
  131. if($toArray){
  132. return json_decode($this->detail_json ?? '{}', true);
  133. }
  134. return json_decode($this->detail_json ?? '{}');
  135. }
  136. public function getDetailJsonValue($field)
  137. {
  138. $parsed = $this->detailJson(true);
  139. if (isset($parsed[$field])) {
  140. return $parsed[$field];
  141. }
  142. return null;
  143. }
  144. public function selectedOptions(){
  145. $orderDetailJson = $this->detailJson();
  146. return json_decode(@$orderDetailJson->selected_options ?? '{}', true);
  147. }
  148. public function getOrderTestsSummary(){
  149. $orderSelectedOptions = $this->selectedOptions();
  150. $tests = @$orderSelectedOptions['tests'] ?? [];
  151. $data = [];
  152. foreach($tests as $k=>$v){
  153. if($v){
  154. $data[$k] = $v;
  155. }
  156. }
  157. return $data;
  158. }
  159. public function getOrderAllTestsRequested(){
  160. $summary = [];
  161. $allTests = [];
  162. $hasPartner = false;
  163. $partnerDiscount = 0;
  164. $orderTotal = 0;
  165. $partnerTotal = 0;
  166. $orders = $this->allAssociatedOrders();
  167. foreach($orders as $order){
  168. $orderSelectedOptions = $this->selectedOptions();
  169. $hasPartner = @$orderSelectedOptions['partner'] ? true:false;
  170. $partnerDiscount = floatval(@$orderSelectedOptions['partner_test_discount_amount']);
  171. $allTests = array_merge($allTests, (array) $order->getOrderTestsSummary());
  172. $partnerTotal = $partnerTotal + floatval(@$orderSelectedOptions['partner_test_total']);
  173. $orderTotal = $orderTotal + floatval($order->order_total);
  174. }
  175. $summary['all_tests'] = $allTests;
  176. $summary['client_total'] = $orderTotal - $partnerTotal;
  177. $summary['partner_total'] = $partnerTotal;
  178. $summary['sub_total'] = $orderTotal + $partnerDiscount;
  179. $summary['final_total'] = $orderTotal;
  180. $summary['has_partner'] = $hasPartner;
  181. $summary['partner_total_discount'] = $partnerDiscount;
  182. return $summary;
  183. }
  184. public function getRequisitionFormAccessToken(){
  185. return $this->getDetailJsonValue('requisition_form_access_token');
  186. }
  187. public function requisitionFormTokenAccessUrl(){
  188. $accessToken = $this->getRequisitionFormAccessToken();
  189. $url = config('app.orderAppUrl').'/requisition-form/download/'.$accessToken;
  190. return $url;
  191. }
  192. }