SupplierForm.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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\models\ErpSupplier;
  9. use app\models\District;
  10. use app\models\ErpInventory;
  11. class SupplierForm extends Model
  12. {
  13. public $store_id;
  14. public $id;
  15. public $ids;
  16. public $name;
  17. public $contact;
  18. public $phone;
  19. public $province_id;
  20. public $city_id;
  21. public $district_id;
  22. public $address;
  23. public $logo;
  24. public $remark;
  25. public $begin_time;
  26. public $end_time;
  27. public $is_delete;
  28. public $key;
  29. public $val;
  30. public $warning;
  31. public function rules()
  32. {
  33. return [
  34. [['id', 'store_id', 'is_delete', 'province_id', 'city_id', 'district_id'], 'integer'],
  35. [['ids', 'name', 'address', 'phone', 'contact', 'logo', 'remark'], 'string'],
  36. [['key', 'val', 'end_time', 'begin_time', 'warning'], 'safe'],
  37. ];
  38. }
  39. public function init() {
  40. parent::init();
  41. if(empty($this->store_id)){
  42. $this->store_id = get_store_id();
  43. }
  44. }
  45. public function selectList ()
  46. {
  47. try {
  48. $is_delete = 0;
  49. $query = ErpSupplier::find()->where(['is_delete' => $is_delete, 'store_id' => $this->store_id]);
  50. if($this->warning){
  51. $warning_num = ERP::getWarningNum($this->store_id);
  52. $query->andWhere(['id' => ErpInventory::find()->where(['<', 'num', $warning_num])->andWhere(['is_delete' => 0])->distinct(true)->select('supplier_id')]);
  53. }
  54. $query->orderBy('id DESC');
  55. $query->select('id, name');
  56. $data = $query->asArray()->all();
  57. return [
  58. 'code' => 0,
  59. 'msg' => 'success',
  60. 'data' => $data,
  61. // 'q' => $query->createCommand()->getRawSql(),
  62. ];
  63. } catch (\Exception $e) {
  64. \Yii::error($e);
  65. return [
  66. 'code' => 1,
  67. 'msg' => $e->getMessage()
  68. ];
  69. }
  70. }
  71. public function search ()
  72. {
  73. try {
  74. $is_delete = 0;
  75. if ($this->is_delete == 1) {
  76. $is_delete = 1;
  77. }
  78. $query = ErpSupplier::find()->where(['is_delete' => $is_delete, 'store_id' => $this->store_id]);
  79. if (!empty($this->id)) {
  80. $query->andWhere(['id' => $this->id]);
  81. }
  82. if (!empty($this->supplier_id)) {
  83. $query->andWhere(['id' => $this->supplier_id]);
  84. }
  85. if (!empty($this->name)) {
  86. $query->andWhere(['like', 'name', $this->name]);
  87. }
  88. if (!empty($this->contact)) {
  89. $query->andWhere(['like', 'contact', $this->contact]);
  90. }
  91. if (!empty($this->phone)) {
  92. $query->andWhere(['like', 'phone', $this->phone]);
  93. }
  94. if (!empty($this->begin_time)) {
  95. $query->andWhere(['>=', 'created_at', strtotime($this->begin_time)]);
  96. }
  97. if (!empty($this->end_time)) {
  98. $query->andWhere(['<=', 'created_at', strtotime($this->end_time)]);
  99. }
  100. $query->orderBy('id DESC');
  101. $pagination = pagination_make($query);
  102. foreach ($pagination['list'] as &$item) {
  103. $item['created_at'] = date("Y-m-d H:i:s", $item['created_at']);
  104. $item['district'] = implode('', District::find()->where(['id' => [$item['province_id'], $item['city_id'], $item['district_id']]])->select('name')->column());
  105. }
  106. return [
  107. 'code' => 0,
  108. 'msg' => 'success',
  109. 'data' => $pagination,
  110. // 'q' => $query->createCommand()->getRawSql(),
  111. ];
  112. } catch (\Exception $e) {
  113. \Yii::error($e);
  114. return [
  115. 'code' => 1,
  116. 'msg' => $e->getMessage()
  117. ];
  118. }
  119. }
  120. public function getInfo()
  121. {
  122. try {
  123. $model = ErpSupplier::find()->where(['id' => $this->id, 'store_id' => $this->store_id])->one();
  124. return [
  125. 'code' => 0,
  126. 'msg' => '获取成功',
  127. 'data' => $model ?: [],
  128. ];
  129. } catch (\Exception $e) {
  130. \Yii::error($e);
  131. return [
  132. 'code' => 1,
  133. 'msg' => $e->getMessage() . $e->getFile() . $e->getLine()
  134. ];
  135. }
  136. }
  137. public function save ()
  138. {
  139. $t = \Yii::$app->db->beginTransaction();
  140. try {
  141. $model = ErpSupplier::findOne(['id' => $this->id, 'store_id' => $this->store_id]);
  142. if (empty($model)) {
  143. $model = new ErpSupplier();
  144. $model->store_id = $this->store_id;
  145. }
  146. $model->name = $this->name;
  147. $model->contact = $this->contact;
  148. $model->logo = $this->logo ?? '';
  149. $model->phone = $this->phone;
  150. $model->province_id = (int)$this->province_id;
  151. $model->city_id = (int)$this->city_id;
  152. $model->district_id = (int)$this->district_id;
  153. $model->address = $this->address;
  154. $model->remark = $this->remark;
  155. if (!$model->save()) {
  156. \Yii::error([__METHOD__, $model->attributes]);
  157. throw new \Exception(array_shift($model->getFirstErrors()));
  158. }
  159. $t->commit();
  160. $province = District::findOne((int)$this->province_id)->name ?? '';
  161. $city = District::findOne((int)$this->city_id)->name ?? '';
  162. $district = District::findOne((int)$this->district_id)->name ?? '';
  163. return [
  164. 'code' => 0,
  165. 'msg' => '操作成功!',
  166. 'data' => [
  167. 'district' => $province . $city . $district
  168. ]
  169. ];
  170. } catch (\Exception $e) {
  171. \Yii::error($e);
  172. $t->rollBack();
  173. return [
  174. 'code' => 1,
  175. 'msg' => $e->getMessage()
  176. ];
  177. }
  178. }
  179. public function saveKey ()
  180. {
  181. $t = \Yii::$app->db->beginTransaction();
  182. try {
  183. $model = ErpSupplier::findOne(['id' => $this->id, 'store_id' => $this->store_id]);
  184. if (empty($model)) {
  185. throw new \Exception('参数错误');
  186. }
  187. $key = $this->key;
  188. $model->$key = $this->val;
  189. if (!$model->save()) {
  190. \Yii::error([__METHOD__, $model->attributes]);
  191. throw new \Exception(array_shift($model->getFirstErrors()));
  192. }
  193. $t->commit();
  194. return [
  195. 'code' => 0,
  196. 'msg' => '操作成功!'
  197. ];
  198. } catch (\Exception $e) {
  199. \Yii::error($e);
  200. $t->rollBack();
  201. return [
  202. 'code' => 1,
  203. 'msg' => $e->getMessage()
  204. ];
  205. }
  206. }
  207. public function del ()
  208. {
  209. try {
  210. if ($this->ids) {
  211. $ids = explode(',', $this->ids);
  212. ErpSupplier::updateAll(['is_delete' => 1, 'deleted_at' => time()], ['and', ['in', 'id', $ids], ['store_id' => $this->store_id]]);
  213. }
  214. return [
  215. 'code' => 0,
  216. 'msg' => '操作成功!'
  217. ];
  218. } catch (\Exception $e) {
  219. \Yii::error($e);
  220. return [
  221. 'code' => 1,
  222. 'msg' => $e->getMessage()
  223. ];
  224. }
  225. }
  226. }