PurchaseForm.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\models\erp;
  8. use app\utils\OrderNo;
  9. use app\models\ErpInventory;
  10. use app\models\ErpInventoryLog;
  11. use app\models\ErpSupplier;
  12. use app\models\ErpPurchase;
  13. use app\models\ErpPurchaseOrder;
  14. use app\models\Goods;
  15. use app\models\Option;
  16. use app\constants\OptionSetting;
  17. use app\models\Store;
  18. use app\models\District;
  19. class PurchaseForm extends Model
  20. {
  21. public $export;
  22. public $store_id;
  23. public $id;
  24. public $ids;
  25. public $goods_name;
  26. public $supplier_name;
  27. public $goods_id;
  28. public $supplier_id;
  29. public $is_delete;
  30. public $status;
  31. public $purchase = [];
  32. public function rules()
  33. {
  34. return [
  35. [['id', 'store_id', 'is_delete', 'status', 'export'], 'integer'],
  36. [['ids', 'goods_name', 'supplier_name'], 'string'],
  37. [['is_delete', 'goods_id', 'supplier_id', 'purchase'], 'safe'],
  38. ];
  39. }
  40. public function init() {
  41. parent::init();
  42. if(empty($this->store_id)){
  43. $this->store_id = get_store_id();
  44. }
  45. }
  46. public function search ()
  47. {
  48. try {
  49. $query = ErpPurchaseOrder::find()->alias('epo')->where(['epo.is_delete' => 0, 'epo.store_id' => $this->store_id]);
  50. $query->leftJoin(['es' => ErpSupplier::tableName()], 'es.id = epo.supplier_id');
  51. if (!empty($this->goods_name)) {
  52. $query1 = ErpPurchase::find()->alias('ep')
  53. ->leftJoin(['ei' => ErpInventory::tableName()], 'ei.id = ep.inventory_id')
  54. ->leftJoin(['g' => Goods::tableName()], 'ei.goods_id = g.id')
  55. ->where(['like', 'g.name', $this->goods_name])
  56. ->groupBy('ep.purchase_order_id')
  57. ->select('ep.purchase_order_id');
  58. $query->andWhere(['epo.id' => $query1]);
  59. }
  60. if (!empty($this->goods_id)) {
  61. $query2 = ErpPurchase::find()->alias('ep')
  62. ->leftJoin(['ei' => ErpInventory::tableName()], 'ei.id = ep.inventory_id')
  63. ->where(['ei.goods_id' => $this->goods_id])
  64. ->groupBy('ep.purchase_order_id')
  65. ->select('ep.purchase_order_id');
  66. $query->andWhere(['epo.id' => $query2]);
  67. }
  68. if (!empty($this->supplier_name)) {
  69. $query->andWhere(['epo.supplier_id' => ErpSupplier::find()->select('id')->where(['like', 'name', $this->supplier_name])]);
  70. }
  71. if (!empty($this->supplier_id)) {
  72. $query->andWhere(['epo.supplier_id' => $this->supplier_id]);
  73. }
  74. if ($this->status > -1) {
  75. $query->andWhere(['epo.status' => $this->status]);
  76. }
  77. if ($this->ids) {
  78. $query->andWhere(['epo.id' => $this->ids]);
  79. }
  80. $query->orderBy('epo.id DESC');
  81. $query->select('epo.*, es.name supplier_name');
  82. $pagination = pagination_make($query);
  83. foreach ($pagination['list'] as &$item) {
  84. $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']);
  85. }
  86. return [
  87. 'code' => 0,
  88. 'msg' => 'success',
  89. 'data' => $pagination,
  90. // 'q' => $query->createCommand()->getRawSql(),
  91. ];
  92. } catch (\Exception $e) {
  93. \Yii::error($e);
  94. return [
  95. 'code' => 1,
  96. 'msg' => $e->getMessage()
  97. ];
  98. }
  99. }
  100. public function info ()
  101. {
  102. try {
  103. $epo = ErpPurchaseOrder::find()->where(['id' => $this->id, 'store_id' => $this->store_id])->one();
  104. $epo['created_at'] = date("Y-m-d H:i:s", $epo['created_at']);
  105. $supplier_name = ErpSupplier::findOne($epo['supplier_id'])['name'];
  106. $ep = ErpPurchase::find()->alias('ep')
  107. ->leftJoin(['ei' => ErpInventory::tableName()], 'ei.id = ep.inventory_id')
  108. ->leftJoin(['g' => Goods::tableName()], 'ei.goods_id = g.id')
  109. ->where(['ep.purchase_order_id' => $this->id])
  110. ->select('ep.id, ep.num, g.cover_pic, g.name goods_name, ei.attr_info')
  111. ->asArray()->all();
  112. $totalPrice = 0;
  113. foreach ($ep as &$item) {
  114. $attr_info = json_decode($item['attr_info'], true);
  115. $item['attr_names'] = implode(',', array_column($attr_info['attr_list'], 'attr_name'));
  116. $item['goods_price'] = $attr_info['price'];
  117. $totalPrice += $item['goods_price'] * $item['num'];
  118. }
  119. $store = Store::findOne($this->store_id);
  120. $district = implode('', District::find()->where(['id' => [$store['province_id'], $store['city_id'], $store['district_id']]])->select('name')->column());
  121. return [
  122. 'code' => 0,
  123. 'msg' => 'success',
  124. 'data' => [
  125. 'supplier_name' => $supplier_name,
  126. 'epo' => $epo,
  127. 'ep' => $ep,
  128. 'totalPrice' => $totalPrice,
  129. 'store' => [
  130. 'name' => $store['name'],
  131. 'contact_tel' => $store['contact_tel'],
  132. 'address' => $store['address'],
  133. 'district' => $district,
  134. ]
  135. ],
  136. // 'q' => $query->createCommand()->getRawSql(),
  137. ];
  138. } catch (\Exception $e) {
  139. \Yii::error($e);
  140. return [
  141. 'code' => 1,
  142. 'msg' => $e->getMessage()
  143. ];
  144. }
  145. }
  146. public function searchPurchase ()
  147. {
  148. try {
  149. $query = ErpPurchase::find()->alias('ep')
  150. ->leftJoin(['epo' => ErpPurchaseOrder::tableName()], 'ep.purchase_order_id = epo.id')
  151. ->leftJoin(['ei' => ErpInventory::tableName()], 'ei.id = ep.inventory_id')
  152. ->leftJoin(['g' => Goods::tableName()], 'ei.goods_id = g.id')
  153. ->where(['epo.is_delete' => 0, 'epo.store_id' => $this->store_id]);
  154. $query->leftJoin(['es' => ErpSupplier::tableName()], 'es.id = epo.supplier_id');
  155. if ($this->ids) {
  156. if(!is_array($this->ids)){
  157. $this->ids = explode(',', $this->ids);
  158. }
  159. $query->andWhere(['epo.id' => $this->ids]);
  160. }
  161. $query->orderBy('epo.id DESC');
  162. $query->select('ep.id, ep.num, es.name supplier_name, epo.order_no, epo.created_at, g.cover_pic, g.name goods_name, ei.attr_info');
  163. $pagination = pagination_make($query);
  164. foreach ($pagination['list'] as &$item) {
  165. $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']);
  166. $attr_info = json_decode($item['attr_info'], true);
  167. $item['attr_names'] = implode(',', array_column($attr_info['attr_list'], 'attr_name'));
  168. $item['goods_price'] = $attr_info['price'];
  169. }
  170. if($this->export){
  171. return $this->export($pagination['list']);
  172. }
  173. return [
  174. 'code' => 0,
  175. 'msg' => 'success',
  176. 'data' => $pagination,
  177. // 'q' => $query->createCommand()->getRawSql(),
  178. ];
  179. } catch (\Exception $e) {
  180. \Yii::error($e);
  181. return [
  182. 'code' => 1,
  183. 'msg' => $e->getMessage()
  184. ];
  185. }
  186. }
  187. private function export($list) {
  188. $rows = [[
  189. 'ID',
  190. '采购单号',
  191. '供货商',
  192. '商品',
  193. '规格',
  194. '数量',
  195. '单价',
  196. '时间',
  197. ]];
  198. foreach($list as $item){
  199. $r = [
  200. $item['id'],
  201. $item['order_no'],
  202. $item['supplier_name'],
  203. $item['goods_name'],
  204. $item['attr_names'],
  205. $item['num'],
  206. $item['goods_price'],
  207. $item['created_at'],
  208. ];
  209. $rows[] = $r;
  210. }
  211. $writer = \Spatie\SimpleExcel\SimpleExcelWriter::streamDownload(time() . '.xlsx')->noHeaderRow()
  212. ->addRows($rows)->toBrowser();
  213. }
  214. public function save ()
  215. {
  216. $t = \Yii::$app->db->beginTransaction();
  217. try {
  218. $model = new ErpPurchaseOrder();
  219. $model->order_no = OrderNo::getOrderNo(OrderNo::ERP_PURCHASE);
  220. $model->store_id = $this->store_id;
  221. $model->supplier_id = $this->supplier_id;
  222. $model->num = count($this->purchase);
  223. if (!$model->save()) {
  224. \Yii::error([__METHOD__, $model->attributes]);
  225. throw new \Exception(array_shift($model->getFirstErrors()));
  226. }
  227. ErpPurchase::saveList($model->id, $this->purchase);
  228. $t->commit();
  229. return [
  230. 'code' => 0,
  231. 'msg' => '操作成功!'
  232. ];
  233. } catch (\Exception $e) {
  234. \Yii::error($e);
  235. $t->rollBack();
  236. return [
  237. 'code' => 1,
  238. 'msg' => $e->getMessage()
  239. ];
  240. }
  241. }
  242. public function statusPrint ()
  243. {
  244. $t = \Yii::$app->db->beginTransaction();
  245. try {
  246. $model = ErpPurchaseOrder::findOne($this->id);
  247. $model->status = 1;
  248. if (!$model->save()) {
  249. \Yii::error([__METHOD__, $model->attributes]);
  250. throw new \Exception(array_shift($model->getFirstErrors()));
  251. }
  252. ErpPurchase::saveList($model->id, $this->purchase);
  253. $t->commit();
  254. return [
  255. 'code' => 0,
  256. 'msg' => '操作成功!'
  257. ];
  258. } catch (\Exception $e) {
  259. \Yii::error($e);
  260. $t->rollBack();
  261. return [
  262. 'code' => 1,
  263. 'msg' => $e->getMessage()
  264. ];
  265. }
  266. }
  267. }