| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\plugins\product_traceability\models\form;
- use app\constants\OptionSetting;
- use app\models\Option;
- use app\plugins\product_traceability\models\ProductBatch;
- use app\plugins\product_traceability\models\ProductBatchProcess;
- use app\plugins\product_traceability\models\ProductBatchProcessLog;
- use app\plugins\product_traceability\models\ProductBatchProcessLogSheet;
- use app\plugins\product_traceability\models\ProductBatchProcessSheet;
- use yii\base\Model;
- use yii\helpers\Json;
- use app\jobs\SyncMdGoodsJob;
- use app\utils\Tools;
- class ProductBatchProcessForm extends Model
- {
- public $id;
- public $store_id;
- public $process_user_id;
- public $product_batch_id;
- public $process_name;
- public $sort;
- public $is_change_sheet;
- public $form_list;
- /**
- * 初始化
- */
- public function init()
- {
- $this->store_id = get_store_id();
- }
- /**
- * @return array
- */
- public function rules()
- {
- return [
- [['store_id', 'process_user_id', 'product_batch_id','form_list', 'process_name', 'sort'], 'required'],
- [['store_id', 'id', 'process_user_id', 'product_batch_id', 'is_change_sheet' ,'sort', 'id'], 'integer'],
- [['process_name',], 'string', 'max' => 255],
- [['form_list',], 'safe',],
- [['product_batch_id', 'is_change_sheet'], 'default', 'value' => 0],
- ];
- }
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'store_id' => '商城id',
- 'process_name' => '流程名称',
- 'process_user_id' => '流程操作用户',
- 'product_batch_id' => '批次id',
- 'created_at' => '创建时间',
- 'updated_at' => '更新时间',
- 'is_delete' => 'Is Delete',
- 'sort' => 'sort',
- ];
- }
- public function save()
- {
- if(!$this->validate()) {
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0],
- ];
- }
- if (!$this->form_list) {
- return [
- 'code'=>1,
- 'msg'=>'请填写流程表单设置'
- ];
- }
- $t = \Yii::$app->db->beginTransaction();
- $product_batch = ProductBatch::findOne($this->product_batch_id);
- if ($this->id) {
- $product_batch_process = ProductBatchProcess::findOne($this->id);
- } else {
- $product_batch_process = new ProductBatchProcess();
- }
- $is_exits_sort =ProductBatchProcess::find()->where(['product_batch_id' => $this->product_batch_id, 'sort' => $this->sort, 'is_delete' => 0])->one();
- if ($is_exits_sort && $is_exits_sort->id != $this->id){
- return [
- 'code'=>1,
- 'msg'=>'该排序序号已存在'
- ];
- }
- $product_batch_process->store_id = $this->store_id;
- $product_batch_process->process_name = $this->process_name;
- $product_batch_process->sort = $this->sort;
- $product_batch_process->process_user_id = $this->process_user_id ? $this->process_user_id : 0;
- $product_batch_process->product_batch_id = $this->product_batch_id ? $this->product_batch_id : 0;
- if ($product_batch_process->save()) {
- // 自定义表单
- if ($this->is_change_sheet) {
- ProductBatchProcessSheet::updateAll(['is_delete' => 1], ['store_id' => $this->store_id, 'product_batch_process_id' => $product_batch_process->id]);
- }
- if ($this->form_list && ($this->id == 0 || ($this->id > 0 && $this->is_change_sheet == 1))) {
- $this->form_list = array_values($this->form_list);
- foreach ($this->form_list as $index => $value) {
- if (!$value['name']) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '请输入字段名称'
- ];
- }
- if (in_array($value['type'], ['radio', 'checkbox'])) {
- if (!$value['default']) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '请输入单选或多选的默认值'
- ];
- }
- }
- if ($value['id']) {
- $product_batch_process_sheet = ProductBatchProcessSheet::findOne(['store_id' => $this->store_id, 'id' => $value['id']]);
- } else {
- $product_batch_process_sheet = new ProductBatchProcessSheet();
- $product_batch_process_sheet->product_batch_process_id = $product_batch_process['id'];
- }
- $product_batch_process_sheet->is_delete = 0;
- $product_batch_process_sheet->type = $value['type'];
- $product_batch_process_sheet->name = $value['name'];
- $product_batch_process_sheet->default = $value['default'];
- $product_batch_process_sheet->required = $value['required'] ? $value['required'] : 0;
- $product_batch_process_sheet->tip = $value['tip'];
- $product_batch_process_sheet->sort = $index;
- $product_batch_process_sheet->store_id = $this->store_id;
- if (!$product_batch_process_sheet->save()) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $product_batch_process_sheet->errors[0]
- ];
- }
- }
- }
- $product_batch_process_log = ProductBatchProcessLog::find()->where([
- 'store_id' => $this->store_id,
- 'product_id' => $product_batch->product_id,
- 'product_batch_id' => $product_batch->id,
- 'product_batch_process_id' => $product_batch_process->id,
- ])->one();
- if (!$product_batch_process_log) {
- $product_batch_process_log = new ProductBatchProcessLog();
- $product_batch_process_log->store_id = $this->store_id;
- $product_batch_process_log->product_id = $product_batch->product_id;
- $product_batch_process_log->product_batch_id = $product_batch->id;
- $product_batch_process_log->product_batch_process_id = $product_batch_process->id;
- $product_batch_process_log->state = 0;
- }
- if ($this->is_change_sheet && $product_batch_process_log) {
- $product_batch_process_log->state = 0;
- ProductBatchProcessLogSheet::updateAll(['is_delete' => 1], ['process_log_id' => $product_batch_process_log->id]);
- }
- $product_batch_process_list = ProductBatchProcess::find()->where(['product_batch_id' => $product_batch->id, 'is_delete' => 0])->all();
- foreach ($product_batch_process_list as $k => $v){
- $up_process = ProductBatchProcess::find()->alias('pbp')
- ->where(['pbp.product_batch_id' => $product_batch->id, 'is_delete' => 0])
- ->andWhere(['<','pbp.sort', $v->sort])->orderBy(['pbp.sort' => SORT_DESC])->one();
- $v->up_process_id = $up_process->id ? $up_process->id : 0;
- $down_process = ProductBatchProcess::find()->alias('pbp')
- ->where(['pbp.product_batch_id' => $product_batch->id, 'is_delete' => 0])
- ->andWhere(['>','pbp.sort', $v->sort])->orderBy(['pbp.sort' => SORT_ASC])->one();
- $v->down_process_id = $down_process->id ? $down_process->id : 0;
- if (!$v->save()) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $v->errors[0]
- ];
- }
- }
- if (!$product_batch_process_log->save()) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $product_batch_process_log->errors[0]
- ];
- }
- $product_batch->state = 1;
- if (!$product_batch->save()) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $product_batch->errors[0]
- ];
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '保存成功'
- ];
- } else {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '添加失败'
- ];
- }
- }
- public function getEdit($id)
- {
- if ($id) {
- $goods = Goods::findOne($id);
- if (empty($goods)) {
- return [
- 'code' => 1,
- 'msg' => '商品获取失败'
- ];
- }
- $getAttrGroupList = $goods->getAttrGroupList();
- $goods = $goods->toArray();
- $cat_id = GoodsCat::find()
- ->where(['goods_id' => $goods['id'], 'is_delete' => 0])
- ->select(['cat_id'])->asArray()->all();
- $cat_id = array_column($cat_id, 'cat_id');
- $goods['cat_id'] = $cat_id;
- $goods['integral'] = json_decode($goods['integral'],true);
- $goods['goods_pic_list'] = GoodsPic::find()->select(['pic_url'])
- ->where(['goods_id' => $id])->asArray()->all();
- foreach ($goods as &$val) {
- $val = is_int($val) ? (string)$val : $val;
- }
- $goods['content'] = $goods['detail'];
- } else {
- $goods = null;
- }
- $send_type = Option::get(OptionSetting::STORE_SEND_TYPE, get_store_id(), 'store')['value'];
- $send_type = Option::get(OptionSetting::STORE_SEND_TYPE, get_store_id(), 'pay', $send_type);
- $send_type = $send_type ? Json::decode($send_type['value']) : [];
- $send_type_arr = [];
- foreach ((array)$send_type as $key => $send) {
- if ($send['value'] == 1) {
- $send_type_arr[$key] = $send['text'];
- }
- }
- $arr = empty($send_type_arr) ? ['express' => '快递', 'shop' => '自提'] : $send_type_arr;
- $goods['goods_delivery_type'] = !empty($goods['delivery_type']) ? Json::decode($goods['delivery_type']) : array_keys($arr);
- $goods['delivery_type'] = $arr;
- // 获取运费规则
- $postage = PostageRules::find()->where([
- 'store_id' => get_store_id(),
- 'is_delete' => 0,
- 'mch_id' => 0
- ])->select(['id', 'name', 'is_enable'])->asArray()->all();
- // 获取所有的会员等级
- $level = Level::find()->where([
- 'store_id' => get_store_id(),
- 'is_delete' => 0,
- 'status' => 1
- ])->select(['level as id', 'name'])->orderBy(['id' => SORT_DESC])->asArray()->all();
- // 卡券
- $verify_list = VerifyCard::find()
- ->where([
- 'store_id' => get_store_id(),
- 'is_delete' => 0
- ])->orderBy(['sort' => SORT_ASC])
- ->select([
- '*'
- ])->asArray()
- ->all();
- $verify_list = Tools::getVerifyList($verify_list);
- $goods_verify_card_list = [];
- // 获取商品已添加核销卡
- if (!empty($goods) && !empty($goods['verify_card_id'])) {
- $verify_cards = explode(',', $goods['verify_card_id']);
- foreach ($verify_cards as $k => $v) {
- $res = VerifyCard::find()
- ->select(['id','name'])
- ->where(['id' => $v,'is_delete' => 0])
- ->asArray()->one();
- if ($res) {
- $goods_verify_card_list[] = $res;
- }
- }
- }
- // 服务商品预约
- $goods['service_book'] = [
- 'date' => 0,
- 'data' => [],
- 'flag' => 1
- ];
- $goods['date_book'] = [
- 'data' => [],
- 'date' => 30
- ];
- $goods['form'] = [
- 'form_list' => [],
- 'is_form' => 0,
- 'form_name' => ''
- ];
- if (isset($goods['id'])) {
- $goods_book_config = GoodsBook::findOne(['goods_id' => $goods['id']]);
- if ($goods_book_config) {
- $service_book = $goods_book_config->service_book;
- $date_book = $goods_book_config->date_book;
- if ($service_book) {
- $goods['service_book'] = Json::decode($service_book);
- }
- if ($date_book) {
- $goods['date_book']['data'] = Json::decode($date_book);
- $goods['date_book']['date'] = count($goods['date_book']['data']);
- }
- }
- $goods['form']['form_list'] = Form::find()->where(['is_delete' => 0, 'store_id' => $this->store_id, 'goods_id' => $goods['id']])->orderBy(['sort' => SORT_ASC])->asArray()->all();
- $goods['form']['is_form'] = intval($goods['is_form']);
- $goods['form']['form_name'] = $goods['form_name'];
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'info' => $goods,
- 'AttrGroupList' => isset($getAttrGroupList) ? $getAttrGroupList : [],
- 'postage' => $postage,
- 'level' => $level,
- 'verify_list' => $verify_list,
- 'goods_verify_card_list' => $goods_verify_card_list
- ]
- ];
- }
- }
|