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

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