public function actionComplete($id)
{
$model = Apply::findOne(['id' => $id, 'status' => Apply::STATUS_PASSED, 'store_id' => Yii::$app->user->identity->store_id]);
if (!$model) {
throw new BadRequestHttpException('参数错误!');
}
$transaction = Yii::$app->db->beginTransaction();
try {
$model->status = Apply::STATUS_COMPLETED;
if (!$model->save(false)) {
throw new \Exception('操作失败!');
}
foreach ($model->goods as $modelGoods) {
if (!$modelGoods->goods->moveSurplus(+$modelGoods->count, "商品采购,采购单号:{$model->apply_sn}。")) {
throw new \Exception('商品库存累加失败!');
}
}
$modelApplyLog = new ApplyLog();
$modelApplyLog->apply_id = $id;
$modelApplyLog->remark = '店铺收货,商品库存自动累加,采购完成。';
if (!$modelApplyLog->save(false)) {
throw new \Exception('申请日志记录失败!');
}
$transaction->commit();
Yii::$app->session->setFlash('success', '操作成功!');
} catch (\Exception $e) {
$transaction->rollBack();
Yii::$app->session->setFlash('danger', $e->getMessage());
}
return $this->redirect(['view', 'id' => $id]);
}