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.

DealerController.php 82KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795
  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 Validator;
  8. use PDF;
  9. use App\Staff;
  10. use App\Model\StaffDetail;
  11. use App\Model\Coverage;
  12. use App\Model\DealerClaim;
  13. use App\Model\DealerClaim2;
  14. use App\Model\Commission;
  15. use App\Model\Company;
  16. use App\Model\Form;
  17. use App\Model\FormStatus;
  18. use App\Model\Subscriber;
  19. use App\Model\PackageDetail;
  20. use App\Model\WorkOrder;
  21. use App\Model\Docket;
  22. class DealerController extends Controller
  23. {
  24. public function viewList(){
  25. $id = Auth::guard('ds')->id();
  26. $user = Staff::with('StaffDetail')->find($id);
  27. return view('dealer.index',compact('user'));
  28. }
  29. /***
  30. / Agent list
  31. ***/
  32. public function gAgentList(){
  33. $id = Auth::guard('ds')->id();
  34. $user = Staff::find($id);
  35. $email = $user->email;
  36. $userDetails = StaffDetail::where('email',$email)->first();
  37. $all = StaffDetail::where('company_id',$userDetails->company_id)->orderBy('created_at','desc')->get();
  38. $curr = Carbon::now()->getTimestamp();
  39. $i = 0;
  40. $nested_data = array();
  41. if(!empty($all)){
  42. foreach ($all as $a){
  43. $i++; $y = ''; $n1 = '';
  44. if($userDetails->_id == $a->_id){
  45. $y = $a->name."-You";
  46. }else {
  47. $y = $a->name;
  48. }
  49. $reg_time = $a->created_at;
  50. $expiry_date = $reg_time->addDays(3);
  51. $expiry_date = $expiry_date->getTimestamp();
  52. if($curr < $expiry_date) {
  53. $n1 = "New/";
  54. }
  55. array_push($nested_data, array(
  56. 'index' => $i,
  57. 'image' => $a->user_pic,
  58. 'name' => $n1.$y,
  59. 'ic' => $a->ic,
  60. 'phone' => $a->phone,
  61. 'email' => $a->email,
  62. 'action' => $a->_id
  63. ));
  64. }
  65. }
  66. return \DataTables::of($nested_data)->make(true);
  67. }
  68. /***
  69. / Application Form List
  70. ***/
  71. public function viewAppList(){
  72. $id = Auth::guard('ds')->id();
  73. $user = Staff::with('StaffDetail')->find($id);
  74. return view('dealer.application-form-list',compact('user'));
  75. }
  76. /***
  77. / Get Application Form List
  78. ***/
  79. public function getSubscribers($dealerCompany)
  80. {
  81. $data = array();
  82. $curr = Carbon::now()->getTimestamp();
  83. $subscriber = Subscriber::where('dealer_company', $dealerCompany)->get();
  84. foreach ($subscriber as $key => $s) {
  85. $n1 = '';
  86. $reg_time = Carbon::createFromTimestamp($s->reg_time);
  87. $expiry_date = $reg_time->addDays(3);
  88. $expiry_date = $expiry_date->getTimestamp();
  89. $agentName = StaffDetail::where('_id',$s->dealer)->first();
  90. $build = Coverage::where('address',$s->building)->first();
  91. if($s->street == ''){
  92. $street = $build->street;
  93. }else {
  94. $street = $s->street;
  95. }
  96. if($curr < $expiry_date) {
  97. $n1 = "New/";
  98. }
  99. $data[] = array(
  100. 'formT' => 'R',
  101. 'wo' => $s->wo,
  102. 'name' => $n1.$s->name,
  103. 'no' => $s->phone1,
  104. 'unit' => $s->unit_no,
  105. 'building' => $s->building,
  106. 'street' => $street,
  107. 'postcode' => $s->postcode,
  108. 'city' => $s->city,
  109. 'sales' => $agentName->name,
  110. 'created_at' => $s->reg_time,
  111. 'action' => $s->_id,
  112. );
  113. }
  114. return $data;
  115. }
  116. public function getBusiness($dealerCompany){
  117. $data = array();
  118. $curr = Carbon::now()->getTimestamp();
  119. $business = Subscriber::where('dealer_company', $dealerCompany)->get();
  120. foreach ($business as $key => $s) {
  121. $n1 = '';
  122. $reg_time = Carbon::createFromTimestamp($s->reg_time);
  123. $expiry_date = $reg_time->addDays(3);
  124. $expiry_date = $expiry_date->getTimestamp();
  125. if($curr < $expiry_date) {
  126. $n1 = "New/";
  127. }
  128. $agentName = StaffDetail::where('_id',$s->dealer)->first();
  129. $data[] = array(
  130. 'formT' => 'B',
  131. 'wo' => $s->wo,
  132. 'name' => $n1.$s->name,
  133. 'no' => $s->phone1,
  134. 'unit' => '-',
  135. 'building' => '-',
  136. 'street' => $s->address,
  137. 'postcode' => $s->postcode,
  138. 'city' => $s->city,
  139. 'sales' => $agentName->name,
  140. 'created_at' => $s->reg_time,
  141. 'action' => $s->_id,
  142. );
  143. }
  144. return $data;
  145. }
  146. public function getOrderHistory($id){
  147. $data = array();
  148. $order_history = FormStatus::where('form_id', $id)->get();
  149. foreach($order_history as $oh){
  150. $utcdatetime = new \MongoDB\BSON\UTCDateTime((string) $oh->date);
  151. $datetime = $utcdatetime->toDateTime();
  152. $time = $datetime->format(DATE_RSS);
  153. $time = strtotime($time.' UTC');
  154. $dateInLocal = date("d/m/Y h:i a", $time);
  155. $data[] = array(
  156. 'date' => $dateInLocal,
  157. 'status' => $oh->status,
  158. 'desc' => $oh->desc
  159. );
  160. }
  161. $json = json_encode($data);
  162. return $json;
  163. }
  164. public function gApplicationList1()
  165. {
  166. $id = Auth::guard('ds')->id();
  167. $user = Staff::with('StaffDetail')->find($id);
  168. $fm = Form::with('Subscriber')->where('company_id',$user->StaffDetail->company_id)->orderBy('created_at','desc')->get();
  169. $nested_data = array();
  170. $i=0;
  171. foreach($fm as $f)
  172. {
  173. $dname="";
  174. $wo = WorkOrder::where('_id', $f->_id)->first();
  175. $dealer = StaffDetail::where('_id', $f->dealer_id)->first();
  176. if(!empty($dealer)){
  177. $dname=$dealer->name;
  178. }
  179. $address = ''; $name = '';
  180. if($f->type_application == 'R'){
  181. $address = $f->Subscriber->building_name;
  182. $name = $f->Subscriber->name;
  183. }else if($f->type_application == 'B'){
  184. $address = '-';
  185. $name = $f->Subscriber->company_name;
  186. }
  187. if(empty($wo) && !empty($f->Subscriber))
  188. {
  189. $i++;
  190. array_push($nested_data, array(
  191. 'index' => $i.$f->type_application,
  192. 'name' => $name,
  193. 'phone' => $f->Subscriber->phone1,
  194. 'unit' => $f->Subscriber->unit_no,
  195. 'building' => $address,
  196. 'street' => $f->Subscriber->street,
  197. 'postcode' => $f->Subscriber->postcode,
  198. 'city' => $f->Subscriber->city,
  199. 'sales' => $dname,
  200. 'action' => $f->_id
  201. ));
  202. }
  203. }
  204. return \DataTables::of($nested_data)->make(true);
  205. }
  206. public function gApplicationList2()
  207. {
  208. $id = Auth::guard('ds')->id();
  209. $user = Staff::with('StaffDetail')->find($id);
  210. $fm = Form::with('Subscriber')->where('company_id',$user->StaffDetail->company_id)->orderBy('created_at','desc')->get();
  211. $nested_data = array();
  212. $i=0;
  213. foreach($fm as $f)
  214. {
  215. $dname="";
  216. $wo = WorkOrder::where('_id', $f->_id)->first();
  217. $dealer = StaffDetail::where('_id', $f->dealer_id)->first();
  218. if(!empty($dealer)){
  219. $dname=$dealer->name;
  220. }
  221. $address = ''; $name = '';
  222. if($f->type_application == 'R'){
  223. $address = $f->Subscriber->building_name;
  224. $name = $f->Subscriber->name;
  225. }else if($f->type_application == 'B'){
  226. $address = '-';
  227. $name = $f->Subscriber->company_name;
  228. }
  229. if(!empty($wo) && $wo->status!='Completed')
  230. {
  231. $i++;
  232. array_push($nested_data, array(
  233. 'index' => $i.$f->type_application,
  234. 'name' => $name,
  235. 'phone' => $f->Subscriber->phone1,
  236. 'unit' => $f->Subscriber->unit_no,
  237. 'building' => $address,
  238. 'street' => $f->Subscriber->street,
  239. 'postcode' => $f->Subscriber->postcode,
  240. 'city' => $f->Subscriber->city,
  241. 'sales' => $dname,
  242. 'action' => $f->_id
  243. ));
  244. }
  245. }
  246. return \DataTables::of($nested_data)->make(true);
  247. }
  248. public function getCompletedList($year, $month)
  249. {
  250. $id = Auth::guard('ds')->id();
  251. $user = Staff::find($id);
  252. $email = $user->email;
  253. $userDetails = StaffDetail::where('email',$email)->first();
  254. $subs = $this->getSubscribers($userDetails->company_id);
  255. $buss = $this->getBusiness($userDetails->company_id);
  256. // Combine
  257. $cdata = array();
  258. foreach ($subs as $key => $s) {
  259. $cdata[] = $s;
  260. }
  261. foreach ($buss as $key => $b) {
  262. $cdata[] = $b;
  263. }
  264. // Sort based on TimeStamp
  265. $sort = array();
  266. foreach ($cdata as $key => $r) {
  267. $sort[$key] = $r['created_at'];
  268. }
  269. array_multisort($sort, SORT_DESC, $cdata);
  270. $wK = WorkDetail::get();
  271. $curr = Carbon::now()->getTimestamp();
  272. $mY = '';
  273. if($year != 'null' && $month != 'null'){
  274. $mY = $year.'/'.$month;
  275. }
  276. $final = array(); $filter = array();
  277. $dW = array();
  278. foreach ($cdata as $c){
  279. if($c['wo'] != ''){
  280. foreach ($wK as $key => $w) {
  281. if($c['wo'] == $w->wo && $w->status == 'Completed'){
  282. $dW[] = $c;
  283. }
  284. }
  285. }
  286. }
  287. foreach($dW as $d){
  288. if($mY != ''){
  289. $docket = Docket::where('work_order_id', $d['wo'])->first();
  290. $cDate = date('Y/m', strtotime($docket->end_job));;
  291. if($mY == $cDate){
  292. $filter[] = $d;
  293. }
  294. }else {
  295. $filter[] = $d;
  296. }
  297. }
  298. foreach ($filter as $c){
  299. $do = Docket::where('work_order_id', $c['wo'])->first();
  300. $n1 = '';
  301. $reg_time = Carbon::createFromTimestamp(strtotime($do->end_job));
  302. $expiry_date = $reg_time->addDays(3);
  303. $expiry_date = $expiry_date->getTimestamp();
  304. if($curr < $expiry_date) {
  305. $n1 = "New/";
  306. }
  307. $final[] = array(
  308. 'formT' => $c['formT'],
  309. 'wo' => $c['wo'],
  310. 'do' => $do->docket_id,
  311. 'activated' => $do->end_job,
  312. 'name' => $n1.str_replace("New/","",$c['name']),
  313. 'no' => $c['no'],
  314. 'unit' => $c['unit'],
  315. 'building' => $c['building'],
  316. 'street' => $c['street'],
  317. 'postcode' => $c['postcode'],
  318. 'city' => $c['city'],
  319. 'sales' => $c['sales'],
  320. 'created_at' => $c['created_at'],
  321. 'action' => $c['action'],
  322. );
  323. }
  324. return $final;
  325. }
  326. public function gApplicationList3($year,$month)
  327. {
  328. $id = Auth::guard('ds')->id();
  329. $user = Staff::with('StaffDetail')->find($id);
  330. $mY = '';
  331. if($year!='null' && $month!='null'){
  332. $mY = $month.'/'.$year;
  333. }
  334. $fm = Form::with('Subscriber','WorkOrder')->where('company_id',$user->StaffDetail->company_id)->orderBy('created_at','desc')->get();
  335. $nested_data = array();
  336. $i=0;
  337. foreach($fm as $f)
  338. {
  339. if(!empty($f->WorkOrder)){
  340. if($f->WorkOrder->status == 'Completed'){
  341. $wo = WorkOrder::with('Docket')->where('wo', $f->WorkOrder->wo)->first();
  342. $dealer = StaffDetail::where('_id', $f->dealer_id)->withTrashed()->first();
  343. $i++;
  344. $address = ''; $name = '';
  345. if($f->type_application == 'R'){
  346. $address = $f->Subscriber->building_name;
  347. $name = $f->Subscriber->name;
  348. }else if($f->type_application == 'B'){
  349. $address = '-';
  350. $name = $f->Subscriber->company_name;
  351. }
  352. if(!empty($wo->Docket)){
  353. $end = date('m/Y',strtotime($wo->Docket->end_job));
  354. if($mY == ''){
  355. array_push($nested_data, array(
  356. 'index' => $i.$f->type_application,
  357. 'wo' => $wo->wo,
  358. 'do' => $f->WorkOrder->docket_id,
  359. 'name' => $name,
  360. 'phone' => $f->Subscriber->phone1,
  361. 'unit' => $f->Subscriber->unit_no,
  362. 'building' => $address,
  363. 'street' => $f->Subscriber->street,
  364. 'postcode' => $f->Subscriber->postcode,
  365. 'city' => $f->Subscriber->city,
  366. 'sales' => $dealer->name,
  367. 'action' => $f->_id
  368. ));
  369. }else if($mY == $end){
  370. array_push($nested_data, array(
  371. 'index' => $i.$f->type_application,
  372. 'wo' => $wo->wo,
  373. 'do' => $f->WorkOrder->docket_id,
  374. 'name' => $name,
  375. 'phone' => $f->Subscriber->phone1,
  376. 'unit' => $f->Subscriber->unit_no,
  377. 'building' => $address,
  378. 'street' => $f->Subscriber->street,
  379. 'postcode' => $f->Subscriber->postcode,
  380. 'city' => $f->Subscriber->city,
  381. 'sales' => $dealer->name,
  382. 'action' => $f->_id
  383. ));
  384. }
  385. }
  386. }
  387. }
  388. }
  389. return \DataTables::of($nested_data)->make(true);
  390. }
  391. /***
  392. / Claim All
  393. ***/
  394. public function claimAll() {
  395. $id = Auth::guard('ds')->id();
  396. $user = Staff::with('StaffDetail')->find($id);
  397. return view('dealer.claim-all',compact('user'));
  398. }
  399. /***
  400. / Convert To PDF
  401. ***/
  402. public function exportPDF1() {
  403. return view('pdf.agent-claim');
  404. }
  405. public function exportPDF($company,$year,$month,$app) {
  406. $final = array();
  407. $id = Auth::guard('ds')->id();
  408. $user = Staff::with('StaffDetail')->find($id);
  409. $com = Company::where('_id',$user->StaffDetail->company_id)->first();
  410. $i = 0;
  411. $dataR = array(); $dataB = array(); $combineData = array();
  412. $count_cyberjaya = array(); $count_jasin = array();
  413. $extra = 0; $basic_total = 0; $totals = 0;
  414. if($year!='null' && $month!='null' && $app=='null'){
  415. $dataClaim = DealerClaim::with(['Subscriber' => function($q) {
  416. $q->with('Form');
  417. }],'Staff')->where('dealer',$company)->where('activated_my',$month.'/'.$year)->orderBy('activated_dt','DESC')->get();
  418. if(($year == '2019' || $year == '2020') && ($month == '12' || $month == '01')){
  419. foreach ($dataClaim as $key => $d) {
  420. if(date('d/m/Y', strtotime($d->activated_dt)) >= '12/12/2019' &&
  421. date('d/m/Y', strtotime($d->activated_dt)) <= '31/01/2020'){
  422. if (strpos($d['address'], 'Cyberjaya') !== false) {
  423. $count_cyberjaya[] = $d;
  424. }
  425. if (strpos($d['address'], 'Jasin') !== false) {
  426. $count_jasin[] = $d;
  427. }
  428. }
  429. }
  430. }else if($year == '2020' && ($month == '04' || $month == '05' || $month == '06' || $month == '07')){
  431. foreach ($dataClaim as $key => $d) {
  432. if(date('d/m/Y', strtotime($d->activated_dt)) >= '27/04/2020' &&
  433. date('d/m/Y', strtotime($d->activated_dt)) <= '31/07/2020'){
  434. if (strpos($d['address'], 'Cyberjaya') !== false) {
  435. $count_cyberjaya[] = $d;
  436. }
  437. if (strpos($d['address'], 'Jasin') !== false) {
  438. $count_jasin[] = $d;
  439. }
  440. }
  441. }
  442. }
  443. }else if($year!='null' && $month!='null' && $app!='null'){
  444. if($app == 'Residential'){
  445. $dataClaim = DealerClaim::with(['Subscriber' => function($q) {
  446. $q->with('Form');
  447. }],'Staff')->where('formT', 'R')->where('dealer',$company)->where('activated_my',$month.'/'.$year)->orderBy('activated_dt','DESC')->get();
  448. if(($year == '2019' || $year == '2020') && ($month == '12' || $month == '01')){
  449. foreach ($dataClaim as $key => $d) {
  450. if(date('d/m/Y', strtotime($d->activated_dt)) >= '12/12/2019' &&
  451. date('d/m/Y', strtotime($d->activated_dt)) <= '31/01/2020'){
  452. if (strpos($d['address'], 'Cyberjaya') !== false) {
  453. $count_cyberjaya[] = $d;
  454. }
  455. if (strpos($d['address'], 'Jasin') !== false) {
  456. $count_jasin[] = $d;
  457. }
  458. }
  459. }
  460. }else if($year == '2020' && ($month == '04' || $month == '05' || $month == '06' || $month == '07')){
  461. foreach ($dataClaim as $key => $d) {
  462. if(date('d/m/Y', strtotime($d->activated_dt)) >= '27/04/2020' &&
  463. date('d/m/Y', strtotime($d->activated_dt)) <= '31/07/2020'){
  464. if (strpos($d['address'], 'Cyberjaya') !== false) {
  465. $count_cyberjaya[] = $d;
  466. }
  467. if (strpos($d['address'], 'Jasin') !== false) {
  468. $count_jasin[] = $d;
  469. }
  470. }
  471. }
  472. }
  473. }else if($app == 'Business'){
  474. $dataClaim = DealerClaim::with(['Subscriber' => function($q) {
  475. $q->with('Form');
  476. }],'Staff')->where('formT', 'B')->where('dealer',$company)->where('activated_my',$month.'/'.$year)->orderBy('activated_dt','DESC')->get();
  477. }
  478. }
  479. if(!empty($dataClaim)){
  480. foreach ($dataClaim as $c){
  481. $address = ''; $name = '';
  482. if($c->Subscriber->Form->type_application == 'R'){
  483. if($c->Subscriber->street != ''){
  484. $address = $c->Subscriber->unit_no. ' , '.$c->Subscriber->building_name. ' , '.$c->Subscriber->street. ' , '.$c->Subscriber->postcode. ' , '.$c->Subscriber->city. ' , '.$c->Subscriber->state;
  485. }else {
  486. $address = $c->Subscriber->unit_no. ' , '.$c->Subscriber->building_name. ' , '.$c->Subscriber->postcode. ' , '.$c->Subscriber->city. ' , '.$c->Subscriber->state;
  487. }
  488. $name = $c->Subscriber->name;
  489. }else if($c->Subscriber->Form->type_application == 'B'){
  490. if($c->Subscriber->unit_no != ''){
  491. $address = $c->Subscriber->unit_no. ', '.$c->Subscriber->company_name. ', '.$c->Subscriber->street. ' , '.$c->Subscriber->postcode. ' , '.$c->Subscriber->city. ' , '.$c->Subscriber->state;
  492. }else {
  493. $address = $c->Subscriber->company_name. ', '.$c->Subscriber->street. ' , '.$c->Subscriber->postcode. ' , '.$c->Subscriber->city. ' , '.$c->Subscriber->state;
  494. }
  495. $name = $c->Subscriber->company_name;
  496. }
  497. $i++;
  498. array_push($final, array(
  499. 'index' => $c->formT,
  500. 'contract' => $c->contract,
  501. 'wo' => $c->wo,
  502. 'activated_dt' => $c->activated_dt,
  503. 'name' => $name,
  504. 'address' => $address,
  505. 'package' => $c->package,
  506. 'retail_price' => 'RM '.$c->retail_price,
  507. 'incentives1' => $c->incentives1,
  508. 'incentives2' => $c->incentives2,
  509. 'total' => 'RM' .$c->total_claim,
  510. 'claimed' => $c->claim_dt,
  511. ));
  512. }
  513. foreach($dataClaim as $f){
  514. $basic_total += $f->total_claim;
  515. }
  516. // Calculate extra incentves
  517. if($month == '01' && $year == '2020'){
  518. if(count($count_cyberjaya) > 50){
  519. $balance = count($count_cyberjaya) - 50;
  520. $extra += $balance * 50;
  521. }
  522. if(count($count_jasin) > 10){
  523. $balance = count($count_jasin) - 10;
  524. $extra += $balance * 50;
  525. }
  526. }else if($year == '2020' && ($month == '04' || $month == '05' || $month == '06' || $month == '07')){
  527. if(count($count_cyberjaya) > 50){
  528. $balance = count($count_cyberjaya) - 50;
  529. $extra += $balance * 50;
  530. }
  531. if(count($count_jasin) > 20){
  532. $balance = count($count_jasin) - 20;
  533. $extra += $balance * 50;
  534. }
  535. }else {
  536. $extra = 0;
  537. }
  538. $totals = $basic_total + $extra;
  539. $pdf = PDF::loadView('pdf.agent-claim', compact('final', 'month', 'year','com','basic_total','extra','totals'));
  540. $pdf->setPaper('A4', 'landscape');
  541. $pdfName = preg_replace('/\s+/', '-', $com->name).'-'.$month.'-'.$year.'.pdf';
  542. return $pdf->download( $pdfName );
  543. }
  544. }
  545. /***
  546. / Encrypt Password
  547. ***/
  548. public function encryptPassword($action, $string) {
  549. $output = false;
  550. $encrypt_method = "AES-256-CBC";
  551. $secret_key = '28472B4B6250655368566D5970337336';
  552. $secret_iv = md5(md5($string)); //'This is my secret iv';
  553. // hash
  554. $key = hash('sha256', $secret_key);
  555. $iv = substr(hash('sha256', $secret_iv), 0, 16);
  556. if ( $action == 'encrypt' ) {
  557. $output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
  558. $output = base64_encode($output);
  559. } else if( $action == 'decrypt' ) {
  560. $output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
  561. }
  562. return $output;
  563. }
  564. /***
  565. / Agent Form
  566. ***/
  567. public function showAgentForm(){
  568. $id = Auth::guard('ds')->id();
  569. $user = Staff::with('StaffDetail')->find($id);
  570. return view('dealer.add_agent',compact('user'));
  571. }
  572. /***
  573. / Add Agent
  574. ***/
  575. public function requestAddAgent(Request $request)
  576. {
  577. $validator = Validator::make($request->all(), [
  578. 'password' => 'min:8|confirmed',
  579. ]);
  580. if ($validator->fails()) {
  581. return redirect()->back()->withInput()->withErrors($validator);
  582. }
  583. $user = Staff::where('email', $request->get('email'))->first();
  584. if (empty($user)) {
  585. $pass = $this->encryptPassword('encrypt', $request->get('password'));
  586. $dealerD = new StaffDetail;
  587. $dealerD->name = $request->get('aname');
  588. $dealerD->ic = $request->get('ic');
  589. $dealerD->email = $request->get('email');
  590. $dealerD->phone = $request->get('phone');
  591. $dealerD->company_id = $request->get('company_id');
  592. $dealerD->color = '';
  593. $dealerD->position = 'Sales';
  594. $dealerD->password = $pass;
  595. $dealerD->user_pic = 'assets/avatar/user.png';
  596. // Save Login Dealer
  597. $loginD = new Staff;
  598. $loginD->email = $request->get('email');
  599. $loginD->password = $pass;
  600. $loginD->roles_access = 'Sales';
  601. $loginD->company_id = $request->get('company_id');
  602. $loginD->last_login_at = '';
  603. $loginD->last_login_ip = '';
  604. // $loginData = [
  605. // 'email' => $request->get('email'),
  606. // 'password' => $pass,
  607. // 'roles_access' => 'Sales',
  608. // 'last_login_at' => '',
  609. // 'last_login_ip' => '',
  610. // ];
  611. $company = Company::where('_id',$request->get('company_id'))->first();
  612. $company->staff()->save($loginD);
  613. // $dealer = DealerLaravel::create($loginData);
  614. $loginD->staffdetail()->save($dealerD);
  615. return redirect()->back()->with('success_msg', '<strong>Success!</strong> register <strong>'.$request->get('email').'</strong>');
  616. }
  617. else
  618. {
  619. return redirect()->back()->withInput()->withErrors('Email already exist!');
  620. }
  621. }
  622. public function editAgentDealer($agent_id)
  623. {
  624. $id = Auth::guard('ds')->id();
  625. $user = Staff::find($id);
  626. $email = $user->email;
  627. $userDetails = StaffDetail::where('email',$email)->first();
  628. $person = StaffDetail::where('_id', $agent_id)->first();
  629. return view('dealer.edit_agent', compact('person','userDetails','user'));
  630. }
  631. public function deleteAgent(Request $request)
  632. {
  633. $agent = Staff::where('_id',$request->id)->first();
  634. $agentdetail = $agent->staffdetail()->first();
  635. if(!empty($agentdetail) && !empty($agent))
  636. {
  637. $agent->delete();
  638. $agentdetail->delete();
  639. return 'true';
  640. }
  641. else
  642. {
  643. return 'false';
  644. }
  645. }
  646. //Show claim list by company
  647. public function mClaimListAll($company)
  648. {
  649. $id = Auth::guard('ds')->id();
  650. $user = Staff::with('StaffDetail')->find($id);
  651. $dealer_claimAll = DealerClaim::where('dealer',$company)->orderBy('activated_dt','desc')->get();
  652. $i = 0;
  653. $nested_data = array();
  654. foreach ($dealer_claimAll as $c){
  655. $customer = Subscriber::with('Form')->where('_id', $c->customer_id)->first();
  656. $address = '';
  657. if($customer->Form->type_application == 'R'){
  658. if($customer->street != ''){
  659. $address = $customer->unit_no. ' , '.$customer->building_name. ' , '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  660. }else {
  661. $address = $customer->unit_no. ' , '.$customer->building_name. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  662. }
  663. }else if($customer->Form->type_application == 'B'){
  664. if($customer->unit_no != ''){
  665. $address = $customer->unit_no. ', '.$customer->company_name. ', '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  666. }else {
  667. $address = $customer->company_name. ', '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  668. }
  669. }
  670. $i++;
  671. array_push($nested_data, array(
  672. 'index' => $c->formT,
  673. 'contract' => $c->contract,
  674. 'wo' => $c->wo,
  675. 'activated_dt' => $c->activated_dt,
  676. 'name' => $customer->name,
  677. 'address' => $address,
  678. 'package' => $c->package,
  679. 'retail_price' => 'RM '.$c->retail_price,
  680. 'incentives1' => '',
  681. 'incentives2' => '',
  682. 'total' => '',
  683. 'claimed' => $c->laim_dt,
  684. 'action' => 'null',
  685. ));
  686. }
  687. return \DataTables::of($nested_data)->make(true);
  688. }
  689. // Get Dealer Claim Residential
  690. public function returnDataR($company,$year,$month){
  691. $dataCR = DealerClaim::where('formT','R')->where('dealer',$company)->get();
  692. $final = array();
  693. // Month & Year
  694. $mY = '';
  695. if($year != 'null' && $month != 'null'){
  696. $mY = $month.'/'.$year;
  697. }
  698. if($mY != ''){
  699. $data12 = array(); $data24 = array();
  700. $countD12 = 0; $countD24 = 0;
  701. foreach($dataCR as $dc){
  702. if($dc->activated_my == $mY || $dc->activated_my == '0'.$mY ){
  703. if($dc->contract == "12"){
  704. $data12[] = $dc;
  705. }else if($dc->contract == "24"){
  706. $data24[] = $dc;
  707. }
  708. }
  709. }
  710. $countD24 = count($data24);
  711. $countD12 = count($data12);
  712. $comm = Commission::where('formT','R')->get();
  713. $commV12 = 0; $commV24 = 0;
  714. $comm12 = ''; $comm24 = '';
  715. foreach($comm as $c){
  716. if($c->contract =='12'){
  717. if(strpos($c->ranges, '>') !== false) {
  718. $pieces = explode(">", $c->ranges);
  719. if($countD12 >= intval($pieces[1])){
  720. $commV12 = floatval($c->comm)/100;
  721. $comm12 = $c->comm.'%';
  722. }
  723. }
  724. if(strpos($c->ranges,'-') !== false){
  725. $pieces = explode("-", $c->ranges);
  726. if($countD12 >= intval($pieces[0]) && $countD12 <= intval($pieces[1])){
  727. $commV12 = floatval($c->comm)/100;
  728. $comm12 = $c->comm.'%';
  729. }
  730. }
  731. }else if($c->contract =='24'){
  732. // Contract => 24
  733. if($countD24 != 0){
  734. if(strpos($c->ranges, '>') !== false) {
  735. $pieces = explode(">", $c->ranges);
  736. if($countD24 > intval($pieces[1])){
  737. $commV24 = floatval($c->comm)/100;
  738. $comm24 = $c->comm.'%';
  739. }
  740. }
  741. if(strpos($c->ranges,'-') !== false){
  742. $pieces = explode("-", $c->ranges);
  743. if($countD24 >= intval($pieces[0]) && $countD24 <= intval($pieces[1])){
  744. $commV24 = floatval($c->comm)/100;
  745. $comm24 = $c->comm.'%';
  746. }
  747. }
  748. }
  749. }
  750. }
  751. foreach ($data12 as $key => $d) {
  752. $customer = Subscriber::with('Form')->where('_id', $d['customer_id'])->first();
  753. $address = ''; $name = '';
  754. if($customer->Form->type_application == 'R'){
  755. if($customer->street != ''){
  756. $address = $customer->unit_no. ' , '.$customer->building_name. ' , '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  757. }else {
  758. $address = $customer->unit_no. ' , '.$customer->building_name. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  759. }
  760. $name = $customer->name;
  761. }else if($customer->Form->type_application == 'B'){
  762. if($customer->unit_no != ''){
  763. $address = $customer->unit_no. ', '.$customer->company_name. ', '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  764. }else {
  765. $address = $customer->company_name. ', '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  766. }
  767. $name = $customer->company_name;
  768. }
  769. $final[] = array(
  770. 'index' => $d['formT'],
  771. 'contract' => $d['contract'],
  772. 'wo' => $d['wo'],
  773. 'activation' => $d['activated_dt'],
  774. 'name' => $name,
  775. 'address' => $address,
  776. 'package' => $d['package'],
  777. 'retail_price' => 'RM '.$d['retail_price'],
  778. 'incentives1' => $comm12,
  779. 'incentives2' => '',
  780. 'total' => 'RM '.number_format($d['retail_price']*$commV12,2),
  781. 'claim' => $d['claim_dt'],
  782. );
  783. }
  784. foreach ($data24 as $key => $d) {
  785. $customer = Subscriber::with('Form')->where('_id', $d['customer_id'])->first();
  786. $address = ''; $name = '';
  787. if($customer->Form->type_application == 'R'){
  788. if($customer->street != ''){
  789. $address = $customer->unit_no. ' , '.$customer->building_name. ' , '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  790. }else {
  791. $address = $customer->unit_no. ' , '.$customer->building_name. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  792. }
  793. $name = $customer->name;
  794. }else if($customer->Form->type_application == 'B'){
  795. if($customer->unit_no != ''){
  796. $address = $customer->unit_no. ', '.$customer->company_name. ', '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  797. }else {
  798. $address = $customer->company_name. ', '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  799. }
  800. $name = $customer->company_name;
  801. }
  802. $final[] = array(
  803. 'index' => $d['formT'],
  804. 'contract' => $d['contract'],
  805. 'wo' => $d['wo'],
  806. 'activation' => $d['activated_dt'],
  807. 'name' => $name,
  808. 'address' => $address,
  809. 'package' => $d['package'],
  810. 'retail_price' => 'RM '.$d['retail_price'],
  811. 'incentives1' => $comm24,
  812. 'incentives2' => '',
  813. 'total' => 'RM '.number_format($d['retail_price']*$commV24,2),
  814. 'claim' => $d['claim_dt'],
  815. );
  816. }
  817. }else {
  818. foreach ($dataCR as $c){
  819. $customer = Subscriber::with('Form')->where('_id', $c['customer_id'])->first();
  820. $address = ''; $name = '';
  821. if($customer->Form->type_application == 'R'){
  822. if($customer->street != ''){
  823. $address = $customer->unit_no. ' , '.$customer->building_name. ' , '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  824. }else {
  825. $address = $customer->unit_no. ' , '.$customer->building_name. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  826. }
  827. $name = $customer->name;
  828. }else if($customer->Form->type_application == 'B'){
  829. if($customer->unit_no != ''){
  830. $address = $customer->unit_no. ', '.$customer->company_name. ', '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  831. }else {
  832. $address = $customer->company_name. ', '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  833. }
  834. $name = $customer->company_name;
  835. }
  836. $final[] = array(
  837. 'index' => $c['formT'],
  838. 'contract' => $c['contract'],
  839. 'wo' => $c['wo'],
  840. 'activation' => $c['activated_dt'],
  841. 'name' => $name,
  842. 'address' => $address,
  843. 'package' => $c['package'],
  844. 'retail_price' => 'RM '.$c['retail_price'],
  845. 'incentives1' => '',
  846. 'incentives2' => '',
  847. 'total' => '',
  848. 'claim' => $c['claim_dt'],
  849. );
  850. }
  851. }
  852. return $final;
  853. }
  854. // Get Claim Business
  855. public function returnDataB($company,$year,$month){
  856. $dataCB = DealerClaim::where('formT','B')->where('dealer',$company)->get();
  857. $final = array();
  858. // Month & Year
  859. $mY = '';
  860. if($year != 'null' && $month != 'null'){
  861. $mY = $month.'/'.$year;
  862. }
  863. if($mY != ''){
  864. /** Contract 12
  865. // 1=>10-100 2=>300 3=>500 4=>1
  866. **/
  867. $data12_1 = array(); $data12_2 = array();
  868. $data12_3 = array(); $data12_4 = array();
  869. $countD12_1 = 0; $countD12_2 = 0;
  870. $countD12_3 = 0; $countD12_4 = 0;
  871. /** Contract 24
  872. // 1=>10-100 2=>300 3=>500 4=>1
  873. **/
  874. $data24_1 = array(); $data24_2 = array();
  875. $data24_3 = array(); $data24_4 = array();
  876. $countD24_1 = 0; $countD24_2 = 0;
  877. $countD24_3 = 0; $countD24_4 = 0;
  878. foreach($dataCB as $dc){
  879. if($dc->activated_my == $mY || $dc->activated_my == '0'.$mY ){
  880. if($dc->contract == "12" && ($dc->package == "10" || $dc->package == "30" || $dc->package == "50" || $dc->package == "100")){
  881. $data12_1[] = $dc;
  882. }else if($dc->contract == "12" && $dc->package == "300"){
  883. $data12_2[] = $dc;
  884. }else if($dc->contract == "12" && $dc->package == "500"){
  885. $data12_3[] = $dc;
  886. }else if($dc->contract == "12" && $dc->package == "1"){
  887. $data12_4[] = $dc;
  888. }else if($dc->contract == "24" && ($dc->package == "10" || $dc->package == "30" || $dc->package == "50" || $dc->package == "100")){
  889. $data24_1[] = $dc;
  890. }else if($dc->contract == "24" && $dc->package == "300"){
  891. $data24_2[] = $dc;
  892. }else if($dc->contract == "24" && $dc->package == "500"){
  893. $data24_3[] = $dc;
  894. }else if($dc->contract == "24" && $dc->package == "1"){
  895. $data24_4[] = $dc;
  896. }
  897. }
  898. }
  899. $countD12_1 = count($data12_1); $countD12_2 = count($data12_2);
  900. $countD12_3 = count($data12_3); $countD12_4 = count($data12_4);
  901. $countD24_1 = count($data24_1); $countD24_2 = count($data24_2);
  902. $countD24_3 = count($data24_3); $countD24_4 = count($data24_4);
  903. $comm = Commission::where('formT','B')->get();
  904. $commV12_1 = 0; $commV12_2 = 0;
  905. $commV12_3 = 0; $commV12_4 = 0;
  906. $commV24_1 = 0; $commV24_2 = 0;
  907. $commV24_3 = 0; $commV24_4 = 0;
  908. $comm12_1 = ''; $comm12_2 = '';
  909. $comm12_3 = ''; $comm12_4 = '';
  910. $comm24_1 = ''; $comm24_2 = '';
  911. $comm24_3 = ''; $comm24_4 = '';
  912. $addRM12_1 = ''; $addRM12_2 = '';
  913. $addRM12_3 = ''; $addRM12_4 = '';
  914. $addRM24_1 = ''; $addRM24_2 = '';
  915. $addRM24_3 = ''; $addRM24_4 = '';
  916. foreach($comm as $c){
  917. /*** Contract 24 **/
  918. if($c->contract =='24' && $c->packageR == "300"){
  919. if(strpos($c->ranges, '>') !== false) {
  920. $pieces = explode(">", $c->ranges);
  921. if($countD24_2 >= intval($pieces[1])){
  922. if(strpos($c->comm, 'RM') !== false) {
  923. $comm24_2 = '+ ' .$c->comm;
  924. $commV24_2 = '';
  925. $addRM24_2 = $c->comm;
  926. }else {
  927. $comms = explode("RM", $c->comm);
  928. if(!empty($comms[1])){
  929. $comm24_2 = 'RM '.$comms[1];
  930. $commV24_2 = floatval($comms[1]);
  931. }else {
  932. if($comms[0] != ''){
  933. $comm24_2 = $comms[0].'%';
  934. $commV24_2 = floatval($comms[0])/100;
  935. }
  936. }
  937. }
  938. }
  939. }
  940. }else if($c->contract =='24' && $c->packageR == "500"){
  941. if(strpos($c->ranges, '>') !== false) {
  942. $pieces = explode(">", $c->ranges);
  943. if($countD24_3 >= intval($pieces[1])){
  944. if(strpos($c->comm, 'RM') !== false) {
  945. $comm24_3 = '+ ' .$c->comm;
  946. $commV24_3 = '';
  947. $addRM24_3 = $c->comm;
  948. }else {
  949. $comms = explode("RM", $c->comm);
  950. if(isset($comms[1])){
  951. $comm24_3 = 'RM '.$comms[1];
  952. $commV24_3 = floatval($comms[1]);
  953. }else {
  954. if($comms[0] != ''){
  955. $comm24_3 = $comms[0].'%';
  956. $commV24_3 = floatval($comms[0])/100;
  957. }
  958. }
  959. }
  960. }
  961. }
  962. }else if($c->contract =='24' && $c->packageR == "1"){
  963. if(strpos($c->ranges, '>') !== false) {
  964. $pieces = explode(">", $c->ranges);
  965. if($countD24_4 >= intval($pieces[1])){
  966. if(strpos($c->comm, 'RM') !== false) {
  967. $comm24_4 = '+ ' .$c->comm;
  968. $commV24_4 = '';
  969. $addRM24_4 = $c->comm;
  970. }else {
  971. $comms = explode("RM", $c->comm);
  972. if(isset($comms[1])){
  973. $comm24_4 = 'RM '.$comms[1];
  974. $commV24_4 = floatval($comms[1]);
  975. }else {
  976. if($comms[0] != ''){
  977. $comm24_4 = $comms[0].'%';
  978. $commV24_4 = floatval($comms[0])/100;
  979. }
  980. }
  981. }
  982. }
  983. }
  984. }else if($c->contract =='24' && $c->packageR == "10,30,50,100"){
  985. if(strpos($c->ranges,'-') !== false){
  986. $pieces = explode("-", $c->ranges);
  987. if($countD24_1 >= intval($pieces[0]) && $countD24_1 <= intval($pieces[1])){
  988. if(strpos($c->comm, 'RM') !== false) {
  989. $comm24_1 = '+ ' .$c->comm;
  990. $commV24_1 = '';
  991. $addRM24_1 = $c->comm;
  992. }else {
  993. $comms = explode("RM", $c->comm);
  994. if(!empty($comms[1])){
  995. $comm24_1 = 'RM '.$comms[1];
  996. $commV24_1 = floatval($comms[1]);
  997. }else {
  998. if($comms[0] != ''){
  999. $comm24_1 = $comms[0].'%';
  1000. $commV24_1 = floatval($comms[0])/100;
  1001. }
  1002. }
  1003. }
  1004. }
  1005. }
  1006. if(strpos($c->ranges, '>') !== false) {
  1007. $pieces1 = explode(">", $c->ranges);
  1008. if($countD24_1 >= intval($pieces1[1])){
  1009. if(strpos($c->comm, "RM") === true) {
  1010. $comm24_1 = '+ ' .$c->comm;
  1011. $commV24_1 = '';
  1012. $addRM24_1 = $c->comm;
  1013. }else {
  1014. $comms = explode("RM", $c->comm);
  1015. if(empty($comms[1])){
  1016. $comm24_1 = 'RM '.$comms[1];
  1017. $commV24_1 = floatval($comms[1]);
  1018. }else {
  1019. if($comms[0] != ''){
  1020. $comm24_1 = str_replace ('+ ','',$comms[0]).'%';
  1021. $commV24_1 = floatval($comms[0])/100;
  1022. $addRM24_1 = $comms[1];
  1023. }
  1024. }
  1025. }
  1026. }
  1027. }
  1028. }
  1029. /*** Contract 12 **/
  1030. else if($c->contract =='12' && $c->packageR == "10,30,50,100"){
  1031. if(strpos($c->ranges, '>') !== false) {
  1032. $pieces = explode(">", $c->ranges);
  1033. if($countD12_1 >= intval($pieces[1])){
  1034. if(strpos($c->comm, 'RM') !== false) {
  1035. $comm12_1 = '+ ' .$c->comm;
  1036. $commV12_1 = '';
  1037. $addRM12_1 = $c->comm;
  1038. }else {
  1039. $comms = explode("RM", $c->comm);
  1040. if(isset($comms[1])){
  1041. $comm12_1 = 'RM '.$comms[1];
  1042. $commV12_1 = floatval($comms[1]);
  1043. }else {
  1044. if($comms[0] != ''){
  1045. $comm12_1 = $comms[0].'%';
  1046. $commV12_1 = floatval($comms[0])/100;
  1047. }
  1048. }
  1049. }
  1050. }
  1051. }
  1052. if(strpos($c->ranges,'-') !== false){
  1053. $pieces = explode("-", $c->ranges);
  1054. if($countD12_1 >= intval($pieces[0]) && $countD12_1 <= intval($pieces[1])){
  1055. if(strpos($c->comm, 'RM') !== false) {
  1056. $comm12_1 = '+ ' .$c->comm;
  1057. $commV12_1 = '';
  1058. $addRM12_1 = $c->comm;
  1059. }else {
  1060. $comms = explode("RM", $c->comm);
  1061. if(isset($comms[1])){
  1062. $comm12_1 = 'RM '.$comms[1];
  1063. $commV12_1 = floatval($comms[1]);
  1064. }else {
  1065. if($comms[0] != ''){
  1066. $comm12_1 = $comms[0].'%';
  1067. $commV12_1 = floatval($comms[0])/100;
  1068. }
  1069. }
  1070. }
  1071. }
  1072. }
  1073. }else if($c->contract =='12' && $c->packageR == "300"){
  1074. if(strpos($c->ranges, '>') !== false) {
  1075. $pieces = explode(">", $c->ranges);
  1076. if($countD12_2 >= intval($pieces[1])){
  1077. if(strpos($c->comm, 'RM') !== false) {
  1078. $comm12_2 = '+ ' .$c->comm;
  1079. $commV12_2 = '';
  1080. $addRM12_2 = $c->comm;
  1081. }else {
  1082. $comms = explode("RM", $c->comm);
  1083. if(isset($comms[1])){
  1084. $comm12_2 = 'RM '.$comms[1];
  1085. $commV12_2 = floatval($comms[1]);
  1086. }else {
  1087. if($comms[0] != ''){
  1088. $comm12_2 = $comms[0].'%';
  1089. $commV12_2 = floatval($comms[0])/100;
  1090. }
  1091. }
  1092. }
  1093. }
  1094. }
  1095. }else if($c->contract =='12' && $c->packageR == "500"){
  1096. if(strpos($c->ranges, '>') !== false) {
  1097. $pieces = explode(">", $c->ranges);
  1098. if($countD12_3 >= intval($pieces[1])){
  1099. if(strpos($c->comm, 'RM') !== false) {
  1100. $comm12_3 = '+ ' .$c->comm;
  1101. $commV12_3 = '';
  1102. $addRM12_3 = $c->comm;
  1103. }else {
  1104. $comms = explode("RM", $c->comm);
  1105. if(isset($comms[1])){
  1106. $comm12_3 = 'RM '.$comms[1];
  1107. $commV12_3 = floatval($comms[1]);
  1108. }else {
  1109. if($comms[0] != ''){
  1110. $comm12_3 = $comms[0].'%';
  1111. $commV12_3 = floatval($comms[0])/100;
  1112. }
  1113. }
  1114. }
  1115. }
  1116. }
  1117. }else if($c->contract =='12' && $c->packageR == "1"){
  1118. if(strpos($c->ranges, '>') !== false) {
  1119. $pieces = explode(">", $c->ranges);
  1120. if($countD12_4 >= intval($pieces[1])){
  1121. if(strpos($c->comm, 'RM') !== false) {
  1122. $comm12_4 = '+ ' .$c->comm;
  1123. $commV12_4 = '';
  1124. $addRM12_4 = $c->comm;
  1125. }else {
  1126. $comms = explode("RM", $c->comm);
  1127. if(isset($comms[1])){
  1128. $comm12_4 = 'RM '.$comms[1];
  1129. $commV12_4 = floatval($comms[1]);
  1130. }else {
  1131. if($comms[0] != ''){
  1132. $comm12_4 = $comms[0].'%';
  1133. $commV12_4 = floatval($comms[0])/100;
  1134. }
  1135. }
  1136. }
  1137. }
  1138. }
  1139. }
  1140. }
  1141. // Category 10,30,50,100
  1142. foreach ($data12_1 as $key => $d) {
  1143. $customer = Subscriber::with('Form')->where('_id', $d['customer_id'])->first();
  1144. $address = ''; $name = '';
  1145. if($customer->Form->type_application == 'R'){
  1146. if($customer->street != ''){
  1147. $address = $customer->unit_no. ' , '.$customer->building_name. ' , '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1148. }else {
  1149. $address = $customer->unit_no. ' , '.$customer->building_name. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1150. }
  1151. $name = $customer->name;
  1152. }else if($customer->Form->type_application == 'B'){
  1153. if($customer->unit_no != ''){
  1154. $address = $customer->unit_no. ', '.$customer->company_name. ', '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1155. }else {
  1156. $address = $customer->company_name. ', '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1157. }
  1158. $name = $customer->company_name;
  1159. }
  1160. $total = '';
  1161. if($commV12_1 == ''){
  1162. $total = $addRM12_1;
  1163. }else {
  1164. $total = number_format($d['retail_price']*$commV12_1,2);
  1165. }
  1166. $final[] = array(
  1167. 'index' => $d['formT'],
  1168. 'contract' => $d['contract'],
  1169. 'wo' => $d['wo'],
  1170. 'activation' => $d['activated_dt'],
  1171. 'name' => $name,
  1172. 'address' => $address,
  1173. 'package' => $d['package'],
  1174. 'retail_price' => 'RM '.$d['retail_price'],
  1175. 'incentives1' => $comm12_1,
  1176. 'incentives2' => $addRM12_1,
  1177. 'total' => 'RM '.$total,
  1178. 'claim' => $c['claim_dt'],
  1179. );
  1180. }
  1181. foreach ($data24_1 as $key => $d) {
  1182. $customer = Subscriber::with('Form')->where('_id', $d['customer_id'])->first();
  1183. $address = ''; $name = '';
  1184. if($customer->Form->type_application == 'R'){
  1185. if($customer->street != ''){
  1186. $address = $customer->unit_no. ' , '.$customer->building_name. ' , '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1187. }else {
  1188. $address = $customer->unit_no. ' , '.$customer->building_name. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1189. }
  1190. $name = $customer->name;
  1191. }else if($customer->Form->type_application == 'B'){
  1192. if($customer->unit_no != ''){
  1193. $address = $customer->unit_no. ', '.$customer->company_name. ', '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1194. }else {
  1195. $address = $customer->company_name. ', '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1196. }
  1197. $name = $customer->company_name;
  1198. }
  1199. $total = '';
  1200. if($commV24_1 == ''){
  1201. $total = $addRM24_1;
  1202. }else {
  1203. $total = number_format(($d['retail_price']*$commV24_1) + floatval($addRM24_1),2);
  1204. }
  1205. $final[] = array(
  1206. 'index' => $d['formT'],
  1207. 'contract' => $d['contract'],
  1208. 'wo' => $d['wo'],
  1209. 'activation' => $d['activated_dt'],
  1210. 'name' => $name,
  1211. 'address' => $address,
  1212. 'package' => $d['package'],
  1213. 'retail_price' => 'RM '.$d['retail_price'],
  1214. 'incentives1' => $comm24_1,
  1215. 'incentives2' => $addRM24_1,
  1216. 'total' => 'RM '.$total,
  1217. 'claim' => $c['claim_dt'],
  1218. );
  1219. }
  1220. // Category 300
  1221. foreach ($data12_2 as $key => $d) {
  1222. $customer = Subscriber::with('Form')->where('_id', $d['customer_id'])->first();
  1223. $address = ''; $name = '';
  1224. if($customer->Form->type_application == 'R'){
  1225. if($customer->street != ''){
  1226. $address = $customer->unit_no. ' , '.$customer->building_name. ' , '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1227. }else {
  1228. $address = $customer->unit_no. ' , '.$customer->building_name. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1229. }
  1230. $name = $customer->name;
  1231. }else if($customer->Form->type_application == 'B'){
  1232. if($customer->unit_no != ''){
  1233. $address = $customer->unit_no. ', '.$customer->company_name. ', '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1234. }else {
  1235. $address = $customer->company_name. ', '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1236. }
  1237. $name = $customer->company_name;
  1238. }
  1239. $total = '';
  1240. if($commV12_2 == ''){
  1241. $total = $addRM12_2;
  1242. }else {
  1243. $total = number_format($d['retail_price']*$commV12_2,2);
  1244. }
  1245. $final[] = array(
  1246. 'index' => $d['formT'],
  1247. 'contract' => $d['contract'],
  1248. 'wo' => $d['wo'],
  1249. 'activation' => $d['activated_dt'],
  1250. 'name' => $name,
  1251. 'address' => $address,
  1252. 'package' => $d['package'],
  1253. 'retail_price' => 'RM '.$d['retail_price'],
  1254. 'incentives1' => $comm12_2,
  1255. 'incentives2' => $addRM12_2,
  1256. 'total' => 'RM '.$total,
  1257. 'claim' => $c['claim_dt'],
  1258. );
  1259. }
  1260. foreach ($data24_2 as $key => $d) {
  1261. $customer = Subscriber::with('Form')->where('_id', $d['customer_id'])->first();
  1262. $address = ''; $name = '';
  1263. if($customer->Form->type_application == 'R'){
  1264. if($customer->street != ''){
  1265. $address = $customer->unit_no. ' , '.$customer->building_name. ' , '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1266. }else {
  1267. $address = $customer->unit_no. ' , '.$customer->building_name. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1268. }
  1269. $name = $customer->name;
  1270. }else if($customer->Form->type_application == 'B'){
  1271. if($customer->unit_no != ''){
  1272. $address = $customer->unit_no. ', '.$customer->company_name. ', '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1273. }else {
  1274. $address = $customer->company_name. ', '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1275. }
  1276. $name = $customer->company_name;
  1277. }
  1278. $total = '';
  1279. if($commV24_2 == ''){
  1280. $total = $addRM24_2;
  1281. }else {
  1282. $total = number_format($d['retail_price']*$commV24_2,2);
  1283. }
  1284. $final[] = array(
  1285. 'index' => $d['formT'],
  1286. 'contract' => $d['contract'],
  1287. 'wo' => $d['wo'],
  1288. 'activation' => $d['activated_dt'],
  1289. 'name' => $name,
  1290. 'address' => $address,
  1291. 'package' => $d['package'],
  1292. 'retail_price' => 'RM '.$d['retail_price'],
  1293. 'incentives1' => $comm24_2,
  1294. 'incentives2' => $addRM24_2,
  1295. 'total' => 'RM '.$total,
  1296. 'claim' => $c['claim_dt'],
  1297. );
  1298. }
  1299. // Category 500
  1300. foreach ($data12_3 as $key => $d) {
  1301. $customer = Subscriber::with('Form')->where('_id', $d['customer_id'])->first();
  1302. $address = ''; $name = $customer->name;
  1303. if($customer->Form->type_application == 'R'){
  1304. if($customer->street != ''){
  1305. $address = $customer->unit_no. ' , '.$customer->building_name. ' , '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1306. }else {
  1307. $address = $customer->unit_no. ' , '.$customer->building_name. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1308. }
  1309. }else if($customer->Form->type_application == 'B'){
  1310. if($customer->unit_no != ''){
  1311. $address = $customer->unit_no. ', '.$customer->company_name. ', '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1312. }else {
  1313. $address = $customer->company_name. ', '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1314. }
  1315. $name = $customer->company_name;
  1316. }
  1317. $total = '';
  1318. if($commV12_3 == ''){
  1319. $total = $addRM12_3;
  1320. }else {
  1321. $total = number_format($d['retail_price']*$commV12_3,2);
  1322. }
  1323. $final[] = array(
  1324. 'index' => $d['formT'],
  1325. 'contract' => $d['contract'],
  1326. 'wo' => $d['wo'],
  1327. 'activation' => $d['activated_dt'],
  1328. 'name' => $name,
  1329. 'address' => $address,
  1330. 'package' => $d['package'],
  1331. 'retail_price' => 'RM '.$d['retail_price'],
  1332. 'incentives1' => $comm12_3,
  1333. 'incentives2' => $addRM12_3,
  1334. 'total' => 'RM '.$total,
  1335. 'claim' => $c['claim_dt'],
  1336. );
  1337. }
  1338. foreach ($data24_3 as $key => $d) {
  1339. $customer = Subscriber::with('Form')->where('_id', $d['customer_id'])->first();
  1340. $address = ''; $name = '';
  1341. if($customer->Form->type_application == 'R'){
  1342. if($customer->street != ''){
  1343. $address = $customer->unit_no. ' , '.$customer->building_name. ' , '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1344. }else {
  1345. $address = $customer->unit_no. ' , '.$customer->building_name. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1346. }
  1347. $name = $customer->name;
  1348. }else if($customer->Form->type_application == 'B'){
  1349. if($customer->unit_no != ''){
  1350. $address = $customer->unit_no. ', '.$customer->company_name. ', '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1351. }else {
  1352. $address = $customer->company_name. ', '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1353. }
  1354. $name = $customer->company_name;
  1355. }
  1356. $total = '';
  1357. if($commV24_3 == ''){
  1358. $total = $addRM24_3;
  1359. }else {
  1360. $total = number_format($d['retail_price']*$commV24_3,2);
  1361. }
  1362. $final[] = array(
  1363. 'index' => $d['formT'],
  1364. 'contract' => $d['contract'],
  1365. 'wo' => $d['wo'],
  1366. 'activation' => $d['activated_dt'],
  1367. 'name' => $name,
  1368. 'address' => $address,
  1369. 'package' => $d['package'],
  1370. 'retail_price' => 'RM '.$d['retail_price'],
  1371. 'incentives1' => $comm24_3,
  1372. 'incentives2' => $addRM24_3,
  1373. 'total' => 'RM '.$total,
  1374. 'claim' => $c['claim_dt'],
  1375. );
  1376. }
  1377. // Category 1
  1378. foreach ($data12_4 as $key => $d) {
  1379. $customer = Subscriber::with('Form')->where('_id', $d['customer_id'])->first();
  1380. $address = ''; $name = '';
  1381. if($customer->Form->type_application == 'R'){
  1382. if($customer->street != ''){
  1383. $address = $customer->unit_no. ' , '.$customer->building_name. ' , '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1384. }else {
  1385. $address = $customer->unit_no. ' , '.$customer->building_name. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1386. }
  1387. $name = $customer->name;
  1388. }else if($customer->Form->type_application == 'B'){
  1389. if($customer->unit_no != ''){
  1390. $address = $customer->unit_no. ', '.$customer->company_name. ', '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1391. }else {
  1392. $address = $customer->company_name. ', '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1393. }
  1394. $name = $customer->company_name;
  1395. }
  1396. $total = '';
  1397. if($commV12_4 == ''){
  1398. $total = $addRM12_4;
  1399. }else {
  1400. $total = number_format($d['retail_price']*$commV12_4,2);
  1401. }
  1402. $final[] = array(
  1403. 'index' => $d['formT'],
  1404. 'contract' => $d['contract'],
  1405. 'wo' => $d['wo'],
  1406. 'activation' => $d['activated_dt'],
  1407. 'name' => $name,
  1408. 'address' => $address,
  1409. 'package' => $d['package'],
  1410. 'retail_price' => 'RM '.$d['retail_price'],
  1411. 'incentives1' => $comm12_4,
  1412. 'incentives2' => $addRM12_4,
  1413. 'total' => 'RM '.$total,
  1414. 'claim' => $c['claim_dt'],
  1415. );
  1416. }
  1417. foreach ($data24_4 as $key => $d) {
  1418. $customer = Subscriber::with('Form')->where('_id', $d['customer_id'])->first();
  1419. $address = ''; $name = '';
  1420. if($customer->Form->type_application == 'R'){
  1421. if($customer->street != ''){
  1422. $address = $customer->unit_no. ' , '.$customer->building_name. ' , '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1423. }else {
  1424. $address = $customer->unit_no. ' , '.$customer->building_name. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1425. }
  1426. $name = $customer->name;
  1427. }else if($customer->Form->type_application == 'B'){
  1428. if($customer->unit_no != ''){
  1429. $address = $customer->unit_no. ', '.$customer->company_name. ', '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1430. }else {
  1431. $address = $customer->company_name. ', '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1432. }
  1433. $name = $customer->company_name;
  1434. }
  1435. $total = '';
  1436. if($commV24_4 == ''){
  1437. $total = $addRM24_4;
  1438. }else {
  1439. $total = number_format($d['retail_price']*$commV24_4,2);
  1440. }
  1441. $final[] = array(
  1442. 'index' => $d['formT'],
  1443. 'contract' => $d['contract'],
  1444. 'wo' => $d['wo'],
  1445. 'activation' => $d['activated_dt'],
  1446. 'name' => $name,
  1447. 'address' => $address,
  1448. 'package' => $d['package'],
  1449. 'retail_price' => 'RM '.$d['retail_price'],
  1450. 'incentives1' => $comm24_4,
  1451. 'incentives2' => $addRM24_4,
  1452. 'total' => 'RM '.$total,
  1453. 'claim' => $c['claim_dt'],
  1454. );
  1455. }
  1456. }else {
  1457. foreach ($dataCB as $c){
  1458. $customer = Subscriber::with('Form')->where('_id', $c['customer_id'])->first();
  1459. $address = ''; $name = '';
  1460. if($customer->Form->type_application == 'R'){
  1461. if($customer->street != ''){
  1462. $address = $customer->unit_no. ' , '.$customer->building_name. ' , '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1463. }else {
  1464. $address = $customer->unit_no. ' , '.$customer->building_name. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1465. }
  1466. $name = $customer->name;
  1467. }else if($customer->Form->type_application == 'B'){
  1468. if($customer->unit_no != ''){
  1469. $address = $customer->unit_no. ', '.$customer->company_name. ', '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1470. }else {
  1471. $address = $customer->company_name. ', '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1472. }
  1473. $name = $customer->company_name;
  1474. }
  1475. $final[] = array(
  1476. 'index' => $c['formT'],
  1477. 'contract' => $c['contract'],
  1478. 'wo' => $c['wo'],
  1479. 'activation' => $c['activated_dt'],
  1480. 'name' => $name,
  1481. 'address' => $address,
  1482. 'package' => $c['package'],
  1483. 'retail_price' => 'RM '.$c['retail_price'],
  1484. 'incentives1' => '',
  1485. 'incentives2' => '',
  1486. 'total' => '',
  1487. 'claim' => $c['claim_dt'],
  1488. );
  1489. }
  1490. }
  1491. return $final;
  1492. }
  1493. /***
  1494. / Return Claim & Combined Business && Resident
  1495. ***/
  1496. public function getClaimRB($company,$year,$month,$app) {
  1497. $dataR = array(); $dataB = array(); $combineData = array();
  1498. if($month=='null' && $year=='null' && $app=='null'){
  1499. return $this->mClaimListAll($company);
  1500. }else if($year!='null' && $month!='null' && $app=='null'){
  1501. $dataClaim = DealerClaim::with(['Subscriber' => function($q) {
  1502. $q->with('Form');
  1503. }],'Staff')->where('dealer',$company)->where('activated_my',$month.'/'.$year)->orderBy('activated_dt','DESC')->get();
  1504. if(($year == '2019' || $year == '2020') && ($month == '12' || $month == '01')){
  1505. foreach ($dataClaim as $key => $d) {
  1506. if(date('d/m/Y', strtotime($d->activated_dt)) >= '12/12/2019' &&
  1507. date('d/m/Y', strtotime($d->activated_dt)) <= '31/01/2020'){
  1508. if (strpos($d['address'], 'Cyberjaya') !== false) {
  1509. $count_cyberjaya[] = $d;
  1510. }
  1511. if (strpos($d['address'], 'Jasin') !== false) {
  1512. $count_jasin[] = $d;
  1513. }
  1514. }
  1515. }
  1516. }else if($year == '2020' && ($month == '04' || $month == '05' || $month == '06' || $month == '07')){
  1517. foreach ($dataClaim as $key => $d) {
  1518. if(date('d/m/Y', strtotime($d->activated_dt)) >= '27/04/2020' &&
  1519. date('d/m/Y', strtotime($d->activated_dt)) <= '31/07/2020'){
  1520. if (strpos($d['address'], 'Cyberjaya') !== false) {
  1521. $count_cyberjaya[] = $d;
  1522. }
  1523. if (strpos($d['address'], 'Jasin') !== false) {
  1524. $count_jasin[] = $d;
  1525. }
  1526. }
  1527. }
  1528. }
  1529. }else if($year=='null' && $month=='null' && $app!='null'){
  1530. // if($app == 'Residential'){
  1531. // $dataClaim = DealerClaim::with(['Subscriber' => function($q) {
  1532. // $q->with('Form');
  1533. // }],'Staff')->where('formT', 'R')->where('dealer',$company)->where('activated_my',$month.'/'.$year)->orderBy('activated_dt','DESC')->get();
  1534. // }else if($app == 'Business'){
  1535. // $dataClaim = DealerClaim::with(['Subscriber' => function($q) {
  1536. // $q->with('Form');
  1537. // }],'Staff')->where('formT', 'B')->where('dealer',$company)->where('activated_my',$month.'/'.$year)->orderBy('activated_dt','DESC')->get();
  1538. // }
  1539. }else if($year!='null' && $month!='null' && $app!='null'){
  1540. if($app == 'Residential'){
  1541. $dataClaim = DealerClaim::with(['Subscriber' => function($q) {
  1542. $q->with('Form');
  1543. }],'Staff')->where('formT', 'R')->where('dealer',$company)->where('activated_my',$month.'/'.$year)->orderBy('activated_dt','DESC')->get();
  1544. if(($year == '2019' || $year == '2020') && ($month == '12' || $month == '01')){
  1545. foreach ($dataClaim as $key => $d) {
  1546. if(date('d/m/Y', strtotime($d->activated_dt)) >= '12/12/2019' &&
  1547. date('d/m/Y', strtotime($d->activated_dt)) <= '31/01/2020'){
  1548. if (strpos($d['address'], 'Cyberjaya') !== false) {
  1549. $count_cyberjaya[] = $d;
  1550. }
  1551. if (strpos($d['address'], 'Jasin') !== false) {
  1552. $count_jasin[] = $d;
  1553. }
  1554. }
  1555. }
  1556. }else if($year == '2020' && ($month == '04' || $month == '05' || $month == '06' || $month == '07')){
  1557. foreach ($dataClaim as $key => $d) {
  1558. if(date('d/m/Y', strtotime($d->activated_dt)) >= '27/04/2020' &&
  1559. date('d/m/Y', strtotime($d->activated_dt)) <= '31/07/2020'){
  1560. if (strpos($d['address'], 'Cyberjaya') !== false) {
  1561. $count_cyberjaya[] = $d;
  1562. }
  1563. if (strpos($d['address'], 'Jasin') !== false) {
  1564. $count_jasin[] = $d;
  1565. }
  1566. }
  1567. }
  1568. }
  1569. }else if($app == 'Business'){
  1570. $dataClaim = DealerClaim::with(['Subscriber' => function($q) {
  1571. $q->with('Form');
  1572. }],'Staff')->where('formT', 'B')->where('dealer',$company)->where('activated_my',$month.'/'.$year)->orderBy('activated_dt','DESC')->get();
  1573. }
  1574. }
  1575. $i = 0;
  1576. $nested_data = array();
  1577. if(!empty($dataClaim)){
  1578. foreach ($dataClaim as $c){
  1579. $address = ''; $name = '';
  1580. if($c->Subscriber->Form->type_application == 'R'){
  1581. if($c->Subscriber->street != ''){
  1582. $address = $c->Subscriber->unit_no. ' , '.$c->Subscriber->building_name. ' , '.$c->Subscriber->street. ' , '.$c->Subscriber->postcode. ' , '.$c->Subscriber->city. ' , '.$c->Subscriber->state;
  1583. }else {
  1584. $address = $c->Subscriber->unit_no. ' , '.$c->Subscriber->building_name. ' , '.$c->Subscriber->postcode. ' , '.$c->Subscriber->city. ' , '.$c->Subscriber->state;
  1585. }
  1586. $name = $c->Subscriber->name;
  1587. }else if($c->Subscriber->Form->type_application == 'B'){
  1588. if($c->Subscriber->unit_no != ''){
  1589. $address = $c->Subscriber->unit_no. ', '.$c->Subscriber->company_name. ', '.$c->Subscriber->street. ' , '.$c->Subscriber->postcode. ' , '.$c->Subscriber->city. ' , '.$c->Subscriber->state;
  1590. }else {
  1591. $address = $c->Subscriber->company_name. ', '.$c->Subscriber->street. ' , '.$c->Subscriber->postcode. ' , '.$c->Subscriber->city. ' , '.$c->Subscriber->state;
  1592. }
  1593. $name = $c->Subscriber->company_name;
  1594. }
  1595. $i++;
  1596. array_push($nested_data, array(
  1597. 'index' => $c->formT,
  1598. 'contract' => $c->contract,
  1599. 'wo' => $c->wo,
  1600. 'activated_dt' => $c->activated_dt,
  1601. 'name' => $name,
  1602. 'address' => $address,
  1603. 'package' => $c->package,
  1604. 'retail_price' => 'RM '.$c->retail_price,
  1605. 'incentives1' => $c->incentives1,
  1606. 'incentives2' => $c->incentives2,
  1607. 'total' => 'RM '.$c->total_claim,
  1608. 'claimed' => $c->claim_dt,
  1609. ));
  1610. }
  1611. }
  1612. return \DataTables::of($nested_data)->make(true);
  1613. }
  1614. public function updateAgent(Request $request)
  1615. {
  1616. $user = Staff::where('_id', $request->agent_id)->first();
  1617. $validate = Staff::where('email', $request->email)->first();
  1618. $detail = $user->staffdetail()->first();
  1619. $getpass = $request->get('password');
  1620. $pass = $this->encryptPassword('encrypt', $request->get('password'));
  1621. if(empty($validate))
  1622. {
  1623. $detail->name = $request->get('aname');
  1624. $detail->ic = $request->get('ic');
  1625. $detail->email = $request->get('email');
  1626. $detail->phone = $request->get('phone');
  1627. $detail->company_id = $request->get('vendor');
  1628. $user->email = $request->get('email');
  1629. $user->company_id = $request->get('vendor');
  1630. if(!empty($getpass))
  1631. {
  1632. $validator = Validator::make($request->all(), [
  1633. 'password' => 'min:8|confirmed',
  1634. ]);
  1635. if ($validator->fails())
  1636. {
  1637. return redirect()->back()->withInput()->withErrors($validator);
  1638. }
  1639. $detail->password = $pass;
  1640. $user->password = $pass;
  1641. $detail->save();
  1642. $user->save();
  1643. }
  1644. else
  1645. {
  1646. $detail->save();
  1647. $user->save();
  1648. }
  1649. return redirect()->route('dHome')->with('success_msg', 'message');
  1650. }
  1651. else
  1652. {
  1653. if($user->email == $request->email)
  1654. {
  1655. echo "".$request->get('password');
  1656. $detail->name = $request->get('aname');
  1657. $detail->ic = $request->get('ic');
  1658. $detail->email = $request->get('email');
  1659. $detail->phone = $request->get('phone');
  1660. $detail->company_id = $request->get('vendor');
  1661. $detail->save();
  1662. $user->email = $request->get('email');
  1663. if(!empty($getpass))
  1664. {
  1665. $validator = Validator::make($request->all(), [
  1666. 'password' => 'min:8|confirmed',
  1667. ]);
  1668. if ($validator->fails())
  1669. {
  1670. return redirect()->back()->withInput()->withErrors($validator);
  1671. }
  1672. $detail->password = $pass;
  1673. $user->password = $pass;
  1674. $detail->save();
  1675. $user->save();
  1676. }
  1677. else
  1678. {
  1679. $detail->save();
  1680. $user->save();
  1681. }
  1682. return redirect()->route('dHome')->with('success_msg', 'message');
  1683. }
  1684. else
  1685. {
  1686. echo "email is used";
  1687. return redirect()->back()->withInput()->withErrors('Email already exist!');
  1688. }
  1689. }
  1690. }
  1691. /***
  1692. / Application Form
  1693. ***/
  1694. public function showResidentialForm(){
  1695. $id = Auth::guard('ds')->id();
  1696. $user = Staff::find($id);
  1697. $email = $user->email;
  1698. $userDetails = StaffDetail::where('email',$email)->first();
  1699. return view('dealer.residential-form',compact('userDetails','user'));
  1700. }
  1701. public function showBusinessForm(){
  1702. $id = Auth::guard('ds')->id();
  1703. $user = Staff::find($id);
  1704. $email = $user->email;
  1705. $userDetails = StaffDetail::where('email',$email)->first();
  1706. return view('dealer.business-form',compact('userDetails','user'));
  1707. }
  1708. public function exportDealerJson(Request $request) {
  1709. $final = array();
  1710. $id = Auth::guard('ds')->id();
  1711. // $user = Staff::with('StaffDetail')->find($id);
  1712. // $com = Company::where('_id',$user->StaffDetail->company_id)->first();
  1713. $dataR = array(); $dataB = array(); $combineData = array();
  1714. $extra = 0;
  1715. $year = $request->year;
  1716. $month = $request->month;
  1717. $app = $request->app;
  1718. $company = $request->company;
  1719. if($year!='null' && $month!='null' && $app=='null'){
  1720. $count_cyberjaya = array(); $count_jasin = array(); $count_ayerKeroh = array(); $count_alorGajah = array();
  1721. $extra = 0; $basic_total = 0; $totals = 0;
  1722. // if(($year == '2019' || $year == '2020') && ($month == '12' || $month == '01')){
  1723. // $dataDis = $this->returnDataR($company,'2019','12');
  1724. // $dataJan = $this->returnDataR($company,'2020','1');
  1725. if($year == '2020' && ($month == '04' || $month == '05' || $month == '06' || $month == '07')){
  1726. $dataApril = $this->returnDataR($company,'2020','4');
  1727. $dataMay = $this->returnDataR($company,'2020','5');
  1728. $dataJune = $this->returnDataR($company,'2020','6');
  1729. $dataJuly = $this->returnDataR($company,'2020','7');
  1730. foreach ($dataApril as $key => $d) {
  1731. // if(date('d/m/Y', strtotime($d['activation'])) >= '12/12/2019'){
  1732. // if (strpos($d['address'], 'Cyberjaya') !== false) {
  1733. // $count_cyberjaya[] = $d;
  1734. // }
  1735. // if (strpos($d['address'], 'Jasin') !== false) {
  1736. // $count_jasin[] = $d;
  1737. // }
  1738. // }
  1739. // // else
  1740. if(date('d/m/Y', strtotime($d['activation'])) >= '27/4/2020'){
  1741. if (strpos($d['address'], 'Cyberjaya') !== false) {
  1742. $count_cyberjaya[] = $d;
  1743. }
  1744. if (strpos($d['address'], 'Jasin') !== false) {
  1745. $count_jasin[] = $d;
  1746. }
  1747. if (strpos($d['address'], 'Ayer Keroh') !== false) {
  1748. $count_ayerKeroh[] = $d;
  1749. }
  1750. if (strpos($d['address'], 'Alor Gajah') !== false) {
  1751. $count_alorGajah[] = $d;
  1752. }
  1753. }
  1754. else {
  1755. if(date('m/Y', strtotime($d['activation'])) == $month.'/'.$year){
  1756. $combineData[] = $d;
  1757. }
  1758. }
  1759. }
  1760. foreach ($dataMay as $key => $d) {
  1761. if (strpos($d['address'], 'Cyberjaya') !== false) {
  1762. $count_cyberjaya[] = $d;
  1763. }
  1764. if (strpos($d['address'], 'Jasin') !== false) {
  1765. $count_jasin[] = $d;
  1766. }
  1767. if (strpos($d['address'], 'Ayer Keroh') !== false) {
  1768. $count_ayerKeroh[] = $d;
  1769. }
  1770. if (strpos($d['address'], 'Alor Gajah') !== false) {
  1771. $count_alorGajah[] = $d;
  1772. }
  1773. }
  1774. foreach ($dataJune as $key => $d) {
  1775. if (strpos($d['address'], 'Cyberjaya') !== false) {
  1776. $count_cyberjaya[] = $d;
  1777. }
  1778. if (strpos($d['address'], 'Jasin') !== false) {
  1779. $count_jasin[] = $d;
  1780. }
  1781. if (strpos($d['address'], 'Ayer Keroh') !== false) {
  1782. $count_ayerKeroh[] = $d;
  1783. }
  1784. if (strpos($d['address'], 'Alor Gajah') !== false) {
  1785. $count_alorGajah[] = $d;
  1786. }
  1787. }
  1788. foreach ($dataJuly as $key => $d) {
  1789. if (strpos($d['address'], 'Cyberjaya') !== false) {
  1790. $count_cyberjaya[] = $d;
  1791. }
  1792. if (strpos($d['address'], 'Jasin') !== false) {
  1793. $count_jasin[] = $d;
  1794. }
  1795. if (strpos($d['address'], 'Ayer Keroh') !== false) {
  1796. $count_ayerKeroh[] = $d;
  1797. }
  1798. if (strpos($d['address'], 'Alor Gajah') !== false) {
  1799. $count_alorGajah[] = $d;
  1800. }
  1801. }
  1802. // Store to combineData New Incentives Cyberjaya
  1803. foreach ($count_cyberjaya as $key => $d) {
  1804. if(count($count_cyberjaya) > 50){
  1805. if(date('m/Y', strtotime($d['activation'])) == $month.'/'.$year){
  1806. if($d['contract'] == '12'){
  1807. array_push($combineData, array(
  1808. 'index' => $d['index'],
  1809. 'contract' => $d['contract'],
  1810. 'wo' => $d['wo'],
  1811. 'activation' => $d['activation'],
  1812. 'name' => $d['name'],
  1813. 'address' => $d['address'],
  1814. 'package' => $d['package'],
  1815. 'retail_price' => $d['retail_price'],
  1816. 'incentives1' => $d['incentives1'],
  1817. 'incentives2' => $d['incentives2'],
  1818. 'total' => $d['total'],
  1819. 'claim' => $d['claim'],
  1820. ));
  1821. }else {
  1822. $total = number_format(floatval(str_replace("RM ", "" , $d['retail_price'])) * 2.3, 2);
  1823. array_push($combineData, array(
  1824. 'index' => $d['index'],
  1825. 'contract' => $d['contract'],
  1826. 'wo' => $d['wo'],
  1827. 'activation' => $d['activation'],
  1828. 'name' => $d['name'],
  1829. 'address' => $d['address'],
  1830. 'package' => $d['package'],
  1831. 'retail_price' => $d['retail_price'],
  1832. 'incentives1' => '230%',
  1833. 'incentives2' => $d['incentives2'],
  1834. 'total' => 'RM '.$total,
  1835. 'claim' => $d['claim'],
  1836. ));
  1837. }
  1838. }
  1839. }else if(count($count_cyberjaya) < 50){
  1840. if(date('m/Y', strtotime($d['activation'])) == $month.'/'.$year){
  1841. array_push($combineData, array(
  1842. 'index' => $d['index'],
  1843. 'contract' => $d['contract'],
  1844. 'wo' => $d['wo'],
  1845. 'activation' => $d['activation'],
  1846. 'name' => $d['name'],
  1847. 'address' => $d['address'],
  1848. 'package' => $d['package'],
  1849. 'retail_price' => $d['retail_price'],
  1850. 'incentives1' => $d['incentives1'],
  1851. 'incentives2' => $d['incentives2'],
  1852. 'total' => $d['total'],
  1853. 'claim' => $d['claim'],
  1854. ));
  1855. }
  1856. }
  1857. }
  1858. // Store to combineData New Incentives Ayer Keroh
  1859. foreach ($count_ayerKeroh as $key => $d) {
  1860. if(count($count_ayerKeroh) > 50){
  1861. if($d['contract'] == '12'){
  1862. array_push($combineData, array(
  1863. 'index' => $d['index'],
  1864. 'contract' => $d['contract'],
  1865. 'wo' => $d['wo'],
  1866. 'activation' => $d['activation'],
  1867. 'name' => $d['name'],
  1868. 'address' => $d['address'],
  1869. 'package' => $d['package'],
  1870. 'retail_price' => $d['retail_price'],
  1871. 'incentives1' => $d['incentives1'],
  1872. 'incentives2' => $d['incentives2'],
  1873. 'total' => $d['total'],
  1874. 'claim' => $d['claim'],
  1875. ));
  1876. }else {
  1877. if(date('m/Y', strtotime($d['activation'])) == $month.'/'.$year){
  1878. $total = number_format(floatval(str_replace("RM ", "" , $d['retail_price'])) * 2.3, 2);
  1879. array_push($combineData, array(
  1880. 'index' => $d['index'],
  1881. 'contract' => $d['contract'],
  1882. 'wo' => $d['wo'],
  1883. 'activation' => $d['activation'],
  1884. 'name' => $d['name'],
  1885. 'address' => $d['address'],
  1886. 'package' => $d['package'],
  1887. 'retail_price' => $d['retail_price'],
  1888. 'incentives1' => '230%',
  1889. 'incentives2' => $d['incentives2'],
  1890. 'total' => 'RM '.$total,
  1891. 'claim' => $d['claim'],
  1892. ));
  1893. }
  1894. }
  1895. }else if(count($count_ayerKeroh) < 50){
  1896. if(date('m/Y', strtotime($d['activation'])) == $month.'/'.$year){
  1897. array_push($combineData, array(
  1898. 'index' => $d['index'],
  1899. 'contract' => $d['contract'],
  1900. 'wo' => $d['wo'],
  1901. 'activation' => $d['activation'],
  1902. 'name' => $d['name'],
  1903. 'address' => $d['address'],
  1904. 'package' => $d['package'],
  1905. 'retail_price' => $d['retail_price'],
  1906. 'incentives1' => $d['incentives1'],
  1907. 'incentives2' => $d['incentives2'],
  1908. 'total' => $d['total'],
  1909. 'claim' => $d['claim'],
  1910. ));
  1911. }
  1912. }
  1913. }
  1914. // Store to combineData New Incentives Alor Gajah
  1915. foreach ($count_alorGajah as $key => $d) {
  1916. if(count($count_alorGajah) > 20){
  1917. if($d['contract'] == '12'){
  1918. array_push($combineData, array(
  1919. 'index' => $d['index'],
  1920. 'contract' => $d['contract'],
  1921. 'wo' => $d['wo'],
  1922. 'activation' => $d['activation'],
  1923. 'name' => $d['name'],
  1924. 'address' => $d['address'],
  1925. 'package' => $d['package'],
  1926. 'retail_price' => $d['retail_price'],
  1927. 'incentives1' => $d['incentives1'],
  1928. 'incentives2' => $d['incentives2'],
  1929. 'total' => $d['total'],
  1930. 'claim' => $d['claim'],
  1931. ));
  1932. }else {
  1933. if(date('m/Y', strtotime($d['activation'])) == $month.'/'.$year){
  1934. $total = number_format(floatval(str_replace("RM ", "" , $d['retail_price'])) * 2.3, 2);
  1935. array_push($combineData, array(
  1936. 'index' => $d['index'],
  1937. 'contract' => $d['contract'],
  1938. 'wo' => $d['wo'],
  1939. 'activation' => $d['activation'],
  1940. 'name' => $d['name'],
  1941. 'address' => $d['address'],
  1942. 'package' => $d['package'],
  1943. 'retail_price' => $d['retail_price'],
  1944. 'incentives1' => '230%',
  1945. 'incentives2' => $d['incentives2'],
  1946. 'total' => 'RM '.$total,
  1947. 'claim' => $d['claim'],
  1948. ));
  1949. }
  1950. }
  1951. }else if(count($count_alorGajah) < 20){
  1952. if(date('m/Y', strtotime($d['activation'])) == $month.'/'.$year){
  1953. array_push($combineData, array(
  1954. 'index' => $d['index'],
  1955. 'contract' => $d['contract'],
  1956. 'wo' => $d['wo'],
  1957. 'activation' => $d['activation'],
  1958. 'name' => $d['name'],
  1959. 'address' => $d['address'],
  1960. 'package' => $d['package'],
  1961. 'retail_price' => $d['retail_price'],
  1962. 'incentives1' => $d['incentives1'],
  1963. 'incentives2' => $d['incentives2'],
  1964. 'total' => $d['total'],
  1965. 'claim' => $d['claim'],
  1966. ));
  1967. }
  1968. }
  1969. }
  1970. // Store to combineData New Incentives Jasin
  1971. foreach ($count_jasin as $key => $d) {
  1972. if(count($count_jasin) > 20){
  1973. if($d['contract'] == '12'){
  1974. array_push($combineData, array(
  1975. 'index' => $d['index'],
  1976. 'contract' => $d['contract'],
  1977. 'wo' => $d['wo'],
  1978. 'activation' => $d['activation'],
  1979. 'name' => $d['name'],
  1980. 'address' => $d['address'],
  1981. 'package' => $d['package'],
  1982. 'retail_price' => $d['retail_price'],
  1983. 'incentives1' => $d['incentives1'],
  1984. 'incentives2' => $d['incentives2'],
  1985. 'total' => $d['total'],
  1986. 'claim' => $d['claim'],
  1987. ));
  1988. }else {
  1989. if(date('m/Y', strtotime($d['activation'])) == $month.'/'.$year){
  1990. $total = number_format(floatval(str_replace("RM ", "" , $d['retail_price'])) * 2.3, 2);
  1991. array_push($combineData, array(
  1992. 'index' => $d['index'],
  1993. 'contract' => $d['contract'],
  1994. 'wo' => $d['wo'],
  1995. 'activation' => $d['activation'],
  1996. 'name' => $d['name'],
  1997. 'address' => $d['address'],
  1998. 'package' => $d['package'],
  1999. 'retail_price' => $d['retail_price'],
  2000. 'incentives1' => '230%',
  2001. 'incentives2' => $d['incentives2'],
  2002. 'total' => 'RM '.$total,
  2003. 'claim' => $d['claim'],
  2004. ));
  2005. }
  2006. }
  2007. }else if(count($count_jasin) < 20){
  2008. if(date('m/Y', strtotime($d['activation'])) == $month.'/'.$year){
  2009. array_push($combineData, array(
  2010. 'index' => $d['index'],
  2011. 'contract' => $d['contract'],
  2012. 'wo' => $d['wo'],
  2013. 'activation' => $d['activation'],
  2014. 'name' => $d['name'],
  2015. 'address' => $d['address'],
  2016. 'package' => $d['package'],
  2017. 'retail_price' => $d['retail_price'],
  2018. 'incentives1' => $d['incentives1'],
  2019. 'incentives2' => $d['incentives2'],
  2020. 'total' => $d['total'],
  2021. 'claim' => $d['claim'],
  2022. ));
  2023. }
  2024. }
  2025. }
  2026. }
  2027. // $dataR = $this->returnDataR($company,$year,$month);
  2028. // foreach($dataR as $r){
  2029. // $combineData[] = $r;
  2030. // }
  2031. $dataB = $this->returnDataB($company,$year,$month);
  2032. foreach($dataB as $b){
  2033. $combineData[] = $b;
  2034. }
  2035. $i = 0;
  2036. $nested_data = array();
  2037. if(!empty($combineData)){
  2038. foreach ($combineData as $c){
  2039. $update_dealer = DealerClaim::where('wo',$c['wo'])->first();
  2040. if(!empty($update_dealer)){
  2041. $update_dealer->incentives1 = $c['incentives1'];
  2042. $update_dealer->incentives2 = $c['incentives2'];
  2043. $update_dealer->total_claim = $c['total'];
  2044. $update_dealer->save();
  2045. }
  2046. // $i++;
  2047. // array_push($final, array(
  2048. // 'index' => $c['index'],
  2049. // 'contract' => $c['contract'],
  2050. // 'wo' => $c['wo'],
  2051. // 'activated_dt' => $c['activation'],
  2052. // 'name' => $c['name'],
  2053. // 'address' => $c['address'],
  2054. // 'package' => $c['package'],
  2055. // 'retail_price' => $c['retail_price'],
  2056. // 'incentives1' => $c['incentives1'],
  2057. // 'incentives2' => $c['incentives2'],
  2058. // 'total' => $c['total'],
  2059. // 'claimed' => $c['claim'],
  2060. // ));
  2061. }
  2062. }
  2063. $dataClaim = DealerClaim::with(['Subscriber' => function($q) {
  2064. $q->with('Form');
  2065. }],'Staff')->where('dealer',$company)->where('activated_my',$month.'/'.$year)->get();
  2066. if(!empty($dataClaim)){
  2067. foreach ($dataClaim as $c){
  2068. $address = ''; $name = '';
  2069. if($c->Subscriber->Form->type_application == 'R'){
  2070. if($c->Subscriber->street != ''){
  2071. $address = $c->Subscriber->unit_no. ' , '.$c->Subscriber->building_name. ' , '.$c->Subscriber->street. ' , '.$c->Subscriber->postcode. ' , '.$c->Subscriber->city. ' , '.$c->Subscriber->state;
  2072. }else {
  2073. $address = $c->Subscriber->unit_no. ' , '.$c->Subscriber->building_name. ' , '.$c->Subscriber->postcode. ' , '.$c->Subscriber->city. ' , '.$c->Subscriber->state;
  2074. }
  2075. $name = $c->Subscriber->name;
  2076. }else if($c->Subscriber->Form->type_application == 'B'){
  2077. if($c->Subscriber->unit_no != ''){
  2078. $address = $c->Subscriber->unit_no. ', '.$c->Subscriber->company_name. ', '.$c->Subscriber->street. ' , '.$c->Subscriber->postcode. ' , '.$c->Subscriber->city. ' , '.$c->Subscriber->state;
  2079. }else {
  2080. $address = $c->Subscriber->company_name. ', '.$c->Subscriber->street. ' , '.$c->Subscriber->postcode. ' , '.$c->Subscriber->city. ' , '.$c->Subscriber->state;
  2081. }
  2082. $name = $c->Subscriber->company_name;
  2083. }
  2084. $i++;
  2085. array_push($final, array(
  2086. 'index' => $c->formT,
  2087. 'contract' => $c->contract,
  2088. 'wo' => $c->wo,
  2089. 'activated_dt' => $c->activated_dt,
  2090. 'name' => $name,
  2091. 'address' => $address,
  2092. 'package' => $c->package,
  2093. 'retail_price' => 'RM '.$c->retail_price,
  2094. 'incentives1' => $c->incentives1,
  2095. 'incentives2' => $c->incentives2,
  2096. 'total' => $c->total_claim,
  2097. 'claimed' => $c->claim_dt,
  2098. ));
  2099. }
  2100. }
  2101. foreach($final as $f){
  2102. $basic_total += floatval(str_replace('RM ', '', $f['total']));
  2103. }
  2104. // Calculate extra incentves
  2105. // if($month == '1' && $year == '2020'){
  2106. // if(count($count_cyberjaya) > 50){
  2107. // $balance = count($count_cyberjaya) - 50;
  2108. // $extra += $balance * 50;
  2109. // }
  2110. // if(count($count_jasin) > 10){
  2111. // $balance = count($count_jasin) - 10;
  2112. // $extra += $balance * 50;
  2113. // }
  2114. // }else if(($month == '4' || $month == '5'|| $month == '6' || $month == '7') && $year == '2020'){
  2115. // if(count($count_cyberjaya) > 50){
  2116. // $balance = count($count_cyberjaya) - 50;
  2117. // $extra += $balance * 50;
  2118. // }
  2119. // if(count($count_jasin) > 20){
  2120. // $balance = count($count_jasin) - 10;
  2121. // $extra += $balance * 50;
  2122. // }
  2123. // }else {
  2124. // $extra = 0;
  2125. // }
  2126. $extra = 0;
  2127. $totals = $basic_total + $extra;
  2128. // $pdf = PDF::loadView('pdf.agent-claim', compact('final', 'month', 'year','com','basic_total','extra','totals'));
  2129. // $pdf->setPaper('A4', 'landscape');
  2130. // $pdfName = preg_replace('/\s+/', '-', $com->name).'-'.$month.'-'.$year.'.pdf';
  2131. // return $pdf->download( $pdfName );
  2132. return $final;
  2133. }
  2134. // else if($year!='null' && $month!='null' && $app!='null'){
  2135. // $count_cyberjaya = array(); $count_jasin = array();
  2136. // $extra = 0; $basic_total = 0; $totals = 0;
  2137. // if($app == 'Residential'){
  2138. // if(($year == '2019' || $year == '2020') && ($month == '12' || $month == '1')){
  2139. // $dataDis = $this->returnDataR($company,'2019','12');
  2140. // $dataJan = $this->returnDataR($company,'2020','1');
  2141. // foreach ($dataDis as $key => $d) {
  2142. // if(date('d/m/Y', strtotime($d['activation'])) >= '12/12/2019'){
  2143. // if (strpos($d['address'], 'Cyberjaya') !== false) {
  2144. // $count_cyberjaya[] = $d;
  2145. // }
  2146. // if (strpos($d['address'], 'Jasin') !== false) {
  2147. // $count_jasin[] = $d;
  2148. // }
  2149. // }else if(date('d/m/Y', strtotime($d['activation'])) >= '27/4/2020' && date('d/m/Y', strtotime($d['activation'])) <= '31/7/2020'){
  2150. // if (strpos($d['address'], 'Cyberjaya') !== false) {
  2151. // $count_cyberjaya[] = $d;
  2152. // }
  2153. // if (strpos($d['address'], 'Jasin') !== false) {
  2154. // $count_jasin[] = $d;
  2155. // }
  2156. // }else {
  2157. // if(date('n/Y', strtotime($d['activation'])) == $month.'/'.$year){
  2158. // $combineData[] = $d;
  2159. // }
  2160. // }
  2161. // }
  2162. // foreach ($dataJan as $key => $d) {
  2163. // if (strpos($d['address'], 'Cyberjaya') !== false) {
  2164. // $count_cyberjaya[] = $d;
  2165. // }
  2166. // if (strpos($d['address'], 'Jasin') !== false) {
  2167. // $count_jasin[] = $d;
  2168. // }
  2169. // }
  2170. // // Store to combineData New Incentives Cyberjaya
  2171. // foreach ($count_cyberjaya as $key => $d) {
  2172. // if(count($count_cyberjaya) > 50){
  2173. // if(date('n/Y', strtotime($d['activation'])) == $month.'/'.$year){
  2174. // if($d['contract'] == '12'){
  2175. // array_push($combineData, array(
  2176. // 'index' => $d['index'],
  2177. // 'contract' => $d['contract'],
  2178. // 'wo' => $d['wo'],
  2179. // 'activation' => $d['activation'],
  2180. // 'name' => $d['name'],
  2181. // 'address' => $d['address'],
  2182. // 'package' => $d['package'],
  2183. // 'retail_price' => $d['retail_price'],
  2184. // 'incentives1' => $d['incentives1'],
  2185. // 'incentives2' => $d['incentives2'],
  2186. // 'total' => $d['total'],
  2187. // 'claim' => $d['claim'],
  2188. // ));
  2189. // }else {
  2190. // $total = number_format(floatval(str_replace("RM ", "" , $d['retail_price'])) * 2.3, 2);
  2191. // array_push($combineData, array(
  2192. // 'index' => $d['index'],
  2193. // 'contract' => $d['contract'],
  2194. // 'wo' => $d['wo'],
  2195. // 'activation' => $d['activation'],
  2196. // 'name' => $d['name'],
  2197. // 'address' => $d['address'],
  2198. // 'package' => $d['package'],
  2199. // 'retail_price' => $d['retail_price'],
  2200. // 'incentives1' => '230%',
  2201. // 'incentives2' => $d['incentives2'],
  2202. // 'total' => 'RM '.$total,
  2203. // 'claim' => $d['claim'],
  2204. // ));
  2205. // }
  2206. // }
  2207. // }else if(count($count_cyberjaya) < 50){
  2208. // if(date('n/Y', strtotime($d['activation'])) == $month.'/'.$year){
  2209. // array_push($combineData, array(
  2210. // 'index' => $d['index'],
  2211. // 'contract' => $d['contract'],
  2212. // 'wo' => $d['wo'],
  2213. // 'activation' => $d['activation'],
  2214. // 'name' => $d['name'],
  2215. // 'address' => $d['address'],
  2216. // 'package' => $d['package'],
  2217. // 'retail_price' => $d['retail_price'],
  2218. // 'incentives1' => $d['incentives1'],
  2219. // 'incentives2' => $d['incentives2'],
  2220. // 'total' => $d['total'],
  2221. // 'claim' => $d['claim'],
  2222. // ));
  2223. // }
  2224. // }
  2225. // }
  2226. // // Store to combineData New Incentives Jasin
  2227. // foreach ($count_jasin as $key => $d) {
  2228. // if(count($count_jasin) > 10){
  2229. // if(date('n/Y', strtotime($d['activation'])) == $month.'/'.$year){
  2230. // $total = number_format(floatval(str_replace("RM ", "" , $d['retail_price'])) * 2.3, 2);
  2231. // array_push($combineData, array(
  2232. // 'index' => $d['index'],
  2233. // 'contract' => $d['contract'],
  2234. // 'wo' => $d['wo'],
  2235. // 'activation' => $d['activation'],
  2236. // 'name' => $d['name'],
  2237. // 'address' => $d['address'],
  2238. // 'package' => $d['package'],
  2239. // 'retail_price' => $d['retail_price'],
  2240. // 'incentives1' => '230%',
  2241. // 'incentives2' => $d['incentives2'],
  2242. // 'total' => 'RM '.$total,
  2243. // 'claim' => $d['claim'],
  2244. // ));
  2245. // }
  2246. // }else if(count($count_jasin) < 10){
  2247. // if(date('n/Y', strtotime($d['activation'])) == $month.'/'.$year){
  2248. // array_push($combineData, array(
  2249. // 'index' => $d['index'],
  2250. // 'contract' => $d['contract'],
  2251. // 'wo' => $d['wo'],
  2252. // 'activation' => $d['activation'],
  2253. // 'name' => $d['name'],
  2254. // 'address' => $d['address'],
  2255. // 'package' => $d['package'],
  2256. // 'retail_price' => $d['retail_price'],
  2257. // 'incentives1' => $d['incentives1'],
  2258. // 'incentives2' => $d['incentives2'],
  2259. // 'total' => $d['total'],
  2260. // 'claim' => $d['claim'],
  2261. // ));
  2262. // }
  2263. // }
  2264. // }
  2265. // }else {
  2266. // $dataR = $this->returnDataR($company,$year,$month);
  2267. // foreach($dataR as $r){
  2268. // $combineData[] = $r;
  2269. // }
  2270. // }
  2271. // }else if($app == 'Business'){
  2272. // $dataB = $this->returnDataB($company,$year,$month);
  2273. // }
  2274. // foreach($dataB as $b){
  2275. // $combineData[] = $b;
  2276. // }
  2277. // $i = 0;
  2278. // $nested_data = array();
  2279. // if(!empty($combineData)){
  2280. // foreach ($combineData as $c){
  2281. // $i++;
  2282. // array_push($final, array(
  2283. // 'index' => $c['index'],
  2284. // 'contract' => $c['contract'],
  2285. // 'wo' => $c['wo'],
  2286. // 'activated_dt' => $c['activation'],
  2287. // 'name' => $c['name'],
  2288. // 'address' => $c['address'],
  2289. // 'package' => $c['package'],
  2290. // 'retail_price' => $c['retail_price'],
  2291. // 'incentives1' => $c['incentives1'],
  2292. // 'incentives2' => $c['incentives2'],
  2293. // 'total' => $c['total'],
  2294. // 'claimed' => $c['claim'],
  2295. // ));
  2296. // }
  2297. // }
  2298. // foreach($final as $f){
  2299. // $basic_total += floatval(str_replace('RM ', '', $f['total']));
  2300. // }
  2301. // //Calculate extra incentves
  2302. // if($month == '1' && $year == '2020'){
  2303. // if(count($count_cyberjaya) > 50){
  2304. // $balance = count($count_cyberjaya) - 50;
  2305. // $extra += $balance * 50;
  2306. // }
  2307. // if(count($count_jasin) > 10){
  2308. // $balance = count($count_jasin) - 10;
  2309. // $extra += $balance * 50;
  2310. // }
  2311. // }else if(($month == '4' || $month == '5'|| $month == '6' || $month == '7') && $year == '2020'){
  2312. // if(count($count_cyberjaya) > 50){
  2313. // $balance = count($count_cyberjaya) - 50;
  2314. // $extra += $balance * 50;
  2315. // }
  2316. // if(count($count_jasin) > 20){
  2317. // $balance = count($count_jasin) - 10;
  2318. // $extra += $balance * 50;
  2319. // }
  2320. // }else {
  2321. // $extra = 0;
  2322. // }
  2323. // $totals = $basic_total + $extra;
  2324. // $pdf = PDF::loadView('pdf.agent-claim', compact('final', 'month', 'year','com','basic_total','extra','totals'));
  2325. // $pdf->setPaper('A4', 'landscape');
  2326. // $pdfName = preg_replace('/\s+/', '-', $com->name).'-'.$month.'-'.$year.'.pdf';
  2327. // return $pdf->download( $pdfName );
  2328. // }
  2329. }
  2330. public function update_total_claim(){
  2331. $dealer = DealerClaim::all();
  2332. foreach ($dealer as $key => $d) {
  2333. $claim = str_replace("RM ","",$d->total_claim);
  2334. $d->total_claim = floatval($claim);
  2335. $d->save();
  2336. }
  2337. }
  2338. }