You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DealerController.php 100KB

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