StoreAdminGoodsForm.php 38 KB

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