[php] ManualInvoiceController.php

Viewer

copydownloadembedprintName: ManualInvoiceController.php
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5. use Illuminate\Http\Request;
  6. use Illuminate\Http\Response;
  7. use Illuminate\Support\Str;
  8. use GuzzleHttp\Client;
  9. use Session;
  10. use Storage;
  11. use Log;
  12. use PDF;
  13. use Exception;
  14. use File;
  15. use Excel;
  16. use Carbon;
  17.  
  18. // Model
  19. use DB;
  20. use App\ManualInvoice;
  21. use App\Department;
  22. use App\Client as ClientModel;
  23. use App\Division;
  24. use App\DetailManual;
  25. use App\ServiceCategory;
  26. use App\Invoice;
  27. use Validator;
  28.  
  29. class ManualInvoiceController extends Controller
  30. {
  31.     public function __construct()
  32.     {
  33.         $this->middleware('checksession');
  34.         $this->middleware('checktoken');
  35.  
  36.         // $logFile = 'log.log';
  37.         // Log::useDailyFiles(storage_path() . '/logs/performance_invoice_upload/' . $logFile);
  38.     }
  39.     /**
  40.      * Display a listing of the resource.
  41.      *
  42.      * @return \Illuminate\Http\Response
  43.      */
  44.     public function index(Request $request)
  45.     {
  46.         // return "Maintain";
  47.         if (empty($_COOKIE["token"])) {
  48.             return redirect("https://backoffice.sprintasia.net:82/sprint-sysadmin/public/login");
  49.         } else {
  50.             $title          = "Invoice";
  51.             $gen            = "Manual Invoice";
  52.             $content        = "Manual Invoice";
  53.  
  54.  
  55.             $data           = DB::table('db_sprint_ar.manual_invoice AS m')
  56.                                 ->join('db_sprint_ar.invoice AS i', 'i.id_manual', 'm.id')
  57.                                 ->whereIn('i.status', [0,2,7])
  58.                                 ->select('i.*', 'm.*', 'i.submit_date AS tanggal')
  59.                                                                 ->orderBy('i.submit_date', 'desc')
  60.                                 ->get();
  61.             $dtdepts        = DB::table('invoice')->select('dept_code', 'dept_name')->get();
  62.             $dtclients      = DB::table('invoice')->select('client_code', 'client_name')->get();
  63.             $dtdivs         = DB::table('invoice')->select('division_code', 'division_name')->get();
  64.             $dtservs        = ServiceCategory::select('service_category_code', 'service_category_name')->get();
  65.  
  66.             // return view('manual_invoice.index', compact('title','content','gen','data','dtdepts', 'dtclients', 'dtdivs', 'dtservs'));
  67.             return view('manual_invoice.index', compact('title', 'content', 'gen', 'data', 'dtdepts', 'dtclients', 'dtdivs', 'dtservs'));
  68.         }
  69.     } 
  70.        
  71.         public function upload_manual_invoice(Request $request)
  72.     {
  73.         $title      = "Manual Invoice";
  74.         $gen        = "Upload Manual Invoice";
  75.         $content    = "Upload Manual Invoice";
  76.  
  77.         return view('manual_invoice.index_upload', compact('title', 'content', 'gen'));
  78.     }
  79.  
  80.     public function get_id_desc($desc)
  81.     {
  82.         try {
  83.             $Uri = new \GuzzleHttp\Client(['base_uri' => 'https://backoffice.sprintasia.net:82/sprint-sysadmin/public/api-master/get_id_desc']);
  84.             $headers = [
  85.                 'Authorization' => 'Bearer ' . $_COOKIE["token"],
  86.                 'Accept'        => 'application/json',
  87.             ];
  88.             $response = $Uri->request('GET', '', [
  89.                 'headers' => $headers,
  90.                 'query' => ['as_description' => $desc]
  91.             ]);
  92.             $data = json_decode($response->getBody()->getContents(), true)['data'][0];
  93.             if(count($data) > 0){
  94.                 return (int)$data['id_provider_odoo'];
  95.             }else{
  96.                 return 0;
  97.             }
  98.         } catch (Exception $e) {
  99.             return 0;
  100.         }
  101.     }
  102.  
  103.     public function store(Request $request)
  104.     { 
  105.         // try{
  106.             date_default_timezone_set("Asia/Bangkok");
  107.             $dateNow = date('Y-m-d H:i:s');            
  108.                 $username_login     = session()->get('profile')['name'];
  109.  
  110.             $cn                 =  $request['cn'];
  111.             $cc                 =  $request['cc'];
  112.             $dic                =  $request['dic'];
  113.             $dec                =  $request['dec'];
  114.             $desc               =  $request['desc'];
  115.             $sc_Add             =  $request['sc_Add'];
  116.             $materai            =  $request['materai'];
  117.             $vat                =  $request['vat'];
  118.             $uso                = $request['uso'];
  119.             $bhp                = $request['bhp'];
  120.             $total_temp         = $request['total_s'];
  121.             $total              = str_replace(',', '', $total_temp);
  122.             $item               = $request['item'];
  123.             $description_inv    = $request['temp_desc'];
  124.             $unitprice          = $request['unitprice'];
  125.             $quantity           = $request['quantity'];
  126.             $manual_detail      = $request['manual_detail'];
  127.  
  128.             // echo "</br>";
  129.             // echo "</br> cn : " ; print_r('cn');
  130.             // echo "</br> cc : " ; print_r('cc');
  131.             // echo "</br> dic : " ; print_r('dic');
  132.             // echo "</br> dec : " ; print_r('dec');
  133.             // echo "</br> desc : " ; print_r('desc');
  134.             // echo "</br> sc_Add : " ; print_r('sc_Add');
  135.             // echo "</br> materai : " ; print_r('materai');
  136.             // echo "</br> vat : " ; print_r('vat');
  137.             // echo "</br> uso : " ; print_r('uso');
  138.             // echo "</br> bhp : " ; print_r('bhp');
  139.             // echo "</br> total_temp : " ; print_r('total_temp');
  140.  
  141.             // die();
  142.             
  143.             $data_div      = $this->get_data_division_by_id($dic);
  144.             
  145.             if (count($data_div) == 0) {
  146.                 return "division";
  147.             }
  148.  
  149.             $division_name          = $data_div['division_name'];
  150.             $bank_acc_number_sprint = $data_div['division_bank_acc_number_sprint'];
  151.             $bank_acc_name_sprint   = $data_div['division_bank_acc_name_sprint'];
  152.             $bank_coa_sprint        = $data_div['division_bank_coa_sprint'];
  153.             $zahir_name             = $data_div['division_zahir_name'];
  154.             $client_npwp_no         = $data_div['division_npwp'];
  155.             $bank_acc_name_client   = $data_div['division_bank_account_name'];
  156.             $bank_acc_number_client = $data_div['division_bank_account_number'];
  157.             $bank_name              = $data_div['division_bank_name_sprint'];
  158.             // $client_coa             = $data_div['division_bank_name_sprint'];
  159.             $division_address       = $data_div['division_address'];
  160.             $division_description   = $data_div['division_description'];
  161.             $ba                     = $data_div['berita_acara'];
  162.             if($ba == 1){
  163.                 $ba = "Yes";
  164.             }else{
  165.                 $ba = "No";
  166.             }
  167.             $fp                     = $data_div['faktur_pajak'];
  168.             if($fp == 1){$fp = "Yes";}else{$fp = "No";}
  169.             $softcopy               = $data_div['soft_copy'];
  170.             if($softcopy == 1){$softcopy = "Yes";}else{$softcopy = "No";}
  171.             $hardcopy               = $data_div['hard_copy'];
  172.             if($hardcopy == 1){$hardcopy = "Yes";}else{$hardcopy = "No";}
  173.          
  174.         
  175.             $data_pic      = $this->get_data_pics_by_id($dic);
  176.             if (count($data_pic) == 0) {
  177.                 return "pics";//redirect()->back()->with('error', 'Data PIC Not Found For '. $division_name .' Division. Please Check AM');
  178.                // return "Data PIC Not Found For $division_name Division. Please Check AM";
  179.             }
  180.  
  181.             $pic_id       = $data_pic['pic_id'];
  182.             $pic_name     = $data_pic['pic_name'];
  183.             $email_client = $data_pic['pic_email'];
  184.             $pic_position = $data_pic['pic_position'];
  185.  
  186.             $data_pic_collection = $this->get_data_pic_collection($dic);
  187.             if (count($data_pic_collection) == 0) {
  188.                 $pic_collection_name        =  $pic_name;
  189.                 $email_pic_collection       = $email_client;
  190.                         }else{
  191.                 $pic_collection_name        = $data_pic_collection['pic_name'];
  192.                 $email_pic_collection       = $data_pic_collection['pic_email'];
  193.             }
  194.             
  195.             $data_pic_pph23 = $this->get_data_pic_pph23($dic);
  196.             if (count($data_pic_pph23) == 0) {
  197.                                 $pic_pph23_name        = $pic_name;
  198.                 $email_pic_pph23       = $email_client;
  199.             }else{
  200.                 $pic_pph23_name        = $data_pic_pph23['pic_name'];
  201.                 $email_pic_pph23       = $data_pic_pph23['pic_email'];
  202.     
  203.             }
  204.             
  205.             $data_client      = $this->get_data_client_by_id($cc);
  206.     
  207.             if (count($data_client) == 0) {
  208.                 return redirect()->back()->with('alert', 'Data PIC Not Found For '. $division_name .' Division. Please Check AM');
  209.        
  210.             }
  211.             $client_domicile_address = $data_client['client_domicile_address'];
  212.             $client_coa                    = $data_client['client_coa'];
  213.             $lmbg                                  = $data_client['lmbnegara']; //"yes"; 
  214.             $no_spk                                = $data_client['client_no_spk'];
  215.             $no_addendum_spk               = $data_client['client_no_addendum_spk']; 
  216.             $max_amount                    = $data_client['max_amount']; //10000000;
  217.             $invoice_rule                   = $data_client['invoice_rule'];
  218.  
  219.             $data_dept                = $this->get_data_dept_by_id($dec);
  220.             if (count($data_dept) == 0) {
  221.                 return "dept"; //$dept_name   = "";
  222.             }
  223.             $dept_name   = $data_dept['dept_name'];
  224.     
  225.             $id_manual = 0;
  226.     
  227.             $description = join(", ", $description_inv);
  228.     
  229.             $sub_total              = ceil($total);            
  230.             $new_ppn                = $this->check_ppn($vat, $sub_total);
  231.             $amount_invoice_rule    = $sub_total + $new_ppn;
  232.             
  233.             // return $lmbg;
  234.             // return $invoice_rule;
  235.             // return $max_amount;
  236.             // return $amount_invoice_rule;
  237.             // return $sub_total;
  238.             // return $vat;
  239.  
  240.             $real                                  = $this->real_field($lmbg, $invoice_rule, $max_amount, $amount_invoice_rule, $sub_total, $vat);
  241.             // return $real;
  242.             
  243.             $sub_total_now                  = $real[0];
  244.             $sub_total_real                 = $real[1];
  245.             $new_ppn_now                    = $real[2];
  246.             $new_ppn_real                   = $real[3];
  247.                 
  248.             if ($materai == "yes") {
  249.                 $new_materai = $this->check_materai($sub_total_now, $new_ppn_now);
  250.             }else if ($materai == "no") {
  251.                 $new_materai = 0;
  252.             }
  253.                 
  254.             $pph23   = $this->check_pph23($cc, $sub_total_now);
  255.     
  256.             if ($bhp == "yes") {
  257.                 $bhp_Add = ($sub_total_now ) * 0.005; //+  $new_ppn_now
  258.                 $bhp_manual = "yes";
  259.             } else {
  260.                 $bhp_Add = 0;
  261.                 $bhp_manual = "no";
  262.             }
  263.     
  264.             if ($uso == "yes") {
  265.                 $uso_Add = ($sub_total_now ) * 0.0125; //+  $new_ppn_now
  266.                 $uso_manual = "yes";
  267.             } else {
  268.                 $uso_Add = 0;
  269.                 $uso_manual = "no";
  270.             }
  271.     
  272.                 $json = [
  273.                     "detail" => [
  274.                         "info"  => [
  275.                             "from" => "PT Sprint Asia",
  276.                             "recipient"    => $cn,
  277.                             "reff_no"      => '',
  278.                             "cust_id"      => '',
  279.                             "sales_person" => ''
  280.                         ],
  281.                         "data"  => array($item),
  282.                         "total" => [
  283.                             "quantity"  => $quantity,
  284.                             "unitprice" => $unitprice,
  285.                             "sub_total" => ceil($sub_total_now), // sum_total
  286.                             "with_ppn"  => floor($new_ppn_now), // new_ppn
  287.                             "bhp"       => $bhp_Add,
  288.                             "uso"       => $uso_Add,
  289.                             "with_pph"  => $pph23
  290.                            
  291.                         ]
  292.                     ],
  293.     
  294.                 ];
  295.  
  296.                 // if($quantity == "NaN" || $unitprice == "NaN"){
  297.                 //     return "NaN";
  298.                 // }
  299.  
  300.                 $serialize = json_encode($json);
  301.  
  302.             // return $serialize;
  303.     
  304.             $Total                         = round($sub_total_now + $new_ppn_now + $new_materai - $pph23 - $bhp_Add - $uso_Add);
  305.             $Total_real    = round($sub_total_real + $new_ppn_real + $new_materai - $pph23 - $bhp_Add - $uso_Add);
  306.             $revenue_coa   = $sc_Add;
  307.                         $data_revebue_coa                 = $this->get_data_revenue_coa($revenue_coa);
  308.                         $product_id                            = $data_revebue_coa['product_id'];
  309.     
  310.             // Manual Invoice
  311.             $manual_invoice = [
  312.                 "client_code"   => $cc,
  313.                 "dept_code"     => $dec,
  314.                 "division_code" => $dic,
  315.                 "desc"          => $desc,
  316.                 "vat"           => $vat,
  317.                 "materai"       => $materai,
  318.                 "bhp"            => $bhp_manual,
  319.                 "uso"            => $uso_manual,
  320.                 "total"         => $Total_real,
  321.                 "created_by"    => $username_login,
  322.                 "created_at"    => NOW()
  323.             ];
  324.     
  325.             try {
  326.                 $last_id = DB::table("db_sprint_ar.manual_invoice")->insertGetId($manual_invoice);
  327.             } catch (Exception $e) {
  328.                 return ["code" => "_ERR_SYSTM", "error" => $e->getMessage()];
  329.             }
  330.     
  331.             $invoice = [
  332.                 "client_code"             => $cc,
  333.                 "client_name"             => $cn,
  334.                 "division_code"           => $dic,
  335.                 "division_name"           => $division_name,
  336.                 "division_description"    => $division_description,
  337.                 "dept_code"               => $dec,
  338.                 "dept_name"               => $dept_name,
  339.                 "pic_id"                  => $pic_id,
  340.                 "inv_month"               => date("m"),
  341.                 "inv_year"                => date("Y"),
  342.                 "unitprice"               => $unitprice,
  343.                 "quantity"                => $quantity,
  344.                 "sub_total"               => ceil($sub_total_now),
  345.                 "sub_total_real"          => ceil($sub_total_real),
  346.                 "ppn"                     => floor($new_ppn_now),
  347.                 "ppn_real"                => floor($new_ppn_real),
  348.                 "materai"                 => $new_materai,
  349.                 "pph23"                   => $pph23,
  350.                 "total"                   => $Total,
  351.                 "total_real"              => $Total_real,
  352.                 "serialize_data"          => $serialize,
  353.                 "selisih"                 => 0,
  354.                 "status"                  => 0,
  355.                 "addmanual"               => 1,
  356.                 "submit_date"             => NOW(),
  357.                 "id_manual"               => $last_id,
  358.                 "pic_name"                => $pic_name,
  359.                 "division_address"        => $division_address,
  360.                 "bank_acc_number_sprint"  => $bank_acc_number_sprint,
  361.                 "bank_acc_name_sprint"    => $bank_acc_name_sprint,
  362.                 "bank_acc_number_client"  => $bank_acc_number_client,
  363.                 "bank_acc_name_client"    => $bank_acc_name_client,
  364.                 "bank_coa_sprint"         => $bank_coa_sprint,
  365.                 "zahir_name"              => $zahir_name,
  366.                 "client_npwp_no"          => $client_npwp_no,
  367.                 "bank_name"               => $bank_name,
  368.                 "client_coa"              => $client_coa,
  369.                 "revenue_coa"             => $revenue_coa,
  370.                                 "product_id"                      => $product_id,
  371.                 "email_client"            => $email_client,
  372.                 "email_collection"        => $email_pic_collection,
  373.                 // "pic_collection"          => $pic_collection_name,
  374.                 "email_pph23"             => $email_pic_pph23,
  375.                 // "pic_pph23"               => $pic_pph23_name,
  376.                 "pic_position"            => $pic_position,
  377.                 "description"             => $description,
  378.                 "id_collection"           =>'[""]',
  379.                 "ba"                      => $ba,
  380.                 "fp"                      => $fp,
  381.                 "softcopy"                => $softcopy,
  382.                 "hardcopy"                => $hardcopy,
  383.                 "created_by"              => $username_login,
  384.                 "created_at"              => NOW()
  385.             ];
  386.  
  387.             try {
  388.                 $last_id_invoice    = DB::table("db_sprint_ar.invoice")->insertGetId($invoice);
  389.                 $now                = NOW();
  390.                 $username           = session()->get('profile')['name'];
  391.  
  392.                 $descriptionss                 = "$last_id_invoice tanggal: $now submit by $username";
  393.  
  394.                 $email                                 = session()->get('profile')['email'];
  395.                 $description_log                = "serialize_data: $serialize";
  396.                 
  397.                 $activity = "Add Manual Invoice";
  398.                 DB::table('invoice_log_history')->insert(['id_inv' => $last_id_invoice, 'activity' => $activity, 'username' => $username, 'email' => $email, 'description' => $description_log,'created_at' => $now]);
  399.  
  400.                 $saved_log_datas = preg_replace( "/\r|\n/", " ", json_encode($invoice) );
  401.                 $saved_log_datas = str_replace("\\", "", $saved_log_datas);
  402.                 // DB::table('history')->insert([ 'activity'=> "Create Manual Invoice", 'description'=> "Create values : ". $saved_log_datas , 'created_by' => $username , 'created_at' => $now ]);
  403.                 DB::table('history')->insert([ 'client_name'=> $cn, 'division_name'=> $division_name, 'dept_name'=> $dept_name, 'inp_desc2' => "Create Manual Invoice, Client Name : " . $cn . " , Division Name : " . $division_name . " , Dept Name : " . $dept_name . " , Quantity : " . $quantity . " , Description : " .$description, 'inp_desc'=> $description, 'activity'=> "Create Manual Invoice", 'description'=> "Create values : ". $saved_log_datas , 'created_by' => $username , 'created_at' => $now ]);
  404.  
  405.                DB::table("db_sprint_ar.manual_invoice")->where('id', $last_id)->update(["total" => $Total]);
  406.             } catch (Exception $e) {
  407.                 return ["code" => "_ERR_SYSTM", "error" => $e->getMessage()];
  408.             }
  409.             
  410.             // Insert to table manual_detail
  411.             for ($i = 0; $i < count($manual_detail); $i++) {
  412.                 $item_temp = $manual_detail[$i]['desc'];
  413.                 $manual_details[] = [
  414.                     "id_manual"    => $last_id,
  415.                     "service"              => $manual_detail[$i]['service'],
  416.                     "quantity"             => $manual_detail[$i]['quantity'],
  417.                     "unit_price"   => $manual_detail[$i]['unit_price'],
  418.                     "desc"                         => $item_temp, //$manual_detail[$i]['desc'],
  419.                     "total"                => $manual_detail[$i]['total']
  420.                 ];
  421.             }
  422.  
  423.             // return $manual_details;
  424.     
  425.             try {
  426.                 $last_id = DB::table("db_sprint_ar.manual_detail")->insert($manual_details);
  427.             } catch (Exception $e) {
  428.                 return ["code" => "_ERR_SYSTM", "error" => $e->getMessage()];
  429.             }
  430.     
  431.             return "success";
  432.            
  433.  
  434.         // }catch (Exception $e) {
  435.         //     return "error";//["code" => "_ERR_SYSTM", "error" => $e->getMessage()];
  436.         // }
  437.        
  438.         // return redirect(url('/manual-invoice'))->with('success');
  439.         
  440.  
  441.  
  442.     }
  443.        
  444.                  public function get_data_revenue_coa($revenue_coa)
  445.     {
  446.         try {
  447.             $Uri = new \GuzzleHttp\Client(['base_uri' => 'https://backoffice.sprintasia.net:82/sprint-sysadmin/public/api-master/get_data_revenue_coa']);
  448.             $headers = [
  449.                 'Authorization' => 'Bearer ' . $_COOKIE["token"],
  450.                 'Accept'        => 'application/json',
  451.             ];
  452.             $response = $Uri->request('GET', '', [
  453.                 'headers' => $headers,
  454.                 'query' => ['revenue_coa' => $revenue_coa]
  455.             ]);
  456.             return $data = json_decode($response->getBody()->getContents(), true)['data'][0];
  457.         } catch (Exception $e) {
  458.             return [];
  459.         }
  460.     }
  461.  
  462.     public function update(Request $request)
  463.     {
  464.         if ($request->input('sc_Edit') == "" || $request->input('sc_Edit') == null || $request->input('sc_Edit') == null) {
  465.             return redirect()->back()->with('alert', 'Service Category Tidak Dapat Dipilih. Silahkan Periksa di Account Management. Data Tidak Tersimpan.');
  466.         }
  467.  
  468.         $id            = $request->input('id');
  469.         $client_name   = $request->input('client');
  470.         $client_code   = $request->input('client_code');
  471.         $division_code = $request->input('division_code');
  472.         $dept_code     = $request->input('dept_code');
  473.         $desc          = $request->input('desc');
  474.         $sc_coa        = $request->input('sc_Edit');
  475.         $vat           = $request->input('vat');
  476.         $bhp           = $request->input('bhp_Edit');
  477.         $uso           = $request->input('uso_Edit');
  478.         $materai       = $request->input('materai');
  479.  
  480.                 $dataInv = DB::table('invoice')->where('id_manual', $id)->get();
  481.                 $status = $dataInv[0]->status;
  482.                 // return $dataInv[0]->serialize_data;
  483.                 $serialize_data = json_decode($dataInv[0]->serialize_data);
  484.                 $cust_id               = $serialize_data->detail->info->cust_id; // data serialize data json (sekarang)
  485.                 $update_printDate = $dataInv[0]->update_print_date;
  486.                
  487.  
  488.         $data_div               = $this->get_data_division_by_id($division_code);
  489.         if (count($data_div) == 0) {
  490.             return redirect()->back()->with('alert', 'Data Division Not Found For '. $division_code .'. Please Check AM');
  491.        
  492.         }
  493.         $division_name          = $data_div['division_name'];
  494.         $bank_acc_number_sprint = $data_div['division_bank_acc_number_sprint'];
  495.         $bank_acc_name_sprint   = $data_div['division_bank_acc_name_sprint'];
  496.         $bank_coa_sprint        = $data_div['division_bank_coa_sprint'];
  497.         $zahir_name             = $data_div['division_zahir_name'];
  498.         $client_npwp_no         = $data_div['division_npwp'];
  499.         $bank_acc_name_client   = $data_div['division_bank_account_name'];
  500.         $bank_acc_number_client = $data_div['division_bank_account_number'];
  501.         $bank_name              = $data_div['division_bank_name_sprint'];
  502.         $client_coa             = $data_div['division_bank_name_sprint'];
  503.         $division_address       = $data_div['division_address'];
  504.                 $division_invoice_rule  = $data_div['division_invoice_rule']; 
  505.                 $division_max_amount    = $data_div['division_max_amount']; 
  506.         $division_description   = $data_div['division_description']; 
  507.                 $ba                     = $data_div['berita_acara'];
  508.         if($ba == 1){$ba = "Yes";}else{$ba = "No";}
  509.         $fp                     = $data_div['faktur_pajak'];
  510.         if($fp == 1){$fp = "Yes";}else{$fp = "No";}
  511.         $softcopy               = $data_div['soft_copy'];
  512.         if($softcopy == 1){$softcopy = "Yes";}else{$softcopy = "No";}
  513.         $hardcopy               = $data_div['hard_copy'];
  514.         if($hardcopy == 1){$hardcopy = "Yes";}else{$hardcopy = "No";}
  515.                
  516.         $data_pic     = $this->get_data_pics_by_id($division_code);
  517.         if (count($data_pic) == 0) {
  518.             return redirect()->back()->with('alert', 'Data PIC Not Found For '. $division_code .'. Please Check AM');
  519.         }
  520.         $pic_id       = $data_pic['pic_id'];
  521.         $pic_name     = $data_pic['pic_name'];
  522.         $email_client = $data_pic['pic_email'];
  523.         $pic_position = $data_pic['pic_position'];
  524.  
  525.         $data_pic_collection = $this->get_data_pic_collection($division_code);
  526.         if (count($data_pic_collection) == 0) {
  527.                         $pic_collection_name        = $pic_name;
  528.             $email_pic_collection       = $email_client;
  529.                 }else{
  530.             $pic_collection_name        = $data_pic_collection['pic_name'];
  531.             $email_pic_collection       = $data_pic_collection['pic_email'];
  532.         }
  533.         
  534.  
  535.         $data_pic_pph23 = $this->get_data_pic_pph23($division_code);
  536.         if (count($data_pic_pph23) == 0) {
  537.                         $pic_pph23_name        = $pic_name;
  538.             $email_pic_pph23       = $email_client;
  539.                 }else{
  540.             $pic_pph23_name        = $data_pic_pph23['pic_name'];
  541.             $email_pic_pph23       = $data_pic_pph23['pic_email'];
  542.         }
  543.         
  544.         $data_client              = $this->get_data_client_by_id($client_code);
  545.         if (count($data_client) == 0) {
  546.             return redirect()->back()->with('alert', 'Data Client Not Found For '. $client_code .'. Please Check AM');
  547.         }
  548.         $client_domicile_address  = $data_client['client_domicile_address'];
  549.         $client_coa               = $data_client['client_coa'];
  550.                 $lmbg                                    = $data_client['lmbnegara']; //"yes" ;
  551.                 $max_amount                      = $data_client['max_amount'];
  552.                 $invoice_rule                     = $data_client['invoice_rule'];
  553.  
  554.         $data_dept   = $this->get_data_dept_by_id($dept_code);
  555.         if (count($data_dept) == 0) {
  556.             return redirect()->back()->with('alert', 'Data Department Not Found For '. $division_name .'. Please Check AM');
  557.        }
  558.         $dept_name   = $data_dept['dept_name'];
  559.         $id_manual = 0;
  560.         $sum_quantity = 0;
  561.         $sum_unitprice = 0;
  562.         $sum_total = 0;
  563.                
  564.         // JSON Serialize
  565.         $length = count($request->input('service_cat'));
  566.         if ($length > 0) {
  567.  
  568.             $item_name   = $request->input('vdesc');
  569.             $service     = $request->input('service_cat');
  570.             $id_service     = $request->input('id_service_cat');
  571.                         $temp_desc = [];
  572.                         for($c = 0; $c < count($item_name); $c++){
  573.                                 if($item_name[$c] == null || $item_name[$c] == '' || $item_name[$c] == ""){
  574.                                         continue;
  575.                                 }
  576.                                 array_push($temp_desc, $item_name[$c]);
  577.                         }
  578.             $description = join(", ", $temp_desc);
  579.             $quantity    = $request->input('quantity');
  580.             $unitprice   = $request->input('unitprice');
  581.             $total       = $request->input('total');
  582.                         // return $description;
  583.             for ($x = 0; $x < $length; $x++) {
  584.                 // return $id_service[2];
  585.                 if($id_service[$x] == '0')  return redirect()->back()->with('alert', "Id Description $service[$x] Tidak Tersedia. Silahkan Create/Update Provider $service[$x]"); 
  586.                 $item_desc = [
  587.                     "item_name" => $service[$x],
  588.                     "quantity"  => $quantity[$x],
  589.                     "unitprice" => $unitprice[$x],
  590.                     "total"     => intval(str_replace(',', '', $total[$x]))
  591.                 ];
  592.  
  593.                 $item[] = [
  594.                     "item"      =>  $item_name[$x],
  595.                     "item_desc" => array($item_desc)
  596.                 ];
  597.  
  598.                 $sum_quantity = $sum_quantity + intval($quantity[$x]);
  599.                 $sum_unitprice = $sum_unitprice + intval($unitprice[$x]);
  600.                 $sum_total = $sum_total + intval(str_replace(',', '', $total[$x]));
  601.             }
  602.             
  603.             $sub_total = ceil($sum_total);
  604.             $new_ppn = floor($this->check_ppn($vat, $sub_total));
  605.                         $amount_invoice_rule = $sub_total + $new_ppn;
  606.  
  607.                         $real                                  = $this->real_field($lmbg, $invoice_rule, $max_amount, $amount_invoice_rule, $sub_total, $vat);
  608.                        
  609.                         $sub_total_now                  = $real[0];
  610.                         $sub_total_real                 = $real[1];
  611.                         $new_ppn_now                    = $real[2];
  612.                         $new_ppn_real                   = $real[3];
  613.                        
  614.                        
  615.             if ($materai == "yes") {
  616.                 $new_materai = $this->check_materai($sub_total_now, $new_ppn_now);
  617.             } else if ($materai == "no") {
  618.                 $new_materai = 0;
  619.             }
  620.             $pph23   = $this->check_pph23($client_code, $sub_total_now);
  621.                        
  622.                         if ($bhp == "yes") {
  623.                                 $bhp_Edit = ($sub_total_now ) * 0.005; //+  $new_ppn_now
  624.                                 $bhp_manual_e = "yes";
  625.                         } else {
  626.                                 $bhp_Edit = 0;
  627.                                 $bhp_manual_e = "no";
  628.                         }
  629.  
  630.                         if ($uso == "yes") {
  631.                                 $uso_Edit = ($sub_total_now ) * 0.0125; //+  $new_ppn_now
  632.                                 $uso_manual_e = "yes";
  633.                         } else {
  634.                                 $uso_Edit = 0;
  635.                                 $uso_manual_e = "no";
  636.                         }
  637.  
  638.            
  639.             $json = [
  640.                 "detail" => [
  641.                     "info"  => [
  642.                         "from" => "PT Sprint Asia",
  643.                         "recipient"    => $client_name,
  644.                         "reff_no"      => '',
  645.                         "cust_id"      => $cust_id,
  646.                         "sales_person" => ''
  647.                     ],
  648.                     "data"  => array($item),
  649.                     "total" => [
  650.                         "quantity"  => $sum_quantity,
  651.                         "unitprice" => $sum_unitprice,
  652.                         "sub_total" => ceil($sub_total_now),
  653.                         "with_ppn"  => floor($new_ppn_now),
  654.                                                 "bhp"       => $bhp_Edit,
  655.                         "uso"       => $uso_Edit,
  656.                         "with_pph"  => $pph23
  657.                         
  658.                     ]
  659.                 ],
  660.  
  661.             ];
  662.             $serialize = json_encode($json);
  663.            
  664.         }
  665.         // return $serialize;
  666.                 $Total = round($sub_total_now + $new_ppn_now + $new_materai - $pph23- $bhp_Edit- $uso_Edit);
  667.                 $Total_real = round($sub_total_real + $new_ppn_real + $new_materai - $pph23 - $bhp_Edit - $uso_Edit);
  668.         $revenue_coa = $sc_coa;
  669.  
  670.         
  671.         $datas_mi = DB::table('manual_invoice')->select('*')->where('id', $id)->get();
  672.         
  673.         $id_mi_old = $datas_mi[0]->id;
  674.         $client_code_old = $datas_mi[0]->client_code;
  675.         $division_code_old = $datas_mi[0]->division_code;
  676.         $dept_code_old = $datas_mi[0]->dept_code;
  677.         $desc_old = $datas_mi[0]->desc;
  678.         $vat_old = $datas_mi[0]->vat;
  679.         $materai_old = $datas_mi[0]->materai;
  680.         $bhp_old = $datas_mi[0]->bhp;
  681.         $uso_old = $datas_mi[0]->uso;
  682.         $total_old = $datas_mi[0]->total;
  683.  
  684.         $conn = DB::connection('account129');
  685.         $datas_clients = $conn->select("Select client_name FROM clients WHERE client_code = '$client_code_old' AND deleted_at is null ");
  686.         $clients_name = $datas_clients[0]->client_name;
  687.  
  688.         $datas_divisions = $conn->select("Select division_name FROM divisions WHERE division_code = '$division_code_old' AND deleted_at is null ");
  689.         $divisions_name = $datas_divisions[0]->division_name;
  690.  
  691.         $datas_depts = $conn->select("Select dept_name FROM depts WHERE dept_code = '$dept_code_old' AND deleted_at is null ");
  692.         $depts_name = $datas_depts[0]->dept_name;
  693.  
  694.         $result_update = "";
  695.         if ( $client_code_old != $client_code ) $result_update = $result_update . ". Client before : " . $clients_name;
  696.         if ( $division_code_old != $division_code ) $result_update = $result_update . ". Division before : " . $divisions_name;
  697.         if ( $dept_code_old != $dept_code ) $result_update = $result_update . ". Departement before : " . $depts_name;
  698.         if ( $desc_old != $request->input('desc') ) $result_update = $result_update . ". Description before : " . $desc_old;
  699.         if ( $vat_old != $vat ) $result_update = $result_update . ". VAT before : " . $vat_old;
  700.         if ( $materai_old != $materai ) $result_update = $result_update . ". Materai before : " . $materai_old;
  701.         if ( $bhp_old != $bhp_manual_e ) $result_update = $result_update . ". BHP before : " . $bhp_old;
  702.         if ( $uso_old != $bhp_manual_e ) $result_update = $result_update . ". USO before : " . $uso_old;
  703.         if ( $total_old != $Total_real ) $result_update = $result_update . ". Total before : " . $total_old;
  704.  
  705.         if( $result_update == "" ) $result_update = "No Data Updated."; else $result_update = $result_update . " has been Updated.";
  706.  
  707.         // Manual Invoice
  708.         $manual_invoice = [
  709.             "client_code"   => $client_code,
  710.             "dept_code"     => $dept_code,
  711.             "division_code" => $division_code,
  712.             "desc"          => $desc,
  713.             "vat"           => $vat,
  714.             "materai"       => $materai,
  715.                         "bhp"            => $bhp_manual_e,
  716.                         "uso"           => $uso_manual_e,
  717.             "total"         => $Total_real
  718.         ];
  719.  
  720.         try {
  721.             DB::table("db_sprint_ar.manual_invoice")->where('id', $id)->update($manual_invoice);
  722.         } catch (Exception $e) {
  723.             return ["code" => "_ERR_SYSTM", "error" => $e->getMessage()];
  724.         }
  725.  
  726.         $invoice = [
  727.             "client_code"             => $client_code,
  728.             "client_name"             => $client_name,
  729.             "division_code"           => $division_code,
  730.             "division_name"           => $division_name,
  731.             "division_description"    => $division_description,
  732.             "dept_code"               => $dept_code,
  733.             "dept_name"               => $dept_name,
  734.             "pic_id"                  => $pic_id,
  735.             // "inv_month"               => date("m"),
  736.             // "inv_year"                => date("yy"),
  737.             "unitprice"               => $sum_unitprice,
  738.             "quantity"                => $sum_quantity,
  739.             "sub_total"               => ceil($sub_total_now),
  740.             "ppn"                     => floor($new_ppn_now),
  741.                         "sub_total_real"          => ceil($sub_total_real),
  742.             "ppn_real"                => floor($new_ppn_real),
  743.             "materai"                 => $new_materai,
  744.             "pph23"                   => $pph23,
  745.             "total"                   => $Total,
  746.                         "total_real"              => $Total_real,
  747.             "serialize_data"          => $serialize,
  748.             "selisih"                 => 0,
  749.          //   "status"                  => $statusInv,
  750.             "addmanual"               => 1,
  751.             "id_manual"               => $id,
  752.             "pic_name"                => $pic_name,
  753.             "division_address"        => $division_address,
  754.             "bank_acc_number_sprint"  => $bank_acc_number_sprint,
  755.             "bank_acc_name_sprint"    => $bank_acc_name_sprint,
  756.             "bank_acc_number_client"  => $bank_acc_number_client,
  757.             "bank_acc_name_client"    => $bank_acc_name_client,
  758.             "bank_coa_sprint"         => $bank_coa_sprint,
  759.             "zahir_name"              => $zahir_name,
  760.             "client_npwp_no"          => $client_npwp_no,
  761.             "bank_name"               => $bank_name,
  762.             "client_coa"              => $client_coa,
  763.             "revenue_coa"             => $revenue_coa,
  764.             "email_client"            => $email_client,
  765.             // "pic_collection"          => $pic_collection_name,
  766.             "email_collection"        => $email_pic_collection,
  767.             // "pic_collection"          => $pic_pph23_name,
  768.             "email_pph23"             => $email_pic_pph23,
  769.             "pic_position"            => $pic_position,
  770.                 //      "update_print_date"               => $update_print_date,
  771.             "description"             => $description,
  772.             "ba"                      => $ba,
  773.             "fp"                      => $fp,
  774.             "softcopy"                => $softcopy,
  775.             "hardcopy"                => $hardcopy
  776.                        
  777.         ];
  778.  
  779.         // $invoice['client_code'] = (bool)$invoice['client_code']; // 0/1 -> PHP false/true
  780.  
  781.         // return json_encode($invoice); // {'id':true}
  782.  
  783.  
  784.         try {
  785.             DB::table("db_sprint_ar.invoice")->where('id_manual', $id)->update($invoice);
  786.             DB::table("db_sprint_ar.manual_invoice")->where('id', $id)->update(["total" => $Total]);
  787.  
  788.             $username                       = session()->get('profile')['name'];
  789.  
  790.             $saved_log_datas = preg_replace( "/\r|\n/", " ", json_encode($invoice) );
  791.             $saved_log_datas = str_replace("\\", "", $saved_log_datas);
  792.             DB::table('history')->insert([ 'client_name'=> $client_name, 'division_name'=> $division_name, 'dept_name'=> $dept_name, 'inp_desc2' => 'Update Manual Invoice , Id : ' . $id_mi_old . ' ' . $result_update , 'inp_desc' => $description, 'activity'=> "Update Manual Invoice", 'description'=> "Update values : ". $saved_log_datas , 'created_by' => $username , 'created_at' => now() ]);
  793.  
  794.         } catch (Exception $e) {
  795.             return ["code" => "_ERR_SYSTM", "error" => $e->getMessage()];
  796.         }
  797.  
  798.         // Insert to table manual_detail
  799.         for ($i = 0; $i < $length; $i++) {
  800.             $item_temp = $item_name[$i];
  801.             $manual_detail[] = [
  802.                 "id_manual"         => $id,
  803.                 "service"           => $service[$i],
  804.                 "quantity"          => $quantity[$i],
  805.                 "unit_price"        => $unitprice[$i],
  806.                 "desc"              => $item_temp, //$item_name[$i],
  807.                 "total"             => intval(str_replace(',', '', $total[$i]))
  808.             ];
  809.         }
  810.  
  811.         try {
  812.             DB::beginTransaction();
  813.             DB::table("db_sprint_ar.manual_detail")->where('id_manual', $id)->delete();
  814.             DB::table("db_sprint_ar.manual_detail")->insert($manual_detail);
  815.             DB::commit();
  816.         } catch (Exception $e) {
  817.             DB::rollBack();
  818.             return ["code" => "_ERR_SYSTM", "error" => $e->getMessage()];
  819.         }
  820.         return redirect(url('/manual-invoice'))->with('success');
  821.     }
  822.  
  823.     public function destroy($id)
  824.     {
  825.         try {
  826.             DB::beginTransaction();
  827.  
  828.             // delete manual invoice
  829.             $mi = ManualInvoice::find($id);
  830.  
  831.             $gets_mi = DB::table('invoice')->select('*')->where('id_manual', $id)->where('addmanual', '1')->get();
  832.             $client_name = $gets_mi[0]->client_name;
  833.             $division_name = $gets_mi[0]->division_name;
  834.             $dept_name = $gets_mi[0]->dept_name;
  835.             $description = $gets_mi[0]->description;
  836.             $totals = $gets_mi[0]->quantity;
  837.  
  838.             $username                       = session()->get('profile')['name'];
  839.             DB::table('history')->insert([ 'client_name'=> $client_name, 'division_name'=> $division_name, 'dept_name'=> $dept_name, 'inp_desc2' => 'Delete Manual Invoice, Id Inv ' . $id . ' , Client Name : ' . $client_name . ' , Division Name : ' . $division_name . ' , Quantity : ' . $totals . ' , Description : ' . $description , 'inp_desc'=> 'Delete Manual Invoice : '.$description . ' Success', 'activity'=> "Delete Manual Invoice", 'description'=> "Delete Manual Invoice id : [". $id . "] , Client Name : [".$client_name."] , Division Name : [".$division_name."] , Dept Name : [".$dept_name."] , Total : [".$totals."]  Success " , 'created_by' => $username , 'created_at' => now() ]);            
  840.             // DB::table('history')->insert([ 'activity'=> "Delete Manual Invoice", 'description'=> "Delete Manual Invoice id : [". $id . "] Success " , 'created_by' => $username , 'created_at' => now() ]);
  841.  
  842.             $mi->delete();
  843.  
  844.             // delete manual detail invoice
  845.             DB::table('manual_detail')->where('manual_detail.id_manual', $id)->delete();
  846.  
  847.             // Delete Invoice
  848.             DB::table('invoice')->where('id_manual', $id)->delete();
  849.  
  850.             DB::commit();
  851.             return response()->json(["success" => "Deleted id " . $id]);
  852.         } catch (Exception $e) {
  853.             DB::rollBack();
  854.             return response()->json(["error" => $e->getMessage()]);
  855.         }
  856.     }
  857.  
  858.    
  859.  
  860.     public function upload(Request $request)
  861.     {
  862.         date_default_timezone_set("Asia/Bangkok");
  863.         $dateNow = date('Y-m-d H:i:s');            
  864.         $username_login     = session()->get('profile')['name'];
  865.  
  866.                 if ($request->input('sc_upload_manual') == "" || $request->input('sc_upload_manual') == "-" || $request->input('sc_upload_manual') == null || $request->input('sc_upload_manual') == '') {
  867.                         return redirect()->back()->with('alert_warning', 'Please select a service category!');
  868.                 }
  869.  
  870.         if ($request->file('file_upload_manual') == "" || $request->file('file_upload_manual') == "-" || $request->file('file_upload_manual') == null || $request->file('file_upload_manual') == '') {
  871.                         return redirect()->back()->with('alert_warning', 'Please upload file!');
  872.                 }
  873.  
  874.         $service_category   = $request->input('sc_upload_manual');
  875.         $File               = $request->file('file_upload_manual');
  876.                 $revenue_coa        = $request->input('sc_upload_manual');
  877.         $nameFileUpload     = $File->getClientOriginalName();
  878.         $nameFile                      = explode("_", $nameFileUpload);
  879.         $client_code       = $nameFile[0];
  880.         $month              = $nameFile[1];
  881.         $fake_year          = $nameFile[2];
  882.                 $fake                          = explode(".", $fake_year);
  883.                 $year                          = $fake[0];
  884.         $path_name          = Storage::put('/upload_manual_invoice', $File);
  885.         $results                       = Excel::selectSheetsByIndex(0)->load($File)->get();
  886.         $header                        = $results->getHeading(); //get xls's header
  887.             $extension          = $File->getClientOriginalExtension();
  888.         $size               = $File->getSize();
  889.         $path               = $File->getRealPath();
  890.         $username                       = session()->get('profile')['name'];
  891.  
  892.         // echo "</br></br> \n service_category : "; print_r($service_category) ; die();
  893.  
  894.         $arrClient = $this->get_data_client_by_id($client_code);
  895.         if ($arrClient == null || $arrClient == '' || $arrClient == "") {
  896.             return redirect()->back()->with('alert_warning', 'Please Check Client Code Again. File Cannot be Executed.');;
  897.         }
  898.  
  899.         $client_coas                           = $arrClient['client_coa'];
  900.         $client_code                           = $arrClient['client_code'];
  901.         $client_names                          = $arrClient['client_name'];
  902.         $client_domicile_address       = $arrClient['client_domicile_address'];
  903.         $no_spk                                    = $arrClient['client_no_spk'];
  904.         $no_addendum_spk                   = $arrClient['client_no_addendum_spk']; 
  905.         $materai_client                        = $arrClient['materai'];
  906.         $lmbg                                      = $arrClient['lmbnegara'];
  907.         $max_amount                            = $arrClient['max_amount'];
  908.         $invoice_rule                          = $arrClient['invoice_rule'];
  909.  
  910.         $uploads_manuals = [];
  911.  
  912.         $conn_am = DB::connection('account129');
  913.  
  914.         if ($extension == "xlsx" || $extension == "xls") 
  915.         {
  916.             $datas = Excel::load($path)->get(); //load all of the data in sheet excel file
  917.             // return $datas;
  918.             // return count($datas);
  919.  
  920.             $headerRow = $datas->first()->keys()->toArray();
  921.  
  922.             $index = 0;
  923.  
  924.             $serialize = [];
  925.             $item_desc_temp = [];
  926.             $datasDivisi = [];
  927.             $rowsDiv = [];
  928.             $jmlhDiv = 0;
  929.             $arryDiv = 0;
  930.             $arryDivMin1 = 0;
  931.             $plusplus = 0;
  932.             $hx = 0;
  933.             $pic_name = '';
  934.  
  935.             //// Menggabungkan & mengklasifikasikan dengan values Division
  936.             foreach ($datas as $vals) 
  937.             {
  938.                 $vals_div = $vals['divisi'];
  939.  
  940.                 // echo "</br></br> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ";
  941.                 // echo "</br></br> \n vals_div : "; print_r($vals_div) ;
  942.  
  943.                 // echo "</br></br> \n rowsDiv sebelum : "; print_r($rowsDiv) ;
  944.  
  945.                 // echo "</br></br> \n datasDivisi sebelum : "; print_r($datasDivisi) ; echo "</br>";
  946.             
  947.  
  948.                 
  949.                 if ( in_array($vals_div, $rowsDiv) ) {
  950.  
  951.                     $checkDiv = array_keys($rowsDiv, $vals_div);
  952.                     $brsDiv = $checkDiv[0];
  953.  
  954.                     $countdDatasDivisi = count($datasDivisi[$brsDiv]); 
  955.  
  956.                     // echo "</br> \n -- checkDiv 1 : "; print_r($checkDiv) ;
  957.                     // echo "</br> \n -- brsDiv 1 : "; print_r($brsDiv) ;
  958.                     // echo "</br> \n -- countdDatasDivisi 1 : "; print_r($countdDatasDivisi) ;
  959.  
  960.                     $datasDivisi[$brsDiv][$countdDatasDivisi] = $vals ;
  961.  
  962.                 }
  963.                 else {
  964.                     array_push($rowsDiv,$vals_div);
  965.                     
  966.                     $checkDiv = array_keys($rowsDiv, $vals_div);
  967.                     $brsDiv = $checkDiv[0];
  968.  
  969.                     // echo "</br> \n -- checkDiv 2 : "; print_r($checkDiv) ;
  970.                     // echo "</br> \n -- brsDiv 2 : "; print_r($brsDiv) ;
  971.  
  972.                     $datasDivisi[$brsDiv][] = $vals ;
  973.                 }
  974.  
  975.                 // echo "</br></br> \n rowsDiv sesudah : "; print_r($rowsDiv) ;
  976.  
  977.                 // echo "</br></br> \n datasDivisi sesudah : "; print_r($datasDivisi) ;
  978.                 // echo "</br></br> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ";
  979.  
  980.             }
  981.  
  982.             // return $datasDivisi;
  983.             // echo "</br></br> \n datasDivisi FINAL : "; print_r($datasDivisi) ;
  984.             // echo "</br></br> \n datasDivisi FINAL : "; print_r($datasDivisi) ; die();
  985.             //// END mengabungkan values Division
  986.  
  987.             // echo "</br></br> \n Datas Divisi Testing : "; print_r($datasDivisi[0][1]['jenis_sms']) ;
  988.             // echo "</br></br> \n Count Divisi : "; print_r(count($datasDivisi)) ;
  989.             
  990.             $item_description = array();
  991.             $serialize = [];
  992.  
  993.             //// Men Serialize Data
  994.             for ($ij=0; $ij < count($datasDivisi) ; $ij++) 
  995.             {
  996.                 $division_name = '';
  997.                 $tot_traffict = 0;
  998.                 $sum_quantity = 0;
  999.                 $sum_uprice = 0;
  1000.                 $sum_total = 0;
  1001.                 $new_ppn = 0;
  1002.                 $new_ppns = 0;
  1003.  
  1004.                 $cDD = count($datasDivisi[$ij]);
  1005.                 // echo "</br></br> \n cDD : "; print_r($cDD) ;
  1006.  
  1007.  
  1008.  
  1009.                 $arrClient = $this->get_data_client_by_id($client_code);
  1010.                 if ($arrClient == null || $arrClient == '' || $arrClient == "") return redirect()->back()->with('alert_warning', 'Please Check Client Code Again. File Cannot be Executed.');
  1011.  
  1012.                 
  1013.  
  1014.                 //// Menghitung TOtal Trafict tot_traffict
  1015.                 for ($ih = 0; $ih < $cDD ; $ih++) 
  1016.                 {
  1017.  
  1018.                     $division_name    = $datasDivisi[$ij][$ih]['divisi'];
  1019.                     $produk           = $datasDivisi[$ij][$ih]['produk'];
  1020.  
  1021.                     if($service_category == '430000001') {$jenis_service = $datasDivisi[$ij][$ih]['jenis_layanan']; $service_code = 'W011'; } // for whatsapp
  1022.                     else if($service_category == '4102004') { $jenis_service = $datasDivisi[$ij][$ih]['jenis_layanan']; } // for SMS Pull
  1023.                             else if($service_category == '410000001') { $jenis_service = $datasDivisi[$ij][$ih]['jenis_layanan']; } // for SMS PUSH
  1024.                     else if($service_category == '450000001') { $jenis_service = $datasDivisi[$ij][$ih]['jenis_layanan']; } // for Email PUSH
  1025.                     else return redirect()->back()->with('alert_warning', 'Service Category Not Available. Please Check Service Category !!! ');
  1026.  
  1027.                     $countDatas = count($datasDivisi[$ij][$ih]);
  1028.  
  1029.                     if (isset($datasDivisi[$ij][$ih]['international'])) {
  1030.                         $countDatasHeader = $countDatas;
  1031.                     } else {
  1032.                         $countDatasHeader = $countDatas-1;
  1033.                     }
  1034.                         // if ($countDatasHeader == 9) $countDatasHeader--;
  1035.                     for ($ik = 3; $ik < $countDatasHeader ; $ik++) {
  1036.                         $headers = [ucfirst($header[$ik])];
  1037.                         $heads = $headers[0];
  1038.                         $heads_lowers = strtolower($headers[0]);
  1039.                         $qtty =  'qty_' . str_replace(" ","_", $heads);
  1040.                        
  1041.                         if($headers[0] != "") {
  1042.                             $qtys = $datasDivisi[$ij][$ih][$heads_lowers];
  1043.                             $contains_qtty = str_contains($qtys, '-');
  1044.                             if($contains_qtty == true){
  1045.                                 continue;
  1046.                             }
  1047.                             if( $heads == 'Hutchinson' ) $tot_traffict = $tot_traffict + $qtys;
  1048.                             if( $heads == 'Indosat' ) $tot_traffict = $tot_traffict + $qtys;
  1049.                             if( $heads == 'Telkomsel' ) $tot_traffict = $tot_traffict + $qtys;
  1050.                             if( $heads == 'Smartfren' ) $tot_traffict = $tot_traffict + $qtys;
  1051.                             if( $heads == 'Excelcom' ) $tot_traffict = $tot_traffict + $qtys;
  1052.                             if( $heads == 'Email Blast' ) $tot_traffict = $tot_traffict + $qtys;
  1053.                             else $tot_traffict = $tot_traffict;                            
  1054.                         }
  1055.                     }
  1056.                 }
  1057.                 // End Menghitung TOtal Trafict tot_traffict
  1058.                 //dd($tot_traffict);
  1059.                 // For generate itemdesc
  1060.                 for ($ih = 0; $ih < $cDD ; $ih++) 
  1061.                 {
  1062.                     $division_name    = $datasDivisi[$ij][$ih]['divisi'];
  1063.                     $produk           = $datasDivisi[$ij][$ih]['produk'];
  1064.  
  1065.                     if($service_category == '430000001') $jenis_service = $datasDivisi[$ij][$ih]['jenis_layanan']; // for whatsapp
  1066.                     else if($service_category == '4102004') { $jenis_service = $datasDivisi[$ij][$ih]['jenis_layanan']; } // for SMS Pull
  1067.                             else if($service_category == '410000001') $jenis_service = $datasDivisi[$ij][$ih]['jenis_layanan']; // for SMS PUSH
  1068.                     else if($service_category == '450000001') { $jenis_service = $datasDivisi[$ij][$ih]['jenis_layanan']; } // for Email PUSH
  1069.                     else return redirect()->back()->with('alert_warning', 'Service Category Not Available. Please Check Service Category !!! ');
  1070.  
  1071.                     if ($division_name == null || $division_name == '' || $division_name == "" || $division_name == "eof")  break;
  1072.                     
  1073.                     $client_coas                = $arrClient['client_coa'];
  1074.                     $client_code                = $arrClient['client_code'];
  1075.                     $client_names               = $arrClient['client_name'];
  1076.                     $client_domicile_address    = $arrClient['client_domicile_address'];
  1077.                     $materai_client             = $arrClient['materai'];
  1078.                     $lmbg                       = $arrClient['lmbnegara'];
  1079.                     $max_amount                 = $arrClient['max_amount'];
  1080.                     $invoice_rule               = $arrClient['invoice_rule'];
  1081.  
  1082.                     $countDatas = count($datasDivisi[$ij][$ih]);
  1083.  
  1084.                     if (isset($datasDivisi[$ij][$ih]['international'])) {
  1085.                         $countDatasHeader = $countDatas;
  1086.                     } else {
  1087.                         $countDatasHeader = $countDatas-1;
  1088.                     }
  1089.                         // dd($countDatasHeader);
  1090.                         // if ($countDatasHeader == 9) $countDatasHeader--;
  1091.                     for ($ik = 3; $ik < $countDatasHeader ; $ik++) 
  1092.                     {
  1093.                         $headers = [ucfirst($header[$ik])];
  1094.                         $heads = $headers[0];
  1095.                         $qtty = explode("_",$heads);
  1096.                         $itemsName = ucwords(join(" ", $qtty));
  1097.  
  1098.                         $headsEmail = explode(" ", $itemsName);
  1099.                         $email_blast = $headsEmail[0];
  1100.                         $email_lower = lcfirst($email_blast);
  1101.  
  1102.                         $heads_lowers = strtolower($headers[0]);
  1103.  
  1104.                         if($jenis_service == "SMS Premium" && $heads != 'International') {
  1105.                             $itemsName = $heads . " Premium"; 
  1106.                         } else {
  1107.                             $itemsName = $itemsName;
  1108.                         }
  1109.                         
  1110.                         $qtys = $datasDivisi[$ij][$ih][$heads_lowers];
  1111.                         $contains = str_contains($qtys, '-');
  1112.  
  1113.                         if($contains == true) {
  1114.                             $check = "true";
  1115.                         } else {
  1116.                             $check = "false";
  1117.                         }
  1118.  
  1119.                         if($check == "false")
  1120.                         {
  1121.                             $unitprice = 0;
  1122.                             if ($service_category == '430000001') {
  1123.                                 $getClientsPrice = $conn_am->select("Select a.*, b.service_name 
  1124.                                                             FROM db_sprint_account.client_price AS a 
  1125.                                                             LEFT JOIN `services` AS b ON a.service_code = b.service_code 
  1126.                                                             WHERE a.client_code = '".$client_code."' AND b.service_name = '".$jenis_service."' AND a.provider_code = '".$itemsName."' LIMIT 1");
  1127.                             } else if ($service_category == '450000001') {
  1128.                                 $getClientsPrice = $conn_am->select("Select a.*, b.service_name 
  1129.                                                             FROM db_sprint_account.client_price AS a 
  1130.                                                             LEFT JOIN `services` AS b ON a.service_code = b.service_code 
  1131.                                                             WHERE a.client_code = '".$client_code."' AND b.service_name = '".$email_blast."' AND a.provider_code = '".$email_blast."' LIMIT 1");
  1132.                             } else {
  1133.                                 $getClientsPrice = $conn_am->select("Select a.*, b.service_name 
  1134.                                                             FROM db_sprint_account.client_price AS a 
  1135.                                                             LEFT JOIN `services` AS b ON a.service_code = b.service_code 
  1136.                                                             WHERE a.client_code = '".$client_code."' AND b.service_name = '".$jenis_service."' AND a.provider_code = '".$heads."' LIMIT 1");
  1137.  
  1138.                             }
  1139.                             
  1140.                             if( !empty($getClientsPrice) )
  1141.                             {
  1142.                                 $prc_type   = $getClientsPrice[0]->price_type;
  1143.                                 $hrgPrc     = $getClientsPrice[0]->price;
  1144.                                 $arryPrice  = json_decode($hrgPrc);
  1145.                                 $countPrice = count($arryPrice);
  1146.                                 $vats       = $getClientsPrice[0]->vat;
  1147.                             } else {
  1148.                                 $prc_type   = 1;
  1149.                                 $arryPrice  = [0];
  1150.                                 $countPrice = count($arryPrice);
  1151.                                 $vats       = 'exclude';
  1152.                             }
  1153.                             
  1154.                             if ($prc_type == 1) {
  1155.                                 $unitprice = $arryPrice[0];
  1156.                             } else if ($prc_type == 2) {
  1157.                                 $unitprice = round(($arryPrice[0] * $arryPrice[1] ));
  1158.                             } else if ($prc_type == 3) {
  1159.                                 for ($p = 0; $p < $countPrice ; $p++) 
  1160.                                 {
  1161.                                     $trfct = intval($tot_traffict);
  1162.                                     $tr1 = intval($arryPrice[$p][0]);
  1163.                                     $tr2 = intval($arryPrice[$p][1]);
  1164.                                     $hrg = intval($arryPrice[$p][2]);
  1165.  
  1166.                                     if ($trfct >= $tr1 && $trfct <= $tr2 ) {
  1167.                                         $unitprice = $hrg;
  1168.                                     } else {
  1169.                                         if ($unitprice > 0 ) {
  1170.                                             $unitprice = $unitprice;
  1171.                                         } else {
  1172.                                             $unitprice = 0;
  1173.                                         }
  1174.                                     }
  1175.                                 }
  1176.                             }
  1177.  
  1178.                                 
  1179.                             $tot_hrg = $unitprice * $qtys;
  1180.  
  1181.                             $item_desc_temp[$ij][$ih][] = [     
  1182.                                 "item_name" => $itemsName, 
  1183.                                 "quantity" => $qtys, 
  1184.                                 "unitprice" => $unitprice, 
  1185.                                 "total" => $tot_hrg,
  1186.                             ];
  1187.  
  1188.                             if($vats == 'exclude') {
  1189.                                 $new_ppns = floor($tot_hrg * 0.11);
  1190.                             } else {
  1191.                                 $new_ppns = 0;
  1192.                             }
  1193.  
  1194.                             $sum_quantity = $sum_quantity + $qtys;
  1195.                             $sum_uprice = $sum_uprice + $unitprice;
  1196.  
  1197.                             $sum_total = $sum_total + $tot_hrg;
  1198.                             $new_ppn  = $new_ppn + $new_ppns;
  1199.  
  1200.                         } else {
  1201.                             continue;
  1202.                         }
  1203.  
  1204.                     }
  1205.  
  1206.                     $data_items[$ij][] = [
  1207.                         "item"      => $produk,
  1208.                         "item_desc" => $item_desc_temp[$ij][$ih]
  1209.  
  1210.                     ];
  1211.  
  1212.                 }
  1213.  
  1214.                 $json = [
  1215.                             "detail" => [
  1216.                                 "info"  => [
  1217.                                     "from"         => "PT Sprint Asia",
  1218.                                     "recipient"    => $client_names,
  1219.                                     "reff_no"      => '',
  1220.                                     "cust_id"      => '',
  1221.                                     "sales_person" => ''
  1222.                                 ],
  1223.                                 "data"  => array($data_items[$ij]),
  1224.                                 "total" => [
  1225.                                     "quantity"  => $sum_quantity,
  1226.                                     "unitprice" => $sum_uprice,
  1227.                                     "sub_total" => ceil($sum_total),
  1228.                                     "with_ppn"  => floor($new_ppn),
  1229.                                     "with_pph"  => 0
  1230.  
  1231.                                 ]
  1232.                             ],
  1233.  
  1234.                         ];
  1235.  
  1236.                 $serialize = json_encode($json);
  1237.  
  1238.  
  1239.                 $data_div           = $this->get_data_division_by_name_and_client_code($division_name, $client_code);
  1240.                 if ($data_div == null || $data_div == '' || $data_div == "") {
  1241.                     return redirect()->back()->with('alert_warning', 'Please Check Data Divisi ' . $division_name .' Again. File Cannot be Executed.');;
  1242.                 }
  1243.  
  1244.                 
  1245.                 //Division
  1246.                 $division_names         = $data_div['division_name'];
  1247.                 $division_code          = $data_div['division_code'];
  1248.                 $bank_acc_number_sprint = $data_div['division_bank_acc_number_sprint'];
  1249.                 $bank_acc_name_sprint   = $data_div['division_bank_acc_name_sprint'];
  1250.                 $bank_coa_sprint        = $data_div['division_bank_coa_sprint'];
  1251.                 $zahir_name             = $data_div['division_zahir_name'];
  1252.                 $client_npwp_no         = $data_div['division_npwp'];
  1253.                 $bank_acc_name_client   = $data_div['division_bank_account_name'];
  1254.                 $bank_acc_number_client = $data_div['division_bank_account_number'];
  1255.                 $bank_name              = $data_div['division_bank_name_sprint'];
  1256.                 $client_coa             = $data_div['division_bank_name_sprint'];
  1257.                 $division_address       = $data_div['division_address'];
  1258.                 $division_invoice_rule  = $data_div['division_invoice_rule']; 
  1259.                 $division_max_amount    = $data_div['division_max_amount'];
  1260.                 $division_description   = $data_div['division_description'];
  1261.                 $ba                     = $data_div['berita_acara'];
  1262.                 if($ba == 1){$ba = "Yes";}else{$ba = "No";}
  1263.                 $fp                     = $data_div['faktur_pajak'];
  1264.                 if($fp == 1){$fp = "Yes";}else{$fp = "No";}
  1265.                 $softcopy               = $data_div['soft_copy'];
  1266.                 if($softcopy == 1){$softcopy = "Yes";}else{$softcopy = "No";}
  1267.                 $hardcopy               = $data_div['hard_copy'];
  1268.                 if($hardcopy == 1){$hardcopy = "Yes";}else{$hardcopy = "No";}
  1269.                 
  1270.                 $dept_code              = "$division_code-00";
  1271.                 $data_dept              = $this->get_data_dept_by_id($dept_code);
  1272.                 
  1273.                 if ($data_dept == null || $data_dept == '' || $data_dept == "") {
  1274.                     return redirect()->back()->with('alert_warning', 'Please Check Data Department '.$dept_code.' Again. File Cannot be Executed.');
  1275.                 }
  1276.                 
  1277.                 $dept_code = $data_dept['dept_code'];
  1278.                 $dept_names = $data_dept['dept_name'];
  1279.  
  1280.                 //PIC 
  1281.                 $data_pic      = $this->get_data_pics_by_id($division_code);
  1282.                 if ($data_pic == null || $data_pic == '' || $data_pic == "") {
  1283.                     return redirect()->back()->with('alert_warning', 'Please Check Data PIC from Division '.$division_code.'. File Cannot be Executed.');
  1284.                 }
  1285.  
  1286.                 $pic_id       = $data_pic['pic_id'];
  1287.                 $pic_name     = $data_pic['pic_name'];
  1288.                 $email_client = $data_pic['pic_email'];
  1289.                 $pic_position = $data_pic['pic_position'];
  1290.  
  1291.                 $data_pic_collection = $this->get_data_pic_collection($division_code);
  1292.  
  1293.                 if ($data_pic_collection == null || $data_pic_collection == '' || $data_pic_collection == "") {
  1294.                     $pic_collection_name        = "";
  1295.                     $email_pic_collection       = $email_client;
  1296.                 }
  1297.                 else {
  1298.                     if( !empty ($data_pic_collection['pic_name']) ) $pic_collection_name        = $data_pic_collection['pic_name'];
  1299.                     else return redirect()->back()->with('alert_warning', 'Please Check pic_type_id at Pic Name ' . $division_name .' Again. File Cannot be Executed.');;
  1300.                     $email_pic_collection       = $data_pic_collection['pic_email'];
  1301.  
  1302.                 }
  1303.  
  1304.                 $data_pic_pph23 = $this->get_data_pic_pph23($division_code);
  1305.                 echo "</br></br> \n ~~~~~ data_pic_pph23 : "; print_r($data_pic_pph23) ; echo "~~~~~  </br> \n ";
  1306.  
  1307.                 if ($data_pic_pph23 == null || $data_pic_pph23 == '' || $data_pic_pph23 == "") {
  1308.                     $pic_pph23_name        = "";
  1309.                     $email_pic_pph23       = $email_client;
  1310.                 }
  1311.                 else {
  1312.                     $pic_pph23_name        = $data_pic_pph23['pic_name'];
  1313.                     $email_pic_pph23       = $data_pic_pph23['pic_email'];
  1314.                 }
  1315.  
  1316.                 $sub_total = ceil($sum_total);
  1317.                 $amount_invoice_rule = $sub_total + $new_ppn;
  1318.                 
  1319.                 if($new_ppn > 0){
  1320.                     $vats = "exclude";
  1321.                 }else if($new_ppn <= 0){
  1322.                     $vats = "include";
  1323.                 }
  1324.                 
  1325.                 //cek BUMN/Lembaga
  1326.                 $real                   = $this->real_field($lmbg, $invoice_rule, $max_amount, $amount_invoice_rule, $sub_total, $vats);
  1327.  
  1328.                 $sub_total_now          = $real[0];
  1329.                 $sub_total_real         = $real[1];
  1330.                 $new_ppn_now            = $real[2];
  1331.                 $new_ppn_real           = $real[3];
  1332.  
  1333.                 $new_materai = 0;
  1334.  
  1335.                 if ($materai_client == 1) $new_materai = $this->check_materai($sub_total_now, $new_ppn_now);
  1336.                 else if ($materai_client     == 0) $new_materai = 0;
  1337.  
  1338.                 $pph23   = $this->check_pph23($client_code, $sub_total_now);
  1339.  
  1340.                 $Total = round($sub_total_now + $new_ppn_now + $new_materai - $pph23);
  1341.                 $Total_real = round($sub_total_real + $new_ppn_real + $new_materai - $pph23);
  1342.                 $now = \Carbon\Carbon::now();
  1343.                 $descManual = "-";
  1344.                 
  1345.                 if($materai_client == 1 ) $materai = "yes"; else $materai = "no";
  1346.                 
  1347.  
  1348.                 $invXls[] = array(
  1349.                     "client_code"               => $client_code,
  1350.                     "client_name"               => $client_names,
  1351.                     "division_code"             => $division_code,
  1352.                     "division_name"             => $division_names,
  1353.                     "division_description"      => $division_description,
  1354.                     "dept_code"                 => $dept_code,
  1355.                     "dept_name"                 => $dept_names,
  1356.                     "pic_id"                    => $pic_id,
  1357.                     "pic_name"                  => $pic_name,
  1358.                     "pic_position"              => $pic_position,
  1359.                     "inv_month"                 => $month,  //date("m"),
  1360.                     "inv_year"                  => $year, //date("yy"),
  1361.                     "unitprice"                 => $sum_uprice,
  1362.                     "quantity"                  => $sum_quantity,
  1363.                     "sub_total"                 => ceil($sub_total_now),
  1364.                     "ppn"                       => floor($new_ppn_now),
  1365.                     "sub_total_real"            => ceil($sub_total_real),
  1366.                     "ppn_real"                  => floor($new_ppn_real),
  1367.                     "materai"                   => $new_materai,
  1368.                     "pph23"                     => $pph23,
  1369.                     "total"                     => $Total,
  1370.                     "total_real"                => $Total_real,
  1371.                     "serialize_data"            => $serialize,
  1372.                     "selisih"                   => 0,
  1373.                     "status"                    => 0,
  1374.                     "addmanual"                 => 2,
  1375.                     "division_address"          => $division_address,
  1376.                     "bank_acc_number_sprint"    => $bank_acc_number_sprint,
  1377.                     "bank_acc_name_sprint"      => $bank_acc_name_sprint,
  1378.                     "bank_acc_number_client"    => $bank_acc_number_client,
  1379.                     "bank_acc_name_client"      => $bank_acc_name_client,
  1380.                     "bank_coa_sprint"           => $bank_coa_sprint,
  1381.                     "zahir_name"                => $zahir_name,
  1382.                     "client_npwp_no"            => $client_npwp_no,
  1383.                     "bank_name"                 => $bank_name,
  1384.                     "client_coa"                => $client_coas,
  1385.                     "revenue_coa"               => $revenue_coa,
  1386.                     "email_client"              => $email_client,
  1387.                     "email_collection"          => $email_pic_collection,
  1388.                     "email_pph23"               => $email_pic_pph23,
  1389.                     "description"               => $produk,
  1390.                     "created_at"                => $now,
  1391.                     "created_by"                => $username ." - Upload Manual Invoice",
  1392.                     "id_collection"             =>'[""]',
  1393.                     "ba"                        => $ba,
  1394.                     "fp"                        => $fp,
  1395.                     "softcopy"                  => $softcopy,
  1396.                     "hardcopy"                  => $hardcopy
  1397.  
  1398.                 );
  1399.             }
  1400.             // END Men Serialize Data
  1401.  
  1402.             // return $invXls;
  1403.             if (!empty($invXls)) {
  1404.                 DB::table('db_sprint_ar.invoice')->insert($invXls);
  1405.  
  1406.                 $username                       = session()->get('profile')['name'];
  1407.                 $now                            = Carbon\Carbon::now('Asia/Jakarta');
  1408.                 
  1409.                 $saved_log_datas = preg_replace( "/\r|\n/", " ", json_encode($invXls) );
  1410.                 $saved_log_datas = str_replace("\\", "", $saved_log_datas);
  1411.                 DB::table('history')->insert([ 'client_name'=> $client_names, 'division_name'=> $division_names, 'dept_name'=> $dept_names, 'inp_desc2' => "Upload Manual Invoice, Clients Name : ".$client_names." , Divisions Name : ".$division_names." , Quantity : ".$sum_quantity. " , Description : ". $produk , 'inp_desc'=> $produk, 'activity'=> "Upload Manual Invoice", 'description'=> "Upload values : ". $saved_log_datas , 'created_by' => $username , 'created_at' => $now ]);
  1412.                 
  1413.             }
  1414.             DB::commit();
  1415.             $up_manualInvoice   = [
  1416.                 "path_name" => $path_name,
  1417.                 "file_name" => $nameFileUpload,
  1418.                 "size"      => $size,
  1419.                 "created_at"    => now()
  1420.  
  1421.             ];
  1422.             
  1423.             $insert_upload_ = DB::table('upload_manual_invoice')->insert($up_manualInvoice);
  1424.             return redirect()->back()->with('alert_import', 'Import file success');
  1425.  
  1426.         } 
  1427.         else 
  1428.         {
  1429.             return redirect()->back()->with('alert_warning', 'Something wrong, excel only');
  1430.         }
  1431.                
  1432.         }
  1433.  
  1434.         public function upload_tes(Request $request)
  1435.     {
  1436.                  try {
  1437.                         if ($request->input('sc_upload_manual') == "" || $request->input('sc_upload_manual') == "-" || $request->input('sc_upload_manual') == null || $request->input('sc_upload_manual') == '') {
  1438.                                 //return redirect()->back()->with('alert', 'Service Category Tidak Dapat Dipilih. Silahkan Periksa di Account Management. Data Tidak Tersimpan.');
  1439.                                         return redirect('/upload-manual-invoice')->with('error', 'Service Category Tidak Dapat Dipilih. Silahkan Periksa di Account Management. Data Tidak Tersimpan.');
  1440.                         }
  1441.                        
  1442.             $File               = $request->file('file_upload_manual');
  1443.             $revenue_coa        = $request->input('sc_upload_manual');
  1444.             
  1445.            
  1446.             $nameFileUpload     = $File->getClientOriginalName();
  1447.             $path_name          = Storage::put('/upload_manual_invoice', $File);
  1448.             $size               = $File->getSize();
  1449.             
  1450.             $nameFile = explode("_", $nameFileUpload);
  1451.             // return $nameFile;
  1452.  
  1453.             $client_code       = $nameFile[0];
  1454.             $month              = $nameFile[1];
  1455.             $fake_year          = $nameFile[2];
  1456.                        
  1457.                         $fake = explode(".", $fake_year);
  1458.                        
  1459.                         $year = $fake[0];
  1460.  
  1461.             $results = Excel::selectSheetsByIndex(0)->load($File)->get();
  1462.  
  1463.             $header = $results->getHeading(); //get xls's header
  1464.  
  1465.                     $extension          = $File->getClientOriginalExtension();
  1466.             $size               = $File->getSize();
  1467.             $path               = $File->getRealPath();
  1468.             // return $path ;
  1469.  
  1470.             if ($extension == "xlsx" || $extension == "xls") {
  1471.                 $datas = Excel::load($path)->get(); //load all of the data in sheet excel file
  1472.                 $index = 0;
  1473.                 // return $datas;
  1474.                                 if ($datas->count() != 0) {
  1475.                    
  1476.                     DB::beginTransaction();
  1477.                     $invXls = array();
  1478.                     foreach ($datas as $data) {
  1479.  
  1480.                         // try {
  1481.                             $division_name            = $data['divisi'];
  1482.                             $item_name                = $data['produk'];
  1483.                             $service_name             = $data['jenis_sms'];
  1484.  
  1485.                             $qty_Hutchinson           = $data[$header[3]];
  1486.                             $qty_Indosat              = $data[$header[4]];
  1487.                             $qty_Telkomsel            = $data[$header[5]];
  1488.                             $qty_Smartfren            = $data[$header[6]];
  1489.                             $qty_Xl                   = $data[$header[7]];
  1490.                                                         // $qty_Inter                = $data[$header[8]];
  1491.                            // $qty_Satelindo            = $data[$header[9]];
  1492.                                                        
  1493.                                                          
  1494.                                                         if ($division_name == null || $division_name == '' || $division_name == "" || $division_name == "eof") {
  1495.                                 break;
  1496.                              }
  1497.  
  1498.                             //  if ($end_of_per == "eof" || $channel_1 == "eof" || $channel_2 == "eof" || $channel_4 == "eof") {
  1499.                             //     continue;
  1500.                             //  }
  1501.                                                          
  1502.                             $temp_item_desc = [];
  1503.                             for ($q = 3; $q < 8; $q++) {
  1504.                                 if ($data[$header[$q]] != null) {
  1505.  
  1506.                                     $uppercased = Str::ucfirst($header[$q]); //set the first alfabet as an uppercase 
  1507.                                     $temp_item_desc[] = [$uppercased, $data[$header[$q]]];
  1508.                                 }
  1509.                             }
  1510.                                                        
  1511.                             $arrClient = $this->get_data_client_by_id($client_code);
  1512.                                                         if ($arrClient == null || $arrClient == '' || $arrClient == "") {
  1513.                                                                 return redirect()->back()->with('alert', 'Please Check Client Code Again. File Cannot be Executed.');;
  1514.                             }
  1515.                             // return $arrClient;
  1516.                                                        
  1517.                             $client_coas                           = $arrClient['client_coa'];
  1518.                             $client_code                           = $arrClient['client_code'];
  1519.                             $client_names                          = $arrClient['client_name'];
  1520.                             $client_domicile_address       = $arrClient['client_domicile_address'];
  1521.                             $materai_client                        = $arrClient['materai'];
  1522.                                                         $lmbg                                      = $arrClient['lmbnegara'];
  1523.                                                         $max_amount                            = $arrClient['max_amount'];
  1524.                                                         $invoice_rule                          = $arrClient['invoice_rule'];
  1525.  
  1526.                             $sum_quantity                          = 0;
  1527.                             $sum_uprice                            = 0;
  1528.                             $sum_total                                     = 0;
  1529.                             $new_ppn                                       = 0;
  1530.                                                        
  1531.                             $item_d = [];
  1532.                             foreach ($temp_item_desc as $temp_id) {
  1533.                                 
  1534.                                                                 $arrServiceSMS = $this->get_data_service_sms($service_name);
  1535.                                                                
  1536.                                                                 if ($arrServiceSMS == null || $arrServiceSMS == '' || $arrServiceSMS == "") {
  1537.                                                                         return redirect()->back()->with('alert', 'Please Check Data Jenis SMS Again. File Cannot be Executed.');;
  1538.                                                                 }
  1539.  
  1540.                                                                 $service_code = $arrServiceSMS['service_code'];
  1541.                                                                
  1542.                                                                 if($temp_id[0] == "Hutchinson" && $service_code == "S101"){ //s101 premium - s102 reguler
  1543.                                                                         $service_code = "S102";
  1544.                                                                 }else if($temp_id[0] == "Smartfren" && $service_code == "S101"){
  1545.                                                                         $service_code = "S102";
  1546.                                                                 }
  1547.                                                                 else if($temp_id[0] == "Hutchinson" && $service_code == "S102"){
  1548.                                                                         $service_code = "S102";
  1549.                                                                 }else if($temp_id[0] == "Smartfren" && $service_code == "S102"){
  1550.                                                                         $service_code = "S102";
  1551.                                                                 }                                              
  1552.                                                                
  1553.                                 $arrPrice = $this->get_data_price($client_code, $temp_id[0], $service_code); 
  1554.                                 
  1555.                                                                 if ($arrPrice == null || $arrPrice == '' || $arrPrice == "") {
  1556.                                                                         return redirect()->back()->with('alert', 'Please Check Data Provider '.$temp_id[0].' Again. File Cannot be Executed.');;
  1557.                                                                 }
  1558.                                 // return $arrPrice;
  1559.                                                                
  1560.                                                                 $price_type     = $arrPrice['price_type'];
  1561.                                 $vat            = $arrPrice['vat'];
  1562.                                 $tmp_uprice     = $arrPrice['price'];
  1563.                                 $price          = json_decode($tmp_uprice);
  1564.                                 
  1565.                                                        
  1566.                                 if ($price_type == 1) {
  1567.                                     $p          = $price[0];
  1568.                                     $real_price = (int)$p;
  1569.                                 } else if ($price_type == 2) {
  1570.                                     $p          = $price[0];
  1571.                                     $amount     = 0;
  1572.                                     $real_price = (int)$p * $amount;
  1573.                                 } else if ($price_type == 3) {
  1574.                                     $traffict   = $temp_id[1];
  1575.  
  1576.                                     for($p = 0; $p < count($price); $p++){
  1577.                                         $from   = $price[$p][0]; 
  1578.                                         $to     = $price[$p][1];
  1579.                                         // $price  = $price[$p][2]; 
  1580.                                         
  1581.                                         if($traffict > $from && $traffict <= $to){
  1582.                                             $real_price = $price[$p][2];
  1583.                                         }else{
  1584.                                             continue;
  1585.                                         }
  1586.                                     }
  1587.  
  1588.                                 }else if($price_type == 4){
  1589.                                     $traffict   = $temp_id[1];
  1590.  
  1591.                                     for($p = 0; $p < count($price); $p++){
  1592.                                         $from   = $price[$p][0]; 
  1593.                                         $to     = $price[$p][1];
  1594.                                         // $price  = $price[$p][2]; 
  1595.                                         
  1596.                                         if($traffict > $from && $traffict <= $to){
  1597.                                             $real_price = $traffict * $price[$p][2];
  1598.                                         }else{
  1599.                                             continue;
  1600.                                         }
  1601.                                     }
  1602.                                 }
  1603.  
  1604.                                
  1605.                                 $tot = $temp_id[1] * $real_price;
  1606.                                                                
  1607.                                 $item_d[] = [
  1608.                                     "item_name" => $temp_id[0],
  1609.                                     "quantity"  => $temp_id[1],
  1610.                                     "unitprice" => $real_price,
  1611.                                     "total"     => $tot
  1612.                                 ];
  1613.  
  1614.                                 $sum_quantity   += $temp_id[1];
  1615.  
  1616.                                 $sum_uprice     += $real_price;
  1617.                                 $sum_total      += $tot;
  1618.                                                                
  1619.                                                                 $new_ppns       = $this->check_ppn($vat, $tot);
  1620.                                                                 $new_ppn        += $new_ppns;
  1621.                             }
  1622.  
  1623.                             $it = [
  1624.                                 "item"      => $data['produk'],
  1625.                                 "item_desc" => $item_d
  1626.                             ];
  1627.  
  1628.                             $json = [
  1629.                                 "detail" => [
  1630.                                     "info"  => [
  1631.                                         "from"         => "PT Sprint Asia",
  1632.                                         "recipient"    => $client_names,
  1633.                                         "reff_no"      => '',
  1634.                                         "cust_id"      => '',
  1635.                                         "sales_person" => ''
  1636.                                     ],
  1637.                                     "data"  => array(array($it)),
  1638.                                     "total" => [
  1639.                                         "quantity"  => $sum_quantity,
  1640.                                         "unitprice" => $sum_uprice,
  1641.                                         "sub_total" => ceil($sum_total),
  1642.                                         "with_ppn"  => floor($new_ppn),
  1643.                                         "with_pph"  => 0
  1644.  
  1645.                                     ]
  1646.                                 ],
  1647.  
  1648.                             ];
  1649.                             $serialize = json_encode($json);
  1650.                             
  1651.                            
  1652.                             $data_div           = $this->get_data_division_by_name_and_client_code($division_name, $client_code);
  1653.                                                         if ($data_div == null || $data_div == '' || $data_div == "") {
  1654.                                                                 return redirect()->back()->with('alert', 'Please Check Data Divisi ' . $division_name .' Again. File Cannot be Executed.');;
  1655.                                                         }
  1656.                             
  1657.                             //Division
  1658.                             $division_names         = $data_div['division_name'];
  1659.                             $division_code          = $data_div['division_code'];
  1660.                             $bank_acc_number_sprint = $data_div['division_bank_acc_number_sprint'];
  1661.                             $bank_acc_name_sprint   = $data_div['division_bank_acc_name_sprint'];
  1662.                             $bank_coa_sprint        = $data_div['division_bank_coa_sprint'];
  1663.                             $zahir_name             = $data_div['division_zahir_name'];
  1664.                             $client_npwp_no         = $data_div['division_npwp'];
  1665.                             $bank_acc_name_client   = $data_div['division_bank_account_name'];
  1666.                             $bank_acc_number_client = $data_div['division_bank_account_number'];
  1667.                             $bank_name              = $data_div['division_bank_name_sprint'];
  1668.                             $client_coa             = $data_div['division_bank_name_sprint'];
  1669.                             $division_address       = $data_div['division_address'];
  1670.                                                         $division_invoice_rule  = $data_div['division_invoice_rule']; 
  1671.                                                         $division_max_amount    = $data_div['division_max_amount'];
  1672.                             $division_description   = $data_div['division_description'];
  1673.                             $ba                     = $data_div['berita_acara'];
  1674.                             if($ba == 1){$ba = "Yes";}else{$ba = "No";}
  1675.                             $fp                     = $data_div['faktur_pajak'];
  1676.                             if($fp == 1){$fp = "Yes";}else{$fp = "No";}
  1677.                             $softcopy               = $data_div['soft_copy'];
  1678.                             if($softcopy == 1){$softcopy = "Yes";}else{$softcopy = "No";}
  1679.                             $hardcopy               = $data_div['hard_copy'];
  1680.                             if($hardcopy == 1){$hardcopy = "Yes";}else{$hardcopy = "No";}
  1681.                                                        
  1682.                                                         $dept_code              = "$division_code-00";
  1683.                                                         $data_dept              = $this->get_data_dept_by_id($dept_code);
  1684.                                                        
  1685.                                                         if ($data_dept == null || $data_dept == '' || $data_dept == "") {
  1686.                                                                 return redirect()->back()->with('alert', 'Please Check Data Department '.$dept_code.' Again. File Cannot be Executed.');
  1687.                                                         }
  1688.                                                        
  1689.                                                         $dept_code = $data_dept['dept_code'];
  1690.                             $dept_names = $data_dept['dept_name'];
  1691.  
  1692.                             //PIC 
  1693.                             $data_pic      = $this->get_data_pics_by_id($division_code);
  1694.                                                         if ($data_pic == null || $data_pic == '' || $data_pic == "") {
  1695.                                                                 return redirect()->back()->with('alert', 'Please Check Data PIC from Division '.$division_code.'. File Cannot be Executed.');
  1696.                                                         }
  1697.                             $pic_id       = $data_pic['pic_id'];
  1698.                             $pic_name     = $data_pic['pic_name'];
  1699.                             $email_client = $data_pic['pic_email'];
  1700.                             $pic_position = $data_pic['pic_position'];
  1701.  
  1702.                             $data_pic_collection = $this->get_data_pic_collection($division_code);
  1703.                             if ($data_pic_collection == null || $data_pic_collection == '' || $data_pic_collection == "") {
  1704.                                                                 $pic_collection_name        = "";
  1705.                                 $email_pic_collection       = $email_client;
  1706.                                                         }
  1707.                             $pic_collection_name        = $data_pic_collection['pic_name'];
  1708.                             $email_pic_collection       = $data_pic_collection['pic_email'];
  1709.  
  1710.                             $data_pic_pph23 = $this->get_data_pic_pph23($division_code);
  1711.                             if ($data_pic_pph23 == null || $data_pic_pph23 == '' || $data_pic_pph23 == "") {
  1712.                                                                 $pic_pph23_name        = "";
  1713.                                 $email_pic_pph23       = $email_client;
  1714.                                                         }
  1715.                             $pic_pph23_name        = $data_pic_pph23['pic_name'];
  1716.                             $email_pic_pph23       = $data_pic_pph23['pic_email'];
  1717.  
  1718.                             $sub_total = ceil($sum_total);
  1719.                                                         $amount_invoice_rule = $sub_total + $new_ppn;
  1720.                                                        
  1721.                                                         if($new_ppn > 0){
  1722.                                                                 $vats = "exclude";
  1723.                                                         }else if($new_ppn <= 0){
  1724.                                                                 $vats = "include";
  1725.                                                         }
  1726.                                                        
  1727.                                                         //cek BUMN/Lembaga
  1728.                                                         $real                                  = $this->real_field($lmbg, $invoice_rule, $max_amount, $amount_invoice_rule, $sub_total, $vats);
  1729.                        
  1730.                                                         $sub_total_now                  = $real[0];
  1731.                                                         $sub_total_real                 = $real[1];
  1732.                                                         $new_ppn_now                    = $real[2];
  1733.                                                         $new_ppn_real                   = $real[3];
  1734.                        
  1735.                                                         $new_materai = 0;
  1736.                             if ($materai_client     == 1) {
  1737.                                 $new_materai = $this->check_materai($sub_total_now, $new_ppn_now);
  1738.                             } else if ($materai_client     == 0) {
  1739.                                 $new_materai = 0;
  1740.                             }
  1741.                             $pph23   = $this->check_pph23($client_code, $sub_total_now);
  1742.  
  1743.                             $Total = round($sub_total_now + $new_ppn_now + $new_materai - $pph23);
  1744.                                                         $Total_real = round($sub_total_real + $new_ppn_real + $new_materai - $pph23);
  1745.                             $now = \Carbon\Carbon::now();
  1746.                             $descManual = "-";
  1747.                                                        
  1748.                                                         if($materai_client == 1 ){
  1749.                                                                 $materai = "yes";
  1750.                                                                
  1751.                                                         }else{
  1752.                                                                 $materai = "no";
  1753.                                                                
  1754.                                                         }
  1755.                                                        
  1756.                             $manual_invoice = [
  1757.                                 "client_code"   => $client_code,
  1758.                                 "dept_code"     => $dept_code,
  1759.                                 "division_code" => $division_code,
  1760.                                 "desc"          => $descManual,
  1761.                                 "vat"           => $vat, //?
  1762.                                 "materai"       => $materai, //?
  1763.                                 "total"         => $Total
  1764.                             ];
  1765.  
  1766.                             $last_id = DB::table("db_sprint_ar.manual_invoice")->insertGetId($manual_invoice);
  1767.  
  1768.                             $invXls[] = array(
  1769.                                 "client_code"                 => $client_code,
  1770.                                 "client_name"                 => $client_names,
  1771.                                 "division_code"               => $division_code,
  1772.                                 "division_name"               => $division_names,
  1773.                                 "division_description"        => $division_description,
  1774.                                 "dept_code"                   => $dept_code,
  1775.                                 "dept_name"                   => $dept_names,
  1776.                                 "pic_id"                      => $pic_id,
  1777.                                 "pic_name"                    => $pic_name,
  1778.                                 "pic_position"                => $pic_position,
  1779.                                 "inv_month"                   => $month,  //date("m"),
  1780.                                 "inv_year"                    => $year, //date("yy"),
  1781.                                 "unitprice"                   => $sum_uprice,
  1782.                                 "quantity"                    => $sum_quantity,
  1783.                                 "sub_total"                   => ceil($sub_total_now),
  1784.                                 "ppn"                         => floor($new_ppn_now),
  1785.                                                                 "sub_total_real"              => ceil($sub_total_real),
  1786.                                 "ppn_real"                    => floor($new_ppn_real),
  1787.                                 "materai"                     => $new_materai,
  1788.                                 "pph23"                       => $pph23,
  1789.                                 "total"                       => $Total,
  1790.                                                                 "total_real"                  => $Total_real,
  1791.                                 "serialize_data"              => $serialize,
  1792.                                 "selisih"                     => 0,
  1793.                                 "status"                      => 0,
  1794.                                 "addmanual"                   => 1,
  1795.                                 "id_manual"                   => $last_id,
  1796.                                 "division_address"            => $division_address,
  1797.                                 "bank_acc_number_sprint"      => $bank_acc_number_sprint,
  1798.                                 "bank_acc_name_sprint"               => $bank_acc_name_sprint,
  1799.                                 "bank_acc_number_client"      => $bank_acc_number_client,
  1800.                                 "bank_acc_name_client"        => $bank_acc_name_client,
  1801.                                 "bank_coa_sprint"             => $bank_coa_sprint,
  1802.                                 "zahir_name"                  => $zahir_name,
  1803.                                 "client_npwp_no"              => $client_npwp_no,
  1804.                                 "bank_name"                   => $bank_name,
  1805.                                 "client_coa"                  => $client_coas,
  1806.                                 "revenue_coa"                 => $revenue_coa,
  1807.                                 "email_client"                => $email_client,
  1808.                                 "email_collection"            => $email_pic_collection,
  1809.                                 "email_pph23"                 => $email_pic_pph23,
  1810.                                 "description"                 => $item_name,
  1811.                                 "created_at"                  => $now,
  1812.                                                                 "id_collection"               =>'[""]',
  1813.                                 "ba"                      => $ba,
  1814.                                 "fp"                      => $fp,
  1815.                                 "softcopy"                => $softcopy,
  1816.                                 "hardcopy"                => $hardcopy
  1817.  
  1818.                             );
  1819.  
  1820.                             // return $invXls;
  1821.                            
  1822.                                                         $manual_detail = [];
  1823.                                                         for($f = 0; $f < count($item_d); $f++){
  1824.                                                                 if($f != 0){
  1825.                                                                         $item_name = null;
  1826.                                                                        
  1827.                                                                 }
  1828.                                                                
  1829.                                                                 $manual_detail[] = [
  1830.                                                                                 "id_manual"         => $last_id,
  1831.                                                                                 "service"           => $item_d[$f]['item_name'] ,
  1832.                                                                                 "quantity"          => $item_d[$f]['quantity'],
  1833.                                                                                 "unit_price"        => $item_d[$f]['unitprice'],
  1834.                                                                                 "desc"              => $item_name,
  1835.                                                                                 "total"             => $item_d[$f]['total']
  1836.                                                                         ];
  1837.                                                         }      
  1838.                                                        
  1839.                                                         $last_id = DB::table("db_sprint_ar.manual_detail")->insert($manual_detail);
  1840.                         // } catch (Exception $e) {
  1841.                         //     return redirect()->back()->with('alert', 'Please Check Your Data Again. File Cannot be Executed.');
  1842.                         // }
  1843.                     }
  1844.                 }
  1845.  
  1846.                
  1847.                                 // return $invXls;
  1848.                 if (!empty($invXls)) {
  1849.                     DB::table('db_sprint_ar.invoice')->insert($invXls);
  1850.                                        
  1851.                 }
  1852.                 DB::commit();
  1853.                 $up_manualInvoice   = [
  1854.                     "path_name" => $path_name,
  1855.                     "file_name" => $nameFileUpload,
  1856.                     "size"      => $size,
  1857.                     "created_at"    => now()
  1858.     
  1859.                 ];
  1860.                 
  1861.                 $insert_upload_ = DB::table('upload_manual_invoice')->insert($up_manualInvoice);
  1862.                 return redirect()->back()->with('alert', 'Import File Success.');
  1863.             } else {
  1864.                 return redirect()->back()->with('alert', 'Something Wrong. Excel Only.');
  1865.             }
  1866.         } catch (Exception $e) {
  1867.             DB::rollback();
  1868.         }
  1869.                
  1870.     }
  1871.     
  1872.     public function upload_manual_invoice_load_data(){
  1873.         try {
  1874.             $datas = [];
  1875.             // return $invoices = DB::table('invoice')->select('id', 'no_inv', 'client_name', 'division_name', 'total')->where('inv_month', date('m'))->where('inv_year', date('Y'))->where('status', $this->status)->get();
  1876.             $file_upload = DB::table('upload_manual_invoice')->select('file_name', 'size', DB::raw('IF(created_at IS NULL, "", DATE_FORMAT(DATE(created_at), "%d/%m/%Y")) AS created_at'))->orderBy('created_at', 'desc')->get();
  1877.             foreach ($file_upload as $key => $fu) {
  1878.                 $datas[] = [
  1879.                     "file_name"     => $fu->file_name,
  1880.                     "size"          => $fu->size,
  1881.                     "created_at"    => date($fu->created_at),
  1882.                     
  1883.                 ];
  1884.             }
  1885.             return $datas;
  1886.         } catch (Exception $e) {
  1887.             return [];
  1888.         }
  1889.     }
  1890.        
  1891.     public function get_data_manual_invoice($id)
  1892.     {
  1893.         $top = DB::table('db_sprint_ar.manual_invoice AS a')
  1894.             ->where('a.id', $id)
  1895.             ->join('db_sprint_ar.invoice AS b', 'b.id_manual', 'a.id')
  1896.             ->select('a.id', 'a.client_code', 'b.client_name', 'a.dept_code', 'a.division_code', 'a.total', 'a.desc', 'a.materai', 'a.bhp', 'a.uso', 'b.revenue_coa', 'a.vat') 
  1897.             ->get();
  1898.  
  1899.         $details = DB::table('db_sprint_ar.manual_detail')
  1900.             ->where('id_manual', $id)
  1901.             ->select('id_manual', 'service', 'quantity', 'unit_price', 'total', 'desc')
  1902.             ->get()
  1903.             ->groupBy('id_manual');
  1904.  
  1905.         foreach ($top as  $d) {
  1906.             foreach ($details[$d->id] as $dt) {
  1907.                 $id_desc = $this->get_id_desc($dt->service);
  1908.                 $datas[] = [
  1909.                     "id_service"     => $id_desc,
  1910.                     "service"        => $dt->service,
  1911.                     "quantity"       => $dt->quantity,
  1912.                     "unit_price"     => $dt->unit_price,
  1913.                     "total"          => $dt->total,
  1914.                                         "desc"           => $dt->desc
  1915.                 ];
  1916.             }
  1917.  
  1918.             return $data = [
  1919.                 "id"                   => $d->id,
  1920.                 "client_code"          => $d->client_code,
  1921.                 "client_name"          => $d->client_name,
  1922.                 "dept_code"            => $d->dept_code,
  1923.                 "division_code"        => $d->division_code,
  1924.                 "desc"                 => $d->desc,
  1925.                 "vat"                  => $d->vat,
  1926.                 "materai"              => $d->materai,
  1927.                 "coa_of_category"      => $d->revenue_coa,
  1928.                 "bhp"                              => $d->bhp,
  1929.                 "uso"                              => $d->uso,
  1930.                 "details"              => $datas
  1931.             ];
  1932.         }
  1933.     }
  1934.        
  1935.         public function real_field($lmbg, $invoice_rule, $max_amount, $amount_invoice_rule, $sub_total, $vat){
  1936.         $sub_total_now = 0; $sub_total_real = 0; $new_ppn_now = 0; $new_ppn_real = 0;
  1937.                 if(($lmbg == "yes" || $lmbg == "Yes" ) && $invoice_rule == 1 && $vat == 'exclude' ){ //exclude
  1938.                                 if($sub_total > $max_amount){
  1939.                                         $sub_total_now                 = ceil($sub_total);
  1940.                                         $sub_total_real        = ceil($sub_total);
  1941.                                         $new_ppn_now           = floor($sub_total * 0.11);
  1942.                                         $new_ppn_real          = 0; //ke sprint
  1943.                                 }else{
  1944.                                         $sub_total_now                 = ceil($sub_total);
  1945.                                         $sub_total_real        = ceil($sub_total); 
  1946.                                         $new_ppn_now           = floor($sub_total * 0.11);
  1947.                                         $new_ppn_real          = floor($sub_total_real * 0.11);
  1948.                                 }
  1949.                         }else if(($lmbg == "yes" || $lmbg == "Yes" ) && $invoice_rule == 1 && $vat == 'include' ){ //include
  1950.                                 if($sub_total > $max_amount){
  1951.                                         $sub_total_now                 = ceil($sub_total);
  1952.                                         $sub_total_real        = ceil($sub_total / 1.11); 
  1953.                                         $new_ppn_now           = 0; //$sub_total * 0.1;
  1954.                                         $new_ppn_real          = 0; //$sub_total * 0.1;
  1955.                                 }else{
  1956.                                         $sub_total_now                 = ceil($sub_total);
  1957.                                         $sub_total_real        = ceil($sub_total / 1.11); 
  1958.                                         $new_ppn_now           = 0;
  1959.                                         $new_ppn_real          = floor($sub_total_real * 0.11);
  1960.                                 }
  1961.                         }else if(($lmbg == "yes" || $lmbg == "Yes" ) && $invoice_rule == 2 && $vat == 'exclude'){ //exclude
  1962.                                 if($amount_invoice_rule > $max_amount){
  1963.                                         $sub_total_now                 = ceil($sub_total);
  1964.                                         $sub_total_real        = ceil($sub_total);
  1965.                                         $new_ppn_now           = floor($sub_total * 0.11);
  1966.                                         $new_ppn_real          = 0; //ke sprint
  1967.                                 }else{
  1968.                                         $sub_total_now                 = ceil($sub_total);
  1969.                                         $sub_total_real        = ceil($sub_total); 
  1970.                                         $new_ppn_now           = floor($sub_total * 0.11);
  1971.                                         $new_ppn_real          = floor($sub_total_real * 0.11);
  1972.                                 }
  1973.                         }else if(($lmbg == "yes" || $lmbg == "Yes" ) && $invoice_rule == 2 && $vat == 'include' ){ //include
  1974.                                 if($amount_invoice_rule > $max_amount){
  1975.                                         $sub_total_now                 = ceil($sub_total);
  1976.                                         $sub_total_real        = ceil($sub_total / 1.11); 
  1977.                                         $new_ppn_now           = 0; //$sub_total * 0.1;
  1978.                                         $new_ppn_real          = 0; //$sub_total * 0.1;
  1979.                                 }else{
  1980.                                         $sub_total_now                 = ceil($sub_total);
  1981.                                         $sub_total_real        = ceil($sub_total / 1.11); 
  1982.                                         $new_ppn_now           = 0;
  1983.                                         $new_ppn_real          = floor($sub_total_real * 0.11);
  1984.                                 }
  1985.                         }else if(($lmbg == "no" || $lmbg == null || $lmbg == '' || $lmbg == "No") && $vat == 'exclude'){ //exlude
  1986.                                
  1987.                                         $sub_total_now                 = ceil($sub_total);
  1988.                                         $sub_total_real        = ceil($sub_total); /// 1.1; 
  1989.                                         $new_ppn_now           = floor($sub_total * 0.11);
  1990.                                         $new_ppn_real          = floor($sub_total_real * 0.11);
  1991.                                
  1992.                         }else if(($lmbg == "no" || $lmbg == null || $lmbg == '' || $lmbg == "No") && $vat == 'include'){ //include
  1993.                                
  1994.                                         $sub_total_now                 = ceil($sub_total);
  1995.                                         $sub_total_real        = ceil($sub_total / 1.11); 
  1996.                                         $new_ppn_now           = 0; //$sub_total * 0.1;
  1997.                                         $new_ppn_real          = floor($sub_total_real * 0.11);
  1998.                                
  1999.                         }
  2000.                        
  2001.                         $real   = [$sub_total_now, $sub_total_real, $new_ppn_now, $new_ppn_real];
  2002.                        
  2003.                         return $real;
  2004.                
  2005.         }
  2006.  
  2007.     public function get_data_manual_detail($id)
  2008.     {
  2009.         return $dtdetail = DB::table('manual_detail')
  2010.             ->join('db_sprint_account.services', 'db_sprint_account.services.service_code', '=', 'manual_detail.service')
  2011.             ->where('manual_detail.id_manual', $id)
  2012.             ->get();
  2013.     }
  2014.   public function get_data_sc(Request $request)
  2015.     {
  2016.         $sc = new \GuzzleHttp\Client(['base_uri' => 'https://backoffice.sprintasia.net:82/sprint-sysadmin/public/api-master/getservice_category']);
  2017.         $headers = [
  2018.             'Authorization' => 'Bearer ' . $_COOKIE["token"],
  2019.             'Accept'        => 'application/json',
  2020.         ];
  2021.         $response = $sc->request('GET', '', [
  2022.             'headers' => $headers,
  2023.             // 'query' => ['client_code' => $code]
  2024.         ]);
  2025.         $dtsc = json_decode($response->getBody()->getContents(), true)['data'];
  2026.         return json_encode($dtsc);
  2027.     }
  2028.  
  2029.     public function get_data_sc_e($code)
  2030.     {
  2031.         $sc_e = new \GuzzleHttp\Client(['base_uri' => 'https://backoffice.sprintasia.net:82/sprint-sysadmin/public/api-master/getservice_category_e']);
  2032.         $headers = [
  2033.             'Authorization' => 'Bearer ' . $_COOKIE["token"],
  2034.             'Accept'        => 'application/json',
  2035.         ];
  2036.         $response = $sc_e->request('GET', '', [
  2037.             'headers' => $headers,
  2038.             'query' => ['revenue_coa' => $code]
  2039.         ]);
  2040.         $dtsc_e = json_decode($response->getBody()->getContents(), true)['data'];
  2041.         return json_encode($dtsc_e);
  2042.     }
  2043.  
  2044.     public function get_data_division($code)
  2045.     {
  2046.         $client = new \GuzzleHttp\Client(['base_uri' => 'https://backoffice.sprintasia.net:82/sprint-sysadmin/public/api-master/get_data_division']);
  2047.         $headers = [
  2048.             'Authorization' => 'Bearer ' . $_COOKIE["token"],
  2049.             'Accept'        => 'application/json',
  2050.         ];
  2051.         $response = $client->request('GET', '', [
  2052.             'headers' => $headers,
  2053.             'query' => ['client_code' => $code]
  2054.         ]);
  2055.         $dtclient = json_decode($response->getBody()->getContents(), true)['data'];
  2056.         return json_encode($dtclient);
  2057.     }
  2058.  
  2059.     public function get_data_dept_name($code)
  2060.     {
  2061.         $client = new \GuzzleHttp\Client(['base_uri' => 'https://backoffice.sprintasia.net:82/sprint-sysadmin/public/api-master/get_data_dept']);
  2062.         $headers = [
  2063.             'Authorization' => 'Bearer ' . $_COOKIE["token"],
  2064.             'Accept'        => 'application/json',
  2065.         ];
  2066.         $response = $client->request('GET', '', [
  2067.             'headers' => $headers,
  2068.             'query' => ['division_code' => $code]
  2069.         ]);
  2070.         $dtclient = json_decode($response->getBody()->getContents(), true)['data'];
  2071.         return json_encode($dtclient);
  2072.     }
  2073.  
  2074.     public function get_data_service()
  2075.     {
  2076.         $dtservs = ServiceCategory::select('service_category_code', 'service_category_name', 'coa_of_category')->get();
  2077.         return response()->json($dtservs);
  2078.     }
  2079.  
  2080.     public function get_client_name($id)
  2081.     {
  2082.         $nameclient = DB::table('clients')->select('client_code', 'client_name')->where('client_code', '=', $id)->get();
  2083.         return response()->json($nameclient);
  2084.     }
  2085.  
  2086.     public function get_service_category(Request $request)
  2087.     {
  2088.         $client = new \GuzzleHttp\Client(['base_uri' => 'https://backoffice.sprintasia.net:82/sprint-sysadmin/public/api-master/get_all_service']);
  2089.         $headers = [
  2090.             'Authorization' => 'Bearer ' . $_COOKIE["token"],
  2091.             'Accept'        => 'application/json',
  2092.         ];
  2093.         $response = $client->request('GET', '', [
  2094.             'headers' => $headers,
  2095.             'query' => ['client_name' => $request["vclient"]]
  2096.         ]);
  2097.         $dtclient = json_decode($response->getBody()->getContents(), true)['data'];
  2098.         return json_encode($dtclient);
  2099.     }
  2100.  
  2101.     public function serviceCategoryAutocomplete(Request $request)
  2102.     {
  2103.         $scategory = new \GuzzleHttp\Client(['base_uri' => 'https://backoffice.sprintasia.net:82/sprint-sysadmin/public/api-master/getservicecategory/']);
  2104.         $headers = [
  2105.             'Authorization' => 'Bearer ' . $_COOKIE["token"],
  2106.             'Accept'        => 'application/json',
  2107.         ];
  2108.         $response = $scategory->request('GET', '', [
  2109.             'headers' => $headers,
  2110.             'query' => ['service_category' => $request["sc_name"]]
  2111.         ]);
  2112.         return json_decode($response->getBody()->getContents(), true)['data'];
  2113.     }
  2114.  
  2115.     public function get_data_division_2($code)
  2116.     {
  2117.         $client = new \GuzzleHttp\Client(['base_uri' => 'https://backoffice.sprintasia.net:82/sprint-sysadmin/public/api-master/get_data_all_division_2']);
  2118.         $headers = [
  2119.             'Authorization' => 'Bearer ' . $_COOKIE["token"],
  2120.             'Accept'        => 'application/json',
  2121.         ];
  2122.         $response = $client->request('GET', '', [
  2123.             'headers' => $headers,
  2124.             'query' => ['client_code' => $code]
  2125.         ]);
  2126.         $dtclient = json_decode($response->getBody()->getContents(), true)['data'];
  2127.         return json_encode($dtclient);
  2128.     }
  2129.  
  2130.     public function get_data_dept_2($code)
  2131.     {
  2132.         $client = new \GuzzleHttp\Client(['base_uri' => 'https://backoffice.sprintasia.net:82/sprint-sysadmin/public/api-master/get_data_all_dept_2']);
  2133.         $headers = [
  2134.             'Authorization' => 'Bearer ' . $_COOKIE["token"],
  2135.             'Accept'        => 'application/json',
  2136.         ];
  2137.         $response = $client->request('GET', '', [
  2138.             'headers' => $headers,
  2139.             'query' => ['division_code' => $code]
  2140.         ]);
  2141.         $dtclient = json_decode($response->getBody()->getContents(), true)['data'];
  2142.         return json_encode($dtclient);
  2143.     }
  2144.  
  2145.     public function ServiceAutocomplete(Request $request)
  2146.     {
  2147.         $Uri = new \GuzzleHttp\Client(['base_uri' => 'https://backoffice.sprintasia.net:82/sprint-sysadmin/public/api-master/getcustomerautocomplete/']);
  2148.         $headers = [
  2149.             'Authorization' => 'Bearer ' . $_COOKIE["token"],
  2150.             'Accept'        => 'application/json',
  2151.         ];
  2152.         $response = $Uri->request('GET', '', [
  2153.             'headers' => $headers,
  2154.             'query' => ['client_name' => $request["vclient"]]
  2155.         ]);
  2156.         $data = json_decode($response->getBody()->getContents(), true)['data'];
  2157.         return json_encode($dtclient);
  2158.     }
  2159.  
  2160.     public function get_data_division_by_id($division_code)
  2161.     {
  2162.         try {
  2163.             $Uri = new \GuzzleHttp\Client(['base_uri' => 'https://backoffice.sprintasia.net:82/sprint-sysadmin/public/api-master/get_data_division_by_id']);
  2164.             $headers = [
  2165.                 'Authorization' => 'Bearer ' . $_COOKIE["token"],
  2166.                 'Accept'        => 'application/json',
  2167.             ];
  2168.             $response = $Uri->request('GET', '', [
  2169.                 'headers' => $headers,
  2170.                 'query' => ['division_code' => $division_code]
  2171.             ]);
  2172.             return $data = json_decode($response->getBody()->getContents(), true)['data'][0];
  2173.         } catch (Exception $e) {
  2174.             return [];
  2175.         }
  2176.     } // end f
  2177.  
  2178.     public function get_data_pics_by_id($division_code)
  2179.     {
  2180.         try {
  2181.             $Uri = new \GuzzleHttp\Client(['base_uri' => 'https://backoffice.sprintasia.net:82/sprint-sysadmin/public/api-master/get_data_pics_by_id']);
  2182.             $headers = [
  2183.                 'Authorization' => 'Bearer ' . $_COOKIE["token"],
  2184.                 'Accept'        => 'application/json',
  2185.             ];
  2186.             $response = $Uri->request('GET', '', [
  2187.                 'headers' => $headers,
  2188.                 'query' => ['division_code' => $division_code]
  2189.             ]);
  2190.             return $data = json_decode($response->getBody()->getContents(), true)['data'][0];
  2191.         } catch (Exception $e) {
  2192.             return [];
  2193.         }
  2194.     } // end f
  2195.  
  2196.     public function get_data_client_by_id($client_code)
  2197.     {   
  2198.         // return $_COOKIE["token"];
  2199.         try {
  2200.             $Uri = new \GuzzleHttp\Client(['base_uri' => 'https://backoffice.sprintasia.net:82/sprint-sysadmin/public/api-master/get_data_client_by_id']);
  2201.             $headers = [
  2202.                 'Authorization' => 'Bearer ' . $_COOKIE["token"],
  2203.                 'Accept'        => 'application/json',
  2204.             ];
  2205.             $response = $Uri->request('GET', '', [
  2206.                 'headers' => $headers,
  2207.                 'query' => ['client_code' => $client_code]
  2208.             ]);
  2209.             return $data = json_decode($response->getBody()->getContents(), true)['data'][0];
  2210.         } catch (Exception $e) {
  2211.             return [];
  2212.         }
  2213.     } // end f
  2214.  
  2215.     public function get_data_dept_by_id($dept_code)
  2216.     {
  2217.         try {
  2218.             $Uri = new \GuzzleHttp\Client(['base_uri' => 'https://backoffice.sprintasia.net:82/sprint-sysadmin/public/api-master/get_data_dept_by_id']);
  2219.             $headers = [
  2220.                 'Authorization' => 'Bearer ' . $_COOKIE["token"],
  2221.                 'Accept'        => 'application/json',
  2222.             ];
  2223.             $response = $Uri->request('GET', '', [
  2224.                 'headers' => $headers,
  2225.                 'query' => ['dept_code' => $dept_code]
  2226.             ]);
  2227.             return $data = json_decode($response->getBody()->getContents(), true)['data'][0];
  2228.         } catch (Exception $e) {
  2229.             return [];
  2230.         }
  2231.     }
  2232.  
  2233.     public function check_ppn($vat, $sub_total)
  2234.     {
  2235.         if ($vat == 'exclude') {
  2236.             return $ppn = $sub_total * 0.11;
  2237.         } else {
  2238.             return $ppn = 0;
  2239.         } // End PPN
  2240.     }
  2241.  
  2242.     public function check_materai($sub_total, $ppn)
  2243.     {
  2244.         $math = $sub_total + $ppn;
  2245.         if ($math < 5000000) {
  2246.             return $materai = 0;
  2247.         }else if ($math >= 5000000) {
  2248.             return $materai = 10000;
  2249.         } // end if
  2250.     }
  2251.  
  2252.     public function check_materai_old($sub_total, $ppn)
  2253.     {
  2254.         $math = $sub_total + $ppn;
  2255.         if ($math < 250000) {
  2256.             return $materai = 0;
  2257.         } else if ($math < 1000000) {
  2258.             return $materai = 3000;
  2259.         } else if ($math >= 1000000) {
  2260.             return $materai = 6000;
  2261.         } // end if
  2262.     } // end function
  2263.  
  2264.     public function check_pph23($client_code, $sub_total)
  2265.     {
  2266.         try {
  2267.             $Uri = new \GuzzleHttp\Client(['base_uri' => 'https://backoffice.sprintasia.net:82/sprint-sysadmin/public/api-master/get_pph23']); // Get Template Invoice from Account Management, Parameter id Template Invoice
  2268.             $headers = [
  2269.                 'Authorization' => 'Bearer ' . $_COOKIE["token"],
  2270.                 'Accept'        => 'application/json',
  2271.             ];
  2272.             $response     = $Uri->request('GET', '', [
  2273.                 'headers'   => $headers,
  2274.                 'query'     => ['client_code' => $client_code]
  2275.             ]);
  2276.             $incomeTaxData = json_decode($response->getBody()->getContents(), true)['data'][0];
  2277.             $incomeTax = $incomeTaxData['income_tax_23'];
  2278.         } catch (Exception $e) {
  2279.             // Log::error("Function Error",["name"=>__FUNCTION__, "message"=>$e->getMessage()]);
  2280.             $incomeTax = 0;
  2281.         }
  2282.         if ($incomeTax !== null) {
  2283.             return $sub_total * ($incomeTaxData['income_tax_23'] / 100);
  2284.         } else {
  2285.             return 0;
  2286.         }
  2287.     }// end f
  2288.  
  2289.     public function get_revenue_coa($service_code)
  2290.     {
  2291.         try {
  2292.             $Uri = new \GuzzleHttp\Client(['base_uri' => 'https://backoffice.sprintasia.net:82/sprint-sysadmin/public/api-master/get_data_service_category_2']); // Get Template Invoice from Account Management, Parameter id Template Invoice
  2293.             $headers = [
  2294.                 'Authorization' => 'Bearer ' . $_COOKIE["token"],
  2295.                 'Accept'        => 'application/json',
  2296.             ];
  2297.             $response     = $Uri->request('GET', '', [
  2298.                 'headers'   => $headers,
  2299.                 'query'     => ['service_code ' => $service_code]
  2300.             ]);
  2301.             return json_decode($response->getBody()->getContents(), true)['data'][0];
  2302.         } catch (Exception $e) {
  2303.             return '';
  2304.         }
  2305.     } // end f
  2306.  
  2307.     public function get_data_dept_by_name($dept_name)
  2308.     {
  2309.         try {
  2310.             $Uri = new \GuzzleHttp\Client(['base_uri' => 'https://backoffice.sprintasia.net:82/sprint-sysadmin/public/api-master/get_data_dept_by_name_2']);
  2311.             $headers = [
  2312.                 'Authorization' => 'Bearer ' . $_COOKIE["token"],
  2313.                 'Accept'        => 'application/json',
  2314.             ];
  2315.             $response = $Uri->request('GET', '', [
  2316.                 'headers' => $headers,
  2317.                 'query' => ['dept_name' => $dept_name]
  2318.             ]);
  2319.             return $data = json_decode($response->getBody()->getContents(), true)['data'][0];
  2320.         } catch (Exception $e) {
  2321.             return [];
  2322.         }
  2323.     } // end f
  2324.  
  2325.     public function get_data_division_by_name($division_name)
  2326.     {
  2327.         try {
  2328.             $Uri = new \GuzzleHttp\Client(['base_uri' => 'https://backoffice.sprintasia.net:82/sprint-sysadmin/public/api-master/get_data_division_by_name']);
  2329.             $headers = [
  2330.                 'Authorization' => 'Bearer ' . $_COOKIE["token"],
  2331.                 'Accept'        => 'application/json',
  2332.             ];
  2333.             $response = $Uri->request('GET', '', [
  2334.                 'headers' => $headers,
  2335.                 'query' => ['division_name' => $division_name]
  2336.             ]);
  2337.             return $data = json_decode($response->getBody()->getContents(), true)['data'][0];
  2338.         } catch (Exception $e) {
  2339.             return [];
  2340.         }
  2341.     } // end f
  2342.  
  2343.     public function get_data_division_by_name_and_client_code($division_name, $client_code)
  2344.     {
  2345.         try {
  2346.             $Uri = new \GuzzleHttp\Client(['base_uri' => 'https://backoffice.sprintasia.net:82/sprint-sysadmin/public/api-master/get_data_division_by_name_and_client_code']);
  2347.             $headers = [
  2348.                 'Authorization' => 'Bearer ' . $_COOKIE["token"],
  2349.                 'Accept'        => 'application/json',
  2350.             ];
  2351.             $response = $Uri->request('GET', '', [
  2352.                 'headers' => $headers,
  2353.                 'query' => ['division_name' => $division_name, 'client_code' => $client_code]
  2354.             ]);
  2355.             return $data = json_decode($response->getBody()->getContents(), true)['data'][0];
  2356.         } catch (Exception $e) {
  2357.             return [];
  2358.         }
  2359.     } // end f
  2360.  
  2361.     public function get_data_client_by_name($client_name)
  2362.     {
  2363.         try {
  2364.             $Uri = new \GuzzleHttp\Client(['base_uri' => 'https://backoffice.sprintasia.net:82/sprint-sysadmin/public/api-master/get_data_client_by_name']);
  2365.             $headers = [
  2366.                 'Authorization' => 'Bearer ' . $_COOKIE["token"],
  2367.                 'Accept'        => 'application/json',
  2368.             ];
  2369.             $response = $Uri->request('GET', '', [
  2370.                 'headers' => $headers,
  2371.                 'query' => ['client_name' => $client_name]
  2372.             ]);
  2373.             return $data = json_decode($response->getBody()->getContents(), true)['data'][0];
  2374.         } catch (Exception $e) {
  2375.             return [];
  2376.         }
  2377.     } // end f
  2378.  
  2379.     public function get_data_service_category_by_name($service_category_name)
  2380.     {
  2381.         try {
  2382.             $Uri = new \GuzzleHttp\Client(['base_uri' => 'https://backoffice.sprintasia.net:82/sprint-sysadmin/public/api-master/get_data_service_category_by_name']);
  2383.             $headers = [
  2384.                 'Authorization' => 'Bearer ' . $_COOKIE["token"],
  2385.                 'Accept'        => 'application/json',
  2386.             ];
  2387.             $response = $Uri->request('GET', '', [
  2388.                 'headers' => $headers,
  2389.                 'query' => ['service_category_name' => $service_category_name]
  2390.             ]);
  2391.             return $data = json_decode($response->getBody()->getContents(), true)['data'][0];
  2392.         } catch (Exception $e) {
  2393.             return [];
  2394.         }
  2395.     } // end f
  2396.  
  2397.     public function get_data_dept_by_client_code_division_code($division_code, $client_code)
  2398.     {
  2399.         try {
  2400.             $Uri = new \GuzzleHttp\Client(['base_uri' => 'https://backoffice.sprintasia.net:82/sprint-sysadmin/public/api-master/get_data_dept_by_client_code']);
  2401.             $headers = [
  2402.                 'Authorization' => 'Bearer ' . $_COOKIE["token"],
  2403.                 'Accept'        => 'application/json',
  2404.             ];
  2405.             $response = $Uri->request('GET', '', [
  2406.                 'headers' => $headers,
  2407.                 'query' => ['division_code' => $division_code, 'client_code' => $client_code]
  2408.             ]);
  2409.             return $data = json_decode($response->getBody()->getContents(), true)['data'][0];
  2410.         } catch (Exception $e) {
  2411.             return [];
  2412.         }
  2413.     } // end f
  2414.  
  2415.     public function get_data_price($client_code, $provider_code, $service_code)
  2416.     {
  2417.         // return $service_code;
  2418.         try {
  2419.             $Uri = new \GuzzleHttp\Client(['base_uri' => 'https://backoffice.sprintasia.net:82/sprint-sysadmin/public/api-master/check_vat']);
  2420.             $headers = [
  2421.                 'Authorization' => 'Bearer ' . $_COOKIE["token"],
  2422.                 'Accept'        => 'application/json',
  2423.             ];
  2424.             $response = $Uri->request('GET', '', [
  2425.                 'headers' => $headers,
  2426.                 'query' => ['client_code' => $client_code, 'provider_code' => $provider_code, 'service_code' => $service_code]
  2427.             ]);
  2428.             return $data = json_decode($response->getBody()->getContents(), true)['data'][0];
  2429.         } catch (Exception $e) {
  2430.             return [];
  2431.         }
  2432.     } // end f
  2433.  
  2434.     public function get_data_service_sms($service_name)
  2435.     {
  2436.         try {
  2437.             $Uri = new \GuzzleHttp\Client(['base_uri' => 'https://backoffice.sprintasia.net:82/sprint-sysadmin/public/api-master/get_data_service_sms']);
  2438.             $headers = [
  2439.                 'Authorization' => 'Bearer ' . $_COOKIE["token"],
  2440.                 'Accept'        => 'application/json',
  2441.             ];
  2442.             $response = $Uri->request('GET', '', [
  2443.                 'headers' => $headers,
  2444.                 'query' => ['service_name' => $service_name]
  2445.             ]);
  2446.             return $data = json_decode($response->getBody()->getContents(), true)['data'][0];
  2447.         } catch (Exception $e) {
  2448.             return [];
  2449.         }
  2450.     } // end f
  2451.  
  2452.     public function get_data_pic_collection($division_code)
  2453.     {
  2454.         try {
  2455.             $Uri = new \GuzzleHttp\Client(['base_uri' => 'https://backoffice.sprintasia.net:82/sprint-sysadmin/public/api-master/get_data_pic_collection']);
  2456.             $headers = [
  2457.                 'Authorization' => 'Bearer ' . $_COOKIE["token"],
  2458.                 'Accept'        => 'application/json',
  2459.             ];
  2460.             $response = $Uri->request('GET', '', [
  2461.                 'headers' => $headers,
  2462.                 'query' => ['division_code' => $division_code]
  2463.             ]);
  2464.             return $data = json_decode($response->getBody()->getContents(), true)['data'][0];
  2465.         } catch (Exception $e) {
  2466.             return [];
  2467.         }
  2468.     } // end f 
  2469.  
  2470.     public function get_data_pic_pph23($division_code)
  2471.     {
  2472.         try {
  2473.             $Uri = new \GuzzleHttp\Client(['base_uri' => 'https://backoffice.sprintasia.net:82/sprint-sysadmin/public/api-master/get_data_pic_pph23']);
  2474.             $headers = [
  2475.                 'Authorization' => 'Bearer ' . $_COOKIE["token"],
  2476.                 'Accept'        => 'application/json',
  2477.             ];
  2478.             $response = $Uri->request('GET', '', [
  2479.                 'headers' => $headers,
  2480.                 'query' => ['division_code' => $division_code]
  2481.             ]);
  2482.             return $data = json_decode($response->getBody()->getContents(), true)['data'][0];
  2483.         } catch (Exception $e) {
  2484.             return [];
  2485.         }
  2486.     } // end f
  2487.  
  2488.     public function zek_view(Request $request)
  2489.     {
  2490.  
  2491.         if (empty($_COOKIE["token"])) {
  2492.             return redirect("https://backoffice.sprintasia.net:82/sprint-sysadmin/public/login");
  2493.         } else {
  2494.             $title = "Invoice";
  2495.             $gen = "Cek Paydate Invoice";
  2496.             $content = "Cek Paydate Invoice";
  2497.  
  2498.  
  2499.             $data = DB::table('db_sprint_ar.manual_invoice AS m')
  2500.                 ->join('db_sprint_ar.invoice AS i', 'i.id_manual', 'm.id')
  2501.                 ->select('i.*', 'm.*', 'i.created_at AS tanggal')
  2502.                 ->get();
  2503.             $dtdepts = DB::table('invoice')->select('dept_code', 'dept_name')->get();
  2504.             $dtclients = DB::table('invoice')->select('client_code', 'client_name')->get();
  2505.             $dtdivs = DB::table('invoice')->select('division_code', 'division_name')->get();
  2506.             $dtservs = ServiceCategory::select('service_category_code', 'service_category_name')->get();
  2507.  
  2508.             // return view('manual_invoice.index', compact('title','content','gen','data','dtdepts', 'dtclients', 'dtdivs', 'dtservs'));
  2509.             return view('manual_invoice.index_paydate', compact('title', 'content', 'gen', 'data', 'dtdepts', 'dtclients', 'dtdivs', 'dtservs'));
  2510.         }
  2511.     }
  2512.        
  2513.         public function cek_paydate(Request $request)
  2514.     {
  2515.  
  2516.         try {
  2517.             $File               = $request->file('file_cek'); //file_upload
  2518.           
  2519.             $nameFileUpload     = $File->getClientOriginalName();
  2520.            
  2521.             $nameFile = explode("_", $nameFileUpload);
  2522.                         $dataFile = Excel::selectSheetsByIndex(0)->load($File, function ($reader) {
  2523.                 $reader->setHeaderRow(0);
  2524.                 $reader->select(['invoice_no', 'total','paid_date'])->get();
  2525.             })->get();
  2526.             $extension          = $File->getClientOriginalExtension();
  2527.             $size               = $File->getSize();
  2528.             $path                 = $File->getRealPath();
  2529.             return $dataFile;
  2530.             if ($extension == "xlsx" || $extension == "xls") {
  2531.                 // $datas = Excel::load($path)->get(); //load all of the data in sheet excel file
  2532.                 $index = 0;
  2533.                                 // return $datas;
  2534.                 if ($dataFile->count() != 0) {
  2535.                     DB::beginTransaction();
  2536.                     $invXls = array();
  2537.                     foreach ($dataFile as $data) {
  2538.                         try {
  2539.                             $invoice_no         = $data['invoice_no'];
  2540.                             $total              = $data['total'];
  2541.                             $pay_date            = $data['paid_date'];
  2542.                                                        
  2543.                                                         // return $invoice_no;
  2544.                             $cekInv = DB::table("db_sprint_ar.invoice")->where('no_inv', $invoice_no)->get();
  2545.                             // return $cekInv;
  2546.                                                         // return count($cekInv);
  2547.                                                        
  2548.                                                         if(count($cekInv) == 0){
  2549.                                                                 continue;
  2550.                                                                 // return "null";
  2551.                                                         }
  2552.                                                        
  2553.                             DB::table("db_sprint_ar.invoice")->where('no_inv', $invoice_no)->update(["paydate" => $pay_date]);
  2554.                             
  2555.                             $invXls[] = array(
  2556.                                 "no_inv"                   => $invoice_no,
  2557.                                 "paydate"                 => $pay_date,
  2558.                             );
  2559.  
  2560.                         } catch (Exception $e) {
  2561.                             return redirect()->back()->with('alert', 'Please Check Your Data Again. File Cannot be Executed.');
  2562.                         }
  2563.                     }
  2564.                 }
  2565.                 // return $invXls; 
  2566.                 DB::commit();
  2567.                 return redirect()->back()->with('alert', 'Paydate Success.');
  2568.             } else {
  2569.                 return redirect()->back()->with('alert', 'Something Wrong. Excel Only.');
  2570.             }
  2571.         } catch (Exception $e) {
  2572.             DB::rollback();
  2573.         }
  2574.     }
  2575.        
  2576.        
  2577.          public function updateInvoice(Request $request)
  2578.     {
  2579.                  $title = "Invoice";
  2580.             $gen = "Manual Invoice";
  2581.             $content = "Manual Invoice"; 
  2582.  
  2583.          return view('manual_invoice.update_invoice', compact('title', 'content', 'gen'));//, 'data', 'dtdepts', 'dtclients', 'dtdivs', 'dtservs'));
  2584.     }  // end f
  2585.        
  2586.         public function updateInvoiceNum_real(Request $request)
  2587.     { 
  2588.         try{
  2589.        
  2590.                 $jlh_invoice = DB::select( "SELECT count(id) from invoice");
  2591.  
  2592.                 $inv = DB::select( "SELECT * from invoice"); 
  2593.                 // return $inv;
  2594.                 $tes = [];
  2595.                 for($c = 0; $c < count($inv); $c++){
  2596.                         $dClient   = $this->get_data_client_by_name($inv[$c]->client_name);
  2597.                         if($dClient == null || $dClient  == '' || $dClient == ""){
  2598.                                 // $client_name = $dClient['client_name'];
  2599.                                 $lmbg = null;
  2600.                                 $invoice_rule = null;
  2601.                                 $max_amount = null;
  2602.                         }else{
  2603.                                 $lmbg = $dClient['lmbnegara'];
  2604.                                 $invoice_rule = $dClient['invoice_rule'];
  2605.                                 $max_amount = $dClient['max_amount'];
  2606.                         }
  2607.        
  2608.                         $idInv                                         = $inv[$c]->id;
  2609.                         $sub_total                             = $inv[$c]->sub_total;
  2610.                         $ppn                                   = $inv[$c]->ppn;
  2611.                         $total                                         = $inv[$c]->total;
  2612.                         $materai                               = $inv[$c]->materai;
  2613.                         $pph23                                  = $inv[$c]->pph23;
  2614.                         $amount_invoice_rule   = $sub_total + $ppn;
  2615.                         $serialize_datas                = json_decode($inv[$c]->serialize_data);
  2616.                         if($serialize_datas == null){
  2617.                                 $bhp                                   = 0;
  2618.                                 $uso                                   = 0;
  2619.                         }else{
  2620.                                 $total                                  = $serialize_datas->detail->total;
  2621.                                 if(array_key_exists('bhp', $total)){
  2622.                                         $bhp                                   = $total->bhp;
  2623.                                         $uso                                   = $total->uso;
  2624.                                 }else{
  2625.                                         $bhp                                   = 0;
  2626.                                         $uso                                   = 0;
  2627.                                 }
  2628.                         }
  2629.                         // return $uso;
  2630.  
  2631.                         if(($lmbg == "yes" || $lmbg == "Yes" ) && $invoice_rule == 1 && $ppn != 0 ){ //exclude
  2632.                                 if($sub_total > $max_amount){
  2633.                                         $sub_total_now = $sub_total;
  2634.                                         $sub_total_real = ceil($sub_total);
  2635.                                         $new_ppn_now = $sub_total * 0.1;
  2636.                                         $new_ppn_real = 0; //ke sprint
  2637.                                 }else{
  2638.                                         $sub_total_now = $sub_total;
  2639.                                         $sub_total_real = ceil($sub_total); 
  2640.                                         $new_ppn_now = $sub_total * 0.1;
  2641.                                         $new_ppn_real = floor($sub_total_real * 0.1);
  2642.                                 }
  2643.                         }else if(($lmbg == "yes" || $lmbg == "Yes" ) && $invoice_rule == 1 && $ppn == 0 ){ //include
  2644.                                 if($sub_total > $max_amount){
  2645.                                         $sub_total_now = $sub_total;
  2646.                                         $sub_total_real = ceil($sub_total / 1.1); 
  2647.                                         $new_ppn_now = 0; //$sub_total * 0.1;
  2648.                                         $new_ppn_real = 0;//$sub_total * 0.1;
  2649.                                 }else{
  2650.                                         $sub_total_now = $sub_total;
  2651.                                         $sub_total_real = ceil($sub_total / 1.1); 
  2652.                                         $new_ppn_now = 0;
  2653.                                         $new_ppn_real = floor($sub_total_real * 0.1);
  2654.                                 }
  2655.                         }else if(($lmbg == "yes" || $lmbg == "Yes" ) && $invoice_rule == 2 && $ppn != 0 ){ //exclude
  2656.                                 if($amount_invoice_rule > $max_amount){
  2657.                                         $sub_total_now = $sub_total;
  2658.                                         $sub_total_real = ceil($sub_total);
  2659.                                         $new_ppn_now = $sub_total * 0.1;
  2660.                                         $new_ppn_real = 0; //ke sprint
  2661.                                 }else{
  2662.                                         $sub_total_now = $sub_total;
  2663.                                         $sub_total_real = ceil($sub_total); 
  2664.                                         $new_ppn_now = $sub_total * 0.1;
  2665.                                         $new_ppn_real = floor($sub_total_real * 0.1);
  2666.                                 }
  2667.                         }else if(($lmbg == "yes" || $lmbg == "Yes" ) && $invoice_rule == 2 && $ppn == 0 ){ //include
  2668.                                 if($amount_invoice_rule > $max_amount){
  2669.                                         $sub_total_now = $sub_total;
  2670.                                         $sub_total_real = ceil($sub_total / 1.1); 
  2671.                                         $new_ppn_now = 0; //$sub_total * 0.1;
  2672.                                         $new_ppn_real = 0; //$sub_total * 0.1;
  2673.                                 }else{
  2674.                                         $sub_total_now = $sub_total;
  2675.                                         $sub_total_real = ceil($sub_total / 1.1); 
  2676.                                         $new_ppn_now = 0;
  2677.                                         $new_ppn_real = floor($sub_total_real * 0.1);
  2678.                                 }
  2679.                         }else if(($lmbg == "no" || $lmbg == null) && $ppn != 0 ){ //exclude
  2680.                                         $sub_total_now = $sub_total;
  2681.                                         $sub_total_real = ceil($sub_total); /// 1.1; 
  2682.                                         $new_ppn_now = $sub_total * 0.1;
  2683.                                         $new_ppn_real = floor($sub_total_real * 0.1);
  2684.                                
  2685.                         }else if(($lmbg == "no" || $lmbg == null) && $ppn == 0 ){ //include
  2686.                                         $sub_total_now = $sub_total;
  2687.                                         $sub_total_real = ceil($sub_total / 1.1); 
  2688.                                         $new_ppn_now = 0; //$sub_total * 0.1;
  2689.                                         $new_ppn_real = floor($sub_total_real * 0.1);
  2690.                         } //
  2691.                        
  2692.                                 $Total_now = $sub_total_now + $new_ppn_now + $materai - $pph23 - $bhp - $uso;
  2693.                 $Total_real = round($sub_total_real + $new_ppn_real + $materai - $pph23 - $bhp - $uso); //subtotal real dan total real bulat atas, ppn real bulat ke bawah
  2694.                         // return $Total_real;
  2695.                         \DB::table('invoice')->where('id', $idInv)
  2696.                         ->update(['total_real' => $Total_real, 'sub_total_real' => $sub_total_real, 'ppn_real' => $new_ppn_real ]);                   
  2697.                 }
  2698.         return "suksses";
  2699.                 } catch (Exception $e) {
  2700.                         return ["code" => "_ERR_SYSTM", "error" => $e->getMessage()];
  2701.                 }      
  2702.     }  // end f
  2703.  
  2704.     public function updateInvoiceNum(Request $request)
  2705.     { 
  2706.         try{
  2707.        
  2708.                 //$jlh_invoice = DB::select( "SELECT * FROM invoice WHERE client_name LIKE '%PT. Bank Rakyat Indonesia,Tbk. (BRI)%' AND created_at LIKE '%2020-11-04%' AND sub_total_real IS NULL");
  2709.  
  2710.         //$inv = DB::select("SELECT * FROM invoice WHERE client_name LIKE '%PT. Bank Rakyat Indonesia,Tbk. (BRI)%' AND created_at LIKE '%2020-11-04%' AND sub_total_real IS NULL"); 
  2711.         //$inv = DB::select("SELECT count(*) FROM invoice WHERE sub_total_real IS NULL AND no_inv IS NOT NULL"); 
  2712.         $inv = DB::select("SELECT * FROM invoice WHERE sub_total_real IS NULL AND status = 2"); 
  2713.         //return $inv;
  2714.         
  2715.                 $tes = [];
  2716.                 for($c = 0; $c < count($inv); $c++){
  2717.                         $dClient   = $this->get_data_client_by_name($inv[$c]->client_name);
  2718.                         if($dClient == null || $dClient  == '' || $dClient == ""){
  2719.                                 // $client_name = $dClient['client_name'];
  2720.                                 $lmbg = null;
  2721.                                 $invoice_rule = null;
  2722.                                 $max_amount = null;
  2723.                         }else{
  2724.                                 $lmbg = $dClient['lmbnegara'];
  2725.                                 $invoice_rule = $dClient['invoice_rule'];
  2726.                                 $max_amount = $dClient['max_amount'];
  2727.                         }
  2728.        
  2729.                         $idInv                                         = $inv[$c]->id;
  2730.                         $sub_total                             = $inv[$c]->sub_total;
  2731.                         $ppn                                   = $inv[$c]->ppn;
  2732.                         $total                                         = $inv[$c]->total;
  2733.                         $materai                               = $inv[$c]->materai;
  2734.                         $pph23                                  = $inv[$c]->pph23;
  2735.                         $amount_invoice_rule   = $sub_total + $ppn;
  2736.                         $serialize_datas                = json_decode($inv[$c]->serialize_data);
  2737.                         if($serialize_datas == null){
  2738.                                 $bhp                                   = 0;
  2739.                                 $uso                                   = 0;
  2740.                         }else{
  2741.                                 $total                                  = $serialize_datas->detail->total;
  2742.                                 if(array_key_exists('bhp', $total)){
  2743.                                         $bhp                                   = $total->bhp;
  2744.                                         $uso                                   = $total->uso;
  2745.                                 }else{
  2746.                                         $bhp                                   = 0;
  2747.                                         $uso                                   = 0;
  2748.                                 }
  2749.                         }
  2750.                         // return $uso;
  2751.  
  2752.                         if(($lmbg == "yes" || $lmbg == "Yes" ) && $invoice_rule == 1 && $ppn != 0 ){ //exclude
  2753.                                 if($sub_total > $max_amount){
  2754.                                         $sub_total_now = $sub_total;
  2755.                                         $sub_total_real = ceil($sub_total);
  2756.                                         $new_ppn_now = $sub_total * 0.11;
  2757.                                         $new_ppn_real = 0; //ke sprint
  2758.                                 }else{
  2759.                                         $sub_total_now = $sub_total;
  2760.                                         $sub_total_real = ceil($sub_total); 
  2761.                                         $new_ppn_now = $sub_total * 0.11;
  2762.                                         $new_ppn_real = floor($sub_total_real * 0.11);
  2763.                                 }
  2764.                         }else if(($lmbg == "yes" || $lmbg == "Yes" ) && $invoice_rule == 1 && $ppn == 0 ){ //include
  2765.                                 if($sub_total > $max_amount){
  2766.                                         $sub_total_now = $sub_total;
  2767.                                         $sub_total_real = ceil($sub_total / 1.1); 
  2768.                                         $new_ppn_now = 0; //$sub_total * 0.1;
  2769.                                         $new_ppn_real = 0;//$sub_total * 0.1;
  2770.                                 }else{
  2771.                                         $sub_total_now = $sub_total;
  2772.                                         $sub_total_real = ceil($sub_total / 1.1); 
  2773.                                         $new_ppn_now = 0;
  2774.                                         $new_ppn_real = floor($sub_total_real * 0.11);
  2775.                                 }
  2776.                         }else if(($lmbg == "yes" || $lmbg == "Yes" ) && $invoice_rule == 2 && $ppn != 0 ){ //exclude
  2777.                                 if($amount_invoice_rule > $max_amount){
  2778.                                         $sub_total_now = $sub_total;
  2779.                                         $sub_total_real = ceil($sub_total);
  2780.                                         $new_ppn_now = $sub_total * 0.11;
  2781.                                         $new_ppn_real = 0; //ke sprint
  2782.                                 }else{
  2783.                                         $sub_total_now = $sub_total;
  2784.                                         $sub_total_real = ceil($sub_total); 
  2785.                                         $new_ppn_now = $sub_total * 0.11;
  2786.                                         $new_ppn_real = floor($sub_total_real * 0.11);
  2787.                                 }
  2788.                         }else if(($lmbg == "yes" || $lmbg == "Yes" ) && $invoice_rule == 2 && $ppn == 0 ){ //include
  2789.                                 if($amount_invoice_rule > $max_amount){
  2790.                                         $sub_total_now = $sub_total;
  2791.                                         $sub_total_real = ceil($sub_total / 1.1); 
  2792.                                         $new_ppn_now = 0; //$sub_total * 0.1;
  2793.                                         $new_ppn_real = 0; //$sub_total * 0.1;
  2794.                                 }else{
  2795.                                         $sub_total_now = $sub_total;
  2796.                                         $sub_total_real = ceil($sub_total / 1.1); 
  2797.                                         $new_ppn_now = 0;
  2798.                                         $new_ppn_real = floor($sub_total_real * 0.11);
  2799.                                 }
  2800.                         }else if(($lmbg == "no" || $lmbg == null) && $ppn != 0 ){ //exclude
  2801.                                         $sub_total_now = $sub_total;
  2802.                                         $sub_total_real = ceil($sub_total); /// 1.1; 
  2803.                                         $new_ppn_now = $sub_total * 0.1;
  2804.                                         $new_ppn_real = floor($sub_total_real * 0.11);
  2805.                                
  2806.                         }else if(($lmbg == "no" || $lmbg == null) && $ppn == 0 ){ //include
  2807.                                         $sub_total_now = $sub_total;
  2808.                                         $sub_total_real = ceil($sub_total / 1.1); 
  2809.                                         $new_ppn_now = 0; //$sub_total * 0.1;
  2810.                                         $new_ppn_real = floor($sub_total_real * 0.11);
  2811.                         } //
  2812.                        
  2813.                                 $Total_now = $sub_total_now + $new_ppn_now + $materai - $pph23 - $bhp - $uso;
  2814.                 $Total_real = round($sub_total_real + $new_ppn_real + $materai - $pph23 - $bhp - $uso); //subtotal real dan total real bulat atas, ppn real bulat ke bawah
  2815.                 $now                   = \Carbon\Carbon::now();                                                                //
  2816.                         // return $Total_real;
  2817.                         \DB::table('invoice')->where('id', $idInv)
  2818.                         ->update(['total_real' => $Total_real, 'sub_total_real' => $sub_total_real, 'ppn_real' => $new_ppn_real, 'updated_at' => $now ]);                  
  2819.                 }
  2820.         return "suksses";
  2821.                 } catch (Exception $e) {
  2822.                         return ["code" => "_ERR_SYSTM", "error" => $e->getMessage()];
  2823.                 }      
  2824.     }  // end f
  2825.  
  2826.     public function updateInv(Request $request)
  2827.     { 
  2828.       try{
  2829.            // $jlh_invoice        = DB::select( "SELECT count(id) from invoice");
  2830.             $inv                   = DB::select( "SELECT * FROM invoice");
  2831.             // return $inv;
  2832.             for($c = 0; $c < count($inv); $c++){
  2833.                 $idInv                      = $inv[$c]->id;
  2834.                 $no_inv                                        = $inv[$c]->no_inv;
  2835.                 //return $no_inv;
  2836.                 $arr_no_inv                 = explode("/", $no_inv);
  2837.                 $c_noInv                    = count($arr_no_inv);
  2838.                //return count($arr_no_inv);
  2839.                if($no_inv != null || $no_inv != '' || $no_inv != ""){
  2840.                     if( $c_noInv == 3){
  2841.                         $no_inv_                    = $no_inv;
  2842.                         $no_inv_real                = $no_inv;
  2843.                     }else if($c_noInv == 4){
  2844.                         $no_inv_                    = $no_inv; 
  2845.                         $no_inv_real                = $arr_no_inv[0] . "/" . $arr_no_inv[1] . "/" . $arr_no_inv[3];
  2846.                     }
  2847.  
  2848.                    \DB::table('invoice')->where('id', $idInv)
  2849.                         ->update(['no_inv_real' => $no_inv_real]);
  2850.  
  2851.                 }
  2852.                                 
  2853.             }
  2854.             
  2855.             return "Done";
  2856.                
  2857.                 } catch (Exception $e) {
  2858.                         return ["code" => "_ERR_SYSTM", "error" => $e->getMessage()];
  2859.                 }
  2860.         }
  2861.        
  2862.        
  2863.        
  2864.        
  2865.        
  2866.        
  2867.        
  2868.        
  2869.        
  2870.        
  2871.        
  2872.        
  2873.        
  2874.        
  2875.        
  2876.        
  2877.        
  2878.        
  2879.        
  2880. }
  2881.  

Editor

You can edit this paste and save as new:


File Description
  • ManualInvoiceController.php
  • Paste Code
  • 06 May-2024
  • 131.69 Kb
You can Share it: