fadlyshafa

Untitled

May 12th, 2020
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 53.86 KB | None | 0 0
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3.  
  4. require_once APPPATH . '/../vendor/autoload.php';
  5.  
  6. class Purchase extends CI_Controller
  7. {
  8.     function __construct() {
  9.         parent::__construct();
  10.         $this->load->model('purchase_model');
  11.         $this->load->model('customer_model');
  12.         $this->load->model('company_setting_model');
  13.         $this->load->model('kurs_model');
  14.         $this->load->model('do_model');
  15.         $this->load->model('signature_model');
  16.         $this->load->model('warehouse_model');
  17.         $this->load->model('supplier_model');
  18.         $this->load->model('log_model');
  19.         $this->load->model('brand_model');
  20.         $this->load->model('ledger_model');
  21.     }
  22.        
  23.     public function index(){
  24.         $filter = null;
  25.         if(isset($_GET['filter'])) {
  26.           if ($_GET['filter'] != null) {
  27.             $filter = $_GET['filter'];
  28.           }
  29.         }
  30.        
  31.         // get all purchase record and display list
  32.         $data['data'] = $this->purchase_model->getPurchase($filter);
  33.         $this->load->view('purchase/list',$data);
  34.     }
  35.     /*
  36.         call add purchase view to add purchase
  37.     */
  38.     public function add()
  39.     {
  40.         // load general model where generateAutoNumber method stored
  41.         $this->load->model('general_model');
  42.  
  43.         $kursdata =  $this->company_setting_model->getCurrency();
  44.        
  45.        
  46.         $data['product'] = $this->purchase_model->getProduct();
  47.         $data['warehouse'] = $this->purchase_model->getWarehouse();
  48.         $data['supplier'] = $this->purchase_model->getSupplier();
  49.         $data['itemrequest'] = $this->warehouse_model->getItemRequestNo();
  50.         $data['brands'] = $this->brand_model->getBrand();
  51.        
  52.         // get purchase autonumber from general model
  53.         $data['reference_no'] = $this->general_model->generateAutoNumber('purchase');
  54.         $data['service'] = $this->purchase_model->getService();
  55.         $data['kursdata'] = $kursdata;
  56.         $data['company'] = $this->company_setting_model->getData();
  57.         $data['tax'] = $this->purchase_model->getTax();
  58.        
  59.         $this->load->view('purchase/add',$data);
  60.        
  61.     }
  62.  
  63.     public function delete_po($id){
  64.         $this->db->set('is_delete',1)->where('purchase_id',$id)->update('purchases');
  65.         redirect('purchase');
  66.     }
  67.  
  68.     public function getItemRequest($supplier_id) {
  69.         $data = $this->warehouse_model->getItemRequestNo($supplier_id);
  70.         echo json_encode($data);
  71.       }
  72.  
  73.    
  74.     public function itemRequestData($id){
  75.         $data = $this->purchase_model->getProductAjaxItem($id);
  76.         $data['discount'] = $this->purchase_model->getDiscount();
  77.         echo json_encode($data);
  78.     }
  79.     /*
  80.         this function is used when product add in purchase table
  81.     */
  82.     public function getProductAjax($id){
  83.         $data = $this->purchase_model->getProductAjax($id);
  84.         $data['discount'] = $this->purchase_model->getDiscount();
  85.         echo json_encode($data);
  86.     }
  87.    
  88.     public function getDetail(){
  89.         $id = $this->input->post('id');
  90.        
  91.         $data = $this->db->select('pi.purchase_item_id, pi.product_id, pi.quantity, pi.quantityreal, p.code, p.name')
  92.             ->from('purchase_items pi')
  93.             ->join('products p', 'pi.product_id=p.product_id' , 'left')
  94.             ->where('pi.purchase_id', $id)
  95.             ->get()
  96.             ->result();    
  97.         $i=0;
  98.        
  99.         echo '<table class="table table-condensed table-border">';
  100.             echo '<thead>';
  101.                 echo '<tr>';
  102.                     echo '<th></th>';
  103.                     echo '<th>Code</th>';
  104.                     echo '<th>Name</th>';
  105.                     echo '<th>Qty Remain</th>';
  106.                     echo '<th>Qty Shipping</th>';
  107.                 echo '</tr>';
  108.             echo '<thead>';
  109.             echo '<tbody>';
  110.         foreach($data as $row){
  111.             echo '<tr>';
  112.            
  113.             echo '<td><input type="checkbox" name="items_id_for_purchase[' . $i . ']" value="' . $row->purchase_item_id . '"></td>';
  114.             echo '<td>' . $row->code . '</td>';
  115.             echo '<td>' . $row->name . '</td>';
  116.             echo '<td>' . ($row->quantity - $row->quantityreal) . '</td>';
  117.             echo '<td><input type="hidden" name="product_id[' . $i . ']" value="' . $row->product_id . '"><input type="number" class="form-control" style="width:100px" value="0" name="items_qty_for_purchase[' . $i . ']" max="' . ($row->quantity - $row->quantityreal) . '"></td>';        
  118.            
  119.             echo '<tr>';
  120.        
  121.             $i++;
  122.         }
  123.             echo '</tbody>';
  124.         echo '</table>';
  125.        
  126.     }
  127.    
  128.     /*
  129.         This function is used to search product code / name in database
  130.     */
  131.     public function getAutoCodeName($code,$search_option){
  132.           //$code = strtolower($code);
  133.           $p_code = $this->input->post('p_code');
  134.           $p_search_option = $this->input->post('p_search_option');
  135.           $data = $this->purchase_model->getProductCodeName($p_code,$p_search_option);
  136.           if($search_option=="Code"){
  137.             $list = "<ul class='auto-product'>";
  138.             foreach ($data as $val){
  139.                 $list .= "<li value=".$val->code.">".$val->code."</li>";
  140.             }
  141.             $list .= "</ul>";
  142.           }
  143.           else{
  144.             $list = "<ul class='auto-product'>";
  145.             foreach ($data as $val){
  146.                 $list .= "<li value=".$val->product_id.">".$val->name."</li>";
  147.             }
  148.             $list .= "</ul>";
  149.           }
  150.          
  151.           echo $list;
  152.           //echo json_encode($data);
  153.           //print_r($list);
  154.     }
  155.    
  156.     public function getProd($id)
  157.     {
  158.         $data = $this->purchase_model->getPurchaseReceiptItem2($id);
  159.         echo json_encode($data);
  160.     }
  161.    
  162.     /*
  163.         This function is used to add purchase in database
  164.     */
  165.    
  166.  
  167.    
  168.     public function addBiaya()
  169.     {
  170.        
  171.     //query
  172.     $ke_slugi = 0;
  173.     $sqlslug= "select count(*) as ke_slug from purchases order by purchase_id desc limit 1";
  174.     $sqlslugi = $this->purchase_model->manualQuery($sqlslug);
  175.     foreach ($sqlslugi->result() as  $ke) {
  176.         $ke_slug = $ke->ke_slug;
  177.     }
  178.     $ke_slugi = $ke_slug + 1;
  179.        
  180.             $dataku = array(
  181.                             "purchase_id"   =>  $ke_slugi,
  182.                             "user"       =>  $this->session->userdata('user_id')
  183.                         );
  184.                        
  185.             $this->purchase_model->insertData("purchases",$dataku);
  186.                
  187.                
  188.         //add biaya
  189.                 $config['upload_path'] = './assets/doc/';
  190.                 $config['allowed_types'] = 'doc|docx|pdf|xls|xlsx';
  191.                 $config['overwrite'] = TRUE;           
  192.                 $this->load->library('upload', $config);
  193.                
  194.                 $this->upload->do_upload('filenya');
  195.                 $aa = $this->upload->data();
  196.                 $ori = $aa['file_name'];
  197.                
  198.                 $data = array(
  199.                             "jenis_biaya" =>  $this->input->post('jenis_biaya'),
  200.                             "keterangan"   =>  $this->input->post('keterangan'),
  201.                             "file_upload"   =>  $ori,
  202.                             "nominal"       =>  $this->input->post('nominal'),
  203.                             "purchase_id"   =>  $ke_slugi
  204.                         );
  205.                        
  206.                 $this->purchase_model->insertData("purchase_biaya",$data);
  207.                 //add biaya
  208.                
  209.                 redirect('purchase/edit/'.$ke_slugi,'refresh');
  210.                
  211.     }
  212.    
  213.    
  214.    
  215.     public function editBiaya($idku)
  216.     {  
  217.                
  218.         //add biaya
  219.                 $config['upload_path'] = './assets/doc/';
  220.                 $config['allowed_types'] = 'doc|docx|pdf|xls|xlsx';
  221.                 $config['overwrite'] = TRUE;           
  222.                 $this->load->library('upload', $config);
  223.                
  224.                 $this->upload->do_upload('filenya');
  225.                 $aa = $this->upload->data();
  226.                 $ori = $aa['file_name'];
  227.                
  228.                 $data = array(
  229.                             "jenis_biaya" =>  $this->input->post('jenis_biaya'),
  230.                             "keterangan"   =>  $this->input->post('keterangan'),
  231.                             "file_upload"   =>  $ori,
  232.                             "nominal"       =>  $this->input->post('nominal'),
  233.                             "purchase_id"   =>  $idku
  234.                         );
  235.                        
  236.                 $this->purchase_model->insertData("purchase_biaya",$data);
  237.                 //add biaya
  238.                
  239.                 redirect('purchase/edit/'.$idku,'refresh');        
  240.     }
  241.    
  242.    
  243.     public function getProdreceived($id)
  244.     {
  245.         $data = $this->do_model->getPurchaseReceiptItem2($id);
  246.         echo json_encode($data);
  247.     }
  248.    
  249.     public function editReceived()
  250.     {
  251.             $purchaseid2 = $this->input->post('purchase_id');
  252.             $purchaseid = $this->input->post('purchase_order');
  253.             $receivedid = $this->input->post('id_received');
  254.        
  255.             $data = array(
  256.                         "receipt_date"          =>  $this->input->post('received_date'),
  257.                         "purchase_id"     =>    $this->input->post('purchase_order'),
  258.                         "supplier"          =>  $this->input->post('supplier'),
  259.                         "warehouse_staff"           =>  $this->input->post('warehouse_staff')
  260.                         );
  261.            
  262.             $datastock=$this->input->post('purchase_item_id');
  263.            
  264.             if(!empty($receivedid))
  265.             {
  266.            
  267.                 if($this->purchase_model->editReceipt($receivedid,$data))
  268.                 {
  269.                     if(!empty($datastock))
  270.                     {
  271.                         $im=0;
  272.                         foreach($datastock as $value)
  273.                         {
  274.                             $dataman=$this->purchase_model->getPurchaseItemId($value);
  275.                            
  276.                             foreach ($dataman as  $valueman)
  277.                             {
  278.                                 $data2 = array(
  279.                                                "quantityreal"           =>  $valueman->quantity
  280.                                               );
  281.                             }
  282.                                
  283.                                          
  284.                             $this->purchase_model->editPurchaseItemId($value,$data2);
  285.                             $im++;
  286.                         }
  287.                     }
  288.                    
  289.                     $this->received($purchaseid2);
  290.                 }
  291.                 else
  292.                 {
  293.                     $this->received($purchaseid2);
  294.                 }
  295.                
  296.             }
  297.             else if(empty($receivedid))
  298.             {
  299.                 if($this->purchase_model->addModelReceipt($data))
  300.                 {
  301.                     if(!empty($datastock))
  302.                     {
  303.                         $im=0;
  304.                         foreach($datastock as $value)
  305.                         {
  306.                             $dataman=$this->purchase_model->getPurchaseItemId($value);
  307.                            
  308.                             foreach ($dataman as  $valueman)
  309.                             {
  310.                                 $data2 = array(
  311.                                                "quantityreal"           =>  $valueman->quantity
  312.                                               );
  313.                             }
  314.                                
  315.                             $this->purchase_model->editPurchaseItemId($value,$data2);                          
  316.                             $im++;
  317.                         }
  318.                     }
  319.                    
  320.                     $this->received($purchaseid2);
  321.                 }
  322.                 else
  323.                 {
  324.                     $this->received($purchaseid2);
  325.                 }
  326.                
  327.             }
  328.             else
  329.             {
  330.                 $this->received($purchaseid2);
  331.             }
  332.     }
  333.    
  334.     /*
  335.         This function is used to add purchase in database
  336.     */
  337.     public function addPurchase()
  338.     {
  339.         $this->form_validation->set_rules('date','Date','trim|required');
  340.         $this->form_validation->set_rules('reference_no','Reference No','trim|required');
  341.         $submit = $this->input->post('submit');
  342.         if ($submit == "Publish") {
  343.           $status = 0;
  344.         } else {
  345.           $status = 6;
  346.         }
  347.         if($this->form_validation->run()==false)
  348.         {
  349.             $this->add();
  350.         }
  351.         else
  352.         {
  353.             $warehouse_id = $this->input->post('warehouse');
  354.             $data = array(
  355.                         "reference_no"  =>  $this->input->post('reference_no'),
  356.                         "date"          =>  $this->input->post('date'),
  357.                         "podate"        =>  $this->input->post('po_date'),
  358.                         "supplier_id"   =>  $this->input->post('supplier'),
  359.                         "upcc"          =>  $this->input->post('main'),
  360.                         "upcc2"         =>  $this->input->post('cc'),
  361.                         "taxdesc"       =>  $this->input->post('tax'),
  362.                         "kurs_id"       =>  $this->input->post('kursdata'),
  363.                         "refer_no"      =>  $this->input->post('refer_no'),
  364.                         "total"         =>  $this->input->post('grand_total'),
  365.                         "discount_value"=>  $this->input->post('total_discount'),
  366.                         "tax_value"     =>  $this->input->post('total_tax'),
  367.                         "pterm"             =>  $this->input->post('paymenterms'),
  368.                         "note"          =>  $this->input->post('note'),
  369.                         "user"          =>  $this->session->userdata('user_id'),
  370.                         "user_name"     => $this->session->userdata('identity'),
  371.                         "diskon"              =>  $this->input->post('diskon_text'),
  372.                         "diskonpersen"        => $this->input->post('diskonpersen_text'),
  373.                         "nominalcurrency"     => $this->input->post('grand_currency'),
  374.                         "dateestimate"  => $this->input->post('dateestimate'),
  375.                         "shipping_address" => $this->input->post('shipping_address'),
  376.                         "billing_address" => $this->input->post('billing_address'),
  377.                         "status" => $status
  378.                     );
  379.  
  380.            
  381.             $invoice = array(
  382.                 "invoice_no" => $this->purchase_model->generateInvoiceNo(),
  383.                 "receipt_amount" => $this->input->post('grand_total'),
  384.                 "receipt_voucher_date" => date('Y-m-d')
  385.             );
  386.  
  387.             // ================ ledger =================
  388.             $am = $this->db->get('account_mapping')->row();
  389.  
  390.             $data2 = array(
  391.                 'ledger_number' => 'PR-'.rand(),
  392.                 'ledger_date' => $this->input->post('date'),
  393.             );
  394.  
  395.             if($idr = $this->ledger_model->saveledger($data2)){
  396.  
  397.                 $id_ledger = $idr;    
  398.                 $account_id = $am->pembelian_cogs;
  399.                 $description = 'Pembelian COGS';
  400.                 $debit = $this->input->post('grand_total');
  401.                 $credit = $this->input->post('grand_total');
  402.                 $datas = array();
  403.  
  404.                 $index = 0;
  405.                 // foreach($account_id as $dataid){
  406.                     array_push($datas,array(
  407.                         'account_id'=>$am->pajak_pembelian,
  408.                         'id_ledger'=>$idr,
  409.                         'description'=>'PPN Masukan',
  410.                         'debit'=>$this->input->post('total_tax'),
  411.                         // 'credit'=>$credit[$index],
  412.                     ));
  413.                     $this->ledger_model->save_batch($datas);
  414.                     $datas = [];
  415.  
  416.                     array_push($datas,array(
  417.                         'account_id'=>$am->pajak_pembelian,
  418.                         'id_ledger'=>$idr,
  419.                         'description'=>'Beban Pokok Pendapatan',
  420.                         'debit'=>$this->input->post('grand_total'),
  421.                         // 'credit'=>$credit[$index],
  422.                     ));
  423.                     $this->ledger_model->save_batch($datas);
  424.                     $datas = [];
  425.  
  426.                     array_push($datas,array(
  427.                         'account_id'=>$am->pembelian_cogs,
  428.                         'id_ledger'=>$idr,
  429.                         'description'=>'Hutang Usaha',
  430.                         'credit'=>$this->input->post('grand_total'),
  431.                         // 'credit'=>$credit[$index],
  432.                     ));
  433.                     $this->ledger_model->save_batch($datas);
  434.                     $datas = [];
  435.                     $index++;
  436.                 // }
  437.             }
  438.            
  439.             if($purchase_id = $this->purchase_model->addModel2($data,$invoice))
  440.             {
  441.                 $log_data = array(
  442.                         'user_id'  => $this->session->userdata('user_id'),
  443.                         'table_id' => $purchase_id,
  444.                         'message'  => 'Purchase Inserted'
  445.                     );
  446.  
  447.                 $this->log_model->insert_log($log_data);
  448.                
  449.                
  450.                 $jumlahtermin= $this->input->post('terminnumber');
  451.        
  452.                 if(!empty($jumlahtermin))
  453.                 {
  454.                     $termins =  $this->input->post('terminvalue');
  455.                     $due_dates =  $this->input->post('due_date');
  456.                     $tanggaltermins = $this->input->post('tanggalterminvalue');
  457.            
  458.                     if(!empty($termins))
  459.                     {
  460.                         $im=0;
  461.                         foreach($termins as $quan)
  462.                         {
  463.                             $value=$this->input->post('grand_total');
  464.                             $nominal=($value*$quan)/100;
  465.                             $nominal = number_format((float)$nominal, 2, '.', '');
  466.                             $due_date = $due_dates[$im];
  467.  
  468.                             if(!empty($tanggaltermins[$im]))
  469.                             {
  470.                                 $datetermin=$tanggaltermins[$im];
  471.                             }
  472.                             else
  473.                             {
  474.                                 $datetermin=date('Y-m-d');
  475.                             }
  476.                            
  477.                             $datatermin = array(
  478.                                     "purchase_id"         =>  $purchase_id,
  479.                                     "persen"          =>  $quan,
  480.                                     "nominal"         =>  $nominal,
  481.                                     "tanggalbayar"    =>  $datetermin,
  482.                                     "due_date"        =>  $due_date
  483.                                    
  484.                                 );
  485.  
  486.                             // echo '<pre>' . var_export($datatermin, true) . '</pre>';
  487.                             // die;
  488.                            
  489.                             $this->purchase_model->addTerminModel($datatermin);
  490.                            
  491.                             $im++;
  492.                         }
  493.                     }
  494.                    
  495.                 }
  496.                
  497.                 $js_data = json_decode($this->input->post('table_data'));
  498.                
  499.                 if(!empty($js_data))
  500.                 {
  501.                     foreach ($js_data as $key => $value)
  502.                     {
  503.                         if($value!=null  || !empty($value->product_id))
  504.                         {
  505.                             $product_id = $value->product_id;
  506.                             $quantity = $value->quantity;
  507.                             $data = array(
  508.                                 "product_id" => $value->product_id,
  509.                                 "quantity" => $value->quantity,
  510.                                 "gross_total" => $value->total,
  511.                                 "discount_id" => @$value->discount_id,
  512.                                 "discount_value" => @$value->discount_value,
  513.                                 "discount" => $value->discount,
  514.                                 "cost" => $value->cost,
  515.                                 "price" => $value->price,
  516.                                 "desc" => $value->desc,
  517.                                 "purchase_id" => $purchase_id
  518.                                 );
  519.  
  520.                             $warehouse_data = array(
  521.                                 "product_id" => $value->product_id,
  522.                                 "warehouse_id" => 0,
  523.                                 "quantity" => $value->quantity
  524.                                 );
  525.                            
  526.                             $this->purchase_model->addProductInWarehouse($product_id,$quantity,$warehouse_id,$warehouse_data);
  527.  
  528.                             $this->db->insert('purchase_items', $data);
  529.                        
  530.                         }
  531.                     }
  532.                 }
  533.                
  534.                 $config = array();
  535.                 $config['upload_path']   = './assets/doc/';
  536.                 $config['allowed_types'] = 'doc|xls|xlsx|docx|pdf|DOC|XLS|XLSX|DOCX|PDF';
  537.                 $config['max_size']      = '0';
  538.                 $config['overwrite']     = FALSE;
  539.                 $config['encrypt_name']  = TRUE;
  540.  
  541.                 $this->load->library('upload');
  542.  
  543.                 $count = count($this->input->post('jenis_biaya'));
  544.  
  545.                 $files = $_FILES;
  546.                 for($i=0; $i< $count; $i++)
  547.                 {          
  548.                     if($_FILES['file']['name'][$i]) {
  549.                         $_FILES['file'.$i]['name']= $files['file']['name'][$i];
  550.                         $_FILES['file'.$i]['type']= $files['file']['type'][$i];
  551.                         $_FILES['file'.$i]['tmp_name']= $files['file']['tmp_name'][$i];
  552.                         $_FILES['file'.$i]['error']= $files['file']['error'][$i];
  553.                         $_FILES['file'.$i]['size']= $files['file']['size'][$i];
  554.  
  555.                         $this->upload->initialize($config);
  556.  
  557.                         if ($this->upload->do_upload('file'.$i)) {
  558.                             $a = $this->upload->data();
  559.                             $filename = $a['file_name'];
  560.                         } else {
  561.                             $filename = '';
  562.                         }
  563.                     }
  564.  
  565.                     $data = array(
  566.                         "jenis_biaya" =>  $this->input->post('jenis_biaya')[$i],
  567.                         "keterangan"   =>  $this->input->post('keterangan')[$i],
  568.                         "pdf"   =>  $this->input->post('pdf')[$i] ?: 0,
  569.                         "file_upload"   =>  $filename,
  570.                         "nominal"       =>  $this->input->post('nominal')[$i],
  571.                         "purchase_id"   =>  $purchase_id
  572.                     );
  573.  
  574.                     $this->db->insert('purchase_biaya', $data);
  575.                 }
  576.                    
  577.            
  578.                 redirect('purchase/view/' . $purchase_id);
  579.             }
  580.             else
  581.             {
  582.                 $this->add();
  583.                
  584.             }
  585.            
  586.         }
  587.        
  588.     }
  589.    
  590.     public function deleteBiaya($id){
  591.         $data = $this->db->select('*')->from('purchase_biaya')->where('id', $id)->get()->result()[0]->file_upload;
  592.         unlink(FCPATH . '/assets/doc/' . $data);
  593.         $this->db->where('id', $id);
  594.         $this->db->delete('purchase_biaya');
  595.     }
  596.  
  597.     public function edit($id)
  598.     {
  599.        
  600.         $data['biaya'] = $this->purchase_model->getBiaya($id);
  601.         $data['purchase_id'] = $id;
  602.        
  603.         $kursdata =  $this->company_setting_model->getCurrency();
  604.        
  605.         // if(!$sock= @fsockopen("www.google.com", 80))
  606.         // {
  607.         // }
  608.         // else
  609.         // {
  610.            
  611.         //  foreach($kursdata as $kursd)
  612.         //  {
  613.         //      $currencyValue2 = $this->currencyConverter2($kursd->kurs,"IDR");
  614.                
  615.         //      $data2 = array(
  616.         //                  "kurs"  =>  $kursd->kurs,
  617.         //                  "nominal"   =>  $currencyValue2
  618.         //              );
  619.                        
  620.         //      $this->kurs_model->editModel($data2,$kursd->kurs_id);
  621.         //  }
  622.         // }
  623.        
  624.         $data['brands'] = $this->brand_model->getBrand();
  625.         $data['product'] = $this->purchase_model->getProduct();
  626.         $data['supplier'] = $this->purchase_model->getSupplier();
  627.         $data['data'] = $this->purchase_model->getRecord($id);
  628.         $data['discount'] = $this->purchase_model->getDiscount();
  629.         $data['tax'] = $this->purchase_model->getTax();
  630.         $data['kursdata'] = $kursdata;
  631.         $data['company'] = $this->company_setting_model->getData();
  632.         $data['termin'] = $this->purchase_model->getTerminId($data['data'][0]->purchase_id);
  633.        
  634.         foreach ($data['data'] as $key)
  635.         {
  636.             $data['items'] = $this->purchase_model->getPurchaseItems($id); 
  637.         }
  638.         $this->load->view('purchase/edit',$data);
  639.     }
  640.  
  641.     public function revisi($id)
  642.     {
  643.        
  644.         $data['biaya'] = $this->purchase_model->getBiaya($id);
  645.         $data['purchase_id'] = $id;
  646.        
  647.         $kursdata =  $this->company_setting_model->getCurrency();
  648.        
  649.         // if(!$sock= @fsockopen("www.google.com", 80))
  650.         // {
  651.         // }
  652.         // else
  653.         // {
  654.            
  655.         //  foreach($kursdata as $kursd)
  656.         //  {
  657.         //      $currencyValue2 = $this->currencyConverter2($kursd->kurs,"IDR");
  658.                
  659.         //      $data2 = array(
  660.         //                  "kurs"  =>  $kursd->kurs,
  661.         //                  "nominal"   =>  $currencyValue2
  662.         //              );
  663.                        
  664.         //      $this->kurs_model->editModel($data2,$kursd->kurs_id);
  665.         //  }
  666.         // }
  667.        
  668.         $data['brands'] = $this->brand_model->getBrand();
  669.         $data['product'] = $this->purchase_model->getProduct();
  670.         $data['supplier'] = $this->purchase_model->getSupplier();
  671.         $data['data'] = $this->purchase_model->getRecord($id);
  672.         $data['discount'] = $this->purchase_model->getDiscount();
  673.         $data['tax'] = $this->purchase_model->getTax();
  674.         $data['kursdata'] = $kursdata;
  675.         $data['company'] = $this->company_setting_model->getData();
  676.         $data['termin'] = $this->purchase_model->getTerminId($data['data'][0]->purchase_id);
  677.        
  678.         foreach ($data['data'] as $key)
  679.         {
  680.             $data['items'] = $this->purchase_model->getPurchaseItems($id); 
  681.         }
  682.         $this->load->view('purchase/revisi',$data);
  683.     }
  684.     /*
  685.         This function is used to delete discount record in databse
  686.     */
  687.     public function delete($id){
  688.         if($this->purchase_model->deleteModel($id)){
  689.             $log_data = array(
  690.                     'user_id'  => $this->session->userdata('user_id'),
  691.                     'table_id' => $id,
  692.                     'message'  => 'Purchase Deleted'
  693.                 );
  694.             $this->log_model->insert_log($log_data);
  695.             redirect('purchase','refresh');
  696.         }
  697.         else{
  698.             redirect('purchase','refresh');
  699.         }
  700.     }
  701.    
  702.    
  703.     /*
  704.         This function is to edit purchase record in database
  705.     */
  706.     public function editVoid()
  707.     {  
  708.             $id = $this->input->post('id');
  709.             $voiddesc = $this->input->post('voiddesc');
  710.        
  711.             $data = array(
  712.                     "voiddesc"            =>  $voiddesc,
  713.                     "status"      => 4
  714.                     );
  715.            
  716.    
  717.             if($this->purchase_model->editModel($id,$data))
  718.             {
  719.                 redirect('purchase');
  720.             }
  721.             else
  722.             {
  723.                 redirect('purchase');
  724.             }
  725.    
  726.     }
  727.  
  728.     public function editClosepo()
  729.     {  
  730.             $id = $this->input->post('id');
  731.             $closepo = $this->input->post('closepo');
  732.             $reference_no = $this->input->post('reference_no');
  733.        
  734.             $data = array(
  735.                     "reference_no"  => $reference_no,
  736.                     "closepo"       => $closepo,
  737.                     "status"        => 6
  738.                     );
  739.            
  740.    
  741.             if($id = $this->purchase_model->editModel($id,$data))
  742.             {
  743.                 $index = 0;
  744.  
  745.                 $data1 = array(
  746.                     "ledger_number" =>  $this->input->post('reference_no'),
  747.                     "ledger_id" =>  $this->input->post('id')
  748.                 );
  749.                 $index++;
  750.                 $this->purchase_model->addModelledger($data1);
  751.  
  752.                 redirect('purchase');
  753.             }
  754.             else
  755.             {
  756.                 redirect('purchase');
  757.             }
  758.    
  759.     }
  760.    
  761.     public function editShipping()
  762.     {  
  763.             $id = $this->input->post('id');
  764.             $shipstatus = $this->input->post('shipstatus');
  765.             $shipawb = $this->input->post('shipawb');
  766.             $shipdate = $this->input->post('shipdate');
  767.             $shiptrackno = $this->input->post('shiptrackno');
  768.             $shipcourier = $this->input->post('shipcourier');
  769.             $shipfreight = $this->input->post('shipfreight');
  770.             $shipfile = $this->input->post('shipfile');
  771.             $shipto = $this->input->post('shipto');
  772.        
  773.            
  774.             if($_FILES["shipfile"]["name"] == null)
  775.             {
  776.                 $url = $this->input->post('hidden_image');
  777.             }
  778.             else
  779.             {
  780.                
  781.                 $type = explode('.',$_FILES["shipfile"]["name"]);
  782.                 $type = $type[count($type)-1];
  783.                 $url = "./assets/shipping/".uniqid(rand()).'.'.$type;
  784.                        
  785.                 if(in_array($type,array("jpg","jpeg","gif","png","doc","docx","xls","xlsx")))
  786.                 {
  787.                     if(is_uploaded_file($_FILES["shipfile"]["tmp_name"]))
  788.                     {          
  789.                         if(move_uploaded_file($_FILES["shipfile"]["tmp_name"],$url))
  790.                         {  
  791.                             $url = base_url().''.$url; 
  792.                         }
  793.                     }  
  794.                 }
  795.                
  796.             }
  797.            
  798.             $data = array(
  799.                     "shipstatus"          =>  $shipstatus,
  800.                     "shipawb"             =>  $shipawb,
  801.                     "shipdate"            =>  $shipdate,
  802.                     "shiptrackno"         =>  $shiptrackno,
  803.                     "shipcourier"         =>  $shipcourier,
  804.                     "freight"             =>  $shipfreight,
  805.                     "shipfile"            =>  $url,
  806.                     "shipto"              =>  $shipto,
  807.                     "purchase_id"         =>  $id
  808.                     );
  809.            
  810.         if($idm = $this->purchase_model->addModelShipping($data))
  811.         {
  812.  
  813.             if ($shipstatus == 'Partial') {
  814.                     foreach($_POST['items_id_for_purchase'] as $i => $si_id){
  815.                     $product_qty = $_POST['items_qty_for_purchase'][$i];
  816.            
  817.                     $qty = $_POST['items_qty_for_purchase'][$i];           
  818.                     $idp = $_POST['product_id'][$i];
  819.                     $idi = $_POST['items_id_for_purchase'][$i];
  820.                    
  821.                     $this->db->query("UPDATE purchase_items SET quantityreal=IFNULL(quantityreal, 0 ) + $qty WHERE purchase_item_id=$idi");
  822.                    
  823.                     $data = array(
  824.                         'purchase_id' => $id,
  825.                         'product_id' => $idp,
  826.                         'purchase_shipping_id' => $idm,
  827.                         'quantity' => $qty,
  828.                         'status' => 2
  829.                     );
  830.  
  831.                     $this->db->insert('purchase_shipping_items', $data);
  832.                    
  833.                 }
  834.             } else {
  835.                 $querys = $this->db->select('*')->from('purchase_items')->where('purchase_id', $id)->get()->result();
  836.                
  837.                 foreach ($querys as $row) {
  838.                     $quantityreal = ($row->quantityreal == NULL) ? 0 : $row->quantityreal;
  839.                     $ids = $row->purchase_item_id;
  840.                     $qty = ($row->quantity - $quantityreal);
  841.                     $idp = $row->product_id;
  842.                     if ($row->quantityreal == NULL) {
  843.                         $quantityreal = 0;
  844.                     } else {
  845.                         $quantityreal = $row->quantityreal;
  846.                     }
  847.  
  848.                     $this->db->query("UPDATE purchase_items SET quantityreal=$quantityreal + $qty WHERE purchase_item_id=$ids");
  849.  
  850.                     $data = array(
  851.                         'purchase_id' => $id,
  852.                         'product_id' => $idp,
  853.                         'purchase_shipping_id' => $idm,
  854.                         'quantity' => $qty,
  855.                         'status' => 2
  856.                     );
  857.  
  858.                     $this->db->insert('purchase_shipping_items', $data);
  859.                 }
  860.  
  861.             }
  862.            
  863.            
  864.                    
  865.            
  866.             $data2 = array(
  867.                     "status"      => 2
  868.                     );
  869.            
  870.             if($this->purchase_model->editModelShipping($id,$data2))
  871.             {
  872.                 redirect('purchase');
  873.             }
  874.             else
  875.             {
  876.                 redirect('purchase');
  877.             }
  878.    
  879.         }
  880.         else
  881.         {
  882.             redirect('purchase');
  883.         }
  884.     }
  885.    
  886.     public function editClerance()
  887.     {  
  888.             $id = $this->input->post('id');
  889.        
  890.             $cleranceawb = $this->input->post('cleranceawb');
  891.             $clerancedate = $this->input->post('clerancedate');
  892.             $clerancetrackno = $this->input->post('clerancetrackno');
  893.             $clerancecourier = $this->input->post('clerancecourier');
  894.             $clerancefreight = $this->input->post('clerancefreight');
  895.             $clerancefile = $this->input->post('clerancefile');
  896.             $clerancepib = $this->input->post('clerancepib');
  897.             $dutytax = $this->input->post('dutytax');
  898.            
  899.             if($_FILES["clerancefile"]["name"] == null)
  900.             {
  901.                 $url = 'null';
  902.                 $type = "null";
  903.                
  904.             }
  905.             else
  906.             {
  907.                
  908.                 $type = explode('.',$_FILES["clerancefile"]["name"]);
  909.                 $type = $type[count($type)-1];
  910.                 $url = "./assets/clerance/".uniqid(rand()).'.'.$type;
  911.                        
  912.                 if(in_array($type,array("jpg","jpeg","gif","png","doc","docx","xls","xlsx")))
  913.                 {
  914.                     if(is_uploaded_file($_FILES["clerancefile"]["tmp_name"]))
  915.                     {          
  916.                         if(move_uploaded_file($_FILES["clerancefile"]["tmp_name"],$url))
  917.                         {  
  918.                             $url = base_url().''.$url; 
  919.                         }
  920.                     }  
  921.                 }
  922.                
  923.             }
  924.        
  925.             $data = array(
  926.                     "cleranceawb"             =>  $cleranceawb,
  927.                     "clerancedate"            =>  $clerancedate,
  928.                     "clerancetrackno"         =>  $clerancetrackno,
  929.                     "clerancecourier"             =>  $clerancecourier,
  930.                     "clerancefreight"             =>  $clerancefreight,
  931.                     "clerancefile"            =>  $url,
  932.                     "clerancepib"             =>  $clerancepib,
  933.                     "dutytax"             =>  $dutytax,
  934.                     "purchase_id"         => $id
  935.                     );
  936.            
  937.         if($idm=$this->purchase_model->addModelClearence($data))
  938.         {
  939.             $data2 = array(
  940.                     "status"      => 3
  941.                     );
  942.            
  943.    
  944.             if($this->purchase_model->editModelClearence($id,$data2))
  945.             {
  946.                 redirect('purchase');
  947.             }
  948.             else
  949.             {
  950.                 redirect('purchase');
  951.             }
  952.    
  953.         }
  954.         else
  955.         {
  956.             redirect('purchase');
  957.         }
  958.  
  959.     }
  960.    
  961.     public function editPurchase()
  962.     {
  963.         $id = $this->input->post('purchase_id');
  964.  
  965.         $submit = $this->input->post('submit');
  966.         if ($submit == "Update") {
  967.           $status = 0;
  968.         } else {
  969.           $status = 6;
  970.         }
  971.         $this->form_validation->set_rules('date','Date','trim|required');
  972.         $this->form_validation->set_rules('reference_no','Reference No','trim|required');
  973.         if($this->form_validation->run()==false)
  974.         {
  975.             $this->add();
  976.         }
  977.         else
  978.         {
  979.            
  980.             $data = array(
  981.                 "reference_no"  =>  $this->input->post('reference_no'),
  982.                 "date"          =>  $this->input->post('date'),
  983.                 "podate"        =>  $this->input->post('po_date'),
  984.                 "supplier_id"   =>  $this->input->post('supplier'),
  985.                 "upcc"          =>  $this->input->post('main'),
  986.                 "upcc2"         =>  $this->input->post('cc'),
  987.                 "taxdesc"       =>  $this->input->post('tax'),
  988.                 "kurs_id"       =>  $this->input->post('kursdata'),
  989.                 "refer_no"      =>  $this->input->post('refer_no'),
  990.                 "total"         =>  $this->input->post('grand_total'),
  991.                 "discount_value"=>  $this->input->post('total_discount'),
  992.                 "tax_value"     =>  $this->input->post('total_tax'),
  993.                 "pterm"             =>  $this->input->post('paymenterms'),
  994.                 "note"          =>  $this->input->post('note'),
  995.                 "user"          =>  $this->session->userdata('user_id'),
  996.                 "user_name"     => $this->session->userdata('identity'),
  997.                 "diskon"              =>  $this->input->post('diskon_text'),
  998.                 "diskonpersen"        => $this->input->post('diskonpersen_text'),
  999.                 "nominalcurrency"     => $this->input->post('grand_currency'),
  1000.                 "dateestimate"  => $this->input->post('dateestimate'),
  1001.                 "shipping_address" => $this->input->post('shipping_address'),
  1002.                 "billing_address" => $this->input->post('billing_address'),
  1003.                 "status" => $status
  1004.             );
  1005.            
  1006.             $invoice = array(
  1007.                 "invoice_no" => $this->purchase_model->generateInvoiceNo(),
  1008.                 "receipt_amount" => $this->input->post('grand_total'),
  1009.                 "receipt_voucher_date" => date('Y-m-d')
  1010.             );
  1011.            
  1012.             if($this->purchase_model->editModel($id,$data))
  1013.             {
  1014.                 $log_data = array(
  1015.                         'user_id'  => $this->session->userdata('user_id'),
  1016.                         'table_id' => $id,
  1017.                         'message'  => 'Purchase Updated'
  1018.                     );
  1019.  
  1020.                 $this->log_model->insert_log($log_data);
  1021.                
  1022.                
  1023.                 $jumlahtermin = $this->input->post('terminnumber');
  1024.        
  1025.                 if(!empty($jumlahtermin))
  1026.                 {
  1027.                     $this->db->where('purchase_id', $id);
  1028.                     $this->db->delete('purchase_termin');
  1029.                     $termins =  $this->input->post('terminvalue');
  1030.                     $tanggaltermins = $this->input->post('tanggalterminvalue');
  1031.            
  1032.                     if(!empty($termins))
  1033.                     {
  1034.                         $im=0;
  1035.                         foreach($termins as $quan)
  1036.                         {
  1037.                             $value=$this->input->post('grand_total');
  1038.                             $nominal=($value*$quan)/100;
  1039.                             $nominal = number_format((float)$nominal, 2, '.', '');
  1040.                            
  1041.                             if(!empty($tanggaltermins[$im]))
  1042.                             {
  1043.                                 $datetermin=$tanggaltermins[$im];
  1044.                             }
  1045.                             else
  1046.                             {
  1047.                                 $datetermin=date('Y-m-d');
  1048.                             }
  1049.                            
  1050.                             $datatermin = array(
  1051.                                     "purchase_id"         =>  $id,
  1052.                                     "persen"          =>  $quan,
  1053.                                     "nominal"         =>  $nominal,
  1054.                                     "tanggalbayar"    =>  $datetermin
  1055.                                    
  1056.                                 );
  1057.                            
  1058.                             $this->purchase_model->addTerminModel($datatermin);
  1059.                            
  1060.                             $im++;
  1061.                         }
  1062.                     }
  1063.                    
  1064.                 }
  1065.                 $this->db->where('purchase_id', $id);
  1066.                 $this->db->delete('purchase_items');
  1067.                 $js_data = json_decode($this->input->post('table_data'));
  1068.  
  1069.                 if(!empty($js_data))
  1070.                 {
  1071.                     foreach ($js_data as $key => $value)
  1072.                     {
  1073.                         if($value!=null  && !empty($value->product_id))
  1074.                         {
  1075.                             $product_id = $value->product_id;
  1076.                             $quantity = $value->quantity;
  1077.                             $data = array(
  1078.                                 "product_id" => $value->product_id,
  1079.                                 "quantity" => $value->quantity,
  1080.                                 "gross_total" => $value->total,
  1081.                                 "discount_id" => @$value->discount_id,
  1082.                                 "discount_value" => @$value->discount_value,
  1083.                                 "discount" => $value->discount,
  1084.                                 "cost" => @$value->cost,
  1085.                                 "price" => $value->price,
  1086.                                 "desc" => $value->desc,
  1087.                                 "purchase_id" => $id
  1088.                                 );
  1089.  
  1090.                             $warehouse_data = array(
  1091.                                 "product_id" => $value->product_id,
  1092.                                 "warehouse_id" => 0,
  1093.                                 "quantity" => $value->quantity
  1094.                                 );
  1095.                            
  1096.                             $this->db->insert('purchase_items', $data);
  1097.                        
  1098.                         }
  1099.                     }
  1100.                 }
  1101.  
  1102.                 $config = array();
  1103.                 $config['upload_path']   = './assets/doc/';
  1104.                 $config['allowed_types'] = 'doc|xls|xlsx|docx|pdf|DOC|XLS|XLSX|DOCX|PDF';
  1105.                 $config['max_size']      = '0';
  1106.                 $config['overwrite']     = FALSE;
  1107.                 $config['encrypt_name']  = TRUE;
  1108.  
  1109.                 $this->load->library('upload');
  1110.  
  1111.                 $count = count($this->input->post('jenis_biaya'));
  1112.                 // $count = 0;
  1113.  
  1114.                 $files = $_FILES;
  1115.                 for($i=0; $i< $count; $i++)
  1116.                 {          
  1117.                     if ($this->input->post('biaya_id')[$i] != NULL || !empty($this->input->post('biaya_id')[$i])) {
  1118.                         if($_FILES['file']['name'][$i]) {
  1119.                             $_FILES['file'.$i]['name']= $files['file']['name'][$i];
  1120.                             $_FILES['file'.$i]['type']= $files['file']['type'][$i];
  1121.                             $_FILES['file'.$i]['tmp_name']= $files['file']['tmp_name'][$i];
  1122.                             $_FILES['file'.$i]['error']= $files['file']['error'][$i];
  1123.                             $_FILES['file'.$i]['size']= $files['file']['size'][$i];
  1124.  
  1125.                             $this->upload->initialize($config);
  1126.  
  1127.                             $data = $this->db->select('*')->from('purchase_biaya')->where('id', $this->input->post('biaya_id')[$i])->get()->result()[0]->file_upload;
  1128.                             unlink(FCPATH . '/assets/doc/' . $data);
  1129.  
  1130.                             if ($this->upload->do_upload('file'.$i)) {
  1131.                                 $a = $this->upload->data();
  1132.                                 $filename = $a['file_name'];
  1133.                             }
  1134.  
  1135.                             $data = array(
  1136.                                 "jenis_biaya" =>  $this->input->post('jenis_biaya')[$i],
  1137.                                 "keterangan"   =>  $this->input->post('keterangan')[$i],
  1138.                                 "pdf"   =>  $this->input->post('pdf')[$i] ?: 0,
  1139.                                 "file_upload"   =>  $filename,
  1140.                                 "nominal"       =>  $this->input->post('nominal')[$i],
  1141.                             );
  1142.                             $this->db->where('id', $this->input->post('biaya_id')[$i]);
  1143.                             $this->db->update('purchase_biaya', $data);
  1144.                         } else {
  1145.                             $data = array(
  1146.                                 "jenis_biaya" =>  $this->input->post('jenis_biaya')[$i],
  1147.                                 "keterangan"   =>  $this->input->post('keterangan')[$i],
  1148.                                 "pdf"   =>  $this->input->post('pdf')[$i] ?: 0,
  1149.                                 "nominal"       =>  $this->input->post('nominal')[$i],
  1150.                             );
  1151.                             $this->db->where('id', $this->input->post('biaya_id')[$i]);
  1152.                             $this->db->update('purchase_biaya', $data);
  1153.                         }
  1154.                     } else {
  1155.                         if($_FILES['file']['name'][$i]) {
  1156.                             $_FILES['file'.$i]['name']= $files['file']['name'][$i];
  1157.                             $_FILES['file'.$i]['type']= $files['file']['type'][$i];
  1158.                             $_FILES['file'.$i]['tmp_name']= $files['file']['tmp_name'][$i];
  1159.                             $_FILES['file'.$i]['error']= $files['file']['error'][$i];
  1160.                             $_FILES['file'.$i]['size']= $files['file']['size'][$i];
  1161.  
  1162.                             $this->upload->initialize($config);
  1163.  
  1164.                             if ($this->upload->do_upload('file'.$i)) {
  1165.                                 $a = $this->upload->data();
  1166.                                 $filename = $a['file_name'];
  1167.                             }
  1168.                         } else {
  1169.                             $filename = '';
  1170.                         }
  1171.  
  1172.                         $data = array(
  1173.                             "jenis_biaya" =>  $this->input->post('jenis_biaya')[$i],
  1174.                             "keterangan"   =>  $this->input->post('keterangan')[$i],
  1175.                             "pdf"   =>  $this->input->post('pdf')[$i] ?: 0,
  1176.                             "file_upload"   =>  $filename,
  1177.                             "nominal"       =>  $this->input->post('nominal')[$i],
  1178.                             "purchase_id"   =>  $id
  1179.                         );
  1180.  
  1181.                         $this->db->insert('purchase_biaya', $data);
  1182.                     }
  1183.                 }
  1184.                    
  1185.            
  1186.                 redirect('purchase/view/' . $id);
  1187.             }
  1188.         }
  1189.     }
  1190.  
  1191.     public function revisiPurchase()
  1192.     {
  1193.         $id = $this->input->post('purchase_id');
  1194.                
  1195.         $submit = $this->input->post('submit');
  1196.         if ($submit == "Publish") {
  1197.           $status = 0;
  1198.         } else {
  1199.           $status = 4;
  1200.         }
  1201.         $this->form_validation->set_rules('date','Date','trim|required');
  1202.         $this->form_validation->set_rules('reference_no','Reference No','trim|required');
  1203.         if($this->form_validation->run()==false)
  1204.         {
  1205.             $this->add();
  1206.         }
  1207.         else
  1208.         {
  1209.            
  1210.             $warehouse_id = $this->input->post('warehouse');
  1211.             $data = array(
  1212.                         "reference_no"  =>  $this->input->post('reference_no'),
  1213.                         "norevisi"          =>  $this->input->post('norevisi'),
  1214.                         "date"          =>  $this->input->post('date'),
  1215.                         "podate"        =>  $this->input->post('po_date'),
  1216.                         "supplier_id"   =>  $this->input->post('supplier'),
  1217.                         "upcc"          =>  $this->input->post('main'),
  1218.                         "upcc2"         =>  $this->input->post('cc'),
  1219.                         "taxdesc"       =>  $this->input->post('tax'),
  1220.                         "kurs_id"       =>  $this->input->post('kursdata'),
  1221.                         "refer_no"      =>  $this->input->post('refer_no'),
  1222.                         "total"         =>  $this->input->post('grand_total'),
  1223.                         "discount_value"=>  $this->input->post('total_discount'),
  1224.                         "tax_value"     =>  $this->input->post('total_tax'),
  1225.                         "pterm"             =>  $this->input->post('paymenterms'),
  1226.                         "note"          =>  $this->input->post('note'),
  1227.                         "user"          =>  $this->session->userdata('user_id'),
  1228.                         "user_name"     => $this->session->userdata('identity'),
  1229.                         "diskon"              =>  $this->input->post('diskon_text'),
  1230.                         "diskonpersen"        => $this->input->post('diskonpersen_text'),
  1231.                         "nominalcurrency"     => $this->input->post('grand_currency'),
  1232.                         "dateestimate"  => $this->input->post('dateestimate'),
  1233.                         "shipping_address" => $this->input->post('shipping_address'),
  1234.                         "billing_address" => $this->input->post('billing_address'),
  1235.                         "status" => $status
  1236.                     );
  1237.  
  1238.            
  1239.             $invoice = array(
  1240.                 "invoice_no" => $this->purchase_model->generateInvoiceNo(),
  1241.                 "receipt_amount" => $this->input->post('grand_total'),
  1242.                 "receipt_voucher_date" => date('Y-m-d')
  1243.             );
  1244.            
  1245.             if($purchase_id = $this->purchase_model->addModel2($data,$invoice))
  1246.             {
  1247.                 $log_data = array(
  1248.                         'user_id'  => $this->session->userdata('user_id'),
  1249.                         'table_id' => $purchase_id,
  1250.                         'message'  => 'Purchase Inserted'
  1251.                     );
  1252.  
  1253.                 $this->log_model->insert_log($log_data);
  1254.                
  1255.                
  1256.                 $jumlahtermin= $this->input->post('terminnumber');
  1257.        
  1258.                 if(!empty($jumlahtermin))
  1259.                 {
  1260.                     $termins =  $this->input->post('terminvalue');
  1261.                     $tanggaltermins = $this->input->post('tanggalterminvalue');
  1262.            
  1263.                     if(!empty($termins))
  1264.                     {
  1265.                         $im=0;
  1266.                         foreach($termins as $quan)
  1267.                         {
  1268.                             $value=$this->input->post('grand_total');
  1269.                             $nominal=($value*$quan)/100;
  1270.                             $nominal = number_format((float)$nominal, 2, '.', '');
  1271.                            
  1272.                             if(!empty($tanggaltermins[$im]))
  1273.                             {
  1274.                                 $datetermin=$tanggaltermins[$im];
  1275.                             }
  1276.                             else
  1277.                             {
  1278.                                 $datetermin=date('Y-m-d');
  1279.                             }
  1280.                            
  1281.                             $datatermin = array(
  1282.                                     "purchase_id"         =>  $purchase_id,
  1283.                                     "persen"          =>  $quan,
  1284.                                     "nominal"         =>  $nominal,
  1285.                                     "tanggalbayar"    =>  $datetermin
  1286.                                    
  1287.                                 );
  1288.                            
  1289.                             $this->purchase_model->addTerminModel($datatermin);
  1290.                            
  1291.                             $im++;
  1292.                         }
  1293.                     }
  1294.                    
  1295.                 }
  1296.                
  1297.                 $js_data = json_decode($this->input->post('table_data'));
  1298.                
  1299.                 if(!empty($js_data))
  1300.                 {
  1301.                     foreach ($js_data as $key => $value)
  1302.                     {
  1303.                         if($value!=null  || !empty($value->product_id))
  1304.                         {
  1305.                             $product_id = $value->product_id;
  1306.                             $quantity = $value->quantity;
  1307.                             $data = array(
  1308.                                 "product_id" => $value->product_id,
  1309.                                 "quantity" => $value->quantity,
  1310.                                 "gross_total" => $value->total,
  1311.                                 "discount_id" => @$value->discount_id,
  1312.                                 "discount_value" => @$value->discount_value,
  1313.                                 "discount" => $value->discount,
  1314.                                 "cost" => $value->cost,
  1315.                                 "price" => $value->price,
  1316.                                 "desc" => $value->desc,
  1317.                                 "purchase_id" => $purchase_id
  1318.                                 );
  1319.  
  1320.                             $warehouse_data = array(
  1321.                                 "product_id" => $value->product_id,
  1322.                                 "warehouse_id" => 0,
  1323.                                 "quantity" => $value->quantity
  1324.                                 );
  1325.                            
  1326.                             $this->purchase_model->addProductInWarehouse($product_id,$quantity,$warehouse_id,$warehouse_data);
  1327.  
  1328.                             $this->db->insert('purchase_items', $data);
  1329.                        
  1330.                         }
  1331.                     }
  1332.                 }
  1333.                
  1334.                 $config = array();
  1335.                 $config['upload_path']   = './assets/doc/';
  1336.                 $config['allowed_types'] = 'doc|xls|xlsx|docx|pdf|DOC|XLS|XLSX|DOCX|PDF';
  1337.                 $config['max_size']      = '0';
  1338.                 $config['overwrite']     = FALSE;
  1339.                 $config['encrypt_name']  = TRUE;
  1340.  
  1341.                 $this->load->library('upload');
  1342.  
  1343.                 $count = count($this->input->post('jenis_biaya'));
  1344.  
  1345.                 $files = $_FILES;
  1346.                 for($i=0; $i< $count; $i++)
  1347.                 {          
  1348.                     if($_FILES['file']['name'][$i]) {
  1349.                         $_FILES['file'.$i]['name']= $files['file']['name'][$i];
  1350.                         $_FILES['file'.$i]['type']= $files['file']['type'][$i];
  1351.                         $_FILES['file'.$i]['tmp_name']= $files['file']['tmp_name'][$i];
  1352.                         $_FILES['file'.$i]['error']= $files['file']['error'][$i];
  1353.                         $_FILES['file'.$i]['size']= $files['file']['size'][$i];
  1354.  
  1355.                         $this->upload->initialize($config);
  1356.  
  1357.                         if ($this->upload->do_upload('file'.$i)) {
  1358.                             $a = $this->upload->data();
  1359.                             $filename = $a['file_name'];
  1360.                         } else {
  1361.                             $filename = '';
  1362.                         }
  1363.                     }
  1364.  
  1365.                     $data = array(
  1366.                         "jenis_biaya" =>  $this->input->post('jenis_biaya')[$i],
  1367.                         "keterangan"   =>  $this->input->post('keterangan')[$i],
  1368.                         "pdf"           =>  $this->input->post('pdf')[$i] ?: 0,
  1369.                         "file_upload"   =>  $filename,
  1370.                         "nominal"       =>  $this->input->post('nominal')[$i],
  1371.                         "purchase_id"   =>  $purchase_id
  1372.                     );
  1373.  
  1374.                     $this->db->insert('purchase_biaya', $data);
  1375.                 }
  1376.  
  1377.                  $dataman = array(
  1378.                     "status_rev" => 2
  1379.                  );
  1380.  
  1381.                  $this->db->where('id', $id);
  1382.                  $this->db->update('purchases', $data);
  1383.                    
  1384.            
  1385.                 redirect('purchase/view/' . $purchase_id);
  1386.             }
  1387.         }
  1388.     }
  1389.    
  1390.     /*
  1391.         view purchase details
  1392.     */
  1393.     public function view($id)
  1394.     {
  1395.         $data['data'] = $this->purchase_model->getDetails($id);
  1396.  
  1397.         $data['purchasetermins'] = $this->purchase_model->getTerminId($id);
  1398.        
  1399.         $data['items'] = $this->purchase_model->getItems($id);
  1400.         // print_r(var_dump($this->db->last_query()));
  1401.         $data['company'] = $this->purchase_model->getCompany();
  1402.         $data['symbol'] = $this->db->select('*')->from("currency")->where("kurs_id", $data['data'][0]->kurs_id)->get()->result()[0]->symbol;
  1403.  
  1404.  
  1405.  
  1406.         $this->load->view('purchase/view',$data);
  1407.     }
  1408.  
  1409.     public function ditolak($id)
  1410.     {
  1411.         $data['data'] = $this->purchase_model->getDetails($id);
  1412.         $data['purchasetermins'] = $this->purchase_model->getTerminId($id);
  1413.        
  1414.         $data['items'] = $this->purchase_model->getItems($id);
  1415.         $data['company'] = $this->purchase_model->getCompany();
  1416.  
  1417.         $this->load->view('purchase/ditolak',$data);
  1418.     }
  1419.  
  1420.     public function disetujui($id)
  1421.     {
  1422.         $data['data'] = $this->purchase_model->getDetails($id);
  1423.         $data['purchasetermins'] = $this->purchase_model->getTerminId($id);
  1424.        
  1425.         $data['items'] = $this->purchase_model->getItems($id);
  1426.         $data['company'] = $this->purchase_model->getCompany();
  1427.  
  1428.         $this->load->view('purchase/disetujui',$data);
  1429.     }
  1430.  
  1431.     public function changeSetujui() {
  1432.        $id = $this->input->post('purchase_id');
  1433.    
  1434.       $data = array(
  1435.             'status' => 2,
  1436.             'disetujuidesc' => $this->input->post('disetujui')
  1437.       );
  1438.  
  1439.       $update = $this->db->where('purchase_id', $id)->update('purchases', $data);
  1440.  
  1441.       if ($update) {
  1442.  
  1443.         $log_data = array(
  1444.             'user_id' => $this->session->userdata('user_id'),
  1445.             'table_id' => $id,
  1446.             'message' => 'Change Ditolak Purchase Updated'
  1447.         );
  1448.  
  1449.         $this->log_model->insert_log($log_data);
  1450.  
  1451.         $this->session->set_flashdata('success', 'Purchase Order Has Been Approved.');
  1452.         redirect('purchase', 'refresh');
  1453.     } else {
  1454.         $this->session->set_flashdata('fail', 'Purchase Order Can\'t Been Approved.');
  1455.         redirect('purchase', 'refresh');
  1456.     }
  1457.   }
  1458.  
  1459.   public function changeTolak() {
  1460.     $id = $this->input->post('purchase_id');
  1461.    
  1462.       $data = array(
  1463.             'status' => 3,
  1464.             'ditolakdesc' => $this->input->post('ditolak')
  1465.       );
  1466.  
  1467.       $update = $this->db->where('purchase_id', $id)->update('purchases', $data);
  1468.  
  1469.       if ($update) {
  1470.  
  1471.         $log_data = array(
  1472.             'user_id' => $this->session->userdata('user_id'),
  1473.             'table_id' => $id,
  1474.             'message' => 'Change Ditolak Purchase Updated'
  1475.         );
  1476.  
  1477.         $this->log_model->insert_log($log_data);
  1478.  
  1479.         $this->session->set_flashdata('success', 'Purchase Order Has Been Rejected.');
  1480.         redirect('purchase', 'refresh');
  1481.     } else {
  1482.         $this->session->set_flashdata('fail', 'Purchase Order Can\'t Reject.');
  1483.         redirect('purchase', 'refresh');
  1484.     }
  1485.   }
  1486.     /*
  1487.         generate pdf
  1488.     */
  1489.     public function pdf($id) {
  1490.  
  1491.     ob_start();
  1492.     $html = ob_get_clean();
  1493.     $html = utf8_encode($html);
  1494.  
  1495.     $data['data'] = $this->purchase_model->getDetails($id);
  1496.     $data['items'] = $this->purchase_model->getItems($id);
  1497.     $data['company'] = $this->purchase_model->getCompany();
  1498.     $data['company_setting'] = $this->company_setting_model->getData();
  1499.     $data['signature'] = $this->signature_model->getActiveSignature();
  1500.     $data['customer'] = $this->customer_model->getRecord($data['data'][0]->supplier_id);
  1501.     $data['supplier'] = $this->supplier_model->getRecord($data['data'][0]->supplier_id);
  1502.     $data['biaya'] = $this->purchase_model->getBiayaDoc($id);
  1503.     $data['symbol'] = $this->db->select('*')->from("currency")->where("kurs_id", $data['data'][0]->kurs_id)->get()->result()[0]->symbol;
  1504.  
  1505.     $t = 0;
  1506.     foreach ($data['items'] as $value) { $t += ($value->price * $value->quantity); }
  1507.     foreach ($data['biaya'] as $value) { $t += $value->nominal; }
  1508.  
  1509.     $r = $t - $data['data'][0]->discount_value + $data['data'][0]->tax_value;
  1510.     if ($r > $t) {
  1511.       $font = $r;
  1512.     } else {
  1513.       $font = $t;
  1514.     }
  1515.  
  1516.     if(strlen($font) <= 9) {
  1517.       $data['style'] = 'style="font-size: 9pt"';
  1518.       $data['style2'] = 'style="font-size: 8pt; font-style: italic; font-weight: bold;"';
  1519.     } elseif (strlen($font) == 10) {
  1520.       $data['style'] = 'style="font-size: 8pt"';
  1521.       $data['style2'] = 'style="font-size: 7pt; font-style: italic; font-weight: bold;"';
  1522.     } elseif (strlen($font) >= 11) {
  1523.       $data['style'] = 'style="font-size: 7pt"';
  1524.       $data['style2'] = 'style="font-size: 6pt; font-style: italic; font-weight: bold;"';
  1525.     }
  1526.    
  1527.     $html = $this->load->view('purchase/pdf', $data, true);
  1528.  
  1529.     $logo = base_url(header_logo('logo'));
  1530.     $width = header_logo('width');
  1531.     $height = header_logo('height');
  1532.    
  1533.     $mpdf = new \Mpdf\Mpdf();
  1534.     // $mpdf->SetHTMLHeader('<table style=" vertical-align: bottom; font-family: serif; font-size: 9pt;" width="100%">
  1535.     //   <tr>
  1536.     //     <td width="40%" valign="top"><img src="'. $logo .'" width="'. $width .'" height="'. $height .'"></td>
  1537.     //     <td style="text-align: right;" width="60%">
  1538.     //       '.header_doc() . '
  1539.          
  1540.     //     </td>
  1541.     //   </tr>
  1542.     // </table>');
  1543.  
  1544.     $mpdf->AddPageByArray([
  1545.         'margin-left' => 5,
  1546.         'margin-right' => 5,
  1547.         // 'margin-top' => 0,
  1548.         // 'margin-bottom' => 0,
  1549.     ]);
  1550.  
  1551.     if ($data['data'][0]->status == 4) {
  1552.       $status = 'Draft';
  1553.       $mpdf->SetWatermarkText($status);
  1554.       $mpdf->showWatermarkText = true;
  1555.     } else {
  1556.       $mpdf->SetWatermarkImage(base_url('/assets/images/logo-glodisaaa.jpg'));
  1557.       //disuruh hapus watermark
  1558.       $mpdf->showWatermarkImage = true;
  1559.     }
  1560.     $mpdf->allow_charset_conversion = true;
  1561.     $mpdf->charset_in = 'UTF-8';
  1562.     $mpdf->WriteHTML($html);
  1563.     $mpdf->Output($data['data'][0]->reference_no . 'pdf', 'I');
  1564.   }
  1565.  
  1566.   public function word($id) {
  1567.  
  1568.     ob_start();
  1569.     $html = ob_get_clean();
  1570.     $html = utf8_encode($html);
  1571.  
  1572.     $data['data'] = $this->purchase_model->getDetails($id);
  1573.     $data['items'] = $this->purchase_model->getItems($id);
  1574.     $data['company'] = $this->purchase_model->getCompany();
  1575.     $data['company_setting'] = $this->company_setting_model->getData();
  1576.     $data['signature'] = $this->signature_model->getActiveSignature();
  1577.     $data['supplier'] = $this->supplier_model->getRecord($data['data'][0]->supplier_id);
  1578.     $data['customer'] = $this->customer_model->getRecord($data['data'][0]->supplier_id);
  1579.     $data['symbol'] = $this->db->select('*')->from("currency")->where("kurs_id", $data['data'][0]->kurs_id)->get()->result()[0]->symbol;
  1580.     $data['biaya'] = $this->purchase_model->getBiayaDoc($id);
  1581.  
  1582.     $t = 0;
  1583.     foreach ($data['items'] as $value) { $t += ($value->price * $value->quantity); }
  1584.     foreach ($data['biaya'] as $value) { $t += $value->nominal; }
  1585.  
  1586.     $r = $t - $data['data'][0]->discount_value + $data['data'][0]->tax_value;
  1587.     if ($r > $t) {
  1588.       $font = $r;
  1589.     } else {
  1590.       $font = $t;
  1591.     }
  1592.  
  1593.     if(strlen($font) <= 9) {
  1594.       $data['style'] = 'style="font-size: 9pt"';
  1595.       $data['style2'] = 'style="font-size: 8pt; font-style: italic; font-weight: bold;"';
  1596.     } elseif (strlen($font) == 10) {
  1597.       $data['style'] = 'style="font-size: 8pt"';
  1598.       $data['style2'] = 'style="font-size: 7pt; font-style: italic; font-weight: bold;"';
  1599.     } elseif (strlen($font) >= 11) {
  1600.       $data['style'] = 'style="font-size: 7pt"';
  1601.       $data['style2'] = 'style="font-size: 6pt; font-style: italic; font-weight: bold;"';
  1602.     }
  1603.  
  1604.     $this->load->library('lmsword');
  1605.     $this->lmsword->page_setup('A4');
  1606.     $this->lmsword->html($this->load->view('purchase/pdf', $data, true));
  1607.     $this->lmsword->create("purchase_$id");
  1608.   }
  1609.     /*
  1610.         send email
  1611.     */
  1612.     public function email($id){
  1613.         $log_data = array(
  1614.                 'user_id'  => $this->session->userdata('user_id'),
  1615.                 'table_id' => 0,
  1616.                 'message'  => 'Purchase Receipt Email Send'
  1617.             );
  1618.         $this->log_model->insert_log($log_data);
  1619.         $data = $this->purchase_model->getSupplierEmail($id);
  1620.         $company = $this->purchase_model->getCompany();
  1621.         $email = $this->purchase_model->getSmtpSetup();
  1622.         $this->load->view('class.phpmailer.php');
  1623.  
  1624.         $mail = new PHPMailer();
  1625.  
  1626.         $mail->IsSMTP();
  1627.         $mail->Host = $email->smtp_host;
  1628.  
  1629.         $mail->SMTPAuth = true;
  1630.         //$mail->SMTPSecure = "ssl";
  1631.         $mail->Port = $email->port;
  1632.         $mail->Username = $email->smtp_username;
  1633.         $mail->Password = $email->smtp_password;
  1634.  
  1635.         $mail->From = $email->from_address;
  1636.         $mail->FromName = $email->form_name;
  1637.         $mail->AddAddress($data[0]->email);
  1638.         //$mail->AddReplyTo("mail@mail.com");
  1639.  
  1640.         $mail->IsHTML(true);
  1641.  
  1642.         $mail->Subject = "Purchase order No : ".$data[0]->reference_no." From ".$company[0]->name;
  1643.         $mail->Body = "Date : ".$data[0]->date."<br>Total : ".$data[0]->total;
  1644.         //$mail->AltBody = "This is the body in plain text for non-HTML mail clients";
  1645.  
  1646.         if(!$mail->Send())
  1647.         {
  1648.             $message =  "Email could not be sent";
  1649.         }
  1650.         else{
  1651.             $message =  "Email has been sent";
  1652.         }
  1653.         $this->session->set_flashdata('message', $message);
  1654.         redirect('purchase','refresh');
  1655.     }
  1656.     /*
  1657.         view payment
  1658.     */
  1659.     public function payment($id){
  1660.         $data['data'] = $this->purchase_model->getDetails($id);
  1661.         $data['items'] = $this->purchase_model->getItems($id);
  1662.         $data['company'] = $this->purchase_model->getCompany();
  1663.         $data['ledger'] = $this->purchase_model->getLedger();
  1664.         $data['p_reference_no'] = $this->purchase_model->generateReferenceNo();
  1665.         $this->load->view('purchase/payment',$data);
  1666.     }
  1667.     /*
  1668.         add payment
  1669.     */
  1670.     public function paymentAdd($id)
  1671.     {
  1672.         $data['data'] = $this->purchase_model->getDetails($id);
  1673.         $data['items'] = $this->purchase_model->getItems($id);
  1674.         $data['company'] = $this->purchase_model->getCompany();
  1675.         $data['p_reference_no'] = $this->purchase_model->generateReferenceNo();
  1676.         $this->load->view('purchase/payment/add',$data);
  1677.     }
  1678.     /*
  1679.         get Discount value for AJAX
  1680.     */
  1681.     public function getDiscountValue($id){
  1682.         $data = $this->purchase_model->getDiscountValue($id);
  1683.         echo json_encode($data);
  1684.     }
  1685.     /*
  1686.         get Tax value for AJAX
  1687.     */
  1688.     public function getTaxValue($id){
  1689.         $data = $this->purchase_model->getTaxValue($id);
  1690.         echo json_encode($data);
  1691.     }
  1692.     /*
  1693.         get payment details to view and send to model
  1694.     */
  1695.     public function addPayment()
  1696.     {
  1697.         $id = $this->input->post('id');
  1698.         $paying_by = $this->input->post('paying_by');
  1699.         $this->form_validation->set_rules('date','Date','trim|required');
  1700.         $this->form_validation->set_rules('paying_by','Paying By','trim|required');
  1701.         if($paying_by == "Cheque"){
  1702.             $this->form_validation->set_rules('bank_name','Bank Name','trim|required');
  1703.             $this->form_validation->set_rules('cheque_no','Cheque No','trim|required|numeric');
  1704.         }
  1705.         if($this->form_validation->run()==false){
  1706.             $this->payment($id);
  1707.         }
  1708.         else
  1709.         {
  1710.             if($paying_by == "Cheque"){
  1711.                 $bank_name = $this->input->post('bank_name');
  1712.                 $cheque_no = $this->input->post('cheque_no');
  1713.             }
  1714.             else{
  1715.                 $bank_name = "";
  1716.                 $cheque_no = "";
  1717.             }
  1718.             $data = array(
  1719.                     "purchase_id"     => $id,
  1720.                     "payment_voucher_date"         => $this->input->post('date'),
  1721.                     "invoice_no" => $this->input->post('reference_no'),
  1722.                     "payment_ledger" => $this->input->post('ledger'),
  1723.                     "payment_amount"       => $this->input->post('amount'),
  1724.                     "mode_of_payment"    => $this->input->post('paying_by'),
  1725.                     "bank_name"    => $bank_name,
  1726.                     "cheque_no"    => $cheque_no,
  1727.                     "description"  => $this->input->post('note')
  1728.                 );
  1729.  
  1730.             if($this->purchase_model->addPayment($data)){
  1731.                 $log_data = array(
  1732.                         'user_id'  => $this->session->userdata('user_id'),
  1733.                         'table_id' => $id,
  1734.                         'message'  => 'Purchase Payable'
  1735.                     );
  1736.                 $this->log_model->insert_log($log_data);
  1737.                 redirect('purchase','refresh');
  1738.             }
  1739.             else{
  1740.                 redirect("purchase",'refresh');
  1741.             }
  1742.         }
  1743.     }
  1744.    
  1745.     function currencyConverter2($currency_from,$currency_to)
  1746.     {
  1747.         if($currency_from==$currency_to)
  1748.         {
  1749.              return 1;
  1750.         }
  1751.         else
  1752.         {
  1753.          //$currency_output = round(currency($currency_input, $currency_from,$currency_to, false),2);
  1754.          //$currency_input=substr($currency_input, 0, -3);
  1755.          $currency_input=filter_var(1, FILTER_SANITIZE_NUMBER_INT);
  1756.          $from_Currency = urlencode($currency_from);
  1757.          $to_Currency = urlencode($currency_to);
  1758.          $get = @file_get_contents("https://finance.google.com/finance/converter?a=1&from=$from_Currency&to=$to_Currency");
  1759.          $get = @explode("<span class=bld>",$get);
  1760.          $get = @explode("</span>",@$get[1]);
  1761.          $converted_currency = preg_replace("/[^0-9\.]/", null, @$get[0]);
  1762.          $converted_currency = $converted_currency;
  1763.          return $converted_currency;
  1764.          
  1765.          //return $currency_output;
  1766.         }
  1767.     }
  1768.  
  1769.      public function getCurrencyAjax($id) {
  1770.     $data = $this->purchase_model->getCurrencyId($id);
  1771.     echo json_encode($data);
  1772.     //print_r($data);
  1773.   }
  1774. }
  1775. ?>
Add Comment
Please, Sign In to add comment