fadlyshafa

Untitled

Jun 19th, 2020
100
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. defined('BASEPATH') OR exit('No direct script access allowed');
  4.  
  5. require_once APPPATH . '/../vendor/autoload.php';
  6.  
  7. class Sales extends CI_Controller {
  8.  
  9.   function __construct() {
  10.     parent::__construct();
  11.     $this->load->model('sales_model');
  12.     $this->load->model('quotation_model');
  13.     $this->load->model('customer_model');
  14.     $this->load->model('category_model');
  15.     $this->load->model('brand_model');
  16.     $this->load->model('bank_model');
  17.     $this->load->model('purchase_model');
  18.     $this->load->model('company_setting_model');
  19.     $this->load->model('kurs_model');
  20.     $this->load->model('signature_model');
  21.     $this->load->model('general_model');
  22.     $this->load->model('log_model');
  23.   }
  24.  
  25.   public function randomKey($length) {
  26.     $key = '';
  27.     $pool = array_merge(range(0, 9), range('a', 'z'), range('A', 'Z'));
  28.     for ($i = 0; $i < $length; $i++) {
  29.       $key .= $pool[mt_rand(0, count($pool) - 1)];
  30.     }
  31.     return $key;
  32.   }
  33.  
  34.   public function alert($msg) {
  35.     echo "<script type='text/javascript'>system.log('$msg');</script>";
  36.   }
  37.  
  38.   public function index() {
  39.     // get all sales to display list
  40.     $filter = null;
  41.     if(isset($_GET['filter'])) {
  42.       if ($_GET['filter'] != null) {
  43.         $filter = $_GET['filter'];
  44.       }
  45.     }
  46.    
  47.     $data['quotation'] = $this->quotation_model->getQuotation();
  48.     $data['data'] = $this->sales_model->getSales($filter);
  49.     $this->load->view('sales/list', $data);
  50.   }
  51.  
  52.   /*
  53.     call add view to add sales record
  54.    */
  55.  
  56.   public function add() {
  57.     $kursdata = $this->company_setting_model->getCurrency();
  58.  
  59.     $data['country'] = $this->db->get('countries')->result();
  60.     $data['brands'] = $this->brand_model->getBrand();
  61.     $data['warehouse'] = $this->sales_model->getWarehouse();
  62.     $data['quotation'] = $this->quotation_model->getQuotation();
  63.     $data['warehouse_products'] = $this->sales_model->getWarehouseProducts();
  64.     $data['tax'] = $this->sales_model->getTax();
  65.     $data['biller'] = $this->sales_model->getBiller();
  66.     $data['customer'] = $this->sales_model->getCustomer();
  67.     $data['quatation'] = $this->sales_model->getQuatation();
  68.     $data['product'] = $this->sales_model->getProducts2();
  69.     $data['service'] = $this->sales_model->getService();
  70.     $data['discount'] = $this->sales_model->getDiscount();
  71.     $data['kursdata'] = $kursdata;
  72.     $data['reference_no'] = $this->sales_model->createReferenceNo();
  73.     $data['so_number'] = $this->general_model->generateAutoNumber('sales_order');
  74.     $this->load->view('sales/add', $data);
  75.   }
  76.  
  77.   /*
  78.     this function is used to get discount data when discount is change
  79.    */
  80.  
  81.   public function getDiscountAjax($id) {
  82.     $data = $this->sales_model->getDiscountAjax($id);
  83.     echo json_encode($data);
  84.   }
  85.  
  86.   /* get all product warehouse wise */
  87.  
  88.   public function getProducts($warehouse_id) {
  89.     $data = $this->sales_model->getProducts($warehouse_id);
  90.     echo json_encode($data);
  91.   }
  92.  
  93.   public function getPartNumber($brand_id) {
  94.     $data = $this->sales_model->getPartNumber($brand_id);
  95.     echo json_encode($data);
  96.   }
  97.  
  98.   public function getCodeProduct($code) {
  99.     $data = $this->sales_model->getCodeProduct($code);
  100.     echo json_encode($data);
  101.   }
  102.  
  103.   /* get single product */
  104.  
  105.   public function getProduct($product_id,$sales_id) {
  106.     $datas = $this->sales_model->getProducts21($product_id);
  107.     // print_r(var_dump($this->db->last_query()));
  108.     for ($i=0; $i < count($datas) ; $i++) {
  109.  
  110.       if ($datas[$i]->product_grouping != NULL) {
  111.          $arlist = json_encode(unserialize($datas[$i]->list));
  112.          $arlist = json_decode($arlist);
  113.          $max = array();
  114.             for ($ilist = 0; $ilist < count($arlist); $ilist++) {
  115.  
  116.                 $qtyp = $this->db->select('quantity')->from('products')->where('product_id', $arlist[$ilist]->pl)->get()->result()[0]->quantity;
  117.                 $count = ($qtyp / intval($arlist[$ilist]->ql));
  118.                 array_push($max, $count);
  119.             }
  120.         $min = min($max);
  121.         $datas[$i]->quantity = intval($min);
  122.       }
  123.     }
  124.  
  125.     $data['items'] = $this->sales_model->getProducts99($sales_id);
  126.  
  127.     $data['discount'] = $this->sales_model->getDiscount();
  128.     $data['bonus'] = $this->sales_model->getBonus($product_id);
  129.     $data['bonus1'] = $this->sales_model->getBonus12();
  130.     $data['tax'] = $this->sales_model->getTax();
  131.     echo json_encode($data);
  132.   }
  133.  
  134.   public function getProduct_old($product_id) {
  135.     $data = $this->sales_model->getProducts21($product_id);
  136.     // print_r(var_dump($this->db->last_query()));
  137.     for ($i=0; $i < count($data) ; $i++) {
  138.  
  139.       if ($data[$i]->product_grouping != NULL) {
  140.          $arlist = json_encode(unserialize($data[$i]->list));
  141.          $arlist = json_decode($arlist);
  142.          $max = array();
  143.             for ($ilist = 0; $ilist < count($arlist); $ilist++) {
  144.  
  145.                 $qtyp = $this->db->select('quantity')->from('products')->where('product_id', $arlist[$ilist]->pl)->get()->result()[0]->quantity;
  146.                 $count = ($qtyp / intval($arlist[$ilist]->ql));
  147.                 array_push($max, $count);
  148.             }
  149.         $min = min($max);
  150.         $data[$i]->quantity = intval($min);
  151.       }
  152.     }
  153.  
  154.     // $data['items'] = $this->sales_model->getProducts99($sales_id);
  155.  
  156.     $data['discount'] = $this->sales_model->getDiscount();
  157.     $data['bonus'] = $this->sales_model->getBonus($product_id);
  158.     $data['bonus1'] = $this->sales_model->getBonus12();
  159.     $data['tax'] = $this->sales_model->getTax();
  160.     echo json_encode($data);
  161.   }
  162.  
  163.   public function getService($service_id) {
  164.     $data = $this->sales_model->getServicetable($service_id);
  165.     $data['discount'] = $this->sales_model->getDiscount();
  166.     $data['tax'] = $this->sales_model->getTax();
  167.     echo json_encode($data);
  168.   }
  169.  
  170.   /*
  171.     this function is used to search product name / code in auto complite
  172.    */
  173.  
  174.   public function getAutoCodeName($code, $search_option, $warehouse) {
  175.     //$code = strtolower($code);
  176.     $p_code = $this->input->post('p_code');
  177.     $p_search_option = $this->input->post('p_search_option');
  178.     $data = $this->sales_model->getProductCodeName($p_code, $p_search_option, $warehouse);
  179.     if ($search_option == "Code") {
  180.       $list = "<ul class='auto-product'>";
  181.       foreach ($data as $val) {
  182.         $list .= "<li value=" . $val->code . ">" . $val->code . "</li>";
  183.       }
  184.       $list .= "</ul>";
  185.     } else {
  186.       $list = "<ul class='auto-product'>";
  187.       foreach ($data as $val) {
  188.         $list .= "<li value=" . $val->product_id . ">" . $val->name . "</li>";
  189.       }
  190.       $list .= "</ul>";
  191.     }
  192.  
  193.     echo $list;
  194.     //echo json_encode($data);
  195.     //print_r($data);
  196.   }
  197.  
  198.   /*
  199.     this fucntion is used to add sales record in database
  200.    */
  201.  
  202.   public function addSales() {
  203.     date_default_timezone_set("Asia/Bangkok");
  204.  
  205.     $submit = $this->input->post('submit');
  206.     if ($submit == "Publish") {
  207.       $status = 0;
  208.     } else {
  209.       $status = 4;
  210.     }
  211.  
  212.     $this->form_validation->set_rules('customer', 'Customer', 'trim|required');
  213.     if ($this->form_validation->run() == false) {
  214.       $this->add();
  215.     } else {
  216.       $data = array(
  217.           "po" => $this->input->post('so'),
  218.           "po_cust" => $this->input->post('po_cust') ?: "",
  219.           "cc_holder" => $this->input->post('taxx'),
  220.           "currentdate" => $this->input->post('sales_date'),
  221.           "date" => $this->input->post('cust_date'),
  222.           "reference_no" => serialize($this->input->post('reference_no')),
  223.           "ketnama" => $this->input->post('reference'),
  224.           "warehouse_id" => "",
  225.           "biller_id" => $this->input->post('biller_id'),
  226.           "customer_id" => $this->input->post('customer'),
  227.           "upcc" => $this->input->post('main'),
  228.           "upcc2" => $this->input->post('cc'),
  229.           "total" => $this->input->post('grand_total'),
  230.           "discount_value" => $this->input->post('total_discount'),
  231.           "tax_value" => $this->input->post('total_tax'),
  232.           "note" => $this->input->post('note'),
  233.           "shipping_city_id" => $this->input->post('city'),
  234.           "shipping_state_id" => $this->input->post('state'),
  235.           "shipping_country_id" => $this->input->post('country'),
  236.           "shipping_address" => $this->input->post('address'),
  237.           "shipping_charge" => $this->input->post('shipping_charge'),
  238.           "internal_note" => $this->input->post('internal_note'),
  239.           "mode_of_transport" => $this->input->post('mode_of_transport'),
  240.           "transporter_name" => $this->input->post('transporter_name'),
  241.           "transporter_code" => $this->input->post('transporter_code'),
  242.           "vehicle_regn_no" => $this->input->post('vehicle_regn_no'),
  243.           "l_r_no" => $this->input->post('l_r_no'),
  244.           "chalan_no" => $this->input->post('chalan_no'),
  245.           "indent_no" => $this->input->post('indent_no'),
  246.           "credit_days" => $this->input->post('credit_days'),
  247.           "date_of_supply" => $this->input->post('date_of_supply'),
  248.           "electronic_ref_no" => $this->input->post('electronic_ref_no'),
  249.           "gst_payable" => $this->input->post('gst_payable'),
  250.           "broker" => $this->input->post('broker'),
  251.           "user" => $this->session->userdata('user_id'),
  252.           "term_and_condition" => $this->input->post('term_and_condition'),
  253.           "user_name" => $this->session->userdata('identity'),
  254.           "kurs_id" => $this->input->post('kursdata'),
  255.           "nominalcurrency" => $this->input->post('grand_currency'),
  256.           "ATTN" => $this->input->post('tax'),
  257.           "shipto" => $this->input->post('shipto'),
  258.           "billto" => $this->input->post('billto'),
  259.           "dateestimate" => $this->input->post('dateestimate'),
  260.           "salesdesc" => $this->input->post('others'),
  261.           "paid_by" => $this->input->post('paymenterms') ?: 'cash',
  262.           "diskonpersen" => $this->input->post('DiscountPersen'),
  263.           "service_description" => $this->input->post('service_description'),
  264.           "additional_description" => $this->input->post('additional_description'),
  265.           "status_rev" => $status,
  266.           "diskon" => $this->input->post('diskonfix')
  267.       );
  268.  
  269.       $invoice = array(
  270.           "invoice_no" => $this->sales_model->generateInvoiceNo(),
  271.           "sales_amount" => $this->input->post('grand_total'),
  272.           "invoice_date" => date('Y-m-d')
  273.       );
  274.  
  275.       if ($sales_id = $this->sales_model->addModel($data, $invoice)) {
  276.         $log_data = array(
  277.             'user_id' => $this->session->userdata('user_id'),
  278.             'table_id' => $sales_id,
  279.             'message' => 'Sales Inserted'
  280.         );
  281.         $this->log_model->insert_log($log_data);
  282.         $jumlahtermin = $this->input->post('terminnumber');
  283.  
  284.  
  285.         if (!empty($jumlahtermin)) {
  286.           $termins = $this->input->post('terminvalue');
  287.           $ket = $this->input->post('terminket');
  288.           $tanggaltermins = $this->input->post('tanggalterminvalue');
  289.           $i = 1;
  290.           if (!empty($termins)) {
  291.             $im = 0;
  292.             $termino = 0;
  293.             foreach ($termins as $quan) {
  294.               $value = $this->input->post('grand_total');
  295.               $nominal = ($value * $quan) / 100;
  296.               $nominal = number_format((float) $nominal, 2, '.', '');
  297.               if (!empty($tanggaltermins[$im])) {
  298.                 $datetermin = $tanggaltermins[$im];
  299.               } else {
  300.                 $datetermin = date('Y-m-d');
  301.               }
  302.               $termino++;
  303.               $datatermin = array(
  304.                   "sales_id" => $sales_id,
  305.                   "persen" => $quan,
  306.                   "nominal" => $nominal,
  307.                   "tanggalbayar" => $datetermin,
  308.                   "keterangan" => $ket[$i]
  309.               );
  310.  
  311.               $this->sales_model->addTerminModel($datatermin);
  312.               $i++;
  313.               $im++;
  314.             }
  315.           } else {
  316.            
  317.           }
  318.         }
  319.  
  320.  
  321.         $js_data = json_decode($this->input->post('table_data'));
  322.         $js_data1 = json_decode($this->input->post('tableadditional_data'));
  323.         $js_data2 = json_decode($this->input->post('tableservice_data'));
  324.         $js_data3 = json_decode($this->input->post('tableitemrequest'));
  325.  
  326.         // if ($js_data) {
  327.         //   foreach ($js_data as $key => $value) {
  328.         //     if ($value != null || !empty($value->product_id)) {
  329.  
  330.         //       $data = array(
  331.         //           "product_id" => $value->product_id,
  332.         //           "quantity" => $value->quantity,
  333.         //           "price" => $value->price,
  334.         //           "gross_total" => $value->total,
  335.         //           "discount_id" => @$value->discount_id,
  336.         //           "discount_value" => @$value->discount_value,
  337.         //           "discount" => $value->discount,
  338.         //           "serial_no" => $value->desc,
  339.         //           "sales_id" => $sales_id
  340.         //       );
  341.  
  342.         //       $this->db->insert('sales_items' ,$data);
  343.         //     }
  344.         //   }
  345.         // }
  346.  
  347.         $product_id = $this->input->post('product_id');
  348.  
  349.         foreach ($product_id as $e=>$pd) {
  350.             $quantity = $this->input->post('qty'.$e);
  351.             $price = $this->input->post('price'.$e);
  352.             $gross_total = $this->input->post('subtotal'.$e);
  353.             $discount_id = $this->input->post('discount_id'.$e);
  354.             $discount_value = $this->input->post('discount_value'.$e);
  355.             $discount = $this->input->post('discount'.$e);
  356.             $serial_no = $this->input->post('serial_no'.$e);
  357.             $sales_id = $sales_id;
  358.  
  359.             $lines['quantity'] = $quantity;
  360.             $lines['price'] = $price;
  361.             $lines['gross_total'] = $gross_total;
  362.             $lines['discount_id'] = $discount_id;
  363.             $lines['discount_value'] = $discount_value;
  364.             $lines['discount'] = $discount;
  365.             $lines['serial_no'] = $serial_no;
  366.             $lines['sales_id'] = $sales_id;
  367.  
  368.             $this->db->insert('sales_items' ,$lines);
  369.         }
  370.  
  371.         if ($js_data1) {
  372.          
  373.           foreach ($js_data1 as $key => $value) {
  374.             if ($value != null || !empty($value->additional_id)) {
  375.  
  376.               $data = array(
  377.                   "product_id" => $value->additional_id,
  378.                   "quantity" => $value->quantity,
  379.                   "price" => $value->price,
  380.                   "gross_total" => $value->total,
  381.                   "discount_id" => @$value->discount_id,
  382.                   "discount_value" => @$value->discount_value,
  383.                   "discount" => $value->discount,
  384.                   "desc" => $value->desc,
  385.                   "sales_id" => $sales_id
  386.               );
  387.              
  388.               $this->db->insert('sales_additional_items' ,$data);
  389.             }
  390.           }
  391.         }
  392.  
  393.         if ($js_data2) {
  394.          
  395.           foreach ($js_data2 as $key => $value) {
  396.             if ($value != null || !empty($value->service_id)) {
  397.              
  398.                $data = array(
  399.                   "service_id" => $value->service_id,
  400.                   "quantity" => @$value->quantity?:0,
  401.                   "price" => @$value->price?:0,
  402.                   "gross_total" => $value->total,
  403.                   "discount_id" => @$value->discount_id,
  404.                   "discount_value" => @$value->discount_value,
  405.                   "discount" => $value->discount,
  406.                   "sales_id" => $sales_id
  407.               );
  408.              
  409.               $this->sales_model->addSalesServiceItem($data);
  410.  
  411.             }
  412.           }
  413.         }
  414.  
  415.         if ($js_data3) {
  416.           var_dump($js_data3);
  417.           foreach ($js_data3 as $value) {
  418.           var_dump($value->request_date);
  419.              
  420.             //random name item req
  421.             $digits = 4;
  422.             $rand = str_pad(rand(0, pow(10, $digits)-1), $digits, '0', STR_PAD_LEFT);
  423.             $number = date("dm").$rand;
  424.             $cek = $this->db->select('*')->from('item_request')->where('sales_id', $sales_id)->get()->result();
  425.             var_dump($cek);
  426.             if (empty($cek)) {
  427.               $deadline = date('Y-m-d', strtotime('14 day', strtotime(date($value->request_date))));
  428.  
  429.               $params = array(
  430.                 'request_number' => $number,
  431.                 'request_date' => $value->request_date,
  432.                 'request_deadline' => $deadline,
  433.                 'reference' => $value->reference,
  434.                 'sales_id' => $sales_id,
  435.                 'request_for' => 'Sales'        
  436.               );
  437.  
  438.               $this->db->insert('item_request', $params);
  439.               $id = $this->db->insert_id();
  440.             } else {
  441.               $id = $cek[0]->id_item_request;
  442.             }
  443.  
  444.             $params = array(
  445.               'item_request_id' => $id,
  446.               'product_id' => $value->id_product,
  447.               'price' => $value->price,
  448.               'qty' => $value->qty
  449.             );
  450.  
  451.  
  452.            
  453.             $this->db->insert('item_request_items', $params);
  454.  
  455.             }
  456.  
  457.         }
  458.        
  459.  
  460.         redirect('sales/view/' . $sales_id);
  461.       } else {
  462.         redirect('sales', 'refresh');
  463.       }
  464.     }
  465.   }
  466.  
  467.   public function revisiSales() {
  468.     $id = $this->input->post('sales_id');
  469.     $status_old = $this->input->post('status_old');
  470.     date_default_timezone_set("Asia/Bangkok");
  471.  
  472.     $this->form_validation->set_rules('customer', 'Customer', 'trim|required');
  473.  
  474.     $submit = $this->input->post('submit');
  475.     if ($submit == "Publish") {
  476.       $status = 1;
  477.     } else {
  478.       $status = 4;
  479.     }
  480.     if ($this->form_validation->run() == false) {
  481.       $this->revisi($id);
  482.     } else {
  483.       $data = array(
  484.           "po" => $this->input->post('po'),
  485.           "cc_holder" => $this->input->post('taxx'),
  486.           "po_cust" => $this->input->post('po_cust'),
  487.           "po_new" => $this->input->post('po_new'),
  488.           "currentdate" => $this->input->post('salesdate'),
  489.           "date" => $this->input->post('cust_date'),
  490.           "ketnama" => $this->input->post('reference'),
  491.           "reference_no" => serialize($this->input->post('reference_no')),
  492.           "warehouse_id" => "",
  493.           "customer_id" => $this->input->post('customer'),
  494.           "upcc" => $this->input->post('main'),
  495.           "upcc2" => $this->input->post('cc'),
  496.           "total" => $this->input->post('grand_total'),
  497.           "discount_value" => $this->input->post('total_discount'),
  498.           "tax_value" => $this->input->post('total_tax'),
  499.           "note" => $this->input->post('note'),
  500.           "shipping_city_id" => $this->input->post('city'),
  501.           "shipping_state_id" => $this->input->post('state'),
  502.           "shipping_country_id" => $this->input->post('country'),
  503.           "shipping_address" => $this->input->post('address'),
  504.           "shipping_charge" => $this->input->post('shipping_charge'),
  505.           "internal_note" => $this->input->post('internal_note'),
  506.           "mode_of_transport" => $this->input->post('mode_of_transport'),
  507.           "transporter_name" => $this->input->post('transporter_name'),
  508.           "transporter_code" => $this->input->post('transporter_code'),
  509.           "vehicle_regn_no" => $this->input->post('vehicle_regn_no'),
  510.           "l_r_no" => $this->input->post('l_r_no'),
  511.           "chalan_no" => $this->input->post('chalan_no'),
  512.           "indent_no" => $this->input->post('indent_no'),
  513.           "credit_days" => $this->input->post('credit_days'),
  514.           "date_of_supply" => $this->input->post('date_of_supply'),
  515.           "electronic_ref_no" => $this->input->post('electronic_ref_no'),
  516.           "gst_payable" => $this->input->post('gst_payable'),
  517.           "broker" => $this->input->post('broker'),
  518.           "user" => $this->session->userdata('user_id'),
  519.           "term_and_condition" => $this->input->post('term_and_condition'),
  520.           "user_name" => $this->session->userdata('identity'),
  521.           "kurs_id" => $this->input->post('kursdata'),
  522.           "nominalcurrency" => $this->input->post('grand_currency'),
  523.           "ATTN" => $this->input->post('tax'),
  524.           "shipto" => $this->input->post('shipto'),
  525.           "billto" => $this->input->post('billto'),
  526.           "dateestimate" => $this->input->post('dateestimate'),
  527.           "salesdesc" => $this->input->post('others'),
  528.           "paid_by" => $this->input->post('paymenterms') ?: 'advance',
  529.           "additional_description" => $this->input->post('additional_description'),
  530.           "service_description" => $this->input->post('service_description'),
  531.           "diskonpersen" => $this->input->post('DiscountPersen'),
  532.           "status_rev" => $status,
  533.           "diskon" => $this->input->post('diskonfix')
  534.       );
  535.  
  536.       $invoice = array(
  537.           "invoice_no" => $this->sales_model->generateInvoiceNo(),
  538.           "sales_amount" => $this->input->post('grand_total'),
  539.           "invoice_date" => date('Y-m-d')
  540.       );
  541.  
  542.       if ($sales_id = $this->sales_model->addModel($data, $invoice)) {
  543.  
  544.         if ($status_old == 2) {
  545.          
  546.           $product_items = $this->db->select('*')
  547.                           ->from('sales_items')
  548.                           ->where('sales_id', $id)
  549.                           ->get()
  550.                           ->result();
  551.  
  552.           foreach ($product_items as $row) {
  553.             $idp = $this->db->select('*')
  554.                     ->from('product_grouping')
  555.                     ->where('product_id', $row->product_id)
  556.                     ->get()
  557.                     ->result();
  558.  
  559.             if ($idp) {
  560.               $arlist = json_encode(unserialize($idp[0]->list));
  561.               $arlist = json_decode($arlist);
  562.  
  563.               for ($ilist = 0; $ilist < count($arlist); $ilist++) {
  564.  
  565.                 $quantity = $row->quantity * $arlist[$ilist]->ql;
  566.  
  567.                 $this->db->set('quantity', 'quantity'-$quantity)
  568.                       ->where('product_id', $arlist[$ilist]->pl)
  569.                       ->update('products');
  570.               }
  571.             } else {
  572.               $this->db->query("UPDATE products SET quantity=quantity + $row->quantity WHERE product_id=$row->product_id");
  573.             }
  574.           }
  575.  
  576.           $additional_items = $this->db->select('*')
  577.                             ->from('sales_additional_items')
  578.                             ->where('sales_id', $id)
  579.                             ->get()
  580.                             ->result();
  581.  
  582.           foreach ($additional_items as $value) {
  583.             $idp = $this->db->select('*')
  584.                     ->from('product_grouping')
  585.                     ->where('product_id', $value->product_id)
  586.                     ->get()
  587.                     ->result();
  588.  
  589.             if ($idp) {
  590.               $arlist = json_encode(unserialize($idp[0]->list));
  591.               $arlist = json_decode($arlist);
  592.  
  593.               for ($ilist = 0; $ilist < count($arlist); $ilist++) {
  594.  
  595.                 $quantity = $value->quantity * $arlist[$ilist]->ql;
  596.  
  597.                 $this->db->set('quantity', 'quantity'-$quantity)
  598.                       ->where('product_id', $arlist[$ilist]->pl)
  599.                       ->update('products');
  600.               }
  601.             } else {
  602.               $this->db->query("UPDATE products SET quantity=quantity + $row->quantity WHERE product_id=$value->product_id");
  603.             }
  604.           }
  605.         }
  606.         $log_data = array(
  607.             'user_id' => $this->session->userdata('user_id'),
  608.             'table_id' => $sales_id,
  609.             'message' => 'Sales Inserted'
  610.         );
  611.         $this->log_model->insert_log($log_data);
  612.         $jumlahtermin = $this->input->post('terminnumber');
  613.  
  614.  
  615.         if (!empty($jumlahtermin)) {
  616.           $termins = $this->input->post('terminvalue');
  617.           $ket = $this->input->post('terminket');
  618.           $tanggaltermins = $this->input->post('tanggalterminvalue');
  619.           $i = 1;
  620.           if (!empty($termins)) {
  621.             $im = 0;
  622.             $termino = 0;
  623.             foreach ($termins as $quan) {
  624.               $value = $this->input->post('grand_total');
  625.               $nominal = ($value * $quan) / 100;
  626.               $nominal = number_format((float) $nominal, 2, '.', '');
  627.               if (!empty($tanggaltermins[$im])) {
  628.                 $datetermin = $tanggaltermins[$im];
  629.               } else {
  630.                 $datetermin = date('Y-m-d');
  631.               }
  632.               $termino++;
  633.               $datatermin = array(
  634.                   "sales_id" => $sales_id,
  635.                   "persen" => $quan,
  636.                   "nominal" => $nominal,
  637.                   "tanggalbayar" => $datetermin,
  638.                   "keterangan" => $ket[$i]
  639.               );
  640.  
  641.               $this->sales_model->addTerminModel($datatermin);
  642.               $i++;
  643.               $im++;
  644.             }
  645.           }
  646.         }
  647.  
  648.  
  649.         $js_data = json_decode($this->input->post('table_data'));
  650.         $js_data1 = json_decode($this->input->post('tableadditional_data'));
  651.         $js_data2 = json_decode($this->input->post('tableservice_data'));
  652.  
  653.         foreach ($js_data as $key => $value) {
  654.           if ($value != null || !empty($value->product_id)) {
  655.            
  656.             $data = array(
  657.                 "product_id" => $value->product_id,
  658.                 "quantity" => $value->quantity,
  659.                 "price" => $value->price,
  660.                 "gross_total" => $value->total,
  661.                 "discount_id" => @$value->discount_id,
  662.                 "discount_value" => @$value->discount_value,
  663.                 "discount" => $value->discount,
  664.                 "serial_no" => $value->desc,
  665.                 "sales_id" => $sales_id
  666.             );
  667.            
  668.             $this->db->insert('sales_items', $data);
  669.           }
  670.         }
  671.  
  672.         foreach ($js_data1 as $key => $value) {
  673.           if ($value != null || !empty($value->additional_id)) {
  674.            
  675.             $data = array(
  676.                 "product_id" => $value->additional_id,
  677.                 "quantity" => $value->quantity,
  678.                 "price" => $value->price,
  679.                 "gross_total" => $value->total,
  680.                 "discount_id" => @$value->discount_id,
  681.                 "discount_value" => @$value->discount_value,
  682.                 "discount" => $value->discount,
  683.                 "desc" => $value->desc,
  684.                 "sales_id" => $sales_id
  685.             );
  686.            
  687.             $this->db->insert('sales_additional_items' ,$data);
  688.           }
  689.         }
  690.  
  691.         if (!empty($js_data2)) {
  692.           foreach ($js_data2 as $key => $value) {
  693.             if ($value != null || !empty($value->service_id)) {
  694.              
  695.               $data = array(
  696.                   "service_id" => $value->service_id,
  697.                   "quantity" => $value->quantity,
  698.                   "price" => $value->price,
  699.                   "gross_total" => $value->total,
  700.                   "discount_id" => @$value->discount_id,
  701.                   "discount_value" => @$value->discount_value,
  702.                   "discount" => $value->discount,
  703.                   "sales_id" => $sales_id
  704.               );
  705.               $this->sales_model->addSalesServiceItem($data);
  706.             }
  707.           }
  708.         }
  709.        
  710.         $dataman = array(
  711.             "status_rev" => 2
  712.         );
  713.  
  714.         if ($this->sales_model->editModel($id, $dataman)) {
  715.  
  716.           $log_data = array(
  717.               'user_id' => $this->session->userdata('user_id'),
  718.               'table_id' => $id,
  719.               'message' => 'Change Revisi Sales Updated'
  720.           );
  721.           $this->log_model->insert_log($log_data);
  722.         }
  723.  
  724.  
  725.         redirect('sales/view/' . $sales_id);
  726.       } else {
  727.         redirect('sales', 'refresh');
  728.       }
  729.     }
  730.   }
  731.  
  732.   /*
  733.     call edit view to edit sales record
  734.    */
  735.  
  736.   public function getCurrencyAjax($id) {
  737.     $data = $this->sales_model->getCurrencyId($id);
  738.     echo json_encode($data);
  739.     //print_r($data);
  740.   }
  741.  
  742.   public function disetujui($id) {
  743.     $data['data'] = $this->sales_model->getDetails($id);
  744.     $data['quotation'] = $this->quotation_model->getQuotation();
  745.     $data['check_invoice'] = $this->sales_model->checkInvoice($id);
  746.     $data['items'] = $this->sales_model->getItems($id);
  747.     $data['serviceitems'] = $this->sales_model->getServiceItems($id);
  748.     $data['company'] = $this->purchase_model->getCompany();
  749.     $data['customer'] = $this->customer_model->getRecord($data['data'][0]->customer_id);
  750.     $data['quotationadditional'] = $this->quotation_model->getAdditional($id);
  751.     $this->load->view('sales/disetujui', $data);
  752.   }
  753.  
  754.   public function changeSetujui() {
  755.     $id = $this->input->post('sales_id');
  756.  
  757.     $this->form_validation->set_rules('status', 'Status', 'required');
  758.     if ($this->form_validation->run() == false) {
  759.       $this->disetujui($id);
  760.     } else {
  761.  
  762.       $data = array(
  763.             'status_rev' => 2
  764.       );
  765.  
  766.       $array['id_medrep'] = $this->input->post("id_medrep");
  767.             $array['id_sales'] = $this->input->post("id_sales");
  768.             $array['total_sales'] = $this->input->post("total_sales");
  769.             $this->sales_model->insert('medrep_transection',$array);
  770.  
  771.       $product_items = $this->db->select('*')
  772.                         ->from('sales_items')
  773.                         ->where('sales_id', $id)
  774.                         ->get()
  775.                         ->result();
  776.  
  777.       foreach ($product_items as $row) {
  778.         $idp = $this->db->select('*')
  779.                 ->from('product_grouping')
  780.                 ->where('product_id', $row->product_id)
  781.                 ->get()
  782.                 ->result();
  783.  
  784.         if ($idp) {
  785.           $arlist = json_encode(unserialize($idp[0]->list));
  786.           $arlist = json_decode($arlist);
  787.  
  788.           for ($ilist = 0; $ilist < count($arlist); $ilist++) {
  789.  
  790.             $quantity = $row->quantity * $arlist[$ilist]->ql;
  791.  
  792.             $this->db->set('quantity', 'quantity'-$quantity)
  793.                   ->where('product_id', $arlist[$ilist]->pl)
  794.                   ->update('products');
  795.           }
  796.         } else {
  797.           $this->db->query("UPDATE products SET quantity=quantity - $row->quantity WHERE product_id=$row->product_id");
  798.         }
  799.       }
  800.  
  801.       $additional_items = $this->db->select('*')
  802.                         ->from('sales_additional_items')
  803.                         ->where('sales_id', $id)
  804.                         ->get()
  805.                         ->result();
  806.  
  807.       foreach ($additional_items as $value) {
  808.         $idp = $this->db->select('*')
  809.                 ->from('product_grouping')
  810.                 ->where('product_id', $value->product_id)
  811.                 ->get()
  812.                 ->result();
  813.  
  814.         if ($idp) {
  815.           $arlist = json_encode(unserialize($idp[0]->list));
  816.           $arlist = json_decode($arlist);
  817.  
  818.           for ($ilist = 0; $ilist < count($arlist); $ilist++) {
  819.  
  820.             $quantity = $value->quantity * $arlist[$ilist]->ql;
  821.  
  822.             $this->db->set('quantity', 'quantity'-$quantity)
  823.                   ->where('product_id', $arlist[$ilist]->pl)
  824.                   ->update('products');
  825.           }
  826.         } else {
  827.           $this->db->query("UPDATE products SET quantity=quantity - $row->quantity WHERE product_id=$value->product_id");
  828.         }
  829.       }
  830.  
  831.       $update = $this->db->where('sales_id', $id)->update('sales', $data);
  832.  
  833.       if ($update) {
  834.  
  835.         $log_data = array(
  836.             'user_id' => $this->session->userdata('user_id'),
  837.             'table_id' => $id,
  838.             'message' => 'Change Setujui Sales Updated'
  839.         );
  840.         $this->log_model->insert_log($log_data);
  841.  
  842.         redirect('sales', 'refresh');
  843.       } else {
  844.  
  845.         redirect('sales', 'refresh');
  846.       }
  847.  
  848.      
  849.  
  850.     }
  851.   }
  852.  
  853.  
  854.   public function ditolak($id) {
  855.     $data['data'] = $this->sales_model->getDetails($id);
  856.     $data['quotation'] = $this->quotation_model->getQuotation();
  857.     $data['check_invoice'] = $this->sales_model->checkInvoice($id);
  858.     $data['items'] = $this->sales_model->getItems($id);
  859.     $data['serviceitems'] = $this->sales_model->getServiceItems($id);
  860.     $data['company'] = $this->purchase_model->getCompany();
  861.     $data['customer'] = $this->customer_model->getRecord($data['data'][0]->customer_id);
  862.     $data['quotationadditional'] = $this->quotation_model->getAdditional($id);
  863.     $this->load->view('sales/ditolak', $data);
  864.   }
  865.  
  866.   public function changeTolak() {
  867.     $id = $this->input->post('sales_id');
  868.     $this->form_validation->set_rules('status', 'Status', 'required');
  869.     $this->form_validation->set_rules('ditolak', 'Keterangan Ditolak', 'required');
  870.     if ($this->form_validation->run() == false) {
  871.       $this->ditolak($id);
  872.     } else {
  873.  
  874.       $data = array(
  875.             'status_rev' => 3
  876.       );
  877.  
  878.       $update = $this->db->where('sales_id', $id)->update('sales', $data);
  879.  
  880.       if ($update) {
  881.  
  882.         $log_data = array(
  883.             'user_id' => $this->session->userdata('user_id'),
  884.             'table_id' => $id,
  885.             'message' => 'Change Ditolak Sales Updated'
  886.         );
  887.  
  888.         $this->log_model->insert_log($log_data);
  889.  
  890.         redirect('sales', 'refresh');
  891.       } else {
  892.  
  893.         redirect('sales', 'refresh');
  894.       }
  895.     }
  896.   }
  897.  
  898.   public function edit($id) {
  899.  
  900.     $kursdata = $this->company_setting_model->getCurrency();
  901.    
  902.     $data['brands'] = $this->brand_model->getBrand();
  903.     $data['customer'] = $this->sales_model->getCustomer();
  904.     $data['biller'] = $this->sales_model->getBiller();
  905.     $data['product2'] = $this->sales_model->getProducts2();
  906.     $data['service'] = $this->sales_model->getService();
  907.     $data['additional'] = $this->sales_model->getAdditional($id);
  908.     $data['discount'] = $this->sales_model->getDiscount();
  909.     $data['bonus'] = $this->sales_model->getBonus($id);
  910.     $data['tax'] = $this->sales_model->getTax();
  911.     $data['data'] = $this->sales_model->getRecord($id);
  912.     $data['country'] = $this->customer_model->getCountry();
  913.     $data['kursdata'] = $kursdata;
  914.     $data['state'] = $this->customer_model->getState($data['data'][0]->shipping_country_id);
  915.     $data['city'] = $this->customer_model->getCity($data['data'][0]->shipping_state_id);
  916.     $data['product'] = $this->sales_model->getProducts($data['data'][0]->warehouse_id);
  917.     $data['items'] = $this->sales_model->getProducts99($id);
  918.  
  919.     $data['items2_old'] = $this->sales_model->getProducts99($id);
  920.     $data['total_items'] = count($data['items2_old']);
  921.     // echo '<pre>' . var_export($data, true) . '</pre>';
  922.     // die;
  923.     $data['serviceitems'] = $this->sales_model->getServiceItems($data['data'][0]->sales_id);
  924.     $data['termin'] = $this->sales_model->getTerminSalesId($data['data'][0]->sales_id);
  925.  
  926.     $this->load->view('sales/edit', $data);
  927.   }
  928.   public function revisi($id) {
  929.  
  930.     $kursdata = $this->company_setting_model->getCurrency();
  931.  
  932.     $data['brands'] = $this->brand_model->getBrand();
  933.     $data['biller'] = $this->sales_model->getBiller();
  934.     $data['customer'] = $this->sales_model->getCustomer();
  935.     $data['product2'] = $this->sales_model->getProducts2();
  936.     $data['service'] = $this->sales_model->getService();
  937.     $data['additional'] = $this->sales_model->getAdditional($id);
  938.     $data['discount'] = $this->sales_model->getDiscount();
  939.     $data['tax'] = $this->sales_model->getTax();
  940.     $data['data'] = $this->sales_model->getRecord($id);
  941.     $data['country'] = $this->customer_model->getCountry();
  942.     $data['kursdata'] = $kursdata;
  943.     $data['state'] = $this->customer_model->getState($data['data'][0]->shipping_country_id);
  944.     $data['city'] = $this->customer_model->getCity($data['data'][0]->shipping_state_id);
  945.     $data['product'] = $this->sales_model->getProducts($data['data'][0]->warehouse_id);
  946.     $data['items'] = $this->sales_model->getSalesItems($data['data'][0]->sales_id);
  947.     $data['serviceitems'] = $this->sales_model->getServiceItems($data['data'][0]->sales_id);
  948.     $data['termin'] = $this->sales_model->getTerminSalesId($data['data'][0]->sales_id);
  949.  
  950.     $this->load->view('sales/revisi', $data);
  951.   }
  952.  
  953.   /*
  954.     this fucntion is to edit sales record and save in database
  955.    */
  956.  
  957.   public function editSales() {
  958.     $id = $this->input->post('sales_id');
  959.     $submit = $this->input->post('submit');
  960.     if ($submit == "Update") {
  961.       $status = 0;
  962.     } else {
  963.       $status = 4;
  964.     }
  965.  
  966.     $this->form_validation->set_rules('customer', 'Customer', 'trim|required');
  967.     if ($this->form_validation->run() == false) {
  968.       $this->edit($id);
  969.     } else {
  970.       $warehouse_id = $this->input->post('warehouse');
  971.       $old_warehouse_id = $this->input->post('old_warehouse_id');
  972.       $warehouse_change = $this->input->post('warehouse_change');
  973.       $data = array(
  974.           "currentdate" => $this->input->post('sales_date'),
  975.           "date" => $this->input->post('cust_date'),
  976.           "cc_holder" => $this->input->post('taxx'),
  977.           "po_cust" => $this->input->post('po_cust'),
  978.           "warehouse_id" => $this->input->post('warehouse'),
  979.           "ketnama" => $this->input->post('reference'),
  980.           "biller_id" => $this->input->post('biller_id'),
  981.           "customer_id" => $this->input->post('customer'),
  982.           "upcc" => $this->input->post('main'),
  983.           "upcc2" => $this->input->post('cc'),
  984.           "total" => $this->input->post('grand_total'),
  985.           "discount_value" => $this->input->post('total_discount'),
  986.           "tax_value" => $this->input->post('total_tax'),
  987.           "note" => $this->input->post('note'),
  988.           "shipping_city_id" => $this->input->post('city'),
  989.           "shipping_state_id" => $this->input->post('state'),
  990.           "shipping_country_id" => $this->input->post('country'),
  991.           "shipping_address" => $this->input->post('address'),
  992.           "shipping_charge" => $this->input->post('shipping_charge'),
  993.           "internal_note" => $this->input->post('internal_note'),
  994.           "mode_of_transport" => $this->input->post('mode_of_transport'),
  995.           "transporter_name" => $this->input->post('transporter_name'),
  996.           "transporter_code" => $this->input->post('transporter_code'),
  997.           "vehicle_regn_no" => $this->input->post('vehicle_regn_no'),
  998.           "l_r_no" => $this->input->post('l_r_no'),
  999.           "chalan_no" => $this->input->post('chalan_no'),
  1000.           "indent_no" => $this->input->post('indent_no'),
  1001.           "credit_days" => $this->input->post('credit_days'),
  1002.           "date_of_supply" => $this->input->post('date_of_supply'),
  1003.           "electronic_ref_no" => $this->input->post('electronic_ref_no'),
  1004.           "gst_payable" => $this->input->post('gst_payable'),
  1005.           "broker" => $this->input->post('broker'),
  1006.           "user" => $this->session->userdata('user_id'),
  1007.           "term_and_condition" => $this->input->post('term_and_condition'),
  1008.           "sales_id" => $this->input->post('sales_id'),
  1009.           "user_name" => $this->session->userdata('identity'),
  1010.           "kurs_id" => $this->input->post('kursdata'),
  1011.           "nominalcurrency" => $this->input->post('grand_currency'),
  1012.           "ATTN" => $this->input->post('tax'),
  1013.           "shipto" => $this->input->post('shipto'),
  1014.           "billto" => $this->input->post('billto'),
  1015.           "dateestimate" => $this->input->post('dateestimate'),
  1016.           "salesdesc" => $this->input->post('others'),
  1017.           "paid_by" => $this->input->post('paymenterms') ?: 'advance',
  1018.           "additional_description" => $this->input->post('additional_description'),
  1019.           "service_description" => $this->input->post('service_description'),
  1020.           "diskonpersen" => $this->input->post('DiscountPersen'),
  1021.           "diskon" => $this->input->post('diskonfix'),
  1022.           "status_rev" => $status
  1023.       );
  1024.  
  1025.       if ($this->sales_model->editModel($id, $data)) {
  1026.         $log_data = array(
  1027.             'user_id' => $this->session->userdata('user_id'),
  1028.             'table_id' => $id,
  1029.             'message' => 'Sales Updated'
  1030.         );
  1031.         $this->log_model->insert_log($log_data);
  1032.  
  1033.  
  1034.         $this->sales_model->deleteTerminModel($id);
  1035.  
  1036.         $jumlahtermin = $this->input->post('terminnumber');
  1037.  
  1038.         if (!empty($jumlahtermin)) {
  1039.           $termins = $this->input->post('terminvalue');
  1040.           $tanggaltermins = $this->input->post('tanggalterminvalue');
  1041.           $ket = $this->input->post('terminket');
  1042.           if (!empty($termins)) {
  1043.             $im = 0;
  1044.             $i = 1;
  1045.             foreach ($termins as $quan) {
  1046.               $value = $this->input->post('grand_total');
  1047.               $nominal = ($value * $quan) / 100;
  1048.               $nominal = number_format((float) $nominal, 2, '.', '');
  1049.               if (!empty($tanggaltermins[$im])) {
  1050.                 $datetermin = $tanggaltermins[$im];
  1051.               } else {
  1052.                 $datetermin = date('Y-m-d');
  1053.               }
  1054.  
  1055.               $datatermin = array(
  1056.                   "sales_id" => $id,
  1057.                   "persen" => $quan,
  1058.                   "nominal" => $nominal,
  1059.                   "tanggalbayar" => $datetermin,
  1060.                   "keterangan" => $ket[$i]
  1061.               );
  1062.  
  1063.               $this->sales_model->addTerminModel($datatermin);
  1064.               $i++;
  1065.               $im++;
  1066.             }
  1067.           } else {
  1068.            
  1069.           }
  1070.         }
  1071.  
  1072.  
  1073.         // $this->sales_model->deleteSalesItemsAll($id);
  1074.  
  1075.         // $product_id = $this->input->post('product_id');
  1076.         // $quantity = $this->input->post('qty');
  1077.         // $price = $this->input->post('price');
  1078.         // $gross_total = $this->input->post('linetotal');
  1079.         // $discount_id = $this->input->post('discount_id');
  1080.         // $discount_value = $this->input->post('discount_value');
  1081.         // $discount = $this->input->post('discount');
  1082.         // $serial_no = $this->input->post('serial_no');
  1083.         // $sales_id = $id;
  1084.  
  1085.         // foreach ($product_id as $e => $pd) {
  1086.  
  1087.         //       $a['product_id'] = $pd;
  1088.         //       $a['quantity'] = $quantity[$e];
  1089.         //       $a['price'] = $price[$e];
  1090.         //       $a['gross_total'] = $gross_total[$e];
  1091.         //       $a['discount_id'] = $discount_id[$e];
  1092.         //       $a['discount_value'] = $discount_value[$e];
  1093.         //       $a['discount'] = $discount[$e];
  1094.         //       $a['serial_no'] = $serial_no[$e];
  1095.         //       $a['sales_id'] = $sales_id;
  1096.              
  1097.         //       $this->db->insert('sales_items', $a);
  1098.  
  1099.         // }
  1100.  
  1101.         $js_data = json_decode($this->input->post('table_data'));
  1102.         $js_data1 = json_decode($this->input->post('tableadditional_data'));
  1103.         $js_data2 = json_decode($this->input->post('tableservice_data'));
  1104.  
  1105.         $this->sales_model->deleteSalesItemsAll($id);
  1106.  
  1107.         $id_product = $this->input->post('idProducts');
  1108.         $quantity = $this->input->post('quantitys');
  1109.         $price = $this->input->post('prices');
  1110.         $total = $this->input->post('totals');
  1111.         // $discount_id = $this->input->post('hidden_discount');
  1112.         $discount_value = $this->input->post('discount_values');
  1113.         // $serial_no =
  1114.         $sales_id = $id;
  1115.  
  1116.         foreach ($id_product as $e => $pd) {
  1117.           $a['product_id'] = $pd;
  1118.           // $a['quantity'] = $quantity[$e];
  1119.           // $a['price'] = $price[$e];
  1120.           // $a['gross_total'] = $total[$e];
  1121.           // // $a['discount_id'] = $discount_id[$e];
  1122.           // $a['discount_value'] = $discount_value[$e];
  1123.           // $a['sales_id'] = $id;
  1124.  
  1125.           $a['quantity'] = $this->input->post('qty'.$e);
  1126.           $a['price'] = $this->input->post('price'.$e);
  1127.           $a['gross_total'] = $this->input->post('linetotal'.$e);
  1128.           // $a['discount_id'] = $discount_id[$e];
  1129.           // $a['discount_value'] = $this->input->post('qty'.$e);
  1130.           $a['sales_id'] = $id;
  1131.  
  1132.           $this->db->insert('sales_items', $a);
  1133.         }
  1134.  
  1135.         // foreach ($js_data as $key => $value) {
  1136.         //   if (!empty($value) and ! empty($value->product_id)) {
  1137.            
  1138.         //     $data = array(
  1139.         //         "product_id" => $value->product_id,
  1140.         //         "quantity" => (!empty($value->quantity)) ? $value->quantity : 0,
  1141.         //         "price" => (!empty($value->price)) ? $value->price : 0,
  1142.         //         "gross_total" => (!empty($value->total)) ? $value->total : 0,
  1143.         //         "discount_id" => (!empty($value->discount_id)) ? $value->discount_id : 0,
  1144.         //         "discount_value" => (!empty($value->discount_value)) ? $value->discount_value : 0,
  1145.         //         "discount" => (!empty($value->discount)) ? $value->discount : 0,
  1146.  
  1147.         //         "serial_no" => (!empty($value->desc)) ? $value->desc : 0,
  1148.         //         "sales_id" => $id
  1149.         //     );
  1150.            
  1151.         //     $this->db->insert('sales_items', $data);
  1152.         //   }
  1153.         // }
  1154.  
  1155.         $this->sales_model->deleteSalesAdditionalAll($id);
  1156.         foreach ($js_data1 as $key => $value) {
  1157.           if ($value != null || !empty($value->additional_id)) {
  1158.            
  1159.             $data = array(
  1160.                 "product_id" => $value->additional_id,
  1161.                 "quantity" => $value->quantity,
  1162.                 "price" => $value->price,
  1163.                 "gross_total" => $value->total,
  1164.                 "discount_id" => @$value->discount_id,
  1165.                 "discount_value" => @$value->discount_value,
  1166.                 "discount" => $value->discount,
  1167.                 "desc" => $value->desc,
  1168.                 "sales_id" => $id
  1169.             );
  1170.            
  1171.             $this->db->insert('sales_additional_items' ,$data);
  1172.           }
  1173.         }
  1174.  
  1175.         $this->sales_model->deleteSalesServiceItemsAll($id);
  1176.         if (!empty($js_data2)) {
  1177.           foreach ($js_data2 as $key => $value) {
  1178.             if ($value != null || !empty($value->service_id)) {
  1179.  
  1180.               $data = array(
  1181.                   "service_id" => $value->service_id,
  1182.                   "quantity" => $value->quantity,
  1183.                   "price" => $value->price,
  1184.                   "gross_total" => $value->total,
  1185.                   "discount_id" => @$value->discount_id,
  1186.                   "discount_value" => @$value->discount_value,
  1187.                   "discount" => $value->discount,
  1188.                   "sales_id" => $id
  1189.               );
  1190.              
  1191.               $this->sales_model->addSalesServiceItem($data);
  1192.             }
  1193.           }
  1194.         }
  1195.  
  1196.         redirect('sales/view/' . $id);
  1197.       } else {
  1198.         redirect('sales', 'refresh');
  1199.       }
  1200.     }
  1201.   }
  1202.  
  1203.   /*
  1204.     this function is used to delete sales record from database
  1205.    */
  1206.  
  1207.   public function delete($id) {
  1208.     if ($this->sales_model->deleteModel($id)) {
  1209.       $log_data = array(
  1210.           'user_id' => $this->session->userdata('user_id'),
  1211.           'table_id' => $id,
  1212.           'message' => 'Sales Deleted'
  1213.       );
  1214.       $this->log_model->insert_log($log_data);
  1215.       redirect('sales', 'refresh');
  1216.     } else {
  1217.       redirect('sales', 'refresh');
  1218.     }
  1219.   }
  1220.  
  1221.   /*
  1222.     display data in dashboard calendar
  1223.    */
  1224.  
  1225.   public function calendar() {
  1226.     log_message('debug', print_r($this->db->get('category')->result(), true));
  1227.     $data = $this->sales_model->getCalendarData();
  1228.     $total = 0;
  1229.     foreach ($data as $value) {
  1230.       $date = Date('Y-m-d');
  1231.       if ($date == $value->date) {
  1232.         $total += $value->total;
  1233.       }
  1234.       $temp = array(
  1235.           "title" => $total,
  1236.           "start" => "2017-04-05T00:01:00+05:30"
  1237.       );
  1238.     }
  1239.     echo json_encode($temp);
  1240.   }
  1241.  
  1242.   /*
  1243.     view Sales details
  1244.    */
  1245.  
  1246.   public function view($id) {
  1247.     $data['quotation'] = $this->quotation_model->getQuotation();
  1248.     $data['data'] = $this->sales_model->getDetails($id);
  1249.     $data['items'] = $this->sales_model->getItems($id);
  1250.     $data['symbol'] = $this->db->select('*')->from("currency")->where("kurs_id", $data['data'][0]->kurs_id)->get()->result()[0]->symbol;
  1251.    
  1252.     for ($i=0; $i < count($data['items']) ; $i++) {
  1253.  
  1254.       if ($data['items'][$i]->product_grouping != NULL) {
  1255.          $arlist = json_encode(unserialize($data['items'][$i]->list));
  1256.          $arlist = json_decode($arlist);
  1257.          $max = array();
  1258.             for ($ilist = 0; $ilist < count($arlist); $ilist++) {
  1259.  
  1260.                 $qtyp = $this->db->select('quantity')->from('products')->where('product_id', $arlist[$ilist]->pl)->get()->result()[0]->quantity;
  1261.                 $count = ($qtyp / intval($arlist[$ilist]->ql));
  1262.                 array_push($max, $count);
  1263.             }
  1264.         $min = min($max);
  1265.         $data['items'][$i]->quantity_product = intval($min);
  1266.       }
  1267.     }
  1268.     $data['additional'] = $this->sales_model->getAdditional($id);
  1269.     for ($i=0; $i < count($data['additional']) ; $i++) {
  1270.  
  1271.       if ($data['additional'][$i]->product_grouping != NULL) {
  1272.          $arlist = json_encode(unserialize($data['additional'][$i]->list));
  1273.          $arlist = json_decode($arlist);
  1274.          $max = array();
  1275.             for ($ilist = 0; $ilist < count($arlist); $ilist++) {
  1276.  
  1277.                 $qtyp = $this->db->select('quantity')->from('products')->where('product_id', $arlist[$ilist]->pl)->get()->result()[0]->quantity;
  1278.                 $count = ($qtyp / intval($arlist[$ilist]->ql));
  1279.                 array_push($max, $count);
  1280.             }
  1281.         $min = min($max);
  1282.         $data['additional'][$i]->quantity_product = intval($min);
  1283.       }
  1284.     }
  1285.     $data['serviceitems'] = $this->sales_model->getServiceItems($data['data'][0]->sales_id);
  1286.     $data['company'] = $this->purchase_model->getCompany();
  1287.     $data['salestermins'] = $this->sales_model->getTerminSalesId($id);
  1288.     $this->load->view('sales/view', $data);
  1289.   }
  1290.  
  1291.   /*
  1292.     generate pdf
  1293.    */
  1294.  
  1295.   public function pdf($id) {
  1296.     $log_data = array(
  1297.         'user_id' => $this->session->userdata('user_id'),
  1298.         'table_id' => $id,
  1299.         'message' => 'Invoice Generated'
  1300.     );
  1301.     $this->log_model->insert_log($log_data);
  1302.     ob_start();
  1303.     $html = ob_get_clean();
  1304.     $html = utf8_encode($html);
  1305.     $data['quotation'] = $this->quotation_model->getQuotation();
  1306.     $data['data'] = $this->sales_model->getDetails($id);
  1307.     $data['items'] = $this->sales_model->getItems($id);
  1308.     for ($i=0; $i < count($data['items']) ; $i++) {
  1309.  
  1310.       if ($data['items'][$i]->product_grouping != NULL) {
  1311.          $arlist = json_encode(unserialize($data['items'][$i]->list));
  1312.          $arlist = json_decode($arlist);
  1313.          $array = array();
  1314.             for ($ilist = 0; $ilist < count($arlist); $ilist++) {
  1315.  
  1316.                 $name = $this->db->select('name')->from('products')->where('product_id', $arlist[$ilist]->pl)->get()->result()[0]->name;
  1317.                 array_push($array, $name);
  1318.             }
  1319.         $list = implode(', ', $array);
  1320.  
  1321.         $data['items'][$i]->name = $data['items'][$i]->name. ' : ' .$list;
  1322.       }
  1323.     }
  1324.     $data['serviceitems'] = $this->sales_model->getServiceItems($data['data'][0]->sales_id);
  1325.     $data['company'] = $this->purchase_model->getCompany();
  1326.     $data['company_setting'] = $this->company_setting_model->getData();
  1327.     $data['signature'] = $this->signature_model->getActiveSignature();
  1328.     $data['customer'] = $this->customer_model->getRecord($data['data'][0]->customer_id);
  1329.     $data['customer_shipto'] = $this->customer_model->getCustomerAddressbyID($data['data'][0]->shipto);
  1330.     $data['additional'] = $this->sales_model->getAdditional($id);
  1331.     $data['salestermins'] = $this->sales_model->getTerminSalesId($id);
  1332.     for ($i=0; $i < count($data['additional']) ; $i++) {
  1333.  
  1334.       if ($data['additional'][$i]->product_grouping != NULL) {
  1335.          $arlist = json_encode(unserialize($data['additional'][$i]->list));
  1336.          $arlist = json_decode($arlist);
  1337.          $array = array();
  1338.             for ($ilist = 0; $ilist < count($arlist); $ilist++) {
  1339.  
  1340.                 $name = $this->db->select('name')->from('products')->where('product_id', $arlist[$ilist]->pl)->get()->result()[0]->name;
  1341.                 array_push($array, $name);
  1342.             }
  1343.         $list = implode(', ', $array);
  1344.  
  1345.         $data['additional'][$i]->name = $data['additional'][$i]->name. ' : ' .$list;
  1346.       }
  1347.     }
  1348.     $data['bank'] = $this->bank_model->getRecordAr();
  1349.     $data['customer_billto'] = $this->customer_model->getCustomerAddressbyID($data['data'][0]->billto);
  1350.     $data['symbol'] = $this->db->select('*')->from("currency")->where("kurs_id", $data['data'][0]->kurs_id)->get()->result()[0]->symbol;
  1351.  
  1352.     // UNTUK FONT
  1353.     $t = 0;
  1354.     foreach ($data['items'] as $value) { $t += ($value->price * $value->quantity); }
  1355.     foreach ($data['additional'] as $value) { $t += $value->gross_total; }
  1356.     foreach ($data['serviceitems'] as $value) { $t += ($value->price * $value->quantity); }
  1357.  
  1358.     $r = $t - $data['data'][0]->discount_value + $data['data'][0]->shipping_charge + $data['data'][0]->tax_value;
  1359.     if ($r > $t) {
  1360.       $font = $r;
  1361.     } else {
  1362.       $font = $t;
  1363.     }
  1364.  
  1365.     if(strlen($font) <= 9) {
  1366.       $data['style'] = 'style="font-size: 9pt"';
  1367.       $data['style2'] = 'style="font-size: 8pt; font-style: italic; font-weight: bold;"';
  1368.     } elseif (strlen($font) == 10) {
  1369.       $data['style'] = 'style="font-size: 8pt"';
  1370.       $data['style2'] = 'style="font-size: 7pt; font-style: italic; font-weight: bold;"';
  1371.     } elseif (strlen($font) >= 11) {
  1372.       $data['style'] = 'style="font-size: 7pt"';
  1373.       $data['style2'] = 'style="font-size: 6pt; font-style: italic; font-weight: bold;"';
  1374.     }
  1375.    
  1376.     $html = $this->load->view('sales/pdf', $data, true);
  1377.  
  1378.     $logo = base_url(header_logo('logo'));
  1379.     $width = header_logo('width');
  1380.     $height = header_logo('height');
  1381.    
  1382.     $mpdf = new \Mpdf\Mpdf(['setAutoTopMargin' => 'stretch', 'format' => [216, 356]]);
  1383.     $mpdf->SetHTMLHeader('
  1384.  
  1385.    <table style=" vertical-align: bottom; font-family: serif; font-size: 9pt;" width="100%">
  1386.      <tr>
  1387.        <td width="40%" valign="top"><img src="'. $logo .'" width="'. $width .'" height="'. $height .'"></td>
  1388.        <td style="text-align: right;" width="60%">
  1389.          '.header_doc() . '
  1390.          
  1391.        </td>
  1392.      </tr>
  1393.    </table>');
  1394.     if ($data['data'][0]->status_rev == 4) {
  1395.       $status = 'Draft';
  1396.       $mpdf->SetWatermarkText($status);
  1397.       $mpdf->showWatermarkText = true;
  1398.     } else {
  1399.       $mpdf->SetWatermarkImage(base_url('/assets/images/logo-glodisa.jpg'));
  1400.       $mpdf->showWatermarkImage = true;
  1401.     }
  1402.     $mpdf->allow_charset_conversion = true;
  1403.     $mpdf->setFooter('|page {PAGENO} of {nbpg}|');
  1404.     $mpdf->charset_in = 'UTF-8';
  1405.     $mpdf->use_kwt = true;
  1406.     $mpdf->WriteHTML($html);
  1407.     $mpdf->Output(($data['data'][0]->po_new ?: $data['data'][0]->po) . '.pdf', 'I');
  1408.   }
  1409.  
  1410.   public function word($id, $ida='1') {
  1411.     $idar = str_replace('_', ',', $ida);
  1412.     $log_data = array(
  1413.         'user_id' => $this->session->userdata('user_id'),
  1414.         'table_id' => $id,
  1415.         'message' => 'Invoice Generated'
  1416.     );
  1417.     $this->log_model->insert_log($log_data);
  1418.     ob_start();
  1419.     $html = ob_get_clean();
  1420.     $html = utf8_encode($html);
  1421.     $data['quotation'] = $this->quotation_model->getQuotation();
  1422.     $data['data'] = $this->sales_model->getDetails($id);
  1423.     $data['items'] = $this->sales_model->getItems($id);
  1424.     for ($i=0; $i < count($data['items']) ; $i++) {
  1425.  
  1426.       if ($data['items'][$i]->product_grouping != NULL) {
  1427.          $arlist = json_encode(unserialize($data['items'][$i]->list));
  1428.          $arlist = json_decode($arlist);
  1429.          $array = array();
  1430.             for ($ilist = 0; $ilist < count($arlist); $ilist++) {
  1431.  
  1432.                 $name = $this->db->select('name')->from('products')->where('product_id', $arlist[$ilist]->pl)->get()->result()[0]->name;
  1433.                 array_push($array, $name);
  1434.             }
  1435.         $list = implode(', ', $array);
  1436.  
  1437.         $data['items'][$i]->name = $data['items'][$i]->name. ' : ' .$list;
  1438.       }
  1439.     }
  1440.     $data['serviceitems'] = $this->sales_model->getServiceItems($data['data'][0]->sales_id);
  1441.     $data['company'] = $this->purchase_model->getCompany();
  1442.     $data['company_setting'] = $this->company_setting_model->getData();
  1443.     $data['signature'] = $this->signature_model->getActiveSignature();
  1444.     $data['customer'] = $this->customer_model->getRecord($data['data'][0]->customer_id);
  1445.     $data['customer_shipto'] = $this->customer_model->getCustomerAddressbyID($data['data'][0]->shipto);
  1446.     $data['additional'] = $this->sales_model->getAdditional($id);
  1447.     $data['salestermins'] = $this->sales_model->getTerminSalesId($id);
  1448.     for ($i=0; $i < count($data['additional']) ; $i++) {
  1449.  
  1450.       if ($data['additional'][$i]->product_grouping != NULL) {
  1451.          $arlist = json_encode(unserialize($data['additional'][$i]->list));
  1452.          $arlist = json_decode($arlist);
  1453.          $array = array();
  1454.             for ($ilist = 0; $ilist < count($arlist); $ilist++) {
  1455.  
  1456.                 $name = $this->db->select('name')->from('products')->where('product_id', $arlist[$ilist]->pl)->get()->result()[0]->name;
  1457.                 array_push($array, $name);
  1458.             }
  1459.         $list = implode(', ', $array);
  1460.  
  1461.         $data['additional'][$i]->name = $data['additional'][$i]->name. ' : ' .$list;
  1462.       }
  1463.     }
  1464.     $data['bank'] = $this->bank_model->getRecordAr();
  1465.     $data['customer_billto'] = $this->customer_model->getCustomerAddressbyID($data['data'][0]->billto);
  1466.     $data['symbol'] = $this->db->select('*')->from("currency")->where("kurs_id", $data['data'][0]->kurs_id)->get()->result()[0]->symbol;
  1467.  
  1468.     // UNTUK FONT
  1469.     $t = 0;
  1470.     foreach ($data['items'] as $value) { $t += ($value->price * $value->quantity); }
  1471.     foreach ($data['additional'] as $value) { $t += $value->gross_total; }
  1472.     foreach ($data['serviceitems'] as $value) { $t += ($value->price * $value->quantity); }
  1473.  
  1474.     $r = $t - $data['data'][0]->discount_value + $data['data'][0]->shipping_charge + $data['data'][0]->tax_value;
  1475.     if ($r > $t) {
  1476.       $font = $r;
  1477.     } else {
  1478.       $font = $t;
  1479.     }
  1480.  
  1481.     if(strlen($font) <= 9) {
  1482.       $data['style'] = 'style="font-size: 9pt"';
  1483.       $data['style2'] = 'style="font-size: 8pt; font-style: italic; font-weight: bold;"';
  1484.     } elseif (strlen($font) == 10) {
  1485.       $data['style'] = 'style="font-size: 8pt"';
  1486.       $data['style2'] = 'style="font-size: 7pt; font-style: italic; font-weight: bold;"';
  1487.     } elseif (strlen($font) >= 11) {
  1488.       $data['style'] = 'style="font-size: 7pt"';
  1489.       $data['style2'] = 'style="font-size: 6pt; font-style: italic; font-weight: bold;"';
  1490.     }
  1491.  
  1492.     $this->load->library('lmsword');
  1493.     $this->lmsword->page_setup('A4');
  1494.     $this->lmsword->html($this->load->view('sales/pdf', $data, true));
  1495.     $this->lmsword->create($data['data'][0]->po_new ?: $data['data'][0]->po);
  1496.   }
  1497.  
  1498.   public function print1($id, $idb='1') {
  1499.     $idar = str_replace('_', ',', $idb);
  1500.     $log_data = array(
  1501.         'user_id' => $this->session->userdata('user_id'),
  1502.         'table_id' => $id,
  1503.         'message' => 'Invoice Printed'
  1504.     );
  1505.     $this->log_model->insert_log($log_data);
  1506.     $data['data'] = $this->sales_model->getDetails($id);
  1507.     $data['items'] = $this->sales_model->getItems($id);
  1508.     $data['company'] = $this->purchase_model->getCompany();
  1509.     $data['quotation'] = $this->quotation_model->getQuotation();
  1510.     $data['data'] = $this->sales_model->getDetails($id);
  1511.     $data['bank'] = $this->bank_model->getRecordAr($idar);
  1512.     $data['additional'] = $this->sales_model->getAdditional($id);
  1513.     $data['items'] = $this->sales_model->getItems($id);
  1514.     $data['serviceitems'] = $this->sales_model->getServiceItems($data['data'][0]->sales_id);
  1515.     $data['company'] = $this->purchase_model->getCompany();
  1516.     $data['company_setting'] = $this->company_setting_model->getData();
  1517.     $data['signature'] = $this->signature_model->getActiveSignature();
  1518.     $data['customer'] = $this->customer_model->getRecord($data['data'][0]->customer_id);
  1519.     $data['customer_shipto'] = $this->customer_model->getCustomerAddressbyID($data['data'][0]->shipto);
  1520.     $data['customer_billto'] = $this->customer_model->getCustomerAddressbyID($data['data'][0]->billto);
  1521.     $this->load->view('sales/pdf', $data);
  1522.   }
  1523.  
  1524.   /*
  1525.     send email
  1526.    */
  1527.  
  1528.   public function email($id) {
  1529.     $log_data = array(
  1530.         'user_id' => $this->session->userdata('user_id'),
  1531.         'table_id' => 0,
  1532.         'message' => 'Invoice Email Send'
  1533.     );
  1534.     $this->log_model->insert_log($log_data);
  1535.     $email = $this->sales_model->getSmtpSetup();
  1536.  
  1537.     $data = $this->sales_model->getCustomerEmail($id);
  1538.     $company = $this->purchase_model->getCompany();
  1539.     $this->load->view('class.phpmailer.php');
  1540.  
  1541.     $mail = new PHPMailer();
  1542.  
  1543.     $mail->IsSMTP();
  1544.     $mail->Host = $email->smtp_host;
  1545.  
  1546.     $mail->SMTPAuth = true;
  1547.     //$mail->SMTPSecure = "ssl";
  1548.     $mail->Port = $email->smtp_port;
  1549.     $mail->Username = $email->smtp_username;
  1550.     $mail->Password = $email->smtp_password;
  1551.  
  1552.     $mail->From = $email->from_address;
  1553.     $mail->FromName = $email->from_name;
  1554.     $mail->AddAddress($data[0]->email);
  1555.     //$mail->AddReplyTo("mail@mail.com");
  1556.  
  1557.     /* $mail->IsHTML(true);
  1558.  
  1559.       $mail->Subject = "Purchase order No : ".$data[0]->reference_no." From ".$company[0]->name;
  1560.       $mail->Body = "Date : ".$data[0]->date."<br>Total : ".$data[0]->total;
  1561.       //$mail->AltBody = "This is the body in plain text for non-HTML mail clients";
  1562.  
  1563.       if(!$mail->Send())
  1564.       {
  1565.       $message =  "Email could not be sent";
  1566.       }
  1567.       else{
  1568.       $message =  "Email has been sent";
  1569.       } */
  1570.     $total = $data[0]->total + $data[0]->shipping_charge;
  1571.     $this->load->library('email');
  1572.  
  1573.     $this->email->from($email->from_address, $email->from_name);
  1574.     $this->email->to($data[0]->email);
  1575.     $this->email->subject("Sales order No : " . $data[0]->reference_no . " From " . $company[0]->name);
  1576.     $this->email->message("Date : " . $data[0]->date . "   \nTotal : " . $total . " \n\n\nComapany Name : " . $company[0]->name . "\nAddress : " . $company[0]->street . " " . $company[0]->country_name . "\nMobile No :" . $company[0]->phone);
  1577.     //Send mail
  1578.     if ($this->email->send())
  1579.       $message = "Email sent successfully.";
  1580.     else
  1581.       $message = "Error in sending Email.";
  1582.  
  1583.     $this->session->set_flashdata('message', $message);
  1584.     redirect('sales', 'refresh');
  1585.   }
  1586.  
  1587.   /*
  1588.     payment  view
  1589.    */
  1590.  
  1591.   public function payment($id) {
  1592.     $data['data'] = $this->sales_model->getDetailsPayment($id);
  1593.     $data['company'] = $this->purchase_model->getCompany();
  1594.     $data['p_reference_no'] = $this->sales_model->generateReferenceNo();
  1595.  
  1596.     $this->load->view('sales/payment', $data);
  1597.   }
  1598.  
  1599.   /*
  1600.     get payment details to view and send to model
  1601.    */
  1602.  
  1603.   public function addPayment() {
  1604.     $id = $this->input->post('id');
  1605.     $paying_by = $this->input->post('paying_by');
  1606.     $this->form_validation->set_rules('date', 'Date', 'trim|required');
  1607.     $this->form_validation->set_rules('paying_by', 'Paying By', 'trim|required');
  1608.     if ($paying_by == "Cheque") {
  1609.       $this->form_validation->set_rules('bank_name', 'Bank Name', 'trim|required|callback_alpha_dash_space');
  1610.       $this->form_validation->set_rules('cheque_no', 'Cheque No', 'trim|required|numeric');
  1611.     }
  1612.     if ($this->form_validation->run() == false) {
  1613.       $this->payment($id);
  1614.     } else {
  1615.       if ($paying_by == "Cheque") {
  1616.         $bank_name = $this->input->post('bank_name');
  1617.         $cheque_no = $this->input->post('cheque_no');
  1618.       } else {
  1619.         $bank_name = "";
  1620.         $cheque_no = "";
  1621.       }
  1622.       $data = array(
  1623.           "sales_id" => $id,
  1624.           "date" => $this->input->post('date'),
  1625.           "reference_no" => $this->input->post('reference_no'),
  1626.           "amount" => $this->input->post('amount'),
  1627.           "paying_by" => $this->input->post('paying_by'),
  1628.           "bank_name" => $bank_name,
  1629.           "cheque_no" => $cheque_no,
  1630.           "description" => $this->input->post('note')
  1631.       );
  1632.  
  1633.       if ($this->sales_model->addPayment($data)) {
  1634.         $log_data = array(
  1635.             'user_id' => $this->session->userdata('user_id'),
  1636.             'table_id' => $id,
  1637.             'message' => 'Sales Payable'
  1638.         );
  1639.         $this->log_model->insert_log($log_data);
  1640.         redirect('sales', 'refresh');
  1641.       } else {
  1642.         redirect("sales", 'refresh');
  1643.       }
  1644.     }
  1645.   }
  1646.  
  1647.   /*
  1648.     generate invoice
  1649.    */
  1650.  
  1651.   public function invoice() {
  1652.     $data['data'] = $this->sales_model->invoice();
  1653.     $this->load->view('invoice/list', $data);
  1654.   }
  1655.  
  1656.   /*
  1657.  
  1658.    */
  1659.  
  1660.   public function getCustomerData($id) {
  1661.     $data['data'] = $this->customer_model->getRecord($id);
  1662.     $data['country'] = $this->customer_model->getCountry();
  1663.     $data['state'] = $this->customer_model->getState($data['data'][0]->country_id);
  1664.     $data['city'] = $this->customer_model->getCity($data['data'][0]->state_id);
  1665.     $data['address'] = $this->customer_model->getListAddressRecord($id);
  1666.     echo json_encode($data);
  1667.   }
  1668.  
  1669.   public function getRecordAjax($id) {
  1670.     $data['data'] = $this->quotation_model->getRecordAjax($id);
  1671.     echo json_encode($data);
  1672.   }
  1673.  
  1674.   public function getCustomerAddressData($id) {
  1675.     $data['data'] = $this->customer_model->getRecord($id);
  1676.     $data['address'] = $this->customer_model->getCustomerAddressbyID($id);
  1677.     echo json_encode($data);
  1678.   }
  1679.  
  1680.   /*
  1681.     check character and space validation
  1682.    */
  1683.  
  1684.   function alpha_dash_space($str) {
  1685.     if (!preg_match("/^([a-zA-Z ])+$/i", $str)) {
  1686.       $this->form_validation->set_message('alpha_dash_space', 'The %s field may only contain alpha and spaces');
  1687.       return FALSE;
  1688.     } else {
  1689.       return TRUE;
  1690.     }
  1691.   }
  1692.  
  1693.   function currencyConverter2($currency_from, $currency_to) {
  1694.     if ($currency_from == $currency_to) {
  1695.       return 1;
  1696.     } else {
  1697.       //$currency_output = round(currency($currency_input, $currency_from,$currency_to, false),2);
  1698.       //$currency_input=substr($currency_input, 0, -3);
  1699.       $currency_input = filter_var(1, FILTER_SANITIZE_NUMBER_INT);
  1700.       $from_Currency = urlencode($currency_from);
  1701.       $to_Currency = urlencode($currency_to);
  1702.       $get = @file_get_contents("https://finance.google.com/finance/converter?a=1&from=$from_Currency&to=$to_Currency");
  1703.       $get = @explode("<span class=bld>", $get);
  1704.       $get = @explode("</span>", @$get[1]);
  1705.       $converted_currency = preg_replace("/[^0-9\.]/", null, @$get[0]);
  1706.       $converted_currency = $converted_currency;
  1707.       return $converted_currency;
  1708.  
  1709.       //return $currency_output;
  1710.     }
  1711.   }
  1712.  
  1713. }
  1714.  
  1715. ?>
RAW Paste Data