PurchaseForm.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models\agent\front_erp;
  8. use app\models\Admin;
  9. use app\models\AgentFrontBind;
  10. use app\models\AgentFrontErpCloudGoods;
  11. use app\models\Supplier;
  12. use app\utils\OrderNo;
  13. use app\models\AgentFrontErpInventory;
  14. use app\models\AgentFrontErpInventoryLog;
  15. use app\models\AgentFrontErpPurchase;
  16. use app\models\AgentFrontErpPurchaseOrder;
  17. use app\models\Goods;
  18. use app\models\Option;
  19. use app\constants\OptionSetting;
  20. use app\models\Store;
  21. use app\models\District;
  22. class PurchaseForm extends Model
  23. {
  24. public $export;
  25. public $store_id;
  26. public $id;
  27. public $ids;
  28. public $goods_name;
  29. public $supplier_name;
  30. public $goods_id;
  31. public $supplier_id;
  32. public $is_delete;
  33. public $status;
  34. public $purchase = [];
  35. public $front_agent_admin_id;
  36. public $admin_supplier_id;
  37. public function rules()
  38. {
  39. return [
  40. [['id', 'store_id', 'is_delete', 'status', 'export', 'front_agent_admin_id', 'admin_supplier_id'], 'integer'],
  41. [['ids', 'goods_name', 'supplier_name'], 'string'],
  42. [['is_delete', 'goods_id', 'supplier_id', 'purchase'], 'safe'],
  43. ];
  44. }
  45. public function init() {
  46. parent::init();
  47. if(empty($this->store_id)){
  48. $this->store_id = get_store_id();
  49. }
  50. }
  51. public function search ()
  52. {
  53. try {
  54. $where = [
  55. 'epo.is_delete' => 0
  56. ];
  57. if (isset($this->admin_supplier_id)) {
  58. $where['epo.supplier_id'] = $this->admin_supplier_id;
  59. } else {
  60. $where['epo.front_agent_admin_id'] = $this->front_agent_admin_id;
  61. }
  62. $query = AgentFrontErpPurchaseOrder::find()->alias('epo')->where($where);
  63. $query->leftJoin(['s' => Supplier::tableName()], 's.id = epo.supplier_id');
  64. if (!empty($this->goods_name)) {
  65. $query1 = AgentFrontErpPurchase::find()->alias('ep')
  66. ->leftJoin(['ei' => AgentFrontErpInventory::tableName()], 'ei.id = ep.inventory_id')
  67. ->leftJoin(['g' => Goods::tableName()], 'ei.goods_id = g.id')
  68. ->where(['like', 'g.name', $this->goods_name])
  69. ->groupBy('ep.purchase_order_id')
  70. ->select('ep.purchase_order_id');
  71. $query->andWhere(['epo.id' => $query1]);
  72. }
  73. if (!empty($this->goods_id)) {
  74. $query2 = AgentFrontErpPurchase::find()->alias('ep')
  75. ->leftJoin(['ei' => AgentFrontErpInventory::tableName()], 'ei.id = ep.inventory_id')
  76. ->where(['ei.goods_id' => $this->goods_id])
  77. ->groupBy('ep.purchase_order_id')
  78. ->select('ep.purchase_order_id');
  79. $query->andWhere(['epo.id' => $query2]);
  80. }
  81. if (!empty($this->supplier_name)) {
  82. $query->andWhere(['like', 's.supplier_name', $this->supplier_name]);
  83. }
  84. if (!empty($this->supplier_id)) {
  85. $query->andWhere(['epo.supplier_id' => $this->supplier_id]);
  86. }
  87. if ($this->status > -1) {
  88. $query->andWhere(['epo.status' => $this->status]);
  89. }
  90. if ($this->ids) {
  91. $query->andWhere(['epo.id' => $this->ids]);
  92. }
  93. $query->orderBy('epo.id DESC');
  94. $query->select('epo.*, s.supplier_name');
  95. $pagination = pagination_make($query);
  96. foreach ($pagination['list'] as &$item) {
  97. $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']);
  98. $item['order_status'] = intval($item['order_status']);
  99. $item['order_status_text'] = AgentFrontErpPurchaseOrder::ORDER_STATUS_ARR[$item['order_status']];
  100. }
  101. return [
  102. 'code' => 0,
  103. 'msg' => 'success',
  104. 'data' => $pagination,
  105. // 'q' => $query->createCommand()->getRawSql(),
  106. ];
  107. } catch (\Exception $e) {
  108. \Yii::error($e);
  109. return [
  110. 'code' => 1,
  111. 'msg' => $e->getMessage()
  112. ];
  113. }
  114. }
  115. public function info ()
  116. {
  117. try {
  118. $where = ['id' => $this->id];
  119. if (isset($this->admin_supplier_id)) {
  120. $where['supplier_id'] = $this->admin_supplier_id;
  121. } else {
  122. $where['front_agent_admin_id'] = $this->front_agent_admin_id;
  123. }
  124. $epo = AgentFrontErpPurchaseOrder::find()->where($where)->one();
  125. if (!$epo) {
  126. throw new \Exception('查询采购详情失败');
  127. }
  128. $epo['created_at'] = date("Y-m-d H:i:s", $epo['created_at']);
  129. $supplier_name = Supplier::findOne($epo['supplier_id'])['supplier_name'];
  130. $ep = AgentFrontErpPurchase::find()->alias('ep')
  131. ->leftJoin(['ei' => AgentFrontErpInventory::tableName()], 'ei.id = ep.inventory_id')
  132. ->leftJoin(['g' => AgentFrontErpCloudGoods::tableName()], 'ei.goods_id = g.cloud_goods_id')
  133. ->where(['ep.purchase_order_id' => $this->id])
  134. ->select('ep.id, ep.num, g.cover_pic, g.goods_name, ei.attr_info')
  135. ->asArray()->all();
  136. $totalPrice = 0;
  137. foreach ($ep as &$item) {
  138. $attr_info = json_decode($item['attr_info'], true);
  139. $item['attr_names'] = implode(',', array_column($attr_info['attr_list'], 'attr_name'));
  140. $item['goods_price'] = $attr_info['price'];
  141. $totalPrice += $item['goods_price'] * $item['num'];
  142. }
  143. $admin = Admin::findOne($epo['front_agent_admin_id']);
  144. $district = implode('', District::find()->where(['id' => [$admin->province_id, $admin->city_id, $admin->district_id]])->select('name')->column());
  145. return [
  146. 'code' => 0,
  147. 'msg' => 'success',
  148. 'data' => [
  149. 'supplier_name' => $supplier_name,
  150. 'epo' => $epo,
  151. 'ep' => $ep,
  152. 'totalPrice' => $totalPrice,
  153. 'store' => [
  154. 'name' => $admin->name,
  155. 'contact_tel' => $admin->mobile,
  156. 'address' => $admin->address,
  157. 'district' => $district,
  158. ]
  159. ],
  160. // 'q' => $query->createCommand()->getRawSql(),
  161. ];
  162. } catch (\Exception $e) {
  163. \Yii::error($e);
  164. return [
  165. 'code' => 1,
  166. 'msg' => $e->getMessage()
  167. ];
  168. }
  169. }
  170. public function searchPurchase ()
  171. {
  172. try {
  173. $where = [
  174. 'epo.is_delete' => 0
  175. ];
  176. if (isset($this->admin_supplier_id)) {
  177. $where['epo.supplier_id'] = $this->admin_supplier_id;
  178. } else {
  179. $where['epo.front_agent_admin_id'] = $this->front_agent_admin_id;
  180. }
  181. $query = AgentFrontErpPurchase::find()->alias('ep')
  182. ->leftJoin(['epo' => AgentFrontErpPurchaseOrder::tableName()], 'ep.purchase_order_id = epo.id')
  183. ->leftJoin(['ei' => AgentFrontErpInventory::tableName()], 'ei.id = ep.inventory_id')
  184. ->leftJoin(['g' => AgentFrontErpCloudGoods::tableName()], 'ei.goods_id = g.cloud_goods_id')
  185. ->where($where);
  186. $query->leftJoin(['es' => Supplier::tableName()], 'es.id = epo.supplier_id');
  187. if ($this->ids) {
  188. if(!is_array($this->ids)){
  189. $this->ids = explode(',', $this->ids);
  190. }
  191. $query->andWhere(['epo.id' => $this->ids]);
  192. }
  193. $query->orderBy('epo.id DESC');
  194. $query->select('ep.id, ep.num, es.supplier_name, epo.order_no, epo.created_at, g.cover_pic, g.goods_name, ei.attr_info, epo.front_agent_admin_id');
  195. $pagination = pagination_make($query);
  196. foreach ($pagination['list'] as &$item) {
  197. $admin = Admin::findOne($item['front_agent_admin_id']);
  198. $item['front_agent_name'] = $admin->name ?? '';
  199. $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']);
  200. $attr_info = json_decode($item['attr_info'], true);
  201. $item['attr_names'] = implode(',', array_column($attr_info['attr_list'], 'attr_name'));
  202. $item['goods_price'] = $attr_info['price'];
  203. }
  204. if($this->export){
  205. return $this->export($pagination['list']);
  206. }
  207. return [
  208. 'code' => 0,
  209. 'msg' => 'success',
  210. 'data' => $pagination,
  211. // 'q' => $query->createCommand()->getRawSql(),
  212. ];
  213. } catch (\Exception $e) {
  214. \Yii::error($e);
  215. return [
  216. 'code' => 1,
  217. 'msg' => $e->getMessage()
  218. ];
  219. }
  220. }
  221. private function export($list) {
  222. $rows = [[
  223. 'ID',
  224. '采购单号',
  225. isset($this->admin_supplier_id) ? '仓库' : '供货商',
  226. '商品',
  227. '规格',
  228. '数量',
  229. '单价',
  230. '时间',
  231. ]];
  232. foreach($list as $item){
  233. $r = [
  234. $item['id'],
  235. $item['order_no'],
  236. isset($this->admin_supplier_id) ? $item['front_agent_name'] : $item['supplier_name'],
  237. $item['goods_name'],
  238. $item['attr_names'],
  239. $item['num'],
  240. $item['goods_price'],
  241. $item['created_at'],
  242. ];
  243. $rows[] = $r;
  244. }
  245. $writer = \Spatie\SimpleExcel\SimpleExcelWriter::streamDownload(time() . '.xlsx')->noHeaderRow()
  246. ->addRows($rows)->toBrowser();
  247. }
  248. public function save ()
  249. {
  250. $t = \Yii::$app->db->beginTransaction();
  251. try {
  252. $supplier = Supplier::findOne(['id' => $this->supplier_id, 'is_delete' => 0]);
  253. if (!$supplier) {
  254. throw new \Exception('供货商不存在');
  255. }
  256. if (!in_array($supplier->cloud_supplier_id, AgentFrontBind::getAgentFrontBindSupplierId($this->front_agent_admin_id) ?? [])) {
  257. throw new \Exception('当前供货商商品未与仓库关联');
  258. }
  259. //采购单生成时候给云仓通知减少对应规格的库存(生成时候判断云仓库存是否足够,足够再减少) start
  260. $purchase = $this->purchase;
  261. foreach($purchase as $purchase_index => $purchase_item){
  262. $updateAttrList = [];
  263. $erpInventory = AgentFrontErpInventory::findOne($purchase_item['id']);
  264. if ($erpInventory) {
  265. $attr_info = json_decode($erpInventory->attr_info, true);
  266. if (!empty($attr_info)) {
  267. $updateAttrList = [
  268. [
  269. 'attr_id_list' => array_column($attr_info['attr_list'], 'attr_id'),
  270. 'num' => -$purchase_item['num']
  271. ]
  272. ];
  273. }
  274. $form = new \app\modules\admin\models\SupplierForm();
  275. $res = $form->setGoodsAttrNum($this->supplier_id, $erpInventory->goods_id, $updateAttrList);
  276. //兼容选择多个但是云仓报错的问题
  277. if ($res['code'] !== 0) {
  278. unset($purchase[$purchase_index]);
  279. }
  280. }
  281. }
  282. //end
  283. $model = new AgentFrontErpPurchaseOrder();
  284. $model->order_no = OrderNo::getOrderNo(OrderNo::ERP_PURCHASE);
  285. $model->front_agent_admin_id = $this->front_agent_admin_id;
  286. $model->supplier_id = $this->supplier_id;
  287. $model->num = count($this->purchase);
  288. if (!$model->save()) {
  289. \Yii::error([__METHOD__, $model->attributes]);
  290. throw new \Exception(array_shift($model->getFirstErrors()));
  291. }
  292. AgentFrontErpPurchase::saveList($model->id, $this->purchase);
  293. $t->commit();
  294. return [
  295. 'code' => 0,
  296. 'msg' => '操作成功!'
  297. ];
  298. } catch (\Exception $e) {
  299. \Yii::error($e);
  300. $t->rollBack();
  301. return [
  302. 'code' => 1,
  303. 'msg' => $e->getMessage()
  304. ];
  305. }
  306. }
  307. public function statusPrint ()
  308. {
  309. $t = \Yii::$app->db->beginTransaction();
  310. try {
  311. $model = AgentFrontErpPurchaseOrder::findOne($this->id);
  312. $model->status = 1;
  313. if (!$model->save()) {
  314. \Yii::error([__METHOD__, $model->attributes]);
  315. throw new \Exception(array_shift($model->getFirstErrors()));
  316. }
  317. AgentFrontErpPurchase::saveList($model->id, $this->purchase);
  318. $t->commit();
  319. return [
  320. 'code' => 0,
  321. 'msg' => '操作成功!'
  322. ];
  323. } catch (\Exception $e) {
  324. \Yii::error($e);
  325. $t->rollBack();
  326. return [
  327. 'code' => 1,
  328. 'msg' => $e->getMessage()
  329. ];
  330. }
  331. }
  332. public function setPurchaseOrderStatus() {
  333. try {
  334. $admin_supplier_id = $this->admin_supplier_id;
  335. $front_agent_admin_id = $this->front_agent_admin_id;
  336. $id = $this->id;
  337. $where = ['id' => $id];
  338. if (isset($admin_supplier_id)) {
  339. $where['supplier_id'] = $admin_supplier_id;
  340. } else {
  341. $where['front_agent_admin_id'] = $front_agent_admin_id;
  342. }
  343. $epo = AgentFrontErpPurchaseOrder::findOne($where);
  344. if (!$epo) {
  345. throw new \Exception('查询采购信息失败');
  346. }
  347. //仓库身份不可发货
  348. if (isset($front_agent_admin_id) && $epo->order_status == AgentFrontErpPurchaseOrder::ORDER_STATUS_CREATE) {
  349. throw new \Exception('当前身份操作权限失败,不可发货');
  350. }
  351. //供货商身份不可收货
  352. if (isset($admin_supplier_id) && $epo->order_status == AgentFrontErpPurchaseOrder::ORDER_STATUS_SEND) {
  353. throw new \Exception('当前身份操作权限失败,不可收货');
  354. }
  355. if (intval($epo->order_status) === AgentFrontErpPurchaseOrder::ORDER_STATUS_CONFIRM) {
  356. throw new \Exception('状态已达最终态不可操作');
  357. }
  358. //发货
  359. if (intval($epo->order_status) === AgentFrontErpPurchaseOrder::ORDER_STATUS_CREATE) {
  360. $epo->order_status = AgentFrontErpPurchaseOrder::ORDER_STATUS_SEND;
  361. } elseif (intval($epo->order_status) === AgentFrontErpPurchaseOrder::ORDER_STATUS_SEND) {
  362. //收货
  363. $epo->order_status = AgentFrontErpPurchaseOrder::ORDER_STATUS_CONFIRM;
  364. }
  365. //收货完成 开始添加库存
  366. if (!$epo->save()) {
  367. throw new \Exception(json_encode($epo->errors, JSON_UNESCAPED_UNICODE));
  368. }
  369. //产品入库时候增加商品库存
  370. //下单以及转单时候只判断仓库是否有货
  371. //转单时候减少仓库库存 云仓判断如果是仓库配送产品转单时候就不减少库存
  372. return [
  373. 'code' => 0,
  374. 'msg' => '操作成功'
  375. ];
  376. } catch (\Exception $e) {
  377. return [
  378. 'code' => 1,
  379. 'msg' => $e->getMessage()
  380. ];
  381. }
  382. }
  383. }