You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DealerClaimController.php 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. namespace App\Http\Controllers\v3\Main;
  3. use Illuminate\Http\Request;
  4. use App\Http\Controllers\Controller;
  5. use Illuminate\Support\Facades\Auth;
  6. use Carbon\Carbon;
  7. use App\SiteSetting;
  8. use App\LatestModel\Staff;
  9. use App\LatestModel\StaffDetail;
  10. use App\LatestModel\Module\Company;
  11. Use App\LatestModel\Module\Commission;
  12. use App\LatestModel\Module\Form;
  13. use App\LatestModel\Module\PackageDetail;
  14. use App\LatestModel\Module\Subscriber;
  15. use App\LatestModel\Module\WorkOrder;
  16. use App\LatestModel\Module\Docket;
  17. use App\LatestModel\Module\DealerClaim;
  18. use App\LatestModel\Module\Product;
  19. class DealerClaimController extends Controller
  20. {
  21. public function viewDealerClaim($_id){
  22. $id = Auth::guard('main')->id();
  23. $user = Staff::with('StaffDetail')->find($id);
  24. $site = SiteSetting::first();
  25. $company = Company::where('_id', $_id)->first();
  26. return view('v3.main-dashboard.dealer_claim', compact('user','site','company'));
  27. }
  28. public function getCompleted(){
  29. $cdata = array(); $finalData = array();
  30. $dealerclaim = DealerClaim::get()->toArray();
  31. $form = Form::with('Subscriber','PackageDetail','WorkOrder')->get();
  32. foreach($form as $f){
  33. if(!empty($f->dealer_id)){
  34. if(!empty($f->WorkOrder)){
  35. if($f->WorkOrder->status == 'Completed'){
  36. $cdata[] = $f->_id;
  37. }
  38. }
  39. }
  40. }
  41. $notExist = array();
  42. foreach($cdata as $c){
  43. if(array_search($c, array_column($dealerclaim, 'customer_id')) !== False) {
  44. } else {
  45. $notExist[] = $c;
  46. $formD = Form::with('Subscriber','PackageDetail','WorkOrder')->where('_id',$c)->first();
  47. $product = Product::where('speed', $formD->PackageDetail->name)->where('formT',$formD->type_application)->first();
  48. $docket = Docket::where('work_order_id',$formD->WorkOrder->wo)->first();
  49. if(!empty($docket->docket_id)){
  50. $subsData = [
  51. 'formT' => $formD->type_application,
  52. 'contract' => $formD->PackageDetail->contract,
  53. 'submission_dt' => Carbon::parse($formD->created_at)->setTimezone('Asia/Kuala_Lumpur')->toDateTimeString(),
  54. 'submission_my' => date('m/Y', strtotime(Carbon::parse($formD->created_at)->setTimezone('Asia/Kuala_Lumpur'))),
  55. 'wo' => $formD->WorkOrder->wo,
  56. 'customer_id' => $formD->_id,
  57. 'package' => $formD->PackageDetail->name,
  58. 'retail_price' => (int)$product->retail_price,
  59. 'docket' => $docket->docket_id,
  60. 'activated_dt' => $docket->end_job,
  61. 'activated_my' => date('m/Y', strtotime($docket->end_job)),
  62. 'claim_dt' => '',
  63. 'total_form_by_month' => '',
  64. 'agent' => $formD->dealer_id,
  65. 'dealer' => $formD->company_id,
  66. ];
  67. DealerClaim::create($subsData);
  68. }
  69. }
  70. }
  71. return $notExist;
  72. }
  73. public function storeDealerClaim(){
  74. $data = $this->getCompleted();
  75. return $data;
  76. }
  77. public function test1(){
  78. $d = DealerClaim::where('formT' == 'R')->all();
  79. $data12 = array(); $data24 = array(); $final = array();
  80. foreach($d as $dc){
  81. if($dc->activated_my == $mY){
  82. if($dc->contract == "12"){
  83. $data12[] = $dc;
  84. }else if($dc->contract == "24"){
  85. $data24[] = $dc;
  86. }
  87. }
  88. }
  89. $countD24 = count($data24);
  90. $countD12 = count($data12);
  91. $comm = Commission::where('formT','R')->get();
  92. $commV12 = 0; $commV24 = 0;
  93. $comm12 = ''; $comm24 = '';
  94. foreach($comm as $c){
  95. if($c->contract =='12'){
  96. if(strpos($c->ranges, '>') !== false) {
  97. $pieces = explode(">", $c->ranges);
  98. if($countD12 >= intval($pieces[1])){
  99. $commV12 = floatval($c->comm)/100;
  100. $comm12 = $c->comm.'%';
  101. }
  102. }
  103. if(strpos($c->ranges,'-') !== false){
  104. $pieces = explode("-", $c->ranges);
  105. if($countD12 >= intval($pieces[0]) && $countD12 <= intval($pieces[1])){
  106. $commV12 = floatval($c->comm)/100;
  107. $comm12 = $c->comm.'%';
  108. }
  109. }
  110. }else if($c->contract =='24'){
  111. // Contract => 24
  112. if($countD24 != 0){
  113. if(strpos($c->ranges, '>') !== false) {
  114. $pieces = explode(">", $c->ranges);
  115. if($countD24 > intval($pieces[1])){
  116. $commV24 = floatval($c->comm)/100;
  117. $comm24 = $c->comm.'%';
  118. }
  119. }
  120. if(strpos($c->ranges,'-') !== false){
  121. $pieces = explode("-", $c->ranges);
  122. if($countD24 >= intval($pieces[0]) && $countD24 <= intval($pieces[1])){
  123. $commV24 = floatval($c->comm)/100;
  124. $comm24 = $c->comm.'%';
  125. }
  126. }
  127. }
  128. }
  129. }
  130. foreach ($data12 as $key => $d) {
  131. $customer = Subscriber::where('_id', $d['customer_id'])->first();
  132. $address = $customer->unit_no.', '.$customer->building.', '.$customer->street.', '.$customer->postcode.', '.$customer->city.', '.$customer->state;
  133. $final[] = array(
  134. 'index' => $d['formT'],
  135. 'contract' => $d['contract'],
  136. 'wo' => $d['wo'],
  137. 'activation' => $d['activated_dt'],
  138. 'name' => $customer->name,
  139. 'address' => $address,
  140. 'package' => $d['package'],
  141. 'retail_price' => 'RM '.$d['retail_price'],
  142. 'incentives1' => $comm12,
  143. 'incentives2' => '',
  144. 'total' => 'RM '.number_format($d['retail_price']*$commV12,2),
  145. 'claim' => $d['claim_dt'],
  146. );
  147. }
  148. foreach ($data24 as $key => $d) {
  149. $customer = Subscriber::where('_id', $d['customer_id'])->first();
  150. $address = $customer->unit_no.', '.$customer->building.', '.$customer->street.', '.$customer->postcode.', '.$customer->city.', '.$customer->state;
  151. $final[] = array(
  152. 'index' => $d['formT'],
  153. 'contract' => $d['contract'],
  154. 'wo' => $d['wo'],
  155. 'activation' => $d['activated_dt'],
  156. 'name' => $customer->name,
  157. 'address' => $address,
  158. 'package' => $d['package'],
  159. 'retail_price' => 'RM '.$d['retail_price'],
  160. 'incentives1' => $comm24,
  161. 'incentives2' => '',
  162. 'total' => 'RM '.number_format($d['retail_price']*$commV24,2),
  163. 'claim' => $d['claim_dt'],
  164. );
  165. }
  166. dd($final);
  167. }
  168. public function getDealerCompany(){
  169. $dataD = $this->storeDealerClaim();
  170. dd($dataD);
  171. // return $this->test1();
  172. }
  173. }