dev

Thêm thanh toán shopee vào hệ thống thanh toán trên hệ thống

Written by anhhien

Thêm thanh toán shopee vào hệ thống sẽ giúp kiểm tra đơn hàng được thanh toán hay chưa giúp kế toán .File thanh toán demo ở đây ShopeePay.ps_reports_wallet_all.20200801_20200903

Đầu tiên cần hiển thị đường dẫn đến mục thanh toán trên menu

<li id="products_count_stock">
      <a class="submenu" href="<?= admin_url('sales/payment_shopee'); ?>">
             <i class="fa fa-plus-circle"></i>
                     <span class="text"> Up file thanh toán shopee</span>
            </a>
</li>

Tiếp theo trong phần đường link upload file ta cần set mod 777

Trong mục controller Sales :

public function payment_shopee()
    {
        $this->sma->checkPermissions('payments', true);
        $paid_by = $this->input->post('paid_by');
        
        if (isset($_FILES["userfile"])) {
                $this->load->library('upload');
                $config['upload_path'] = $this->digital_upload_path;
                $config['allowed_types'] = 'csv';
                $config['max_size'] = $this->allowed_file_size;
                $config['overwrite'] = true;
                $this->upload->initialize($config);
                if (!$this->upload->do_upload()) {
                    echo $this->upload->display_errors();
                    die;
                    $error = $this->upload->display_errors();
                    $this->session->set_flashdata('error', $error);
                    admin_redirect("sales/payment_shopee");
                }
            $csv = $this->upload->file_name;
            $data['attachment'] = $csv;
            $arrResult = array();
            // đọc dữ liệu
            $handle = fopen($this->digital_upload_path . $csv, "r");
            if ($handle) {
                while (($row = fgetcsv($handle, 1000000, ",")) !== false) {
                    $arrResult[] = $row;
                }
                fclose($handle);
            }
            #xóa file xls;
            unlink($this->digital_upload_path . $csv);
            
            $titles = array_shift($arrResult);
            //phân tích dữ liệu
            // lấy id các đơn hàng trong file đã sang giao nhận
            $array_id_donhang = array();
            $array_infocoban_donhang = array();
            foreach ($arrResult as $key => $value) {
                if (!in_array($value[0], $array_id_donhang) && $value[5] != '') {
                    $array_id_donhang[] = $value[0];
                    $array_infocoban_donhang[] = $value;
                }
            }
            for ($i = 4; $i < count($array_infocoban_donhang); $i++) {
                $pattern = '/#(.+)/';
                $chuoi_donhangcanloc = $array_infocoban_donhang[$i][3];
                /* không có điều chỉnh chuỗi 
                preg_match($pattern, $chuoi_donhangcanloc, $chuoi_id_donhang);
                $id_donhang = $chuoi_id_donhang[1];
                $sotien_donhang = $array_infocoban_donhang[$i][2];
                echo $id_donhang;
                echo '<br/>';
                */
                $id_donhang = str_replace('Doanh Thu từ Đơn Hàng #2', '2', $chuoi_donhangcanloc);
                $id_donhang = str_replace('Adjustment for 2', '2', $id_donhang);
                $id_donhang = str_replace('Adjustment', '', $id_donhang);
                
                // chuẩn bị add thanh toán vào 
                $paid_by ='cash';
                $cc_type = 'Visa';
                $sale = $this->sales_model->getInfoSalebyReference_no($id_donhang);
                $sale_id =  $sale->id;
                $payment_status = $sale->payment_status;
                $date = date('Y-m-d H:i:s');
                if ($payment_status =='pending' || $payment_status =='due')
                {
                    $payment = array(
                        'date' => $date,
                        'sale_id' => $sale_id,
                        'reference_no' => $this->input->post('reference_no') ? $this->input->post('reference_no') : $this->site->getReference('pay'),
                        'amount' => $sale->grand_total ,
                        'paid_by' => $paid_by,
                        'cc_type' => $cc_type,
                        'cc_no' => $this->input->post('paid_by') == 'gift_card' ? $this->input->post('gift_card_no') : $this->input->post('pcc_no'),
                        'created_by' => $this->session->userdata('user_id'),
                        'type' => 'received',
                    );
                    
                    $customer_id = null;
                    $status_payment = $this->sales_model->addPayment($payment, $customer_id);
                    
                }
                //$this->sma->print_arrays($payment);
            }
            $this->session->set_flashdata('message', 'Thêm thanh toán thành công');
            
            admin_redirect("sales");
            
        } else {
            
            $data['error'] = (validation_errors() ? validation_errors() : $this->session->flashdata('error'));
            $bc = array(array('link' => base_url(), 'page' => lang('home')), array('link' => admin_url('sales'), 'page' => lang('sales')), array('link' => '#', 'page' => lang('Thêm file xls từ shopee ')));
            $meta = array('page_title' => lang('Thêm thanh toán bằng csv'), 'bc' => $bc);
            $this->page_construct('sales/payment_shopee', $meta, $this->data);
        }
    }

Trong model : Sales_model.php thêm function

public function getInfoSalebyReference_no($reference_no) {
        $q = $this->db->get_where('sales', array('reference_no' => $reference_no), 1);
        if ($q->num_rows() > 0) {
            return $q->row();
        }

        return FALSE;
    }

 

Trong mục views ở sale thêm file payment_shopee.php

<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<?php

header('Content-Type: text/html; charset=UTF-8');

?>

<div class="box">
    <div class="box-header">
        <h2 class="blue"><i class="fa-fw fa fa-plus"></i><?= lang('Thanh toán shopee '); ?></h2>
    </div>
    <div class="box-content">
        <div class="row">
            <div class="col-lg-12">

                <p class="introtext"><?php echo lang('enter_info'); ?></p>
                <?php
                $attrib = array('data-toggle' => 'validator', 'role' => 'form');
                echo admin_form_open_multipart("sales/payment_shopee", $attrib);

                ?>
                <div class="row">
                    <div class="col-lg-12">
                    
                        
                        <div class="col-md-6">
                            <div class="form-group">
                                <h2>File thanh toán shopee</h2>
                                <input type="file" data-browse-label="<?= lang('browse'); ?>" name="userfile" required="required"
                                       data-show-upload="false" data-show-preview="false" class="form-control file">
                            </div>
                            
                        </div>
                         <div class="clearfix"></div>
                        <div class="col-sm-4">
                                                <div class="form-group">
                                                    <?= lang("paying_by", "paid_by_1"); ?>
                                                    <select name="paid_by" id="paid_by_1" class="form-control paid_by">
                                                        <?= $this->sma->paid_opts(); ?>
                                                    </select>
                                                </div>
                                            </div>
                        <div class="col-md-12">
                            <div
                                class="fprom-group"><?php echo form_submit('add_sale', $this->lang->line("submit"), 'id="add_sale" class="btn btn-primary" style="padding: 6px 15px; margin:15px 0;"'); ?>
                                <button type="button" class="btn btn-danger" id="reset"><?= lang('reset') ?></div>
                        </div>	
                        
                        
                       
                <?php echo form_close(); ?>

            </div>

        </div>
    </div>
</div>

<script type="text/javascript">
    
    $(document).ready(function () {
        var $customer = $('#slcustomer');
    $customer.change(function (e) {
        localStorage.setItem('slcustomer', $(this).val());
        //$('#slcustomer_id').val($(this).val());
    });
    if (slcustomer = localStorage.getItem('slcustomer')) {
        $customer.val(slcustomer).select2({
            minimumInputLength: 1,
            data: [],
            initSelection: function (element, callback) {
                $.ajax({
                    type: "get", async: false,
                    url: site.base_url+"customers/getCustomer/" + $(element).val(),
                    dataType: "json",
                    success: function (data) {
                        callback(data[0]);
                    }
                });
            },
            ajax: {
                url: site.base_url + "customers/suggestions",
                dataType: 'json',
                quietMillis: 15,
                data: function (term, page) {
                    return {
                        term: term,
                        limit: 10
                    };
                },
                results: function (data, page) {
                    if (data.results != null) {
                        return {results: data.results};
                    } else {
                        return {results: [{id: '', text: 'No Match Found'}]};
                    }
                }
            }
        });
        if (count > 1) {
            $customer.select2("readonly", true);
            $customer.val(slcustomer);
            $('#slwarehouse').select2("readonly", true);
            //$('#slcustomer_id').val(slcustomer);
        }
    } else {
        nsCustomer();
    }
    
// Order level shipping and discount localStorage 
if (sldiscount = localStorage.getItem('sldiscount')) {
    $('#sldiscount').val(sldiscount);
}
$('#sltax2').change(function (e) {
    localStorage.setItem('sltax2', $(this).val());
});
if (sltax2 = localStorage.getItem('sltax2')) {
    $('#sltax2').select2("val", sltax2);
}
$('#slsale_status').change(function (e) {
    localStorage.setItem('slsale_status', $(this).val());
});
if (slsale_status = localStorage.getItem('slsale_status')) {
    $('#slsale_status').select2("val", slsale_status);
}


var old_payment_term;
$('#slpayment_term').focus(function () {
    old_payment_term = $(this).val();
}).change(function (e) {
    var new_payment_term = $(this).val() ? parseFloat($(this).val()) : 0;
    if (!is_numeric($(this).val())) {
        $(this).val(old_payment_term);
        bootbox.alert('Unexpected value provided!');
        return;
    } else {
        localStorage.setItem('slpayment_term', new_payment_term);
        $('#slpayment_term').val(new_payment_term);
    }
});
if (slpayment_term = localStorage.getItem('slpayment_term')) {
    $('#slpayment_term').val(slpayment_term);
}

var old_shipping;
$('#slshipping').focus(function () {
    old_shipping = $(this).val();
}).change(function () {
    if (!is_numeric($(this).val())) {
        $(this).val(old_shipping);
        bootbox.alert('Unexpected value provided!');
        return;
    } else {
        shipping = $(this).val() ? parseFloat($(this).val()) : '0';
    }
    localStorage.setItem('slshipping', shipping);
    var gtotal = ((total + product_tax + invoice_tax) - total_discount) + shipping;
    $('#gtotal').text(formatMoney(gtotal));
});
if (slshipping = localStorage.getItem('slshipping')) {
    shipping = parseFloat(slshipping);
    $('#slshipping').val(shipping);
} else {
    shipping = 0;
}

$('#slref').change(function (e) {
    localStorage.setItem('slref', $(this).val());
});
if (slref = localStorage.getItem('slref')) {
    $('#slref').val(slref);
}

$('#slwarehouse').change(function (e) {
    localStorage.setItem('slwarehouse', $(this).val());
});
if (slwarehouse = localStorage.getItem('slwarehouse')) {
    $('#slwarehouse').select2("val", slwarehouse);
}

        // prevent default action usln enter
$('body').bind('keypress', function (e) {
    if (e.keyCode == 13) {
        e.preventDefault();
        return false;
    }
});

// Order tax calcuation 
if (site.settings.tax2 != 0) {
    $('#sltax2').change(function () {
        localStorage.setItem('sltax2', $(this).val());
        loadItems();
        return;
    });
}

// Order discount calcuation 
var old_sldiscount;
$('#sldiscount').focus(function () {
    old_sldiscount = $(this).val();
}).change(function () {
    var new_discount = $(this).val() ? $(this).val() : '0';
    if (is_valid_discount(new_discount)) {
        localStorage.removeItem('sldiscount');
        localStorage.setItem('sldiscount', new_discount);
        loadItems();
        return;
    } else {
        $(this).val(old_sldiscount);
        bootbox.alert('Unexpected value provided!');
        return;
    }

});

//$(document).on('change', '#slnote', function (e) {
        // $('#slnote').redactor('destroy');
        // $('#slnote').redactor({
        //     buttons: ['formatting', '|', 'alignleft', 'aligncenter', 'alignright', 'justify', '|', 'bold', 'italic', 'underline', '|', 'unorderedlist', 'orderedlist', '|', 'link', '|', 'html'],
        //     formattingTags: ['p', 'pre', 'h3', 'h4'],
        //     minHeight: 100,
        //     changeCallback: function (e) {
        //         var v = this.get();
        //         localStorage.setItem('slnote', v);
        //     }
        // });
        // if (slnote = localStorage.getItem('slnote')) {
        //     $('#slnote').redactor('set', slnote);
        // }
        // $('#slinnote').redactor('destroy');
        // $('#slinnote').redactor({
        //     buttons: ['formatting', '|', 'alignleft', 'aligncenter', 'alignright', 'justify', '|', 'bold', 'italic', 'underline', '|', 'unorderedlist', 'orderedlist', '|', 'link', '|', 'html'],
        //     formattingTags: ['p', 'pre', 'h3', 'h4'],
        //     minHeight: 100,
        //     changeCallback: function (e) {
        //         var v = this.get();
        //         localStorage.setItem('slinnote', v);
        //     }
        // });
        // if (slinnote = localStorage.getItem('slinnote')) {
        //     $('#slinnote').redactor('set', slinnote);
        // }

    });

function nsCustomer() {
    $('#slcustomer').select2({
        minimumInputLength: 1,
        ajax: {
            url: site.base_url + "customers/suggestions",
            dataType: 'json',
            quietMillis: 15,
            data: function (term, page) {
                return {
                    term: term,
                    limit: 10
                };
            },
            results: function (data, page) {
                if (data.results != null) {
                    return {results: data.results};
                } else {
                    return {results: [{id: '', text: 'No Match Found'}]};
                }
            }
        }
    });
}
</script>

 

About the author

anhhien