PurchaseinForm.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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\ErpPurchasein;
  13. use app\models\ErpPurchaseinOrder;
  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 PurchaseinForm extends Model
  20. {
  21. public $export;
  22. public $store_id;
  23. public $id;
  24. public $ids;
  25. public $inventory_id;
  26. public $goods_name;
  27. public $supplier_name;
  28. public $goods_id;
  29. public $supplier_id;
  30. public $is_delete;
  31. public $status;
  32. public $remark;
  33. public $purchase = [];
  34. public $operator;
  35. public $operator_id;
  36. public $order_no;
  37. public function rules()
  38. {
  39. return [
  40. [['id', 'store_id', 'is_delete', 'status', 'export'], 'integer'],
  41. [['ids', 'goods_name', 'supplier_name'], 'string'],
  42. [['is_delete', 'goods_id', 'supplier_id', 'purchase', 'operator', 'operator_id', 'order_no', 'remark', 'inventory_id'], '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. $query = ErpPurchaseinOrder::find()->alias('epo')->where(['epo.is_delete' => 0, 'epo.store_id' => $this->store_id]);
  55. $query->leftJoin(['es' => ErpSupplier::tableName()], 'es.id = epo.supplier_id');
  56. if (!empty($this->inventory_id)) {
  57. $query->andWhere(['epo.id' => ErpPurchasein::find()->select('purchase_order_id')->where(['inventory_id' => $this->inventory_id])]);
  58. }
  59. if (!empty($this->operator_id)) {
  60. $query->andWhere(['epo.operator_id' => $this->operator_id]);
  61. }
  62. if (!empty($this->operator)) {
  63. $query->andWhere(['like', 'epo.operator', $this->operator]);
  64. }
  65. if (!empty($this->order_no)) {
  66. $query->andWhere(['like', 'epo.order_no', $this->order_no]);
  67. }
  68. if (!empty($this->goods_name)) {
  69. $query1 = ErpPurchasein::find()->alias('ep')
  70. ->leftJoin(['ei' => ErpInventory::tableName()], 'ei.id = ep.inventory_id')
  71. ->leftJoin(['g' => Goods::tableName()], 'ei.goods_id = g.id')
  72. ->where(['like', 'g.name', $this->goods_name])
  73. ->groupBy('ep.purchase_order_id')
  74. ->select('ep.purchase_order_id');
  75. $query->andWhere(['epo.id' => $query1]);
  76. }
  77. if (!empty($this->goods_id)) {
  78. $query2 = ErpPurchasein::find()->alias('ep')
  79. ->leftJoin(['ei' => ErpInventory::tableName()], 'ei.id = ep.inventory_id')
  80. ->where(['ei.goods_id' => $this->goods_id])
  81. ->groupBy('ep.purchase_order_id')
  82. ->select('ep.purchase_order_id');
  83. $query->andWhere(['epo.id' => $query2]);
  84. }
  85. if (!empty($this->supplier_name)) {
  86. $query->andWhere(['epo.supplier_id' => ErpSupplier::find()->select('id')->where(['like', 'name', $this->supplier_name])]);
  87. }
  88. if (!empty($this->supplier_id)) {
  89. $query->andWhere(['epo.supplier_id' => $this->supplier_id]);
  90. }
  91. if ($this->status > -1) {
  92. $query->andWhere(['epo.status' => $this->status]);
  93. }
  94. if ($this->ids) {
  95. $query->andWhere(['epo.id' => $this->ids]);
  96. }
  97. $query->orderBy('epo.id DESC');
  98. $query->select('epo.*, es.name supplier_name');
  99. $pagination = pagination_make($query);
  100. foreach ($pagination['list'] as &$item) {
  101. $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']);
  102. }
  103. return [
  104. 'code' => 0,
  105. 'msg' => 'success',
  106. 'data' => $pagination,
  107. // 'q' => $query->createCommand()->getRawSql(),
  108. ];
  109. } catch (\Exception $e) {
  110. \Yii::error($e);
  111. return [
  112. 'code' => 1,
  113. 'msg' => $e->getMessage()
  114. ];
  115. }
  116. }
  117. public function info ()
  118. {
  119. try {
  120. $epo = ErpPurchaseinOrder::find()->where(['id' => $this->id, 'store_id' => $this->store_id])->one();
  121. $epo['created_at'] = date("Y-m-d H:i:s", $epo['created_at']);
  122. $supplier_name = ErpSupplier::findOne($epo['supplier_id'])['name'];
  123. $ep = ErpPurchasein::find()->alias('ep')
  124. ->leftJoin(['ei' => ErpInventory::tableName()], 'ei.id = ep.inventory_id')
  125. ->leftJoin(['g' => Goods::tableName()], 'ei.goods_id = g.id')
  126. ->where(['ep.purchase_order_id' => $this->id])
  127. ->select('ep.*, g.cover_pic, g.name goods_name, ei.attr_info')
  128. ->asArray()->all();
  129. $totalPrice = 0;
  130. foreach ($ep as &$item) {
  131. $attr_info = json_decode($item['attr_info'], true);
  132. $item['attr_names'] = implode(',', array_column($attr_info['attr_list'], 'attr_name'));
  133. if($attr_info['pic']){
  134. $item['cover_pic'] = $attr_info['pic'];
  135. }
  136. $item['goods_price'] = $attr_info['price'];
  137. $totalPrice += $item['goods_price'] * $item['num'];
  138. }
  139. $store = Store::findOne($this->store_id);
  140. $district = implode('', District::find()->where(['id' => [$store['province_id'], $store['city_id'], $store['district_id']]])->select('name')->column());
  141. return [
  142. 'code' => 0,
  143. 'msg' => 'success',
  144. 'data' => [
  145. 'supplier_name' => $supplier_name,
  146. 'epo' => $epo,
  147. 'ep' => $ep,
  148. 'totalPrice' => $totalPrice,
  149. 'store' => [
  150. 'name' => $store['name'],
  151. 'contact_tel' => $store['contact_tel'],
  152. 'address' => $store['address'],
  153. 'district' => $district,
  154. ]
  155. ],
  156. // 'q' => $query->createCommand()->getRawSql(),
  157. ];
  158. } catch (\Exception $e) {
  159. \Yii::error($e);
  160. return [
  161. 'code' => 1,
  162. 'msg' => $e->getMessage()
  163. ];
  164. }
  165. }
  166. public function searchPurchase ()
  167. {
  168. try {
  169. $query = ErpPurchasein::find()->alias('ep')
  170. ->leftJoin(['epo' => ErpPurchaseinOrder::tableName()], 'ep.purchase_order_id = epo.id')
  171. ->leftJoin(['ei' => ErpInventory::tableName()], 'ei.id = ep.inventory_id')
  172. ->leftJoin(['g' => Goods::tableName()], 'ei.goods_id = g.id')
  173. ->where(['epo.is_delete' => 0, 'epo.store_id' => $this->store_id]);
  174. $query->leftJoin(['es' => ErpSupplier::tableName()], 'es.id = epo.supplier_id');
  175. if ($this->ids) {
  176. if(!is_array($this->ids)){
  177. $this->ids = explode(',', $this->ids);
  178. }
  179. $query->andWhere(['epo.id' => $this->ids]);
  180. }
  181. $query->orderBy('epo.id DESC');
  182. $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');
  183. $pagination = pagination_make($query);
  184. foreach ($pagination['list'] as &$item) {
  185. $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']);
  186. $attr_info = json_decode($item['attr_info'], true);
  187. $item['attr_names'] = implode(',', array_column($attr_info['attr_list'], 'attr_name'));
  188. $item['goods_price'] = $attr_info['price'];
  189. }
  190. if($this->export){
  191. return $this->export($pagination['list']);
  192. }
  193. return [
  194. 'code' => 0,
  195. 'msg' => 'success',
  196. 'data' => $pagination,
  197. // 'q' => $query->createCommand()->getRawSql(),
  198. ];
  199. } catch (\Exception $e) {
  200. \Yii::error($e);
  201. return [
  202. 'code' => 1,
  203. 'msg' => $e->getMessage()
  204. ];
  205. }
  206. }
  207. private function export($list) {
  208. $rows = [[
  209. 'ID',
  210. '入库单号',
  211. '供货商',
  212. '商品',
  213. '规格',
  214. '数量',
  215. '单价',
  216. '时间',
  217. ]];
  218. foreach($list as $item){
  219. $r = [
  220. $item['id'],
  221. $item['order_no'],
  222. $item['supplier_name'],
  223. $item['goods_name'],
  224. $item['attr_names'],
  225. $item['num'],
  226. $item['goods_price'],
  227. $item['created_at'],
  228. ];
  229. $rows[] = $r;
  230. }
  231. $writer = \Spatie\SimpleExcel\SimpleExcelWriter::streamDownload(time() . '.xlsx')->noHeaderRow()
  232. ->addRows($rows)->toBrowser();
  233. }
  234. public function save ()
  235. {
  236. $t = \Yii::$app->db->beginTransaction();
  237. try {
  238. if(!InventoryForm::isOpen($this->store_id)){
  239. throw new \Exception('操作失败!进销存功能未开启');
  240. }
  241. $model = new ErpPurchaseinOrder();
  242. $model->operator = get_admin()->name;
  243. $model->operator_id = get_admin()->id;
  244. $model->store_id = $this->store_id;
  245. $model->order_no = OrderNo::getOrderNo(OrderNo::ERP_PURCHASE_IN);
  246. $model->store_id = $this->store_id;
  247. $model->supplier_id = $this->supplier_id;
  248. $model->remark = (string)$this->remark;
  249. $model->num = count($this->purchase);
  250. if (!$model->save()) {
  251. \Yii::error([__METHOD__, $model->attributes]);
  252. throw new \Exception(array_shift($model->getFirstErrors()));
  253. }
  254. ErpPurchasein::saveList($model, $this->purchase);
  255. $t->commit();
  256. return [
  257. 'code' => 0,
  258. 'msg' => '操作成功!'
  259. ];
  260. } catch (\Exception $e) {
  261. \Yii::error($e);
  262. $t->rollBack();
  263. return [
  264. 'code' => 1,
  265. 'msg' => $e->getMessage()
  266. ];
  267. }
  268. }
  269. public function statusPrint ()
  270. {
  271. $t = \Yii::$app->db->beginTransaction();
  272. try {
  273. $model = ErpPurchaseinOrder::findOne($this->id);
  274. $model->status = 1;
  275. if (!$model->save()) {
  276. \Yii::error([__METHOD__, $model->attributes]);
  277. throw new \Exception(array_shift($model->getFirstErrors()));
  278. }
  279. ErpPurchasein::saveList($model->id, $this->purchase);
  280. $t->commit();
  281. return [
  282. 'code' => 0,
  283. 'msg' => '操作成功!'
  284. ];
  285. } catch (\Exception $e) {
  286. \Yii::error($e);
  287. $t->rollBack();
  288. return [
  289. 'code' => 1,
  290. 'msg' => $e->getMessage()
  291. ];
  292. }
  293. }
  294. }