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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214
  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; $countAugust = array(); $finalprice = 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. $countAugust[] = $d;
  2303. }
  2304. }
  2305. // Store to combineData New Incentive August
  2306. foreach ($countAugust as $key => $d) {
  2307. if(count($countAugust) >= 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. if ($d['package']!= 1){
  2326. $total = number_format(floatval((str_replace("RM ", "" , $d['retail_price']))- 10) * 2.5 + 50, 2);
  2327. $finalprice = number_format(floatval(str_replace("RM ", "" , $d['retail_price']))- 10, 2);
  2328. }
  2329. else{
  2330. $total = number_format(floatval(str_replace("RM ", "" , $d['retail_price'])) * 2.5 + 50, 2);
  2331. $finalprice = number_format(floatval(str_replace("RM ", "" , $d['retail_price'])), 2);
  2332. }
  2333. array_push($combineData, array(
  2334. 'index' => $d['index'],
  2335. 'contract' => $d['contract'],
  2336. 'wo' => $d['wo'],
  2337. 'activation' => $d['activation'],
  2338. 'name' => $d['name'],
  2339. 'address' => $d['address'],
  2340. 'package' => $d['package'],
  2341. 'retail_price' => 'RM '.$finalprice,
  2342. 'incentives1' => '250% + RM50',
  2343. 'incentives2' => $d['incentives2'],
  2344. 'total' => 'RM '.$total,
  2345. 'claim' => $d['claim'],
  2346. ));
  2347. }
  2348. }
  2349. }else if(count($countAugust) < 50){
  2350. if(date('m/Y', strtotime($d['activation'])) == $month.'/'.$year){
  2351. if($d['contract'] == '12'){
  2352. array_push($combineData, array(
  2353. 'index' => $d['index'],
  2354. 'contract' => $d['contract'],
  2355. 'wo' => $d['wo'],
  2356. 'activation' => $d['activation'],
  2357. 'name' => $d['name'],
  2358. 'address' => $d['address'],
  2359. 'package' => $d['package'],
  2360. 'retail_price' => $d['retail_price'],
  2361. 'incentives1' => $d['incentives1'],
  2362. 'incentives2' => $d['incentives2'],
  2363. 'total' => $d['total'],
  2364. 'claim' => $d['claim'],
  2365. ));
  2366. }else {
  2367. if ($d['package']!= 1){
  2368. $total = number_format(floatval((str_replace("RM ", "" , $d['retail_price']))- 10) * 2.5, 2);
  2369. $finalprice = number_format(floatval(str_replace("RM ", "" , $d['retail_price']))- 10, 2);
  2370. }
  2371. else{
  2372. $total = number_format(floatval(str_replace("RM ", "" , $d['retail_price'])) * 2.5, 2);
  2373. $finalprice = number_format(floatval(str_replace("RM ", "" , $d['retail_price'])), 2);
  2374. }
  2375. array_push($combineData, array(
  2376. 'index' => $d['index'],
  2377. 'contract' => $d['contract'],
  2378. 'wo' => $d['wo'],
  2379. 'activation' => $d['activation'],
  2380. 'name' => $d['name'],
  2381. 'address' => $d['address'],
  2382. 'package' => $d['package'],
  2383. 'retail_price' => 'RM '.$finalprice,
  2384. 'incentives1' => '250%',
  2385. 'incentives2' => $d['incentives2'],
  2386. 'total' => 'RM '.$total,
  2387. 'claim' => $d['claim'],
  2388. ));
  2389. }
  2390. }
  2391. }
  2392. }
  2393. }else{
  2394. $dataR = $this->returnDataR($company,$year,$month);
  2395. foreach($dataR as $r){
  2396. $combineData[] = $r;
  2397. }
  2398. $dataB = $this->returnDataB($company,$year,$month);
  2399. foreach($dataB as $b){
  2400. $combineData[] = $b;
  2401. }
  2402. }
  2403. $i = 0;
  2404. $nested_data = array();
  2405. if(!empty($combineData)){
  2406. foreach ($combineData as $c){
  2407. $update_dealer = DealerClaim::where('wo',$c['wo'])->first();
  2408. if(!empty($update_dealer)){
  2409. $update_dealer->incentives1 = $c['incentives1'];
  2410. $update_dealer->incentives2 = $c['incentives2'];
  2411. $update_dealer->retail_price = $c['retail_price'];
  2412. $update_dealer->total_claim = $c['total'];
  2413. $update_dealer->save();
  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. }