issue voucher for campaign的流程

代码在controllers/csa.php 的request_issue()里

 /*
  * @desc request issue vouchers for given campaign
  */
 function request_issue()
 {
     //@TODO: Lock table to check if value limit exceed.
     $post = $this->input->post();
     
     $summary = $this->voucher_api_campaign->campaign_get_summary( $post['cp_id'] );
     
     $this_batch_summary = $this->voucher_api->voucher_summary_by_prefix_suffix_no( $post['prefix'], $post['suffix'], $post['first'], $post['last'] );
     $batch_value = $this_batch_summary['total_value'];
     
     $rt = array();
     if($summary['value_limit'] < ($summary['value_pending'] + $summary['value_issued'] + $batch_value))
     {
      $rt['error'] = 'Voucher Value exceeded';
     }
     else
     {
      $option = array();
      $option['first'] = $post['first'];
      $option['last'] = $post['last'];
      $option['prefix'] = $post['prefix'];
      $option['suffix'] = $post['suffix'];
      $option['remarks'] = $post['remarks'];
      $option['campaign_id'] = $post['cp_id'];
      
      $option['from_stock'] = $this->_my_stock_id;
   $option['to_stock'] = $this->voucher_lkup->get_stock_id_by_name ( 'issued' );
      
      $this->voucher_api_campaign->campaign_request_issue( $option );
     }
     
     echo json_encode($rt);
 }

 

主要的流程是$this->voucher_api_campaign->campaign_request_issue( $option );

在models/campign_voucher.php  的 campaign_request_issue()内

 public function campaign_request_issue( $options )
 {
  $prefix = $options['prefix'];
  $suffix = $options['suffix'];
  $first = $options['first'];
  $last = $options['last'];
  $campaign_id = $options['campaign_id'];
  $campaign_details = $this->campaign_get_details ($campaign_id);
 
  $lock_id = $this->voucher_lkup->get_lock_id_by_name('request_issue');
 
  //update vouchers set is_locked = 'Y'
  $sql = <<<SQL
  UPDATE  $this->_voucher a
  SET is_locked = 'Y', lock_id = '$lock_id'
  WHERE prefix = '$prefix' AND suffix = '$suffix'
  AND   voucher_no BETWEEN $first AND $last
SQL;
 
  $this->db->query($sql);
 
  //add into in transfer.
  $batch_id = $this->create_vouchers_batch_by_numbers( $prefix, $suffix, $first, $last );
 
  $request_action_id = $this->voucher_lkup->get_action_id_by_name('request_issue');
  $approve_action_id = $this->voucher_lkup->get_action_id_by_name('approve_issue');
 
  $request_record = array();
  $request_record['from_stock'] = $options['from_stock'];
  $request_record['to_stock'] = $options['to_stock'];
  $request_record['request_remarks'] = $options['remarks'];
  $request_record['request_time'] = date('Y-m-d H:i:s');
  $request_record['request_action'] = $request_action_id;
  $request_record['batch_id'] = $batch_id;
  $request_record['approve_action'] = $approve_action_id;
  $request_record['request_user'] = $this->_user_id;
  $request_record['is_done'] = 'N';
  $this->db->insert( $this->_voucher_request_approve, $request_record );
  $request_id = $this->db->insert_id();
  
  //get campaign expiry date:
  if($campaign_details['expiry_type'] == 1)
  {
   $voucher_expiry_date = $campaign_details['expiry_date'];
  }
  else
  {
   $expiry_duration_value = empty($campaign_details['duration_value']) ? 1 : $campaign_details['duration_value'];
   $expiry_duration_unit = empty($campaign_details['duration_unit']) ? 'DAY' : $campaign_details['duration_unit'];
    
   $row = $this->db->query("SELECT DATE_ADD(CURDATE(), INTERVAL $expiry_duration_value $expiry_duration_unit) AS expiry_date")->row_array();
   $voucher_expiry_date = $row['expiry_date'];
  }
  
  $remarks_info = array();
  $remarks_info['Campaign Code'] = $campaign_details['campaign_code'];
  $remarks_info['Type'] = $this->voucher_lkup->get_type_desc_by_id( $campaign_details['type_id'] );
  $remarks_info['Expiry Date'] = $voucher_expiry_date;
  if(isset($options['remarks']) && !empty($options['remarks']))
  {
   $remarks_info['Remarks'] = $options['remarks'];
  }
  $remarks_text = $this->voucher_lkup->array_to_text( $remarks_info );
  
  $this->voucher_action_insert_by_prefix_suffix( $request_action_id, $prefix, $suffix, $first, $last, $remarks_text );
 
  $request_record = array();
  $request_record['campaign_id'] = $campaign_id;
  $request_record['request_id'] = $request_id;
  $this->db->insert( $this->_voucher_campaign_issue_request, $request_record );
  
  $action_info = array();
  $action_info['campaign_details'] = $campaign_details;
  $action_info['remarks'] = $options['remarks'];
  $action_info['campaign_type'] = $this->voucher_lkup->get_type_by_id($campaign_details['type_id']);
  
  $first_voucher = $this->_voucher_get_by_prefix_suffix_no( $prefix, $suffix, $first );
  $last_voucher = $this->_voucher_get_by_prefix_suffix_no( $prefix, $suffix, $last );
  $action_info['first_voucher'] = $first_voucher['prefix'] . $first_voucher['voucher_text'] . $first_voucher['suffix'];
  $action_info['last_voucher'] = $last_voucher['prefix'] . $last_voucher['voucher_text'] . $last_voucher['suffix'];
  
  $this->load->model('email_notifications');
  $this->email_notifications->action_trigger_notification( $request_action_id, $action_info );
  
  return TRUE;
 }

1.将相关voucher锁住

2.创建batch记录

$batch_id = $this->create_vouchers_batch_by_numbers( $prefix, $suffix, $first, $last );

3.插入记录到 Table -> voucher_request_approve 

4.通过voucher_action_insert_by_prefix_suffix() 插入voucher_action 表

5.插入voucher_campaign_issue_request 记录 campaign_id 和 request_id

6.最后整理notinotification的一些数据发送邮件

 

大致的过程如上。

关于mysql having的使用

以前不常用这个函数having,今天一个东西要对比不同的数据。我就有点摸不着头脑了,应为以前对写mysql sql并不是很熟练,然后鸿池发给我如下SQL语句:

select *
from `voucher_campaign_corporate_conversion` m
left join (
 select b.campaign_id, sum(a.voucher_value) as issued_amount, count(distinct a.voucher_id) as issued_vouchers
 from voucher a, voucher_campaign b, voucher_campaign_issue c
 where a.voucher_id = c.voucher_id
 and b.campaign_id = c.campaign_id
 and c.is_deleted = 'N'
 and b.type_id = 10
 group by 1
) n on m.campaign_id = n.campaign_id
having m.issue_amount != n.issued_amount

 

其中用到了having 对查询出来的数据与表里的issued_amount进行对比,找出数据不相同的行,很好用。