SupplierForm.php 8.3 KB

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