Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 1 | # ERPNext - web based ERP (http://erpnext.com) |
| 2 | # Copyright (C) 2012 Web Notes Technologies Pvt Ltd |
| 3 | # |
| 4 | # This program is free software: you can redistribute it and/or modify |
| 5 | # it under the terms of the GNU General Public License as published by |
| 6 | # the Free Software Foundation, either version 3 of the License, or |
| 7 | # (at your option) any later version. |
| 8 | # |
| 9 | # This program is distributed in the hope that it will be useful, |
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | # GNU General Public License for more details. |
| 13 | # |
| 14 | # You should have received a copy of the GNU General Public License |
| 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | |
| 17 | import webnotes |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 18 | from webnotes import msgprint |
Nabin Hait | 26d4655 | 2013-01-09 15:23:05 +0530 | [diff] [blame] | 19 | from webnotes.utils import cint, flt, cstr |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 20 | from stock.utils import get_valuation_method |
Nabin Hait | 26d4655 | 2013-01-09 15:23:05 +0530 | [diff] [blame] | 21 | import json |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 22 | |
| 23 | # future reposting |
| 24 | |
| 25 | _exceptions = [] |
| 26 | def update_entries_after(args, verbose=1): |
| 27 | """ |
| 28 | update valution rate and qty after transaction |
| 29 | from the current time-bucket onwards |
| 30 | |
| 31 | args = { |
| 32 | "item_code": "ABC", |
| 33 | "warehouse": "XYZ", |
| 34 | "posting_date": "2012-12-12", |
| 35 | "posting_time": "12:00" |
| 36 | } |
| 37 | """ |
Nabin Hait | f4591ec | 2013-05-23 19:07:10 +0530 | [diff] [blame] | 38 | global _exceptions |
| 39 | _exceptions = [] |
| 40 | |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 41 | previous_sle = get_sle_before_datetime(args) |
Anand Doshi | 71bed31 | 2013-03-13 12:57:04 +0530 | [diff] [blame] | 42 | |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 43 | qty_after_transaction = flt(previous_sle.get("qty_after_transaction")) |
| 44 | valuation_rate = flt(previous_sle.get("valuation_rate")) |
| 45 | stock_queue = json.loads(previous_sle.get("stock_queue") or "[]") |
Nabin Hait | 9514d17 | 2013-01-10 10:40:37 +0530 | [diff] [blame] | 46 | stock_value = 0.0 |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 47 | |
| 48 | entries_to_fix = get_sle_after_datetime(previous_sle or \ |
Anand Doshi | 4dc7caa | 2013-01-11 11:44:49 +0530 | [diff] [blame] | 49 | {"item_code": args["item_code"], "warehouse": args["warehouse"]}, for_update=True) |
Anand Doshi | 71bed31 | 2013-03-13 12:57:04 +0530 | [diff] [blame] | 50 | |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 51 | valuation_method = get_valuation_method(args["item_code"]) |
| 52 | |
| 53 | for sle in entries_to_fix: |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 54 | if sle.serial_no or not cint(webnotes.conn.get_default("allow_negative_stock")): |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 55 | # validate negative stock for serialized items, fifo valuation |
| 56 | # or when negative stock is not allowed for moving average |
| 57 | if not validate_negative_stock(qty_after_transaction, sle): |
| 58 | qty_after_transaction += flt(sle.actual_qty) |
| 59 | continue |
Nabin Hait | 1b4f56c | 2013-01-17 17:01:51 +0530 | [diff] [blame] | 60 | |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 61 | if sle.serial_no: |
Nabin Hait | 1b4f56c | 2013-01-17 17:01:51 +0530 | [diff] [blame] | 62 | valuation_rate = get_serialized_values(qty_after_transaction, sle, valuation_rate) |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 63 | elif valuation_method == "Moving Average": |
Nabin Hait | 1b4f56c | 2013-01-17 17:01:51 +0530 | [diff] [blame] | 64 | valuation_rate = get_moving_average_values(qty_after_transaction, sle, valuation_rate) |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 65 | else: |
Nabin Hait | 1b4f56c | 2013-01-17 17:01:51 +0530 | [diff] [blame] | 66 | valuation_rate = get_fifo_values(qty_after_transaction, sle, stock_queue) |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 67 | |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 68 | qty_after_transaction += flt(sle.actual_qty) |
| 69 | |
| 70 | # get stock value |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 71 | if sle.serial_no: |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 72 | stock_value = qty_after_transaction * valuation_rate |
| 73 | elif valuation_method == "Moving Average": |
| 74 | stock_value = (qty_after_transaction > 0) and \ |
| 75 | (qty_after_transaction * valuation_rate) or 0 |
| 76 | else: |
| 77 | stock_value = sum((flt(batch[0]) * flt(batch[1]) for batch in stock_queue)) |
Nabin Hait | c3afb25 | 2013-03-19 12:01:24 +0530 | [diff] [blame] | 78 | # print sle.posting_date, sle.actual_qty, sle.incoming_rate, stock_queue, stock_value |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 79 | # update current sle |
| 80 | webnotes.conn.sql("""update `tabStock Ledger Entry` |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 81 | set qty_after_transaction=%s, valuation_rate=%s, stock_queue=%s, |
Nabin Hait | 914c6df | 2013-01-14 13:15:42 +0530 | [diff] [blame] | 82 | stock_value=%s where name=%s""", |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 83 | (qty_after_transaction, valuation_rate, |
Nabin Hait | 914c6df | 2013-01-14 13:15:42 +0530 | [diff] [blame] | 84 | json.dumps(stock_queue), stock_value, sle.name)) |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 85 | |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 86 | if _exceptions: |
Nabin Hait | 9514d17 | 2013-01-10 10:40:37 +0530 | [diff] [blame] | 87 | _raise_exceptions(args, verbose) |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 88 | |
| 89 | # update bin |
Nabin Hait | ac53b11 | 2013-01-11 19:25:46 +0530 | [diff] [blame] | 90 | if not webnotes.conn.exists({"doctype": "Bin", "item_code": args["item_code"], |
| 91 | "warehouse": args["warehouse"]}): |
Rushabh Mehta | c53231a | 2013-02-18 18:24:28 +0530 | [diff] [blame] | 92 | bin_wrapper = webnotes.bean([{ |
Nabin Hait | ac53b11 | 2013-01-11 19:25:46 +0530 | [diff] [blame] | 93 | "doctype": "Bin", |
| 94 | "item_code": args["item_code"], |
| 95 | "warehouse": args["warehouse"], |
Anand Doshi | c313d66 | 2013-01-14 15:46:17 +0530 | [diff] [blame] | 96 | }]) |
| 97 | bin_wrapper.ignore_permissions = 1 |
| 98 | bin_wrapper.insert() |
Nabin Hait | ac53b11 | 2013-01-11 19:25:46 +0530 | [diff] [blame] | 99 | |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 100 | webnotes.conn.sql("""update `tabBin` set valuation_rate=%s, actual_qty=%s, |
| 101 | stock_value=%s, |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 102 | projected_qty = (actual_qty + indented_qty + ordered_qty + planned_qty - reserved_qty) |
| 103 | where item_code=%s and warehouse=%s""", (valuation_rate, qty_after_transaction, |
| 104 | stock_value, args["item_code"], args["warehouse"])) |
| 105 | |
Anand Doshi | 4dc7caa | 2013-01-11 11:44:49 +0530 | [diff] [blame] | 106 | def get_sle_before_datetime(args, for_update=False): |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 107 | """ |
| 108 | get previous stock ledger entry before current time-bucket |
| 109 | |
| 110 | Details: |
| 111 | get the last sle before the current time-bucket, so that all values |
| 112 | are reposted from the current time-bucket onwards. |
| 113 | this is necessary because at the time of cancellation, there may be |
| 114 | entries between the cancelled entries in the same time-bucket |
| 115 | """ |
| 116 | sle = get_stock_ledger_entries(args, |
Nabin Hait | 26d4655 | 2013-01-09 15:23:05 +0530 | [diff] [blame] | 117 | ["timestamp(posting_date, posting_time) < timestamp(%(posting_date)s, %(posting_time)s)"], |
Anand Doshi | 4dc7caa | 2013-01-11 11:44:49 +0530 | [diff] [blame] | 118 | "desc", "limit 1", for_update=for_update) |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 119 | |
| 120 | return sle and sle[0] or webnotes._dict() |
| 121 | |
Anand Doshi | 4dc7caa | 2013-01-11 11:44:49 +0530 | [diff] [blame] | 122 | def get_sle_after_datetime(args, for_update=False): |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 123 | """get Stock Ledger Entries after a particular datetime, for reposting""" |
Anand Doshi | 4dc7caa | 2013-01-11 11:44:49 +0530 | [diff] [blame] | 124 | # NOTE: using for update of |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 125 | return get_stock_ledger_entries(args, |
Nabin Hait | 9514d17 | 2013-01-10 10:40:37 +0530 | [diff] [blame] | 126 | ["timestamp(posting_date, posting_time) > timestamp(%(posting_date)s, %(posting_time)s)"], |
Anand Doshi | 4dc7caa | 2013-01-11 11:44:49 +0530 | [diff] [blame] | 127 | "asc", for_update=for_update) |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 128 | |
Anand Doshi | 4dc7caa | 2013-01-11 11:44:49 +0530 | [diff] [blame] | 129 | def get_stock_ledger_entries(args, conditions=None, order="desc", limit=None, for_update=False): |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 130 | """get stock ledger entries filtered by specific posting datetime conditions""" |
| 131 | if not args.get("posting_date"): |
| 132 | args["posting_date"] = "1900-01-01" |
| 133 | if not args.get("posting_time"): |
Anand Doshi | 71bed31 | 2013-03-13 12:57:04 +0530 | [diff] [blame] | 134 | args["posting_time"] = "00:00" |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 135 | |
| 136 | return webnotes.conn.sql("""select * from `tabStock Ledger Entry` |
| 137 | where item_code = %%(item_code)s |
| 138 | and warehouse = %%(warehouse)s |
| 139 | and ifnull(is_cancelled, 'No') = 'No' |
| 140 | %(conditions)s |
Nabin Hait | 9514d17 | 2013-01-10 10:40:37 +0530 | [diff] [blame] | 141 | order by timestamp(posting_date, posting_time) %(order)s, name %(order)s |
Anand Doshi | 4dc7caa | 2013-01-11 11:44:49 +0530 | [diff] [blame] | 142 | %(limit)s %(for_update)s""" % { |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 143 | "conditions": conditions and ("and " + " and ".join(conditions)) or "", |
Nabin Hait | 9514d17 | 2013-01-10 10:40:37 +0530 | [diff] [blame] | 144 | "limit": limit or "", |
Anand Doshi | 4dc7caa | 2013-01-11 11:44:49 +0530 | [diff] [blame] | 145 | "for_update": for_update and "for update" or "", |
Nabin Hait | 9514d17 | 2013-01-10 10:40:37 +0530 | [diff] [blame] | 146 | "order": order |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 147 | }, args, as_dict=1) |
| 148 | |
| 149 | def validate_negative_stock(qty_after_transaction, sle): |
| 150 | """ |
| 151 | validate negative stock for entries current datetime onwards |
| 152 | will not consider cancelled entries |
| 153 | """ |
| 154 | diff = qty_after_transaction + flt(sle.actual_qty) |
| 155 | |
| 156 | if diff < 0 and abs(diff) > 0.0001: |
| 157 | # negative stock! |
| 158 | global _exceptions |
| 159 | exc = sle.copy().update({"diff": diff}) |
| 160 | _exceptions.append(exc) |
| 161 | return False |
| 162 | else: |
| 163 | return True |
| 164 | |
| 165 | def get_serialized_values(qty_after_transaction, sle, valuation_rate): |
| 166 | incoming_rate = flt(sle.incoming_rate) |
| 167 | actual_qty = flt(sle.actual_qty) |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 168 | serial_no = cstr(sle.serial_no).split("\n") |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 169 | |
| 170 | if incoming_rate < 0: |
| 171 | # wrong incoming rate |
| 172 | incoming_rate = valuation_rate |
| 173 | elif incoming_rate == 0 or flt(sle.actual_qty) < 0: |
| 174 | # In case of delivery/stock issue, get average purchase rate |
| 175 | # of serial nos of current entry |
| 176 | incoming_rate = flt(webnotes.conn.sql("""select avg(ifnull(purchase_rate, 0)) |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 177 | from `tabSerial No` where name in (%s)""" % (", ".join(["%s"]*len(serial_no))), |
| 178 | tuple(serial_no))[0][0]) |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 179 | |
| 180 | if incoming_rate and not valuation_rate: |
| 181 | valuation_rate = incoming_rate |
| 182 | else: |
| 183 | new_stock_qty = qty_after_transaction + actual_qty |
| 184 | if new_stock_qty > 0: |
| 185 | new_stock_value = qty_after_transaction * valuation_rate + actual_qty * incoming_rate |
| 186 | if new_stock_value > 0: |
| 187 | # calculate new valuation rate only if stock value is positive |
| 188 | # else it remains the same as that of previous entry |
| 189 | valuation_rate = new_stock_value / new_stock_qty |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 190 | |
Nabin Hait | 914c6df | 2013-01-14 13:15:42 +0530 | [diff] [blame] | 191 | return valuation_rate |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 192 | |
| 193 | def get_moving_average_values(qty_after_transaction, sle, valuation_rate): |
| 194 | incoming_rate = flt(sle.incoming_rate) |
Nabin Hait | 914c6df | 2013-01-14 13:15:42 +0530 | [diff] [blame] | 195 | actual_qty = flt(sle.actual_qty) |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 196 | |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 197 | if not incoming_rate: |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 198 | # In case of delivery/stock issue in_rate = 0 or wrong incoming rate |
| 199 | incoming_rate = valuation_rate |
| 200 | |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 201 | elif qty_after_transaction < 0: |
| 202 | # if negative stock, take current valuation rate as incoming rate |
| 203 | valuation_rate = incoming_rate |
| 204 | |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 205 | new_stock_qty = qty_after_transaction + actual_qty |
| 206 | new_stock_value = qty_after_transaction * valuation_rate + actual_qty * incoming_rate |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 207 | |
| 208 | if new_stock_qty > 0 and new_stock_value > 0: |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 209 | valuation_rate = new_stock_value / flt(new_stock_qty) |
| 210 | elif new_stock_qty <= 0: |
| 211 | valuation_rate = 0.0 |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 212 | |
| 213 | # NOTE: val_rate is same as previous entry if new stock value is negative |
| 214 | |
Nabin Hait | 914c6df | 2013-01-14 13:15:42 +0530 | [diff] [blame] | 215 | return valuation_rate |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 216 | |
| 217 | def get_fifo_values(qty_after_transaction, sle, stock_queue): |
| 218 | incoming_rate = flt(sle.incoming_rate) |
| 219 | actual_qty = flt(sle.actual_qty) |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 220 | if not stock_queue: |
| 221 | stock_queue.append([0, 0]) |
Nabin Hait | 9514d17 | 2013-01-10 10:40:37 +0530 | [diff] [blame] | 222 | |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 223 | if actual_qty > 0: |
| 224 | if stock_queue[-1][0] > 0: |
| 225 | stock_queue.append([actual_qty, incoming_rate]) |
| 226 | else: |
| 227 | qty = stock_queue[-1][0] + actual_qty |
| 228 | stock_queue[-1] = [qty, qty > 0 and incoming_rate or 0] |
| 229 | else: |
| 230 | incoming_cost = 0 |
| 231 | qty_to_pop = abs(actual_qty) |
| 232 | while qty_to_pop: |
Nabin Hait | 9514d17 | 2013-01-10 10:40:37 +0530 | [diff] [blame] | 233 | if not stock_queue: |
| 234 | stock_queue.append([0, 0]) |
| 235 | |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 236 | batch = stock_queue[0] |
| 237 | |
| 238 | if 0 < batch[0] <= qty_to_pop: |
| 239 | # if batch qty > 0 |
| 240 | # not enough or exactly same qty in current batch, clear batch |
| 241 | incoming_cost += flt(batch[0]) * flt(batch[1]) |
| 242 | qty_to_pop -= batch[0] |
| 243 | stock_queue.pop(0) |
| 244 | else: |
| 245 | # all from current batch |
| 246 | incoming_cost += flt(qty_to_pop) * flt(batch[1]) |
| 247 | batch[0] -= qty_to_pop |
| 248 | qty_to_pop = 0 |
| 249 | |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 250 | stock_value = sum((flt(batch[0]) * flt(batch[1]) for batch in stock_queue)) |
| 251 | stock_qty = sum((flt(batch[0]) for batch in stock_queue)) |
| 252 | |
| 253 | valuation_rate = stock_qty and (stock_value / flt(stock_qty)) or 0 |
Nabin Hait | 9514d17 | 2013-01-10 10:40:37 +0530 | [diff] [blame] | 254 | |
Nabin Hait | 914c6df | 2013-01-14 13:15:42 +0530 | [diff] [blame] | 255 | return valuation_rate |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 256 | |
Nabin Hait | 9514d17 | 2013-01-10 10:40:37 +0530 | [diff] [blame] | 257 | def _raise_exceptions(args, verbose=1): |
Nabin Hait | 902e860 | 2013-01-08 18:29:24 +0530 | [diff] [blame] | 258 | deficiency = min(e["diff"] for e in _exceptions) |
| 259 | msg = """Negative stock error: |
| 260 | Cannot complete this transaction because stock will start |
| 261 | becoming negative (%s) for Item <b>%s</b> in Warehouse |
| 262 | <b>%s</b> on <b>%s %s</b> in Transaction %s %s. |
| 263 | Total Quantity Deficiency: <b>%s</b>""" % \ |
| 264 | (_exceptions[0]["diff"], args.get("item_code"), args.get("warehouse"), |
| 265 | _exceptions[0]["posting_date"], _exceptions[0]["posting_time"], |
| 266 | _exceptions[0]["voucher_type"], _exceptions[0]["voucher_no"], |
| 267 | abs(deficiency)) |
| 268 | if verbose: |
| 269 | msgprint(msg, raise_exception=1) |
| 270 | else: |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 271 | raise webnotes.ValidationError, msg |
| 272 | |
Anand Doshi | 4dc7caa | 2013-01-11 11:44:49 +0530 | [diff] [blame] | 273 | def get_previous_sle(args, for_update=False): |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 274 | """ |
| 275 | get the last sle on or before the current time-bucket, |
| 276 | to get actual qty before transaction, this function |
| 277 | is called from various transaction like stock entry, reco etc |
| 278 | |
| 279 | args = { |
| 280 | "item_code": "ABC", |
| 281 | "warehouse": "XYZ", |
| 282 | "posting_date": "2012-12-12", |
| 283 | "posting_time": "12:00", |
| 284 | "sle": "name of reference Stock Ledger Entry" |
| 285 | } |
| 286 | """ |
| 287 | if not args.get("sle"): args["sle"] = "" |
| 288 | |
| 289 | sle = get_stock_ledger_entries(args, ["name != %(sle)s", |
| 290 | "timestamp(posting_date, posting_time) <= timestamp(%(posting_date)s, %(posting_time)s)"], |
Anand Doshi | 4dc7caa | 2013-01-11 11:44:49 +0530 | [diff] [blame] | 291 | "desc", "limit 1", for_update=for_update) |
Anand Doshi | 1b53186 | 2013-01-10 19:29:51 +0530 | [diff] [blame] | 292 | return sle and sle[0] or {} |