Esempio n. 1
0
 function form($id = false)
 {
     $today_date = date("Ymd");
     //die(print_r($_POST));
     $this->load->helper(array('form', 'date', 'url'));
     $folderName = 'uploads/voucher/' . $today_date . '/';
     $config['upload_path'] = $folderName;
     if (!is_dir($folderName)) {
         mkdir($folderName, 0777, TRUE);
         //mkdir('./uploads/coupon/' . $today_date.'/thumbs', 0777, TRUE);
     }
     $config['allowed_types'] = 'gif|jpg|png';
     $config['max_size'] = $this->config->item('size_limit');
     $config['encrypt_name'] = true;
     $this->load->library('upload', $config);
     $this->load->library('form_validation');
     $branches = $this->Branch_model->get_branch_list($this->current_admin, TRUE);
     $branch_list = array();
     foreach ($branches as $branch) {
         $branch_list[$branch['id']] = $branch['name'];
     }
     $data['branches'] = $branch_list;
     $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
     $this->voucher_id = $id;
     $data['page_title'] = lang('voucher_form');
     //default values are empty if the product is new
     $data['id'] = '';
     $data['code'] = '';
     $data['name'] = '';
     $data['start_date'] = '';
     $data['whole_order_voucher'] = 1;
     $data['max_product_instances'] = '';
     $data['end_date'] = '';
     $data['max_uses'] = '';
     $data['reduction_target'] = 'price';
     $data['reduction_type'] = '';
     $data['reduction_amount'] = '';
     $data['point_consume'] = '';
     $data['credit_consume'] = '';
     $data['image'] = '';
     $data['desc'] = '';
     $data['branch_id'] = '';
     $added = array();
     if ($id) {
         $voucher = $this->Voucher_model->get_voucher($id);
         //if the product does not exist, redirect them to the product list with an error
         if (!$voucher) {
             $this->session->set_flashdata('message', lang('error_not_found'));
             redirect($this->config->item('admin_folder') . '/product');
         }
         //set values to db values
         $data['id'] = $voucher->id;
         $data['code'] = $voucher->code;
         $data['name'] = $voucher->name;
         $data['start_date'] = $voucher->start_date;
         $data['end_date'] = $voucher->end_date;
         //$data['whole_order_voucher']	= $voucher->whole_order_voucher;
         $data['whole_order_voucher'] = 1;
         $data['whole_order_voucher'] = $voucher->whole_order_voucher;
         $data['max_product_instances'] = $voucher->max_product_instances;
         $data['num_uses'] = $voucher->num_uses;
         $data['max_uses'] = $voucher->max_uses;
         //$data['reduction_target']		= $voucher->reduction_target;
         $data['reduction_target'] = 'price';
         $data['reduction_type'] = $voucher->reduction_type;
         $data['reduction_amount'] = $voucher->reduction_amount;
         $data['point_consume'] = $voucher->point_consume;
         $data['credit_consume'] = $voucher->credit_consume;
         $data['image'] = $voucher->image;
         $data['desc'] = $voucher->desc;
         $data['branch_id'] = $voucher->branch_id;
         $added = $this->Voucher_model->get_product_ids($id);
     }
     //Checking for super admin
     if ($this->current_admin['branch'] == 0) {
         $this->form_validation->set_rules('branch_id', 'lang:branch', 'trim|required');
     }
     $this->form_validation->set_rules('code', 'lang:code', 'trim|required|callback_check_code');
     $this->form_validation->set_rules('name', 'lang:name', 'trim|required');
     $this->form_validation->set_rules('max_uses', 'lang:max_uses', 'trim|numeric');
     $this->form_validation->set_rules('max_product_instances', 'lang:limit_per_order', 'trim|numeric');
     $this->form_validation->set_rules('whole_order_voucher', 'lang:whole_order_discount');
     //$this->form_validation->set_rules('reduction_target', 'lang:reduction_target', 'trim|required');
     $this->form_validation->set_rules('reduction_target', 'lang:reduction_target', 'trim');
     $this->form_validation->set_rules('reduction_type', 'lang:reduction_type', 'trim');
     $this->form_validation->set_rules('reduction_amount', 'lang:reduction_amount', 'trim|numeric');
     $this->form_validation->set_rules('point_consume', 'lang:point_consume', 'trim|numeric');
     $this->form_validation->set_rules('credit_consume', 'lang:credit_consume', 'trim|numeric');
     $this->form_validation->set_rules('image', 'lang:image', 'trim');
     $this->form_validation->set_rules('desc', 'lang:desc', 'trim');
     $this->form_validation->set_rules('start_date', 'lang:start_date');
     $this->form_validation->set_rules('end_date', 'lang:end_date');
     // create product list
     $products = $this->Product_model->get_products();
     // set up a 2x2 row list for now
     $data['product_rows'] = "";
     $x = 0;
     while (TRUE) {
         // Yes, forever, until we find the end of our list
         if (!isset($products[$x])) {
             break;
         }
         // stop if we get to the end of our list
         $checked = "";
         if (in_array($products[$x]->id, $added)) {
             $checked = "checked='checked'";
         }
         $data['product_rows'] .= "<tr><td><input type='checkbox' name='product[]' value='" . $products[$x]->id . "' {$checked}></td><td> " . $products[$x]->name . "</td>";
         $x++;
         //reset the checked value to nothing
         $checked = "";
         if (isset($products[$x])) {
             // if we've gotten to the end on this row
             if (in_array($products[$x]->id, $added)) {
                 $checked = "checked='checked'";
             }
             $data['product_rows'] .= "<td><input type='checkbox' name='product[]' value='" . $products[$x]->id . "' {$checked}><td><td> " . $products[$x]->name . "</td></tr>";
         } else {
             $data['product_rows'] .= "<td> </td></tr>";
         }
         $x++;
     }
     if ($this->form_validation->run() == FALSE) {
         $this->view($this->config->item('admin_folder') . '/voucher_form', $data);
     } else {
         $this->load->helper('text');
         $uploaded = $this->upload->do_upload('image');
         $save['id'] = $id;
         $save['code'] = $this->input->post('code');
         $save['name'] = $this->input->post('name');
         $save['start_date'] = format_ymd_malaysia($this->input->post('start_date'));
         $save['end_date'] = format_ymd_malaysia($this->input->post('end_date'));
         $save['max_uses'] = $this->input->post('max_uses');
         $save['whole_order_voucher'] = 1;
         //$save['whole_order_voucher'] 	= $this->input->post('whole_order_voucher');
         $save['max_product_instances'] = $this->input->post('max_product_instances');
         //$save['reduction_target']		= $this->input->post('reduction_target');
         $save['reduction_target'] = 'price';
         $save['reduction_type'] = $this->input->post('reduction_type');
         $save['reduction_amount'] = $this->input->post('reduction_amount');
         $save['point_consume'] = $this->input->post('point_consume');
         $save['credit_consume'] = $this->input->post('credit_consume');
         $save['desc'] = $this->input->post('desc');
         $save['staff_id'] = $this->current_admin['id'];
         $save['created_date'] = date('Y-m-d H:i:s');
         //Checking for super admin
         if ($this->current_admin['branch'] == 0) {
             $save['branch_id'] = $this->input->post('branch_id');
         } else {
             $save['branch_id'] = $this->current_admin['branch'];
         }
         if ($save['start_date'] == '') {
             $save['start_date'] = null;
         }
         if ($save['end_date'] == '') {
             $save['end_date'] = null;
         }
         $product = $this->input->post('product');
         if ($id) {
             //delete the original file if another is uploaded
             if ($uploaded) {
                 if ($data['image'] != '') {
                     //$file = 'uploads/'.$data['image'];
                     //$config['upload_path'] = FCPATH . 'uploads/';
                     $file = $folderName . $data['image'];
                     //delete the existing file if needed
                     if (file_exists($file)) {
                         unlink($file);
                     }
                 }
             }
         } else {
             if (!$uploaded) {
                 $data['error'] = $this->upload->display_errors();
                 $this->view(config_item('admin_folder') . '/voucher_form', $data);
                 return;
                 //end script here if there is an error
             }
         }
         if ($uploaded) {
             if (!is_dir($folderName)) {
                 mkdir($folderName, 0777, TRUE);
                 //mkdir('./uploads/voucher/' . $today_date.'/thumbs', 0777, TRUE);
             }
             $image = $this->upload->data();
             $save['image'] = $folderName . $image['file_name'];
             //$save['image']	= $image['file_name'];
         }
         // save voucher
         $promo_id = $this->Voucher_model->save($save);
         // save products if not a whole order voucher
         //   clear products first, then save again (the lazy way, but sequence is not utilized at the moment)
         $this->Voucher_model->remove_product($id);
         if (!$save['whole_order_voucher'] && $product) {
             while (list(, $product_id) = each($product)) {
                 $this->Voucher_model->add_product($promo_id, $product_id);
             }
         }
         // We're done
         $this->session->set_flashdata('message', lang('message_saved_voucher'));
         //go back to the product list
         redirect($this->config->item('admin_folder') . '/vouchers');
     }
 }
Esempio n. 2
0
 function get_voucher_trx($start, $end, $current_admin = false)
 {
     $this->db->select(' credit.*, credit.id as credit_id, branch.name as branch_name, customers.name as customer_name, customers.card as customer_card, vouchers.name as voucher_name');
     $this->db->join('branch', 'credit.branch_id = branch.id');
     $this->db->join('customers', 'customers.id = credit.customer_id');
     $this->db->join('vouchers', 'vouchers.id = credit.voucher_id');
     if (!empty($start)) {
         $this->db->where('created >=', format_ymd_malaysia($start) . ' 00:00:00');
     }
     if (!empty($end)) {
         $this->db->where('created <', format_ymd_malaysia($end) . ' 23:59:59');
     }
     $this->db->where('out > 0');
     if (isset($current_admin) && !empty($current_admin)) {
         if ($current_admin['branch'] > 0) {
             $this->db->where('credit.branch_id', $current_admin['branch']);
         }
     }
     // just fetch a list of order id's
     $credits = $this->db->get('credit')->result();
     return $credits;
 }
Esempio n. 3
0
 function consume_form($id = false)
 {
     $data['activemenu'] = $this->activemenu;
     //$this->load->helper('form');
     $this->load->helper(array('form', 'date'));
     $this->load->library('form_validation');
     $data['page_title'] = lang('consume_form');
     //default values are empty if the customer is new
     $data['id'] = '';
     $data['card'] = '';
     $data['created'] = '';
     $data['cost'] = '';
     $data['in'] = '';
     $data['out'] = '';
     $data['remark'] = '';
     $data['options'] = '';
     $data['active'] = false;
     $data['voucher_id'] = '';
     $vouchers = $this->Voucher_model->get_vouchers(NULL, $this->current_admin, TRUE);
     $voucher_list = array();
     foreach ($vouchers as $voucher) {
         $voucher_list[$voucher->id] = $voucher->name;
     }
     $data['vouchers'] = $voucher_list;
     $customer = '';
     if ($id) {
         //$this->customer_id	= $id;
         $customer = $this->Customer_model->get_customer_by_id($id);
         //if the customer does not exist, redirect them to the customer list with an error
         if (!$customer) {
             $this->session->set_flashdata('error', lang('error_not_found'));
             redirect($this->config->item('admin_folder') . '/credit');
         }
         //set values to db values
         $data['id'] = $customer->id;
         $data['name'] = $customer->name;
         $data['firstname'] = $customer->firstname;
         $data['lastname'] = $customer->lastname;
         $data['email'] = $customer->email;
         $data['phone'] = $customer->phone;
         $data['company'] = $customer->company;
         $data['active'] = $customer->active;
         $data['options'] = '';
         $data['email_subscribe'] = $customer->email_subscribe;
     }
     $this->form_validation->set_rules('card', 'lang:card', 'trim|required|max_length[255]|callback_check_card');
     $this->form_validation->set_rules('consume_date', 'lang:consume_date', 'trim|required');
     $this->form_validation->set_rules('consume_amount', 'lang:consume_amount', 'trim|required|numeric|callback_check_credit');
     $this->form_validation->set_rules('remark', 'lang:remark', 'trim|required');
     $this->form_validation->set_rules('payment', 'lang:payment', 'required');
     $this->form_validation->set_rules('voucher_id', 'lang:products');
     if ($this->form_validation->run() == FALSE) {
         $this->view($this->config->item('admin_folder') . '/consume_form', $data);
     } else {
         $card = $this->input->post('card');
         $customer = $this->Customer_model->get_customer_by_card($card);
         $payment = $this->input->post('payment');
         $consume_amount = $this->input->post('consume_amount');
         $voucher_id = $this->input->post('voucher_id');
         $remark = $this->input->post('remark');
         $payment = $this->input->post('payment');
         $last_id = '';
         //get voucher details for retrieve branch from:
         $voucher_row = $this->Voucher_model->get_voucher($voucher_id);
         if ($payment == 'Credit') {
             $save['id'] = $id;
             $save['customer_id'] = $customer['id'];
             $save['out'] = $consume_amount;
             $save['remark'] = $remark;
             $save['created'] = format_ymd_malaysia($this->input->post('consume_date'));
             $save['staff_id'] = $this->current_admin['id'];
             $save['voucher_id'] = $voucher_id;
             $save['branch_id'] = $voucher_row->branch_id;
             //$save['branch'] = $staff_branch;
             $last_id = $this->Credit_model->save_credit($save);
             //in same time, if credit consume can earn point:
             $point_in['id'] = '';
             $point_in['customer_id'] = $customer['id'];
             $point_in['point'] = $consume_amount;
             $point_in['created'] = format_ymd_malaysia($this->input->post('consume_date'));
             $point_in['staff_id'] = $this->current_admin['id'];
             $point_in['branch_id'] = $voucher_row->branch_id;
             //$point_in['branch'] = $staff_branch;
             $point_in['voucher_id'] = $voucher_id;
             //$point_in['status'] = 1; //enable
             $point_in['remark'] = 'Bonus Point from consumption';
             $this->Point_model->save_point($point_in);
         } else {
             $save['id'] = '';
             $save['customer_id'] = $customer['id'];
             $save['depoint'] = $consume_amount;
             $save['created'] = format_ymd_malaysia($this->input->post('consume_date'));
             $save['staff_id'] = $this->current_admin['id'];
             $save['branch_id'] = $voucher_row->branch_id;
             //$save['branch'] = $staff_branch;
             //$save['status'] = 1; //enable
             $save['remark'] = $remark;
             $save['voucher_id'] = $voucher_id;
             $last_id = $this->Point_model->save_point($save);
         }
         //check if voucher customer has this voucher then update
         $save_voucher['voucher_id'] = $voucher_id;
         $save_voucher['customer_id'] = $customer['id'];
         $is_exist = $this->Voucher_model->check_voucher_customer($voucher_id, $customer['id']);
         if ($is_exist) {
             $voucher_details = $this->Voucher_model->my_voucher_details($voucher_id, $customer['id']);
             $save_voucher['qty'] = $voucher_details['qty'] + 1;
             $this->Voucher_model->update_voucher_customer($save_voucher);
         } else {
             $this->Voucher_model->add_voucher_customer($save_voucher);
         }
         //$save['status']				= 1;
         //$save['active']				= $this->input->post('active');
         $this->session->set_flashdata('message', lang('message_saved_customer'));
         //go back to the credit
         //redirect($this->config->item('admin_folder').'/credit');
         redirect($this->config->item('admin_folder') . '/credit/consume_info/' . $last_id . '/' . $payment);
     }
 }