中标后生成订单

/**

* 中标后生成订单

* 准备废弃

*/

public function create_order() {

if (!$quotation_id = input('get.quotation_id', false)) {

return $this->api_error(130007);

}

//检查该报价单是否中标

$self_info = $this->model->__find($quotation_id, false, true);

if ($self_info === false) {

return $this->api_error(-1);

}

if (!$self_info) {

return $this->api_error(130007);

}

if ($self_info['is_bidding'] == 2) {

return $this->api_error(130007);

}

$responsible_userid_list = model('supplier/Supplier')->__getContactBySecondId($self_info['company_id'], $this->user_info['company_id']);

//没有供应商负责人

if (empty($responsible_userid_list)) {

return $this->api_error(90008);

}

//开始生成订单

$params = [];

$params['responsible_userid_list'] = $responsible_userid_list;

$params['company_id'] = $this->user_info['company_id'];

$params['create_user'] = $this->user_info['userid'];

$params['create_time'] = time();

$params['id'] = uuid_create(1);

$params['first_com_id'] = $this->user_info['company_id'];

$params['second_com_id'] = $self_info['company_id'];

//折扣后合计金额

$params['order_price'] = $self_info['discount_amount'];

$params['order_status'] = 1;

//生成订单编码

$params['order_code'] = date('md') . str_pad(mt_rand(1, 999999), 7, '0', STR_PAD_LEFT);

$task_quo_detail = $this->model->getTaskIdListById($quotation_id);

$task_detail_id_list = array_column($task_quo_detail, 'task_detail_id');

$errcode = 0;

//任务明细数据状态一致性校验

if (!$this->uniformity_check($task_detail_id_list, 'check', 'create_order')) {

//$errcode = 90007;

}

//任务状态为未指派的才可以

if ($errcode == 0 && $check_state = model('task/taskDetail')->__checkTaskDetailState($task_detail_id_list)) {

if ($check_state === -1) {

$errcode = -1;

}

if ($check_state === false) {

$errcode = 90007;

}

}

if ($errcode != 0) {

if (!$this->uniformity_check($task_detail_id_list, 'remove', 'create_order')) {

$errcode = 90007;

}

return $this->api_error($errcode);

}

Db::startTrans();

try {

$errcode = 1;

//回填询价单状态 已生成订单

if (false === model('comparison/Enquiry')->update(['id' => $self_info['enquiry_id'], 'has_order' => 1])) {

$errcode = -1;

}

//创建订单

$order_model = new JsonModel('order');

if (!$order_model->save($params)) {

$errcode = 90010;

}

if ($errcode == 1) {

//创建订单明细

$task_detail_list = [];

$time = time();

foreach ($task_quo_detail as $value) {

$item = [];

$item['id'] = uuid_create(1);

$item['order_id'] = $params['id'];

$item['company_id'] = $this->user_info['company_id'];

$item['create_user'] = $this->user_info['userid'];

$item['create_time'] = $time;

//订单明细状态说明  1 待确认  2 未开始 3 已开始 4 已完成 5 已关闭

$item['order_detail_status'] = 1;

$item['task_detail_id'] = $value['task_detail_id'];

$item['price'] = $value['unit_price'] + 0;

$item['quantity'] = $value['task_count'] + 0;

$task_detail_list[] = $item;

}

}

if ($errcode == 1 && !model('order/OrderDetails')->__create($task_detail_list)) {

$errcode = 90011;

}

if ($errcode == 1 && false === controller('task/TaskDetail')->updateStatus($task_detail_id_list, 2)) {

$errcode = -1;

}

//置空

if (!$this->uniformity_check($task_detail_id_list, 'remove', 'create_order')) {

$errcode = 90007;

}

if ($errcode == 1) {

Db::commit();

} else {

Db::rollback();

}

//发送乙方通知消息

//msg_type: 1 订单待确认 2 订单确认 3 订单退回 4 进度反馈

$send_data = [

'order_id' => $params['id'],

'to_userid' => $responsible_userid_list[0],

'msg_type' => 1,

];

controller('message/Message')->send($send_data);

} catch (\Exception $ex) {

$errcode = -1;

Db::rollback();

}

return $this->api_error($errcode);

}

你可能感兴趣的:(中标后生成订单)