[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 PDF;
  12. use Exception;
  13. use File;
  14. use Excel;
  15. use Carbon;
  16.  
  17. // Model
  18. use DB;
  19. use App\ManualInvoice;
  20. use App\Department;
  21. use App\Client as ClientModel;
  22. use App\Division;
  23. use App\DetailManual;
  24. use App\ServiceCategory;
  25. use App\Invoice;
  26. use Validator;
  27.  
  28. class ManualInvoiceController extends Controller
  29. {
  30.     public function __construct()
  31.     {
  32.         $this->middleware('checksession');
  33.         $this->middleware('checktoken');
  34.     }
  35.     /**
  36.      * Display a listing of the resource.
  37.      *
  38.      * @return \Illuminate\Http\Response
  39.      */
  40.     public function index(Request $request)
  41.     {
  42.         if (empty($_COOKIE["token"])) {
  43.             return redirect("http://192.168.100.53/sprint-sysadmin-dev/public/login");
  44.         } else {
  45.             $title = "Invoice";
  46.             $gen = "Manual Invoice";
  47.             $content = "Manual Invoice"; 
  48.  
  49.  
  50.             $data = DB::table('db_sprint_ar.manual_invoice AS m')
  51.                 ->join('db_sprint_ar.invoice AS i', 'i.id_manual', 'm.id')
  52.                                 ->whereIn('i.status', [0,7])
  53.                 ->select('i.*', 'm.*', 'i.submit_date AS tanggal')
  54.                                 ->orderBy('i.submit_date', 'desc')
  55.                 ->get();
  56.             
  57.             $dtdepts = DB::table('invoice')->select('dept_code', 'dept_name')->get();
  58.             $dtclients = DB::table('invoice')->select('client_code', 'client_name')->get();
  59.             $dtdivs = DB::table('invoice')->select('division_code', 'division_name')->get();
  60.             $dtservs = ServiceCategory::select('service_category_code', 'service_category_name')->get();
  61.  
  62.             // return view('manual_invoice.index', compact('title','content','gen','data','dtdepts', 'dtclients', 'dtdivs', 'dtservs'));
  63.             return view('manual_invoice.index', compact('title', 'content', 'gen', 'data', 'dtdepts', 'dtclients', 'dtdivs', 'dtservs'));
  64.         }
  65.     }
  66.  
  67.     public function get_id_desc($desc)
  68.     {
  69.         try {
  70.             $Uri = new \GuzzleHttp\Client(['base_uri' => 'http://192.168.100.53/sprint-sysadmin-dev/public/api-master/get_id_desc']);
  71.             $headers = [
  72.                 'Authorization' => 'Bearer ' . $_COOKIE["token"],
  73.                 'Accept'        => 'application/json',
  74.             ];
  75.             $response = $Uri->request('GET', '', [
  76.                 'headers' => $headers,
  77.                 'query' => ['as_description' => $desc]
  78.             ]);
  79.             $data = json_decode($response->getBody()->getContents(), true)['data'][0];
  80.             if(count($data) > 0){
  81.                 return (int)$data['id_provider_odoo'];
  82.             }else{
  83.                 return 0;
  84.             }
  85.         } catch (Exception $e) {
  86.             return 0;
  87.         }
  88.     }
  89.  
  90.     public function store(Request $request)
  91.     { 
  92.         // try{
  93.             date_default_timezone_set("Asia/Bangkok");
  94.             $dateNow = date('Y-m-d H:i:s');            
  95.                 $username_login     = session()->get('profile')['name'];
  96.  
  97.             $cn                 =  $request['cn'];
  98.             $cc                 =  $request['cc'];
  99.             $dic                =  $request['dic'];
  100.             $dec                =  $request['dec'];
  101.             $desc               =  $request['desc'];
  102.             $sc_Add             =  $request['sc_Add'];
  103.             $materai            =  $request['materai'];
  104.             $vat                =  $request['vat'];
  105.             $uso                = $request['uso'];
  106.             $bhp                = $request['bhp'];
  107.             $total_temp         = $request['total_s'];
  108.             $total              = str_replace(',', '', $total_temp);
  109.             $item               = $request['item'];
  110.             $description_inv    = $request['temp_desc'];
  111.             $unitprice          = $request['unitprice'];
  112.             $quantity           = $request['quantity'];
  113.             $manual_detail      = $request['manual_detail'];
  114.  
  115.             // echo "</br>";
  116.             // echo "</br> cn : " ; print_r('cn');
  117.             // echo "</br> cc : " ; print_r('cc');
  118.             // echo "</br> dic : " ; print_r('dic');
  119.             // echo "</br> dec : " ; print_r('dec');
  120.             // echo "</br> desc : " ; print_r('desc');
  121.             // echo "</br> sc_Add : " ; print_r('sc_Add');
  122.             // echo "</br> materai : " ; print_r('materai');
  123.             // echo "</br> vat : " ; print_r('vat');
  124.             // echo "</br> uso : " ; print_r('uso');
  125.             // echo "</br> bhp : " ; print_r('bhp');
  126.             // echo "</br> total_temp : " ; print_r('total_temp');
  127.  
  128.             // die();
  129.             
  130.             $data_div      = $this->get_data_division_by_id($dic);
  131.             
  132.             if (count($data_div) == 0) {
  133.                 return "division";
  134.             }
  135.  
  136.             $division_name          = $data_div['division_name'];
  137.             $bank_acc_number_sprint = $data_div['division_bank_acc_number_sprint'];
  138.             $bank_acc_name_sprint   = $data_div['division_bank_acc_name_sprint'];
  139.             $bank_coa_sprint        = $data_div['division_bank_coa_sprint'];
  140.             $zahir_name             = $data_div['division_zahir_name'];
  141.             $client_npwp_no         = $data_div['division_npwp'];
  142.             $bank_acc_name_client   = $data_div['division_bank_account_name'];
  143.             $bank_acc_number_client = $data_div['division_bank_account_number'];
  144.             $bank_name              = $data_div['division_bank_name_sprint'];
  145.             // $client_coa             = $data_div['division_bank_name_sprint'];
  146.             $division_address       = $data_div['division_address'];
  147.             $division_description   = $data_div['division_description'];
  148.             $ba                     = $data_div['berita_acara'];
  149.             if($ba == 1){
  150.                 $ba = "Yes";
  151.             }else{
  152.                 $ba = "No";
  153.             }
  154.             $fp                     = $data_div['faktur_pajak'];
  155.             if($fp == 1){$fp = "Yes";}else{$fp = "No";}
  156.             $softcopy               = $data_div['soft_copy'];
  157.             if($softcopy == 1){$softcopy = "Yes";}else{$softcopy = "No";}
  158.             $hardcopy               = $data_div['hard_copy'];
  159.             if($hardcopy == 1){$hardcopy = "Yes";}else{$hardcopy = "No";}
  160.          
  161.         
  162.             $data_pic      = $this->get_data_pics_by_id($dic);
  163.             if (count($data_pic) == 0) {
  164.                 return "pics";//redirect()->back()->with('error', 'Data PIC Not Found For '. $division_name .' Division. Please Check AM');
  165.                // return "Data PIC Not Found For $division_name Division. Please Check AM";
  166.             }
  167.  
  168.             $pic_id       = $data_pic['pic_id'];
  169.             $pic_name     = $data_pic['pic_name'];
  170.             $email_client = $data_pic['pic_email'];
  171.             $pic_position = $data_pic['pic_position'];
  172.  
  173.             $data_pic_collection = $this->get_data_pic_collection($dic);
  174.             if (count($data_pic_collection) == 0) {
  175.                 $pic_collection_name        =  $pic_name;
  176.                 $email_pic_collection       = $email_client;
  177.                         }else{
  178.                 $pic_collection_name        = $data_pic_collection['pic_name'];
  179.                 $email_pic_collection       = $data_pic_collection['pic_email'];
  180.             }
  181.             
  182.             $data_pic_pph23 = $this->get_data_pic_pph23($dic);
  183.             if (count($data_pic_pph23) == 0) {
  184.                                 $pic_pph23_name        = $pic_name;
  185.                 $email_pic_pph23       = $email_client;
  186.             }else{
  187.                 $pic_pph23_name        = $data_pic_pph23['pic_name'];
  188.                 $email_pic_pph23       = $data_pic_pph23['pic_email'];
  189.     
  190.             }
  191.             
  192.             $data_client      = $this->get_data_client_by_id($cc);
  193.     
  194.             if (count($data_client) == 0) {
  195.                 return redirect()->back()->with('alert', 'Data PIC Not Found For '. $division_name .' Division. Please Check AM');
  196.        
  197.             }
  198.             $client_domicile_address = $data_client['client_domicile_address'];
  199.             $client_coa                    = $data_client['client_coa'];
  200.             $lmbg                                  = $data_client['lmbnegara']; //"yes"; 
  201.             $no_spk                                = $data_client['client_no_spk'];
  202.             $no_addendum_spk               = $data_client['client_no_addendum_spk']; 
  203.             $max_amount                    = $data_client['max_amount']; //10000000;
  204.             $invoice_rule                   = $data_client['invoice_rule'];
  205.  
  206.             $data_dept                = $this->get_data_dept_by_id($dec);
  207.             if (count($data_dept) == 0) {
  208.                 return "dept"; //$dept_name   = "";
  209.             }
  210.             $dept_name   = $data_dept['dept_name'];
  211.     
  212.             $id_manual = 0;
  213.     
  214.             $description = join(", ", $description_inv);
  215.     
  216.             $sub_total              = ceil($total);            
  217.             $new_ppn                = $this->check_ppn($vat, $sub_total);
  218.             $amount_invoice_rule    = $sub_total + $new_ppn;
  219.                 
  220.             $real                                  = $this->real_field($lmbg, $invoice_rule, $max_amount, $amount_invoice_rule, $sub_total, $vat);
  221.             // return $real;
  222.             
  223.             $sub_total_now                  = $real[0];
  224.             $sub_total_real                 = $real[1];
  225.             $new_ppn_now                    = $real[2];
  226.             $new_ppn_real                   = $real[3];
  227.                 
  228.             if ($materai == "yes") {
  229.                 $new_materai = $this->check_materai($sub_total_now, $new_ppn_now);
  230.             }else if ($materai == "no") {
  231.                 $new_materai = 0;
  232.             }
  233.                 
  234.             $pph23   = $this->check_pph23($cc, $sub_total_now);
  235.     
  236.             if ($bhp == "yes") {
  237.                 $bhp_Add = ($sub_total_now ) * 0.005; //+  $new_ppn_now
  238.                 $bhp_manual = "yes";
  239.             } else {
  240.                 $bhp_Add = 0;
  241.                 $bhp_manual = "no";
  242.             }
  243.     
  244.             if ($uso == "yes") {
  245.                 $uso_Add = ($sub_total_now ) * 0.0125; //+  $new_ppn_now
  246.                 $uso_manual = "yes";
  247.             } else {
  248.                 $uso_Add = 0;
  249.                 $uso_manual = "no";
  250.             }
  251.     
  252.                 $json = [
  253.                     "detail" => [
  254.                         "info"  => [
  255.                             "from" => "PT Sprint Asia",
  256.                             "recipient"    => $cn,
  257.                             "reff_no"      => '',
  258.                             "cust_id"      => '',
  259.                             "sales_person" => ''
  260.                         ],
  261.                         "data"  => array($item),
  262.                         "total" => [
  263.                             "quantity"  => $quantity,
  264.                             "unitprice" => $unitprice,
  265.                             "sub_total" => ceil($sub_total_now), // sum_total
  266.                             "with_ppn"  => floor($new_ppn_now), // new_ppn
  267.                             "bhp"       => $bhp_Add,
  268.                             "uso"       => $uso_Add,
  269.                             "with_pph"  => $pph23
  270.                            
  271.                         ]
  272.                     ],
  273.     
  274.                 ];
  275.  
  276.                 // if($quantity == "NaN" || $unitprice == "NaN"){
  277.                 //     return "NaN";
  278.                 // }
  279.  
  280.                 $serialize = json_encode($json);
  281.                 $serialize = json_encode($json, JSON_UNESCAPED_SLASHES);
  282.  
  283.  
  284.  
  285.             // return $serialize;
  286.     
  287.             $Total                         = round($sub_total_now + $new_ppn_now + $new_materai - $pph23 - $bhp_Add - $uso_Add);
  288.             $Total_real    = round($sub_total_real + $new_ppn_real + $new_materai - $pph23 - $bhp_Add - $uso_Add);
  289.             $revenue_coa   = $sc_Add;
  290.                         $data_revebue_coa                 = $this->get_data_revenue_coa($revenue_coa);
  291.                         $product_id                            = $data_revebue_coa['product_id'];
  292.     
  293.             // Manual Invoice
  294.             $manual_invoice = [
  295.                 "client_code"   => $cc,
  296.                 "dept_code"     => $dec,
  297.                 "division_code" => $dic,
  298.                 "desc"          => $desc,
  299.                 "vat"           => $vat,
  300.                 "materai"       => $materai,
  301.                 "bhp"            => $bhp_manual,
  302.                 "uso"            => $uso_manual,
  303.                 "total"         => 0,
  304.                 "created_by"    => $username_login,
  305.                 "created_at"    => NOW()
  306.             ];
  307.     
  308.             try {
  309.                 $last_id = DB::table("db_sprint_ar.manual_invoice")->insertGetId($manual_invoice);
  310.             } catch (Exception $e) {
  311.                 return ["code" => "_ERR_SYSTM", "error" => $e->getMessage()];
  312.             }
  313.     
  314.             $invoice = [
  315.                 "client_code"             => $cc,
  316.                 "client_name"             => $cn,
  317.                 "division_code"           => $dic,
  318.                 "division_name"           => $division_name,
  319.                 "division_description"    => $division_description,
  320.                 "dept_code"               => $dec,
  321.                 "dept_name"               => $dept_name,
  322.                 "pic_id"                  => $pic_id,
  323.                 "inv_month"               => date("m"),
  324.                 "inv_year"                => date("Y"),
  325.                 "unitprice"               => $unitprice,
  326.                 "quantity"                => $quantity,
  327.                 "sub_total"               => ceil($sub_total_now),
  328.                 "ppn"                     => floor($new_ppn_now),
  329.                 "sub_total_real"          => ceil($sub_total_real),
  330.                 "ppn_real"                => floor($new_ppn_real),
  331.                 "materai"                 => $new_materai,
  332.                 "pph23"                   => $pph23,
  333.                 "total"                   => $Total,
  334.                 "total_real"              => $Total_real,
  335.                 "serialize_data"          => $serialize,
  336.                 "selisih"                 => 0,
  337.                 "status"                  => 0,
  338.                 "addmanual"               => 1,
  339.                 "submit_date"             => NOW(),
  340.                 "id_manual"               => $last_id,
  341.                 "pic_name"                => $pic_name,
  342.                 "division_address"        => $division_address,
  343.                 "bank_acc_number_sprint"  => $bank_acc_number_sprint,
  344.                 "bank_acc_name_sprint"    => $bank_acc_name_sprint,
  345.                 "bank_acc_number_client"  => $bank_acc_number_client,
  346.                 "bank_acc_name_client"    => $bank_acc_name_client,
  347.                 "bank_coa_sprint"         => $bank_coa_sprint,
  348.                 "zahir_name"              => $zahir_name,
  349.                 "client_npwp_no"          => $client_npwp_no,
  350.                 "bank_name"               => $bank_name,
  351.                 "client_coa"              => $client_coa,
  352.                 "revenue_coa"             => $revenue_coa,
  353.                                 "product_id"                      => $product_id,
  354.                 "email_client"            => $email_client,
  355.                 "email_collection"        => $email_pic_collection,
  356.                 "pic_collection"          => $pic_collection_name,
  357.                 "email_pph23"             => $email_pic_pph23,
  358.                 "pic_pph23"               => $pic_pph23_name,
  359.                 "pic_position"            => $pic_position,
  360.                 "description"             => $description,
  361.                 "id_collection"           =>'[""]',
  362.                 "ba"                      => $ba,
  363.                 "fp"                      => $fp,
  364.                 "softcopy"                => $softcopy,
  365.                 "hardcopy"                => $hardcopy,
  366.                 "created_by"              => $username_login,
  367.                 "created_at"              => NOW()
  368.             ];
  369.  
  370.             try {
  371.                 $last_id_invoice    = DB::table("db_sprint_ar.invoice")->insertGetId($invoice);
  372.                 $now                = NOW();
  373.                 $username           = session()->get('profile')['name'];
  374.  
  375.                 $descriptionss                 = "$last_id_invoice tanggal: $now submit by $username";
  376.  
  377.                 $email                                 = session()->get('profile')['email'];
  378.                 $description_log                = "serialize_data: $serialize";
  379.                 
  380.                 $activity = "Add Manual Invoice";
  381.                 DB::table('invoice_log_history')->insert(['id_inv' => $last_id_invoice, 'activity' => $activity, 'username' => $username, 'email' => $email, 'description' => $description_log,'created_at' => $now]);
  382.  
  383.                 $saved_log_datas = preg_replace( "/\r|\n/", " ", json_encode($invoice) );
  384.                 $saved_log_datas = str_replace("\\", "", $saved_log_datas);
  385.                 // DB::table('history')->insert([ 'activity'=> "Create Manual Invoice", 'description'=> "Create values : ". $saved_log_datas , 'created_by' => $username , 'created_at' => $now ]);
  386.                 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 ]);
  387.  
  388.                DB::table("db_sprint_ar.manual_invoice")->where('id', $last_id)->update(["total" => $Total]);
  389.             } catch (Exception $e) {
  390.                 return ["code" => "_ERR_SYSTM", "error" => $e->getMessage()];
  391.             }
  392.             
  393.             // Insert to table manual_detail
  394.             for ($i = 0; $i < count($manual_detail); $i++) {
  395.                 $item_temp = $manual_detail[$i]['desc'];
  396.                 $manual_details[] = [
  397.                     "id_manual"    => $last_id,
  398.                     "service"              => $manual_detail[$i]['service'],
  399.                     "quantity"             => $manual_detail[$i]['quantity'],
  400.                     "unit_price"   => $manual_detail[$i]['unit_price'],
  401.                     "desc"                         => $item_temp, //$manual_detail[$i]['desc'],
  402.                     "total"                => $manual_detail[$i]['total']
  403.                 ];
  404.             }
  405.  
  406.             // return $manual_details;
  407.     
  408.             try {
  409.                 $last_id = DB::table("db_sprint_ar.manual_detail")->insert($manual_details);
  410.             } catch (Exception $e) {
  411.                 return ["code" => "_ERR_SYSTM", "error" => $e->getMessage()];
  412.             }
  413.     
  414.             return "success";
  415.            
  416.  
  417.         // }catch (Exception $e) {
  418.         //     return "error";//["code" => "_ERR_SYSTM", "error" => $e->getMessage()];
  419.         // }
  420.        
  421.         // return redirect(url('/manual-invoice'))->with('success');
  422.     }
  423.  
  424.     public function update(Request $request)
  425.     {
  426.         if ($request->input('sc_Edit') == "" || $request->input('sc_Edit') == null || $request->input('sc_Edit') == null) {
  427.             return redirect()->back()->with('alert', 'Service Category Tidak Dapat Dipilih. Silahkan Periksa di Account Management. Data Tidak Tersimpan.');
  428.         }
  429.  
  430.         $id            = $request->input('id');
  431.         $client_name   = $request->input('client');
  432.         $client_code   = $request->input('client_code');
  433.         $division_code = $request->input('division_code');
  434.         $dept_code     = $request->input('dept_code');
  435.         $desc          = $request->input('desc');
  436.         $sc_coa        = $request->input('sc_Edit');
  437.         $vat           = $request->input('vat');
  438.         $bhp           = $request->input('bhp_Edit');
  439.         $uso           = $request->input('uso_Edit');
  440.         $materai       = $request->input('materai');
  441.  
  442.                 $dataInv = DB::table('invoice')->where('id_manual', $id)->get();
  443.                 $status = $dataInv[0]->status;
  444.                 // return $dataInv[0]->serialize_data;
  445.                 $serialize_data = json_decode($dataInv[0]->serialize_data);
  446.                 $cust_id               = $serialize_data->detail->info->cust_id; // data serialize data json (sekarang)
  447.                 $update_printDate = $dataInv[0]->update_print_date;
  448.                
  449.  
  450.         $data_div               = $this->get_data_division_by_id($division_code);
  451.         if (count($data_div) == 0) {
  452.             return redirect()->back()->with('alert', 'Data Division Not Found For '. $division_code .'. Please Check AM');
  453.        
  454.         }
  455.         $division_name          = $data_div['division_name'];
  456.         $bank_acc_number_sprint = $data_div['division_bank_acc_number_sprint'];
  457.         $bank_acc_name_sprint   = $data_div['division_bank_acc_name_sprint'];
  458.         $bank_coa_sprint        = $data_div['division_bank_coa_sprint'];
  459.         $zahir_name             = $data_div['division_zahir_name'];
  460.         $client_npwp_no         = $data_div['division_npwp'];
  461.         $bank_acc_name_client   = $data_div['division_bank_account_name'];
  462.         $bank_acc_number_client = $data_div['division_bank_account_number'];
  463.         $bank_name              = $data_div['division_bank_name_sprint'];
  464.         $client_coa             = $data_div['division_bank_name_sprint'];
  465.         $division_address       = $data_div['division_address'];
  466.                 $division_invoice_rule  = $data_div['division_invoice_rule']; 
  467.                 $division_max_amount    = $data_div['division_max_amount']; 
  468.         $division_description   = $data_div['division_description']; 
  469.                 $ba                     = $data_div['berita_acara'];
  470.         if($ba == 1){$ba = "Yes";}else{$ba = "No";}
  471.         $fp                     = $data_div['faktur_pajak'];
  472.         if($fp == 1){$fp = "Yes";}else{$fp = "No";}
  473.         $softcopy               = $data_div['soft_copy'];
  474.         if($softcopy == 1){$softcopy = "Yes";}else{$softcopy = "No";}
  475.         $hardcopy               = $data_div['hard_copy'];
  476.         if($hardcopy == 1){$hardcopy = "Yes";}else{$hardcopy = "No";}
  477.                
  478.         $data_pic     = $this->get_data_pics_by_id($division_code);
  479.         if (count($data_pic) == 0) {
  480.             return redirect()->back()->with('alert', 'Data PIC Not Found For '. $division_code .'. Please Check AM');
  481.         }
  482.         $pic_id       = $data_pic['pic_id'];
  483.         $pic_name     = $data_pic['pic_name'];
  484.         $email_client = $data_pic['pic_email'];
  485.         $pic_position = $data_pic['pic_position'];
  486.  
  487.         $data_pic_collection = $this->get_data_pic_collection($division_code);
  488.         if (count($data_pic_collection) == 0) {
  489.                         $pic_collection_name        = $pic_name;
  490.             $email_pic_collection       = $email_client;
  491.                 }else{
  492.             $pic_collection_name        = $data_pic_collection['pic_name'];
  493.             $email_pic_collection       = $data_pic_collection['pic_email'];
  494.         }
  495.         
  496.  
  497.         $data_pic_pph23 = $this->get_data_pic_pph23($division_code);
  498.         if (count($data_pic_pph23) == 0) {
  499.                         $pic_pph23_name        = $pic_name;
  500.             $email_pic_pph23       = $email_client;
  501.                 }else{
  502.             $pic_pph23_name        = $data_pic_pph23['pic_name'];
  503.             $email_pic_pph23       = $data_pic_pph23['pic_email'];
  504.         }
  505.         
  506.         $data_client              = $this->get_data_client_by_id($client_code);
  507.         if (count($data_client) == 0) {
  508.             return redirect()->back()->with('alert', 'Data Client Not Found For '. $client_code .'. Please Check AM');
  509.         }
  510.         $client_domicile_address  = $data_client['client_domicile_address'];
  511.         $client_coa               = $data_client['client_coa'];
  512.                 $lmbg                                    = $data_client['lmbnegara']; //"yes" ;
  513.                 $max_amount                      = $data_client['max_amount'];
  514.                 $invoice_rule                     = $data_client['invoice_rule'];
  515.  
  516.         $data_dept   = $this->get_data_dept_by_id($dept_code);
  517.         if (count($data_dept) == 0) {
  518.             return redirect()->back()->with('alert', 'Data Department Not Found For '. $division_name .'. Please Check AM');
  519.        }
  520.         $dept_name   = $data_dept['dept_name'];
  521.         $id_manual = 0;
  522.         $sum_quantity = 0;
  523.         $sum_unitprice = 0;
  524.         $sum_total = 0;
  525.                
  526.         // JSON Serialize
  527.         $length = count($request->input('service_cat'));
  528.         if ($length > 0) {
  529.  
  530.             $item_name   = $request->input('vdesc');
  531.             $service     = $request->input('service_cat');
  532.             $id_service     = $request->input('id_service_cat');
  533.                         $temp_desc = [];
  534.                         for($c = 0; $c < count($item_name); $c++){
  535.                                 if($item_name[$c] == null || $item_name[$c] == '' || $item_name[$c] == ""){
  536.                                         continue;
  537.                                 }
  538.                                 array_push($temp_desc, $item_name[$c]);
  539.                         }
  540.             $description = join(", ", $temp_desc);
  541.             $quantity    = $request->input('quantity');
  542.             $unitprice   = $request->input('unitprice');
  543.             $total       = $request->input('total');
  544.                         // return $description;
  545.             for ($x = 0; $x < $length; $x++) {
  546.                 // return $id_service[2];
  547.                 if($id_service[$x] == '0')  return redirect()->back()->with('alert', "Id Description $service[$x] Tidak Tersedia. Silahkan Create/Update Provider $service[$x]"); 
  548.                 $item_desc = [
  549.                     "item_name" => $service[$x],
  550.                     "quantity"  => $quantity[$x],
  551.                     "unitprice" => $unitprice[$x],
  552.                     "total"     => intval(str_replace(',', '', $total[$x]))
  553.                 ];
  554.  
  555.                 $item[] = [
  556.                     "item"      =>  $item_name[$x],
  557.                     "item_desc" => array($item_desc)
  558.                 ];
  559.  
  560.                 $sum_quantity = $sum_quantity + intval($quantity[$x]);
  561.                 $sum_unitprice = $sum_unitprice + intval($unitprice[$x]);
  562.                 $sum_total = $sum_total + intval(str_replace(',', '', $total[$x]));
  563.             }
  564.             
  565.             $sub_total = ceil($sum_total);
  566.             $new_ppn = floor($this->check_ppn($vat, $sub_total));
  567.                         $amount_invoice_rule = $sub_total + $new_ppn;
  568.  
  569.                         $real                                  = $this->real_field($lmbg, $invoice_rule, $max_amount, $amount_invoice_rule, $sub_total, $vat);
  570.                        
  571.                         $sub_total_now                  = $real[0];
  572.                         $sub_total_real                 = $real[1];
  573.                         $new_ppn_now                    = $real[2];
  574.                         $new_ppn_real                   = $real[3];
  575.                        
  576.                        
  577.             if ($materai == "yes") {
  578.                 $new_materai = $this->check_materai($sub_total_now, $new_ppn_now);
  579.             } else if ($materai == "no") {
  580.                 $new_materai = 0;
  581.             }
  582.             $pph23   = $this->check_pph23($client_code, $sub_total_now);
  583.                        
  584.                         if ($bhp == "yes") {
  585.                                 $bhp_Edit = ($sub_total_now ) * 0.005; //+  $new_ppn_now
  586.                                 $bhp_manual_e = "yes";
  587.                         } else {
  588.                                 $bhp_Edit = 0;
  589.                                 $bhp_manual_e = "no";
  590.                         }
  591.  
  592.                         if ($uso == "yes") {
  593.                                 $uso_Edit = ($sub_total_now ) * 0.0125; //+  $new_ppn_now
  594.                                 $uso_manual_e = "yes";
  595.                         } else {
  596.                                 $uso_Edit = 0;
  597.                                 $uso_manual_e = "no";
  598.                         }
  599.  
  600.            
  601.             $json = [
  602.                 "detail" => [
  603.                     "info"  => [
  604.                         "from" => "PT Sprint Asia",
  605.                         "recipient"    => $client_name,
  606.                         "reff_no"      => '',
  607.                         "cust_id"      => $cust_id,
  608.                         "sales_person" => ''
  609.                     ],
  610.                     "data"  => array($item),
  611.                     "total" => [
  612.                         "quantity"  => $sum_quantity,
  613.                         "unitprice" => $sum_unitprice,
  614.                         "sub_total" => ceil($sub_total_now),
  615.                         "with_ppn"  => floor($new_ppn_now),
  616.                                                 "bhp"       => $bhp_Edit,
  617.                         "uso"       => $uso_Edit,
  618.                         "with_pph"  => $pph23
  619.                         
  620.                     ]
  621.                 ],
  622.             ];
  623.             $serialize = json_encode($json);
  624.            
  625.         }
  626.         // return $serialize;
  627.                 $Total = round($sub_total_now + $new_ppn_now + $new_materai - $pph23- $bhp_Edit- $uso_Edit);
  628.                 $Total_real = round($sub_total_real + $new_ppn_real + $new_materai - $pph23 - $bhp_Edit - $uso_Edit);
  629.         $revenue_coa = $sc_coa;
  630.  
  631.         
  632.         $datas_mi = DB::table('manual_invoice')->select('*')->where('id', $id)->get();
  633.         
  634.         $id_mi_old = $datas_mi[0]->id;
  635.         $client_code_old = $datas_mi[0]->client_code;
  636.         $division_code_old = $datas_mi[0]->division_code;
  637.         $dept_code_old = $datas_mi[0]->dept_code;
  638.         $desc_old = $datas_mi[0]->desc;
  639.         $vat_old = $datas_mi[0]->vat;
  640.         $materai_old = $datas_mi[0]->materai;
  641.         $bhp_old = $datas_mi[0]->bhp;
  642.         $uso_old = $datas_mi[0]->uso;
  643.         $total_old = $datas_mi[0]->total;
  644.  
  645.         $conn = DB::connection('account129');
  646.         $datas_clients = $conn->select("Select client_name FROM clients WHERE client_code = '$client_code_old' AND deleted_at is null ");
  647.         $clients_name = $datas_clients[0]->client_name;
  648.  
  649.         $datas_divisions = $conn->select("Select division_name FROM divisions WHERE division_code = '$division_code_old' AND deleted_at is null ");
  650.         $divisions_name = $datas_divisions[0]->division_name;
  651.  
  652.         $datas_depts = $conn->select("Select dept_name FROM depts WHERE dept_code = '$dept_code_old' AND deleted_at is null ");
  653.         $depts_name = $datas_depts[0]->dept_name;
  654.  
  655.         $result_update = "";
  656.         if ( $client_code_old != $client_code ) $result_update = $result_update . ". Client before : " . $clients_name;
  657.         if ( $division_code_old != $division_code ) $result_update = $result_update . ". Division before : " . $divisions_name;
  658.         if ( $dept_code_old != $dept_code ) $result_update = $result_update . ". Departement before : " . $depts_name;
  659.         if ( $desc_old != $request->input('desc') ) $result_update = $result_update . ". Description before : " . $desc_old;
  660.         if ( $vat_old != $vat ) $result_update = $result_update . ". VAT before : " . $vat_old;
  661.         if ( $materai_old != $materai ) $result_update = $result_update . ". Materai before : " . $materai_old;
  662.         if ( $bhp_old != $bhp_manual_e ) $result_update = $result_update . ". BHP before : " . $bhp_old;
  663.         if ( $uso_old != $bhp_manual_e ) $result_update = $result_update . ". USO before : " . $uso_old;
  664.         if ( $total_old != $Total_real ) $result_update = $result_update . ". Total before : " . $total_old;
  665.  
  666.         if( $result_update == "" ) $result_update = "No Data Updated."; else $result_update = $result_update . " has been Updated.";
  667.  
  668.         // Manual Invoice
  669.         $manual_invoice = [
  670.             "client_code"   => $client_code,
  671.             "dept_code"     => $dept_code,
  672.             "division_code" => $division_code,
  673.             "desc"          => $desc,
  674.             "vat"           => $vat,
  675.             "materai"       => $materai,
  676.                         "bhp"            => $bhp_manual_e,
  677.                         "uso"           => $uso_manual_e,
  678.             "total"         => $Total_real
  679.         ];
  680.  
  681.         try {
  682.             DB::table("db_sprint_ar.manual_invoice")->where('id', $id)->update($manual_invoice);
  683.         } catch (Exception $e) {
  684.             return ["code" => "_ERR_SYSTM", "error" => $e->getMessage()];
  685.         }
  686.  
  687.         $invoice = [
  688.             "client_code"             => $client_code,
  689.             "client_name"             => $client_name,
  690.             "division_code"           => $division_code,
  691.             "division_name"           => $division_name,
  692.             "division_description"    => $division_description,
  693.             "dept_code"               => $dept_code,
  694.             "dept_name"               => $dept_name,
  695.             "pic_id"                  => $pic_id,
  696.             // "inv_month"               => date("m"),
  697.             // "inv_year"                => date("yy"),
  698.             "unitprice"               => $sum_unitprice,
  699.             "quantity"                => $sum_quantity,
  700.             "sub_total"               => ceil($sub_total_now),
  701.             "ppn"                     => floor($new_ppn_now),
  702.                         "sub_total_real"          => ceil($sub_total_real),
  703.             "ppn_real"                => floor($new_ppn_real),
  704.             "materai"                 => $new_materai,
  705.             "pph23"                   => $pph23,
  706.             "total"                   => $Total,
  707.                         "total_real"              => $Total_real,
  708.             "serialize_data"          => $serialize,
  709.             "selisih"                 => 0,
  710.             //   "status"                  => $statusInv,
  711.             "addmanual"               => 1,
  712.             "id_manual"               => $id,
  713.             "pic_name"                => $pic_name,
  714.             "division_address"        => $division_address,
  715.             "bank_acc_number_sprint"  => $bank_acc_number_sprint,
  716.             "bank_acc_name_sprint"    => $bank_acc_name_sprint,
  717.             "bank_acc_number_client"  => $bank_acc_number_client,
  718.             "bank_acc_name_client"    => $bank_acc_name_client,
  719.             "bank_coa_sprint"         => $bank_coa_sprint,
  720.             "zahir_name"              => $zahir_name,
  721.             "client_npwp_no"          => $client_npwp_no,
  722.             "bank_name"               => $bank_name,
  723.             "client_coa"              => $client_coa,
  724.             "revenue_coa"             => $revenue_coa,
  725.             "email_client"            => $email_client,
  726.             "pic_collection"          => $pic_collection_name,
  727.             "email_collection"        => $email_pic_collection,
  728.             "pic_collection"          => $pic_pph23_name,
  729.             "email_pph23"             => $email_pic_pph23,
  730.             "pic_position"            => $pic_position,
  731.                     //      "update_print_date"               => $update_print_date,
  732.             "description"             => $description,
  733.             "ba"                      => $ba,
  734.             "fp"                      => $fp,
  735.             "softcopy"                => $softcopy,
  736.             "hardcopy"                => $hardcopy
  737.         ];
  738.  
  739.  
  740.         // $invoice['client_code'] = (bool)$invoice['client_code']; // 0/1 -> PHP false/true
  741.         // return json_encode($invoice); // {'id':true}
  742.  
  743.         try {
  744.             DB::table("db_sprint_ar.invoice")->where('id_manual', $id)->update($invoice);
  745.             DB::table("db_sprint_ar.manual_invoice")->where('id', $id)->update(["total" => $Total]);
  746.  
  747.             $username                       = session()->get('profile')['name'];
  748.  
  749.             $saved_log_datas = preg_replace( "/\r|\n/", " ", json_encode($invoice) );
  750.             $saved_log_datas = str_replace("\\", "", $saved_log_datas);
  751.             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() ]);
  752.  
  753.         } catch (Exception $e) {
  754.             return ["code" => "_ERR_SYSTM", "error" => $e->getMessage()];
  755.         }
  756.  
  757.         // Insert to table manual_detail
  758.         for ($i = 0; $i < $length; $i++) {
  759.             $item_temp = $item_name[$i];
  760.             $manual_detail[] = [
  761.                 "id_manual"         => $id,
  762.                 "service"           => $service[$i],
  763.                 "quantity"          => $quantity[$i],
  764.                 "unit_price"        => $unitprice[$i],
  765.                 "desc"              => $item_temp, //$item_name[$i],
  766.                 "total"             => intval(str_replace(',', '', $total[$i]))
  767.             ];
  768.         }
  769.  
  770.         try {
  771.             DB::beginTransaction();
  772.             DB::table("db_sprint_ar.manual_detail")->where('id_manual', $id)->delete();
  773.             DB::table("db_sprint_ar.manual_detail")->insert($manual_detail);
  774.             DB::commit();
  775.         } catch (Exception $e) {
  776.             DB::rollBack();
  777.             return ["code" => "_ERR_SYSTM", "error" => $e->getMessage()];
  778.         }
  779.         return redirect(url('/manual-invoice'))->with('success');
  780.     }
  781.  
  782.     public function destroy($id)
  783.     {
  784.         // return "Masuk Pak Eko";
  785.         // try {
  786.  
  787.             DB::beginTransaction();
  788.  
  789.             // delete manual invoice
  790.             $mi = ManualInvoice::find($id);
  791.  
  792.             $gets_mi = DB::table('invoice')->select('*')->where('id_manual', $id)->where('addmanual', '1')->get();
  793.             $client_name = $gets_mi[0]->client_name;
  794.             $division_name = $gets_mi[0]->division_name;
  795.             $dept_name = $gets_mi[0]->dept_name;
  796.             $description = $gets_mi[0]->description;
  797.             $totals = $gets_mi[0]->quantity;
  798.  
  799.             $username                       = session()->get('profile')['name'];
  800.             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() ]);            
  801.             // DB::table('history')->insert([ 'activity'=> "Delete Manual Invoice", 'description'=> "Delete Manual Invoice id : [". $id . "] Success " , 'created_by' => $username , 'created_at' => now() ]);
  802.  
  803.             $mi->delete();
  804.  
  805.             // delete manual detail invoice
  806.             DB::table('manual_detail')->where('manual_detail.id_manual', $id)->delete();
  807.  
  808.             // Delete Invoice
  809.             DB::table('invoice')->where('id_manual', $id)->delete();
  810.  
  811.             DB::commit();
  812.             return response()->json(["success" => "Deleted id " . $id]);
  813.         // } catch (Exception $e) {
  814.         //     DB::rollBack();
  815.         //     return response()->json(["error" => $e->getMessage()]);
  816.         // }
  817.     }
  818.  
  819.  
  820.     
  821.  
  822.         public function upload_tes(Request $request)
  823.     {
  824.                  try {
  825.                         if ($request->input('sc_upload_manual') == "" || $request->input('sc_upload_manual') == "-" || $request->input('sc_upload_manual') == null || $request->input('sc_upload_manual') == '') {
  826.                                 //return redirect()->back()->with('alert', 'Service Category Tidak Dapat Dipilih. Silahkan Periksa di Account Management. Data Tidak Tersimpan.');
  827.                                         return redirect('/upload-manual-invoice')->with('error', 'Service Category Tidak Dapat Dipilih. Silahkan Periksa di Account Management. Data Tidak Tersimpan.');
  828.                         }
  829.                        
  830.             $File               = $request->file('file_upload_manual');
  831.             $revenue_coa        = $request->input('sc_upload_manual');
  832.             
  833.            
  834.             $nameFileUpload     = $File->getClientOriginalName();
  835.             $path_name          = Storage::put('/upload_manual_invoice', $File);
  836.             $size               = $File->getSize();
  837.             
  838.             $nameFile = explode("_", $nameFileUpload);
  839.             // return $nameFile;
  840.  
  841.             $client_code       = $nameFile[0];
  842.             $month              = $nameFile[1];
  843.             $fake_year          = $nameFile[2];
  844.                        
  845.                         $fake = explode(".", $fake_year);
  846.                        
  847.                         $year = $fake[0];
  848.  
  849.             $results = Excel::selectSheetsByIndex(0)->load($File)->get();
  850.  
  851.             $header = $results->getHeading(); //get xls's header
  852.  
  853.                     $extension          = $File->getClientOriginalExtension();
  854.             $size               = $File->getSize();
  855.             $path               = $File->getRealPath();
  856.             // return $path ;
  857.             
  858.  
  859.             if ($extension == "xlsx" || $extension == "xls") {
  860.                 $datas = Excel::load($path)->get(); //load all of the data in sheet excel file
  861.                 $index = 0;
  862.                 // return $datas;
  863.                                 if ($datas->count() != 0) {
  864.                    
  865.                     DB::beginTransaction();
  866.                     $invXls = array();
  867.                     foreach ($datas as $data) {
  868.  
  869.                         // try {
  870.                             $division_name            = $data['divisi'];
  871.                             $item_name                = $data['produk'];
  872.                             $service_name             = $data['jenis_sms'];
  873.  
  874.                             $qty_Hutchinson           = $data[$header[3]];
  875.                             $qty_Indosat              = $data[$header[4]];
  876.                             $qty_Telkomsel            = $data[$header[5]];
  877.                             $qty_Smartfren            = $data[$header[6]];
  878.                             $qty_Xl                   = $data[$header[7]];
  879.                                                         // $qty_Inter                = $data[$header[8]];
  880.                            // $qty_Satelindo            = $data[$header[9]];
  881.                                                        
  882.                                                          
  883.                                                         if ($division_name == null || $division_name == '' || $division_name == "" || $division_name == "eof") {
  884.                             break;
  885.                              }
  886.                                                          
  887.                             $temp_item_desc = [];
  888.                             for ($q = 3; $q < 8; $q++) {
  889.                                 if ($data[$header[$q]] != null) {
  890.  
  891.                                     $uppercased = Str::ucfirst($header[$q]); //set the first alfabet as an uppercase 
  892.                                     $temp_item_desc[] = [$uppercased, $data[$header[$q]]];
  893.                                 }
  894.                             }
  895.                                                        
  896.                             $arrClient = $this->get_data_client_by_id($client_code);
  897.                                                         if ($arrClient == null || $arrClient == '' || $arrClient == "") {
  898.                                                                 return redirect()->back()->with('alert', 'Please Check Client Code Again. File Cannot be Executed.');;
  899.                             }
  900.                             // return $arrClient;
  901.                                                        
  902.                             $client_coas                           = $arrClient['client_coa'];
  903.                             $client_code                           = $arrClient['client_code'];
  904.                             $client_names                          = $arrClient['client_name'];
  905.                             $client_domicile_address       = $arrClient['client_domicile_address'];
  906.                             $materai_client                        = $arrClient['materai'];
  907.                                                         $lmbg                                      = $arrClient['lmbnegara'];
  908.                                                         $max_amount                            = $arrClient['max_amount'];
  909.                                                         $invoice_rule                          = $arrClient['invoice_rule'];
  910.  
  911.                             $sum_quantity                          = 0;
  912.                             $sum_uprice                            = 0;
  913.                             $sum_total                                     = 0;
  914.                             $new_ppn                                       = 0;
  915.                                                        
  916.                             $item_d = [];
  917.                             foreach ($temp_item_desc as $temp_id) {
  918.                                 
  919.                                                                 $arrServiceSMS = $this->get_data_service_sms($service_name);
  920.                                                                
  921.                                                                 if ($arrServiceSMS == null || $arrServiceSMS == '' || $arrServiceSMS == "") {
  922.                                                                         return redirect()->back()->with('alert', 'Please Check Data Jenis SMS Again. File Cannot be Executed.');;
  923.                                                                 }
  924.  
  925.                                                                 $service_code = $arrServiceSMS['service_code'];
  926.                                                                
  927.                                                                 if($temp_id[0] == "Hutchinson" && $service_code == "S101"){ //s101 premium - s102 reguler
  928.                                                                         $service_code = "S102";
  929.                                                                 }else if($temp_id[0] == "Smartfren" && $service_code == "S101"){
  930.                                                                         $service_code = "S102";
  931.                                                                 }
  932.                                                                 else if($temp_id[0] == "Hutchinson" && $service_code == "S102"){
  933.                                                                         $service_code = "S102";
  934.                                                                 }else if($temp_id[0] == "Smartfren" && $service_code == "S102"){
  935.                                                                         $service_code = "S102";
  936.                                                                 }                                              
  937.                                                                
  938.                                 $arrPrice = $this->get_data_price($client_code, $temp_id[0], $service_code); 
  939.                                 
  940.                                                                 if ($arrPrice == null || $arrPrice == '' || $arrPrice == "") {
  941.                                                                         return redirect()->back()->with('alert', 'Please Check Data Provider '.$temp_id[0].' Again. File Cannot be Executed.');;
  942.                                                                 }
  943.                                 // return $arrPrice;
  944.                                                                
  945.                                                                 $price_type     = $arrPrice['price_type'];
  946.                                 $vat            = $arrPrice['vat'];
  947.                                 $tmp_uprice     = $arrPrice['price'];
  948.                                 $price          = json_decode($tmp_uprice);
  949.                                 
  950.                                                        
  951.                                 if ($price_type == 1) {
  952.                                     $p          = $price[0];
  953.                                     $real_price = (int)$p;
  954.                                 } else if ($price_type == 2) {
  955.                                     $p          = $price[0];
  956.                                     $amount     = 0;
  957.                                     $real_price = (int)$p * $amount;
  958.                                 } else if ($price_type == 3) {
  959.                                     $traffict   = $temp_id[1];
  960.  
  961.                                     for($p = 0; $p < count($price); $p++){
  962.                                         $from   = $price[$p][0]; 
  963.                                         $to     = $price[$p][1];
  964.                                         // $price  = $price[$p][2]; 
  965.                                         
  966.                                         if($traffict > $from && $traffict <= $to){
  967.                                             $real_price = $price[$p][2];
  968.                                         }else{
  969.                                             continue;
  970.                                         }
  971.                                     }
  972.  
  973.                                 }else if($price_type == 4){
  974.                                     $traffict   = $temp_id[1];
  975.  
  976.                                     for($p = 0; $p < count($price); $p++){
  977.                                         $from   = $price[$p][0]; 
  978.                                         $to     = $price[$p][1];
  979.                                         // $price  = $price[$p][2]; 
  980.                                         
  981.                                         if($traffict > $from && $traffict <= $to){
  982.                                             $real_price = $traffict * $price[$p][2];
  983.                                         }else{
  984.                                             continue;
  985.                                         }
  986.                                     }
  987.                                 }
  988.  
  989.                                
  990.                                 $tot = $temp_id[1] * $real_price;
  991.                                                                
  992.                                 $item_d[] = [
  993.                                     "item_name" => $temp_id[0],
  994.                                     "quantity"  => $temp_id[1],
  995.                                     "unitprice" => $real_price,
  996.                                     "total"     => $tot
  997.                                 ];
  998.  
  999.                                 $sum_quantity   += $temp_id[1];
  1000.  
  1001.                                 $sum_uprice     += $real_price;
  1002.                                 $sum_total      += $tot;
  1003.                                                                
  1004.                                                                 $new_ppns       = $this->check_ppn($vat, $tot);
  1005.                                                                 $new_ppn        += $new_ppns;
  1006.                             }
  1007.  
  1008.                             $it = [
  1009.                                 "item"      => $data['produk'],
  1010.                                 "item_desc" => $item_d
  1011.                             ];
  1012.  
  1013.                             $json = [
  1014.                                 "detail" => [
  1015.                                     "info"  => [
  1016.                                         "from"         => "PT Sprint Asia",
  1017.                                         "recipient"    => $client_names,
  1018.                                         "reff_no"      => '',
  1019.                                         "cust_id"      => '',
  1020.                                         "sales_person" => ''
  1021.                                     ],
  1022.                                     "data"  => array(array($it)),
  1023.                                     "total" => [
  1024.                                         "quantity"  => $sum_quantity,
  1025.                                         "unitprice" => $sum_uprice,
  1026.                                         "sub_total" => ceil($sum_total),
  1027.                                         "with_ppn"  => floor($new_ppn),
  1028.                                         "with_pph"  => 0
  1029.  
  1030.                                     ]
  1031.                                 ],
  1032.  
  1033.                             ];
  1034.                             $serialize = json_encode($json);
  1035.                             
  1036.                            
  1037.                             $data_div           = $this->get_data_division_by_name_and_client_code($division_name, $client_code);
  1038.                                                         if ($data_div == null || $data_div == '' || $data_div == "") {
  1039.                                                                 return redirect()->back()->with('alert', 'Please Check Data Divisi ' . $division_name .' Again. File Cannot be Executed.');;
  1040.                                                         }
  1041.                             
  1042.                             //Division
  1043.                             $division_names         = $data_div['division_name'];
  1044.                             $division_code          = $data_div['division_code'];
  1045.                             $bank_acc_number_sprint = $data_div['division_bank_acc_number_sprint'];
  1046.                             $bank_acc_name_sprint   = $data_div['division_bank_acc_name_sprint'];
  1047.                             $bank_coa_sprint        = $data_div['division_bank_coa_sprint'];
  1048.                             $zahir_name             = $data_div['division_zahir_name'];
  1049.                             $client_npwp_no         = $data_div['division_npwp'];
  1050.                             $bank_acc_name_client   = $data_div['division_bank_account_name'];
  1051.                             $bank_acc_number_client = $data_div['division_bank_account_number'];
  1052.                             $bank_name              = $data_div['division_bank_name_sprint'];
  1053.                             $client_coa             = $data_div['division_bank_name_sprint'];
  1054.                             $division_address       = $data_div['division_address'];
  1055.                                                         $division_invoice_rule  = $data_div['division_invoice_rule']; 
  1056.                                                         $division_max_amount    = $data_div['division_max_amount'];
  1057.                             $division_description   = $data_div['division_description'];
  1058.                             $ba                     = $data_div['berita_acara'];
  1059.                             if($ba == 1){$ba = "Yes";}else{$ba = "No";}
  1060.                             $fp                     = $data_div['faktur_pajak'];
  1061.                             if($fp == 1){$fp = "Yes";}else{$fp = "No";}
  1062.                             $softcopy               = $data_div['soft_copy'];
  1063.                             if($softcopy == 1){$softcopy = "Yes";}else{$softcopy = "No";}
  1064.                             $hardcopy               = $data_div['hard_copy'];
  1065.                             if($hardcopy == 1){$hardcopy = "Yes";}else{$hardcopy = "No";}
  1066.                                                        
  1067.                                                         $dept_code              = "$division_code-00";
  1068.                                                         $data_dept              = $this->get_data_dept_by_id($dept_code);
  1069.                                                        
  1070.                                                         if ($data_dept == null || $data_dept == '' || $data_dept == "") {
  1071.                                                                 return redirect()->back()->with('alert', 'Please Check Data Department '.$dept_code.' Again. File Cannot be Executed.');
  1072.                                                         }
  1073.                                                        
  1074.                                                         $dept_code = $data_dept['dept_code'];
  1075.                             $dept_names = $data_dept['dept_name'];
  1076.  
  1077.                             //PIC 
  1078.                             $data_pic      = $this->get_data_pics_by_id($division_code);
  1079.                                                         if ($data_pic == null || $data_pic == '' || $data_pic == "") {
  1080.                                                                 return redirect()->back()->with('alert', 'Please Check Data PIC from Division '.$division_code.'. File Cannot be Executed.');
  1081.                                                         }
  1082.                             $pic_id       = $data_pic['pic_id'];
  1083.                             $pic_name     = $data_pic['pic_name'];
  1084.                             $email_client = $data_pic['pic_email'];
  1085.                             $pic_position = $data_pic['pic_position'];
  1086.  
  1087.                             $data_pic_collection = $this->get_data_pic_collection($division_code);
  1088.                             if ($data_pic_collection == null || $data_pic_collection == '' || $data_pic_collection == "") {
  1089.                                                                 $pic_collection_name        = "";
  1090.                                 $email_pic_collection       = $email_client;
  1091.                                                         }
  1092.                             $pic_collection_name        = $data_pic_collection['pic_name'];
  1093.                             $email_pic_collection       = $data_pic_collection['pic_email'];
  1094.  
  1095.                             $data_pic_pph23 = $this->get_data_pic_pph23($division_code);
  1096.                             if ($data_pic_pph23 == null || $data_pic_pph23 == '' || $data_pic_pph23 == "") {
  1097.                                                                 $pic_pph23_name        = "";
  1098.                                 $email_pic_pph23       = $email_client;
  1099.                                                         }
  1100.                             $pic_pph23_name        = $data_pic_pph23['pic_name'];
  1101.                             $email_pic_pph23       = $data_pic_pph23['pic_email'];
  1102.  
  1103.                             $sub_total = ceil($sum_total);
  1104.                                                         $amount_invoice_rule = $sub_total + $new_ppn;
  1105.                                                        
  1106.                                                         if($new_ppn > 0){
  1107.                                                                 $vats = "exclude";
  1108.                                                         }else if($new_ppn <= 0){
  1109.                                                                 $vats = "include";
  1110.                                                         }
  1111.                                                        
  1112.                                                         //cek BUMN/Lembaga
  1113.                                                         $real                                  = $this->real_field($lmbg, $invoice_rule, $max_amount, $amount_invoice_rule, $sub_total, $vats);
  1114.                        
  1115.                                                         $sub_total_now                  = $real[0];
  1116.                                                         $sub_total_real                 = $real[1];
  1117.                                                         $new_ppn_now                    = $real[2];
  1118.                                                         $new_ppn_real                   = $real[3];
  1119.                        
  1120.                                                         $new_materai = 0;
  1121.                             if ($materai_client     == 1) {
  1122.                                 $new_materai = $this->check_materai($sub_total_now, $new_ppn_now);
  1123.                             } else if ($materai_client     == 0) {
  1124.                                 $new_materai = 0;
  1125.                             }
  1126.                             $pph23   = $this->check_pph23($client_code, $sub_total_now);
  1127.  
  1128.                             $Total = round($sub_total_now + $new_ppn_now + $new_materai - $pph23);
  1129.                                                         $Total_real = round($sub_total_real + $new_ppn_real + $new_materai - $pph23);
  1130.                             $now = \Carbon\Carbon::now();
  1131.                             $descManual = "-";
  1132.                                                        
  1133.                                                         if($materai_client == 1 ){
  1134.                                                                 $materai = "yes";
  1135.                                                                
  1136.                                                         }else{
  1137.                                                                 $materai = "no";
  1138.                                                                
  1139.                                                         }
  1140.                                                        
  1141.                             $manual_invoice = [
  1142.                                 "client_code"   => $client_code,
  1143.                                 "dept_code"     => $dept_code,
  1144.                                 "division_code" => $division_code,
  1145.                                 "desc"          => $descManual,
  1146.                                 "vat"           => $vat, //?
  1147.                                 "materai"       => $materai, //?
  1148.                                 "total"         => $Total
  1149.                             ];
  1150.  
  1151.                             $last_id = DB::table("db_sprint_ar.manual_invoice")->insertGetId($manual_invoice);
  1152.  
  1153.                             $invXls[] = array(
  1154.                                 "client_code"                 => $client_code,
  1155.                                 "client_name"                 => $client_names,
  1156.                                 "division_code"               => $division_code,
  1157.                                 "division_name"               => $division_names,
  1158.                                 "division_description"        => $division_description,
  1159.                                 "dept_code"                   => $dept_code,
  1160.                                 "dept_name"                   => $dept_names,
  1161.                                 "pic_id"                      => $pic_id,
  1162.                                 "pic_name"                    => $pic_name,
  1163.                                 "pic_position"                => $pic_position,
  1164.                                 "inv_month"                   => $month,  //date("m"),
  1165.                                 "inv_year"                    => $year, //date("yy"),
  1166.                                 "unitprice"                   => $sum_uprice,
  1167.                                 "quantity"                    => $sum_quantity,
  1168.                                 "sub_total"                   => ceil($sub_total_now),
  1169.                                 "ppn"                         => floor($new_ppn_now),
  1170.                                                                 "sub_total_real"              => ceil($sub_total_real),
  1171.                                 "ppn_real"                    => floor($new_ppn_real),
  1172.                                 "materai"                     => $new_materai,
  1173.                                 "pph23"                       => $pph23,
  1174.                                 "total"                       => $Total,
  1175.                                                                 "total_real"                  => $Total_real,
  1176.                                 "serialize_data"              => $serialize,
  1177.                                 "selisih"                     => 0,
  1178.                                 "status"                      => 0,
  1179.                                 "addmanual"                   => 1,
  1180.                                 "id_manual"                   => $last_id,
  1181.                                 "division_address"            => $division_address,
  1182.                                 "bank_acc_number_sprint"      => $bank_acc_number_sprint,
  1183.                                 "bank_acc_name_sprint"               => $bank_acc_name_sprint,
  1184.                                 "bank_acc_number_client"      => $bank_acc_number_client,
  1185.                                 "bank_acc_name_client"        => $bank_acc_name_client,
  1186.                                 "bank_coa_sprint"             => $bank_coa_sprint,
  1187.                                 "zahir_name"                  => $zahir_name,
  1188.                                 "client_npwp_no"              => $client_npwp_no,
  1189.                                 "bank_name"                   => $bank_name,
  1190.                                 "client_coa"                  => $client_coas,
  1191.                                 "revenue_coa"                 => $revenue_coa,
  1192.                                 "email_client"                => $email_client,
  1193.                                 "email_collection"            => $email_pic_collection,
  1194.                                 "email_pph23"                 => $email_pic_pph23,
  1195.                                 "description"                 => $item_name,
  1196.                                 "created_at"                  => $now,
  1197.                                                                 "id_collection"               =>'[""]',
  1198.                                 "ba"                      => $ba,
  1199.                                 "fp"                      => $fp,
  1200.                                 "softcopy"                => $softcopy,
  1201.                                 "hardcopy"                => $hardcopy
  1202.  
  1203.                             );
  1204.  
  1205.                             // return $invXls;
  1206.                            
  1207.                                                         $manual_detail = [];
  1208.                                                         for($f = 0; $f < count($item_d); $f++){
  1209.                                                                 if($f != 0){
  1210.                                                                         $item_name = null;
  1211.                                                                        
  1212.                                                                 }
  1213.                                                                
  1214.                                                                 $manual_detail[] = [
  1215.                                                                                 "id_manual"         => $last_id,
  1216.                                                                                 "service"           => $item_d[$f]['item_name'] ,
  1217.                                                                                 "quantity"          => $item_d[$f]['quantity'],
  1218.                                                                                 "unit_price"        => $item_d[$f]['unitprice'],
  1219.                                                                                 "desc"              => $item_name,
  1220.                                                                                 "total"             => $item_d[$f]['total']
  1221.                                                                         ];
  1222.                                                         }      
  1223.                                                        
  1224.                                                         $last_id = DB::table("db_sprint_ar.manual_detail")->insert($manual_detail);
  1225.                         // } catch (Exception $e) {
  1226.                         //     return redirect()->back()->with('alert', 'Please Check Your Data Again. File Cannot be Executed.');
  1227.                         // }
  1228.                     }
  1229.                 }
  1230.  
  1231.                
  1232.                                 // return $invXls;
  1233.                 if (!empty($invXls)) {
  1234.                     DB::table('db_sprint_ar.invoice')->insert($invXls);
  1235.                                        
  1236.                 }
  1237.                 DB::commit();
  1238.                 $up_manualInvoice   = [
  1239.                     "path_name" => $path_name,
  1240.                     "file_name" => $nameFileUpload,
  1241.                     "size"      => $size,
  1242.                     "created_at"    => now()
  1243.     
  1244.                 ];
  1245.                 
  1246.                 $insert_upload_ = DB::table('upload_manual_invoice')->insert($up_manualInvoice);
  1247.                 return redirect()->back()->with('alert', 'Import File Success.');
  1248.             } else {
  1249.                 return redirect()->back()->with('alert', 'Something Wrong. Excel Only.');
  1250.             }
  1251.         } catch (Exception $e) {
  1252.             DB::rollback();
  1253.         }
  1254.                
  1255.     }
  1256.        
  1257.     public function get_data_manual_invoice($id)
  1258.     {
  1259.         $top = DB::table('db_sprint_ar.manual_invoice AS a')
  1260.             ->where('a.id', $id)
  1261.             ->join('db_sprint_ar.invoice AS b', 'b.id_manual', 'a.id')
  1262.             ->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') 
  1263.             ->get();
  1264.  
  1265.         $details = DB::table('db_sprint_ar.manual_detail')
  1266.             ->where('id_manual', $id)
  1267.             ->select('id_manual', 'service', 'quantity', 'unit_price', 'total', 'desc')
  1268.             ->get()
  1269.             ->groupBy('id_manual');
  1270.  
  1271.         foreach ($top as  $d) {
  1272.             foreach ($details[$d->id] as $dt) {
  1273.                 $id_desc = $this->get_id_desc($dt->service);
  1274.                 $datas[] = [
  1275.                     "id_service"     => $id_desc,
  1276.                     "service"        => $dt->service,
  1277.                     "quantity"       => $dt->quantity,
  1278.                     "unit_price"     => $dt->unit_price,
  1279.                     "total"          => $dt->total,
  1280.                                         "desc"           => $dt->desc
  1281.                 ];
  1282.             }
  1283.  
  1284.             return $data = [
  1285.                 "id"                   => $d->id,
  1286.                 "client_code"          => $d->client_code,
  1287.                 "client_name"          => $d->client_name,
  1288.                 "dept_code"            => $d->dept_code,
  1289.                 "division_code"        => $d->division_code,
  1290.                 "desc"                 => $d->desc,
  1291.                 "vat"                  => $d->vat,
  1292.                 "materai"              => $d->materai,
  1293.                 "coa_of_category"      => $d->revenue_coa,
  1294.                 "bhp"                              => $d->bhp,
  1295.                 "uso"                              => $d->uso,
  1296.                 "details"              => $datas
  1297.             ];
  1298.         }
  1299.     }
  1300.        
  1301.         public function real_field($lmbg, $invoice_rule, $max_amount, $amount_invoice_rule, $sub_total, $vat){
  1302.                 if(($lmbg == "yes" || $lmbg == "Yes" ) && $invoice_rule == 1 && $vat == 'exclude' ){ //exclude
  1303.                                 if($sub_total > $max_amount){
  1304.                                         $sub_total_now                 = ceil($sub_total);
  1305.                                         $sub_total_real        = ceil($sub_total);
  1306.                                         $new_ppn_now           = floor($sub_total * 0.11);
  1307.                                         $new_ppn_real          = 0; //ke sprint
  1308.                                 }else{
  1309.                                         $sub_total_now                 = ceil($sub_total);
  1310.                                         $sub_total_real        = ceil($sub_total); 
  1311.                                         $new_ppn_now           = floor($sub_total * 0.11);
  1312.                                         $new_ppn_real          = floor($sub_total_real * 0.11);
  1313.                                 }
  1314.                         }else if(($lmbg == "yes" || $lmbg == "Yes" ) && $invoice_rule == 1 && $vat == 'include' ){ //include
  1315.                                 if($sub_total > $max_amount){
  1316.                                         $sub_total_now                 = ceil($sub_total);
  1317.                                         $sub_total_real        = ceil($sub_total / 1.11); 
  1318.                                         $new_ppn_now           = 0; //$sub_total * 0.1;
  1319.                                         $new_ppn_real          = 0; //$sub_total * 0.1;
  1320.                                 }else{
  1321.                                         $sub_total_now                 = ceil($sub_total);
  1322.                                         $sub_total_real        = ceil($sub_total / 1.11); 
  1323.                                         $new_ppn_now           = 0;
  1324.                                         $new_ppn_real          = floor($sub_total_real * 0.11);
  1325.                                 }
  1326.                         }else if(($lmbg == "yes" || $lmbg == "Yes" ) && $invoice_rule == 2 && $vat == 'exclude'){ //exclude
  1327.                                 if($amount_invoice_rule > $max_amount){
  1328.                                         $sub_total_now                 = ceil($sub_total);
  1329.                                         $sub_total_real        = ceil($sub_total);
  1330.                                         $new_ppn_now           = floor($sub_total * 0.11);
  1331.                                         $new_ppn_real          = 0; //ke sprint
  1332.                                 }else{
  1333.                                         $sub_total_now                 = ceil($sub_total);
  1334.                                         $sub_total_real        = ceil($sub_total); 
  1335.                                         $new_ppn_now           = floor($sub_total * 0.11);
  1336.                                         $new_ppn_real          = floor($sub_total_real * 0.11);
  1337.                                 }
  1338.                         }else if(($lmbg == "yes" || $lmbg == "Yes" ) && $invoice_rule == 2 && $vat == 'include' ){ //include
  1339.                                 if($amount_invoice_rule > $max_amount){
  1340.                                         $sub_total_now                 = ceil($sub_total);
  1341.                                         $sub_total_real        = ceil($sub_total / 1.11); 
  1342.                                         $new_ppn_now           = 0; //$sub_total * 0.1;
  1343.                                         $new_ppn_real          = 0; //$sub_total * 0.1;
  1344.                                 }else{
  1345.                                         $sub_total_now                 = ceil($sub_total);
  1346.                                         $sub_total_real        = ceil($sub_total / 1.11); 
  1347.                                         $new_ppn_now           = 0;
  1348.                                         $new_ppn_real          = floor($sub_total_real * 0.11);
  1349.                                 }
  1350.                         }else if(($lmbg == "no" || $lmbg == "No" || $lmbg == null || $lmbg == '' ) && $vat == 'exclude'){ //exlude
  1351.                                
  1352.                                         $sub_total_now                 = ceil($sub_total);
  1353.                                         $sub_total_real        = ceil($sub_total); /// 1.1; 
  1354.                                         $new_ppn_now           = floor($sub_total * 0.11);
  1355.                                         $new_ppn_real          = floor($sub_total_real * 0.11);
  1356.                                
  1357.                         }else if(($lmbg == "no" || $lmbg == "No" || $lmbg == null || $lmbg == '') && $vat == 'include'){ //include
  1358.                                
  1359.                                         $sub_total_now                 = ceil($sub_total);
  1360.                                         $sub_total_real        = ceil($sub_total / 1.11); 
  1361.                                         $new_ppn_now           = 0; //$sub_total * 0.1;
  1362.                                         $new_ppn_real          = floor($sub_total_real * 0.11);
  1363.                                
  1364.                         }
  1365.                        
  1366.                         $real   = [$sub_total_now, $sub_total_real, $new_ppn_now, $new_ppn_real];
  1367.                        
  1368.                         return $real;
  1369.                
  1370.         }
  1371.  
  1372.     public function get_data_manual_detail($id)
  1373.     {
  1374.         return $dtdetail = DB::table('manual_detail')
  1375.             ->join('db_sprint_account.services', 'db_sprint_account.services.service_code', '=', 'manual_detail.service')
  1376.             ->where('manual_detail.id_manual', $id)
  1377.             ->get();
  1378.     }
  1379.  
  1380.     public function get_data_sc(Request $request)
  1381.     {
  1382.         $sc = new \GuzzleHttp\Client(['base_uri' => 'http://192.168.100.53/sprint-sysadmin-dev/public/api-master/getservice_category']);
  1383.         $headers = [
  1384.             'Authorization' => 'Bearer ' . $_COOKIE["token"],
  1385.             'Accept'        => 'application/json',
  1386.         ];
  1387.         $response = $sc->request('GET', '', [
  1388.             'headers' => $headers,
  1389.             // 'query' => ['client_code' => $code]
  1390.         ]);
  1391.         $dtsc = json_decode($response->getBody()->getContents(), true)['data'];
  1392.         return json_encode($dtsc);
  1393.     }
  1394.  
  1395.     public function get_data_service_odoo(Request $request, $id)
  1396.     {
  1397.         $sc = new \GuzzleHttp\Client(['base_uri' => 'http://192.168.100.53/sprint-sysadmin-dev/public/api-master/get_data_service_odoo']);
  1398.         $headers = [
  1399.             'Authorization' => 'Bearer ' . $_COOKIE["token"],
  1400.             'Accept'        => 'application/json',
  1401.         ];
  1402.         $response = $sc->request('GET', '', [
  1403.             'headers' => $headers,
  1404.             'query' => ['coa_of_category' => $id]
  1405.         ]);
  1406.         $dtsc = json_decode($response->getBody()->getContents(), true)['data'];
  1407.         return json_encode($dtsc);
  1408.     }
  1409.  
  1410.     public function get_data_sc_e($code)
  1411.     {
  1412.         $sc_e = new \GuzzleHttp\Client(['base_uri' => 'http://192.168.100.53/sprint-sysadmin-dev/public/api-master/getservice_category_e']);
  1413.         $headers = [
  1414.             'Authorization' => 'Bearer ' . $_COOKIE["token"],
  1415.             'Accept'        => 'application/json',
  1416.         ];
  1417.         $response = $sc_e->request('GET', '', [
  1418.             'headers' => $headers,
  1419.             'query' => ['revenue_coa' => $code]
  1420.         ]);
  1421.         $dtsc_e = json_decode($response->getBody()->getContents(), true)['data'];
  1422.         return json_encode($dtsc_e);
  1423.     }
  1424.  
  1425.     public function get_data_division($code)
  1426.     {
  1427.         $client = new \GuzzleHttp\Client(['base_uri' => 'http://192.168.100.53/sprint-sysadmin-dev/public/api-master/get_data_division']);
  1428.         $headers = [
  1429.             'Authorization' => 'Bearer ' . $_COOKIE["token"],
  1430.             'Accept'        => 'application/json',
  1431.         ];
  1432.         $response = $client->request('GET', '', [
  1433.             'headers' => $headers,
  1434.             'query' => ['client_code' => $code]
  1435.         ]);
  1436.         $dtclient = json_decode($response->getBody()->getContents(), true)['data'];
  1437.         return json_encode($dtclient);
  1438.     }
  1439.  
  1440.     public function get_data_dept_name($code)
  1441.     {
  1442.         $client = new \GuzzleHttp\Client(['base_uri' => 'http://192.168.100.53/sprint-sysadmin-dev/public/api-master/get_data_dept']);
  1443.         $headers = [
  1444.             'Authorization' => 'Bearer ' . $_COOKIE["token"],
  1445.             'Accept'        => 'application/json',
  1446.         ];
  1447.         $response = $client->request('GET', '', [
  1448.             'headers' => $headers,
  1449.             'query' => ['division_code' => $code]
  1450.         ]);
  1451.         $dtclient = json_decode($response->getBody()->getContents(), true)['data'];
  1452.         return json_encode($dtclient);
  1453.     }
  1454.  
  1455.     public function get_data_service()
  1456.     {
  1457.         $dtservs = ServiceCategory::select('service_category_code', 'service_category_name', 'coa_of_category')->get();
  1458.         return response()->json($dtservs);
  1459.     }
  1460.  
  1461.     public function get_client_name($id)
  1462.     {
  1463.         $nameclient = DB::table('clients')->select('client_code', 'client_name')->where('client_code', '=', $id)->get();
  1464.         return response()->json($nameclient);
  1465.     }
  1466.  
  1467.     public function get_service_category(Request $request) 
  1468.     {
  1469.         $client = new \GuzzleHttp\Client(['base_uri' => 'http://192.168.100.53/sprint-sysadmin-dev/public/api-master/get_all_service']);
  1470.         $headers = [
  1471.             'Authorization' => 'Bearer ' . $_COOKIE["token"],
  1472.             'Accept'        => 'application/json',
  1473.         ];
  1474.         $response = $client->request('GET', '', [
  1475.             'headers' => $headers,
  1476.             'query' => ['client_name' => $request["vclient"]]
  1477.         ]);
  1478.         $dtclient = json_decode($response->getBody()->getContents(), true)['data'];
  1479.         return json_encode($dtclient);
  1480.     }
  1481.  
  1482.     public function serviceCategoryAutocomplete(Request $request)
  1483.     {
  1484.         $scategory = new \GuzzleHttp\Client(['base_uri' => 'http://192.168.100.53/sprint-sysadmin-dev/public/api-master/getservicecategory/']);
  1485.         $headers = [
  1486.             'Authorization' => 'Bearer ' . $_COOKIE["token"],
  1487.             'Accept'        => 'application/json',
  1488.         ];
  1489.         $response = $scategory->request('GET', '', [
  1490.             'headers' => $headers,
  1491.             'query' => ['service_category' => $request["sc_name"]]
  1492.         ]);
  1493.         return json_decode($response->getBody()->getContents(), true)['data'];
  1494.     }
  1495.  
  1496.     public function get_data_division_2($code)
  1497.     {
  1498.         $client = new \GuzzleHttp\Client(['base_uri' => 'http://192.168.100.53/sprint-sysadmin-dev/public/api-master/get_data_all_division_2']);
  1499.         $headers = [
  1500.             'Authorization' => 'Bearer ' . $_COOKIE["token"],
  1501.             'Accept'        => 'application/json',
  1502.         ];
  1503.         $response = $client->request('GET', '', [
  1504.             'headers' => $headers,
  1505.             'query' => ['client_code' => $code]
  1506.         ]);
  1507.         $dtclient = json_decode($response->getBody()->getContents(), true)['data'];
  1508.         return json_encode($dtclient);
  1509.     }
  1510.  
  1511.     public function get_data_dept_2($code)
  1512.     {
  1513.         $client = new \GuzzleHttp\Client(['base_uri' => 'http://192.168.100.53/sprint-sysadmin-dev/public/api-master/get_data_all_dept_2']);
  1514.         $headers = [
  1515.             'Authorization' => 'Bearer ' . $_COOKIE["token"],
  1516.             'Accept'        => 'application/json',
  1517.         ];
  1518.         $response = $client->request('GET', '', [
  1519.             'headers' => $headers,
  1520.             'query' => ['division_code' => $code]
  1521.         ]);
  1522.         $dtclient = json_decode($response->getBody()->getContents(), true)['data'];
  1523.         return json_encode($dtclient);
  1524.     }
  1525.  
  1526.     public function ServiceAutocomplete(Request $request)
  1527.     {
  1528.         $Uri = new \GuzzleHttp\Client(['base_uri' => 'http://192.168.100.53/sprint-sysadmin-dev/public/api-master/getcustomerautocomplete/']);
  1529.         $headers = [
  1530.             'Authorization' => 'Bearer ' . $_COOKIE["token"],
  1531.             'Accept'        => 'application/json',
  1532.         ];
  1533.         $response = $Uri->request('GET', '', [
  1534.             'headers' => $headers,
  1535.             'query' => ['client_name' => $request["vclient"]]
  1536.         ]);
  1537.         $data = json_decode($response->getBody()->getContents(), true)['data'];
  1538.         return json_encode($dtclient);
  1539.     }
  1540.  
  1541.     public function get_data_division_by_id($division_code)
  1542.     {
  1543.         try {
  1544.             $Uri = new \GuzzleHttp\Client(['base_uri' => 'http://192.168.100.53/sprint-sysadmin-dev/public/api-master/get_data_division_by_id']);
  1545.             $headers = [
  1546.                 'Authorization' => 'Bearer ' . $_COOKIE["token"],
  1547.                 'Accept'        => 'application/json',
  1548.             ];
  1549.             $response = $Uri->request('GET', '', [
  1550.                 'headers' => $headers,
  1551.                 'query' => ['division_code' => $division_code]
  1552.             ]);
  1553.             return $data = json_decode($response->getBody()->getContents(), true)['data'][0];
  1554.         } catch (Exception $e) {
  1555.             return [];
  1556.         }
  1557.     } // end f
  1558.  
  1559.     public function get_data_pics_by_id($division_code)
  1560.     {
  1561.         try {
  1562.             $Uri = new \GuzzleHttp\Client(['base_uri' => 'http://192.168.100.53/sprint-sysadmin-dev/public/api-master/get_data_pics_by_id']);
  1563.             $headers = [
  1564.                 'Authorization' => 'Bearer ' . $_COOKIE["token"],
  1565.                 'Accept'        => 'application/json',
  1566.             ];
  1567.                 //dd($division_code);
  1568.             $response = $Uri->request('GET', '', [
  1569.                 'headers' => $headers,
  1570.                 'query' => ['division_code' => $division_code]
  1571.                 //'query' => ['division_code' => "181100883-00"]
  1572.             ]);
  1573.                 //dd($response->getBody()->getContents());
  1574.             return $data = json_decode($response->getBody()->getContents(), true)['data'][0];
  1575.         } catch (Exception $e) {
  1576.             return [];
  1577.         }
  1578.     } // end f
  1579.  
  1580.     public function get_data_pic_collection($division_code)
  1581.     {
  1582.         try {
  1583.             $Uri = new \GuzzleHttp\Client(['base_uri' => 'http://192.168.100.53/sprint-sysadmin-dev/public/api-master/get_data_pic_collection']);
  1584.             $headers = [
  1585.                 'Authorization' => 'Bearer ' . $_COOKIE["token"],
  1586.                 'Accept'        => 'application/json',
  1587.             ];
  1588.             $response = $Uri->request('GET', '', [
  1589.                 'headers' => $headers,
  1590.                 'query' => ['division_code' => $division_code]
  1591.             ]);
  1592.             return $data = json_decode($response->getBody()->getContents(), true)['data'][0];
  1593.         } catch (Exception $e) {
  1594.             return [];
  1595.         }
  1596.     } // end f 
  1597.  
  1598.     public function get_data_pic_pph23($division_code)
  1599.     {
  1600.         try {
  1601.             $Uri = new \GuzzleHttp\Client(['base_uri' => 'http://192.168.100.53/sprint-sysadmin-dev/public/api-master/get_data_pic_pph23']);
  1602.             $headers = [
  1603.                 'Authorization' => 'Bearer ' . $_COOKIE["token"],
  1604.                 'Accept'        => 'application/json',
  1605.             ];
  1606.             $response = $Uri->request('GET', '', [
  1607.                 'headers' => $headers,
  1608.                 'query' => ['division_code' => $division_code]
  1609.             ]);
  1610.             return $data = json_decode($response->getBody()->getContents(), true)['data'][0];
  1611.         } catch (Exception $e) {
  1612.             return [];
  1613.         }
  1614.     }
  1615.  
  1616.     public function get_data_client_by_id($client_code)
  1617.     {
  1618.         try {
  1619.             $Uri = new \GuzzleHttp\Client(['base_uri' => 'http://192.168.100.53/sprint-sysadmin-dev/public/api-master/get_data_client_by_id']);
  1620.             $headers = [
  1621.                 'Authorization' => 'Bearer ' . $_COOKIE["token"],
  1622.                 'Accept'        => 'application/json',
  1623.             ];
  1624.             $response = $Uri->request('GET', '', [
  1625.                 'headers' => $headers,
  1626.                 'query' => ['client_code' => $client_code]
  1627.             ]);
  1628.             return $data = json_decode($response->getBody()->getContents(), true)['data'][0];
  1629.         } catch (Exception $e) {
  1630.             return [];
  1631.         }
  1632.     } // end f
  1633.  
  1634.     public function get_data_dept_by_id($dept_code)
  1635.     {
  1636.         try {
  1637.             $Uri = new \GuzzleHttp\Client(['base_uri' => 'http://192.168.100.53/sprint-sysadmin-dev/public/api-master/get_data_dept_by_id']);
  1638.             $headers = [
  1639.                 'Authorization' => 'Bearer ' . $_COOKIE["token"],
  1640.                 'Accept'        => 'application/json',
  1641.             ];
  1642.             $response = $Uri->request('GET', '', [
  1643.                 'headers' => $headers,
  1644.                 'query' => ['dept_code' => $dept_code]
  1645.             ]);
  1646.             return $data = json_decode($response->getBody()->getContents(), true)['data'][0];
  1647.         } catch (Exception $e) {
  1648.             return [];
  1649.         }
  1650.     }
  1651.  
  1652.     public function check_ppn($vat, $sub_total)
  1653.     {
  1654.         if ($vat == 'exclude') {
  1655.             return $ppn = floor($sub_total * 0.11);
  1656.         } else {
  1657.             return $ppn = 0;
  1658.         } // End PPN
  1659.     }
  1660.  
  1661.     public function check_materai($sub_total, $ppn)
  1662.     {
  1663.         $math = $sub_total + $ppn;
  1664.         if ($math < 5000000) {
  1665.             return $materai = 0;
  1666.         }else if ($math >= 5000000) {
  1667.             return $materai = 10000;
  1668.         } // end if
  1669.     } // end function
  1670.  
  1671.     public function check_materai_old($sub_total, $ppn)
  1672.     {
  1673.         $math = $sub_total + $ppn;
  1674.         if ($math < 250000) {
  1675.             return $materai = 0;
  1676.         } else if ($math < 1000000) {
  1677.             return $materai = 3000;
  1678.         } else if ($math >= 1000000) {
  1679.             return $materai = 6000;
  1680.         } // end if
  1681.     } // end function
  1682.  
  1683.     public function check_pph23($client_code, $sub_total)
  1684.     {
  1685.         try {
  1686.             $Uri = new \GuzzleHttp\Client(['base_uri' => 'http://192.168.100.53/sprint-sysadmin-dev/public/api-master/get_pph23']); // Get Template Invoice from Account Management, Parameter id Template Invoice
  1687.             $headers = [
  1688.                 'Authorization' => 'Bearer ' . $_COOKIE["token"],
  1689.                 'Accept'        => 'application/json',
  1690.             ];
  1691.             $response     = $Uri->request('GET', '', [
  1692.                 'headers'   => $headers,
  1693.                 'query'     => ['client_code' => $client_code]
  1694.             ]);
  1695.             $incomeTaxData = json_decode($response->getBody()->getContents(), true)['data'][0];
  1696.             $incomeTax = $incomeTaxData['income_tax_23'];
  1697.         } catch (Exception $e) {
  1698.             // Log::error("Function Error",["name"=>__FUNCTION__, "message"=>$e->getMessage()]);
  1699.             $incomeTax = 0;
  1700.         }
  1701.         if ($incomeTax !== null) {
  1702.             return $sub_total * ($incomeTaxData['income_tax_23'] / 100);
  1703.         } else {
  1704.             return 0;
  1705.         }
  1706.     }
  1707.  
  1708.     public function get_revenue_coa($service_code)
  1709.     {
  1710.         try {
  1711.             $Uri = new \GuzzleHttp\Client(['base_uri' => 'http://192.168.100.53/sprint-sysadmin-dev/public/api-master/get_data_service_category_2']); // Get Template Invoice from Account Management, Parameter id Template Invoice
  1712.             $headers = [
  1713.                 'Authorization' => 'Bearer ' . $_COOKIE["token"],
  1714.                 'Accept'        => 'application/json',
  1715.             ];
  1716.             $response     = $Uri->request('GET', '', [
  1717.                 'headers'   => $headers,
  1718.                 'query'     => ['service_code ' => $service_code]
  1719.             ]);
  1720.             return json_decode($response->getBody()->getContents(), true)['data'][0];
  1721.         } catch (Exception $e) {
  1722.             return '';
  1723.         }
  1724.     }
  1725.  
  1726.     public function get_data_dept_by_name($dept_name)
  1727.     {
  1728.         try {
  1729.             $Uri = new \GuzzleHttp\Client(['base_uri' => 'http://192.168.100.53/sprint-sysadmin-dev/public/api-master/get_data_dept_by_name_2']);
  1730.             $headers = [
  1731.                 'Authorization' => 'Bearer ' . $_COOKIE["token"],
  1732.                 'Accept'        => 'application/json',
  1733.             ];
  1734.             $response = $Uri->request('GET', '', [
  1735.                 'headers' => $headers,
  1736.                 'query' => ['dept_name' => $dept_name]
  1737.             ]);
  1738.             return $data = json_decode($response->getBody()->getContents(), true)['data'][0];
  1739.         } catch (Exception $e) {
  1740.             return [];
  1741.         }
  1742.     }
  1743.  
  1744.     public function get_data_division_by_name($division_name)
  1745.     {
  1746.         try {
  1747.             $Uri = new \GuzzleHttp\Client(['base_uri' => 'http://192.168.100.53/sprint-sysadmin-dev/public/api-master/get_data_division_by_name']);
  1748.             $headers = [
  1749.                 'Authorization' => 'Bearer ' . $_COOKIE["token"],
  1750.                 'Accept'        => 'application/json',
  1751.             ];
  1752.             $response = $Uri->request('GET', '', [
  1753.                 'headers' => $headers,
  1754.                 'query' => ['division_name' => $division_name]
  1755.             ]);
  1756.             return $data = json_decode($response->getBody()->getContents(), true)['data'][0];
  1757.         } catch (Exception $e) {
  1758.             return [];
  1759.         }
  1760.     } //get_data_division_by_name_and_client_code
  1761.  
  1762.     public function get_data_division_by_name_and_client_code($division_name, $client_code)
  1763.     {
  1764.         try {
  1765.             $Uri = new \GuzzleHttp\Client(['base_uri' => 'http://192.168.100.53/sprint-sysadmin-dev/public/api-master/get_data_division_by_name_and_client_code']);
  1766.             $headers = [
  1767.                 'Authorization' => 'Bearer ' . $_COOKIE["token"],
  1768.                 'Accept'        => 'application/json',
  1769.             ];
  1770.             $response = $Uri->request('GET', '', [
  1771.                 'headers' => $headers,
  1772.                 'query' => ['division_name' => $division_name, 'client_code' => $client_code]
  1773.             ]);
  1774.             return $data = json_decode($response->getBody()->getContents(), true)['data'][0];
  1775.         } catch (Exception $e) {
  1776.             return [];
  1777.         }
  1778.     }
  1779.  
  1780.     public function get_data_client_by_name($client_name)
  1781.     {
  1782.         try {
  1783.             $Uri = new \GuzzleHttp\Client(['base_uri' => 'http://192.168.100.53/sprint-sysadmin-dev/public/api-master/get_data_client_by_name']);
  1784.             $headers = [
  1785.                 'Authorization' => 'Bearer ' . $_COOKIE["token"],
  1786.                 'Accept'        => 'application/json',
  1787.             ];
  1788.             $response = $Uri->request('GET', '', [
  1789.                 'headers' => $headers,
  1790.                 'query' => ['client_name' => $client_name]
  1791.             ]);
  1792.             return $data = json_decode($response->getBody()->getContents(), true)['data'][0];
  1793.         } catch (Exception $e) {
  1794.             return [];
  1795.         }
  1796.     }
  1797.  
  1798.     public function get_data_service_category_by_name($service_category_name)
  1799.     {
  1800.         try {
  1801.             $Uri = new \GuzzleHttp\Client(['base_uri' => 'http://192.168.100.53/sprint-sysadmin-dev/public/api-master/get_data_service_category_by_name']);
  1802.             $headers = [
  1803.                 'Authorization' => 'Bearer ' . $_COOKIE["token"],
  1804.                 'Accept'        => 'application/json',
  1805.             ];
  1806.             $response = $Uri->request('GET', '', [
  1807.                 'headers' => $headers,
  1808.                 'query' => ['service_category_name' => $service_category_name]
  1809.             ]);
  1810.             return $data = json_decode($response->getBody()->getContents(), true)['data'][0];
  1811.         } catch (Exception $e) {
  1812.             return [];
  1813.         }
  1814.     }
  1815.  
  1816.     public function get_data_dept_by_client_code_division_code($division_code, $client_code)
  1817.     {
  1818.         try {
  1819.             $Uri = new \GuzzleHttp\Client(['base_uri' => 'http://192.168.100.53/sprint-sysadmin-dev/public/api-master/get_data_dept_by_client_code']);
  1820.             $headers = [
  1821.                 'Authorization' => 'Bearer ' . $_COOKIE["token"],
  1822.                 'Accept'        => 'application/json',
  1823.             ];
  1824.             $response = $Uri->request('GET', '', [
  1825.                 'headers' => $headers,
  1826.                 'query' => ['division_code' => $division_code, 'client_code' => $client_code]
  1827.             ]);
  1828.             return $data = json_decode($response->getBody()->getContents(), true)['data'][0];
  1829.         } catch (Exception $e) {
  1830.             return [];
  1831.         }
  1832.     }
  1833.  
  1834.     public function get_data_price($client_code, $provider_code, $service_code)
  1835.     {
  1836.         try {
  1837.             $Uri = new \GuzzleHttp\Client(['base_uri' => 'http://192.168.100.53/sprint-sysadmin-dev/public/api-master/check_vat']);
  1838.             $headers = [
  1839.                 'Authorization' => 'Bearer ' . $_COOKIE["token"],
  1840.                 'Accept'        => 'application/json',
  1841.             ];
  1842.             $response = $Uri->request('GET', '', [
  1843.                 'headers' => $headers,
  1844.                 'query' => ['client_code' => $client_code, 'provider_code' => $provider_code, 'service_code' => $service_code]
  1845.             ]);
  1846.             return $data = json_decode($response->getBody()->getContents(), true)['data'][0];
  1847.         } catch (Exception $e) {
  1848.             return [];
  1849.         }
  1850.     }
  1851.        
  1852.          public function get_data_revenue_coa($revenue_coa)
  1853.     {
  1854.         try {
  1855.             $Uri = new \GuzzleHttp\Client(['base_uri' => 'http://192.168.100.53/sprint-sysadmin-dev/public/api-master/get_data_revenue_coa']);
  1856.             $headers = [
  1857.                 'Authorization' => 'Bearer ' . $_COOKIE["token"],
  1858.                 'Accept'        => 'application/json',
  1859.             ];
  1860.             $response = $Uri->request('GET', '', [
  1861.                 'headers' => $headers,
  1862.                 'query' => ['revenue_coa' => $revenue_coa]
  1863.             ]);
  1864.             return $data = json_decode($response->getBody()->getContents(), true)['data'][0];
  1865.         } catch (Exception $e) {
  1866.             return [];
  1867.         }
  1868.     }
  1869.  
  1870.     public function get_data_service_sms($service_name)
  1871.     {
  1872.         try {
  1873.             $Uri = new \GuzzleHttp\Client(['base_uri' => 'http://192.168.100.53/sprint-sysadmin-dev/public/api-master/get_data_service_sms']);
  1874.             $headers = [
  1875.                 'Authorization' => 'Bearer ' . $_COOKIE["token"],
  1876.                 'Accept'        => 'application/json',
  1877.             ];
  1878.             $response = $Uri->request('GET', '', [
  1879.                 'headers' => $headers,
  1880.                 'query' => ['service_name' => $service_name]
  1881.             ]);
  1882.             return $data = json_decode($response->getBody()->getContents(), true)['data'][0];
  1883.         } catch (Exception $e) {
  1884.             return [];
  1885.         }
  1886.     }
  1887.                
  1888.         public function zek_view(Request $request)
  1889.     {
  1890.  
  1891.         if (empty($_COOKIE["token"])) {
  1892.             return redirect("http://192.168.100.53/sprint-sysadmin-dev/public/login");
  1893.         } else {
  1894.             $title = "Invoice";
  1895.             $gen = "Cek Paydate Invoice";
  1896.             $content = "Cek Paydate Invoice";
  1897.  
  1898.  
  1899.             $data = DB::table('db_sprint_ar.manual_invoice AS m')
  1900.                 ->join('db_sprint_ar.invoice AS i', 'i.id_manual', 'm.id')
  1901.                 ->select('i.*', 'm.*', 'i.created_at AS tanggal')
  1902.                 ->get();
  1903.             $dtdepts = DB::table('invoice')->select('dept_code', 'dept_name')->get();
  1904.             $dtclients = DB::table('invoice')->select('client_code', 'client_name')->get();
  1905.             $dtdivs = DB::table('invoice')->select('division_code', 'division_name')->get();
  1906.             $dtservs = ServiceCategory::select('service_category_code', 'service_category_name')->get();
  1907.  
  1908.             return view('manual_invoice.zek', compact('title', 'content', 'gen', 'data', 'dtdepts', 'dtclients', 'dtdivs', 'dtservs'));
  1909.         }
  1910.     }
  1911.        
  1912.         public function cek_paydate(Request $request)
  1913.     {
  1914.                 // return "ya?";
  1915.  
  1916.         try {
  1917.             $File               = $request->file('file_cek'); //file_upload
  1918.             $nameFileUpload     = $File->getClientOriginalName();
  1919.             $nameFile = explode("_", $nameFileUpload);
  1920.                         // return $nameFile;
  1921.                         $results = Excel::selectSheetsByIndex(0)->load($File)->get();
  1922.  
  1923.             $header = $results->getHeading(); //get xls's header
  1924.                         // return $header;
  1925.  
  1926.                         $dataFile = Excel::selectSheetsByIndex(0)->load($File, function ($reader) {
  1927.                 $reader->setHeaderRow(0);
  1928.                 $reader->select(['invoice_no', 'total','paid_date'])->get();
  1929.             })->get();
  1930.                         // dd( $dataFile);
  1931.             $extension          = $File->getClientOriginalExtension();
  1932.             $size               = $File->getSize();
  1933.             $path                 = $File->getRealPath();
  1934.                         // return $dataFile->getHeading();
  1935.  
  1936.             if ($extension == "xlsx" || $extension == "xls") {
  1937.                 // $datas = Excel::load($path)->get(); //load all of the data in sheet excel file
  1938.                 $index = 0;
  1939.                                
  1940.                 if ($dataFile->count() != 0) {
  1941.                     DB::beginTransaction();
  1942.                     $invXls = array();
  1943.                     foreach ($dataFile as $data) {
  1944.                         try {
  1945.                             $invoice_no         = $data['invoice_no'];
  1946.                             $total              = $data['total'];
  1947.                             $pay_date            = $data['paid_date'];
  1948.                                                        
  1949.                                                         // return $invoice_no;
  1950.                                                         $cekInv = DB::table("db_sprint_ar.invoice")->where('no_inv', $invoice_no)->get();
  1951.                                                         // return count($cekInv);
  1952.                                                        
  1953.                                                         if(count($cekInv) == 0){
  1954.                                                                 continue;
  1955.                                                                 // return "null";
  1956.                                                         }
  1957.                                                        
  1958.                                                         DB::table("db_sprint_ar.invoice")->where('no_inv', $invoice_no)->update(["paydate" => $pay_date]);
  1959.  
  1960.                         } catch (Exception $e) {
  1961.                             return redirect()->back()->with('alert', 'Please Check Your Data Again. File Cannot be Executed.');
  1962.                         }
  1963.                     }
  1964.                 }
  1965.  
  1966.                 DB::commit();
  1967.                 return redirect()->back()->with('alert', 'Paydate Success.');
  1968.             } else {
  1969.                 return redirect()->back()->with('alert', 'Something Wrong. Excel Only.');
  1970.             }
  1971.         } catch (Exception $e) {
  1972.             DB::rollback();
  1973.         }
  1974.     }
  1975.        
  1976.        
  1977.          public function updateInvoice(Request $request)
  1978.     {
  1979.                  $title = "Invoice";
  1980.             $gen = "Manual Invoice";
  1981.             $content = "Manual Invoice"; 
  1982.  
  1983.          return view('manual_invoice.update_invoice', compact('title', 'content', 'gen'));//, 'data', 'dtdepts', 'dtclients', 'dtdivs', 'dtservs'));
  1984.     } 
  1985.        
  1986.         public function updateInvoiceNum_real(Request $request)
  1987.     { 
  1988.                 // $clients           = DB::select( "SELECT * from clients where lmbnegara is null "); 
  1989.                 // return $clients;
  1990.         try{
  1991.        
  1992.                         $jlh_invoice = DB::select( "SELECT count(id) from invoice");
  1993.                         $inv           = DB::select( "SELECT * from invoice"); 
  1994.                         $tes           = [];
  1995.                         for($c = 0; $c < count($inv); $c++){
  1996.                                 $dClient   = $this->get_data_client_by_name($inv[$c]->client_name);
  1997.                                 if($dClient == null || $dClient  == '' || $dClient == ""){
  1998.                                         $lmbg = null;
  1999.                                         $invoice_rule = null;
  2000.                                         $max_amount = null;
  2001.                                 }else{
  2002.                                         $lmbg = $dClient['lmbnegara'];
  2003.                                         $invoice_rule = $dClient['invoice_rule'];
  2004.                                         $max_amount = $dClient['max_amount'];
  2005.                                 }
  2006.                
  2007.                                 $idInv                                         = $inv[$c]->id;
  2008.                                 $sub_total                             = $inv[$c]->sub_total;
  2009.                                 $ppn                                   = $inv[$c]->ppn;
  2010.                                 $total                                         = $inv[$c]->total;
  2011.                                 $materai                               = $inv[$c]->materai;
  2012.                                 $pph23                                  = $inv[$c]->pph23;
  2013.                                 $amount_invoice_rule   = $sub_total + $ppn;
  2014.                                 $serialize_datas                = json_decode($inv[$c]->serialize_data);
  2015.                                 if($serialize_datas == null){
  2016.                                         $bhp                                   = 0;
  2017.                                         $uso                                   = 0;
  2018.                                 }else{
  2019.                                         $total                                  = $serialize_datas->detail->total;
  2020.                                         if(array_key_exists('bhp', $total)){
  2021.                                                 $bhp                                   = $total->bhp;
  2022.                                                 $uso                                   = $total->uso;
  2023.                                         }else{
  2024.                                                 $bhp                                   = 0;
  2025.                                                 $uso                                   = 0;
  2026.                                         }
  2027.                                 }
  2028.  
  2029.                                 if(($lmbg == "yes" || $lmbg == "Yes" ) && $invoice_rule == 1 && $ppn != 0 ){ //exclude
  2030.                                         if($sub_total > $max_amount){
  2031.                                                 $sub_total_now = $sub_total;
  2032.                                                 $sub_total_real = ceil($sub_total);
  2033.                                                 $new_ppn_now = $sub_total * 0.1;
  2034.                                                 $new_ppn_real = 0; //ke sprint
  2035.                                         }else{
  2036.                                                 $sub_total_now = $sub_total;
  2037.                                                 $sub_total_real = ceil($sub_total); 
  2038.                                                 $new_ppn_now = $sub_total * 0.1;
  2039.                                                 $new_ppn_real = floor($sub_total_real * 0.1);
  2040.                                         }
  2041.                                 }else if(($lmbg == "yes" || $lmbg == "Yes" ) && $invoice_rule == 1 && $ppn == 0 ){ //include
  2042.                                         if($sub_total > $max_amount){
  2043.                                                 $sub_total_now = $sub_total;
  2044.                                                 $sub_total_real = ceil($sub_total / 1.1); 
  2045.                                                 $new_ppn_now = 0; //$sub_total * 0.1;
  2046.                                                 $new_ppn_real = 0;//$sub_total * 0.1;
  2047.                                         }else{
  2048.                                                 $sub_total_now = $sub_total;
  2049.                                                 $sub_total_real = ceil($sub_total / 1.1); 
  2050.                                                 $new_ppn_now = 0;
  2051.                                                 $new_ppn_real = floor($sub_total_real * 0.1);
  2052.                                         }
  2053.                                 }else if(($lmbg == "yes" || $lmbg == "Yes" ) && $invoice_rule == 2 && $ppn != 0 ){ //exclude
  2054.                                         if($amount_invoice_rule > $max_amount){
  2055.                                                 $sub_total_now = $sub_total;
  2056.                                                 $sub_total_real = ceil($sub_total);
  2057.                                                 $new_ppn_now = $sub_total * 0.1;
  2058.                                                 $new_ppn_real = 0; //ke sprint
  2059.                                         }else{
  2060.                                                 $sub_total_now = $sub_total;
  2061.                                                 $sub_total_real = ceil($sub_total); 
  2062.                                                 $new_ppn_now = $sub_total * 0.1;
  2063.                                                 $new_ppn_real = floor($sub_total_real * 0.1);
  2064.                                         }
  2065.                                 }else if(($lmbg == "yes" || $lmbg == "Yes" ) && $invoice_rule == 2 && $ppn == 0 ){ //include
  2066.                                         if($amount_invoice_rule > $max_amount){
  2067.                                                 $sub_total_now = $sub_total;
  2068.                                                 $sub_total_real = ceil($sub_total / 1.1); 
  2069.                                                 $new_ppn_now = 0; //$sub_total * 0.1;
  2070.                                                 $new_ppn_real = 0; //$sub_total * 0.1;
  2071.                                         }else{
  2072.                                                 $sub_total_now = $sub_total;
  2073.                                                 $sub_total_real = ceil($sub_total / 1.1); 
  2074.                                                 $new_ppn_now = 0;
  2075.                                                 $new_ppn_real = floor($sub_total_real * 0.1);
  2076.                                         }
  2077.                                 }else if(($lmbg == "no" || $lmbg == null) && $ppn != 0 ){ //exclude
  2078.                                                 $sub_total_now = $sub_total;
  2079.                                                 $sub_total_real = ceil($sub_total); /// 1.1; 
  2080.                                                 $new_ppn_now = $sub_total * 0.1;
  2081.                                                 $new_ppn_real = floor($sub_total_real * 0.1);
  2082.                                        
  2083.                                 }else if(($lmbg == "no" || $lmbg == null) && $ppn == 0 ){ //include
  2084.                                                 $sub_total_now = $sub_total;
  2085.                                                 $sub_total_real = ceil($sub_total / 1.1); 
  2086.                                                 $new_ppn_now = 0; //$sub_total * 0.1;
  2087.                                                 $new_ppn_real = floor($sub_total_real * 0.1);
  2088.                                 } //
  2089.                                
  2090.                                         $Total_now = $sub_total_now + $new_ppn_now + $materai - $pph23 - $bhp - $uso;
  2091.                                         $Total_real = $sub_total_real + $new_ppn_real + $materai - $pph23 - $bhp - $uso; //subtotal real dan total real bulat atas, ppn real bulat ke bawah
  2092.                  
  2093.                                         \DB::table('invoice')->where('id', $idInv)
  2094.                                                 ->update(['total_real' => $Total_real, 'sub_total_real' => $sub_total_real, 'ppn_real' => $new_ppn_real ]);                   
  2095.                                 }
  2096.                                 return "suksses";
  2097.                 } catch (Exception $e) {
  2098.                         return ["code" => "_ERR_SYSTM", "error" => $e->getMessage()];
  2099.                 }      
  2100.     } 
  2101.  
  2102.     public function updateInvoiceNum(Request $request)
  2103.     { 
  2104.                 // $clients           = DB::select( "SELECT * from clients where lmbnegara is null "); 
  2105.                 // return $clients;
  2106.        try{
  2107.             $inv           = DB::select( "SELECT * from invoice order by id desc limit 2"); 
  2108.             $pic_empty[] = ['No Inv','Client Name', 'Client Code', 'Division Name', 'Division Code', 'Dept Name', 'Dept Code'];
  2109.                     
  2110.                         for($c = 0; $c < count($inv); $c++){
  2111.                 $id             = $inv[$c]->id;
  2112.                 $no_inv         = $inv[$c]->no_inv;
  2113.                 $client_name    = $inv[$c]->client_name;
  2114.                 $client_code    = $inv[$c]->client_code;
  2115.                 $division_code  = $inv[$c]->division_code;
  2116.                 $division_name  = $inv[$c]->division_name;
  2117.                 $dept_code      = $inv[$c]->dept_code;
  2118.                 $dept_name      = $inv[$c]->dept_name;
  2119.                 
  2120.                 $pics = DB::connection('account129')->select("SELECT * FROM db_sprint_account.pics_copy WHERE client_code like '%$client_code%' and division_code like '%$division_code%' and dept_code like '%$dept_code%'");
  2121.                 $cpics = count($pics);
  2122.                 if($cpics = 0 || $pics == null || $pics == '' || $pics == ""){
  2123.                     $pic_empty[] = [$no_inv,$client_name, $client_code,  $division_name,$division_code, $dept_name, $dept_code];
  2124.                     continue;
  2125.                 }
  2126.                 
  2127.                 // $email_client = $pics[0]->pic_email;  
  2128.                             // \DB::table('invoice')->where('id', $id)//->where('client_code', $client_code)->where('division_code', $division_code)->where('dept_code', $dept_code)
  2129.                                 //     ->update(['email_client' => $email_client ]);                       
  2130.             }
  2131.  
  2132.             if(count($pic_empty) > 1){
  2133.                 $df = Excel::create("PIC Empty", function ($excel) use ($pic_empty) {
  2134.                 $excel->setTitle('PIC Empty');
  2135.                 $excel->setCreator('AR')->setCompany('Sprint Asia');
  2136.     
  2137.                 $excel->sheet('PIC Empty', function ($sheet) use ($pic_empty) {
  2138.                   $sheet->fromArray($pic_empty, null, 'A1', false, false);
  2139.                 });
  2140.                 })->export('xlsx');
  2141.             }
  2142.  
  2143.                 } catch (Exception $e) {
  2144.                         return ["code" => "_ERR_SYSTM", "error" => $e->getMessage()];
  2145.                 }      
  2146.     } 
  2147.  
  2148.     public function updateInv(Request $request)
  2149.     { 
  2150.       try{
  2151.            // $jlh_invoice        = DB::select( "SELECT count(id) from invoice");
  2152.             $inv                   = DB::select( "SELECT * FROM invoice");
  2153.             //return $inv;
  2154.             for($c = 0; $c < count($inv); $c++){
  2155.                 $idInv                      = $inv[$c]->id;
  2156.                 $no_inv                                        = $inv[$c]->no_inv;
  2157.                 //return $no_inv;
  2158.                 $arr_no_inv                 = explode("/", $no_inv);
  2159.                 $c_noInv                    = count($arr_no_inv);
  2160.                //return count($arr_no_inv);
  2161.                if($no_inv != null || $no_inv != '' || $no_inv != ""){
  2162.                     if( $c_noInv == 3){
  2163.                         $no_inv_                    = $no_inv;
  2164.                         $no_inv_real                = $no_inv;
  2165.                     }else if($c_noInv == 4){
  2166.                         $no_inv_                    = $no_inv; 
  2167.                         $no_inv_real                = $arr_no_inv[0] . "/" . $arr_no_inv[1] . "/" . $arr_no_inv[3];
  2168.                     }
  2169.  
  2170.                    \DB::table('invoice')->where('id', $idInv)
  2171.                         ->update(['no_inv_real' => $no_inv_real]);
  2172.  
  2173.                 }
  2174.                                 
  2175.             }
  2176.             
  2177.             return "Done";
  2178.                
  2179.                 } catch (Exception $e) {
  2180.                         return ["code" => "_ERR_SYSTM", "error" => $e->getMessage()];
  2181.                 }      
  2182.     } 
  2183.        
  2184.        
  2185.        
  2186.        
  2187.        
  2188.        
  2189.        
  2190.        
  2191.        
  2192.        
  2193.        
  2194.        
  2195.        
  2196.        
  2197.        
  2198.        
  2199.        
  2200.        
  2201.        
  2202.        
  2203.        
  2204.        
  2205.        
  2206. }
  2207.  

Editor

You can edit this paste and save as new:


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