Advertisement
hungaudi

sales c

Nov 13th, 2015
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 154.13 KB | None | 0 0
  1. <?php
  2.  
  3. require_once(APPPATH . 'third_party/Classes/PHPExcel/IOFactory.php');
  4. require_once(APPPATH . 'libraries/php-excel.class.php');
  5. require_once ("secure_area.php");
  6.  
  7. class Sales extends Secure_area {
  8.  
  9.     function __construct() {
  10.         parent::__construct('sales');
  11.         $this->load->library('sale_lib');
  12.     }
  13.  
  14.     function index() {
  15.         if ($this->config->item('track_cash')) {
  16.             if ($this->input->post('opening_amount') != '') {
  17.                 $now = date('Y-m-d H:i:s');
  18.                 $cash_register = new stdClass();
  19.                 $cash_register->employee_id = $this->session->userdata('person_id');
  20.                 $cash_register->shift_start = $now;
  21.                 $cash_register->open_amount = $this->input->post('opening_amount');
  22.                 $cash_register->close_amount = 0;
  23.                 $cash_register->cash_sales_amount = 0;
  24.                 $this->Sale->insert_register($cash_register);
  25.                 redirect(site_url('sales'));
  26.             } else if ($this->Sale->is_register_log_open()) {
  27.                 $this->_reload(array(), false);
  28.             } else {
  29.                 $this->load->view('sales/opening_amount');
  30.             }
  31.         } else {
  32.             $this->_reload(array(), false);
  33.         }
  34.     }
  35.  
  36.     function closeregister() {
  37.         if (!$this->Sale->is_register_log_open()) {
  38.             redirect(site_url('home'));
  39.             return;
  40.         }
  41.         $cash_register = $this->Sale->get_current_register_log();
  42.         $continueUrl = $this->input->get('continue');
  43.         if ($this->input->post('closing_amount') != '') {
  44.             $now = date('Y-m-d H:i:s');
  45.             $cash_register->shift_end = $now;
  46.             $cash_register->close_amount = $this->input->post('closing_amount');
  47.             $cash_register->cash_sales_amount = $this->Sale->get_cash_sales_total_for_shift($cash_register->shift_start, $cash_register->shift_end);
  48.             unset($cash_register->register_log_id);
  49.             $this->Sale->update_register_log($cash_register);
  50.             if ($continueUrl == 'logout') {
  51.                 redirect(site_url('home/logout'));
  52.             } else {
  53.                 redirect(site_url('home'));
  54.             }
  55.         } else {
  56.             $this->load->view('sales/closing_amount', array(
  57.                 'continue' => $continueUrl ? "?continue=$continueUrl" : '',
  58.                 'closeout' => to_currency($cash_register->open_amount + $this->Sale->get_cash_sales_total_for_shift($cash_register->shift_start, date("Y-m-d H:i:s")))
  59.             ));
  60.         }
  61.     }
  62.  
  63.     function item_search1() {
  64.         $suggestions = $this->Item->get_item_search_suggestions($this->input->get('term'), 100);
  65.         $suggestions = array_merge($suggestions, $this->Item_kit->get_item_kit_search_suggestions($this->input->get('term'), 100));
  66.         echo json_encode($suggestions);
  67.     }
  68.  
  69.     function item_search() {
  70.         $store = $this->sale_lib->get_store();
  71.         if ($this->Employee->get_logged_in_employee_info()->person_id == 1) {
  72.             if ($store != '' && $store != '1999') {
  73.                 $suggestions = $this->Item->get_item_search_suggestions_stored($this->input->get('term'), $store, 100);
  74.                 echo json_encode($suggestions);
  75.             } elseif ($store == '1999') {
  76.                 $suggestions = $this->Pack->get_pack_search_suggestions($this->input->get('term'), 100);
  77.                 echo json_encode($suggestions);
  78.             } else {
  79.                 $suggestions = $this->Item->get_item_search_suggestions_sales($this->input->get('term'), 100);
  80.                 echo json_encode($suggestions);
  81.             }
  82.         } else {
  83.             if ($store != '') {
  84.                 $suggestions = $this->Item->get_item_search_suggestions_stored($this->input->get('term'), $store, 100);
  85.                 echo json_encode($suggestions);
  86.             }
  87.         }
  88.     }
  89.  
  90.     function customer_search() {
  91.         $suggestions = $this->Customer->get_info_customer_thu_chi($this->input->get('term'), 100);
  92.         echo json_encode($suggestions);
  93.     }
  94.  
  95.     //hung audi 8-4-15
  96.     function supplier_search_cost() {
  97.         $suggestions = $this->Supplier->get_supplier_search_cost($this->input->get('term'), 100);
  98.         echo json_encode($suggestions);
  99.     }
  100.  
  101.     function select_customer() {
  102.         $data = array();
  103.         $customer_id = $this->input->post("customer");
  104.         if ($this->Customer->exists($customer_id)) {
  105.             $this->sale_lib->set_customer($customer_id);
  106.         } else {
  107.             $data['error'] = lang('sales_unable_to_add_customer');
  108.         }
  109.  
  110.         $this->_reload($data);
  111.     }
  112.  
  113.     function change_mode() {
  114.         $mode = $this->input->post("mode");
  115.         $this->sale_lib->set_mode($mode);
  116.         $this->_reload();
  117.     }
  118.  
  119.     function set_comment() {
  120.         $this->sale_lib->set_comment($this->input->post('comment'));
  121.     }
  122.  
  123.     //huyenlt^^
  124.     function set_employees_id() {
  125.         $this->sale_lib->set_employees_id($this->input->post('employees_id'));
  126.     }
  127.  
  128.     function set_store() {
  129.         $this->sale_lib->set_store($this->input->post('store'));
  130.     }
  131.  
  132.     function set_employees_delivery() {
  133.         $this->sale_lib->set_employees_delivery($this->input->post('delivery_employee'));
  134.     }
  135.  
  136.     function set_discount_money() {
  137.  
  138.         $this->sale_lib->set_discount_money($this->input->post('discount_money'));
  139.     }
  140.  
  141.     /* phan lam 02/09/2013 */
  142.  
  143.     function add_date_debt() {
  144.         $date_debt = date('Y-m-d', strtotime($this->input->post('date_debt')));
  145.         $this->sale_lib->set_date_debt($date_debt);
  146.     }
  147.  
  148.     function set_comment_on_receipt() {
  149.         $this->sale_lib->set_comment_on_receipt($this->input->post('show_comment_on_receipt'));
  150.     }
  151.  
  152.     function set_email_receipt() {
  153.         $this->sale_lib->set_email_receipt($this->input->post('email_receipt'));
  154.     }
  155.  
  156.     //Alain Multiple Payments
  157.     function add_payment() {
  158.         $tienchietkhau = $this->input->post('tienkhauhao');
  159.         //$discount_money = $this->input->post('discount_money');
  160.         $data['discount_money'] = $this->sale_lib->get_discount_money();
  161.         //echo $tienchietkhau;
  162.         $data = array();
  163.         //$this->form_validation->set_rules('amount_tendered', 'lang:sales_amount_tendered', 'required');
  164.         $this->form_validation->set_rules('amount_tendered', 'lang:sales_amount_tendered', '');
  165.         if ($this->form_validation->run() == FALSE) {
  166.             if ($this->input->post('payment_type') == lang('sales_giftcard'))
  167.                 $data['error'] = lang('sales_must_enter_numeric_giftcard');
  168.             else
  169.                 $data['error'] = lang('sales_must_enter_numeric');
  170.             $this->_reload($data);
  171.             return;
  172.         }
  173.         $payment_type = $this->input->post('payment_type');
  174.         if ($payment_type == lang('sales_giftcard')) {
  175.             if (!$this->Giftcard->exists($this->Giftcard->get_giftcard_id($this->input->post('amount_tendered')))) {
  176.                 $data['error'] = lang('sales_giftcard_does_not_exist');
  177.                 $this->_reload($data);
  178.                 return;
  179.             }
  180.             $payments = $this->sale_lib->get_payments();
  181.             $payment_type = $this->input->post('payment_type') . ':' . $this->input->post('amount_tendered');
  182.             $current_payments_with_giftcard = isset($payments[$payment_type]) ? $payments[$payment_type]['payment_amount'] : 0;
  183.             $cur_giftcard_value = $this->Giftcard->get_giftcard_value($this->input->post('amount_tendered')) - $current_payments_with_giftcard;
  184.             if ($cur_giftcard_value <= 0 && $this->sale_lib->get_total() > 0) {
  185.                 $data['error'] = lang('sales_giftcard_balance_is') . ' ' . to_currency($this->Giftcard->get_giftcard_value($this->input->post('amount_tendered'))) . ' !';
  186.                 $this->_reload($data);
  187.                 return;
  188.             } elseif (( $this->Giftcard->get_giftcard_value($this->input->post('amount_tendered')) - $this->sale_lib->get_total() ) > 0) {
  189.                 $data['warning'] = lang('sales_giftcard_balance_is') . ' ' . to_currency($this->Giftcard->get_giftcard_value($this->input->post('amount_tendered')) - $this->sale_lib->get_total()) . ' !';
  190.             }
  191.             $payment_amount = min($this->sale_lib->get_amount_due(), $this->Giftcard->get_giftcard_value(to_un_currency($this->input->post('amount_tendered'))));
  192.         } else {
  193.             $payment_amount = str_replace(array(',', '.'), '', $this->input->post('amount_tendered'));
  194.             $discount_money = str_replace(array(',', '.'), '', $this->input->post('discount_money'));
  195.         }
  196.         //add money
  197.         if (!$this->sale_lib->add_payment($payment_type, $payment_amount, $discount_money)) {
  198.             $data['error'] = lang('sales_unable_to_add_payment');
  199.         }
  200.         $this->_reload($data);
  201.     }
  202.  
  203.     //Alain Multiple Payments
  204.     function delete_payment($payment_id) {
  205.         $this->sale_lib->delete_payment(rawurldecode($payment_id));
  206.         $this->_reload();
  207.     }
  208.  
  209.     function add() {
  210.         $data = array();
  211.         $mode = $this->sale_lib->get_mode();
  212.         $item_id_or_number_or_item_kit_or_receipt = $this->input->post("item");
  213.         $quantity = $mode == "sale" ? 1 : -1;
  214.         $kho = $this->sale_lib->get_store();
  215.  
  216.         if ($this->sale_lib->is_valid_receipt($item_id_or_number_or_item_kit_or_receipt) && $mode == 'return') {
  217.             $this->sale_lib->return_entire_sale($item_id_or_number_or_item_kit_or_receipt);
  218.         } elseif ($this->sale_lib->is_valid_item_kit($item_id_or_number_or_item_kit_or_receipt)) {
  219.             $this->sale_lib->add_item_kit($item_id_or_number_or_item_kit_or_receipt, $quantity);
  220.  
  221.             $item_kit_id = $this->sale_lib->get_valid_item_kit_id($item_id_or_number_or_item_kit_or_receipt);
  222.             if ($this->sale_lib->out_of_stock_kit($item_kit_id)) {
  223.                 $data['warning'] = lang('sales_quantity_less_than_zero');
  224.             }
  225.         } elseif ($this->sale_lib->is_valid_pack($item_id_or_number_or_item_kit_or_receipt)) {
  226.             $this->sale_lib->add_pack($item_id_or_number_or_item_kit_or_receipt, $quantity);
  227.  
  228.             $pack_id = $this->sale_lib->get_valid_pack_id($item_id_or_number_or_item_kit_or_receipt);
  229.             if ($this->sale_lib->out_of_stock_pack($pack_id)) {
  230.                 $data['warning'] = lang('sales_quantity_less_than_zero');
  231.             }
  232.         } elseif (!$this->sale_lib->add_item($item_id_or_number_or_item_kit_or_receipt, $quantity)) {
  233.             $data['error'] = lang('sales_unable_to_add_item');
  234.         }
  235.         if ($this->sale_lib->out_of_stock($item_id_or_number_or_item_kit_or_receipt)) {
  236.             $data['warning'] = lang('sales_quantity_less_than_zero');
  237.         }
  238.         $this->_reload($data);
  239.     }
  240.  
  241.     function edit_item678($line) {
  242.         $data = array();
  243.         $this->form_validation->set_rules('price', 'lang:items_price', 'required|numeric');
  244.         $this->form_validation->set_rules('quantity', 'lang:items_quantity', 'required|numeric');
  245.  
  246.         $description = $this->input->post("description");
  247.         $serialnumber = $this->input->post("serialnumber");
  248.         $price = $this->input->post("price");
  249.         $quantity = $this->input->post("quantity");
  250.         $discount = $this->input->post("discount");
  251.  
  252.  
  253.  
  254.         if ($this->form_validation->run() != FALSE) {
  255.             $this->sale_lib->edit_item($line, $description, $serialnumber, $quantity, $discount, $price);
  256.         } else {
  257.             $data['error'] = lang('sales_error_editing_item');
  258.         }
  259.  
  260.         if ($this->sale_lib->is_kit_or_item($line) == 'item') {
  261.             if ($this->sale_lib->out_of_stock($this->sale_lib->get_item_id($line))) {
  262.                 $data['warning'] = lang('sales_quantity_less_than_zero');
  263.             }
  264.         } elseif ($this->sale_lib->is_kit_or_item($line) == 'kit') {
  265.             if ($this->sale_lib->out_of_stock_kit($this->sale_lib->get_kit_id($line))) {
  266.                 $data['warning'] = lang('sales_quantity_less_than_zero');
  267.             }
  268.         }
  269.         $this->_reload($data);
  270.     }
  271.  
  272.     function edit_item($line) {
  273.         $data = array();
  274.         $this->form_validation->set_rules('quantity', 'lang:items_quantity', 'required|numeric');
  275.         $this->form_validation->set_rules('discount', 'lang:items_discount', 'required|numeric');
  276.         $store = $this->sale_lib->get_store();
  277.         $item_id = $this->input->post("item_id");
  278.         $type_item = $this->input->post("type_item");
  279.         $info_item = $this->Item->get_info($item_id);
  280.         $unit = $this->input->post("unit");
  281.         $description = $this->input->post("description");
  282.         $serialnumber = $this->input->post("serialnumber");
  283.         $price = str_replace(array(',', '.00'), '', $this->input->post('price'));
  284.         $price_rate = str_replace(array(',', '.00'), '', $this->input->post('price_rate'));
  285.  
  286.         $quantity = $this->input->post("quantity");
  287.         $discount = $this->input->post("discount");
  288.  
  289.  
  290.         if ($this->session->userdata('unit_' . $line)) {
  291.             $this->session->unset_userdata('unit_' . $line);
  292.             $this->session->set_userdata('unit_' . $line, $unit);
  293.         } else {
  294.  
  295.             $this->session->set_userdata('unit_' . $line, $unit);
  296.         }
  297.         if ($unit == 'unit') {
  298.             if ($store != '1999') {
  299.                 if ($info_item->quantity_first != 0) {
  300.                     if ($store != "") {
  301.                         $info_item_store = $this->Item->get_Stores_Items($item_id, $store);
  302.                         $so_nguyen = FLOOR($info_item_store->quantity / $info_item->unit_rate);
  303.                         $abs = abs($info_item_store->quantity / $info_item->unit_rate);
  304.                     } else {
  305.                         $so_nguyen = FLOOR($info_item->quantity_total / $info_item->unit_rate);
  306.                         $abs = abs($info_item->quantity_total / $info_item->unit_rate);
  307.                     }
  308.                     $so_du = abs($info_item->quantity_total % $info_item->unit_rate);
  309.                     if ($abs < 1) {
  310.                         $data['warning'] = "Cảnh báo, số lượng mong muốn không đủ. Bạn vẫn có thể thực hiện đơn hàng này, nhưng hãy kiểm tra hàng tồn kho của của bạn.";
  311. //                        $data['warning'] = "Không thể bán theo đơn vị trước quy đổi. Số lượng theo đơn vị trước quy đổi là " . $so_nguyen;
  312. //                        $unit = "unit_from";
  313.                     } elseif ($quantity > $so_nguyen) {
  314.                         $data['warning'] = "Cảnh báo, số lượng mong muốn không đủ. Bạn vẫn có thể thực hiện đơn hàng này, nhưng hãy kiểm tra hàng tồn kho của của bạn.";
  315. //                        $data['warning'] = "Số lượng mong muốn không đúng. Số lượng tối đa có thể bán theo đơn vị trước quy đổi là " . $so_nguyen;
  316. //                        $unit = "unit_from";
  317.                     } else {
  318.                         $unit = "unit";
  319.                     }
  320.                 }
  321.             }
  322.         } elseif ($this->sale_lib->is_kit_or_item($line) == 'item') {
  323.             if ($this->sale_lib->out_of_stock($this->sale_lib->get_item_id($line))) {
  324.                 $data['warning'] = lang('sales_quantity_less_than_zero');
  325.             }
  326.         } elseif ($this->sale_lib->is_kit_or_item($line) == 'kit') {
  327.             if ($this->sale_lib->out_of_stock_kit($this->sale_lib->get_kit_id($line))) {
  328.                 $data['warning'] = lang('sales_quantity_less_than_zero');
  329.             }
  330.         }
  331.         if ($this->form_validation->run() != FALSE) {
  332.             $this->sale_lib->edit_item($line, $description, $serialnumber, $quantity, $discount, $price, $price_rate, $unit);
  333.         } else {
  334.             $data['error'] = lang('sales_error_editing_item');
  335.         }
  336.         $this->_reload($data);
  337.     }
  338.  
  339.     function delete_item($item_number) {
  340.         $this->sale_lib->delete_item($item_number);
  341.         $this->_reload();
  342.     }
  343.  
  344.     function delete_customer() {
  345.         $this->sale_lib->delete_customer();
  346.         $this->_reload();
  347.     }
  348.  
  349.     function start_cc_processing() {
  350.         $service_url = (!defined("ENVIRONMENT") or ENVIRONMENT == 'development') ? 'https://hc.mercurydev.net/hcws/hcservice.asmx?WSDL' : 'https://hc.mercurypay.com/hcws/hcservice.asmx?WSDL';
  351.         $payments = $this->sale_lib->get_payments();
  352.         $cc_amount = number_format($payments[lang('sales_credit')]['payment_amount'], 2);
  353.         $tax_amount = number_format(($this->sale_lib->get_total() - $this->sale_lib->get_subtotal()) * ($payments[lang('sales_credit')]['payment_amount'] / $this->sale_lib->get_total()), 2);
  354.         $customer_id = $this->sale_lib->get_customer();
  355.         $customer_name = '';
  356.         if ($customer_id != -1) {
  357.             $customer_info = $this->Customer->get_info($customer_id);
  358.             $customer_name = $customer_info->first_name . ' ' . $customer_info->last_name;
  359.         }
  360.         $invoice_number = substr((date('mdy')) . (time() - strtotime("today")) . ($this->Employee->get_logged_in_employee_info()->person_id), 0, 16);
  361.         $parameters = array(
  362.             'request' => array(
  363.                 'MerchantID' => $this->config->item('merchant_id'),
  364.                 'Password' => $this->config->item('merchant_password'),
  365.                 'TranType' => $cc_amount > 0 ? 'Sale' : 'Return',
  366.                 'TotalAmount' => abs($cc_amount),
  367.                 'PartialAuth' => 'On',
  368.                 'Frequency' => 'OneTime',
  369.                 'OperatorID' => (!defined("ENVIRONMENT") or ENVIRONMENT == 'development') ? 'test' : $this->Employee->get_logged_in_employee_info()->person_id,
  370.                 'Invoice' => $invoice_number,
  371.                 'Memo' => 'PHP POS ' . APPLICATION_VERSION,
  372.                 'TaxAmount' => abs($tax_amount),
  373.                 'CardHolderName' => $customer_name,
  374.                 'ProcessCompleteUrl' => site_url('sales/finish_cc_processing'),
  375.                 'ReturnUrl' => site_url('sales/cancel_cc_processing'),
  376.             )
  377.         );
  378.  
  379.         if (isset($customer_info) && $customer_info->zip) {
  380.             $parameters['request']['AVSZip'] = $customer_info->zip;
  381.         }
  382.  
  383.         $client = new SoapClient($service_url, array('trace' => TRUE));
  384.         $result = $client->InitializePayment($parameters);
  385.         $response_code = $result->InitializePaymentResult->ResponseCode;
  386.  
  387.         if ($response_code == 0) {
  388.             $payment_id = $result->InitializePaymentResult->PaymentID;
  389.             $hosted_checkout_url = (!defined("ENVIRONMENT") or ENVIRONMENT == 'development') ? 'https://hc.mercurydev.net/CheckoutPOS.aspx' : 'https://hc.mercurypay.com/CheckoutPOS.aspx';
  390.             $this->load->view('sales/hosted_checkout', array('payment_id' => $payment_id, 'hosted_checkout_url' => $hosted_checkout_url));
  391.         } else {
  392.             $this->_reload(array('error' => lang('sales_credit_card_processing_is_down')), false);
  393.         }
  394.     }
  395.  
  396.     function finish_cc_processing() {
  397.         $return_code = $this->input->get("ReturnCode");
  398.         $service_url = (!defined("ENVIRONMENT") or ENVIRONMENT == 'development') ? 'https://hc.mercurydev.net/hcws/hcservice.asmx?WSDL' : 'https://hc.mercurypay.com/hcws/hcservice.asmx?WSDL';
  399.         $parameters = array(
  400.             'request' => array(
  401.                 'MerchantID' => $this->config->item('merchant_id'),
  402.                 'PaymentID' => $this->input->get('PaymentID'),
  403.                 'Password' => $this->config->item('merchant_password'),
  404.             )
  405.         );
  406.         $client = new SoapClient($service_url, array('trace' => TRUE));
  407.         $result = $client->VerifyPayment($parameters);
  408.         $response_code = $result->VerifyPaymentResult->ResponseCode;
  409.         $status = $result->VerifyPaymentResult->Status;
  410.         $total_amount = $result->VerifyPaymentResult->Amount;
  411.         $auth_amount = $result->VerifyPaymentResult->AuthAmount;
  412.  
  413.         $auth_code = $result->VerifyPaymentResult->AuthCode;
  414.         $acq_ref_data = $result->VerifyPaymentResult->AcqRefData;
  415.         $ref_no = $result->VerifyPaymentResult->RefNo;
  416.         $token = $result->VerifyPaymentResult->Token;
  417.         $process_data = $result->VerifyPaymentResult->ProcessData;
  418.  
  419.         if ($response_code == 0 && $status == 'Approved') {
  420.             $result = $client->AcknowledgePayment($parameters);
  421.             $response_code = $result->AcknowledgePaymentResult;
  422.  
  423.             if ($response_code == 0 && $auth_amount == $total_amount) {
  424.                 $this->session->set_flashdata('ref_no', $ref_no);
  425.                 redirect(site_url('sales/complete'));
  426.             } elseif ($response_code == 0 && $auth_amount < $total_amount) {
  427.                 $invoice_number = substr((date('mdy')) . (time() - strtotime("today")) . ($this->Employee->get_logged_in_employee_info()->person_id), 0, 16);
  428.  
  429.                 $partial_transaction = array(
  430.                     'AuthCode' => $auth_code,
  431.                     'Frequency' => 'OneTime',
  432.                     'Memo' => 'PHP POS ' . APPLICATION_VERSION,
  433.                     'Invoice' => $invoice_number,
  434.                     'MerchantID' => $this->config->item('merchant_id'),
  435.                     'OperatorID' => (!defined("ENVIRONMENT") or ENVIRONMENT == 'development') ? 'test' : $this->Employee->get_logged_in_employee_info()->person_id,
  436.                     'PurchaseAmount' => $auth_amount,
  437.                     'RefNo' => $ref_no,
  438.                     'Token' => $token,
  439.                     'AcqRefData' => $acq_ref_data,
  440.                     'ProcessData' => $process_data,
  441.                 );
  442.  
  443.                 $this->sale_lib->delete_payment(lang('sales_credit'));
  444.                 $this->sale_lib->add_payment(lang('sales_partial_credit'), $auth_amount);
  445.                 $this->sale_lib->add_partial_transaction($partial_transaction);
  446.                 $this->_reload(array('warning' => lang('sales_credit_card_partially_charged_please_complete_sale_with_another_payment_method')), false);
  447.             } else {
  448.                 $this->_reload(array('error' => lang('sales_acknowledge_payment_failed_please_contact_support')), false);
  449.             }
  450.         } else {
  451.             $client->AcknowledgePayment($parameters);
  452.             $this->_reload(array('error' => $result->VerifyPaymentResult->DisplayMessage), false);
  453.         }
  454.     }
  455.  
  456.     function cancel_cc_processing() {
  457.         $this->sale_lib->delete_payment(lang('sales_credit'));
  458.         $this->_reload(array('error' => lang('sales_cc_processing_cancelled')), false);
  459.     }
  460.  
  461.     //Created by San
  462.     function download_matarial() {
  463.         header('Content-Type: text/html; charset=utf-8');
  464.         $file = $_GET['file'];
  465.         $data = file_get_contents(APPPATH . "/../excel_materials/" . $file);
  466.         force_download($file, $data);
  467.     }
  468.  
  469.     // phan lam bao gia hop dong
  470.     function baogia($sale_id) {
  471.         $id_quotes_contract = $this->input->get("quotes");
  472.         $data['info_quotes_contract'] = $this->M_quotes_contract->get_info($id_quotes_contract);
  473.         $data['is_sale'] = FALSE;
  474.         $sale_info = $this->Sale->get_info($sale_id)->row_array();
  475.         $this->sale_lib->copy_entire_sale($sale_id);
  476.         $data['cart'] = $this->sale_lib->get_cart();
  477.         $data['payments'] = $this->sale_lib->get_payments();
  478.         $data['subtotal'] = $this->sale_lib->get_subtotal();
  479.         $data['taxes'] = $this->sale_lib->get_taxes($sale_id);
  480.         $data['total'] = $this->sale_lib->get_total($sale_id);
  481.         $data['receipt_title'] = lang('sales_receipt');
  482.         $data['comment'] = $this->Sale->get_comment($sale_id);
  483.         $data['show_comment_on_receipt'] = $this->Sale->get_comment_on_receipt($sale_id);
  484.         $data['transaction_time'] = date(get_date_format() . ' ' . get_time_format(), strtotime($sale_info['sale_time']));
  485.         $customer_id = $this->sale_lib->get_customer();
  486.         $emp_info = $this->Employee->get_info($sale_info['employee_id']);
  487.         $info_empss = $this->Employee->get_info($sale_info['employees_id']);
  488.         $data['employees_id'] = $info_empss->first_name . ' ' . $info_empss->last_name;
  489.         $data['phone_number1'] = $info_empss->phone_number;
  490.         $data['email1'] = $info_empss->email;
  491.         $data['payment_type'] = $sale_info['payment_type'];
  492.         $data['amount_change'] = $this->sale_lib->get_amount_due($sale_id) * -1;
  493.         $data['employee'] = $emp_info->first_name . ' ' . $emp_info->last_name;
  494.         $data['phone_number'] = $emp_info->phone_number;
  495.         $data['email'] = $emp_info->email;
  496.         $data['ref_no'] = $sale_info['cc_ref_no'];
  497.         $this->load->helper('string');
  498.         $data['payment_type'] = str_replace(array('<sup>VNĐ</sup><br />', ''), ' .VNĐ', $sale_info['payment_type']);
  499.         $data['amount_due'] = $this->sale_lib->get_amount_due();
  500.  
  501.         if ($customer_id != -1) {
  502.             $cust_info = $this->Customer->get_info($customer_id);
  503.             $data['customer'] = $cust_info->first_name . ' ' . $cust_info->last_name;
  504.             $data['cus_name'] = $cust_info->company_name == '' ? '' : $cust_info->company_name;
  505.             $data['code_tax'] = $cust_info->code_tax;
  506.             $data['address'] = $cust_info->address_1;
  507.             $data['account_number'] = $cust_info->account_number;
  508.         }
  509.         $data['sale_id'] = $sale_id;
  510.         $word = $this->input->get('baogiabutton');
  511.         $cat_baogia = $this->input->get("cat_baogia");
  512.         $data['word'] = $word;
  513.         $data['cat_baogia'] = $cat_baogia;
  514.         if ($word == 0) {
  515.             $this->load->view("sales/report_quotes", $data);
  516.         } else {
  517.             $file_name = "BG_" . $sale_id . "_" . str_replace(" ", "", replace_character($data['customer'])) . "_" . date('dmYHis') . ".doc";
  518.             $fp = fopen("excel_materials/" . $file_name, 'w+');
  519.             $arr_item = array();
  520.             $arr_service = array();
  521.             foreach ($data['cart'] as $line => $val) {
  522.                 if ($val['item_id']) {
  523.                     $info_item = $this->Item->get_info($val['item_id']);
  524.                     if ($info_item->service == 0) {
  525.                         $arr_item[] = array(
  526.                             'item_id' => $val['item_id'],
  527.                             'line' => $line,
  528.                             'name' => $val['name'],
  529.                             'item_number' => $val['item_number'],
  530.                             'description' => $val['description'],
  531.                             'serialnumber' => $val['serialnumber'],
  532.                             'allow_alt_description' => $val['allow_alt_description'],
  533.                             'is_serialized' => $val['is_serialized'],
  534.                             'quantity' => $val['quantity'],
  535.                             'stored_id' => $val['stored_id'],
  536.                             'discount' => $val['discount'],
  537.                             'price' => $val['price'],
  538.                             'price_rate' => $val['price_rate'],
  539.                             'taxes' => $val['taxes'],
  540.                             'unit' => $val['unit']
  541.                         );
  542.                     } else {
  543.                         $arr_service[] = array(
  544.                             'item_id' => $val['item_id'],
  545.                             'line' => $line,
  546.                             'name' => $val['name'],
  547.                             'item_number' => $val['item_number'],
  548.                             'description' => $val['description'],
  549.                             'serialnumber' => $val['serialnumber'],
  550.                             'allow_alt_description' => $val['allow_alt_description'],
  551.                             'is_serialized' => $val['is_serialized'],
  552.                             'quantity' => $val['quantity'],
  553.                             'stored_id' => $val['stored_id'],
  554.                             'discount' => $val['discount'],
  555.                             'price' => $val['price'],
  556.                             'price_rate' => $val['price_rate'],
  557.                             'taxes' => $val['taxes'],
  558.                             'unit' => $val['unit']
  559.                         );
  560.                     }
  561.                 } else {
  562.                     $arr_item[] = array(
  563.                         'pack_id' => $val['pack_id'],
  564.                         'line' => $val['line'],
  565.                         'pack_number' => $val['pack_number'],
  566.                         'name' => $val['name'],
  567.                         'description' => $val['description'],
  568.                         'quantity' => $val['quantity'],
  569.                         'discount' => $val['discount'],
  570.                         'price' => $val['price'],
  571.                         'taxes' => $val['taxes'],
  572.                         'unit' => $val['unit']
  573.                     );
  574.                 }
  575.             }
  576.             $str = "";
  577.             $str .= "<table style='border-collapse: collapse; width: 100%; margin: 0px auto; font-size: 14px;'>";
  578.             $str .= "<tr>";
  579.             $str .= "<th style='text-align: center; border: 1px solid #000000; padding: 10px 0px;'>STT</th>";
  580.             $str .= "<th style='text-align: center; border: 1px solid #000000; padding: 10px 0px;'>Mã/Tên HH, DC, Gói SP</th>";
  581.             $str .= "<th style='text-align: center; border: 1px solid #000000; padding: 10px 0px;' colspan='2'>Mô tả/Hình ảnh</th>";
  582.             $str .= "<th style='text-align: center; border: 1px solid #000000; padding: 10px 0px;'>ĐVT</th>";
  583.             $str .= "<th style='text-align: center; border: 1px solid #000000; padding: 10px 0px;'>SL</th>";
  584.             $str .= "<th style='text-align: center; border: 1px solid #000000; padding: 10px 0px;'>Đơn giá</th>";
  585.             $str .= "<th style='text-align: center; border: 1px solid #000000; padding: 10px 0px;'>CK(%)</th>";
  586.             $str .= "<th style='text-align: center; border: 1px solid #000000; padding: 10px 0px;'>Thuế(%)</th>";
  587.             $str .= "<th style='text-align: center; border: 1px solid #000000; padding: 10px 0px;'>Thành tiền</th>";
  588.             $str .= "</tr>";
  589.             $str .= "<tr>";
  590.             $str .= "<td style='text-align: center; border: 1px solid #000000; font-style: italic; padding: 5px 0px; width: 5%;'>(No.)</td>";
  591.             $str .= "<td style='text-align: center; border: 1px solid #000000; font-style: italic; padding: 5px 0px; width: 17.5%;'>(Code/Name)</td>";
  592.             $str .= "<td style='text-align: center; border: 1px solid #000000; font-style: italic; padding: 5px 0px; width: 17.5%;'>(Description)</td>";
  593.             $str .= "<td style='text-align: center; border: 1px solid #000000; font-style: italic; padding: 5px 0px; width: 10%;'>(Images)</td>";
  594.             $str .= "<td style='text-align: center; border: 1px solid #000000; font-style: italic; padding: 5px 0px; width: 10%;'>(Units)</td>";
  595.             $str .= "<td style='text-align: center; border: 1px solid #000000; font-style: italic; padding: 5px 0px; width: 10%;'>(Quantity)</td>";
  596.             $str .= "<td style='text-align: center; border: 1px solid #000000; font-style: italic; padding: 5px 0px; width: 10%;'>(Unit price)</td>";
  597.             $str .= "<td style='text-align: center; border: 1px solid #000000; font-style: italic; padding: 5px 0px; width: 5%;'>(Discount)</td>";
  598.             $str .= "<td style='text-align: center; border: 1px solid #000000; font-style: italic; padding: 5px 0px; width: 5%;'>(Tax)</td>";
  599.             $str .= "<td style='text-align: center; border: 1px solid #000000; font-style: italic; padding: 5px 0px; width: 10%;'>(Amount)</td>";
  600.             $str .= "</tr>";
  601.             $stt = 1;
  602.             $total = 0;
  603.             if ($cat_baogia == 1) {
  604.                 foreach ($arr_item as $line => $item) {
  605.                     if ($item['pack_id']) {
  606.                         $info_pack = $this->Pack->get_info($item['pack_id']);
  607.                         $pack_item = $this->Pack_items->get_info($item['pack_id']);
  608.                         $info_sale_pack = $this->Sale->get_sale_pack_by_sale_pack($sale_id, $item['pack_id']);
  609.                         $info_unit = $this->Unit->get_info($info_sale_pack->unit_pack);
  610.                         $thanh_tien = $item['quantity'] * $item['price'] - $item['quantity'] * $item['price'] * $item['discount'] / 100 + ($item['quantity'] * $item['price'] - $item['quantity'] * $item['price'] * $item['discount'] / 100) * $item['taxes'] / 100;
  611.                         $str .= "<tr>";
  612.                         $str .= "<td style='text-align: center; border: 1px solid #000000; padding: 10px 5px'>" . $stt . "</td>";
  613.                         $str .= "<td style='border: 1px solid #000000; padding: 10px 5px'>";
  614.                         $str .= "<strong>" . $info_pack->pack_number . "/" . $info_pack->name . "(Gói SP)</strong><br>";
  615.                         foreach ($pack_item as $val) {
  616.                             $info_item = $this->Item->get_info($val->item_id);
  617.                             $str .= "<p>- <strong>" . $info_item->item_number . "</strong>/" . $info_item->name . "</p>";
  618.                         }
  619.  
  620.                         $str .= "</td>";
  621.                         $str .= "<td style='border: 1px solid #000000; padding: 10px 5px'>" . $item['description'] . "</td>";
  622.                         $str .= "<td style='text-align: center; border: 1px solid #000000; padding: 10px 5px'>";
  623.                         if ($info_pack->images) {
  624.                             $str .= "<img src='" . base_url('packs/' . $info_pack->images) . "' style='width:45px; height:45px'/>";
  625.                         }
  626.                         $str .= "</td>";
  627.                         $str .= "<td style='border: 1px solid #000000; padding: 10px 5px'>" . $info_unit->name . "</td>";
  628.                         $str .= "<td style='text-align: right; border: 1px solid #000000; padding: 10px 5px'>" . format_quantity($item['quantity']) . "</td>";
  629.                         $str .= "<td style='text-align: right; border: 1px solid #000000; padding: 10px 5px'>" . number_format($item['price']) . "</td>";
  630.                         $str .= "<td style='text-align: right; border: 1px solid #000000; padding: 10px 5px'>" . number_format($item['discount']) . "</td>";
  631.                         $str .= "<td style='text-align: right; border: 1px solid #000000; padding: 10px 5px'>" . number_format($item['taxes']) . "</td>";
  632.                         $str .= "<td style='text-align: right; border: 1px solid #000000; padding: 10px 5px'>" . number_format($thanh_tien) . "</td>";
  633.                         $str .= "</tr>";
  634.                         $stt++;
  635.                         $total += $thanh_tien;
  636.                     } else {
  637.                         $info_item = $this->Item->get_info($item['item_id']);
  638.                         $info_sale_item = $this->Sale->get_sale_item_by_sale_item($sale_id, $item['item_id']);
  639.                         $info_unit = $this->Unit->get_info($info_sale_item->unit_item);
  640.                         $thanh_tien = $item['quantity'] * ($item['unit'] == 'unit_from' ? $item['price_rate'] : $item['price']) - $item['quantity'] * ($item['unit'] == 'unit_from' ? $item['price_rate'] : $item['price']) * $item['discount'] / 100 + ($item['quantity'] * ($item['unit'] == 'unit_from' ? $item['price_rate'] : $item['price']) - $item['quantity'] * ($item['unit'] == 'unit_from' ? $item['price_rate'] : $item['price']) * $item['discount'] / 100) * $item['taxes'] / 100;
  641.                         $str .= "<tr>";
  642.                         $str .= "<td style='text-align: center; border: 1px solid #000000; padding: 10px 5px'>" . $stt . "</td>";
  643.                         $str .= "<td style='border: 1px solid #000000; padding: 10px 5px'><strong>" . $info_item->item_number . "</strong>/" . $info_item->name . "</td>";
  644.                         $str .= "<td style='border: 1px solid #000000; padding: 10px 5px'>" . $item['description'] . "</td>";
  645.                         $str .= "<td style='text-align: center; border: 1px solid #000000; padding: 10px 5px'>";
  646.                         if ($info_item->images) {
  647.                             $str .= "<img src='" . base_url('item/' . $info_item->images) . "' style='width:45px; height:45px'/>";
  648.                         }
  649.                         $str .= "</td>";
  650.                         $str .= "<td style='border: 1px solid #000000; padding: 10px 5px'>" . $info_unit->name . "</td>";
  651.                         $str .= "<td style='text-align: right; border: 1px solid #000000; padding: 10px 5px'>" . format_quantity($item['quantity']) . "</td>";
  652.                         $str .= "<td style='text-align: right; border: 1px solid #000000; padding: 10px 5px'>" . number_format(($item['unit'] == 'unit_from' ? $item['price_rate'] : $item['price'])) . "</td>";
  653.                         $str .= "<td style='text-align: right; border: 1px solid #000000; padding: 10px 5px'>" . number_format($item['discount']) . "</td>";
  654.                         $str .= "<td style='text-align: right; border: 1px solid #000000; padding: 10px 5px'>" . number_format($item['taxes']) . "</td>";
  655.                         $str .= "<td style='text-align: right; border: 1px solid #000000; padding: 10px 5px'>" . number_format($thanh_tien) . "</td>";
  656.                         $str .= "</tr>";
  657.                         $stt++;
  658.                         $total += $thanh_tien;
  659.                     }
  660.                 }
  661.             } else if ($cat_baogia == 2) {
  662.                 foreach ($arr_service as $line => $item) {
  663.                     $info_item = $this->Item->get_info($item['item_id']);
  664.                     $info_sale_item = $this->Sale->get_sale_item_by_sale_item($sale_id, $item['item_id']);
  665.                     $info_unit = $this->Unit->get_info($info_sale_item->unit_item);
  666.                     $thanh_tien = $item['quantity'] * $item['price'] - $item['quantity'] * $item['price'] * $item['discount'] / 100 + ($item['quantity'] * $item['price'] - $item['quantity'] * $item['price'] * $item['discount'] / 100) * $item['taxes'] / 100;
  667.                     $str .= "<tr>";
  668.                     $str .= "<td style='text-align: center; border: 1px solid #000000; padding: 10px 5px'>" . $stt . "</td>";
  669.                     $str .= "<td style='border: 1px solid #000000; padding: 10px 5px'><strong>" . $info_item->item_number . "</strong/" . $info_item->name . "</td>";
  670.                     $str .= "<td style='border: 1px solid #000000; padding: 10px 5px'>" . $item['description'] . "</td>";
  671.                     $str .= "<td style='text-align: center; border: 1px solid #000000; padding: 10px 5px'>";
  672.                     if ($info_item->images) {
  673.                         $str .= "<img src='" . base_url('item/' . $info_item->images) . "' style='width:45px; height:45px'/>";
  674.                     }
  675.                     $str .= "</td>";
  676.                     $str .= "<td style='border: 1px solid #000000; padding: 10px 5px'>" . $info_unit->name . "</td>";
  677.                     $str .= "<td style='text-align: right; border: 1px solid #000000; padding: 10px 5px'>" . format_quantity($item['quantity']) . "</td>";
  678.                     $str .= "<td style='text-align: right; border: 1px solid #000000; padding: 10px 5px'>" . number_format($item['price']) . "</td>";
  679.                     $str .= "<td style='text-align: right; border: 1px solid #000000; padding: 10px 5px'>" . number_format($item['discount']) . "</td>";
  680.                     $str .= "<td style='text-align: right; border: 1px solid #000000; padding: 10px 5px'>" . number_format($item['taxes']) . "</td>";
  681.                     $str .= "<td style='text-align: right; border: 1px solid #000000; padding: 10px 5px'>" . number_format($thanh_tien) . "</td>";
  682.                     $str .= "</tr>";
  683.                     $stt ++;
  684.                     $total += $thanh_tien;
  685.                 }
  686.             } else {
  687.                 foreach ($data['cart'] as $line => $item) {
  688.                     if ($item['pack_id']) {
  689.                         $info_pack = $this->Pack->get_info($item['pack_id']);
  690.                         $pack_item = $this->Pack_items->get_info($item['pack_id']);
  691.                         $info_sale_pack = $this->Sale->get_sale_pack_by_sale_pack($sale_id, $item['pack_id']);
  692.                         $info_unit = $this->Unit->get_info($info_sale_pack->unit_pack);
  693.                         $thanh_tien = $item['quantity'] * $item['price'] - $item['quantity'] * $item['price'] * $item['discount'] / 100 + ($item['quantity'] * $item['price'] - $item['quantity'] * $item['price'] * $item['discount'] / 100) * $item['taxes'] / 100;
  694.                         $str .= "<tr>";
  695.                         $str .= "<td style='text-align: center; border: 1px solid #000000; padding: 10px 5px'>" . $stt . "</td>";
  696.                         $str .= "<td style='border: 1px solid #000000; padding: 10px 5px'>";
  697.                         $str .= "<strong>" . $info_pack->pack_number . "/" . $info_pack->name . "(Gói SP)</strong><br>";
  698.                         foreach ($pack_item as $val) {
  699.                             $info_item = $this->Item->get_info($val->item_id);
  700.                             $str .= "<p>- <strong>" . $info_item->item_number . "</strong>/" . $info_item->name . "</p>";
  701.                         }
  702.  
  703.                         $str .= "</td>";
  704.                         $str .= "<td style='border: 1px solid #000000; padding: 10px 5px'>" . $item['description'] . "</td>";
  705.                         $str .= "<td style='text-align: center; border: 1px solid #000000; padding: 10px 5px'>";
  706.                         if ($info_pack->images) {
  707.                             $str .= "<img src='" . base_url('packs/' . $info_pack->images) . "' width='20px' height='20px'/>";
  708.                         }
  709.                         $str .= "</td>";
  710.                         $str .= "<td style='border: 1px solid #000000; padding: 10px 5px'>" . $info_unit->name . "</td>";
  711.                         $str .= "<td style='text-align: right; border: 1px solid #000000; padding: 10px 5px'>" . format_quantity($item['quantity']) . "</td>";
  712.                         $str .= "<td style='text-align: right; border: 1px solid #000000; padding: 10px 5px'>" . number_format($item['price']) . "</td>";
  713.                         $str .= "<td style='text-align: right; border: 1px solid #000000; padding: 10px 5px'>" . number_format($item['discount']) . "</td>";
  714.                         $str .= "<td style='text-align: right; border: 1px solid #000000; padding: 10px 5px'>" . number_format($item['taxes']) . "</td>";
  715.                         $str .= "<td style='text-align: right; border: 1px solid #000000; padding: 10px 5px'>" . number_format($thanh_tien) . "</td>";
  716.                         $str .= "</tr>";
  717.                         $total += $thanh_tien;
  718.                     } else {
  719.                         $info_item = $this->Item->get_info($item['item_id']);
  720.                         $info_sale_item = $this->Sale->get_sale_item_by_sale_item($sale_id, $item['item_id']);
  721.                         $info_unit = $this->Unit->get_info($info_sale_item->unit_item);
  722.                         $thanh_tien = $item['quantity'] * ($item['unit'] == 'unit_from' ? $item['price_rate'] : $item['price']) - $item['quantity'] * ($item['unit'] == 'unit_from' ? $item['price_rate'] : $item['price']) * $item['discount'] / 100 + ($item['quantity'] * ($item['unit'] == 'unit_from' ? $item['price_rate'] : $item['price']) - $item['quantity'] * ($item['unit'] == 'unit_from' ? $item['price_rate'] : $item['price']) * $item['discount'] / 100) * $item['taxes'] / 100;
  723.                         $str .= "<tr>";
  724.                         $str .= "<td style='text-align: center; border: 1px solid #000000; padding: 10px 5px'>" . $stt . "</td>";
  725.                         $str .= "<td style='border: 1px solid #000000; padding: 10px 5px'><strong>" . $info_item->item_number . "</strong>/" . $info_item->name . "</td>";
  726.                         $str .= "<td style='border: 1px solid #000000; padding: 10px 5px'>" . $item['description'] . "</td>";
  727.                         $str .= "<td style='text-align: center; border: 1px solid #000000; padding: 10px 5px'>";
  728.                         if ($info_item->images) {
  729.                             $str .= "<img src='" . base_url('item/' . $info_item->images) . "' width='20px' height='20px'/>";
  730.                         }
  731.                         $str .= "</td>";
  732.                         $str .= "<td style='border: 1px solid #000000; padding: 10px 5px'>" . $info_unit->name . "</td>";
  733.                         $str .= "<td style='text-align: right; border: 1px solid #000000; padding: 10px 5px'>" . format_quantity($item['quantity']) . "</td>";
  734.                         $str .= "<td style='text-align: right; border: 1px solid #000000; padding: 10px 5px'>" . number_format(($item['unit'] == 'unit_from' ? $item['price_rate'] : $item['price'])) . "</td>";
  735.                         $str .= "<td style='text-align: right; border: 1px solid #000000; padding: 10px 5px'>" . number_format($item['discount']) . "</td>";
  736.                         $str .= "<td style='text-align: right; border: 1px solid #000000; padding: 10px 5px'>" . number_format($item['taxes']) . "</td>";
  737.                         $str .= "<td style='text-align: right; border: 1px solid #000000; padding: 10px 5px'>" . number_format($thanh_tien) . "</td>";
  738.                         $str .= "</tr>";
  739.                         $total += $thanh_tien;
  740.                     }
  741.                     $stt++;
  742.                 }
  743.             }
  744.             $str .= "<tr>";
  745.             $str .= "<td colspan='5' style='text-align: center; border: 1px solid #000000; padding: 10px 5px; font-weight: bold'>Tổng</td>";
  746.             $str .= "<td colspan='5' style='text-align: right; border: 1px solid #000000; padding: 10px 5px; font-weight: bold'>" . number_format($total) . "</td>";
  747.             $str .= "</tr>";
  748.             $str .= "</table>";
  749.             $str .= "<p>Tổng giá trị (Bằng chữ): <strong><em>" . $this->Cost->get_string_number($total) . "</em></strong></p>";
  750.             $content1 = "<html>";
  751.             $content1 .= "<meta charset='utf-8'/>";
  752.             $content1 .= "<body style='font-size: 100% !important'>";
  753.             $content1 .= $data['info_quotes_contract']->content_quotes_contract;
  754.             $content1 .= "</body>";
  755.             $content1 .= "</html>";
  756.             $info_sale = $this->Sale->get_info_sale_order($sale_id);
  757.             $d = $info_sale->date_debt != '0000-00-00' ? date('d', strtotime($info_sale->date_debt)) : '...';
  758.             $m = $info_sale->date_debt != '0000-00-00' ? date('m', strtotime($info_sale->date_debt)) : '...';
  759.             $y = $info_sale->date_debt != '0000-00-00' ? date('Y', strtotime($info_sale->date_debt)) : '...';
  760.             $content1 = str_replace('{TITLE}', $data['info_quotes_contract']->title_quotes_contract, $content1);
  761.             $content1 = str_replace('{TABLE_DATA}', $str, $content1);
  762.             $content1 = str_replace('{LOGO}', "<img src='" . base_url('images/logoreport/' . $this->config->item('report_logo')) . "'/>", $content1);
  763.             $content1 = str_replace('{TEN_NCC}', $this->config->item('company'), $content1);
  764.             $content1 = str_replace('{DIA_CHI_NCC}', $this->config->item('address'), $content1);
  765.             $content1 = str_replace('{SDT_NCC}', $this->config->item('phone'), $content1);
  766.             $content1 = str_replace('{DD_NCC}', $this->config->item('corp_master_account'), $content1);
  767.             $content1 = str_replace('{CHUCVU_NCC}', '', $content1);
  768.             $content1 = str_replace('{TKNH_NCC}', $this->config->item('corp_number_account'), $content1);
  769.             $content1 = str_replace('{NH_NCC}', $this->config->item('corp_bank_name'), $content1);
  770.             $content1 = str_replace('{TEN_KH}', $data['cus_name'], $content1);
  771.             $content1 = str_replace('{DIA_CHI_KH}', $data['address'], $content1);
  772.             $content1 = str_replace('{SDT_KH}', '', $content1);
  773.             $content1 = str_replace('{DD_KH}', $data['customer'], $content1);
  774.             $content1 = str_replace('{CHUCVU_KH}', $data['positions'], $content1);
  775.             $content1 = str_replace('{TKNH_KH}', $data['code_tax'], $content1);
  776.             $content1 = str_replace('{NH_KH}', '', $content1);
  777.             $content1 = str_replace('{CODE}', $sale_id, $content1);
  778.             $content1 = str_replace('{DATE}', $d, $content1);
  779.             $content1 = str_replace('{MONTH}', $m, $content1);
  780.             $content1 = str_replace('{YEAR}', $y, $content1);
  781.             fwrite($fp, $content1);
  782.             fclose($fp);
  783.  
  784.             /* phan lam mail */
  785.             $cust_info = $this->Customer->get_info($customer_id);
  786.             $config = Array(
  787.                 'protocol' => 'smtp',
  788.                 'smtp_host' => 'ssl://smtp.googlemail.com',
  789.                 'smtp_port' => 465,
  790.                 'smtp_user' => $this->config->item('email'),
  791.                 'smtp_pass' => $this->config->item('pass_email'),
  792.                 'charset' => 'utf-8',
  793.                 'mailtype' => 'html'
  794.             );
  795.             $this->load->library('email', $config);
  796.             $this->email->set_newline("\r\n");
  797.             $this->email->from($this->config->item('email'), $this->config->item('company'));
  798.             $this->email->to($cust_info->email);
  799.             $this->email->subject($this->config->item('company') . " xin chân trọng gửi tới quý khách thư báo giá");
  800.             $content = "<p>Dear anh/chị:" . $data['customer'] . "</p>";
  801.             $content .= "<p>Dựa vào nhu cầu của Quý khách hàng.</p>";
  802.             $content .= "<p><b>" . $this->config->item('company') . "</b> xin phép được gửi tới Quý khách hàng báo giá chi tiết như sau:</p>";
  803.             $content .= "<p>Xin vui lòng xem ở file đính kèm</p>";
  804.             $content .= "<p><i>Để biết thêm thông tin, vui lòng liên hệ Dịch vụ khách hàng theo số điện thoại: " . $this->config->item("phone") . "</i></p>";
  805.             $content .= "<i>(Xin vui lòng không phản hồi email này. Đây là email được tự động gửi đi từ hệ thống của chúng tôi).</i>";
  806.             $content .= "<p>-----</p>";
  807.             $content .= "<p><i>Thanks and Regards!</i></p>";
  808.             if ($sale_info['employees_id'] != 0) {
  809.                 $content .= "<p><i>" . $data['employees_id'] . "</i></p>";
  810.                 $content .= "<p>Mobile: " . $data['phone_number1'] . "</p>";
  811.                 $content .= "<p>Email: " . $data['email1'] . "</p>";
  812.             } else {
  813.                 $content .= "<p><i>" . $data['employee'] . "</i></p>";
  814.                 $content .= "<p>Mobile: " . $data['phone_number'] . "</p>";
  815.                 $content .= "<p>Email: " . $data['email'] . "</p>";
  816.             }
  817.             $content .= "<p style='text-transform: uppercase;'>" . $this->config->item("company") . "</p>";
  818.             $content .= "<p>Rep Off  :" . $this->config->item('address') . "</p>";
  819.             $content .= "<p>Email    :" . $this->config->item('email') . "</p>";
  820.             $content .= "<p>Tel      :" . $this->config->item('phone') . " | Fax: " . $this->config->item('fax') . "</p>";
  821.             $content .= "<p>Web      :" . $this->config->item('website') . "</p>";
  822.             $this->email->message($content);
  823.             $file = APPPATH . "/../excel_materials/" . $file_name;
  824.             $this->email->attach($file);
  825.             if ($this->email->send()) {
  826.                 $send_success[] = $cust_info->email;
  827.                 $data = array(
  828.                     'sale_id' => $sale_id,
  829.                     'name' => $file_name,
  830.                 );
  831.                 $this->Sale->insert_sale_material($data);
  832.                 $data_history = array(
  833.                     'person_id' => $customer_id,
  834.                     'employee_id' => $this->session->userdata('person_id'),
  835.                     'title' => 'Báo giá',
  836.                     'content' => $content,
  837.                     'time' => date('Y-m-d H:i:s'),
  838.                     'file' => $file_name,
  839.                     'status' => 1,
  840.                 );
  841.                 $this->Customer->add_mail_history($data_history);
  842.                 $this->sale_lib->clear_all();
  843.                 redirect('sales');
  844.             } else {
  845.                 $data_history = array(
  846.                     'person_id' => $customer_id,
  847.                     'employee_id' => $this->session->userdata('person_id'),
  848.                     'title' => 'Báo giá',
  849.                     'content' => $content,
  850.                     'time' => date('Y-m-d H:i:s'),
  851.                     'file' => $file_name,
  852.                     'status' => 0,
  853.                 );
  854.                 $this->Customer->add_mail_history($data_history);
  855.                 $send_fail[] = $cust_info->email;
  856.                 show_error($this->email->print_debugger());
  857.             }
  858.             /* end phan lam mail */
  859.         }
  860.         $this->sale_lib->clear_all();
  861.     }
  862.  
  863.     function baogia1($sale_id) {
  864.  
  865.         $data['is_sale'] = FALSE;
  866.         $sale_info = $this->Sale->get_info($sale_id)->row_array();
  867.  
  868.         $this->sale_lib->copy_entire_sale($sale_id);
  869.  
  870.         $data['cart'] = $this->sale_lib->get_cart();
  871.  
  872.         $data['payments'] = $this->sale_lib->get_payments();
  873.  
  874.         $data['subtotal'] = $this->sale_lib->get_subtotal();
  875.  
  876.         $data['taxes'] = $this->sale_lib->get_taxes($sale_id);
  877.  
  878.         $data['total'] = $this->sale_lib->get_total($sale_id);
  879.  
  880.         $data['receipt_title'] = lang('sales_receipt');
  881.  
  882.         $data['comment'] = $this->Sale->get_comment($sale_id);
  883.  
  884.         $data['show_comment_on_receipt'] = $this->Sale->get_comment_on_receipt($sale_id);
  885.  
  886.         $data['transaction_time'] = date(get_date_format() . ' ' . get_time_format(), strtotime($sale_info['sale_time']));
  887.  
  888.         $customer_id = $this->sale_lib->get_customer();
  889.  
  890.         $emp_info = $this->Employee->get_info($sale_info['employee_id']);
  891. //nhân viên báo giá
  892.         $info_empss = $this->Employee->get_info($sale_info['employees_id']);
  893.         $data['employees_id'] = $info_empss->first_name . ' ' . $info_empss->last_name;
  894.  
  895.         $data['payment_type'] = $sale_info['payment_type'];
  896.  
  897.         $data['amount_change'] = $this->sale_lib->get_amount_due($sale_id) * -1;
  898.  
  899.         $data['employee'] = $emp_info->first_name . ' ' . $emp_info->last_name;
  900.  
  901.         $data['ref_no'] = $sale_info['cc_ref_no'];
  902.  
  903.         $this->load->helper('string');
  904.  
  905.         $data['payment_type'] = str_replace(array('<sup>VNĐ</sup><br />', ''), ' .VNĐ', $sale_info['payment_type']);
  906.  
  907.         $data['amount_due'] = $this->sale_lib->get_amount_due();
  908.  
  909.         foreach ($data['payments'] as $payment_id => $payment) {
  910.  
  911.             $payment_amount = $payment['payment_amount'];
  912.         }
  913.  
  914.         $k = 28;
  915.  
  916.         $tongtienhang = 0;
  917.  
  918.         foreach (array_reverse($data['cart'], true) as $line => $item) {
  919.  
  920.             $tongtienhang_1 += $item['price'] * $item['quantity'] - $item['price'] * $item['quantity'] * $item['discount'] / 100;
  921.  
  922.             $k++;
  923.         }
  924.  
  925.         $payments_cost = $tongtienhang_1 - $payment_amount;
  926.  
  927.         if ($customer_id != -1) {
  928.  
  929.             $cust_info = $this->Customer->get_info($customer_id);
  930.  
  931.             $data['customer'] = $cust_info->first_name . ' ' . $cust_info->last_name;
  932.  
  933.             $data['cus_name'] = $cust_info->company_name == '' ? '' : $cust_info->company_name;
  934.  
  935.             $data['code_tax'] = $cust_info->code_tax;
  936.  
  937.             $data['address'] = $cust_info->address_1;
  938.  
  939.             $data['account_number'] = $cust_info->account_number;
  940.         }
  941.  
  942.         $data['sale_id'] = 'VH ' . $sale_id;
  943.  
  944. // $this->load->view("sales/receipt",$data);
  945. // $this->sale_lib->clear_all();
  946.  
  947.         $cities = $this->City->get_all();
  948.  
  949.         $excel = $this->input->get('baogiabutton');
  950.  
  951.         if ($excel == 1) {
  952.  
  953. //require_once APPPATH . "/third_party/Classes/export_baogia.php";
  954.             /* lam bao gia mau excel */
  955.             $excel_info = $this->Template->get_info_cat(2);
  956.             $excel_link = $excel_info->link;
  957.             $excel_name_cus = $excel_info->name_cus;
  958.             $excel_add_cus = $excel_info->add_cus;
  959.             $this->load->library('Excel');
  960.             $objPHPExcel = new PHPExcel();
  961. //error_reporting(E_ALL);
  962. //ini_set('display_errors', TRUE);
  963. //ini_set('display_startup_errors', TRUE);
  964.             define('EOL', (PHP_SAPI == 'cli') ? PHP_EOL : '<br />');
  965.             $link = base_url() . "application/controllers/sales.xlsx";
  966.             date_default_timezone_set('Europe/London');
  967.             print_r(str_replace('.php', '.xlsx', pathinfo(__FILE__, PATHINFO_BASENAME)));
  968.             require_once APPPATH . '/third_party/Classes/PHPExcel.php';
  969.             echo date('H:i:s'), " Load Excel2007 template file", EOL;
  970.             $objReader = PHPExcel_IOFactory::createReader('Excel2007');
  971.             $objPHPExcel = $objReader->load("$excel_link");
  972.             $objPHPExcel->getActiveSheet()->setCellValue($excel_name_cus, $data['customer']);
  973.             $objPHPExcel->getActiveSheet()->setCellValue($excel_add_cus, $data['address']);
  974.             $k_row = 25;
  975.             $stt = 1;
  976.             $tongtienhang = 0;
  977.             foreach (array_reverse($data['cart'], true) as $line => $item) {
  978.                 $objPHPExcel->getActiveSheet()->setCellValue('B' . $k_row, $stt);
  979.                 $objPHPExcel->getActiveSheet()->setCellValue('C' . $k_row, $item['name']);
  980.                 $objPHPExcel->getActiveSheet()->setCellValue('F' . $k_row, $this->Unit->item_unit($this->Item->get_info($item['item_id'])->unit)->name);
  981.                 $objPHPExcel->getActiveSheet()->getStyle('F' . $k_row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  982.                 $objPHPExcel->getActiveSheet()->setCellValue('G' . $k_row, $item['quantity']);
  983.                 $objPHPExcel->getActiveSheet()->setCellValue('H' . $k_row, to_currency_unVND_nomar($item['price']));
  984.                 $objPHPExcel->getActiveSheet()->setCellValue('I' . $k_row, to_currency_unVND_nomar($item['price'] * $item['quantity'] - $item['price'] * $item['quantity'] * $item['discount'] / 100));
  985.                 $this->excel->getActiveSheet()->getStyle('I' . $k_row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
  986.                 $tongtienhang += $item['price'] * $item['quantity'] - $item['price'] * $item['quantity'] * $item['discount'] / 100;
  987.                 $k_row++;
  988.                 $stt++;
  989.                 $objPHPExcel->getActiveSheet()->insertNewRowBefore($k_row, 1);
  990.                 $objPHPExcel->setActiveSheetIndex(0)->mergeCells('C' . $k_row . ':E' . $k_row);
  991.                 $objPHPExcel->getActiveSheet()->getStyle('C' . $k_row)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
  992.             }
  993.             echo date('H:i:s'), " Write to Excel5 format", EOL;
  994.             $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
  995.             $objWriter->save(str_replace('.php', '.xlsx', __FILE__));
  996.             echo str_replace('.php', '.xlsx', pathinfo(__FILE__, PATHINFO_BASENAME)), EOL;
  997.             echo "<a href=$link>tai ve</a>";
  998.             /* end lam bao gia excel */
  999.         } else {
  1000.  
  1001.             /* phan lam mail */
  1002.  
  1003.             $customer_ids = array($customer_id);
  1004.             $mail_id = $this->input->post('mail_id');
  1005.  
  1006.             $mail_info = $this->Customer->get_info_mail($mail_id);
  1007.  
  1008.             $ses = new SimpleEmailService($this->config->item('amazon_access_key'), $this->config->item('amazon_secret_key'));
  1009.  
  1010.             $ses->enableVerifyHost(false);
  1011.  
  1012.             $ses->enableVerifyPeer(false);
  1013. //get list_email of customer from multi ids
  1014.  
  1015.             $list_customer_email = array();
  1016.  
  1017.             $send_success = array();
  1018.  
  1019.             $send_fail = array();
  1020.             foreach ($customer_ids as $c_ids) {
  1021.  
  1022.                 if ($this->Customer->get_info($c_ids)->email != '') {
  1023.  
  1024.                     $list_customer_email[] = $this->Customer->get_info($c_ids)->email;
  1025.  
  1026.                     $list_customer = $this->Customer->get_info($c_ids);
  1027.  
  1028.  
  1029.  
  1030.                     $m = new SimpleEmailServiceMessage();
  1031.  
  1032.                     $m->setFrom('gs.daycon@gmail.com');
  1033.  
  1034.                     $m->addCC($this->config->item('email'));
  1035.  
  1036. //set mail title
  1037.  
  1038.                     $m->setSubject("Công ty " . $this->config->item('company') . " xin chân trọng gửi tới quý khách " . $list_customer->first_name . " thư báo giá");
  1039.  
  1040.                     $m->addTo($this->Customer->get_info($c_ids)->email);
  1041.  
  1042.  
  1043.  
  1044. //set mall content
  1045.  
  1046.                     $content = $this->load->view('sales/baogia', $data, true);
  1047.  
  1048.                     $content = str_replace('__FIRST_NAME__', $list_customer->first_name, $content);
  1049.  
  1050.                     $content = str_replace('__LAST_NAME__', $list_customer->last_name, $content);
  1051.  
  1052.                     $content = str_replace('__PHONE_NUMBER__', $list_customer->phone_number, $content);
  1053.  
  1054.                     $content = str_replace('__EMAIL__', $list_customer->email, $content);
  1055.  
  1056.  
  1057.  
  1058.                     $m->setMessageFromString('', $content);
  1059.  
  1060.                     if ($ses->sendEmail($m) === false) {
  1061.  
  1062.                         $send_fail[] = $this->Customer->get_info($c_ids)->email;
  1063.  
  1064. //echo json_encode(array('success'=>false,'message'=>'Không gửi được mail'));
  1065.                     } else {
  1066.  
  1067.                         $send_success[] = $this->Customer->get_info($c_ids)->email;
  1068.  
  1069.                         $this->sale_lib->clear_all();
  1070.  
  1071.                         redirect('sales');
  1072.                     }
  1073.                 }
  1074.             }
  1075.  
  1076.             /* end phan lam mail */
  1077.         }
  1078.  
  1079.         $this->sale_lib->clear_all();
  1080.     }
  1081.  
  1082. // phan lam hop dong
  1083.     function contract($sale_id) {
  1084.         $id_quotes_contract = $this->input->get("contract");
  1085.         $data['info_quotes_contract'] = $this->M_quotes_contract->get_info($id_quotes_contract);
  1086.         $data['is_sale'] = FALSE;
  1087.         $sale_info = $this->Sale->get_info($sale_id)->row_array();
  1088.         $this->sale_lib->copy_entire_sale($sale_id);
  1089.         $data['cart'] = $this->sale_lib->get_cart();
  1090.         $data['payments'] = $this->sale_lib->get_payments();
  1091.         $data['subtotal'] = $this->sale_lib->get_subtotal();
  1092.         $data['taxes'] = $this->sale_lib->get_taxes($sale_id);
  1093.         $data['total'] = $this->sale_lib->get_total($sale_id);
  1094.         $data['receipt_title'] = lang('sales_receipt');
  1095.         $data['comment'] = $this->Sale->get_comment($sale_id);
  1096.         $data['show_comment_on_receipt'] = $this->Sale->get_comment_on_receipt($sale_id);
  1097.         $data['transaction_time'] = date(get_date_format() . ' ' . get_time_format(), strtotime($sale_info['sale_time']));
  1098.         $customer_id = $this->sale_lib->get_customer();
  1099.         $emp_info = $this->Employee->get_info($sale_info['employee_id']);
  1100.         $info_empss = $this->Employee->get_info($sale_info['employees_id']);
  1101.         $data['payment_type'] = $sale_info['payment_type'];
  1102.         $data['amount_change'] = $this->sale_lib->get_amount_due($sale_id) * -1;
  1103.         $data['employee'] = $emp_info->first_name . ' ' . $emp_info->last_name;
  1104.         $data['phone'] = $emp_info->phone_number;
  1105.         $data['email'] = $emp_info->email;
  1106.         $data['ref_no'] = $sale_info['cc_ref_no'];
  1107.         $this->load->helper('string');
  1108.         $data['payment_type'] = str_replace(array('<sup>VNĐ</sup><br />', ''), ' .VNĐ', $sale_info['payment_type']);
  1109.         $data['amount_due'] = $this->sale_lib->get_amount_due();
  1110.         foreach ($data['payments'] as $payment_id => $payment) {
  1111.             $payment_amount = $payment['payment_amount'];
  1112.         }
  1113.         $k = 28;
  1114.         $tongtienhang = 0;
  1115.         foreach (array_reverse($data['cart'], true) as $line => $item) {
  1116.             $tongtienhang_1 += $item['price'] * $item['quantity'] - $item['price'] * $item['quantity'] * $item['discount'] / 100;
  1117.             $k++;
  1118.         }
  1119.         $payments_cost = $tongtienhang_1 - $payment_amount;
  1120.         if ($customer_id != -1) {
  1121.             $cust_info = $this->Customer->get_info($customer_id);
  1122.             $data['customer'] = $cust_info->first_name . ' ' . $cust_info->last_name;
  1123.             $data['cus_name'] = $cust_info->company_name == '' ? '' : $cust_info->company_name;
  1124.             $data['code_tax'] = $cust_info->code_tax;
  1125.             $data['address'] = $cust_info->address_1;
  1126.             $data['account_number'] = $cust_info->account_number;
  1127.             $data['positions'] = $cust_info->positions;
  1128.         }
  1129.         $data['sale_id'] = $sale_id;
  1130.         $cities = $this->City->get_all();
  1131.         $word = $this->input->get('hopdongbutton');
  1132.         $cat_hopdong = $this->input->get("cat_hopdong");
  1133.         $data['word'] = $word;
  1134.         $data['cat_hopdong'] = $cat_hopdong;
  1135.         if ($word == 0) {
  1136.             $this->load->view("sales/report_contract", $data);
  1137.         } else {
  1138.             $file_name = "HD_" . $sale_id . "_" . str_replace(" ", "", replace_character($data['customer'])) . "_" . date('dmYHis') . ".doc";
  1139.             $fp = fopen("excel_materials/" . $file_name, 'w+');
  1140.             $arr_item = array();
  1141.             $arr_service = array();
  1142.             foreach ($data['cart'] as $line => $val) {
  1143.                 if ($val['item_id']) {
  1144.                     $info_item = $this->Item->get_info($val['item_id']);
  1145.                     if ($info_item->service == 0) {
  1146.                         $arr_item[] = array(
  1147.                             'item_id' => $val['item_id'],
  1148.                             'line' => $line,
  1149.                             'name' => $val['name'],
  1150.                             'item_number' => $val['item_number'],
  1151.                             'description' => $val['description'],
  1152.                             'serialnumber' => $val['serialnumber'],
  1153.                             'allow_alt_description' => $val['allow_alt_description'],
  1154.                             'is_serialized' => $val['is_serialized'],
  1155.                             'quantity' => $val['quantity'],
  1156.                             'stored_id' => $val['stored_id'],
  1157.                             'discount' => $val['discount'],
  1158.                             'price' => $val['price'],
  1159.                             'price_rate' => $val['price_rate'],
  1160.                             'taxes' => $val['taxes'],
  1161.                             'unit' => $val['unit']
  1162.                         );
  1163.                     } else {
  1164.                         $arr_service[] = array(
  1165.                             'item_id' => $val['item_id'],
  1166.                             'line' => $line,
  1167.                             'name' => $val['name'],
  1168.                             'item_number' => $val['item_number'],
  1169.                             'description' => $val['description'],
  1170.                             'serialnumber' => $val['serialnumber'],
  1171.                             'allow_alt_description' => $val['allow_alt_description'],
  1172.                             'is_serialized' => $val['is_serialized'],
  1173.                             'quantity' => $val['quantity'],
  1174.                             'stored_id' => $val['stored_id'],
  1175.                             'discount' => $val['discount'],
  1176.                             'price' => $val['price'],
  1177.                             'price_rate' => $val['price_rate'],
  1178.                             'taxes' => $val['taxes'],
  1179.                             'unit' => $val['unit']
  1180.                         );
  1181.                     }
  1182.                 } else {
  1183.                     $arr_item[] = array(
  1184.                         'pack_id' => $val['pack_id'],
  1185.                         'line' => $val['line'],
  1186.                         'pack_number' => $val['pack_number'],
  1187.                         'name' => $val['name'],
  1188.                         'description' => $val['description'],
  1189.                         'quantity' => $val['quantity'],
  1190.                         'discount' => $val['discount'],
  1191.                         'price' => $val['price'],
  1192.                         'taxes' => $val['taxes'],
  1193.                         'unit' => $val['unit']
  1194.                     );
  1195.                 }
  1196.             }
  1197.             $str .= "<table style='width: 100%; border-collapse: collapse'>";
  1198.             $str .= "<tr>";
  1199.             $str .= "<th style='text-align: center; border: 1px solid #000; padding: 8px 0px; width: 5%'>STT</th>";
  1200.             $str .= "<th style='text-align: center; border: 1px solid #000; padding: 8px 0px; width: 30%'>Tên hàng</th>";
  1201.             $str .= "<th style='text-align: center; border: 1px solid #000; padding: 8px 0px; width: 5%'>ĐVT</th>";
  1202.             $str .= "<th style='text-align: center; border: 1px solid #000; padding: 8px 0px; width: 8%'>SL</th>";
  1203.             $str .= "<th style='text-align: center; border: 1px solid #000; padding: 8px 0px; width: 14%'>Đơn giá</th>";
  1204.             $str .= "<th style='text-align: center; border: 1px solid #000; padding: 8px 0px; width: 14%'>CK(%)</th>";
  1205.             $str .= "<th style='text-align: center; border: 1px solid #000; padding: 8px 0px; width: 14%'>Thuế(%)</th>";
  1206.             $str .= "<th style='text-align: center; border: 1px solid #000; padding: 8px 0px; width: 14%'>Thành tiền</th>";
  1207.             $str .= "</tr>";
  1208.  
  1209.             $stt = 1;
  1210.             $total = 0;
  1211.             if ($cat_hopdong == 1) {
  1212.                 foreach ($arr_item as $line => $item) {
  1213.                     if ($item['pack_id']) {
  1214.                         $info_pack = $this->Pack->get_info($item['pack_id']);
  1215.                         $pack_item = $this->Pack_items->get_info($item['pack_id']);
  1216.                         $info_sale_pack = $this->Sale->get_sale_pack_by_sale_pack($sale_id, $item['pack_id']);
  1217.                         $info_unit = $this->Unit->get_info($info_sale_pack->unit_pack);
  1218.                         $thanh_tien = $item['quantity'] * $item['price'] - $item['quantity'] * $item['price'] * $item['discount'] / 100 + ($item['quantity'] * $item['price'] - $item['quantity'] * $item['price'] * $item['discount'] / 100) * $item['taxes'] / 100;
  1219.                         $str .= "<tr>";
  1220.                         $str .= "<td style='text-align: center; border: 1px solid #000000; padding: 10px 5px'>" . $stt . "</td>";
  1221.                         $str .= "<td style='border: 1px solid #000000; padding: 10px 5px'>";
  1222.                         $str .= "<strong>" . $info_pack->pack_number . "/" . $info_pack->name . "(Gói SP)</strong><br>";
  1223.                         foreach ($pack_item as $val) {
  1224.                             $info_item = $this->Item->get_info($val->item_id);
  1225.                             $str .= "<p>- <strong>" . $info_item->item_number . "</strong>/" . $info_item->name . "</p>";
  1226.                         }
  1227.  
  1228.                         $str .= "</td>";
  1229.                         $str .= "<td style='border: 1px solid #000000; padding: 10px 5px'>" . $info_unit->name . "</td>";
  1230.                         $str .= "<td style='text-align: right; border: 1px solid #000000; padding: 10px 5px'>" . format_quantity($item['quantity']) . "</td>";
  1231.                         $str .= "<td style='text-align: right; border: 1px solid #000000; padding: 10px 5px'>" . number_format($item['price']) . "</td>";
  1232.                         $str .= "<td style='text-align: right; border: 1px solid #000000; padding: 10px 5px'>" . number_format($item['discount']) . "</td>";
  1233.                         $str .= "<td style='text-align: right; border: 1px solid #000000; padding: 10px 5px'>" . number_format($item['taxes']) . "</td>";
  1234.                         $str .= "<td style='text-align: right; border: 1px solid #000000; padding: 10px 5px'>" . number_format($thanh_tien) . "</td>";
  1235.                         $str .= "</tr>";
  1236.                         $total += $thanh_tien;
  1237.                     } else {
  1238.                         $info_item = $this->Item->get_info($item['item_id']);
  1239.                         $info_sale_item = $this->Sale->get_sale_item_by_sale_item($sale_id, $item['item_id']);
  1240.                         $info_unit = $this->Unit->get_info($info_sale_item->unit_item);
  1241.                         $thanh_tien = $item['quantity'] * ($item['unit'] == 'unit_from' ? $item['price_rate'] : $item['price']) - $item['quantity'] * ($item['unit'] == 'unit_from' ? $item['price_rate'] : $item['price']) * $item['discount'] / 100 + ($item['quantity'] * ($item['unit'] == 'unit_from' ? $item['price_rate'] : $item['price']) - $item['quantity'] * ($item['unit'] == 'unit_from' ? $item['price_rate'] : $item['price']) * $item['discount'] / 100) * $item['taxes'] / 100;
  1242.                         $str .= "<tr>";
  1243.                         $str .= "<td style='text-align: center; border: 1px solid #000000; padding: 10px 5px'>" . $stt . "</td>";
  1244.                         $str .= "<td style='border: 1px solid #000000; padding: 10px 5px'><strong>" . $info_item->item_number . "</strong>/" . $info_item->name . "</td>";
  1245.                         $str .= "<td style='border: 1px solid #000000; padding: 10px 5px'>" . $info_unit->name . "</td>";
  1246.                         $str .= "<td style='text-align: right; border: 1px solid #000000; padding: 10px 5px'>" . format_quantity($item['quantity']) . "</td>";
  1247.                         $str .= "<td style='text-align: right; border: 1px solid #000000; padding: 10px 5px'>" . number_format(($item['unit'] == 'unit_from' ? $item['price_rate'] : $item['price'])) . "</td>";
  1248.                         $str .= "<td style='text-align: right; border: 1px solid #000000; padding: 10px 5px'>" . number_format($item['discount']) . "</td>";
  1249.                         $str .= "<td style='text-align: right; border: 1px solid #000000; padding: 10px 5px'>" . number_format($item['taxes']) . "</td>";
  1250.                         $str .= "<td style='text-align: right; border: 1px solid #000000; padding: 10px 5px'>" . number_format($thanh_tien) . "</td>";
  1251.                         $str .= "</tr>";
  1252.                         $total += $thanh_tien;
  1253.                     }
  1254.                     $stt++;
  1255.                 }
  1256.             } else if ($cat_hopdong == 2) {
  1257.                 foreach ($arr_service as $line => $item) {
  1258.                     $info_item = $this->Item->get_info($item['item_id']);
  1259.                     $info_sale_item = $this->Sale->get_sale_item_by_sale_item($sale_id, $item['item_id']);
  1260.                     $info_unit = $this->Unit->get_info($info_sale_item->unit_item);
  1261.                     $thanh_tien = $item['quantity'] * $item['price'] - $item['quantity'] * $item['price'] * $item['discount'] / 100 + ($item['quantity'] * $item['price'] - $item['quantity'] * $item['price'] * $item['discount'] / 100) * $item['taxes'] / 100;
  1262.                     $str .= "<tr>";
  1263.                     $str .= "<td style='text-align: center; border: 1px solid #000000; padding: 10px 5px'>" . $stt . "</td>";
  1264.                     $str .= "<td style='border: 1px solid #000000; padding: 10px 5px'><strong>" . $info_item->item_number . "</strong>/" . $info_item->name . "</td>";
  1265.                     $str .= "<td style='border: 1px solid #000000; padding: 10px 5px'>" . $info_unit->name . "</td>";
  1266.                     $str .= "<td style='text-align: right; border: 1px solid #000000; padding: 10px 5px'>" . format_quantity($item['quantity']) . "</td>";
  1267.                     $str .= "<td style='text-align: right; border: 1px solid #000000; padding: 10px 5px'>" . number_format($item['price']) . "</td>";
  1268.                     $str .= "<td style='text-align: right; border: 1px solid #000000; padding: 10px 5px'>" . number_format($item['discount']) . "</td>";
  1269.                     $str .= "<td style='text-align: right; border: 1px solid #000000; padding: 10px 5px'>" . number_format($item['taxes']) . "</td>";
  1270.                     $str .= "<td style='text-align: right; border: 1px solid #000000; padding: 10px 5px'>" . number_format($thanh_tien) . "</td>";
  1271.                     $str .= "</tr>";
  1272.                     $total += $thanh_tien;
  1273.                     $stt++;
  1274.                 }
  1275.             } else {
  1276.                 foreach ($data['cart'] as $line => $item) {
  1277.                     if ($item['pack_id']) {
  1278.                         $info_pack = $this->Pack->get_info($item['pack_id']);
  1279.                         $pack_item = $this->Pack_items->get_info($item['pack_id']);
  1280.                         $info_sale_pack = $this->Sale->get_sale_pack_by_sale_pack($sale_id, $item['pack_id']);
  1281.                         $info_unit = $this->Unit->get_info($info_sale_pack->unit_pack);
  1282.                         $thanh_tien = $item['quantity'] * $item['price'] - $item['quantity'] * $item['price'] * $item['discount'] / 100 + ($item['quantity'] * $item['price'] - $item['quantity'] * $item['price'] * $item['discount'] / 100) * $item['taxes'] / 100;
  1283.                         $str .= "<tr>";
  1284.                         $str .= "<td style='text-align: center; border: 1px solid #000000; padding: 10px 5px'>" . $stt . "</td>";
  1285.                         $str .= "<td style='border: 1px solid #000000; padding: 10px 5px'>";
  1286.                         $str .= "<strong>" . $info_pack->pack_number . "/" . $info_pack->name . "(Gói SP)</strong><br>";
  1287.                         foreach ($pack_item as $val) {
  1288.                             $info_item = $this->Item->get_info($val->item_id);
  1289.                             $str .= "<p>- <strong>" . $info_item->item_number . "</strong>/" . $info_item->name . "</p>";
  1290.                         }
  1291.  
  1292.                         $str .= "</td>";
  1293.                         $str .= "<td style='border: 1px solid #000000; padding: 10px 5px'>" . $info_unit->name . "</td>";
  1294.                         $str .= "<td style='text-align: right; border: 1px solid #000000; padding: 10px 5px'>" . format_quantity($item['quantity']) . "</td>";
  1295.                         $str .= "<td style='text-align: right; border: 1px solid #000000; padding: 10px 5px'>" . number_format($item['price']) . "</td>";
  1296.                         $str .= "<td style='text-align: right; border: 1px solid #000000; padding: 10px 5px'>" . number_format($item['discount']) . "</td>";
  1297.                         $str .= "<td style='text-align: right; border: 1px solid #000000; padding: 10px 5px'>" . number_format($item['taxes']) . "</td>";
  1298.                         $str .= "<td style='text-align: right; border: 1px solid #000000; padding: 10px 5px'>" . number_format($thanh_tien) . "</td>";
  1299.                         $str .= "</tr>";
  1300.                         $total += $thanh_tien;
  1301.                     } else {
  1302.                         $info_item = $this->Item->get_info($item['item_id']);
  1303.                         $info_sale_item = $this->Sale->get_sale_item_by_sale_item($sale_id, $item['item_id']);
  1304.                         $info_unit = $this->Unit->get_info($info_sale_item->unit_item);
  1305.                         $thanh_tien = $item['quantity'] * ($item['unit'] == 'unit_from' ? $item['price_rate'] : $item['price']) - $item['quantity'] * ($item['unit'] == 'unit_from' ? $item['price_rate'] : $item['price']) * $item['discount'] / 100 + ($item['quantity'] * ($item['unit'] == 'unit_from' ? $item['price_rate'] : $item['price']) - $item['quantity'] * ($item['unit'] == 'unit_from' ? $item['price_rate'] : $item['price']) * $item['discount'] / 100) * $item['taxes'] / 100;
  1306.                         $str .= "<tr>";
  1307.                         $str .= "<td style='text-align: center; border: 1px solid #000000; padding: 10px 5px'>" . $stt . "</td>";
  1308.                         $str .= "<td style='border: 1px solid #000000; padding: 10px 5px'><strong>" . $info_item->item_number . "</strong>/" . $info_item->name . "</td>";
  1309.                         $str .= "<td style='border: 1px solid #000000; padding: 10px 5px'>" . $info_unit->name . "</td>";
  1310.                         $str .= "<td style='text-align: right; border: 1px solid #000000; padding: 10px 5px'>" . format_quantity($item['quantity']) . "</td>";
  1311.                         $str .= "<td style='text-align: right; border: 1px solid #000000; padding: 10px 5px'>" . number_format(($item['unit'] == 'unit_from' ? $item['price_rate'] : $item['price'])) . "</td>";
  1312.                         $str .= "<td style='text-align: right; border: 1px solid #000000; padding: 10px 5px'>" . number_format($item['discount']) . "</td>";
  1313.                         $str .= "<td style='text-align: right; border: 1px solid #000000; padding: 10px 5px'>" . number_format($item['taxes']) . "</td>";
  1314.                         $str .= "<td style='text-align: right; border: 1px solid #000000; padding: 10px 5px'>" . number_format($thanh_tien) . "</td>";
  1315.                         $str .= "</tr>";
  1316.                         $total += $thanh_tien;
  1317.                     }
  1318.                     $stt++;
  1319.                 }
  1320.             }
  1321.             $str .= "<tr>";
  1322.             $str .= "<td colspan='3' style='text-align: center; font-weight: bold; border: 1px solid #000000; padding: 10px 5px'>Tổng</td>";
  1323.             $str .= "<td colspan='5' style='text-align: right; font-weight: bold; border: 1px solid #000000; padding: 10px 5px'>" . number_format($total) . "</td>";
  1324.             $str .= "</tr>";
  1325.             $str .= "</table>";
  1326.             $str .= "<p>Tổng giá trị (Bằng chữ): <strong><em>" . $this->Cost->get_string_number($total) . "</em></strong></p>";
  1327.             $content1 = "<html>";
  1328.             $content1 .= "<meta charset='utf-8'/>";
  1329.             $content1 .= "<body style='font-size: 100% !important'>";
  1330.             $content1 .= $data['info_quotes_contract']->content_quotes_contract;
  1331.             $content1 .= "</body>";
  1332.             $content1 .= "</html>";
  1333.             $info_sale = $this->Sale->get_info_sale_order($sale_id);
  1334.             $d = $info_sale->date_debt != '0000-00-00' ? date('d', strtotime($info_sale->date_debt)) : '...';
  1335.             $m = $info_sale->date_debt != '0000-00-00' ? date('m', strtotime($info_sale->date_debt)) : '...';
  1336.             $y = $info_sale->date_debt != '0000-00-00' ? date('Y', strtotime($info_sale->date_debt)) : '...';
  1337.             $content1 = str_replace('{TITLE}', $data['info_quotes_contract']->title_quotes_contract, $content1);
  1338.             $content1 = str_replace('{TABLE_DATA}', $str, $content1);
  1339.             $content1 = str_replace('{LOGO}', "<img src='" . base_url('images/logoreport/' . $this->config->item('report_logo')) . "'/>", $content1);
  1340.             $content1 = str_replace('{TEN_NCC}', $this->config->item('company'), $content1);
  1341.             $content1 = str_replace('{DIA_CHI_NCC}', $this->config->item('address'), $content1);
  1342.             $content1 = str_replace('{SDT_NCC}', $this->config->item('phone'), $content1);
  1343.             $content1 = str_replace('{DD_NCC}', $this->config->item('corp_master_account'), $content1);
  1344.             $content1 = str_replace('{CHUCVU_NCC}', '', $content1);
  1345.             $content1 = str_replace('{TKNH_NCC}', $this->config->item('corp_number_account'), $content1);
  1346.             $content1 = str_replace('{NH_NCC}', $this->config->item('corp_bank_name'), $content1);
  1347.             $content1 = str_replace('{TEN_KH}', $data['cus_name'], $content1);
  1348.             $content1 = str_replace('{DIA_CHI_KH}', $data['address'], $content1);
  1349.             $content1 = str_replace('{SDT_KH}', '', $content1);
  1350.             $content1 = str_replace('{DD_KH}', $data['customer'], $content1);
  1351.             $content1 = str_replace('{CHUCVU_KH}', $data['positions'], $content1);
  1352.             $content1 = str_replace('{TKNH_KH}', $data['code_tax'], $content1);
  1353.             $content1 = str_replace('{NH_KH}', '', $content1);
  1354.             $content1 = str_replace('{CODE}', $sale_id, $content1);
  1355.             $content1 = str_replace('{DATE}', $d, $content1);
  1356.             $content1 = str_replace('{MONTH}', $m, $content1);
  1357.             $content1 = str_replace('{YEAR}', $y, $content1);
  1358.             fwrite($fp, $content1);
  1359.             fclose($fp);
  1360.             /* phan lam mail */
  1361.             $cust_info = $this->Customer->get_info($customer_id);
  1362.             $config = Array(
  1363.                 'protocol' => 'smtp',
  1364.                 'smtp_host' => 'ssl://smtp.googlemail.com',
  1365.                 'smtp_port' => 465,
  1366.                 'smtp_user' => $this->config->item('email'),
  1367.                 'smtp_pass' => $this->config->item('pass_email'),
  1368.                 'charset' => 'utf-8',
  1369.                 'mailtype' => 'html'
  1370.             );
  1371.             $this->load->library('email', $config);
  1372.             $this->email->set_newline("\r\n");
  1373.             $this->email->from($this->config->item('email'), $this->config->item('company'));
  1374.             $this->email->to($cust_info->email);
  1375.             $this->email->subject($this->config->item('company') . " xin chân trọng gửi tới quý khách hợp đồng");
  1376.             $content = "<p>Dear anh/chị:" . $data['customer'] . "</p>";
  1377.             $content .= "<p>Dựa vào nhu cầu của Quý khách hàng.</p>";
  1378.             $content .= "<p><b>" . $this->config->item('company') . "</b> xin phép được gửi tới Quý khách hàng hợp đồng chi tiết như sau:</p>";
  1379.             $content .= "<p>Xin vui lòng xem ở file đính kèm</p>";
  1380.             $content .= "<p><i>Để biết thêm thông tin, vui lòng liên hệ Dịch vụ khách hàng theo số điện thoại: " . $this->config->item("phone") . "</i></p>";
  1381.             $content .= "<i>(Xin vui lòng không phản hồi email này. Đây là email được tự động gửi đi từ hệ thống của chúng tôi).</i>";
  1382.             $content .= "<p>-----</p>";
  1383.             $content .= "<p><i>Thanks and Regards!</i></p>";
  1384.             $content .= "<p><i>" . $data['employee'] . "</i></p>";
  1385.             $content .= "<p>Mobile: " . $data['phone'] . "</p>";
  1386.             $content .= "<p>Email: " . $data['email'] . "</p>";
  1387.  
  1388.             $content .= "------------------------------------------------------------------------";
  1389.             $content .= "<img src='" . base_url() . "images/logoreport/11.png'>";
  1390.             $content .= "<p style='text-transform: uppercase;'>" . $this->config->item("company") . "</p>";
  1391.             $content .= "<p>Rep Off  :" . $this->config->item('address') . "</p>";
  1392.             $content .= "<p>Email    :" . $this->config->item('email') . "</p>";
  1393.             $content .= "<p>Tel      :" . $this->config->item('phone') . " | Fax: " . $this->config->item('fax') . "</p>";
  1394.             $content .= "<p>Web      :" . $this->config->item('website') . "</p>";
  1395.             $this->email->message($content);
  1396.             $file = APPPATH . "/../excel_materials/" . $file_name;
  1397.             $this->email->attach($file);
  1398.             if ($this->email->send()) {
  1399.                 $send_success[] = $cust_info->email;
  1400.                 $data_history = array(
  1401.                     'person_id' => $customer_id,
  1402.                     'employee_id' => $this->session->userdata('person_id'),
  1403.                     'title' => 'Hợp đồng',
  1404.                     'content' => $content,
  1405.                     'time' => date('Y-m-d H:i:s'),
  1406.                     'file' => $file_name,
  1407.                     'status' => 1,
  1408.                 );
  1409.                 $this->Customer->add_mail_history($data_history);
  1410.                 $this->sale_lib->clear_all();
  1411.                 redirect('sales');
  1412.             } else {
  1413.                 $send_fail[] = $cust_info->email;
  1414.                 $data_history = array(
  1415.                     'person_id' => $customer_id,
  1416.                     'employee_id' => $this->session->userdata('person_id'),
  1417.                     'title' => 'Hợp đồng',
  1418.                     'content' => $content,
  1419.                     'time' => date('Y-m-d H:i:s'),
  1420.                     'file' => $file_name,
  1421.                     'status' => 0,
  1422.                 );
  1423.                 $this->Customer->add_mail_history($data_history);
  1424.                 show_error($this->email->print_debugger());
  1425.             }
  1426.             /* end phan lam mail */
  1427.         }
  1428.         $this->sale_lib->clear_all();
  1429.     }
  1430.  
  1431.     function contract1($sale_id) {
  1432.         $data['is_sale'] = FALSE;
  1433.  
  1434.         $sale_info = $this->Sale->get_info($sale_id)->row_array();
  1435.  
  1436.         $this->sale_lib->copy_entire_sale($sale_id);
  1437.  
  1438.         $data['cart'] = $this->sale_lib->get_cart();
  1439.  
  1440.         $data['payments'] = $this->sale_lib->get_payments();
  1441.  
  1442.         $data['subtotal'] = $this->sale_lib->get_subtotal();
  1443.  
  1444.         $data['taxes'] = $this->sale_lib->get_taxes($sale_id);
  1445.  
  1446.         $data['total'] = $this->sale_lib->get_total($sale_id);
  1447.  
  1448.         $data['receipt_title'] = lang('sales_receipt');
  1449.  
  1450.         $data['comment'] = $this->Sale->get_comment($sale_id);
  1451.  
  1452.         $data['show_comment_on_receipt'] = $this->Sale->get_comment_on_receipt($sale_id);
  1453.  
  1454.         $data['transaction_time'] = date(get_date_format() . ' ' . get_time_format(), strtotime($sale_info['sale_time']));
  1455.  
  1456.         $customer_id = $this->sale_lib->get_customer();
  1457.  
  1458.         $emp_info = $this->Employee->get_info($sale_info['employee_id']);
  1459.  
  1460.         $data['payment_type'] = $sale_info['payment_type'];
  1461.  
  1462.         $data['amount_change'] = $this->sale_lib->get_amount_due($sale_id) * -1;
  1463.  
  1464.         $data['employee'] = $emp_info->first_name . ' ' . $emp_info->last_name;
  1465.  
  1466.         $data['ref_no'] = $sale_info['cc_ref_no'];
  1467.  
  1468.         $this->load->helper('string');
  1469.  
  1470.         $data['payment_type'] = str_replace(array('<sup>VNĐ</sup><br />', ''), ' .VNĐ', $sale_info['payment_type']);
  1471.  
  1472.         $data['amount_due'] = $this->sale_lib->get_amount_due();
  1473.  
  1474.         foreach ($data['payments'] as $payment_id => $payment) {
  1475.  
  1476.             $payment_amount = $payment['payment_amount'];
  1477.         }
  1478.  
  1479.         $k = 28;
  1480.  
  1481.         $tongtienhang = 0;
  1482.  
  1483.         foreach (array_reverse($data['cart'], true) as $line => $item) {
  1484.  
  1485.             $tongtienhang_1 += $item['price'] * $item['quantity'] - $item['price'] * $item['quantity'] * $item['discount'] / 100;
  1486.  
  1487.             $k++;
  1488.         }
  1489.  
  1490.         $payments_cost = $tongtienhang_1 - $payment_amount;
  1491.  
  1492.         if ($customer_id != -1) {
  1493.  
  1494.             $cust_info = $this->Customer->get_info($customer_id);
  1495.  
  1496.             $data['customer'] = $cust_info->first_name . ' ' . $cust_info->last_name;
  1497.  
  1498.             $data['cus_name'] = $cust_info->company_name == '' ? '' : $cust_info->company_name;
  1499.  
  1500.             $data['code_tax'] = $cust_info->code_tax;
  1501.  
  1502.             $data['address'] = $cust_info->address_1;
  1503.  
  1504.             $data['account_number'] = $cust_info->account_number;
  1505.         }
  1506.  
  1507.         $data['sale_id'] = 'VH ' . $sale_id;
  1508.  
  1509. // $this->load->view("sales/receipt",$data);
  1510. // $this->sale_lib->clear_all();
  1511.  
  1512.         $cities = $this->City->get_all();
  1513.  
  1514.         $excel = $this->input->get('hopdongbutton');
  1515.  
  1516.  
  1517.         if ($excel == 1) {
  1518.  
  1519.             require_once APPPATH . "/third_party/Classes/export_contract_in.php";
  1520.         } elseif ($excel == 2) {
  1521.             require_once APPPATH . "/third_party/Classes/export_contract_may.php";
  1522.         } elseif ($excel == 3) {
  1523.             require_once APPPATH . "/third_party/Classes/export_contract_bang.php";
  1524.         } else {
  1525.  
  1526.             /* phan lam mail */
  1527.             $customer_ids = array($customer_id);
  1528.             $mail_id = $this->input->post('mail_id');
  1529.             $mail_info = $this->Customer->get_info_mail($mail_id);
  1530.             $ses = new SimpleEmailService($this->config->item('amazon_access_key'), $this->config->item('amazon_secret_key'));
  1531.             $ses->enableVerifyHost(false);
  1532.             $ses->enableVerifyPeer(false);
  1533. //get list_email of customer from multi ids
  1534.             $list_customer_email = array();
  1535.             $send_success = array();
  1536.             $send_fail = array();
  1537.             foreach ($customer_ids as $c_ids) {
  1538.                 if ($this->Customer->get_info($c_ids)->email != '') {
  1539.                     $list_customer_email[] = $this->Customer->get_info($c_ids)->email;
  1540.                     $list_customer = $this->Customer->get_info($c_ids);
  1541.                     $m = new SimpleEmailServiceMessage();
  1542.                     $m->setFrom('gs.daycon@gmail.com');
  1543. //set mail title
  1544.                     $m->setSubject("Công ty " . $this->config->item('company') . " xin chân trọng gửi tới quý khách " . $list_customer->first_name . " thư hợp đồng");
  1545.                     $m->addTo($this->Customer->get_info($c_ids)->email);
  1546.                     $m->addCC($this->config->item('email'));
  1547. //set mall content
  1548.                     $content = $this->load->view('sales/contract', $data, true);
  1549.                     $content = str_replace('__FIRST_NAME__', $list_customer->first_name, $content);
  1550.                     $content = str_replace('__LAST_NAME__', $list_customer->last_name, $content);
  1551.                     $content = str_replace('__PHONE_NUMBER__', $list_customer->phone_number, $content);
  1552.                     $content = str_replace('__EMAIL__', $list_customer->email, $content);
  1553.                     $m->setMessageFromString('', $content);
  1554.                     if ($ses->sendEmail($m) === false) {
  1555.                         $send_fail[] = $this->Customer->get_info($c_ids)->email;
  1556. //echo json_encode(array('success'=>false,'message'=>'Không gửi được mail'));
  1557.                     } else {
  1558.                         $send_success[] = $this->Customer->get_info($c_ids)->email;
  1559.                         $this->sale_lib->clear_all();
  1560.                         redirect('sales');
  1561.                     }
  1562.                 }
  1563.             }
  1564.             /* end phan lam mail */
  1565.         }
  1566.         $this->sale_lib->clear_all();
  1567.     }
  1568.  
  1569. // phan lam biên bản
  1570.     function bienban($sale_id) {
  1571.         $data['is_sale'] = FALSE;
  1572.  
  1573.         $sale_info = $this->Sale->get_info($sale_id)->row_array();
  1574.         $this->sale_lib->copy_entire_sale($sale_id);
  1575.         $data['cart'] = $this->sale_lib->get_cart();
  1576.         $data['payments'] = $this->sale_lib->get_payments();
  1577.         $data['subtotal'] = $this->sale_lib->get_subtotal();
  1578.         $data['taxes'] = $this->sale_lib->get_taxes($sale_id);
  1579.         $data['total'] = $this->sale_lib->get_total($sale_id);
  1580.         $data['receipt_title'] = lang('sales_receipt');
  1581.         $data['comment'] = $this->Sale->get_comment($sale_id);
  1582.         $data['show_comment_on_receipt'] = $this->Sale->get_comment_on_receipt($sale_id);
  1583.         $data['transaction_time'] = date(get_date_format() . ' ' . get_time_format(), strtotime($sale_info['sale_time']));
  1584.         $customer_id = $this->sale_lib->get_customer();
  1585.         $emp_info = $this->Employee->get_info($sale_info['employee_id']);
  1586.         $data['payment_type'] = $sale_info['payment_type'];
  1587.         $data['amount_change'] = $this->sale_lib->get_amount_due($sale_id) * -1;
  1588.         $data['employee'] = $emp_info->first_name . ' ' . $emp_info->last_name;
  1589.         $data['ref_no'] = $sale_info['cc_ref_no'];
  1590.         $this->load->helper('string');
  1591.         $data['payment_type'] = str_replace(array('<sup>VNĐ</sup><br />', ''), ' .VNĐ', $sale_info['payment_type']);
  1592.         $data['amount_due'] = $this->sale_lib->get_amount_due();
  1593.         foreach ($data['payments'] as $payment_id => $payment) {
  1594.             $payment_amount = $payment['payment_amount'];
  1595.         }
  1596.         $k = 28;
  1597.         $tongtienhang = 0;
  1598.         foreach (array_reverse($data['cart'], true) as $line => $item) {
  1599.             $tongtienhang_1 += $item['price'] * $item['quantity'] - $item['price'] * $item['quantity'] * $item['discount'] / 100;
  1600.             $k++;
  1601.         }
  1602.         $payments_cost = $tongtienhang_1 - $payment_amount;
  1603.         if ($customer_id != -1) {
  1604.             $cust_info = $this->Customer->get_info($customer_id);
  1605.             $data['customer'] = $cust_info->first_name . ' ' . $cust_info->last_name;
  1606.             $data['cus_name'] = $cust_info->company_name == '' ? '' : $cust_info->company_name;
  1607.             $data['code_tax'] = $cust_info->code_tax;
  1608.             $data['address'] = $cust_info->address_1;
  1609.             $data['account_number'] = $cust_info->account_number;
  1610.             $data['code_tax'] = $cust_info->code_tax;
  1611.             $data['positions'] = $cust_info->positions;
  1612.         }
  1613.         $data['sale_id'] = 'VH ' . $sale_id;
  1614. // $this->load->view("sales/receipt",$data);
  1615. // $this->sale_lib->clear_all();
  1616.         $cities = $this->City->get_all();
  1617. // $excel = $this->input->get('hopdongbutton');
  1618. // if ($excel == 1){
  1619.         require_once APPPATH . "/third_party/Classes/export_nghiemthu.php";
  1620. //  }
  1621. // else {
  1622.         /* phan lam mail */
  1623.         $customer_ids = array($customer_id);
  1624.  
  1625.         $mail_id = $this->input->post('mail_id');
  1626.         $mail_info = $this->Customer->get_info_mail($mail_id);
  1627.  
  1628.  
  1629.         $ses = new SimpleEmailService($this->config->item('amazon_access_key'), $this->config->item('amazon_secret_key'));
  1630.         $ses->enableVerifyHost(false);
  1631.         $ses->enableVerifyPeer(false);
  1632.  
  1633. //get list_email of customer from multi ids
  1634.         $list_customer_email = array();
  1635.         $send_success = array();
  1636.         $send_fail = array();
  1637.  
  1638.         foreach ($customer_ids as $c_ids) {
  1639.             if ($this->Customer->get_info($c_ids)->email != '') {
  1640.                 $list_customer_email[] = $this->Customer->get_info($c_ids)->email;
  1641.                 $list_customer = $this->Customer->get_info($c_ids);
  1642.  
  1643.                 $m = new SimpleEmailServiceMessage();
  1644.                 $m->setFrom('gs.daycon@gmail.com');
  1645. //set mail title
  1646.                 $m->setSubject("Công ty " . $this->config->item('company') . " xin chân trọng gửi tới quý khách " . $list_customer->first_name . " thư hợp đồng");
  1647.                 $m->addTo($this->Customer->get_info($c_ids)->email);
  1648.                 $m->addCC($this->config->item('email'));
  1649.  
  1650. //set mall content
  1651.                 $content = $this->load->view('sales/contract', $data, true);
  1652.                 $content = str_replace('__FIRST_NAME__', $list_customer->first_name, $content);
  1653.                 $content = str_replace('__LAST_NAME__', $list_customer->last_name, $content);
  1654.                 $content = str_replace('__PHONE_NUMBER__', $list_customer->phone_number, $content);
  1655.                 $content = str_replace('__EMAIL__', $list_customer->email, $content);
  1656.  
  1657.                 $m->setMessageFromString('', $content);
  1658.                 if ($ses->sendEmail($m) === false) {
  1659.                     $send_fail[] = $this->Customer->get_info($c_ids)->email;
  1660. //echo json_encode(array('success'=>false,'message'=>'Không gửi được mail'));
  1661.                 } else {
  1662.                     $send_success[] = $this->Customer->get_info($c_ids)->email;
  1663.                     $this->sale_lib->clear_all();
  1664.                     redirect('sales');
  1665.                 }
  1666.             }
  1667. //  }
  1668.             /* end phan lam mail */
  1669.         }
  1670.         $this->sale_lib->clear_all();
  1671.     }
  1672.  
  1673. // phan lam thanh lý hợp đồng
  1674.     function thanhly($sale_id) {
  1675.         $data['is_sale'] = FALSE;
  1676.  
  1677.         $sale_info = $this->Sale->get_info($sale_id)->row_array();
  1678.         $this->sale_lib->copy_entire_sale($sale_id);
  1679.         $data['cart'] = $this->sale_lib->get_cart();
  1680.         $data['payments'] = $this->sale_lib->get_payments();
  1681.         $data['subtotal'] = $this->sale_lib->get_subtotal();
  1682.         $data['taxes'] = $this->sale_lib->get_taxes($sale_id);
  1683.         $data['total'] = $this->sale_lib->get_total($sale_id);
  1684.         $data['receipt_title'] = lang('sales_receipt');
  1685.         $data['comment'] = $this->Sale->get_comment($sale_id);
  1686.         $data['show_comment_on_receipt'] = $this->Sale->get_comment_on_receipt($sale_id);
  1687.         $data['transaction_time'] = date(get_date_format() . ' ' . get_time_format(), strtotime($sale_info['sale_time']));
  1688.         $customer_id = $this->sale_lib->get_customer();
  1689.         $emp_info = $this->Employee->get_info($sale_info['employee_id']);
  1690.         $data['payment_type'] = $sale_info['payment_type'];
  1691.         $data['amount_change'] = $this->sale_lib->get_amount_due($sale_id) * -1;
  1692.         $data['employee'] = $emp_info->first_name . ' ' . $emp_info->last_name;
  1693.         $data['ref_no'] = $sale_info['cc_ref_no'];
  1694.         $this->load->helper('string');
  1695.         $data['payment_type'] = str_replace(array('<sup>VNĐ</sup><br />', ''), ' .VNĐ', $sale_info['payment_type']);
  1696.         $data['amount_due'] = $this->sale_lib->get_amount_due();
  1697.         foreach ($data['payments'] as $payment_id => $payment) {
  1698.             $payment_amount = $payment['payment_amount'];
  1699.         }
  1700.         $k = 28;
  1701.         $tongtienhang = 0;
  1702.         foreach (array_reverse($data['cart'], true) as $line => $item) {
  1703.             $tongtienhang_1 += $item['price'] * $item['quantity'] - $item['price'] * $item['quantity'] * $item['discount'] / 100;
  1704.             $k++;
  1705.         }
  1706.         $payments_cost = $tongtienhang_1 - $payment_amount;
  1707.         if ($customer_id != -1) {
  1708.             $cust_info = $this->Customer->get_info($customer_id);
  1709.             $data['customer'] = $cust_info->first_name . ' ' . $cust_info->last_name;
  1710.             $data['cus_name'] = $cust_info->company_name == '' ? '' : $cust_info->company_name;
  1711.             $data['code_tax'] = $cust_info->code_tax;
  1712.             $data['address'] = $cust_info->address_1;
  1713.             $data['account_number'] = $cust_info->account_number;
  1714.             $data['code_tax'] = $cust_info->code_tax;
  1715.             $data['positions'] = $cust_info->positions;
  1716.         }
  1717.         $data['sale_id'] = 'VH ' . $sale_id;
  1718. // $this->load->view("sales/receipt",$data);
  1719. // $this->sale_lib->clear_all();
  1720.         $cities = $this->City->get_all();
  1721.         require_once APPPATH . "/third_party/Classes/export_thanhly.php";
  1722.         $this->sale_lib->clear_all();
  1723.     }
  1724.  
  1725. // phan lam đề nghị thanh toán
  1726.     function thanhtoan($sale_id) {
  1727.         $data['is_sale'] = FALSE;
  1728.  
  1729.         $sale_info = $this->Sale->get_info($sale_id)->row_array();
  1730.         $this->sale_lib->copy_entire_sale($sale_id);
  1731.         $data['cart'] = $this->sale_lib->get_cart();
  1732.         $data['payments'] = $this->sale_lib->get_payments();
  1733.         $data['subtotal'] = $this->sale_lib->get_subtotal();
  1734.         $data['taxes'] = $this->sale_lib->get_taxes($sale_id);
  1735.         $data['total'] = $this->sale_lib->get_total($sale_id);
  1736.         $data['receipt_title'] = lang('sales_receipt');
  1737.         $data['comment'] = $this->Sale->get_comment($sale_id);
  1738.         $data['show_comment_on_receipt'] = $this->Sale->get_comment_on_receipt($sale_id);
  1739.         $data['transaction_time'] = date(get_date_format() . ' ' . get_time_format(), strtotime($sale_info['sale_time']));
  1740.         $customer_id = $this->sale_lib->get_customer();
  1741.         $emp_info = $this->Employee->get_info($sale_info['employee_id']);
  1742.         $data['payment_type'] = $sale_info['payment_type'];
  1743.         $data['amount_change'] = $this->sale_lib->get_amount_due($sale_id) * -1;
  1744.         $data['employee'] = $emp_info->first_name . ' ' . $emp_info->last_name;
  1745.         $data['ref_no'] = $sale_info['cc_ref_no'];
  1746.         $this->load->helper('string');
  1747.         $data['payment_type'] = str_replace(array('<sup>VNĐ</sup><br />', ''), ' .VNĐ', $sale_info['payment_type']);
  1748.         $data['amount_due'] = $this->sale_lib->get_amount_due();
  1749.         foreach ($data['payments'] as $payment_id => $payment) {
  1750.             $payment_amount = $payment['payment_amount'];
  1751.         }
  1752.         $k = 28;
  1753.         $tongtienhang = 0;
  1754.         foreach (array_reverse($data['cart'], true) as $line => $item) {
  1755.             $tongtienhang_1 += $item['price'] * $item['quantity'] - $item['price'] * $item['quantity'] * $item['discount'] / 100;
  1756.             $k++;
  1757.         }
  1758.         $payments_cost = $tongtienhang_1 - $payment_amount;
  1759.         if ($customer_id != -1) {
  1760.             $cust_info = $this->Customer->get_info($customer_id);
  1761.             $data['customer'] = $cust_info->first_name . ' ' . $cust_info->last_name;
  1762.             $data['cus_name'] = $cust_info->company_name == '' ? '' : $cust_info->company_name;
  1763.             $data['code_tax'] = $cust_info->code_tax;
  1764.             $data['address'] = $cust_info->address_1;
  1765.             $data['account_number'] = $cust_info->account_number;
  1766.             $data['code_tax'] = $cust_info->code_tax;
  1767.             $data['positions'] = $cust_info->positions;
  1768.         }
  1769.         $data['sale_id'] = 'VH ' . $sale_id;
  1770. // $this->load->view("sales/receipt",$data);
  1771. // $this->sale_lib->clear_all();
  1772.         $cities = $this->City->get_all();
  1773.         require_once APPPATH . "/third_party/Classes/export_thanhtoan.php";
  1774.         $this->sale_lib->clear_all();
  1775.     }
  1776.  
  1777.     function hoihang($sale_id) {
  1778.         $data['is_sale'] = FALSE;
  1779.  
  1780.         $suppliers = array();
  1781.  
  1782.         $sale_items = $this->Sale->get_sale_items($sale_id)->result_array();
  1783.  
  1784.  
  1785.         foreach ($sale_items as $sale_item) {
  1786.  
  1787.             $suppliers[$sale_item['item_id']] = $this->Item->get_info($sale_item['item_id'])->supplier_id;
  1788.         }
  1789.  
  1790.  
  1791.         /* phan lam mail */
  1792.  
  1793.         $mail_id = $this->input->post('mail_id');
  1794.  
  1795.         $mail_info = $this->Customer->get_info_mail($mail_id);
  1796.  
  1797.         $ses = new SimpleEmailService($this->config->item('amazon_access_key'), $this->config->item('amazon_secret_key'));
  1798.  
  1799.  
  1800.         $ses->enableVerifyHost(false);
  1801.  
  1802.         $ses->enableVerifyPeer(false);
  1803.  
  1804. //get list_email of customer from multi ids
  1805. // $list_customer_email = array();
  1806.  
  1807.         $send_success = array();
  1808.  
  1809.         $send_fail = array();
  1810.  
  1811.         foreach ($suppliers as $id_item => $id_supplier) {
  1812.  
  1813.             if ($this->Supplier->get_info($id_supplier)->email != '') {
  1814.  
  1815. //  $list_customer_email[] = $this->Supplier->get_info($id_supplier)->email;
  1816.  
  1817.                 $m = new SimpleEmailServiceMessage();
  1818.  
  1819.                 $m->setFrom('gs.daycon@gmail.com');
  1820.  
  1821. //set mail title
  1822.  
  1823.                 $m->setSubject("Công ty " . $this->config->item('company') . " xin chân trọng gửi tới nhà cung cấp " . $list_customer->first_name . " thư hỏi hàng");
  1824.  
  1825.                 $m->addTo($this->Supplier->get_info($id_supplier)->email);
  1826.  
  1827.                 $m->addCC($this->config->item('email'));
  1828.  
  1829.                 $data['item'] = $this->Item->get_info($id_item)->name;
  1830.  
  1831. //set mall content
  1832.  
  1833.                 $content = $this->load->view('sales/hoihang', $data, true);
  1834.  
  1835.                 $m->setMessageFromString('', $content);
  1836.  
  1837.                 if ($ses->sendEmail($m) === false) {
  1838.  
  1839.                     $send_fail[] = $this->Customer->get_info($c_ids)->email;
  1840.  
  1841. //echo json_encode(array('success'=>false,'message'=>'Không gửi được mail'));
  1842.                 } else {
  1843.  
  1844.                     $send_success[] = $this->Customer->get_info($c_ids)->email;
  1845.                 }
  1846.             }
  1847.         }
  1848.  
  1849.         /* end phan lam mail */
  1850.  
  1851.         redirect('sales');
  1852.     }
  1853.  
  1854. // end lam bao gia hop dong
  1855.  
  1856.     function complete() {
  1857.         $data['is_sale'] = TRUE;
  1858.         $mode = $this->sale_lib->get_mode();
  1859.         $data['mode'] = $mode;
  1860.         $data['cart'] = $this->sale_lib->get_cart();
  1861.  
  1862.         $data['subtotal'] = $this->sale_lib->get_subtotal();
  1863.  
  1864.         $data['taxes'] = $this->sale_lib->get_taxes();
  1865.  
  1866.         $data['total'] = $this->sale_lib->get_total();
  1867.  
  1868.         $data['receipt_title'] = 'Hóa Đơn Bán Hàng';
  1869.  
  1870.         $data['transaction_time'] = date(get_date_format() . ' ' . get_time_format());
  1871.  
  1872.         $customer_id = $this->sale_lib->get_customer();
  1873.  
  1874.         $employee_id = $this->Employee->get_logged_in_employee_info()->person_id;
  1875.  
  1876.         $data['comment'] = $this->input->post("comment");
  1877.         $data['employees_id'] = $this->sale_lib->get_employees_id();
  1878.  
  1879.         $data['delivery_employee'] = $this->sale_lib->get_employees_delivery();
  1880.  
  1881.         $data['show_comment_on_receipt'] = $this->sale_lib->get_comment_on_receipt();
  1882.  
  1883.         $emp_info = $this->Employee->get_info($employee_id);
  1884.  
  1885.         $data['payments'] = $this->sale_lib->get_payments();
  1886.  
  1887.         $data['amount_change'] = $this->sale_lib->get_amount_due() * -1;
  1888.         $data['amount_due'] = $this->sale_lib->get_amount_due();
  1889.  
  1890.         $data['employee'] = $emp_info->first_name . ' ' . $emp_info->last_name;
  1891.  
  1892.         $data['store'] = $this->sale_lib->get_store();
  1893.  
  1894.         $data['address'] = $emp_info->address_1;
  1895.  
  1896.         $data['ref_no'] = $this->session->flashdata('ref_no') ? $this->session->flashdata('ref_no') : '';
  1897.  
  1898.         $data['item_info'] = $this->Item->get_info($item_id);
  1899. //Creted by San
  1900.         $data['total_order'] = $this->sale_lib->get_total_order_of_item();
  1901.         $data['total_discount'] = $this->sale_lib->get_total_discount_of_item();
  1902.         $data['total_taxes'] = $this->sale_lib->get_total_taxes();
  1903. //End San
  1904.         //Nov 4 hƯnG aUdI
  1905.         $bank_account = $this->sale_lib->get_bank_account();
  1906.         if ($customer_id != -1) {
  1907.  
  1908.             $cust_info = $this->Customer->get_info($customer_id);
  1909.  
  1910.             $data['customer'] = $cust_info->first_name . ' ' . $cust_info->last_name;
  1911.  
  1912.             $data['cus_name'] = $cust_info->company_name == '' ? '' : ($cust_info->company_name);
  1913.  
  1914.             $data['account_number'] = $cust_info->account_number;
  1915.  
  1916.             $data['tax_code'] = $cust_info->tax_code;
  1917.  
  1918.             $data['address1'] = $cust_info->address_1;
  1919.         }
  1920.  
  1921. //SAVE sale to database          
  1922.         $data['discount_money'] = $this->sale_lib->get_discount_money();
  1923.         $total_taxes_percent = 0;
  1924.         foreach ($data['cart'] as $item) {
  1925.             if ($item['unit'] == 'unit') {
  1926.                 $total_taxes_percent += $item['taxes'] * ($item['price'] * $item['quantity'] - $item['discount'] * $item['price'] * $item['quantity'] / 100) / 100;
  1927.             } else {
  1928.                 $total_taxes_percent += $item['taxes'] * ($item['price_rate'] * $item['quantity'] - $item['discount'] * $item['price_rate'] * $item['quantity'] / 100) / 100;
  1929.             }
  1930.         }
  1931.         $total_owe = 0;
  1932.         foreach ($data['payments'] as $payment) {
  1933.             $total_owe += $payment['payment_amount'] + $payment['discount_money'];
  1934.         }
  1935.         $data['amount_due1'] = $data['total_order'] + $total_taxes_percent - $total_owe;
  1936.         $later_cost_price = $data['total_order'] + $total_taxes_percent - $discount_money;
  1937.         $actual_money = $later_cost_price;
  1938.         if ($this->sale_lib->get_suspended_sale_id() != '') {
  1939.             $stt = 1;
  1940.             $data['sale_id'] = $this->Sale->save($data['cart'], $customer_id, $employee_id, $data['comment'], $data['employees_id'], $data['show_comment_on_receipt'], $data['payments'], $data['discount_money'], $later_cost_price, $actual_money, $data['amount_due'], $data['amount_due1'], $this->sale_lib->get_suspended_sale_id(), 0, 0, $stt, $data['ref_no'], '', $mode, $data['delivery_employee'], $bank_account);
  1941.         } else {
  1942.             $stt = 0;
  1943.             $data['sale_id'] = $this->Sale->save($data['cart'], $customer_id, $employee_id, $data['comment'], $data['employees_id'], $data['show_comment_on_receipt'], $data['payments'], $data['discount_money'], $later_cost_price, $actual_money, $data['amount_due'], $data['amount_due1'], $this->sale_lib->get_suspended_sale_id(), 0, 0, $stt, $data['ref_no'], '', $mode, $data['delivery_employee'], $bank_account);
  1944.         }
  1945.  
  1946. //lay ten nv giao hang
  1947.         $sale_info1 = $this->Sale->get_info($data['sale_id'])->row_array();
  1948.         $info_empss1 = $this->Employee->get_info($sale_info1['delivery_employee']);
  1949.         $data['delivery_employee1'] = $info_empss1->first_name . ' ' . $info_empss1->last_name;
  1950.  
  1951.         if ($data['sale_id'] == 'VH -1') {
  1952.  
  1953.             $data['error_message'] = lang('sales_transaction_failed');
  1954.         } else {
  1955.  
  1956.             if ($this->sale_lib->get_email_receipt() && !empty($cust_info->email)) {
  1957.  
  1958.                 $this->load->library('email');
  1959.  
  1960.                 $config['mailtype'] = 'html';
  1961.  
  1962.                 $this->email->initialize($config);
  1963.  
  1964.                 $this->email->from($this->config->item('email'), $this->config->item('company'));
  1965.  
  1966.                 $this->email->to($cust_info->email);
  1967.  
  1968.                 $this->email->subject(lang('sales_receipt'));
  1969.  
  1970.                 $this->email->message($this->load->view("sales/receipt_email", $data, true));
  1971.  
  1972.                 $this->email->send();
  1973.             }
  1974.         }
  1975.         if ($this->config->item('print_excel') == 'print') {
  1976.             $this->load->view("sales/receipt", $data);
  1977.             $this->sale_lib->clear_all();
  1978.         } else if ($this->config->item('print_excel') == 'print_a5') {
  1979.             $this->load->view("sales/receipt_a5", $data);
  1980.             $this->sale_lib->clear_all();
  1981.         } else {
  1982.             $this->sale_lib->clear_all();
  1983.             require_once APPPATH . "/third_party/Classes/export_receipt.php";
  1984.             redirect('sales');
  1985.             $this->sale_lib->clear_all();
  1986.             $this->_reload();
  1987.         }
  1988.     }
  1989.  
  1990.     function email_receipt($sale_id) {
  1991.         $sale_info = $this->Sale->get_info($sale_id)->row_array();
  1992.         $this->sale_lib->copy_entire_sale($sale_id);
  1993.         $data['cart'] = $this->sale_lib->get_cart();
  1994.         $data['payments'] = $this->sale_lib->get_payments();
  1995.         $data['subtotal'] = $this->sale_lib->get_subtotal();
  1996.         $data['taxes'] = $this->sale_lib->get_taxes($sale_id);
  1997.         $data['total'] = $this->sale_lib->get_total($sale_id);
  1998.         $data['receipt_title'] = lang('sales_receipt');
  1999.         $data['transaction_time'] = date(get_date_format() . ' ' . get_time_format(), strtotime($sale_info['sale_time']));
  2000.         $customer_id = $this->sale_lib->get_customer();
  2001.         $emp_info = $this->Employee->get_info($sale_info['employee_id']);
  2002.         $data['payment_type'] = $sale_info['payment_type'];
  2003.         $data['amount_change'] = $this->sale_lib->get_amount_due($sale_id) * -1;
  2004.         $data['employee'] = $emp_info->first_name . ' ' . $emp_info->last_name;
  2005.  
  2006.         if ($customer_id != -1) {
  2007.             $cust_info = $this->Customer->get_info($customer_id);
  2008.             $data['customer'] = $cust_info->first_name . ' ' . $cust_info->last_name . ($cust_info->company_name == '' ? '' : ' (' . $cust_info->company_name . ')');
  2009.         }
  2010.         $data['sale_id'] = 'POS ' . $sale_id;
  2011.         if (!empty($cust_info->email)) {
  2012.             $this->load->library('email');
  2013.             $config['mailtype'] = 'html';
  2014.             $this->email->initialize($config);
  2015.             $this->email->from($this->config->item('email'), $this->config->item('company'));
  2016.             $this->email->to($cust_info->email);
  2017.  
  2018.             $this->email->subject(lang('sales_receipt'));
  2019.             $this->email->message($this->load->view("sales/receipt_email", $data, true));
  2020.             $this->email->send();
  2021.         }
  2022.  
  2023.         $this->sale_lib->clear_all();
  2024.     }
  2025.  
  2026.     function receipt($sale_id) {
  2027.         $data['is_sale'] = FALSE;
  2028.  
  2029.         $sale_info = $this->Sale->get_info($sale_id)->row_array();
  2030.         $this->sale_lib->copy_entire_sale($sale_id);
  2031.         $data['cart'] = $this->sale_lib->get_cart();
  2032.         $data['payments'] = $this->sale_lib->get_payments();
  2033.         $data['subtotal'] = $this->sale_lib->get_subtotal();
  2034.         $data['taxes'] = $this->sale_lib->get_taxes($sale_id);
  2035.         $data['total'] = $this->sale_lib->get_total($sale_id);
  2036.         $data['receipt_title'] = lang('sales_receipt');
  2037.         $data['comment'] = $this->Sale->get_comment($sale_id);
  2038.         $data['show_comment_on_receipt'] = $this->Sale->get_comment_on_receipt($sale_id);
  2039.         $data['transaction_time'] = date(get_date_format() . ' ' . get_time_format(), strtotime($sale_info['sale_time']));
  2040.         $customer_id = $this->sale_lib->get_customer();
  2041.         $emp_info = $this->Employee->get_info($sale_info['employee_id']);
  2042.         $data['payment_type'] = $sale_info['payment_type'];
  2043.         $data['amount_change'] = $this->sale_lib->get_amount_due($sale_id) * -1;
  2044.         $data['employee'] = $emp_info->first_name . ' ' . $emp_info->last_name;
  2045.         $data['ref_no'] = $sale_info['cc_ref_no'];
  2046.         $this->load->helper('string');
  2047.         $data['payment_type'] = str_replace(array('<sup>VNĐ</sup><br />', ''), ' .VNĐ', $sale_info['payment_type']);
  2048.         $data['amount_due'] = $this->sale_lib->get_amount_due();
  2049.         foreach ($data['payments'] as $payment_id => $payment) {
  2050.             $payment_amount = $payment['payment_amount'];
  2051.         }
  2052.         $k = 28;
  2053.         $tongtienhang = 0;
  2054.         foreach (array_reverse($data['cart'], true) as $line => $item) {
  2055.             $tongtienhang_1 += $item['price'] * $item['quantity'] - $item['price'] * $item['quantity'] * $item['discount'] / 100;
  2056.             $k++;
  2057.         }
  2058.         $payments_cost = $tongtienhang_1 - $payment_amount;
  2059.         if ($customer_id != -1) {
  2060.             $cust_info = $this->Customer->get_info($customer_id);
  2061.             $data['customer'] = $cust_info->first_name . ' ' . $cust_info->last_name;
  2062.             $data['cus_name'] = $cust_info->company_name == '' ? '' : $cust_info->company_name;
  2063.             $data['code_tax'] = $cust_info->code_tax;
  2064.             $data['address'] = $cust_info->address_1;
  2065.             $data['account_number'] = $cust_info->account_number;
  2066.         }
  2067.         $data['sale_id'] = 'VH ' . $sale_id;
  2068. // $this->load->view("sales/receipt",$data);
  2069. // $this->sale_lib->clear_all();
  2070.         $cities = $this->City->get_all();
  2071.         require_once APPPATH . "/third_party/Classes/export_sales.php";
  2072.         $this->sale_lib->clear_all();
  2073.     }
  2074.  
  2075.     function edit($sale_id) {
  2076.         $data = array();
  2077.  
  2078.         $data['customers'] = array('' => 'No Customer');
  2079.         foreach ($this->Customer->get_all()->result() as $customer) {
  2080.             $data['customers'][$customer->person_id] = $customer->first_name . ' ' . $customer->last_name;
  2081.         }
  2082.  
  2083.         $data['employees'] = array();
  2084.         foreach ($this->Employee->get_all_receiving()->result() as $employee) {
  2085.             $data['employees'][$employee->person_id] = $employee->first_name . ' ' . $employee->last_name;
  2086.         }
  2087.  
  2088.         $data['sale_info'] = $this->Sale->get_info($sale_id)->row_array();
  2089.  
  2090.  
  2091.         $this->load->view('sales/edit', $data);
  2092.     }
  2093.  
  2094.     function delete($sale_id) {
  2095.         $data = array();
  2096.  
  2097.         if ($this->Sale->delete($sale_id)) {
  2098.             $data['success'] = true;
  2099.         } else {
  2100.             $data['success'] = false;
  2101.         }
  2102.  
  2103.         $this->load->view('sales/delete', $data);
  2104.     }
  2105.  
  2106.     function undelete($sale_id) {
  2107.         $data = array();
  2108.  
  2109.         if ($this->Sale->undelete($sale_id)) {
  2110.             $data['success'] = true;
  2111.         } else {
  2112.             $data['success'] = false;
  2113.         }
  2114.  
  2115.         $this->load->view('sales/undelete', $data);
  2116.     }
  2117.  
  2118.     function save($sale_id) {
  2119.         $sale_data = array(
  2120.             'sale_time' => date('Y-m-d', strtotime($this->input->post('date'))),
  2121.             'customer_id' => $this->input->post('customer_id') ? $this->input->post('customer_id') : null,
  2122.             'employee_id' => $this->input->post('employee_id'),
  2123.             'comment' => $this->input->post('comment'),
  2124.             'show_comment_on_receipt' => $this->input->post('show_comment_on_receipt') ? 1 : 0
  2125.         );
  2126.  
  2127.         if ($this->Sale->update($sale_data, $sale_id)) {
  2128.             echo json_encode(array('success' => true, 'message' => lang('sales_successfully_updated')));
  2129.         } else {
  2130.             echo json_encode(array('success' => false, 'message' => lang('sales_unsuccessfully_updated')));
  2131.         }
  2132.     }
  2133.  
  2134.     function _payments_cover_total1() {
  2135.  
  2136.         $total_payments = 0;
  2137.         foreach ($this->sale_lib->get_payments() as $payment) {
  2138.  
  2139.             $total_payments += $payment['payment_amount'] + $payment['discount_money'];
  2140.         }
  2141.  
  2142.         /* Changed the conditional to account for floating point rounding */
  2143.         $total_order = $this->sale_lib->get_total_order_of_item();
  2144.         $total_taxes_percent = 0;
  2145.         foreach ($this->sale_lib->get_cart() as $item) {
  2146.             if ($item['unit'] == "unit") {
  2147.                 $total_taxes_percent += $item['taxes'] * ($item['price'] * $item['quantity'] - $item['discount'] * $item['price'] * $item['quantity'] / 100) / 100;
  2148.             } else {
  2149.                 $total_taxes_percent += $item['taxes'] * ($item['price_rate'] * $item['quantity'] - $item['discount'] * $item['price_rate'] * $item['quantity'] / 100) / 100;
  2150.             }
  2151.         }
  2152.         if (( $this->sale_lib->get_mode() == 'sale' ) && (($total_order + $total_taxes_percent - $total_payments ) > 1e-6 )) {
  2153.             return false;
  2154.         }
  2155.         return true;
  2156.     }
  2157.  
  2158.     function _payments_cover_total() {
  2159.         $total_payments = 0;
  2160.  
  2161.         foreach ($this->sale_lib->get_payments() as $payment) {
  2162.             $total_payments += $payment['payment_amount'];
  2163.         }
  2164.  
  2165.         /* Changed the conditional to account for floating point rounding */
  2166.         if (( $this->sale_lib->get_mode() == 'sale' ) && ( ( to_currency_no_money($this->sale_lib->get_total()) - $total_payments ) > 1e-6 )) {
  2167.             return false;
  2168.         }
  2169.  
  2170.         return true;
  2171.     }
  2172.  
  2173.     function reload() {
  2174.         $this->_reload();
  2175.     }
  2176.  
  2177.     function _reload($data = array(), $is_ajax = true) {
  2178.         $person_info = $this->Employee->get_logged_in_employee_info();
  2179.         $data['controller_name'] = strtolower(get_class());
  2180.         $data['cart'] = $this->sale_lib->get_cart();
  2181.         $data['modes'] = array('sale' => lang('sales_sale'), 'return' => lang('sales_return'));
  2182.         $data['mode'] = $this->sale_lib->get_mode();
  2183.         $data['items_in_cart'] = $this->sale_lib->get_items_in_cart();
  2184.         $data['subtotal'] = $this->sale_lib->get_subtotal();
  2185.         $data['taxes'] = $this->sale_lib->get_taxes();
  2186.         $data['total'] = $this->sale_lib->get_total();
  2187.         $data['items_module_allowed'] = $this->Employee->has_module_permission('items', $person_info->person_id);
  2188.         $data['comment'] = $this->sale_lib->get_comment();
  2189.         $data['sale_id'] = $this->sale_lib->get_suspended_sale_id();
  2190.         $data['date_debt'] = $this->sale_lib->get_date_debt();
  2191.         $data['show_comment_on_receipt'] = $this->sale_lib->get_comment_on_receipt();
  2192.         $data['email_receipt'] = $this->sale_lib->get_email_receipt();
  2193.         $data['payments_total'] = $this->sale_lib->get_payments_total();
  2194.         $data['amount_due'] = $this->sale_lib->get_amount_due();
  2195.         $data['amount_due1'] = $this->sale_lib->get_amount_owe();
  2196. //Creted by San
  2197.         $data['total_order'] = $this->sale_lib->get_total_order_of_item();
  2198.         $data['total_discount'] = $this->sale_lib->get_total_discount_of_item();
  2199.         $data['inventory'] = $this->Create_invetory->get_all2()->result_array();
  2200.         $data['total_taxes'] = $this->sale_lib->get_total_taxes();
  2201. //End San
  2202.         $data['payments'] = $this->sale_lib->get_payments();
  2203. //huyenlt^^  
  2204. //dungbv lấy nv báo giá ,giao hàng
  2205.         $employees_id = $this->sale_lib->get_employees_id();
  2206.         $info_employeess = $this->Employee->get_info($employees_id);
  2207.         $data['employees'] = $info_employeess->first_name . ' ' . $info_employeess->last_name;
  2208.  
  2209.         $delivery_employee = $this->sale_lib->get_employees_delivery();
  2210.         $info_delivery = $this->Employee->get_info($delivery_employee);
  2211.         $data['delivery'] = $info_delivery->first_name . ' ' . $info_delivery->last_name;
  2212. //end dung
  2213.         $data['selected_employees'] = $this->Item->get_info($item_id)->supplier_id;
  2214. //end huyenlt^^
  2215.         if ($this->config->item('enable_credit_card_processing')) {
  2216.             $data['payment_options'] = array(
  2217.                 '' => '-- Chọn --',
  2218.                 lang('sales_cash') => lang('sales_cash'),
  2219.                 lang('sales_credit') => lang('sales_credit'),
  2220.                 'COD' => 'COD'
  2221.             );
  2222.         } else {
  2223.             $data['payment_options'] = array(
  2224.                 '' => '-- Chọn --',
  2225.                 lang('sales_cash') => lang('sales_cash'),
  2226.                 lang('sales_credit') => lang('sales_credit'),
  2227.                 'COD' => 'COD'
  2228.             );
  2229.         }
  2230.         foreach ($this->Appconfig->get_additional_payment_types() as $additional_payment_type) {
  2231.             $data['payment_options'][$additional_payment_type] = $additional_payment_type;
  2232.         }
  2233.         $customer_id = $this->sale_lib->get_customer();
  2234.         if ($customer_id != -1) {
  2235.             $info = $this->Customer->get_info($customer_id);
  2236.             $data['customer'] = $info->company_name == '' ? '' . (' (' . $info->first_name . ' ' . $info->last_name . ')') : $info->company_name . (' (' . $info->first_name . ' ' . $info->last_name . ')');
  2237.             $data['customer_email'] = $info->email;
  2238.             $data['customer_id'] = $customer_id;
  2239.         }
  2240.         $data['payments_cover_total'] = $this->_payments_cover_total();
  2241.         $data['payments_cover_total1'] = $this->_payments_cover_total1();
  2242.        
  2243.         //Hưng Audi Nov 3        
  2244.         $bank_list = array('' => '-- Chọn tài khoản ngân hàng --');
  2245.         foreach ($this->Tkdu->get_bank_list()->result() as $bank){
  2246.             $bank_list[$bank->id] = $bank->id.' - '.$bank->name;
  2247.         }
  2248.         $data['bank_list'] = $bank_list;
  2249.         $data['bank_account'] = $this->sale_lib->get_bank_account();
  2250.         $data['payment_type'] = $this->sale_lib->get_payment_type();
  2251.        
  2252.         if ($is_ajax) {
  2253.             $this->load->view("sales/register", $data);
  2254.         } else {
  2255.             $this->load->view("sales/register_initial", $data);
  2256.         }
  2257.     }
  2258.  
  2259.     function cancel_sale() {
  2260.         if (!$this->_void_partial_transactions()) {
  2261.             $this->_reload(array('error' => lang('sales_attempted_to_reverse_partial_transactions_failed_please_contact_support')), true);
  2262.         }
  2263.  
  2264.         $this->sale_lib->clear_all();
  2265.         $this->_reload();
  2266.     }
  2267.  
  2268.     function _void_partial_transactions() {
  2269.         $void_success = true;
  2270.  
  2271.         if ($partial_transactions = $this->sale_lib->get_partial_transactions()) {
  2272.             $service_url = (!defined("ENVIRONMENT") or ENVIRONMENT == 'development') ? 'https://hc.mercurydev.net/tws/transactionservice.asmx?WSDL' : 'https://hc.mercurypay.com/tws/transactionservice.asmx?WSDL';
  2273.  
  2274.             foreach ($partial_transactions as $partial_transaction) {
  2275.                 $parameters = array(
  2276.                     'request' => $partial_transaction,
  2277.                     'password' => $this->config->item('merchant_password'),
  2278.                 );
  2279.  
  2280.                 $client = new SoapClient($service_url, array('trace' => TRUE));
  2281.                 $result = $client->CreditReversalToken($parameters);
  2282.  
  2283.                 $status = $result->CreditReversalTokenResult->Status;
  2284.                 if ($status != 'Approved') {
  2285.                     unset($parameters['AcqRefData']);
  2286.                     unset($parameters['ProcessData']);
  2287.                     $result = $client->CreditVoidSaleToken($parameters);
  2288.                     $status = $result->CreditVoidSaleTokenResult->Status;
  2289.  
  2290.                     if ($status != 'Approved') {
  2291.                         $void_success = false;
  2292.                     }
  2293.                 }
  2294.             }
  2295.         }
  2296.  
  2297.         return $void_success;
  2298.     }
  2299.  
  2300.     function suspend() {
  2301.         $customer_id = $this->sale_lib->get_customer();
  2302.         $bank_account = $this->sale_lib->get_bank_account();
  2303.         if ($this->sale_lib->get_date_debt() == null) {
  2304.             echo "<script>alert('Bạn cần chọn ngày trả nợ');
  2305.                window.location = '" . base_url() . "sales';</script>";
  2306.         } elseif ($customer_id == -1) {
  2307.             echo "<script>alert('Bạn cần chọn khách hàng để hoàn thành lưu vào sổ nợ');
  2308.                window.location = '" . base_url() . "sales';</script>";
  2309.         } else {
  2310.             $data['discount_money'] = $this->sale_lib->get_discount_money();
  2311.             $actual = $this->input->post('actual_money_post');
  2312.             $data['cart'] = $this->sale_lib->get_cart();
  2313.             $data['subtotal'] = $this->sale_lib->get_subtotal();
  2314.             $data['taxes'] = $this->sale_lib->get_taxes();
  2315. //Created by San
  2316.             $data['total_order'] = $this->sale_lib->get_total_order_of_item();
  2317.             $data['total_discount'] = $this->sale_lib->get_total_discount_of_item();
  2318.             $data['total_taxes'] = $this->sale_lib->get_total_taxes();
  2319. //End
  2320.             $data['total'] = $this->sale_lib->get_total();
  2321.             $data['receipt_title'] = lang('sales_receipt');
  2322.             $data['transaction_time'] = date(get_date_format() . ' ' . get_time_format());
  2323.             $customer_id = $this->sale_lib->get_customer();
  2324.             $employee_id = $this->Employee->get_logged_in_employee_info()->person_id;
  2325.             $comment = $this->sale_lib->get_comment();
  2326.             $date_debt = $this->sale_lib->get_date_debt();
  2327.             $show_comment_on_receipt = $this->sale_lib->get_comment_on_receipt();
  2328.             $emp_info = $this->Employee->get_info($employee_id);
  2329.             $data['employees_id'] = $this->sale_lib->get_employees_id();
  2330.             $data['delivery_employee'] = $this->sale_lib->get_employees_delivery();
  2331.             $data['payments'] = $this->sale_lib->get_payments();
  2332.             $data['amount_change'] = $this->sale_lib->get_amount_due() * -1;
  2333.             $data['amount_due'] = $this->sale_lib->get_amount_due();
  2334.             $data['employee'] = $emp_info->first_name;
  2335.             $data['customer_id'] = $this->sale_lib->get_customer();
  2336.  
  2337.             $data['store'] = $this->sale_lib->get_store();
  2338.             if ($customer_id != -1) {
  2339.                 $cust_info = $this->Customer->get_info($customer_id);
  2340.                 $data['customer'] = $cust_info->first_name . ' ' . $cust_info->last_name;
  2341.                 $data['cus_name'] = $cust_info->company_name == '' ? '' : $cust_info->company_name;
  2342.                 $data['code_tax'] = $cust_info->code_tax;
  2343.                 $data['address'] = $cust_info->address_1;
  2344.                 $data['account_number'] = $cust_info->account_number;
  2345.             }
  2346.             $total_payments = 0;
  2347.             foreach ($data['payments'] as $payment) {
  2348.                 $total_payments += $payment['payment_amount'];
  2349.             }
  2350.             $sale_id = $this->sale_lib->get_suspended_sale_id();
  2351.             $total_taxes_percent = 0;
  2352.             foreach ($data['cart'] as $item) {
  2353.                 if ($item['unit'] == 'unit') {
  2354.                     $total_taxes_percent += $item['taxes'] * ($item['price'] * $item['quantity'] - $item['discount'] * $item['price'] * $item['quantity'] / 100) / 100;
  2355.                 } else {
  2356.                     $total_taxes_percent += $item['taxes'] * ($item['price_rate'] * $item['quantity'] - $item['discount'] * $item['price_rate'] * $item['quantity'] / 100) / 100;
  2357.                 }
  2358.             }
  2359.             $total_owe = 0;
  2360.             foreach ($data['payments'] as $payment) {
  2361.                 $total_owe += $payment['payment_amount'] + $payment['discount_money'];
  2362.             }
  2363.             $data['amount_due1'] = $data['total_order'] + $total_taxes_percent - $total_owe;
  2364.             $later_cost_price = $data['total_order'] + $total_taxes_percent - $discount_money;
  2365.             $actual_money = $later_cost_price;
  2366.             if ($sale_id != '') {
  2367.                 $stt = 1;
  2368.                 $data['sale_id'] = $this->Sale->save($data['cart'], $customer_id, $employee_id, $comment, $data['employees_id'], $show_comment_on_receipt, $data['payments'], $data['discount_money'], $later_cost_price, $actual_money, $data['amount_due'], $data['amount_due1'], $sale_id, 1, 0, $stt, $date_debt, '', '', $data['delivery_employee'], $bank_account);
  2369.             } else {
  2370.                 $stt = 0;
  2371.                 $data['sale_id'] = $this->Sale->save($data['cart'], $customer_id, $employee_id, $comment, $data['employees_id'], $show_comment_on_receipt, $data['payments'], $data['discount_money'], $later_cost_price, $actual_money, $data['amount_due'], $data['amount_due1'], $sale_id, 1, 0, $stt, $date_debt, '', '', $data['delivery_employee'], $bank_account);
  2372.             }
  2373.  
  2374.             //tổng nợ cũ-----------------------------------------------------------------------------------------------------------
  2375.             $data['sale_complete_invs'] = $this->Inventory->find_sale_complete_customer_materials($customer_id);
  2376.             $data['customer_id'] = $customer_id;
  2377.             foreach ($data['sale_complete_invs'] as $val) {
  2378.                 $data_sale_item = $this->Sale->get_sale_item_by_sale_id($val['sale_id']); // lay thong tin item trong don hang
  2379.                 $data_sale_item_kit = $this->Sale->get_sale_item_kit_by_sale_id($val['sale_id']); //lay thong tin item_kit trong don hang
  2380.                 $detail_sale[$val['sale_id']][] = $data_sale_item;
  2381.                 $detail_sale[$val['sale_id']][] = $data_sale_item_kit;
  2382.                 $sale_data[] = $this->Sale->get_info($val['sale_id'])->row();
  2383.                 $total_item = 0;
  2384.                 $total_item_kit = 0;
  2385.                 foreach ($data_sale_item as $value) {  // tinh tong item
  2386.                     $total_item = $total_item + $value['quantity_purchased'];
  2387.                 }
  2388.                 foreach ($data_sale_item_kit as $value) {
  2389.                     $total_item_kit = $total_item_kit + $value['quantity_purchased'];
  2390.                 }
  2391.  
  2392.                 $detail_sale[$val['sale_id']]['total_item'] = $total_item + $total_item_kit;
  2393.             }
  2394.             $data['detail_sale'] = $detail_sale;
  2395.             $data['sale_data'] = $sale_data;
  2396.             //end tổng nợ ---------------------------------------------------------------------------------------------------------  
  2397.             if ($data['sale_id'] == 'VH -1') {
  2398.                 $data['error_message'] = lang('sales_transaction_failed');
  2399.             }
  2400.             if ($this->config->item('print_excel') == 'print_a5') {
  2401.                 $this->load->view('sales/print_now_congno_a5', $data);
  2402.             } else {
  2403.                 $this->load->view('sales/print_now_congno', $data);
  2404.             }
  2405.             $this->sale_lib->clear_all();
  2406.         }
  2407.     }
  2408.  
  2409. //ghi nợ
  2410.     function suspended() {
  2411.         $data = array();
  2412.         $data['suspended_sales'] = $this->Sale->get_all_suspended()->result_array();
  2413.         $this->load->view('sales/suspended', $data);
  2414.     }
  2415.  
  2416. //báo giá
  2417.     function materialed() {
  2418.         $data = array();
  2419.         $data['material_sales'] = $this->Sale->get_all_materials()->result_array();
  2420.         $data['quotes'] = $this->M_quotes_contract->get_list_template_quotes_contract(2);
  2421.         $data['contract'] = $this->M_quotes_contract->get_list_template_quotes_contract(1);
  2422.         $this->load->view('sales/materials', $data);
  2423.     }
  2424.  
  2425. //huyenlt^^
  2426.     function liabilityed() {
  2427.         $data = array();
  2428.         $data['liability_sales'] = $this->Sale->get_all_liability()->result_array();
  2429.         $this->load->view('sales/liability', $data);
  2430.     }
  2431.  
  2432. //đặt hàng
  2433.     function liability() {
  2434.         $customer_id = $this->sale_lib->get_customer();
  2435.         if ($this->sale_lib->get_date_debt() == null) {
  2436.             echo "<script>alert('Bạn cần chọn ngày đưa hàng cho khách!');
  2437.                window.location = '" . base_url() . "sales';</script>";
  2438.         } elseif ($customer_id == -1) {
  2439.             echo "<script>alert('Bạn cần chọn khách hàng để hoàn thành đặt hàng');
  2440.                window.location = '" . base_url() . "sales';</script>";
  2441.         } else {
  2442. //$discount_money = $this->input->post('discount_money');
  2443.             $mode = $this->sale_lib->get_mode();
  2444.             $data['discount_money'] = $this->sale_lib->get_discount_money();
  2445.             $data['cart'] = $this->sale_lib->get_cart();
  2446.  
  2447.             $data['subtotal'] = $this->sale_lib->get_subtotal();
  2448.  
  2449.             $data['taxes'] = $this->sale_lib->get_taxes();
  2450.             $data['total_taxes'] = $this->sale_lib->get_total_taxes();
  2451. //Created by San
  2452.             $data['total_order'] = $this->sale_lib->get_total_order_of_item();
  2453.             $data['total_discount'] = $this->sale_lib->get_total_discount_of_item();
  2454. //End
  2455.             $data['total'] = $this->sale_lib->get_total();
  2456.  
  2457.             $data['store'] = $this->sale_lib->get_store();
  2458.            
  2459.             $data['receipt_title'] = lang('sales_receipt');
  2460.  
  2461.             $data['transaction_time'] = date(get_date_format() . ' ' . get_time_format());
  2462.  
  2463.             $customer_id = $this->sale_lib->get_customer();
  2464.  
  2465.             $employee_id = $this->Employee->get_logged_in_employee_info()->person_id;
  2466.  
  2467.             $comment = $this->sale_lib->get_comment();
  2468.  
  2469.             $date_debt = $this->sale_lib->get_date_debt();
  2470.  
  2471.             $show_comment_on_receipt = $this->sale_lib->get_comment_on_receipt();
  2472.  
  2473.             $emp_info = $this->Employee->get_info($employee_id);
  2474.  
  2475.             $data['amount_due'] = $this->sale_lib->get_amount_due();
  2476.  
  2477. //Alain Multiple payments
  2478.             $data['employees_id'] = $this->sale_lib->get_employees_id();
  2479.             $data['delivery_employee'] = $this->sale_lib->get_employees_delivery();
  2480.             $data['payments'] = $this->sale_lib->get_payments();
  2481.             $data['amount_change'] = $this->sale_lib->get_amount_due() * -1;
  2482.  
  2483.             $data['employee'] = $emp_info->first_name . ' ' . $emp_info->last_name;
  2484.  
  2485.             if ($customer_id != -1) {
  2486.                 $cust_info = $this->Customer->get_info($customer_id);
  2487.  
  2488.                 $data['customer'] = $cust_info->first_name . ' ' . $cust_info->last_name;
  2489.  
  2490.                 $data['cus_name'] = $cust_info->company_name == '' ? '' : $cust_info->company_name;
  2491.  
  2492.                 $data['code_tax'] = $cust_info->code_tax;
  2493.  
  2494.                 $data['address'] = $cust_info->address_1;
  2495.  
  2496.                 $data['account_number'] = $cust_info->account_number;
  2497.             }
  2498.             $total_payments = 0;
  2499.             foreach ($data['payments'] as $payment) {
  2500.                 $total_payments += $payment['payment_amount'];
  2501.             }
  2502.             $sale_id = $this->sale_lib->get_suspended_sale_id();
  2503. //SAVE sale to database
  2504.             $total_taxes_percent = 0;
  2505.             foreach ($data['cart'] as $item) {
  2506.                 $total_taxes_percent += $item['taxes'] * ($item['price'] * $item['quantity'] - $item['discount'] * $item['price'] * $item['quantity'] / 100) / 100;
  2507.             }
  2508.             $total_owe = 0;
  2509.             foreach ($data['payments'] as $payment) {
  2510.                 $total_owe += $payment['payment_amount'] + $payment['discount_money'];
  2511.             }
  2512.             $data['amount_due1'] = $data['total_order'] + $total_taxes_percent - $total_owe;
  2513.             $later_cost_price = $data['total_order'] + $total_taxes_percent - $discount_money;
  2514. //$later_cost_price = $data['total'] - $discount_money;
  2515.             $actual_money = $payment['payment_amount'];
  2516.             if ($sale_id != '') {
  2517.                 $stt = 0;
  2518.                 $data['sale_id'] = $this->Sale->save_liability($data['cart'], $customer_id, $employee_id, $comment, $data['employees_id'], $show_comment_on_receipt, $data['payments'], $data['discount_money'], $later_cost_price, $actual_money, $data['amount_due'], $data['amount_due1'], $sale_id, 0, 1, $stt, 1, $date_debt, '', $data['delivery_employee']);
  2519.             } else {
  2520.                 $stt = 0;
  2521.                 $data['sale_id'] = $this->Sale->save_liability($data['cart'], $customer_id, $employee_id, $comment, $data['employees_id'], $show_comment_on_receipt, $data['payments'], $data['discount_money'], $later_cost_price, $actual_money, $data['amount_due'], $data['amount_due1'], $sale_id, 0, 1, $stt, 1, $date_debt, '', $data['delivery_employee']);
  2522.             }
  2523.             if ($data['sale_id'] == 'VH -1') {
  2524.                 $data['error_message'] = lang('sales_transaction_failed');
  2525.             }
  2526.             if ($this->config->item('print_excel') == 'print_a5') {
  2527.                 $this->load->view('sales/print_dat_hang_a5', $data);
  2528.             } else {
  2529.                 $this->load->view('sales/print_dat_hang', $data);
  2530.             }
  2531.             $this->sale_lib->clear_all();
  2532.         }
  2533.     }
  2534.  
  2535. //materials
  2536.     function materials() {
  2537.         $customer_id = $this->sale_lib->get_customer();
  2538.         if ($this->sale_lib->get_date_debt() == null) {
  2539.             echo "<script>alert('Bạn cần chọn ngày báo giá!');
  2540.                window.location = '" . base_url() . "sales';</script>";
  2541.         } elseif ($customer_id == -1) {
  2542.             echo "<script>alert('Bạn cần chọn khách hàng để hoàn thành báo giá!');
  2543.                window.location = '" . base_url() . "sales';</script>";
  2544.         } else {
  2545. //$discount_money = $this->input->post('discount_money');
  2546.             $data['discount_money'] = $this->sale_lib->get_discount_money();
  2547.             $data['cart'] = $this->sale_lib->get_cart();
  2548.  
  2549.             $data['subtotal'] = $this->sale_lib->get_subtotal();
  2550.  
  2551.             $data['taxes'] = $this->sale_lib->get_taxes();
  2552.  
  2553.             $data['total'] = $this->sale_lib->get_total();
  2554.  
  2555.             $data['receipt_title'] = lang('sales_receipt');
  2556.  
  2557.             $data['transaction_time'] = date(get_date_format() . ' ' . get_time_format());
  2558.  
  2559.             $customer_id = $this->sale_lib->get_customer();
  2560.  
  2561.             $employee_id = $this->Employee->get_logged_in_employee_info()->person_id;
  2562.  
  2563.             $comment = $this->sale_lib->get_comment();
  2564.  
  2565.             $date_debt = $this->sale_lib->get_date_debt();
  2566.  
  2567.             $show_comment_on_receipt = $this->sale_lib->get_comment_on_receipt();
  2568.  
  2569.             $emp_info = $this->Employee->get_info($employee_id);
  2570.  
  2571.             $data['amount_due'] = $this->sale_lib->get_amount_due();
  2572.             $data['amount_due1'] = $this->sale_lib->get_amount_due1();
  2573.  
  2574. //Alain Multiple payments
  2575.             $data['employees_id'] = $this->sale_lib->get_employees_id();
  2576.             $data['payments'] = $this->sale_lib->get_payments();
  2577. //      print_r($data['payments']);
  2578. //      die('xxxxx');
  2579.             $data['amount_change'] = $this->sale_lib->get_amount_due() * -1;
  2580.  
  2581.             $data['employee'] = $emp_info->first_name;
  2582.  
  2583.             if ($customer_id != -1) {
  2584.                 $cust_info = $this->Customer->get_info($customer_id);
  2585.  
  2586.                 $data['customer'] = $cust_info->first_name . ' ' . $cust_info->last_name;
  2587.  
  2588.                 $data['cus_name'] = $cust_info->company_name == '' ? '' : $cust_info->company_name;
  2589.  
  2590.                 $data['code_tax'] = $cust_info->code_tax;
  2591.  
  2592.                 $data['address'] = $cust_info->address_1;
  2593.  
  2594.                 $data['account_number'] = $cust_info->account_number;
  2595.             }
  2596.             $total_payments = 0;
  2597.             foreach ($data['payments'] as $payment) {
  2598.                 $total_payments += $payment['payment_amount'];
  2599.             }
  2600.             $sale_id = $this->sale_lib->get_suspended_sale_id();
  2601. //SAVE sale to database
  2602.             $later_cost_price = $data['total'] - $discount_money;
  2603.             $actual_money = $payment['payment_amount'];
  2604.  
  2605.             if ($sale_id != '') {
  2606.                 $stt = 0;
  2607.                 $data['sale_id'] = $this->Sale->save_materials($data['cart'], $customer_id, $employee_id, $comment, $data['employees_id'], $show_comment_on_receipt, $data['payments'], $data['discount_money'], $later_cost_price, $actual_money, $data['amount_due'], $data['amount_due1'], $sale_id, 0, 0, $stt, 1, $date_debt);
  2608.             } else {
  2609.                 $stt = 0;
  2610.                 $data['sale_id'] = $this->Sale->save_materials($data['cart'], $customer_id, $employee_id, $comment, $data['employees_id'], $show_comment_on_receipt, $data['payments'], $data['discount_money'], $later_cost_price, $actual_money, $data['amount_due'], $data['amount_due1'], $sale_id, 0, 0, $stt, 1, $date_debt);
  2611.             }
  2612.             if ($data['sale_id'] == 'VH -1') {
  2613.                 $data['error_message'] = lang('sales_transaction_failed');
  2614.             }
  2615.  
  2616. // $this->load->view('sales/print_dat_hang', $data);
  2617. //require_once APPPATH . "/third_party/Classes/export_liability.php";
  2618.             $this->sale_lib->clear_all();
  2619.             $this->_reload(array('success' => lang('sales_successfully_liability_sale')));
  2620. //die('aaaaa');
  2621. //redirect('sales');  
  2622. // die('sssss');
  2623.         }
  2624.     }
  2625.  
  2626. //end huyenlt^^
  2627.  
  2628.     function unsuspend() {
  2629.         $sale_id = $this->input->post('suspended_sale_id');
  2630.         $this->sale_lib->clear_all();
  2631.         $this->sale_lib->copy_entire_sale($sale_id);
  2632.         $this->sale_lib->set_suspended_sale_id($sale_id);
  2633.         $this->_reload(array(), false);
  2634.     }
  2635.  
  2636.     function delete_all() {
  2637.         $this->sale_lib->empty_cart();
  2638.         $this->_reload();
  2639.     }
  2640.  
  2641.     function delete_suspended_sale() {
  2642.         $suspended_sale_id = $this->input->post('suspended_sale_id');
  2643.         if ($suspended_sale_id) {
  2644.             $this->sale_lib->delete_suspended_sale_id();
  2645.             $info_sale_liablity = $this->Sale->get_sales_tam($suspended_sale_id);
  2646.             $info_sale = $this->Sale->get_info_sale($suspended_sale_id);
  2647.             $info_cus = $this->Customer->get_info($info_sale['customer_id']);
  2648.             $total_pay_money = 0;
  2649.             foreach ($info_sale_liablity as $sale_liablity) {
  2650.                 $total_pay_money += $sale_liablity['pays_amount'];
  2651.             }
  2652.             $name = ($info_cus->company_name != "") ? $info_cus->company_name : ($info_cus->first_name . " " . $info_cus->last_name);
  2653.             $command = "Chi tiền trả lại tiền cho " . $name . " khi hủy đơn đặt hàng số " . $suspended_sale_id;
  2654.             $data_inser_cost = array(
  2655.                 'id_customer' => $info_sale['customer_id'],
  2656.                 'name' => 1,
  2657.                 'money' => $total_pay_money,
  2658.                 'form_cost' => 1,
  2659.                 'date' => date('Y-m-d H:i:s'),
  2660.                 'cost_date_ct' => date('Y-m-d'),
  2661.                 'comment' => $command,
  2662.                 'deleted' => 0,
  2663.                 'id_sale' => $suspended_sale_id,
  2664.                 'cost_employees' => $this->session->userdata('person_id'),
  2665.                 'tk_no' => 131,
  2666.                 'tk_co' => 111
  2667.             );
  2668.             $this->Cost->save($data_inser_cost, (-1));
  2669.             $this->Sale->delete_liablity($suspended_sale_id);
  2670.         }
  2671.         $this->sale_lib->clear_all();
  2672.         redirect('sales');
  2673.         $this->_reload(array('success' => lang('sales_successfully_deleted')), false);
  2674.     }
  2675.  
  2676.     function delete_detail_materials() {
  2677.         header('Content-Type: text/html; charset=utf-8');
  2678.         $suspended_sale_id = $this->input->post('suspended_sale_id');
  2679.         $customer_id = $this->input->post('suspended_customer_id');
  2680.         $info_sale_material = $this->Sale->get_sale_material($suspended_sale_id);
  2681.         if ($suspended_sale_id) {
  2682.             $this->sale_lib->delete_suspended_sale_id();
  2683.             $this->Sale->delete($suspended_sale_id);
  2684.             $this->load->model('Cost');
  2685.             $this->Cost->delete_sale_id($suspended_sale_id);
  2686.             foreach ($info_sale_material as $item) {
  2687.                 unlink(APPPATH . "/../excel_materials/" . $item['name']);
  2688.             }
  2689.             $this->Sale->delete_sale_material($suspended_sale_id);
  2690.         }
  2691.         $this->sale_lib->clear_all();
  2692.         redirect('customers/detail_customer_sale/' . $customer_id);
  2693.         $this->_reload(array('success' => lang('sales_successfully_deleted')), false);
  2694.     }
  2695.  
  2696.     function check_cong_no() {
  2697.         $customer_id = $_POST['customer_id'];
  2698.         $info_cus = $this->Customer->get_info($customer_id);
  2699.         $amount_final = $_POST['amount_final'];
  2700.         $sales = $this->Inventory->find_sale_complete_by_customer($customer_id);
  2701.         $total = 0;
  2702.         $to = 0;
  2703.         foreach ($sales as $sale) {
  2704.             $total += $sale['later_cost_price'];
  2705.             $sale_payments = $this->Sale->get_payment_sale_by_sale_id($sale['sale_id']);
  2706.             foreach ($sale_payments as $val) {
  2707.                 $to += $val['payment_amount'];
  2708.             }
  2709.         }
  2710.         $cong_no = $total - $to;
  2711.         if ($info_cus->debt != 0 && ($cong_no + $amount_final) > $info_cus->debt) {
  2712.             echo "0";
  2713.         } else {
  2714.             echo "1";
  2715.         }
  2716.     }
  2717.  
  2718.     function send_mail() {
  2719.         $config = Array(
  2720.             'protocol' => 'smtp',
  2721.             'smtp_host' => 'ssl://smtp.googlemail.com',
  2722.             'smtp_port' => 465,
  2723.             'smtp_user' => 'quangsan90@gmail.com',
  2724.             'smtp_pass' => 'vuvansan',
  2725.             'charset' => 'utf-8',
  2726.             'mailtype' => 'html',
  2727.         );
  2728.         $this->load->library('email', $config);
  2729.         $this->email->set_newline("\r\n");
  2730.         $mail_info = $this->Customer->get_info_mail($this->config->item('mail_template_birthday'));
  2731.         $this->email->subject($mail_info->mail_title);
  2732.         $content = $mail_info->mail_content;
  2733.         $cus_info = $this->Customer->get_info(16);
  2734.         $this->email->from($this->config->item('email'), $this->config->item('company'));
  2735.         $this->email->to('quangsancntt@gmail.com');
  2736.         $content = str_replace('__FIRST_NAME__', $cus_info->first_name, $content);
  2737.         $content = str_replace('__LAST_NAME__', $cus_info->last_name, $content);
  2738.         $content = str_replace('__PHONE_NUMBER__', $cus_info->phone_number, $content);
  2739.         $content = str_replace('__EMAIL__', $cus_info->email, $content);
  2740. //Thong tin chu ky cong ty gui mail
  2741.         $content = str_replace('__NAME_COMPANY__', '<b>' . $this->config->item('company') . '</b>', $content);
  2742.         $content = str_replace('__ADDRESS_COMPANY__', $this->config->item('address'), $content);
  2743.         $content = str_replace('__EMAIL_COMPANY__', $this->config->item('email'), $content);
  2744.         $content = str_replace('__FAX_COMPANY__', $this->config->item('fax'), $content);
  2745.         $content = str_replace('__WEBSITE_COMPANY__', $this->config->item('website'), $content);
  2746.         $this->email->message($content);
  2747.         if (!$this->email->send()) {
  2748.             show_error($this->email->print_debugger());
  2749.         } else {
  2750.             echo 'Your e-mail has been sent!';
  2751.             echo date('d-m-Y H:i:s');
  2752.         }
  2753.     }
  2754.  
  2755. //hung audi 29-6-15
  2756.     function customer_search_reports() {
  2757.         $suggestions = $this->Customer->get_customer_search_reports($this->input->get('term'), 100);
  2758.         echo json_encode($suggestions);
  2759.     }
  2760.  
  2761. //hoa don ban hang dungbv
  2762.     public function sales_order() {
  2763.         $this->check_action_permission('sales_order');
  2764.         $config['base_url'] = site_url('sales/sale_order_sorting');
  2765.         $config['total_rows'] = $this->Sale->count_all();
  2766.         $config['per_page'] = $this->config->item('number_of_items_per_page') ? (int) $this->config->item('number_of_items_per_page') : 20;
  2767.         $this->pagination->initialize($config);
  2768.         $data['pagination'] = $this->pagination->create_links();
  2769.         $data['controller_name'] = strtolower(get_class());
  2770.         $data['per_page'] = $config['per_page'];
  2771.         $data['manage_table'] = get_sales_orders_manage_table($this->Sale->get_all($data['per_page']), $this);
  2772.         $this->load->view('sales_order/manage', $data);
  2773.     }
  2774.  
  2775.     function sale_order_sorting() {
  2776.         $this->check_action_permission('sales_order');
  2777.         $start_date1 = $this->input->post('start_date');
  2778.         $end_date1 = $this->input->post('end_date');
  2779.         $tam = $end_date1;
  2780.         $tam .='23:59:59';
  2781.         $start_date = date('Y-m-d H:i:s', strtotime($start_date1));
  2782.         $end_date = date('Y-m-d H:i:s', strtotime($tam));
  2783.  
  2784.         $search = $this->input->post('search');
  2785.         $per_page = $this->config->item('number_of_items_per_page') ? (int) $this->config->item('number_of_items_per_page') : 20;
  2786.         if ($search || $start_date || $end_date) {
  2787.             $config['total_rows'] = $this->Sale->search_count_all($start_date, $end_date, $search);
  2788.             $table_data = $this->Sale->search(
  2789.                     $start_date, $end_date, $search, $per_page, $this->input->post('offset') ? $this->input->post('offset') : 0, $this->input->post('order_col') ? $this->input->post('order_col') : 'sale_id', $this->input->post('order_dir') ? $this->input->post('order_dir') : 'desc'
  2790.             );
  2791.         } else {
  2792.             $config['total_rows'] = $this->Sale->count_all();
  2793.             $table_data = $this->Sale->get_all(
  2794.                     $per_page, $this->input->post('offset') ? $this->input->post('offset') : 0, $this->input->post('order_col') ? $this->input->post('order_col') : 'sale_id', $this->input->post('order_dir') ? $this->input->post('order_dir') : 'desc'
  2795.             );
  2796.         }
  2797.         $config['base_url'] = site_url('sales/sale_order_sorting');
  2798.         $config['per_page'] = $per_page;
  2799.         $this->pagination->initialize($config);
  2800.         $data['pagination'] = $this->pagination->create_links();
  2801.         $data['manage_table'] = get_sales_orders_manage_table_data_rows($table_data, $this);
  2802.         echo json_encode(array('manage_table' => $data['manage_table'], 'pagination' => $data['pagination']));
  2803.     }
  2804.  
  2805.     function sale_order_suggest() {
  2806.         $suggestions = $this->Sale->get_search_suggestions($this->input->get('term'), 100);
  2807.         echo json_encode($suggestions);
  2808.     }
  2809.  
  2810.     function sale_order_search() {
  2811.         $this->check_action_permission('sales_order');
  2812.         $start_date1 = $this->input->post('start_date');
  2813.         $end_date1 = $this->input->post('end_date');
  2814.         $tam = $end_date1;
  2815.         $tam .='23:59:59';
  2816.         $start_date = date('Y-m-d H:i:s', strtotime($start_date1));
  2817.         $end_date = date('Y-m-d H:i:s', strtotime($tam));
  2818.  
  2819.         $search = $this->input->post('search');
  2820.         $per_page = $this->config->item('number_of_items_per_page') ? (int) $this->config->item('number_of_items_per_page') : 20;
  2821.         $search_data = $this->Sale->search(
  2822.                 $start_date, $end_date, $search, $per_page, $this->input->post('offset') ? $this->input->post('offset') : 0, $this->input->post('order_col') ? $this->input->post('order_col') : 'sale_id', $this->input->post('order_dir') ? $this->input->post('order_dir') : 'desc'
  2823.         );
  2824.         $config['base_url'] = site_url('sales/sale_order_search');
  2825.         $config['total_rows'] = $this->Sale->search_count_all($start_date, $end_date, $search);
  2826.         $config['per_page'] = $per_page;
  2827.         $this->pagination->initialize($config);
  2828.         $data['pagination'] = $this->pagination->create_links();
  2829.         $data['manage_table'] = get_sales_orders_manage_table_data_rows($search_data, $this);
  2830.         echo json_encode(array('manage_table' => $data['manage_table'], 'pagination' => $data['pagination']));
  2831.     }
  2832.  
  2833. //load order_view
  2834.     function view_order($sale_id = -1) {
  2835.         $data['sale_id'] = $sale_id;
  2836.         $data['info_sale'] = $this->Sale->get_info_sale_order($sale_id);
  2837.         $data['info_sale_item'] = $this->Sale->get_sale_item_order();
  2838.         $this->load->view('sales_order/view_order', $data);
  2839.     }
  2840.  
  2841. //delete sale_order
  2842.     public function delete_sale() {
  2843.         $this->check_action_permission('delete_sale');
  2844.         $ids = $this->input->post('ids');
  2845.         foreach ($ids as $id) {
  2846.             $data_delete = array('deleted' => 1);
  2847.             $this->Sale->delete_sale_order($data_delete, $id);
  2848.         }
  2849.         echo json_encode(array('success' => true, 'message' => 'Bạn đã xóa thành công hóa đơn số - ' . $id));
  2850.     }
  2851.  
  2852.     public function print_order($sale_id) {
  2853.         $this->check_action_permission("print_order");
  2854.         $data['sale_id'] = $sale_id;
  2855.         $customer_id = $this->sale_lib->get_customer();
  2856.         $cust_info = $this->Customer->get_info($customer_id);
  2857.         $data['address1'] = $cust_info->address_1;
  2858.         $data['info_sale'] = $this->Sale->get_info_sale_order($sale_id);
  2859.         $data['info_sale_item'] = $this->Sale->get_sale_item_order();
  2860.         if ($this->config->item('print_excel') == 'print_a5') {
  2861.             $this->load->view('sales_order/print_order_a5', $data);
  2862.         } elseif ($this->config->item('print_excel') == 'print') {
  2863.             $this->load->view("sales_order/print_a8", $data);
  2864.         } else {
  2865.             require_once APPPATH . "/third_party/Classes/export_receipt_order.php";
  2866.             redirect('sales/sales_order');
  2867.         }
  2868.     }
  2869.  
  2870. //end dungbv
  2871.     //Nov 3
  2872.     function set_bank_account() {
  2873.         $this->sale_lib->set_bank_account($this->input->post('bank_account'));
  2874.     }
  2875.     function set_payment_type() {
  2876.         $this->sale_lib->set_payment_type($this->input->post('payment_type'));
  2877.     }
  2878. }
  2879.  
  2880. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement