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 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <?php
  2. namespace App\Http\Controllers\DS;
  3. use Illuminate\Http\Request;
  4. use App\Http\Controllers\Controller;
  5. use Illuminate\Support\Facades\Auth;
  6. use Carbon\Carbon;
  7. use App\Model\Staff;
  8. use App\Model\StaffDetail;
  9. use App\Model\Company;
  10. Use App\Model\Commission;
  11. use App\Model\Form;
  12. use App\Model\PackageDetail;
  13. use App\Model\Subscriber;
  14. use App\Model\WorkOrder;
  15. use App\Model\Docket;
  16. use App\Model\DealerClaim;
  17. use App\Model\DealerClaim2;
  18. use App\Model\Product;
  19. class DealerClaimController extends Controller
  20. {
  21. public function getCompleted(){
  22. $cdata = array(); $finalData = array();
  23. $dealerclaim = DealerClaim::get()->toArray();
  24. $form = Form::with('Subscriber','PackageDetail','WorkOrder')->get();
  25. foreach($form as $f){
  26. if(!empty($f->dealer_id)){
  27. if(!empty($f->WorkOrder)){
  28. if($f->WorkOrder->status == 'Completed' && $f->WorkOrder->nature_work == 'Activation'){
  29. $cdata[] = $f->_id;
  30. }
  31. }
  32. }
  33. }
  34. $notExist = array();
  35. foreach($cdata as $c){
  36. if(array_search($c, array_column($dealerclaim, 'customer_id')) !== False) {
  37. } else {
  38. $notExist[] = $c;
  39. $formD = Form::with('Subscriber','PackageDetail','WorkOrder')->where('_id',$c)->first();
  40. $product = Product::where('speed', $formD->PackageDetail->name)->where('formT',$formD->type_application)->first();
  41. $docket = Docket::where('work_order_id',$formD->WorkOrder->wo)->first();
  42. // if($product->promotion_price != "0"){
  43. // $price = (int)$product->promotion_price;
  44. // } else {
  45. $price = (int)$product->retail_price;
  46. // }
  47. if(!empty($docket->docket_id)){
  48. $subsData = [
  49. 'formT' => $formD->type_application,
  50. 'projectT' => $formD->project_type,
  51. 'contract' => $formD->PackageDetail->contract,
  52. 'submission_dt' => Carbon::parse($formD->created_at)->setTimezone('Asia/Kuala_Lumpur')->toDateTimeString(),
  53. 'submission_my' => date('m/Y', strtotime(Carbon::parse($formD->created_at)->setTimezone('Asia/Kuala_Lumpur'))),
  54. 'wo' => $formD->WorkOrder->wo,
  55. 'customer_id' => $formD->_id,
  56. 'package' => $formD->PackageDetail->name,
  57. 'retail_price' => $price,
  58. 'docket' => $docket->docket_id,
  59. 'activated_dt' => $docket->end_job,
  60. 'activated_my' => date('m/Y', strtotime($docket->end_job)),
  61. 'claim_dt' => '',
  62. 'total_form_by_month' => '',
  63. 'agent' => $formD->dealer_id,
  64. 'dealer' => $formD->company_id,
  65. 'invoice_no' => $this->generateInvoiceNumber($formD->type_application,$formD->company_id,date('m', strtotime($docket->end_job)),date('Y', strtotime($docket->end_job))),
  66. ];
  67. DealerClaim::create($subsData);
  68. }
  69. }
  70. }
  71. return $notExist;
  72. }
  73. public function getCompleted2(){
  74. $cdata = array(); $finalData = array();
  75. $dealerclaim = DealerClaim2::get()->toArray();
  76. $form = Form::with('Subscriber','PackageDetail','WorkOrder')->get();
  77. foreach($form as $f){
  78. if(!empty($f->dealer_id)){
  79. if(!empty($f->WorkOrder)){
  80. if($f->WorkOrder->status == 'Completed' && $f->WorkOrder->nature_work == 'Activation'){
  81. $cdata[] = $f->_id;
  82. }
  83. }
  84. }
  85. }
  86. $notExist = array();
  87. foreach($cdata as $c){
  88. if(array_search($c, array_column($dealerclaim, 'customer_id')) !== False) {
  89. } else {
  90. $notExist[] = $c;
  91. $formD = Form::with('Subscriber','PackageDetail','WorkOrder')->where('_id',$c)->first();
  92. $product = Product::where('speed', $formD->PackageDetail->name)->where('formT',$formD->type_application)->withTrashed()->first();
  93. $docket = Docket::where('work_order_id',$formD->WorkOrder->wo)->first();
  94. if(!empty($docket->docket_id)){
  95. $subsData = [
  96. 'formT' => $formD->type_application,
  97. 'contract' => $formD->PackageDetail->contract,
  98. 'submission_dt' => Carbon::parse($formD->created_at)->setTimezone('Asia/Kuala_Lumpur')->toDateTimeString(),
  99. 'submission_my' => date('m/Y', strtotime(Carbon::parse($formD->created_at)->setTimezone('Asia/Kuala_Lumpur'))),
  100. 'wo' => $formD->WorkOrder->wo,
  101. 'customer_id' => $formD->_id,
  102. 'package' => $formD->PackageDetail->name,
  103. 'retail_price' => (int)$product->retail_price,
  104. 'docket' => $docket->docket_id,
  105. 'activated_dt' => $docket->end_job,
  106. 'activated_my' => date('m/Y', strtotime($docket->end_job)),
  107. 'claim_dt' => '',
  108. 'total_form_by_month' => '',
  109. 'agent' => $formD->dealer_id,
  110. 'dealer' => $formD->company_id,
  111. ];
  112. DealerClaim2::create($subsData);
  113. }
  114. }
  115. }
  116. return $notExist;
  117. }
  118. public function storeDealerClaim(){
  119. $data = $this->getCompleted();
  120. return $data;
  121. }
  122. public function storeDealerClaim2(){
  123. $data = $this->getCompleted2();
  124. return $data;
  125. }
  126. public function getDealerCompany2(){
  127. $dataD = $this->storeDealerClaim2();
  128. dd($dataD);
  129. }
  130. // public function test1(){
  131. // $d = DealerClaim::where('formT' == 'R')->all();
  132. // $mY='';
  133. // $data12 = array(); $data24 = array(); $final = array();
  134. // foreach($d as $dc){
  135. // if($dc->activated_my == $mY){
  136. // if($dc->contract == "12"){
  137. // $data12[] = $dc;
  138. // }else if($dc->contract == "24"){
  139. // $data24[] = $dc;
  140. // }
  141. // }
  142. // }
  143. // $countD24 = count($data24);
  144. // $countD12 = count($data12);
  145. // $comm = Commission::where('formT','R')->get();
  146. // $commV12 = 0; $commV24 = 0;
  147. // $comm12 = ''; $comm24 = '';
  148. // foreach($comm as $c){
  149. // if($c->contract =='12'){
  150. // if(strpos($c->ranges, '>') !== false) {
  151. // $pieces = explode(">", $c->ranges);
  152. // if($countD12 >= intval($pieces[1])){
  153. // $commV12 = floatval($c->comm)/100;
  154. // $comm12 = $c->comm.'%';
  155. // }
  156. // }
  157. // if(strpos($c->ranges,'-') !== false){
  158. // $pieces = explode("-", $c->ranges);
  159. // if($countD12 >= intval($pieces[0]) && $countD12 <= intval($pieces[1])){
  160. // $commV12 = floatval($c->comm)/100;
  161. // $comm12 = $c->comm.'%';
  162. // }
  163. // }
  164. // }else if($c->contract =='24'){
  165. // // Contract => 24
  166. // if($countD24 != 0){
  167. // if(strpos($c->ranges, '>') !== false) {
  168. // $pieces = explode(">", $c->ranges);
  169. // if($countD24 > intval($pieces[1])){
  170. // $commV24 = floatval($c->comm)/100;
  171. // $comm24 = $c->comm.'%';
  172. // }
  173. // }
  174. // if(strpos($c->ranges,'-') !== false){
  175. // $pieces = explode("-", $c->ranges);
  176. // if($countD24 >= intval($pieces[0]) && $countD24 <= intval($pieces[1])){
  177. // $commV24 = floatval($c->comm)/100;
  178. // $comm24 = $c->comm.'%';
  179. // }
  180. // }
  181. // }
  182. // }
  183. // }
  184. // foreach ($data12 as $key => $d) {
  185. // $customer = Subscriber::where('_id', $d['customer_id'])->first();
  186. // $address = $customer->unit_no.', '.$customer->building.', '.$customer->street.', '.$customer->postcode.', '.$customer->city.', '.$customer->state;
  187. // $final[] = array(
  188. // 'index' => $d['formT'],
  189. // 'contract' => $d['contract'],
  190. // 'wo' => $d['wo'],
  191. // 'activation' => $d['activated_dt'],
  192. // 'name' => $customer->name,
  193. // 'address' => $address,
  194. // 'package' => $d['package'],
  195. // 'retail_price' => 'RM '.$d['retail_price'],
  196. // 'incentives1' => $comm12,
  197. // 'incentives2' => '',
  198. // 'total' => 'RM '.number_format($d['retail_price']*$commV12,2),
  199. // 'claim' => $d['claim_dt'],
  200. // );
  201. // }
  202. // foreach ($data24 as $key => $d) {
  203. // $customer = Subscriber::where('_id', $d['customer_id'])->first();
  204. // $address = $customer->unit_no.', '.$customer->building.', '.$customer->street.', '.$customer->postcode.', '.$customer->city.', '.$customer->state;
  205. // $final[] = array(
  206. // 'index' => $d['formT'],
  207. // 'contract' => $d['contract'],
  208. // 'wo' => $d['wo'],
  209. // 'activation' => $d['activated_dt'],
  210. // 'name' => $customer->name,
  211. // 'address' => $address,
  212. // 'package' => $d['package'],
  213. // 'retail_price' => 'RM '.$d['retail_price'],
  214. // 'incentives1' => $comm24,
  215. // 'incentives2' => '',
  216. // 'total' => 'RM '.number_format($d['retail_price']*$commV24,2),
  217. // 'claim' => $d['claim_dt'],
  218. // );
  219. // }
  220. // dd($final);
  221. // }
  222. public function getDealerCompany(){
  223. $dataD = $this->storeDealerClaim();
  224. dd($dataD);
  225. }
  226. public function updateIncentives(){
  227. $data_not_activated = array();
  228. $dealerclaim = DealerClaim::get();
  229. foreach ($dealerclaim as $key => $d) {
  230. $wo = WorkOrder::where('wo',$d->wo)->first();
  231. if(!empty($wo)){
  232. if($wo->nature_work !== 'Activation'){
  233. $data_not_activated[] = $d;
  234. }
  235. }
  236. }
  237. dd($data_not_activated);
  238. }
  239. public function generateInvoiceNumber($formT,$dealer,$month,$year){
  240. $claim = DealerClaim::where('formT',$formT)->where('dealer',$dealer)->where('activated_my',$month.'/'.$year)->first();
  241. if(!empty($claim->invoice_no)){
  242. return $claim->invoice_no;
  243. }else{
  244. //get latest invoice number
  245. $invoice = DealerClaim::where('formT',$formT)->withTrashed()->latest()->first();
  246. //sanitize invoice number to return only integer
  247. $num = filter_var($invoice['invoice_no'], FILTER_SANITIZE_NUMBER_INT);
  248. //compulsory increment to store new invoice number, WHILE number is found, keep looping
  249. do{
  250. (int)$num += 1;
  251. //add 0s at the fornt if number to complete 9 digit
  252. if ((strlen((string) $num)) == 2) {
  253. $num = '0000000'.$num;
  254. }elseif ((strlen((string)$num)) == 3){
  255. $num = '000000'.$num;
  256. }elseif ((strlen((string)$num )) == 4) {
  257. $num = '00000'.$num;
  258. }elseif ((strlen((string)$num )) == 5) {
  259. $num = '0000'.$num;
  260. }elseif ((strlen((string)$num )) == 6) {
  261. $num = '000'.$num;
  262. }elseif ((strlen((string)$num )) == 7) {
  263. $num = '00'.$num;
  264. }elseif ((strlen((string)$num )) == 8) {
  265. $num = '0'.$num;
  266. }
  267. }while(!empty(DealerClaim::where('formT', $formT)->where('invoice_no', $formT.$num)->first()));
  268. $num = $formT.$num;
  269. return $num;
  270. }
  271. }
  272. }