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.

MarketingController.php 106KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625
  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\Company;
  14. use App\Model\Commission;
  15. use App\Model\Form;
  16. use App\Model\PackageDetail;
  17. use App\Model\Subscriber;
  18. use App\Model\WorkOrder;
  19. use App\Model\Docket;
  20. use App\Model\Product;
  21. class MarketingController extends Controller
  22. {
  23. public function dashboard()
  24. {
  25. $id = Auth::guard('ms')->id();
  26. $user = Staff::with('StaffDetail')->find($id);
  27. $finalD1 = array(); $finalD2 = array(); $finalD3 = array(); $finalD4 = array(); $finalD5 = array(); $finalD6 = array();
  28. $jan = array(); $feb = array(); $mac = array(); $april = array(); $may = array(); $jun = array();
  29. $july = array(); $august = array(); $sep = array(); $oct = array(); $nov = array(); $dec = array();
  30. $jan1 = array(); $feb1 = array(); $mac1 = array(); $april1 = array(); $may1 = array(); $jun1 = array();
  31. $july1 = array(); $august1 = array(); $sep1 = array(); $oct1 = array(); $nov1 = array(); $dec1 = array();
  32. $de = Form::all();
  33. foreach ($de as $key => $d) {
  34. if(!empty($d->dealer_id)){
  35. $finalD1[] = $d;
  36. $mY = Carbon::parse($d->created_at)->format('m/Y');
  37. if($mY == '01/2019'){
  38. $jan[] = $d;
  39. }else if($mY == '02/2019'){
  40. $feb[] = $d;
  41. }else if($mY == '03/2019'){
  42. $mac[] = $d;
  43. }else if($mY == '04/2019'){
  44. $april[] = $d;
  45. }else if($mY == '05/2019'){
  46. $may[] = $d;
  47. }else if($mY == '06/2019'){
  48. $jun[] = $d;
  49. }else if($mY == '07/2019'){
  50. $july[] = $d;
  51. }else if($mY == '08/2019'){
  52. $august[] = $d;
  53. }else if($mY == '09/2019'){
  54. $sep[] = $d;
  55. }else if($mY == '10/2019'){
  56. $oct[] = $d;
  57. }else if($mY == '11/2019'){
  58. $nov[] = $d;
  59. }else if($mY == '12/2019'){
  60. $dec[] = $d;
  61. }
  62. $finalD6 = [count($jan),count($feb),count($mac),count($april),count($may),count($jun),count($july),count($august),count($sep),count($oct),count($nov),count($dec)];
  63. }else if(empty($d->dealer_id)){
  64. $mY = Carbon::parse($d->created_at)->format('m/Y');
  65. if($mY == '01/2019'){
  66. $jan1[] = $d;
  67. }else if($mY == '02/2019'){
  68. $feb1[] = $d;
  69. }else if($mY == '03/2019'){
  70. $mac1[] = $d;
  71. }else if($mY == '04/2019'){
  72. $april1[] = $d;
  73. }else if($mY == '05/2019'){
  74. $may1[] = $d;
  75. }else if($mY == '06/2019'){
  76. $jun1[] = $d;
  77. }else if($mY == '07/2019'){
  78. $july1[] = $d;
  79. }else if($mY == '08/2019'){
  80. $august1[] = $d;
  81. }else if($mY == '09/2019'){
  82. $sep1[] = $d;
  83. }else if($mY == '10/2019'){
  84. $oct1[] = $d;
  85. }else if($mY == '11/2019'){
  86. $nov1[] = $d;
  87. }else if($mY == '12/2019'){
  88. $dec1[] = $d;
  89. }
  90. $finalD5 = [count($jan1),count($feb1),count($mac1),count($april1),count($may1),count($jun1),count($july1),count($august1),count($sep1),count($oct1),count($nov1),count($dec1)];
  91. if($d->type_service != 'Rectification'){
  92. $finalD2[] = $d;
  93. $finalD4[] = $d;
  94. }
  95. }
  96. if($d->type_service == 'Rectification'){
  97. $finalD3[] = $d;
  98. }
  99. }
  100. $total = count($finalD4);
  101. $dealer = count($finalD1);
  102. $normal = count($finalD2);
  103. $rec = count($finalD3);
  104. $rejected = count(Form::onlyTrashed()->get());
  105. return view('market.dashboard',compact('user','total','rec','dealer','normal','rejected','finalD5','finalD6'));
  106. }
  107. public function index(){
  108. $id = Auth::guard('ms')->id();
  109. $user = Staff::with('StaffDetail')->find($id);
  110. return view('market.index',compact('user'));
  111. }
  112. //Dealer's List
  113. public function marketindex(){
  114. $id = Auth::guard('ms')->id();
  115. $user = Staff::with('StaffDetail')->find($id);
  116. $marketdetail= Company::where('team','Dealer')->orderBy('created_at','desc')->get();
  117. $curr = Carbon::now()->getTimestamp();
  118. $i = 0;
  119. $nested_data = array();
  120. if(!empty($marketdetail)){
  121. foreach ($marketdetail as $a)
  122. {
  123. $i++; $n1 = '';
  124. $reg_time = $a->created_at;
  125. $expiry_date = $reg_time->addDays(3);
  126. $expiry_date = $expiry_date->getTimestamp();
  127. if($curr < $expiry_date) {
  128. $n1 = "New/";
  129. }
  130. array_push($nested_data, array(
  131. 'index' => $n1.$i,
  132. 'name' => $a->name,
  133. 'address' => $a->address,
  134. 'city' => $a->city,
  135. 'state' => $a->state,
  136. 'postcode' => $a->postcode,
  137. 'action' => $a->_id
  138. ));
  139. }
  140. }
  141. return \DataTables::of($nested_data)->make(true);
  142. }
  143. //Dealer form
  144. public function showDealerForm(){
  145. $id = Auth::guard('ms')->id();
  146. $user = Staff::with('StaffDetail')->find($id);
  147. return view('market.add_dealer',compact('user'));
  148. }
  149. //Agent's list
  150. public function agentList()
  151. {
  152. $id = Auth::guard('ms')->id();
  153. $user = Staff::with('StaffDetail')->find($id);
  154. return view('market.agent_list',compact('user'));
  155. }
  156. //
  157. public function getAgentList()
  158. {
  159. $id = Auth::guard('ms')->id();
  160. $user = Staff::with('StaffDetail')->find($id);
  161. $agent = Staff::with('Company','StaffDetail')->orderBy('created_at','desc')->get();
  162. $curr = Carbon::now()->getTimestamp();
  163. $i = 0;
  164. $nested_data = array();
  165. if(!empty($agent))
  166. {
  167. foreach ($agent as $a)
  168. {
  169. if($a->StaffDetail->position == 'Dealer')
  170. {
  171. $i++; $n1 = '';
  172. $reg_time = $a->created_at;
  173. $expiry_date = $reg_time->addDays(3);
  174. $expiry_date = $expiry_date->getTimestamp();
  175. if($curr < $expiry_date) {
  176. $n1 = "New/";
  177. }
  178. array_push($nested_data, array(
  179. 'index' => $n1.$i,
  180. 'user_pic' => $a->StaffDetail->user_pic,
  181. 'name' => $a->StaffDetail->name,
  182. 'ic' => $a->StaffDetail->ic,
  183. 'email' => $a->StaffDetail->email,
  184. 'phone' => $a->StaffDetail->phone,
  185. 'company_id' => $a->Company->name,
  186. 'position' => $a->StaffDetail->position,
  187. 'action' => $a->_id
  188. ));
  189. }
  190. }
  191. }
  192. return \DataTables::of($nested_data)->make(true);
  193. }
  194. //Show form for adding agent
  195. public function showAgentForm()
  196. {
  197. $id = Auth::guard('ms')->id();
  198. $user = Staff::with('StaffDetail')->find($id);
  199. $company = Company::where('team', 'Dealer')->get();
  200. return view('market.agent_add',compact('user','company'));
  201. }
  202. //Show commission list R
  203. public function commissionListR()
  204. {
  205. $id = Auth::guard('ms')->id();
  206. $user = Staff::with('StaffDetail')->find($id);
  207. return view('market.commission_listR',compact('user'));
  208. }
  209. //Show comission list B
  210. public function commissionListB()
  211. {
  212. $id = Auth::guard('ms')->id();
  213. $user = Staff::find($id);
  214. $email = $user->email;
  215. $staffDetails = StaffDetail::where('email',$email)->first();
  216. return view('market.commission_listB',compact('staffDetails','user'));
  217. }
  218. //Show form for adding new commission R
  219. public function commissionFormR()
  220. {
  221. $id = Auth::guard('ms')->id();
  222. $user = Staff::find($id);
  223. $email = $user->email;
  224. $staffDetails = StaffDetail::where('email',$email)->first();
  225. return view('market.commission_formR',compact('staffDetails','user'));
  226. }
  227. //Show form for adding new commission B
  228. public function commissionFormB()
  229. {
  230. $id = Auth::guard('ms')->id();
  231. $user = Staff::find($id);
  232. $email = $user->email;
  233. $staffDetails = StaffDetail::where('email',$email)->first();
  234. return view('market.commission_formB',compact('staffDetails','user'));
  235. }
  236. public function getCommissionListR24()
  237. {
  238. $id = Auth::guard('ms')->id();
  239. $user = Staff::find($id);
  240. $email = $user->email;
  241. $marketdetail= Commission::where('contract','24')->where('formT','R')->orderBy('created_by','desc')->get();
  242. $curr = Carbon::now()->getTimestamp();
  243. $i = 0;
  244. $nested_data = array();
  245. if(!empty($marketdetail))
  246. {
  247. foreach ($marketdetail as $a)
  248. {
  249. $i++; $n1 = '';
  250. $reg_time = $a->created_at;
  251. $expiry_date = $reg_time->addDays(3);
  252. $expiry_date = $expiry_date->getTimestamp();
  253. if($curr < $expiry_date) {
  254. $n1 = "New/";
  255. }
  256. array_push($nested_data, array(
  257. 'index' => $n1.$i,
  258. 'formT' => $a->formT,
  259. 'ranges' => $a->ranges,
  260. 'comm' => $a->comm,
  261. 'category' => $a->category,
  262. 'contract' => $a->contract,
  263. 'action' => $a->_id
  264. ));
  265. }
  266. }
  267. return \DataTables::of($nested_data)->make(true);
  268. }
  269. public function getCommissionListR12()
  270. {
  271. $id = Auth::guard('ms')->id();
  272. $user = Staff::find($id);
  273. $email = $user->email;
  274. $marketdetail= Commission::where('contract','12')->where('formT','R')->orderBy('created_by','desc')->get();
  275. $curr = Carbon::now()->getTimestamp();
  276. $i = 0;
  277. $nested_data = array();
  278. if(!empty($marketdetail))
  279. {
  280. foreach ($marketdetail as $a)
  281. {
  282. $i++; $n1 = '';
  283. $reg_time = $a->created_at;
  284. $expiry_date = $reg_time->addDays(3);
  285. $expiry_date = $expiry_date->getTimestamp();
  286. if($curr < $expiry_date) {
  287. $n1 = "New/";
  288. }
  289. array_push($nested_data, array(
  290. 'index' => $n1.$i,
  291. 'formT' => $a->formT,
  292. 'ranges' => $a->ranges,
  293. 'comm' => $a->comm,
  294. 'category' => $a->category,
  295. 'contract' => $a->contract,
  296. 'action' => $a->_id
  297. ));
  298. }
  299. }
  300. return \DataTables::of($nested_data)->make(true);
  301. }
  302. public function getCommissionListB24Low()
  303. {
  304. $id = Auth::guard('ms')->id();
  305. $user = Staff::find($id);
  306. $email = $user->email;
  307. $marketdetail= Commission::where('contract','24')->where('formT','B')->where('packageR','10,30,50,100')->orderBy('created_by','desc')->get();
  308. $curr = Carbon::now()->getTimestamp();
  309. $i = 0;
  310. $nested_data = array();
  311. if(!empty($marketdetail))
  312. {
  313. foreach ($marketdetail as $a)
  314. {
  315. $i++; $n1 = '';
  316. $reg_time = $a->created_at;
  317. $expiry_date = $reg_time->addDays(3);
  318. $expiry_date = $expiry_date->getTimestamp();
  319. if($curr < $expiry_date) {
  320. $n1 = "New/";
  321. }
  322. array_push($nested_data, array(
  323. 'index' => $n1.$i,
  324. 'packageR' => $a->packageR,
  325. 'ranges' => $a->ranges,
  326. 'comm' => $a->comm,
  327. 'category' => $a->category,
  328. 'contract' => $a->contract,
  329. 'action' => $a->_id
  330. ));
  331. }
  332. }
  333. return \DataTables::of($nested_data)->make(true);
  334. }
  335. public function getCommissionListB24High()
  336. {
  337. $id = Auth::guard('ms')->id();
  338. $user = Staff::find($id);
  339. $email = $user->email;
  340. $marketdetail= Commission::where('contract','24')->where('formT','B')->orwhere('packageR','300')->orwhere('packageR','500')->orwhere('packageR','1')->orderBy('created_by','desc')->get();
  341. $curr = Carbon::now()->getTimestamp();
  342. $i = 0;
  343. $nested_data = array();
  344. if(!empty($marketdetail))
  345. {
  346. foreach ($marketdetail as $a)
  347. {
  348. $i++; $n1 = '';
  349. $reg_time = $a->created_at;
  350. $expiry_date = $reg_time->addDays(3);
  351. $expiry_date = $expiry_date->getTimestamp();
  352. if($curr < $expiry_date) {
  353. $n1 = "New/";
  354. }
  355. array_push($nested_data, array(
  356. 'index' => $n1.$i,
  357. 'packageR' => $a->packageR,
  358. 'ranges' => $a->ranges,
  359. 'comm' => $a->comm,
  360. 'category' => $a->category,
  361. 'contract' => $a->contract,
  362. 'action' => $a->_id
  363. ));
  364. }
  365. }
  366. return \DataTables::of($nested_data)->make(true);
  367. }
  368. public function getCommissionListB12Low()
  369. {
  370. $id = Auth::guard('ms')->id();
  371. $user = Staff::find($id);
  372. $email = $user->email;
  373. $marketdetail= Commission::where('contract','12')->where('formT','B')->where('packageR','10,30,50,100')->orderBy('created_by','desc')->get();
  374. $curr = Carbon::now()->getTimestamp();
  375. $i = 0;
  376. $nested_data = array();
  377. if(!empty($marketdetail))
  378. {
  379. foreach ($marketdetail as $a)
  380. {
  381. $i++; $n1 = '';
  382. $reg_time = $a->created_at;
  383. $expiry_date = $reg_time->addDays(3);
  384. $expiry_date = $expiry_date->getTimestamp();
  385. if($curr < $expiry_date) {
  386. $n1 = "New/";
  387. }
  388. array_push($nested_data, array(
  389. 'index' => $n1.$i,
  390. 'packageR' => $a->packageR,
  391. 'ranges' => $a->ranges,
  392. 'comm' => $a->comm,
  393. 'category' => $a->category,
  394. 'contract' => $a->contract,
  395. 'action' => $a->_id
  396. ));
  397. }
  398. }
  399. return \DataTables::of($nested_data)->make(true);
  400. }
  401. //Function to get commission list (1 year)
  402. public function getCommissionListB12High()
  403. {
  404. $id = Auth::guard('ms')->id();
  405. $user = Staff::find($id);
  406. $email = $user->email;
  407. $marketdetail= Commission::where('contract','12')->where('formT','B')->orwhere('packageR','300')->orwhere('packageR','500')->orwhere('packageR','1')->orderBy('created_by','desc')->get();
  408. $curr = Carbon::now()->getTimestamp();
  409. $i = 0;
  410. $nested_data = array();
  411. if(!empty($marketdetail))
  412. {
  413. foreach ($marketdetail as $a)
  414. {
  415. $i++; $n1 = '';
  416. $reg_time = $a->created_at;
  417. $expiry_date = $reg_time->addDays(3);
  418. $expiry_date = $expiry_date->getTimestamp();
  419. if($curr < $expiry_date) {
  420. $n1 = "New/";
  421. }
  422. array_push($nested_data, array(
  423. 'index' => $n1.$i,
  424. 'packageR' => $a->packageR,
  425. 'ranges' => $a->ranges,
  426. 'comm' => $a->comm,
  427. 'category' => $a->category,
  428. 'contract' => $a->contract,
  429. 'action' => $a->_id
  430. ));
  431. }
  432. }
  433. return \DataTables::of($nested_data)->make(true);
  434. }
  435. //Function to add company
  436. public function requestAddCompany(Request $request)
  437. {
  438. $companyData = [
  439. 'name' => $request->get('aname'),
  440. 'address' => $request->get('address'),
  441. 'city' => $request->get('city'),
  442. 'state' => $request->get('state'),
  443. 'postcode' => $request->get('postcode'),
  444. 'color' => '',
  445. 'team' => 'Dealer',
  446. 'status' => '',
  447. ];
  448. $companyDetail = Company::create($companyData);
  449. return redirect()->back()->with('success_msg', 'message');
  450. }
  451. //Function to add commission R
  452. public function requestAddCommissionR(Request $request)
  453. {
  454. $commissionData = [
  455. 'contract' => $request->get('contract'),
  456. 'ranges' => $request->get('ranges'),
  457. 'comm' => $request->get('comm'),
  458. 'category' => $request->get('categori'),
  459. 'formT' => 'R',
  460. 'packageR' => '',
  461. ];
  462. $commissionDetail = Commission::create($commissionData);
  463. return redirect()->back()->with('success_msg', 'message');
  464. }
  465. //Function to add commission B
  466. public function requestAddCommissionB(Request $request)
  467. {
  468. $commissionData = [
  469. 'packageR' => $request->get('packageR'),
  470. 'contract' => $request->get('contract'),
  471. 'ranges' => $request->get('ranges'),
  472. 'comm' => $request->get('comm'),
  473. 'category' => $request->get('categori'),
  474. 'formT' => 'B',
  475. ];
  476. $commissionDetail = Commission::create($commissionData);
  477. return redirect()->back()->with('success_msg', 'message');
  478. }
  479. /***
  480. / Encrypt Password
  481. ***/
  482. public function encryptPassword($action, $string) {
  483. $output = false;
  484. $encrypt_method = "AES-256-CBC";
  485. $secret_key = '28472B4B6250655368566D5970337336';
  486. $secret_iv = md5(md5($string)); //'This is my secret iv';
  487. // hash
  488. $key = hash('sha256', $secret_key);
  489. $iv = substr(hash('sha256', $secret_iv), 0, 16);
  490. if ( $action == 'encrypt' ) {
  491. $output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
  492. $output = base64_encode($output);
  493. } else if( $action == 'decrypt' ) {
  494. $output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
  495. }
  496. return $output;
  497. }
  498. //Function to add agent
  499. public function requestAddAgent(Request $request)
  500. {
  501. $validator = Validator::make($request->all(), [
  502. 'password' => 'min:8|confirmed',
  503. ]);
  504. if ($validator->fails()) {
  505. return redirect()->back()->withInput()->withErrors($validator);
  506. }
  507. $user = Staff::where('email', $request->get('email'))->first();
  508. if (empty($user)) {
  509. $pass = $this->encryptPassword('encrypt', $request->get('password'));
  510. $dealerD = new StaffDetail;
  511. $dealerD->name = $request->get('aname');
  512. $dealerD->ic = $request->get('ic');
  513. $dealerD->email = $request->get('email');
  514. $dealerD->phone = $request->get('phone');
  515. $dealerD->company_id = $request->get('vendor');
  516. $dealerD->color = '';
  517. $dealerD->position = 'Dealer';
  518. $dealerD->password = $pass;
  519. $dealerD->user_pic = 'assets/avatar/user.png';
  520. // Save Login Dealer
  521. $loginD = new Staff;
  522. $loginD->email = $request->get('email');
  523. $loginD->password = $pass;
  524. $loginD->roles_access = 'Dealer';
  525. $loginD->company_id = $request->get('vendor');
  526. $loginD->last_login_at = '';
  527. $loginD->last_login_ip = '';
  528. $company = Company::where('_id', $request->get('vendor'))->first();
  529. $company->staff()->save($loginD);
  530. // $dealer = DealerLaravel::create($loginData);
  531. $loginD->staffdetail()->save($dealerD);
  532. return redirect()->back()->with('success_msg', '<strong>Success!</strong> register <strong>'.$request->get('email').'</strong>');
  533. }
  534. else
  535. {
  536. return redirect()->back()->withInput()->withErrors('Email already exist!');
  537. }
  538. }
  539. //Function to delete company(dealer)
  540. public function deleteCompany(Request $request)
  541. {
  542. $company = Company::where('_id',$request->id)->first();
  543. if(!empty($company)){
  544. $company->delete();
  545. return 'true';
  546. }else {
  547. return 'false';
  548. }
  549. }
  550. //Function to delete Commission (R)
  551. public function deleteCommission(Request $request)
  552. {
  553. // CommissionLaravel::destroy($request);
  554. // return response()->json(['result'=>'success']);
  555. // return response()->json(['success'=>'Got Simple Ajax Request.']);
  556. $commission = Commission::where('_id',$request->id)->first();
  557. if(!empty($commission)){
  558. $commission->delete();
  559. return 'true';
  560. }else {
  561. return 'false';
  562. }
  563. }
  564. public function deleteAgent(Request $request)
  565. {
  566. // $agentdetail = StaffDetailLaravel::where('email',$request->email)->first();
  567. $agent = Staff::where('_id',$request->id)->first();
  568. $agentdetail = $agent->staffdetail()->first();
  569. if(!empty($agentdetail) && !empty($agent))
  570. {
  571. $agent->delete();
  572. $agentdetail->delete();
  573. return 'true';
  574. }
  575. else
  576. {
  577. return 'false';
  578. }
  579. }
  580. public function editCompany($company_id)
  581. {
  582. $id = Auth::guard('ms')->id();
  583. $user = Staff::with('StaffDetail')->find($id);
  584. $company = Company::where('_id', $company_id)->get();
  585. return view('market.edit_dealer',compact('user','company'));
  586. }
  587. public function updateCompany(Request $request)
  588. {
  589. $user = Company::where('_id', $request->get('company_id'))->first();
  590. $user->name = $request->get('aname');
  591. $user->address = $request->get('address');
  592. $user->city = $request->get('city');
  593. $user->state = $request->get('state');
  594. $user->postcode = $request->get('postcode');
  595. $user->color = '';
  596. $user->team = 'Dealer';
  597. $user->status = '';
  598. $user->save();
  599. return redirect()->route('mHome')->with('success_msg', 'message');
  600. }
  601. public function editCommissionR($commission_id)
  602. {
  603. $id = Auth::guard('ms')->id();
  604. $user = Staff::with('staffdetail')->find($id);
  605. $commission = Commission::where('_id', $commission_id)->get();
  606. return view('market.edit_commissionR',compact('user','commission'));
  607. }
  608. public function editCommissionB($commission_id)
  609. {
  610. $id = Auth::guard('ms')->id();
  611. $user = Staff::find($id);
  612. $email = $user->email;
  613. $staffDetails = StaffDetail::where('email',$email)->first();
  614. $commission = Commission::where('_id', $commission_id)->get();
  615. return view('market.edit_commissionB',compact('staffDetails','user','commission'));
  616. }
  617. public function editAgent($agent_id)
  618. {
  619. $id = Auth::guard('ms')->id();
  620. $user = Staff::with('StaffDetail')->find($id);
  621. $agentdetail = Staff::where('_id', $agent_id)->first();
  622. $company = Company::where('team', 'Dealer')->get();
  623. return view('market.edit_agent', compact('user', 'agentdetail', 'company'));
  624. }
  625. public function updateCommissionR(Request $request)
  626. {
  627. $commission = Commission::where('_id', $request->get('commission_id'))->first();
  628. $commission->contract = $request->get('contract');
  629. $commission->ranges = $request->get('ranges');
  630. $commission->comm = $request->get('comm');
  631. $commission->category = $request->get('categori');
  632. $commission->formT = 'R';
  633. $commission->packageR = '';
  634. $commission->save();
  635. return redirect()->route('commissionListR')->with('success_msg', 'message');
  636. }
  637. public function updateCommissionB(Request $request)
  638. {
  639. $commission = Commission::where('_id', $request->get('commission_id'))->first();
  640. $commission->packageR = $request->get('packageR');
  641. $commission->contract = $request->get('contract');
  642. $commission->ranges = $request->get('ranges');
  643. $commission->comm = $request->get('comm');
  644. $commission->category = $request->get('categori');
  645. $commission->formT = 'B';
  646. $commission->save();
  647. return redirect()->route('commissionListB')->with('success_msg', 'message');
  648. }
  649. public function updateAgent(Request $request)
  650. {
  651. $user = Staff::where('_id', $request->agent_id)->first();
  652. $validate = Staff::where('email', $request->email)->first();
  653. $detail = $user->staffdetail()->first();
  654. $getpass = $request->get('password');
  655. $pass = $this->encryptPassword('encrypt', $request->get('password'));
  656. if(empty($validate))
  657. {
  658. $detail->name = $request->get('aname');
  659. $detail->ic = $request->get('ic');
  660. $detail->email = $request->get('email');
  661. $detail->phone = $request->get('phone');
  662. $detail->company_id = $request->get('vendor');
  663. $user->email = $request->get('email');
  664. $user->company_id = $request->get('vendor');
  665. if(!empty($getpass))
  666. {
  667. $validator = Validator::make($request->all(), [
  668. 'password' => 'min:8|confirmed',
  669. ]);
  670. if ($validator->fails())
  671. {
  672. return redirect()->back()->withInput()->withErrors($validator);
  673. }
  674. $detail->password = $pass;
  675. $user->password = $pass;
  676. $detail->save();
  677. $user->save();
  678. }
  679. else
  680. {
  681. $detail->save();
  682. $user->save();
  683. }
  684. return redirect()->route('agentList')->with('success_msg', 'message');
  685. }
  686. else
  687. {
  688. if($user->email == $request->email)
  689. {
  690. echo "".$request->get('password');
  691. $detail->name = $request->get('aname');
  692. $detail->ic = $request->get('ic');
  693. $detail->email = $request->get('email');
  694. $detail->phone = $request->get('phone');
  695. $detail->company_id = $request->get('vendor');
  696. $detail->save();
  697. $user->email = $request->get('email');
  698. if(!empty($getpass))
  699. {
  700. $validator = Validator::make($request->all(), [
  701. 'password' => 'min:8|confirmed',
  702. ]);
  703. if ($validator->fails())
  704. {
  705. return redirect()->back()->withInput()->withErrors($validator);
  706. }
  707. $detail->password = $pass;
  708. $user->password = $pass;
  709. $detail->save();
  710. $user->save();
  711. }
  712. else
  713. {
  714. $detail->save();
  715. $user->save();
  716. }
  717. return redirect()->route('agentList')->with('success_msg', 'message');
  718. }
  719. else
  720. {
  721. echo "email is used";
  722. return redirect()->back()->withInput()->withErrors('Email already exist!');
  723. }
  724. }
  725. }
  726. public function filterDocket($year, $month)
  727. {
  728. $year = $year;
  729. $month = $month;
  730. $nested_data = array();
  731. $arr = array();
  732. $i=0;
  733. if($year=='null' && $month=='null')
  734. {
  735. $docket = Docket::with('WorkOrder')->orderBy('created_at', 'desc')->get();
  736. $arr = array();
  737. $i = 0;$n1 = '';
  738. $nested_data = array();
  739. $test = 0;
  740. foreach ($docket as $a)
  741. {
  742. $i++;
  743. $building = "";
  744. if(!empty($a->WorkOrder->_id)){
  745. $subDetail = Form::with('WorkOrder','Subscriber')->where('_id',$a->WorkOrder->_id)->first();
  746. $contractor = Company::where('_id', $a->WorkOrder->contractor_id)->first();
  747. $tempInstaller = Staff::with('StaffDetail')->withTrashed()->where('_id',$a->installer_id)->first();
  748. if(!empty($subDetail->Subscriber->building_name))
  749. $building=$subDetail->Subscriber->building_name;
  750. else
  751. $building=$subDetail->Subscriber->company_name;
  752. array_push($nested_data, array(
  753. 'index' => $n1.$i,
  754. 'docket_id' => $a->docket_id,
  755. 'work_order_id' => $a->work_order_id,
  756. 'nature_work' => $a->nature_work,
  757. 'contractor_id' => $contractor->name,
  758. 'installer_name' => $tempInstaller->StaffDetail->name,
  759. 'customer_id' => $building,
  760. 'installer_id' => $tempInstaller->StaffDetail->phone,
  761. 'end_job' => $a->end_job,
  762. 'rating' => round((($a->Rating1 + $a->Rating2 + $a->Rating3)/15) * 100)."%",
  763. ));
  764. }
  765. }
  766. }
  767. elseif($year!='' && $month=='null')
  768. {
  769. $timestamp = $year."-01-01 00:00:00.000Z";
  770. $masa = strtotime($timestamp);
  771. $go = Carbon::createFromTimestamp($masa);
  772. $go2 = Carbon::createFromTimestamp($masa);
  773. $end_year = $go2->endOfYear();
  774. // $time = Carbon::createFromTimeString($timestamp);
  775. // echo $month;
  776. $docket = Docket::with('WorkOrder')->whereBetween('created_at', [$go, $end_year])->orderBy('created_at', 'desc')->get();
  777. $arr = array();
  778. $i = 0;$n1 = '';
  779. $nested_data = array();
  780. $test = 0;
  781. foreach ($docket as $a)
  782. {
  783. $i++;
  784. $building = "";
  785. if(!empty($a->WorkOrder->_id)){
  786. $subDetail = Form::with('WorkOrder','Subscriber')->where('_id',$a->WorkOrder->_id)->first();
  787. $contractor = Company::where('_id', $a->WorkOrder->contractor_id)->first();
  788. $tempInstaller = StaffDetail::where('_id', $a->installer_id)->withTrashed()->first();
  789. if(!empty($subDetail->Subscriber->building_name))
  790. $building=$subDetail->Subscriber->building_name;
  791. else
  792. $building=$subDetail->Subscriber->company_name;
  793. array_push($nested_data, array(
  794. 'index' => $n1.$i,
  795. 'docket_id' => $a->docket_id,
  796. 'work_order_id' => $a->work_order_id,
  797. 'nature_work' => $a->nature_work,
  798. 'contractor_id' => $contractor->name,
  799. 'installer_name' => $tempInstaller->name,
  800. 'customer_id' => $building,
  801. 'installer_id' => $tempInstaller->phone,
  802. 'end_job' => $a->end_job,
  803. 'rating' => round((($a->Rating1 + $a->Rating2 + $a->Rating3)/15) * 100)."%",
  804. ));
  805. }
  806. }
  807. }
  808. elseif($year!='' && $month !='')
  809. {
  810. $timestamp = $year."-".$month."-01 00:00:00.000Z";
  811. $masa = strtotime($timestamp);
  812. $go = Carbon::createFromTimestamp($masa);
  813. $go2 = Carbon::createFromTimestamp($masa);
  814. $end_year = $go2->endOfMonth();
  815. // $time = Carbon::createFromTimeString($timestamp);
  816. // $docket = Docket::whereBetween('created_at', [$go, $end_year])->get();
  817. $docket = Docket::with('WorkOrder')->whereBetween('created_at', [$go, $end_year])->orderBy('created_at', 'desc')->get();
  818. $arr = array();
  819. $i = 0;$n1 = '';
  820. $nested_data = array();
  821. $test = 0;
  822. foreach ($docket as $a)
  823. {
  824. $i++;
  825. $building = "";
  826. if(!empty($a->WorkOrder->_id)){
  827. $subDetail = Form::with('WorkOrder','Subscriber')->where('_id',$a->WorkOrder->_id)->first();
  828. $contractor = Company::where('_id', $a->WorkOrder->contractor_id)->first();
  829. $tempInstaller = StaffDetail::where('_id', $a->installer_id)->withTrashed()->first();
  830. if(!empty($subDetail->Subscriber->building_name))
  831. $building=$subDetail->Subscriber->building_name;
  832. else
  833. $building=$subDetail->Subscriber->company_name;
  834. array_push($nested_data, array(
  835. 'index' => $n1.$i,
  836. 'docket_id' => $a->docket_id,
  837. 'work_order_id' => $a->work_order_id,
  838. 'nature_work' => $a->nature_work,
  839. 'contractor_id' => $contractor->name,
  840. 'installer_name' => $tempInstaller->name,
  841. 'customer_id' => $building,
  842. 'installer_id' => $tempInstaller->phone,
  843. 'end_job' => $a->end_job,
  844. 'rating' => round((($a->Rating1 + $a->Rating2 + $a->Rating3)/15) * 100)."%",
  845. ));
  846. }
  847. }
  848. }
  849. return \DataTables::of($nested_data)->make(true);
  850. }
  851. public function requestClaim(Request $request)
  852. {
  853. $claim = DealerClaim::where('_id',$request->id)->first();
  854. // $agentdetail = StaffDetailLaravel::where('email',$request->email)->first();
  855. $mytime = Carbon::now();
  856. if(!empty($claim))
  857. {
  858. $claim->claim_dt = $mytime->toDateTimeString();
  859. $claim->save();
  860. return 'true';
  861. }
  862. else
  863. {
  864. return 'false';
  865. }
  866. }
  867. public function docketList()
  868. {
  869. $id = Auth::guard('ms')->id();
  870. $user = Staff::with('StaffDetail')->find($id);
  871. return view('market.docket',compact('user'));
  872. }
  873. public function getDocketList()
  874. {
  875. $id = Auth::guard('ms')->id();
  876. $user = Staff::with('StaffDetail')->find($id);
  877. $docket = Docket::with('WorkOrder')->orderBy('created_at', 'desc')->get();
  878. $arr = array();
  879. $i = 0;$n1 = '';
  880. $nested_data = array();
  881. $test = 0;
  882. foreach ($docket as $a)
  883. {
  884. $i++;
  885. $building = "";
  886. if(!empty($a->WorkOrder->_id)){
  887. $subDetail = Form::with('WorkOrder','Subscriber')->where('_id',$a->WorkOrder->_id)->first();
  888. $contractor = Company::where('_id', $a->WorkOrder->contractor_id)->first();
  889. $tempInstaller = Staff::with('StaffDetail')->withTrashed()->where('_id',$a->installer_id)->first();
  890. if(!empty($subDetail->Subscriber->building_name))
  891. $building=$subDetail->Subscriber->building_name;
  892. else
  893. $building=$subDetail->Subscriber->company_name;
  894. array_push($nested_data, array(
  895. 'index' => $n1.$i,
  896. 'docket_id' => $a->docket_id,
  897. 'work_order_id' => $a->work_order_id,
  898. 'nature_work' => $a->nature_work,
  899. 'contractor_id' => $contractor->name,
  900. 'installer_name' => $tempInstaller->StaffDetail->name,
  901. 'customer_id' => $building,
  902. 'installer_id' => $tempInstaller->StaffDetail->phone,
  903. 'end_job' => $a->end_job,
  904. 'rating' => round((($a->Rating1 + $a->Rating2 + $a->Rating3)/15) * 100)."%",
  905. ));
  906. }
  907. }
  908. return \DataTables::of($nested_data)->make(true);
  909. }
  910. public function updateClaimDate(Request $request)
  911. {
  912. $claim = DealerClaim::where("_id", $request->id)->first();
  913. if(!empty($claim))
  914. {
  915. $claim->claim_dt = $request->date." ".$request->time;
  916. $claim->save();
  917. return 'true';
  918. }
  919. else
  920. return 'false';
  921. }
  922. public function downloadPDF()
  923. {
  924. $pdf = PDF::loadView('pdf.docket-pdf');
  925. return $pdf->download('invoice.pdf');
  926. // return view('pdf.docket-pdf');
  927. }
  928. public function woList()
  929. {
  930. $id = Auth::guard('ms')->id();
  931. $user = Staff::with('StaffDetail')->find($id);
  932. return view('market.workorder',compact('user'));
  933. }
  934. public function getWoList()
  935. {
  936. $id = Auth::guard('ms')->id();
  937. $user = Staff::with('StaffDetail')->find($id);
  938. // $marketdetail= WorkOrder::with('Form')->where('_id','5b4c18a4ee0dc2b707473b7d')->first();
  939. // $b = array();
  940. $r=0;$b=0;
  941. $subDetail = Form::with('WorkOrder','Subscriber')->orderBy('created_at','desc')->get();
  942. $curr = Carbon::now()->getTimestamp();
  943. $i = 0;
  944. $nested_data = array();
  945. foreach($subDetail as $a)
  946. {
  947. $var1="";
  948. if(empty($a->WorkOrder)){}
  949. else
  950. {
  951. $i++;
  952. $staff = StaffDetail::where('_id', $a->WorkOrder->installer_id)->withTrashed()->first();
  953. $contractor = Company::where('_id', $a->WorkOrder->contractor_id)->first();
  954. if(!empty($staff))
  955. {
  956. $var1 =$staff->name;
  957. }
  958. $reg_time = $a->created_at;
  959. $expiry_date = $reg_time->addDays(3);
  960. $expiry_date = $expiry_date->getTimestamp();
  961. if($curr < $expiry_date) {
  962. $n1 = "New/";
  963. }
  964. $building = '';
  965. if($a->type_application == 'R'){
  966. $building = $a->Subscriber->building_name;
  967. }else if($a->type_application == 'B'){
  968. $building = $a->Subscriber->company_name;
  969. }
  970. if(!empty($a->Subscriber)){
  971. array_push($nested_data, array(
  972. 'index' => $n1.$i.$a->type_application,
  973. 'nature_work' => $a->WorkOrder->nature_work,
  974. 'wo'=>$a->WorkOrder->wo,
  975. 'customer_name' => $a->Subscriber->name,
  976. 'customer_phone' => $a->Subscriber->phone1,
  977. 'customer_unit' => $a->Subscriber->unit_no,
  978. 'customer_building' => $building,
  979. 'customer_postcode' => $a->Subscriber->postcode,
  980. 'customer_city' => $a->Subscriber->city,
  981. 'contractor_id' => $contractor->name,
  982. 'installer_id' => $var1,
  983. 'dateTimeEnd' => $a->WorkOrder->dateTimeEnd,
  984. 'status' => $a->WorkOrder->status,
  985. ));
  986. }
  987. }
  988. }
  989. return \DataTables::of($nested_data)->make(true);
  990. }
  991. public function filterWo($year, $month, $status)
  992. {
  993. $r = 0;
  994. $b = 0;
  995. $nested_data = array();
  996. if($year == 'null' && $month == 'null' & $status !='')
  997. {
  998. $i = 0;
  999. // $subDetail = Form::with('WorkOrder','Subscriber')->where('_id', $a->id)->first();
  1000. $wo = WorkOrder::with('Form')->orderBy('dateTimeStart', 'desc')->where('status', $status)->get();
  1001. foreach($wo as $a)
  1002. {
  1003. $customer = Subscriber::where('_id', $a->Form->_id)->first();
  1004. $contractor = Company::where('_id', $a->contractor_id)->first();
  1005. $installer = StaffDetail::where('_id', $a->installer_id)->withTrashed()->first();
  1006. $var1 = "";
  1007. if(!empty($installer)){
  1008. $var1 =$installer->name;
  1009. }
  1010. if($a->Form->type_application == "R")
  1011. {
  1012. $r++; $n1 = 'R';
  1013. $customer = Subscriber::where('_id', $a->Form->_id)->first();
  1014. if(!empty($customer)){
  1015. array_push($nested_data, array(
  1016. 'index' => $r.$n1,
  1017. 'nature_work' => $a->nature_work,
  1018. 'wo'=>$a->wo,
  1019. 'customer_name' => $customer->name,
  1020. 'customer_phone' => $customer->phone1,
  1021. 'customer_unit' => $customer->unit_no,
  1022. 'customer_building' => $customer->building_name,
  1023. 'customer_postcode' => $customer->postcode,
  1024. 'customer_city' => $customer->city,
  1025. 'contractor_id' => $contractor->name,
  1026. 'installer_id' => $var1,
  1027. 'dateTimeEnd' => $a->dateTimeEnd,
  1028. 'status' => $a->status,
  1029. ));
  1030. }
  1031. }
  1032. if($a->Form->type_application == "B")
  1033. {
  1034. $b++; $n1 = 'B';
  1035. $customer = Subscriber::where('_id', $a->Form->_id)->first();
  1036. if(!empty($customer)){
  1037. array_push($nested_data, array(
  1038. 'index' => $b.$n1,
  1039. 'nature_work' => $a->nature_work,
  1040. 'wo'=>$a->wo,
  1041. 'customer_name' => $customer->name,
  1042. 'customer_phone' => $customer->phone1,
  1043. 'customer_unit' => '-',
  1044. 'customer_building' => $customer->company_name,
  1045. 'customer_postcode' => $customer->postcode,
  1046. 'customer_city' => $customer->city,
  1047. 'contractor_id' => $contractor->name,
  1048. 'installer_id' => $var1,
  1049. 'dateTimeEnd' => $a->dateTimeEnd,
  1050. 'status' => $a->status,
  1051. ));
  1052. }
  1053. }
  1054. }
  1055. }
  1056. if($year=='null' && $month == 'null' & $status == 'null')
  1057. {
  1058. $i = 0;
  1059. // $subDetail = Form::with('WorkOrder','Subscriber')->where('_id', $a->id)->first();
  1060. $wo = WorkOrder::with('Form')->orderBy('dateTimeStart','desc')->get();
  1061. foreach($wo as $a)
  1062. {
  1063. $customer = Subscriber::where('_id', $a->Form->_id)->first();
  1064. $contractor = Company::where('_id', $a->contractor_id)->first();
  1065. $installer = StaffDetail::where('_id', $a->installer_id)->withTrashed()->first();
  1066. $var1 = "";
  1067. if(!empty($installer)){
  1068. $var1 =$installer->name;
  1069. }
  1070. if($a->Form->type_application == "R")
  1071. {
  1072. $r++; $n1 = 'R';
  1073. $customer = Subscriber::where('_id', $a->Form->_id)->first();
  1074. if(!empty($customer)){
  1075. array_push($nested_data, array(
  1076. 'index' => $r.$n1,
  1077. 'nature_work' => $a->nature_work,
  1078. 'wo'=>$a->wo,
  1079. 'customer_name' => $customer->name,
  1080. 'customer_phone' => $customer->phone1,
  1081. 'customer_unit' => $customer->unit_no,
  1082. 'customer_building' => $customer->building_name,
  1083. 'customer_postcode' => $customer->postcode,
  1084. 'customer_city' => $customer->city,
  1085. 'contractor_id' => $contractor->name,
  1086. 'installer_id' => $var1,
  1087. 'dateTimeEnd' => $a->dateTimeEnd,
  1088. 'status' => $a->status,
  1089. ));
  1090. }
  1091. }
  1092. if($a->Form->type_application == "B")
  1093. {
  1094. $b++; $n1 = 'B';
  1095. $customer = Subscriber::where('_id', $a->Form->_id)->first();
  1096. if(!empty($customer)){
  1097. array_push($nested_data, array(
  1098. 'index' => $b.$n1,
  1099. 'nature_work' => $a->nature_work,
  1100. 'wo'=>$a->wo,
  1101. 'customer_name' => $customer->name,
  1102. 'customer_phone' => $customer->phone1,
  1103. 'customer_unit' => '-',
  1104. 'customer_building' => $customer->company_name,
  1105. 'customer_postcode' => $customer->postcode,
  1106. 'customer_city' => $customer->city,
  1107. 'contractor_id' => $contractor->name,
  1108. 'installer_id' => $var1,
  1109. 'dateTimeEnd' => $a->dateTimeEnd,
  1110. 'status' => $a->status,
  1111. ));
  1112. }
  1113. }
  1114. }
  1115. }
  1116. if($year!='' && $month!='' && $status !='')
  1117. {
  1118. $start_month = $year."-".$month."-01 00:00:00";
  1119. $end_month = $year."-".$month."-32 23:59:59";
  1120. // $wo = WorkOrder::orderBy('dateTimeStart', 'desc')->whereBetween('dateTimeStart', [$start_month, $end_month])->where('status',$status)->get();
  1121. $i = 0;
  1122. // $subDetail = Form::with('WorkOrder','Subscriber')->where('_id', $a->id)->first();
  1123. $wo = WorkOrder::with('Form')->orderBy('dateTimeStart', 'desc')->whereBetween('dateTimeStart', [$start_month, $end_month])->where('status', $status)->get();
  1124. foreach($wo as $a)
  1125. {
  1126. $customer = Subscriber::where('_id', $a->Form->_id)->first();
  1127. $contractor = Company::where('_id', $a->contractor_id)->first();
  1128. $installer = StaffDetail::where('_id', $a->installer_id)->withTrashed()->first();
  1129. $var1 = "";
  1130. if(!empty($installer)){
  1131. $var1 =$installer->name;
  1132. }
  1133. if($a->Form->type_application == "R")
  1134. {
  1135. $r++; $n1 = 'R';
  1136. $customer = Subscriber::where('_id', $a->Form->_id)->first();
  1137. if(!empty($customer)){
  1138. array_push($nested_data, array(
  1139. 'index' => $r.$n1,
  1140. 'nature_work' => $a->nature_work,
  1141. 'wo'=>$a->wo,
  1142. 'customer_name' => $customer->name,
  1143. 'customer_phone' => $customer->phone1,
  1144. 'customer_unit' => $customer->unit_no,
  1145. 'customer_building' => $customer->building_name,
  1146. 'customer_postcode' => $customer->postcode,
  1147. 'customer_city' => $customer->city,
  1148. 'contractor_id' => $contractor->name,
  1149. 'installer_id' => $var1,
  1150. 'dateTimeEnd' => $a->dateTimeEnd,
  1151. 'status' => $a->status,
  1152. ));
  1153. }
  1154. }
  1155. if($a->Form->type_application == "B")
  1156. {
  1157. $b++; $n1 = 'B';
  1158. $customer = Subscriber::where('_id', $a->Form->_id)->first();
  1159. if(!empty($customer)){
  1160. array_push($nested_data, array(
  1161. 'index' => $b.$n1,
  1162. 'nature_work' => $a->nature_work,
  1163. 'wo'=>$a->wo,
  1164. 'customer_name' => $customer->name,
  1165. 'customer_phone' => $customer->phone1,
  1166. 'customer_unit' => '-',
  1167. 'customer_building' => $customer->company_name,
  1168. 'customer_postcode' => $customer->postcode,
  1169. 'customer_city' => $customer->city,
  1170. 'contractor_id' => $contractor->name,
  1171. 'installer_id' => $var1,
  1172. 'dateTimeEnd' => $a->dateTimeEnd,
  1173. 'status' => $a->status,
  1174. ));
  1175. }
  1176. }
  1177. }
  1178. }
  1179. if($year!='' && $month!='' && $status =='null')
  1180. {
  1181. $start_month = $year."-".$month."-01 00:00:00";
  1182. $end_month = $year."-".$month."-32 23:59:59";
  1183. $i = 0;
  1184. // $subDetail = Form::with('WorkOrder','Subscriber')->where('_id', $a->id)->first();
  1185. $wo = WorkOrder::with('Form')->orderBy('dateTimeStart', 'desc')->whereBetween('dateTimeStart', [$start_month, $end_month])->get();
  1186. foreach($wo as $a)
  1187. {
  1188. $customer = Subscriber::where('_id', $a->Form->_id)->first();
  1189. $contractor = Company::where('_id', $a->contractor_id)->first();
  1190. $installer = StaffDetail::where('_id', $a->installer_id)->withTrashed()->first();
  1191. $var1 = "";
  1192. if(!empty($installer)){
  1193. $var1 =$installer->name;
  1194. }
  1195. if($a->Form->type_application == "R")
  1196. {
  1197. $r++; $n1 = 'R';
  1198. $customer = Subscriber::where('_id', $a->Form->_id)->first();
  1199. if(!empty($customer)){
  1200. array_push($nested_data, array(
  1201. 'index' => $r.$n1,
  1202. 'nature_work' => $a->nature_work,
  1203. 'wo'=>$a->wo,
  1204. 'customer_name' => $customer->name,
  1205. 'customer_phone' => $customer->phone1,
  1206. 'customer_unit' => $customer->unit_no,
  1207. 'customer_building' => $customer->building_name,
  1208. 'customer_postcode' => $customer->postcode,
  1209. 'customer_city' => $customer->city,
  1210. 'contractor_id' => $contractor->name,
  1211. 'installer_id' => $var1,
  1212. 'dateTimeEnd' => $a->dateTimeEnd,
  1213. 'status' => $a->status,
  1214. ));
  1215. }
  1216. }
  1217. if($a->Form->type_application == "B")
  1218. {
  1219. $b++; $n1 = 'B';
  1220. $customer = Subscriber::where('_id', $a->Form->_id)->first();
  1221. if(!empty($customer)){
  1222. array_push($nested_data, array(
  1223. 'index' => $b.$n1,
  1224. 'nature_work' => $a->nature_work,
  1225. 'wo'=>$a->wo,
  1226. 'customer_name' => $customer->name,
  1227. 'customer_phone' => $customer->phone1,
  1228. 'customer_unit' => '-',
  1229. 'customer_building' => $customer->company_name,
  1230. 'customer_postcode' => $customer->postcode,
  1231. 'customer_city' => $customer->city,
  1232. 'contractor_id' => $contractor->name,
  1233. 'installer_id' => $var1,
  1234. 'dateTimeEnd' => $a->dateTimeEnd,
  1235. 'status' => $a->status,
  1236. ));
  1237. }
  1238. }
  1239. }
  1240. }
  1241. return \DataTables::of($nested_data)->make(true);
  1242. }
  1243. /**
  1244. ALL DOWN HERE ARE FOR TESTING AND UTILITIES PURPOSES
  1245. **/
  1246. public function woToLaravel()
  1247. {
  1248. }
  1249. public function checktry()
  1250. {
  1251. $data = Subscriber::where('wo','!=','')->get();
  1252. $wo = WorkOrder::all()->toArray();
  1253. $adawork = array();
  1254. $newarr = array();
  1255. $hmm = array();
  1256. $i = 0;
  1257. foreach($data as $d){
  1258. if(array_search($d->wo, array_column($wo, 'wo')) !== False) { // search value in the array
  1259. // echo "FOUND";
  1260. }else {
  1261. $adawork[] = $d->wo;
  1262. $i++;
  1263. }
  1264. }
  1265. dd($adawork);
  1266. }
  1267. //Insert data into Commission_Laravel table
  1268. public function commissionlara()
  1269. {
  1270. $olddata = Commission::all();
  1271. foreach($olddata as $od)
  1272. {
  1273. $subsData = [
  1274. 'formT' => $od['formT'],
  1275. 'ranges' => $od['ranges'],
  1276. 'comm' => $od['comm'],
  1277. 'category' => $od['category'],
  1278. 'contract' => $od['contract'],
  1279. 'reg_time' => $od['reg_time'],
  1280. 'packageR' => $od['packageR'],
  1281. ];
  1282. Commission::create($subsData);
  1283. }
  1284. return "success";
  1285. }
  1286. //Insert data into Company_Laravel table
  1287. public function companylara()
  1288. {
  1289. $olddata = Company::all();
  1290. foreach($olddata as $od)
  1291. {
  1292. $subsData = [
  1293. 'name' => $od['name'],
  1294. 'address' => $od['address'],
  1295. 'city' => $od['city'],
  1296. 'state' => $od['state'],
  1297. 'postcode' => $od['postcode'],
  1298. 'color' => $od['color'],
  1299. 'team' => $od['team'],
  1300. 'status' => $od['status'],
  1301. 'reg_time' => $od['reg_time'],
  1302. ];
  1303. Company::create($subsData);
  1304. }
  1305. }
  1306. //Insert data into Claim_Laravel table
  1307. public function claimtolara()
  1308. {
  1309. $olddata = DealerClaim::all();
  1310. foreach($olddata as $od)
  1311. {
  1312. $subsData = [
  1313. '_id' => $od['_id'],
  1314. 'formT' => $od['formT'],
  1315. 'wo' => $od['wo'],
  1316. 'dateTimeStart' => $od['dateTimeEnd'],
  1317. 'customer_id' => $od['customer_id'],
  1318. 'contractor_id' => $od['contractor_id'],
  1319. 'installer_id' => $od['installer_id'],
  1320. 'nature_work' => $od['nature_work'],
  1321. 'sub_category' => $od['sub_category'],
  1322. 'img_url' => $od['img_url'],
  1323. 'docket' => $od['docket'],
  1324. 'status'=>$od['status'],
  1325. 'remark'=>$od['remark'],
  1326. 'remarks_inst'=>$od['remarks_inst'],
  1327. 'customer_code'=>$od['customer_code'],
  1328. 'package_bandwidth'=>$od['package_bandwidth'],
  1329. 'subnet_mask'=>$od['subnet_mask'],
  1330. 'login_id'=>$od['login_id'],
  1331. 'password'=>$od['password'],
  1332. 'wan_ip'=>$od['wan_ip'],
  1333. 'lan_ip'=>$od['lan_ip'],
  1334. 'gateway'=>$od['getaway'],
  1335. 'service_id1'=>$od['service_id2'],
  1336. 'ip_address_onu'=>$od['ip_address_onu'],
  1337. 'gateway_onu'=>$od['gateaway_onu'],
  1338. 'subnet_mask_onu'=>$od['subnet_mask_onu'],
  1339. 'ip_address_srp'=>$od['ip_address_srp'],
  1340. 'dectphone1'=>$od['dectphone1'],
  1341. 'dectphone2'=>$od['dectphone2'],
  1342. 'onu'=>$od['onu'],
  1343. 'router'=>$od['router'],
  1344. 'wifi_extender'=>$od['wifi_extender'],
  1345. 'reg_time'=>$od['reg_time'],
  1346. ];
  1347. DealerClaim::create($subsData);
  1348. }
  1349. }
  1350. //Add total form by month to Claim Laravel collection
  1351. public function claimaddtotal()
  1352. {
  1353. $getdata = DealerClaim::all();
  1354. $total = 0;
  1355. foreach($getdata as $a)
  1356. {
  1357. if($a->activated_my == "2/2019")
  1358. {
  1359. $total += 1;
  1360. }
  1361. }
  1362. $adddata = DealerClaim::all();
  1363. foreach($adddata as $a)
  1364. {
  1365. if($a->activated_my == "2/2019")
  1366. {
  1367. $a->total_form_by_month = $total;
  1368. $a->save();
  1369. }
  1370. }
  1371. }
  1372. public function generateWorkOrderPDF($wo){
  1373. $id = Auth::guard('ms')->id();
  1374. $user = Staff::with('StaffDetail')->find($id);
  1375. $wo = WorkOrder::where('wo',$wo)->first();
  1376. $form = Form::with('PackageDetail','Subscriber')->where('_id',$wo->_id)->first();
  1377. $product = Product::where('formT',$form->type_application)->where('speed',$form->PackageDetail->name)->first();
  1378. if(empty($product)){
  1379. $product = 'RMbps';
  1380. }else {
  1381. $product = $product->package_name;
  1382. }
  1383. $created_by = Staff::with('StaffDetail')->find($wo->created_by)->first();
  1384. if(empty($created_by)){
  1385. $created_by = '';
  1386. }
  1387. $created_at = Carbon::parse($wo->created_at)->toDateTimeString();
  1388. $address = '';
  1389. if($form->type_application == 'R'){
  1390. if($form->Subscriber->street != ''){
  1391. $address = $form->Subscriber->unit_no. ' , '.$form->Subscriber->building_name. ' , '.$form->Subscriber->street. ' , '.$form->Subscriber->postcode. ' , '.$form->Subscriber->city. ' , '.$form->Subscriber->state;
  1392. }else {
  1393. $address = $form->Subscriber->unit_no. ' , '.$form->Subscriber->building_name. ' , '.$form->Subscriber->postcode. ' , '.$form->Subscriber->city. ' , '.$form->Subscriber->state;
  1394. }
  1395. }else if($form->type_application == 'B'){
  1396. if($form->Subscriber->unit_no != ''){
  1397. $address = $form->Subscriber->unit_no. ', '.$form->Subscriber->company_name. ', '.$form->Subscriber->street. ' , '.$form->Subscriber->postcode. ' , '.$form->Subscriber->city. ' , '.$form->Subscriber->state;
  1398. }else {
  1399. $address = $form->Subscriber->company_name. ', '.$form->Subscriber->street. ' , '.$form->Subscriber->postcode. ' , '.$form->Subscriber->city. ' , '.$form->Subscriber->state;
  1400. }
  1401. }
  1402. $pdf = PDF::loadView('pdf.workorder-pdf',compact('wo','form','product','created_at','created_by','address'));
  1403. $pdf->setPaper('A4', 'potrait');
  1404. return $pdf->stream();
  1405. }
  1406. public function generateDocketPDF($do)
  1407. {
  1408. $id = Auth::guard('ms')->id();
  1409. $user = Staff::with('StaffDetail')->find($id);
  1410. $docket = Docket::with('WorkOrder')->where('docket_id',$do)->first();
  1411. if(!empty($docket)){
  1412. $form = Form::with('Subscriber','PackageDetail')->where('_id',$docket->WorkOrder->_id)->first();
  1413. $address = '';
  1414. if($form->type_application == 'R'){
  1415. if($form->Subscriber->street != ''){
  1416. $address = $form->Subscriber->unit_no. ' , '.$form->Subscriber->building_name. ' , '.$form->Subscriber->street. ' , '.$form->Subscriber->postcode. ' , '.$form->Subscriber->city. ' , '.$form->Subscriber->state;
  1417. }else {
  1418. $address = $form->Subscriber->unit_no. ' , '.$form->Subscriber->building_name. ' , '.$form->Subscriber->postcode. ' , '.$form->Subscriber->city. ' , '.$form->Subscriber->state;
  1419. }
  1420. }else if($form->type_application == 'B'){
  1421. if($form->Subscriber->unit_no != ''){
  1422. $address = $form->Subscriber->unit_no. ', '.$form->Subscriber->company_name. ', '.$form->Subscriber->street. ' , '.$form->Subscriber->postcode. ' , '.$form->Subscriber->city. ' , '.$form->Subscriber->state;
  1423. }else {
  1424. $address = $form->Subscriber->company_name. ', '.$form->Subscriber->street. ' , '.$form->Subscriber->postcode. ' , '.$form->Subscriber->city. ' , '.$form->Subscriber->state;
  1425. }
  1426. }
  1427. $product = Product::where('formT',$form->type_application)->where('speed',$form->PackageDetail->name)->first();
  1428. $installer = Staff::with('StaffDetail')->where('_id',$docket->installer_id)->withTrashed()->first();
  1429. if(empty($product)){
  1430. $product = 'RMbps';
  1431. }else {
  1432. $product = $product->package_name;
  1433. }
  1434. $dateTime = Carbon::parse($docket->WorkOrder->dateTimeStart)->toDateTimeString();
  1435. $edateTime = Carbon::parse($docket->end_job)->toDateTimeString();
  1436. $pdf = PDF::loadView('pdf.docket-pdf',compact('docket','form','address','product','installer','dateTime','edateTime'));
  1437. $pdf->setPaper('A4', 'potrait');
  1438. return $pdf->stream();
  1439. }
  1440. }
  1441. public function mClaimAll($agent_id)
  1442. {
  1443. $id = Auth::guard('ms')->id();
  1444. $user = Staff::with('StaffDetail')->find($id);
  1445. $getid = $agent_id;
  1446. $temp = StaffDetail::where('_id', $agent_id)->first();
  1447. $company = $temp->company_id;
  1448. return view('market.claim_list',compact('user','getid','company'));
  1449. }
  1450. //Show claim list by company
  1451. public function mClaimListAll($company)
  1452. {
  1453. $id = Auth::guard('ms')->id();
  1454. $user = Staff::with('StaffDetail')->find($id);
  1455. $dealer_claimAll = DealerClaim::where('dealer',$company)->orderBy('activated_dt','desc')->get();
  1456. $i = 0;
  1457. $nested_data = array();
  1458. foreach ($dealer_claimAll as $c){
  1459. $customer = Subscriber::with('Form')->where('_id', $c->customer_id)->first();
  1460. $address = ''; $name = '';
  1461. if($customer->Form->type_application == 'R'){
  1462. if($customer->street != ''){
  1463. $address = $customer->unit_no. ' , '.$customer->building_name. ' , '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1464. }else {
  1465. $address = $customer->unit_no. ' , '.$customer->building_name. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1466. }
  1467. $name = $customer->name;
  1468. }else if($customer->Form->type_application == 'B'){
  1469. if($customer->unit_no != ''){
  1470. $address = $customer->unit_no. ', '.$customer->company_name. ', '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1471. }else {
  1472. $address = $customer->company_name. ', '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1473. }
  1474. $name = $customer->company_name;
  1475. }
  1476. $i++;
  1477. array_push($nested_data, array(
  1478. 'index' => $c->formT,
  1479. 'contract' => $c->contract,
  1480. 'wo' => $c->wo,
  1481. 'activated_dt' => $c->activated_dt,
  1482. 'name' => $name,
  1483. 'address' => $address,
  1484. 'package' => $c->package,
  1485. 'retail_price' => 'RM '.$c->retail_price,
  1486. 'incentives1' => '',
  1487. 'incentives2' => '',
  1488. 'total' => '',
  1489. 'claimed' => $c->laim_dt,
  1490. 'action' => 'null',
  1491. ));
  1492. }
  1493. return \DataTables::of($nested_data)->make(true);
  1494. }
  1495. // Get Dealer Claim Residential
  1496. public function returnDataR($company,$year,$month){
  1497. $dataCR = DealerClaim::where('formT','R')->where('dealer',$company)->orderBy('activated_dt', 'desc')->get();
  1498. $final = array();
  1499. // Month & Year
  1500. $mY = '';
  1501. if($year != 'null' && $month != 'null'){
  1502. $mY = $month.'/'.$year;
  1503. }
  1504. if($mY != ''){
  1505. $data12 = array(); $data24 = array();
  1506. $countD12 = 0; $countD24 = 0;
  1507. foreach($dataCR as $dc){
  1508. if($dc->activated_my == $mY || $dc->activated_my == '0'.$mY ){
  1509. if($dc->contract == "12"){
  1510. $data12[] = $dc;
  1511. }else if($dc->contract == "24"){
  1512. $data24[] = $dc;
  1513. }
  1514. }
  1515. }
  1516. $countD24 = count($data24);
  1517. $countD12 = count($data12);
  1518. $comm = Commission::where('formT','R')->get();
  1519. $commV12 = 0; $commV24 = 0;
  1520. $comm12 = ''; $comm24 = '';
  1521. foreach($comm as $c){
  1522. if($c->contract =='12'){
  1523. if(strpos($c->ranges, '>') !== false) {
  1524. $pieces = explode(">", $c->ranges);
  1525. if($countD12 >= intval($pieces[1])){
  1526. $commV12 = floatval($c->comm)/100;
  1527. $comm12 = $c->comm.'%';
  1528. }
  1529. }
  1530. if(strpos($c->ranges,'-') !== false){
  1531. $pieces = explode("-", $c->ranges);
  1532. if($countD12 >= intval($pieces[0]) && $countD12 <= intval($pieces[1])){
  1533. $commV12 = floatval($c->comm)/100;
  1534. $comm12 = $c->comm.'%';
  1535. }
  1536. }
  1537. }else if($c->contract =='24'){
  1538. // Contract => 24
  1539. if($countD24 != 0){
  1540. if(strpos($c->ranges, '>') !== false) {
  1541. $pieces = explode(">", $c->ranges);
  1542. if($countD24 > intval($pieces[1])){
  1543. $commV24 = floatval($c->comm)/100;
  1544. $comm24 = $c->comm.'%';
  1545. }
  1546. }
  1547. if(strpos($c->ranges,'-') !== false){
  1548. $pieces = explode("-", $c->ranges);
  1549. if($countD24 >= intval($pieces[0]) && $countD24 <= intval($pieces[1])){
  1550. $commV24 = floatval($c->comm)/100;
  1551. $comm24 = $c->comm.'%';
  1552. }
  1553. }
  1554. }
  1555. }
  1556. }
  1557. foreach ($data12 as $key => $d) {
  1558. $customer = Subscriber::with('Form')->where('_id', $d['customer_id'])->first();
  1559. $address = ''; $name = '';
  1560. if($customer->Form->type_application == 'R'){
  1561. if($customer->street != ''){
  1562. $address = $customer->unit_no. ' , '.$customer->building_name. ' , '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1563. }else {
  1564. $address = $customer->unit_no. ' , '.$customer->building_name. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1565. }
  1566. $name = $customer->name;
  1567. }else if($customer->Form->type_application == 'B'){
  1568. if($customer->unit_no != ''){
  1569. $address = $customer->unit_no. ', '.$customer->company_name. ', '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1570. }else {
  1571. $address = $customer->company_name. ', '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1572. }
  1573. $name = $customer->company_name;
  1574. }
  1575. $final[] = array(
  1576. 'index' => $d['formT'],
  1577. 'contract' => $d['contract'],
  1578. 'wo' => $d['wo'],
  1579. 'activation' => $d['activated_dt'],
  1580. 'name' => $name,
  1581. 'address' => $address,
  1582. 'package' => $d['package'],
  1583. 'retail_price' => 'RM '.$d['retail_price'],
  1584. 'incentives1' => $comm12,
  1585. 'incentives2' => '',
  1586. 'total' => 'RM '.number_format($d['retail_price']*$commV12,2),
  1587. 'claim' => $d['claim_dt'],
  1588. );
  1589. }
  1590. foreach ($data24 as $key => $d) {
  1591. $customer = Subscriber::with('Form')->where('_id', $d['customer_id'])->first();
  1592. $address = ''; $name = '';
  1593. if($customer->Form->type_application == 'R'){
  1594. if($customer->street != ''){
  1595. $address = $customer->unit_no. ' , '.$customer->building_name. ' , '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1596. }else {
  1597. $address = $customer->unit_no. ' , '.$customer->building_name. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1598. }
  1599. $name = $customer->name;
  1600. }else if($customer->Form->type_application == 'B'){
  1601. if($customer->unit_no != ''){
  1602. $address = $customer->unit_no. ', '.$customer->company_name. ', '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1603. }else {
  1604. $address = $customer->company_name. ', '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1605. }
  1606. $name = $customer->company_name;
  1607. }
  1608. $final[] = array(
  1609. 'index' => $d['formT'],
  1610. 'contract' => $d['contract'],
  1611. 'wo' => $d['wo'],
  1612. 'activation' => $d['activated_dt'],
  1613. 'name' => $name,
  1614. 'address' => $address,
  1615. 'package' => $d['package'],
  1616. 'retail_price' => 'RM '.$d['retail_price'],
  1617. 'incentives1' => $comm24,
  1618. 'incentives2' => '',
  1619. 'total' => 'RM '.number_format($d['retail_price']*$commV24,2),
  1620. 'claim' => $d['claim_dt'],
  1621. );
  1622. }
  1623. }else {
  1624. foreach ($dataCR as $c){
  1625. $customer = Subscriber::with('Form')->where('_id', $c['customer_id'])->first();
  1626. $address = ''; $name = '';
  1627. if($customer->Form->type_application == 'R'){
  1628. if($customer->street != ''){
  1629. $address = $customer->unit_no. ' , '.$customer->building_name. ' , '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1630. }else {
  1631. $address = $customer->unit_no. ' , '.$customer->building_name. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1632. }
  1633. $name = $customer->name;
  1634. }else if($customer->Form->type_application == 'B'){
  1635. if($customer->unit_no != ''){
  1636. $address = $customer->unit_no. ', '.$customer->company_name. ', '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1637. }else {
  1638. $address = $customer->company_name. ', '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1639. }
  1640. $name = $customer->company_name;
  1641. }
  1642. $final[] = array(
  1643. 'index' => $c['formT'],
  1644. 'contract' => $c['contract'],
  1645. 'wo' => $c['wo'],
  1646. 'activation' => $c['activated_dt'],
  1647. 'name' => $name,
  1648. 'address' => $address,
  1649. 'package' => $c['package'],
  1650. 'retail_price' => 'RM '.$c['retail_price'],
  1651. 'incentives1' => '',
  1652. 'incentives2' => '',
  1653. 'total' => '',
  1654. 'claim' => $c['claim_dt'],
  1655. );
  1656. }
  1657. }
  1658. return $final;
  1659. }
  1660. // Get Claim Business
  1661. public function returnDataB($company,$year,$month){
  1662. $dataCB = DealerClaim::where('formT','B')->where('dealer',$company)->orderBy('activated_dt', 'desc')->get();
  1663. $final = array();
  1664. // Month & Year
  1665. $mY = '';
  1666. if($year != 'null' && $month != 'null'){
  1667. $mY = $month.'/'.$year;
  1668. }
  1669. if($mY != ''){
  1670. /** Contract 12
  1671. // 1=>10-100 2=>300 3=>500 4=>1
  1672. **/
  1673. $data12_1 = array(); $data12_2 = array();
  1674. $data12_3 = array(); $data12_4 = array();
  1675. $countD12_1 = 0; $countD12_2 = 0;
  1676. $countD12_3 = 0; $countD12_4 = 0;
  1677. /** Contract 24
  1678. // 1=>10-100 2=>300 3=>500 4=>1
  1679. **/
  1680. $data24_1 = array(); $data24_2 = array();
  1681. $data24_3 = array(); $data24_4 = array();
  1682. $countD24_1 = 0; $countD24_2 = 0;
  1683. $countD24_3 = 0; $countD24_4 = 0;
  1684. foreach($dataCB as $dc){
  1685. if($dc->activated_my == $mY || $dc->activated_my == '0'.$mY){
  1686. if($dc->contract == "12" && ($dc->package == "10" || $dc->package == "30" || $dc->package == "50" || $dc->package == "100")){
  1687. $data12_1[] = $dc;
  1688. }else if($dc->contract == "12" && $dc->package == "300"){
  1689. $data12_2[] = $dc;
  1690. }else if($dc->contract == "12" && $dc->package == "500"){
  1691. $data12_3[] = $dc;
  1692. }else if($dc->contract == "12" && $dc->package == "1"){
  1693. $data12_4[] = $dc;
  1694. }else if($dc->contract == "24" && ($dc->package == "10" || $dc->package == "30" || $dc->package == "50" || $dc->package == "100")){
  1695. $data24_1[] = $dc;
  1696. }else if($dc->contract == "24" && $dc->package == "300"){
  1697. $data24_2[] = $dc;
  1698. }else if($dc->contract == "24" && $dc->package == "500"){
  1699. $data24_3[] = $dc;
  1700. }else if($dc->contract == "24" && $dc->package == "1"){
  1701. $data24_4[] = $dc;
  1702. }
  1703. }
  1704. }
  1705. $countD12_1 = count($data12_1); $countD12_2 = count($data12_2);
  1706. $countD12_3 = count($data12_3); $countD12_4 = count($data12_4);
  1707. $countD24_1 = count($data24_1); $countD24_2 = count($data24_2);
  1708. $countD24_3 = count($data24_3); $countD24_4 = count($data24_4);
  1709. $comm = Commission::where('formT','B')->get();
  1710. $commV12_1 = 0; $commV12_2 = 0;
  1711. $commV12_3 = 0; $commV12_4 = 0;
  1712. $commV24_1 = 0; $commV24_2 = 0;
  1713. $commV24_3 = 0; $commV24_4 = 0;
  1714. $comm12_1 = ''; $comm12_2 = '';
  1715. $comm12_3 = ''; $comm12_4 = '';
  1716. $comm24_1 = ''; $comm24_2 = '';
  1717. $comm24_3 = ''; $comm24_4 = '';
  1718. $addRM12_1 = ''; $addRM12_2 = '';
  1719. $addRM12_3 = ''; $addRM12_4 = '';
  1720. $addRM24_1 = ''; $addRM24_2 = '';
  1721. $addRM24_3 = ''; $addRM24_4 = '';
  1722. foreach($comm as $c){
  1723. /*** Contract 24 **/
  1724. if($c->contract =='24' && $c->packageR == "300"){
  1725. if(strpos($c->ranges, '>') !== false) {
  1726. $pieces = explode(">", $c->ranges);
  1727. if($countD24_2 >= intval($pieces[1])){
  1728. if(strpos($c->comm, 'RM') !== false) {
  1729. $comm24_2 = '+ ' .$c->comm;
  1730. $commV24_2 = '';
  1731. $comms = explode("RM", $c->comm);
  1732. $addRM24_2 = floatval($comms[1]);
  1733. }else {
  1734. $comms = explode("RM", $c->comm);
  1735. if(isset($comms[1])){
  1736. $comm24_2 = 'RM '.$comms[1];
  1737. $commV24_2 = floatval($comms[1]);
  1738. }else {
  1739. if($comms[0] != ''){
  1740. $comm24_2 = $comms[0].'%';
  1741. $commV24_2 = floatval($comms[0])/100;
  1742. }
  1743. }
  1744. }
  1745. }
  1746. }
  1747. }else if($c->contract =='24' && $c->packageR == "500"){
  1748. if(strpos($c->ranges, '>') !== false) {
  1749. $pieces = explode(">", $c->ranges);
  1750. if($countD24_3 >= intval($pieces[1])){
  1751. if(strpos($c->comm, 'RM') !== false) {
  1752. $comm24_3 = '+ ' .$c->comm;
  1753. $commV24_3 = '';
  1754. $comms = explode("RM", $c->comm);
  1755. $addRM24_3 = floatval($comms[1]);
  1756. }else {
  1757. $comms = explode("RM", $c->comm);
  1758. if(isset($comms[1])){
  1759. $comm24_3 = 'RM '.$comms[1];
  1760. $commV24_3 = floatval($comms[1]);
  1761. }else {
  1762. if($comms[0] != ''){
  1763. $comm24_3 = $comms[0].'%';
  1764. $commV24_3 = floatval($comms[0])/100;
  1765. }
  1766. }
  1767. }
  1768. }
  1769. }
  1770. }else if($c->contract =='24' && $c->packageR == "1"){
  1771. if(strpos($c->ranges, '>') !== false) {
  1772. $pieces = explode(">", $c->ranges);
  1773. if($countD24_4 >= intval($pieces[1])){
  1774. if(strpos($c->comm, 'RM') !== false) {
  1775. $comm24_4 = '+ ' .$c->comm;
  1776. $commV24_4 = '';
  1777. $comms = explode("RM", $c->comm);
  1778. $addRM24_4 = floatval($comms[1]);
  1779. }else {
  1780. $comms = explode("RM", $c->comm);
  1781. if(isset($comms[1])){
  1782. $comm24_4 = 'RM '.$comms[1];
  1783. $commV24_4 = floatval($comms[1]);
  1784. }else {
  1785. if($comms[0] != ''){
  1786. $comm24_4 = $comms[0].'%';
  1787. $commV24_4 = floatval($comms[0])/100;
  1788. }
  1789. }
  1790. }
  1791. }
  1792. }
  1793. }else if($c->contract =='24' && $c->packageR == "10,30,50,100"){
  1794. if(strpos($c->ranges,'-') !== false){
  1795. $pieces = explode("-", $c->ranges);
  1796. if($countD24_1 >= intval($pieces[0]) && $countD24_1 <= intval($pieces[1])){
  1797. if(strpos($c->comm, 'RM') !== false) {
  1798. $comm24_1 = '+ ' .$c->comm;
  1799. $commV24_1 = '';
  1800. $addRM24_1 = $c->comm;
  1801. }else {
  1802. $comms = explode("RM", $c->comm);
  1803. if(!empty($comms[1])){
  1804. $comm24_1 = 'RM '.$comms[1];
  1805. $commV24_1 = floatval($comms[1]);
  1806. }else {
  1807. if($comms[0] != ''){
  1808. $comm24_1 = $comms[0].'%';
  1809. $commV24_1 = floatval($comms[0])/100;
  1810. }
  1811. }
  1812. }
  1813. }
  1814. }
  1815. if(strpos($c->ranges, '>') !== false) {
  1816. $pieces1 = explode(">", $c->ranges);
  1817. if($countD24_1 >= intval($pieces1[1])){
  1818. if(strpos($c->comm, "RM") === true) {
  1819. $comm24_1 = '+ ' .$c->comm;
  1820. $commV24_1 = '';
  1821. $addRM24_1 = $c->comm;
  1822. }else {
  1823. $comms = explode("RM", $c->comm);
  1824. if(empty($comms[1])){
  1825. $comm24_1 = 'RM '.$comms[1];
  1826. $commV24_1 = floatval($comms[1]);
  1827. }else {
  1828. if($comms[0] != ''){
  1829. $comm24_1 = str_replace ('+ ','',$comms[0]).'%';
  1830. $commV24_1 = floatval($comms[0])/100;
  1831. $addRM24_1 = $comms[1];
  1832. }
  1833. }
  1834. }
  1835. }
  1836. }
  1837. }
  1838. /*** Contract 12 **/
  1839. else if($c->contract =='12' && $c->packageR == "10,30,50,100"){
  1840. if(strpos($c->ranges, '>') !== false) {
  1841. $pieces = explode(">", $c->ranges);
  1842. if($countD12_1 >= intval($pieces[1])){
  1843. if(strpos($c->comm, 'RM') !== false) {
  1844. $comm12_1 = '+ ' .$c->comm;
  1845. $commV12_1 = '';
  1846. $addRM12_1 = $c->comm;
  1847. }else {
  1848. $comms = explode("RM", $c->comm);
  1849. if(isset($comms[1])){
  1850. $comm12_1 = 'RM '.$comms[1];
  1851. $commV12_1 = floatval($comms[1]);
  1852. }else {
  1853. if($comms[0] != ''){
  1854. $comm12_1 = $comms[0].'%';
  1855. $commV12_1 = floatval($comms[0])/100;
  1856. }
  1857. }
  1858. }
  1859. }
  1860. }
  1861. if(strpos($c->ranges,'-') !== false){
  1862. $pieces = explode("-", $c->ranges);
  1863. if($countD12_1 >= intval($pieces[0]) && $countD12_1 <= intval($pieces[1])){
  1864. if(strpos($c->comm, 'RM') !== false) {
  1865. $comm12_1 = '+ ' .$c->comm;
  1866. $commV12_1 = '';
  1867. $addRM12_1 = $c->comm;
  1868. }else {
  1869. $comms = explode("RM", $c->comm);
  1870. if(isset($comms[1])){
  1871. $comm12_1 = 'RM '.$comms[1];
  1872. $commV12_1 = floatval($comms[1]);
  1873. }else {
  1874. if($comms[0] != ''){
  1875. $comm12_1 = $comms[0].'%';
  1876. $commV12_1 = floatval($comms[0])/100;
  1877. }
  1878. }
  1879. }
  1880. }
  1881. }
  1882. }else if($c->contract =='12' && $c->packageR == "300"){
  1883. if(strpos($c->ranges, '>') !== false) {
  1884. $pieces = explode(">", $c->ranges);
  1885. if($countD12_2 >= intval($pieces[1])){
  1886. if(strpos($c->comm, 'RM') !== false) {
  1887. $comm12_2 = '+ ' .$c->comm;
  1888. $commV12_2 = '';
  1889. $addRM12_2 = $c->comm;
  1890. }else {
  1891. $comms = explode("RM", $c->comm);
  1892. if(isset($comms[1])){
  1893. $comm12_2 = 'RM '.$comms[1];
  1894. $commV12_2 = floatval($comms[1]);
  1895. }else {
  1896. if($comms[0] != ''){
  1897. $comm12_2 = $comms[0].'%';
  1898. $commV12_2 = floatval($comms[0])/100;
  1899. }
  1900. }
  1901. }
  1902. }
  1903. }
  1904. }else if($c->contract =='12' && $c->packageR == "500"){
  1905. if(strpos($c->ranges, '>') !== false) {
  1906. $pieces = explode(">", $c->ranges);
  1907. if($countD12_3 >= intval($pieces[1])){
  1908. if(strpos($c->comm, 'RM') !== false) {
  1909. $comm12_3 = '+ ' .$c->comm;
  1910. $commV12_3 = '';
  1911. $addRM12_3 = $c->comm;
  1912. }else {
  1913. $comms = explode("RM", $c->comm);
  1914. if(isset($comms[1])){
  1915. $comm12_3 = 'RM '.$comms[1];
  1916. $commV12_3 = floatval($comms[1]);
  1917. }else {
  1918. if($comms[0] != ''){
  1919. $comm12_3 = $comms[0].'%';
  1920. $commV12_3 = floatval($comms[0])/100;
  1921. }
  1922. }
  1923. }
  1924. }
  1925. }
  1926. }else if($c->contract =='12' && $c->packageR == "1"){
  1927. if(strpos($c->ranges, '>') !== false) {
  1928. $pieces = explode(">", $c->ranges);
  1929. if($countD12_4 >= intval($pieces[1])){
  1930. if(strpos($c->comm, 'RM') !== false) {
  1931. $comm12_4 = '+ ' .$c->comm;
  1932. $commV12_4 = '';
  1933. $addRM12_4 = $c->comm;
  1934. }else {
  1935. $comms = explode("RM", $c->comm);
  1936. if(isset($comms[1])){
  1937. $comm12_4 = 'RM '.$comms[1];
  1938. $commV12_4 = floatval($comms[1]);
  1939. }else {
  1940. if($comms[0] != ''){
  1941. $comm12_4 = $comms[0].'%';
  1942. $commV12_4 = floatval($comms[0])/100;
  1943. }
  1944. }
  1945. }
  1946. }
  1947. }
  1948. }
  1949. }
  1950. // Category 10,30,50,100
  1951. foreach ($data12_1 as $key => $d) {
  1952. $customer = Subscriber::with('Form')->where('_id', $d['customer_id'])->first();
  1953. $address = ''; $$name = '';
  1954. if($customer->Form->type_application == 'R'){
  1955. if($customer->street != ''){
  1956. $address = $customer->unit_no. ' , '.$customer->building_name. ' , '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1957. }else {
  1958. $address = $customer->unit_no. ' , '.$customer->building_name. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1959. }
  1960. $name = $customer->name;
  1961. }else if($customer->Form->type_application == 'B'){
  1962. if($customer->unit_no != ''){
  1963. $address = $customer->unit_no. ', '.$customer->company_name. ', '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1964. }else {
  1965. $address = $customer->company_name. ', '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1966. }
  1967. $name = $customer->company_name;
  1968. }
  1969. $total = '';
  1970. if($commV12_1 == ''){
  1971. $total = $addRM12_1;
  1972. }else {
  1973. $total = number_format($d['retail_price']*$commV12_1,2);
  1974. }
  1975. $final[] = array(
  1976. 'index' => $d['formT'],
  1977. 'contract' => $d['contract'],
  1978. 'wo' => $d['wo'],
  1979. 'activation' => $d['activated_dt'],
  1980. 'name' => $name,
  1981. 'address' => $address,
  1982. 'package' => $d['package'],
  1983. 'retail_price' => 'RM '.$d['retail_price'],
  1984. 'incentives1' => $comm12_1,
  1985. 'incentives2' => $addRM12_1,
  1986. 'total' => 'RM '.$total,
  1987. 'claim' => $c['claim_dt'],
  1988. );
  1989. }
  1990. foreach ($data24_1 as $key => $d) {
  1991. $customer = Subscriber::with('Form')->where('_id', $d['customer_id'])->first();
  1992. $address = ''; $name = '';
  1993. if($customer->Form->type_application == 'R'){
  1994. if($customer->street != ''){
  1995. $address = $customer->unit_no. ' , '.$customer->building_name. ' , '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1996. }else {
  1997. $address = $customer->unit_no. ' , '.$customer->building_name. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  1998. }
  1999. $name = $customer->name;
  2000. }else if($customer->Form->type_application == 'B'){
  2001. if($customer->unit_no != ''){
  2002. $address = $customer->unit_no. ', '.$customer->company_name. ', '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  2003. }else {
  2004. $address = $customer->company_name. ', '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  2005. }
  2006. $name = $customer->company_name;
  2007. }
  2008. $total = '';
  2009. if($commV24_1 == ''){
  2010. $total = $addRM24_1;
  2011. }else {
  2012. $total = number_format(($d['retail_price']*$commV24_1) + floatval($addRM24_1),2);
  2013. }
  2014. $final[] = array(
  2015. 'index' => $d['formT'],
  2016. 'contract' => $d['contract'],
  2017. 'wo' => $d['wo'],
  2018. 'activation' => $d['activated_dt'],
  2019. 'name' => $name,
  2020. 'address' => $address,
  2021. 'package' => $d['package'],
  2022. 'retail_price' => 'RM '.$d['retail_price'],
  2023. 'incentives1' => $comm24_1,
  2024. 'incentives2' => $addRM24_1,
  2025. 'total' => 'RM '.$total,
  2026. 'claim' => $c['claim_dt'],
  2027. );
  2028. }
  2029. // Category 300
  2030. foreach ($data12_2 as $key => $d) {
  2031. $customer = Subscriber::with('Form')->where('_id', $d['customer_id'])->first();
  2032. $address = ''; $name = '';
  2033. if($customer->Form->type_application == 'R'){
  2034. if($customer->street != ''){
  2035. $address = $customer->unit_no. ' , '.$customer->building_name. ' , '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  2036. }else {
  2037. $address = $customer->unit_no. ' , '.$customer->building_name. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  2038. }
  2039. $name = $customer->name;
  2040. }else if($customer->Form->type_application == 'B'){
  2041. if($customer->unit_no != ''){
  2042. $address = $customer->unit_no. ', '.$customer->company_name. ', '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  2043. }else {
  2044. $address = $customer->company_name. ', '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  2045. }
  2046. $name = $customer->company_name;
  2047. }
  2048. $total = '';
  2049. if($commV12_2 == ''){
  2050. $total = $addRM12_2;
  2051. }else {
  2052. $total = number_format($d['retail_price']*$commV12_2,2);
  2053. }
  2054. $final[] = array(
  2055. 'index' => $d['formT'],
  2056. 'contract' => $d['contract'],
  2057. 'wo' => $d['wo'],
  2058. 'activation' => $d['activated_dt'],
  2059. 'name' => $name,
  2060. 'address' => $address,
  2061. 'package' => $d['package'],
  2062. 'retail_price' => 'RM '.$d['retail_price'],
  2063. 'incentives1' => $commV12_2,
  2064. 'incentives2' => $addRM12_2,
  2065. 'total' => 'RM '.$total,
  2066. 'claim' => $c['claim_dt'],
  2067. );
  2068. }
  2069. foreach ($data24_2 as $key => $d) {
  2070. $customer = Subscriber::with('Form')->where('_id', $d['customer_id'])->first();
  2071. $address = ''; $name = '';
  2072. if($customer->Form->type_application == 'R'){
  2073. if($customer->street != ''){
  2074. $address = $customer->unit_no. ' , '.$customer->building_name. ' , '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  2075. }else {
  2076. $address = $customer->unit_no. ' , '.$customer->building_name. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  2077. }
  2078. $name = $customer->name;
  2079. }else if($customer->Form->type_application == 'B'){
  2080. if($customer->unit_no != ''){
  2081. $address = $customer->unit_no. ', '.$customer->company_name. ', '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  2082. }else {
  2083. $address = $customer->company_name. ', '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  2084. }
  2085. $name = $customer->company_name;
  2086. }
  2087. $total = '';
  2088. if($commV24_2 == ''){
  2089. $total = $addRM24_2;//number_format($d['retail_price']+$addRM24_2,2);
  2090. }else {
  2091. $total = number_format($d['retail_price']*$commV24_2,2);
  2092. }
  2093. $final[] = array(
  2094. 'index' => $d['formT'],
  2095. 'contract' => $d['contract'],
  2096. 'wo' => $d['wo'],
  2097. 'activation' => $d['activated_dt'],
  2098. 'name' => $name,
  2099. 'address' => $address,
  2100. 'package' => $d['package'],
  2101. 'retail_price' => 'RM '.$d['retail_price'],
  2102. 'incentives1' => $commV24_2,
  2103. 'incentives2' => $comm24_2,
  2104. 'total' => 'RM '.$total,
  2105. 'claim' => $c['claim_dt'],
  2106. );
  2107. }
  2108. // Category 500
  2109. foreach ($data12_3 as $key => $d) {
  2110. $customer = Subscriber::with('Form')->where('_id', $d['customer_id'])->first();
  2111. $address = ''; $name = '';
  2112. if($customer->Form->type_application == 'R'){
  2113. if($customer->street != ''){
  2114. $address = $customer->unit_no. ' , '.$customer->building_name. ' , '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  2115. }else {
  2116. $address = $customer->unit_no. ' , '.$customer->building_name. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  2117. }
  2118. $name = $customer->name;
  2119. }else if($customer->Form->type_application == 'B'){
  2120. if($customer->unit_no != ''){
  2121. $address = $customer->unit_no. ', '.$customer->company_name. ', '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  2122. }else {
  2123. $address = $customer->company_name. ', '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  2124. }
  2125. $name = $customer->company_name;
  2126. }
  2127. $total = '';
  2128. if($commV12_3 == ''){
  2129. $total = $addRM12_3;
  2130. }else {
  2131. $total = number_format($d['retail_price']*$commV12_3,2);
  2132. }
  2133. $final[] = array(
  2134. 'index' => $d['formT'],
  2135. 'contract' => $d['contract'],
  2136. 'wo' => $d['wo'],
  2137. 'activation' => $d['activated_dt'],
  2138. 'name' => $name,
  2139. 'address' => $address,
  2140. 'package' => $d['package'],
  2141. 'retail_price' => 'RM '.$d['retail_price'],
  2142. 'incentives1' => $commV12_3,
  2143. 'incentives2' => $addRM12_3,
  2144. 'total' => 'RM '.$total,
  2145. 'claim' => $c['claim_dt'],
  2146. );
  2147. }
  2148. foreach ($data24_3 as $key => $d) {
  2149. $customer = Subscriber::with('Form')->where('_id', $d['customer_id'])->first();
  2150. $address = ''; $name = '';
  2151. if($customer->Form->type_application == 'R'){
  2152. if($customer->street != ''){
  2153. $address = $customer->unit_no. ' , '.$customer->building_name. ' , '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  2154. }else {
  2155. $address = $customer->unit_no. ' , '.$customer->building_name. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  2156. }
  2157. $name = $customer->name;
  2158. }else if($customer->Form->type_application == 'B'){
  2159. if($customer->unit_no != ''){
  2160. $address = $customer->unit_no. ', '.$customer->company_name. ', '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  2161. }else {
  2162. $address = $customer->company_name. ', '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  2163. }
  2164. $name = $customer->company_name;
  2165. }
  2166. $total = '';
  2167. if($commV24_3 == ''){
  2168. $total = $addRM24_3; //number_format($d['retail_price']+$addRM24_3,2);
  2169. }else {
  2170. $total = number_format($d['retail_price']*$commV24_3,2);
  2171. }
  2172. $final[] = array(
  2173. 'index' => $d['formT'],
  2174. 'contract' => $d['contract'],
  2175. 'wo' => $d['wo'],
  2176. 'activation' => $d['activated_dt'],
  2177. 'name' => $name,
  2178. 'address' => $address,
  2179. 'package' => $d['package'],
  2180. 'retail_price' => 'RM '.$d['retail_price'],
  2181. 'incentives1' => $commV24_3,
  2182. 'incentives2' => $comm24_3,
  2183. 'total' => 'RM '.$total,
  2184. 'claim' => $c['claim_dt'],
  2185. );
  2186. }
  2187. // Category 1
  2188. foreach ($data12_4 as $key => $d) {
  2189. $customer = Subscriber::with('Form')->where('_id', $d['customer_id'])->first();
  2190. $address = ''; $name = '';
  2191. if($customer->Form->type_application == 'R'){
  2192. if($customer->street != ''){
  2193. $address = $customer->unit_no. ' , '.$customer->building_name. ' , '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  2194. }else {
  2195. $address = $customer->unit_no. ' , '.$customer->building_name. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  2196. }
  2197. $name = $customer->name;
  2198. }else if($customer->Form->type_application == 'B'){
  2199. if($customer->unit_no != ''){
  2200. $address = $customer->unit_no. ', '.$customer->company_name. ', '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  2201. }else {
  2202. $address = $customer->company_name. ', '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  2203. }
  2204. $name = $customer->company_name;
  2205. }
  2206. $total = '';
  2207. if($commV12_4 == ''){
  2208. $total = $addRM12_4;
  2209. }else {
  2210. $total = number_format($d['retail_price']*$commV12_4,2);
  2211. }
  2212. $final[] = array(
  2213. 'index' => $d['formT'],
  2214. 'contract' => $d['contract'],
  2215. 'wo' => $d['wo'],
  2216. 'activation' => $d['activated_dt'],
  2217. 'name' => $name,
  2218. 'address' => $address,
  2219. 'package' => $d['package'],
  2220. 'retail_price' => 'RM '.$d['retail_price'],
  2221. 'incentives1' => $commV12_4,
  2222. 'incentives2' => $addRM12_4,
  2223. 'total' => 'RM '.$total,
  2224. 'claim' => $c['claim_dt'],
  2225. );
  2226. }
  2227. foreach ($data24_4 as $key => $d) {
  2228. $customer = Subscriber::with('Form')->where('_id', $d['customer_id'])->first();
  2229. $address = ''; $name = '';
  2230. if($customer->Form->type_application == 'R'){
  2231. if($customer->street != ''){
  2232. $address = $customer->unit_no. ' , '.$customer->building_name. ' , '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  2233. }else {
  2234. $address = $customer->unit_no. ' , '.$customer->building_name. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  2235. }
  2236. $name = $customer->name;
  2237. }else if($customer->Form->type_application == 'B'){
  2238. if($customer->unit_no != ''){
  2239. $address = $customer->unit_no. ', '.$customer->company_name. ', '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  2240. }else {
  2241. $address = $customer->company_name. ', '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  2242. }
  2243. $name = $customer->company_name;
  2244. }
  2245. $total = '';
  2246. if($commV24_4 == ''){
  2247. $total = $addRM24_4; //number_format($d['retail_price']+$addRM24_4,2);
  2248. }else {
  2249. $total = number_format($d['retail_price']*$commV24_4,2);
  2250. }
  2251. $final[] = array(
  2252. 'index' => $d['formT'],
  2253. 'contract' => $d['contract'],
  2254. 'wo' => $d['wo'],
  2255. 'activation' => $d['activated_dt'],
  2256. 'name' => $name,
  2257. 'address' => $address,
  2258. 'package' => $d['package'],
  2259. 'retail_price' => 'RM '.$d['retail_price'],
  2260. 'incentives1' => $commV24_4,
  2261. 'incentives2' => $comm24_4,
  2262. 'total' => 'RM '.$total,
  2263. 'claim' => $c['claim_dt'],
  2264. );
  2265. }
  2266. }else {
  2267. foreach ($dataCB as $c){
  2268. $customer = Subscriber::with('Form')->where('_id', $c['customer_id'])->first();
  2269. $address = ''; $name = '';
  2270. if($customer->Form->type_application == 'R'){
  2271. if($customer->street != ''){
  2272. $address = $customer->unit_no. ' , '.$customer->building_name. ' , '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  2273. }else {
  2274. $address = $customer->unit_no. ' , '.$customer->building_name. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  2275. }
  2276. $name = $customer->name;
  2277. }else if($customer->Form->type_application == 'B'){
  2278. if($customer->unit_no != ''){
  2279. $address = $customer->unit_no. ', '.$customer->company_name. ', '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  2280. }else {
  2281. $address = $customer->company_name. ', '.$customer->street. ' , '.$customer->postcode. ' , '.$customer->city. ' , '.$customer->state;
  2282. }
  2283. $name = $customer->company_name;
  2284. }
  2285. $final[] = array(
  2286. 'index' => $c['formT'],
  2287. 'contract' => $c['contract'],
  2288. 'wo' => $c['wo'],
  2289. 'activation' => $c['activated_dt'],
  2290. 'name' => $name,
  2291. 'address' => $address,
  2292. 'package' => $c['package'],
  2293. 'retail_price' => 'RM '.$c['retail_price'],
  2294. 'incentives1' => '',
  2295. 'incentives2' => '',
  2296. 'total' => '',
  2297. 'claim' => $c['claim_dt'],
  2298. );
  2299. }
  2300. }
  2301. return $final;
  2302. }
  2303. public function filterclaim($company,$year,$month,$app)
  2304. {
  2305. $id = Auth::guard('ms')->id();
  2306. $user = Staff::with('StaffDetail')->find($id);
  2307. $dataR = array(); $dataB = array(); $combineData = array();
  2308. if($year!='null' && $month!='null' && $app=='null'){
  2309. if(($year == '2019' || $year == '2020') && ($month == '12' || $month == '1')){
  2310. $dataDis = $this->returnDataR($company,'2019','12');
  2311. $dataJan = $this->returnDataR($company,'2020','1');
  2312. $count_cyberjaya = array(); $count_jasin = array();
  2313. foreach ($dataDis as $key => $d) {
  2314. if(date('d/m/Y', strtotime($d['activation'])) >= '12/12/2019'){
  2315. if (strpos($d['address'], 'Cyberjaya') !== false) {
  2316. $count_cyberjaya[] = $d;
  2317. }
  2318. if (strpos($d['address'], 'Jasin') !== false) {
  2319. $count_jasin[] = $d;
  2320. }
  2321. }else {
  2322. if(date('n/Y', strtotime($d['activation'])) == $month.'/'.$year){
  2323. $combineData[] = $d;
  2324. }
  2325. }
  2326. }
  2327. foreach ($dataJan as $key => $d) {
  2328. if (strpos($d['address'], 'Cyberjaya') !== false) {
  2329. $count_cyberjaya[] = $d;
  2330. }
  2331. if (strpos($d['address'], 'Jasin') !== false) {
  2332. $count_jasin[] = $d;
  2333. }
  2334. }
  2335. // Store to combineData New Incentives Cyberjaya
  2336. foreach ($count_cyberjaya as $key => $d) {
  2337. if(count($count_cyberjaya) > 50){
  2338. if(date('n/Y', strtotime($d['activation'])) == $month.'/'.$year){
  2339. if($d['contract'] == '12'){
  2340. array_push($combineData, array(
  2341. 'index' => $d['index'],
  2342. 'contract' => $d['contract'],
  2343. 'wo' => $d['wo'],
  2344. 'activation' => $d['activation'],
  2345. 'name' => $d['name'],
  2346. 'address' => $d['address'],
  2347. 'package' => $d['package'],
  2348. 'retail_price' => $d['retail_price'],
  2349. 'incentives1' => $d['incentives1'],
  2350. 'incentives2' => $d['incentives2'],
  2351. 'total' => $d['total'],
  2352. 'claim' => $d['claim'],
  2353. ));
  2354. }else {
  2355. $total = number_format(floatval(str_replace("RM ", "" , $d['retail_price'])) * 2.3, 2);
  2356. array_push($combineData, array(
  2357. 'index' => $d['index'],
  2358. 'contract' => $d['contract'],
  2359. 'wo' => $d['wo'],
  2360. 'activation' => $d['activation'],
  2361. 'name' => $d['name'],
  2362. 'address' => $d['address'],
  2363. 'package' => $d['package'],
  2364. 'retail_price' => $d['retail_price'],
  2365. 'incentives1' => '230%',
  2366. 'incentives2' => $d['incentives2'],
  2367. 'total' => 'RM '.$total,
  2368. 'claim' => $d['claim'],
  2369. ));
  2370. }
  2371. }
  2372. }else if(count($count_cyberjaya) < 50){
  2373. if(date('n/Y', strtotime($d['activation'])) == $month.'/'.$year){
  2374. array_push($combineData, array(
  2375. 'index' => $d['index'],
  2376. 'contract' => $d['contract'],
  2377. 'wo' => $d['wo'],
  2378. 'activation' => $d['activation'],
  2379. 'name' => $d['name'],
  2380. 'address' => $d['address'],
  2381. 'package' => $d['package'],
  2382. 'retail_price' => $d['retail_price'],
  2383. 'incentives1' => $d['incentives1'],
  2384. 'incentives2' => $d['incentives2'],
  2385. 'total' => $d['total'],
  2386. 'claim' => $d['claim'],
  2387. ));
  2388. }
  2389. }
  2390. }
  2391. // Store to combineData New Incentives Jasin
  2392. foreach ($count_jasin as $key => $d) {
  2393. if(count($count_jasin) > 10){
  2394. if(date('n/Y', strtotime($d['activation'])) == $month.'/'.$year){
  2395. $total = number_format(floatval(str_replace("RM ", "" , $d['retail_price'])) * 2.3, 2);
  2396. array_push($combineData, array(
  2397. 'index' => $d['index'],
  2398. 'contract' => $d['contract'],
  2399. 'wo' => $d['wo'],
  2400. 'activation' => $d['activation'],
  2401. 'name' => $d['name'],
  2402. 'address' => $d['address'],
  2403. 'package' => $d['package'],
  2404. 'retail_price' => $d['retail_price'],
  2405. 'incentives1' => '230%',
  2406. 'incentives2' => $d['incentives2'],
  2407. 'total' => 'RM '.$total,
  2408. 'claim' => $d['claim'],
  2409. ));
  2410. }
  2411. }else if(count($count_jasin) < 10){
  2412. if(date('n/Y', strtotime($d['activation'])) == $month.'/'.$year){
  2413. array_push($combineData, array(
  2414. 'index' => $d['index'],
  2415. 'contract' => $d['contract'],
  2416. 'wo' => $d['wo'],
  2417. 'activation' => $d['activation'],
  2418. 'name' => $d['name'],
  2419. 'address' => $d['address'],
  2420. 'package' => $d['package'],
  2421. 'retail_price' => $d['retail_price'],
  2422. 'incentives1' => $d['incentives1'],
  2423. 'incentives2' => $d['incentives2'],
  2424. 'total' => $d['total'],
  2425. 'claim' => $d['claim'],
  2426. ));
  2427. }
  2428. }
  2429. }
  2430. }else {
  2431. $dataR = $this->returnDataR($company,$year,$month);
  2432. foreach($dataR as $r){
  2433. $combineData[] = $r;
  2434. }
  2435. }
  2436. $dataB = $this->returnDataB($company,$year,$month);
  2437. foreach($dataB as $b){
  2438. $combineData[] = $b;
  2439. }
  2440. $i = 0;
  2441. $nested_data = array();
  2442. if(!empty($combineData)){
  2443. foreach ($combineData as $c){
  2444. $i++;
  2445. array_push($nested_data, array(
  2446. 'index' => $c['index'],
  2447. 'contract' => $c['contract'],
  2448. 'wo' => $c['wo'],
  2449. 'activated_dt' => $c['activation'],
  2450. 'name' => $c['name'],
  2451. 'address' => $c['address'],
  2452. 'package' => $c['package'],
  2453. 'retail_price' => $c['retail_price'],
  2454. 'incentives1' => $c['incentives1'],
  2455. 'incentives2' => $c['incentives2'],
  2456. 'total' => $c['total'],
  2457. 'claimed' => $c['claim'],
  2458. ));
  2459. }
  2460. }
  2461. usort($nested_data, function ($a, $b) {
  2462. $dateA = $a['activated_dt'];
  2463. $dateB = $b['activated_dt'];
  2464. // ascending ordering, use `<=` for descending
  2465. return $dateA <= $dateB;
  2466. });
  2467. return \DataTables::of($nested_data)->make(true);
  2468. }else if($year=='null' && $month=='null' && $app!='null'){
  2469. if($app == 'Residential'){
  2470. $dataR = $this->returnDataR($company,$year,$month);
  2471. }else if($app == 'Business'){
  2472. $dataB = $this->returnDataB($company,$year,$month);
  2473. }
  2474. foreach($dataR as $r){
  2475. $combineData[] = $r;
  2476. }
  2477. foreach($dataB as $b){
  2478. $combineData[] = $b;
  2479. }
  2480. $i = 0;
  2481. $nested_data = array();
  2482. if(!empty($combineData)){
  2483. foreach ($combineData as $c){
  2484. $i++;
  2485. array_push($nested_data, array(
  2486. 'index' => $c['index'],
  2487. 'contract' => $c['contract'],
  2488. 'wo' => $c['wo'],
  2489. 'activated_dt' => $c['activation'],
  2490. 'name' => $c['name'],
  2491. 'address' => $c['address'],
  2492. 'package' => $c['package'],
  2493. 'retail_price' => $c['retail_price'],
  2494. 'incentives1' => $c['incentives1'],
  2495. 'incentives2' => $c['incentives2'],
  2496. 'total' => $c['total'],
  2497. 'claimed' => $c['claim'],
  2498. ));
  2499. }
  2500. }
  2501. usort($nested_data, function ($a, $b) {
  2502. $dateA = $a['activated_dt'];
  2503. $dateB = $b['activated_dt'];
  2504. // ascending ordering, use `<=` for descending
  2505. return $dateA <= $dateB;
  2506. });
  2507. return \DataTables::of($nested_data)->make(true);
  2508. }else if($year!='null' && $month!='null' && $app!='null'){
  2509. if($app == 'Residential'){
  2510. if(($year == '2019' || $year == '2020') && ($month == '12' || $month == '1')){
  2511. $dataDis = $this->returnDataR($company,'2019','12');
  2512. $dataJan = $this->returnDataR($company,'2020','1');
  2513. $count_cyberjaya = array(); $count_jasin = array();
  2514. foreach ($dataDis as $key => $d) {
  2515. if(date('d/m/Y', strtotime($d['activation'])) >= '12/12/2019'){
  2516. if (strpos($d['address'], 'Cyberjaya') !== false) {
  2517. $count_cyberjaya[] = $d;
  2518. }
  2519. if (strpos($d['address'], 'Jasin') !== false) {
  2520. $count_jasin[] = $d;
  2521. }
  2522. }else {
  2523. if(date('n/Y', strtotime($d['activation'])) == $month.'/'.$year){
  2524. $combineData[] = $d;
  2525. }
  2526. }
  2527. }
  2528. foreach ($dataJan as $key => $d) {
  2529. if (strpos($d['address'], 'Cyberjaya') !== false) {
  2530. $count_cyberjaya[] = $d;
  2531. }
  2532. if (strpos($d['address'], 'Jasin') !== false) {
  2533. $count_jasin[] = $d;
  2534. }
  2535. }
  2536. // Store to combineData New Incentives Cyberjaya
  2537. foreach ($count_cyberjaya as $key => $d) {
  2538. if(count($count_cyberjaya) > 50){
  2539. if(date('n/Y', strtotime($d['activation'])) == $month.'/'.$year){
  2540. if($d['contract'] == '12'){
  2541. array_push($combineData, array(
  2542. 'index' => $d['index'],
  2543. 'contract' => $d['contract'],
  2544. 'wo' => $d['wo'],
  2545. 'activation' => $d['activation'],
  2546. 'name' => $d['name'],
  2547. 'address' => $d['address'],
  2548. 'package' => $d['package'],
  2549. 'retail_price' => $d['retail_price'],
  2550. 'incentives1' => $d['incentives1'],
  2551. 'incentives2' => $d['incentives2'],
  2552. 'total' => $d['total'],
  2553. 'claim' => $d['claim'],
  2554. ));
  2555. }else {
  2556. $total = number_format(floatval(str_replace("RM ", "" , $d['retail_price'])) * 2.3, 2);
  2557. array_push($combineData, array(
  2558. 'index' => $d['index'],
  2559. 'contract' => $d['contract'],
  2560. 'wo' => $d['wo'],
  2561. 'activation' => $d['activation'],
  2562. 'name' => $d['name'],
  2563. 'address' => $d['address'],
  2564. 'package' => $d['package'],
  2565. 'retail_price' => $d['retail_price'],
  2566. 'incentives1' => '230%',
  2567. 'incentives2' => $d['incentives2'],
  2568. 'total' => 'RM '.$total,
  2569. 'claim' => $d['claim'],
  2570. ));
  2571. }
  2572. }
  2573. }else if(count($count_cyberjaya) < 50){
  2574. if(date('n/Y', strtotime($d['activation'])) == $month.'/'.$year){
  2575. array_push($combineData, array(
  2576. 'index' => $d['index'],
  2577. 'contract' => $d['contract'],
  2578. 'wo' => $d['wo'],
  2579. 'activation' => $d['activation'],
  2580. 'name' => $d['name'],
  2581. 'address' => $d['address'],
  2582. 'package' => $d['package'],
  2583. 'retail_price' => $d['retail_price'],
  2584. 'incentives1' => $d['incentives1'],
  2585. 'incentives2' => $d['incentives2'],
  2586. 'total' => $d['total'],
  2587. 'claim' => $d['claim'],
  2588. ));
  2589. }
  2590. }
  2591. }
  2592. // Store to combineData New Incentives Jasin
  2593. foreach ($count_jasin as $key => $d) {
  2594. if(count($count_jasin) > 10){
  2595. if(date('n/Y', strtotime($d['activation'])) == $month.'/'.$year){
  2596. $total = number_format(floatval(str_replace("RM ", "" , $d['retail_price'])) * 2.3, 2);
  2597. array_push($combineData, array(
  2598. 'index' => $d['index'],
  2599. 'contract' => $d['contract'],
  2600. 'wo' => $d['wo'],
  2601. 'activation' => $d['activation'],
  2602. 'name' => $d['name'],
  2603. 'address' => $d['address'],
  2604. 'package' => $d['package'],
  2605. 'retail_price' => $d['retail_price'],
  2606. 'incentives1' => '230%',
  2607. 'incentives2' => $d['incentives2'],
  2608. 'total' => 'RM '.$total,
  2609. 'claim' => $d['claim'],
  2610. ));
  2611. }
  2612. }else if(count($count_jasin) < 10){
  2613. if(date('n/Y', strtotime($d['activation'])) == $month.'/'.$year){
  2614. array_push($combineData, array(
  2615. 'index' => $d['index'],
  2616. 'contract' => $d['contract'],
  2617. 'wo' => $d['wo'],
  2618. 'activation' => $d['activation'],
  2619. 'name' => $d['name'],
  2620. 'address' => $d['address'],
  2621. 'package' => $d['package'],
  2622. 'retail_price' => $d['retail_price'],
  2623. 'incentives1' => $d['incentives1'],
  2624. 'incentives2' => $d['incentives2'],
  2625. 'total' => $d['total'],
  2626. 'claim' => $d['claim'],
  2627. ));
  2628. }
  2629. }
  2630. }
  2631. }else {
  2632. $dataR = $this->returnDataR($company,$year,$month);
  2633. foreach($dataR as $r){
  2634. $combineData[] = $r;
  2635. }
  2636. }
  2637. }else if($app == 'Business'){
  2638. $dataB = $this->returnDataB($company,$year,$month);
  2639. }
  2640. foreach($dataB as $b){
  2641. $combineData[] = $b;
  2642. }
  2643. $i = 0;
  2644. $nested_data = array();
  2645. if(!empty($combineData)){
  2646. foreach ($combineData as $c){
  2647. $i++;
  2648. array_push($nested_data, array(
  2649. 'index' => $c['index'],
  2650. 'contract' => $c['contract'],
  2651. 'wo' => $c['wo'],
  2652. 'activated_dt' => $c['activation'],
  2653. 'name' => $c['name'],
  2654. 'address' => $c['address'],
  2655. 'package' => $c['package'],
  2656. 'retail_price' => $c['retail_price'],
  2657. 'incentives1' => $c['incentives1'],
  2658. 'incentives2' => $c['incentives2'],
  2659. 'total' => $c['total'],
  2660. 'claimed' => $c['claim'],
  2661. ));
  2662. }
  2663. }
  2664. usort($nested_data, function ($a, $b) {
  2665. $dateA = $a['activated_dt'];
  2666. $dateB = $b['activated_dt'];
  2667. // ascending ordering, use `<=` for descending
  2668. return $dateA <= $dateB;
  2669. });
  2670. return \DataTables::of($nested_data)->make(true);
  2671. }
  2672. }
  2673. public function exportPDFClaim($company,$year,$month,$app) {
  2674. $final = array();
  2675. $id = Auth::guard('ms')->id();
  2676. $user = Staff::with('StaffDetail')->find($id);
  2677. $com = Company::where('_id',$company)->first();
  2678. $dataR = array(); $dataB = array(); $combineData = array();
  2679. if($year!='null' && $month!='null' && $app=='null'){
  2680. $count_cyberjaya = array(); $count_jasin = array();
  2681. $extra = 0; $basic_total = 0; $totals = 0;
  2682. if(($year == '2019' || $year == '2020') && ($month == '12' || $month == '1')){
  2683. $dataDis = $this->returnDataR($company,'2019','12');
  2684. $dataJan = $this->returnDataR($company,'2020','1');
  2685. foreach ($dataDis as $key => $d) {
  2686. if(date('d/m/Y', strtotime($d['activation'])) >= '12/12/2019'){
  2687. if (strpos($d['address'], 'Cyberjaya') !== false) {
  2688. $count_cyberjaya[] = $d;
  2689. }
  2690. if (strpos($d['address'], 'Jasin') !== false) {
  2691. $count_jasin[] = $d;
  2692. }
  2693. }else {
  2694. if(date('n/Y', strtotime($d['activation'])) == $month.'/'.$year){
  2695. $combineData[] = $d;
  2696. }
  2697. }
  2698. }
  2699. foreach ($dataJan as $key => $d) {
  2700. if (strpos($d['address'], 'Cyberjaya') !== false) {
  2701. $count_cyberjaya[] = $d;
  2702. }
  2703. if (strpos($d['address'], 'Jasin') !== false) {
  2704. $count_jasin[] = $d;
  2705. }
  2706. }
  2707. // Store to combineData New Incentives Cyberjaya
  2708. foreach ($count_cyberjaya as $key => $d) {
  2709. if(count($count_cyberjaya) > 50){
  2710. if(date('n/Y', strtotime($d['activation'])) == $month.'/'.$year){
  2711. if($d['contract'] == '12'){
  2712. array_push($combineData, array(
  2713. 'index' => $d['index'],
  2714. 'contract' => $d['contract'],
  2715. 'wo' => $d['wo'],
  2716. 'activation' => $d['activation'],
  2717. 'name' => $d['name'],
  2718. 'address' => $d['address'],
  2719. 'package' => $d['package'],
  2720. 'retail_price' => $d['retail_price'],
  2721. 'incentives1' => $d['incentives1'],
  2722. 'incentives2' => $d['incentives2'],
  2723. 'total' => $d['total'],
  2724. 'claim' => $d['claim'],
  2725. ));
  2726. }else {
  2727. $total = number_format(floatval(str_replace("RM ", "" , $d['retail_price'])) * 2.3, 2);
  2728. array_push($combineData, array(
  2729. 'index' => $d['index'],
  2730. 'contract' => $d['contract'],
  2731. 'wo' => $d['wo'],
  2732. 'activation' => $d['activation'],
  2733. 'name' => $d['name'],
  2734. 'address' => $d['address'],
  2735. 'package' => $d['package'],
  2736. 'retail_price' => $d['retail_price'],
  2737. 'incentives1' => '230%',
  2738. 'incentives2' => $d['incentives2'],
  2739. 'total' => 'RM '.$total,
  2740. 'claim' => $d['claim'],
  2741. ));
  2742. }
  2743. }
  2744. }else if(count($count_cyberjaya) < 50){
  2745. if(date('n/Y', strtotime($d['activation'])) == $month.'/'.$year){
  2746. $total = number_format(floatval(str_replace("RM ", "" , $d['retail_price'])) * 2.1, 2);
  2747. array_push($combineData, array(
  2748. 'index' => $d['index'],
  2749. 'contract' => $d['contract'],
  2750. 'wo' => $d['wo'],
  2751. 'activation' => $d['activation'],
  2752. 'name' => $d['name'],
  2753. 'address' => $d['address'],
  2754. 'package' => $d['package'],
  2755. 'retail_price' => $d['retail_price'],
  2756. 'incentives1' => $d['incentives1'],
  2757. 'incentives2' => $d['incentives2'],
  2758. 'total' => $d['total'],
  2759. 'claim' => $d['claim'],
  2760. ));
  2761. }
  2762. }
  2763. }
  2764. // Store to combineData New Incentives Jasin
  2765. foreach ($count_jasin as $key => $d) {
  2766. if(count($count_jasin) > 10){
  2767. if(date('n/Y', strtotime($d['activation'])) == $month.'/'.$year){
  2768. $total = number_format(floatval(str_replace("RM ", "" , $d['retail_price'])) * 2.3, 2);
  2769. array_push($combineData, array(
  2770. 'index' => $d['index'],
  2771. 'contract' => $d['contract'],
  2772. 'wo' => $d['wo'],
  2773. 'activation' => $d['activation'],
  2774. 'name' => $d['name'],
  2775. 'address' => $d['address'],
  2776. 'package' => $d['package'],
  2777. 'retail_price' => $d['retail_price'],
  2778. 'incentives1' => '230%',
  2779. 'incentives2' => $d['incentives2'],
  2780. 'total' => 'RM '.$total,
  2781. 'claim' => $d['claim'],
  2782. ));
  2783. }
  2784. }else if(count($count_jasin) < 10){
  2785. if(date('n/Y', strtotime($d['activation'])) == $month.'/'.$year){
  2786. array_push($combineData, array(
  2787. 'index' => $d['index'],
  2788. 'contract' => $d['contract'],
  2789. 'wo' => $d['wo'],
  2790. 'activation' => $d['activation'],
  2791. 'name' => $d['name'],
  2792. 'address' => $d['address'],
  2793. 'package' => $d['package'],
  2794. 'retail_price' => $d['retail_price'],
  2795. 'incentives1' => $d['incentives1'],
  2796. 'incentives2' => $d['incentives2'],
  2797. 'total' => $d['total'],
  2798. 'claim' => $d['claim'],
  2799. ));
  2800. }
  2801. }
  2802. }
  2803. }else {
  2804. $dataR = $this->returnDataR($company,$year,$month);
  2805. foreach($dataR as $r){
  2806. $combineData[] = $r;
  2807. }
  2808. }
  2809. $dataB = $this->returnDataB($company,$year,$month);
  2810. foreach($dataB as $b){
  2811. $combineData[] = $b;
  2812. }
  2813. $i = 0;
  2814. $nested_data = array();
  2815. if(!empty($combineData)){
  2816. foreach ($combineData as $c){
  2817. $i++;
  2818. array_push($final, array(
  2819. 'index' => $c['index'],
  2820. 'contract' => $c['contract'],
  2821. 'wo' => $c['wo'],
  2822. 'activated_dt' => $c['activation'],
  2823. 'name' => $c['name'],
  2824. 'address' => $c['address'],
  2825. 'package' => $c['package'],
  2826. 'retail_price' => $c['retail_price'],
  2827. 'incentives1' => $c['incentives1'],
  2828. 'incentives2' => $c['incentives2'],
  2829. 'total' => $c['total'],
  2830. 'claimed' => $c['claim'],
  2831. ));
  2832. }
  2833. }
  2834. foreach($final as $f){
  2835. $basic_total += floatval(str_replace('RM ', '', $f['total']));
  2836. }
  2837. // Calculate extra incentves
  2838. if($month == '1' && $year == '2020'){
  2839. if(count($count_cyberjaya) > 50){
  2840. $balance = count($count_cyberjaya) - 50;
  2841. $extra += $balance * 50;
  2842. }
  2843. if(count($count_jasin) > 10){
  2844. $balance = count($count_jasin) - 10;
  2845. $extra += $balance * 50;
  2846. }
  2847. }else {
  2848. $extra = 0;
  2849. }
  2850. $totals = $basic_total + $extra;
  2851. usort($final, function ($a, $b) {
  2852. $dateA = $a['activated_dt'];
  2853. $dateB = $b['activated_dt'];
  2854. // ascending ordering, use `<=` for descending
  2855. return $dateA <= $dateB;
  2856. });
  2857. $pdf = PDF::loadView('pdf.agent-claim', compact('final', 'month', 'year','com','basic_total','extra','totals'));
  2858. $pdf->setPaper('A4', 'landscape');
  2859. $pdfName = preg_replace('/\s+/', '-', $com->name).'-'.$month.'-'.$year.'.pdf';
  2860. return $pdf->download( $pdfName );
  2861. }else if($year!='null' && $month!='null' && $app!='null'){
  2862. $count_cyberjaya = array(); $count_jasin = array();
  2863. $extra = 0; $basic_total = 0; $totals = 0;
  2864. if($app == 'Residential'){
  2865. if(($year == '2019' || $year == '2020') && ($month == '12' || $month == '1')){
  2866. $dataDis = $this->returnDataR($company,'2019','12');
  2867. $dataJan = $this->returnDataR($company,'2020','1');
  2868. foreach ($dataDis as $key => $d) {
  2869. if(date('d/m/Y', strtotime($d['activation'])) >= '12/12/2019'){
  2870. if (strpos($d['address'], 'Cyberjaya') !== false) {
  2871. $count_cyberjaya[] = $d;
  2872. }
  2873. if (strpos($d['address'], 'Jasin') !== false) {
  2874. $count_jasin[] = $d;
  2875. }
  2876. }else {
  2877. if(date('n/Y', strtotime($d['activation'])) == $month.'/'.$year){
  2878. $combineData[] = $d;
  2879. }
  2880. }
  2881. }
  2882. foreach ($dataJan as $key => $d) {
  2883. if (strpos($d['address'], 'Cyberjaya') !== false) {
  2884. $count_cyberjaya[] = $d;
  2885. }
  2886. if (strpos($d['address'], 'Jasin') !== false) {
  2887. $count_jasin[] = $d;
  2888. }
  2889. }
  2890. // Store to combineData New Incentives Cyberjaya
  2891. foreach ($count_cyberjaya as $key => $d) {
  2892. if(count($count_cyberjaya) > 50){
  2893. if(date('n/Y', strtotime($d['activation'])) == $month.'/'.$year){
  2894. if($d['contract'] == '12'){
  2895. array_push($combineData, array(
  2896. 'index' => $d['index'],
  2897. 'contract' => $d['contract'],
  2898. 'wo' => $d['wo'],
  2899. 'activation' => $d['activation'],
  2900. 'name' => $d['name'],
  2901. 'address' => $d['address'],
  2902. 'package' => $d['package'],
  2903. 'retail_price' => $d['retail_price'],
  2904. 'incentives1' => $d['incentives1'],
  2905. 'incentives2' => $d['incentives2'],
  2906. 'total' => $d['total'],
  2907. 'claim' => $d['claim'],
  2908. ));
  2909. }else {
  2910. $total = number_format(floatval(str_replace("RM ", "" , $d['retail_price'])) * 2.3, 2);
  2911. array_push($combineData, array(
  2912. 'index' => $d['index'],
  2913. 'contract' => $d['contract'],
  2914. 'wo' => $d['wo'],
  2915. 'activation' => $d['activation'],
  2916. 'name' => $d['name'],
  2917. 'address' => $d['address'],
  2918. 'package' => $d['package'],
  2919. 'retail_price' => $d['retail_price'],
  2920. 'incentives1' => '230%',
  2921. 'incentives2' => $d['incentives2'],
  2922. 'total' => 'RM '.$total,
  2923. 'claim' => $d['claim'],
  2924. ));
  2925. }
  2926. }
  2927. }else if(count($count_cyberjaya) < 50){
  2928. if(date('n/Y', strtotime($d['activation'])) == $month.'/'.$year){
  2929. array_push($combineData, array(
  2930. 'index' => $d['index'],
  2931. 'contract' => $d['contract'],
  2932. 'wo' => $d['wo'],
  2933. 'activation' => $d['activation'],
  2934. 'name' => $d['name'],
  2935. 'address' => $d['address'],
  2936. 'package' => $d['package'],
  2937. 'retail_price' => $d['retail_price'],
  2938. 'incentives1' => $d['incentives1'],
  2939. 'incentives2' => $d['incentives2'],
  2940. 'total' => $d['total'],
  2941. 'claim' => $d['claim'],
  2942. ));
  2943. }
  2944. }
  2945. }
  2946. // Store to combineData New Incentives Jasin
  2947. foreach ($count_jasin as $key => $d) {
  2948. if(count($count_jasin) > 10){
  2949. if(date('n/Y', strtotime($d['activation'])) == $month.'/'.$year){
  2950. $total = number_format(floatval(str_replace("RM ", "" , $d['retail_price'])) * 2.3, 2);
  2951. array_push($combineData, array(
  2952. 'index' => $d['index'],
  2953. 'contract' => $d['contract'],
  2954. 'wo' => $d['wo'],
  2955. 'activation' => $d['activation'],
  2956. 'name' => $d['name'],
  2957. 'address' => $d['address'],
  2958. 'package' => $d['package'],
  2959. 'retail_price' => $d['retail_price'],
  2960. 'incentives1' => '230%',
  2961. 'incentives2' => $d['incentives2'],
  2962. 'total' => 'RM '.$total,
  2963. 'claim' => $d['claim'],
  2964. ));
  2965. }
  2966. }else if(count($count_jasin) < 10){
  2967. if(date('n/Y', strtotime($d['activation'])) == $month.'/'.$year){
  2968. array_push($combineData, array(
  2969. 'index' => $d['index'],
  2970. 'contract' => $d['contract'],
  2971. 'wo' => $d['wo'],
  2972. 'activation' => $d['activation'],
  2973. 'name' => $d['name'],
  2974. 'address' => $d['address'],
  2975. 'package' => $d['package'],
  2976. 'retail_price' => $d['retail_price'],
  2977. 'incentives1' => $d['incentives1'],
  2978. 'incentives2' => $d['incentives2'],
  2979. 'total' => $d['total'],
  2980. 'claim' => $d['claim'],
  2981. ));
  2982. }
  2983. }
  2984. }
  2985. }else {
  2986. $dataR = $this->returnDataR($company,$year,$month);
  2987. foreach($dataR as $r){
  2988. $combineData[] = $r;
  2989. }
  2990. }
  2991. }else if($app == 'Business'){
  2992. $dataB = $this->returnDataB($company,$year,$month);
  2993. }
  2994. foreach($dataB as $b){
  2995. $combineData[] = $b;
  2996. }
  2997. $i = 0;
  2998. $nested_data = array();
  2999. if(!empty($combineData)){
  3000. foreach ($combineData as $c){
  3001. $i++;
  3002. array_push($final, array(
  3003. 'index' => $c['index'],
  3004. 'contract' => $c['contract'],
  3005. 'wo' => $c['wo'],
  3006. 'activated_dt' => $c['activation'],
  3007. 'name' => $c['name'],
  3008. 'address' => $c['address'],
  3009. 'package' => $c['package'],
  3010. 'retail_price' => $c['retail_price'],
  3011. 'incentives1' => $c['incentives1'],
  3012. 'incentives2' => $c['incentives2'],
  3013. 'total' => $c['total'],
  3014. 'claimed' => $c['claim'],
  3015. ));
  3016. }
  3017. }
  3018. foreach($final as $f){
  3019. $basic_total += floatval(str_replace('RM ', '', $f['total']));
  3020. }
  3021. //Calculate extra incentves
  3022. if($month == '1' && $year == '2020'){
  3023. if(count($count_cyberjaya) > 50){
  3024. $balance = count($count_cyberjaya) - 50;
  3025. $extra += $balance * 50;
  3026. }
  3027. if(count($count_jasin) > 10){
  3028. $balance = count($count_jasin) - 10;
  3029. $extra += $balance * 50;
  3030. }
  3031. }else {
  3032. $extra = 0;
  3033. }
  3034. $totals = $basic_total + $extra;
  3035. usort($final, function ($a, $b) {
  3036. $dateA = $a['activated_dt'];
  3037. $dateB = $b['activated_dt'];
  3038. // ascending ordering, use `<=` for descending
  3039. return $dateA <= $dateB;
  3040. });
  3041. $pdf = PDF::loadView('pdf.agent-claim', compact('final', 'month', 'year','com','basic_total','extra','totals'));
  3042. $pdf->setPaper('A4', 'landscape');
  3043. $pdfName = preg_replace('/\s+/', '-', $com->name).'-'.$month.'-'.$year.'.pdf';
  3044. return $pdf->download( $pdfName );
  3045. }
  3046. }
  3047. public function manualDate(){
  3048. $final = array();
  3049. $com = Company::where('_id','5bb2bc66ee0dc2271c6fedf6')->first();
  3050. $dataR = array(); $dataB = array(); $combineData = array();
  3051. $count_cyberjaya = array(); $count_jasin = array();
  3052. $extra = 0; $basic_total = 0; $totals = 0;
  3053. $dataDis = $this->returnDataR('5bb2bc66ee0dc2271c6fedf6','2019','12');
  3054. foreach ($dataDis as $key => $d) {
  3055. if(date('d/m/Y', strtotime($d['activation'])) >= '12/12/2019'){
  3056. $combineData[] = $d;
  3057. }
  3058. }
  3059. $dataB = $this->returnDataB('5bb2bc66ee0dc2271c6fedf6','2019','12');
  3060. foreach($dataB as $b){
  3061. $combineData[] = $b;
  3062. }
  3063. $i = 0;
  3064. $nested_data = array();
  3065. if(!empty($combineData)){
  3066. foreach ($combineData as $c){
  3067. $i++;
  3068. array_push($final, array(
  3069. 'index' => $c['index'],
  3070. 'contract' => $c['contract'],
  3071. 'wo' => $c['wo'],
  3072. 'activated_dt' => $c['activation'],
  3073. 'name' => $c['name'],
  3074. 'address' => $c['address'],
  3075. 'package' => $c['package'],
  3076. 'retail_price' => $c['retail_price'],
  3077. 'incentives1' => $c['incentives1'],
  3078. 'incentives2' => $c['incentives2'],
  3079. 'total' => $c['total'],
  3080. 'claimed' => $c['claim'],
  3081. ));
  3082. }
  3083. }
  3084. usort($final, function ($a, $b) {
  3085. $dateA = $a['activated_dt'];
  3086. $dateB = $b['activated_dt'];
  3087. // ascending ordering, use `<=` for descending
  3088. return $dateA <= $dateB;
  3089. });
  3090. return json_encode($final);
  3091. }
  3092. }