[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.                     echo "countDatasHeader ATAS : ". $countDatasHeader. "</br>";
  1035.                         // if ($countDatasHeader == 9) $countDatasHeader--;
  1036.                     for ($ik = 3; $ik < $countDatasHeader -1; $ik++) {
  1037.                         $headers = [ucfirst($header[$ik])];
  1038.                         $heads = $headers[0];
  1039.                         $heads_lowers = strtolower($headers[0]);
  1040.                         $qtty =  'qty_' . str_replace(" ","_", $heads);
  1041.                        
  1042.                         if($headers[0] != "") {
  1043.                             $qtys = $datasDivisi[$ij][$ih][$heads_lowers];
  1044.                             $contains_qtty = str_contains($qtys, '-');
  1045.                             if($contains_qtty == true){
  1046.                                 continue;
  1047.                             }
  1048.                             if( $heads == 'Hutchinson' ) $tot_traffict = $tot_traffict + $qtys;
  1049.                             if( $heads == 'Indosat' ) $tot_traffict = $tot_traffict + $qtys;
  1050.                             if( $heads == 'Telkomsel' ) $tot_traffict = $tot_traffict + $qtys;
  1051.                             if( $heads == 'Smartfren' ) $tot_traffict = $tot_traffict + $qtys;
  1052.                             if( $heads == 'Excelcom' ) $tot_traffict = $tot_traffict + $qtys;
  1053.                             if( $heads == 'Email Blast' ) $tot_traffict = $tot_traffict + $qtys;
  1054.                             else $tot_traffict = $tot_traffict;                            
  1055.                         }
  1056.                     }
  1057.                 }
  1058.                 // End Menghitung TOtal Trafict tot_traffict
  1059.                         //dd($tot_traffict);
  1060.                 // For generate itemdesc
  1061.                 for ($ih = 0; $ih < $cDD ; $ih++) 
  1062.                 {
  1063.                     $division_name    = $datasDivisi[$ij][$ih]['divisi'];
  1064.                     $produk           = $datasDivisi[$ij][$ih]['produk'];
  1065.  
  1066.  
  1067.                     if($service_category == '430000001') $jenis_service = $datasDivisi[$ij][$ih]['jenis_layanan']; // for whatsapp
  1068.                     else if($service_category == '4102004') { $jenis_service = $datasDivisi[$ij][$ih]['jenis_layanan']; } // for SMS Pull
  1069.                             else if($service_category == '410000001') $jenis_service = $datasDivisi[$ij][$ih]['jenis_layanan']; // for SMS PUSH
  1070.                     else if($service_category == '450000001') { $jenis_service = $datasDivisi[$ij][$ih]['jenis_layanan']; } // for Email PUSH
  1071.                     else return redirect()->back()->with('alert_warning', 'Service Category Not Available. Please Check Service Category !!! ');
  1072.  
  1073.                     if ($division_name == null || $division_name == '' || $division_name == "" || $division_name == "eof")  break;
  1074.                     
  1075.                     $client_coas                = $arrClient['client_coa'];
  1076.                     $client_code                = $arrClient['client_code'];
  1077.                     $client_names               = $arrClient['client_name'];
  1078.                     $client_domicile_address    = $arrClient['client_domicile_address'];
  1079.                     $materai_client             = $arrClient['materai'];
  1080.                     $lmbg                       = $arrClient['lmbnegara'];
  1081.                     $max_amount                 = $arrClient['max_amount'];
  1082.                     $invoice_rule               = $arrClient['invoice_rule'];
  1083.  
  1084.                     $countDatas = count($datasDivisi[$ij][$ih]);
  1085.  
  1086.                     if (isset($datasDivisi[$ij][$ih]['international'])) {
  1087.                         $countDatasHeader = $countDatas;
  1088.                     } else {
  1089.                         $countDatasHeader = $countDatas-1;
  1090.                     }
  1091.                     dd($countDatasHeader);
  1092.                     if ($countDatasHeader == 9) $countDatasHeader++;
  1093.                         // dd($countDatasHeader);
  1094.                         // if ($countDatasHeader == 9) $countDatasHeader--;
  1095.                     for ($ik = 3; $ik < $countDatasHeader -1; $ik++) 
  1096.                     {
  1097.                         $headers = [ucfirst($header[$ik])];
  1098.                         $heads = $headers[0];
  1099.                         $qtty = explode("_",$heads);
  1100.                         $itemsName = ucwords(join(" ", $qtty));
  1101.  
  1102.                         $headsEmail = explode(" ", $itemsName);
  1103.                         $email_blast = $headsEmail[0];
  1104.                         $email_lower = lcfirst($email_blast);
  1105.  
  1106.                         $heads_lowers = strtolower($headers[0]);
  1107.  
  1108.                         if($jenis_service == "SMS Premium" && $heads != 'International') {
  1109.                             $itemsName = $heads . " Premium"; 
  1110.                         } else {
  1111.                             $itemsName = $itemsName;
  1112.                         }
  1113.                         
  1114.                         $qtys = $datasDivisi[$ij][$ih][$heads_lowers];
  1115.                         $contains = str_contains($qtys, '-');
  1116.  
  1117.                         if($contains == true) {
  1118.                             $check = "true";
  1119.                         } else {
  1120.                             $check = "false";
  1121.                         }
  1122.  
  1123.                         if($check == "false")
  1124.                         {
  1125.                             $unitprice = 0;
  1126.                             if ($service_category == '430000001') {
  1127.                                 $getClientsPrice = $conn_am->select("Select a.*, b.service_name 
  1128.                                                             FROM db_sprint_account.client_price AS a 
  1129.                                                             LEFT JOIN `services` AS b ON a.service_code = b.service_code 
  1130.                                                             WHERE a.client_code = '".$client_code."' AND b.service_name = '".$jenis_service."' AND a.provider_code = '".$itemsName."' LIMIT 1");
  1131.                             } else if ($service_category == '450000001') {
  1132.                                 $getClientsPrice = $conn_am->select("Select a.*, b.service_name 
  1133.                                                             FROM db_sprint_account.client_price AS a 
  1134.                                                             LEFT JOIN `services` AS b ON a.service_code = b.service_code 
  1135.                                                             WHERE a.client_code = '".$client_code."' AND b.service_name = '".$email_blast."' AND a.provider_code = '".$email_blast."' LIMIT 1");
  1136.                             } else {
  1137.                                 $getClientsPrice = $conn_am->select("Select a.*, b.service_name 
  1138.                                                             FROM db_sprint_account.client_price AS a 
  1139.                                                             LEFT JOIN db_sprint_account.`services` AS b ON a.service_code = b.service_code 
  1140.                                                             WHERE a.client_code = '".$client_code."' AND b.service_name = '".$jenis_service."' AND a.provider_code = '".$heads."' LIMIT 1");
  1141.  
  1142.                             }
  1143.                             
  1144.                             if( !empty($getClientsPrice) )
  1145.                             {
  1146.  
  1147.                                 $prc_type   = $getClientsPrice[0]->price_type;
  1148.                                 $hrgPrc     = $getClientsPrice[0]->price;
  1149.                                 $arryPrice  = json_decode($hrgPrc);
  1150.                                 $countPrice = count($arryPrice);
  1151.                                 $vats       = $getClientsPrice[0]->vat;
  1152.                             } else {
  1153.                                 $prc_type   = 1;
  1154.                                 $arryPrice  = [0];
  1155.                                 $countPrice = count($arryPrice);
  1156.                                 $vats       = 'exclude';
  1157.                             }
  1158.                             
  1159.                             if ($prc_type == 1) {
  1160.                                 $unitprice = $arryPrice[0];
  1161.                             } else if ($prc_type == 2) {
  1162.                                 $unitprice = round(($arryPrice[0] * $arryPrice[1] ));
  1163.                             } else if ($prc_type == 3) {
  1164.                                 for ($p = 0; $p < $countPrice ; $p++) 
  1165.                                 {
  1166.                                     $trfct = intval($tot_traffict);
  1167.                                     $tr1 = intval($arryPrice[$p][0]);
  1168.                                     $tr2 = intval($arryPrice[$p][1]);
  1169.                                     $hrg = intval($arryPrice[$p][2]);
  1170.  
  1171.                                     if ($trfct >= $tr1 && $trfct <= $tr2 ) {
  1172.                                         $unitprice = $hrg;
  1173.                                     } else {
  1174.                                         if ($unitprice > 0 ) {
  1175.                                             $unitprice = $unitprice;
  1176.                                         } else {
  1177.                                             $unitprice = 0;
  1178.                                         }
  1179.                                     }
  1180.                                 }
  1181.                             }
  1182.  
  1183.                                 
  1184.                             $tot_hrg = $unitprice * $qtys;
  1185.  
  1186.                             $item_desc_temp[$ij][$ih][] = [     
  1187.                                 "item_name" => $itemsName, 
  1188.                                 "quantity" => $qtys, 
  1189.                                 "unitprice" => $unitprice, 
  1190.                                 "total" => $tot_hrg,
  1191.                             ];
  1192.  
  1193.                             if($vats == 'exclude') {
  1194.                                 $new_ppns = floor($tot_hrg * 0.11);
  1195.                             } else {
  1196.                                 $new_ppns = 0;
  1197.                             }
  1198.  
  1199.                             $sum_quantity = $sum_quantity + $qtys;
  1200.                             $sum_uprice = $sum_uprice + $unitprice;
  1201.  
  1202.                             $sum_total = $sum_total + $tot_hrg;
  1203.                             $new_ppn  = $new_ppn + $new_ppns;
  1204.  
  1205.  
  1206.                         } else {
  1207.                             continue;
  1208.                         }
  1209.  
  1210.                     }
  1211.                     
  1212.                     $data_items[$ij][] = [
  1213.                         "item"      => $produk,
  1214.                         "item_desc" => $item_desc_temp[$ij][$ih]
  1215.  
  1216.                     ];
  1217.  
  1218.                 }
  1219.  
  1220.                 if ($ij > 0) {
  1221.                     continue;
  1222.                 }
  1223.                 $json = [
  1224.                             "detail" => [
  1225.                                 "info"  => [
  1226.                                     "from"         => "PT Sprint Asia",
  1227.                                     "recipient"    => $client_names,
  1228.                                     "reff_no"      => '',
  1229.                                     "cust_id"      => '',
  1230.                                     "sales_person" => ''
  1231.                                 ],
  1232.                                 "data"  => array($data_items[$ij]),
  1233.                                 "total" => [
  1234.                                     "quantity"  => $sum_quantity,
  1235.                                     "unitprice" => $sum_uprice,
  1236.                                     "sub_total" => ceil($sum_total),
  1237.                                     "with_ppn"  => floor($new_ppn),
  1238.                                     "with_pph"  => 0
  1239.  
  1240.                                 ]
  1241.                             ],
  1242.  
  1243.                         ];
  1244.  
  1245.                 $serialize = json_encode($json);
  1246.  
  1247.  
  1248.                 $data_div           = $this->get_data_division_by_name_and_client_code($division_name, $client_code);
  1249.                 if ($data_div == null || $data_div == '' || $data_div == "") {
  1250.                     return redirect()->back()->with('alert_warning', 'Please Check Data Divisi ' . $division_name .' Again. File Cannot be Executed.');;
  1251.                 }
  1252.  
  1253.                 
  1254.                 //Division
  1255.                 $division_names         = $data_div['division_name'];
  1256.                 $division_code          = $data_div['division_code'];
  1257.                 $bank_acc_number_sprint = $data_div['division_bank_acc_number_sprint'];
  1258.                 $bank_acc_name_sprint   = $data_div['division_bank_acc_name_sprint'];
  1259.                 $bank_coa_sprint        = $data_div['division_bank_coa_sprint'];
  1260.                 $zahir_name             = $data_div['division_zahir_name'];
  1261.                 $client_npwp_no         = $data_div['division_npwp'];
  1262.                 $bank_acc_name_client   = $data_div['division_bank_account_name'];
  1263.                 $bank_acc_number_client = $data_div['division_bank_account_number'];
  1264.                 $bank_name              = $data_div['division_bank_name_sprint'];
  1265.                 $client_coa             = $data_div['division_bank_name_sprint'];
  1266.                 $division_address       = $data_div['division_address'];
  1267.                 $division_invoice_rule  = $data_div['division_invoice_rule']; 
  1268.                 $division_max_amount    = $data_div['division_max_amount'];
  1269.                 $division_description   = $data_div['division_description'];
  1270.                 $ba                     = $data_div['berita_acara'];
  1271.                 if($ba == 1){$ba = "Yes";}else{$ba = "No";}
  1272.                 $fp                     = $data_div['faktur_pajak'];
  1273.                 if($fp == 1){$fp = "Yes";}else{$fp = "No";}
  1274.                 $softcopy               = $data_div['soft_copy'];
  1275.                 if($softcopy == 1){$softcopy = "Yes";}else{$softcopy = "No";}
  1276.                 $hardcopy               = $data_div['hard_copy'];
  1277.                 if($hardcopy == 1){$hardcopy = "Yes";}else{$hardcopy = "No";}
  1278.                 
  1279.                 $dept_code              = "$division_code-00";
  1280.                 $data_dept              = $this->get_data_dept_by_id($dept_code);
  1281.                 
  1282.                 if ($data_dept == null || $data_dept == '' || $data_dept == "") {
  1283.                     return redirect()->back()->with('alert_warning', 'Please Check Data Department '.$dept_code.' Again. File Cannot be Executed.');
  1284.                 }
  1285.                 
  1286.                 $dept_code = $data_dept['dept_code'];
  1287.                 $dept_names = $data_dept['dept_name'];
  1288.  
  1289.                 //PIC 
  1290.                 $data_pic      = $this->get_data_pics_by_id($division_code);
  1291.                 if ($data_pic == null || $data_pic == '' || $data_pic == "" || $data_pic == []) {
  1292.                     return redirect()->back()->with('alert_warning', 'Please Check Data PIC from Division '.$division_code.'. File Cannot be Executed.');
  1293.                 }
  1294.  
  1295.                 $pic_id       = $data_pic['pic_id'];
  1296.                 $pic_name     = $data_pic['pic_name'];
  1297.                 $email_client = $data_pic['pic_email'];
  1298.                 $pic_position = $data_pic['pic_position'];
  1299.  
  1300.                 $data_pic_collection = $this->get_data_pic_collection($division_code);
  1301.  
  1302.                 if ($data_pic_collection == null || $data_pic_collection == '' || $data_pic_collection == "") {
  1303.                     $pic_collection_name        = "";
  1304.                     $email_pic_collection       = $email_client;
  1305.                 }
  1306.                 else {
  1307.                     if( !empty ($data_pic_collection['pic_name']) ) $pic_collection_name        = $data_pic_collection['pic_name'];
  1308.                     else return redirect()->back()->with('alert_warning', 'Please Check pic_type_id at Pic Name ' . $division_name .' Again. File Cannot be Executed.');;
  1309.                     $email_pic_collection       = $data_pic_collection['pic_email'];
  1310.  
  1311.                 }
  1312.  
  1313.                 $data_pic_pph23 = $this->get_data_pic_pph23($division_code);
  1314.                 // echo "</br></br> \n ~~~~~ data_pic_pph23 : "; print_r($data_pic_pph23) ; echo "~~~~~  </br> \n ";
  1315.                 if ($data_pic_pph23 == null || $data_pic_pph23 == '' || $data_pic_pph23 == "") {
  1316.                     $pic_pph23_name        = "";
  1317.                     $email_pic_pph23       = $email_client;
  1318.                 }
  1319.                 else {
  1320.                     $pic_pph23_name        = $data_pic_pph23['pic_name'];
  1321.                     $email_pic_pph23       = $data_pic_pph23['pic_email'];
  1322.                 }
  1323.  
  1324.                 $sub_total = ceil($sum_total);
  1325.                 $amount_invoice_rule = $sub_total + $new_ppn;
  1326.                 
  1327.                 if($new_ppn > 0){
  1328.                     $vats = "exclude";
  1329.                 }else if($new_ppn <= 0){
  1330.                     $vats = "include";
  1331.                 }
  1332.                 
  1333.                 //cek BUMN/Lembaga
  1334.                 $real                   = $this->real_field($lmbg, $invoice_rule, $max_amount, $amount_invoice_rule, $sub_total, $vats);
  1335.  
  1336.                 $sub_total_now          = $real[0];
  1337.                 $sub_total_real         = $real[1];
  1338.                 $new_ppn_now            = $real[2];
  1339.                 $new_ppn_real           = $real[3];
  1340.  
  1341.                 $new_materai = 0;
  1342.  
  1343.                 if ($materai_client == 1) $new_materai = $this->check_materai($sub_total_now, $new_ppn_now);
  1344.                 else if ($materai_client     == 0) $new_materai = 0;
  1345.  
  1346.                 $pph23   = $this->check_pph23($client_code, $sub_total_now);
  1347.  
  1348.                 $Total = round($sub_total_now + $new_ppn_now + $new_materai - $pph23);
  1349.                 $Total_real = round($sub_total_real + $new_ppn_real + $new_materai - $pph23);
  1350.                 $now = \Carbon\Carbon::now();
  1351.                 $descManual = "-";
  1352.                 
  1353.                 if($materai_client == 1 ) $materai = "yes"; else $materai = "no";
  1354.                 
  1355.  
  1356.                 $invXls[] = array(
  1357.                     "client_code"               => $client_code,
  1358.                     "client_name"               => $client_names,
  1359.                     "division_code"             => $division_code,
  1360.                     "division_name"             => $division_names,
  1361.                     "division_description"      => $division_description,
  1362.                     "dept_code"                 => $dept_code,
  1363.                     "dept_name"                 => $dept_names,
  1364.                     "pic_id"                    => $pic_id,
  1365.                     "pic_name"                  => $pic_name,
  1366.                     "pic_position"              => $pic_position,
  1367.                     "inv_month"                 => $month,  //date("m"),
  1368.                     "inv_year"                  => $year, //date("yy"),
  1369.                     "unitprice"                 => $sum_uprice,
  1370.                     "quantity"                  => $sum_quantity,
  1371.                     "sub_total"                 => ceil($sub_total_now),
  1372.                     "ppn"                       => floor($new_ppn_now),
  1373.                     "sub_total_real"            => ceil($sub_total_real),
  1374.                     "ppn_real"                  => floor($new_ppn_real),
  1375.                     "materai"                   => $new_materai,
  1376.                     "pph23"                     => $pph23,
  1377.                     "total"                     => $Total,
  1378.                     "total_real"                => $Total_real,
  1379.                     "serialize_data"            => $serialize,
  1380.                     "selisih"                   => 0,
  1381.                     "status"                    => 0,
  1382.                     "addmanual"                 => 2,
  1383.                     "division_address"          => $division_address,
  1384.                     "bank_acc_number_sprint"    => $bank_acc_number_sprint,
  1385.                     "bank_acc_name_sprint"      => $bank_acc_name_sprint,
  1386.                     "bank_acc_number_client"    => $bank_acc_number_client,
  1387.                     "bank_acc_name_client"      => $bank_acc_name_client,
  1388.                     "bank_coa_sprint"           => $bank_coa_sprint,
  1389.                     "zahir_name"                => $zahir_name,
  1390.                     "client_npwp_no"            => $client_npwp_no,
  1391.                     "bank_name"                 => $bank_name,
  1392.                     "client_coa"                => $client_coas,
  1393.                     "revenue_coa"               => $revenue_coa,
  1394.                     "email_client"              => $email_client,
  1395.                     "email_collection"          => $email_pic_collection,
  1396.                     "email_pph23"               => $email_pic_pph23,
  1397.                     "description"               => $produk,
  1398.                     "created_at"                => $now,
  1399.                     "created_by"                => $username ." - Upload Manual Invoice",
  1400.                     "id_collection"             =>'[""]',
  1401.                     "ba"                        => $ba,
  1402.                     "fp"                        => $fp,
  1403.                     "softcopy"                  => $softcopy,
  1404.                     "hardcopy"                  => $hardcopy
  1405.  
  1406.                 );
  1407.             }
  1408.             //// END Men Serialize Data
  1409.  
  1410.             // return $invXls;
  1411.             if (!empty($invXls)) {
  1412.                 DB::table('db_sprint_ar.invoice')->insert($invXls);
  1413.  
  1414.                 $username                       = session()->get('profile')['name'];
  1415.                 $now                            = Carbon\Carbon::now('Asia/Jakarta');
  1416.                 
  1417.                 $saved_log_datas = preg_replace( "/\r|\n/", " ", json_encode($invXls) );
  1418.                 $saved_log_datas = str_replace("\\", "", $saved_log_datas);
  1419.                 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 ]);
  1420.                 
  1421.             }
  1422.             DB::commit();
  1423.             $up_manualInvoice   = [
  1424.                 "path_name" => $path_name,
  1425.                 "file_name" => $nameFileUpload,
  1426.                 "size"      => $size,
  1427.                 "created_at"    => now()
  1428.  
  1429.             ];
  1430.             
  1431.             $insert_upload_ = DB::table('upload_manual_invoice')->insert($up_manualInvoice);
  1432.             return redirect()->back()->with('alert_import', 'Import file success');
  1433.  
  1434.         } 
  1435.         else 
  1436.         {
  1437.             return redirect()->back()->with('alert_warning', 'Something wrong, excel only');
  1438.         }
  1439.                
  1440.         }
  1441.  
  1442.         public function upload_tes(Request $request)
  1443.     {
  1444.                  try {
  1445.                         if ($request->input('sc_upload_manual') == "" || $request->input('sc_upload_manual') == "-" || $request->input('sc_upload_manual') == null || $request->input('sc_upload_manual') == '') {
  1446.                                 //return redirect()->back()->with('alert', 'Service Category Tidak Dapat Dipilih. Silahkan Periksa di Account Management. Data Tidak Tersimpan.');
  1447.                                         return redirect('/upload-manual-invoice')->with('error', 'Service Category Tidak Dapat Dipilih. Silahkan Periksa di Account Management. Data Tidak Tersimpan.');
  1448.                         }
  1449.                        
  1450.             $File               = $request->file('file_upload_manual');
  1451.             $revenue_coa        = $request->input('sc_upload_manual');
  1452.             
  1453.            
  1454.             $nameFileUpload     = $File->getClientOriginalName();
  1455.             $path_name          = Storage::put('/upload_manual_invoice', $File);
  1456.             $size               = $File->getSize();
  1457.             
  1458.             $nameFile = explode("_", $nameFileUpload);
  1459.             // return $nameFile;
  1460.  
  1461.             $client_code       = $nameFile[0];
  1462.             $month              = $nameFile[1];
  1463.             $fake_year          = $nameFile[2];
  1464.                        
  1465.                         $fake = explode(".", $fake_year);
  1466.                        
  1467.                         $year = $fake[0];
  1468.  
  1469.             $results = Excel::selectSheetsByIndex(0)->load($File)->get();
  1470.  
  1471.             $header = $results->getHeading(); //get xls's header
  1472.  
  1473.                     $extension          = $File->getClientOriginalExtension();
  1474.             $size               = $File->getSize();
  1475.             $path               = $File->getRealPath();
  1476.             // return $path ;
  1477.  
  1478.             if ($extension == "xlsx" || $extension == "xls") {
  1479.                 $datas = Excel::load($path)->get(); //load all of the data in sheet excel file
  1480.                 $index = 0;
  1481.                 // return $datas;
  1482.                                 if ($datas->count() != 0) {
  1483.                    
  1484.                     DB::beginTransaction();
  1485.                     $invXls = array();
  1486.                     foreach ($datas as $data) {
  1487.  
  1488.                         // try {
  1489.                             $division_name            = $data['divisi'];
  1490.                             $item_name                = $data['produk'];
  1491.                             $service_name             = $data['jenis_sms'];
  1492.  
  1493.                             $qty_Hutchinson           = $data[$header[3]];
  1494.                             $qty_Indosat              = $data[$header[4]];
  1495.                             $qty_Telkomsel            = $data[$header[5]];
  1496.                             $qty_Smartfren            = $data[$header[6]];
  1497.                             $qty_Xl                   = $data[$header[7]];
  1498.                                                         // $qty_Inter                = $data[$header[8]];
  1499.                            // $qty_Satelindo            = $data[$header[9]];
  1500.                                                        
  1501.                                                          
  1502.                                                         if ($division_name == null || $division_name == '' || $division_name == "" || $division_name == "eof") {
  1503.                                 break;
  1504.                              }
  1505.  
  1506.                             //  if ($end_of_per == "eof" || $channel_1 == "eof" || $channel_2 == "eof" || $channel_4 == "eof") {
  1507.                             //     continue;
  1508.                             //  }
  1509.                                                          
  1510.                             $temp_item_desc = [];
  1511.                             for ($q = 3; $q < 8; $q++) {
  1512.                                 if ($data[$header[$q]] != null) {
  1513.  
  1514.                                     $uppercased = Str::ucfirst($header[$q]); //set the first alfabet as an uppercase 
  1515.                                     $temp_item_desc[] = [$uppercased, $data[$header[$q]]];
  1516.                                 }
  1517.                             }
  1518.                                                        
  1519.                             $arrClient = $this->get_data_client_by_id($client_code);
  1520.                                                         if ($arrClient == null || $arrClient == '' || $arrClient == "") {
  1521.                                                                 return redirect()->back()->with('alert', 'Please Check Client Code Again. File Cannot be Executed.');;
  1522.                             }
  1523.                             // return $arrClient;
  1524.                                                        
  1525.                             $client_coas                           = $arrClient['client_coa'];
  1526.                             $client_code                           = $arrClient['client_code'];
  1527.                             $client_names                          = $arrClient['client_name'];
  1528.                             $client_domicile_address       = $arrClient['client_domicile_address'];
  1529.                             $materai_client                        = $arrClient['materai'];
  1530.                                                         $lmbg                                      = $arrClient['lmbnegara'];
  1531.                                                         $max_amount                            = $arrClient['max_amount'];
  1532.                                                         $invoice_rule                          = $arrClient['invoice_rule'];
  1533.  
  1534.                             $sum_quantity                          = 0;
  1535.                             $sum_uprice                            = 0;
  1536.                             $sum_total                                     = 0;
  1537.                             $new_ppn                                       = 0;
  1538.                                                        
  1539.                             $item_d = [];
  1540.                             foreach ($temp_item_desc as $temp_id) {
  1541.                                 
  1542.                                                                 $arrServiceSMS = $this->get_data_service_sms($service_name);
  1543.                                                                
  1544.                                                                 if ($arrServiceSMS == null || $arrServiceSMS == '' || $arrServiceSMS == "") {
  1545.                                                                         return redirect()->back()->with('alert', 'Please Check Data Jenis SMS Again. File Cannot be Executed.');;
  1546.                                                                 }
  1547.  
  1548.                                                                 $service_code = $arrServiceSMS['service_code'];
  1549.                                                                
  1550.                                                                 if($temp_id[0] == "Hutchinson" && $service_code == "S101"){ //s101 premium - s102 reguler
  1551.                                                                         $service_code = "S102";
  1552.                                                                 }else if($temp_id[0] == "Smartfren" && $service_code == "S101"){
  1553.                                                                         $service_code = "S102";
  1554.                                                                 }
  1555.                                                                 else if($temp_id[0] == "Hutchinson" && $service_code == "S102"){
  1556.                                                                         $service_code = "S102";
  1557.                                                                 }else if($temp_id[0] == "Smartfren" && $service_code == "S102"){
  1558.                                                                         $service_code = "S102";
  1559.                                                                 }                                              
  1560.                                                                
  1561.                                 $arrPrice = $this->get_data_price($client_code, $temp_id[0], $service_code); 
  1562.                                 
  1563.                                                                 if ($arrPrice == null || $arrPrice == '' || $arrPrice == "") {
  1564.                                                                         return redirect()->back()->with('alert', 'Please Check Data Provider '.$temp_id[0].' Again. File Cannot be Executed.');;
  1565.                                                                 }
  1566.                                 // return $arrPrice;
  1567.                                                                
  1568.                                                                 $price_type     = $arrPrice['price_type'];
  1569.                                 $vat            = $arrPrice['vat'];
  1570.                                 $tmp_uprice     = $arrPrice['price'];
  1571.                                 $price          = json_decode($tmp_uprice);
  1572.                                 
  1573.                                                        
  1574.                                 if ($price_type == 1) {
  1575.                                     $p          = $price[0];
  1576.                                     $real_price = (int)$p;
  1577.                                 } else if ($price_type == 2) {
  1578.                                     $p          = $price[0];
  1579.                                     $amount     = 0;
  1580.                                     $real_price = (int)$p * $amount;
  1581.                                 } else if ($price_type == 3) {
  1582.                                     $traffict   = $temp_id[1];
  1583.  
  1584.                                     for($p = 0; $p < count($price); $p++){
  1585.                                         $from   = $price[$p][0]; 
  1586.                                         $to     = $price[$p][1];
  1587.                                         // $price  = $price[$p][2]; 
  1588.                                         
  1589.                                         if($traffict > $from && $traffict <= $to){
  1590.                                             $real_price = $price[$p][2];
  1591.                                         }else{
  1592.                                             continue;
  1593.                                         }
  1594.                                     }
  1595.  
  1596.                                 }else if($price_type == 4){
  1597.                                     $traffict   = $temp_id[1];
  1598.  
  1599.                                     for($p = 0; $p < count($price); $p++){
  1600.                                         $from   = $price[$p][0]; 
  1601.                                         $to     = $price[$p][1];
  1602.                                         // $price  = $price[$p][2]; 
  1603.                                         
  1604.                                         if($traffict > $from && $traffict <= $to){
  1605.                                             $real_price = $traffict * $price[$p][2];
  1606.                                         }else{
  1607.                                             continue;
  1608.                                         }
  1609.                                     }
  1610.                                 }
  1611.  
  1612.                                
  1613.                                 $tot = $temp_id[1] * $real_price;
  1614.                                                                
  1615.                                 $item_d[] = [
  1616.                                     "item_name" => $temp_id[0],
  1617.                                     "quantity"  => $temp_id[1],
  1618.                                     "unitprice" => $real_price,
  1619.                                     "total"     => $tot
  1620.                                 ];
  1621.  
  1622.                                 $sum_quantity   += $temp_id[1];
  1623.  
  1624.                                 $sum_uprice     += $real_price;
  1625.                                 $sum_total      += $tot;
  1626.                                                                
  1627.                                                                 $new_ppns       = $this->check_ppn($vat, $tot);
  1628.                                                                 $new_ppn        += $new_ppns;
  1629.                             }
  1630.  
  1631.                             $it = [
  1632.                                 "item"      => $data['produk'],
  1633.                                 "item_desc" => $item_d
  1634.                             ];
  1635.  
  1636.                             $json = [
  1637.                                 "detail" => [
  1638.                                     "info"  => [
  1639.                                         "from"         => "PT Sprint Asia",
  1640.                                         "recipient"    => $client_names,
  1641.                                         "reff_no"      => '',
  1642.                                         "cust_id"      => '',
  1643.                                         "sales_person" => ''
  1644.                                     ],
  1645.                                     "data"  => array(array($it)),
  1646.                                     "total" => [
  1647.                                         "quantity"  => $sum_quantity,
  1648.                                         "unitprice" => $sum_uprice,
  1649.                                         "sub_total" => ceil($sum_total),
  1650.                                         "with_ppn"  => floor($new_ppn),
  1651.                                         "with_pph"  => 0
  1652.  
  1653.                                     ]
  1654.                                 ],
  1655.  
  1656.                             ];
  1657.                             $serialize = json_encode($json);
  1658.                             
  1659.                            
  1660.                             $data_div           = $this->get_data_division_by_name_and_client_code($division_name, $client_code);
  1661.                                                         if ($data_div == null || $data_div == '' || $data_div == "") {
  1662.                                                                 return redirect()->back()->with('alert', 'Please Check Data Divisi ' . $division_name .' Again. File Cannot be Executed.');;
  1663.                                                         }
  1664.                             
  1665.                             //Division
  1666.                             $division_names         = $data_div['division_name'];
  1667.                             $division_code          = $data_div['division_code'];
  1668.                             $bank_acc_number_sprint = $data_div['division_bank_acc_number_sprint'];
  1669.                             $bank_acc_name_sprint   = $data_div['division_bank_acc_name_sprint'];
  1670.                             $bank_coa_sprint        = $data_div['division_bank_coa_sprint'];
  1671.                             $zahir_name             = $data_div['division_zahir_name'];
  1672.                             $client_npwp_no         = $data_div['division_npwp'];
  1673.                             $bank_acc_name_client   = $data_div['division_bank_account_name'];
  1674.                             $bank_acc_number_client = $data_div['division_bank_account_number'];
  1675.                             $bank_name              = $data_div['division_bank_name_sprint'];
  1676.                             $client_coa             = $data_div['division_bank_name_sprint'];
  1677.                             $division_address       = $data_div['division_address'];
  1678.                                                         $division_invoice_rule  = $data_div['division_invoice_rule']; 
  1679.                                                         $division_max_amount    = $data_div['division_max_amount'];
  1680.                             $division_description   = $data_div['division_description'];
  1681.                             $ba                     = $data_div['berita_acara'];
  1682.                             if($ba == 1){$ba = "Yes";}else{$ba = "No";}
  1683.                             $fp                     = $data_div['faktur_pajak'];
  1684.                             if($fp == 1){$fp = "Yes";}else{$fp = "No";}
  1685.                             $softcopy               = $data_div['soft_copy'];
  1686.                             if($softcopy == 1){$softcopy = "Yes";}else{$softcopy = "No";}
  1687.                             $hardcopy               = $data_div['hard_copy'];
  1688.                             if($hardcopy == 1){$hardcopy = "Yes";}else{$hardcopy = "No";}
  1689.                                                        
  1690.                                                         $dept_code              = "$division_code-00";
  1691.                                                         $data_dept              = $this->get_data_dept_by_id($dept_code);
  1692.                                                        
  1693.                                                         if ($data_dept == null || $data_dept == '' || $data_dept == "") {
  1694.                                                                 return redirect()->back()->with('alert', 'Please Check Data Department '.$dept_code.' Again. File Cannot be Executed.');
  1695.                                                         }
  1696.                                                        
  1697.                                                         $dept_code = $data_dept['dept_code'];
  1698.                             $dept_names = $data_dept['dept_name'];
  1699.  
  1700.                             //PIC 
  1701.                             $data_pic      = $this->get_data_pics_by_id($division_code);
  1702.                                                         if ($data_pic == null || $data_pic == '' || $data_pic == "") {
  1703.                                                                 return redirect()->back()->with('alert', 'Please Check Data PIC from Division '.$division_code.'. File Cannot be Executed.');
  1704.                                                         }
  1705.                             $pic_id       = $data_pic['pic_id'];
  1706.                             $pic_name     = $data_pic['pic_name'];
  1707.                             $email_client = $data_pic['pic_email'];
  1708.                             $pic_position = $data_pic['pic_position'];
  1709.  
  1710.                             $data_pic_collection = $this->get_data_pic_collection($division_code);
  1711.                             if ($data_pic_collection == null || $data_pic_collection == '' || $data_pic_collection == "") {
  1712.                                                                 $pic_collection_name        = "";
  1713.                                 $email_pic_collection       = $email_client;
  1714.                                                         }
  1715.                             $pic_collection_name        = $data_pic_collection['pic_name'];
  1716.                             $email_pic_collection       = $data_pic_collection['pic_email'];
  1717.  
  1718.                             $data_pic_pph23 = $this->get_data_pic_pph23($division_code);
  1719.                             if ($data_pic_pph23 == null || $data_pic_pph23 == '' || $data_pic_pph23 == "") {
  1720.                                                                 $pic_pph23_name        = "";
  1721.                                 $email_pic_pph23       = $email_client;
  1722.                                                         }
  1723.                             $pic_pph23_name        = $data_pic_pph23['pic_name'];
  1724.                             $email_pic_pph23       = $data_pic_pph23['pic_email'];
  1725.  
  1726.                             $sub_total = ceil($sum_total);
  1727.                                                         $amount_invoice_rule = $sub_total + $new_ppn;
  1728.                                                        
  1729.                                                         if($new_ppn > 0){
  1730.                                                                 $vats = "exclude";
  1731.                                                         }else if($new_ppn <= 0){
  1732.                                                                 $vats = "include";
  1733.                                                         }
  1734.                                                        
  1735.                                                         //cek BUMN/Lembaga
  1736.                                                         $real                                  = $this->real_field($lmbg, $invoice_rule, $max_amount, $amount_invoice_rule, $sub_total, $vats);
  1737.                        
  1738.                                                         $sub_total_now                  = $real[0];
  1739.                                                         $sub_total_real                 = $real[1];
  1740.                                                         $new_ppn_now                    = $real[2];
  1741.                                                         $new_ppn_real                   = $real[3];
  1742.                        
  1743.                                                         $new_materai = 0;
  1744.                             if ($materai_client     == 1) {
  1745.                                 $new_materai = $this->check_materai($sub_total_now, $new_ppn_now);
  1746.                             } else if ($materai_client     == 0) {
  1747.                                 $new_materai = 0;
  1748.                             }
  1749.                             $pph23   = $this->check_pph23($client_code, $sub_total_now);
  1750.  
  1751.                             $Total = round($sub_total_now + $new_ppn_now + $new_materai - $pph23);
  1752.                                                         $Total_real = round($sub_total_real + $new_ppn_real + $new_materai - $pph23);
  1753.                             $now = \Carbon\Carbon::now();
  1754.                             $descManual = "-";
  1755.                                                        
  1756.                                                         if($materai_client == 1 ){
  1757.                                                                 $materai = "yes";
  1758.                                                                
  1759.                                                         }else{
  1760.                                                                 $materai = "no";
  1761.                                                                
  1762.                                                         }
  1763.                                                        
  1764.                             $manual_invoice = [
  1765.                                 "client_code"   => $client_code,
  1766.                                 "dept_code"     => $dept_code,
  1767.                                 "division_code" => $division_code,
  1768.                                 "desc"          => $descManual,
  1769.                                 "vat"           => $vat, //?
  1770.                                 "materai"       => $materai, //?
  1771.                                 "total"         => $Total
  1772.                             ];
  1773.  
  1774.                             $last_id = DB::table("db_sprint_ar.manual_invoice")->insertGetId($manual_invoice);
  1775.  
  1776.                             $invXls[] = array(
  1777.                                 "client_code"                 => $client_code,
  1778.                                 "client_name"                 => $client_names,
  1779.                                 "division_code"               => $division_code,
  1780.                                 "division_name"               => $division_names,
  1781.                                 "division_description"        => $division_description,
  1782.                                 "dept_code"                   => $dept_code,
  1783.                                 "dept_name"                   => $dept_names,
  1784.                                 "pic_id"                      => $pic_id,
  1785.                                 "pic_name"                    => $pic_name,
  1786.                                 "pic_position"                => $pic_position,
  1787.                                 "inv_month"                   => $month,  //date("m"),
  1788.                                 "inv_year"                    => $year, //date("yy"),
  1789.                                 "unitprice"                   => $sum_uprice,
  1790.                                 "quantity"                    => $sum_quantity,
  1791.                                 "sub_total"                   => ceil($sub_total_now),
  1792.                                 "ppn"                         => floor($new_ppn_now),
  1793.                                                                 "sub_total_real"              => ceil($sub_total_real),
  1794.                                 "ppn_real"                    => floor($new_ppn_real),
  1795.                                 "materai"                     => $new_materai,
  1796.                                 "pph23"                       => $pph23,
  1797.                                 "total"                       => $Total,
  1798.                                                                 "total_real"                  => $Total_real,
  1799.                                 "serialize_data"              => $serialize,
  1800.                                 "selisih"                     => 0,
  1801.                                 "status"                      => 0,
  1802.                                 "addmanual"                   => 1,
  1803.                                 "id_manual"                   => $last_id,
  1804.                                 "division_address"            => $division_address,
  1805.                                 "bank_acc_number_sprint"      => $bank_acc_number_sprint,
  1806.                                 "bank_acc_name_sprint"               => $bank_acc_name_sprint,
  1807.                                 "bank_acc_number_client"      => $bank_acc_number_client,
  1808.                                 "bank_acc_name_client"        => $bank_acc_name_client,
  1809.                                 "bank_coa_sprint"             => $bank_coa_sprint,
  1810.                                 "zahir_name"                  => $zahir_name,
  1811.                                 "client_npwp_no"              => $client_npwp_no,
  1812.                                 "bank_name"                   => $bank_name,
  1813.                                 "client_coa"                  => $client_coas,
  1814.                                 "revenue_coa"                 => $revenue_coa,
  1815.                                 "email_client"                => $email_client,
  1816.                                 "email_collection"            => $email_pic_collection,
  1817.                                 "email_pph23"                 => $email_pic_pph23,
  1818.                                 "description"                 => $item_name,
  1819.                                 "created_at"                  => $now,
  1820.                                                                 "id_collection"               =>'[""]',
  1821.                                 "ba"                      => $ba,
  1822.                                 "fp"                      => $fp,
  1823.                                 "softcopy"                => $softcopy,
  1824.                                 "hardcopy"                => $hardcopy
  1825.  
  1826.                             );
  1827.  
  1828.                             // return $invXls;
  1829.                            
  1830.                                                         $manual_detail = [];
  1831.                                                         for($f = 0; $f < count($item_d); $f++){
  1832.                                                                 if($f != 0){
  1833.                                                                         $item_name = null;
  1834.                                                                        
  1835.                                                                 }
  1836.                                                                
  1837.                                                                 $manual_detail[] = [
  1838.                                                                                 "id_manual"         => $last_id,
  1839.                                                                                 "service"           => $item_d[$f]['item_name'] ,
  1840.                                                                                 "quantity"          => $item_d[$f]['quantity'],
  1841.                                                                                 "unit_price"        => $item_d[$f]['unitprice'],
  1842.                                                                                 "desc"              => $item_name,
  1843.                                                                                 "total"             => $item_d[$f]['total']
  1844.                                                                         ];
  1845.                                                         }      
  1846.                                                        
  1847.                                                         $last_id = DB::table("db_sprint_ar.manual_detail")->insert($manual_detail);
  1848.                         // } catch (Exception $e) {
  1849.                         //     return redirect()->back()->with('alert', 'Please Check Your Data Again. File Cannot be Executed.');
  1850.                         // }
  1851.                     }
  1852.                 }
  1853.  
  1854.                
  1855.                                 // return $invXls;
  1856.                 if (!empty($invXls)) {
  1857.                     DB::table('db_sprint_ar.invoice')->insert($invXls);
  1858.                                        
  1859.                 }
  1860.                 DB::commit();
  1861.                 $up_manualInvoice   = [
  1862.                     "path_name" => $path_name,
  1863.                     "file_name" => $nameFileUpload,
  1864.                     "size"      => $size,
  1865.                     "created_at"    => now()
  1866.     
  1867.                 ];
  1868.                 
  1869.                 $insert_upload_ = DB::table('upload_manual_invoice')->insert($up_manualInvoice);
  1870.                 return redirect()->back()->with('alert', 'Import File Success.');
  1871.             } else {
  1872.                 return redirect()->back()->with('alert', 'Something Wrong. Excel Only.');
  1873.             }
  1874.         } catch (Exception $e) {
  1875.             DB::rollback();
  1876.         }
  1877.                
  1878.     }
  1879.     
  1880.     public function upload_manual_invoice_load_data(){
  1881.         try {
  1882.             $datas = [];
  1883.             // 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();
  1884.             $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();
  1885.             foreach ($file_upload as $key => $fu) {
  1886.                 $datas[] = [
  1887.                     "file_name"     => $fu->file_name,
  1888.                     "size"          => $fu->size,
  1889.                     "created_at"    => date($fu->created_at),
  1890.                     
  1891.                 ];
  1892.             }
  1893.             return $datas;
  1894.         } catch (Exception $e) {
  1895.             return [];
  1896.         }
  1897.     }
  1898.        
  1899.     public function get_data_manual_invoice($id)
  1900.     {
  1901.         $top = DB::table('db_sprint_ar.manual_invoice AS a')
  1902.             ->where('a.id', $id)
  1903.             ->join('db_sprint_ar.invoice AS b', 'b.id_manual', 'a.id')
  1904.             ->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') 
  1905.             ->get();
  1906.  
  1907.         $details = DB::table('db_sprint_ar.manual_detail')
  1908.             ->where('id_manual', $id)
  1909.             ->select('id_manual', 'service', 'quantity', 'unit_price', 'total', 'desc')
  1910.             ->get()
  1911.             ->groupBy('id_manual');
  1912.  
  1913.         foreach ($top as  $d) {
  1914.             foreach ($details[$d->id] as $dt) {
  1915.                 $id_desc = $this->get_id_desc($dt->service);
  1916.                 $datas[] = [
  1917.                     "id_service"     => $id_desc,
  1918.                     "service"        => $dt->service,
  1919.                     "quantity"       => $dt->quantity,
  1920.                     "unit_price"     => $dt->unit_price,
  1921.                     "total"          => $dt->total,
  1922.                                         "desc"           => $dt->desc
  1923.                 ];
  1924.             }
  1925.  
  1926.             return $data = [
  1927.                 "id"                   => $d->id,
  1928.                 "client_code"          => $d->client_code,
  1929.                 "client_name"          => $d->client_name,
  1930.                 "dept_code"            => $d->dept_code,
  1931.                 "division_code"        => $d->division_code,
  1932.                 "desc"                 => $d->desc,
  1933.                 "vat"                  => $d->vat,
  1934.                 "materai"              => $d->materai,
  1935.                 "coa_of_category"      => $d->revenue_coa,
  1936.                 "bhp"                              => $d->bhp,
  1937.                 "uso"                              => $d->uso,
  1938.                 "details"              => $datas
  1939.             ];
  1940.         }
  1941.     }
  1942.        
  1943.         public function real_field($lmbg, $invoice_rule, $max_amount, $amount_invoice_rule, $sub_total, $vat){
  1944.         $sub_total_now = 0; $sub_total_real = 0; $new_ppn_now = 0; $new_ppn_real = 0;
  1945.                 if(($lmbg == "yes" || $lmbg == "Yes" ) && $invoice_rule == 1 && $vat == 'exclude' ){ //exclude
  1946.                                 if($sub_total > $max_amount){
  1947.                                         $sub_total_now                 = ceil($sub_total);
  1948.                                         $sub_total_real        = ceil($sub_total);
  1949.                                         $new_ppn_now           = floor($sub_total * 0.11);
  1950.                                         $new_ppn_real          = 0; //ke sprint
  1951.                                 }else{
  1952.                                         $sub_total_now                 = ceil($sub_total);
  1953.                                         $sub_total_real        = ceil($sub_total); 
  1954.                                         $new_ppn_now           = floor($sub_total * 0.11);
  1955.                                         $new_ppn_real          = floor($sub_total_real * 0.11);
  1956.                                 }
  1957.                         }else if(($lmbg == "yes" || $lmbg == "Yes" ) && $invoice_rule == 1 && $vat == 'include' ){ //include
  1958.                                 if($sub_total > $max_amount){
  1959.                                         $sub_total_now                 = ceil($sub_total);
  1960.                                         $sub_total_real        = ceil($sub_total / 1.11); 
  1961.                                         $new_ppn_now           = 0; //$sub_total * 0.1;
  1962.                                         $new_ppn_real          = 0; //$sub_total * 0.1;
  1963.                                 }else{
  1964.                                         $sub_total_now                 = ceil($sub_total);
  1965.                                         $sub_total_real        = ceil($sub_total / 1.11); 
  1966.                                         $new_ppn_now           = 0;
  1967.                                         $new_ppn_real          = floor($sub_total_real * 0.11);
  1968.                                 }
  1969.                         }else if(($lmbg == "yes" || $lmbg == "Yes" ) && $invoice_rule == 2 && $vat == 'exclude'){ //exclude
  1970.                                 if($amount_invoice_rule > $max_amount){
  1971.                                         $sub_total_now                 = ceil($sub_total);
  1972.                                         $sub_total_real        = ceil($sub_total);
  1973.                                         $new_ppn_now           = floor($sub_total * 0.11);
  1974.                                         $new_ppn_real          = 0; //ke sprint
  1975.                                 }else{
  1976.                                         $sub_total_now                 = ceil($sub_total);
  1977.                                         $sub_total_real        = ceil($sub_total); 
  1978.                                         $new_ppn_now           = floor($sub_total * 0.11);
  1979.                                         $new_ppn_real          = floor($sub_total_real * 0.11);
  1980.                                 }
  1981.                         }else if(($lmbg == "yes" || $lmbg == "Yes" ) && $invoice_rule == 2 && $vat == 'include' ){ //include
  1982.                                 if($amount_invoice_rule > $max_amount){
  1983.                                         $sub_total_now                 = ceil($sub_total);
  1984.                                         $sub_total_real        = ceil($sub_total / 1.11); 
  1985.                                         $new_ppn_now           = 0; //$sub_total * 0.1;
  1986.                                         $new_ppn_real          = 0; //$sub_total * 0.1;
  1987.                                 }else{
  1988.                                         $sub_total_now                 = ceil($sub_total);
  1989.                                         $sub_total_real        = ceil($sub_total / 1.11); 
  1990.                                         $new_ppn_now           = 0;
  1991.                                         $new_ppn_real          = floor($sub_total_real * 0.11);
  1992.                                 }
  1993.                         }else if(($lmbg == "no" || $lmbg == null || $lmbg == '' || $lmbg == "No") && $vat == 'exclude'){ //exlude
  1994.                                
  1995.                                         $sub_total_now                 = ceil($sub_total);
  1996.                                         $sub_total_real        = ceil($sub_total); /// 1.1; 
  1997.                                         $new_ppn_now           = floor($sub_total * 0.11);
  1998.                                         $new_ppn_real          = floor($sub_total_real * 0.11);
  1999.                                
  2000.                         }else if(($lmbg == "no" || $lmbg == null || $lmbg == '' || $lmbg == "No") && $vat == 'include'){ //include
  2001.                                
  2002.                                         $sub_total_now                 = ceil($sub_total);
  2003.                                         $sub_total_real        = ceil($sub_total / 1.11); 
  2004.                                         $new_ppn_now           = 0; //$sub_total * 0.1;
  2005.                                         $new_ppn_real          = floor($sub_total_real * 0.11);
  2006.                                
  2007.                         }
  2008.                        
  2009.                         $real   = [$sub_total_now, $sub_total_real, $new_ppn_now, $new_ppn_real];
  2010.                        
  2011.                         return $real;
  2012.                
  2013.         }
  2014.  
  2015.     public function get_data_manual_detail($id)
  2016.     {
  2017.         return $dtdetail = DB::table('manual_detail')
  2018.             ->join('db_sprint_account.services', 'db_sprint_account.services.service_code', '=', 'manual_detail.service')
  2019.             ->where('manual_detail.id_manual', $id)
  2020.             ->get();
  2021.     }
  2022.     public function get_data_sc(Request $request)
  2023.     {
  2024.         $sc = new \GuzzleHttp\Client(['base_uri' => env('HTTP_URL_CLIENT') . 'getservice_category']);
  2025.         $headers = [
  2026.             'Authorization' => 'Bearer ' . $_COOKIE["token"],
  2027.             'Accept'        => 'application/json',
  2028.         ];
  2029.         $response = $sc->request('GET', '', [
  2030.             'headers' => $headers,
  2031.             // 'query' => ['client_code' => $code]
  2032.         ]);
  2033.         $dtsc = json_decode($response->getBody()->getContents(), true)['data'];
  2034.         return json_encode($dtsc);
  2035.     }
  2036.  
  2037.     public function get_data_sc_e($code)
  2038.     {
  2039.         $sc_e = new \GuzzleHttp\Client(['base_uri' => 'https://backoffice.sprintasia.net:82/sprint-sysadmin/public/api-master/getservice_category_e']);
  2040.         $headers = [
  2041.             'Authorization' => 'Bearer ' . $_COOKIE["token"],
  2042.             'Accept'        => 'application/json',
  2043.         ];
  2044.         $response = $sc_e->request('GET', '', [
  2045.             'headers' => $headers,
  2046.             'query' => ['revenue_coa' => $code]
  2047.         ]);
  2048.         $dtsc_e = json_decode($response->getBody()->getContents(), true)['data'];
  2049.         return json_encode($dtsc_e);
  2050.     }
  2051.  
  2052.     public function get_data_division($code)
  2053.     {
  2054.         $client = new \GuzzleHttp\Client(['base_uri' => env('HTTP_URL_CLIENT') . 'get_data_division']);
  2055.         $headers = [
  2056.             'Authorization' => 'Bearer ' . $_COOKIE["token"],
  2057.             'Accept'        => 'application/json',
  2058.         ];
  2059.         $response = $client->request('GET', '', [
  2060.             'headers' => $headers,
  2061.             'query' => ['client_code' => $code]
  2062.         ]);
  2063.         $dtclient = json_decode($response->getBody()->getContents(), true)['data'];
  2064.         return json_encode($dtclient);
  2065.     }
  2066.  
  2067.     public function get_data_dept_name($code)
  2068.     {
  2069.         $client = new \GuzzleHttp\Client(['base_uri' =>  env('HTTP_URL_CLIENT') . 'get_data_dept']);
  2070.         $headers = [
  2071.             'Authorization' => 'Bearer ' . $_COOKIE["token"],
  2072.             'Accept'        => 'application/json',
  2073.         ];
  2074.         $response = $client->request('GET', '', [
  2075.             'headers' => $headers,
  2076.             'query' => ['division_code' => $code]
  2077.         ]);
  2078.         $dtclient = json_decode($response->getBody()->getContents(), true)['data'];
  2079.         return json_encode($dtclient);
  2080.     }
  2081.  
  2082.     public function get_data_service()
  2083.     {
  2084.         $dtservs = ServiceCategory::select('service_category_code', 'service_category_name', 'coa_of_category')->get();
  2085.         return response()->json($dtservs);
  2086.     }
  2087.  
  2088.     public function get_client_name($id)
  2089.     {
  2090.         $nameclient = DB::table('clients')->select('client_code', 'client_name')->where('client_code', '=', $id)->get();
  2091.         return response()->json($nameclient);
  2092.     }
  2093.  
  2094.     public function get_service_category(Request $request)
  2095.     {
  2096.         $client = new \GuzzleHttp\Client(['base_uri' => 'https://backoffice.sprintasia.net:82/sprint-sysadmin/public/api-master/get_all_service']);
  2097.         $headers = [
  2098.             'Authorization' => 'Bearer ' . $_COOKIE["token"],
  2099.             'Accept'        => 'application/json',
  2100.         ];
  2101.         $response = $client->request('GET', '', [
  2102.             'headers' => $headers,
  2103.             'query' => ['client_name' => $request["vclient"]]
  2104.         ]);
  2105.         $dtclient = json_decode($response->getBody()->getContents(), true)['data'];
  2106.         return json_encode($dtclient);
  2107.     }
  2108.  
  2109.     public function serviceCategoryAutocomplete(Request $request)
  2110.     {
  2111.         $scategory = new \GuzzleHttp\Client(['base_uri' => 'https://backoffice.sprintasia.net:82/sprint-sysadmin/public/api-master/getservicecategory/']);
  2112.         $headers = [
  2113.             'Authorization' => 'Bearer ' . $_COOKIE["token"],
  2114.             'Accept'        => 'application/json',
  2115.         ];
  2116.         $response = $scategory->request('GET', '', [
  2117.             'headers' => $headers,
  2118.             'query' => ['service_category' => $request["sc_name"]]
  2119.         ]);
  2120.         return json_decode($response->getBody()->getContents(), true)['data'];
  2121.     }
  2122.  
  2123.     public function get_data_division_2($code)
  2124.     {
  2125.         $client = new \GuzzleHttp\Client(['base_uri' => 'https://backoffice.sprintasia.net:82/sprint-sysadmin/public/api-master/get_data_all_division_2']);
  2126.         $headers = [
  2127.             'Authorization' => 'Bearer ' . $_COOKIE["token"],
  2128.             'Accept'        => 'application/json',
  2129.         ];
  2130.         $response = $client->request('GET', '', [
  2131.             'headers' => $headers,
  2132.             'query' => ['client_code' => $code]
  2133.         ]);
  2134.         $dtclient = json_decode($response->getBody()->getContents(), true)['data'];
  2135.         return json_encode($dtclient);
  2136.     }
  2137.  
  2138.     public function get_data_dept_2($code)
  2139.     {
  2140.         $client = new \GuzzleHttp\Client(['base_uri' => 'https://backoffice.sprintasia.net:82/sprint-sysadmin/public/api-master/get_data_all_dept_2']);
  2141.         $headers = [
  2142.             'Authorization' => 'Bearer ' . $_COOKIE["token"],
  2143.             'Accept'        => 'application/json',
  2144.         ];
  2145.         $response = $client->request('GET', '', [
  2146.             'headers' => $headers,
  2147.             'query' => ['division_code' => $code]
  2148.         ]);
  2149.         $dtclient = json_decode($response->getBody()->getContents(), true)['data'];
  2150.         return json_encode($dtclient);
  2151.     }
  2152.  
  2153.     public function ServiceAutocomplete(Request $request)
  2154.     {
  2155.         $Uri = new \GuzzleHttp\Client(['base_uri' => 'https://backoffice.sprintasia.net:82/sprint-sysadmin/public/api-master/getcustomerautocomplete/']);
  2156.         $headers = [
  2157.             'Authorization' => 'Bearer ' . $_COOKIE["token"],
  2158.             'Accept'        => 'application/json',
  2159.         ];
  2160.         $response = $Uri->request('GET', '', [
  2161.             'headers' => $headers,
  2162.             'query' => ['client_name' => $request["vclient"]]
  2163.         ]);
  2164.         $data = json_decode($response->getBody()->getContents(), true)['data'];
  2165.         return json_encode($dtclient);
  2166.     }
  2167.  
  2168.     public function get_data_division_by_id($division_code)
  2169.     {
  2170.         try {
  2171.             $Uri = new \GuzzleHttp\Client(['base_uri' => 'https://backoffice.sprintasia.net:82/sprint-sysadmin/public/api-master/get_data_division_by_id']);
  2172.             $headers = [
  2173.                 'Authorization' => 'Bearer ' . $_COOKIE["token"],
  2174.                 'Accept'        => 'application/json',
  2175.             ];
  2176.             $response = $Uri->request('GET', '', [
  2177.                 'headers' => $headers,
  2178.                 'query' => ['division_code' => $division_code]
  2179.             ]);
  2180.             return $data = json_decode($response->getBody()->getContents(), true)['data'][0];
  2181.         } catch (Exception $e) {
  2182.             return [];
  2183.         }
  2184.     } // end f
  2185.  
  2186.     public function get_data_pics_by_id($division_code)
  2187.     {
  2188.         try {
  2189.             $Uri = new \GuzzleHttp\Client(['base_uri' => env('HTTP_URL_CLIENT') . 'get_data_pics_by_id']);
  2190.             $headers = [
  2191.                 'Authorization' => 'Bearer ' . $_COOKIE["token"],
  2192.                 'Accept'        => 'application/json',
  2193.             ];
  2194.             $response = $Uri->request('GET', '', [
  2195.                 'headers' => $headers,
  2196.                 'query' => ['division_code' => $division_code]
  2197.             ]);
  2198.             return $data = json_decode($response->getBody()->getContents(), true)['data'][0];
  2199.         } catch (Exception $e) {
  2200.             return [];
  2201.         }
  2202.     } // end f
  2203.  
  2204.     public function get_data_client_by_id($client_code)
  2205.     {   
  2206.         // return $_COOKIE["token"];
  2207.         try {
  2208.             $Uri = new \GuzzleHttp\Client(['base_uri' => env('HTTP_URL_CLIENT') . 'get_data_client_by_id']);
  2209.             $headers = [
  2210.                 'Authorization' => 'Bearer ' . $_COOKIE["token"],
  2211.                 'Accept'        => 'application/json',
  2212.             ];
  2213.             $response = $Uri->request('GET', '', [
  2214.                 'headers' => $headers,
  2215.                 'query' => ['client_code' => $client_code]
  2216.             ]);
  2217.             return $data = json_decode($response->getBody()->getContents(), true)['data'][0];
  2218.         } catch (Exception $e) {
  2219.             return [];
  2220.         }
  2221.     } // end f
  2222.  
  2223.     public function get_data_dept_by_id($dept_code)
  2224.     {
  2225.         try {
  2226.             $Uri = new \GuzzleHttp\Client(['base_uri' => env('HTTP_URL_CLIENT') . 'get_data_dept_by_id']);
  2227.             $headers = [
  2228.                 'Authorization' => 'Bearer ' . $_COOKIE["token"],
  2229.                 'Accept'        => 'application/json',
  2230.             ];
  2231.             $response = $Uri->request('GET', '', [
  2232.                 'headers' => $headers,
  2233.                 'query' => ['dept_code' => $dept_code]
  2234.             ]);
  2235.             return $data = json_decode($response->getBody()->getContents(), true)['data'][0];
  2236.         } catch (Exception $e) {
  2237.             return [];
  2238.         }
  2239.     }
  2240.  
  2241.     public function check_ppn($vat, $sub_total)
  2242.     {
  2243.         if ($vat == 'exclude') {
  2244.             return $ppn = $sub_total * 0.11;
  2245.         } else {
  2246.             return $ppn = 0;
  2247.         } // End PPN
  2248.     }
  2249.  
  2250.     public function check_materai($sub_total, $ppn)
  2251.     {
  2252.         $math = $sub_total + $ppn;
  2253.         if ($math < 5000000) {
  2254.             return $materai = 0;
  2255.         }else if ($math >= 5000000) {
  2256.             return $materai = 10000;
  2257.         } // end if
  2258.     }
  2259.  
  2260.     public function check_materai_old($sub_total, $ppn)
  2261.     {
  2262.         $math = $sub_total + $ppn;
  2263.         if ($math < 250000) {
  2264.             return $materai = 0;
  2265.         } else if ($math < 1000000) {
  2266.             return $materai = 3000;
  2267.         } else if ($math >= 1000000) {
  2268.             return $materai = 6000;
  2269.         } // end if
  2270.     } // end function
  2271.  
  2272.     public function check_pph23($client_code, $sub_total)
  2273.     {
  2274.         try {
  2275.             $Uri = new \GuzzleHttp\Client(['base_uri' => env('HTTP_URL_CLIENT') . 'get_pph23']); // Get Template Invoice from Account Management, Parameter id Template Invoice
  2276.             $headers = [
  2277.                 'Authorization' => 'Bearer ' . $_COOKIE["token"],
  2278.                 'Accept'        => 'application/json',
  2279.             ];
  2280.             $response     = $Uri->request('GET', '', [
  2281.                 'headers'   => $headers,
  2282.                 'query'     => ['client_code' => $client_code]
  2283.             ]);
  2284.             $incomeTaxData = json_decode($response->getBody()->getContents(), true)['data'][0];
  2285.             $incomeTax = $incomeTaxData['income_tax_23'];
  2286.         } catch (Exception $e) {
  2287.             // Log::error("Function Error",["name"=>__FUNCTION__, "message"=>$e->getMessage()]);
  2288.             $incomeTax = 0;
  2289.         }
  2290.         if ($incomeTax !== null) {
  2291.             return $sub_total * ($incomeTaxData['income_tax_23'] / 100);
  2292.         } else {
  2293.             return 0;
  2294.         }
  2295.     }// end f
  2296.  
  2297.     public function get_revenue_coa($service_code)
  2298.     {
  2299.         try {
  2300.             $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
  2301.             $headers = [
  2302.                 'Authorization' => 'Bearer ' . $_COOKIE["token"],
  2303.                 'Accept'        => 'application/json',
  2304.             ];
  2305.             $response     = $Uri->request('GET', '', [
  2306.                 'headers'   => $headers,
  2307.                 'query'     => ['service_code ' => $service_code]
  2308.             ]);
  2309.             return json_decode($response->getBody()->getContents(), true)['data'][0];
  2310.         } catch (Exception $e) {
  2311.             return '';
  2312.         }
  2313.     } // end f
  2314.  
  2315.     public function get_data_dept_by_name($dept_name)
  2316.     {
  2317.         try {
  2318.             $Uri = new \GuzzleHttp\Client(['base_uri' => 'https://backoffice.sprintasia.net:82/sprint-sysadmin/public/api-master/get_data_dept_by_name_2']);
  2319.             $headers = [
  2320.                 'Authorization' => 'Bearer ' . $_COOKIE["token"],
  2321.                 'Accept'        => 'application/json',
  2322.             ];
  2323.             $response = $Uri->request('GET', '', [
  2324.                 'headers' => $headers,
  2325.                 'query' => ['dept_name' => $dept_name]
  2326.             ]);
  2327.             return $data = json_decode($response->getBody()->getContents(), true)['data'][0];
  2328.         } catch (Exception $e) {
  2329.             return [];
  2330.         }
  2331.     } // end f
  2332.  
  2333.     public function get_data_division_by_name($division_name)
  2334.     {
  2335.         try {
  2336.             $Uri = new \GuzzleHttp\Client(['base_uri' => 'https://backoffice.sprintasia.net:82/sprint-sysadmin/public/api-master/get_data_division_by_name']);
  2337.             $headers = [
  2338.                 'Authorization' => 'Bearer ' . $_COOKIE["token"],
  2339.                 'Accept'        => 'application/json',
  2340.             ];
  2341.             $response = $Uri->request('GET', '', [
  2342.                 'headers' => $headers,
  2343.                 'query' => ['division_name' => $division_name]
  2344.             ]);
  2345.             return $data = json_decode($response->getBody()->getContents(), true)['data'][0];
  2346.         } catch (Exception $e) {
  2347.             return [];
  2348.         }
  2349.     } // end f
  2350.  
  2351.     public function get_data_division_by_name_and_client_code($division_name, $client_code)
  2352.     {
  2353.         try {
  2354.             $Uri = new \GuzzleHttp\Client(['base_uri' => env('HTTP_URL_CLIENT') . 'get_data_division_by_name_and_client_code']);
  2355.             $headers = [
  2356.                 'Authorization' => 'Bearer ' . $_COOKIE["token"],
  2357.                 'Accept'        => 'application/json',
  2358.             ];
  2359.             $response = $Uri->request('GET', '', [
  2360.                 'headers' => $headers,
  2361.                 'query' => ['division_name' => $division_name, 'client_code' => $client_code]
  2362.             ]);
  2363.             return $data = json_decode($response->getBody()->getContents(), true)['data'][0];
  2364.         } catch (Exception $e) {
  2365.             return [];
  2366.         }
  2367.     } // end f
  2368.  
  2369.     public function get_data_client_by_name($client_name)
  2370.     {
  2371.         try {
  2372.             $Uri = new \GuzzleHttp\Client(['base_uri' => 'https://backoffice.sprintasia.net:82/sprint-sysadmin/public/api-master/get_data_client_by_name']);
  2373.             $headers = [
  2374.                 'Authorization' => 'Bearer ' . $_COOKIE["token"],
  2375.                 'Accept'        => 'application/json',
  2376.             ];
  2377.             $response = $Uri->request('GET', '', [
  2378.                 'headers' => $headers,
  2379.                 'query' => ['client_name' => $client_name]
  2380.             ]);
  2381.             return $data = json_decode($response->getBody()->getContents(), true)['data'][0];
  2382.         } catch (Exception $e) {
  2383.             return [];
  2384.         }
  2385.     } // end f
  2386.  
  2387.     public function get_data_service_category_by_name($service_category_name)
  2388.     {
  2389.         try {
  2390.             $Uri = new \GuzzleHttp\Client(['base_uri' => 'https://backoffice.sprintasia.net:82/sprint-sysadmin/public/api-master/get_data_service_category_by_name']);
  2391.             $headers = [
  2392.                 'Authorization' => 'Bearer ' . $_COOKIE["token"],
  2393.                 'Accept'        => 'application/json',
  2394.             ];
  2395.             $response = $Uri->request('GET', '', [
  2396.                 'headers' => $headers,
  2397.                 'query' => ['service_category_name' => $service_category_name]
  2398.             ]);
  2399.             return $data = json_decode($response->getBody()->getContents(), true)['data'][0];
  2400.         } catch (Exception $e) {
  2401.             return [];
  2402.         }
  2403.     } // end f
  2404.  
  2405.     public function get_data_dept_by_client_code_division_code($division_code, $client_code)
  2406.     {
  2407.         try {
  2408.             $Uri = new \GuzzleHttp\Client(['base_uri' => 'https://backoffice.sprintasia.net:82/sprint-sysadmin/public/api-master/get_data_dept_by_client_code']);
  2409.             $headers = [
  2410.                 'Authorization' => 'Bearer ' . $_COOKIE["token"],
  2411.                 'Accept'        => 'application/json',
  2412.             ];
  2413.             $response = $Uri->request('GET', '', [
  2414.                 'headers' => $headers,
  2415.                 'query' => ['division_code' => $division_code, 'client_code' => $client_code]
  2416.             ]);
  2417.             return $data = json_decode($response->getBody()->getContents(), true)['data'][0];
  2418.         } catch (Exception $e) {
  2419.             return [];
  2420.         }
  2421.     } // end f
  2422.  
  2423.     public function get_data_price($client_code, $provider_code, $service_code)
  2424.     {
  2425.         // return $service_code;
  2426.         try {
  2427.             $Uri = new \GuzzleHttp\Client(['base_uri' => 'https://backoffice.sprintasia.net:82/sprint-sysadmin/public/api-master/check_vat']);
  2428.             $headers = [
  2429.                 'Authorization' => 'Bearer ' . $_COOKIE["token"],
  2430.                 'Accept'        => 'application/json',
  2431.             ];
  2432.             $response = $Uri->request('GET', '', [
  2433.                 'headers' => $headers,
  2434.                 'query' => ['client_code' => $client_code, 'provider_code' => $provider_code, 'service_code' => $service_code]
  2435.             ]);
  2436.             return $data = json_decode($response->getBody()->getContents(), true)['data'][0];
  2437.         } catch (Exception $e) {
  2438.             return [];
  2439.         }
  2440.     } // end f
  2441.  
  2442.     public function get_data_service_sms($service_name)
  2443.     {
  2444.         try {
  2445.             $Uri = new \GuzzleHttp\Client(['base_uri' => 'https://backoffice.sprintasia.net:82/sprint-sysadmin/public/api-master/get_data_service_sms']);
  2446.             $headers = [
  2447.                 'Authorization' => 'Bearer ' . $_COOKIE["token"],
  2448.                 'Accept'        => 'application/json',
  2449.             ];
  2450.             $response = $Uri->request('GET', '', [
  2451.                 'headers' => $headers,
  2452.                 'query' => ['service_name' => $service_name]
  2453.             ]);
  2454.             return $data = json_decode($response->getBody()->getContents(), true)['data'][0];
  2455.         } catch (Exception $e) {
  2456.             return [];
  2457.         }
  2458.     } // end f
  2459.  
  2460.     public function get_data_pic_collection($division_code)
  2461.     {
  2462.         try {
  2463.             $Uri = new \GuzzleHttp\Client(['base_uri' => 'https://backoffice.sprintasia.net:82/sprint-sysadmin/public/api-master/get_data_pic_collection']);
  2464.             $headers = [
  2465.                 'Authorization' => 'Bearer ' . $_COOKIE["token"],
  2466.                 'Accept'        => 'application/json',
  2467.             ];
  2468.             $response = $Uri->request('GET', '', [
  2469.                 'headers' => $headers,
  2470.                 'query' => ['division_code' => $division_code]
  2471.             ]);
  2472.             return $data = json_decode($response->getBody()->getContents(), true)['data'][0];
  2473.         } catch (Exception $e) {
  2474.             return [];
  2475.         }
  2476.     } // end f 
  2477.  
  2478.     public function get_data_pic_pph23($division_code)
  2479.     {
  2480.         try {
  2481.             $Uri = new \GuzzleHttp\Client(['base_uri' => env('HTTP_URL_CLIENT') . 'get_data_pic_pph23']);
  2482.             $headers = [
  2483.                 'Authorization' => 'Bearer ' . $_COOKIE["token"],
  2484.                 'Accept'        => 'application/json',
  2485.             ];
  2486.             $response = $Uri->request('GET', '', [
  2487.                 'headers' => $headers,
  2488.                 'query' => ['division_code' => $division_code]
  2489.             ]);
  2490.             return $data = json_decode($response->getBody()->getContents(), true)['data'][0];
  2491.         } catch (Exception $e) {
  2492.             return [];
  2493.         }
  2494.     } // end f
  2495.  
  2496.     public function zek_view(Request $request)
  2497.     {
  2498.  
  2499.         if (empty($_COOKIE["token"])) {
  2500.             return redirect("https://backoffice.sprintasia.net:82/sprint-sysadmin/public/login");
  2501.         } else {
  2502.             $title = "Invoice";
  2503.             $gen = "Cek Paydate Invoice";
  2504.             $content = "Cek Paydate Invoice";
  2505.  
  2506.  
  2507.             $data = DB::table('db_sprint_ar.manual_invoice AS m')
  2508.                 ->join('db_sprint_ar.invoice AS i', 'i.id_manual', 'm.id')
  2509.                 ->select('i.*', 'm.*', 'i.created_at AS tanggal')
  2510.                 ->get();
  2511.             $dtdepts = DB::table('invoice')->select('dept_code', 'dept_name')->get();
  2512.             $dtclients = DB::table('invoice')->select('client_code', 'client_name')->get();
  2513.             $dtdivs = DB::table('invoice')->select('division_code', 'division_name')->get();
  2514.             $dtservs = ServiceCategory::select('service_category_code', 'service_category_name')->get();
  2515.  
  2516.             // return view('manual_invoice.index', compact('title','content','gen','data','dtdepts', 'dtclients', 'dtdivs', 'dtservs'));
  2517.             return view('manual_invoice.index_paydate', compact('title', 'content', 'gen', 'data', 'dtdepts', 'dtclients', 'dtdivs', 'dtservs'));
  2518.         }
  2519.     }
  2520.        
  2521.         public function cek_paydate(Request $request)
  2522.     {
  2523.  
  2524.         try {
  2525.             $File               = $request->file('file_cek'); //file_upload
  2526.           
  2527.             $nameFileUpload     = $File->getClientOriginalName();
  2528.            
  2529.             $nameFile = explode("_", $nameFileUpload);
  2530.                         $dataFile = Excel::selectSheetsByIndex(0)->load($File, function ($reader) {
  2531.                 $reader->setHeaderRow(0);
  2532.                 $reader->select(['invoice_no', 'total','paid_date'])->get();
  2533.             })->get();
  2534.             $extension          = $File->getClientOriginalExtension();
  2535.             $size               = $File->getSize();
  2536.             $path                 = $File->getRealPath();
  2537.             return $dataFile;
  2538.             if ($extension == "xlsx" || $extension == "xls") {
  2539.                 // $datas = Excel::load($path)->get(); //load all of the data in sheet excel file
  2540.                 $index = 0;
  2541.                                 // return $datas;
  2542.                 if ($dataFile->count() != 0) {
  2543.                     DB::beginTransaction();
  2544.                     $invXls = array();
  2545.                     foreach ($dataFile as $data) {
  2546.                         try {
  2547.                             $invoice_no         = $data['invoice_no'];
  2548.                             $total              = $data['total'];
  2549.                             $pay_date            = $data['paid_date'];
  2550.                                                        
  2551.                                                         // return $invoice_no;
  2552.                             $cekInv = DB::table("db_sprint_ar.invoice")->where('no_inv', $invoice_no)->get();
  2553.                             // return $cekInv;
  2554.                                                         // return count($cekInv);
  2555.                                                        
  2556.                                                         if(count($cekInv) == 0){
  2557.                                                                 continue;
  2558.                                                                 // return "null";
  2559.                                                         }
  2560.                                                        
  2561.                             DB::table("db_sprint_ar.invoice")->where('no_inv', $invoice_no)->update(["paydate" => $pay_date]);
  2562.                             
  2563.                             $invXls[] = array(
  2564.                                 "no_inv"                   => $invoice_no,
  2565.                                 "paydate"                 => $pay_date,
  2566.                             );
  2567.  
  2568.                         } catch (Exception $e) {
  2569.                             return redirect()->back()->with('alert', 'Please Check Your Data Again. File Cannot be Executed.');
  2570.                         }
  2571.                     }
  2572.                 }
  2573.                 // return $invXls; 
  2574.                 DB::commit();
  2575.                 return redirect()->back()->with('alert', 'Paydate Success.');
  2576.             } else {
  2577.                 return redirect()->back()->with('alert', 'Something Wrong. Excel Only.');
  2578.             }
  2579.         } catch (Exception $e) {
  2580.             DB::rollback();
  2581.         }
  2582.     }
  2583.        
  2584.        
  2585.          public function updateInvoice(Request $request)
  2586.     {
  2587.                  $title = "Invoice";
  2588.             $gen = "Manual Invoice";
  2589.             $content = "Manual Invoice"; 
  2590.  
  2591.          return view('manual_invoice.update_invoice', compact('title', 'content', 'gen'));//, 'data', 'dtdepts', 'dtclients', 'dtdivs', 'dtservs'));
  2592.     }  // end f
  2593.        
  2594.         public function updateInvoiceNum_real(Request $request)
  2595.     { 
  2596.         try{
  2597.        
  2598.                 $jlh_invoice = DB::select( "SELECT count(id) from invoice");
  2599.  
  2600.                 $inv = DB::select( "SELECT * from invoice"); 
  2601.                 // return $inv;
  2602.                 $tes = [];
  2603.                 for($c = 0; $c < count($inv); $c++){
  2604.                         $dClient   = $this->get_data_client_by_name($inv[$c]->client_name);
  2605.                         if($dClient == null || $dClient  == '' || $dClient == ""){
  2606.                                 // $client_name = $dClient['client_name'];
  2607.                                 $lmbg = null;
  2608.                                 $invoice_rule = null;
  2609.                                 $max_amount = null;
  2610.                         }else{
  2611.                                 $lmbg = $dClient['lmbnegara'];
  2612.                                 $invoice_rule = $dClient['invoice_rule'];
  2613.                                 $max_amount = $dClient['max_amount'];
  2614.                         }
  2615.        
  2616.                         $idInv                                         = $inv[$c]->id;
  2617.                         $sub_total                             = $inv[$c]->sub_total;
  2618.                         $ppn                                   = $inv[$c]->ppn;
  2619.                         $total                                         = $inv[$c]->total;
  2620.                         $materai                               = $inv[$c]->materai;
  2621.                         $pph23                                  = $inv[$c]->pph23;
  2622.                         $amount_invoice_rule   = $sub_total + $ppn;
  2623.                         $serialize_datas                = json_decode($inv[$c]->serialize_data);
  2624.                         if($serialize_datas == null){
  2625.                                 $bhp                                   = 0;
  2626.                                 $uso                                   = 0;
  2627.                         }else{
  2628.                                 $total                                  = $serialize_datas->detail->total;
  2629.                                 if(array_key_exists('bhp', $total)){
  2630.                                         $bhp                                   = $total->bhp;
  2631.                                         $uso                                   = $total->uso;
  2632.                                 }else{
  2633.                                         $bhp                                   = 0;
  2634.                                         $uso                                   = 0;
  2635.                                 }
  2636.                         }
  2637.                         // return $uso;
  2638.  
  2639.                         if(($lmbg == "yes" || $lmbg == "Yes" ) && $invoice_rule == 1 && $ppn != 0 ){ //exclude
  2640.                                 if($sub_total > $max_amount){
  2641.                                         $sub_total_now = $sub_total;
  2642.                                         $sub_total_real = ceil($sub_total);
  2643.                                         $new_ppn_now = $sub_total * 0.1;
  2644.                                         $new_ppn_real = 0; //ke sprint
  2645.                                 }else{
  2646.                                         $sub_total_now = $sub_total;
  2647.                                         $sub_total_real = ceil($sub_total); 
  2648.                                         $new_ppn_now = $sub_total * 0.1;
  2649.                                         $new_ppn_real = floor($sub_total_real * 0.1);
  2650.                                 }
  2651.                         }else if(($lmbg == "yes" || $lmbg == "Yes" ) && $invoice_rule == 1 && $ppn == 0 ){ //include
  2652.                                 if($sub_total > $max_amount){
  2653.                                         $sub_total_now = $sub_total;
  2654.                                         $sub_total_real = ceil($sub_total / 1.1); 
  2655.                                         $new_ppn_now = 0; //$sub_total * 0.1;
  2656.                                         $new_ppn_real = 0;//$sub_total * 0.1;
  2657.                                 }else{
  2658.                                         $sub_total_now = $sub_total;
  2659.                                         $sub_total_real = ceil($sub_total / 1.1); 
  2660.                                         $new_ppn_now = 0;
  2661.                                         $new_ppn_real = floor($sub_total_real * 0.1);
  2662.                                 }
  2663.                         }else if(($lmbg == "yes" || $lmbg == "Yes" ) && $invoice_rule == 2 && $ppn != 0 ){ //exclude
  2664.                                 if($amount_invoice_rule > $max_amount){
  2665.                                         $sub_total_now = $sub_total;
  2666.                                         $sub_total_real = ceil($sub_total);
  2667.                                         $new_ppn_now = $sub_total * 0.1;
  2668.                                         $new_ppn_real = 0; //ke sprint
  2669.                                 }else{
  2670.                                         $sub_total_now = $sub_total;
  2671.                                         $sub_total_real = ceil($sub_total); 
  2672.                                         $new_ppn_now = $sub_total * 0.1;
  2673.                                         $new_ppn_real = floor($sub_total_real * 0.1);
  2674.                                 }
  2675.                         }else if(($lmbg == "yes" || $lmbg == "Yes" ) && $invoice_rule == 2 && $ppn == 0 ){ //include
  2676.                                 if($amount_invoice_rule > $max_amount){
  2677.                                         $sub_total_now = $sub_total;
  2678.                                         $sub_total_real = ceil($sub_total / 1.1); 
  2679.                                         $new_ppn_now = 0; //$sub_total * 0.1;
  2680.                                         $new_ppn_real = 0; //$sub_total * 0.1;
  2681.                                 }else{
  2682.                                         $sub_total_now = $sub_total;
  2683.                                         $sub_total_real = ceil($sub_total / 1.1); 
  2684.                                         $new_ppn_now = 0;
  2685.                                         $new_ppn_real = floor($sub_total_real * 0.1);
  2686.                                 }
  2687.                         }else if(($lmbg == "no" || $lmbg == null) && $ppn != 0 ){ //exclude
  2688.                                         $sub_total_now = $sub_total;
  2689.                                         $sub_total_real = ceil($sub_total); /// 1.1; 
  2690.                                         $new_ppn_now = $sub_total * 0.1;
  2691.                                         $new_ppn_real = floor($sub_total_real * 0.1);
  2692.                                
  2693.                         }else if(($lmbg == "no" || $lmbg == null) && $ppn == 0 ){ //include
  2694.                                         $sub_total_now = $sub_total;
  2695.                                         $sub_total_real = ceil($sub_total / 1.1); 
  2696.                                         $new_ppn_now = 0; //$sub_total * 0.1;
  2697.                                         $new_ppn_real = floor($sub_total_real * 0.1);
  2698.                         } //
  2699.                        
  2700.                                 $Total_now = $sub_total_now + $new_ppn_now + $materai - $pph23 - $bhp - $uso;
  2701.                 $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
  2702.                         // return $Total_real;
  2703.                         \DB::table('invoice')->where('id', $idInv)
  2704.                         ->update(['total_real' => $Total_real, 'sub_total_real' => $sub_total_real, 'ppn_real' => $new_ppn_real ]);                   
  2705.                 }
  2706.         return "suksses";
  2707.                 } catch (Exception $e) {
  2708.                         return ["code" => "_ERR_SYSTM", "error" => $e->getMessage()];
  2709.                 }      
  2710.     }  // end f
  2711.  
  2712.     public function updateInvoiceNum(Request $request)
  2713.     { 
  2714.         try{
  2715.        
  2716.                 //$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");
  2717.  
  2718.         //$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"); 
  2719.         //$inv = DB::select("SELECT count(*) FROM invoice WHERE sub_total_real IS NULL AND no_inv IS NOT NULL"); 
  2720.         $inv = DB::select("SELECT * FROM invoice WHERE sub_total_real IS NULL AND status = 2"); 
  2721.         //return $inv;
  2722.         
  2723.                 $tes = [];
  2724.                 for($c = 0; $c < count($inv); $c++){
  2725.                         $dClient   = $this->get_data_client_by_name($inv[$c]->client_name);
  2726.                         if($dClient == null || $dClient  == '' || $dClient == ""){
  2727.                                 // $client_name = $dClient['client_name'];
  2728.                                 $lmbg = null;
  2729.                                 $invoice_rule = null;
  2730.                                 $max_amount = null;
  2731.                         }else{
  2732.                                 $lmbg = $dClient['lmbnegara'];
  2733.                                 $invoice_rule = $dClient['invoice_rule'];
  2734.                                 $max_amount = $dClient['max_amount'];
  2735.                         }
  2736.        
  2737.                         $idInv                                         = $inv[$c]->id;
  2738.                         $sub_total                             = $inv[$c]->sub_total;
  2739.                         $ppn                                   = $inv[$c]->ppn;
  2740.                         $total                                         = $inv[$c]->total;
  2741.                         $materai                               = $inv[$c]->materai;
  2742.                         $pph23                                  = $inv[$c]->pph23;
  2743.                         $amount_invoice_rule   = $sub_total + $ppn;
  2744.                         $serialize_datas                = json_decode($inv[$c]->serialize_data);
  2745.                         if($serialize_datas == null){
  2746.                                 $bhp                                   = 0;
  2747.                                 $uso                                   = 0;
  2748.                         }else{
  2749.                                 $total                                  = $serialize_datas->detail->total;
  2750.                                 if(array_key_exists('bhp', $total)){
  2751.                                         $bhp                                   = $total->bhp;
  2752.                                         $uso                                   = $total->uso;
  2753.                                 }else{
  2754.                                         $bhp                                   = 0;
  2755.                                         $uso                                   = 0;
  2756.                                 }
  2757.                         }
  2758.                         // return $uso;
  2759.  
  2760.                         if(($lmbg == "yes" || $lmbg == "Yes" ) && $invoice_rule == 1 && $ppn != 0 ){ //exclude
  2761.                                 if($sub_total > $max_amount){
  2762.                                         $sub_total_now = $sub_total;
  2763.                                         $sub_total_real = ceil($sub_total);
  2764.                                         $new_ppn_now = $sub_total * 0.11;
  2765.                                         $new_ppn_real = 0; //ke sprint
  2766.                                 }else{
  2767.                                         $sub_total_now = $sub_total;
  2768.                                         $sub_total_real = ceil($sub_total); 
  2769.                                         $new_ppn_now = $sub_total * 0.11;
  2770.                                         $new_ppn_real = floor($sub_total_real * 0.11);
  2771.                                 }
  2772.                         }else if(($lmbg == "yes" || $lmbg == "Yes" ) && $invoice_rule == 1 && $ppn == 0 ){ //include
  2773.                                 if($sub_total > $max_amount){
  2774.                                         $sub_total_now = $sub_total;
  2775.                                         $sub_total_real = ceil($sub_total / 1.1); 
  2776.                                         $new_ppn_now = 0; //$sub_total * 0.1;
  2777.                                         $new_ppn_real = 0;//$sub_total * 0.1;
  2778.                                 }else{
  2779.                                         $sub_total_now = $sub_total;
  2780.                                         $sub_total_real = ceil($sub_total / 1.1); 
  2781.                                         $new_ppn_now = 0;
  2782.                                         $new_ppn_real = floor($sub_total_real * 0.11);
  2783.                                 }
  2784.                         }else if(($lmbg == "yes" || $lmbg == "Yes" ) && $invoice_rule == 2 && $ppn != 0 ){ //exclude
  2785.                                 if($amount_invoice_rule > $max_amount){
  2786.                                         $sub_total_now = $sub_total;
  2787.                                         $sub_total_real = ceil($sub_total);
  2788.                                         $new_ppn_now = $sub_total * 0.11;
  2789.                                         $new_ppn_real = 0; //ke sprint
  2790.                                 }else{
  2791.                                         $sub_total_now = $sub_total;
  2792.                                         $sub_total_real = ceil($sub_total); 
  2793.                                         $new_ppn_now = $sub_total * 0.11;
  2794.                                         $new_ppn_real = floor($sub_total_real * 0.11);
  2795.                                 }
  2796.                         }else if(($lmbg == "yes" || $lmbg == "Yes" ) && $invoice_rule == 2 && $ppn == 0 ){ //include
  2797.                                 if($amount_invoice_rule > $max_amount){
  2798.                                         $sub_total_now = $sub_total;
  2799.                                         $sub_total_real = ceil($sub_total / 1.1); 
  2800.                                         $new_ppn_now = 0; //$sub_total * 0.1;
  2801.                                         $new_ppn_real = 0; //$sub_total * 0.1;
  2802.                                 }else{
  2803.                                         $sub_total_now = $sub_total;
  2804.                                         $sub_total_real = ceil($sub_total / 1.1); 
  2805.                                         $new_ppn_now = 0;
  2806.                                         $new_ppn_real = floor($sub_total_real * 0.11);
  2807.                                 }
  2808.                         }else if(($lmbg == "no" || $lmbg == null) && $ppn != 0 ){ //exclude
  2809.                                         $sub_total_now = $sub_total;
  2810.                                         $sub_total_real = ceil($sub_total); /// 1.1; 
  2811.                                         $new_ppn_now = $sub_total * 0.1;
  2812.                                         $new_ppn_real = floor($sub_total_real * 0.11);
  2813.                                
  2814.                         }else if(($lmbg == "no" || $lmbg == null) && $ppn == 0 ){ //include
  2815.                                         $sub_total_now = $sub_total;
  2816.                                         $sub_total_real = ceil($sub_total / 1.1); 
  2817.                                         $new_ppn_now = 0; //$sub_total * 0.1;
  2818.                                         $new_ppn_real = floor($sub_total_real * 0.11);
  2819.                         } //
  2820.                        
  2821.                                 $Total_now = $sub_total_now + $new_ppn_now + $materai - $pph23 - $bhp - $uso;
  2822.                 $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
  2823.                 $now                   = \Carbon\Carbon::now();                                                                //
  2824.                         // return $Total_real;
  2825.                         \DB::table('invoice')->where('id', $idInv)
  2826.                         ->update(['total_real' => $Total_real, 'sub_total_real' => $sub_total_real, 'ppn_real' => $new_ppn_real, 'updated_at' => $now ]);                  
  2827.                 }
  2828.         return "suksses";
  2829.                 } catch (Exception $e) {
  2830.                         return ["code" => "_ERR_SYSTM", "error" => $e->getMessage()];
  2831.                 }      
  2832.     }  // end f
  2833.  
  2834.     public function updateInv(Request $request)
  2835.     { 
  2836.       try{
  2837.            // $jlh_invoice        = DB::select( "SELECT count(id) from invoice");
  2838.             $inv                   = DB::select( "SELECT * FROM invoice");
  2839.             // return $inv;
  2840.             for($c = 0; $c < count($inv); $c++){
  2841.                 $idInv                      = $inv[$c]->id;
  2842.                 $no_inv                                        = $inv[$c]->no_inv;
  2843.                 //return $no_inv;
  2844.                 $arr_no_inv                 = explode("/", $no_inv);
  2845.                 $c_noInv                    = count($arr_no_inv);
  2846.                //return count($arr_no_inv);
  2847.                if($no_inv != null || $no_inv != '' || $no_inv != ""){
  2848.                     if( $c_noInv == 3){
  2849.                         $no_inv_                    = $no_inv;
  2850.                         $no_inv_real                = $no_inv;
  2851.                     }else if($c_noInv == 4){
  2852.                         $no_inv_                    = $no_inv; 
  2853.                         $no_inv_real                = $arr_no_inv[0] . "/" . $arr_no_inv[1] . "/" . $arr_no_inv[3];
  2854.                     }
  2855.  
  2856.                    \DB::table('invoice')->where('id', $idInv)
  2857.                         ->update(['no_inv_real' => $no_inv_real]);
  2858.  
  2859.                 }
  2860.                                 
  2861.             }
  2862.             
  2863.             return "Done";
  2864.                
  2865.                 } catch (Exception $e) {
  2866.                         return ["code" => "_ERR_SYSTM", "error" => $e->getMessage()];
  2867.                 }
  2868.         }
  2869.        
  2870.        
  2871.        
  2872.        
  2873.        
  2874.        
  2875.        
  2876.        
  2877.        
  2878.        
  2879.        
  2880.        
  2881.        
  2882.        
  2883.        
  2884.        
  2885.        
  2886.        
  2887.        
  2888. }
  2889.  

Editor

You can edit this paste and save as new:


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