PurchaseoutForm.php 12 KB

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