PurchaseinForm.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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\Admin;
  9. use app\models\AgentFrontBind;
  10. use app\models\AgentFrontErpCloudGoods;
  11. use app\models\Supplier;
  12. use app\utils\OrderNo;
  13. use app\models\AgentFrontErpInventory;
  14. use app\models\AgentFrontErpInventoryLog;
  15. use app\models\AgentFrontErpSupplier;
  16. use app\models\AgentFrontErpPurchasein;
  17. use app\models\AgentFrontErpPurchaseinOrder;
  18. use app\models\Goods;
  19. use app\models\Option;
  20. use app\constants\OptionSetting;
  21. use app\models\Store;
  22. use app\models\District;
  23. class PurchaseinForm extends Model
  24. {
  25. public $export;
  26. public $store_id;
  27. public $id;
  28. public $ids;
  29. public $goods_name;
  30. public $supplier_name;
  31. public $goods_id;
  32. public $supplier_id;
  33. public $is_delete;
  34. public $status;
  35. public $purchase = [];
  36. public $operator;
  37. public $operator_id;
  38. public $order_no;
  39. public $front_agent_admin_id;
  40. public function rules()
  41. {
  42. return [
  43. [['id', 'store_id', 'is_delete', 'status', 'export', 'front_agent_admin_id'], 'integer'],
  44. [['ids', 'goods_name', 'supplier_name'], 'string'],
  45. [['is_delete', 'goods_id', 'supplier_id', 'purchase', 'operator', 'operator_id', 'order_no'], 'safe'],
  46. ];
  47. }
  48. public function init() {
  49. parent::init();
  50. if(empty($this->store_id)){
  51. $this->store_id = get_store_id();
  52. }
  53. }
  54. public function search ()
  55. {
  56. try {
  57. $query = AgentFrontErpPurchaseinOrder::find()->alias('epo')->where(['epo.is_delete' => 0, 'epo.front_agent_admin_id' => $this->front_agent_admin_id]);
  58. $query->leftJoin(['es' => Supplier::tableName()], 'es.id = epo.supplier_id');
  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 = AgentFrontErpPurchasein::find()->alias('ep')
  70. ->leftJoin(['ei' => AgentFrontErpInventory::tableName()], 'ei.id = ep.inventory_id')
  71. ->leftJoin(['g' => AgentFrontErpCloudGoods::tableName()], 'ei.goods_id = g.cloud_goods_id')
  72. ->where(['like', 'g.goods_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 = AgentFrontErpPurchasein::find()->alias('ep')
  79. ->leftJoin(['ei' => AgentFrontErpInventory::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(['like', 'es.supplier_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 = AgentFrontErpPurchaseinOrder::find()->where(['id' => $this->id, 'front_agent_admin_id' => $this->front_agent_admin_id])->one();
  121. $epo['created_at'] = date("Y-m-d H:i:s", $epo['created_at']);
  122. $supplier_name = Supplier::findOne($epo['supplier_id'])['supplier_name'];
  123. $ep = AgentFrontErpPurchasein::find()->alias('ep')
  124. ->leftJoin(['ei' => AgentFrontErpInventory::tableName()], 'ei.id = ep.inventory_id')
  125. ->leftJoin(['g' => AgentFrontErpCloudGoods::tableName()], 'ei.goods_id = g.cloud_goods_id')
  126. ->where(['ep.purchase_order_id' => $this->id])
  127. ->select('ep.id, ep.num, g.cover_pic, g.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. $item['goods_price'] = $attr_info['price'];
  134. $totalPrice += $item['goods_price'] * $item['num'];
  135. }
  136. $admin = Admin::findOne($this->front_agent_admin_id);
  137. $district = implode('', District::find()->where(['id' => [$admin->province_id ?? 0, $admin->city_id ?? 0, $admin->district_id ?? 0]])->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' => $admin->name,
  148. 'contact_tel' => $admin->mobile,
  149. 'address' => $admin->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 = AgentFrontErpPurchasein::find()->alias('ep')
  167. ->leftJoin(['epo' => AgentFrontErpPurchaseinOrder::tableName()], 'ep.purchase_order_id = epo.id')
  168. ->leftJoin(['ei' => AgentFrontErpInventory::tableName()], 'ei.id = ep.inventory_id')
  169. ->leftJoin(['g' => AgentFrontErpCloudGoods::tableName()], 'ei.goods_id = g.cloud_goods_id')
  170. ->where(['epo.is_delete' => 0, 'epo.front_agent_admin_id' => $this->front_agent_admin_id]);
  171. $query->leftJoin(['es' => Supplier::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.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. $supplier = Supplier::findOne(['id' => $this->supplier_id, 'is_delete' => 0]);
  236. if (!$supplier) {
  237. throw new \Exception('供货商不存在');
  238. }
  239. if (!in_array($supplier->cloud_supplier_id, AgentFrontBind::getAgentFrontBindSupplierId($this->front_agent_admin_id) ?? [])) {
  240. throw new \Exception('当前供货商商品未与仓库关联');
  241. }
  242. $model = new AgentFrontErpPurchaseinOrder();
  243. $model->operator = get_admin()->name;
  244. $model->operator_id = get_admin()->id;
  245. $model->front_agent_admin_id = $this->front_agent_admin_id;
  246. $model->order_no = OrderNo::getOrderNo(OrderNo::ERP_PURCHASE_IN);
  247. $model->supplier_id = $this->supplier_id;
  248. $model->num = count($this->purchase);
  249. if (!$model->save()) {
  250. \Yii::error([__METHOD__, $model->attributes]);
  251. throw new \Exception(array_shift($model->getFirstErrors()));
  252. }
  253. AgentFrontErpPurchasein::saveList($model, $this->purchase);
  254. $t->commit();
  255. return [
  256. 'code' => 0,
  257. 'msg' => '操作成功!'
  258. ];
  259. } catch (\Exception $e) {
  260. \Yii::error($e);
  261. $t->rollBack();
  262. return [
  263. 'code' => 1,
  264. 'msg' => $e->getMessage()
  265. ];
  266. }
  267. }
  268. public function statusPrint ()
  269. {
  270. $t = \Yii::$app->db->beginTransaction();
  271. try {
  272. $model = AgentFrontErpPurchaseinOrder::findOne($this->id);
  273. $model->status = 1;
  274. if (!$model->save()) {
  275. \Yii::error([__METHOD__, $model->attributes]);
  276. throw new \Exception(array_shift($model->getFirstErrors()));
  277. }
  278. AgentFrontErpPurchasein::saveList($model->id, $this->purchase);
  279. $t->commit();
  280. return [
  281. 'code' => 0,
  282. 'msg' => '操作成功!'
  283. ];
  284. } catch (\Exception $e) {
  285. \Yii::error($e);
  286. $t->rollBack();
  287. return [
  288. 'code' => 1,
  289. 'msg' => $e->getMessage()
  290. ];
  291. }
  292. }
  293. }