WaterOrder.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/11/11
  6. */
  7. namespace app\admin\controller\water;
  8. use app\admin\controller\AuthController;
  9. use app\admin\model\user\User;
  10. use app\admin\model\water\WaterMaterial;
  11. use crmeb\services\{ExpressService,
  12. JsonService,
  13. JsonService as Json,
  14. MiniProgramService,
  15. PHPExcelService,
  16. WechatService,
  17. FormBuilder as Form,
  18. CacheService,
  19. UtilService as Util};
  20. use think\facade\Route as Url;
  21. use think\facade\Validate;
  22. Use app\admin\model\water\WaterOrder as model;
  23. /**
  24. * 订单管理控制器 同一个订单表放在一个控制器
  25. * Class StoreOrder
  26. * @package app\admin\controller\store
  27. */
  28. class WaterOrder extends AuthController
  29. {
  30. /**
  31. * @return mixed
  32. */
  33. public function index()
  34. {
  35. return $this->fetch();
  36. }
  37. public function list()
  38. {
  39. $where = Util::getMore([
  40. ['page', 1],
  41. ['limit', 20],
  42. ['name', ''],
  43. ['card', ''],
  44. ['order_id', ''],
  45. ]);
  46. return Json::successlayui(model::list($where));
  47. }
  48. /**
  49. * 显示创建资源表单页.
  50. *
  51. * @return \think\Response
  52. */
  53. public function create($id = 0)
  54. {
  55. $f = [];
  56. $f[] = Form::input('name', '名称')->col(12);
  57. $f[] = Form::select('type', '选择分类', '')->options([
  58. ['value' => 0, 'label' => '请选择分类'],
  59. ['value' => 1, 'label' => '顶板'],
  60. ['value' => 2, 'label' => '侧板'],
  61. ['value' => 3, 'label' => '拉筋'],
  62. ['value' => 4, 'label' => '底板'],
  63. ])->filterable(true);
  64. $f[] = Form::select('th_id', '选择厚度', '')->options(
  65. array_merge([['value' => 0, 'label' => '请选择分类']], \app\admin\model\water\WaterThickness::order('id DESC')->field('id as value,name as label')->select()->toArray())
  66. )->filterable(true);
  67. $f[] = Form::input('weight', '重量(kg)');
  68. $f[] = Form::input('unit_price', '单价');
  69. $f[] = Form::input('company', '单位');
  70. $f[] = Form::input('long', '长');
  71. $f[] = Form::input('wide', '宽');
  72. $f[] = Form::radio('is_gc', '公差', 0)->options([['value' => 0, 'label' => '正常'], ['value' => 1, 'label' => '大公差']]);
  73. $form = Form::make_post_form('添加', $f, Url::buildUrl('save'));
  74. $this->assign(compact('form'));
  75. return $this->fetch('public/form-builder');
  76. }
  77. public function save()
  78. {
  79. $model = new model;
  80. $data = Util::postMore([
  81. 'name',
  82. 'type',
  83. 'th_id',
  84. 'weight',
  85. 'unit_price',
  86. 'company',
  87. 'long',
  88. 'wide',
  89. ]);
  90. $validate = Validate::rule('name', 'require')->rule([
  91. 'name' => 'require',
  92. 'type' => 'require',
  93. 'th_id' => 'require',
  94. 'weight' => 'require',
  95. 'unit_price' => 'require',
  96. 'company' => 'require',
  97. 'long' => 'require',
  98. 'wide' => 'require',
  99. ]);
  100. $validate->message([
  101. 'name.require' => '名称不能为空',
  102. 'type.require' => '请选择分类',
  103. 'th_id.require' => '请选择厚度',
  104. 'weight.require' => '请填写重量',
  105. 'unit_price.require' => '请填写单价',
  106. 'company.require' => '请填写单位',
  107. 'long.require' => '请填写长度',
  108. 'wide.require' => '请填写宽度',
  109. ]);
  110. if (!$validate->check($data)) {
  111. return Json::fail($validate->getError());
  112. }
  113. $res = $model->save($data);
  114. if ($res) return Json::successful('添加成功');
  115. return Json::fail('添加失败');
  116. }
  117. /**
  118. * 显示创建资源表单页.
  119. *
  120. * @return \think\Response
  121. */
  122. public function edit($id = 0)
  123. {
  124. $data = model::find($id);
  125. $f = [];
  126. $f[] = Form::input('name', '名称', $data['name'])->col(12);
  127. $f[] = Form::select('type', '选择分类', (string)$data['type'])->options([
  128. ['value' => 0, 'label' => '请选择分类'],
  129. ['value' => 1, 'label' => '顶板'],
  130. ['value' => 2, 'label' => '侧板'],
  131. ['value' => 3, 'label' => '拉筋'],
  132. ['value' => 4, 'label' => '底板'],
  133. ])->filterable(true);
  134. $f[] = Form::select('th_id', '选择厚度', (string)$data['th_id'])->options(
  135. array_merge([['value' => 0, 'label' => '请选择分类']], \app\admin\model\water\WaterThickness::order('id DESC')->field('id as value,name as label')->select()->toArray())
  136. )->filterable(true);
  137. $f[] = Form::input('weight', '重量(kg)', $data['weight']);
  138. $f[] = Form::input('unit_price', '单价', $data['unit_price']);
  139. $f[] = Form::input('company', '单位', $data['company']);
  140. $f[] = Form::input('long', '长', $data['long']);
  141. $f[] = Form::input('wide', '宽', $data['wide']);
  142. $f[] = Form::hidden('id', $id);
  143. $form = Form::make_post_form('修改', $f, Url::buildUrl('update'));
  144. $this->assign(compact('form'));
  145. return $this->fetch('public/form-builder');
  146. }
  147. /**
  148. * 修改
  149. * @return void
  150. * @throws \think\db\exception\DataNotFoundException
  151. * @throws \think\db\exception\DbException
  152. * @throws \think\db\exception\ModelNotFoundException
  153. */
  154. public function update()
  155. {
  156. $model = new model;
  157. $data = Util::postMore([
  158. 'name',
  159. 'type',
  160. 'th_id',
  161. 'weight',
  162. 'unit_price',
  163. 'company',
  164. 'long',
  165. 'wide',
  166. 'id',
  167. ]);
  168. $validate = Validate::rule('name', 'require')->rule([
  169. 'name' => 'require',
  170. 'type' => 'require',
  171. 'th_id' => 'require',
  172. 'weight' => 'require',
  173. 'unit_price' => 'require',
  174. 'company' => 'require',
  175. 'long' => 'require',
  176. 'wide' => 'require',
  177. ]);
  178. $validate->message([
  179. 'name.require' => '名称不能为空',
  180. 'type.require' => '请选择分类',
  181. 'th_id.require' => '请选择厚度',
  182. 'weight.require' => '请填写重量',
  183. 'unit_price.require' => '请填写单价',
  184. 'company.require' => '请填写单位',
  185. 'long.require' => '请填写长度',
  186. 'wide.require' => '请填写宽度',
  187. ]);
  188. $details = $model->find($data['id']);
  189. $details['name'] = $data['name'];
  190. $details['thickness'] = $data['thickness'];
  191. $res = $details->save();
  192. if ($res) return Json::successful('修改成功');
  193. return Json::fail('修改失败');
  194. }
  195. /**
  196. * 删除
  197. * @param $id
  198. * @return void
  199. * @throws \Exception
  200. */
  201. public function delete($id)
  202. {
  203. if (!$id) Json::fail('删除失败');
  204. $model = new model;
  205. $res = model::destroy($id);
  206. if ($res){
  207. return Json::success('删除成功!');
  208. }else{
  209. return Json::fail($model->getErrorInfo());
  210. }
  211. }
  212. public function excel($id)
  213. {
  214. $details = model::where('id', $id)->find();
  215. $list = WaterMaterial::where('query_id', $id)->select();
  216. $user = User::where('uid', $details['uid'])->find();
  217. $export = [];
  218. foreach ($list as $item)
  219. {
  220. $export[] = [
  221. $item['title'],
  222. $item['name'],
  223. $item['specifications'],
  224. $item['number'],
  225. $item['unit_price'],
  226. $item['ot_price'],
  227. $item['company'],
  228. $item['create_time'],
  229. ];
  230. }
  231. PHPExcelService::setExcelHeader(['标题', '名称', '规格', '数量', '单价', '总价', '单位','时间'])
  232. ->setExcelTile('用户'.$user['phone'].'计算订单导出-总价'.$details['price'], '计算订单' . time(), '记录ID'.$details['id'].'-水口价格'.$details['water_mouth'].'-隔板价格'.$details['partition'].'-税点'.($details['tax_point']).'%')
  233. ->setExcelContent($export)
  234. ->ExcelSave();
  235. }
  236. }