StoreAdminGoodsForm.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\client\models\v1\store;
  8. use app\constants\OptionSetting;
  9. use app\models\Admin;
  10. use app\models\AttrLibrary;
  11. use app\models\Cat;
  12. use app\models\CloudGoodsBind;
  13. use app\models\ErpInventory;
  14. use app\models\ErpPurchasein;
  15. use app\models\ErpPurchaseinOrder;
  16. use app\models\Goods;
  17. use app\models\GoodsBook;
  18. use app\models\GoodsCat;
  19. use app\models\GoodsFullMinus;
  20. use app\models\GoodsPic;
  21. use app\models\Option;
  22. use app\models\Order;
  23. use app\models\OrderDetail;
  24. use app\models\PostageRules;
  25. use app\utils\OrderNo;
  26. use yii\base\Model;
  27. use yii\helpers\Json;
  28. use app\models\MchCat;
  29. use app\modules\admin\models\MchForm;
  30. class StoreAdminGoodsForm extends Model
  31. {
  32. public $status = 0;//商品列表状态 /分类状态
  33. public $sort = 0;//时间排序
  34. public $model;
  35. public $id;//商品ID
  36. public $goods_name;//商品名称
  37. public $goods_num;//商品数量
  38. public $goods_price;//商品价格
  39. public $attr;//商品规格
  40. public $goods_info;//商品信息
  41. public $cat_name; //分类名称
  42. public $pic_url; //分类缩略图
  43. public $parent_id; //分类父级
  44. public $store_id;
  45. public $goods_no;
  46. public $purchase;
  47. public $mch_id;
  48. public function rules()
  49. {
  50. return [
  51. [['status', 'sort', 'goods_num', 'goods_price', 'parent_id', 'store_id'], 'integer'],
  52. [['goods_name', 'id', 'pic_url', 'cat_name', 'goods_no'], 'string'],
  53. [['mch_id'], 'safe'],
  54. [['attr', 'goods_info', 'purchase'], 'safe']
  55. ];
  56. }
  57. public function __construct($config = [])
  58. {
  59. parent::__construct($config);
  60. $this->model = Goods::find()->where(['store_id'=>get_store_id(), 'is_delete' => 0]);
  61. $whereMch = [];
  62. if($this->mch_id){
  63. $whereMch = ['mch_id' => $this->mch_id];
  64. $this->model->andWhere($whereMch);
  65. }
  66. }
  67. //商品列表
  68. public function goodsList(){
  69. try {
  70. $model = $this->model;
  71. $up_status = (clone $model)->andWhere(['status'=>1])->count();
  72. $down_status = (clone $model)->andWhere(['status'=>0])->count();
  73. //商品状态
  74. $model->andWhere(['status'=>$this->status]);
  75. //排序方式
  76. //创建时间
  77. if ($this->sort != 0) {
  78. $model->orderBy('created_at asc');
  79. } else {
  80. $model->orderBy('created_at desc');
  81. }
  82. $model->select('product_type, mch_id, id, name, price, goods_num, virtual_sales, cover_pic, created_at, status, is_delete, use_attr, attr, cost_price');
  83. //分页
  84. $pagination = pagination_make($model);
  85. $list = $pagination['list'];
  86. foreach ($list as &$item) {
  87. if(!empty($item['attr'])){
  88. $item['attr'] = json_decode($item['attr'], true);
  89. }
  90. $item['cost_price'] = $item['cost_price'] ?: $item['price'];
  91. }
  92. $is_add_goods = Option::get('is_add_goods', get_store_id(), 'store', 1)['value'];
  93. return [
  94. 'code' => 0,
  95. 'msg' => '获取成功',
  96. 'data' => [
  97. 'list' => $list,
  98. 'up_status' => $up_status,
  99. 'down_status' => $down_status,
  100. 'totalCount' => $pagination['totalCount'],
  101. 'pageNo' => $pagination['pageNo'],
  102. 'is_add_goods' => (int)$is_add_goods
  103. ]
  104. ];
  105. } catch (\Exception $e) {
  106. return [
  107. 'code' => 1,
  108. 'msg' => $e->getMessage()
  109. ];
  110. }
  111. }
  112. //商品规格库存
  113. public function getGoodsAttr(){
  114. try {
  115. $id = $this->id;
  116. $model = $this->model;
  117. $goods = $model->andWhere(['id' => $id, 'use_attr' => 1])->select('id, attr')->one();
  118. if (empty($goods->attr) || empty($goods)) {
  119. throw new \Exception("获取商品规格库存失败");
  120. }
  121. $attr = !empty($goods->attr) ? json_decode($goods->attr, true) : [];
  122. $getAttrGroupList = $goods->getAttrGroupList();
  123. $getAttrGroupList = json_decode(json_encode($getAttrGroupList), true);
  124. $title = null;
  125. if (!empty($getAttrGroupList)) {
  126. $title = $getAttrGroupList[0]['attr_list'];
  127. }
  128. $arr = [];
  129. if (!empty($title) && !empty($attr)) {
  130. foreach ($title as $index =>$item) {
  131. foreach ($attr as $at) {
  132. if (!empty($at)) {
  133. foreach ($at['attr_list'] as $al) {
  134. if ($item['attr_id'] === $al['attr_id']) {
  135. $arr[$index]['list'][] = $at;
  136. }
  137. }
  138. }
  139. }
  140. }
  141. }
  142. return [
  143. 'code' => 0,
  144. 'msg' => "获取成功",
  145. 'data' => [
  146. 'attr' => $attr,
  147. 'attr_group' => $arr,
  148. 'title' => $title
  149. ]
  150. ];
  151. } catch (\Exception $e) {
  152. return [
  153. 'code' => 1,
  154. 'msg' => $e->getMessage()
  155. ];
  156. }
  157. }
  158. //修改标题
  159. public function setGoodsName(){
  160. try {
  161. $model = $this->model;
  162. $goods = $model->andWhere(['id' => $this->id])->select('id, name')->one();
  163. if (empty($goods) || empty($goods->name)) {
  164. throw new \Exception("信息不存在");
  165. }
  166. $goods->name = $this->goods_name;
  167. if (!$goods->save()) {
  168. throw new \Exception("保存失败");
  169. }
  170. return [
  171. 'code' => 0,
  172. 'msg' => "修改成功"
  173. ];
  174. } catch (\Exception $e) {
  175. return [
  176. 'code' => 1,
  177. 'msg' => $e->getMessage()
  178. ];
  179. }
  180. }
  181. //删除
  182. public function goodsDel(){
  183. try {
  184. $id = $this->id;
  185. $id = explode(',',$id);
  186. $res = Goods::updateAll(['is_delete' => 1], ['id' => $id]);
  187. $cloud_store_token = get_merchant_token(0, get_store_id(), $message);
  188. if (!$cloud_store_token && $message) {
  189. debug_log('删除转单商品' . $message['msg']);
  190. }
  191. $mch_set_submit_order_url = "/cloud/mch/delTransGoods";
  192. $mch_set_submit_order_data = [];
  193. $mch_set_submit_order_data['access_token'] = $cloud_store_token;
  194. foreach ($id as $item) {
  195. $goods = Goods::findOne($item);
  196. if (!$goods->cloud_goods_id || intval($goods->is_wholesale)) {
  197. continue;
  198. }
  199. $mch_set_submit_order_data['goods_id'] = $goods->cloud_goods_id;
  200. $domain = (new OptionSetting)->getCloudDomainName();
  201. $mchSetSubmitOrderInfo = cloud_post( $domain . $mch_set_submit_order_url, $mch_set_submit_order_data);
  202. $mchSetSubmitOrderInfo = json_decode($mchSetSubmitOrderInfo,true);
  203. if($mchSetSubmitOrderInfo['code'] != 0){
  204. debug_log('删除转单商品' . $mchSetSubmitOrderInfo['msg']);
  205. } else {
  206. CloudGoodsBind::updateAll(['is_delete' => 1], ['store_id' => get_store_id(), 'cloud_goods_id' => $goods->cloud_goods_id, 'is_delete' => 0]);
  207. }
  208. }
  209. if ($res) {
  210. return [
  211. 'code' => 0,
  212. 'msg' => "删除成功"
  213. ];
  214. }
  215. throw new \Exception("删除失败");
  216. } catch (\Exception $e) {
  217. return [
  218. 'code' => 1,
  219. 'msg' => $e->getMessage()
  220. ];
  221. }
  222. }
  223. //上下架
  224. public function setGoodsStatus(){
  225. try {
  226. $id = $this->id;
  227. $id = explode(',',$id);
  228. if (empty($id) ) {
  229. throw new \Exception("没有传入有效数据");
  230. }
  231. $model = $this->model;
  232. if(!empty($this->status)){
  233. //判断库存
  234. $goods = $model->andWhere(['id' => $id])->select('mch_id, id, status, goods_num, name')->all();
  235. if (empty($goods)) {
  236. throw new \Exception("获取产品信息失败");
  237. }
  238. foreach ($goods as $item) {
  239. if (empty($item->goods_num) && $item->goods_num <= 0) {
  240. throw new \Exception('商品' . $item->name . "库存不足");
  241. }
  242. if($item->mch_id){
  243. $mchSetting = (new MchForm(['store_id' => $item->store_id]))->mchSetting();
  244. if($mchSetting['mch_goods_audit']){
  245. $item->mch_audit = 1;
  246. $item->mch_audit_time = time();
  247. $item->status = Goods::STATUS_DISABLE;
  248. $item->save();
  249. return [
  250. 'code' => 0,
  251. 'msg' => '已提交审核,请等待管理员审核'
  252. ];
  253. }
  254. }
  255. }
  256. }
  257. $status = empty($this->status) ? 0 : 1;
  258. //修改上下架状态
  259. $res = Goods::updateAll(['status' => $status], ['id' => $id]);
  260. if ($res) {
  261. return [
  262. 'code' => 0,
  263. 'msg' => "操作成功"
  264. ];
  265. }
  266. throw new \Exception('信息修改失败');
  267. } catch (\Exception $e){
  268. return [
  269. 'code' => 0,
  270. 'msg' => $e->getMessage()
  271. ];
  272. }
  273. }
  274. //修改价格/数量 (规格库存价格)//未测试带规格的
  275. public function setPrice(){
  276. try {
  277. $model = $this->model;
  278. $goods = $model->andWhere(['id' => $this->id])->select('id, status, goods_num ,use_attr, attr, price')->one();
  279. if (empty($goods)) {
  280. throw new \Exception("获取产品信息失败");
  281. }
  282. //判断是否存在以及开启规格
  283. if ($goods->use_attr == 1 && !empty($this->attr)) {
  284. $attr = $this->attr;
  285. $arr = [];
  286. foreach ($attr as $item) {
  287. if ($item['num'] >=0 && $item['price'] >=0) {
  288. $arr[] = $item;
  289. } else {
  290. throw new \Exception("数据错误");
  291. }
  292. }
  293. $goods->attr = json_encode($arr, JSON_UNESCAPED_UNICODE);
  294. } elseif ($goods->use_attr == 0) {
  295. $this->goods_num > 0 && $goods->goods_num = $this->goods_num;
  296. $this->goods_price > 0 && $goods->price = $this->goods_price;
  297. //如果库存不足,则下架处理
  298. if ($this->goods_num == 0) {
  299. $goods->status = 0;
  300. }
  301. // 更新attr字段
  302. try {
  303. $attr = \json_decode($goods->attr, true);
  304. $attr[0]['num'] = $this->goods_num > 0 ? $this->goods_num : 0;
  305. $goods->attr = \json_encode($attr, JSON_UNESCAPED_UNICODE);
  306. } catch(\Exception $e) {}
  307. }
  308. if (!$goods->save()) {
  309. throw new \Exception('数据保存错误' . json_encode($goods->errors));
  310. }
  311. return [
  312. 'code' => 0,
  313. 'msg' => '修改成功'
  314. ];
  315. } catch (\Exception $e) {
  316. return [
  317. 'code' => 1,
  318. 'msg' => $e->getMessage()
  319. ];
  320. }
  321. }
  322. //修改添加商品
  323. public function getGoodsEdit(){
  324. try {
  325. $id = $this->id;
  326. $model = $this->model;
  327. if ($id) {
  328. $goods = $model->where(['id'=>$id])->select('id, cat_id, product_type, name, attr, use_attr, freight, price, goods_num, detail, delivery_type, pieces, forehead, cover_pic, cost_price, is_front_delivery, is_front_centralize, agent_goods_status')->one();
  329. if (empty($goods)) {
  330. return [
  331. 'code' => 1,
  332. 'msg' => '商品获取失败'
  333. ];
  334. }
  335. $getAttrGroupList = $goods->getAttrGroupList();
  336. $goods = $goods->toArray();
  337. $cat_id = GoodsCat::find()
  338. ->where(['goods_id' => $goods['id'], 'is_delete' => 0])
  339. ->select(['cat_id'])->asArray()->all();
  340. $cat_id = array_column($cat_id, 'cat_id');
  341. $goods['cat_id'] = $cat_id;
  342. $goods['integral'] = json_decode($goods['integral'],true);
  343. $goods['goods_pic_list'] = GoodsPic::find()->select(['pic_url'])
  344. ->where(['goods_id' => $id, 'is_delete' => 0])->asArray()->all();
  345. foreach ($goods as &$val) {
  346. $val = is_int($val) ? (string)$val : $val;
  347. }
  348. $goods['attr'] = !empty($goods['attr']) ? json_decode($goods['attr'], true) : [];
  349. $goods['is_front_delivery'] = intval($goods['is_front_delivery']);
  350. $goods['is_front_centralize'] = intval($goods['is_front_centralize']);
  351. $goods['agent_goods_status'] = intval($goods['agent_goods_status']);
  352. $goods['use_attr'] = intval($goods['use_attr']);
  353. //unset($goods['attr']);
  354. } else {
  355. $goods = null;
  356. }
  357. //商品规格
  358. $AttrLibrary = AttrLibrary::find()->where(['is_delete' => 0, 'store_id' => get_store_id()])->select('id, name, parent_id')->asArray()->all();
  359. $new_AttrLibrary = $this->getmenu($AttrLibrary);
  360. $send_type = Option::get(OptionSetting::STORE_SEND_TYPE, get_store_id(), 'store')['value'];
  361. $send_type = Option::get(OptionSetting::STORE_SEND_TYPE, get_store_id(), 'pay', $send_type);
  362. $send_type = $send_type ? Json::decode($send_type['value']) : [];
  363. $send_type_arr = [];
  364. foreach ((array)$send_type as $key => $send) {
  365. if ($send['value'] == 1) {
  366. $send_type_arr[$key] = $send['text'];
  367. }
  368. }
  369. //物流方式
  370. $arr = empty($send_type_arr) ? ['express' => '快递', 'shop' => '自提'] : $send_type_arr;
  371. $goods_delivery_type = array_keys($arr);
  372. foreach ($goods_delivery_type as &$item) {
  373. switch ($item) {
  374. case "express":
  375. $value = '快递';
  376. break;
  377. case "shop":
  378. $value = '到店自提';
  379. break;
  380. case "delivery":
  381. $value = '同城配送';
  382. break;
  383. default:
  384. $value = '';
  385. break;
  386. }
  387. $item = [
  388. 'key' => $item,
  389. 'value' => $value
  390. ];
  391. }
  392. $goods['delivery_type'] = !empty($goods['delivery_type']) ? json_decode($goods['delivery_type'], true) : [];
  393. // unset($goods['goods_delivery_type']);
  394. // 获取运费规则
  395. $postage = PostageRules::find()->where([
  396. 'store_id' => get_store_id(),
  397. 'is_delete' => 0,
  398. 'mch_id' => 0,
  399. 'is_enable' => 1
  400. ])->select(['id', 'name', 'is_enable'])->asArray()->all();
  401. //分类列表
  402. $cat = Cat::find()->where(['is_delete' => 0, 'is_show' => 1, 'store_id' => get_store_id()])->select('id, name, parent_id')->asArray()->all();
  403. $cat_arr = $this->getmenu($cat);
  404. return [
  405. 'code' => 0,
  406. 'msg' => 'success',
  407. 'data' => [
  408. 'info' => $goods,
  409. 'postage' => $postage,
  410. 'cat' => $cat_arr,
  411. 'goods_delivery_type' => $goods_delivery_type,
  412. 'new_AttrLibrary' => $new_AttrLibrary,
  413. 'AttrGroupList' => $getAttrGroupList ?? [],
  414. ]
  415. ];
  416. } catch (\Exception $e) {
  417. return [
  418. 'code' => 1,
  419. 'msg' => $e->getMessage()
  420. ];
  421. }
  422. }
  423. //获取运费规则
  424. public function getPostageData()
  425. {
  426. try {
  427. // 获取运费规则
  428. $postage = PostageRules::find()->where([
  429. 'store_id' => get_store_id(),
  430. 'is_delete' => 0,
  431. 'mch_id' => 0,
  432. 'is_enable' => 1
  433. ])->select(['id', 'name', 'is_enable'])->asArray()->all();
  434. return [
  435. 'code' => 0,
  436. 'msg' => "获取成功",
  437. 'data' => [
  438. 'postage' => $postage
  439. ]
  440. ];
  441. } catch (\Exception $e) {
  442. return [
  443. 'code' => 1,
  444. 'msg' => $e->getMessage()
  445. ];
  446. }
  447. }
  448. //商品信息保存
  449. public function saveGoods(){
  450. try {
  451. $model = $this->model;
  452. $goods_info = $this->goods_info;
  453. if (empty($goods_info)) {
  454. throw new \Exception("数据错误");
  455. }
  456. $id = $goods_info['id'];
  457. if (!empty($id)) {
  458. $model = $model->where(['id' => $id])->select('id, cat_id, product_type, attr, use_attr, price, goods_num, freight, detail, delivery_type, pieces, forehead, cover_pic, original_price, goods_no, cost_price')->one();
  459. if (empty($model)) {
  460. throw new \Exception("获取产品信息失败");
  461. }
  462. } else {
  463. $model = new Goods();
  464. $model->store_id = get_store_id();
  465. }
  466. // if (($goods_info['product_type'] != 2 || $goods_info['product_type'] != 3) && $goods_info['use_attr'] == 1) {
  467. //
  468. // foreach ($goods_info['attr'] as $item) {
  469. // if ((empty($item['num']) || $item['num'] <= 0) || (empty($item['price']) || $item['price'] <= 0)) {
  470. // throw new \Exception("规格信息错误");
  471. // }
  472. // }
  473. //
  474. // } elseif (empty($id) || $goods_info['product_type'] == 2 || $goods_info['product_type'] == 3) {
  475. //
  476. // $goods_info['attr'] = [
  477. // [
  478. // "attr_list" => [
  479. // [
  480. // "attr_id"=>16,
  481. // "attr_name"=> "默认"
  482. // ]
  483. // ],
  484. // "num" => 0 ,
  485. // "price" => 0,
  486. // "no" => "",
  487. // "pic" => "",
  488. // "share_commission_first" => "",
  489. // "share_commission_second" => "",
  490. // "share_commission_third" => ""
  491. // ]
  492. // ];
  493. //
  494. // }
  495. $model->use_attr = $goods_info['use_attr'];
  496. $model->product_type = $goods_info['product_type'];
  497. // $model->cat_id = $goods_info['cat_id'];
  498. // $model->attr = json_encode($goods_info['attr']);
  499. $model->price = $goods_info['price'];
  500. $model->goods_num = $goods_info['goods_num'];
  501. $model->name = $goods_info['name'];
  502. // if ($goods_info['use_attr'] === 0) {
  503. //
  504. // }
  505. $model->original_price = $goods_info['original_price'];
  506. $model->goods_no = $goods_info['goods_no'];
  507. $model->detail = $goods_info['detail'];
  508. $model->delivery_type = json_encode($goods_info['delivery_type']);
  509. $model->pieces = $goods_info['pieces'];
  510. $model->forehead = $goods_info['forehead'];
  511. $model->cover_pic = $goods_info['cover_pic'];
  512. $model->freight = $goods_info['freight'];
  513. $model->cost_price = $goods_info['cost_price'] ?: 0;
  514. $goods_pic_list = [];
  515. if ($goods_info['goods_pic_list']) {
  516. $goods_pic_list = $goods_info['goods_pic_list'];
  517. }
  518. // if ($goods_info['cover_pic']) {
  519. // $goods_pic_list = array_merge($goods_pic_list, [['pic_url' => $goods_info['cover_pic']]]);
  520. // }
  521. $goods_pic_list = array_values($goods_pic_list);
  522. if($this->mch_id){
  523. $mchSetting = (new MchForm(['store_id' => $model->store_id]))->mchSetting();
  524. if($mchSetting['mch_goods_audit'] && !$id){
  525. $model->mch_audit = 1;
  526. $model->mch_audit_time = time();
  527. $model->status = Goods::STATUS_DISABLE;
  528. }
  529. $model->mch_id = $this->mch_id;
  530. }
  531. if ($model->save()) {
  532. if($this->mch_id && $goods_info['mch_cat_id']){
  533. $saveCat = \app\models\MchGoodsCat::saveCat($model->id, $goods_info['mch_cat_id']);
  534. if ($saveCat['code']) {
  535. $t->rollBack();
  536. return $saveCat;
  537. }
  538. }
  539. //判断如果规格发生改变就删除满减规则
  540. if (!empty($goods_info['is_change_attr'])) {
  541. $res = GoodsFullMinus::updateAll(['is_delete' => 1], ['goods_id' => $model->id]);
  542. \Yii::error($res, "规格发生改变就删除满减规则");
  543. }
  544. $goods_info['id'] = $model->id;
  545. $form = new StoreAdminAttrForm();
  546. $res = $form->goodsAttrBook($goods_info);
  547. if ($res['code'] != 0) {
  548. throw new \Exception($res['msg']);
  549. }
  550. //图片
  551. if (!empty($goods_pic_list)) {
  552. if ($id) {
  553. GoodsPic::updateAll(['is_delete' => 1], ['goods_id' => $id, 'is_delete' => 0]);
  554. }
  555. foreach ($goods_pic_list as $item) {
  556. $goods_pic = new GoodsPic();
  557. $goods_pic->goods_id = $model->id;
  558. $goods_pic->pic_url = $item['pic_url'];
  559. if (!$goods_pic->save()) {
  560. throw new \Exception(json_encode($goods_pic->errors));
  561. };
  562. }
  563. }
  564. //分类
  565. if (!empty($goods_info['cat_id'])) {
  566. $cat_id = is_string($goods_info['cat_id']) ? json_decode($goods_info['cat_id']) : $goods_info['cat_id'];
  567. if ($id) {
  568. GoodsCat::updateAll(['is_delete' => 1], ['goods_id' => $id, 'is_delete' => 0]);
  569. }
  570. foreach ((array)$cat_id as $item) {
  571. $goods_cat = new GoodsCat();
  572. $goods_cat->goods_id = $model->id;
  573. $goods_cat->store_id = get_store_id();
  574. $goods_cat->cat_id = $item;
  575. if (!$goods_cat->save()) {
  576. throw new \Exception(json_encode($goods_cat->errors));
  577. };
  578. }
  579. }
  580. return [
  581. 'code' => 0,
  582. 'msg' => "保存成功"
  583. ];
  584. }
  585. throw new \Exception(json_encode($model->errors));
  586. } catch (\Exception $e) {
  587. debug_log($e->getMessage().$e->getLine());
  588. return [
  589. 'code' => 1,
  590. 'msg' => $e->getMessage().$e->getLine()
  591. ];
  592. }
  593. }
  594. //展示商品详情
  595. public function goodsDetail(){
  596. try {
  597. $id = $this->id;
  598. $model = $this->model;
  599. if (empty($id)) {
  600. throw new \Exception("获取参数失败");
  601. }
  602. $goods = $model->where(['id' => $id])->select('id, detail, name, price, goods_num, virtual_sales, cat_id, attr, use_attr')->with('goodsPicList')->asArray()->one();
  603. if (empty($goods)) {
  604. throw new \Exception("获取产品信息失败");
  605. }
  606. //分类列表
  607. $cat = Cat::find()->where(['is_delete' => 0, 'is_show' => 1, 'store_id' => get_store_id()])->select('id, name, parent_id')->asArray()->all();
  608. $cat_arr = $this->getmenu($cat);
  609. //实际销量
  610. $actual_num = Order::find()->where(['and',
  611. ['o.is_delete' => 0, 'o.store_id' => get_store_id(), 'od.goods_id' => $id]])
  612. ->andWhere(['or', ['<>', 'o.trade_status', '-1'],['<>', 'o.trade_status', '1']])
  613. ->alias('o')->leftJoin(['od' => OrderDetail::tableName()], 'od.order_id = o.id')
  614. ->count();
  615. $goods['virtual_sales'] += $actual_num;
  616. //规格
  617. $goods['attr'] = !empty($goods['attr']) ? json_decode($goods['attr'], true) : '';
  618. return [
  619. 'code' => 0,
  620. 'msg' => "获取成功",
  621. 'data' => [
  622. 'goods_info' => $goods,
  623. 'cat' => $cat_arr
  624. ]
  625. ];
  626. } catch (\Exception $e) {
  627. return [
  628. 'code' => 1,
  629. 'msg' => $e->getMessage()
  630. ];
  631. }
  632. }
  633. //分类列表
  634. public function catList(){
  635. $data = Cat::find()->where(['store_id' => get_store_id(), 'is_delete' => 0])->select("id, parent_id, name, pic_url, is_show")->asArray()->all();
  636. if($this->mch_id){
  637. $data = \app\models\MchCat::find()->where(['mch_id' => $this->mch_id, 'is_delete' => 0])->select("id, parent_id, name, pic_url, is_show")->asArray()->all();
  638. }
  639. $list = $this->getmenu($data, 0, 1);
  640. return [
  641. 'code' => 0,
  642. 'msg' => "获取成功",
  643. 'data' => $list
  644. ];
  645. }
  646. //分类保存
  647. public function catAdd(){
  648. try {
  649. $id = $this->id;
  650. $cat_name = $this->cat_name;
  651. $pic_url = $this->pic_url;
  652. $parent_id = $this->parent_id;
  653. $status = $this->status;
  654. $model = Cat::findOne($id)? : new Cat();
  655. if($this->mch_id){
  656. $model = MchCat::findOne($id)? : new MchCat();
  657. $model->mch_id = $this->mch_id;
  658. }
  659. $model->parent_id = $parent_id;
  660. $model->name = $cat_name;
  661. $model->store_id = get_store_id();
  662. $model->pic_url = $pic_url;
  663. $model->is_show = $status;
  664. $model->created_at = time();
  665. $model->updated_at = time();
  666. if (!$model->save()) {
  667. throw new \Exception(json_encode($model->errors));
  668. }
  669. return [
  670. 'code' => 0,
  671. 'msg' => "保存成功"
  672. ];
  673. } catch (\Exception $e){
  674. return [
  675. 'code' => 1,
  676. 'msg' => $e->getMessage()
  677. ];
  678. }
  679. }
  680. //分类状态更改
  681. public function setCatStatus(){
  682. try {
  683. $id = $this->id;
  684. $status = $this->status;
  685. if (empty($id)) {
  686. throw new \Exception("找不到对应的数据信息");
  687. }
  688. $table = new Cat;
  689. if($this->mch_id){
  690. $table = new MchCat;
  691. }
  692. $model = $table::findOne($id);
  693. if (empty($model)) {
  694. throw new \Exception("找不到对应的数据信息");
  695. }
  696. switch ($status) {
  697. case 0:
  698. $res = $table::updateAll(['is_show' => 0], ['OR', ['id' => $id], ['parent_id' => $id]]);
  699. break;
  700. case 1:
  701. $res = $table::updateAll(['is_show' => 1], ['OR', ['id' => $id], ['parent_id' => $id]]);
  702. break;
  703. case 2:
  704. $res = $table::updateAll(['is_delete' => 1], ['OR', ['id' => $id], ['parent_id' => $id]]);
  705. break;
  706. }
  707. if (!$res) {
  708. throw new \Exception("失败");
  709. }
  710. return [
  711. 'code' => 0,
  712. 'msg' => "成功"
  713. ];
  714. } catch (\Exception $e){
  715. return [
  716. 'code' => 1,
  717. 'msg' => $e->getMessage()
  718. ];
  719. }
  720. }
  721. //erp扫码入库
  722. public function erpScanGetGoodsList() {
  723. $store_id = $this->store_id;
  724. $goods_no = $this->goods_no;
  725. try {
  726. $erp_open = intval(Option::get(OptionSetting::ERP_OPEN, $store_id, 'store', 0)['value']);
  727. if (!$erp_open) {
  728. throw new \Exception('未开启本地进销存');
  729. }
  730. if (empty($goods_no)) {
  731. return [
  732. 'code' => 0,
  733. 'msg' => '货号不能为空',
  734. 'data' => [
  735. 'list' => []
  736. ]
  737. ];
  738. }
  739. $goods_id = Goods::find()->where(['store_id' => $store_id, 'is_delete' => 0])
  740. ->andWhere(['LIKE', 'goods_no', $goods_no])->select('id')->column();
  741. $query = ErpInventory::find()->alias('ei')->where(['ei.is_delete' => 0, 'ei.store_id' => $store_id]);
  742. $query->leftJoin(['g' => Goods::tableName()], 'g.id = ei.goods_id');
  743. if ($goods_id) {
  744. $query->andWhere(['OR', ['ei.goods_no' => $goods_no], ['LIKE', 'ei.sku_id', $goods_no], ['ei.goods_id' => $goods_id]]);
  745. } else {
  746. $query->andWhere(['OR', ['ei.goods_no' => $goods_no], ['LIKE', 'ei.sku_id', $goods_no]]);
  747. }
  748. $query->orderBy('ei.goods_id DESC');
  749. $data = $query->select('ei.*, g.name goods_name, g.cover_pic')->asArray()->all();
  750. foreach ($data as &$item) {
  751. // $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']);
  752. $attr_info = json_decode($item['attr_info'], true);
  753. $item['attr_names'] = implode(',', array_column($attr_info['attr_list'], 'attr_name'));
  754. $item['price'] = $attr_info['price'];
  755. if($attr_info['pic']){
  756. $item['cover_pic'] = $attr_info['pic'];
  757. }
  758. $item['num'] = 0;
  759. }
  760. return [
  761. 'code' => 0,
  762. 'msg' => 'success',
  763. 'data' => [
  764. 'list' => $data
  765. ],
  766. // 'q' => $query->createCommand()->getRawSql(),
  767. ];
  768. } catch (\Exception $e) {
  769. \Yii::error($e);
  770. return [
  771. 'code' => 1,
  772. 'msg' => $e->getMessage()
  773. ];
  774. }
  775. }
  776. //入库
  777. public function purchaseInSave ()
  778. {
  779. $t = \Yii::$app->db->beginTransaction();
  780. try {
  781. $store_id = $this->store_id;
  782. $erp_open = intval(Option::get(OptionSetting::ERP_OPEN, $store_id, 'store', 0)['value']);
  783. if (!$erp_open) {
  784. throw new \Exception('未开启本地进销存');
  785. }
  786. $admin = Admin::findOne(['type' => 'store', 'type_id' => $store_id, 'is_delete' => 0]);
  787. $model = new ErpPurchaseinOrder();
  788. $model->operator = $admin->name;
  789. $model->operator_id = $admin->id;
  790. $model->store_id = $this->store_id;
  791. $model->order_no = OrderNo::getOrderNo(OrderNo::ERP_PURCHASE_IN);
  792. $model->supplier_id = 0;
  793. $model->num = count($this->purchase);
  794. if (!$model->save()) {
  795. \Yii::error([__METHOD__, $model->attributes]);
  796. throw new \Exception(json_encode($model->errors, JSON_UNESCAPED_UNICODE));
  797. }
  798. ErpPurchasein::saveList($model, $this->purchase);
  799. $t->commit();
  800. return [
  801. 'code' => 0,
  802. 'msg' => '操作成功!'
  803. ];
  804. } catch (\Exception $e) {
  805. \Yii::error($e);
  806. $t->rollBack();
  807. return [
  808. 'code' => 1,
  809. 'msg' => $e->getMessage()
  810. ];
  811. }
  812. }
  813. //递归
  814. public function getmenu($menu, $id = 0, $show_parent = 0, $name = ""){
  815. $arr = [];
  816. foreach ($menu as $k => $v) {
  817. if($v['parent_id'] == $id){
  818. if ($show_parent == 1) {
  819. $v['parent_name'] = $name;
  820. $v['children'] = $this -> getmenu($menu, $v['id'], $show_parent, $v['name']);
  821. }else {
  822. $v['children'] = $this -> getmenu($menu, $v['id']);
  823. }
  824. $arr[] = $v;
  825. }
  826. }
  827. return $arr;
  828. }
  829. }