PurchaseoutForm.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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\ErpPurchaseout;
  13. use app\models\ErpPurchaseoutOrder;
  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 PurchaseoutForm 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 $remark;
  32. public $purchase = [];
  33. public $operator;
  34. public $operator_id;
  35. public $order_no;
  36. public function rules()
  37. {
  38. return [
  39. [['id', 'store_id', 'is_delete', 'status', 'export'], 'integer'],
  40. [['ids', 'goods_name', 'supplier_name'], 'string'],
  41. [['is_delete', 'goods_id', 'supplier_id', 'purchase', 'operator', 'operator_id', 'order_no', 'remark'], 'safe'],
  42. ];
  43. }
  44. public function init() {
  45. parent::init();
  46. if(empty($this->store_id)){
  47. $this->store_id = get_store_id();
  48. }
  49. }
  50. public function search ()
  51. {
  52. try {
  53. $query = ErpPurchaseoutOrder::find()->alias('epo')->where(['epo.is_delete' => 0, 'epo.store_id' => $this->store_id]);
  54. $query->leftJoin(['es' => ErpSupplier::tableName()], 'es.id = epo.supplier_id');
  55. if (!empty($this->operator_id)) {
  56. $query->andWhere(['epo.operator_id' => $this->operator_id]);
  57. }
  58. if (!empty($this->operator)) {
  59. $query->andWhere(['like', 'epo.operator', $this->operator]);
  60. }
  61. if (!empty($this->order_no)) {
  62. $query->andWhere(['like', 'epo.order_no', $this->order_no]);
  63. }
  64. if (!empty($this->goods_name)) {
  65. $query1 = ErpPurchaseout::find()->alias('ep')
  66. ->leftJoin(['ei' => ErpInventory::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 = ErpPurchaseout::find()->alias('ep')
  75. ->leftJoin(['ei' => ErpInventory::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(['epo.supplier_id' => ErpSupplier::find()->select('id')->where(['like', '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.*, es.name 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. }
  99. return [
  100. 'code' => 0,
  101. 'msg' => 'success',
  102. 'data' => $pagination,
  103. // 'q' => $query->createCommand()->getRawSql(),
  104. ];
  105. } catch (\Exception $e) {
  106. \Yii::error($e);
  107. return [
  108. 'code' => 1,
  109. 'msg' => $e->getMessage()
  110. ];
  111. }
  112. }
  113. public function info ()
  114. {
  115. try {
  116. $epo = ErpPurchaseoutOrder::find()->where(['id' => $this->id, 'store_id' => $this->store_id])->one();
  117. $epo['created_at'] = date("Y-m-d H:i:s", $epo['created_at']);
  118. $supplier_name = ErpSupplier::findOne($epo['supplier_id'])['name'];
  119. $ep = ErpPurchaseout::find()->alias('ep')
  120. ->leftJoin(['ei' => ErpInventory::tableName()], 'ei.id = ep.inventory_id')
  121. ->leftJoin(['g' => Goods::tableName()], 'ei.goods_id = g.id')
  122. ->where(['ep.purchase_order_id' => $this->id])
  123. ->select('ep.*, g.cover_pic, g.name goods_name, ei.attr_info')
  124. ->asArray()->all();
  125. $totalPrice = 0;
  126. foreach ($ep as &$item) {
  127. $item['eip'] = \app\models\ErpInventoryProp::findOne($item['eip_id']);
  128. $attr_info = json_decode($item['attr_info'], true);
  129. $item['attr_names'] = implode(',', array_column($attr_info['attr_list'], 'attr_name'));
  130. if($attr_info['pic']){
  131. $item['cover_pic'] = $attr_info['pic'];
  132. }
  133. $item['goods_price'] = $attr_info['price'];
  134. $totalPrice += $item['goods_price'] * $item['num'];
  135. }
  136. $store = Store::findOne($this->store_id);
  137. $district = implode('', District::find()->where(['id' => [$store['province_id'], $store['city_id'], $store['district_id']]])->select('name')->column());
  138. return [
  139. 'code' => 0,
  140. 'msg' => 'success',
  141. 'data' => [
  142. 'supplier_name' => $supplier_name,
  143. 'epo' => $epo,
  144. 'ep' => $ep,
  145. 'totalPrice' => $totalPrice,
  146. 'store' => [
  147. 'name' => $store['name'],
  148. 'contact_tel' => $store['contact_tel'],
  149. 'address' => $store['address'],
  150. 'district' => $district,
  151. ]
  152. ],
  153. // 'q' => $query->createCommand()->getRawSql(),
  154. ];
  155. } catch (\Exception $e) {
  156. \Yii::error($e);
  157. return [
  158. 'code' => 1,
  159. 'msg' => $e->getMessage()
  160. ];
  161. }
  162. }
  163. public function searchPurchase ()
  164. {
  165. try {
  166. $query = ErpPurchaseout::find()->alias('ep')
  167. ->leftJoin(['epo' => ErpPurchaseoutOrder::tableName()], 'ep.purchase_order_id = epo.id')
  168. ->leftJoin(['ei' => ErpInventory::tableName()], 'ei.id = ep.inventory_id')
  169. ->leftJoin(['g' => Goods::tableName()], 'ei.goods_id = g.id')
  170. ->where(['epo.is_delete' => 0, 'epo.store_id' => $this->store_id]);
  171. $query->leftJoin(['es' => ErpSupplier::tableName()], 'es.id = epo.supplier_id');
  172. if ($this->ids) {
  173. if(!is_array($this->ids)){
  174. $this->ids = explode(',', $this->ids);
  175. }
  176. $query->andWhere(['epo.id' => $this->ids]);
  177. }
  178. $query->orderBy('epo.id DESC');
  179. $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');
  180. $pagination = pagination_make($query);
  181. foreach ($pagination['list'] as &$item) {
  182. $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']);
  183. $attr_info = json_decode($item['attr_info'], true);
  184. $item['attr_names'] = implode(',', array_column($attr_info['attr_list'], 'attr_name'));
  185. $item['goods_price'] = $attr_info['price'];
  186. }
  187. if($this->export){
  188. return $this->export($pagination['list']);
  189. }
  190. return [
  191. 'code' => 0,
  192. 'msg' => 'success',
  193. 'data' => $pagination,
  194. // 'q' => $query->createCommand()->getRawSql(),
  195. ];
  196. } catch (\Exception $e) {
  197. \Yii::error($e);
  198. return [
  199. 'code' => 1,
  200. 'msg' => $e->getMessage()
  201. ];
  202. }
  203. }
  204. private function export($list) {
  205. $rows = [[
  206. 'ID',
  207. '出库单号',
  208. '供货商',
  209. '商品',
  210. '规格',
  211. '数量',
  212. '单价',
  213. '时间',
  214. ]];
  215. foreach($list as $item){
  216. $r = [
  217. $item['id'],
  218. $item['order_no'],
  219. $item['supplier_name'],
  220. $item['goods_name'],
  221. $item['attr_names'],
  222. $item['num'],
  223. $item['goods_price'],
  224. $item['created_at'],
  225. ];
  226. $rows[] = $r;
  227. }
  228. $writer = \Spatie\SimpleExcel\SimpleExcelWriter::streamDownload(time() . '.xlsx')->noHeaderRow()
  229. ->addRows($rows)->toBrowser();
  230. }
  231. public function save ()
  232. {
  233. $t = \Yii::$app->db->beginTransaction();
  234. try {
  235. $model = new ErpPurchaseoutOrder();
  236. $model->operator = get_admin()->name;
  237. $model->operator_id = get_admin()->id;
  238. $model->store_id = $this->store_id;
  239. $model->order_no = OrderNo::getOrderNo(OrderNo::ERP_PURCHASE_OUT);
  240. $model->store_id = $this->store_id;
  241. $model->supplier_id = $this->supplier_id;
  242. $model->remark = (string)$this->remark;
  243. $model->num = count($this->purchase);
  244. if (!$model->save()) {
  245. \Yii::error([__METHOD__, $model->attributes]);
  246. throw new \Exception(array_shift($model->getFirstErrors()));
  247. }
  248. ErpPurchaseout::saveList($model, $this->purchase);
  249. $t->commit();
  250. return [
  251. 'code' => 0,
  252. 'msg' => '操作成功!'
  253. ];
  254. } catch (\Exception $e) {
  255. \Yii::error($e);
  256. $t->rollBack();
  257. return [
  258. 'code' => 1,
  259. 'msg' => $e->getMessage()
  260. ];
  261. }
  262. }
  263. public function delSave ($eip_id = 0)
  264. {
  265. $t = \Yii::$app->db->beginTransaction();
  266. try {
  267. $eip = \app\models\ErpInventoryProp::findOne($eip_id);
  268. if($eip->num == 0){
  269. $t->commit();
  270. return [
  271. 'code' => 0,
  272. 'msg' => '操作成功!'
  273. ];
  274. }
  275. $ei = ErpInventory::findOne($eip->inventory_id);
  276. $this->store_id = $ei->store_id;
  277. $this->supplier_id = $ei->supplier_id;
  278. $this->remark = '**删除';
  279. $this->purchase = [[
  280. 'eip_id' => $eip->id,
  281. 'id' => $eip->inventory_id,
  282. 'num' => $eip->num,
  283. ]];
  284. $save = $this->save();
  285. if ($save['code']) {
  286. \Yii::error([__METHOD__, $this->purchase, $save]);
  287. throw new \Exception($save['msg']);
  288. }
  289. $t->commit();
  290. return [
  291. 'code' => 0,
  292. 'msg' => '操作成功!'
  293. ];
  294. } catch (\Exception $e) {
  295. \Yii::error($e);
  296. $t->rollBack();
  297. return [
  298. 'code' => 1,
  299. 'msg' => $e->getMessage()
  300. ];
  301. }
  302. }
  303. public function statusPrint ()
  304. {
  305. $t = \Yii::$app->db->beginTransaction();
  306. try {
  307. $model = ErpPurchaseoutOrder::findOne($this->id);
  308. $model->status = 1;
  309. if (!$model->save()) {
  310. \Yii::error([__METHOD__, $model->attributes]);
  311. throw new \Exception(array_shift($model->getFirstErrors()));
  312. }
  313. ErpPurchaseout::saveList($model->id, $this->purchase);
  314. $t->commit();
  315. return [
  316. 'code' => 0,
  317. 'msg' => '操作成功!'
  318. ];
  319. } catch (\Exception $e) {
  320. \Yii::error($e);
  321. $t->rollBack();
  322. return [
  323. 'code' => 1,
  324. 'msg' => $e->getMessage()
  325. ];
  326. }
  327. }
  328. }