tp6框架的销售统计系统

Order.php 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. namespace app\controller;
  3. use think\App;
  4. use think\facade\View;
  5. use app\model\Order as OrderModel;
  6. use app\model\OrderItem as OrderItemModel;
  7. use app\model\OrderDetails as OrderDetailsModel;
  8. use app\model\Department as DepartmentModel;
  9. use think\facade\Db;
  10. class Order extends Base
  11. {
  12. protected $model;
  13. protected $itemModel;
  14. protected $detailsModel;
  15. public function __construct(App $app)
  16. {
  17. parent::__construct($app);
  18. $this->model = new OrderModel();
  19. $this->itemModel = new OrderItemModel();
  20. $this->detailsModel = new OrderDetailsModel();
  21. }
  22. public function index()
  23. {
  24. return View::fetch();
  25. }
  26. public function lists($year=0, $month=0, $key="")
  27. {
  28. $map = [];
  29. if ($year) {
  30. $map[] = ['year','=', $year];
  31. }
  32. if ($key) {
  33. $map[] = ['company_name','LIKE', '%'.$key.'%'];
  34. }
  35. if ($month) {
  36. $map[] = ['month','=', $month];
  37. }
  38. $limit = $this->request->cookie('limit') ? : 50;
  39. $list = $this->detailsModel->where($map)->order('id')->paginate([
  40. 'list_rows' => $limit,
  41. 'query' => $this->request->param()
  42. ]);
  43. // 统计去除已取消合同
  44. $map[] = ['status', '<>', -1];
  45. $total_contract_money = $this->detailsModel->where($map)->sum('contract_money');
  46. $total_payment = $this->detailsModel->where($map)->sum('payment');
  47. $page = $list->render();
  48. return View::fetch('',[
  49. 'year' => $year,
  50. 'list' => $list,
  51. 'page' => $page,
  52. 'key' => $key,
  53. 'month' => $month,
  54. 'limit' => $limit,
  55. 'total_contract_money' => round($total_contract_money, 2),
  56. 'total_payment' => round($total_payment, 2)
  57. ]);
  58. }
  59. public function save($year=0)
  60. {
  61. if ($this->request->isPost()) {
  62. $param = $this->request->param();
  63. // var_dump($param);
  64. // exit;
  65. try {
  66. if ($param['id']) {
  67. $order = $this->model->find(intval($param['id']));
  68. } else {
  69. $order = new OrderModel();
  70. $order->create_time = time();
  71. }
  72. $order->no = $param['no'];
  73. $order->contract_no = $param['contract_no'];
  74. $order->contract_time = $param['contract_time'];
  75. $order->company_name = $param['company_name'];
  76. $order->department_name = $param['department_name'];
  77. $order->seller_name = $param['seller_name'];
  78. $order->consignor = $param['consignor'];
  79. $order->fare = $param['fare'];
  80. $order->agency = $param['agency'];
  81. $order->remark = $param['remark'];
  82. $order->save();
  83. } catch (\Exception $e) {
  84. $this->error($e->getMessage());
  85. }
  86. $this->success('操作成功', 'seller/index');
  87. } else {
  88. $id = $this->request->has('id','route') ? intval($this->request->param('id')) : 0;
  89. if ($id) {
  90. $data = $this->model->find($id);
  91. } else {
  92. $data = [
  93. 'id' => '0',
  94. 'no' => '',
  95. 'contract_no' => '',
  96. 'contract_time' => '',
  97. 'company_name' => '',
  98. 'department_name' => '',
  99. 'seller_name' => '',
  100. 'consignor' => '',
  101. 'fare' => '0.00',
  102. 'agency' => '0.00',
  103. 'remark' => '',
  104. ];
  105. }
  106. return View::fetch('',[
  107. 'data' => $data,
  108. 'year' => $year
  109. ]);
  110. }
  111. }
  112. public function delete($id)
  113. {
  114. if ($this->model->destroy($id)) {
  115. return json(['code'=>2]);
  116. } else {
  117. return json(['code'=>0]);
  118. }
  119. }
  120. public function item($order_id)
  121. {
  122. $list = $this->itemModel->where('order_id', $order_id)->select();
  123. $orderInfo = $this->model->field('id,no,contract_no,company_name,contract_time')->find($order_id);
  124. $year = date('Y', strtotime($orderInfo->contract_time));
  125. return View::fetch('',[
  126. 'list' => $list,
  127. 'order_id' => $order_id,
  128. 'orderInfo' => $orderInfo,
  129. 'year' => $year,
  130. ]);
  131. }
  132. public function itemInfo($id)
  133. {
  134. $data = $this->itemModel->find($id);
  135. if ($this->request->isAjax()) {
  136. return json(['code'=>2, 'data'=>$data]);
  137. }
  138. }
  139. public function itemSave()
  140. {
  141. if ($this->request->isAjax()) {
  142. $param = $this->request->param();
  143. // var_dump($param);
  144. // exit;
  145. try {
  146. if ($param['id']) {
  147. $order = $this->itemModel->find(intval($param['id']));
  148. } else {
  149. $order = new OrderItemModel();
  150. }
  151. $order->order_id = intval($param['order_id']);
  152. $order->goods_category = $param['goods_category'];
  153. $order->goods_name = $param['goods_name'];
  154. $order->goods_price = $param['goods_price'];
  155. $order->sales_amount = $param['sales_amount'];
  156. $order->sales_price = $param['sales_price'];
  157. $order->actual_amount = $param['actual_amount'];
  158. $order->actual_price = $param['actual_price'];
  159. $order->agency = $param['agency'];
  160. $order->fare = $param['fare'];
  161. $order->type = intval($param['type']);
  162. $order->remark = $param['remark'];
  163. $order->save();
  164. } catch (\Exception $e) {
  165. // $this->error($e->getMessage());
  166. return json(['code'=>0, 'msg'=>$e->getMessage()]);
  167. }
  168. return json(['code'=>2]);
  169. }
  170. }
  171. public function itemDelete($id)
  172. {
  173. if ($this->request->isPost()) {
  174. if ($this->itemModel->destroy($id)) {
  175. return json(['code'=>2]);
  176. } else {
  177. return json(['code'=>0]);
  178. }
  179. }
  180. }
  181. }