ProductBatchProcessForm.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\plugins\product_traceability\models\form;
  8. use app\constants\OptionSetting;
  9. use app\models\Option;
  10. use app\plugins\product_traceability\models\ProductBatch;
  11. use app\plugins\product_traceability\models\ProductBatchProcess;
  12. use app\plugins\product_traceability\models\ProductBatchProcessLog;
  13. use app\plugins\product_traceability\models\ProductBatchProcessLogSheet;
  14. use app\plugins\product_traceability\models\ProductBatchProcessSheet;
  15. use yii\base\Model;
  16. use yii\helpers\Json;
  17. use app\jobs\SyncMdGoodsJob;
  18. use app\utils\Tools;
  19. class ProductBatchProcessForm extends Model
  20. {
  21. public $id;
  22. public $store_id;
  23. public $process_user_id;
  24. public $product_batch_id;
  25. public $process_name;
  26. public $sort;
  27. public $is_change_sheet;
  28. public $form_list;
  29. /**
  30. * 初始化
  31. */
  32. public function init()
  33. {
  34. $this->store_id = get_store_id();
  35. }
  36. /**
  37. * @return array
  38. */
  39. public function rules()
  40. {
  41. return [
  42. [['store_id', 'process_user_id', 'product_batch_id','form_list', 'process_name', 'sort'], 'required'],
  43. [['store_id', 'id', 'process_user_id', 'product_batch_id', 'is_change_sheet' ,'sort', 'id'], 'integer'],
  44. [['process_name',], 'string', 'max' => 255],
  45. [['form_list',], 'safe',],
  46. [['product_batch_id', 'is_change_sheet'], 'default', 'value' => 0],
  47. ];
  48. }
  49. public function attributeLabels()
  50. {
  51. return [
  52. 'id' => 'ID',
  53. 'store_id' => '商城id',
  54. 'process_name' => '流程名称',
  55. 'process_user_id' => '流程操作用户',
  56. 'product_batch_id' => '批次id',
  57. 'created_at' => '创建时间',
  58. 'updated_at' => '更新时间',
  59. 'is_delete' => 'Is Delete',
  60. 'sort' => 'sort',
  61. ];
  62. }
  63. public function save()
  64. {
  65. if(!$this->validate()) {
  66. return [
  67. 'code' => 1,
  68. 'msg' => $this->getErrorSummary(false)[0],
  69. ];
  70. }
  71. if (!$this->form_list) {
  72. return [
  73. 'code'=>1,
  74. 'msg'=>'请填写流程表单设置'
  75. ];
  76. }
  77. $t = \Yii::$app->db->beginTransaction();
  78. $product_batch = ProductBatch::findOne($this->product_batch_id);
  79. if ($this->id) {
  80. $product_batch_process = ProductBatchProcess::findOne($this->id);
  81. } else {
  82. $product_batch_process = new ProductBatchProcess();
  83. }
  84. $is_exits_sort =ProductBatchProcess::find()->where(['product_batch_id' => $this->product_batch_id, 'sort' => $this->sort, 'is_delete' => 0])->one();
  85. if ($is_exits_sort && $is_exits_sort->id != $this->id){
  86. return [
  87. 'code'=>1,
  88. 'msg'=>'该排序序号已存在'
  89. ];
  90. }
  91. $product_batch_process->store_id = $this->store_id;
  92. $product_batch_process->process_name = $this->process_name;
  93. $product_batch_process->sort = $this->sort;
  94. $product_batch_process->process_user_id = $this->process_user_id ? $this->process_user_id : 0;
  95. $product_batch_process->product_batch_id = $this->product_batch_id ? $this->product_batch_id : 0;
  96. if ($product_batch_process->save()) {
  97. // 自定义表单
  98. if ($this->is_change_sheet) {
  99. ProductBatchProcessSheet::updateAll(['is_delete' => 1], ['store_id' => $this->store_id, 'product_batch_process_id' => $product_batch_process->id]);
  100. }
  101. if ($this->form_list && ($this->id == 0 || ($this->id > 0 && $this->is_change_sheet == 1))) {
  102. $this->form_list = array_values($this->form_list);
  103. foreach ($this->form_list as $index => $value) {
  104. if (!$value['name']) {
  105. $t->rollBack();
  106. return [
  107. 'code' => 1,
  108. 'msg' => '请输入字段名称'
  109. ];
  110. }
  111. if (in_array($value['type'], ['radio', 'checkbox'])) {
  112. if (!$value['default']) {
  113. $t->rollBack();
  114. return [
  115. 'code' => 1,
  116. 'msg' => '请输入单选或多选的默认值'
  117. ];
  118. }
  119. }
  120. if ($value['id']) {
  121. $product_batch_process_sheet = ProductBatchProcessSheet::findOne(['store_id' => $this->store_id, 'id' => $value['id']]);
  122. } else {
  123. $product_batch_process_sheet = new ProductBatchProcessSheet();
  124. $product_batch_process_sheet->product_batch_process_id = $product_batch_process['id'];
  125. }
  126. $product_batch_process_sheet->is_delete = 0;
  127. $product_batch_process_sheet->type = $value['type'];
  128. $product_batch_process_sheet->name = $value['name'];
  129. $product_batch_process_sheet->default = $value['default'];
  130. $product_batch_process_sheet->required = $value['required'] ? $value['required'] : 0;
  131. $product_batch_process_sheet->tip = $value['tip'];
  132. $product_batch_process_sheet->sort = $index;
  133. $product_batch_process_sheet->store_id = $this->store_id;
  134. if (!$product_batch_process_sheet->save()) {
  135. $t->rollBack();
  136. return [
  137. 'code' => 1,
  138. 'msg' => $product_batch_process_sheet->errors[0]
  139. ];
  140. }
  141. }
  142. }
  143. $product_batch_process_log = ProductBatchProcessLog::find()->where([
  144. 'store_id' => $this->store_id,
  145. 'product_id' => $product_batch->product_id,
  146. 'product_batch_id' => $product_batch->id,
  147. 'product_batch_process_id' => $product_batch_process->id,
  148. ])->one();
  149. if (!$product_batch_process_log) {
  150. $product_batch_process_log = new ProductBatchProcessLog();
  151. $product_batch_process_log->store_id = $this->store_id;
  152. $product_batch_process_log->product_id = $product_batch->product_id;
  153. $product_batch_process_log->product_batch_id = $product_batch->id;
  154. $product_batch_process_log->product_batch_process_id = $product_batch_process->id;
  155. $product_batch_process_log->state = 0;
  156. }
  157. if ($this->is_change_sheet && $product_batch_process_log) {
  158. $product_batch_process_log->state = 0;
  159. ProductBatchProcessLogSheet::updateAll(['is_delete' => 1], ['process_log_id' => $product_batch_process_log->id]);
  160. }
  161. $product_batch_process_list = ProductBatchProcess::find()->where(['product_batch_id' => $product_batch->id, 'is_delete' => 0])->all();
  162. foreach ($product_batch_process_list as $k => $v){
  163. $up_process = ProductBatchProcess::find()->alias('pbp')
  164. ->where(['pbp.product_batch_id' => $product_batch->id, 'is_delete' => 0])
  165. ->andWhere(['<','pbp.sort', $v->sort])->orderBy(['pbp.sort' => SORT_DESC])->one();
  166. $v->up_process_id = $up_process->id ? $up_process->id : 0;
  167. $down_process = ProductBatchProcess::find()->alias('pbp')
  168. ->where(['pbp.product_batch_id' => $product_batch->id, 'is_delete' => 0])
  169. ->andWhere(['>','pbp.sort', $v->sort])->orderBy(['pbp.sort' => SORT_ASC])->one();
  170. $v->down_process_id = $down_process->id ? $down_process->id : 0;
  171. if (!$v->save()) {
  172. $t->rollBack();
  173. return [
  174. 'code' => 1,
  175. 'msg' => $v->errors[0]
  176. ];
  177. }
  178. }
  179. if (!$product_batch_process_log->save()) {
  180. $t->rollBack();
  181. return [
  182. 'code' => 1,
  183. 'msg' => $product_batch_process_log->errors[0]
  184. ];
  185. }
  186. $product_batch->state = 1;
  187. if (!$product_batch->save()) {
  188. $t->rollBack();
  189. return [
  190. 'code' => 1,
  191. 'msg' => $product_batch->errors[0]
  192. ];
  193. }
  194. $t->commit();
  195. return [
  196. 'code' => 0,
  197. 'msg' => '保存成功'
  198. ];
  199. } else {
  200. $t->rollBack();
  201. return [
  202. 'code' => 1,
  203. 'msg' => '添加失败'
  204. ];
  205. }
  206. }
  207. public function getEdit($id)
  208. {
  209. if ($id) {
  210. $goods = Goods::findOne($id);
  211. if (empty($goods)) {
  212. return [
  213. 'code' => 1,
  214. 'msg' => '商品获取失败'
  215. ];
  216. }
  217. $getAttrGroupList = $goods->getAttrGroupList();
  218. $goods = $goods->toArray();
  219. $cat_id = GoodsCat::find()
  220. ->where(['goods_id' => $goods['id'], 'is_delete' => 0])
  221. ->select(['cat_id'])->asArray()->all();
  222. $cat_id = array_column($cat_id, 'cat_id');
  223. $goods['cat_id'] = $cat_id;
  224. $goods['integral'] = json_decode($goods['integral'],true);
  225. $goods['goods_pic_list'] = GoodsPic::find()->select(['pic_url'])
  226. ->where(['goods_id' => $id])->asArray()->all();
  227. foreach ($goods as &$val) {
  228. $val = is_int($val) ? (string)$val : $val;
  229. }
  230. $goods['content'] = $goods['detail'];
  231. } else {
  232. $goods = null;
  233. }
  234. $send_type = Option::get(OptionSetting::STORE_SEND_TYPE, get_store_id(), 'store')['value'];
  235. $send_type = Option::get(OptionSetting::STORE_SEND_TYPE, get_store_id(), 'pay', $send_type);
  236. $send_type = $send_type ? Json::decode($send_type['value']) : [];
  237. $send_type_arr = [];
  238. foreach ((array)$send_type as $key => $send) {
  239. if ($send['value'] == 1) {
  240. $send_type_arr[$key] = $send['text'];
  241. }
  242. }
  243. $arr = empty($send_type_arr) ? ['express' => '快递', 'shop' => '自提'] : $send_type_arr;
  244. $goods['goods_delivery_type'] = !empty($goods['delivery_type']) ? Json::decode($goods['delivery_type']) : array_keys($arr);
  245. $goods['delivery_type'] = $arr;
  246. // 获取运费规则
  247. $postage = PostageRules::find()->where([
  248. 'store_id' => get_store_id(),
  249. 'is_delete' => 0,
  250. 'mch_id' => 0
  251. ])->select(['id', 'name', 'is_enable'])->asArray()->all();
  252. // 获取所有的会员等级
  253. $level = Level::find()->where([
  254. 'store_id' => get_store_id(),
  255. 'is_delete' => 0,
  256. 'status' => 1
  257. ])->select(['level as id', 'name'])->orderBy(['id' => SORT_DESC])->asArray()->all();
  258. // 卡券
  259. $verify_list = VerifyCard::find()
  260. ->where([
  261. 'store_id' => get_store_id(),
  262. 'is_delete' => 0
  263. ])->orderBy(['sort' => SORT_ASC])
  264. ->select([
  265. '*'
  266. ])->asArray()
  267. ->all();
  268. $verify_list = Tools::getVerifyList($verify_list);
  269. $goods_verify_card_list = [];
  270. // 获取商品已添加核销卡
  271. if (!empty($goods) && !empty($goods['verify_card_id'])) {
  272. $verify_cards = explode(',', $goods['verify_card_id']);
  273. foreach ($verify_cards as $k => $v) {
  274. $res = VerifyCard::find()
  275. ->select(['id','name'])
  276. ->where(['id' => $v,'is_delete' => 0])
  277. ->asArray()->one();
  278. if ($res) {
  279. $goods_verify_card_list[] = $res;
  280. }
  281. }
  282. }
  283. // 服务商品预约
  284. $goods['service_book'] = [
  285. 'date' => 0,
  286. 'data' => [],
  287. 'flag' => 1
  288. ];
  289. $goods['date_book'] = [
  290. 'data' => [],
  291. 'date' => 30
  292. ];
  293. $goods['form'] = [
  294. 'form_list' => [],
  295. 'is_form' => 0,
  296. 'form_name' => ''
  297. ];
  298. if (isset($goods['id'])) {
  299. $goods_book_config = GoodsBook::findOne(['goods_id' => $goods['id']]);
  300. if ($goods_book_config) {
  301. $service_book = $goods_book_config->service_book;
  302. $date_book = $goods_book_config->date_book;
  303. if ($service_book) {
  304. $goods['service_book'] = Json::decode($service_book);
  305. }
  306. if ($date_book) {
  307. $goods['date_book']['data'] = Json::decode($date_book);
  308. $goods['date_book']['date'] = count($goods['date_book']['data']);
  309. }
  310. }
  311. $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();
  312. $goods['form']['is_form'] = intval($goods['is_form']);
  313. $goods['form']['form_name'] = $goods['form_name'];
  314. }
  315. return [
  316. 'code' => 0,
  317. 'msg' => 'success',
  318. 'data' => [
  319. 'info' => $goods,
  320. 'AttrGroupList' => isset($getAttrGroupList) ? $getAttrGroupList : [],
  321. 'postage' => $postage,
  322. 'level' => $level,
  323. 'verify_list' => $verify_list,
  324. 'goods_verify_card_list' => $goods_verify_card_list
  325. ]
  326. ];
  327. }
  328. }